stream_encoder.c 95.7 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2000,2001  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy() */
23
#include "FLAC/assert.h"
24
#include "FLAC/seek_table.h"
25
#include "protected/stream_encoder.h"
Josh Coalson's avatar
Josh Coalson committed
26
#include "private/bitbuffer.h"
27
#include "private/bitmath.h"
28
#include "private/crc.h"
29
#include "private/cpu.h"
30
#include "private/stream_encoder_framing.h"
Josh Coalson's avatar
Josh Coalson committed
31
32
#include "private/fixed.h"
#include "private/lpc.h"
Josh Coalson's avatar
Josh Coalson committed
33
#include "private/md5.h"
34
#include "private/memory.h"
Josh Coalson's avatar
Josh Coalson committed
35
36
37
38
39
40
41
42
43
44
45

#ifdef min
#undef min
#endif
#define min(x,y) ((x)<(y)?(x):(y))

#ifdef max
#undef max
#endif
#define max(x,y) ((x)>(y)?(x):(y))

46
47
48
49
50
51
/***********************************************************************
 *
 * Private class method prototypes
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
52
53
54
static FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
static FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
static FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
55
static 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 Coalson's avatar
Josh Coalson committed
56
57
static FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
static unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
58
59
static 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);
static 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 Coalson's avatar
Josh Coalson committed
60
static unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
61
62
static 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[]);
static 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);
63
64
65
static 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);
#ifdef DONT_ESTIMATE_RICE_BITS
static 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);
66
static 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);
67
68
#else
static 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);
69
static 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);
70
#endif
Josh Coalson's avatar
Josh Coalson committed
71
static unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
72
73
74
75
76
77
78
79

/***********************************************************************
 *
 * Private class data
 *
 ***********************************************************************/

typedef struct FLAC__StreamEncoderPrivate {
80
	unsigned input_capacity;                          /* current size (in samples) of the signal and residual buffers */
Josh Coalson's avatar
Josh Coalson committed
81
82
83
84
	FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
	FLAC__int32 *integer_signal_mid_side[2];          /* the integer version of the mid-side input signal (stereo only) */
	FLAC__real *real_signal[FLAC__MAX_CHANNELS];      /* the floating-point version of the input signal */
	FLAC__real *real_signal_mid_side[2];              /* the floating-point version of the mid-side input signal (stereo only) */
85
86
	unsigned subframe_bps[FLAC__MAX_CHANNELS];        /* the effective bits per sample of the input signal (stream bps - wasted bits) */
	unsigned subframe_bps_mid_side[2];                /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
Josh Coalson's avatar
Josh Coalson committed
87
88
	FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
	FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson's avatar
Josh Coalson committed
89
90
91
92
	FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
	FLAC__Subframe subframe_workspace_mid_side[2][2];
	FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
	FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
93
	unsigned best_subframe[FLAC__MAX_CHANNELS];       /* index into the above workspaces */
Josh Coalson's avatar
Josh Coalson committed
94
	unsigned best_subframe_mid_side[2];
95
	unsigned best_subframe_bits[FLAC__MAX_CHANNELS];  /* size in bits of the best subframe for each channel */
Josh Coalson's avatar
Josh Coalson committed
96
	unsigned best_subframe_bits_mid_side[2];
Josh Coalson's avatar
Josh Coalson committed
97
	FLAC__uint32 *abs_residual;                       /* workspace where abs(candidate residual) is stored */
98
	FLAC__uint64 *abs_residual_partition_sums;        /* workspace where the sum of abs(candidate residual) for each partition is stored */
99
100
101
102
103
	unsigned *raw_bits_per_partition;                 /* workspace where the sum of silog2(candidate residual) for each partition is stored */
	FLAC__BitBuffer frame;                            /* the current frame being worked on */
	double loose_mid_side_stereo_frames_exact;        /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
	unsigned loose_mid_side_stereo_frames;            /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
	unsigned loose_mid_side_stereo_frame_count;       /* number of frames using the current channel assignment */
104
	FLAC__ChannelAssignment last_channel_assignment;
Josh Coalson's avatar
Josh Coalson committed
105
106
107
	FLAC__StreamMetaData metadata;
	unsigned current_sample_number;
	unsigned current_frame_number;
Josh Coalson's avatar
Josh Coalson committed
108
	struct MD5Context md5context;
109
	FLAC__CPUInfo cpuinfo;
Josh Coalson's avatar
Josh Coalson committed
110
111
112
113
	unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
	void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
	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[]);
	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[]);
