blob: 309abb31d6091554f9761cfbe7cf807f96634fd6 [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 Coalson0a72e182001-04-13 19:17:16 +000024#include "FLAC/seek_table.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000025#include "protected/stream_encoder.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 Coalson0a15c142001-06-13 17:59:57 +000030#include "private/stream_encoder_framing.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000031#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 Coalson0a15c142001-06-13 17:59:57 +000046/***********************************************************************
47 *
48 * Private class method prototypes
49 *
50 ***********************************************************************/
51
Josh Coalson77e3f312001-06-23 03:03:24 +000052static FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
53static FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
54static FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson8395d022001-07-12 21:25:22 +000055static FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
Josh Coalson77e3f312001-06-23 03:03:24 +000056static FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
57static unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalsonb3347bd2001-07-16 18:06:41 +000058static unsigned stream_encoder_evaluate_fixed_subframe_(const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 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, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
59static unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__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, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
Josh Coalson77e3f312001-06-23 03:03:24 +000060static unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalsonb3347bd2001-07-16 18:06:41 +000061static unsigned stream_encoder_find_best_partition_order_(const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 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, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
62static void stream_encoder_precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
Josh Coalson8395d022001-07-12 21:25:22 +000063static void stream_encoder_precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
64#ifdef DONT_ESTIMATE_RICE_BITS
65static FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], 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 *bits);
Josh Coalsonb3347bd2001-07-16 18:06:41 +000066static FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 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, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
Josh Coalson8395d022001-07-12 21:25:22 +000067#else
68static FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], 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 *bits);
Josh Coalsonb3347bd2001-07-16 18:06:41 +000069static FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 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, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
Josh Coalson0a15c142001-06-13 17:59:57 +000070#endif
Josh Coalson77e3f312001-06-23 03:03:24 +000071static unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson0a15c142001-06-13 17:59:57 +000072
73/***********************************************************************
74 *
75 * Private class data
76 *
77 ***********************************************************************/
78
79typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +000080 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +000081 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
82 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
83 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
84 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
Josh Coalson8395d022001-07-12 21:25:22 +000085 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
86 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
Josh Coalson77e3f312001-06-23 03:03:24 +000087 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
88 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +000089 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
90 FLAC__Subframe subframe_workspace_mid_side[2][2];
91 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
92 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalson8395d022001-07-12 21:25:22 +000093 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +000094 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +000095 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +000096 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +000097 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +000098 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
Josh Coalson8395d022001-07-12 21:25:22 +000099 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
100 FLAC__BitBuffer frame; /* the current frame being worked on */
101 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
102 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
103 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000104 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000105 FLAC__StreamMetaData metadata;
106 unsigned current_sample_number;
107 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000108 struct MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000109 FLAC__CPUInfo cpuinfo;
Josh Coalson77e3f312001-06-23 03:03:24 +0000110 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
111 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
112 void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
113 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
Josh Coalson8395d022001-07-12 21:25:22 +0000114 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
115 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
116 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
117 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
Josh Coalson77e3f312001-06-23 03:03:24 +0000118 FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
Josh Coalson0a15c142001-06-13 17:59:57 +0000119 void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000120 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000121 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000122 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
123 FLAC__int32 *integer_signal_mid_side_unaligned[2];
124 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
125 FLAC__real *real_signal_mid_side_unaligned[2];
126 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
127 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
128 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000129 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000130 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson0a15c142001-06-13 17:59:57 +0000131} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000132
Josh Coalson0a15c142001-06-13 17:59:57 +0000133/***********************************************************************
134 *
135 * Public static class data
136 *
137 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000138
Josh Coalson0a15c142001-06-13 17:59:57 +0000139const char *FLAC__StreamEncoderStateString[] = {
140 "FLAC__STREAM_ENCODER_OK",
Josh Coalson00e53872001-06-16 07:32:25 +0000141 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
Josh Coalson0a15c142001-06-13 17:59:57 +0000142 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
143 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
144 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
145 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
146 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
147 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
148 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
149 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
150 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
151 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
152 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson8395d022001-07-12 21:25:22 +0000153 "FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE",
Josh Coalson0a15c142001-06-13 17:59:57 +0000154 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
155 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
156 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
157 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
158 "FLAC__STREAM_ENCODER_UNINITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000159};
160
Josh Coalson0a15c142001-06-13 17:59:57 +0000161const char *FLAC__StreamEncoderWriteStatusString[] = {
162 "FLAC__STREAM_ENCODER_WRITE_OK",
163 "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000164};
165
Josh Coalson0a15c142001-06-13 17:59:57 +0000166/***********************************************************************
167 *
168 * Class constructor/destructor
169 *
170 ***********************************************************************/
171FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000172{
Josh Coalson0a15c142001-06-13 17:59:57 +0000173 FLAC__StreamEncoder *encoder;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000174
Josh Coalson0a15c142001-06-13 17:59:57 +0000175 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000176
Josh Coalson0a15c142001-06-13 17:59:57 +0000177 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
178 if(encoder == 0) {
179 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000180 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000181 encoder->protected = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
182 if(encoder->protected == 0) {
183 free(encoder);
184 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000185 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000186 encoder->private = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
187 if(encoder->private == 0) {
188 free(encoder->protected);
189 free(encoder);
190 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000191 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000192
Josh Coalson0a15c142001-06-13 17:59:57 +0000193 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000194
Josh Coalson00e53872001-06-16 07:32:25 +0000195 encoder->protected->streamable_subset = true;
196 encoder->protected->do_mid_side_stereo = false;
197 encoder->protected->loose_mid_side_stereo = false;
198 encoder->protected->channels = 2;
199 encoder->protected->bits_per_sample = 16;
200 encoder->protected->sample_rate = 44100;
201 encoder->protected->blocksize = 1152;
202 encoder->protected->max_lpc_order = 0;
203 encoder->protected->qlp_coeff_precision = 0;
204 encoder->protected->do_qlp_coeff_prec_search = false;
205 encoder->protected->do_exhaustive_model_search = false;
Josh Coalson8395d022001-07-12 21:25:22 +0000206 encoder->protected->do_escape_coding = false;
Josh Coalson00e53872001-06-16 07:32:25 +0000207 encoder->protected->min_residual_partition_order = 0;
208 encoder->protected->max_residual_partition_order = 0;
209 encoder->protected->rice_parameter_search_dist = 0;
210 encoder->protected->total_samples_estimate = 0;
211 encoder->protected->seek_table = 0;
212 encoder->protected->padding = 0;
213 encoder->protected->last_metadata_is_last = true;
214
215 encoder->private->write_callback = 0;
216 encoder->private->metadata_callback = 0;
217 encoder->private->client_data = 0;
218
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000219 return encoder;
220}
221
Josh Coalson0a15c142001-06-13 17:59:57 +0000222void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000223{
Josh Coalson1b689822001-05-31 20:11:02 +0000224 FLAC__ASSERT(encoder != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000225 FLAC__ASSERT(encoder->protected != 0);
226 FLAC__ASSERT(encoder->private != 0);
227
228 free(encoder->private);
229 free(encoder->protected);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000230 free(encoder);
231}
232
Josh Coalson0a15c142001-06-13 17:59:57 +0000233/***********************************************************************
234 *
235 * Public class methods
236 *
237 ***********************************************************************/
238
Josh Coalson00e53872001-06-16 07:32:25 +0000239FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000240{
241 unsigned i;
Josh Coalson0a15c142001-06-13 17:59:57 +0000242 FLAC__StreamMetaData padding_block;
243 FLAC__StreamMetaData seek_table_block;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000244
Josh Coalson1b689822001-05-31 20:11:02 +0000245 FLAC__ASSERT(encoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000246
Josh Coalson0a15c142001-06-13 17:59:57 +0000247 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
248 return encoder->protected->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000249
Josh Coalson0a15c142001-06-13 17:59:57 +0000250 encoder->protected->state = FLAC__STREAM_ENCODER_OK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000251
Josh Coalson00e53872001-06-16 07:32:25 +0000252 if(0 == encoder->private->write_callback || 0 == encoder->private->metadata_callback)
253 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000254
Josh Coalson0a15c142001-06-13 17:59:57 +0000255 if(encoder->protected->channels == 0 || encoder->protected->channels > FLAC__MAX_CHANNELS)
256 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000257
Josh Coalson0a15c142001-06-13 17:59:57 +0000258 if(encoder->protected->do_mid_side_stereo && encoder->protected->channels != 2)
259 return encoder->protected->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000260
Josh Coalson0a15c142001-06-13 17:59:57 +0000261 if(encoder->protected->loose_mid_side_stereo && !encoder->protected->do_mid_side_stereo)
262 return encoder->protected->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000263
Josh Coalson0a15c142001-06-13 17:59:57 +0000264 if(encoder->protected->bits_per_sample >= 32)
265 encoder->protected->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000266
Josh Coalson0a15c142001-06-13 17:59:57 +0000267 if(encoder->protected->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
268 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000269
Josh Coalson0a15c142001-06-13 17:59:57 +0000270 if(encoder->protected->sample_rate == 0 || encoder->protected->sample_rate > FLAC__MAX_SAMPLE_RATE)
271 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000272
Josh Coalson0a15c142001-06-13 17:59:57 +0000273 if(encoder->protected->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected->blocksize > FLAC__MAX_BLOCK_SIZE)
274 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
275
276 if(encoder->protected->blocksize < encoder->protected->max_lpc_order)
277 return encoder->protected->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
278
279 if(encoder->protected->qlp_coeff_precision == 0) {
280 if(encoder->protected->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000281 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
282 /* @@@ until then we'll make a guess */
Josh Coalson0a15c142001-06-13 17:59:57 +0000283 encoder->protected->qlp_coeff_precision = max(5, 2 + encoder->protected->bits_per_sample / 2);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000284 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000285 else if(encoder->protected->bits_per_sample == 16) {
286 if(encoder->protected->blocksize <= 192)
287 encoder->protected->qlp_coeff_precision = 7;
288 else if(encoder->protected->blocksize <= 384)
289 encoder->protected->qlp_coeff_precision = 8;
290 else if(encoder->protected->blocksize <= 576)
291 encoder->protected->qlp_coeff_precision = 9;
292 else if(encoder->protected->blocksize <= 1152)
293 encoder->protected->qlp_coeff_precision = 10;
294 else if(encoder->protected->blocksize <= 2304)
295 encoder->protected->qlp_coeff_precision = 11;
296 else if(encoder->protected->blocksize <= 4608)
297 encoder->protected->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000298 else
Josh Coalson0a15c142001-06-13 17:59:57 +0000299 encoder->protected->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000300 }
301 else {
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000302 encoder->protected->qlp_coeff_precision = min(13, 8*sizeof(FLAC__int32) - encoder->protected->bits_per_sample - 1 - 2); /* @@@ -2 to keep things 32-bit safe */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000303 }
304 }
Josh Coalson77e3f312001-06-23 03:03:24 +0000305 else if(encoder->protected->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected->qlp_coeff_precision + encoder->protected->bits_per_sample >= 8*sizeof(FLAC__uint32) || encoder->protected->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
Josh Coalson0a15c142001-06-13 17:59:57 +0000306 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000307
Josh Coalson0a15c142001-06-13 17:59:57 +0000308 if(encoder->protected->streamable_subset) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000309 /*@@@ add check for blocksize here */
Josh Coalson0a15c142001-06-13 17:59:57 +0000310 if(encoder->protected->bits_per_sample != 8 && encoder->protected->bits_per_sample != 12 && encoder->protected->bits_per_sample != 16 && encoder->protected->bits_per_sample != 20 && encoder->protected->bits_per_sample != 24)
311 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
312 if(encoder->protected->sample_rate > 655350)
313 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000314 }
315
Josh Coalson0a15c142001-06-13 17:59:57 +0000316 if(encoder->protected->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
317 encoder->protected->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
318 if(encoder->protected->min_residual_partition_order >= encoder->protected->max_residual_partition_order)
319 encoder->protected->min_residual_partition_order = encoder->protected->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000320
Josh Coalson0a15c142001-06-13 17:59:57 +0000321 encoder->private->input_capacity = 0;
322 for(i = 0; i < encoder->protected->channels; i++) {
323 encoder->private->integer_signal_unaligned[i] = encoder->private->integer_signal[i] = 0;
324 encoder->private->real_signal_unaligned[i] = encoder->private->real_signal[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000325 }
326 for(i = 0; i < 2; i++) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000327 encoder->private->integer_signal_mid_side_unaligned[i] = encoder->private->integer_signal_mid_side[i] = 0;
328 encoder->private->real_signal_mid_side_unaligned[i] = encoder->private->real_signal_mid_side[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000329 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000330 for(i = 0; i < encoder->protected->channels; i++) {
331 encoder->private->residual_workspace_unaligned[i][0] = encoder->private->residual_workspace[i][0] = 0;
332 encoder->private->residual_workspace_unaligned[i][1] = encoder->private->residual_workspace[i][1] = 0;
333 encoder->private->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000334 }
335 for(i = 0; i < 2; i++) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000336 encoder->private->residual_workspace_mid_side_unaligned[i][0] = encoder->private->residual_workspace_mid_side[i][0] = 0;
337 encoder->private->residual_workspace_mid_side_unaligned[i][1] = encoder->private->residual_workspace_mid_side[i][1] = 0;
338 encoder->private->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000339 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000340 for(i = 0; i < encoder->protected->channels; i++) {
341 encoder->private->subframe_workspace_ptr[i][0] = &encoder->private->subframe_workspace[i][0];
342 encoder->private->subframe_workspace_ptr[i][1] = &encoder->private->subframe_workspace[i][1];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000343 }
344 for(i = 0; i < 2; i++) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000345 encoder->private->subframe_workspace_ptr_mid_side[i][0] = &encoder->private->subframe_workspace_mid_side[i][0];
346 encoder->private->subframe_workspace_ptr_mid_side[i][1] = &encoder->private->subframe_workspace_mid_side[i][1];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000347 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000348 encoder->private->abs_residual_unaligned = encoder->private->abs_residual = 0;
349 encoder->private->abs_residual_partition_sums_unaligned = encoder->private->abs_residual_partition_sums = 0;
350 encoder->private->raw_bits_per_partition_unaligned = encoder->private->raw_bits_per_partition = 0;
351 encoder->private->loose_mid_side_stereo_frames_exact = (double)encoder->protected->sample_rate * 0.4 / (double)encoder->protected->blocksize;
352 encoder->private->loose_mid_side_stereo_frames = (unsigned)(encoder->private->loose_mid_side_stereo_frames_exact + 0.5);
353 if(encoder->private->loose_mid_side_stereo_frames == 0)
354 encoder->private->loose_mid_side_stereo_frames = 1;
355 encoder->private->loose_mid_side_stereo_frame_count = 0;
356 encoder->private->current_sample_number = 0;
357 encoder->private->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000358
Josh Coalson8395d022001-07-12 21:25:22 +0000359 encoder->private->use_wide_by_block = (encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected->blocksize)+1 > 30);
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000360 encoder->private->use_wide_by_order = (encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
Josh Coalson8395d022001-07-12 21:25:22 +0000361 encoder->private->use_wide_by_partition = (false); /*@@@ need to set this */
362
Josh Coalsoncf30f502001-05-23 20:57:44 +0000363 /*
364 * get the CPU info and set the function pointers
365 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000366 FLAC__cpu_info(&encoder->private->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000367 /* first default to the non-asm routines */
Josh Coalson0a15c142001-06-13 17:59:57 +0000368 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
369 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
370 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
371 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000372 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000373#ifndef FLAC__NO_ASM
Josh Coalson021ad3b2001-07-18 00:25:52 +0000374 if(encoder->private->cpuinfo.use_asm) {
Josh Coalsoncf30f502001-05-23 20:57:44 +0000375#ifdef FLAC__CPU_IA32
Josh Coalson021ad3b2001-07-18 00:25:52 +0000376 FLAC__ASSERT(encoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000377#ifdef FLAC__HAS_NASM
Josh Coalson021ad3b2001-07-18 00:25:52 +0000378 if(0 && encoder->private->cpuinfo.data.ia32.sse) {
379 if(encoder->protected->max_lpc_order < 4)
380 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
381 else if(encoder->protected->max_lpc_order < 8)
382 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
383 else if(encoder->protected->max_lpc_order < 12)
384 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
385 else
386 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
387 }
388 else if(0 && encoder->private->cpuinfo.data.ia32._3dnow) /*@@@ turn back on in first beta after 1.0 */
389 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000390 else
Josh Coalsone6499bd2001-06-13 18:11:25 +0000391 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000392 if(encoder->private->cpuinfo.data.ia32.mmx && encoder->private->cpuinfo.data.ia32.cmov)
393 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
394 if(encoder->private->cpuinfo.data.ia32.mmx) {
395 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
396 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
397 }
398 else {
399 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
400 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
401 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000402#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000403#endif
Josh Coalson021ad3b2001-07-18 00:25:52 +0000404 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000405#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000406 /* finally override based on wide-ness if necessary */
407 if(encoder->private->use_wide_by_block) {
408 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
409 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000410
Josh Coalson8395d022001-07-12 21:25:22 +0000411 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
412 encoder->private->precompute_partition_sums = (encoder->protected->max_residual_partition_order > encoder->protected->min_residual_partition_order) || encoder->protected->do_escape_coding;
Josh Coalsoneef56702001-03-30 00:45:22 +0000413
Josh Coalson0a15c142001-06-13 17:59:57 +0000414 if(!stream_encoder_resize_buffers_(encoder, encoder->protected->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000415 /* the above function sets the state for us in case of an error */
Josh Coalson0a15c142001-06-13 17:59:57 +0000416 return encoder->protected->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000417 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000418 FLAC__bitbuffer_init(&encoder->private->frame);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000419
420 /*
421 * write the stream header
422 */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000423
Josh Coalson0a15c142001-06-13 17:59:57 +0000424 if(!FLAC__bitbuffer_clear(&encoder->private->frame))
425 return encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
426 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
427 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000428
Josh Coalson0a15c142001-06-13 17:59:57 +0000429 encoder->private->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson00e53872001-06-16 07:32:25 +0000430 encoder->private->metadata.is_last = (encoder->protected->seek_table == 0 && encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last);
Josh Coalson0a15c142001-06-13 17:59:57 +0000431 encoder->private->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
432 encoder->private->metadata.data.stream_info.min_blocksize = encoder->protected->blocksize; /* this encoder uses the same blocksize for the whole stream */
433 encoder->private->metadata.data.stream_info.max_blocksize = encoder->protected->blocksize;
434 encoder->private->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
435 encoder->private->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
436 encoder->private->metadata.data.stream_info.sample_rate = encoder->protected->sample_rate;
437 encoder->private->metadata.data.stream_info.channels = encoder->protected->channels;
438 encoder->private->metadata.data.stream_info.bits_per_sample = encoder->protected->bits_per_sample;
439 encoder->private->metadata.data.stream_info.total_samples = encoder->protected->total_samples_estimate; /* we will replace this later with the real total */
440 memset(encoder->private->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
441 MD5Init(&encoder->private->md5context);
442 if(!FLAC__add_metadata_block(&encoder->private->metadata, &encoder->private->frame))
443 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000444
Josh Coalson0a15c142001-06-13 17:59:57 +0000445 if(0 != encoder->protected->seek_table) {
446 if(!FLAC__seek_table_is_valid(encoder->protected->seek_table))
447 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE;
448 seek_table_block.type = FLAC__METADATA_TYPE_SEEKTABLE;
Josh Coalson00e53872001-06-16 07:32:25 +0000449 seek_table_block.is_last = (encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last);
Josh Coalson0a15c142001-06-13 17:59:57 +0000450 seek_table_block.length = encoder->protected->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
451 seek_table_block.data.seek_table = *encoder->protected->seek_table;
452 if(!FLAC__add_metadata_block(&seek_table_block, &encoder->private->frame))
453 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson0a72e182001-04-13 19:17:16 +0000454 }
455
Josh Coalsonc692d382001-02-23 21:05:05 +0000456 /* add a PADDING block if requested */
Josh Coalson00e53872001-06-16 07:32:25 +0000457 if(encoder->protected->padding > 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000458 padding_block.type = FLAC__METADATA_TYPE_PADDING;
Josh Coalson00e53872001-06-16 07:32:25 +0000459 padding_block.is_last = encoder->protected->last_metadata_is_last;
460 padding_block.length = encoder->protected->padding;
Josh Coalson0a15c142001-06-13 17:59:57 +0000461 if(!FLAC__add_metadata_block(&padding_block, &encoder->private->frame))
462 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsonc692d382001-02-23 21:05:05 +0000463 }
464
Josh Coalson0a15c142001-06-13 17:59:57 +0000465 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned before writing */
466 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
467 if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, 0, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK)
468 return encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000469
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000470 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalson0a15c142001-06-13 17:59:57 +0000471 encoder->private->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000472 /* ... and clear this to 0 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000473 encoder->private->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000474
Josh Coalson0a15c142001-06-13 17:59:57 +0000475 return encoder->protected->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000476}
477
Josh Coalson0a15c142001-06-13 17:59:57 +0000478void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000479{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000480 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000481
Josh Coalson1b689822001-05-31 20:11:02 +0000482 FLAC__ASSERT(encoder != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000483 if(encoder->protected->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000484 return;
Josh Coalson0a15c142001-06-13 17:59:57 +0000485 if(encoder->private->current_sample_number != 0) {
486 encoder->protected->blocksize = encoder->private->current_sample_number;
487 stream_encoder_process_frame_(encoder, true); /* true => is last frame */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000488 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000489 MD5Final(encoder->private->metadata.data.stream_info.md5sum, &encoder->private->md5context);
490 encoder->private->metadata_callback(encoder, &encoder->private->metadata, encoder->private->client_data);
491 for(i = 0; i < encoder->protected->channels; i++) {
492 if(encoder->private->integer_signal_unaligned[i] != 0) {
493 free(encoder->private->integer_signal_unaligned[i]);
494 encoder->private->integer_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000495 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000496 if(encoder->private->real_signal_unaligned[i] != 0) {
497 free(encoder->private->real_signal_unaligned[i]);
498 encoder->private->real_signal_unaligned[i] = 0;
499 }
500 }
501 for(i = 0; i < 2; i++) {
502 if(encoder->private->integer_signal_mid_side_unaligned[i] != 0) {
503 free(encoder->private->integer_signal_mid_side_unaligned[i]);
504 encoder->private->integer_signal_mid_side_unaligned[i] = 0;
505 }
506 if(encoder->private->real_signal_mid_side_unaligned[i] != 0) {
507 free(encoder->private->real_signal_mid_side_unaligned[i]);
508 encoder->private->real_signal_mid_side_unaligned[i] = 0;
509 }
510 }
511 for(channel = 0; channel < encoder->protected->channels; channel++) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000512 for(i = 0; i < 2; i++) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000513 if(encoder->private->residual_workspace_unaligned[channel][i] != 0) {
514 free(encoder->private->residual_workspace_unaligned[channel][i]);
515 encoder->private->residual_workspace_unaligned[channel][i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000516 }
517 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000518 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000519 for(channel = 0; channel < 2; channel++) {
520 for(i = 0; i < 2; i++) {
521 if(encoder->private->residual_workspace_mid_side_unaligned[channel][i] != 0) {
522 free(encoder->private->residual_workspace_mid_side_unaligned[channel][i]);
523 encoder->private->residual_workspace_mid_side_unaligned[channel][i] = 0;
524 }
525 }
526 }
527 if(encoder->private->abs_residual_unaligned != 0) {
528 free(encoder->private->abs_residual_unaligned);
529 encoder->private->abs_residual_unaligned = 0;
530 }
531 if(encoder->private->abs_residual_partition_sums_unaligned != 0) {
532 free(encoder->private->abs_residual_partition_sums_unaligned);
533 encoder->private->abs_residual_partition_sums_unaligned = 0;
534 }
535 if(encoder->private->raw_bits_per_partition_unaligned != 0) {
536 free(encoder->private->raw_bits_per_partition_unaligned);
537 encoder->private->raw_bits_per_partition_unaligned = 0;
538 }
539 FLAC__bitbuffer_free(&encoder->private->frame);
540
541 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000542}
543
Josh Coalson77e3f312001-06-23 03:03:24 +0000544FLAC__bool FLAC__stream_encoder_set_streamable_subset(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000545{
546 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
547 return false;
548 encoder->protected->streamable_subset = value;
549 return true;
550}
551
Josh Coalson77e3f312001-06-23 03:03:24 +0000552FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000553{
554 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
555 return false;
556 encoder->protected->do_mid_side_stereo = value;
557 return true;
558}
559
Josh Coalson77e3f312001-06-23 03:03:24 +0000560FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000561{
562 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
563 return false;
564 encoder->protected->loose_mid_side_stereo = value;
565 return true;
566}
567
Josh Coalson77e3f312001-06-23 03:03:24 +0000568FLAC__bool FLAC__stream_encoder_set_channels(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000569{
570 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
571 return false;
572 encoder->protected->channels = value;
573 return true;
574}
575
Josh Coalson77e3f312001-06-23 03:03:24 +0000576FLAC__bool FLAC__stream_encoder_set_bits_per_sample(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000577{
578 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
579 return false;
580 encoder->protected->bits_per_sample = value;
581 return true;
582}
583
Josh Coalson77e3f312001-06-23 03:03:24 +0000584FLAC__bool FLAC__stream_encoder_set_sample_rate(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000585{
586 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
587 return false;
588 encoder->protected->sample_rate = value;
589 return true;
590}
591
Josh Coalson77e3f312001-06-23 03:03:24 +0000592FLAC__bool FLAC__stream_encoder_set_blocksize(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000593{
594 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
595 return false;
596 encoder->protected->blocksize = value;
597 return true;
598}
599
Josh Coalson77e3f312001-06-23 03:03:24 +0000600FLAC__bool FLAC__stream_encoder_set_max_lpc_order(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000601{
602 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
603 return false;
604 encoder->protected->max_lpc_order = value;
605 return true;
606}
607
Josh Coalson77e3f312001-06-23 03:03:24 +0000608FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000609{
610 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
611 return false;
612 encoder->protected->qlp_coeff_precision = value;
613 return true;
614}
615
Josh Coalson77e3f312001-06-23 03:03:24 +0000616FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000617{
618 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
619 return false;
620 encoder->protected->do_qlp_coeff_prec_search = value;
621 return true;
622}
623
Josh Coalson8395d022001-07-12 21:25:22 +0000624FLAC__bool FLAC__stream_encoder_set_do_escape_coding(const FLAC__StreamEncoder *encoder, FLAC__bool value)
625{
626 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
627 return false;
628 encoder->protected->do_escape_coding = value;
629 return true;
630}
631
Josh Coalson77e3f312001-06-23 03:03:24 +0000632FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000633{
634 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
635 return false;
636 encoder->protected->do_exhaustive_model_search = value;
637 return true;
638}
639
Josh Coalson77e3f312001-06-23 03:03:24 +0000640FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000641{
642 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
643 return false;
644 encoder->protected->min_residual_partition_order = value;
645 return true;
646}
647
Josh Coalson77e3f312001-06-23 03:03:24 +0000648FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000649{
650 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
651 return false;
652 encoder->protected->max_residual_partition_order = value;
653 return true;
654}
655
Josh Coalson77e3f312001-06-23 03:03:24 +0000656FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000657{
658 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
659 return false;
660 encoder->protected->rice_parameter_search_dist = value;
661 return true;
662}
663
Josh Coalson77e3f312001-06-23 03:03:24 +0000664FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(const FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +0000665{
666 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
667 return false;
668 encoder->protected->total_samples_estimate = value;
669 return true;
670}
671
Josh Coalson77e3f312001-06-23 03:03:24 +0000672FLAC__bool FLAC__stream_encoder_set_seek_table(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData_SeekTable *value)
Josh Coalson00e53872001-06-16 07:32:25 +0000673{
674 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
675 return false;
676 encoder->protected->seek_table = value;
677 return true;
678}
679
Josh Coalson77e3f312001-06-23 03:03:24 +0000680FLAC__bool FLAC__stream_encoder_set_padding(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000681{
682 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
683 return false;
684 encoder->protected->padding = value;
685 return true;
686}
687
Josh Coalson77e3f312001-06-23 03:03:24 +0000688FLAC__bool FLAC__stream_encoder_set_last_metadata_is_last(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000689{
690 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
691 return false;
692 encoder->protected->last_metadata_is_last = value;
693 return true;
694}
695
Josh Coalson77e3f312001-06-23 03:03:24 +0000696FLAC__bool FLAC__stream_encoder_set_write_callback(const FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteStatus (*value)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data))
Josh Coalson00e53872001-06-16 07:32:25 +0000697{
698 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
699 return false;
700 encoder->private->write_callback = value;
701 return true;
702}
703
Josh Coalson77e3f312001-06-23 03:03:24 +0000704FLAC__bool FLAC__stream_encoder_set_metadata_callback(const FLAC__StreamEncoder *encoder, void (*value)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data))
Josh Coalson00e53872001-06-16 07:32:25 +0000705{
706 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
707 return false;
708 encoder->private->metadata_callback = value;
709 return true;
710}
711
Josh Coalson77e3f312001-06-23 03:03:24 +0000712FLAC__bool FLAC__stream_encoder_set_client_data(const FLAC__StreamEncoder *encoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +0000713{
714 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
715 return false;
716 encoder->private->client_data = value;
717 return true;
718}
719
720FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000721{
722 return encoder->protected->state;
723}
724
Josh Coalson77e3f312001-06-23 03:03:24 +0000725FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000726{
727 return encoder->protected->streamable_subset;
728}
729
Josh Coalson77e3f312001-06-23 03:03:24 +0000730FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000731{
732 return encoder->protected->do_mid_side_stereo;
733}
734
Josh Coalson77e3f312001-06-23 03:03:24 +0000735FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000736{
737 return encoder->protected->loose_mid_side_stereo;
738}
739
Josh Coalson00e53872001-06-16 07:32:25 +0000740unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000741{
742 return encoder->protected->channels;
743}
744
Josh Coalson00e53872001-06-16 07:32:25 +0000745unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000746{
747 return encoder->protected->bits_per_sample;
748}
749
Josh Coalson00e53872001-06-16 07:32:25 +0000750unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000751{
752 return encoder->protected->sample_rate;
753}
754
Josh Coalson00e53872001-06-16 07:32:25 +0000755unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000756{
757 return encoder->protected->blocksize;
758}
759
Josh Coalson00e53872001-06-16 07:32:25 +0000760unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000761{
762 return encoder->protected->max_lpc_order;
763}
764
Josh Coalson00e53872001-06-16 07:32:25 +0000765unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000766{
767 return encoder->protected->qlp_coeff_precision;
768}
769
Josh Coalson77e3f312001-06-23 03:03:24 +0000770FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000771{
772 return encoder->protected->do_qlp_coeff_prec_search;
773}
774
Josh Coalson8395d022001-07-12 21:25:22 +0000775FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
776{
777 return encoder->protected->do_escape_coding;
778}
779
Josh Coalson77e3f312001-06-23 03:03:24 +0000780FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000781{
782 return encoder->protected->do_exhaustive_model_search;
783}
784
Josh Coalson00e53872001-06-16 07:32:25 +0000785unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000786{
787 return encoder->protected->min_residual_partition_order;
788}
789
Josh Coalson00e53872001-06-16 07:32:25 +0000790unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000791{
792 return encoder->protected->max_residual_partition_order;
793}
794
Josh Coalson00e53872001-06-16 07:32:25 +0000795unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000796{
797 return encoder->protected->rice_parameter_search_dist;
798}
799
Josh Coalson77e3f312001-06-23 03:03:24 +0000800FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 *buf[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000801{
802 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +0000803 FLAC__int32 x, mid, side;
Josh Coalson0a15c142001-06-13 17:59:57 +0000804 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000805
Josh Coalson1b689822001-05-31 20:11:02 +0000806 FLAC__ASSERT(encoder != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000807 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000808
809 j = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +0000810 if(encoder->protected->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000811 do {
Josh Coalson0a15c142001-06-13 17:59:57 +0000812 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000813 x = mid = side = buf[0][j];
Josh Coalson0a15c142001-06-13 17:59:57 +0000814 encoder->private->integer_signal[0][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000815 encoder->private->real_signal[0][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000816 x = buf[1][j];
Josh Coalson0a15c142001-06-13 17:59:57 +0000817 encoder->private->integer_signal[1][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000818 encoder->private->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000819 mid += x;
820 side -= x;
821 mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
Josh Coalson0a15c142001-06-13 17:59:57 +0000822 encoder->private->integer_signal_mid_side[1][i] = side;
823 encoder->private->integer_signal_mid_side[0][i] = mid;
Josh Coalson77e3f312001-06-23 03:03:24 +0000824 encoder->private->real_signal_mid_side[1][i] = (FLAC__real)side;
825 encoder->private->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson0a15c142001-06-13 17:59:57 +0000826 encoder->private->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000827 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000828 if(i == blocksize) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000829 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +0000830 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000831 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000832 } while(j < samples);
833 }
834 else {
835 do {
Josh Coalson0a15c142001-06-13 17:59:57 +0000836 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000837 for(channel = 0; channel < channels; channel++) {
838 x = buf[channel][j];
Josh Coalson0a15c142001-06-13 17:59:57 +0000839 encoder->private->integer_signal[channel][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000840 encoder->private->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000841 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000842 encoder->private->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +0000843 }
844 if(i == blocksize) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000845 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +0000846 return false;
847 }
848 } while(j < samples);
849 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000850
851 return true;
852}
853
854/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
Josh Coalson77e3f312001-06-23 03:03:24 +0000855FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buf[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000856{
857 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +0000858 FLAC__int32 x, mid, side;
Josh Coalson0a15c142001-06-13 17:59:57 +0000859 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000860
Josh Coalson1b689822001-05-31 20:11:02 +0000861 FLAC__ASSERT(encoder != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000862 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000863
864 j = k = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +0000865 if(encoder->protected->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000866 do {
Josh Coalson0a15c142001-06-13 17:59:57 +0000867 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000868 x = mid = side = buf[k++];
Josh Coalson0a15c142001-06-13 17:59:57 +0000869 encoder->private->integer_signal[0][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000870 encoder->private->real_signal[0][i] = (FLAC__real)x;
Josh Coalsond37d1352001-05-30 23:09:31 +0000871 x = buf[k++];
Josh Coalson0a15c142001-06-13 17:59:57 +0000872 encoder->private->integer_signal[1][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000873 encoder->private->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000874 mid += x;
875 side -= x;
876 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
Josh Coalson0a15c142001-06-13 17:59:57 +0000877 encoder->private->integer_signal_mid_side[1][i] = side;
878 encoder->private->integer_signal_mid_side[0][i] = mid;
Josh Coalson77e3f312001-06-23 03:03:24 +0000879 encoder->private->real_signal_mid_side[1][i] = (FLAC__real)side;
880 encoder->private->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson0a15c142001-06-13 17:59:57 +0000881 encoder->private->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000882 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000883 if(i == blocksize) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000884 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +0000885 return false;
886 }
887 } while(j < samples);
888 }
889 else {
890 do {
Josh Coalson0a15c142001-06-13 17:59:57 +0000891 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000892 for(channel = 0; channel < channels; channel++) {
893 x = buf[k++];
Josh Coalson0a15c142001-06-13 17:59:57 +0000894 encoder->private->integer_signal[channel][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000895 encoder->private->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000896 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000897 encoder->private->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +0000898 }
899 if(i == blocksize) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000900 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +0000901 return false;
902 }
903 } while(j < samples);
904 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000905
906 return true;
907}
908
Josh Coalson77e3f312001-06-23 03:03:24 +0000909FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000910{
Josh Coalson77e3f312001-06-23 03:03:24 +0000911 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +0000912 unsigned i, channel;
913
914 FLAC__ASSERT(new_size > 0);
915 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
916 FLAC__ASSERT(encoder->private->current_sample_number == 0);
917
918 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
919 if(new_size <= encoder->private->input_capacity)
920 return true;
921
922 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +0000923
924 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() requires that the input arrays (in our case the integer signals) have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
925
Josh Coalson0a15c142001-06-13 17:59:57 +0000926 for(i = 0; ok && i < encoder->protected->channels; i++) {
Josh Coalson8395d022001-07-12 21:25:22 +0000927 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private->integer_signal_unaligned[i], &encoder->private->integer_signal[i]);
Josh Coalson0a15c142001-06-13 17:59:57 +0000928 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_unaligned[i], &encoder->private->real_signal[i]);
Josh Coalson8395d022001-07-12 21:25:22 +0000929 memset(encoder->private->integer_signal[i], 0, sizeof(FLAC__int32)*4);
930 encoder->private->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +0000931 }
932 for(i = 0; ok && i < 2; i++) {
Josh Coalson8395d022001-07-12 21:25:22 +0000933 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private->integer_signal_mid_side_unaligned[i], &encoder->private->integer_signal_mid_side[i]);
Josh Coalson0a15c142001-06-13 17:59:57 +0000934 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_mid_side_unaligned[i], &encoder->private->real_signal_mid_side[i]);
Josh Coalson8395d022001-07-12 21:25:22 +0000935 memset(encoder->private->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
936 encoder->private->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +0000937 }
938 for(channel = 0; ok && channel < encoder->protected->channels; channel++) {
939 for(i = 0; ok && i < 2; i++) {
940 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_unaligned[channel][i], &encoder->private->residual_workspace[channel][i]);
941 }
942 }
943 for(channel = 0; ok && channel < 2; channel++) {
944 for(i = 0; ok && i < 2; i++) {
945 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_mid_side_unaligned[channel][i], &encoder->private->residual_workspace_mid_side[channel][i]);
946 }
947 }
948 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private->abs_residual_unaligned, &encoder->private->abs_residual);
Josh Coalson8395d022001-07-12 21:25:22 +0000949 if(encoder->private->precompute_partition_sums || encoder->protected->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000950 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private->abs_residual_partition_sums_unaligned, &encoder->private->abs_residual_partition_sums);
Josh Coalson8395d022001-07-12 21:25:22 +0000951 if(encoder->protected->do_escape_coding)
952 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private->raw_bits_per_partition_unaligned, &encoder->private->raw_bits_per_partition);
Josh Coalson0a15c142001-06-13 17:59:57 +0000953
954 if(ok)
955 encoder->private->input_capacity = new_size;
956 else
957 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
958
959 return ok;
960}
961
962/***********************************************************************
963 *
964 * Private class methods
965 *
966 ***********************************************************************/
967
Josh Coalson77e3f312001-06-23 03:03:24 +0000968FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +0000969{
970 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000971
972 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000973 * Accumulate raw signal to the MD5 signature
974 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000975 /* 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 Coalson0a15c142001-06-13 17:59:57 +0000976 if(!FLAC__MD5Accumulate(&encoder->private->md5context, encoder->private->integer_signal, encoder->protected->channels, encoder->protected->blocksize, (encoder->protected->bits_per_sample+7) / 8)) {
977 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000978 return false;
979 }
980
981 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000982 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000983 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000984 if(!stream_encoder_process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000985 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000986 return false;
987 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000988
989 /*
990 * Zero-pad the frame to a byte_boundary
991 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000992 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->private->frame)) {
993 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000994 return false;
995 }
996
997 /*
Josh Coalson215af572001-03-27 01:15:58 +0000998 * CRC-16 the whole thing
999 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001000 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned */
1001 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
1002 FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__crc16(encoder->private->frame.buffer, encoder->private->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
Josh Coalson215af572001-03-27 01:15:58 +00001003
1004 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001005 * Write it
1006 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001007 if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, encoder->protected->blocksize, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) {
1008 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001009 return false;
1010 }
1011
1012 /*
1013 * Get ready for the next frame
1014 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001015 encoder->private->current_sample_number = 0;
1016 encoder->private->current_frame_number++;
Josh Coalson77e3f312001-06-23 03:03:24 +00001017 encoder->private->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001018 encoder->private->metadata.data.stream_info.min_framesize = min(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.min_framesize);
1019 encoder->private->metadata.data.stream_info.max_framesize = max(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.max_framesize);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001020
1021 return true;
1022}
1023
Josh Coalson77e3f312001-06-23 03:03:24 +00001024FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001025{
1026 FLAC__FrameHeader frame_header;
Josh Coalson0a15c142001-06-13 17:59:57 +00001027 unsigned channel, min_partition_order = encoder->protected->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00001028 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001029
1030 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00001031 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00001032 */
1033 if(is_last_frame) {
1034 max_partition_order = 0;
1035 }
1036 else {
Josh Coalson0a15c142001-06-13 17:59:57 +00001037 unsigned limit = 0, b = encoder->protected->blocksize;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001038 while(!(b & 1)) {
1039 limit++;
1040 b >>= 1;
1041 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001042 max_partition_order = min(encoder->protected->max_residual_partition_order, limit);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001043 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001044 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001045
Josh Coalson8395d022001-07-12 21:25:22 +00001046 precompute_partition_sums = encoder->private->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected->do_escape_coding);
1047
Josh Coalson94e02cd2001-01-25 10:41:06 +00001048 /*
1049 * Setup the frame
1050 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001051 if(!FLAC__bitbuffer_clear(&encoder->private->frame)) {
1052 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001053 return false;
1054 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001055 frame_header.blocksize = encoder->protected->blocksize;
1056 frame_header.sample_rate = encoder->protected->sample_rate;
1057 frame_header.channels = encoder->protected->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001058 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalson0a15c142001-06-13 17:59:57 +00001059 frame_header.bits_per_sample = encoder->protected->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001060 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalson0a15c142001-06-13 17:59:57 +00001061 frame_header.number.frame_number = encoder->private->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001062
1063 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001064 * Figure out what channel assignments to try
1065 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001066 if(encoder->protected->do_mid_side_stereo) {
1067 if(encoder->protected->loose_mid_side_stereo) {
1068 if(encoder->private->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001069 do_independent = true;
1070 do_mid_side = true;
1071 }
1072 else {
Josh Coalson0a15c142001-06-13 17:59:57 +00001073 do_independent = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001074 do_mid_side = !do_independent;
1075 }
1076 }
1077 else {
1078 do_independent = true;
1079 do_mid_side = true;
1080 }
1081 }
1082 else {
1083 do_independent = true;
1084 do_mid_side = false;
1085 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001086
Josh Coalson1b689822001-05-31 20:11:02 +00001087 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001088
1089 /*
Josh Coalson82b73242001-03-28 22:17:05 +00001090 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00001091 */
1092 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +00001093 unsigned w;
Josh Coalson0a15c142001-06-13 17:59:57 +00001094 for(channel = 0; channel < encoder->protected->channels; channel++) {
1095 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal[channel], encoder->protected->blocksize);
1096 encoder->private->subframe_workspace[channel][0].wasted_bits = encoder->private->subframe_workspace[channel][1].wasted_bits = w;
1097 encoder->private->subframe_bps[channel] = encoder->protected->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00001098 }
Josh Coalson859bc542001-03-27 22:22:27 +00001099 }
1100 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00001101 unsigned w;
Josh Coalson0a15c142001-06-13 17:59:57 +00001102 FLAC__ASSERT(encoder->protected->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00001103 for(channel = 0; channel < 2; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001104 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal_mid_side[channel], encoder->protected->blocksize);
1105 encoder->private->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1106 encoder->private->subframe_bps_mid_side[channel] = encoder->protected->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00001107 }
Josh Coalson859bc542001-03-27 22:22:27 +00001108 }
1109
1110 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001111 * First do a normal encoding pass of each independent channel
1112 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001113 if(do_independent) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001114 for(channel = 0; channel < encoder->protected->channels; channel++) {
Josh Coalson8395d022001-07-12 21:25:22 +00001115 if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private->subframe_bps[channel], encoder->private->integer_signal[channel], encoder->private->real_signal[channel], encoder->private->subframe_workspace_ptr[channel], encoder->private->residual_workspace[channel], encoder->private->best_subframe+channel, encoder->private->best_subframe_bits+channel))
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001116 return false;
1117 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001118 }
1119
1120 /*
1121 * Now do mid and side channels if requested
1122 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001123 if(do_mid_side) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001124 FLAC__ASSERT(encoder->protected->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001125
1126 for(channel = 0; channel < 2; channel++) {
Josh Coalson8395d022001-07-12 21:25:22 +00001127 if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private->subframe_bps_mid_side[channel], encoder->private->integer_signal_mid_side[channel], encoder->private->real_signal_mid_side[channel], encoder->private->subframe_workspace_ptr_mid_side[channel], encoder->private->residual_workspace_mid_side[channel], encoder->private->best_subframe_mid_side+channel, encoder->private->best_subframe_bits_mid_side+channel))
Josh Coalson94e02cd2001-01-25 10:41:06 +00001128 return false;
1129 }
1130 }
1131
1132 /*
1133 * Compose the frame bitbuffer
1134 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001135 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00001136 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1137 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001138 FLAC__ChannelAssignment channel_assignment;
1139
Josh Coalson0a15c142001-06-13 17:59:57 +00001140 FLAC__ASSERT(encoder->protected->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001141
Josh Coalson0a15c142001-06-13 17:59:57 +00001142 if(encoder->protected->loose_mid_side_stereo && encoder->private->loose_mid_side_stereo_frame_count > 0) {
1143 channel_assignment = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001144 }
1145 else {
1146 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1147 unsigned min_bits;
1148 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001149
Josh Coalson1b689822001-05-31 20:11:02 +00001150 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001151
1152 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalson0a15c142001-06-13 17:59:57 +00001153 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits [1];
1154 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits_mid_side[1];
1155 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private->best_subframe_bits [1] + encoder->private->best_subframe_bits_mid_side[1];
1156 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private->best_subframe_bits_mid_side[0] + encoder->private->best_subframe_bits_mid_side[1];
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001157
1158 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1159 if(bits[ca] < min_bits) {
1160 min_bits = bits[ca];
1161 channel_assignment = ca;
1162 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001163 }
1164 }
1165
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001166 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001167
Josh Coalson0a15c142001-06-13 17:59:57 +00001168 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1169 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001170 return false;
1171 }
1172
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001173 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001174 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson0a15c142001-06-13 17:59:57 +00001175 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
1176 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001177 break;
1178 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001179 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
1180 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001181 break;
1182 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001183 left_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1184 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001185 break;
1186 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001187 left_subframe = &encoder->private->subframe_workspace_mid_side[0][encoder->private->best_subframe_mid_side[0]];
1188 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001189 break;
1190 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001191 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001192 }
Josh Coalson82b73242001-03-28 22:17:05 +00001193
1194 switch(channel_assignment) {
1195 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson0a15c142001-06-13 17:59:57 +00001196 left_bps = encoder->private->subframe_bps [0];
1197 right_bps = encoder->private->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001198 break;
1199 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001200 left_bps = encoder->private->subframe_bps [0];
1201 right_bps = encoder->private->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001202 break;
1203 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001204 left_bps = encoder->private->subframe_bps_mid_side[1];
1205 right_bps = encoder->private->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001206 break;
1207 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001208 left_bps = encoder->private->subframe_bps_mid_side[0];
1209 right_bps = encoder->private->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001210 break;
1211 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001212 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00001213 }
1214
1215 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalson0a15c142001-06-13 17:59:57 +00001216 if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->private->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001217 return false;
Josh Coalson0a15c142001-06-13 17:59:57 +00001218 if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->private->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001219 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001220 }
1221 else {
Josh Coalson0a15c142001-06-13 17:59:57 +00001222 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1223 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001224 return false;
1225 }
1226
Josh Coalson0a15c142001-06-13 17:59:57 +00001227 for(channel = 0; channel < encoder->protected->channels; channel++) {
1228 if(!stream_encoder_add_subframe_(encoder, &frame_header, encoder->private->subframe_bps[channel], &encoder->private->subframe_workspace[channel][encoder->private->best_subframe[channel]], &encoder->private->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001229 /* the above function sets the state for us in case of an error */
1230 return false;
1231 }
1232 }
1233 }
1234
Josh Coalson0a15c142001-06-13 17:59:57 +00001235 if(encoder->protected->loose_mid_side_stereo) {
1236 encoder->private->loose_mid_side_stereo_frame_count++;
1237 if(encoder->private->loose_mid_side_stereo_frame_count >= encoder->private->loose_mid_side_stereo_frames)
1238 encoder->private->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001239 }
1240
Josh Coalson0a15c142001-06-13 17:59:57 +00001241 encoder->private->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001242
Josh Coalson94e02cd2001-01-25 10:41:06 +00001243 return true;
1244}
1245
Josh Coalson8395d022001-07-12 21:25:22 +00001246FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001247{
Josh Coalson77e3f312001-06-23 03:03:24 +00001248 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1249 FLAC__real lpc_residual_bits_per_sample;
1250 FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected->max_lpc_order might be less; some asm routines need all the space */
1251 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
1252 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001253 unsigned min_lpc_order, max_lpc_order, lpc_order;
1254 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1255 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1256 unsigned rice_parameter;
1257 unsigned _candidate_bits, _best_bits;
1258 unsigned _best_subframe;
1259
1260 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1261 _best_subframe = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +00001262 _best_bits = stream_encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001263
1264 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1265 /* check for constant subframe */
Josh Coalson8395d022001-07-12 21:25:22 +00001266 guess_fixed_order = encoder->private->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 +00001267 if(fixed_residual_bits_per_sample[1] == 0.0) {
1268 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1269 unsigned i, signal_is_constant = true;
1270 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1271 if(integer_signal[0] != integer_signal[i]) {
1272 signal_is_constant = false;
1273 break;
1274 }
1275 }
1276 if(signal_is_constant) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001277 _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001278 if(_candidate_bits < _best_bits) {
1279 _best_subframe = !_best_subframe;
1280 _best_bits = _candidate_bits;
1281 }
1282 }
1283 }
1284 else {
1285 /* encode fixed */
Josh Coalson0a15c142001-06-13 17:59:57 +00001286 if(encoder->protected->do_exhaustive_model_search) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001287 min_fixed_order = 0;
1288 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1289 }
1290 else {
1291 min_fixed_order = max_fixed_order = guess_fixed_order;
1292 }
1293 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson77e3f312001-06-23 03:03:24 +00001294 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001295 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +00001296 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 +00001297#ifndef FLAC__SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +00001298 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001299#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001300 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00001301#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00001302 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1303#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001304 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001305 }
1306 _candidate_bits = stream_encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->private->abs_residual, encoder->private->abs_residual_partition_sums, encoder->private->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected->do_escape_coding, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001307 if(_candidate_bits < _best_bits) {
1308 _best_subframe = !_best_subframe;
1309 _best_bits = _candidate_bits;
1310 }
1311 }
1312
1313 /* encode lpc */
Josh Coalson0a15c142001-06-13 17:59:57 +00001314 if(encoder->protected->max_lpc_order > 0) {
1315 if(encoder->protected->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001316 max_lpc_order = frame_header->blocksize-1;
1317 else
Josh Coalson0a15c142001-06-13 17:59:57 +00001318 max_lpc_order = encoder->protected->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001319 if(max_lpc_order > 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001320 encoder->private->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001321 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1322 if(autoc[0] != 0.0) {
1323 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
Josh Coalson0a15c142001-06-13 17:59:57 +00001324 if(encoder->protected->do_exhaustive_model_search) {
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001325 min_lpc_order = 1;
1326 }
1327 else {
Josh Coalson82b73242001-03-28 22:17:05 +00001328 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 +00001329 min_lpc_order = max_lpc_order = guess_lpc_order;
1330 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001331 if(encoder->protected->do_qlp_coeff_prec_search) {
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001332 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001333 max_qlp_coeff_precision = min(8*sizeof(FLAC__int32) - subframe_bps - 1 - 2, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); /* -2 to keep things 32-bit safe */
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001334 }
1335 else {
Josh Coalson0a15c142001-06-13 17:59:57 +00001336 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected->qlp_coeff_precision;
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001337 }
1338 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1339 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
Josh Coalson77e3f312001-06-23 03:03:24 +00001340 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001341 continue; /* don't even try */
1342 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 +00001343#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001344 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001345#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001346 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00001347#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00001348 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1349#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001350 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001351 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001352 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
Josh Coalson8395d022001-07-12 21:25:22 +00001353 _candidate_bits = stream_encoder_evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private->abs_residual, encoder->private->abs_residual_partition_sums, encoder->private->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, precompute_partition_sums, encoder->protected->do_escape_coding, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001354 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1355 if(_candidate_bits < _best_bits) {
1356 _best_subframe = !_best_subframe;
1357 _best_bits = _candidate_bits;
1358 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001359 }
1360 }
1361 }
1362 }
1363 }
1364 }
1365 }
1366 }
1367
1368 *best_subframe = _best_subframe;
1369 *best_bits = _best_bits;
1370
1371 return true;
1372}
1373
Josh Coalson77e3f312001-06-23 03:03:24 +00001374FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001375{
1376 switch(subframe->type) {
1377 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00001378 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001379 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001380 return false;
1381 }
1382 break;
1383 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00001384 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001385 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001386 return false;
1387 }
1388 break;
1389 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00001390 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001391 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001392 return false;
1393 }
1394 break;
1395 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00001396 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001397 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001398 return false;
1399 }
1400 break;
1401 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001402 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001403 }
1404
1405 return true;
1406}
1407
Josh Coalson77e3f312001-06-23 03:03:24 +00001408unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001409{
1410 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1411 subframe->data.constant.value = signal;
1412
Josh Coalson82b73242001-03-28 22:17:05 +00001413 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 +00001414}
1415
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001416unsigned stream_encoder_evaluate_fixed_subframe_(const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 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, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001417{
1418 unsigned i, residual_bits;
1419 const unsigned residual_samples = blocksize - order;
1420
1421 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1422
1423 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1424
1425 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1426 subframe->data.fixed.residual = residual;
1427
Josh Coalson8395d022001-07-12 21:25:22 +00001428 residual_bits = stream_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, precompute_partition_sums, do_escape_coding, 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 +00001429
1430 subframe->data.fixed.order = order;
1431 for(i = 0; i < order; i++)
1432 subframe->data.fixed.warmup[i] = signal[i];
1433
Josh Coalson82b73242001-03-28 22:17:05 +00001434 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 +00001435}
1436
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001437unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__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, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001438{
Josh Coalson77e3f312001-06-23 03:03:24 +00001439 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001440 unsigned i, residual_bits;
1441 int quantization, ret;
1442 const unsigned residual_samples = blocksize - order;
1443
Josh Coalson82b73242001-03-28 22:17:05 +00001444 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001445 if(ret != 0)
1446 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1447
Josh Coalson92d42402001-05-31 20:53:19 +00001448 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
Josh Coalson0a15c142001-06-13 17:59:57 +00001449 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalson92d42402001-05-31 20:53:19 +00001450 else
Josh Coalson0a15c142001-06-13 17:59:57 +00001451 encoder->private->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001452
1453 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1454
1455 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1456 subframe->data.lpc.residual = residual;
1457
Josh Coalson8395d022001-07-12 21:25:22 +00001458 residual_bits = stream_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, precompute_partition_sums, do_escape_coding, 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 +00001459
1460 subframe->data.lpc.order = order;
1461 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1462 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00001463 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001464 for(i = 0; i < order; i++)
1465 subframe->data.lpc.warmup[i] = signal[i];
1466
Josh Coalson82b73242001-03-28 22:17:05 +00001467 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 +00001468}
1469
Josh Coalson77e3f312001-06-23 03:03:24 +00001470unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001471{
1472 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1473
1474 subframe->data.verbatim.data = signal;
1475
Josh Coalson82b73242001-03-28 22:17:05 +00001476 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 +00001477}
1478
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001479unsigned stream_encoder_find_best_partition_order_(const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 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, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
Josh Coalson94e02cd2001-01-25 10:41:06 +00001480{
Josh Coalson77e3f312001-06-23 03:03:24 +00001481 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001482 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001483 unsigned residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001484 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001485 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001486
Josh Coalson2051dd42001-04-12 22:22:34 +00001487 /* compute abs(residual) for use later */
1488 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1489 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00001490 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00001491 }
1492
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001493 while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order)
1494 max_partition_order--;
1495 FLAC__ASSERT(blocksize >> max_partition_order > predictor_order);
1496 min_partition_order = min(min_partition_order, max_partition_order);
1497
Josh Coalson8395d022001-07-12 21:25:22 +00001498 if(precompute_partition_sums) {
1499 int partition_order;
1500 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001501
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001502 stream_encoder_precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
Josh Coalson8395d022001-07-12 21:25:22 +00001503
1504 if(do_escape_coding)
1505 stream_encoder_precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
1506
1507 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1508#ifdef DONT_ESTIMATE_RICE_BITS
1509 if(!stream_encoder_set_partitioned_rice_with_precompute_(residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits))
Josh Coalsonafcd8772001-04-18 22:59:25 +00001510#else
Josh Coalson8395d022001-07-12 21:25:22 +00001511 if(!stream_encoder_set_partitioned_rice_with_precompute_(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, do_escape_coding, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits))
1512#endif
1513 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001514 FLAC__ASSERT(best_residual_bits != 0);
1515 break;
Josh Coalson8395d022001-07-12 21:25:22 +00001516 }
1517 sum += 1u << partition_order;
1518 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1519 best_residual_bits = residual_bits;
1520 *best_partition_order = partition_order;
1521 best_parameters_index = !best_parameters_index;
1522 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001523 }
1524 }
Josh Coalson8395d022001-07-12 21:25:22 +00001525 else {
1526 unsigned partition_order;
1527 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1528#ifdef DONT_ESTIMATE_RICE_BITS
1529 if(!stream_encoder_set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], &residual_bits))
1530#else
1531 if(!stream_encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], &residual_bits))
Josh Coalsonafcd8772001-04-18 22:59:25 +00001532#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001533 {
1534 FLAC__ASSERT(best_residual_bits != 0);
1535 break;
1536 }
1537 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1538 best_residual_bits = residual_bits;
1539 *best_partition_order = partition_order;
1540 best_parameters_index = !best_parameters_index;
1541 }
1542 }
1543 }
1544
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001545 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1546 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1547
1548 return best_residual_bits;
1549}
1550
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001551void stream_encoder_precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001552{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001553 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001554 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001555 const unsigned blocksize = residual_samples + predictor_order;
1556
Josh Coalsonaef013c2001-04-24 01:25:42 +00001557 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001558 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001559 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00001560 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001561 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001562 const unsigned partitions = 1u << partition_order;
1563 const unsigned default_partition_samples = blocksize >> partition_order;
1564
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001565 FLAC__ASSERT(default_partition_samples > predictor_order);
1566
1567 for(partition = residual_sample = 0; partition < partitions; partition++) {
1568 partition_samples = default_partition_samples;
1569 if(partition == 0)
1570 partition_samples -= predictor_order;
1571 abs_residual_partition_sum = 0;
1572 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1573 abs_r = abs_residual[residual_sample];
1574 abs_residual_partition_sum += abs_r;
1575 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001576 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001577 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001578 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001579 to_partition = partitions;
1580 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001581 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001582
Josh Coalson8395d022001-07-12 21:25:22 +00001583 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00001584 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001585 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001586 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001587 const unsigned partitions = 1u << partition_order;
1588 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00001589 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001590 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001591 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001592 from_partition++;
1593 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001594 }
1595 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001596}
Josh Coalson8395d022001-07-12 21:25:22 +00001597
1598void stream_encoder_precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
1599{
1600 int partition_order;
1601 unsigned from_partition, to_partition = 0;
1602 const unsigned blocksize = residual_samples + predictor_order;
1603
1604 /* first do max_partition_order */
1605 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1606 FLAC__int32 r, residual_partition_min, residual_partition_max;
1607 unsigned silog2_min, silog2_max;
1608 unsigned partition, partition_sample, partition_samples, residual_sample;
1609 const unsigned partitions = 1u << partition_order;
1610 const unsigned default_partition_samples = blocksize >> partition_order;
1611
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001612 FLAC__ASSERT(default_partition_samples > predictor_order);
1613
1614 for(partition = residual_sample = 0; partition < partitions; partition++) {
1615 partition_samples = default_partition_samples;
1616 if(partition == 0)
1617 partition_samples -= predictor_order;
1618 residual_partition_min = residual_partition_max = 0;
1619 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1620 r = residual[residual_sample];
1621 if(r < residual_partition_min)
1622 residual_partition_min = r;
1623 else if(r > residual_partition_max)
1624 residual_partition_max = r;
1625 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00001626 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001627 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
1628 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
1629 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00001630 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001631 to_partition = partitions;
1632 break;
Josh Coalson8395d022001-07-12 21:25:22 +00001633 }
1634
1635 /* now merge partitions for lower orders */
1636 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1637 unsigned m;
1638 unsigned i;
1639 const unsigned partitions = 1u << partition_order;
1640 for(i = 0; i < partitions; i++) {
1641 m = raw_bits_per_partition[from_partition];
1642 from_partition++;
1643 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1644 from_partition++;
1645 to_partition++;
1646 }
1647 }
1648}
Josh Coalson94e02cd2001-01-25 10:41:06 +00001649
Josh Coalson352e0f62001-03-20 22:55:50 +00001650#ifdef VARIABLE_RICE_BITS
1651#undef VARIABLE_RICE_BITS
1652#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001653#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00001654#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00001655#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001656
Josh Coalson8395d022001-07-12 21:25:22 +00001657#ifdef DONT_ESTIMATE_RICE_BITS
1658FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], 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 *bits)
1659#else
1660FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], 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 *bits)
1661#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001662{
Josh Coalson034dfab2001-04-27 19:10:23 +00001663 unsigned rice_parameter, partition_bits;
1664#ifndef NO_RICE_SEARCH
1665 unsigned best_partition_bits;
1666 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1667#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001668 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1669
Josh Coalson1b689822001-05-31 20:11:02 +00001670 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00001671
Josh Coalson94e02cd2001-01-25 10:41:06 +00001672 if(partition_order == 0) {
1673 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001674
Josh Coalson034dfab2001-04-27 19:10:23 +00001675#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001676 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001677 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001678 min_rice_parameter = 0;
1679 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001680 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1681 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00001682 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00001683#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00001684 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1685#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00001686 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001687 }
1688 }
1689 else
1690 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1691
1692 best_partition_bits = 0xffffffff;
1693 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1694#endif
1695#ifdef VARIABLE_RICE_BITS
1696#ifdef FLAC__SYMMETRIC_RICE
1697 partition_bits = (2+rice_parameter) * residual_samples;
1698#else
1699 const unsigned rice_parameter_estimate = rice_parameter-1;
1700 partition_bits = (1+rice_parameter) * residual_samples;
1701#endif
1702#else
1703 partition_bits = 0;
1704#endif
1705 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1706 for(i = 0; i < residual_samples; i++) {
1707#ifdef VARIABLE_RICE_BITS
1708#ifdef FLAC__SYMMETRIC_RICE
1709 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1710#else
1711 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1712#endif
1713#else
1714 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1715#endif
1716 }
1717#ifndef NO_RICE_SEARCH
1718 if(partition_bits < best_partition_bits) {
1719 best_rice_parameter = rice_parameter;
1720 best_partition_bits = partition_bits;
1721 }
1722 }
1723#endif
1724 parameters[0] = best_rice_parameter;
1725 bits_ += best_partition_bits;
1726 }
1727 else {
1728 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001729 unsigned partition_samples;
1730 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00001731 const unsigned partitions = 1u << partition_order;
1732 for(partition = residual_sample = 0; partition < partitions; partition++) {
1733 partition_samples = (residual_samples+predictor_order) >> partition_order;
1734 if(partition == 0) {
1735 if(partition_samples <= predictor_order)
1736 return false;
1737 else
1738 partition_samples -= predictor_order;
1739 }
1740 mean = 0;
1741 save_residual_sample = residual_sample;
1742 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001743 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00001744 residual_sample = save_residual_sample;
1745#ifdef FLAC__SYMMETRIC_RICE
1746 mean += partition_samples >> 1; /* for rounding effect */
1747 mean /= partition_samples;
1748
1749 /* calc rice_parameter = floor(log2(mean)) */
1750 rice_parameter = 0;
1751 mean>>=1;
1752 while(mean) {
1753 rice_parameter++;
1754 mean >>= 1;
1755 }
1756#else
1757 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001758 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00001759 ;
1760#endif
1761 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00001762#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00001763 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1764#endif
1765 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1766 }
1767
1768#ifndef NO_RICE_SEARCH
1769 if(rice_parameter_search_dist) {
1770 if(rice_parameter < rice_parameter_search_dist)
1771 min_rice_parameter = 0;
1772 else
1773 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1774 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1775 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00001776#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00001777 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1778#endif
1779 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1780 }
1781 }
1782 else
1783 min_rice_parameter = max_rice_parameter = rice_parameter;
1784
1785 best_partition_bits = 0xffffffff;
1786 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1787#endif
1788#ifdef VARIABLE_RICE_BITS
1789#ifdef FLAC__SYMMETRIC_RICE
1790 partition_bits = (2+rice_parameter) * partition_samples;
1791#else
1792 const unsigned rice_parameter_estimate = rice_parameter-1;
1793 partition_bits = (1+rice_parameter) * partition_samples;
1794#endif
1795#else
1796 partition_bits = 0;
1797#endif
1798 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1799 save_residual_sample = residual_sample;
1800 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1801#ifdef VARIABLE_RICE_BITS
1802#ifdef FLAC__SYMMETRIC_RICE
1803 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1804#else
1805 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1806#endif
1807#else
1808 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1809#endif
1810 }
1811#ifndef NO_RICE_SEARCH
1812 if(rice_parameter != max_rice_parameter)
1813 residual_sample = save_residual_sample;
1814 if(partition_bits < best_partition_bits) {
1815 best_rice_parameter = rice_parameter;
1816 best_partition_bits = partition_bits;
1817 }
1818 }
1819#endif
1820 parameters[partition] = best_rice_parameter;
1821 bits_ += best_partition_bits;
1822 }
1823 }
1824
1825 *bits = bits_;
1826 return true;
1827}
1828
1829#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001830FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 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, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
Josh Coalson8395d022001-07-12 21:25:22 +00001831#else
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001832FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 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, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
Josh Coalson8395d022001-07-12 21:25:22 +00001833#endif
1834{
1835 unsigned rice_parameter, partition_bits;
1836#ifndef NO_RICE_SEARCH
1837 unsigned best_partition_bits;
1838 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1839#endif
1840 unsigned flat_bits;
1841 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1842
1843 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1844
1845 if(partition_order == 0) {
1846 unsigned i;
1847
1848#ifndef NO_RICE_SEARCH
1849 if(rice_parameter_search_dist) {
1850 if(suggested_rice_parameter < rice_parameter_search_dist)
1851 min_rice_parameter = 0;
1852 else
1853 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1854 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1855 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00001856#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00001857 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1858#endif
1859 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1860 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001861 }
1862 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001863 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001864
Josh Coalson034dfab2001-04-27 19:10:23 +00001865 best_partition_bits = 0xffffffff;
1866 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1867#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001868#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001869#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001870 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001871#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001872 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00001873 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001874#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001875#else
1876 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001877#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001878 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001879 for(i = 0; i < residual_samples; i++) {
1880#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001881#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001882 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001883#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001884 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001885#endif
1886#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001887 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 +00001888#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001889 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001890#ifndef NO_RICE_SEARCH
1891 if(partition_bits < best_partition_bits) {
1892 best_rice_parameter = rice_parameter;
1893 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001894 }
1895 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001896#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001897 if(search_for_escapes) {
1898 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
1899 if(flat_bits <= best_partition_bits) {
1900 raw_bits[0] = raw_bits_per_partition[0];
1901 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1902 best_partition_bits = flat_bits;
1903 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001904 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001905 parameters[0] = best_rice_parameter;
1906 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001907 }
1908 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00001909 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001910 unsigned partition_samples;
1911 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001912 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00001913 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001914 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001915 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001916 if(partition_samples <= predictor_order)
1917 return false;
1918 else
1919 partition_samples -= predictor_order;
1920 }
Josh Coalson05d20792001-06-29 23:12:26 +00001921 mean = abs_residual_partition_sums[partition];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001922#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson05d20792001-06-29 23:12:26 +00001923 mean += partition_samples >> 1; /* for rounding effect */
1924 mean /= partition_samples;
1925
Josh Coalson034dfab2001-04-27 19:10:23 +00001926 /* calc rice_parameter = floor(log2(mean)) */
1927 rice_parameter = 0;
1928 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001929 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001930 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001931 mean >>= 1;
1932 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001933#else
Josh Coalson05d20792001-06-29 23:12:26 +00001934 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001935 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00001936 ;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001937#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001938 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00001939#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00001940 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1941#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001942 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001943 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001944
Josh Coalson034dfab2001-04-27 19:10:23 +00001945#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001946 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001947 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001948 min_rice_parameter = 0;
1949 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001950 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1951 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00001952 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00001953#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00001954 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1955#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00001956 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001957 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001958 }
1959 else
1960 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00001961
Josh Coalson034dfab2001-04-27 19:10:23 +00001962 best_partition_bits = 0xffffffff;
1963 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1964#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001965#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001966#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001967 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001968#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001969 const unsigned rice_parameter_estimate = rice_parameter-1;
1970 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001971#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001972#else
1973 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001974#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001975 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00001976 save_residual_sample = residual_sample;
1977 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001978#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001979#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson4dacd192001-06-06 21:11:44 +00001980 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001981#else
Josh Coalson4dacd192001-06-06 21:11:44 +00001982 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001983#endif
1984#else
Josh Coalson4dacd192001-06-06 21:11:44 +00001985 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 +00001986#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001987 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001988#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00001989 if(rice_parameter != max_rice_parameter)
1990 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00001991 if(partition_bits < best_partition_bits) {
1992 best_rice_parameter = rice_parameter;
1993 best_partition_bits = partition_bits;
1994 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001995 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001996#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001997 if(search_for_escapes) {
1998 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
1999 if(flat_bits <= best_partition_bits) {
2000 raw_bits[partition] = raw_bits_per_partition[partition];
2001 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2002 best_partition_bits = flat_bits;
2003 }
Josh Coalson2051dd42001-04-12 22:22:34 +00002004 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002005 parameters[partition] = best_rice_parameter;
2006 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002007 }
2008 }
2009
2010 *bits = bits_;
2011 return true;
2012}
Josh Coalson859bc542001-03-27 22:22:27 +00002013
Josh Coalson77e3f312001-06-23 03:03:24 +00002014unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00002015{
2016 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00002017 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00002018
2019 for(i = 0; i < samples && !(x&1); i++)
2020 x |= signal[i];
2021
2022 if(x == 0) {
2023 shift = 0;
2024 }
2025 else {
2026 for(shift = 0; !(x&1); shift++)
2027 x >>= 1;
2028 }
2029
2030 if(shift > 0) {
2031 for(i = 0; i < samples; i++)
2032 signal[i] >>= shift;
2033 }
2034
2035 return shift;
2036}