blob: 635b5d4d2873af93bff21f9ec86edd468a7f116c [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 Coalsoncf30f502001-05-23 20:57:44 +000029#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000030#include "private/encoder_framing.h"
31#include "private/fixed.h"
32#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000033#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000034#include "private/memory.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000035
36#ifdef min
37#undef min
38#endif
39#define min(x,y) ((x)<(y)?(x):(y))
40
41#ifdef max
42#undef max
43#endif
44#define max(x,y) ((x)>(y)?(x):(y))
45
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000046typedef struct FLAC__EncoderPrivate {
47 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
48 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
49 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
50 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
51 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 +000052 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 +000053 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 +000054 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
55 int32 *residual_workspace_mid_side[2][2];
56 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
57 FLAC__Subframe subframe_workspace_mid_side[2][2];
58 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
59 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
60 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
61 unsigned best_subframe_mid_side[2];
62 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
63 unsigned best_subframe_bits_mid_side[2];
Josh Coalson2051dd42001-04-12 22:22:34 +000064 uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000065 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 +000066 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 +000067 FLAC__BitBuffer frame; /* the current frame being worked on */
Josh Coalsonb5e60e52001-01-28 09:27:27 +000068 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
69 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
70 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
71 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000072 FLAC__StreamMetaData metadata;
73 unsigned current_sample_number;
74 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000075 struct MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +000076 FLAC__CPUInfo cpuinfo;
77 unsigned (*local_fixed_compute_best_predictor)(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
78 void (*local_lpc_compute_autocorrelation)(const real data[], unsigned data_len, unsigned lag, real autoc[]);
Josh Coalson8e833622001-05-29 20:49:51 +000079 void (*local_lpc_compute_residual_from_qlp_coefficients)(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[]);
Josh Coalsoneef56702001-03-30 00:45:22 +000080 bool use_slow; /* use slow 64-bit versions of some functions */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000081 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
82 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
83 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +000084 /* unaligned (original) pointers to allocated data */
85 int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
86 int32 *integer_signal_mid_side_unaligned[2];
87 real *real_signal_unaligned[FLAC__MAX_CHANNELS];
88 real *real_signal_mid_side_unaligned[2];
89 int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
90 int32 *residual_workspace_mid_side_unaligned[2][2];
91 uint32 *abs_residual_unaligned;
92 uint32 *abs_residual_partition_sums_unaligned;
93 unsigned *raw_bits_per_partition_unaligned;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000094} FLAC__EncoderPrivate;
95
96static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
97static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000098static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson60f77d72001-04-25 02:16:36 +000099static 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 +0000100static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
101static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000102static 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);
Josh Coalson8e833622001-05-29 20:49:51 +0000103static unsigned encoder_evaluate_lpc_subframe_(FLAC__Encoder *encoder, 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 +0000104static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000105static 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 +0000106#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +0000107static 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 +0000108#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000109static 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 +0000110static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000111
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000112const char *FLAC__EncoderWriteStatusString[] = {
113 "FLAC__ENCODER_WRITE_OK",
114 "FLAC__ENCODER_WRITE_FATAL_ERROR"
115};
116
117const char *FLAC__EncoderStateString[] = {
118 "FLAC__ENCODER_OK",
119 "FLAC__ENCODER_UNINITIALIZED",
120 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
121 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
122 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
123 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
124 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
125 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
126 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000127 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000128 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
129 "FLAC__ENCODER_NOT_STREAMABLE",
130 "FLAC__ENCODER_FRAMING_ERROR",
131 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
132 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
133 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
134};
135
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000136
137bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
138{
139 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000140 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000141
142 assert(new_size > 0);
143 assert(encoder->state == FLAC__ENCODER_OK);
144 assert(encoder->guts->current_sample_number == 0);
145
146 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
147 if(new_size <= encoder->guts->input_capacity)
148 return true;
149
Josh Coalsond98c43d2001-05-13 05:17:01 +0000150 ok = true;
151 for(i = 0; ok && i < encoder->channels; i++) {
152 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_unaligned[i], &encoder->guts->integer_signal[i]);
153 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 +0000154 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000155 for(i = 0; ok && i < 2; i++) {
156 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]);
157 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]);
158 }
159 for(channel = 0; ok && channel < encoder->channels; channel++) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000160 for(i = 0; ok && i < 2; i++) {
Josh Coalsond98c43d2001-05-13 05:17:01 +0000161 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 +0000162 }
163 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000164 for(channel = 0; ok && channel < 2; channel++) {
165 for(i = 0; ok && i < 2; i++) {
166 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 +0000167 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000168 }
169 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 +0000170#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond98c43d2001-05-13 05:17:01 +0000171 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 +0000172#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000173#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsond98c43d2001-05-13 05:17:01 +0000174 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 +0000175#endif
Josh Coalsond98c43d2001-05-13 05:17:01 +0000176
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000177 if(ok)
178 encoder->guts->input_capacity = new_size;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000179 else
180 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000181
182 return ok;
183}
184
185FLAC__Encoder *FLAC__encoder_get_new_instance()
186{
187 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
188 if(encoder != 0) {
189 encoder->state = FLAC__ENCODER_UNINITIALIZED;
190 encoder->guts = 0;
191 }
192 return encoder;
193}
194
195void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
196{
197 assert(encoder != 0);
198 free(encoder);
199}
200
201FLAC__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)
202{
203 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000204 FLAC__StreamMetaData padding;
Josh Coalson0a72e182001-04-13 19:17:16 +0000205 FLAC__StreamMetaData seek_table;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000206
207 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
208 assert(encoder != 0);
209 assert(write_callback != 0);
210 assert(metadata_callback != 0);
211 assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
212 assert(encoder->guts == 0);
213
214 encoder->state = FLAC__ENCODER_OK;
215
216 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
217 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
218
219 if(encoder->do_mid_side_stereo && encoder->channels != 2)
220 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
221
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000222 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000223 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
224
Josh Coalsond37d1352001-05-30 23:09:31 +0000225 if(encoder->bits_per_sample >= 32)
226 encoder->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
227
Josh Coalson82b73242001-03-28 22:17:05 +0000228 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 +0000229 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
230
231 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
232 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
233
234 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
235 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
236
237 if(encoder->blocksize < encoder->max_lpc_order)
238 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
239
240 if(encoder->qlp_coeff_precision == 0) {
241 if(encoder->bits_per_sample < 16) {
242 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
243 /* @@@ until then we'll make a guess */
244 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
245 }
246 else if(encoder->bits_per_sample == 16) {
247 if(encoder->blocksize <= 192)
248 encoder->qlp_coeff_precision = 7;
249 else if(encoder->blocksize <= 384)
250 encoder->qlp_coeff_precision = 8;
251 else if(encoder->blocksize <= 576)
252 encoder->qlp_coeff_precision = 9;
253 else if(encoder->blocksize <= 1152)
254 encoder->qlp_coeff_precision = 10;
255 else if(encoder->blocksize <= 2304)
256 encoder->qlp_coeff_precision = 11;
257 else if(encoder->blocksize <= 4608)
258 encoder->qlp_coeff_precision = 12;
259 else
260 encoder->qlp_coeff_precision = 13;
261 }
262 else {
263 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
264 }
265 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000266 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 +0000267 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
268
269 if(encoder->streamable_subset) {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000270 //@@@ add check for blocksize here
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000271 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)
272 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
273 if(encoder->sample_rate > 655350)
274 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
275 }
276
Josh Coalson60f77d72001-04-25 02:16:36 +0000277 if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
278 encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
279 if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
280 encoder->min_residual_partition_order = encoder->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000281
282 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
283 if(encoder->guts == 0)
284 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
285
286 encoder->guts->input_capacity = 0;
287 for(i = 0; i < encoder->channels; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000288 encoder->guts->integer_signal_unaligned[i] = encoder->guts->integer_signal[i] = 0;
289 encoder->guts->real_signal_unaligned[i] = encoder->guts->real_signal[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000290 }
291 for(i = 0; i < 2; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000292 encoder->guts->integer_signal_mid_side_unaligned[i] = encoder->guts->integer_signal_mid_side[i] = 0;
293 encoder->guts->real_signal_mid_side_unaligned[i] = encoder->guts->real_signal_mid_side[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000294 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000295 for(i = 0; i < encoder->channels; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000296 encoder->guts->residual_workspace_unaligned[i][0] = encoder->guts->residual_workspace[i][0] = 0;
297 encoder->guts->residual_workspace_unaligned[i][1] = encoder->guts->residual_workspace[i][1] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000298 encoder->guts->best_subframe[i] = 0;
299 }
300 for(i = 0; i < 2; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000301 encoder->guts->residual_workspace_mid_side_unaligned[i][0] = encoder->guts->residual_workspace_mid_side[i][0] = 0;
302 encoder->guts->residual_workspace_mid_side_unaligned[i][1] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000303 encoder->guts->best_subframe_mid_side[i] = 0;
304 }
305 for(i = 0; i < encoder->channels; i++) {
306 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
307 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
308 }
309 for(i = 0; i < 2; i++) {
310 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
311 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
312 }
Josh Coalson6bd17572001-05-25 19:02:01 +0000313 encoder->guts->abs_residual_unaligned = encoder->guts->abs_residual = 0;
314 encoder->guts->abs_residual_partition_sums_unaligned = encoder->guts->abs_residual_partition_sums = 0;
315 encoder->guts->raw_bits_per_partition_unaligned = encoder->guts->raw_bits_per_partition = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000316 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
317 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
318 if(encoder->guts->loose_mid_side_stereo_frames == 0)
319 encoder->guts->loose_mid_side_stereo_frames = 1;
320 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000321 encoder->guts->current_sample_number = 0;
322 encoder->guts->current_frame_number = 0;
323
Josh Coalsoncf30f502001-05-23 20:57:44 +0000324 /*
325 * get the CPU info and set the function pointers
326 */
327 FLAC__cpu_info(&encoder->guts->cpuinfo);
328 /* first default to the non-asm routines */
329 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
330 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson8e833622001-05-29 20:49:51 +0000331 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000332 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000333#ifndef FLAC__NO_ASM
334 assert(encoder->guts->cpuinfo.use_asm);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000335#ifdef FLAC__CPU_IA32
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000336 assert(encoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000337#ifdef FLAC__HAS_NASM
Josh Coalsoncf30f502001-05-23 20:57:44 +0000338#if 0
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000339 /* @@@ SSE version not working yet */
Josh Coalsonaa255362001-05-31 06:17:41 +0000340 if(encoder->guts->cpuinfo.data.ia32.sse) {
341 if(encoder->max_lpc_order < 4)
Josh Coalsoneae92172001-05-30 19:20:36 +0000342{//@@@
Josh Coalsonaa255362001-05-31 06:17:41 +0000343 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_4;
344fprintf(stderr,"@@@ got _asm_i386_sse_4 of lpc_compute_autocorrelation()\n");}
345 else if(encoder->max_lpc_order < 8)
346{//@@@
347 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_8;
348fprintf(stderr,"@@@ got _asm_i386_sse_8 of lpc_compute_autocorrelation()\n");}
349 else if(encoder->max_lpc_order < 12)
350{//@@@
351 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_12;
352fprintf(stderr,"@@@ got _asm_i386_sse_12 of lpc_compute_autocorrelation()\n");}
353 else
354{//@@@
355 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
356fprintf(stderr,"@@@ got _asm_i386 of lpc_compute_autocorrelation()\n");}
357 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000358 else
Josh Coalsoncf30f502001-05-23 20:57:44 +0000359#endif
Josh Coalsoneae92172001-05-30 19:20:36 +0000360{//@@@
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000361 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
Josh Coalsoneae92172001-05-30 19:20:36 +0000362fprintf(stderr,"@@@ got _asm_i386 of lpc_compute_autocorrelation()\n");}
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000363 if(encoder->guts->cpuinfo.data.ia32.mmx && encoder->guts->cpuinfo.data.ia32.cmov)
Josh Coalsoneae92172001-05-30 19:20:36 +0000364{//@@@
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000365 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000366fprintf(stderr,"@@@ got _asm_i386_mmx_cmov of fixed_compute_best_predictor()\n");}
Josh Coalson8e833622001-05-29 20:49:51 +0000367#if 0
368 /* @@@ MMX version needs bps check */
Josh Coalsoneae92172001-05-30 19:20:36 +0000369 if(encoder->guts->cpuinfo.data.ia32.mmx && @@@bps check here@@@)
370{//@@@
Josh Coalson8e833622001-05-29 20:49:51 +0000371 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386_mmx;
Josh Coalsoneae92172001-05-30 19:20:36 +0000372fprintf(stderr,"@@@ got _asm_i386_mmx of lpc_compute_residual_from_qlp_coefficients()\n");}
Josh Coalson8e833622001-05-29 20:49:51 +0000373 else
374#endif
Josh Coalsoneae92172001-05-30 19:20:36 +0000375{//@@@
376 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
Josh Coalson8e833622001-05-29 20:49:51 +0000377fprintf(stderr,"@@@ got _asm_i386 of lpc_compute_residual_from_qlp_coefficients()\n");}
Josh Coalsoncf30f502001-05-23 20:57:44 +0000378#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000379#endif
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000380#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000381
Josh Coalsoneef56702001-03-30 00:45:22 +0000382 if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
383 encoder->guts->use_slow = true;
384 else
385 encoder->guts->use_slow = false;
386
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000387 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
388 /* the above function sets the state for us in case of an error */
389 return encoder->state;
390 }
391 FLAC__bitbuffer_init(&encoder->guts->frame);
392 encoder->guts->write_callback = write_callback;
393 encoder->guts->metadata_callback = metadata_callback;
394 encoder->guts->client_data = client_data;
395
396 /*
397 * write the stream header
398 */
399 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
400 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
401
402 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
403 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
404
Josh Coalsonc692d382001-02-23 21:05:05 +0000405 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson0a72e182001-04-13 19:17:16 +0000406 encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
Josh Coalsonc692d382001-02-23 21:05:05 +0000407 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
408 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
409 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
410 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
411 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
412 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
413 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
414 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
415 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
416 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 +0000417 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000418 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
419 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
420
Josh Coalson0a72e182001-04-13 19:17:16 +0000421 if(0 != encoder->seek_table) {
422 if(!FLAC__seek_table_is_valid(encoder->seek_table))
423 return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
424 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
425 seek_table.is_last = (encoder->padding == 0);
426 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
427 seek_table.data.seek_table = *encoder->seek_table;
428 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
429 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
430 }
431
Josh Coalsonc692d382001-02-23 21:05:05 +0000432 /* add a PADDING block if requested */
433 if(encoder->padding > 0) {
434 padding.type = FLAC__METADATA_TYPE_PADDING;
435 padding.is_last = true;
436 padding.length = encoder->padding;
437 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
438 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
439 }
440
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000441 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
442 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
443 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)
444 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
445
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000446 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000447 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 +0000448 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000449 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000450
451 return encoder->state;
452}
453
454void FLAC__encoder_finish(FLAC__Encoder *encoder)
455{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000456 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000457
458 assert(encoder != 0);
459 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
460 return;
461 if(encoder->guts->current_sample_number != 0) {
462 encoder->blocksize = encoder->guts->current_sample_number;
463 encoder_process_frame_(encoder, true); /* true => is last frame */
464 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000465 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000466 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
467 if(encoder->guts != 0) {
468 for(i = 0; i < encoder->channels; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000469 if(encoder->guts->integer_signal_unaligned[i] != 0) {
470 free(encoder->guts->integer_signal_unaligned[i]);
471 encoder->guts->integer_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000472 }
Josh Coalson18301942001-05-16 19:25:33 +0000473 if(encoder->guts->real_signal_unaligned[i] != 0) {
474 free(encoder->guts->real_signal_unaligned[i]);
475 encoder->guts->real_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000476 }
477 }
478 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000479 if(encoder->guts->integer_signal_mid_side_unaligned[i] != 0) {
480 free(encoder->guts->integer_signal_mid_side_unaligned[i]);
481 encoder->guts->integer_signal_mid_side_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000482 }
Josh Coalson18301942001-05-16 19:25:33 +0000483 if(encoder->guts->real_signal_mid_side_unaligned[i] != 0) {
484 free(encoder->guts->real_signal_mid_side_unaligned[i]);
485 encoder->guts->real_signal_mid_side_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000486 }
487 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000488 for(channel = 0; channel < encoder->channels; channel++) {
489 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000490 if(encoder->guts->residual_workspace_unaligned[channel][i] != 0) {
491 free(encoder->guts->residual_workspace_unaligned[channel][i]);
492 encoder->guts->residual_workspace_unaligned[channel][i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000493 }
494 }
495 }
496 for(channel = 0; channel < 2; channel++) {
497 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000498 if(encoder->guts->residual_workspace_mid_side_unaligned[channel][i] != 0) {
499 free(encoder->guts->residual_workspace_mid_side_unaligned[channel][i]);
500 encoder->guts->residual_workspace_mid_side_unaligned[channel][i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000501 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000502 }
503 }
Josh Coalson18301942001-05-16 19:25:33 +0000504 if(encoder->guts->abs_residual_unaligned != 0) {
505 free(encoder->guts->abs_residual_unaligned);
506 encoder->guts->abs_residual_unaligned = 0;
Josh Coalsone77287e2001-01-20 01:27:55 +0000507 }
Josh Coalson18301942001-05-16 19:25:33 +0000508 if(encoder->guts->abs_residual_partition_sums_unaligned != 0) {
509 free(encoder->guts->abs_residual_partition_sums_unaligned);
510 encoder->guts->abs_residual_partition_sums_unaligned = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000511 }
Josh Coalson18301942001-05-16 19:25:33 +0000512 if(encoder->guts->raw_bits_per_partition_unaligned != 0) {
513 free(encoder->guts->raw_bits_per_partition_unaligned);
514 encoder->guts->raw_bits_per_partition_unaligned = 0;
Josh Coalson2051dd42001-04-12 22:22:34 +0000515 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000516 FLAC__bitbuffer_free(&encoder->guts->frame);
517 free(encoder->guts);
518 encoder->guts = 0;
519 }
520 encoder->state = FLAC__ENCODER_UNINITIALIZED;
521}
522
523bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
524{
525 unsigned i, j, channel;
526 int32 x, mid, side;
Josh Coalsond37d1352001-05-30 23:09:31 +0000527 const unsigned channels = encoder->channels, blocksize = encoder->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000528
529 assert(encoder != 0);
530 assert(encoder->state == FLAC__ENCODER_OK);
531
532 j = 0;
Josh Coalsonaa255362001-05-31 06:17:41 +0000533 if(encoder->do_mid_side_stereo && channels == 2) {
534 do {
535 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
536 x = mid = side = buf[0][j];
537 encoder->guts->integer_signal[0][i] = x;
538 encoder->guts->real_signal[0][i] = (real)x;
539 x = buf[1][j];
540 encoder->guts->integer_signal[1][i] = x;
541 encoder->guts->real_signal[1][i] = (real)x;
542 mid += x;
543 side -= x;
544 mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
545 encoder->guts->integer_signal_mid_side[1][i] = side;
546 encoder->guts->integer_signal_mid_side[0][i] = mid;
547 encoder->guts->real_signal_mid_side[1][i] = (real)side;
548 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
549 encoder->guts->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000550 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000551 if(i == blocksize) {
552 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
553 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000554 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000555 } while(j < samples);
556 }
557 else {
558 do {
559 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
560 for(channel = 0; channel < channels; channel++) {
561 x = buf[channel][j];
562 encoder->guts->integer_signal[channel][i] = x;
563 encoder->guts->real_signal[channel][i] = (real)x;
564 }
565 encoder->guts->current_sample_number++;
566 }
567 if(i == blocksize) {
568 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
569 return false;
570 }
571 } while(j < samples);
572 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000573
574 return true;
575}
576
577/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
578bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
579{
580 unsigned i, j, k, channel;
Josh Coalsonaa255362001-05-31 06:17:41 +0000581 int32 x, mid, side;
582 const unsigned channels = encoder->channels, blocksize = encoder->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000583
584 assert(encoder != 0);
585 assert(encoder->state == FLAC__ENCODER_OK);
586
587 j = k = 0;
Josh Coalsonaa255362001-05-31 06:17:41 +0000588 if(encoder->do_mid_side_stereo && channels == 2) {
589 do {
590 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
591 x = mid = side = buf[k++];
592 encoder->guts->integer_signal[0][i] = x;
593 encoder->guts->real_signal[0][i] = (real)x;
Josh Coalsond37d1352001-05-30 23:09:31 +0000594 x = buf[k++];
Josh Coalsonaa255362001-05-31 06:17:41 +0000595 encoder->guts->integer_signal[1][i] = x;
596 encoder->guts->real_signal[1][i] = (real)x;
597 mid += x;
598 side -= x;
599 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
600 encoder->guts->integer_signal_mid_side[1][i] = side;
601 encoder->guts->integer_signal_mid_side[0][i] = mid;
602 encoder->guts->real_signal_mid_side[1][i] = (real)side;
603 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
604 encoder->guts->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000605 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000606 if(i == blocksize) {
607 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
608 return false;
609 }
610 } while(j < samples);
611 }
612 else {
613 do {
614 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
615 for(channel = 0; channel < channels; channel++) {
616 x = buf[k++];
617 encoder->guts->integer_signal[channel][i] = x;
618 encoder->guts->real_signal[channel][i] = (real)x;
619 }
620 encoder->guts->current_sample_number++;
621 }
622 if(i == blocksize) {
623 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
624 return false;
625 }
626 } while(j < samples);
627 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000628
629 return true;
630}
631
632bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
633{
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000634 assert(encoder->state == FLAC__ENCODER_OK);
635
636 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000637 * Accumulate raw signal to the MD5 signature
638 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000639 /* 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 +0000640 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
641 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
642 return false;
643 }
644
645 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000646 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000647 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000648 if(!encoder_process_subframes_(encoder, is_last_frame)) {
649 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000650 return false;
651 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000652
653 /*
654 * Zero-pad the frame to a byte_boundary
655 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000656 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000657 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
658 return false;
659 }
660
661 /*
Josh Coalson215af572001-03-27 01:15:58 +0000662 * CRC-16 the whole thing
663 */
664 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
665 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
666 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
667
668 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000669 * Write it
670 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000671 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 +0000672 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
673 return false;
674 }
675
676 /*
677 * Get ready for the next frame
678 */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000679 encoder->guts->current_sample_number = 0;
680 encoder->guts->current_frame_number++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000681 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
682 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
683 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 +0000684
685 return true;
686}
687
Josh Coalson94e02cd2001-01-25 10:41:06 +0000688bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
689{
690 FLAC__FrameHeader frame_header;
Josh Coalson034dfab2001-04-27 19:10:23 +0000691 unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000692 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000693
694 /*
Josh Coalson60f77d72001-04-25 02:16:36 +0000695 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +0000696 */
697 if(is_last_frame) {
698 max_partition_order = 0;
699 }
700 else {
701 unsigned limit = 0, b = encoder->blocksize;
702 while(!(b & 1)) {
703 limit++;
704 b >>= 1;
705 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000706 max_partition_order = min(encoder->max_residual_partition_order, limit);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000707 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000708 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000709
710 /*
711 * Setup the frame
712 */
713 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
714 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
715 return false;
716 }
717 frame_header.blocksize = encoder->blocksize;
718 frame_header.sample_rate = encoder->sample_rate;
719 frame_header.channels = encoder->channels;
720 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
721 frame_header.bits_per_sample = encoder->bits_per_sample;
722 frame_header.number.frame_number = encoder->guts->current_frame_number;
723
724 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000725 * Figure out what channel assignments to try
726 */
727 if(encoder->do_mid_side_stereo) {
728 if(encoder->loose_mid_side_stereo) {
729 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
730 do_independent = true;
731 do_mid_side = true;
732 }
733 else {
734 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
735 do_mid_side = !do_independent;
736 }
737 }
738 else {
739 do_independent = true;
740 do_mid_side = true;
741 }
742 }
743 else {
744 do_independent = true;
745 do_mid_side = false;
746 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000747
748 assert(do_independent || do_mid_side);
749
750 /*
Josh Coalson82b73242001-03-28 22:17:05 +0000751 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +0000752 */
753 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +0000754 unsigned w;
755 for(channel = 0; channel < encoder->channels; channel++) {
756 w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
757 encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
758 encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
759 }
Josh Coalson859bc542001-03-27 22:22:27 +0000760 }
761 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000762 unsigned w;
Josh Coalson859bc542001-03-27 22:22:27 +0000763 assert(encoder->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +0000764 for(channel = 0; channel < 2; channel++) {
765 w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
766 encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
767 encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
768 }
Josh Coalson859bc542001-03-27 22:22:27 +0000769 }
770
771 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000772 * First do a normal encoding pass of each independent channel
773 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000774 if(do_independent) {
775 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000776 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 +0000777 return false;
778 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000779 }
780
781 /*
782 * Now do mid and side channels if requested
783 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000784 if(do_mid_side) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000785 assert(encoder->channels == 2);
786
787 for(channel = 0; channel < 2; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000788 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 +0000789 return false;
790 }
791 }
792
793 /*
794 * Compose the frame bitbuffer
795 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000796 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000797 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
798 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000799 FLAC__ChannelAssignment channel_assignment;
800
Josh Coalson94e02cd2001-01-25 10:41:06 +0000801 assert(encoder->channels == 2);
802
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000803 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
804 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
805 }
806 else {
807 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
808 unsigned min_bits;
809 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000810
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000811 assert(do_independent && do_mid_side);
812
813 /* We have to figure out which channel assignent results in the smallest frame */
814 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
815 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
816 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
817 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
818
819 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
820 if(bits[ca] < min_bits) {
821 min_bits = bits[ca];
822 channel_assignment = ca;
823 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000824 }
825 }
826
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000827 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000828
829 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
830 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
831 return false;
832 }
833
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000834 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000835 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson82b73242001-03-28 22:17:05 +0000836 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
837 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000838 break;
839 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000840 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
841 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000842 break;
843 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000844 left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
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_MID_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000848 left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[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 default:
852 assert(0);
853 }
Josh Coalson82b73242001-03-28 22:17:05 +0000854
855 switch(channel_assignment) {
856 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
857 left_bps = encoder->guts->subframe_bps [0];
858 right_bps = encoder->guts->subframe_bps [1];
859 break;
860 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
861 left_bps = encoder->guts->subframe_bps [0];
862 right_bps = encoder->guts->subframe_bps_mid_side[1];
863 break;
864 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
865 left_bps = encoder->guts->subframe_bps_mid_side[1];
866 right_bps = encoder->guts->subframe_bps [1];
867 break;
868 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
869 left_bps = encoder->guts->subframe_bps_mid_side[0];
870 right_bps = encoder->guts->subframe_bps_mid_side[1];
871 break;
872 default:
873 assert(0);
874 }
875
876 /* note that encoder_add_subframe_ sets the state for us in case of an error */
877 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
878 return false;
879 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
880 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000881 }
882 else {
883 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
884 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
885 return false;
886 }
887
888 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000889 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 +0000890 /* the above function sets the state for us in case of an error */
891 return false;
892 }
893 }
894 }
895
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000896 if(encoder->loose_mid_side_stereo) {
897 encoder->guts->loose_mid_side_stereo_frame_count++;
898 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
899 encoder->guts->loose_mid_side_stereo_frame_count = 0;
900 }
901
902 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
903
Josh Coalson94e02cd2001-01-25 10:41:06 +0000904 return true;
905}
906
Josh Coalson60f77d72001-04-25 02:16:36 +0000907bool 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 +0000908{
909 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
910 real lpc_residual_bits_per_sample;
Josh Coalsond16370d2001-05-31 05:56:41 +0000911 real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->max_lpc_order might be less; some asm routines need all the space */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000912 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
913 real lpc_error[FLAC__MAX_LPC_ORDER];
914 unsigned min_lpc_order, max_lpc_order, lpc_order;
915 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
916 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
917 unsigned rice_parameter;
918 unsigned _candidate_bits, _best_bits;
919 unsigned _best_subframe;
920
921 /* verbatim subframe is the baseline against which we measure other compressed subframes */
922 _best_subframe = 0;
Josh Coalson82b73242001-03-28 22:17:05 +0000923 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000924
925 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
926 /* check for constant subframe */
Josh Coalsoneef56702001-03-30 00:45:22 +0000927 if(encoder->guts->use_slow)
928 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);
929 else
Josh Coalsoncf30f502001-05-23 20:57:44 +0000930 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 +0000931 if(fixed_residual_bits_per_sample[1] == 0.0) {
932 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
933 unsigned i, signal_is_constant = true;
934 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
935 if(integer_signal[0] != integer_signal[i]) {
936 signal_is_constant = false;
937 break;
938 }
939 }
940 if(signal_is_constant) {
Josh Coalson82b73242001-03-28 22:17:05 +0000941 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000942 if(_candidate_bits < _best_bits) {
943 _best_subframe = !_best_subframe;
944 _best_bits = _candidate_bits;
945 }
946 }
947 }
948 else {
949 /* encode fixed */
950 if(encoder->do_exhaustive_model_search) {
951 min_fixed_order = 0;
952 max_fixed_order = FLAC__MAX_FIXED_ORDER;
953 }
954 else {
955 min_fixed_order = max_fixed_order = guess_fixed_order;
956 }
957 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000958 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000959 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000960 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 +0000961#ifndef FLAC__SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000962 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000963#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000964 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
965 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +0000966 _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 +0000967 if(_candidate_bits < _best_bits) {
968 _best_subframe = !_best_subframe;
969 _best_bits = _candidate_bits;
970 }
971 }
972
973 /* encode lpc */
974 if(encoder->max_lpc_order > 0) {
975 if(encoder->max_lpc_order >= frame_header->blocksize)
976 max_lpc_order = frame_header->blocksize-1;
977 else
978 max_lpc_order = encoder->max_lpc_order;
979 if(max_lpc_order > 0) {
Josh Coalsoncf30f502001-05-23 20:57:44 +0000980 encoder->guts->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000981 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
982 if(autoc[0] != 0.0) {
983 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
984 if(encoder->do_exhaustive_model_search) {
985 min_lpc_order = 1;
986 }
987 else {
Josh Coalson82b73242001-03-28 22:17:05 +0000988 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 +0000989 min_lpc_order = max_lpc_order = guess_lpc_order;
990 }
991 if(encoder->do_qlp_coeff_prec_search) {
992 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalson82b73242001-03-28 22:17:05 +0000993 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 +0000994 }
995 else {
996 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
997 }
998 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
999 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 +00001000 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001001 continue; /* don't even try */
1002 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 +00001003#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001004 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001005#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001006 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1007 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001008 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
Josh Coalson8e833622001-05-29 20:49:51 +00001009 _candidate_bits = encoder_evaluate_lpc_subframe_(encoder, 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 +00001010 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1011 if(_candidate_bits < _best_bits) {
1012 _best_subframe = !_best_subframe;
1013 _best_bits = _candidate_bits;
1014 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001015 }
1016 }
1017 }
1018 }
1019 }
1020 }
1021 }
1022 }
1023
1024 *best_subframe = _best_subframe;
1025 *best_bits = _best_bits;
1026
1027 return true;
1028}
1029
Josh Coalson82b73242001-03-28 22:17:05 +00001030bool 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 +00001031{
1032 switch(subframe->type) {
1033 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00001034 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001035 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1036 return false;
1037 }
1038 break;
1039 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00001040 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 +00001041 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1042 return false;
1043 }
1044 break;
1045 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00001046 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 +00001047 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1048 return false;
1049 }
1050 break;
1051 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00001052 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001053 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1054 return false;
1055 }
1056 break;
1057 default:
1058 assert(0);
1059 }
1060
1061 return true;
1062}
1063
Josh Coalson82b73242001-03-28 22:17:05 +00001064unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001065{
1066 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1067 subframe->data.constant.value = signal;
1068
Josh Coalson82b73242001-03-28 22:17:05 +00001069 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 +00001070}
1071
Josh Coalson60f77d72001-04-25 02:16:36 +00001072unsigned 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 +00001073{
1074 unsigned i, residual_bits;
1075 const unsigned residual_samples = blocksize - order;
1076
1077 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1078
1079 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1080
1081 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1082 subframe->data.fixed.residual = residual;
1083
Josh Coalson60f77d72001-04-25 02:16:36 +00001084 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 +00001085
1086 subframe->data.fixed.order = order;
1087 for(i = 0; i < order; i++)
1088 subframe->data.fixed.warmup[i] = signal[i];
1089
Josh Coalson82b73242001-03-28 22:17:05 +00001090 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 +00001091}
1092
Josh Coalson8e833622001-05-29 20:49:51 +00001093unsigned encoder_evaluate_lpc_subframe_(FLAC__Encoder *encoder, 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 +00001094{
1095 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1096 unsigned i, residual_bits;
1097 int quantization, ret;
1098 const unsigned residual_samples = blocksize - order;
1099
Josh Coalson82b73242001-03-28 22:17:05 +00001100 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001101 if(ret != 0)
1102 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1103
Josh Coalson8e833622001-05-29 20:49:51 +00001104 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001105
1106 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1107
1108 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1109 subframe->data.lpc.residual = residual;
1110
Josh Coalson60f77d72001-04-25 02:16:36 +00001111 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 +00001112
1113 subframe->data.lpc.order = order;
1114 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1115 subframe->data.lpc.quantization_level = quantization;
1116 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1117 for(i = 0; i < order; i++)
1118 subframe->data.lpc.warmup[i] = signal[i];
1119
Josh Coalson82b73242001-03-28 22:17:05 +00001120 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 +00001121}
1122
Josh Coalson82b73242001-03-28 22:17:05 +00001123unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001124{
1125 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1126
1127 subframe->data.verbatim.data = signal;
1128
Josh Coalson82b73242001-03-28 22:17:05 +00001129 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 +00001130}
1131
Josh Coalson60f77d72001-04-25 02:16:36 +00001132unsigned 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 +00001133{
Josh Coalson94e02cd2001-01-25 10:41:06 +00001134 int32 r;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001135#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalsonafcd8772001-04-18 22:59:25 +00001136 unsigned sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001137 int partition_order;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001138#else
1139 unsigned partition_order;
1140#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001141 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001142 unsigned residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001143 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 +00001144
Josh Coalson2051dd42001-04-12 22:22:34 +00001145 /* compute abs(residual) for use later */
1146 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1147 r = residual[residual_sample];
1148 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1149 }
1150
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001151#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001152 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);
1153 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001154
Josh Coalson60f77d72001-04-25 02:16:36 +00001155 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1156 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 +00001157 assert(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001158 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001159 sum += 1u << partition_order;
1160 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1161 best_residual_bits = residual_bits;
1162 *best_partition_order = partition_order;
1163 best_parameters_index = !best_parameters_index;
1164 }
1165 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001166#else
Josh Coalson60f77d72001-04-25 02:16:36 +00001167 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1168 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 +00001169 assert(best_residual_bits != 0);
1170 break;
1171 }
1172 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1173 best_residual_bits = residual_bits;
1174 *best_partition_order = partition_order;
1175 best_parameters_index = !best_parameters_index;
1176 }
1177 }
1178#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001179 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1180 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1181
1182 return best_residual_bits;
1183}
1184
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001185#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001186unsigned 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 +00001187{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001188 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001189 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001190 const unsigned blocksize = residual_samples + predictor_order;
1191
Josh Coalsonaef013c2001-04-24 01:25:42 +00001192 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001193 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001194#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001195 uint32 abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001196#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001197#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001198 uint32 abs_residual_partition_max;
1199 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1200#endif
1201 uint32 abs_r;
1202 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001203 const unsigned partitions = 1u << partition_order;
1204 const unsigned default_partition_samples = blocksize >> partition_order;
1205
1206 if(default_partition_samples <= predictor_order) {
1207 assert(max_partition_order > 0);
1208 max_partition_order--;
1209 }
1210 else {
1211 for(partition = residual_sample = 0; partition < partitions; partition++) {
1212 partition_samples = default_partition_samples;
1213 if(partition == 0)
1214 partition_samples -= predictor_order;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001215#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001216 abs_residual_partition_sum = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001217#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001218#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001219 abs_residual_partition_max = 0;
1220#endif
1221 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1222 abs_r = abs_residual[residual_sample];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001223#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001224 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1225#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001226#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001227 if(abs_r > abs_residual_partition_max) {
1228 abs_residual_partition_max = abs_r;
1229 abs_residual_partition_max_index = residual_sample;
1230 }
1231#endif
1232 residual_sample++;
1233 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001234#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001235 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001236#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001237#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001238 if(abs_residual_partition_max > 0)
1239 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1240 else
1241 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1242#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001243 }
Josh Coalsonaef013c2001-04-24 01:25:42 +00001244 to_partition = partitions;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001245 break;
1246 }
1247 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001248
Josh Coalsonaef013c2001-04-24 01:25:42 +00001249 /* now merge for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00001250 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001251#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001252 uint32 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001253#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001254#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001255 unsigned m;
1256#endif
1257 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001258 const unsigned partitions = 1u << partition_order;
1259 for(i = 0; i < partitions; i++) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001260#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001261 s = abs_residual_partition_sums[from_partition];
1262#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001263#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001264 m = raw_bits_per_partition[from_partition];
1265#endif
1266 from_partition++;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001267#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001268 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1269#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001270#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001271 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1272#endif
1273 from_partition++;
1274 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001275 }
1276 }
1277
Josh Coalsonf76a3612001-04-18 02:28:11 +00001278 return max_partition_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001279}
Josh Coalsonafcd8772001-04-18 22:59:25 +00001280#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001281
Josh Coalson352e0f62001-03-20 22:55:50 +00001282#ifdef VARIABLE_RICE_BITS
1283#undef VARIABLE_RICE_BITS
1284#endif
1285#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1286
Josh Coalson034dfab2001-04-27 19:10:23 +00001287bool 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 +00001288{
Josh Coalson034dfab2001-04-27 19:10:23 +00001289 unsigned rice_parameter, partition_bits;
1290#ifndef NO_RICE_SEARCH
1291 unsigned best_partition_bits;
1292 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1293#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001294#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001295 unsigned flat_bits;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001296#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001297 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1298
Josh Coalson034dfab2001-04-27 19:10:23 +00001299 assert(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00001300
Josh Coalson94e02cd2001-01-25 10:41:06 +00001301 if(partition_order == 0) {
1302 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001303
Josh Coalson034dfab2001-04-27 19:10:23 +00001304#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001305 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001306 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001307 min_rice_parameter = 0;
1308 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001309 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1310 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001311 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1312 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1313 }
1314 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001315 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001316
Josh Coalson034dfab2001-04-27 19:10:23 +00001317 best_partition_bits = 0xffffffff;
1318 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1319#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001320#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001321#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001322 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001323#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001324 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00001325 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001326#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001327#else
1328 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001329#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001330 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001331 for(i = 0; i < residual_samples; i++) {
1332#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001333#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001334 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001335#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001336 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001337#endif
1338#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001339 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 +00001340#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001341 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001342#ifndef NO_RICE_SEARCH
1343 if(partition_bits < best_partition_bits) {
1344 best_rice_parameter = rice_parameter;
1345 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001346 }
1347 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001348#endif
1349#ifdef FLAC__SEARCH_FOR_ESCAPES
1350 flat_bits = raw_bits_per_partition[0] * residual_samples;
1351 if(flat_bits <= best_partition_bits) {
1352 raw_bits[0] = raw_bits_per_partition[0];
1353 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1354 best_partition_bits = flat_bits;
1355 }
1356#endif
1357 parameters[0] = best_rice_parameter;
1358 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001359 }
1360 else {
Josh Coalson034dfab2001-04-27 19:10:23 +00001361 unsigned partition, j, save_j, k;
1362 unsigned mean, partition_samples;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001363 const unsigned partitions = 1u << partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001364 for(partition = j = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001365 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001366 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001367 if(partition_samples <= predictor_order)
1368 return false;
1369 else
1370 partition_samples -= predictor_order;
1371 }
1372 mean = partition_samples >> 1;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001373#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalson034dfab2001-04-27 19:10:23 +00001374 mean += abs_residual_partition_sums[partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001375#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001376 save_j = j;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001377 for(k = 0; k < partition_samples; j++, k++)
1378 mean += abs_residual[j];
Josh Coalson034dfab2001-04-27 19:10:23 +00001379 j = save_j;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001380#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 Coalson034dfab2001-04-27 19:10:23 +00001383 /* calc rice_parameter = floor(log2(mean)) */
1384 rice_parameter = 0;
1385 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001386 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001387 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001388 mean >>= 1;
1389 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001390#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001391 /* calc rice_parameter = floor(log2(mean)) + 1 */
1392 rice_parameter = 0;
Josh Coalson352e0f62001-03-20 22:55:50 +00001393 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001394 rice_parameter++;
Josh Coalson352e0f62001-03-20 22:55:50 +00001395 mean >>= 1;
1396 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001397#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001398 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1399 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +00001400
Josh Coalson034dfab2001-04-27 19:10:23 +00001401#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001402 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001403 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001404 min_rice_parameter = 0;
1405 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001406 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1407 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001408 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1409 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1410 }
1411 else
1412 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00001413
Josh Coalson034dfab2001-04-27 19:10:23 +00001414 best_partition_bits = 0xffffffff;
1415 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1416#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001417#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001418#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001419 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001420#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001421 const unsigned rice_parameter_estimate = rice_parameter-1;
1422 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001423#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001424#else
1425 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001426#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001427 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1428 save_j = j;
1429 for(k = 0; k < partition_samples; j++, k++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001430#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001431#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001432 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001433#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001434 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001435#endif
1436#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001437 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 +00001438#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001439 }
1440 if(rice_parameter != max_rice_parameter)
1441 j = save_j;
1442#ifndef NO_RICE_SEARCH
1443 if(partition_bits < best_partition_bits) {
1444 best_rice_parameter = rice_parameter;
1445 best_partition_bits = partition_bits;
1446 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001447 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001448#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001449#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalson034dfab2001-04-27 19:10:23 +00001450 flat_bits = raw_bits_per_partition[partition] * partition_samples;
1451 if(flat_bits <= best_partition_bits) {
1452 raw_bits[partition] = raw_bits_per_partition[partition];
1453 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1454 best_partition_bits = flat_bits;
Josh Coalson2051dd42001-04-12 22:22:34 +00001455 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001456#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001457 parameters[partition] = best_rice_parameter;
1458 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001459 }
1460 }
1461
1462 *bits = bits_;
1463 return true;
1464}
Josh Coalson859bc542001-03-27 22:22:27 +00001465
Josh Coalson8e92f212001-05-01 18:50:45 +00001466unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00001467{
1468 unsigned i, shift;
1469 int32 x = 0;
1470
1471 for(i = 0; i < samples && !(x&1); i++)
1472 x |= signal[i];
1473
1474 if(x == 0) {
1475 shift = 0;
1476 }
1477 else {
1478 for(shift = 0; !(x&1); shift++)
1479 x >>= 1;
1480 }
1481
1482 if(shift > 0) {
1483 for(i = 0; i < samples; i++)
1484 signal[i] >>= shift;
1485 }
1486
1487 return shift;
1488}