114
115
116
117
	FLAC__bool use_wide_by_block;                     /* use slow 64-bit versions of some functions because of the block size */
	FLAC__bool use_wide_by_partition;                 /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
	FLAC__bool use_wide_by_order;                     /* use slow 64-bit versions of some functions because of the lpc order */
	FLAC__bool precompute_partition_sums;             /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
Josh Coalson's avatar
Josh Coalson committed
118
	FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
119
	void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
Josh Coalson's avatar
Josh Coalson committed
120
	void *client_data;
121
	/* unaligned (original) pointers to allocated data */
Josh Coalson's avatar
Josh Coalson committed
122
123
124
125
126
127
128
	FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
	FLAC__int32 *integer_signal_mid_side_unaligned[2];
	FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
	FLAC__real *real_signal_mid_side_unaligned[2];
	FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
	FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
	FLAC__uint32 *abs_residual_unaligned;
129
	FLAC__uint64 *abs_residual_partition_sums_unaligned;
130
	unsigned *raw_bits_per_partition_unaligned;
131
} FLAC__StreamEncoderPrivate;
Josh Coalson's avatar
Josh Coalson committed
132

133
134
135
136
137
138
139
140
/***********************************************************************
 *
 * Public static class data
 *
 ***********************************************************************/

const char *FLAC__StreamEncoderStateString[] = {
	"FLAC__STREAM_ENCODER_OK",
141
	"FLAC__STREAM_ENCODER_INVALID_CALLBACK",
142
143
144
145
146
147
148
149
150
151
152
	"FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
	"FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
	"FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
	"FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
	"FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
	"FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
	"FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
	"FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
	"FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
	"FLAC__STREAM_ENCODER_NOT_STREAMABLE",
	"FLAC__STREAM_ENCODER_FRAMING_ERROR",
153
	"FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE",
154
155
156
157
158
	"FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
	"FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
	"FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
	"FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
	"FLAC__STREAM_ENCODER_UNINITIALIZED"
Josh Coalson's avatar
Josh Coalson committed
159
160
};

161
162
163
const char *FLAC__StreamEncoderWriteStatusString[] = {
	"FLAC__STREAM_ENCODER_WRITE_OK",
	"FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR"
Josh Coalson's avatar
Josh Coalson committed
164
165
};

166
167
168
169
170
171
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/
FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalson's avatar
Josh Coalson committed
172
{
173
	FLAC__StreamEncoder *encoder;
Josh Coalson's avatar
Josh Coalson committed
174

175
	FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalson's avatar
Josh Coalson committed
176

177
178
179
	encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
	if(encoder == 0) {
		return 0;
180
	}
181
182
	encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
	if(encoder->protected_ == 0) {
183
184
		free(encoder);
		return 0;
Josh Coalson's avatar
Josh Coalson committed
185
	}
186
187
188
	encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
	if(encoder->private_ == 0) {
		free(encoder->protected_);
189
190
		free(encoder);
		return 0;
Josh Coalson's avatar
Josh Coalson committed
191
	}
192

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;

	encoder->protected_->streamable_subset = true;
	encoder->protected_->do_mid_side_stereo = false;
	encoder->protected_->loose_mid_side_stereo = false;
	encoder->protected_->channels = 2;
	encoder->protected_->bits_per_sample = 16;
	encoder->protected_->sample_rate = 44100;
	encoder->protected_->blocksize = 1152;
	encoder->protected_->max_lpc_order = 0;
	encoder->protected_->qlp_coeff_precision = 0;
	encoder->protected_->do_qlp_coeff_prec_search = false;
	encoder->protected_->do_exhaustive_model_search = false;
	encoder->protected_->do_escape_coding = false;
	encoder->protected_->min_residual_partition_order = 0;
	encoder->protected_->max_residual_partition_order = 0;
	encoder->protected_->rice_parameter_search_dist = 0;
	encoder->protected_->total_samples_estimate = 0;
	encoder->protected_->seek_table = 0;
	encoder->protected_->padding = 0;
	encoder->protected_->last_metadata_is_last = true;

	encoder->private_->write_callback = 0;
	encoder->private_->metadata_callback = 0;
	encoder->private_->client_data = 0;
218

Josh Coalson's avatar
Josh Coalson committed
219
220
221
	return encoder;
}

