blob: c1890385f550370af7ef916e0ade9778ea92ae4a [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"
Josh Coalson0a72e182001-04-13 19:17:16 +000025#include "FLAC/seek_table.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000026#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000027#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000028#include "private/crc.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000029#include "private/encoder_framing.h"
30#include "private/fixed.h"
31#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000032#include "private/md5.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000033
34#ifdef min
35#undef min
36#endif
37#define min(x,y) ((x)<(y)?(x):(y))
38
39#ifdef max
40#undef max
41#endif
42#define max(x,y) ((x)>(y)?(x):(y))
43
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000044typedef struct FLAC__EncoderPrivate {
45 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
46 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
47 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
48 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
49 real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
Josh Coalson82b73242001-03-28 22:17:05 +000050 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
Josh Coalsonac4c1582001-03-28 23:10:22 +000051 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
Josh Coalson94e02cd2001-01-25 10:41:06 +000052 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
53 int32 *residual_workspace_mid_side[2][2];
54 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
55 FLAC__Subframe subframe_workspace_mid_side[2][2];
56 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
57 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
58 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
59 unsigned best_subframe_mid_side[2];
60 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
61 unsigned best_subframe_bits_mid_side[2];
Josh Coalson2051dd42001-04-12 22:22:34 +000062 uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000063 uint32 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
Josh Coalsonaef013c2001-04-24 01:25:42 +000064 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000065 FLAC__BitBuffer frame; /* the current frame being worked on */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000066 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 +000067 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
68 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
69 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
70 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000071 FLAC__StreamMetaData metadata;
72 unsigned current_sample_number;
73 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000074 struct MD5Context md5context;
Josh Coalsoneef56702001-03-30 00:45:22 +000075 bool use_slow; /* use slow 64-bit versions of some functions */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000076 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
77 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
78 void *client_data;
79} FLAC__EncoderPrivate;
80
81static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
82static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000083static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson60f77d72001-04-25 02:16:36 +000084static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
Josh Coalson82b73242001-03-28 22:17:05 +000085static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
86static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +000087static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
88static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
Josh Coalson82b73242001-03-28 22:17:05 +000089static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +000090static unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
Josh Coalsonbb6712e2001-04-24 22:54:07 +000091#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +000092static unsigned encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
Josh Coalsonafcd8772001-04-18 22:59:25 +000093#endif
Josh Coalson60f77d72001-04-25 02:16:36 +000094static bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, unsigned rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
Josh Coalson859bc542001-03-27 22:22:27 +000095static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000096
Josh Coalsoncbf595f2000-12-22 22:35:33 +000097const char *FLAC__EncoderWriteStatusString[] = {
98 "FLAC__ENCODER_WRITE_OK",
99 "FLAC__ENCODER_WRITE_FATAL_ERROR"
100};
101
102const char *FLAC__EncoderStateString[] = {
103 "FLAC__ENCODER_OK",
104 "FLAC__ENCODER_UNINITIALIZED",
105 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
106 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
107 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
108 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
109 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
110 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
111 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000112 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000113 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
114 "FLAC__ENCODER_NOT_STREAMABLE",
115 "FLAC__ENCODER_FRAMING_ERROR",
116 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
117 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
118 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
119};
120
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000121
122bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
123{
124 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000125 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000126 int32 *previous_is, *current_is;
127 real *previous_rs, *current_rs;
128 int32 *residual;
Josh Coalsone77287e2001-01-20 01:27:55 +0000129 uint32 *abs_residual;
Josh Coalsonaef013c2001-04-24 01:25:42 +0000130 unsigned *raw_bits_per_partition;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000131
132 assert(new_size > 0);
133 assert(encoder->state == FLAC__ENCODER_OK);
134 assert(encoder->guts->current_sample_number == 0);
135
136 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
137 if(new_size <= encoder->guts->input_capacity)
138 return true;
139
140 ok = 1;
141 if(ok) {
142 for(i = 0; ok && i < encoder->channels; i++) {
143 /* integer version of the signal */
144 previous_is = encoder->guts->integer_signal[i];
145 current_is = (int32*)malloc(sizeof(int32) * new_size);
146 if(0 == current_is) {
147 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
148 ok = 0;
149 }
150 else {
151 encoder->guts->integer_signal[i] = current_is;
152 if(previous_is != 0)
153 free(previous_is);
154 }
155 /* real version of the signal */
156 previous_rs = encoder->guts->real_signal[i];
157 current_rs = (real*)malloc(sizeof(real) * new_size);
158 if(0 == current_rs) {
159 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
160 ok = 0;
161 }
162 else {
163 encoder->guts->real_signal[i] = current_rs;
164 if(previous_rs != 0)
165 free(previous_rs);
166 }
167 }
168 }
169 if(ok) {
170 for(i = 0; ok && i < 2; i++) {
171 /* integer version of the signal */
172 previous_is = encoder->guts->integer_signal_mid_side[i];
173 current_is = (int32*)malloc(sizeof(int32) * new_size);
174 if(0 == current_is) {
175 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
176 ok = 0;
177 }
178 else {
179 encoder->guts->integer_signal_mid_side[i] = current_is;
180 if(previous_is != 0)
181 free(previous_is);
182 }
183 /* real version of the signal */
184 previous_rs = encoder->guts->real_signal_mid_side[i];
185 current_rs = (real*)malloc(sizeof(real) * new_size);
186 if(0 == current_rs) {
187 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
188 ok = 0;
189 }
190 else {
191 encoder->guts->real_signal_mid_side[i] = current_rs;
192 if(previous_rs != 0)
193 free(previous_rs);
194 }
195 }
196 }
197 if(ok) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000198 for(channel = 0; channel < encoder->channels; channel++) {
199 for(i = 0; i < 2; i++) {
200 residual = (int32*)malloc(sizeof(int32) * new_size);
201 if(0 == residual) {
202 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
203 ok = 0;
204 }
205 else {
206 if(encoder->guts->residual_workspace[channel][i] != 0)
207 free(encoder->guts->residual_workspace[channel][i]);
208 encoder->guts->residual_workspace[channel][i] = residual;
209 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000210 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000211 }
212 for(channel = 0; channel < 2; channel++) {
213 for(i = 0; i < 2; i++) {
214 residual = (int32*)malloc(sizeof(int32) * new_size);
215 if(0 == residual) {
216 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
217 ok = 0;
218 }
219 else {
220 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0)
221 free(encoder->guts->residual_workspace_mid_side[channel][i]);
222 encoder->guts->residual_workspace_mid_side[channel][i] = residual;
223 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000224 }
225 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000226 abs_residual = (uint32*)malloc(sizeof(uint32) * new_size);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000227 if(0 == abs_residual) {
Josh Coalsone77287e2001-01-20 01:27:55 +0000228 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
229 ok = 0;
230 }
231 else {
232 if(encoder->guts->abs_residual != 0)
233 free(encoder->guts->abs_residual);
234 encoder->guts->abs_residual = abs_residual;
235 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000236#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000237 abs_residual = (uint32*)malloc(sizeof(uint32) * (new_size * 2));
238 if(0 == abs_residual) {
239 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
240 ok = 0;
241 }
242 else {
243 if(encoder->guts->abs_residual_partition_sums != 0)
244 free(encoder->guts->abs_residual_partition_sums);
245 encoder->guts->abs_residual_partition_sums = abs_residual;
246 }
Josh Coalson96611082001-04-24 01:30:10 +0000247#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000248#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +0000249 raw_bits_per_partition = (unsigned*)malloc(sizeof(unsigned) * (new_size * 2));
250 if(0 == raw_bits_per_partition) {
Josh Coalson2051dd42001-04-12 22:22:34 +0000251 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
252 ok = 0;
253 }
254 else {
Josh Coalsonaef013c2001-04-24 01:25:42 +0000255 if(encoder->guts->raw_bits_per_partition != 0)
256 free(encoder->guts->raw_bits_per_partition);
257 encoder->guts->raw_bits_per_partition = raw_bits_per_partition;
Josh Coalson2051dd42001-04-12 22:22:34 +0000258 }
Josh Coalson96611082001-04-24 01:30:10 +0000259#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000260 }
261 if(ok)
262 encoder->guts->input_capacity = new_size;
263
264 return ok;
265}
266
267FLAC__Encoder *FLAC__encoder_get_new_instance()
268{
269 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
270 if(encoder != 0) {
271 encoder->state = FLAC__ENCODER_UNINITIALIZED;
272 encoder->guts = 0;
273 }
274 return encoder;
275}
276
277void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
278{
279 assert(encoder != 0);
280 free(encoder);
281}
282
283FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data), void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data), void *client_data)
284{
285 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000286 FLAC__StreamMetaData padding;
Josh Coalson0a72e182001-04-13 19:17:16 +0000287 FLAC__StreamMetaData seek_table;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000288
289 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
290 assert(encoder != 0);
291 assert(write_callback != 0);
292 assert(metadata_callback != 0);
293 assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
294 assert(encoder->guts == 0);
295
296 encoder->state = FLAC__ENCODER_OK;
297
298 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
299 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
300
301 if(encoder->do_mid_side_stereo && encoder->channels != 2)
302 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
303
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000304 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000305 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
306
Josh Coalson82b73242001-03-28 22:17:05 +0000307 if(encoder->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000308 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
309
310 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
311 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
312
313 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
314 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
315
316 if(encoder->blocksize < encoder->max_lpc_order)
317 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
318
319 if(encoder->qlp_coeff_precision == 0) {
320 if(encoder->bits_per_sample < 16) {
321 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
322 /* @@@ until then we'll make a guess */
323 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
324 }
325 else if(encoder->bits_per_sample == 16) {
326 if(encoder->blocksize <= 192)
327 encoder->qlp_coeff_precision = 7;
328 else if(encoder->blocksize <= 384)
329 encoder->qlp_coeff_precision = 8;
330 else if(encoder->blocksize <= 576)
331 encoder->qlp_coeff_precision = 9;
332 else if(encoder->blocksize <= 1152)
333 encoder->qlp_coeff_precision = 10;
334 else if(encoder->blocksize <= 2304)
335 encoder->qlp_coeff_precision = 11;
336 else if(encoder->blocksize <= 4608)
337 encoder->qlp_coeff_precision = 12;
338 else
339 encoder->qlp_coeff_precision = 13;
340 }
341 else {
342 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
343 }
344 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000345 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 +0000346 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
347
348 if(encoder->streamable_subset) {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000349 //@@@ add check for blocksize here
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000350 if(encoder->bits_per_sample != 8 && encoder->bits_per_sample != 12 && encoder->bits_per_sample != 16 && encoder->bits_per_sample != 20 && encoder->bits_per_sample != 24)
351 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
352 if(encoder->sample_rate > 655350)
353 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
354 }
355
Josh Coalson60f77d72001-04-25 02:16:36 +0000356 if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
357 encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
358 if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
359 encoder->min_residual_partition_order = encoder->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000360
361 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
362 if(encoder->guts == 0)
363 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
364
365 encoder->guts->input_capacity = 0;
366 for(i = 0; i < encoder->channels; i++) {
367 encoder->guts->integer_signal[i] = 0;
368 encoder->guts->real_signal[i] = 0;
369 }
370 for(i = 0; i < 2; i++) {
371 encoder->guts->integer_signal_mid_side[i] = 0;
372 encoder->guts->real_signal_mid_side[i] = 0;
373 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000374 for(i = 0; i < encoder->channels; i++) {
375 encoder->guts->residual_workspace[i][0] = encoder->guts->residual_workspace[i][1] = 0;
376 encoder->guts->best_subframe[i] = 0;
377 }
378 for(i = 0; i < 2; i++) {
379 encoder->guts->residual_workspace_mid_side[i][0] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
380 encoder->guts->best_subframe_mid_side[i] = 0;
381 }
382 for(i = 0; i < encoder->channels; i++) {
383 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
384 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
385 }
386 for(i = 0; i < 2; i++) {
387 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
388 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
389 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000390 encoder->guts->abs_residual = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000391 encoder->guts->abs_residual_partition_sums = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +0000392 encoder->guts->raw_bits_per_partition = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000393 encoder->guts->current_frame_can_do_mid_side = true;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000394 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
395 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
396 if(encoder->guts->loose_mid_side_stereo_frames == 0)
397 encoder->guts->loose_mid_side_stereo_frames = 1;
398 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000399 encoder->guts->current_sample_number = 0;
400 encoder->guts->current_frame_number = 0;
401
Josh Coalsoneef56702001-03-30 00:45:22 +0000402 if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
403 encoder->guts->use_slow = true;
404 else
405 encoder->guts->use_slow = false;
406
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000407 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
408 /* the above function sets the state for us in case of an error */
409 return encoder->state;
410 }
411 FLAC__bitbuffer_init(&encoder->guts->frame);
412 encoder->guts->write_callback = write_callback;
413 encoder->guts->metadata_callback = metadata_callback;
414 encoder->guts->client_data = client_data;
415
416 /*
417 * write the stream header
418 */
419 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
420 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
421
422 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
423 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
424
Josh Coalsonc692d382001-02-23 21:05:05 +0000425 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson0a72e182001-04-13 19:17:16 +0000426 encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
Josh Coalsonc692d382001-02-23 21:05:05 +0000427 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
428 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
429 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
430 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
431 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
432 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
433 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
434 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
435 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
436 memset(encoder->guts->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000437 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000438 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
439 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
440
Josh Coalson0a72e182001-04-13 19:17:16 +0000441 if(0 != encoder->seek_table) {
442 if(!FLAC__seek_table_is_valid(encoder->seek_table))
443 return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
444 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
445 seek_table.is_last = (encoder->padding == 0);
446 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
447 seek_table.data.seek_table = *encoder->seek_table;
448 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
449 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
450 }
451
Josh Coalsonc692d382001-02-23 21:05:05 +0000452 /* add a PADDING block if requested */
453 if(encoder->padding > 0) {
454 padding.type = FLAC__METADATA_TYPE_PADDING;
455 padding.is_last = true;
456 padding.length = encoder->padding;
457 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
458 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
459 }
460
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000461 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
462 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
463 if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, 0, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK)
464 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
465
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000466 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000467 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 +0000468 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000469 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000470
471 return encoder->state;
472}
473
474void FLAC__encoder_finish(FLAC__Encoder *encoder)
475{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000476 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000477
478 assert(encoder != 0);
479 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
480 return;
481 if(encoder->guts->current_sample_number != 0) {
482 encoder->blocksize = encoder->guts->current_sample_number;
483 encoder_process_frame_(encoder, true); /* true => is last frame */
484 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000485 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000486 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
487 if(encoder->guts != 0) {
488 for(i = 0; i < encoder->channels; i++) {
489 if(encoder->guts->integer_signal[i] != 0) {
490 free(encoder->guts->integer_signal[i]);
491 encoder->guts->integer_signal[i] = 0;
492 }
493 if(encoder->guts->real_signal[i] != 0) {
494 free(encoder->guts->real_signal[i]);
495 encoder->guts->real_signal[i] = 0;
496 }
497 }
498 for(i = 0; i < 2; i++) {
499 if(encoder->guts->integer_signal_mid_side[i] != 0) {
500 free(encoder->guts->integer_signal_mid_side[i]);
501 encoder->guts->integer_signal_mid_side[i] = 0;
502 }
503 if(encoder->guts->real_signal_mid_side[i] != 0) {
504 free(encoder->guts->real_signal_mid_side[i]);
505 encoder->guts->real_signal_mid_side[i] = 0;
506 }
507 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000508 for(channel = 0; channel < encoder->channels; channel++) {
509 for(i = 0; i < 2; i++) {
510 if(encoder->guts->residual_workspace[channel][i] != 0) {
511 free(encoder->guts->residual_workspace[channel][i]);
512 encoder->guts->residual_workspace[channel][i] = 0;
513 }
514 }
515 }
516 for(channel = 0; channel < 2; channel++) {
517 for(i = 0; i < 2; i++) {
518 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0) {
519 free(encoder->guts->residual_workspace_mid_side[channel][i]);
520 encoder->guts->residual_workspace_mid_side[channel][i] = 0;
521 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000522 }
523 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000524 if(encoder->guts->abs_residual != 0) {
525 free(encoder->guts->abs_residual);
526 encoder->guts->abs_residual = 0;
527 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000528 if(encoder->guts->abs_residual_partition_sums != 0) {
529 free(encoder->guts->abs_residual_partition_sums);
530 encoder->guts->abs_residual_partition_sums = 0;
531 }
Josh Coalsonaef013c2001-04-24 01:25:42 +0000532 if(encoder->guts->raw_bits_per_partition != 0) {
533 free(encoder->guts->raw_bits_per_partition);
534 encoder->guts->raw_bits_per_partition = 0;
Josh Coalson2051dd42001-04-12 22:22:34 +0000535 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000536 FLAC__bitbuffer_free(&encoder->guts->frame);
537 free(encoder->guts);
538 encoder->guts = 0;
539 }
540 encoder->state = FLAC__ENCODER_UNINITIALIZED;
541}
542
543bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
544{
545 unsigned i, j, channel;
546 int32 x, mid, side;
547 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
548 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
549 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
550
551 assert(encoder != 0);
552 assert(encoder->state == FLAC__ENCODER_OK);
553
554 j = 0;
555 do {
556 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
557 for(channel = 0; channel < encoder->channels; channel++) {
558 x = buf[channel][j];
559 encoder->guts->integer_signal[channel][i] = x;
560 encoder->guts->real_signal[channel][i] = (real)x;
561 }
562 if(ms && encoder->guts->current_frame_can_do_mid_side) {
563 side = buf[0][j] - buf[1][j];
564 if(side < min_side || side > max_side) {
565 encoder->guts->current_frame_can_do_mid_side = false;
566 }
567 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000568 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 +0000569 encoder->guts->integer_signal_mid_side[0][i] = mid;
570 encoder->guts->integer_signal_mid_side[1][i] = side;
571 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
572 encoder->guts->real_signal_mid_side[1][i] = (real)side;
573 }
574 }
575 encoder->guts->current_sample_number++;
576 }
577 if(i == encoder->blocksize) {
578 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
579 return false;
580 }
581 } while(j < samples);
582
583 return true;
584}
585
586/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
587bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
588{
589 unsigned i, j, k, channel;
590 int32 x, left = 0, mid, side;
591 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
592 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
593 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
594
595 assert(encoder != 0);
596 assert(encoder->state == FLAC__ENCODER_OK);
597
598 j = k = 0;
599 do {
600 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
601 for(channel = 0; channel < encoder->channels; channel++, k++) {
602 x = buf[k];
603 encoder->guts->integer_signal[channel][i] = x;
604 encoder->guts->real_signal[channel][i] = (real)x;
605 if(ms && encoder->guts->current_frame_can_do_mid_side) {
606 if(channel == 0) {
607 left = x;
608 }
609 else {
610 side = left - x;
611 if(side < min_side || side > max_side) {
612 encoder->guts->current_frame_can_do_mid_side = false;
613 }
614 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000615 mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000616 encoder->guts->integer_signal_mid_side[0][i] = mid;
617 encoder->guts->integer_signal_mid_side[1][i] = side;
618 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
619 encoder->guts->real_signal_mid_side[1][i] = (real)side;
620 }
621 }
622 }
623 }
624 encoder->guts->current_sample_number++;
625 }
626 if(i == encoder->blocksize) {
627 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
628 return false;
629 }
630 } while(j < samples);
631
632 return true;
633}
634
635bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
636{
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000637 assert(encoder->state == FLAC__ENCODER_OK);
638
639 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000640 * Accumulate raw signal to the MD5 signature
641 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000642 /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000643 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
644 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
645 return false;
646 }
647
648 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000649 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000650 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000651 if(!encoder_process_subframes_(encoder, is_last_frame)) {
652 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000653 return false;
654 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000655
656 /*
657 * Zero-pad the frame to a byte_boundary
658 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000659 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000660 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
661 return false;
662 }
663
664 /*
Josh Coalson215af572001-03-27 01:15:58 +0000665 * CRC-16 the whole thing
666 */
667 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
668 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
669 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
670
671 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000672 * Write it
673 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000674 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 +0000675 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
676 return false;
677 }
678
679 /*
680 * Get ready for the next frame
681 */
682 encoder->guts->current_frame_can_do_mid_side = true;
683 encoder->guts->current_sample_number = 0;
684 encoder->guts->current_frame_number++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000685 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
686 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
687 encoder->guts->metadata.data.stream_info.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.max_framesize);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000688
689 return true;
690}
691
Josh Coalson94e02cd2001-01-25 10:41:06 +0000692bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
693{
694 FLAC__FrameHeader frame_header;
Josh Coalson60f77d72001-04-25 02:16:36 +0000695 unsigned channel, min_partition_order, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000696 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000697
698 /*
Josh Coalson60f77d72001-04-25 02:16:36 +0000699 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +0000700 */
701 if(is_last_frame) {
702 max_partition_order = 0;
703 }
704 else {
705 unsigned limit = 0, b = encoder->blocksize;
706 while(!(b & 1)) {
707 limit++;
708 b >>= 1;
709 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000710 max_partition_order = min(encoder->max_residual_partition_order, limit);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000711 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000712 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000713
714 /*
715 * Setup the frame
716 */
717 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
718 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
719 return false;
720 }
721 frame_header.blocksize = encoder->blocksize;
722 frame_header.sample_rate = encoder->sample_rate;
723 frame_header.channels = encoder->channels;
724 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
725 frame_header.bits_per_sample = encoder->bits_per_sample;
726 frame_header.number.frame_number = encoder->guts->current_frame_number;
727
728 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000729 * Figure out what channel assignments to try
730 */
731 if(encoder->do_mid_side_stereo) {
732 if(encoder->loose_mid_side_stereo) {
733 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
734 do_independent = true;
735 do_mid_side = true;
736 }
737 else {
738 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
739 do_mid_side = !do_independent;
740 }
741 }
742 else {
743 do_independent = true;
744 do_mid_side = true;
745 }
746 }
747 else {
748 do_independent = true;
749 do_mid_side = false;
750 }
751 if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
752 do_independent = true;
753 do_mid_side = false;
754 }
755
756 assert(do_independent || do_mid_side);
757
758 /*
Josh Coalson82b73242001-03-28 22:17:05 +0000759 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +0000760 */
761 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +0000762 unsigned w;
763 for(channel = 0; channel < encoder->channels; channel++) {
764 w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
765 encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
766 encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
767 }
Josh Coalson859bc542001-03-27 22:22:27 +0000768 }
769 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000770 unsigned w;
Josh Coalson859bc542001-03-27 22:22:27 +0000771 assert(encoder->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +0000772 for(channel = 0; channel < 2; channel++) {
773 w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
774 encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
775 encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
776 }
Josh Coalson859bc542001-03-27 22:22:27 +0000777 }
778
779 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000780 * First do a normal encoding pass of each independent channel
781 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000782 if(do_independent) {
783 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000784 if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps[channel], encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel))
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000785 return false;
786 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000787 }
788
789 /*
790 * Now do mid and side channels if requested
791 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000792 if(do_mid_side) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000793 assert(encoder->channels == 2);
794
795 for(channel = 0; channel < 2; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000796 if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps_mid_side[channel], encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000797 return false;
798 }
799 }
800
801 /*
802 * Compose the frame bitbuffer
803 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000804 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000805 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
806 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000807 FLAC__ChannelAssignment channel_assignment;
808
Josh Coalson94e02cd2001-01-25 10:41:06 +0000809 assert(encoder->channels == 2);
810
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000811 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
812 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
813 }
814 else {
815 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
816 unsigned min_bits;
817 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000818
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000819 assert(do_independent && do_mid_side);
820
821 /* We have to figure out which channel assignent results in the smallest frame */
822 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
823 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
824 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
825 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
826
827 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
828 if(bits[ca] < min_bits) {
829 min_bits = bits[ca];
830 channel_assignment = ca;
831 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000832 }
833 }
834
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000835 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000836
837 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
838 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
839 return false;
840 }
841
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000842 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000843 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson82b73242001-03-28 22:17:05 +0000844 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
845 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000846 break;
847 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000848 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
849 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000850 break;
851 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000852 left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
853 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000854 break;
855 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000856 left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
857 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000858 break;
859 default:
860 assert(0);
861 }
Josh Coalson82b73242001-03-28 22:17:05 +0000862
863 switch(channel_assignment) {
864 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
865 left_bps = encoder->guts->subframe_bps [0];
866 right_bps = encoder->guts->subframe_bps [1];
867 break;
868 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
869 left_bps = encoder->guts->subframe_bps [0];
870 right_bps = encoder->guts->subframe_bps_mid_side[1];
871 break;
872 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
873 left_bps = encoder->guts->subframe_bps_mid_side[1];
874 right_bps = encoder->guts->subframe_bps [1];
875 break;
876 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
877 left_bps = encoder->guts->subframe_bps_mid_side[0];
878 right_bps = encoder->guts->subframe_bps_mid_side[1];
879 break;
880 default:
881 assert(0);
882 }
883
884 /* note that encoder_add_subframe_ sets the state for us in case of an error */
885 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
886 return false;
887 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
888 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000889 }
890 else {
891 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
892 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
893 return false;
894 }
895
896 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000897 if(!encoder_add_subframe_(encoder, &frame_header, encoder->guts->subframe_bps[channel], &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000898 /* the above function sets the state for us in case of an error */
899 return false;
900 }
901 }
902 }
903
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000904 if(encoder->loose_mid_side_stereo) {
905 encoder->guts->loose_mid_side_stereo_frame_count++;
906 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
907 encoder->guts->loose_mid_side_stereo_frame_count = 0;
908 }
909
910 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
911
Josh Coalson94e02cd2001-01-25 10:41:06 +0000912 return true;
913}
914
Josh Coalson60f77d72001-04-25 02:16:36 +0000915bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000916{
917 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
918 real lpc_residual_bits_per_sample;
919 real autoc[FLAC__MAX_LPC_ORDER+1];
920 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
921 real lpc_error[FLAC__MAX_LPC_ORDER];
922 unsigned min_lpc_order, max_lpc_order, lpc_order;
923 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
924 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
925 unsigned rice_parameter;
926 unsigned _candidate_bits, _best_bits;
927 unsigned _best_subframe;
928
929 /* verbatim subframe is the baseline against which we measure other compressed subframes */
930 _best_subframe = 0;
Josh Coalson82b73242001-03-28 22:17:05 +0000931 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000932
933 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
934 /* check for constant subframe */
Josh Coalsoneef56702001-03-30 00:45:22 +0000935 if(encoder->guts->use_slow)
936 guess_fixed_order = FLAC__fixed_compute_best_predictor_slow(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
937 else
938 guess_fixed_order = FLAC__fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000939 if(fixed_residual_bits_per_sample[1] == 0.0) {
940 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
941 unsigned i, signal_is_constant = true;
942 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
943 if(integer_signal[0] != integer_signal[i]) {
944 signal_is_constant = false;
945 break;
946 }
947 }
948 if(signal_is_constant) {
Josh Coalson82b73242001-03-28 22:17:05 +0000949 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000950 if(_candidate_bits < _best_bits) {
951 _best_subframe = !_best_subframe;
952 _best_bits = _candidate_bits;
953 }
954 }
955 }
956 else {
957 /* encode fixed */
958 if(encoder->do_exhaustive_model_search) {
959 min_fixed_order = 0;
960 max_fixed_order = FLAC__MAX_FIXED_ORDER;
961 }
962 else {
963 min_fixed_order = max_fixed_order = guess_fixed_order;
964 }
965 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000966 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000967 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000968 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 Coalsonbb6712e2001-04-24 22:54:07 +0000969#ifndef FLAC__SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000970 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000971#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +0000972 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
973 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +0000974 _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, encoder->rice_parameter_search_dist, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000975 if(_candidate_bits < _best_bits) {
976 _best_subframe = !_best_subframe;
977 _best_bits = _candidate_bits;
978 }
979 }
980
981 /* encode lpc */
982 if(encoder->max_lpc_order > 0) {
983 if(encoder->max_lpc_order >= frame_header->blocksize)
984 max_lpc_order = frame_header->blocksize-1;
985 else
986 max_lpc_order = encoder->max_lpc_order;
987 if(max_lpc_order > 0) {
988 FLAC__lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000989 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
990 if(autoc[0] != 0.0) {
991 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
992 if(encoder->do_exhaustive_model_search) {
993 min_lpc_order = 1;
994 }
995 else {
Josh Coalson82b73242001-03-28 22:17:05 +0000996 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000997 min_lpc_order = max_lpc_order = guess_lpc_order;
998 }
999 if(encoder->do_qlp_coeff_prec_search) {
1000 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalson82b73242001-03-28 22:17:05 +00001001 max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001002 }
1003 else {
1004 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
1005 }
1006 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1007 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
Josh Coalson82b73242001-03-28 22:17:05 +00001008 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001009 continue; /* don't even try */
1010 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001011#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001012 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001013#endif
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001014 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
1015 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
1016 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
Josh Coalson60f77d72001-04-25 02:16:36 +00001017 _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->raw_bits_per_partition, lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, encoder->rice_parameter_search_dist, subframe[!_best_subframe]);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001018 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1019 if(_candidate_bits < _best_bits) {
1020 _best_subframe = !_best_subframe;
1021 _best_bits = _candidate_bits;
1022 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001023 }
1024 }
1025 }
1026 }
1027 }
1028 }
1029 }
1030 }
1031
1032 *best_subframe = _best_subframe;
1033 *best_bits = _best_bits;
1034
1035 return true;
1036}
1037
Josh Coalson82b73242001-03-28 22:17:05 +00001038bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001039{
1040 switch(subframe->type) {
1041 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00001042 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001043 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1044 return false;
1045 }
1046 break;
1047 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00001048 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001049 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1050 return false;
1051 }
1052 break;
1053 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00001054 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001055 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1056 return false;
1057 }
1058 break;
1059 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00001060 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001061 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1062 return false;
1063 }
1064 break;
1065 default:
1066 assert(0);
1067 }
1068
1069 return true;
1070}
1071
Josh Coalson82b73242001-03-28 22:17:05 +00001072unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001073{
1074 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1075 subframe->data.constant.value = signal;
1076
Josh Coalson82b73242001-03-28 22:17:05 +00001077 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001078}
1079
Josh Coalson60f77d72001-04-25 02:16:36 +00001080unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001081{
1082 unsigned i, residual_bits;
1083 const unsigned residual_samples = blocksize - order;
1084
1085 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1086
1087 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1088
1089 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1090 subframe->data.fixed.residual = residual;
1091
Josh Coalson60f77d72001-04-25 02:16:36 +00001092 residual_bits = encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, rice_parameter_search_dist, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001093
1094 subframe->data.fixed.order = order;
1095 for(i = 0; i < order; i++)
1096 subframe->data.fixed.warmup[i] = signal[i];
1097
Josh Coalson82b73242001-03-28 22:17:05 +00001098 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001099}
1100
Josh Coalson60f77d72001-04-25 02:16:36 +00001101unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001102{
1103 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1104 unsigned i, residual_bits;
1105 int quantization, ret;
1106 const unsigned residual_samples = blocksize - order;
1107
Josh Coalson82b73242001-03-28 22:17:05 +00001108 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001109 if(ret != 0)
1110 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1111
1112 FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1113
1114 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1115
1116 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1117 subframe->data.lpc.residual = residual;
1118
Josh Coalson60f77d72001-04-25 02:16:36 +00001119 residual_bits = encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, rice_parameter_search_dist, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001120
1121 subframe->data.lpc.order = order;
1122 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1123 subframe->data.lpc.quantization_level = quantization;
1124 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1125 for(i = 0; i < order; i++)
1126 subframe->data.lpc.warmup[i] = signal[i];
1127
Josh Coalson82b73242001-03-28 22:17:05 +00001128 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001129}
1130
Josh Coalson82b73242001-03-28 22:17:05 +00001131unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001132{
1133 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1134
1135 subframe->data.verbatim.data = signal;
1136
Josh Coalson82b73242001-03-28 22:17:05 +00001137 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001138}
1139
Josh Coalson60f77d72001-04-25 02:16:36 +00001140unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
Josh Coalson94e02cd2001-01-25 10:41:06 +00001141{
Josh Coalson94e02cd2001-01-25 10:41:06 +00001142 int32 r;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001143#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalsonafcd8772001-04-18 22:59:25 +00001144 unsigned sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001145 int partition_order;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001146#else
1147 unsigned partition_order;
1148#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001149 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001150 unsigned residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001151 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001152
Josh Coalson2051dd42001-04-12 22:22:34 +00001153 /* compute abs(residual) for use later */
1154 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1155 r = residual[residual_sample];
1156 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1157 }
1158
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001159#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001160 max_partition_order = encoder_precompute_partition_info_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
1161 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001162
Josh Coalson60f77d72001-04-25 02:16:36 +00001163 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1164 if(!encoder_set_partitioned_rice_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00001165 assert(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001166 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001167 sum += 1u << partition_order;
1168 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1169 best_residual_bits = residual_bits;
1170 *best_partition_order = partition_order;
1171 best_parameters_index = !best_parameters_index;
1172 }
1173 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001174#else
Josh Coalson60f77d72001-04-25 02:16:36 +00001175 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1176 if(!encoder_set_partitioned_rice_(abs_residual, 0, 0, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
Josh Coalsonafcd8772001-04-18 22:59:25 +00001177 assert(best_residual_bits != 0);
1178 break;
1179 }
1180 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1181 best_residual_bits = residual_bits;
1182 *best_partition_order = partition_order;
1183 best_parameters_index = !best_parameters_index;
1184 }
1185 }
1186#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001187 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1188 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1189
1190 return best_residual_bits;
1191}
1192
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001193#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001194unsigned encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001195{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001196 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001197 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001198 const unsigned blocksize = residual_samples + predictor_order;
1199
Josh Coalsonaef013c2001-04-24 01:25:42 +00001200 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001201 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001202#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001203 uint32 abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001204#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001205#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001206 uint32 abs_residual_partition_max;
1207 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1208#endif
1209 uint32 abs_r;
1210 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001211 const unsigned partitions = 1u << partition_order;
1212 const unsigned default_partition_samples = blocksize >> partition_order;
1213
1214 if(default_partition_samples <= predictor_order) {
1215 assert(max_partition_order > 0);
1216 max_partition_order--;
1217 }
1218 else {
1219 for(partition = residual_sample = 0; partition < partitions; partition++) {
1220 partition_samples = default_partition_samples;
1221 if(partition == 0)
1222 partition_samples -= predictor_order;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001223#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001224 abs_residual_partition_sum = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001225#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001226#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001227 abs_residual_partition_max = 0;
1228#endif
1229 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1230 abs_r = abs_residual[residual_sample];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001231#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001232 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1233#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001234#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001235 if(abs_r > abs_residual_partition_max) {
1236 abs_residual_partition_max = abs_r;
1237 abs_residual_partition_max_index = residual_sample;
1238 }
1239#endif
1240 residual_sample++;
1241 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001242#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001243 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001244#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001245#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001246 if(abs_residual_partition_max > 0)
1247 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1248 else
1249 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1250#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001251 }
Josh Coalsonaef013c2001-04-24 01:25:42 +00001252 to_partition = partitions;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001253 break;
1254 }
1255 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001256
Josh Coalsonaef013c2001-04-24 01:25:42 +00001257 /* now merge for lower orders */
Josh Coalson60f77d72001-04-25 02:16:36 +00001258 for(from_partition = 0; partition_order >= min_partition_order; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001259#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001260 uint32 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001261#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001262#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001263 unsigned m;
1264#endif
1265 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001266 const unsigned partitions = 1u << partition_order;
1267 for(i = 0; i < partitions; i++) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001268#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001269 s = abs_residual_partition_sums[from_partition];
1270#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001271#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001272 m = raw_bits_per_partition[from_partition];
1273#endif
1274 from_partition++;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001275#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001276 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1277#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001278#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001279 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1280#endif
1281 from_partition++;
1282 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001283 }
1284 }
1285
Josh Coalsonf76a3612001-04-18 02:28:11 +00001286 return max_partition_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001287}
Josh Coalsonafcd8772001-04-18 22:59:25 +00001288#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001289
Josh Coalson352e0f62001-03-20 22:55:50 +00001290#ifdef VARIABLE_RICE_BITS
1291#undef VARIABLE_RICE_BITS
1292#endif
1293#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1294
Josh Coalson60f77d72001-04-25 02:16:36 +00001295bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, unsigned rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001296{
Josh Coalsonafcd8772001-04-18 22:59:25 +00001297 unsigned partition_bits;
Josh Coalson60f77d72001-04-25 02:16:36 +00001298 unsigned min_rice_parameter, max_rice_parameter;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001299#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001300 unsigned flat_bits;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001301#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001302 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1303
Josh Coalson2051dd42001-04-12 22:22:34 +00001304 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1305 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1306
Josh Coalson94e02cd2001-01-25 10:41:06 +00001307 if(partition_order == 0) {
1308 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001309
Josh Coalson60f77d72001-04-25 02:16:36 +00001310#if 0
1311 /*@@@ pending @@@*/
1312 if(rice_parameter_search_dist) {
1313 if(rice_parameter < rice_parameter_search_dist)
1314 min_rice_parameter = 0;
1315 else
1316 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1317 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1318 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1319 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1320 }
1321 else
1322 min_rice_parameter = max_rice_parameter = rice_parameter;
1323#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001324
Josh Coalson60f77d72001-04-25 02:16:36 +00001325 partition_bits = 0;
Josh Coalson352e0f62001-03-20 22:55:50 +00001326 {
1327#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001328#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001329 partition_bits += (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001330#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001331 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson2051dd42001-04-12 22:22:34 +00001332 partition_bits += (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001333#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001334#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001335 parameters[0] = rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001336 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001337 for(i = 0; i < residual_samples; i++) {
1338#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001339#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001340 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001341#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001342 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001343#endif
1344#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001345 partition_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 +00001346#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001347 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001348#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001349 flat_bits = raw_bits_per_partition[0] * residual_samples;
Josh Coalson2051dd42001-04-12 22:22:34 +00001350 if(flat_bits < partition_bits) {
1351 parameters[0] = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001352 raw_bits[0] = raw_bits_per_partition[0];
Josh Coalson2051dd42001-04-12 22:22:34 +00001353 partition_bits = flat_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001354 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001355#endif
Josh Coalsonaef013c2001-04-24 01:25:42 +00001356 bits_ += partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001357 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001358 }
1359 else {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001360 unsigned i, j, k;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001361 unsigned mean, parameter, partition_samples;
1362 const unsigned max_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001363 const unsigned partitions = 1u << partition_order;
1364 for(i = j = 0; i < partitions; i++) {
Josh Coalson2051dd42001-04-12 22:22:34 +00001365 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001366 partition_samples = (residual_samples+predictor_order) >> partition_order;
1367 if(i == 0) {
1368 if(partition_samples <= predictor_order)
1369 return false;
1370 else
1371 partition_samples -= predictor_order;
1372 }
1373 mean = partition_samples >> 1;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001374#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001375 mean += abs_residual_partition_sums[i];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001376#else
1377 for(k = 0; k < partition_samples; j++, k++)
1378 mean += abs_residual[j];
1379 j -= k;
1380#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001381 mean /= partition_samples;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001382#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson352e0f62001-03-20 22:55:50 +00001383 /* calc parameter = floor(log2(mean)) */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001384 parameter = 0;
1385mean>>=1;
1386 while(mean) {
1387 parameter++;
1388 mean >>= 1;
1389 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001390#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001391 /* calc parameter = floor(log2(mean)) + 1 */
1392 parameter = 0;
1393 while(mean) {
1394 parameter++;
1395 mean >>= 1;
1396 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001397#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001398 if(parameter > max_parameter)
1399 parameter = max_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001400 if(parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1401 parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +00001402
1403#if 0
1404 /*@@@ pending @@@*/
1405 if(rice_parameter_search_dist) {
1406 if(parameter < rice_parameter_search_dist)
1407 min_rice_parameter = 0;
1408 else
1409 min_rice_parameter = parameter - rice_parameter_search_dist;
1410 max_rice_parameter = parameter + rice_parameter_search_dist;
1411 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1412 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1413 }
1414 else
1415 min_rice_parameter = max_rice_parameter = rice_parameter;
1416#endif
1417
Josh Coalson94e02cd2001-01-25 10:41:06 +00001418 parameters[i] = parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001419 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001420#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001421#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001422 partition_bits += (2+parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001423#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001424 partition_bits += (1+parameter) * partition_samples;
Josh Coalson352e0f62001-03-20 22:55:50 +00001425 --parameter;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001426#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001427#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001428 for(k = 0; k < partition_samples; j++, k++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001429#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001430#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001431 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001432#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001433 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], parameter);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001434#endif
1435#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001436 partition_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 +00001437#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001438 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001439#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001440 flat_bits = raw_bits_per_partition[i] * partition_samples;
Josh Coalson2051dd42001-04-12 22:22:34 +00001441 if(flat_bits < partition_bits) {
1442 parameters[i] = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001443 raw_bits[i] = raw_bits_per_partition[i];
Josh Coalson2051dd42001-04-12 22:22:34 +00001444 partition_bits = flat_bits;
1445 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001446#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001447 bits_ += partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001448 }
1449 }
1450
1451 *bits = bits_;
1452 return true;
1453}
Josh Coalson859bc542001-03-27 22:22:27 +00001454
1455static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
1456{
1457 unsigned i, shift;
1458 int32 x = 0;
1459
1460 for(i = 0; i < samples && !(x&1); i++)
1461 x |= signal[i];
1462
1463 if(x == 0) {
1464 shift = 0;
1465 }
1466 else {
1467 for(shift = 0; !(x&1); shift++)
1468 x >>= 1;
1469 }
1470
1471 if(shift > 0) {
1472 for(i = 0; i < samples; i++)
1473 signal[i] >>= shift;
1474 }
1475
1476 return shift;
1477}