blob: e07ecfb78bed32f887011ba592b112323a7c764a [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson305ae2e2002-01-26 17:36:39 +00002 * Copyright (C) 2000,2001,2002 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 Coalsone6b3bbe2002-10-08 06:03:25 +000020#include <limits.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000021#include <stdio.h>
22#include <stdlib.h> /* for malloc() */
23#include <string.h> /* for memcpy() */
Josh Coalson1b689822001-05-31 20:11:02 +000024#include "FLAC/assert.h"
Josh Coalsond86e03b2002-08-03 21:56:15 +000025#include "FLAC/stream_decoder.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000026#include "protected/stream_encoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000027#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000028#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000029#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000030#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000031#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000032#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000033#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000034#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000035#include "private/memory.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000036#include "private/stream_encoder_framing.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000037
Josh Coalson5e31be12002-12-04 07:07:35 +000038#ifdef HAVE_CONFIG_H
39#include <config.h>
40#endif
41
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000042#ifdef min
43#undef min
44#endif
45#define min(x,y) ((x)<(y)?(x):(y))
46
47#ifdef max
48#undef max
49#endif
50#define max(x,y) ((x)>(y)?(x):(y))
51
Josh Coalsond86e03b2002-08-03 21:56:15 +000052typedef struct {
53 FLAC__int32 *data[FLAC__MAX_CHANNELS];
54 unsigned size; /* of each data[] in samples */
55 unsigned tail;
56} verify_input_fifo;
57
58typedef struct {
59 const FLAC__byte *data;
60 unsigned capacity;
61 unsigned bytes;
62} verify_output;
63
64typedef enum {
65 ENCODER_IN_MAGIC = 0,
66 ENCODER_IN_METADATA = 1,
67 ENCODER_IN_AUDIO = 2
68} EncoderStateHint;
69
Josh Coalson0a15c142001-06-13 17:59:57 +000070/***********************************************************************
71 *
72 * Private class method prototypes
73 *
74 ***********************************************************************/
75
Josh Coalsonf1eff452002-07-31 07:05:33 +000076static void set_defaults_(FLAC__StreamEncoder *encoder);
77static void free_(FLAC__StreamEncoder *encoder);
78static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
Josh Coalsond86e03b2002-08-03 21:56:15 +000079static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
Josh Coalsonf1eff452002-07-31 07:05:33 +000080static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
81static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson6fe72f72002-08-20 04:01:59 +000082
83static FLAC__bool process_subframe_(
84 FLAC__StreamEncoder *encoder,
85 unsigned min_partition_order,
86 unsigned max_partition_order,
87 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +000088 const FLAC__FrameHeader *frame_header,
89 unsigned subframe_bps,
90 const FLAC__int32 integer_signal[],
91 const FLAC__real real_signal[],
92 FLAC__Subframe *subframe[2],
93 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
94 FLAC__int32 *residual[2],
95 unsigned *best_subframe,
96 unsigned *best_bits
97);
98
99static FLAC__bool add_subframe_(
100 FLAC__StreamEncoder *encoder,
101 const FLAC__FrameHeader *frame_header,
102 unsigned subframe_bps,
103 const FLAC__Subframe *subframe,
104 FLAC__BitBuffer *frame
105);
106
107static unsigned evaluate_constant_subframe_(
108 const FLAC__int32 signal,
109 unsigned subframe_bps,
110 FLAC__Subframe *subframe
111);
112
113static unsigned evaluate_fixed_subframe_(
114 FLAC__StreamEncoder *encoder,
115 const FLAC__int32 signal[],
116 FLAC__int32 residual[],
117 FLAC__uint32 abs_residual[],
118 FLAC__uint64 abs_residual_partition_sums[],
119 unsigned raw_bits_per_partition[],
120 unsigned blocksize,
121 unsigned subframe_bps,
122 unsigned order,
123 unsigned rice_parameter,
124 unsigned min_partition_order,
125 unsigned max_partition_order,
126 FLAC__bool precompute_partition_sums,
127 FLAC__bool do_escape_coding,
128 unsigned rice_parameter_search_dist,
129 FLAC__Subframe *subframe,
130 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
131);
132
133static unsigned evaluate_lpc_subframe_(
134 FLAC__StreamEncoder *encoder,
135 const FLAC__int32 signal[],
136 FLAC__int32 residual[],
137 FLAC__uint32 abs_residual[],
138 FLAC__uint64 abs_residual_partition_sums[],
139 unsigned raw_bits_per_partition[],
140 const FLAC__real lp_coeff[],
141 unsigned blocksize,
142 unsigned subframe_bps,
143 unsigned order,
144 unsigned qlp_coeff_precision,
145 unsigned rice_parameter,
146 unsigned min_partition_order,
147 unsigned max_partition_order,
148 FLAC__bool precompute_partition_sums,
149 FLAC__bool do_escape_coding,
150 unsigned rice_parameter_search_dist,
151 FLAC__Subframe *subframe,
152 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
153);
154
155static unsigned evaluate_verbatim_subframe_(
156 const FLAC__int32 signal[],
157 unsigned blocksize,
158 unsigned subframe_bps,
159 FLAC__Subframe *subframe
160);
161
162static unsigned find_best_partition_order_(
163 struct FLAC__StreamEncoderPrivate *private_,
164 const FLAC__int32 residual[],
165 FLAC__uint32 abs_residual[],
166 FLAC__uint64 abs_residual_partition_sums[],
167 unsigned raw_bits_per_partition[],
168 unsigned residual_samples,
169 unsigned predictor_order,
170 unsigned rice_parameter,
171 unsigned min_partition_order,
172 unsigned max_partition_order,
173 FLAC__bool precompute_partition_sums,
174 FLAC__bool do_escape_coding,
175 unsigned rice_parameter_search_dist,
176 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
177);
178
179static void precompute_partition_info_sums_(
180 const FLAC__uint32 abs_residual[],
181 FLAC__uint64 abs_residual_partition_sums[],
182 unsigned residual_samples,
183 unsigned predictor_order,
184 unsigned min_partition_order,
185 unsigned max_partition_order
186);
187
188static void precompute_partition_info_escapes_(
189 const FLAC__int32 residual[],
190 unsigned raw_bits_per_partition[],
191 unsigned residual_samples,
192 unsigned predictor_order,
193 unsigned min_partition_order,
194 unsigned max_partition_order
195);
196
Josh Coalson8395d022001-07-12 21:25:22 +0000197#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000198static FLAC__bool set_partitioned_rice_(
199 const FLAC__uint32 abs_residual[],
200 const FLAC__int32 residual[],
201 const unsigned residual_samples,
202 const unsigned predictor_order,
203 const unsigned suggested_rice_parameter,
204 const unsigned rice_parameter_search_dist,
205 const unsigned partition_order,
206 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
207 unsigned *bits
208);
209
210static FLAC__bool set_partitioned_rice_with_precompute_(
211 const FLAC__int32 residual[],
212 const FLAC__uint64 abs_residual_partition_sums[],
213 const unsigned raw_bits_per_partition[],
214 const unsigned residual_samples,
215 const unsigned predictor_order,
216 const unsigned suggested_rice_parameter,
217 const unsigned rice_parameter_search_dist,
218 const unsigned partition_order,
219 const FLAC__bool search_for_escapes,
220 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
221 unsigned *bits
222);
Josh Coalson8395d022001-07-12 21:25:22 +0000223#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000224static FLAC__bool set_partitioned_rice_(
225 const FLAC__uint32 abs_residual[],
226 const unsigned residual_samples,
227 const unsigned predictor_order,
228 const unsigned suggested_rice_parameter,
229 const unsigned rice_parameter_search_dist,
230 const unsigned partition_order,
231 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
232 unsigned *bits
233);
234
235static FLAC__bool set_partitioned_rice_with_precompute_(
236 const FLAC__uint32 abs_residual[],
237 const FLAC__uint64 abs_residual_partition_sums[],
238 const unsigned raw_bits_per_partition[],
239 const unsigned residual_samples,
240 const unsigned predictor_order,
241 const unsigned suggested_rice_parameter,
242 const unsigned rice_parameter_search_dist,
243 const unsigned partition_order,
244 const FLAC__bool search_for_escapes,
245 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
246 unsigned *bits
247);
Josh Coalson0a15c142001-06-13 17:59:57 +0000248#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000249
Josh Coalsonf1eff452002-07-31 07:05:33 +0000250static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000251
Josh Coalsond86e03b2002-08-03 21:56:15 +0000252/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000253static void append_to_verify_fifo_(
254 verify_input_fifo *fifo,
255 const FLAC__int32 * const input[],
256 unsigned input_offset,
257 unsigned channels,
258 unsigned wide_samples
259);
260
261static void append_to_verify_fifo_interleaved_(
262 verify_input_fifo *fifo,
263 const FLAC__int32 input[],
264 unsigned input_offset,
265 unsigned channels,
266 unsigned wide_samples
267);
268
269static FLAC__StreamDecoderReadStatus verify_read_callback_(
270 const FLAC__StreamDecoder *decoder,
271 FLAC__byte buffer[],
272 unsigned *bytes,
273 void *client_data
274);
275
276static FLAC__StreamDecoderWriteStatus verify_write_callback_(
277 const FLAC__StreamDecoder *decoder,
278 const FLAC__Frame *frame,
279 const FLAC__int32 * const buffer[],
280 void *client_data
281);
282
283static void verify_metadata_callback_(
284 const FLAC__StreamDecoder *decoder,
285 const FLAC__StreamMetadata *metadata,
286 void *client_data
287);
288
289static void verify_error_callback_(
290 const FLAC__StreamDecoder *decoder,
291 FLAC__StreamDecoderErrorStatus status,
292 void *client_data
293);
294
Josh Coalson0a15c142001-06-13 17:59:57 +0000295
296/***********************************************************************
297 *
298 * Private class data
299 *
300 ***********************************************************************/
301
302typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000303 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000304 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
305 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
306 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
307 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 +0000308 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
309 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 +0000310 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
311 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000312 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
313 FLAC__Subframe subframe_workspace_mid_side[2][2];
314 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
315 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000316 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
317 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
318 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
319 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000320 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000321 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000322 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000323 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000324 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000325 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 +0000326 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
Josh Coalsonaec256b2002-03-12 16:19:54 +0000327 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000328 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
329 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
330 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000331 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsoncc682512002-06-08 04:53:42 +0000332 FLAC__StreamMetadata metadata;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000333 unsigned current_sample_number;
334 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000335 struct MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000336 FLAC__CPUInfo cpuinfo;
Josh Coalson77e3f312001-06-23 03:03:24 +0000337 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
338 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
339 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[]);
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000340 void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
Josh Coalson77e3f312001-06-23 03:03:24 +0000341 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 Coalson3262b0d2002-08-14 20:58:42 +0000342 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
343 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
344 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
345 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +0000346 FLAC__bool disable_constant_subframes;
347 FLAC__bool disable_fixed_subframes;
348 FLAC__bool disable_verbatim_subframes;
Josh Coalson681c2932002-08-01 08:19:37 +0000349 FLAC__StreamEncoderWriteCallback write_callback;
350 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000351 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000352 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000353 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
354 FLAC__int32 *integer_signal_mid_side_unaligned[2];
355 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
356 FLAC__real *real_signal_mid_side_unaligned[2];
357 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
358 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
359 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000360 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000361 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000362 /*
363 * These fields have been moved here from private function local
364 * declarations merely to save stack space during encoding.
365 */
Josh Coalsonf1eff452002-07-31 07:05:33 +0000366 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalsona37ba462002-08-19 21:36:39 +0000367 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000368 /*
369 * The data for the verify section
370 */
371 struct {
372 FLAC__StreamDecoder *decoder;
373 EncoderStateHint state_hint;
374 FLAC__bool needs_magic_hack;
375 verify_input_fifo input_fifo;
376 verify_output output;
377 struct {
378 FLAC__uint64 absolute_sample;
379 unsigned frame_number;
380 unsigned channel;
381 unsigned sample;
382 FLAC__int32 expected;
383 FLAC__int32 got;
384 } error_stats;
385 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000386 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000387} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000388
Josh Coalson0a15c142001-06-13 17:59:57 +0000389/***********************************************************************
390 *
391 * Public static class data
392 *
393 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000394
Josh Coalson6afed9f2002-10-16 22:29:47 +0000395FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000396 "FLAC__STREAM_ENCODER_OK",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000397 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
398 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson00e53872001-06-16 07:32:25 +0000399 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
Josh Coalson0a15c142001-06-13 17:59:57 +0000400 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
401 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
402 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
403 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
Josh Coalson20ac2c12002-08-30 05:47:14 +0000404 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
Josh Coalson0a15c142001-06-13 17:59:57 +0000405 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
406 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
407 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
408 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
409 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
410 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
411 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson66075c12002-06-01 05:39:38 +0000412 "FLAC__STREAM_ENCODER_INVALID_METADATA",
Josh Coalson0a15c142001-06-13 17:59:57 +0000413 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
414 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
415 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
416 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
417 "FLAC__STREAM_ENCODER_UNINITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000418};
419
Josh Coalson6afed9f2002-10-16 22:29:47 +0000420FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000421 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
422 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000423};
424
Josh Coalson0a15c142001-06-13 17:59:57 +0000425/***********************************************************************
426 *
427 * Class constructor/destructor
428 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000429 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000430FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000431{
Josh Coalson0a15c142001-06-13 17:59:57 +0000432 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000433 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000434
Josh Coalson0a15c142001-06-13 17:59:57 +0000435 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000436
Josh Coalsonea7155f2002-10-18 05:49:19 +0000437 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000438 if(encoder == 0) {
439 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000440 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000441
Josh Coalsonea7155f2002-10-18 05:49:19 +0000442 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000443 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000444 free(encoder);
445 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000446 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000447
Josh Coalsonea7155f2002-10-18 05:49:19 +0000448 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000449 if(encoder->private_ == 0) {
450 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000451 free(encoder);
452 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000453 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000454
Josh Coalsonaec256b2002-03-12 16:19:54 +0000455 encoder->private_->frame = FLAC__bitbuffer_new();
456 if(encoder->private_->frame == 0) {
457 free(encoder->private_);
458 free(encoder->protected_);
459 free(encoder);
460 return 0;
461 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000462
Josh Coalsonf1eff452002-07-31 07:05:33 +0000463 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000464
Josh Coalson3262b0d2002-08-14 20:58:42 +0000465 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000466
467 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
468 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
469 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
470 }
471 for(i = 0; i < 2; i++) {
472 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
473 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
474 }
475 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000476 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
477 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000478 }
479 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000480 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
481 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000482 }
483
484 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000485 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
486 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000487 }
488 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000489 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
490 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000491 }
492 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000493 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000494
Josh Coalsonfa697a92001-08-16 20:07:29 +0000495 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000496
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000497 return encoder;
498}
499
Josh Coalson6afed9f2002-10-16 22:29:47 +0000500FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000501{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000502 unsigned i;
503
Josh Coalsonf1eff452002-07-31 07:05:33 +0000504 FLAC__ASSERT(0 != encoder);
505 FLAC__ASSERT(0 != encoder->protected_);
506 FLAC__ASSERT(0 != encoder->private_);
507 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000508
Josh Coalson3262b0d2002-08-14 20:58:42 +0000509 encoder->private_->is_being_deleted = true;
510
511 FLAC__stream_encoder_finish(encoder);
512
Josh Coalson4fa90592002-12-04 07:01:37 +0000513 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000514 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000515
516 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000517 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
518 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000519 }
520 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000521 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
522 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000523 }
524 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000525 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000526
Josh Coalsonaec256b2002-03-12 16:19:54 +0000527 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000528 free(encoder->private_);
529 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000530 free(encoder);
531}
532
Josh Coalson0a15c142001-06-13 17:59:57 +0000533/***********************************************************************
534 *
535 * Public class methods
536 *
537 ***********************************************************************/
538
Josh Coalson6afed9f2002-10-16 22:29:47 +0000539FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000540{
541 unsigned i;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000542 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000543
Josh Coalsonf1eff452002-07-31 07:05:33 +0000544 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000545
Josh Coalsonfa697a92001-08-16 20:07:29 +0000546 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
547 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000548
Josh Coalsonfa697a92001-08-16 20:07:29 +0000549 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000550
Josh Coalsonfa697a92001-08-16 20:07:29 +0000551 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
552 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000553
Josh Coalsonfa697a92001-08-16 20:07:29 +0000554 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
555 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000556
Josh Coalsonfa697a92001-08-16 20:07:29 +0000557 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
558 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000559
Josh Coalsonfa697a92001-08-16 20:07:29 +0000560 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
561 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000562
Josh Coalsonfa697a92001-08-16 20:07:29 +0000563 if(encoder->protected_->bits_per_sample >= 32)
564 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 +0000565
Josh Coalson76c68bc2002-05-17 06:22:02 +0000566 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000567 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000568
Josh Coalson0833f342002-07-15 05:31:55 +0000569 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000570 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000571
Josh Coalsonfa697a92001-08-16 20:07:29 +0000572 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
573 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000574
Josh Coalson20ac2c12002-08-30 05:47:14 +0000575 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
576 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
577
Josh Coalsonfa697a92001-08-16 20:07:29 +0000578 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
579 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000580
Josh Coalsonfa697a92001-08-16 20:07:29 +0000581 if(encoder->protected_->qlp_coeff_precision == 0) {
582 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000583 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
584 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000585 encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000586 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000587 else if(encoder->protected_->bits_per_sample == 16) {
588 if(encoder->protected_->blocksize <= 192)
589 encoder->protected_->qlp_coeff_precision = 7;
590 else if(encoder->protected_->blocksize <= 384)
591 encoder->protected_->qlp_coeff_precision = 8;
592 else if(encoder->protected_->blocksize <= 576)
593 encoder->protected_->qlp_coeff_precision = 9;
594 else if(encoder->protected_->blocksize <= 1152)
595 encoder->protected_->qlp_coeff_precision = 10;
596 else if(encoder->protected_->blocksize <= 2304)
597 encoder->protected_->qlp_coeff_precision = 11;
598 else if(encoder->protected_->blocksize <= 4608)
599 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000600 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000601 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000602 }
603 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000604 if(encoder->protected_->blocksize <= 384)
605 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
606 else if(encoder->protected_->blocksize <= 1152)
607 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
608 else
609 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000610 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000611 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000612 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000613 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000614 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000615
Josh Coalsonfa697a92001-08-16 20:07:29 +0000616 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000617 if(
618 encoder->protected_->blocksize != 192 &&
619 encoder->protected_->blocksize != 576 &&
620 encoder->protected_->blocksize != 1152 &&
621 encoder->protected_->blocksize != 2304 &&
622 encoder->protected_->blocksize != 4608 &&
623 encoder->protected_->blocksize != 256 &&
624 encoder->protected_->blocksize != 512 &&
625 encoder->protected_->blocksize != 1024 &&
626 encoder->protected_->blocksize != 2048 &&
627 encoder->protected_->blocksize != 4096 &&
628 encoder->protected_->blocksize != 8192 &&
629 encoder->protected_->blocksize != 16384
630 )
Josh Coalsonfa697a92001-08-16 20:07:29 +0000631 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000632 if(
633 encoder->protected_->sample_rate != 8000 &&
634 encoder->protected_->sample_rate != 16000 &&
635 encoder->protected_->sample_rate != 22050 &&
636 encoder->protected_->sample_rate != 24000 &&
637 encoder->protected_->sample_rate != 32000 &&
638 encoder->protected_->sample_rate != 44100 &&
639 encoder->protected_->sample_rate != 48000 &&
640 encoder->protected_->sample_rate != 96000
641 )
642 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
643 if(
644 encoder->protected_->bits_per_sample != 8 &&
645 encoder->protected_->bits_per_sample != 12 &&
646 encoder->protected_->bits_per_sample != 16 &&
647 encoder->protected_->bits_per_sample != 20 &&
648 encoder->protected_->bits_per_sample != 24
649 )
650 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000651 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000652 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000653 }
654
Josh Coalsonfa697a92001-08-16 20:07:29 +0000655 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
656 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
657 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
658 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000659
Josh Coalson66075c12002-06-01 05:39:38 +0000660 /* validate metadata */
661 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
662 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000663 metadata_has_seektable = false;
664 metadata_has_vorbis_comment = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000665 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalsone4869382002-11-15 05:41:48 +0000666 if(encoder->protected_->metadata[i]->type >= FLAC__METADATA_TYPE_UNDEFINED)
667 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
668 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson66075c12002-06-01 05:39:38 +0000669 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
670 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000671 if(metadata_has_seektable) /* only one is allowed */
672 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
673 metadata_has_seektable = true;
Josh Coalson0833f342002-07-15 05:31:55 +0000674 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
Josh Coalson66075c12002-06-01 05:39:38 +0000675 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
676 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000677 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
678 if(metadata_has_vorbis_comment) /* only one is allowed */
679 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
680 metadata_has_vorbis_comment = true;
681 }
Josh Coalsone4869382002-11-15 05:41:48 +0000682 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000683 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
Josh Coalsone4869382002-11-15 05:41:48 +0000684 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
685 }
Josh Coalson66075c12002-06-01 05:39:38 +0000686 }
687
Josh Coalsonfa697a92001-08-16 20:07:29 +0000688 encoder->private_->input_capacity = 0;
689 for(i = 0; i < encoder->protected_->channels; i++) {
690 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
691 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000692 }
693 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000694 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
695 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000696 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000697 for(i = 0; i < encoder->protected_->channels; i++) {
698 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
699 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
700 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000701 }
702 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000703 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
704 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
705 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000706 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000707 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
708 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
709 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
710 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
711 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
712 if(encoder->private_->loose_mid_side_stereo_frames == 0)
713 encoder->private_->loose_mid_side_stereo_frames = 1;
714 encoder->private_->loose_mid_side_stereo_frame_count = 0;
715 encoder->private_->current_sample_number = 0;
716 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000717
Josh Coalsonfa697a92001-08-16 20:07:29 +0000718 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
719 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? */
720 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000721
Josh Coalsoncf30f502001-05-23 20:57:44 +0000722 /*
723 * get the CPU info and set the function pointers
724 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000725 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000726 /* first default to the non-asm routines */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000727 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
728 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
729 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000730 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000731 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000732 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000733#ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000734 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalsoncf30f502001-05-23 20:57:44 +0000735#ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000736 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000737#ifdef FLAC__HAS_NASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000738 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
739 if(encoder->protected_->max_lpc_order < 4)
740 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
741 else if(encoder->protected_->max_lpc_order < 8)
742 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
743 else if(encoder->protected_->max_lpc_order < 12)
744 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000745 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000746 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000747 }
Josh Coalson395938e2001-11-15 21:53:25 +0000748 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000749 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000750 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000751 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
752 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
753 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
754 if(encoder->private_->cpuinfo.data.ia32.mmx) {
755 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
756 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000757 }
758 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000759 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
760 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000761 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000762#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000763#endif
Josh Coalson021ad3b2001-07-18 00:25:52 +0000764 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000765#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000766 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000767 if(encoder->private_->use_wide_by_block) {
768 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000769 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000770
Josh Coalson8395d022001-07-12 21:25:22 +0000771 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000772 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 +0000773
Josh Coalsonf1eff452002-07-31 07:05:33 +0000774 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000775 /* the above function sets the state for us in case of an error */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000776 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000777 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000778
779 if(!FLAC__bitbuffer_init(encoder->private_->frame))
780 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000781
782 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000783 * Set up the verify stuff if necessary
784 */
785 if(encoder->protected_->verify) {
786 /*
787 * First, set up the fifo which will hold the
788 * original signal to compare against
789 */
790 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
791 for(i = 0; i < encoder->protected_->channels; i++) {
792 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
793 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
794 }
795 encoder->private_->verify.input_fifo.tail = 0;
796
797 /*
798 * Now set up a stream decoder for verification
799 */
800 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
801 if(0 == encoder->private_->verify.decoder)
802 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
803
804 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
805 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
806 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
807 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
808 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
809 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
810 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
811 }
Josh Coalson589f8c72002-08-07 23:54:55 +0000812 encoder->private_->verify.error_stats.absolute_sample = 0;
813 encoder->private_->verify.error_stats.frame_number = 0;
814 encoder->private_->verify.error_stats.channel = 0;
815 encoder->private_->verify.error_stats.sample = 0;
816 encoder->private_->verify.error_stats.expected = 0;
817 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000818
819 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000820 * write the stream header
821 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000822 if(encoder->protected_->verify)
823 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000824 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000825 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000826 if(!write_bitbuffer_(encoder, 0)) {
827 /* the above function sets the state for us in case of an error */
828 return encoder->protected_->state;
829 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000830
Josh Coalson5c491a12002-08-01 06:39:40 +0000831 /*
832 * write the STREAMINFO metadata block
833 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000834 if(encoder->protected_->verify)
835 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000836 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000837 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000838 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
839 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
840 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
841 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
842 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
843 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
844 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
845 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
846 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
847 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
848 MD5Init(&encoder->private_->md5context);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000849 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
850 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000851 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000852 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000853 if(!write_bitbuffer_(encoder, 0)) {
854 /* the above function sets the state for us in case of an error */
855 return encoder->protected_->state;
856 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000857
Josh Coalson5c491a12002-08-01 06:39:40 +0000858 /*
859 * Now that the STREAMINFO block is written, we can init this to an
860 * absurdly-high value...
861 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000862 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 +0000863 /* ... and clear this to 0 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000864 encoder->private_->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000865
Josh Coalson5c491a12002-08-01 06:39:40 +0000866 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000867 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
868 * if not, we will write an empty one (FLAC__add_metadata_block()
869 * automatically supplies the vendor string).
870 */
871 if(!metadata_has_vorbis_comment) {
872 FLAC__StreamMetadata vorbis_comment;
873 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
874 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
875 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
876 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
877 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
878 vorbis_comment.data.vorbis_comment.num_comments = 0;
879 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson7424d2f2002-11-06 07:10:38 +0000880 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
881 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000882 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
883 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
884 if(!write_bitbuffer_(encoder, 0)) {
885 /* the above function sets the state for us in case of an error */
886 return encoder->protected_->state;
887 }
888 }
889
890 /*
Josh Coalson5c491a12002-08-01 06:39:40 +0000891 * write the user's metadata blocks
892 */
893 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
894 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000895 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
896 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +0000897 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
898 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000899 if(!write_bitbuffer_(encoder, 0)) {
900 /* the above function sets the state for us in case of an error */
901 return encoder->protected_->state;
902 }
Josh Coalson5c491a12002-08-01 06:39:40 +0000903 }
904
Josh Coalsond86e03b2002-08-03 21:56:15 +0000905 if(encoder->protected_->verify)
906 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
907
Josh Coalsonfa697a92001-08-16 20:07:29 +0000908 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000909}
910
Josh Coalson6afed9f2002-10-16 22:29:47 +0000911FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000912{
Josh Coalsonf1eff452002-07-31 07:05:33 +0000913 FLAC__ASSERT(0 != encoder);
Josh Coalson2b245f22002-08-07 17:10:50 +0000914
Josh Coalsonfa697a92001-08-16 20:07:29 +0000915 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000916 return;
Josh Coalson2b245f22002-08-07 17:10:50 +0000917
Josh Coalson3262b0d2002-08-14 20:58:42 +0000918 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +0000919 if(encoder->private_->current_sample_number != 0) {
920 encoder->protected_->blocksize = encoder->private_->current_sample_number;
921 process_frame_(encoder, true); /* true => is last frame */
922 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000923 }
Josh Coalson2b245f22002-08-07 17:10:50 +0000924
Josh Coalsonfa697a92001-08-16 20:07:29 +0000925 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +0000926
Josh Coalson3262b0d2002-08-14 20:58:42 +0000927 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +0000928 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
929 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000930
Josh Coalsond86e03b2002-08-03 21:56:15 +0000931 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
932 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
933
Josh Coalsonf1eff452002-07-31 07:05:33 +0000934 free_(encoder);
935 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000936
Josh Coalsonfa697a92001-08-16 20:07:29 +0000937 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000938}
939
Josh Coalson6afed9f2002-10-16 22:29:47 +0000940FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000941{
942 FLAC__ASSERT(0 != encoder);
943 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
944 return false;
945 encoder->protected_->verify = value;
946 return true;
947}
948
Josh Coalson6afed9f2002-10-16 22:29:47 +0000949FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000950{
Josh Coalson92031602002-07-24 06:02:11 +0000951 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000952 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000953 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000954 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000955 return true;
956}
957
Josh Coalson6afed9f2002-10-16 22:29:47 +0000958FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000959{
Josh Coalson92031602002-07-24 06:02:11 +0000960 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000961 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000962 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000963 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000964 return true;
965}
966
Josh Coalson6afed9f2002-10-16 22:29:47 +0000967FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000968{
Josh Coalson92031602002-07-24 06:02:11 +0000969 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000970 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000971 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000972 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000973 return true;
974}
975
Josh Coalson6afed9f2002-10-16 22:29:47 +0000976FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000977{
Josh Coalson92031602002-07-24 06:02:11 +0000978 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000979 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000980 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000981 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000982 return true;
983}
984
Josh Coalson6afed9f2002-10-16 22:29:47 +0000985FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000986{
Josh Coalson92031602002-07-24 06:02:11 +0000987 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000988 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000989 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000990 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000991 return true;
992}
993
Josh Coalson6afed9f2002-10-16 22:29:47 +0000994FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000995{
Josh Coalson92031602002-07-24 06:02:11 +0000996 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000997 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000998 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000999 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001000 return true;
1001}
1002
Josh Coalson6afed9f2002-10-16 22:29:47 +00001003FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001004{
Josh Coalson92031602002-07-24 06:02:11 +00001005 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001006 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001007 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001008 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001009 return true;
1010}
1011
Josh Coalson6afed9f2002-10-16 22:29:47 +00001012FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001013{
Josh Coalson92031602002-07-24 06:02:11 +00001014 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001015 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001016 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001017 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001018 return true;
1019}
1020
Josh Coalson6afed9f2002-10-16 22:29:47 +00001021FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001022{
Josh Coalson92031602002-07-24 06:02:11 +00001023 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001024 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001025 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001026 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001027 return true;
1028}
1029
Josh Coalson6afed9f2002-10-16 22:29:47 +00001030FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001031{
Josh Coalson92031602002-07-24 06:02:11 +00001032 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001033 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001034 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001035 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001036 return true;
1037}
1038
Josh Coalson6afed9f2002-10-16 22:29:47 +00001039FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001040{
Josh Coalson92031602002-07-24 06:02:11 +00001041 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001042 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001043 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001044#if 0
1045 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001046 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001047#else
1048 (void)value;
1049#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001050 return true;
1051}
1052
Josh Coalson6afed9f2002-10-16 22:29:47 +00001053FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001054{
Josh Coalson92031602002-07-24 06:02:11 +00001055 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001056 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001057 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001058 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001059 return true;
1060}
1061
Josh Coalson6afed9f2002-10-16 22:29:47 +00001062FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001063{
Josh Coalson92031602002-07-24 06:02:11 +00001064 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001065 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001066 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001067 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001068 return true;
1069}
1070
Josh Coalson6afed9f2002-10-16 22:29:47 +00001071FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001072{
Josh Coalson92031602002-07-24 06:02:11 +00001073 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001074 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001075 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001076 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001077 return true;
1078}
1079
Josh Coalson6afed9f2002-10-16 22:29:47 +00001080FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001081{
Josh Coalson92031602002-07-24 06:02:11 +00001082 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001083 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001084 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001085#if 0
1086 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001087 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001088#else
1089 (void)value;
1090#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001091 return true;
1092}
1093
Josh Coalson6afed9f2002-10-16 22:29:47 +00001094FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001095{
Josh Coalson92031602002-07-24 06:02:11 +00001096 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001097 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001098 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001099 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001100 return true;
1101}
1102
Josh Coalson6afed9f2002-10-16 22:29:47 +00001103FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001104{
Josh Coalson92031602002-07-24 06:02:11 +00001105 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001106 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001107 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001108 encoder->protected_->metadata = metadata;
1109 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson00e53872001-06-16 07:32:25 +00001110 return true;
1111}
1112
Josh Coalson6afed9f2002-10-16 22:29:47 +00001113FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001114{
Josh Coalson92031602002-07-24 06:02:11 +00001115 FLAC__ASSERT(0 != encoder);
1116 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001117 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001118 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001119 encoder->private_->write_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001120 return true;
1121}
1122
Josh Coalson6afed9f2002-10-16 22:29:47 +00001123FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001124{
Josh Coalson92031602002-07-24 06:02:11 +00001125 FLAC__ASSERT(0 != encoder);
1126 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001127 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001128 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001129 encoder->private_->metadata_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001130 return true;
1131}
1132
Josh Coalson6afed9f2002-10-16 22:29:47 +00001133FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +00001134{
Josh Coalson92031602002-07-24 06:02:11 +00001135 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001136 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001137 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001138 encoder->private_->client_data = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001139 return true;
1140}
1141
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001142/*
1143 * These three functions are not static, but not publically exposed in
1144 * include/FLAC/ either. They are used by the test suite.
1145 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001146FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001147{
1148 FLAC__ASSERT(0 != encoder);
1149 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1150 return false;
1151 encoder->private_->disable_constant_subframes = value;
1152 return true;
1153}
1154
Josh Coalson6afed9f2002-10-16 22:29:47 +00001155FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001156{
1157 FLAC__ASSERT(0 != encoder);
1158 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1159 return false;
1160 encoder->private_->disable_fixed_subframes = value;
1161 return true;
1162}
1163
Josh Coalson6afed9f2002-10-16 22:29:47 +00001164FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001165{
1166 FLAC__ASSERT(0 != encoder);
1167 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1168 return false;
1169 encoder->private_->disable_verbatim_subframes = value;
1170 return true;
1171}
1172
Josh Coalson6afed9f2002-10-16 22:29:47 +00001173FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001174{
Josh Coalson92031602002-07-24 06:02:11 +00001175 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001176 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001177}
1178
Josh Coalson6afed9f2002-10-16 22:29:47 +00001179FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001180{
1181 FLAC__ASSERT(0 != encoder);
1182 if(encoder->protected_->verify)
1183 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1184 else
1185 return FLAC__STREAM_DECODER_UNINITIALIZED;
1186}
1187
Josh Coalson02954222002-11-08 06:16:31 +00001188FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1189{
1190 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1191 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1192 else
1193 return FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(encoder->private_->verify.decoder)];
1194}
1195
Josh Coalson6afed9f2002-10-16 22:29:47 +00001196FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
Josh Coalson589f8c72002-08-07 23:54:55 +00001197{
1198 FLAC__ASSERT(0 != encoder);
1199 if(0 != absolute_sample)
1200 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1201 if(0 != frame_number)
1202 *frame_number = encoder->private_->verify.error_stats.frame_number;
1203 if(0 != channel)
1204 *channel = encoder->private_->verify.error_stats.channel;
1205 if(0 != sample)
1206 *sample = encoder->private_->verify.error_stats.sample;
1207 if(0 != expected)
1208 *expected = encoder->private_->verify.error_stats.expected;
1209 if(0 != got)
1210 *got = encoder->private_->verify.error_stats.got;
1211}
1212
Josh Coalson6afed9f2002-10-16 22:29:47 +00001213FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001214{
1215 FLAC__ASSERT(0 != encoder);
1216 return encoder->protected_->verify;
1217}
1218
Josh Coalson6afed9f2002-10-16 22:29:47 +00001219FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001220{
Josh Coalson92031602002-07-24 06:02:11 +00001221 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001222 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001223}
1224
Josh Coalson6afed9f2002-10-16 22:29:47 +00001225FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001226{
Josh Coalson92031602002-07-24 06:02:11 +00001227 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001228 return encoder->protected_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001229}
1230
Josh Coalson6afed9f2002-10-16 22:29:47 +00001231FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001232{
Josh Coalson92031602002-07-24 06:02:11 +00001233 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001234 return encoder->protected_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001235}
1236
Josh Coalson6afed9f2002-10-16 22:29:47 +00001237FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001238{
Josh Coalson92031602002-07-24 06:02:11 +00001239 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001240 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001241}
1242
Josh Coalson6afed9f2002-10-16 22:29:47 +00001243FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001244{
Josh Coalson92031602002-07-24 06:02:11 +00001245 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001246 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001247}
1248
Josh Coalson6afed9f2002-10-16 22:29:47 +00001249FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001250{
Josh Coalson92031602002-07-24 06:02:11 +00001251 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001252 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001253}
1254
Josh Coalson6afed9f2002-10-16 22:29:47 +00001255FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001256{
Josh Coalson92031602002-07-24 06:02:11 +00001257 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001258 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001259}
1260
Josh Coalson6afed9f2002-10-16 22:29:47 +00001261FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001262{
Josh Coalson92031602002-07-24 06:02:11 +00001263 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001264 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001265}
1266
Josh Coalson6afed9f2002-10-16 22:29:47 +00001267FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001268{
Josh Coalson92031602002-07-24 06:02:11 +00001269 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001270 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001271}
1272
Josh Coalson6afed9f2002-10-16 22:29:47 +00001273FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001274{
Josh Coalson92031602002-07-24 06:02:11 +00001275 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001276 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001277}
1278
Josh Coalson6afed9f2002-10-16 22:29:47 +00001279FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001280{
Josh Coalson92031602002-07-24 06:02:11 +00001281 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001282 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001283}
1284
Josh Coalson6afed9f2002-10-16 22:29:47 +00001285FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001286{
Josh Coalson92031602002-07-24 06:02:11 +00001287 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001288 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001289}
1290
Josh Coalson6afed9f2002-10-16 22:29:47 +00001291FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001292{
Josh Coalson92031602002-07-24 06:02:11 +00001293 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001294 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001295}
1296
Josh Coalson6afed9f2002-10-16 22:29:47 +00001297FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001298{
Josh Coalson92031602002-07-24 06:02:11 +00001299 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001300 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001301}
1302
Josh Coalson6afed9f2002-10-16 22:29:47 +00001303FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001304{
Josh Coalson92031602002-07-24 06:02:11 +00001305 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001306 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001307}
1308
Josh Coalson6afed9f2002-10-16 22:29:47 +00001309FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001310{
1311 FLAC__ASSERT(0 != encoder);
1312 return encoder->protected_->total_samples_estimate;
1313}
1314
Josh Coalson6afed9f2002-10-16 22:29:47 +00001315FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001316{
1317 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001318 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001319 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001320
Josh Coalsonf1eff452002-07-31 07:05:33 +00001321 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001322 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001323
1324 j = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001325 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001326 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001327 if(encoder->protected_->verify)
1328 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1329
Josh Coalsonfa697a92001-08-16 20:07:29 +00001330 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001331 x = mid = side = buffer[0][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001332 encoder->private_->integer_signal[0][i] = x;
1333 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001334 x = buffer[1][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001335 encoder->private_->integer_signal[1][i] = x;
1336 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001337 mid += x;
1338 side -= x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001339 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001340 encoder->private_->integer_signal_mid_side[1][i] = side;
1341 encoder->private_->integer_signal_mid_side[0][i] = mid;
1342 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1343 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1344 encoder->private_->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001345 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001346 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001347 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001348 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001349 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001350 } while(j < samples);
1351 }
1352 else {
1353 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001354 if(encoder->protected_->verify)
1355 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1356
Josh Coalsonfa697a92001-08-16 20:07:29 +00001357 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001358 for(channel = 0; channel < channels; channel++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001359 x = buffer[channel][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001360 encoder->private_->integer_signal[channel][i] = x;
1361 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001362 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001363 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001364 }
1365 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001366 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001367 return false;
1368 }
1369 } while(j < samples);
1370 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001371
1372 return true;
1373}
1374
Josh Coalson6afed9f2002-10-16 22:29:47 +00001375FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001376{
1377 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001378 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001379 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001380
Josh Coalsonf1eff452002-07-31 07:05:33 +00001381 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001382 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001383
1384 j = k = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001385 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001386 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001387 if(encoder->protected_->verify)
1388 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1389
Josh Coalsonfa697a92001-08-16 20:07:29 +00001390 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001391 x = mid = side = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001392 encoder->private_->integer_signal[0][i] = x;
1393 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001394 x = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001395 encoder->private_->integer_signal[1][i] = x;
1396 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001397 mid += x;
1398 side -= x;
1399 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001400 encoder->private_->integer_signal_mid_side[1][i] = side;
1401 encoder->private_->integer_signal_mid_side[0][i] = mid;
1402 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1403 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1404 encoder->private_->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001405 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001406 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001407 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001408 return false;
1409 }
1410 } while(j < samples);
1411 }
1412 else {
1413 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001414 if(encoder->protected_->verify)
1415 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1416
Josh Coalsonfa697a92001-08-16 20:07:29 +00001417 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001418 for(channel = 0; channel < channels; channel++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001419 x = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001420 encoder->private_->integer_signal[channel][i] = x;
1421 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001422 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001423 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001424 }
1425 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001426 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001427 return false;
1428 }
1429 } while(j < samples);
1430 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001431
1432 return true;
1433}
1434
Josh Coalsonf1eff452002-07-31 07:05:33 +00001435/***********************************************************************
1436 *
1437 * Private class methods
1438 *
1439 ***********************************************************************/
1440
1441void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00001442{
1443 FLAC__ASSERT(0 != encoder);
1444
Josh Coalsond86e03b2002-08-03 21:56:15 +00001445 encoder->protected_->verify = false;
Josh Coalson92031602002-07-24 06:02:11 +00001446 encoder->protected_->streamable_subset = true;
1447 encoder->protected_->do_mid_side_stereo = false;
1448 encoder->protected_->loose_mid_side_stereo = false;
1449 encoder->protected_->channels = 2;
1450 encoder->protected_->bits_per_sample = 16;
1451 encoder->protected_->sample_rate = 44100;
1452 encoder->protected_->blocksize = 1152;
1453 encoder->protected_->max_lpc_order = 0;
1454 encoder->protected_->qlp_coeff_precision = 0;
1455 encoder->protected_->do_qlp_coeff_prec_search = false;
1456 encoder->protected_->do_exhaustive_model_search = false;
1457 encoder->protected_->do_escape_coding = false;
1458 encoder->protected_->min_residual_partition_order = 0;
1459 encoder->protected_->max_residual_partition_order = 0;
1460 encoder->protected_->rice_parameter_search_dist = 0;
1461 encoder->protected_->total_samples_estimate = 0;
1462 encoder->protected_->metadata = 0;
1463 encoder->protected_->num_metadata_blocks = 0;
1464
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001465 encoder->private_->disable_constant_subframes = false;
1466 encoder->private_->disable_fixed_subframes = false;
1467 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson92031602002-07-24 06:02:11 +00001468 encoder->private_->write_callback = 0;
1469 encoder->private_->metadata_callback = 0;
1470 encoder->private_->client_data = 0;
1471}
1472
Josh Coalsonf1eff452002-07-31 07:05:33 +00001473void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00001474{
1475 unsigned i, channel;
1476
Josh Coalsonf1eff452002-07-31 07:05:33 +00001477 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00001478 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001479 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001480 free(encoder->private_->integer_signal_unaligned[i]);
1481 encoder->private_->integer_signal_unaligned[i] = 0;
1482 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001483 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001484 free(encoder->private_->real_signal_unaligned[i]);
1485 encoder->private_->real_signal_unaligned[i] = 0;
1486 }
1487 }
1488 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001489 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001490 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1491 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1492 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001493 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001494 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1495 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1496 }
1497 }
1498 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1499 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001500 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001501 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1502 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1503 }
1504 }
1505 }
1506 for(channel = 0; channel < 2; channel++) {
1507 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001508 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001509 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1510 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1511 }
1512 }
1513 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001514 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001515 free(encoder->private_->abs_residual_unaligned);
1516 encoder->private_->abs_residual_unaligned = 0;
1517 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001518 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001519 free(encoder->private_->abs_residual_partition_sums_unaligned);
1520 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1521 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001522 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001523 free(encoder->private_->raw_bits_per_partition_unaligned);
1524 encoder->private_->raw_bits_per_partition_unaligned = 0;
1525 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001526 if(encoder->protected_->verify) {
1527 for(i = 0; i < encoder->protected_->channels; i++) {
1528 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1529 free(encoder->private_->verify.input_fifo.data[i]);
1530 encoder->private_->verify.input_fifo.data[i] = 0;
1531 }
1532 }
1533 }
Josh Coalson639aeb02002-07-25 05:38:23 +00001534 FLAC__bitbuffer_free(encoder->private_->frame);
1535}
1536
Josh Coalsonf1eff452002-07-31 07:05:33 +00001537FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001538{
Josh Coalson77e3f312001-06-23 03:03:24 +00001539 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00001540 unsigned i, channel;
1541
1542 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001543 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1544 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00001545
1546 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001547 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00001548 return true;
1549
1550 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00001551
Josh Coalsonc9c0d132002-10-04 05:29:05 +00001552 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1553 * requires that the input arrays (in our case the integer signals)
1554 * have a buffer of up to 3 zeroes in front (at negative indices) for
1555 * alignment purposes; we use 4 to keep the data well-aligned.
1556 */
Josh Coalson8395d022001-07-12 21:25:22 +00001557
Josh Coalsonfa697a92001-08-16 20:07:29 +00001558 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1559 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1560 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1561 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1562 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001563 }
1564 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001565 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]);
1566 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]);
1567 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1568 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001569 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001570 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001571 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001572 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
Josh Coalson0a15c142001-06-13 17:59:57 +00001573 }
1574 }
1575 for(channel = 0; ok && channel < 2; channel++) {
1576 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001577 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]);
Josh Coalson0a15c142001-06-13 17:59:57 +00001578 }
1579 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001580 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1581 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 */
1582 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1583 if(encoder->protected_->do_escape_coding)
1584 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 +00001585
1586 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001587 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00001588 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001589 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00001590
1591 return ok;
1592}
1593
Josh Coalsond86e03b2002-08-03 21:56:15 +00001594FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00001595{
1596 const FLAC__byte *buffer;
1597 unsigned bytes;
1598
1599 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1600
1601 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1602
Josh Coalsond86e03b2002-08-03 21:56:15 +00001603 if(encoder->protected_->verify) {
1604 encoder->private_->verify.output.data = buffer;
1605 encoder->private_->verify.output.bytes = bytes;
1606 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1607 encoder->private_->verify.needs_magic_hack = true;
1608 }
1609 else {
1610 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1611 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1612 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1613 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1614 return false;
1615 }
1616 }
1617 }
1618
1619 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00001620 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001621 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalson5c491a12002-08-01 06:39:40 +00001622 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001623 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001624
1625 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1626
Josh Coalsond86e03b2002-08-03 21:56:15 +00001627 if(samples > 0) {
1628 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1629 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1630 }
1631
Josh Coalson5c491a12002-08-01 06:39:40 +00001632 return true;
1633}
1634
Josh Coalsonf1eff452002-07-31 07:05:33 +00001635FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00001636{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001637 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001638
1639 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00001640 * Accumulate raw signal to the MD5 signature
1641 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00001642 if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001643 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00001644 return false;
1645 }
1646
1647 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001648 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001649 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00001650 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001651 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001652 return false;
1653 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001654
1655 /*
1656 * Zero-pad the frame to a byte_boundary
1657 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001658 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001659 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001660 return false;
1661 }
1662
1663 /*
Josh Coalson215af572001-03-27 01:15:58 +00001664 * CRC-16 the whole thing
1665 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001666 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1667 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
Josh Coalson215af572001-03-27 01:15:58 +00001668
1669 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001670 * Write it
1671 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001672 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1673 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001674 return false;
1675 }
1676
1677 /*
1678 * Get ready for the next frame
1679 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001680 encoder->private_->current_sample_number = 0;
1681 encoder->private_->current_frame_number++;
1682 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001683
1684 return true;
1685}
1686
Josh Coalsonf1eff452002-07-31 07:05:33 +00001687FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001688{
1689 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001690 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00001691 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001692
1693 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00001694 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00001695 */
1696 if(is_last_frame) {
1697 max_partition_order = 0;
1698 }
1699 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001700 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1701 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001702 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001703 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001704
Josh Coalsonfa697a92001-08-16 20:07:29 +00001705 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
Josh Coalson8395d022001-07-12 21:25:22 +00001706
Josh Coalson94e02cd2001-01-25 10:41:06 +00001707 /*
1708 * Setup the frame
1709 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001710 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001711 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001712 return false;
1713 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001714 frame_header.blocksize = encoder->protected_->blocksize;
1715 frame_header.sample_rate = encoder->protected_->sample_rate;
1716 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001717 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001718 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001719 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001720 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001721
1722 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001723 * Figure out what channel assignments to try
1724 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001725 if(encoder->protected_->do_mid_side_stereo) {
1726 if(encoder->protected_->loose_mid_side_stereo) {
1727 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001728 do_independent = true;
1729 do_mid_side = true;
1730 }
1731 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001732 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001733 do_mid_side = !do_independent;
1734 }
1735 }
1736 else {
1737 do_independent = true;
1738 do_mid_side = true;
1739 }
1740 }
1741 else {
1742 do_independent = true;
1743 do_mid_side = false;
1744 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001745
Josh Coalson1b689822001-05-31 20:11:02 +00001746 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001747
1748 /*
Josh Coalson82b73242001-03-28 22:17:05 +00001749 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00001750 */
1751 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001752 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001753 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001754 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1755 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00001756 }
Josh Coalson859bc542001-03-27 22:22:27 +00001757 }
1758 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001759 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00001760 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001761 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001762 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1763 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00001764 }
Josh Coalson859bc542001-03-27 22:22:27 +00001765 }
1766
1767 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001768 * First do a normal encoding pass of each independent channel
1769 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001770 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001771 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00001772 if(!
1773 process_subframe_(
1774 encoder,
1775 min_partition_order,
1776 max_partition_order,
1777 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001778 &frame_header,
1779 encoder->private_->subframe_bps[channel],
1780 encoder->private_->integer_signal[channel],
1781 encoder->private_->real_signal[channel],
1782 encoder->private_->subframe_workspace_ptr[channel],
1783 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1784 encoder->private_->residual_workspace[channel],
1785 encoder->private_->best_subframe+channel,
1786 encoder->private_->best_subframe_bits+channel
1787 )
1788 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001789 return false;
1790 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001791 }
1792
1793 /*
1794 * Now do mid and side channels if requested
1795 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001796 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001797 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001798
1799 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00001800 if(!
1801 process_subframe_(
1802 encoder,
1803 min_partition_order,
1804 max_partition_order,
1805 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001806 &frame_header,
1807 encoder->private_->subframe_bps_mid_side[channel],
1808 encoder->private_->integer_signal_mid_side[channel],
1809 encoder->private_->real_signal_mid_side[channel],
1810 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1811 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1812 encoder->private_->residual_workspace_mid_side[channel],
1813 encoder->private_->best_subframe_mid_side+channel,
1814 encoder->private_->best_subframe_bits_mid_side+channel
1815 )
1816 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00001817 return false;
1818 }
1819 }
1820
1821 /*
1822 * Compose the frame bitbuffer
1823 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001824 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00001825 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1826 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001827 FLAC__ChannelAssignment channel_assignment;
1828
Josh Coalsonfa697a92001-08-16 20:07:29 +00001829 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001830
Josh Coalsonfa697a92001-08-16 20:07:29 +00001831 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1832 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 +00001833 }
1834 else {
1835 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1836 unsigned min_bits;
1837 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001838
Josh Coalson1b689822001-05-31 20:11:02 +00001839 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001840
1841 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001842 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1843 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1844 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1845 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 +00001846
Josh Coalson7424d2f2002-11-06 07:10:38 +00001847 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001848 if(bits[ca] < min_bits) {
1849 min_bits = bits[ca];
1850 channel_assignment = ca;
1851 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001852 }
1853 }
1854
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001855 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001856
Josh Coalsonaec256b2002-03-12 16:19:54 +00001857 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001858 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001859 return false;
1860 }
1861
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001862 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001863 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001864 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1865 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001866 break;
1867 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001868 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1869 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001870 break;
1871 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001872 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1873 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001874 break;
1875 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001876 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1877 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001878 break;
1879 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001880 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001881 }
Josh Coalson82b73242001-03-28 22:17:05 +00001882
1883 switch(channel_assignment) {
1884 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001885 left_bps = encoder->private_->subframe_bps [0];
1886 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001887 break;
1888 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001889 left_bps = encoder->private_->subframe_bps [0];
1890 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001891 break;
1892 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001893 left_bps = encoder->private_->subframe_bps_mid_side[1];
1894 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001895 break;
1896 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001897 left_bps = encoder->private_->subframe_bps_mid_side[0];
1898 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001899 break;
1900 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001901 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00001902 }
1903
1904 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00001905 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001906 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00001907 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001908 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001909 }
1910 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001911 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001912 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001913 return false;
1914 }
1915
Josh Coalsonfa697a92001-08-16 20:07:29 +00001916 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001917 if(!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 +00001918 /* the above function sets the state for us in case of an error */
1919 return false;
1920 }
1921 }
1922 }
1923
Josh Coalsonfa697a92001-08-16 20:07:29 +00001924 if(encoder->protected_->loose_mid_side_stereo) {
1925 encoder->private_->loose_mid_side_stereo_frame_count++;
1926 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1927 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001928 }
1929
Josh Coalsonfa697a92001-08-16 20:07:29 +00001930 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001931
Josh Coalson94e02cd2001-01-25 10:41:06 +00001932 return true;
1933}
1934
Josh Coalson6fe72f72002-08-20 04:01:59 +00001935FLAC__bool process_subframe_(
1936 FLAC__StreamEncoder *encoder,
1937 unsigned min_partition_order,
1938 unsigned max_partition_order,
1939 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001940 const FLAC__FrameHeader *frame_header,
1941 unsigned subframe_bps,
1942 const FLAC__int32 integer_signal[],
1943 const FLAC__real real_signal[],
1944 FLAC__Subframe *subframe[2],
1945 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1946 FLAC__int32 *residual[2],
1947 unsigned *best_subframe,
1948 unsigned *best_bits
1949)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001950{
Josh Coalson77e3f312001-06-23 03:03:24 +00001951 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1952 FLAC__real lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001953 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 */
Josh Coalson77e3f312001-06-23 03:03:24 +00001954 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001955 unsigned min_lpc_order, max_lpc_order, lpc_order;
1956 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1957 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1958 unsigned rice_parameter;
1959 unsigned _candidate_bits, _best_bits;
1960 unsigned _best_subframe;
1961
1962 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1963 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001964 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
1965 _best_bits = UINT_MAX;
1966 else
1967 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001968
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001969 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1970 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001971 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 Coalsone6b3bbe2002-10-08 06:03:25 +00001972 /* check for constant subframe */
1973 if(!encoder->private_->disable_constant_subframes && fixed_residual_bits_per_sample[1] == 0.0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001974 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001975 unsigned i;
1976 signal_is_constant = true;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001977 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1978 if(integer_signal[0] != integer_signal[i]) {
1979 signal_is_constant = false;
1980 break;
1981 }
1982 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001983 }
1984 if(signal_is_constant) {
1985 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1986 if(_candidate_bits < _best_bits) {
1987 _best_subframe = !_best_subframe;
1988 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001989 }
1990 }
1991 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001992 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
1993 /* encode fixed */
1994 if(encoder->protected_->do_exhaustive_model_search) {
1995 min_fixed_order = 0;
1996 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00001997 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001998 else {
1999 min_fixed_order = max_fixed_order = guess_fixed_order;
2000 }
2001 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
2002 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
2003 continue; /* don't even try */
2004 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 */
2005#ifndef FLAC__SYMMETRIC_RICE
2006 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2007#endif
2008 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2009#ifdef DEBUG_VERBOSE
2010 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2011#endif
2012 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2013 }
2014 _candidate_bits =
2015 evaluate_fixed_subframe_(
2016 encoder,
2017 integer_signal,
2018 residual[!_best_subframe],
2019 encoder->private_->abs_residual,
2020 encoder->private_->abs_residual_partition_sums,
2021 encoder->private_->raw_bits_per_partition,
2022 frame_header->blocksize,
2023 subframe_bps,
2024 fixed_order,
2025 rice_parameter,
2026 min_partition_order,
2027 max_partition_order,
2028 precompute_partition_sums,
2029 encoder->protected_->do_escape_coding,
2030 encoder->protected_->rice_parameter_search_dist,
2031 subframe[!_best_subframe],
2032 partitioned_rice_contents[!_best_subframe]
2033 );
2034 if(_candidate_bits < _best_bits) {
2035 _best_subframe = !_best_subframe;
2036 _best_bits = _candidate_bits;
2037 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002038 }
2039 }
2040
2041 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002042 if(encoder->protected_->max_lpc_order > 0) {
2043 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002044 max_lpc_order = frame_header->blocksize-1;
2045 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002046 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002047 if(max_lpc_order > 0) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002048 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002049 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2050 if(autoc[0] != 0.0) {
Josh Coalson8084b052001-11-01 00:27:29 +00002051 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002052 if(encoder->protected_->do_exhaustive_model_search) {
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002053 min_lpc_order = 1;
2054 }
2055 else {
Josh Coalson82b73242001-03-28 22:17:05 +00002056 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 +00002057 min_lpc_order = max_lpc_order = guess_lpc_order;
2058 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002059 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2060 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 +00002061 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002062 continue; /* don't even try */
2063 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 +00002064#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002065 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00002066#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002067 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002068#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002069 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2070#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002071 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002072 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002073 if(encoder->protected_->do_qlp_coeff_prec_search) {
2074 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2075 /* ensure a 32-bit datapath throughout for 16bps or less */
2076 if(subframe_bps <= 16)
2077 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2078 else
2079 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2080 }
2081 else {
2082 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2083 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002084 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002085 _candidate_bits =
2086 evaluate_lpc_subframe_(
2087 encoder,
2088 integer_signal,
2089 residual[!_best_subframe],
2090 encoder->private_->abs_residual,
2091 encoder->private_->abs_residual_partition_sums,
2092 encoder->private_->raw_bits_per_partition,
2093 encoder->private_->lp_coeff[lpc_order-1],
2094 frame_header->blocksize,
2095 subframe_bps,
2096 lpc_order,
2097 qlp_coeff_precision,
2098 rice_parameter,
2099 min_partition_order,
2100 max_partition_order,
2101 precompute_partition_sums,
2102 encoder->protected_->do_escape_coding,
2103 encoder->protected_->rice_parameter_search_dist,
2104 subframe[!_best_subframe],
2105 partitioned_rice_contents[!_best_subframe]
2106 );
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002107 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2108 if(_candidate_bits < _best_bits) {
2109 _best_subframe = !_best_subframe;
2110 _best_bits = _candidate_bits;
2111 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002112 }
2113 }
2114 }
2115 }
2116 }
2117 }
2118 }
2119 }
2120
Josh Coalson72695802002-10-11 06:25:16 +00002121 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2122 if(_best_bits == UINT_MAX) {
2123 FLAC__ASSERT(_best_subframe == 0);
2124 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2125 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002126
Josh Coalson94e02cd2001-01-25 10:41:06 +00002127 *best_subframe = _best_subframe;
2128 *best_bits = _best_bits;
2129
2130 return true;
2131}
2132
Josh Coalson6fe72f72002-08-20 04:01:59 +00002133FLAC__bool add_subframe_(
2134 FLAC__StreamEncoder *encoder,
2135 const FLAC__FrameHeader *frame_header,
2136 unsigned subframe_bps,
2137 const FLAC__Subframe *subframe,
2138 FLAC__BitBuffer *frame
2139)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002140{
2141 switch(subframe->type) {
2142 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00002143 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002144 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002145 return false;
2146 }
2147 break;
2148 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00002149 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002150 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002151 return false;
2152 }
2153 break;
2154 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00002155 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002156 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002157 return false;
2158 }
2159 break;
2160 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00002161 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002162 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002163 return false;
2164 }
2165 break;
2166 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002167 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002168 }
2169
2170 return true;
2171}
2172
Josh Coalson6fe72f72002-08-20 04:01:59 +00002173unsigned evaluate_constant_subframe_(
2174 const FLAC__int32 signal,
2175 unsigned subframe_bps,
2176 FLAC__Subframe *subframe
2177)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002178{
2179 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2180 subframe->data.constant.value = signal;
2181
Josh Coalson82b73242001-03-28 22:17:05 +00002182 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 +00002183}
2184
Josh Coalson6fe72f72002-08-20 04:01:59 +00002185unsigned evaluate_fixed_subframe_(
2186 FLAC__StreamEncoder *encoder,
2187 const FLAC__int32 signal[],
2188 FLAC__int32 residual[],
2189 FLAC__uint32 abs_residual[],
2190 FLAC__uint64 abs_residual_partition_sums[],
2191 unsigned raw_bits_per_partition[],
2192 unsigned blocksize,
2193 unsigned subframe_bps,
2194 unsigned order,
2195 unsigned rice_parameter,
2196 unsigned min_partition_order,
2197 unsigned max_partition_order,
2198 FLAC__bool precompute_partition_sums,
2199 FLAC__bool do_escape_coding,
2200 unsigned rice_parameter_search_dist,
2201 FLAC__Subframe *subframe,
2202 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2203)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002204{
2205 unsigned i, residual_bits;
2206 const unsigned residual_samples = blocksize - order;
2207
2208 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2209
2210 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2211
2212 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002213 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002214 subframe->data.fixed.residual = residual;
2215
Josh Coalson6fe72f72002-08-20 04:01:59 +00002216 residual_bits =
2217 find_best_partition_order_(
2218 encoder->private_,
2219 residual,
2220 abs_residual,
2221 abs_residual_partition_sums,
2222 raw_bits_per_partition,
2223 residual_samples,
2224 order,
2225 rice_parameter,
2226 min_partition_order,
2227 max_partition_order,
2228 precompute_partition_sums,
2229 do_escape_coding,
2230 rice_parameter_search_dist,
2231 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2232 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002233
2234 subframe->data.fixed.order = order;
2235 for(i = 0; i < order; i++)
2236 subframe->data.fixed.warmup[i] = signal[i];
2237
Josh Coalson82b73242001-03-28 22:17:05 +00002238 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 +00002239}
2240
Josh Coalson6fe72f72002-08-20 04:01:59 +00002241unsigned evaluate_lpc_subframe_(
2242 FLAC__StreamEncoder *encoder,
2243 const FLAC__int32 signal[],
2244 FLAC__int32 residual[],
2245 FLAC__uint32 abs_residual[],
2246 FLAC__uint64 abs_residual_partition_sums[],
2247 unsigned raw_bits_per_partition[],
2248 const FLAC__real lp_coeff[],
2249 unsigned blocksize,
2250 unsigned subframe_bps,
2251 unsigned order,
2252 unsigned qlp_coeff_precision,
2253 unsigned rice_parameter,
2254 unsigned min_partition_order,
2255 unsigned max_partition_order,
2256 FLAC__bool precompute_partition_sums,
2257 FLAC__bool do_escape_coding,
2258 unsigned rice_parameter_search_dist,
2259 FLAC__Subframe *subframe,
2260 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2261)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002262{
Josh Coalson77e3f312001-06-23 03:03:24 +00002263 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002264 unsigned i, residual_bits;
2265 int quantization, ret;
2266 const unsigned residual_samples = blocksize - order;
2267
Josh Coalson20ac2c12002-08-30 05:47:14 +00002268 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2269 if(subframe_bps <= 16) {
2270 FLAC__ASSERT(order > 0);
2271 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2272 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2273 }
2274
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002275 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002276 if(ret != 0)
2277 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2278
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00002279 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2280 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2281 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2282 else
2283 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002284 else
2285 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002286
2287 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2288
2289 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002290 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002291 subframe->data.lpc.residual = residual;
2292
Josh Coalson6fe72f72002-08-20 04:01:59 +00002293 residual_bits =
2294 find_best_partition_order_(
2295 encoder->private_,
2296 residual,
2297 abs_residual,
2298 abs_residual_partition_sums,
2299 raw_bits_per_partition,
2300 residual_samples,
2301 order,
2302 rice_parameter,
2303 min_partition_order,
2304 max_partition_order,
2305 precompute_partition_sums,
2306 do_escape_coding,
2307 rice_parameter_search_dist,
2308 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2309 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002310
2311 subframe->data.lpc.order = order;
2312 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2313 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00002314 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002315 for(i = 0; i < order; i++)
2316 subframe->data.lpc.warmup[i] = signal[i];
2317
Josh Coalson82b73242001-03-28 22:17:05 +00002318 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 +00002319}
2320
Josh Coalson6fe72f72002-08-20 04:01:59 +00002321unsigned evaluate_verbatim_subframe_(
2322 const FLAC__int32 signal[],
2323 unsigned blocksize,
2324 unsigned subframe_bps,
2325 FLAC__Subframe *subframe
2326)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002327{
2328 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2329
2330 subframe->data.verbatim.data = signal;
2331
Josh Coalson82b73242001-03-28 22:17:05 +00002332 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 +00002333}
2334
Josh Coalson6fe72f72002-08-20 04:01:59 +00002335unsigned find_best_partition_order_(
2336 FLAC__StreamEncoderPrivate *private_,
2337 const FLAC__int32 residual[],
2338 FLAC__uint32 abs_residual[],
2339 FLAC__uint64 abs_residual_partition_sums[],
2340 unsigned raw_bits_per_partition[],
2341 unsigned residual_samples,
2342 unsigned predictor_order,
2343 unsigned rice_parameter,
2344 unsigned min_partition_order,
2345 unsigned max_partition_order,
2346 FLAC__bool precompute_partition_sums,
2347 FLAC__bool do_escape_coding,
2348 unsigned rice_parameter_search_dist,
2349 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2350)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002351{
Josh Coalson77e3f312001-06-23 03:03:24 +00002352 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002353 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00002354 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00002355 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002356 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002357
Josh Coalson2051dd42001-04-12 22:22:34 +00002358 /* compute abs(residual) for use later */
2359 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2360 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00002361 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00002362 }
2363
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002364 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002365 min_partition_order = min(min_partition_order, max_partition_order);
2366
Josh Coalson8395d022001-07-12 21:25:22 +00002367 if(precompute_partition_sums) {
2368 int partition_order;
2369 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002370
Josh Coalsonf1eff452002-07-31 07:05:33 +00002371 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 +00002372
2373 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00002374 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
Josh Coalson8395d022001-07-12 21:25:22 +00002375
2376 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2377#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002378 if(!
2379 set_partitioned_rice_with_precompute_(
2380 residual,
2381 abs_residual_partition_sums+sum,
2382 raw_bits_per_partition+sum,
2383 residual_samples,
2384 predictor_order,
2385 rice_parameter,
2386 rice_parameter_search_dist,
2387 (unsigned)partition_order,
2388 do_escape_coding,
2389 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2390 &residual_bits
2391 )
2392 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002393#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002394 if(!
2395 set_partitioned_rice_with_precompute_(
2396 abs_residual,
2397 abs_residual_partition_sums+sum,
2398 raw_bits_per_partition+sum,
2399 residual_samples,
2400 predictor_order,
2401 rice_parameter,
2402 rice_parameter_search_dist,
2403 (unsigned)partition_order,
2404 do_escape_coding,
2405 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2406 &residual_bits
2407 )
2408 )
Josh Coalson8395d022001-07-12 21:25:22 +00002409#endif
2410 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002411 FLAC__ASSERT(best_residual_bits != 0);
2412 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002413 }
2414 sum += 1u << partition_order;
2415 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2416 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002417 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002418 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002419 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00002420 }
2421 }
Josh Coalson8395d022001-07-12 21:25:22 +00002422 else {
2423 unsigned partition_order;
2424 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2425#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002426 if(!
2427 set_partitioned_rice_(
2428 abs_residual,
2429 residual,
2430 residual_samples,
2431 predictor_order,
2432 rice_parameter,
2433 rice_parameter_search_dist,
2434 partition_order,
2435 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2436 &residual_bits
2437 )
2438 )
Josh Coalson8395d022001-07-12 21:25:22 +00002439#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002440 if(!
2441 set_partitioned_rice_(
2442 abs_residual,
2443 residual_samples,
2444 predictor_order,
2445 rice_parameter,
2446 rice_parameter_search_dist,
2447 partition_order,
2448 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2449 &residual_bits
2450 )
2451 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002452#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002453 {
2454 FLAC__ASSERT(best_residual_bits != 0);
2455 break;
2456 }
2457 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2458 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002459 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002460 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002461 }
2462 }
2463 }
2464
Josh Coalsona37ba462002-08-19 21:36:39 +00002465 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00002466 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00002467 * it is const to the outside world.
2468 */
2469 {
2470 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2471 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2472 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2473 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2474 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002475
2476 return best_residual_bits;
2477}
2478
Josh Coalson6fe72f72002-08-20 04:01:59 +00002479void precompute_partition_info_sums_(
2480 const FLAC__uint32 abs_residual[],
2481 FLAC__uint64 abs_residual_partition_sums[],
2482 unsigned residual_samples,
2483 unsigned predictor_order,
2484 unsigned min_partition_order,
2485 unsigned max_partition_order
2486)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002487{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002488 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002489 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002490 const unsigned blocksize = residual_samples + predictor_order;
2491
Josh Coalsonaef013c2001-04-24 01:25:42 +00002492 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002493 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002494 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00002495 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002496 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002497 const unsigned partitions = 1u << partition_order;
2498 const unsigned default_partition_samples = blocksize >> partition_order;
2499
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002500 FLAC__ASSERT(default_partition_samples > predictor_order);
2501
2502 for(partition = residual_sample = 0; partition < partitions; partition++) {
2503 partition_samples = default_partition_samples;
2504 if(partition == 0)
2505 partition_samples -= predictor_order;
2506 abs_residual_partition_sum = 0;
2507 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2508 abs_r = abs_residual[residual_sample];
2509 abs_residual_partition_sum += abs_r;
2510 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002511 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002512 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002513 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002514 to_partition = partitions;
2515 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002516 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00002517
Josh Coalson8395d022001-07-12 21:25:22 +00002518 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00002519 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002520 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002521 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002522 const unsigned partitions = 1u << partition_order;
2523 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00002524 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00002525 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002526 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00002527 from_partition++;
2528 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002529 }
2530 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002531}
Josh Coalson8395d022001-07-12 21:25:22 +00002532
Josh Coalson6fe72f72002-08-20 04:01:59 +00002533void precompute_partition_info_escapes_(
2534 const FLAC__int32 residual[],
2535 unsigned raw_bits_per_partition[],
2536 unsigned residual_samples,
2537 unsigned predictor_order,
2538 unsigned min_partition_order,
2539 unsigned max_partition_order
2540)
Josh Coalson8395d022001-07-12 21:25:22 +00002541{
2542 int partition_order;
2543 unsigned from_partition, to_partition = 0;
2544 const unsigned blocksize = residual_samples + predictor_order;
2545
2546 /* first do max_partition_order */
2547 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2548 FLAC__int32 r, residual_partition_min, residual_partition_max;
2549 unsigned silog2_min, silog2_max;
2550 unsigned partition, partition_sample, partition_samples, residual_sample;
2551 const unsigned partitions = 1u << partition_order;
2552 const unsigned default_partition_samples = blocksize >> partition_order;
2553
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002554 FLAC__ASSERT(default_partition_samples > predictor_order);
2555
2556 for(partition = residual_sample = 0; partition < partitions; partition++) {
2557 partition_samples = default_partition_samples;
2558 if(partition == 0)
2559 partition_samples -= predictor_order;
2560 residual_partition_min = residual_partition_max = 0;
2561 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2562 r = residual[residual_sample];
2563 if(r < residual_partition_min)
2564 residual_partition_min = r;
2565 else if(r > residual_partition_max)
2566 residual_partition_max = r;
2567 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00002568 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002569 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2570 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2571 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00002572 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002573 to_partition = partitions;
2574 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002575 }
2576
2577 /* now merge partitions for lower orders */
2578 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2579 unsigned m;
2580 unsigned i;
2581 const unsigned partitions = 1u << partition_order;
2582 for(i = 0; i < partitions; i++) {
2583 m = raw_bits_per_partition[from_partition];
2584 from_partition++;
2585 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2586 from_partition++;
2587 to_partition++;
2588 }
2589 }
2590}
Josh Coalson94e02cd2001-01-25 10:41:06 +00002591
Josh Coalson352e0f62001-03-20 22:55:50 +00002592#ifdef VARIABLE_RICE_BITS
2593#undef VARIABLE_RICE_BITS
2594#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002595#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00002596#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00002597#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002598
Josh Coalson8395d022001-07-12 21:25:22 +00002599#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002600FLAC__bool set_partitioned_rice_(
2601 const FLAC__uint32 abs_residual[],
2602 const FLAC__int32 residual[],
2603 const unsigned residual_samples,
2604 const unsigned predictor_order,
2605 const unsigned suggested_rice_parameter,
2606 const unsigned rice_parameter_search_dist,
2607 const unsigned partition_order,
2608 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2609 unsigned *bits
2610)
Josh Coalson8395d022001-07-12 21:25:22 +00002611#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002612FLAC__bool set_partitioned_rice_(
2613 const FLAC__uint32 abs_residual[],
2614 const unsigned residual_samples,
2615 const unsigned predictor_order,
2616 const unsigned suggested_rice_parameter,
2617 const unsigned rice_parameter_search_dist,
2618 const unsigned partition_order,
2619 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2620 unsigned *bits
2621)
Josh Coalson8395d022001-07-12 21:25:22 +00002622#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002623{
Josh Coalson034dfab2001-04-27 19:10:23 +00002624 unsigned rice_parameter, partition_bits;
2625#ifndef NO_RICE_SEARCH
2626 unsigned best_partition_bits;
2627 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2628#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002629 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002630 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002631
Josh Coalson1b689822001-05-31 20:11:02 +00002632 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00002633
Josh Coalsona37ba462002-08-19 21:36:39 +00002634 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2635 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002636
Josh Coalson94e02cd2001-01-25 10:41:06 +00002637 if(partition_order == 0) {
2638 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00002639
Josh Coalson034dfab2001-04-27 19:10:23 +00002640#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00002641 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002642 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00002643 min_rice_parameter = 0;
2644 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002645 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2646 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00002647 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002648#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002649 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2650#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00002651 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002652 }
2653 }
2654 else
2655 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2656
2657 best_partition_bits = 0xffffffff;
2658 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2659#endif
2660#ifdef VARIABLE_RICE_BITS
2661#ifdef FLAC__SYMMETRIC_RICE
2662 partition_bits = (2+rice_parameter) * residual_samples;
2663#else
2664 const unsigned rice_parameter_estimate = rice_parameter-1;
2665 partition_bits = (1+rice_parameter) * residual_samples;
2666#endif
2667#else
2668 partition_bits = 0;
2669#endif
2670 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2671 for(i = 0; i < residual_samples; i++) {
2672#ifdef VARIABLE_RICE_BITS
2673#ifdef FLAC__SYMMETRIC_RICE
2674 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2675#else
2676 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2677#endif
2678#else
2679 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2680#endif
2681 }
2682#ifndef NO_RICE_SEARCH
2683 if(partition_bits < best_partition_bits) {
2684 best_rice_parameter = rice_parameter;
2685 best_partition_bits = partition_bits;
2686 }
2687 }
2688#endif
2689 parameters[0] = best_rice_parameter;
2690 bits_ += best_partition_bits;
2691 }
2692 else {
2693 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002694 unsigned partition_samples;
2695 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00002696 const unsigned partitions = 1u << partition_order;
2697 for(partition = residual_sample = 0; partition < partitions; partition++) {
2698 partition_samples = (residual_samples+predictor_order) >> partition_order;
2699 if(partition == 0) {
2700 if(partition_samples <= predictor_order)
2701 return false;
2702 else
2703 partition_samples -= predictor_order;
2704 }
2705 mean = 0;
2706 save_residual_sample = residual_sample;
2707 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002708 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00002709 residual_sample = save_residual_sample;
2710#ifdef FLAC__SYMMETRIC_RICE
2711 mean += partition_samples >> 1; /* for rounding effect */
2712 mean /= partition_samples;
2713
2714 /* calc rice_parameter = floor(log2(mean)) */
2715 rice_parameter = 0;
2716 mean>>=1;
2717 while(mean) {
2718 rice_parameter++;
2719 mean >>= 1;
2720 }
2721#else
2722 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002723 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00002724 ;
2725#endif
2726 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002727#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002728 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2729#endif
2730 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2731 }
2732
2733#ifndef NO_RICE_SEARCH
2734 if(rice_parameter_search_dist) {
2735 if(rice_parameter < rice_parameter_search_dist)
2736 min_rice_parameter = 0;
2737 else
2738 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2739 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2740 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002741#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002742 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2743#endif
2744 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2745 }
2746 }
2747 else
2748 min_rice_parameter = max_rice_parameter = rice_parameter;
2749
2750 best_partition_bits = 0xffffffff;
2751 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2752#endif
2753#ifdef VARIABLE_RICE_BITS
2754#ifdef FLAC__SYMMETRIC_RICE
2755 partition_bits = (2+rice_parameter) * partition_samples;
2756#else
2757 const unsigned rice_parameter_estimate = rice_parameter-1;
2758 partition_bits = (1+rice_parameter) * partition_samples;
2759#endif
2760#else
2761 partition_bits = 0;
2762#endif
2763 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2764 save_residual_sample = residual_sample;
2765 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2766#ifdef VARIABLE_RICE_BITS
2767#ifdef FLAC__SYMMETRIC_RICE
2768 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2769#else
2770 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2771#endif
2772#else
2773 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2774#endif
2775 }
2776#ifndef NO_RICE_SEARCH
2777 if(rice_parameter != max_rice_parameter)
2778 residual_sample = save_residual_sample;
2779 if(partition_bits < best_partition_bits) {
2780 best_rice_parameter = rice_parameter;
2781 best_partition_bits = partition_bits;
2782 }
2783 }
2784#endif
2785 parameters[partition] = best_rice_parameter;
2786 bits_ += best_partition_bits;
2787 }
2788 }
2789
2790 *bits = bits_;
2791 return true;
2792}
2793
2794#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002795FLAC__bool set_partitioned_rice_with_precompute_(
2796 const FLAC__int32 residual[],
2797 const FLAC__uint64 abs_residual_partition_sums[],
2798 const unsigned raw_bits_per_partition[],
2799 const unsigned residual_samples,
2800 const unsigned predictor_order,
2801 const unsigned suggested_rice_parameter,
2802 const unsigned rice_parameter_search_dist,
2803 const unsigned partition_order,
2804 const FLAC__bool search_for_escapes,
2805 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2806 unsigned *bits
2807)
Josh Coalson8395d022001-07-12 21:25:22 +00002808#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002809FLAC__bool set_partitioned_rice_with_precompute_(
2810 const FLAC__uint32 abs_residual[],
2811 const FLAC__uint64 abs_residual_partition_sums[],
2812 const unsigned raw_bits_per_partition[],
2813 const unsigned residual_samples,
2814 const unsigned predictor_order,
2815 const unsigned suggested_rice_parameter,
2816 const unsigned rice_parameter_search_dist,
2817 const unsigned partition_order,
2818 const FLAC__bool search_for_escapes,
2819 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2820 unsigned *bits
2821)
Josh Coalson8395d022001-07-12 21:25:22 +00002822#endif
2823{
2824 unsigned rice_parameter, partition_bits;
2825#ifndef NO_RICE_SEARCH
2826 unsigned best_partition_bits;
2827 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2828#endif
2829 unsigned flat_bits;
2830 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002831 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002832
2833 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2834
Josh Coalsona37ba462002-08-19 21:36:39 +00002835 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2836 parameters = partitioned_rice_contents->parameters;
2837 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002838
Josh Coalson8395d022001-07-12 21:25:22 +00002839 if(partition_order == 0) {
2840 unsigned i;
2841
2842#ifndef NO_RICE_SEARCH
2843 if(rice_parameter_search_dist) {
2844 if(suggested_rice_parameter < rice_parameter_search_dist)
2845 min_rice_parameter = 0;
2846 else
2847 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2848 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2849 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002850#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002851 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2852#endif
2853 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2854 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002855 }
2856 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002857 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00002858
Josh Coalson034dfab2001-04-27 19:10:23 +00002859 best_partition_bits = 0xffffffff;
2860 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2861#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002862#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002863#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00002864 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002865#else
Josh Coalson352e0f62001-03-20 22:55:50 +00002866 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00002867 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002868#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002869#else
2870 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002871#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00002872 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00002873 for(i = 0; i < residual_samples; i++) {
2874#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002875#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00002876 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002877#else
Josh Coalson2051dd42001-04-12 22:22:34 +00002878 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00002879#endif
2880#else
Josh Coalson2051dd42001-04-12 22:22:34 +00002881 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 +00002882#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00002883 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002884#ifndef NO_RICE_SEARCH
2885 if(partition_bits < best_partition_bits) {
2886 best_rice_parameter = rice_parameter;
2887 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00002888 }
2889 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002890#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002891 if(search_for_escapes) {
2892 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;
2893 if(flat_bits <= best_partition_bits) {
2894 raw_bits[0] = raw_bits_per_partition[0];
2895 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2896 best_partition_bits = flat_bits;
2897 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002898 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002899 parameters[0] = best_rice_parameter;
2900 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002901 }
2902 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00002903 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002904 unsigned partition_samples;
2905 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002906 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00002907 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002908 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00002909 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002910 if(partition_samples <= predictor_order)
2911 return false;
2912 else
2913 partition_samples -= predictor_order;
2914 }
Josh Coalson05d20792001-06-29 23:12:26 +00002915 mean = abs_residual_partition_sums[partition];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002916#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson05d20792001-06-29 23:12:26 +00002917 mean += partition_samples >> 1; /* for rounding effect */
2918 mean /= partition_samples;
2919
Josh Coalson034dfab2001-04-27 19:10:23 +00002920 /* calc rice_parameter = floor(log2(mean)) */
2921 rice_parameter = 0;
2922 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002923 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002924 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002925 mean >>= 1;
2926 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00002927#else
Josh Coalson05d20792001-06-29 23:12:26 +00002928 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002929 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00002930 ;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002931#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002932 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002933#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002934 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2935#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002936 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002937 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002938
Josh Coalson034dfab2001-04-27 19:10:23 +00002939#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00002940 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002941 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00002942 min_rice_parameter = 0;
2943 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002944 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2945 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00002946 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002947#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002948 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2949#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00002950 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002951 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002952 }
2953 else
2954 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00002955
Josh Coalson034dfab2001-04-27 19:10:23 +00002956 best_partition_bits = 0xffffffff;
2957 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2958#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002959#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002960#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00002961 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002962#else
Josh Coalson034dfab2001-04-27 19:10:23 +00002963 const unsigned rice_parameter_estimate = rice_parameter-1;
2964 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002965#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002966#else
2967 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002968#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002969 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00002970 save_residual_sample = residual_sample;
2971 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00002972#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002973#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson4dacd192001-06-06 21:11:44 +00002974 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002975#else
Josh Coalson4dacd192001-06-06 21:11:44 +00002976 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00002977#endif
2978#else
Josh Coalson4dacd192001-06-06 21:11:44 +00002979 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 +00002980#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002981 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002982#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00002983 if(rice_parameter != max_rice_parameter)
2984 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00002985 if(partition_bits < best_partition_bits) {
2986 best_rice_parameter = rice_parameter;
2987 best_partition_bits = partition_bits;
2988 }
Josh Coalson2051dd42001-04-12 22:22:34 +00002989 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002990#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002991 if(search_for_escapes) {
2992 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;
2993 if(flat_bits <= best_partition_bits) {
2994 raw_bits[partition] = raw_bits_per_partition[partition];
2995 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2996 best_partition_bits = flat_bits;
2997 }
Josh Coalson2051dd42001-04-12 22:22:34 +00002998 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002999 parameters[partition] = best_rice_parameter;
3000 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003001 }
3002 }
3003
3004 *bits = bits_;
3005 return true;
3006}
Josh Coalson859bc542001-03-27 22:22:27 +00003007
Josh Coalsonf1eff452002-07-31 07:05:33 +00003008unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00003009{
3010 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00003011 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00003012
3013 for(i = 0; i < samples && !(x&1); i++)
3014 x |= signal[i];
3015
3016 if(x == 0) {
3017 shift = 0;
3018 }
3019 else {
3020 for(shift = 0; !(x&1); shift++)
3021 x >>= 1;
3022 }
3023
3024 if(shift > 0) {
3025 for(i = 0; i < samples; i++)
3026 signal[i] >>= shift;
3027 }
3028
3029 return shift;
3030}
Josh Coalsond86e03b2002-08-03 21:56:15 +00003031
3032void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3033{
3034 unsigned channel;
3035
3036 for(channel = 0; channel < channels; channel++)
3037 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3038
3039 fifo->tail += wide_samples;
3040
3041 FLAC__ASSERT(fifo->tail <= fifo->size);
3042}
3043
3044void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3045{
3046 unsigned channel;
3047 unsigned sample, wide_sample;
3048 unsigned tail = fifo->tail;
3049
3050 sample = input_offset * channels;
3051 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3052 for(channel = 0; channel < channels; channel++)
3053 fifo->data[channel][tail] = input[sample++];
3054 tail++;
3055 }
3056 fifo->tail = tail;
3057
3058 FLAC__ASSERT(fifo->tail <= fifo->size);
3059}
3060
3061FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3062{
3063 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3064 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3065 (void)decoder;
3066
3067 if(encoder->private_->verify.needs_magic_hack) {
3068 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3069 *bytes = FLAC__STREAM_SYNC_LENGTH;
3070 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3071 encoder->private_->verify.needs_magic_hack = false;
3072 }
3073 else {
3074 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00003075 /*
3076 * If we get here, a FIFO underflow has occurred,
3077 * which means there is a bug somewhere.
3078 */
3079 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003080 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3081 }
3082 else if(encoded_bytes < *bytes)
3083 *bytes = encoded_bytes;
3084 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3085 encoder->private_->verify.output.data += *bytes;
3086 encoder->private_->verify.output.bytes -= *bytes;
3087 }
3088
3089 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3090}
3091
3092FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3093{
3094 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3095 unsigned channel;
3096 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3097 const unsigned blocksize = frame->header.blocksize;
3098 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3099
3100 for(channel = 0; channel < channels; channel++) {
3101 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3102 unsigned i, sample = 0;
3103 FLAC__int32 expect = 0, got = 0;
3104
3105 for(i = 0; i < blocksize; i++) {
3106 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3107 sample = i;
3108 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3109 got = (FLAC__int32)buffer[channel][i];
3110 break;
3111 }
3112 }
3113 FLAC__ASSERT(i < blocksize);
3114 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3115 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00003116 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003117 encoder->private_->verify.error_stats.channel = channel;
3118 encoder->private_->verify.error_stats.sample = sample;
3119 encoder->private_->verify.error_stats.expected = expect;
3120 encoder->private_->verify.error_stats.got = got;
3121 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3122 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3123 }
3124 }
3125 /* dequeue the frame from the fifo */
3126 for(channel = 0; channel < channels; channel++) {
3127 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3128 }
3129 encoder->private_->verify.input_fifo.tail -= blocksize;
3130 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3131}
3132
3133void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3134{
3135 (void)decoder, (void)metadata, (void)client_data;
3136}
3137
3138void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3139{
3140 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3141 (void)decoder, (void)status;
3142 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3143}