222
void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalson's avatar
Josh Coalson committed
223
{
224
	FLAC__ASSERT(encoder != 0);
225
226
	FLAC__ASSERT(encoder->protected_ != 0);
	FLAC__ASSERT(encoder->private_ != 0);
227

228
229
	free(encoder->private_);
	free(encoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
230
231
232
	free(encoder);
}

233
234
235
236
237
238
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

239
FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
Josh Coalson's avatar
Josh Coalson committed
240
241
{
	unsigned i;
242
243
	FLAC__StreamMetaData padding_block;
	FLAC__StreamMetaData seek_table_block;
Josh Coalson's avatar
Josh Coalson committed
244

245
	FLAC__ASSERT(encoder != 0);
Josh Coalson's avatar
Josh Coalson committed
246

247
248
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
249

250
	encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
Josh Coalson's avatar
Josh Coalson committed
251

252
253
	if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
Josh Coalson's avatar
Josh Coalson committed
254

255
256
	if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson's avatar
Josh Coalson committed
257

258
259
	if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
260

261
262
	if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
263

264
265
	if(encoder->protected_->bits_per_sample >= 32)
		encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
Josh Coalson's avatar
Josh Coalson committed
266

267
268
	if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
Josh Coalson's avatar
Josh Coalson committed
269

270
271
	if(encoder->protected_->sample_rate == 0 || encoder->protected_->sample_rate > FLAC__MAX_SAMPLE_RATE)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
Josh Coalson's avatar
Josh Coalson committed
272

273
274
	if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
Josh Coalson's avatar
Josh Coalson committed
275

276
277
	if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
278

279
280
	if(encoder->protected_->qlp_coeff_precision == 0) {
		if(encoder->protected_->bits_per_sample < 16) {
Josh Coalson's avatar
Josh Coalson committed
281
282
			/* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
			/* @@@ until then we'll make a guess */
283
			encoder->protected_->qlp_coeff_precision = max(5, 2 + encoder->protected_->bits_per_sample / 2);
Josh Coalson's avatar
Josh Coalson committed
284
		}
285
286
287
288
289
290
291
292
293
294
295
296
297
		else if(encoder->protected_->bits_per_sample == 16) {
			if(encoder->protected_->blocksize <= 192)
				encoder->protected_->qlp_coeff_precision = 7;
			else if(encoder->protected_->blocksize <= 384)
				encoder->protected_->qlp_coeff_precision = 8;
			else if(encoder->protected_->blocksize <= 576)
				encoder->protected_->qlp_coeff_precision = 9;
			else if(encoder->protected_->blocksize <= 1152)
				encoder->protected_->qlp_coeff_precision = 10;
			else if(encoder->protected_->blocksize <= 2304)
				encoder->protected_->qlp_coeff_precision = 11;
			else if(encoder->protected_->blocksize <= 4608)
				encoder->protected_->qlp_coeff_precision = 12;
Josh Coalson's avatar
Josh Coalson committed
298
			else
299
				encoder->protected_->qlp_coeff_precision = 13;
Josh Coalson's avatar
Josh Coalson committed
300
301
		}
		else {
302
			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 Coalson's avatar
Josh Coalson committed
303
304
		}
	}
305
306
	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))
		return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
