blob: a1470ce2817ce8a969c0b1e2c8ffbf71077c2854 [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() */
Josh Coalsoncf30f502001-05-23 20:57:44 +000024#include "FLAC/config.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000025#include "FLAC/encoder.h"
Josh Coalson0a72e182001-04-13 19:17:16 +000026#include "FLAC/seek_table.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000027#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000028#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000029#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000030#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000031#include "private/encoder_framing.h"
32#include "private/fixed.h"
33#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000034#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000035#include "private/memory.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000036
37#ifdef min
38#undef min
39#endif
40#define min(x,y) ((x)<(y)?(x):(y))
41
42#ifdef max
43#undef max
44#endif
45#define max(x,y) ((x)>(y)?(x):(y))
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 Coalson82b73242001-03-28 22:17:05 +000053 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 +000054 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 +000055 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
56 int32 *residual_workspace_mid_side[2][2];
57 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
58 FLAC__Subframe subframe_workspace_mid_side[2][2];
59 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
60 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
61 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
62 unsigned best_subframe_mid_side[2];
63 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
64 unsigned best_subframe_bits_mid_side[2];
Josh Coalson2051dd42001-04-12 22:22:34 +000065 uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000066 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 +000067 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 +000068 FLAC__BitBuffer frame; /* the current frame being worked on */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000069 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 +000070 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
71 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
72 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
73 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000074 FLAC__StreamMetaData metadata;
75 unsigned current_sample_number;
76 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000077 struct MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +000078 FLAC__CPUInfo cpuinfo;
79 unsigned (*local_fixed_compute_best_predictor)(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
80 void (*local_lpc_compute_autocorrelation)(const real data[], unsigned data_len, unsigned lag, real autoc[]);
Josh Coalsoneef56702001-03-30 00:45:22 +000081 bool use_slow; /* use slow 64-bit versions of some functions */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000082 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
83 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
84 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +000085 /* unaligned (original) pointers to allocated data */
86 int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
87 int32 *integer_signal_mid_side_unaligned[2];
88 real *real_signal_unaligned[FLAC__MAX_CHANNELS];
89 real *real_signal_mid_side_unaligned[2];
90 int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
91 int32 *residual_workspace_mid_side_unaligned[2][2];
92 uint32 *abs_residual_unaligned;
93 uint32 *abs_residual_partition_sums_unaligned;
94 unsigned *raw_bits_per_partition_unaligned;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000095} FLAC__EncoderPrivate;
96
97static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
98static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000099static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson60f77d72001-04-25 02:16:36 +0000100static 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 +0000101static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
102static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000103static 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);
104static 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 +0000105static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000106static 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 +0000107#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +0000108static 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 +0000109#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000110static 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, const unsigned suggested_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 +0000111static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000112
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000113const char *FLAC__EncoderWriteStatusString[] = {
114 "FLAC__ENCODER_WRITE_OK",
115 "FLAC__ENCODER_WRITE_FATAL_ERROR"
116};
117
118const char *FLAC__EncoderStateString[] = {
119 "FLAC__ENCODER_OK",
120 "FLAC__ENCODER_UNINITIALIZED",
121 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
122 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
123 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
124 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
125 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
126 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
127 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000128 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000129 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
130 "FLAC__ENCODER_NOT_STREAMABLE",
131 "FLAC__ENCODER_FRAMING_ERROR",
132 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
133 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
134 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
135};
136
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000137
138bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
139{
140 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000141 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000142
143 assert(new_size > 0);
144 assert(encoder->state == FLAC__ENCODER_OK);
145 assert(encoder->guts->current_sample_number == 0);
146
147 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
148 if(new_size <= encoder->guts->input_capacity)
149 return true;
150
Josh Coalsond98c43d2001-05-13 05:17:01 +0000151 ok = true;
152 for(i = 0; ok && i < encoder->channels; i++) {
153 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_unaligned[i], &encoder->guts->integer_signal[i]);
154 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_unaligned[i], &encoder->guts->real_signal[i]);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000155 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000156 for(i = 0; ok && i < 2; i++) {
157 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_mid_side_unaligned[i], &encoder->guts->integer_signal_mid_side[i]);
158 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_mid_side_unaligned[i], &encoder->guts->real_signal_mid_side[i]);
159 }
160 for(channel = 0; ok && channel < encoder->channels; channel++) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000161 for(i = 0; ok && i < 2; i++) {
Josh Coalsond98c43d2001-05-13 05:17:01 +0000162 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_unaligned[channel][i], &encoder->guts->residual_workspace[channel][i]);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000163 }
164 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000165 for(channel = 0; ok && channel < 2; channel++) {
166 for(i = 0; ok && i < 2; i++) {
167 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_mid_side_unaligned[channel][i], &encoder->guts->residual_workspace_mid_side[channel][i]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000168 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000169 }
170 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->guts->abs_residual_unaligned, &encoder->guts->abs_residual);
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000171#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond98c43d2001-05-13 05:17:01 +0000172 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->guts->abs_residual_partition_sums_unaligned, &encoder->guts->abs_residual_partition_sums);
Josh Coalson96611082001-04-24 01:30:10 +0000173#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000174#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsond98c43d2001-05-13 05:17:01 +0000175 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->guts->raw_bits_per_partition_unaligned, &encoder->guts->raw_bits_per_partition);
Josh Coalson96611082001-04-24 01:30:10 +0000176#endif
Josh Coalsond98c43d2001-05-13 05:17:01 +0000177
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000178 if(ok)
179 encoder->guts->input_capacity = new_size;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000180 else
181 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000182
183 return ok;
184}
185
186FLAC__Encoder *FLAC__encoder_get_new_instance()
187{
188 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
189 if(encoder != 0) {
190 encoder->state = FLAC__ENCODER_UNINITIALIZED;
191 encoder->guts = 0;
192 }
193 return encoder;
194}
195
196void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
197{
198 assert(encoder != 0);
199 free(encoder);
200}
201
202FLAC__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)
203{
204 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000205 FLAC__StreamMetaData padding;
Josh Coalson0a72e182001-04-13 19:17:16 +0000206 FLAC__StreamMetaData seek_table;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000207
208 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
209 assert(encoder != 0);
210 assert(write_callback != 0);
211 assert(metadata_callback != 0);
212 assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
213 assert(encoder->guts == 0);
214
215 encoder->state = FLAC__ENCODER_OK;
216
217 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
218 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
219
220 if(encoder->do_mid_side_stereo && encoder->channels != 2)
221 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
222
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000223 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000224 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
225
Josh Coalson82b73242001-03-28 22:17:05 +0000226 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 +0000227 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
228
229 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
230 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
231
232 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
233 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
234
235 if(encoder->blocksize < encoder->max_lpc_order)
236 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
237
238 if(encoder->qlp_coeff_precision == 0) {
239 if(encoder->bits_per_sample < 16) {
240 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
241 /* @@@ until then we'll make a guess */
242 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
243 }
244 else if(encoder->bits_per_sample == 16) {
245 if(encoder->blocksize <= 192)
246 encoder->qlp_coeff_precision = 7;
247 else if(encoder->blocksize <= 384)
248 encoder->qlp_coeff_precision = 8;
249 else if(encoder->blocksize <= 576)
250 encoder->qlp_coeff_precision = 9;
251 else if(encoder->blocksize <= 1152)
252 encoder->qlp_coeff_precision = 10;
253 else if(encoder->blocksize <= 2304)
254 encoder->qlp_coeff_precision = 11;
255 else if(encoder->blocksize <= 4608)
256 encoder->qlp_coeff_precision = 12;
257 else
258 encoder->qlp_coeff_precision = 13;
259 }
260 else {
261 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
262 }
263 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000264 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 +0000265 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
266
267 if(encoder->streamable_subset) {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000268 //@@@ add check for blocksize here
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000269 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)
270 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
271 if(encoder->sample_rate > 655350)
272 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
273 }
274
Josh Coalson60f77d72001-04-25 02:16:36 +0000275 if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
276 encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
277 if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
278 encoder->min_residual_partition_order = encoder->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000279
280 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
281 if(encoder->guts == 0)
282 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
283
284 encoder->guts->input_capacity = 0;
285 for(i = 0; i < encoder->channels; i++) {
286 encoder->guts->integer_signal[i] = 0;
287 encoder->guts->real_signal[i] = 0;
288 }
289 for(i = 0; i < 2; i++) {
290 encoder->guts->integer_signal_mid_side[i] = 0;
291 encoder->guts->real_signal_mid_side[i] = 0;
292 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000293 for(i = 0; i < encoder->channels; i++) {
294 encoder->guts->residual_workspace[i][0] = encoder->guts->residual_workspace[i][1] = 0;
295 encoder->guts->best_subframe[i] = 0;
296 }
297 for(i = 0; i < 2; i++) {
298 encoder->guts->residual_workspace_mid_side[i][0] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
299 encoder->guts->best_subframe_mid_side[i] = 0;
300 }
301 for(i = 0; i < encoder->channels; i++) {
302 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
303 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
304 }
305 for(i = 0; i < 2; i++) {
306 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
307 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
308 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000309 encoder->guts->abs_residual = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000310 encoder->guts->abs_residual_partition_sums = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +0000311 encoder->guts->raw_bits_per_partition = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000312 encoder->guts->current_frame_can_do_mid_side = true;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000313 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
314 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
315 if(encoder->guts->loose_mid_side_stereo_frames == 0)
316 encoder->guts->loose_mid_side_stereo_frames = 1;
317 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000318 encoder->guts->current_sample_number = 0;
319 encoder->guts->current_frame_number = 0;
320
Josh Coalsoncf30f502001-05-23 20:57:44 +0000321 /*
322 * get the CPU info and set the function pointers
323 */
324 FLAC__cpu_info(&encoder->guts->cpuinfo);
325 /* first default to the non-asm routines */
326 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
327 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
328 /* now override with asm where appropriate */
329 if(encoder->guts->cpuinfo.use_asm) {
330#ifdef FLAC__CPU_IA32
331 assert(encoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
332#if 0
333 /* @@@ SSE version not working yet */
334 if(encoder->guts->cpuinfo.data.ia32.sse)
335 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse;
336 else
337#endif
338fprintf(stderr,"@@@ got _asm_i386 of lpc_compute_autocorrelation()\n");
339 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
340 if(encoder->guts->cpuinfo.data.ia32.mmx && encoder->guts->cpuinfo.data.ia32.cmov)
341{
342 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov;
343fprintf(stderr,"@@@ got _asm_i386_mmx_cmov of fixed_compute_best_predictor()\n");}
344#endif
345 }
346
Josh Coalsoneef56702001-03-30 00:45:22 +0000347 if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
348 encoder->guts->use_slow = true;
349 else
350 encoder->guts->use_slow = false;
351
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000352 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
353 /* the above function sets the state for us in case of an error */
354 return encoder->state;
355 }
356 FLAC__bitbuffer_init(&encoder->guts->frame);
357 encoder->guts->write_callback = write_callback;
358 encoder->guts->metadata_callback = metadata_callback;
359 encoder->guts->client_data = client_data;
360
361 /*
362 * write the stream header
363 */
364 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
365 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
366
367 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
368 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
369
Josh Coalsonc692d382001-02-23 21:05:05 +0000370 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson0a72e182001-04-13 19:17:16 +0000371 encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
Josh Coalsonc692d382001-02-23 21:05:05 +0000372 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
373 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
374 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
375 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
376 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
377 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
378 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
379 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
380 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
381 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 +0000382 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000383 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
384 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
385
Josh Coalson0a72e182001-04-13 19:17:16 +0000386 if(0 != encoder->seek_table) {
387 if(!FLAC__seek_table_is_valid(encoder->seek_table))
388 return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
389 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
390 seek_table.is_last = (encoder->padding == 0);
391 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
392 seek_table.data.seek_table = *encoder->seek_table;
393 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
394 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
395 }
396
Josh Coalsonc692d382001-02-23 21:05:05 +0000397 /* add a PADDING block if requested */
398 if(encoder->padding > 0) {
399 padding.type = FLAC__METADATA_TYPE_PADDING;
400 padding.is_last = true;
401 padding.length = encoder->padding;
402 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
403 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
404 }
405
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000406 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
407 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
408 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)
409 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
410
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000411 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000412 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 +0000413 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000414 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000415
416 return encoder->state;
417}
418
419void FLAC__encoder_finish(FLAC__Encoder *encoder)
420{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000421 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000422
423 assert(encoder != 0);
424 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
425 return;
426 if(encoder->guts->current_sample_number != 0) {
427 encoder->blocksize = encoder->guts->current_sample_number;
428 encoder_process_frame_(encoder, true); /* true => is last frame */
429 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000430 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000431 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
432 if(encoder->guts != 0) {
433 for(i = 0; i < encoder->channels; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000434 if(encoder->guts->integer_signal_unaligned[i] != 0) {
435 free(encoder->guts->integer_signal_unaligned[i]);
436 encoder->guts->integer_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000437 }
Josh Coalson18301942001-05-16 19:25:33 +0000438 if(encoder->guts->real_signal_unaligned[i] != 0) {
439 free(encoder->guts->real_signal_unaligned[i]);
440 encoder->guts->real_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000441 }
442 }
443 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000444 if(encoder->guts->integer_signal_mid_side_unaligned[i] != 0) {
445 free(encoder->guts->integer_signal_mid_side_unaligned[i]);
446 encoder->guts->integer_signal_mid_side_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000447 }
Josh Coalson18301942001-05-16 19:25:33 +0000448 if(encoder->guts->real_signal_mid_side_unaligned[i] != 0) {
449 free(encoder->guts->real_signal_mid_side_unaligned[i]);
450 encoder->guts->real_signal_mid_side_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000451 }
452 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000453 for(channel = 0; channel < encoder->channels; channel++) {
454 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000455 if(encoder->guts->residual_workspace_unaligned[channel][i] != 0) {
456 free(encoder->guts->residual_workspace_unaligned[channel][i]);
457 encoder->guts->residual_workspace_unaligned[channel][i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000458 }
459 }
460 }
461 for(channel = 0; channel < 2; channel++) {
462 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000463 if(encoder->guts->residual_workspace_mid_side_unaligned[channel][i] != 0) {
464 free(encoder->guts->residual_workspace_mid_side_unaligned[channel][i]);
465 encoder->guts->residual_workspace_mid_side_unaligned[channel][i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000466 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000467 }
468 }
Josh Coalson18301942001-05-16 19:25:33 +0000469 if(encoder->guts->abs_residual_unaligned != 0) {
470 free(encoder->guts->abs_residual_unaligned);
471 encoder->guts->abs_residual_unaligned = 0;
Josh Coalsone77287e2001-01-20 01:27:55 +0000472 }
Josh Coalson18301942001-05-16 19:25:33 +0000473 if(encoder->guts->abs_residual_partition_sums_unaligned != 0) {
474 free(encoder->guts->abs_residual_partition_sums_unaligned);
475 encoder->guts->abs_residual_partition_sums_unaligned = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000476 }
Josh Coalson18301942001-05-16 19:25:33 +0000477 if(encoder->guts->raw_bits_per_partition_unaligned != 0) {
478 free(encoder->guts->raw_bits_per_partition_unaligned);
479 encoder->guts->raw_bits_per_partition_unaligned = 0;
Josh Coalson2051dd42001-04-12 22:22:34 +0000480 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000481 FLAC__bitbuffer_free(&encoder->guts->frame);
482 free(encoder->guts);
483 encoder->guts = 0;
484 }
485 encoder->state = FLAC__ENCODER_UNINITIALIZED;
486}
487
488bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
489{
490 unsigned i, j, channel;
491 int32 x, mid, side;
492 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
493 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
494 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
495
496 assert(encoder != 0);
497 assert(encoder->state == FLAC__ENCODER_OK);
498
499 j = 0;
500 do {
501 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
502 for(channel = 0; channel < encoder->channels; channel++) {
503 x = buf[channel][j];
504 encoder->guts->integer_signal[channel][i] = x;
505 encoder->guts->real_signal[channel][i] = (real)x;
506 }
507 if(ms && encoder->guts->current_frame_can_do_mid_side) {
508 side = buf[0][j] - buf[1][j];
509 if(side < min_side || side > max_side) {
510 encoder->guts->current_frame_can_do_mid_side = false;
511 }
512 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000513 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 +0000514 encoder->guts->integer_signal_mid_side[0][i] = mid;
515 encoder->guts->integer_signal_mid_side[1][i] = side;
516 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
517 encoder->guts->real_signal_mid_side[1][i] = (real)side;
518 }
519 }
520 encoder->guts->current_sample_number++;
521 }
522 if(i == encoder->blocksize) {
523 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
524 return false;
525 }
526 } while(j < samples);
527
528 return true;
529}
530
531/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
532bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
533{
534 unsigned i, j, k, channel;
535 int32 x, left = 0, mid, side;
536 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
537 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
538 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
539
540 assert(encoder != 0);
541 assert(encoder->state == FLAC__ENCODER_OK);
542
543 j = k = 0;
544 do {
545 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
546 for(channel = 0; channel < encoder->channels; channel++, k++) {
547 x = buf[k];
548 encoder->guts->integer_signal[channel][i] = x;
549 encoder->guts->real_signal[channel][i] = (real)x;
550 if(ms && encoder->guts->current_frame_can_do_mid_side) {
551 if(channel == 0) {
552 left = x;
553 }
554 else {
555 side = left - x;
556 if(side < min_side || side > max_side) {
557 encoder->guts->current_frame_can_do_mid_side = false;
558 }
559 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000560 mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000561 encoder->guts->integer_signal_mid_side[0][i] = mid;
562 encoder->guts->integer_signal_mid_side[1][i] = side;
563 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
564 encoder->guts->real_signal_mid_side[1][i] = (real)side;
565 }
566 }
567 }
568 }
569 encoder->guts->current_sample_number++;
570 }
571 if(i == encoder->blocksize) {
572 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
573 return false;
574 }
575 } while(j < samples);
576
577 return true;
578}
579
580bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
581{
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000582 assert(encoder->state == FLAC__ENCODER_OK);
583
584 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000585 * Accumulate raw signal to the MD5 signature
586 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000587 /* 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 +0000588 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
589 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
590 return false;
591 }
592
593 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000594 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000595 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000596 if(!encoder_process_subframes_(encoder, is_last_frame)) {
597 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000598 return false;
599 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000600
601 /*
602 * Zero-pad the frame to a byte_boundary
603 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000604 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000605 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
606 return false;
607 }
608
609 /*
Josh Coalson215af572001-03-27 01:15:58 +0000610 * CRC-16 the whole thing
611 */
612 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
613 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
614 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
615
616 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000617 * Write it
618 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000619 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 +0000620 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
621 return false;
622 }
623
624 /*
625 * Get ready for the next frame
626 */
627 encoder->guts->current_frame_can_do_mid_side = true;
628 encoder->guts->current_sample_number = 0;
629 encoder->guts->current_frame_number++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000630 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
631 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
632 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 +0000633
634 return true;
635}
636
Josh Coalson94e02cd2001-01-25 10:41:06 +0000637bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
638{
639 FLAC__FrameHeader frame_header;
Josh Coalson034dfab2001-04-27 19:10:23 +0000640 unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000641 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000642
643 /*
Josh Coalson60f77d72001-04-25 02:16:36 +0000644 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +0000645 */
646 if(is_last_frame) {
647 max_partition_order = 0;
648 }
649 else {
650 unsigned limit = 0, b = encoder->blocksize;
651 while(!(b & 1)) {
652 limit++;
653 b >>= 1;
654 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000655 max_partition_order = min(encoder->max_residual_partition_order, limit);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000656 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000657 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000658
659 /*
660 * Setup the frame
661 */
662 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
663 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
664 return false;
665 }
666 frame_header.blocksize = encoder->blocksize;
667 frame_header.sample_rate = encoder->sample_rate;
668 frame_header.channels = encoder->channels;
669 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
670 frame_header.bits_per_sample = encoder->bits_per_sample;
671 frame_header.number.frame_number = encoder->guts->current_frame_number;
672
673 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000674 * Figure out what channel assignments to try
675 */
676 if(encoder->do_mid_side_stereo) {
677 if(encoder->loose_mid_side_stereo) {
678 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
679 do_independent = true;
680 do_mid_side = true;
681 }
682 else {
683 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
684 do_mid_side = !do_independent;
685 }
686 }
687 else {
688 do_independent = true;
689 do_mid_side = true;
690 }
691 }
692 else {
693 do_independent = true;
694 do_mid_side = false;
695 }
696 if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
697 do_independent = true;
698 do_mid_side = false;
699 }
700
701 assert(do_independent || do_mid_side);
702
703 /*
Josh Coalson82b73242001-03-28 22:17:05 +0000704 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +0000705 */
706 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +0000707 unsigned w;
708 for(channel = 0; channel < encoder->channels; channel++) {
709 w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
710 encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
711 encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
712 }
Josh Coalson859bc542001-03-27 22:22:27 +0000713 }
714 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000715 unsigned w;
Josh Coalson859bc542001-03-27 22:22:27 +0000716 assert(encoder->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +0000717 for(channel = 0; channel < 2; channel++) {
718 w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
719 encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
720 encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
721 }
Josh Coalson859bc542001-03-27 22:22:27 +0000722 }
723
724 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000725 * First do a normal encoding pass of each independent channel
726 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000727 if(do_independent) {
728 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000729 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 +0000730 return false;
731 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000732 }
733
734 /*
735 * Now do mid and side channels if requested
736 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000737 if(do_mid_side) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000738 assert(encoder->channels == 2);
739
740 for(channel = 0; channel < 2; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000741 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 +0000742 return false;
743 }
744 }
745
746 /*
747 * Compose the frame bitbuffer
748 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000749 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000750 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
751 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000752 FLAC__ChannelAssignment channel_assignment;
753
Josh Coalson94e02cd2001-01-25 10:41:06 +0000754 assert(encoder->channels == 2);
755
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000756 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
757 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
758 }
759 else {
760 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
761 unsigned min_bits;
762 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000763
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000764 assert(do_independent && do_mid_side);
765
766 /* We have to figure out which channel assignent results in the smallest frame */
767 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
768 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
769 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
770 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
771
772 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
773 if(bits[ca] < min_bits) {
774 min_bits = bits[ca];
775 channel_assignment = ca;
776 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000777 }
778 }
779
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000780 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000781
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
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000787 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000788 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson82b73242001-03-28 22:17:05 +0000789 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
790 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000791 break;
792 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000793 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
794 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000795 break;
796 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000797 left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
798 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000799 break;
800 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000801 left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
802 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000803 break;
804 default:
805 assert(0);
806 }
Josh Coalson82b73242001-03-28 22:17:05 +0000807
808 switch(channel_assignment) {
809 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
810 left_bps = encoder->guts->subframe_bps [0];
811 right_bps = encoder->guts->subframe_bps [1];
812 break;
813 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
814 left_bps = encoder->guts->subframe_bps [0];
815 right_bps = encoder->guts->subframe_bps_mid_side[1];
816 break;
817 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
818 left_bps = encoder->guts->subframe_bps_mid_side[1];
819 right_bps = encoder->guts->subframe_bps [1];
820 break;
821 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
822 left_bps = encoder->guts->subframe_bps_mid_side[0];
823 right_bps = encoder->guts->subframe_bps_mid_side[1];
824 break;
825 default:
826 assert(0);
827 }
828
829 /* note that encoder_add_subframe_ sets the state for us in case of an error */
830 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
831 return false;
832 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
833 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000834 }
835 else {
836 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
837 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
838 return false;
839 }
840
841 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000842 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 +0000843 /* the above function sets the state for us in case of an error */
844 return false;
845 }
846 }
847 }
848
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000849 if(encoder->loose_mid_side_stereo) {
850 encoder->guts->loose_mid_side_stereo_frame_count++;
851 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
852 encoder->guts->loose_mid_side_stereo_frame_count = 0;
853 }
854
855 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
856
Josh Coalson94e02cd2001-01-25 10:41:06 +0000857 return true;
858}
859
Josh Coalson60f77d72001-04-25 02:16:36 +0000860bool 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 +0000861{
862 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
863 real lpc_residual_bits_per_sample;
864 real autoc[FLAC__MAX_LPC_ORDER+1];
865 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
866 real lpc_error[FLAC__MAX_LPC_ORDER];
867 unsigned min_lpc_order, max_lpc_order, lpc_order;
868 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
869 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
870 unsigned rice_parameter;
871 unsigned _candidate_bits, _best_bits;
872 unsigned _best_subframe;
873
874 /* verbatim subframe is the baseline against which we measure other compressed subframes */
875 _best_subframe = 0;
Josh Coalson82b73242001-03-28 22:17:05 +0000876 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000877
878 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
879 /* check for constant subframe */
Josh Coalsoneef56702001-03-30 00:45:22 +0000880 if(encoder->guts->use_slow)
881 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);
882 else
Josh Coalsoncf30f502001-05-23 20:57:44 +0000883 guess_fixed_order = encoder->guts->local_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 +0000884 if(fixed_residual_bits_per_sample[1] == 0.0) {
885 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
886 unsigned i, signal_is_constant = true;
887 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
888 if(integer_signal[0] != integer_signal[i]) {
889 signal_is_constant = false;
890 break;
891 }
892 }
893 if(signal_is_constant) {
Josh Coalson82b73242001-03-28 22:17:05 +0000894 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000895 if(_candidate_bits < _best_bits) {
896 _best_subframe = !_best_subframe;
897 _best_bits = _candidate_bits;
898 }
899 }
900 }
901 else {
902 /* encode fixed */
903 if(encoder->do_exhaustive_model_search) {
904 min_fixed_order = 0;
905 max_fixed_order = FLAC__MAX_FIXED_ORDER;
906 }
907 else {
908 min_fixed_order = max_fixed_order = guess_fixed_order;
909 }
910 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000911 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000912 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000913 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 +0000914#ifndef FLAC__SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000915 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000916#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000917 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
918 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +0000919 _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 +0000920 if(_candidate_bits < _best_bits) {
921 _best_subframe = !_best_subframe;
922 _best_bits = _candidate_bits;
923 }
924 }
925
926 /* encode lpc */
927 if(encoder->max_lpc_order > 0) {
928 if(encoder->max_lpc_order >= frame_header->blocksize)
929 max_lpc_order = frame_header->blocksize-1;
930 else
931 max_lpc_order = encoder->max_lpc_order;
932 if(max_lpc_order > 0) {
Josh Coalsoncf30f502001-05-23 20:57:44 +0000933 encoder->guts->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000934 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
935 if(autoc[0] != 0.0) {
936 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
937 if(encoder->do_exhaustive_model_search) {
938 min_lpc_order = 1;
939 }
940 else {
Josh Coalson82b73242001-03-28 22:17:05 +0000941 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 +0000942 min_lpc_order = max_lpc_order = guess_lpc_order;
943 }
944 if(encoder->do_qlp_coeff_prec_search) {
945 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalson82b73242001-03-28 22:17:05 +0000946 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 +0000947 }
948 else {
949 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
950 }
951 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
952 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 +0000953 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000954 continue; /* don't even try */
955 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 +0000956#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000957 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000958#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000959 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
960 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000961 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 +0000962 _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 +0000963 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
964 if(_candidate_bits < _best_bits) {
965 _best_subframe = !_best_subframe;
966 _best_bits = _candidate_bits;
967 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000968 }
969 }
970 }
971 }
972 }
973 }
974 }
975 }
976
977 *best_subframe = _best_subframe;
978 *best_bits = _best_bits;
979
980 return true;
981}
982
Josh Coalson82b73242001-03-28 22:17:05 +0000983bool 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 +0000984{
985 switch(subframe->type) {
986 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +0000987 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000988 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
989 return false;
990 }
991 break;
992 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +0000993 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 +0000994 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
995 return false;
996 }
997 break;
998 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +0000999 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 +00001000 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1001 return false;
1002 }
1003 break;
1004 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00001005 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001006 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1007 return false;
1008 }
1009 break;
1010 default:
1011 assert(0);
1012 }
1013
1014 return true;
1015}
1016
Josh Coalson82b73242001-03-28 22:17:05 +00001017unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001018{
1019 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1020 subframe->data.constant.value = signal;
1021
Josh Coalson82b73242001-03-28 22:17:05 +00001022 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 +00001023}
1024
Josh Coalson60f77d72001-04-25 02:16:36 +00001025unsigned 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 +00001026{
1027 unsigned i, residual_bits;
1028 const unsigned residual_samples = blocksize - order;
1029
1030 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1031
1032 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1033
1034 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1035 subframe->data.fixed.residual = residual;
1036
Josh Coalson60f77d72001-04-25 02:16:36 +00001037 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 +00001038
1039 subframe->data.fixed.order = order;
1040 for(i = 0; i < order; i++)
1041 subframe->data.fixed.warmup[i] = signal[i];
1042
Josh Coalson82b73242001-03-28 22:17:05 +00001043 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 +00001044}
1045
Josh Coalson60f77d72001-04-25 02:16:36 +00001046unsigned 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 +00001047{
1048 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1049 unsigned i, residual_bits;
1050 int quantization, ret;
1051 const unsigned residual_samples = blocksize - order;
1052
Josh Coalson82b73242001-03-28 22:17:05 +00001053 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001054 if(ret != 0)
1055 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1056
1057 FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1058
1059 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1060
1061 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1062 subframe->data.lpc.residual = residual;
1063
Josh Coalson60f77d72001-04-25 02:16:36 +00001064 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 +00001065
1066 subframe->data.lpc.order = order;
1067 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1068 subframe->data.lpc.quantization_level = quantization;
1069 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1070 for(i = 0; i < order; i++)
1071 subframe->data.lpc.warmup[i] = signal[i];
1072
Josh Coalson82b73242001-03-28 22:17:05 +00001073 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 +00001074}
1075
Josh Coalson82b73242001-03-28 22:17:05 +00001076unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001077{
1078 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1079
1080 subframe->data.verbatim.data = signal;
1081
Josh Coalson82b73242001-03-28 22:17:05 +00001082 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 +00001083}
1084
Josh Coalson60f77d72001-04-25 02:16:36 +00001085unsigned 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 +00001086{
Josh Coalson94e02cd2001-01-25 10:41:06 +00001087 int32 r;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001088#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalsonafcd8772001-04-18 22:59:25 +00001089 unsigned sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001090 int partition_order;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001091#else
1092 unsigned partition_order;
1093#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001094 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001095 unsigned residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001096 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 +00001097
Josh Coalson2051dd42001-04-12 22:22:34 +00001098 /* compute abs(residual) for use later */
1099 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1100 r = residual[residual_sample];
1101 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1102 }
1103
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001104#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001105 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);
1106 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001107
Josh Coalson60f77d72001-04-25 02:16:36 +00001108 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1109 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 +00001110 assert(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001111 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001112 sum += 1u << partition_order;
1113 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1114 best_residual_bits = residual_bits;
1115 *best_partition_order = partition_order;
1116 best_parameters_index = !best_parameters_index;
1117 }
1118 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001119#else
Josh Coalson60f77d72001-04-25 02:16:36 +00001120 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1121 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 +00001122 assert(best_residual_bits != 0);
1123 break;
1124 }
1125 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1126 best_residual_bits = residual_bits;
1127 *best_partition_order = partition_order;
1128 best_parameters_index = !best_parameters_index;
1129 }
1130 }
1131#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001132 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1133 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1134
1135 return best_residual_bits;
1136}
1137
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001138#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001139unsigned 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 +00001140{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001141 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001142 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001143 const unsigned blocksize = residual_samples + predictor_order;
1144
Josh Coalsonaef013c2001-04-24 01:25:42 +00001145 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001146 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001147#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001148 uint32 abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001149#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001150#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001151 uint32 abs_residual_partition_max;
1152 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1153#endif
1154 uint32 abs_r;
1155 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001156 const unsigned partitions = 1u << partition_order;
1157 const unsigned default_partition_samples = blocksize >> partition_order;
1158
1159 if(default_partition_samples <= predictor_order) {
1160 assert(max_partition_order > 0);
1161 max_partition_order--;
1162 }
1163 else {
1164 for(partition = residual_sample = 0; partition < partitions; partition++) {
1165 partition_samples = default_partition_samples;
1166 if(partition == 0)
1167 partition_samples -= predictor_order;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001168#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001169 abs_residual_partition_sum = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001170#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001171#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001172 abs_residual_partition_max = 0;
1173#endif
1174 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1175 abs_r = abs_residual[residual_sample];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001176#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001177 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1178#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001179#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001180 if(abs_r > abs_residual_partition_max) {
1181 abs_residual_partition_max = abs_r;
1182 abs_residual_partition_max_index = residual_sample;
1183 }
1184#endif
1185 residual_sample++;
1186 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001187#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001188 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001189#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001190#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001191 if(abs_residual_partition_max > 0)
1192 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1193 else
1194 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1195#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001196 }
Josh Coalsonaef013c2001-04-24 01:25:42 +00001197 to_partition = partitions;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001198 break;
1199 }
1200 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001201
Josh Coalsonaef013c2001-04-24 01:25:42 +00001202 /* now merge for lower orders */
Josh Coalson034dfab2001-04-27 19:10:23 +00001203 for(from_partition = 0; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001204#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001205 uint32 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001206#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001207#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001208 unsigned m;
1209#endif
1210 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001211 const unsigned partitions = 1u << partition_order;
1212 for(i = 0; i < partitions; i++) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001213#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001214 s = abs_residual_partition_sums[from_partition];
1215#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001216#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001217 m = raw_bits_per_partition[from_partition];
1218#endif
1219 from_partition++;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001220#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001221 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1222#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001223#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001224 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1225#endif
1226 from_partition++;
1227 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001228 }
1229 }
1230
Josh Coalsonf76a3612001-04-18 02:28:11 +00001231 return max_partition_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001232}
Josh Coalsonafcd8772001-04-18 22:59:25 +00001233#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001234
Josh Coalson352e0f62001-03-20 22:55:50 +00001235#ifdef VARIABLE_RICE_BITS
1236#undef VARIABLE_RICE_BITS
1237#endif
1238#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1239
Josh Coalson034dfab2001-04-27 19:10:23 +00001240bool 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, const unsigned suggested_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 +00001241{
Josh Coalson034dfab2001-04-27 19:10:23 +00001242 unsigned rice_parameter, partition_bits;
1243#ifndef NO_RICE_SEARCH
1244 unsigned best_partition_bits;
1245 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1246#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001247#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001248 unsigned flat_bits;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001249#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001250 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1251
Josh Coalson034dfab2001-04-27 19:10:23 +00001252 assert(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00001253
Josh Coalson94e02cd2001-01-25 10:41:06 +00001254 if(partition_order == 0) {
1255 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001256
Josh Coalson034dfab2001-04-27 19:10:23 +00001257#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001258 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001259 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001260 min_rice_parameter = 0;
1261 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001262 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1263 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001264 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1265 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1266 }
1267 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001268 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001269
Josh Coalson034dfab2001-04-27 19:10:23 +00001270 best_partition_bits = 0xffffffff;
1271 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1272#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001273#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001274#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001275 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001276#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001277 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00001278 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001279#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001280#else
1281 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001282#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001283 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001284 for(i = 0; i < residual_samples; i++) {
1285#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001286#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001287 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001288#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001289 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001290#endif
1291#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001292 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 +00001293#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001294 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001295#ifndef NO_RICE_SEARCH
1296 if(partition_bits < best_partition_bits) {
1297 best_rice_parameter = rice_parameter;
1298 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001299 }
1300 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001301#endif
1302#ifdef FLAC__SEARCH_FOR_ESCAPES
1303 flat_bits = raw_bits_per_partition[0] * residual_samples;
1304 if(flat_bits <= best_partition_bits) {
1305 raw_bits[0] = raw_bits_per_partition[0];
1306 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1307 best_partition_bits = flat_bits;
1308 }
1309#endif
1310 parameters[0] = best_rice_parameter;
1311 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001312 }
1313 else {
Josh Coalson034dfab2001-04-27 19:10:23 +00001314 unsigned partition, j, save_j, k;
1315 unsigned mean, partition_samples;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001316 const unsigned partitions = 1u << partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001317 for(partition = j = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001318 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001319 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001320 if(partition_samples <= predictor_order)
1321 return false;
1322 else
1323 partition_samples -= predictor_order;
1324 }
1325 mean = partition_samples >> 1;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001326#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalson034dfab2001-04-27 19:10:23 +00001327 mean += abs_residual_partition_sums[partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001328#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001329 save_j = j;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001330 for(k = 0; k < partition_samples; j++, k++)
1331 mean += abs_residual[j];
Josh Coalson034dfab2001-04-27 19:10:23 +00001332 j = save_j;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001333#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001334 mean /= partition_samples;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001335#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001336 /* calc rice_parameter = floor(log2(mean)) */
1337 rice_parameter = 0;
1338 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001339 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001340 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001341 mean >>= 1;
1342 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001343#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001344 /* calc rice_parameter = floor(log2(mean)) + 1 */
1345 rice_parameter = 0;
Josh Coalson352e0f62001-03-20 22:55:50 +00001346 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001347 rice_parameter++;
Josh Coalson352e0f62001-03-20 22:55:50 +00001348 mean >>= 1;
1349 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001350#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001351 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1352 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +00001353
Josh Coalson034dfab2001-04-27 19:10:23 +00001354#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001355 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001356 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001357 min_rice_parameter = 0;
1358 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001359 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1360 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001361 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1362 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1363 }
1364 else
1365 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00001366
Josh Coalson034dfab2001-04-27 19:10:23 +00001367 best_partition_bits = 0xffffffff;
1368 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1369#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001370#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001371#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001372 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001373#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001374 const unsigned rice_parameter_estimate = rice_parameter-1;
1375 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001376#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001377#else
1378 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001379#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001380 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1381 save_j = j;
1382 for(k = 0; k < partition_samples; j++, k++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001383#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001384#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001385 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001386#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001387 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001388#endif
1389#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001390 partition_bits += FLAC__bitbuffer_rice_bits(residual[j], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001391#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001392 }
1393 if(rice_parameter != max_rice_parameter)
1394 j = save_j;
1395#ifndef NO_RICE_SEARCH
1396 if(partition_bits < best_partition_bits) {
1397 best_rice_parameter = rice_parameter;
1398 best_partition_bits = partition_bits;
1399 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001400 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001401#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001402#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalson034dfab2001-04-27 19:10:23 +00001403 flat_bits = raw_bits_per_partition[partition] * partition_samples;
1404 if(flat_bits <= best_partition_bits) {
1405 raw_bits[partition] = raw_bits_per_partition[partition];
1406 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1407 best_partition_bits = flat_bits;
Josh Coalson2051dd42001-04-12 22:22:34 +00001408 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001409#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001410 parameters[partition] = best_rice_parameter;
1411 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001412 }
1413 }
1414
1415 *bits = bits_;
1416 return true;
1417}
Josh Coalson859bc542001-03-27 22:22:27 +00001418
Josh Coalson8e92f212001-05-01 18:50:45 +00001419unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00001420{
1421 unsigned i, shift;
1422 int32 x = 0;
1423
1424 for(i = 0; i < samples && !(x&1); i++)
1425 x |= signal[i];
1426
1427 if(x == 0) {
1428 shift = 0;
1429 }
1430 else {
1431 for(shift = 0; !(x&1); shift++)
1432 x >>= 1;
1433 }
1434
1435 if(shift > 0) {
1436 for(i = 0; i < samples; i++)
1437 signal[i] >>= shift;
1438 }
1439
1440 return shift;
1441}