blob: 4a96b5f5bfeab8943d8bcc4f7312a44c546e3e05 [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
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000020#include <stdio.h>
21#include <stdlib.h> /* for malloc() */
22#include <string.h> /* for memcpy() */
Josh Coalson1b689822001-05-31 20:11:02 +000023#include "FLAC/assert.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000024#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 Coalson92d42402001-05-31 20:53:19 +000080 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[]);
Josh Coalsoneef56702001-03-30 00:45:22 +000081 bool use_slow; /* use slow 64-bit versions of some functions */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000082 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
83 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
84 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +000085 /* unaligned (original) pointers to allocated data */
86 int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
87 int32 *integer_signal_mid_side_unaligned[2];
88 real *real_signal_unaligned[FLAC__MAX_CHANNELS];
89 real *real_signal_mid_side_unaligned[2];
90 int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
91 int32 *residual_workspace_mid_side_unaligned[2][2];
92 uint32 *abs_residual_unaligned;
93 uint32 *abs_residual_partition_sums_unaligned;
94 unsigned *raw_bits_per_partition_unaligned;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000095} FLAC__EncoderPrivate;
96
97static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
98static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000099static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson60f77d72001-04-25 02:16:36 +0000100static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
Josh Coalson82b73242001-03-28 22:17:05 +0000101static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
102static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000103static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
Josh Coalson8e833622001-05-29 20:49:51 +0000104static 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 +0000105static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000106static unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000107#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +0000108static unsigned encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
Josh Coalsonafcd8772001-04-18 22:59:25 +0000109#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000110static bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
Josh Coalson859bc542001-03-27 22:22:27 +0000111static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000112
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000113const char *FLAC__EncoderWriteStatusString[] = {
114 "FLAC__ENCODER_WRITE_OK",
115 "FLAC__ENCODER_WRITE_FATAL_ERROR"
116};
117
118const char *FLAC__EncoderStateString[] = {
119 "FLAC__ENCODER_OK",
120 "FLAC__ENCODER_UNINITIALIZED",
121 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
122 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
123 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
124 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
125 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
126 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
127 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000128 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000129 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
130 "FLAC__ENCODER_NOT_STREAMABLE",
131 "FLAC__ENCODER_FRAMING_ERROR",
132 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
133 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
134 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
135};
136
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000137
138bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
139{
140 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000141 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000142
Josh Coalson1b689822001-05-31 20:11:02 +0000143 FLAC__ASSERT(new_size > 0);
144 FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
145 FLAC__ASSERT(encoder->guts->current_sample_number == 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000146
147 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
148 if(new_size <= encoder->guts->input_capacity)
149 return true;
150
Josh Coalsond98c43d2001-05-13 05:17:01 +0000151 ok = true;
152 for(i = 0; ok && i < encoder->channels; i++) {
153 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_unaligned[i], &encoder->guts->integer_signal[i]);
154 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_unaligned[i], &encoder->guts->real_signal[i]);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000155 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000156 for(i = 0; ok && i < 2; i++) {
157 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_mid_side_unaligned[i], &encoder->guts->integer_signal_mid_side[i]);
158 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_mid_side_unaligned[i], &encoder->guts->real_signal_mid_side[i]);
159 }
160 for(channel = 0; ok && channel < encoder->channels; channel++) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000161 for(i = 0; ok && i < 2; i++) {
Josh Coalsond98c43d2001-05-13 05:17:01 +0000162 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_unaligned[channel][i], &encoder->guts->residual_workspace[channel][i]);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000163 }
164 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000165 for(channel = 0; ok && channel < 2; channel++) {
166 for(i = 0; ok && i < 2; i++) {
167 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_mid_side_unaligned[channel][i], &encoder->guts->residual_workspace_mid_side[channel][i]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000168 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000169 }
170 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->guts->abs_residual_unaligned, &encoder->guts->abs_residual);
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000171#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond98c43d2001-05-13 05:17:01 +0000172 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->guts->abs_residual_partition_sums_unaligned, &encoder->guts->abs_residual_partition_sums);
Josh Coalson96611082001-04-24 01:30:10 +0000173#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000174#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsond98c43d2001-05-13 05:17:01 +0000175 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->guts->raw_bits_per_partition_unaligned, &encoder->guts->raw_bits_per_partition);
Josh Coalson96611082001-04-24 01:30:10 +0000176#endif
Josh Coalsond98c43d2001-05-13 05:17:01 +0000177
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000178 if(ok)
179 encoder->guts->input_capacity = new_size;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000180 else
181 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000182
183 return ok;
184}
185
186FLAC__Encoder *FLAC__encoder_get_new_instance()
187{
188 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
189 if(encoder != 0) {
190 encoder->state = FLAC__ENCODER_UNINITIALIZED;
191 encoder->guts = 0;
192 }
193 return encoder;
194}
195
196void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
197{
Josh Coalson1b689822001-05-31 20:11:02 +0000198 FLAC__ASSERT(encoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000199 free(encoder);
200}
201
202FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data), void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data), void *client_data)
203{
204 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000205 FLAC__StreamMetaData padding;
Josh Coalson0a72e182001-04-13 19:17:16 +0000206 FLAC__StreamMetaData seek_table;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000207
Josh Coalson1b689822001-05-31 20:11:02 +0000208 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
209 FLAC__ASSERT(encoder != 0);
210 FLAC__ASSERT(write_callback != 0);
211 FLAC__ASSERT(metadata_callback != 0);
212 FLAC__ASSERT(encoder->state == FLAC__ENCODER_UNINITIALIZED);
213 FLAC__ASSERT(encoder->guts == 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000214
215 encoder->state = FLAC__ENCODER_OK;
216
217 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
218 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
219
220 if(encoder->do_mid_side_stereo && encoder->channels != 2)
221 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
222
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000223 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000224 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
225
Josh Coalsond37d1352001-05-30 23:09:31 +0000226 if(encoder->bits_per_sample >= 32)
227 encoder->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
228
Josh Coalson82b73242001-03-28 22:17:05 +0000229 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 +0000230 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
231
232 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
233 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
234
235 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
236 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
237
238 if(encoder->blocksize < encoder->max_lpc_order)
239 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
240
241 if(encoder->qlp_coeff_precision == 0) {
242 if(encoder->bits_per_sample < 16) {
243 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
244 /* @@@ until then we'll make a guess */
245 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
246 }
247 else if(encoder->bits_per_sample == 16) {
248 if(encoder->blocksize <= 192)
249 encoder->qlp_coeff_precision = 7;
250 else if(encoder->blocksize <= 384)
251 encoder->qlp_coeff_precision = 8;
252 else if(encoder->blocksize <= 576)
253 encoder->qlp_coeff_precision = 9;
254 else if(encoder->blocksize <= 1152)
255 encoder->qlp_coeff_precision = 10;
256 else if(encoder->blocksize <= 2304)
257 encoder->qlp_coeff_precision = 11;
258 else if(encoder->blocksize <= 4608)
259 encoder->qlp_coeff_precision = 12;
260 else
261 encoder->qlp_coeff_precision = 13;
262 }
263 else {
264 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
265 }
266 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000267 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 +0000268 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
269
270 if(encoder->streamable_subset) {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000271 //@@@ add check for blocksize here
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000272 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)
273 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
274 if(encoder->sample_rate > 655350)
275 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
276 }
277
Josh Coalson60f77d72001-04-25 02:16:36 +0000278 if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
279 encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
280 if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
281 encoder->min_residual_partition_order = encoder->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000282
283 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
284 if(encoder->guts == 0)
285 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
286
287 encoder->guts->input_capacity = 0;
288 for(i = 0; i < encoder->channels; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000289 encoder->guts->integer_signal_unaligned[i] = encoder->guts->integer_signal[i] = 0;
290 encoder->guts->real_signal_unaligned[i] = encoder->guts->real_signal[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000291 }
292 for(i = 0; i < 2; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000293 encoder->guts->integer_signal_mid_side_unaligned[i] = encoder->guts->integer_signal_mid_side[i] = 0;
294 encoder->guts->real_signal_mid_side_unaligned[i] = encoder->guts->real_signal_mid_side[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000295 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000296 for(i = 0; i < encoder->channels; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000297 encoder->guts->residual_workspace_unaligned[i][0] = encoder->guts->residual_workspace[i][0] = 0;
298 encoder->guts->residual_workspace_unaligned[i][1] = encoder->guts->residual_workspace[i][1] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000299 encoder->guts->best_subframe[i] = 0;
300 }
301 for(i = 0; i < 2; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000302 encoder->guts->residual_workspace_mid_side_unaligned[i][0] = encoder->guts->residual_workspace_mid_side[i][0] = 0;
303 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 +0000304 encoder->guts->best_subframe_mid_side[i] = 0;
305 }
306 for(i = 0; i < encoder->channels; i++) {
307 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
308 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
309 }
310 for(i = 0; i < 2; i++) {
311 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
312 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
313 }
Josh Coalson6bd17572001-05-25 19:02:01 +0000314 encoder->guts->abs_residual_unaligned = encoder->guts->abs_residual = 0;
315 encoder->guts->abs_residual_partition_sums_unaligned = encoder->guts->abs_residual_partition_sums = 0;
316 encoder->guts->raw_bits_per_partition_unaligned = encoder->guts->raw_bits_per_partition = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000317 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
318 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
319 if(encoder->guts->loose_mid_side_stereo_frames == 0)
320 encoder->guts->loose_mid_side_stereo_frames = 1;
321 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000322 encoder->guts->current_sample_number = 0;
323 encoder->guts->current_frame_number = 0;
324
Josh Coalsoncf30f502001-05-23 20:57:44 +0000325 /*
326 * get the CPU info and set the function pointers
327 */
328 FLAC__cpu_info(&encoder->guts->cpuinfo);
329 /* first default to the non-asm routines */
330 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
331 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson8e833622001-05-29 20:49:51 +0000332 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson92d42402001-05-31 20:53:19 +0000333 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000334 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000335#ifndef FLAC__NO_ASM
Josh Coalson1b689822001-05-31 20:11:02 +0000336 FLAC__ASSERT(encoder->guts->cpuinfo.use_asm);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000337#ifdef FLAC__CPU_IA32
Josh Coalson1b689822001-05-31 20:11:02 +0000338 FLAC__ASSERT(encoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000339#ifdef FLAC__HAS_NASM
Josh Coalson91f2fb32001-06-01 18:56:52 +0000340 if(0 && encoder->guts->cpuinfo.data.ia32.sse) { /* SSE version lacks necessary resolution, plus SSE flag doesn't check for OS support */
Josh Coalsonaa255362001-05-31 06:17:41 +0000341 if(encoder->max_lpc_order < 4)
Josh Coalson525c22c2001-06-05 22:45:50 +0000342 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_4;
Josh Coalsonaa255362001-05-31 06:17:41 +0000343 else if(encoder->max_lpc_order < 8)
Josh Coalson525c22c2001-06-05 22:45:50 +0000344 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_8;
Josh Coalsonaa255362001-05-31 06:17:41 +0000345 else if(encoder->max_lpc_order < 12)
Josh Coalson525c22c2001-06-05 22:45:50 +0000346 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_12;
Josh Coalsonaa255362001-05-31 06:17:41 +0000347 else
Josh Coalsonaa255362001-05-31 06:17:41 +0000348 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
Josh Coalsonaa255362001-05-31 06:17:41 +0000349 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000350 else
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000351 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
352 if(encoder->guts->cpuinfo.data.ia32.mmx && encoder->guts->cpuinfo.data.ia32.cmov)
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000353 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov;
Josh Coalson92d42402001-05-31 20:53:19 +0000354 if(encoder->guts->cpuinfo.data.ia32.mmx) {
Josh Coalsoneae92172001-05-30 19:20:36 +0000355 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
Josh Coalson92d42402001-05-31 20:53:19 +0000356 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386_mmx;
Josh Coalson92d42402001-05-31 20:53:19 +0000357 }
358 else {
359 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
360 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
Josh Coalson92d42402001-05-31 20:53:19 +0000361 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000362#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000363#endif
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000364#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000365
Josh Coalsoneef56702001-03-30 00:45:22 +0000366 if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
367 encoder->guts->use_slow = true;
368 else
369 encoder->guts->use_slow = false;
370
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000371 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
372 /* the above function sets the state for us in case of an error */
373 return encoder->state;
374 }
375 FLAC__bitbuffer_init(&encoder->guts->frame);
376 encoder->guts->write_callback = write_callback;
377 encoder->guts->metadata_callback = metadata_callback;
378 encoder->guts->client_data = client_data;
379
380 /*
381 * write the stream header
382 */
383 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
384 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
385
386 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
387 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
388
Josh Coalsonc692d382001-02-23 21:05:05 +0000389 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson0a72e182001-04-13 19:17:16 +0000390 encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
Josh Coalsonc692d382001-02-23 21:05:05 +0000391 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
392 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
393 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
394 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
395 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
396 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
397 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
398 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
399 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
400 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 +0000401 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000402 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
403 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
404
Josh Coalson0a72e182001-04-13 19:17:16 +0000405 if(0 != encoder->seek_table) {
406 if(!FLAC__seek_table_is_valid(encoder->seek_table))
407 return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
408 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
409 seek_table.is_last = (encoder->padding == 0);
410 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
411 seek_table.data.seek_table = *encoder->seek_table;
412 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
413 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
414 }
415
Josh Coalsonc692d382001-02-23 21:05:05 +0000416 /* add a PADDING block if requested */
417 if(encoder->padding > 0) {
418 padding.type = FLAC__METADATA_TYPE_PADDING;
419 padding.is_last = true;
420 padding.length = encoder->padding;
421 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
422 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
423 }
424
Josh Coalson1b689822001-05-31 20:11:02 +0000425 FLAC__ASSERT(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
426 FLAC__ASSERT(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000427 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)
428 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
429
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000430 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000431 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 +0000432 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000433 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000434
435 return encoder->state;
436}
437
438void FLAC__encoder_finish(FLAC__Encoder *encoder)
439{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000440 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000441
Josh Coalson1b689822001-05-31 20:11:02 +0000442 FLAC__ASSERT(encoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000443 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
444 return;
445 if(encoder->guts->current_sample_number != 0) {
446 encoder->blocksize = encoder->guts->current_sample_number;
447 encoder_process_frame_(encoder, true); /* true => is last frame */
448 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000449 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000450 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
451 if(encoder->guts != 0) {
452 for(i = 0; i < encoder->channels; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000453 if(encoder->guts->integer_signal_unaligned[i] != 0) {
454 free(encoder->guts->integer_signal_unaligned[i]);
455 encoder->guts->integer_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000456 }
Josh Coalson18301942001-05-16 19:25:33 +0000457 if(encoder->guts->real_signal_unaligned[i] != 0) {
458 free(encoder->guts->real_signal_unaligned[i]);
459 encoder->guts->real_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000460 }
461 }
462 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000463 if(encoder->guts->integer_signal_mid_side_unaligned[i] != 0) {
464 free(encoder->guts->integer_signal_mid_side_unaligned[i]);
465 encoder->guts->integer_signal_mid_side_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000466 }
Josh Coalson18301942001-05-16 19:25:33 +0000467 if(encoder->guts->real_signal_mid_side_unaligned[i] != 0) {
468 free(encoder->guts->real_signal_mid_side_unaligned[i]);
469 encoder->guts->real_signal_mid_side_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000470 }
471 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000472 for(channel = 0; channel < encoder->channels; channel++) {
473 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000474 if(encoder->guts->residual_workspace_unaligned[channel][i] != 0) {
475 free(encoder->guts->residual_workspace_unaligned[channel][i]);
476 encoder->guts->residual_workspace_unaligned[channel][i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000477 }
478 }
479 }
480 for(channel = 0; channel < 2; channel++) {
481 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000482 if(encoder->guts->residual_workspace_mid_side_unaligned[channel][i] != 0) {
483 free(encoder->guts->residual_workspace_mid_side_unaligned[channel][i]);
484 encoder->guts->residual_workspace_mid_side_unaligned[channel][i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000485 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000486 }
487 }
Josh Coalson18301942001-05-16 19:25:33 +0000488 if(encoder->guts->abs_residual_unaligned != 0) {
489 free(encoder->guts->abs_residual_unaligned);
490 encoder->guts->abs_residual_unaligned = 0;
Josh Coalsone77287e2001-01-20 01:27:55 +0000491 }
Josh Coalson18301942001-05-16 19:25:33 +0000492 if(encoder->guts->abs_residual_partition_sums_unaligned != 0) {
493 free(encoder->guts->abs_residual_partition_sums_unaligned);
494 encoder->guts->abs_residual_partition_sums_unaligned = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000495 }
Josh Coalson18301942001-05-16 19:25:33 +0000496 if(encoder->guts->raw_bits_per_partition_unaligned != 0) {
497 free(encoder->guts->raw_bits_per_partition_unaligned);
498 encoder->guts->raw_bits_per_partition_unaligned = 0;
Josh Coalson2051dd42001-04-12 22:22:34 +0000499 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000500 FLAC__bitbuffer_free(&encoder->guts->frame);
501 free(encoder->guts);
502 encoder->guts = 0;
503 }
504 encoder->state = FLAC__ENCODER_UNINITIALIZED;
505}
506
507bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
508{
509 unsigned i, j, channel;
510 int32 x, mid, side;
Josh Coalsond37d1352001-05-30 23:09:31 +0000511 const unsigned channels = encoder->channels, blocksize = encoder->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000512
Josh Coalson1b689822001-05-31 20:11:02 +0000513 FLAC__ASSERT(encoder != 0);
514 FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000515
516 j = 0;
Josh Coalsonaa255362001-05-31 06:17:41 +0000517 if(encoder->do_mid_side_stereo && channels == 2) {
518 do {
519 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
520 x = mid = side = buf[0][j];
521 encoder->guts->integer_signal[0][i] = x;
522 encoder->guts->real_signal[0][i] = (real)x;
523 x = buf[1][j];
524 encoder->guts->integer_signal[1][i] = x;
525 encoder->guts->real_signal[1][i] = (real)x;
526 mid += x;
527 side -= x;
528 mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
529 encoder->guts->integer_signal_mid_side[1][i] = side;
530 encoder->guts->integer_signal_mid_side[0][i] = mid;
531 encoder->guts->real_signal_mid_side[1][i] = (real)side;
532 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
533 encoder->guts->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000534 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000535 if(i == blocksize) {
536 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
537 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000538 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000539 } while(j < samples);
540 }
541 else {
542 do {
543 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
544 for(channel = 0; channel < channels; channel++) {
545 x = buf[channel][j];
546 encoder->guts->integer_signal[channel][i] = x;
547 encoder->guts->real_signal[channel][i] = (real)x;
548 }
549 encoder->guts->current_sample_number++;
550 }
551 if(i == blocksize) {
552 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
553 return false;
554 }
555 } while(j < samples);
556 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000557
558 return true;
559}
560
561/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
562bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
563{
564 unsigned i, j, k, channel;
Josh Coalsonaa255362001-05-31 06:17:41 +0000565 int32 x, mid, side;
566 const unsigned channels = encoder->channels, blocksize = encoder->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000567
Josh Coalson1b689822001-05-31 20:11:02 +0000568 FLAC__ASSERT(encoder != 0);
569 FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000570
571 j = k = 0;
Josh Coalsonaa255362001-05-31 06:17:41 +0000572 if(encoder->do_mid_side_stereo && channels == 2) {
573 do {
574 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
575 x = mid = side = buf[k++];
576 encoder->guts->integer_signal[0][i] = x;
577 encoder->guts->real_signal[0][i] = (real)x;
Josh Coalsond37d1352001-05-30 23:09:31 +0000578 x = buf[k++];
Josh Coalsonaa255362001-05-31 06:17:41 +0000579 encoder->guts->integer_signal[1][i] = x;
580 encoder->guts->real_signal[1][i] = (real)x;
581 mid += x;
582 side -= x;
583 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
584 encoder->guts->integer_signal_mid_side[1][i] = side;
585 encoder->guts->integer_signal_mid_side[0][i] = mid;
586 encoder->guts->real_signal_mid_side[1][i] = (real)side;
587 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
588 encoder->guts->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000589 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000590 if(i == blocksize) {
591 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
592 return false;
593 }
594 } while(j < samples);
595 }
596 else {
597 do {
598 for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
599 for(channel = 0; channel < channels; channel++) {
600 x = buf[k++];
601 encoder->guts->integer_signal[channel][i] = x;
602 encoder->guts->real_signal[channel][i] = (real)x;
603 }
604 encoder->guts->current_sample_number++;
605 }
606 if(i == blocksize) {
607 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
608 return false;
609 }
610 } while(j < samples);
611 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000612
613 return true;
614}
615
616bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
617{
Josh Coalson1b689822001-05-31 20:11:02 +0000618 FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000619
620 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000621 * Accumulate raw signal to the MD5 signature
622 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000623 /* 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 +0000624 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
625 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
626 return false;
627 }
628
629 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000630 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000631 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000632 if(!encoder_process_subframes_(encoder, is_last_frame)) {
633 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000634 return false;
635 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000636
637 /*
638 * Zero-pad the frame to a byte_boundary
639 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000640 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000641 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
642 return false;
643 }
644
645 /*
Josh Coalson215af572001-03-27 01:15:58 +0000646 * CRC-16 the whole thing
647 */
Josh Coalson1b689822001-05-31 20:11:02 +0000648 FLAC__ASSERT(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
649 FLAC__ASSERT(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
Josh Coalson215af572001-03-27 01:15:58 +0000650 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
651
652 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000653 * Write it
654 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000655 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 +0000656 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
657 return false;
658 }
659
660 /*
661 * Get ready for the next frame
662 */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000663 encoder->guts->current_sample_number = 0;
664 encoder->guts->current_frame_number++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000665 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
666 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
667 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 +0000668
669 return true;
670}
671
Josh Coalson94e02cd2001-01-25 10:41:06 +0000672bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
673{
674 FLAC__FrameHeader frame_header;
Josh Coalson034dfab2001-04-27 19:10:23 +0000675 unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000676 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000677
678 /*
Josh Coalson60f77d72001-04-25 02:16:36 +0000679 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +0000680 */
681 if(is_last_frame) {
682 max_partition_order = 0;
683 }
684 else {
685 unsigned limit = 0, b = encoder->blocksize;
686 while(!(b & 1)) {
687 limit++;
688 b >>= 1;
689 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000690 max_partition_order = min(encoder->max_residual_partition_order, limit);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000691 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000692 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000693
694 /*
695 * Setup the frame
696 */
697 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
698 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
699 return false;
700 }
701 frame_header.blocksize = encoder->blocksize;
702 frame_header.sample_rate = encoder->sample_rate;
703 frame_header.channels = encoder->channels;
704 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
705 frame_header.bits_per_sample = encoder->bits_per_sample;
706 frame_header.number.frame_number = encoder->guts->current_frame_number;
707
708 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000709 * Figure out what channel assignments to try
710 */
711 if(encoder->do_mid_side_stereo) {
712 if(encoder->loose_mid_side_stereo) {
713 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
714 do_independent = true;
715 do_mid_side = true;
716 }
717 else {
718 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
719 do_mid_side = !do_independent;
720 }
721 }
722 else {
723 do_independent = true;
724 do_mid_side = true;
725 }
726 }
727 else {
728 do_independent = true;
729 do_mid_side = false;
730 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000731
Josh Coalson1b689822001-05-31 20:11:02 +0000732 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000733
734 /*
Josh Coalson82b73242001-03-28 22:17:05 +0000735 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +0000736 */
737 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +0000738 unsigned w;
739 for(channel = 0; channel < encoder->channels; channel++) {
740 w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
741 encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
742 encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
743 }
Josh Coalson859bc542001-03-27 22:22:27 +0000744 }
745 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000746 unsigned w;
Josh Coalson1b689822001-05-31 20:11:02 +0000747 FLAC__ASSERT(encoder->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +0000748 for(channel = 0; channel < 2; channel++) {
749 w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
750 encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
751 encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
752 }
Josh Coalson859bc542001-03-27 22:22:27 +0000753 }
754
755 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000756 * First do a normal encoding pass of each independent channel
757 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000758 if(do_independent) {
759 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000760 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 +0000761 return false;
762 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000763 }
764
765 /*
766 * Now do mid and side channels if requested
767 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000768 if(do_mid_side) {
Josh Coalson1b689822001-05-31 20:11:02 +0000769 FLAC__ASSERT(encoder->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000770
771 for(channel = 0; channel < 2; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000772 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 +0000773 return false;
774 }
775 }
776
777 /*
778 * Compose the frame bitbuffer
779 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000780 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000781 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
782 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000783 FLAC__ChannelAssignment channel_assignment;
784
Josh Coalson1b689822001-05-31 20:11:02 +0000785 FLAC__ASSERT(encoder->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000786
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000787 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
788 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
789 }
790 else {
791 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
792 unsigned min_bits;
793 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000794
Josh Coalson1b689822001-05-31 20:11:02 +0000795 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000796
797 /* We have to figure out which channel assignent results in the smallest frame */
798 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
799 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
800 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
801 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
802
803 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
804 if(bits[ca] < min_bits) {
805 min_bits = bits[ca];
806 channel_assignment = ca;
807 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000808 }
809 }
810
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000811 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000812
813 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
814 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
815 return false;
816 }
817
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000818 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000819 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson82b73242001-03-28 22:17:05 +0000820 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
821 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000822 break;
823 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000824 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
825 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000826 break;
827 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000828 left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
829 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000830 break;
831 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000832 left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
833 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000834 break;
835 default:
Josh Coalson1b689822001-05-31 20:11:02 +0000836 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000837 }
Josh Coalson82b73242001-03-28 22:17:05 +0000838
839 switch(channel_assignment) {
840 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
841 left_bps = encoder->guts->subframe_bps [0];
842 right_bps = encoder->guts->subframe_bps [1];
843 break;
844 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
845 left_bps = encoder->guts->subframe_bps [0];
846 right_bps = encoder->guts->subframe_bps_mid_side[1];
847 break;
848 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
849 left_bps = encoder->guts->subframe_bps_mid_side[1];
850 right_bps = encoder->guts->subframe_bps [1];
851 break;
852 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
853 left_bps = encoder->guts->subframe_bps_mid_side[0];
854 right_bps = encoder->guts->subframe_bps_mid_side[1];
855 break;
856 default:
Josh Coalson1b689822001-05-31 20:11:02 +0000857 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +0000858 }
859
860 /* note that encoder_add_subframe_ sets the state for us in case of an error */
861 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
862 return false;
863 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
864 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000865 }
866 else {
867 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
868 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
869 return false;
870 }
871
872 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000873 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 +0000874 /* the above function sets the state for us in case of an error */
875 return false;
876 }
877 }
878 }
879
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000880 if(encoder->loose_mid_side_stereo) {
881 encoder->guts->loose_mid_side_stereo_frame_count++;
882 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
883 encoder->guts->loose_mid_side_stereo_frame_count = 0;
884 }
885
886 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
887
Josh Coalson94e02cd2001-01-25 10:41:06 +0000888 return true;
889}
890
Josh Coalson60f77d72001-04-25 02:16:36 +0000891bool 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 +0000892{
893 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
894 real lpc_residual_bits_per_sample;
Josh Coalsond16370d2001-05-31 05:56:41 +0000895 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 +0000896 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
897 real lpc_error[FLAC__MAX_LPC_ORDER];
898 unsigned min_lpc_order, max_lpc_order, lpc_order;
899 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
900 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
901 unsigned rice_parameter;
902 unsigned _candidate_bits, _best_bits;
903 unsigned _best_subframe;
904
905 /* verbatim subframe is the baseline against which we measure other compressed subframes */
906 _best_subframe = 0;
Josh Coalson82b73242001-03-28 22:17:05 +0000907 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000908
909 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
910 /* check for constant subframe */
Josh Coalsoneef56702001-03-30 00:45:22 +0000911 if(encoder->guts->use_slow)
912 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);
913 else
Josh Coalsoncf30f502001-05-23 20:57:44 +0000914 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 +0000915 if(fixed_residual_bits_per_sample[1] == 0.0) {
916 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
917 unsigned i, signal_is_constant = true;
918 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
919 if(integer_signal[0] != integer_signal[i]) {
920 signal_is_constant = false;
921 break;
922 }
923 }
924 if(signal_is_constant) {
Josh Coalson82b73242001-03-28 22:17:05 +0000925 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000926 if(_candidate_bits < _best_bits) {
927 _best_subframe = !_best_subframe;
928 _best_bits = _candidate_bits;
929 }
930 }
931 }
932 else {
933 /* encode fixed */
934 if(encoder->do_exhaustive_model_search) {
935 min_fixed_order = 0;
936 max_fixed_order = FLAC__MAX_FIXED_ORDER;
937 }
938 else {
939 min_fixed_order = max_fixed_order = guess_fixed_order;
940 }
941 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000942 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000943 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000944 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 +0000945#ifndef FLAC__SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000946 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000947#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000948 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
949 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +0000950 _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 +0000951 if(_candidate_bits < _best_bits) {
952 _best_subframe = !_best_subframe;
953 _best_bits = _candidate_bits;
954 }
955 }
956
957 /* encode lpc */
958 if(encoder->max_lpc_order > 0) {
959 if(encoder->max_lpc_order >= frame_header->blocksize)
960 max_lpc_order = frame_header->blocksize-1;
961 else
962 max_lpc_order = encoder->max_lpc_order;
963 if(max_lpc_order > 0) {
Josh Coalsoncf30f502001-05-23 20:57:44 +0000964 encoder->guts->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000965 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
966 if(autoc[0] != 0.0) {
967 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
968 if(encoder->do_exhaustive_model_search) {
969 min_lpc_order = 1;
970 }
971 else {
Josh Coalson82b73242001-03-28 22:17:05 +0000972 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 +0000973 min_lpc_order = max_lpc_order = guess_lpc_order;
974 }
975 if(encoder->do_qlp_coeff_prec_search) {
976 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalson82b73242001-03-28 22:17:05 +0000977 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 +0000978 }
979 else {
980 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
981 }
982 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
983 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 +0000984 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000985 continue; /* don't even try */
986 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 +0000987#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000988 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000989#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000990 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
991 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000992 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 +0000993 _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 +0000994 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
995 if(_candidate_bits < _best_bits) {
996 _best_subframe = !_best_subframe;
997 _best_bits = _candidate_bits;
998 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000999 }
1000 }
1001 }
1002 }
1003 }
1004 }
1005 }
1006 }
1007
1008 *best_subframe = _best_subframe;
1009 *best_bits = _best_bits;
1010
1011 return true;
1012}
1013
Josh Coalson82b73242001-03-28 22:17:05 +00001014bool 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 +00001015{
1016 switch(subframe->type) {
1017 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00001018 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001019 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1020 return false;
1021 }
1022 break;
1023 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00001024 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 +00001025 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1026 return false;
1027 }
1028 break;
1029 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00001030 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 +00001031 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1032 return false;
1033 }
1034 break;
1035 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00001036 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001037 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1038 return false;
1039 }
1040 break;
1041 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001042 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001043 }
1044
1045 return true;
1046}
1047
Josh Coalson82b73242001-03-28 22:17:05 +00001048unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001049{
1050 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1051 subframe->data.constant.value = signal;
1052
Josh Coalson82b73242001-03-28 22:17:05 +00001053 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 +00001054}
1055
Josh Coalson60f77d72001-04-25 02:16:36 +00001056unsigned 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 +00001057{
1058 unsigned i, residual_bits;
1059 const unsigned residual_samples = blocksize - order;
1060
1061 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1062
1063 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1064
1065 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1066 subframe->data.fixed.residual = residual;
1067
Josh Coalson60f77d72001-04-25 02:16:36 +00001068 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 +00001069
1070 subframe->data.fixed.order = order;
1071 for(i = 0; i < order; i++)
1072 subframe->data.fixed.warmup[i] = signal[i];
1073
Josh Coalson82b73242001-03-28 22:17:05 +00001074 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 +00001075}
1076
Josh Coalson8e833622001-05-29 20:49:51 +00001077unsigned 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 +00001078{
1079 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1080 unsigned i, residual_bits;
1081 int quantization, ret;
1082 const unsigned residual_samples = blocksize - order;
1083
Josh Coalson82b73242001-03-28 22:17:05 +00001084 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001085 if(ret != 0)
1086 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1087
Josh Coalson92d42402001-05-31 20:53:19 +00001088 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
1089 encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1090 else
1091 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 +00001092
1093 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1094
1095 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1096 subframe->data.lpc.residual = residual;
1097
Josh Coalson60f77d72001-04-25 02:16:36 +00001098 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 +00001099
1100 subframe->data.lpc.order = order;
1101 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1102 subframe->data.lpc.quantization_level = quantization;
1103 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1104 for(i = 0; i < order; i++)
1105 subframe->data.lpc.warmup[i] = signal[i];
1106
Josh Coalson82b73242001-03-28 22:17:05 +00001107 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 +00001108}
1109
Josh Coalson82b73242001-03-28 22:17:05 +00001110unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001111{
1112 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1113
1114 subframe->data.verbatim.data = signal;
1115
Josh Coalson82b73242001-03-28 22:17:05 +00001116 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 +00001117}
1118
Josh Coalson60f77d72001-04-25 02:16:36 +00001119unsigned 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 +00001120{
Josh Coalson94e02cd2001-01-25 10:41:06 +00001121 int32 r;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001122#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalsonafcd8772001-04-18 22:59:25 +00001123 unsigned sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001124 int partition_order;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001125#else
1126 unsigned partition_order;
1127#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001128 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001129 unsigned residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001130 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 +00001131
Josh Coalson2051dd42001-04-12 22:22:34 +00001132 /* compute abs(residual) for use later */
1133 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1134 r = residual[residual_sample];
1135 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1136 }
1137
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001138#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001139 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);
1140 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001141
Josh Coalson60f77d72001-04-25 02:16:36 +00001142 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1143 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 Coalson1b689822001-05-31 20:11:02 +00001144 FLAC__ASSERT(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001145 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001146 sum += 1u << partition_order;
1147 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1148 best_residual_bits = residual_bits;
1149 *best_partition_order = partition_order;
1150 best_parameters_index = !best_parameters_index;
1151 }
1152 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001153#else
Josh Coalson60f77d72001-04-25 02:16:36 +00001154 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1155 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 Coalson1b689822001-05-31 20:11:02 +00001156 FLAC__ASSERT(best_residual_bits != 0);
Josh Coalsonafcd8772001-04-18 22:59:25 +00001157 break;
1158 }
1159 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1160 best_residual_bits = residual_bits;
1161 *best_partition_order = partition_order;
1162 best_parameters_index = !best_parameters_index;
1163 }
1164 }
1165#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001166 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1167 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1168
1169 return best_residual_bits;
1170}
1171
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001172#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001173unsigned 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 +00001174{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001175 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001176 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001177 const unsigned blocksize = residual_samples + predictor_order;
1178
Josh Coalsonaef013c2001-04-24 01:25:42 +00001179 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001180 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001181#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001182 uint32 abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001183#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001184#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001185 uint32 abs_residual_partition_max;
1186 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1187#endif
1188 uint32 abs_r;
1189 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001190 const unsigned partitions = 1u << partition_order;
1191 const unsigned default_partition_samples = blocksize >> partition_order;
1192
1193 if(default_partition_samples <= predictor_order) {
Josh Coalson1b689822001-05-31 20:11:02 +00001194 FLAC__ASSERT(max_partition_order > 0);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001195 max_partition_order--;
1196 }
1197 else {
1198 for(partition = residual_sample = 0; partition < partitions; partition++) {
1199 partition_samples = default_partition_samples;
1200 if(partition == 0)
1201 partition_samples -= predictor_order;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001202#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001203 abs_residual_partition_sum = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001204#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001205#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001206 abs_residual_partition_max = 0;
1207#endif
1208 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1209 abs_r = abs_residual[residual_sample];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001210#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001211 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1212#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001213#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001214 if(abs_r > abs_residual_partition_max) {
1215 abs_residual_partition_max = abs_r;
1216 abs_residual_partition_max_index = residual_sample;
1217 }
1218#endif
1219 residual_sample++;
1220 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001221#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001222 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001223#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001224#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001225 if(abs_residual_partition_max > 0)
1226 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1227 else
1228 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1229#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001230 }
Josh Coalsonaef013c2001-04-24 01:25:42 +00001231 to_partition = partitions;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001232 break;
1233 }
1234 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001235
Josh Coalsonaef013c2001-04-24 01:25:42 +00001236 /* now merge for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00001237 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001238#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001239 uint32 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001240#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001241#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001242 unsigned m;
1243#endif
1244 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001245 const unsigned partitions = 1u << partition_order;
1246 for(i = 0; i < partitions; i++) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001247#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001248 s = abs_residual_partition_sums[from_partition];
1249#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001250#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001251 m = raw_bits_per_partition[from_partition];
1252#endif
1253 from_partition++;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001254#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001255 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1256#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001257#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001258 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1259#endif
1260 from_partition++;
1261 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001262 }
1263 }
1264
Josh Coalsonf76a3612001-04-18 02:28:11 +00001265 return max_partition_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001266}
Josh Coalsonafcd8772001-04-18 22:59:25 +00001267#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001268
Josh Coalson352e0f62001-03-20 22:55:50 +00001269#ifdef VARIABLE_RICE_BITS
1270#undef VARIABLE_RICE_BITS
1271#endif
1272#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1273
Josh Coalson034dfab2001-04-27 19:10:23 +00001274bool 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 +00001275{
Josh Coalson034dfab2001-04-27 19:10:23 +00001276 unsigned rice_parameter, partition_bits;
1277#ifndef NO_RICE_SEARCH
1278 unsigned best_partition_bits;
1279 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1280#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001281#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001282 unsigned flat_bits;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001283#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001284 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1285
Josh Coalson1b689822001-05-31 20:11:02 +00001286 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00001287
Josh Coalson94e02cd2001-01-25 10:41:06 +00001288 if(partition_order == 0) {
1289 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001290
Josh Coalson034dfab2001-04-27 19:10:23 +00001291#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001292 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001293 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001294 min_rice_parameter = 0;
1295 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001296 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1297 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001298 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1299 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1300 }
1301 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001302 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001303
Josh Coalson034dfab2001-04-27 19:10:23 +00001304 best_partition_bits = 0xffffffff;
1305 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1306#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001307#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001308#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001309 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001310#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001311 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00001312 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001313#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001314#else
1315 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001316#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001317 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001318 for(i = 0; i < residual_samples; i++) {
1319#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001320#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001321 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001322#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001323 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001324#endif
1325#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001326 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 +00001327#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001328 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001329#ifndef NO_RICE_SEARCH
1330 if(partition_bits < best_partition_bits) {
1331 best_rice_parameter = rice_parameter;
1332 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001333 }
1334 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001335#endif
1336#ifdef FLAC__SEARCH_FOR_ESCAPES
1337 flat_bits = raw_bits_per_partition[0] * residual_samples;
1338 if(flat_bits <= best_partition_bits) {
1339 raw_bits[0] = raw_bits_per_partition[0];
1340 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1341 best_partition_bits = flat_bits;
1342 }
1343#endif
1344 parameters[0] = best_rice_parameter;
1345 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001346 }
1347 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00001348 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00001349 unsigned mean, partition_samples;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001350 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00001351 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001352 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001353 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001354 if(partition_samples <= predictor_order)
1355 return false;
1356 else
1357 partition_samples -= predictor_order;
1358 }
1359 mean = partition_samples >> 1;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001360#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalson034dfab2001-04-27 19:10:23 +00001361 mean += abs_residual_partition_sums[partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001362#else
Josh Coalson4dacd192001-06-06 21:11:44 +00001363 save_residual_sample = residual_sample;
1364 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
1365 mean += abs_residual[residual_sample];
1366 residual_sample = save_residual_sample;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001367#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001368 mean /= partition_samples;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001369#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001370 /* calc rice_parameter = floor(log2(mean)) */
1371 rice_parameter = 0;
1372 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001373 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001374 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001375 mean >>= 1;
1376 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001377#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001378 /* calc rice_parameter = floor(log2(mean)) + 1 */
1379 rice_parameter = 0;
Josh Coalson352e0f62001-03-20 22:55:50 +00001380 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001381 rice_parameter++;
Josh Coalson352e0f62001-03-20 22:55:50 +00001382 mean >>= 1;
1383 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001384#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001385 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1386 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +00001387
Josh Coalson034dfab2001-04-27 19:10:23 +00001388#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001389 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001390 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001391 min_rice_parameter = 0;
1392 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001393 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1394 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001395 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1396 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1397 }
1398 else
1399 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00001400
Josh Coalson034dfab2001-04-27 19:10:23 +00001401 best_partition_bits = 0xffffffff;
1402 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1403#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001404#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001405#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001406 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001407#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001408 const unsigned rice_parameter_estimate = rice_parameter-1;
1409 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001410#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001411#else
1412 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001413#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001414 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00001415 save_residual_sample = residual_sample;
1416 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
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 Coalson4dacd192001-06-06 21:11:44 +00001419 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001420#else
Josh Coalson4dacd192001-06-06 21:11:44 +00001421 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001422#endif
1423#else
Josh Coalson4dacd192001-06-06 21:11:44 +00001424 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001425#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001426 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001427#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00001428 if(rice_parameter != max_rice_parameter)
1429 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00001430 if(partition_bits < best_partition_bits) {
1431 best_rice_parameter = rice_parameter;
1432 best_partition_bits = partition_bits;
1433 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001434 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001435#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001436#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalson034dfab2001-04-27 19:10:23 +00001437 flat_bits = raw_bits_per_partition[partition] * partition_samples;
1438 if(flat_bits <= best_partition_bits) {
1439 raw_bits[partition] = raw_bits_per_partition[partition];
1440 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1441 best_partition_bits = flat_bits;
Josh Coalson2051dd42001-04-12 22:22:34 +00001442 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001443#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001444 parameters[partition] = best_rice_parameter;
1445 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001446 }
1447 }
1448
1449 *bits = bits_;
1450 return true;
1451}
Josh Coalson859bc542001-03-27 22:22:27 +00001452
Josh Coalson8e92f212001-05-01 18:50:45 +00001453unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00001454{
1455 unsigned i, shift;
1456 int32 x = 0;
1457
1458 for(i = 0; i < samples && !(x&1); i++)
1459 x |= signal[i];
1460
1461 if(x == 0) {
1462 shift = 0;
1463 }
1464 else {
1465 for(shift = 0; !(x&1); shift++)
1466 x >>= 1;
1467 }
1468
1469 if(shift > 0) {
1470 for(i = 0; i < samples; i++)
1471 signal[i] >>= shift;
1472 }
1473
1474 return shift;
1475}