Josh Coalson's avatar
Josh Coalson committed
307

308
	if(encoder->protected_->streamable_subset) {
309
		/*@@@ add check for blocksize here */
310
311
312
313
		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)
			return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
		if(encoder->protected_->sample_rate > 655350)
			return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalson's avatar
Josh Coalson committed
314
315
	}

316
317
318
319
	if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
		encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
	if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
		encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalson's avatar
Josh Coalson committed
320

321
322
323
324
	encoder->private_->input_capacity = 0;
	for(i = 0; i < encoder->protected_->channels; i++) {
		encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
		encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
325
326
	}
	for(i = 0; i < 2; i++) {
327
328
		encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
		encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
329
	}
330
331
332
333
	for(i = 0; i < encoder->protected_->channels; i++) {
		encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
		encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
		encoder->private_->best_subframe[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
334
335
	}
	for(i = 0; i < 2; i++) {
336
337
338
		encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
		encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
		encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
339
	}
340
341
342
	for(i = 0; i < encoder->protected_->channels; i++) {
		encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
		encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
Josh Coalson's avatar
Josh Coalson committed
343
344
	}
	for(i = 0; i < 2; i++) {
345
346
		encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
		encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
Josh Coalson's avatar
Josh Coalson committed
347
	}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
	encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
	encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
	encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
	encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
	encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
	if(encoder->private_->loose_mid_side_stereo_frames == 0)
		encoder->private_->loose_mid_side_stereo_frames = 1;
	encoder->private_->loose_mid_side_stereo_frame_count = 0;
	encoder->private_->current_sample_number = 0;
	encoder->private_->current_frame_number = 0;

	encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
	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? */
	encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
362

363
364
365
	/*
	 * get the CPU info and set the function pointers
	 */
366
	FLAC__cpu_info(&encoder->private_->cpuinfo);
367
	/* first default to the non-asm routines */
368
369
370
371
	encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
	encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
	encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
	encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
372
	/* now override with asm where appropriate */
373
#ifndef FLAC__NO_ASM
374
	if(encoder->private_->cpuinfo.use_asm) {
375
#ifdef FLAC__CPU_IA32
376
		FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
377
#ifdef FLAC__HAS_NASM
378
379
380
381
382
383
384
		if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
			if(encoder->protected_->max_lpc_order < 4)
				encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
			else if(encoder->protected_->max_lpc_order < 8)
				encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
			else if(encoder->protected_->max_lpc_order < 12)
				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
389
		else if(0 && encoder->private_->cpuinfo.data.ia32._3dnow) /*@@@ turn back on in first beta after 1.0 */
			encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
390
		else
391
392
393
394
395
396
			encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
		if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
			encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
		if(encoder->private_->cpuinfo.data.ia32.mmx) {
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
397
398
		}
		else {
399
400
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
			encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
401
		}
402
#endif
403
#endif
404
	}
405
#endif
406
	/* finally override based on wide-ness if necessary */
407
408
	if(encoder->private_->use_wide_by_block) {
		encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
409
	}
410

411
	/* 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;
413

414
	if(!stream_encoder_resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalson's avatar
Josh Coalson committed
415
		/* the above function sets the state for us in case of an error */
416
		return encoder->protected_->state;
Josh Coalson's avatar
Josh Coalson committed
417
	}
418
	FLAC__bitbuffer_init(&encoder->private_->frame);
Josh Coalson's avatar
Josh Coalson committed
419
420
421
422

	/*
	 * write the stream header
	 */
