blob: 533c31ff35c23ed0a92e9e84029018369ebe7587 [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>
Josh Coalson352e0f62001-03-20 22:55:50 +000021#include <math.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000022#include <stdio.h>
23#include <stdlib.h> /* for malloc() */
24#include <string.h> /* for memcpy() */
25#include "FLAC/encoder.h"
26#include "private/bitbuffer.h"
27#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 Coalson352e0f62001-03-20 22:55:50 +000042#ifndef M_LN2
43/* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
44#define M_LN2 0.69314718055994530942
45#endif
46
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000047typedef struct FLAC__EncoderPrivate {
48 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
49 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
50 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
51 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
52 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 +000053 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
54 int32 *residual_workspace_mid_side[2][2];
55 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
56 FLAC__Subframe subframe_workspace_mid_side[2][2];
57 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
58 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
59 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
60 unsigned best_subframe_mid_side[2];
61 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
62 unsigned best_subframe_bits_mid_side[2];
Josh Coalsone77287e2001-01-20 01:27:55 +000063 uint32 *abs_residual; /* workspace where the abs(candidate residual) is stored */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000064 FLAC__BitBuffer frame; /* the current frame being worked on */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000065 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 +000066 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
67 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
68 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
69 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000070 FLAC__StreamMetaData metadata;
71 unsigned current_sample_number;
72 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000073 struct MD5Context md5context;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000074 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
75 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
76 void *client_data;
77} FLAC__EncoderPrivate;
78
79static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
80static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000081static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalsonb5e60e52001-01-28 09:27:27 +000082static 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);
83static 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 +000084static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe);
85static 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);
86static 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 +000087static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe);
Josh Coalsone77287e2001-01-20 01:27:55 +000088static 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 Coalson352e0f62001-03-20 22:55:50 +000089static bool encoder_set_partitioned_rice_(const int32 residual[], 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 +000090
Josh Coalsoncbf595f2000-12-22 22:35:33 +000091const char *FLAC__EncoderWriteStatusString[] = {
92 "FLAC__ENCODER_WRITE_OK",
93 "FLAC__ENCODER_WRITE_FATAL_ERROR"
94};
95
96const char *FLAC__EncoderStateString[] = {
97 "FLAC__ENCODER_OK",
98 "FLAC__ENCODER_UNINITIALIZED",
99 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
100 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
101 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
102 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
103 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
104 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
105 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000106 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000107 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
108 "FLAC__ENCODER_NOT_STREAMABLE",
109 "FLAC__ENCODER_FRAMING_ERROR",
110 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
111 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
112 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
113};
114
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000115
116bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
117{
118 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000119 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000120 int32 *previous_is, *current_is;
121 real *previous_rs, *current_rs;
122 int32 *residual;
Josh Coalsone77287e2001-01-20 01:27:55 +0000123 uint32 *abs_residual;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000124
125 assert(new_size > 0);
126 assert(encoder->state == FLAC__ENCODER_OK);
127 assert(encoder->guts->current_sample_number == 0);
128
129 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
130 if(new_size <= encoder->guts->input_capacity)
131 return true;
132
133 ok = 1;
134 if(ok) {
135 for(i = 0; ok && i < encoder->channels; i++) {
136 /* integer version of the signal */
137 previous_is = encoder->guts->integer_signal[i];
138 current_is = (int32*)malloc(sizeof(int32) * new_size);
139 if(0 == current_is) {
140 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
141 ok = 0;
142 }
143 else {
144 encoder->guts->integer_signal[i] = current_is;
145 if(previous_is != 0)
146 free(previous_is);
147 }
148 /* real version of the signal */
149 previous_rs = encoder->guts->real_signal[i];
150 current_rs = (real*)malloc(sizeof(real) * new_size);
151 if(0 == current_rs) {
152 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
153 ok = 0;
154 }
155 else {
156 encoder->guts->real_signal[i] = current_rs;
157 if(previous_rs != 0)
158 free(previous_rs);
159 }
160 }
161 }
162 if(ok) {
163 for(i = 0; ok && i < 2; i++) {
164 /* integer version of the signal */
165 previous_is = encoder->guts->integer_signal_mid_side[i];
166 current_is = (int32*)malloc(sizeof(int32) * new_size);
167 if(0 == current_is) {
168 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
169 ok = 0;
170 }
171 else {
172 encoder->guts->integer_signal_mid_side[i] = current_is;
173 if(previous_is != 0)
174 free(previous_is);
175 }
176 /* real version of the signal */
177 previous_rs = encoder->guts->real_signal_mid_side[i];
178 current_rs = (real*)malloc(sizeof(real) * new_size);
179 if(0 == current_rs) {
180 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
181 ok = 0;
182 }
183 else {
184 encoder->guts->real_signal_mid_side[i] = current_rs;
185 if(previous_rs != 0)
186 free(previous_rs);
187 }
188 }
189 }
190 if(ok) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000191 for(channel = 0; channel < encoder->channels; channel++) {
192 for(i = 0; i < 2; i++) {
193 residual = (int32*)malloc(sizeof(int32) * new_size);
194 if(0 == residual) {
195 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
196 ok = 0;
197 }
198 else {
199 if(encoder->guts->residual_workspace[channel][i] != 0)
200 free(encoder->guts->residual_workspace[channel][i]);
201 encoder->guts->residual_workspace[channel][i] = residual;
202 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000203 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000204 }
205 for(channel = 0; channel < 2; channel++) {
206 for(i = 0; i < 2; i++) {
207 residual = (int32*)malloc(sizeof(int32) * new_size);
208 if(0 == residual) {
209 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
210 ok = 0;
211 }
212 else {
213 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0)
214 free(encoder->guts->residual_workspace_mid_side[channel][i]);
215 encoder->guts->residual_workspace_mid_side[channel][i] = residual;
216 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000217 }
218 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000219 abs_residual = (uint32*)malloc(sizeof(uint32) * new_size);
220 if(0 == residual) {
221 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
222 ok = 0;
223 }
224 else {
225 if(encoder->guts->abs_residual != 0)
226 free(encoder->guts->abs_residual);
227 encoder->guts->abs_residual = abs_residual;
228 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000229 }
230 if(ok)
231 encoder->guts->input_capacity = new_size;
232
233 return ok;
234}
235
236FLAC__Encoder *FLAC__encoder_get_new_instance()
237{
238 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
239 if(encoder != 0) {
240 encoder->state = FLAC__ENCODER_UNINITIALIZED;
241 encoder->guts = 0;
242 }
243 return encoder;
244}
245
246void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
247{
248 assert(encoder != 0);
249 free(encoder);
250}
251
252FLAC__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)
253{
254 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000255 FLAC__StreamMetaData padding;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000256
257 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
258 assert(encoder != 0);
259 assert(write_callback != 0);
260 assert(metadata_callback != 0);
261 assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
262 assert(encoder->guts == 0);
263
264 encoder->state = FLAC__ENCODER_OK;
265
266 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
267 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
268
269 if(encoder->do_mid_side_stereo && encoder->channels != 2)
270 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
271
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000272 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000273 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
274
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000275 if(encoder->bits_per_sample == 0 || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
276 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
277
278 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
279 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
280
281 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
282 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
283
284 if(encoder->blocksize < encoder->max_lpc_order)
285 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
286
287 if(encoder->qlp_coeff_precision == 0) {
288 if(encoder->bits_per_sample < 16) {
289 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
290 /* @@@ until then we'll make a guess */
291 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
292 }
293 else if(encoder->bits_per_sample == 16) {
294 if(encoder->blocksize <= 192)
295 encoder->qlp_coeff_precision = 7;
296 else if(encoder->blocksize <= 384)
297 encoder->qlp_coeff_precision = 8;
298 else if(encoder->blocksize <= 576)
299 encoder->qlp_coeff_precision = 9;
300 else if(encoder->blocksize <= 1152)
301 encoder->qlp_coeff_precision = 10;
302 else if(encoder->blocksize <= 2304)
303 encoder->qlp_coeff_precision = 11;
304 else if(encoder->blocksize <= 4608)
305 encoder->qlp_coeff_precision = 12;
306 else
307 encoder->qlp_coeff_precision = 13;
308 }
309 else {
310 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
311 }
312 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000313 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 +0000314 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
315
316 if(encoder->streamable_subset) {
317 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)
318 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
319 if(encoder->sample_rate > 655350)
320 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
321 }
322
323 if(encoder->rice_optimization_level >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
324 encoder->rice_optimization_level = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
325
326 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
327 if(encoder->guts == 0)
328 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
329
330 encoder->guts->input_capacity = 0;
331 for(i = 0; i < encoder->channels; i++) {
332 encoder->guts->integer_signal[i] = 0;
333 encoder->guts->real_signal[i] = 0;
334 }
335 for(i = 0; i < 2; i++) {
336 encoder->guts->integer_signal_mid_side[i] = 0;
337 encoder->guts->real_signal_mid_side[i] = 0;
338 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000339 for(i = 0; i < encoder->channels; i++) {
340 encoder->guts->residual_workspace[i][0] = encoder->guts->residual_workspace[i][1] = 0;
341 encoder->guts->best_subframe[i] = 0;
342 }
343 for(i = 0; i < 2; i++) {
344 encoder->guts->residual_workspace_mid_side[i][0] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
345 encoder->guts->best_subframe_mid_side[i] = 0;
346 }
347 for(i = 0; i < encoder->channels; i++) {
348 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
349 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
350 }
351 for(i = 0; i < 2; i++) {
352 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
353 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
354 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000355 encoder->guts->abs_residual = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000356 encoder->guts->current_frame_can_do_mid_side = true;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000357 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
358 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
359 if(encoder->guts->loose_mid_side_stereo_frames == 0)
360 encoder->guts->loose_mid_side_stereo_frames = 1;
361 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000362 encoder->guts->current_sample_number = 0;
363 encoder->guts->current_frame_number = 0;
364
365 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
366 /* the above function sets the state for us in case of an error */
367 return encoder->state;
368 }
369 FLAC__bitbuffer_init(&encoder->guts->frame);
370 encoder->guts->write_callback = write_callback;
371 encoder->guts->metadata_callback = metadata_callback;
372 encoder->guts->client_data = client_data;
373
374 /*
375 * write the stream header
376 */
377 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
378 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
379
380 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
381 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
382
Josh Coalsonc692d382001-02-23 21:05:05 +0000383 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
384 encoder->guts->metadata.is_last = (encoder->padding == 0);
385 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
386 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
387 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
388 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
389 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
390 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
391 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
392 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
393 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
394 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 +0000395 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000396 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
397 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
398
Josh Coalsonc692d382001-02-23 21:05:05 +0000399 /* add a PADDING block if requested */
400 if(encoder->padding > 0) {
401 padding.type = FLAC__METADATA_TYPE_PADDING;
402 padding.is_last = true;
403 padding.length = encoder->padding;
404 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
405 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
406 }
407
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000408 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
409 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
410 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)
411 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
412
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000413 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000414 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 +0000415 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000416 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000417
418 return encoder->state;
419}
420
421void FLAC__encoder_finish(FLAC__Encoder *encoder)
422{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000423 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000424
425 assert(encoder != 0);
426 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
427 return;
428 if(encoder->guts->current_sample_number != 0) {
429 encoder->blocksize = encoder->guts->current_sample_number;
430 encoder_process_frame_(encoder, true); /* true => is last frame */
431 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000432 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000433 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
434 if(encoder->guts != 0) {
435 for(i = 0; i < encoder->channels; i++) {
436 if(encoder->guts->integer_signal[i] != 0) {
437 free(encoder->guts->integer_signal[i]);
438 encoder->guts->integer_signal[i] = 0;
439 }
440 if(encoder->guts->real_signal[i] != 0) {
441 free(encoder->guts->real_signal[i]);
442 encoder->guts->real_signal[i] = 0;
443 }
444 }
445 for(i = 0; i < 2; i++) {
446 if(encoder->guts->integer_signal_mid_side[i] != 0) {
447 free(encoder->guts->integer_signal_mid_side[i]);
448 encoder->guts->integer_signal_mid_side[i] = 0;
449 }
450 if(encoder->guts->real_signal_mid_side[i] != 0) {
451 free(encoder->guts->real_signal_mid_side[i]);
452 encoder->guts->real_signal_mid_side[i] = 0;
453 }
454 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000455 for(channel = 0; channel < encoder->channels; channel++) {
456 for(i = 0; i < 2; i++) {
457 if(encoder->guts->residual_workspace[channel][i] != 0) {
458 free(encoder->guts->residual_workspace[channel][i]);
459 encoder->guts->residual_workspace[channel][i] = 0;
460 }
461 }
462 }
463 for(channel = 0; channel < 2; channel++) {
464 for(i = 0; i < 2; i++) {
465 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0) {
466 free(encoder->guts->residual_workspace_mid_side[channel][i]);
467 encoder->guts->residual_workspace_mid_side[channel][i] = 0;
468 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000469 }
470 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000471 if(encoder->guts->abs_residual != 0) {
472 free(encoder->guts->abs_residual);
473 encoder->guts->abs_residual = 0;
474 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000475 FLAC__bitbuffer_free(&encoder->guts->frame);
476 free(encoder->guts);
477 encoder->guts = 0;
478 }
479 encoder->state = FLAC__ENCODER_UNINITIALIZED;
480}
481
482bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
483{
484 unsigned i, j, channel;
485 int32 x, mid, side;
486 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
487 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
488 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
489
490 assert(encoder != 0);
491 assert(encoder->state == FLAC__ENCODER_OK);
492
493 j = 0;
494 do {
495 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
496 for(channel = 0; channel < encoder->channels; channel++) {
497 x = buf[channel][j];
498 encoder->guts->integer_signal[channel][i] = x;
499 encoder->guts->real_signal[channel][i] = (real)x;
500 }
501 if(ms && encoder->guts->current_frame_can_do_mid_side) {
502 side = buf[0][j] - buf[1][j];
503 if(side < min_side || side > max_side) {
504 encoder->guts->current_frame_can_do_mid_side = false;
505 }
506 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000507 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 +0000508 encoder->guts->integer_signal_mid_side[0][i] = mid;
509 encoder->guts->integer_signal_mid_side[1][i] = side;
510 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
511 encoder->guts->real_signal_mid_side[1][i] = (real)side;
512 }
513 }
514 encoder->guts->current_sample_number++;
515 }
516 if(i == encoder->blocksize) {
517 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
518 return false;
519 }
520 } while(j < samples);
521
522 return true;
523}
524
525/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
526bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
527{
528 unsigned i, j, k, channel;
529 int32 x, left = 0, mid, side;
530 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
531 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
532 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
533
534 assert(encoder != 0);
535 assert(encoder->state == FLAC__ENCODER_OK);
536
537 j = k = 0;
538 do {
539 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
540 for(channel = 0; channel < encoder->channels; channel++, k++) {
541 x = buf[k];
542 encoder->guts->integer_signal[channel][i] = x;
543 encoder->guts->real_signal[channel][i] = (real)x;
544 if(ms && encoder->guts->current_frame_can_do_mid_side) {
545 if(channel == 0) {
546 left = x;
547 }
548 else {
549 side = left - x;
550 if(side < min_side || side > max_side) {
551 encoder->guts->current_frame_can_do_mid_side = false;
552 }
553 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000554 mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000555 encoder->guts->integer_signal_mid_side[0][i] = mid;
556 encoder->guts->integer_signal_mid_side[1][i] = side;
557 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
558 encoder->guts->real_signal_mid_side[1][i] = (real)side;
559 }
560 }
561 }
562 }
563 encoder->guts->current_sample_number++;
564 }
565 if(i == encoder->blocksize) {
566 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
567 return false;
568 }
569 } while(j < samples);
570
571 return true;
572}
573
574bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
575{
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000576 assert(encoder->state == FLAC__ENCODER_OK);
577
578 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000579 * Accumulate raw signal to the MD5 signature
580 */
581 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
582 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
583 return false;
584 }
585
586 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000587 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000588 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000589 if(!encoder_process_subframes_(encoder, is_last_frame)) {
590 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000591 return false;
592 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000593
594 /*
595 * Zero-pad the frame to a byte_boundary
596 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000597 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000598 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
599 return false;
600 }
601
602 /*
603 * Write it
604 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000605 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
606 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
607 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 +0000608 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
609 return false;
610 }
611
612 /*
613 * Get ready for the next frame
614 */
615 encoder->guts->current_frame_can_do_mid_side = true;
616 encoder->guts->current_sample_number = 0;
617 encoder->guts->current_frame_number++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000618 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
619 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
620 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 +0000621
622 return true;
623}
624
Josh Coalson94e02cd2001-01-25 10:41:06 +0000625bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
626{
627 FLAC__FrameHeader frame_header;
628 unsigned channel, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000629 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000630
631 /*
632 * Calculate the max Rice partition order
633 */
634 if(is_last_frame) {
635 max_partition_order = 0;
636 }
637 else {
638 unsigned limit = 0, b = encoder->blocksize;
639 while(!(b & 1)) {
640 limit++;
641 b >>= 1;
642 }
643 max_partition_order = min(encoder->rice_optimization_level, limit);
644 }
645
646 /*
647 * Setup the frame
648 */
649 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
650 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
651 return false;
652 }
653 frame_header.blocksize = encoder->blocksize;
654 frame_header.sample_rate = encoder->sample_rate;
655 frame_header.channels = encoder->channels;
656 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
657 frame_header.bits_per_sample = encoder->bits_per_sample;
658 frame_header.number.frame_number = encoder->guts->current_frame_number;
659
660 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000661 * Figure out what channel assignments to try
662 */
663 if(encoder->do_mid_side_stereo) {
664 if(encoder->loose_mid_side_stereo) {
665 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
666 do_independent = true;
667 do_mid_side = true;
668 }
669 else {
670 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
671 do_mid_side = !do_independent;
672 }
673 }
674 else {
675 do_independent = true;
676 do_mid_side = true;
677 }
678 }
679 else {
680 do_independent = true;
681 do_mid_side = false;
682 }
683 if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
684 do_independent = true;
685 do_mid_side = false;
686 }
687
688 assert(do_independent || do_mid_side);
689
690 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000691 * First do a normal encoding pass of each independent channel
692 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000693 if(do_independent) {
694 for(channel = 0; channel < encoder->channels; channel++) {
695 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))
696 return false;
697 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000698 }
699
700 /*
701 * Now do mid and side channels if requested
702 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000703 if(do_mid_side) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000704 assert(encoder->channels == 2);
705
706 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000707 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 +0000708 return false;
709 }
710 }
711
712 /*
713 * Compose the frame bitbuffer
714 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000715 if(do_mid_side) {
716 FLAC__ChannelAssignment channel_assignment;
717
Josh Coalson94e02cd2001-01-25 10:41:06 +0000718 assert(encoder->channels == 2);
719
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000720 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
721 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
722 }
723 else {
724 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
725 unsigned min_bits;
726 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000727
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000728 assert(do_independent && do_mid_side);
729
730 /* We have to figure out which channel assignent results in the smallest frame */
731 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
732 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
733 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
734 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
735
736 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
737 if(bits[ca] < min_bits) {
738 min_bits = bits[ca];
739 channel_assignment = ca;
740 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000741 }
742 }
743
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000744 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000745
746 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
747 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
748 return false;
749 }
750
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000751 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000752 /* note that encoder_add_subframe_ sets the state for us in case of an error */
753 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000754 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 +0000755 return false;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000756 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 +0000757 return false;
758 break;
759 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000760 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 +0000761 return false;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000762 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 +0000763 return false;
764 break;
765 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000766 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 +0000767 return false;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000768 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 +0000769 return false;
770 break;
771 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000772 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 +0000773 return false;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000774 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 +0000775 return false;
776 break;
777 default:
778 assert(0);
779 }
780 }
781 else {
782 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
783 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
784 return false;
785 }
786
787 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000788 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 +0000789 /* the above function sets the state for us in case of an error */
790 return false;
791 }
792 }
793 }
794
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000795 if(encoder->loose_mid_side_stereo) {
796 encoder->guts->loose_mid_side_stereo_frame_count++;
797 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
798 encoder->guts->loose_mid_side_stereo_frame_count = 0;
799 }
800
801 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
802
Josh Coalson94e02cd2001-01-25 10:41:06 +0000803 return true;
804}
805
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000806bool 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 +0000807{
808 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
809 real lpc_residual_bits_per_sample;
810 real autoc[FLAC__MAX_LPC_ORDER+1];
811 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
812 real lpc_error[FLAC__MAX_LPC_ORDER];
813 unsigned min_lpc_order, max_lpc_order, lpc_order;
814 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
815 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
816 unsigned rice_parameter;
817 unsigned _candidate_bits, _best_bits;
818 unsigned _best_subframe;
819
820 /* verbatim subframe is the baseline against which we measure other compressed subframes */
821 _best_subframe = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000822 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, bits_per_sample, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000823
824 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
825 /* check for constant subframe */
826 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);
827 if(fixed_residual_bits_per_sample[1] == 0.0) {
828 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
829 unsigned i, signal_is_constant = true;
830 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
831 if(integer_signal[0] != integer_signal[i]) {
832 signal_is_constant = false;
833 break;
834 }
835 }
836 if(signal_is_constant) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000837 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], bits_per_sample, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000838 if(_candidate_bits < _best_bits) {
839 _best_subframe = !_best_subframe;
840 _best_bits = _candidate_bits;
841 }
842 }
843 }
844 else {
845 /* encode fixed */
846 if(encoder->do_exhaustive_model_search) {
847 min_fixed_order = 0;
848 max_fixed_order = FLAC__MAX_FIXED_ORDER;
849 }
850 else {
851 min_fixed_order = max_fixed_order = guess_fixed_order;
852 }
853 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000854 if(fixed_residual_bits_per_sample[fixed_order] >= (real)bits_per_sample)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000855 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000856 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 +0000857#ifndef SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000858 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000859#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +0000860 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
861 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000862 _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 +0000863 if(_candidate_bits < _best_bits) {
864 _best_subframe = !_best_subframe;
865 _best_bits = _candidate_bits;
866 }
867 }
868
869 /* encode lpc */
870 if(encoder->max_lpc_order > 0) {
871 if(encoder->max_lpc_order >= frame_header->blocksize)
872 max_lpc_order = frame_header->blocksize-1;
873 else
874 max_lpc_order = encoder->max_lpc_order;
875 if(max_lpc_order > 0) {
876 FLAC__lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000877 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
878 if(autoc[0] != 0.0) {
879 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
880 if(encoder->do_exhaustive_model_search) {
881 min_lpc_order = 1;
882 }
883 else {
884 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, bits_per_sample);
885 min_lpc_order = max_lpc_order = guess_lpc_order;
886 }
887 if(encoder->do_qlp_coeff_prec_search) {
888 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
889 max_qlp_coeff_precision = min(32 - bits_per_sample - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
890 }
891 else {
892 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
893 }
894 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
895 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
896 if(lpc_residual_bits_per_sample >= (real)bits_per_sample)
897 continue; /* don't even try */
898 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 +0000899#ifndef SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000900 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000901#endif
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000902 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
903 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
904 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
905 _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]);
906 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
907 if(_candidate_bits < _best_bits) {
908 _best_subframe = !_best_subframe;
909 _best_bits = _candidate_bits;
910 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000911 }
912 }
913 }
914 }
915 }
916 }
917 }
918 }
919
920 *best_subframe = _best_subframe;
921 *best_bits = _best_bits;
922
923 return true;
924}
925
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000926bool 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 +0000927{
928 switch(subframe->type) {
929 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000930 if(!FLAC__subframe_add_constant(&(subframe->data.constant), bits_per_sample, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000931 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
932 return false;
933 }
934 break;
935 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000936 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 +0000937 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
938 return false;
939 }
940 break;
941 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000942 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 +0000943 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
944 return false;
945 }
946 break;
947 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000948 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, bits_per_sample, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000949 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
950 return false;
951 }
952 break;
953 default:
954 assert(0);
955 }
956
957 return true;
958}
959
960unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe)
961{
962 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
963 subframe->data.constant.value = signal;
964
965 return FLAC__SUBFRAME_TYPE_LEN + bits_per_sample;
966}
967
968unsigned 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)
969{
970 unsigned i, residual_bits;
971 const unsigned residual_samples = blocksize - order;
972
973 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
974
975 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
976
977 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
978 subframe->data.fixed.residual = residual;
979
980 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);
981
982 subframe->data.fixed.order = order;
983 for(i = 0; i < order; i++)
984 subframe->data.fixed.warmup[i] = signal[i];
985
986 return FLAC__SUBFRAME_TYPE_LEN + (order * bits_per_sample) + residual_bits;
987}
988
989unsigned 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)
990{
991 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
992 unsigned i, residual_bits;
993 int quantization, ret;
994 const unsigned residual_samples = blocksize - order;
995
996 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, bits_per_sample, qlp_coeff, &quantization);
997 if(ret != 0)
998 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
999
1000 FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1001
1002 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1003
1004 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1005 subframe->data.lpc.residual = residual;
1006
1007 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);
1008
1009 subframe->data.lpc.order = order;
1010 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1011 subframe->data.lpc.quantization_level = quantization;
1012 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1013 for(i = 0; i < order; i++)
1014 subframe->data.lpc.warmup[i] = signal[i];
1015
1016 return FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + bits_per_sample)) + residual_bits;
1017}
1018
1019unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe)
1020{
1021 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1022
1023 subframe->data.verbatim.data = signal;
1024
1025 return FLAC__SUBFRAME_TYPE_LEN + (blocksize * bits_per_sample);
1026}
1027
1028unsigned 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[])
1029{
1030 unsigned residual_bits, best_residual_bits = 0;
1031 unsigned i, partition_order;
1032 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
1033 int32 r;
1034
1035 /* compute the abs(residual) for use later */
1036 for(i = 0; i < residual_samples; i++) {
1037 r = residual[i];
1038 abs_residual[i] = (uint32)(r<0? -r : r);
1039 }
1040
1041 for(partition_order = 0; partition_order <= max_partition_order; partition_order++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001042 if(!encoder_set_partitioned_rice_(residual, abs_residual, residual_samples, predictor_order, rice_parameter, partition_order, parameters[!best_parameters_index], &residual_bits)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001043 assert(best_residual_bits != 0);
1044 break;
1045 }
1046 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1047 best_residual_bits = residual_bits;
1048 *best_partition_order = partition_order;
1049 best_parameters_index = !best_parameters_index;
1050 }
1051 }
1052 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1053
1054 return best_residual_bits;
1055}
1056
Josh Coalson352e0f62001-03-20 22:55:50 +00001057static unsigned iilog2_(unsigned v)
1058{
1059 unsigned l = 0;
1060 assert(v > 0);
1061 while(v >>= 1)
1062 l++;
1063 return l;
1064}
Josh Coalson94e02cd2001-01-25 10:41:06 +00001065
Josh Coalson352e0f62001-03-20 22:55:50 +00001066static uint32 get_thresh_(const int32 residual[], const unsigned residual_samples)
1067{
1068 double sum, sos, r, stddev, mean;
1069 const double smult = 2.0;
1070 unsigned i;
1071 uint32 thresh;
1072
1073 sum = sos = 0.0;
1074 for(i = 0; i < residual_samples; i++) {
1075 r = (double)residual[i];
1076 sum += r;
1077 sos += r*r;
1078 }
1079 mean = sum / residual_samples;
1080 stddev = sqrt((sos - (sum * sum / residual_samples)) / (residual_samples-1));
1081 thresh = mean+smult*stddev;
1082 thresh = iilog2_(thresh);
1083 return thresh;
1084}
1085
1086#ifdef VARIABLE_RICE_BITS
1087#undef VARIABLE_RICE_BITS
1088#endif
1089#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1090
1091bool encoder_set_partitioned_rice_(const int32 residual[], 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 +00001092{
1093 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1094
1095 if(partition_order == 0) {
1096 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001097 uint32 thresh = get_thresh_(residual, residual_samples);
1098 const unsigned rpdec = 1;
1099 bool cross = false;
1100
1101#ifdef SYMMETRIC_RICE
1102 for(i=0;i<residual_samples;i++) {
1103 if(abs_residual[i] >= thresh) {
1104 cross = true;
1105 break;
1106 }
1107 }
1108 if(cross) {
1109 rice_parameter -= rpdec;
1110 }
1111#endif
1112
1113 {
1114#ifdef VARIABLE_RICE_BITS
1115#ifdef SYMMETRIC_RICE
1116 bits_ += (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001117#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001118 const unsigned rice_parameter_estimate = rice_parameter-1;
1119 bits_ += (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001120#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001121#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001122 parameters[0] = rice_parameter;
1123 bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1124 for(i = 0; i < residual_samples; i++) {
1125#ifdef VARIABLE_RICE_BITS
1126#ifdef SYMMETRIC_RICE
1127 if(cross && abs_residual[i] >= thresh)
1128 bits_ += VARIABLE_RICE_BITS(0, rice_parameter) + 5 + iilog2_(abs_residual) + 1;
1129 else
1130 bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001131#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001132 bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001133#endif
1134#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001135 bits_ += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001136#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001137 }
1138 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001139 }
1140 else {
1141 unsigned i, j, k = 0, k_last = 0;
1142 unsigned mean, parameter, partition_samples;
1143 const unsigned max_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
1144 for(i = 0; i < (1u<<partition_order); i++) {
1145 partition_samples = (residual_samples+predictor_order) >> partition_order;
1146 if(i == 0) {
1147 if(partition_samples <= predictor_order)
1148 return false;
1149 else
1150 partition_samples -= predictor_order;
1151 }
1152 mean = partition_samples >> 1;
1153 for(j = 0; j < partition_samples; j++, k++)
1154 mean += abs_residual[k];
1155 mean /= partition_samples;
Josh Coalson352e0f62001-03-20 22:55:50 +00001156#ifdef SYMMETRIC_RICE
1157 /* calc parameter = floor(log2(mean)) */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001158 parameter = 0;
1159mean>>=1;
1160 while(mean) {
1161 parameter++;
1162 mean >>= 1;
1163 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001164#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001165 /* calc parameter = floor(log2(mean)) + 1 */
1166 parameter = 0;
1167 while(mean) {
1168 parameter++;
1169 mean >>= 1;
1170 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001171#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001172 if(parameter > max_parameter)
1173 parameter = max_parameter;
1174 parameters[i] = parameter;
1175 bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001176#ifdef VARIABLE_RICE_BITS
1177#ifdef SYMMETRIC_RICE
Josh Coalsonb9433f92001-03-17 01:07:00 +00001178 bits_ += (2+parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001179#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001180 bits_ += (1+parameter) * partition_samples;
1181 --parameter;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001182#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001183#endif
1184 for(j = k_last; j < k; j++)
Josh Coalson352e0f62001-03-20 22:55:50 +00001185#ifdef VARIABLE_RICE_BITS
1186#ifdef SYMMETRIC_RICE
1187 bits_ += VARIABLE_RICE_BITS(abs_residual[j], parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001188#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001189 bits_ += VARIABLE_RICE_BITS(abs_residual[j], parameter);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001190#endif
1191#else
Josh Coalson94e02cd2001-01-25 10:41:06 +00001192 bits_ += FLAC__bitbuffer_rice_bits(residual[j], parameter);
1193#endif
1194 k_last = k;
1195 }
1196 }
1197
1198 *bits = bits_;
1199 return true;
1200}