blob: 37f01f54d0caa83018e31ed7258e400b2413784a [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson70118f62001-01-16 20:17:53 +00002 * Copyright (C) 2000,2001 Josh Coalson
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003 *
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"
Josh Coalson215af572001-03-27 01:15:58 +000026#include "private/crc.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000027#include "private/encoder_framing.h"
28#include "private/fixed.h"
29#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000030#include "private/md5.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000031
32#ifdef min
33#undef min
34#endif
35#define min(x,y) ((x)<(y)?(x):(y))
36
37#ifdef max
38#undef max
39#endif
40#define max(x,y) ((x)>(y)?(x):(y))
41
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000042typedef struct FLAC__EncoderPrivate {
43 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
44 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
45 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
46 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
47 real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
Josh Coalson94e02cd2001-01-25 10:41:06 +000048 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
49 int32 *residual_workspace_mid_side[2][2];
50 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
51 FLAC__Subframe subframe_workspace_mid_side[2][2];
52 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
53 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
54 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
55 unsigned best_subframe_mid_side[2];
56 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
57 unsigned best_subframe_bits_mid_side[2];
Josh Coalsone77287e2001-01-20 01:27:55 +000058 uint32 *abs_residual; /* workspace where the abs(candidate residual) is stored */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000059 FLAC__BitBuffer frame; /* the current frame being worked on */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000060 bool current_frame_can_do_mid_side; /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */
Josh Coalsonb5e60e52001-01-28 09:27:27 +000061 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
62 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
63 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
64 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000065 FLAC__StreamMetaData metadata;
66 unsigned current_sample_number;
67 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000068 struct MD5Context md5context;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000069 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
70 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
71 void *client_data;
72} FLAC__EncoderPrivate;
73
74static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
75static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000076static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalsonb5e60e52001-01-28 09:27:27 +000077static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
78static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
Josh Coalson6dcea512001-01-23 23:07:36 +000079static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe);
80static 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);
81static 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);
Josh Coalson94e02cd2001-01-25 10:41:06 +000082static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe);
Josh Coalsone77287e2001-01-20 01:27:55 +000083static 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[]);
Josh Coalson3b5f4712001-03-21 22:53:43 +000084static 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);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000085
Josh Coalsoncbf595f2000-12-22 22:35:33 +000086const char *FLAC__EncoderWriteStatusString[] = {
87 "FLAC__ENCODER_WRITE_OK",
88 "FLAC__ENCODER_WRITE_FATAL_ERROR"
89};
90
91const char *FLAC__EncoderStateString[] = {
92 "FLAC__ENCODER_OK",
93 "FLAC__ENCODER_UNINITIALIZED",
94 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
95 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
96 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
97 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
98 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
99 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
100 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000101 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000102 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
103 "FLAC__ENCODER_NOT_STREAMABLE",
104 "FLAC__ENCODER_FRAMING_ERROR",
105 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
106 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
107 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
108};
109
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000110
111bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
112{
113 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000114 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000115 int32 *previous_is, *current_is;
116 real *previous_rs, *current_rs;
117 int32 *residual;
Josh Coalsone77287e2001-01-20 01:27:55 +0000118 uint32 *abs_residual;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000119
120 assert(new_size > 0);
121 assert(encoder->state == FLAC__ENCODER_OK);
122 assert(encoder->guts->current_sample_number == 0);
123
124 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
125 if(new_size <= encoder->guts->input_capacity)
126 return true;
127
128 ok = 1;
129 if(ok) {
130 for(i = 0; ok && i < encoder->channels; i++) {
131 /* integer version of the signal */
132 previous_is = encoder->guts->integer_signal[i];
133 current_is = (int32*)malloc(sizeof(int32) * new_size);
134 if(0 == current_is) {
135 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
136 ok = 0;
137 }
138 else {
139 encoder->guts->integer_signal[i] = current_is;
140 if(previous_is != 0)
141 free(previous_is);
142 }
143 /* real version of the signal */
144 previous_rs = encoder->guts->real_signal[i];
145 current_rs = (real*)malloc(sizeof(real) * new_size);
146 if(0 == current_rs) {
147 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
148 ok = 0;
149 }
150 else {
151 encoder->guts->real_signal[i] = current_rs;
152 if(previous_rs != 0)
153 free(previous_rs);
154 }
155 }
156 }
157 if(ok) {
158 for(i = 0; ok && i < 2; i++) {
159 /* integer version of the signal */
160 previous_is = encoder->guts->integer_signal_mid_side[i];
161 current_is = (int32*)malloc(sizeof(int32) * new_size);
162 if(0 == current_is) {
163 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
164 ok = 0;
165 }
166 else {
167 encoder->guts->integer_signal_mid_side[i] = current_is;
168 if(previous_is != 0)
169 free(previous_is);
170 }
171 /* real version of the signal */
172 previous_rs = encoder->guts->real_signal_mid_side[i];
173 current_rs = (real*)malloc(sizeof(real) * new_size);
174 if(0 == current_rs) {
175 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
176 ok = 0;
177 }
178 else {
179 encoder->guts->real_signal_mid_side[i] = current_rs;
180 if(previous_rs != 0)
181 free(previous_rs);
182 }
183 }
184 }
185 if(ok) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000186 for(channel = 0; channel < encoder->channels; channel++) {
187 for(i = 0; i < 2; i++) {
188 residual = (int32*)malloc(sizeof(int32) * new_size);
189 if(0 == residual) {
190 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
191 ok = 0;
192 }
193 else {
194 if(encoder->guts->residual_workspace[channel][i] != 0)
195 free(encoder->guts->residual_workspace[channel][i]);
196 encoder->guts->residual_workspace[channel][i] = residual;
197 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000198 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000199 }
200 for(channel = 0; channel < 2; channel++) {
201 for(i = 0; i < 2; i++) {
202 residual = (int32*)malloc(sizeof(int32) * new_size);
203 if(0 == residual) {
204 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
205 ok = 0;
206 }
207 else {
208 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0)
209 free(encoder->guts->residual_workspace_mid_side[channel][i]);
210 encoder->guts->residual_workspace_mid_side[channel][i] = residual;
211 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000212 }
213 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000214 abs_residual = (uint32*)malloc(sizeof(uint32) * new_size);
215 if(0 == residual) {
216 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
217 ok = 0;
218 }
219 else {
220 if(encoder->guts->abs_residual != 0)
221 free(encoder->guts->abs_residual);
222 encoder->guts->abs_residual = abs_residual;
223 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000224 }
225 if(ok)
226 encoder->guts->input_capacity = new_size;
227
228 return ok;
229}
230
231FLAC__Encoder *FLAC__encoder_get_new_instance()
232{
233 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
234 if(encoder != 0) {
235 encoder->state = FLAC__ENCODER_UNINITIALIZED;
236 encoder->guts = 0;
237 }
238 return encoder;
239}
240
241void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
242{
243 assert(encoder != 0);
244 free(encoder);
245}
246
247FLAC__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)
248{
249 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000250 FLAC__StreamMetaData padding;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000251
252 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
253 assert(encoder != 0);
254 assert(write_callback != 0);
255 assert(metadata_callback != 0);
256 assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
257 assert(encoder->guts == 0);
258
259 encoder->state = FLAC__ENCODER_OK;
260
261 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
262 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
263
264 if(encoder->do_mid_side_stereo && encoder->channels != 2)
265 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
266
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000267 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000268 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
269
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000270 if(encoder->bits_per_sample == 0 || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
271 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
272
273 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
274 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
275
276 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
277 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
278
279 if(encoder->blocksize < encoder->max_lpc_order)
280 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
281
282 if(encoder->qlp_coeff_precision == 0) {
283 if(encoder->bits_per_sample < 16) {
284 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
285 /* @@@ until then we'll make a guess */
286 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
287 }
288 else if(encoder->bits_per_sample == 16) {
289 if(encoder->blocksize <= 192)
290 encoder->qlp_coeff_precision = 7;
291 else if(encoder->blocksize <= 384)
292 encoder->qlp_coeff_precision = 8;
293 else if(encoder->blocksize <= 576)
294 encoder->qlp_coeff_precision = 9;
295 else if(encoder->blocksize <= 1152)
296 encoder->qlp_coeff_precision = 10;
297 else if(encoder->blocksize <= 2304)
298 encoder->qlp_coeff_precision = 11;
299 else if(encoder->blocksize <= 4608)
300 encoder->qlp_coeff_precision = 12;
301 else
302 encoder->qlp_coeff_precision = 13;
303 }
304 else {
305 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
306 }
307 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000308 else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32) || encoder->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000309 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
310
311 if(encoder->streamable_subset) {
Josh Coalson215af572001-03-27 01:15:58 +0000312//@@@ add check for blocksize here
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000313 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 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000335 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 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000351 encoder->guts->abs_residual = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000352 encoder->guts->current_frame_can_do_mid_side = true;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000353 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
354 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
355 if(encoder->guts->loose_mid_side_stereo_frames == 0)
356 encoder->guts->loose_mid_side_stereo_frames = 1;
357 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000358 encoder->guts->current_sample_number = 0;
359 encoder->guts->current_frame_number = 0;
360
361 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
362 /* the above function sets the state for us in case of an error */
363 return encoder->state;
364 }
365 FLAC__bitbuffer_init(&encoder->guts->frame);
366 encoder->guts->write_callback = write_callback;
367 encoder->guts->metadata_callback = metadata_callback;
368 encoder->guts->client_data = client_data;
369
370 /*
371 * write the stream header
372 */
373 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
374 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
375
376 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
377 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
378
Josh Coalsonc692d382001-02-23 21:05:05 +0000379 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
380 encoder->guts->metadata.is_last = (encoder->padding == 0);
381 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
382 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
383 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
384 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
385 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
386 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
387 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
388 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
389 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
390 memset(encoder->guts->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000391 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000392 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
393 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
394
Josh Coalsonc692d382001-02-23 21:05:05 +0000395 /* add a PADDING block if requested */
396 if(encoder->padding > 0) {
397 padding.type = FLAC__METADATA_TYPE_PADDING;
398 padding.is_last = true;
399 padding.length = encoder->padding;
400 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
401 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
402 }
403
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000404 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
405 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
406 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)
407 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
408
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000409 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000410 encoder->guts->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000411 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000412 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000413
414 return encoder->state;
415}
416
417void FLAC__encoder_finish(FLAC__Encoder *encoder)
418{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000419 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000420
421 assert(encoder != 0);
422 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
423 return;
424 if(encoder->guts->current_sample_number != 0) {
425 encoder->blocksize = encoder->guts->current_sample_number;
426 encoder_process_frame_(encoder, true); /* true => is last frame */
427 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000428 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000429 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
430 if(encoder->guts != 0) {
431 for(i = 0; i < encoder->channels; i++) {
432 if(encoder->guts->integer_signal[i] != 0) {
433 free(encoder->guts->integer_signal[i]);
434 encoder->guts->integer_signal[i] = 0;
435 }
436 if(encoder->guts->real_signal[i] != 0) {
437 free(encoder->guts->real_signal[i]);
438 encoder->guts->real_signal[i] = 0;
439 }
440 }
441 for(i = 0; i < 2; i++) {
442 if(encoder->guts->integer_signal_mid_side[i] != 0) {
443 free(encoder->guts->integer_signal_mid_side[i]);
444 encoder->guts->integer_signal_mid_side[i] = 0;
445 }
446 if(encoder->guts->real_signal_mid_side[i] != 0) {
447 free(encoder->guts->real_signal_mid_side[i]);
448 encoder->guts->real_signal_mid_side[i] = 0;
449 }
450 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000451 for(channel = 0; channel < encoder->channels; channel++) {
452 for(i = 0; i < 2; i++) {
453 if(encoder->guts->residual_workspace[channel][i] != 0) {
454 free(encoder->guts->residual_workspace[channel][i]);
455 encoder->guts->residual_workspace[channel][i] = 0;
456 }
457 }
458 }
459 for(channel = 0; channel < 2; channel++) {
460 for(i = 0; i < 2; i++) {
461 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0) {
462 free(encoder->guts->residual_workspace_mid_side[channel][i]);
463 encoder->guts->residual_workspace_mid_side[channel][i] = 0;
464 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000465 }
466 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000467 if(encoder->guts->abs_residual != 0) {
468 free(encoder->guts->abs_residual);
469 encoder->guts->abs_residual = 0;
470 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000471 FLAC__bitbuffer_free(&encoder->guts->frame);
472 free(encoder->guts);
473 encoder->guts = 0;
474 }
475 encoder->state = FLAC__ENCODER_UNINITIALIZED;
476}
477
478bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
479{
480 unsigned i, j, channel;
481 int32 x, mid, side;
482 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
483 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
484 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
485
486 assert(encoder != 0);
487 assert(encoder->state == FLAC__ENCODER_OK);
488
489 j = 0;
490 do {
491 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
492 for(channel = 0; channel < encoder->channels; channel++) {
493 x = buf[channel][j];
494 encoder->guts->integer_signal[channel][i] = x;
495 encoder->guts->real_signal[channel][i] = (real)x;
496 }
497 if(ms && encoder->guts->current_frame_can_do_mid_side) {
498 side = buf[0][j] - buf[1][j];
499 if(side < min_side || side > max_side) {
500 encoder->guts->current_frame_can_do_mid_side = false;
501 }
502 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000503 mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000504 encoder->guts->integer_signal_mid_side[0][i] = mid;
505 encoder->guts->integer_signal_mid_side[1][i] = side;
506 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
507 encoder->guts->real_signal_mid_side[1][i] = (real)side;
508 }
509 }
510 encoder->guts->current_sample_number++;
511 }
512 if(i == encoder->blocksize) {
513 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
514 return false;
515 }
516 } while(j < samples);
517
518 return true;
519}
520
521/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
522bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
523{
524 unsigned i, j, k, channel;
525 int32 x, left = 0, mid, side;
526 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
527 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
528 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
529
530 assert(encoder != 0);
531 assert(encoder->state == FLAC__ENCODER_OK);
532
533 j = k = 0;
534 do {
535 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
536 for(channel = 0; channel < encoder->channels; channel++, k++) {
537 x = buf[k];
538 encoder->guts->integer_signal[channel][i] = x;
539 encoder->guts->real_signal[channel][i] = (real)x;
540 if(ms && encoder->guts->current_frame_can_do_mid_side) {
541 if(channel == 0) {
542 left = x;
543 }
544 else {
545 side = left - x;
546 if(side < min_side || side > max_side) {
547 encoder->guts->current_frame_can_do_mid_side = false;
548 }
549 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000550 mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000551 encoder->guts->integer_signal_mid_side[0][i] = mid;
552 encoder->guts->integer_signal_mid_side[1][i] = side;
553 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
554 encoder->guts->real_signal_mid_side[1][i] = (real)side;
555 }
556 }
557 }
558 }
559 encoder->guts->current_sample_number++;
560 }
561 if(i == encoder->blocksize) {
562 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
563 return false;
564 }
565 } while(j < samples);
566
567 return true;
568}
569
570bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
571{
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000572 assert(encoder->state == FLAC__ENCODER_OK);
573
574 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000575 * Accumulate raw signal to the MD5 signature
576 */
577 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
578 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
579 return false;
580 }
581
582 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000583 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000584 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000585 if(!encoder_process_subframes_(encoder, is_last_frame)) {
586 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000587 return false;
588 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000589
590 /*
591 * Zero-pad the frame to a byte_boundary
592 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000593 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000594 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
595 return false;
596 }
597
598 /*
Josh Coalson215af572001-03-27 01:15:58 +0000599 * CRC-16 the whole thing
600 */
601 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
602 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
603 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
604
605 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000606 * Write it
607 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000608 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) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000609 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++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000619 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
620 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
621 encoder->guts->metadata.data.stream_info.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.max_framesize);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000622
623 return true;
624}
625
Josh Coalson94e02cd2001-01-25 10:41:06 +0000626bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
627{
628 FLAC__FrameHeader frame_header;
629 unsigned channel, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000630 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000631
632 /*
633 * Calculate the max Rice partition order
634 */
635 if(is_last_frame) {
636 max_partition_order = 0;
637 }
638 else {
639 unsigned limit = 0, b = encoder->blocksize;
640 while(!(b & 1)) {
641 limit++;
642 b >>= 1;
643 }
644 max_partition_order = min(encoder->rice_optimization_level, limit);
645 }
646
647 /*
648 * Setup the frame
649 */
650 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
651 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
652 return false;
653 }
654 frame_header.blocksize = encoder->blocksize;
655 frame_header.sample_rate = encoder->sample_rate;
656 frame_header.channels = encoder->channels;
657 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
658 frame_header.bits_per_sample = encoder->bits_per_sample;
659 frame_header.number.frame_number = encoder->guts->current_frame_number;
660
661 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000662 * Figure out what channel assignments to try
663 */
664 if(encoder->do_mid_side_stereo) {
665 if(encoder->loose_mid_side_stereo) {
666 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
667 do_independent = true;
668 do_mid_side = true;
669 }
670 else {
671 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
672 do_mid_side = !do_independent;
673 }
674 }
675 else {
676 do_independent = true;
677 do_mid_side = true;
678 }
679 }
680 else {
681 do_independent = true;
682 do_mid_side = false;
683 }
684 if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
685 do_independent = true;
686 do_mid_side = false;
687 }
688
689 assert(do_independent || do_mid_side);
690
691 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000692 * First do a normal encoding pass of each independent channel
693 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000694 if(do_independent) {
695 for(channel = 0; channel < encoder->channels; channel++) {
696 if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->bits_per_sample, 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))
697 return false;
698 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000699 }
700
701 /*
702 * Now do mid and side channels if requested
703 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000704 if(do_mid_side) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000705 assert(encoder->channels == 2);
706
707 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000708 if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->bits_per_sample+(channel==0? 0:1), 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))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000709 return false;
710 }
711 }
712
713 /*
714 * Compose the frame bitbuffer
715 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000716 if(do_mid_side) {
717 FLAC__ChannelAssignment channel_assignment;
718
Josh Coalson94e02cd2001-01-25 10:41:06 +0000719 assert(encoder->channels == 2);
720
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000721 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
722 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
723 }
724 else {
725 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
726 unsigned min_bits;
727 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000728
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000729 assert(do_independent && do_mid_side);
730
731 /* We have to figure out which channel assignent results in the smallest frame */
732 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
733 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
734 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
735 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
736
737 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
738 if(bits[ca] < min_bits) {
739 min_bits = bits[ca];
740 channel_assignment = ca;
741 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000742 }
743 }
744
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000745 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000746
747 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
748 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
749 return false;
750 }
751
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000752 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000753 /* note that encoder_add_subframe_ sets the state for us in case of an error */
754 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000755 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample , &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]], &encoder->guts->frame))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000756 return false;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000757 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample , &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]], &encoder->guts->frame))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000758 return false;
759 break;
760 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000761 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample , &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]], &encoder->guts->frame))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000762 return false;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000763 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000764 return false;
765 break;
766 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000767 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000768 return false;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000769 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample , &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]], &encoder->guts->frame))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000770 return false;
771 break;
772 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000773 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample , &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]], &encoder->guts->frame))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000774 return false;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000775 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000776 return false;
777 break;
778 default:
779 assert(0);
780 }
781 }
782 else {
783 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
784 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
785 return false;
786 }
787
788 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000789 if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample, &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000790 /* the above function sets the state for us in case of an error */
791 return false;
792 }
793 }
794 }
795
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000796 if(encoder->loose_mid_side_stereo) {
797 encoder->guts->loose_mid_side_stereo_frame_count++;
798 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
799 encoder->guts->loose_mid_side_stereo_frame_count = 0;
800 }
801
802 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
803
Josh Coalson94e02cd2001-01-25 10:41:06 +0000804 return true;
805}
806
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000807bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000808{
809 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
810 real lpc_residual_bits_per_sample;
811 real autoc[FLAC__MAX_LPC_ORDER+1];
812 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
813 real lpc_error[FLAC__MAX_LPC_ORDER];
814 unsigned min_lpc_order, max_lpc_order, lpc_order;
815 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
816 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
817 unsigned rice_parameter;
818 unsigned _candidate_bits, _best_bits;
819 unsigned _best_subframe;
820
821 /* verbatim subframe is the baseline against which we measure other compressed subframes */
822 _best_subframe = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000823 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, bits_per_sample, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000824
825 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
826 /* check for constant subframe */
827 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);
828 if(fixed_residual_bits_per_sample[1] == 0.0) {
829 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
830 unsigned i, signal_is_constant = true;
831 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
832 if(integer_signal[0] != integer_signal[i]) {
833 signal_is_constant = false;
834 break;
835 }
836 }
837 if(signal_is_constant) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000838 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], bits_per_sample, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000839 if(_candidate_bits < _best_bits) {
840 _best_subframe = !_best_subframe;
841 _best_bits = _candidate_bits;
842 }
843 }
844 }
845 else {
846 /* encode fixed */
847 if(encoder->do_exhaustive_model_search) {
848 min_fixed_order = 0;
849 max_fixed_order = FLAC__MAX_FIXED_ORDER;
850 }
851 else {
852 min_fixed_order = max_fixed_order = guess_fixed_order;
853 }
854 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000855 if(fixed_residual_bits_per_sample[fixed_order] >= (real)bits_per_sample)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000856 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000857 rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */
Josh Coalson352e0f62001-03-20 22:55:50 +0000858#ifndef SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000859 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000860#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +0000861 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
862 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000863 _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, frame_header->blocksize, bits_per_sample, fixed_order, rice_parameter, max_partition_order, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000864 if(_candidate_bits < _best_bits) {
865 _best_subframe = !_best_subframe;
866 _best_bits = _candidate_bits;
867 }
868 }
869
870 /* encode lpc */
871 if(encoder->max_lpc_order > 0) {
872 if(encoder->max_lpc_order >= frame_header->blocksize)
873 max_lpc_order = frame_header->blocksize-1;
874 else
875 max_lpc_order = encoder->max_lpc_order;
876 if(max_lpc_order > 0) {
877 FLAC__lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000878 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
879 if(autoc[0] != 0.0) {
880 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
881 if(encoder->do_exhaustive_model_search) {
882 min_lpc_order = 1;
883 }
884 else {
885 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, bits_per_sample);
886 min_lpc_order = max_lpc_order = guess_lpc_order;
887 }
888 if(encoder->do_qlp_coeff_prec_search) {
889 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
890 max_qlp_coeff_precision = min(32 - bits_per_sample - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
891 }
892 else {
893 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
894 }
895 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
896 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
897 if(lpc_residual_bits_per_sample >= (real)bits_per_sample)
898 continue; /* don't even try */
899 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
Josh Coalson352e0f62001-03-20 22:55:50 +0000900#ifndef SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000901 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000902#endif
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000903 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
904 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
905 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
906 _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, lp_coeff[lpc_order-1], frame_header->blocksize, bits_per_sample, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, subframe[!_best_subframe]);
907 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
908 if(_candidate_bits < _best_bits) {
909 _best_subframe = !_best_subframe;
910 _best_bits = _candidate_bits;
911 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000912 }
913 }
914 }
915 }
916 }
917 }
918 }
919 }
920
921 *best_subframe = _best_subframe;
922 *best_bits = _best_bits;
923
924 return true;
925}
926
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000927bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000928{
929 switch(subframe->type) {
930 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000931 if(!FLAC__subframe_add_constant(&(subframe->data.constant), bits_per_sample, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000932 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
933 return false;
934 }
935 break;
936 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000937 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, bits_per_sample, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000938 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
939 return false;
940 }
941 break;
942 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000943 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, bits_per_sample, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000944 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
945 return false;
946 }
947 break;
948 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000949 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, bits_per_sample, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000950 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
951 return false;
952 }
953 break;
954 default:
955 assert(0);
956 }
957
958 return true;
959}
960
961unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe)
962{
963 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
964 subframe->data.constant.value = signal;
965
Josh Coalson215af572001-03-27 01:15:58 +0000966 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + bits_per_sample;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000967}
968
969unsigned 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)
970{
971 unsigned i, residual_bits;
972 const unsigned residual_samples = blocksize - order;
973
974 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
975
976 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
977
978 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
979 subframe->data.fixed.residual = residual;
980
981 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);
982
983 subframe->data.fixed.order = order;
984 for(i = 0; i < order; i++)
985 subframe->data.fixed.warmup[i] = signal[i];
986
Josh Coalson215af572001-03-27 01:15:58 +0000987 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * bits_per_sample) + residual_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000988}
989
990unsigned 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)
991{
992 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
993 unsigned i, residual_bits;
994 int quantization, ret;
995 const unsigned residual_samples = blocksize - order;
996
997 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, bits_per_sample, qlp_coeff, &quantization);
998 if(ret != 0)
999 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1000
1001 FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1002
1003 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1004
1005 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1006 subframe->data.lpc.residual = residual;
1007
1008 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);
1009
1010 subframe->data.lpc.order = order;
1011 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1012 subframe->data.lpc.quantization_level = quantization;
1013 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1014 for(i = 0; i < order; i++)
1015 subframe->data.lpc.warmup[i] = signal[i];
1016
Josh Coalson215af572001-03-27 01:15:58 +00001017 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + bits_per_sample)) + residual_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001018}
1019
1020unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe)
1021{
1022 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1023
1024 subframe->data.verbatim.data = signal;
1025
Josh Coalson215af572001-03-27 01:15:58 +00001026 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * bits_per_sample);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001027}
1028
1029unsigned 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[])
1030{
1031 unsigned residual_bits, best_residual_bits = 0;
1032 unsigned i, partition_order;
1033 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
1034 int32 r;
1035
1036 /* compute the abs(residual) for use later */
1037 for(i = 0; i < residual_samples; i++) {
1038 r = residual[i];
1039 abs_residual[i] = (uint32)(r<0? -r : r);
1040 }
1041
1042 for(partition_order = 0; partition_order <= max_partition_order; partition_order++) {
Josh Coalson3b5f4712001-03-21 22:53:43 +00001043 if(!encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, partition_order, parameters[!best_parameters_index], &residual_bits)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001044 assert(best_residual_bits != 0);
1045 break;
1046 }
1047 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1048 best_residual_bits = residual_bits;
1049 *best_partition_order = partition_order;
1050 best_parameters_index = !best_parameters_index;
1051 }
1052 }
1053 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1054
1055 return best_residual_bits;
1056}
1057
Josh Coalson352e0f62001-03-20 22:55:50 +00001058#ifdef VARIABLE_RICE_BITS
1059#undef VARIABLE_RICE_BITS
1060#endif
1061#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1062
Josh Coalson3b5f4712001-03-21 22:53:43 +00001063bool 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)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001064{
1065 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1066
1067 if(partition_order == 0) {
1068 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001069
1070 {
1071#ifdef VARIABLE_RICE_BITS
1072#ifdef SYMMETRIC_RICE
1073 bits_ += (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001074#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001075 const unsigned rice_parameter_estimate = rice_parameter-1;
1076 bits_ += (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001077#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001078#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001079 parameters[0] = rice_parameter;
1080 bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1081 for(i = 0; i < residual_samples; i++) {
1082#ifdef VARIABLE_RICE_BITS
1083#ifdef SYMMETRIC_RICE
Josh Coalson3b5f4712001-03-21 22:53:43 +00001084 bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001085#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001086 bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001087#endif
1088#else
Josh Coalson3b5f4712001-03-21 22:53:43 +00001089 bits_ += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001090#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001091 }
1092 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001093 }
1094 else {
1095 unsigned i, j, k = 0, k_last = 0;
1096 unsigned mean, parameter, partition_samples;
1097 const unsigned max_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
1098 for(i = 0; i < (1u<<partition_order); i++) {
1099 partition_samples = (residual_samples+predictor_order) >> partition_order;
1100 if(i == 0) {
1101 if(partition_samples <= predictor_order)
1102 return false;
1103 else
1104 partition_samples -= predictor_order;
1105 }
1106 mean = partition_samples >> 1;
1107 for(j = 0; j < partition_samples; j++, k++)
1108 mean += abs_residual[k];
1109 mean /= partition_samples;
Josh Coalson352e0f62001-03-20 22:55:50 +00001110#ifdef SYMMETRIC_RICE
1111 /* calc parameter = floor(log2(mean)) */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001112 parameter = 0;
1113mean>>=1;
1114 while(mean) {
1115 parameter++;
1116 mean >>= 1;
1117 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001118#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001119 /* calc parameter = floor(log2(mean)) + 1 */
1120 parameter = 0;
1121 while(mean) {
1122 parameter++;
1123 mean >>= 1;
1124 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001125#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001126 if(parameter > max_parameter)
1127 parameter = max_parameter;
1128 parameters[i] = parameter;
1129 bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001130#ifdef VARIABLE_RICE_BITS
1131#ifdef SYMMETRIC_RICE
Josh Coalsonb9433f92001-03-17 01:07:00 +00001132 bits_ += (2+parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001133#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001134 bits_ += (1+parameter) * partition_samples;
1135 --parameter;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001136#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001137#endif
1138 for(j = k_last; j < k; j++)
Josh Coalson352e0f62001-03-20 22:55:50 +00001139#ifdef VARIABLE_RICE_BITS
1140#ifdef SYMMETRIC_RICE
1141 bits_ += VARIABLE_RICE_BITS(abs_residual[j], parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001142#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001143 bits_ += VARIABLE_RICE_BITS(abs_residual[j], parameter);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001144#endif
1145#else
Josh Coalson3b5f4712001-03-21 22:53:43 +00001146 bits_ += FLAC__bitbuffer_rice_bits(residual[j], parameter); /* NOTE we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001147#endif
1148 k_last = k;
1149 }
1150 }
1151
1152 *bits = bits_;
1153 return true;
1154}