423

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
	if(!FLAC__bitbuffer_clear(&encoder->private_->frame))
		return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
	if(!FLAC__bitbuffer_write_raw_uint32(&encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
		return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;

	encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
	encoder->private_->metadata.is_last = (encoder->protected_->seek_table == 0 && encoder->protected_->padding == 0 && encoder->protected_->last_metadata_is_last);
	encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
	encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
	encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
	encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
	encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
	encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
	encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
	encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
	encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
	memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
	MD5Init(&encoder->private_->md5context);
	if(!FLAC__add_metadata_block(&encoder->private_->metadata, &encoder->private_->frame))
		return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;

	if(0 != encoder->protected_->seek_table) {
		if(!FLAC__seek_table_is_valid(encoder->protected_->seek_table))
			return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE;
448
		seek_table_block.type = FLAC__METADATA_TYPE_SEEKTABLE;
449
450
451
452
453
		seek_table_block.is_last = (encoder->protected_->padding == 0 && encoder->protected_->last_metadata_is_last);
		seek_table_block.length = encoder->protected_->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
		seek_table_block.data.seek_table = *encoder->protected_->seek_table;
		if(!FLAC__add_metadata_block(&seek_table_block, &encoder->private_->frame))
			return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
454
455
	}

456
	/* add a PADDING block if requested */
457
	if(encoder->protected_->padding > 0) {
458
		padding_block.type = FLAC__METADATA_TYPE_PADDING;
459
460
461
462
		padding_block.is_last = encoder->protected_->last_metadata_is_last;
		padding_block.length = encoder->protected_->padding;
		if(!FLAC__add_metadata_block(&padding_block, &encoder->private_->frame))
			return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
463
464
	}

465
466
467
468
	FLAC__ASSERT(encoder->private_->frame.bits == 0); /* assert that we're byte-aligned before writing */
	FLAC__ASSERT(encoder->private_->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
	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)
		return encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalson's avatar
Josh Coalson committed
469

Josh Coalson's avatar
Josh Coalson committed
470
	/* now that the metadata block is written, we can init this to an absurdly-high value... */
471
	encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
Josh Coalson's avatar
Josh Coalson committed
472
	/* ... and clear this to 0 */
473
	encoder->private_->metadata.data.stream_info.total_samples = 0;
Josh Coalson's avatar
Josh Coalson committed
474

475
	return encoder->protected_->state;
Josh Coalson's avatar
Josh Coalson committed
476
477
}

478
void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalson's avatar
Josh Coalson committed
479
{
Josh Coalson's avatar
Josh Coalson committed
480
	unsigned i, channel;
Josh Coalson's avatar
Josh Coalson committed
481

482
	FLAC__ASSERT(encoder != 0);
483
	if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson's avatar
Josh Coalson committed
484
		return;
485
486
	if(encoder->private_->current_sample_number != 0) {
		encoder->protected_->blocksize = encoder->private_->current_sample_number;
487
		stream_encoder_process_frame_(encoder, true); /* true => is last frame */
Josh Coalson's avatar
Josh Coalson committed
488
	}
489
490
491
492
493
494
	MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
	encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
	for(i = 0; i < encoder->protected_->channels; i++) {
		if(encoder->private_->integer_signal_unaligned[i] != 0) {
			free(encoder->private_->integer_signal_unaligned[i]);
			encoder->private_->integer_signal_unaligned[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
495
		}
496
497
498
		if(encoder->private_->real_signal_unaligned[i] != 0) {
			free(encoder->private_->real_signal_unaligned[i]);
			encoder->private_->real_signal_unaligned[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
499
		}
500
501
	}
	for(i = 0; i < 2; i++) {
502
503
504
		if(encoder->private_->integer_signal_mid_side_unaligned[i] != 0) {
			free(encoder->private_->integer_signal_mid_side_unaligned[i]);
			encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
505
		}
506
507
508
		if(encoder->private_->real_signal_mid_side_unaligned[i] != 0) {
			free(encoder->private_->real_signal_mid_side_unaligned[i]);
			encoder->private_->real_signal_mid_side_unaligned[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
509
		}
510
	}
511
	for(channel = 0; channel < encoder->protected_->channels; channel++) {
512
		for(i = 0; i < 2; i++) {
513
514
515
			if(encoder->private_->residual_workspace_unaligned[channel][i] != 0) {
				free(encoder->private_->residual_workspace_unaligned[channel][i]);
				encoder->private_->residual_workspace_unaligned[channel][i] = 0;
516
			}
517
		}
518
519
520
	}
	for(channel = 0; channel < 2; channel++) {
		for(i = 0; i < 2; i++) {
521
522
523
			if(encoder->private_->residual_workspace_mid_side_unaligned[channel][i] != 0) {
				free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
				encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
524
			}
525
		}
Josh Coalson's avatar
Josh Coalson committed
526
	}
527
528
529
	if(encoder->private_->abs_residual_unaligned != 0) {
		free(encoder->private_->abs_residual_unaligned);
		encoder->private_->abs_residual_unaligned = 0;
530
	}
531
532
533
	if(encoder->private_->abs_residual_partition_sums_unaligned != 0) {
		free(encoder->private_->abs_residual_partition_sums_unaligned);
		encoder->private_->abs_residual_partition_sums_unaligned = 0;
534
	}
535
536
537
	if(encoder->private_->raw_bits_per_partition_unaligned != 0) {
		free(encoder->private_->raw_bits_per_partition_unaligned);
		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;
542
543
}

Josh Coalson's avatar
Josh Coalson committed
544
FLAC__bool FLAC__stream_encoder_set_streamable_subset(const FLAC__StreamEncoder *encoder, FLAC__bool value)
545
{
546
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
547
		return false;
548
	encoder->protected_->streamable_subset = value;
549
550
551
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
552
FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
553
{
554
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
555
		return false;
556
	encoder->protected_->do_mid_side_stereo = value;
557
558
559
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
560
FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
561
{
562
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
563
		return false;
564
	encoder->protected_->loose_mid_side_stereo = value;
565
566
567
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
568
FLAC__bool FLAC__stream_encoder_set_channels(const FLAC__StreamEncoder *encoder, unsigned value)
569
{
570
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
571
		return false;
572
	encoder->protected_->channels = value;
573
574
575
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
576
FLAC__bool FLAC__stream_encoder_set_bits_per_sample(const FLAC__StreamEncoder *encoder, unsigned value)
577
{
578
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
579
		return false;
580
	encoder->protected_->bits_per_sample = value;
581
582
583
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
584
FLAC__bool FLAC__stream_encoder_set_sample_rate(const FLAC__StreamEncoder *encoder, unsigned value)
585
{
586
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
587
		return false;
588
	encoder->protected_->sample_rate = value;
589
590
591
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
592
FLAC__bool FLAC__stream_encoder_set_blocksize(const FLAC__StreamEncoder *encoder, unsigned value)
593
{
594
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
595
		return false;
596
	encoder->protected_->blocksize = value;
597
598
599
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
600
FLAC__bool FLAC__stream_encoder_set_max_lpc_order(const FLAC__StreamEncoder *encoder, unsigned value)
601
{
602
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
603
		return false;
604
	encoder->protected_->max_lpc_order = value;
605
606
607
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
608
FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(const FLAC__StreamEncoder *encoder, unsigned value)
609
{
610
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
611
		return false;
612
	encoder->protected_->qlp_coeff_precision = value;
613
614
615
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
616
FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
617
{
618
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
619
		return false;
620
	encoder->protected_->do_qlp_coeff_prec_search = value;
621
622
623
	return true;
}

624
625
FLAC__bool FLAC__stream_encoder_set_do_escape_coding(const FLAC__StreamEncoder *encoder, FLAC__bool value)
{
626
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
627
		return false;
628
	encoder->protected_->do_escape_coding = value;
629
630
631
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
632
FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
633
{
634
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
635
		return false;
636
	encoder->protected_->do_exhaustive_model_search = value;
637
638
639
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
640
FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
641
{
642
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
643
		return false;
644
	encoder->protected_->min_residual_partition_order = value;
645
646
647
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
648
FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
649
{
650
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
651
		return false;
652
	encoder->protected_->max_residual_partition_order = value;
653
654
655
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
656
FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder, unsigned value)
657
{
658
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
659
		return false;
660
	encoder->protected_->rice_parameter_search_dist = value;
661
662
663
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
664
FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(const FLAC__StreamEncoder *encoder, FLAC__uint64 value)
665
{
666
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
667
		return false;
668
	encoder->protected_->total_samples_estimate = value;
669
670
671
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
672
FLAC__bool FLAC__stream_encoder_set_seek_table(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData_SeekTable *value)
673
{
674
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
675
		return false;
676
	encoder->protected_->seek_table = value;
677
678
679
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
680
FLAC__bool FLAC__stream_encoder_set_padding(const FLAC__StreamEncoder *encoder, unsigned value)
681
{
682
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
683
		return false;
684
	encoder->protected_->padding = value;
685
686
687
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
688
FLAC__bool FLAC__stream_encoder_set_last_metadata_is_last(const FLAC__StreamEncoder *encoder, FLAC__bool value)
689
{
690
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
691
		return false;
692
	encoder->protected_->last_metadata_is_last = value;
693
694
695
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
696
FLAC__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))
697
{
698
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
699
		return false;
700
	encoder->private_->write_callback = value;
701
702
703
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
704
FLAC__bool FLAC__stream_encoder_set_metadata_callback(const FLAC__StreamEncoder *encoder, void (*value)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data))
705
{
706
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
707
		return false;
708
	encoder->private_->metadata_callback = value;
709
710
711
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
712
FLAC__bool FLAC__stream_encoder_set_client_data(const FLAC__StreamEncoder *encoder, void *value)
713
{
714
	if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
715
		return false;
716
	encoder->private_->client_data = value;
717
718
719
720
	return true;
}

FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
721
{
722
	return encoder->protected_->state;
723
724
}

Josh Coalson's avatar
Josh Coalson committed
725
FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
726
{
727
	return encoder->protected_->streamable_subset;
728
729
}

Josh Coalson's avatar
Josh Coalson committed
730
FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
731
{
732
	return encoder->protected_->do_mid_side_stereo;
733
734
}

Josh Coalson's avatar
Josh Coalson committed
735
FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
736
{
737
	return encoder->protected_->loose_mid_side_stereo;
738
739
}

740
unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
741
{
742
	return encoder->protected_->channels;
743
744
}

745
unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
746
{
747
	return encoder->protected_->bits_per_sample;
748
749
}

750
unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
751
{
752
	return encoder->protected_->sample_rate;
753
754
}

755
unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
756
{
757
	return encoder->protected_->blocksize;
758
759
}

760
unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
761
{
762
	return encoder->protected_->max_lpc_order;
763
764
}

765
unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
766
{
767
	return encoder->protected_->qlp_coeff_precision;
768
769
}

Josh Coalson's avatar
Josh Coalson committed
770
FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
771
{
772
	return encoder->protected_->do_qlp_coeff_prec_search;
773
774
}

775
776
FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
{
777
	return encoder->protected_->do_escape_coding;
778
779
}

Josh Coalson's avatar
Josh Coalson committed
780
FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
781
{
782
	return encoder->protected_->do_exhaustive_model_search;
783
784
}

785
unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
786
{
787
	return encoder->protected_->min_residual_partition_order;
788
789
}

790
unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
791
{
792
	return encoder->protected_->max_residual_partition_order;
Josh Coalson's avatar
Josh Coalson committed
793
794
}

795
unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
796
{
797
	return encoder->protected_->rice_parameter_search_dist;
798
799
}

Josh Coalson's avatar
Josh Coalson committed
800
FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 *buf[], unsigned samples)
Josh Coalson's avatar
Josh Coalson committed
801
802
{
	unsigned i, j, channel;
Josh Coalson's avatar
Josh Coalson committed
803
	FLAC__int32 x, mid, side;
804
	const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalson's avatar
Josh Coalson committed
805