blob: 9275ce2a9bdaadff40c74844e2a6badbfccc9609 [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
38#ifdef min
39#undef min
40#endif
41#define min(x,y) ((x)<(y)?(x):(y))
42
43#ifdef max
44#undef max
45#endif
46#define max(x,y) ((x)>(y)?(x):(y))
47
Josh Coalsond86e03b2002-08-03 21:56:15 +000048typedef struct {
49 FLAC__int32 *data[FLAC__MAX_CHANNELS];
50 unsigned size; /* of each data[] in samples */
51 unsigned tail;
52} verify_input_fifo;
53
54typedef struct {
55 const FLAC__byte *data;
56 unsigned capacity;
57 unsigned bytes;
58} verify_output;
59
60typedef enum {
61 ENCODER_IN_MAGIC = 0,
62 ENCODER_IN_METADATA = 1,
63 ENCODER_IN_AUDIO = 2
64} EncoderStateHint;
65
Josh Coalson0a15c142001-06-13 17:59:57 +000066/***********************************************************************
67 *
68 * Private class method prototypes
69 *
70 ***********************************************************************/
71
Josh Coalsonf1eff452002-07-31 07:05:33 +000072static void set_defaults_(FLAC__StreamEncoder *encoder);
73static void free_(FLAC__StreamEncoder *encoder);
74static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
Josh Coalsond86e03b2002-08-03 21:56:15 +000075static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
Josh Coalsonf1eff452002-07-31 07:05:33 +000076static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
77static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson6fe72f72002-08-20 04:01:59 +000078
79static FLAC__bool process_subframe_(
80 FLAC__StreamEncoder *encoder,
81 unsigned min_partition_order,
82 unsigned max_partition_order,
83 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +000084 const FLAC__FrameHeader *frame_header,
85 unsigned subframe_bps,
86 const FLAC__int32 integer_signal[],
87 const FLAC__real real_signal[],
88 FLAC__Subframe *subframe[2],
89 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
90 FLAC__int32 *residual[2],
91 unsigned *best_subframe,
92 unsigned *best_bits
93);
94
95static FLAC__bool add_subframe_(
96 FLAC__StreamEncoder *encoder,
97 const FLAC__FrameHeader *frame_header,
98 unsigned subframe_bps,
99 const FLAC__Subframe *subframe,
100 FLAC__BitBuffer *frame
101);
102
103static unsigned evaluate_constant_subframe_(
104 const FLAC__int32 signal,
105 unsigned subframe_bps,
106 FLAC__Subframe *subframe
107);
108
109static unsigned evaluate_fixed_subframe_(
110 FLAC__StreamEncoder *encoder,
111 const FLAC__int32 signal[],
112 FLAC__int32 residual[],
113 FLAC__uint32 abs_residual[],
114 FLAC__uint64 abs_residual_partition_sums[],
115 unsigned raw_bits_per_partition[],
116 unsigned blocksize,
117 unsigned subframe_bps,
118 unsigned order,
119 unsigned rice_parameter,
120 unsigned min_partition_order,
121 unsigned max_partition_order,
122 FLAC__bool precompute_partition_sums,
123 FLAC__bool do_escape_coding,
124 unsigned rice_parameter_search_dist,
125 FLAC__Subframe *subframe,
126 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
127);
128
129static unsigned evaluate_lpc_subframe_(
130 FLAC__StreamEncoder *encoder,
131 const FLAC__int32 signal[],
132 FLAC__int32 residual[],
133 FLAC__uint32 abs_residual[],
134 FLAC__uint64 abs_residual_partition_sums[],
135 unsigned raw_bits_per_partition[],
136 const FLAC__real lp_coeff[],
137 unsigned blocksize,
138 unsigned subframe_bps,
139 unsigned order,
140 unsigned qlp_coeff_precision,
141 unsigned rice_parameter,
142 unsigned min_partition_order,
143 unsigned max_partition_order,
144 FLAC__bool precompute_partition_sums,
145 FLAC__bool do_escape_coding,
146 unsigned rice_parameter_search_dist,
147 FLAC__Subframe *subframe,
148 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
149);
150
151static unsigned evaluate_verbatim_subframe_(
152 const FLAC__int32 signal[],
153 unsigned blocksize,
154 unsigned subframe_bps,
155 FLAC__Subframe *subframe
156);
157
158static unsigned find_best_partition_order_(
159 struct FLAC__StreamEncoderPrivate *private_,
160 const FLAC__int32 residual[],
161 FLAC__uint32 abs_residual[],
162 FLAC__uint64 abs_residual_partition_sums[],
163 unsigned raw_bits_per_partition[],
164 unsigned residual_samples,
165 unsigned predictor_order,
166 unsigned rice_parameter,
167 unsigned min_partition_order,
168 unsigned max_partition_order,
169 FLAC__bool precompute_partition_sums,
170 FLAC__bool do_escape_coding,
171 unsigned rice_parameter_search_dist,
172 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
173);
174
175static void precompute_partition_info_sums_(
176 const FLAC__uint32 abs_residual[],
177 FLAC__uint64 abs_residual_partition_sums[],
178 unsigned residual_samples,
179 unsigned predictor_order,
180 unsigned min_partition_order,
181 unsigned max_partition_order
182);
183
184static void precompute_partition_info_escapes_(
185 const FLAC__int32 residual[],
186 unsigned raw_bits_per_partition[],
187 unsigned residual_samples,
188 unsigned predictor_order,
189 unsigned min_partition_order,
190 unsigned max_partition_order
191);
192
Josh Coalson8395d022001-07-12 21:25:22 +0000193#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000194static FLAC__bool set_partitioned_rice_(
195 const FLAC__uint32 abs_residual[],
196 const FLAC__int32 residual[],
197 const unsigned residual_samples,
198 const unsigned predictor_order,
199 const unsigned suggested_rice_parameter,
200 const unsigned rice_parameter_search_dist,
201 const unsigned partition_order,
202 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
203 unsigned *bits
204);
205
206static FLAC__bool set_partitioned_rice_with_precompute_(
207 const FLAC__int32 residual[],
208 const FLAC__uint64 abs_residual_partition_sums[],
209 const unsigned raw_bits_per_partition[],
210 const unsigned residual_samples,
211 const unsigned predictor_order,
212 const unsigned suggested_rice_parameter,
213 const unsigned rice_parameter_search_dist,
214 const unsigned partition_order,
215 const FLAC__bool search_for_escapes,
216 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
217 unsigned *bits
218);
Josh Coalson8395d022001-07-12 21:25:22 +0000219#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000220static FLAC__bool set_partitioned_rice_(
221 const FLAC__uint32 abs_residual[],
222 const unsigned residual_samples,
223 const unsigned predictor_order,
224 const unsigned suggested_rice_parameter,
225 const unsigned rice_parameter_search_dist,
226 const unsigned partition_order,
227 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
228 unsigned *bits
229);
230
231static FLAC__bool set_partitioned_rice_with_precompute_(
232 const FLAC__uint32 abs_residual[],
233 const FLAC__uint64 abs_residual_partition_sums[],
234 const unsigned raw_bits_per_partition[],
235 const unsigned residual_samples,
236 const unsigned predictor_order,
237 const unsigned suggested_rice_parameter,
238 const unsigned rice_parameter_search_dist,
239 const unsigned partition_order,
240 const FLAC__bool search_for_escapes,
241 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
242 unsigned *bits
243);
Josh Coalson0a15c142001-06-13 17:59:57 +0000244#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000245
Josh Coalsonf1eff452002-07-31 07:05:33 +0000246static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000247
Josh Coalsond86e03b2002-08-03 21:56:15 +0000248/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000249static void append_to_verify_fifo_(
250 verify_input_fifo *fifo,
251 const FLAC__int32 * const input[],
252 unsigned input_offset,
253 unsigned channels,
254 unsigned wide_samples
255);
256
257static void append_to_verify_fifo_interleaved_(
258 verify_input_fifo *fifo,
259 const FLAC__int32 input[],
260 unsigned input_offset,
261 unsigned channels,
262 unsigned wide_samples
263);
264
265static FLAC__StreamDecoderReadStatus verify_read_callback_(
266 const FLAC__StreamDecoder *decoder,
267 FLAC__byte buffer[],
268 unsigned *bytes,
269 void *client_data
270);
271
272static FLAC__StreamDecoderWriteStatus verify_write_callback_(
273 const FLAC__StreamDecoder *decoder,
274 const FLAC__Frame *frame,
275 const FLAC__int32 * const buffer[],
276 void *client_data
277);
278
279static void verify_metadata_callback_(
280 const FLAC__StreamDecoder *decoder,
281 const FLAC__StreamMetadata *metadata,
282 void *client_data
283);
284
285static void verify_error_callback_(
286 const FLAC__StreamDecoder *decoder,
287 FLAC__StreamDecoderErrorStatus status,
288 void *client_data
289);
290
Josh Coalson0a15c142001-06-13 17:59:57 +0000291
292/***********************************************************************
293 *
294 * Private class data
295 *
296 ***********************************************************************/
297
298typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000299 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000300 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
301 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
302 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
303 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 +0000304 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
305 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 +0000306 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
307 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000308 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
309 FLAC__Subframe subframe_workspace_mid_side[2][2];
310 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
311 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000312 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
313 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
314 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
315 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000316 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000317 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000318 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000319 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000320 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000321 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 +0000322 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 +0000323 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000324 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
325 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
326 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000327 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsoncc682512002-06-08 04:53:42 +0000328 FLAC__StreamMetadata metadata;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000329 unsigned current_sample_number;
330 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000331 struct MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000332 FLAC__CPUInfo cpuinfo;
Josh Coalson77e3f312001-06-23 03:03:24 +0000333 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
334 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
335 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 +0000336 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 +0000337 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 +0000338 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
339 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
340 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
341 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 +0000342 FLAC__bool disable_constant_subframes;
343 FLAC__bool disable_fixed_subframes;
344 FLAC__bool disable_verbatim_subframes;
Josh Coalson681c2932002-08-01 08:19:37 +0000345 FLAC__StreamEncoderWriteCallback write_callback;
346 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000347 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000348 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000349 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
350 FLAC__int32 *integer_signal_mid_side_unaligned[2];
351 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
352 FLAC__real *real_signal_mid_side_unaligned[2];
353 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
354 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
355 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000356 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000357 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000358 /*
359 * These fields have been moved here from private function local
360 * declarations merely to save stack space during encoding.
361 */
Josh Coalsonf1eff452002-07-31 07:05:33 +0000362 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalsona37ba462002-08-19 21:36:39 +0000363 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000364 /*
365 * The data for the verify section
366 */
367 struct {
368 FLAC__StreamDecoder *decoder;
369 EncoderStateHint state_hint;
370 FLAC__bool needs_magic_hack;
371 verify_input_fifo input_fifo;
372 verify_output output;
373 struct {
374 FLAC__uint64 absolute_sample;
375 unsigned frame_number;
376 unsigned channel;
377 unsigned sample;
378 FLAC__int32 expected;
379 FLAC__int32 got;
380 } error_stats;
381 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000382 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000383} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000384
Josh Coalson0a15c142001-06-13 17:59:57 +0000385/***********************************************************************
386 *
387 * Public static class data
388 *
389 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000390
Josh Coalson6afed9f2002-10-16 22:29:47 +0000391FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000392 "FLAC__STREAM_ENCODER_OK",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000393 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
394 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson00e53872001-06-16 07:32:25 +0000395 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
Josh Coalson0a15c142001-06-13 17:59:57 +0000396 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
397 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
398 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
399 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
Josh Coalson20ac2c12002-08-30 05:47:14 +0000400 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
Josh Coalson0a15c142001-06-13 17:59:57 +0000401 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
402 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
403 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
404 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
405 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
406 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
407 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson66075c12002-06-01 05:39:38 +0000408 "FLAC__STREAM_ENCODER_INVALID_METADATA",
Josh Coalson0a15c142001-06-13 17:59:57 +0000409 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
410 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
411 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
412 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
413 "FLAC__STREAM_ENCODER_UNINITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000414};
415
Josh Coalson6afed9f2002-10-16 22:29:47 +0000416FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000417 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
418 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000419};
420
Josh Coalson0a15c142001-06-13 17:59:57 +0000421/***********************************************************************
422 *
423 * Class constructor/destructor
424 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000425 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000426FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000427{
Josh Coalson0a15c142001-06-13 17:59:57 +0000428 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000429 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000430
Josh Coalson0a15c142001-06-13 17:59:57 +0000431 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000432
Josh Coalsonea7155f2002-10-18 05:49:19 +0000433 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000434 if(encoder == 0) {
435 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000436 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000437
Josh Coalsonea7155f2002-10-18 05:49:19 +0000438 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000439 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000440 free(encoder);
441 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000442 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000443
Josh Coalsonea7155f2002-10-18 05:49:19 +0000444 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000445 if(encoder->private_ == 0) {
446 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000447 free(encoder);
448 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000449 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000450
Josh Coalsonaec256b2002-03-12 16:19:54 +0000451 encoder->private_->frame = FLAC__bitbuffer_new();
452 if(encoder->private_->frame == 0) {
453 free(encoder->private_);
454 free(encoder->protected_);
455 free(encoder);
456 return 0;
457 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000458
Josh Coalsonf1eff452002-07-31 07:05:33 +0000459 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000460
Josh Coalson3262b0d2002-08-14 20:58:42 +0000461 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000462
463 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
464 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
465 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
466 }
467 for(i = 0; i < 2; i++) {
468 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
469 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
470 }
471 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000472 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
473 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000474 }
475 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000476 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
477 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 +0000478 }
479
480 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000481 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
482 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000483 }
484 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000485 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
486 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 +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_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000490
Josh Coalsonfa697a92001-08-16 20:07:29 +0000491 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000492
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000493 return encoder;
494}
495
Josh Coalson6afed9f2002-10-16 22:29:47 +0000496FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000497{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000498 unsigned i;
499
Josh Coalsonf1eff452002-07-31 07:05:33 +0000500 FLAC__ASSERT(0 != encoder);
501 FLAC__ASSERT(0 != encoder->protected_);
502 FLAC__ASSERT(0 != encoder->private_);
503 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000504
Josh Coalson3262b0d2002-08-14 20:58:42 +0000505 encoder->private_->is_being_deleted = true;
506
507 FLAC__stream_encoder_finish(encoder);
508
Josh Coalsond86e03b2002-08-03 21:56:15 +0000509 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
510 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000511
512 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000513 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
514 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000515 }
516 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000517 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
518 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 +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_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000522
Josh Coalsonaec256b2002-03-12 16:19:54 +0000523 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000524 free(encoder->private_);
525 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000526 free(encoder);
527}
528
Josh Coalson0a15c142001-06-13 17:59:57 +0000529/***********************************************************************
530 *
531 * Public class methods
532 *
533 ***********************************************************************/
534
Josh Coalson6afed9f2002-10-16 22:29:47 +0000535FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000536{
537 unsigned i;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000538 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000539
Josh Coalsonf1eff452002-07-31 07:05:33 +0000540 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000541
Josh Coalsonfa697a92001-08-16 20:07:29 +0000542 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
543 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000544
Josh Coalsonfa697a92001-08-16 20:07:29 +0000545 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000546
Josh Coalsonfa697a92001-08-16 20:07:29 +0000547 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
548 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000549
Josh Coalsonfa697a92001-08-16 20:07:29 +0000550 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
551 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000552
Josh Coalsonfa697a92001-08-16 20:07:29 +0000553 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
554 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000555
Josh Coalsonfa697a92001-08-16 20:07:29 +0000556 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
557 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000558
Josh Coalsonfa697a92001-08-16 20:07:29 +0000559 if(encoder->protected_->bits_per_sample >= 32)
560 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 +0000561
Josh Coalson76c68bc2002-05-17 06:22:02 +0000562 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 +0000563 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000564
Josh Coalson0833f342002-07-15 05:31:55 +0000565 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000566 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000567
Josh Coalsonfa697a92001-08-16 20:07:29 +0000568 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
569 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000570
Josh Coalson20ac2c12002-08-30 05:47:14 +0000571 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
572 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
573
Josh Coalsonfa697a92001-08-16 20:07:29 +0000574 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
575 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000576
Josh Coalsonfa697a92001-08-16 20:07:29 +0000577 if(encoder->protected_->qlp_coeff_precision == 0) {
578 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000579 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
580 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000581 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 +0000582 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000583 else if(encoder->protected_->bits_per_sample == 16) {
584 if(encoder->protected_->blocksize <= 192)
585 encoder->protected_->qlp_coeff_precision = 7;
586 else if(encoder->protected_->blocksize <= 384)
587 encoder->protected_->qlp_coeff_precision = 8;
588 else if(encoder->protected_->blocksize <= 576)
589 encoder->protected_->qlp_coeff_precision = 9;
590 else if(encoder->protected_->blocksize <= 1152)
591 encoder->protected_->qlp_coeff_precision = 10;
592 else if(encoder->protected_->blocksize <= 2304)
593 encoder->protected_->qlp_coeff_precision = 11;
594 else if(encoder->protected_->blocksize <= 4608)
595 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000596 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000597 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000598 }
599 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000600 if(encoder->protected_->blocksize <= 384)
601 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
602 else if(encoder->protected_->blocksize <= 1152)
603 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
604 else
605 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000606 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000607 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000608 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000609 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 +0000610 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000611
Josh Coalsonfa697a92001-08-16 20:07:29 +0000612 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000613 if(
614 encoder->protected_->blocksize != 192 &&
615 encoder->protected_->blocksize != 576 &&
616 encoder->protected_->blocksize != 1152 &&
617 encoder->protected_->blocksize != 2304 &&
618 encoder->protected_->blocksize != 4608 &&
619 encoder->protected_->blocksize != 256 &&
620 encoder->protected_->blocksize != 512 &&
621 encoder->protected_->blocksize != 1024 &&
622 encoder->protected_->blocksize != 2048 &&
623 encoder->protected_->blocksize != 4096 &&
624 encoder->protected_->blocksize != 8192 &&
625 encoder->protected_->blocksize != 16384
626 )
Josh Coalsonfa697a92001-08-16 20:07:29 +0000627 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000628 if(
629 encoder->protected_->sample_rate != 8000 &&
630 encoder->protected_->sample_rate != 16000 &&
631 encoder->protected_->sample_rate != 22050 &&
632 encoder->protected_->sample_rate != 24000 &&
633 encoder->protected_->sample_rate != 32000 &&
634 encoder->protected_->sample_rate != 44100 &&
635 encoder->protected_->sample_rate != 48000 &&
636 encoder->protected_->sample_rate != 96000
637 )
638 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
639 if(
640 encoder->protected_->bits_per_sample != 8 &&
641 encoder->protected_->bits_per_sample != 12 &&
642 encoder->protected_->bits_per_sample != 16 &&
643 encoder->protected_->bits_per_sample != 20 &&
644 encoder->protected_->bits_per_sample != 24
645 )
646 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000647 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000648 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000649 }
650
Josh Coalsonfa697a92001-08-16 20:07:29 +0000651 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
652 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
653 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
654 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000655
Josh Coalson66075c12002-06-01 05:39:38 +0000656 /* validate metadata */
657 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
658 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000659 metadata_has_seektable = false;
660 metadata_has_vorbis_comment = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000661 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
662 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
663 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
664 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000665 if(metadata_has_seektable) /* only one is allowed */
666 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
667 metadata_has_seektable = true;
Josh Coalson0833f342002-07-15 05:31:55 +0000668 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
Josh Coalson66075c12002-06-01 05:39:38 +0000669 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
670 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000671 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
672 if(metadata_has_vorbis_comment) /* only one is allowed */
673 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
674 metadata_has_vorbis_comment = true;
675 }
Josh Coalson66075c12002-06-01 05:39:38 +0000676 }
677
Josh Coalsonfa697a92001-08-16 20:07:29 +0000678 encoder->private_->input_capacity = 0;
679 for(i = 0; i < encoder->protected_->channels; i++) {
680 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
681 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000682 }
683 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000684 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
685 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000686 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000687 for(i = 0; i < encoder->protected_->channels; i++) {
688 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
689 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
690 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000691 }
692 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000693 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
694 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
695 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000696 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000697 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
698 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
699 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
700 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
701 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
702 if(encoder->private_->loose_mid_side_stereo_frames == 0)
703 encoder->private_->loose_mid_side_stereo_frames = 1;
704 encoder->private_->loose_mid_side_stereo_frame_count = 0;
705 encoder->private_->current_sample_number = 0;
706 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000707
Josh Coalsonfa697a92001-08-16 20:07:29 +0000708 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
709 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? */
710 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000711
Josh Coalsoncf30f502001-05-23 20:57:44 +0000712 /*
713 * get the CPU info and set the function pointers
714 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000715 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000716 /* first default to the non-asm routines */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000717 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
718 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
719 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000720 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 +0000721 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000722 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000723#ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000724 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalsoncf30f502001-05-23 20:57:44 +0000725#ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000726 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000727#ifdef FLAC__HAS_NASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000728 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
729 if(encoder->protected_->max_lpc_order < 4)
730 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
731 else if(encoder->protected_->max_lpc_order < 8)
732 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
733 else if(encoder->protected_->max_lpc_order < 12)
734 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000735 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000736 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000737 }
Josh Coalson395938e2001-11-15 21:53:25 +0000738 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000739 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000740 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000741 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
742 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
743 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
744 if(encoder->private_->cpuinfo.data.ia32.mmx) {
745 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
746 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 +0000747 }
748 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000749 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
750 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 +0000751 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000752#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000753#endif
Josh Coalson021ad3b2001-07-18 00:25:52 +0000754 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000755#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000756 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000757 if(encoder->private_->use_wide_by_block) {
758 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000759 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000760
Josh Coalson8395d022001-07-12 21:25:22 +0000761 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000762 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 +0000763
Josh Coalsonf1eff452002-07-31 07:05:33 +0000764 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000765 /* the above function sets the state for us in case of an error */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000766 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000767 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000768
769 if(!FLAC__bitbuffer_init(encoder->private_->frame))
770 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000771
772 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000773 * Set up the verify stuff if necessary
774 */
775 if(encoder->protected_->verify) {
776 /*
777 * First, set up the fifo which will hold the
778 * original signal to compare against
779 */
780 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
781 for(i = 0; i < encoder->protected_->channels; i++) {
782 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
783 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
784 }
785 encoder->private_->verify.input_fifo.tail = 0;
786
787 /*
788 * Now set up a stream decoder for verification
789 */
790 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
791 if(0 == encoder->private_->verify.decoder)
792 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
793
794 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
795 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
796 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
797 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
798 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
799 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
800 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
801 }
Josh Coalson589f8c72002-08-07 23:54:55 +0000802 encoder->private_->verify.error_stats.absolute_sample = 0;
803 encoder->private_->verify.error_stats.frame_number = 0;
804 encoder->private_->verify.error_stats.channel = 0;
805 encoder->private_->verify.error_stats.sample = 0;
806 encoder->private_->verify.error_stats.expected = 0;
807 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000808
809 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000810 * write the stream header
811 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000812 if(encoder->protected_->verify)
813 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000814 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000815 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000816 if(!write_bitbuffer_(encoder, 0)) {
817 /* the above function sets the state for us in case of an error */
818 return encoder->protected_->state;
819 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000820
Josh Coalson5c491a12002-08-01 06:39:40 +0000821 /*
822 * write the STREAMINFO metadata block
823 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000824 if(encoder->protected_->verify)
825 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000826 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000827 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000828 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
829 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
830 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
831 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
832 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
833 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
834 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
835 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
836 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
837 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
838 MD5Init(&encoder->private_->md5context);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000839 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
840 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000841 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000842 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000843 if(!write_bitbuffer_(encoder, 0)) {
844 /* the above function sets the state for us in case of an error */
845 return encoder->protected_->state;
846 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000847
Josh Coalson5c491a12002-08-01 06:39:40 +0000848 /*
849 * Now that the STREAMINFO block is written, we can init this to an
850 * absurdly-high value...
851 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000852 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 +0000853 /* ... and clear this to 0 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000854 encoder->private_->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000855
Josh Coalson5c491a12002-08-01 06:39:40 +0000856 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000857 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
858 * if not, we will write an empty one (FLAC__add_metadata_block()
859 * automatically supplies the vendor string).
860 */
861 if(!metadata_has_vorbis_comment) {
862 FLAC__StreamMetadata vorbis_comment;
863 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
864 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
865 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
866 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
867 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
868 vorbis_comment.data.vorbis_comment.num_comments = 0;
869 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson7424d2f2002-11-06 07:10:38 +0000870 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
871 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000872 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
873 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
874 if(!write_bitbuffer_(encoder, 0)) {
875 /* the above function sets the state for us in case of an error */
876 return encoder->protected_->state;
877 }
878 }
879
880 /*
Josh Coalson5c491a12002-08-01 06:39:40 +0000881 * write the user's metadata blocks
882 */
883 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
884 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000885 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
886 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +0000887 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
888 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000889 if(!write_bitbuffer_(encoder, 0)) {
890 /* the above function sets the state for us in case of an error */
891 return encoder->protected_->state;
892 }
Josh Coalson5c491a12002-08-01 06:39:40 +0000893 }
894
Josh Coalsond86e03b2002-08-03 21:56:15 +0000895 if(encoder->protected_->verify)
896 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
897
Josh Coalsonfa697a92001-08-16 20:07:29 +0000898 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000899}
900
Josh Coalson6afed9f2002-10-16 22:29:47 +0000901FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000902{
Josh Coalsonf1eff452002-07-31 07:05:33 +0000903 FLAC__ASSERT(0 != encoder);
Josh Coalson2b245f22002-08-07 17:10:50 +0000904
Josh Coalsonfa697a92001-08-16 20:07:29 +0000905 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000906 return;
Josh Coalson2b245f22002-08-07 17:10:50 +0000907
Josh Coalson3262b0d2002-08-14 20:58:42 +0000908 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +0000909 if(encoder->private_->current_sample_number != 0) {
910 encoder->protected_->blocksize = encoder->private_->current_sample_number;
911 process_frame_(encoder, true); /* true => is last frame */
912 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000913 }
Josh Coalson2b245f22002-08-07 17:10:50 +0000914
Josh Coalsonfa697a92001-08-16 20:07:29 +0000915 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +0000916
Josh Coalson3262b0d2002-08-14 20:58:42 +0000917 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +0000918 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
919 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000920
Josh Coalsond86e03b2002-08-03 21:56:15 +0000921 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
922 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
923
Josh Coalsonf1eff452002-07-31 07:05:33 +0000924 free_(encoder);
925 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000926
Josh Coalsonfa697a92001-08-16 20:07:29 +0000927 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000928}
929
Josh Coalson6afed9f2002-10-16 22:29:47 +0000930FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000931{
932 FLAC__ASSERT(0 != encoder);
933 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
934 return false;
935 encoder->protected_->verify = value;
936 return true;
937}
938
Josh Coalson6afed9f2002-10-16 22:29:47 +0000939FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000940{
Josh Coalson92031602002-07-24 06:02:11 +0000941 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000942 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000943 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000944 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000945 return true;
946}
947
Josh Coalson6afed9f2002-10-16 22:29:47 +0000948FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000949{
Josh Coalson92031602002-07-24 06:02:11 +0000950 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000951 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000952 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000953 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000954 return true;
955}
956
Josh Coalson6afed9f2002-10-16 22:29:47 +0000957FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000958{
Josh Coalson92031602002-07-24 06:02:11 +0000959 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000960 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000961 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000962 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000963 return true;
964}
965
Josh Coalson6afed9f2002-10-16 22:29:47 +0000966FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000967{
Josh Coalson92031602002-07-24 06:02:11 +0000968 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000969 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000970 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000971 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000972 return true;
973}
974
Josh Coalson6afed9f2002-10-16 22:29:47 +0000975FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000976{
Josh Coalson92031602002-07-24 06:02:11 +0000977 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000978 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000979 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000980 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000981 return true;
982}
983
Josh Coalson6afed9f2002-10-16 22:29:47 +0000984FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000985{
Josh Coalson92031602002-07-24 06:02:11 +0000986 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000987 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000988 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000989 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000990 return true;
991}
992
Josh Coalson6afed9f2002-10-16 22:29:47 +0000993FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000994{
Josh Coalson92031602002-07-24 06:02:11 +0000995 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000996 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000997 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000998 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000999 return true;
1000}
1001
Josh Coalson6afed9f2002-10-16 22:29:47 +00001002FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001003{
Josh Coalson92031602002-07-24 06:02:11 +00001004 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001005 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001006 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001007 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001008 return true;
1009}
1010
Josh Coalson6afed9f2002-10-16 22:29:47 +00001011FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001012{
Josh Coalson92031602002-07-24 06:02:11 +00001013 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001014 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001015 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001016 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001017 return true;
1018}
1019
Josh Coalson6afed9f2002-10-16 22:29:47 +00001020FLAC_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 +00001021{
Josh Coalson92031602002-07-24 06:02:11 +00001022 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001023 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001024 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001025 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001026 return true;
1027}
1028
Josh Coalson6afed9f2002-10-16 22:29:47 +00001029FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001030{
Josh Coalson92031602002-07-24 06:02:11 +00001031 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001032 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001033 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001034#if 0
1035 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001036 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001037#else
1038 (void)value;
1039#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001040 return true;
1041}
1042
Josh Coalson6afed9f2002-10-16 22:29:47 +00001043FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001044{
Josh Coalson92031602002-07-24 06:02:11 +00001045 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001046 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001047 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001048 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001049 return true;
1050}
1051
Josh Coalson6afed9f2002-10-16 22:29:47 +00001052FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001053{
Josh Coalson92031602002-07-24 06:02:11 +00001054 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001055 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001056 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001057 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001058 return true;
1059}
1060
Josh Coalson6afed9f2002-10-16 22:29:47 +00001061FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001062{
Josh Coalson92031602002-07-24 06:02:11 +00001063 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001064 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001065 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001066 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001067 return true;
1068}
1069
Josh Coalson6afed9f2002-10-16 22:29:47 +00001070FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001071{
Josh Coalson92031602002-07-24 06:02:11 +00001072 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001073 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001074 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001075#if 0
1076 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001077 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001078#else
1079 (void)value;
1080#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001081 return true;
1082}
1083
Josh Coalson6afed9f2002-10-16 22:29:47 +00001084FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001085{
Josh Coalson92031602002-07-24 06:02:11 +00001086 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001087 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001088 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001089 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001090 return true;
1091}
1092
Josh Coalson6afed9f2002-10-16 22:29:47 +00001093FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001094{
Josh Coalson92031602002-07-24 06:02:11 +00001095 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001096 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001097 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001098 encoder->protected_->metadata = metadata;
1099 encoder->protected_->num_metadata_blocks = num_blocks;
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_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001104{
Josh Coalson92031602002-07-24 06:02:11 +00001105 FLAC__ASSERT(0 != encoder);
1106 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001107 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001108 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001109 encoder->private_->write_callback = value;
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_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback 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_->metadata_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_client_data(FLAC__StreamEncoder *encoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +00001124{
Josh Coalson92031602002-07-24 06:02:11 +00001125 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001126 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001127 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001128 encoder->private_->client_data = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001129 return true;
1130}
1131
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001132/*
1133 * These three functions are not static, but not publically exposed in
1134 * include/FLAC/ either. They are used by the test suite.
1135 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001136FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001137{
1138 FLAC__ASSERT(0 != encoder);
1139 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1140 return false;
1141 encoder->private_->disable_constant_subframes = value;
1142 return true;
1143}
1144
Josh Coalson6afed9f2002-10-16 22:29:47 +00001145FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001146{
1147 FLAC__ASSERT(0 != encoder);
1148 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1149 return false;
1150 encoder->private_->disable_fixed_subframes = value;
1151 return true;
1152}
1153
Josh Coalson6afed9f2002-10-16 22:29:47 +00001154FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001155{
1156 FLAC__ASSERT(0 != encoder);
1157 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1158 return false;
1159 encoder->private_->disable_verbatim_subframes = value;
1160 return true;
1161}
1162
Josh Coalson6afed9f2002-10-16 22:29:47 +00001163FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001164{
Josh Coalson92031602002-07-24 06:02:11 +00001165 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001166 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001167}
1168
Josh Coalson6afed9f2002-10-16 22:29:47 +00001169FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001170{
1171 FLAC__ASSERT(0 != encoder);
1172 if(encoder->protected_->verify)
1173 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1174 else
1175 return FLAC__STREAM_DECODER_UNINITIALIZED;
1176}
1177
Josh Coalson02954222002-11-08 06:16:31 +00001178FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1179{
1180 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1181 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1182 else
1183 return FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(encoder->private_->verify.decoder)];
1184}
1185
Josh Coalson6afed9f2002-10-16 22:29:47 +00001186FLAC_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 +00001187{
1188 FLAC__ASSERT(0 != encoder);
1189 if(0 != absolute_sample)
1190 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1191 if(0 != frame_number)
1192 *frame_number = encoder->private_->verify.error_stats.frame_number;
1193 if(0 != channel)
1194 *channel = encoder->private_->verify.error_stats.channel;
1195 if(0 != sample)
1196 *sample = encoder->private_->verify.error_stats.sample;
1197 if(0 != expected)
1198 *expected = encoder->private_->verify.error_stats.expected;
1199 if(0 != got)
1200 *got = encoder->private_->verify.error_stats.got;
1201}
1202
Josh Coalson6afed9f2002-10-16 22:29:47 +00001203FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001204{
1205 FLAC__ASSERT(0 != encoder);
1206 return encoder->protected_->verify;
1207}
1208
Josh Coalson6afed9f2002-10-16 22:29:47 +00001209FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001210{
Josh Coalson92031602002-07-24 06:02:11 +00001211 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001212 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001213}
1214
Josh Coalson6afed9f2002-10-16 22:29:47 +00001215FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001216{
Josh Coalson92031602002-07-24 06:02:11 +00001217 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001218 return encoder->protected_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001219}
1220
Josh Coalson6afed9f2002-10-16 22:29:47 +00001221FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001222{
Josh Coalson92031602002-07-24 06:02:11 +00001223 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001224 return encoder->protected_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001225}
1226
Josh Coalson6afed9f2002-10-16 22:29:47 +00001227FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001228{
Josh Coalson92031602002-07-24 06:02:11 +00001229 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001230 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001231}
1232
Josh Coalson6afed9f2002-10-16 22:29:47 +00001233FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001234{
Josh Coalson92031602002-07-24 06:02:11 +00001235 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001236 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001237}
1238
Josh Coalson6afed9f2002-10-16 22:29:47 +00001239FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001240{
Josh Coalson92031602002-07-24 06:02:11 +00001241 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001242 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001243}
1244
Josh Coalson6afed9f2002-10-16 22:29:47 +00001245FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001246{
Josh Coalson92031602002-07-24 06:02:11 +00001247 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001248 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001249}
1250
Josh Coalson6afed9f2002-10-16 22:29:47 +00001251FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001252{
Josh Coalson92031602002-07-24 06:02:11 +00001253 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001254 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001255}
1256
Josh Coalson6afed9f2002-10-16 22:29:47 +00001257FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001258{
Josh Coalson92031602002-07-24 06:02:11 +00001259 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001260 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001261}
1262
Josh Coalson6afed9f2002-10-16 22:29:47 +00001263FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001264{
Josh Coalson92031602002-07-24 06:02:11 +00001265 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001266 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001267}
1268
Josh Coalson6afed9f2002-10-16 22:29:47 +00001269FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001270{
Josh Coalson92031602002-07-24 06:02:11 +00001271 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001272 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001273}
1274
Josh Coalson6afed9f2002-10-16 22:29:47 +00001275FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001276{
Josh Coalson92031602002-07-24 06:02:11 +00001277 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001278 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001279}
1280
Josh Coalson6afed9f2002-10-16 22:29:47 +00001281FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001282{
Josh Coalson92031602002-07-24 06:02:11 +00001283 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001284 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001285}
1286
Josh Coalson6afed9f2002-10-16 22:29:47 +00001287FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001288{
Josh Coalson92031602002-07-24 06:02:11 +00001289 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001290 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001291}
1292
Josh Coalson6afed9f2002-10-16 22:29:47 +00001293FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001294{
Josh Coalson92031602002-07-24 06:02:11 +00001295 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001296 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001297}
1298
Josh Coalson6afed9f2002-10-16 22:29:47 +00001299FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001300{
1301 FLAC__ASSERT(0 != encoder);
1302 return encoder->protected_->total_samples_estimate;
1303}
1304
Josh Coalson6afed9f2002-10-16 22:29:47 +00001305FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001306{
1307 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001308 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001309 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001310
Josh Coalsonf1eff452002-07-31 07:05:33 +00001311 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001312 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001313
1314 j = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001315 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001316 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001317 if(encoder->protected_->verify)
1318 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1319
Josh Coalsonfa697a92001-08-16 20:07:29 +00001320 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001321 x = mid = side = buffer[0][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001322 encoder->private_->integer_signal[0][i] = x;
1323 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001324 x = buffer[1][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001325 encoder->private_->integer_signal[1][i] = x;
1326 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001327 mid += x;
1328 side -= x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001329 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001330 encoder->private_->integer_signal_mid_side[1][i] = side;
1331 encoder->private_->integer_signal_mid_side[0][i] = mid;
1332 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1333 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1334 encoder->private_->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001335 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001336 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001337 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001338 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001339 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001340 } while(j < samples);
1341 }
1342 else {
1343 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001344 if(encoder->protected_->verify)
1345 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1346
Josh Coalsonfa697a92001-08-16 20:07:29 +00001347 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001348 for(channel = 0; channel < channels; channel++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001349 x = buffer[channel][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001350 encoder->private_->integer_signal[channel][i] = x;
1351 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001352 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001353 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001354 }
1355 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001356 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001357 return false;
1358 }
1359 } while(j < samples);
1360 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001361
1362 return true;
1363}
1364
Josh Coalson6afed9f2002-10-16 22:29:47 +00001365FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001366{
1367 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001368 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001369 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001370
Josh Coalsonf1eff452002-07-31 07:05:33 +00001371 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001372 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001373
1374 j = k = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001375 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001376 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001377 if(encoder->protected_->verify)
1378 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1379
Josh Coalsonfa697a92001-08-16 20:07:29 +00001380 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001381 x = mid = side = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001382 encoder->private_->integer_signal[0][i] = x;
1383 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001384 x = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001385 encoder->private_->integer_signal[1][i] = x;
1386 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001387 mid += x;
1388 side -= x;
1389 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001390 encoder->private_->integer_signal_mid_side[1][i] = side;
1391 encoder->private_->integer_signal_mid_side[0][i] = mid;
1392 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1393 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1394 encoder->private_->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001395 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001396 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001397 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001398 return false;
1399 }
1400 } while(j < samples);
1401 }
1402 else {
1403 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001404 if(encoder->protected_->verify)
1405 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1406
Josh Coalsonfa697a92001-08-16 20:07:29 +00001407 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001408 for(channel = 0; channel < channels; channel++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001409 x = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001410 encoder->private_->integer_signal[channel][i] = x;
1411 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001412 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001413 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001414 }
1415 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001416 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001417 return false;
1418 }
1419 } while(j < samples);
1420 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001421
1422 return true;
1423}
1424
Josh Coalsonf1eff452002-07-31 07:05:33 +00001425/***********************************************************************
1426 *
1427 * Private class methods
1428 *
1429 ***********************************************************************/
1430
1431void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00001432{
1433 FLAC__ASSERT(0 != encoder);
1434
Josh Coalsond86e03b2002-08-03 21:56:15 +00001435 encoder->protected_->verify = false;
Josh Coalson92031602002-07-24 06:02:11 +00001436 encoder->protected_->streamable_subset = true;
1437 encoder->protected_->do_mid_side_stereo = false;
1438 encoder->protected_->loose_mid_side_stereo = false;
1439 encoder->protected_->channels = 2;
1440 encoder->protected_->bits_per_sample = 16;
1441 encoder->protected_->sample_rate = 44100;
1442 encoder->protected_->blocksize = 1152;
1443 encoder->protected_->max_lpc_order = 0;
1444 encoder->protected_->qlp_coeff_precision = 0;
1445 encoder->protected_->do_qlp_coeff_prec_search = false;
1446 encoder->protected_->do_exhaustive_model_search = false;
1447 encoder->protected_->do_escape_coding = false;
1448 encoder->protected_->min_residual_partition_order = 0;
1449 encoder->protected_->max_residual_partition_order = 0;
1450 encoder->protected_->rice_parameter_search_dist = 0;
1451 encoder->protected_->total_samples_estimate = 0;
1452 encoder->protected_->metadata = 0;
1453 encoder->protected_->num_metadata_blocks = 0;
1454
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001455 encoder->private_->disable_constant_subframes = false;
1456 encoder->private_->disable_fixed_subframes = false;
1457 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson92031602002-07-24 06:02:11 +00001458 encoder->private_->write_callback = 0;
1459 encoder->private_->metadata_callback = 0;
1460 encoder->private_->client_data = 0;
1461}
1462
Josh Coalsonf1eff452002-07-31 07:05:33 +00001463void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00001464{
1465 unsigned i, channel;
1466
Josh Coalsonf1eff452002-07-31 07:05:33 +00001467 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00001468 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001469 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001470 free(encoder->private_->integer_signal_unaligned[i]);
1471 encoder->private_->integer_signal_unaligned[i] = 0;
1472 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001473 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001474 free(encoder->private_->real_signal_unaligned[i]);
1475 encoder->private_->real_signal_unaligned[i] = 0;
1476 }
1477 }
1478 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001479 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001480 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1481 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1482 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001483 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001484 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1485 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1486 }
1487 }
1488 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1489 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001490 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001491 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1492 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1493 }
1494 }
1495 }
1496 for(channel = 0; channel < 2; channel++) {
1497 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001498 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001499 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1500 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1501 }
1502 }
1503 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001504 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001505 free(encoder->private_->abs_residual_unaligned);
1506 encoder->private_->abs_residual_unaligned = 0;
1507 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001508 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001509 free(encoder->private_->abs_residual_partition_sums_unaligned);
1510 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1511 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001512 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001513 free(encoder->private_->raw_bits_per_partition_unaligned);
1514 encoder->private_->raw_bits_per_partition_unaligned = 0;
1515 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001516 if(encoder->protected_->verify) {
1517 for(i = 0; i < encoder->protected_->channels; i++) {
1518 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1519 free(encoder->private_->verify.input_fifo.data[i]);
1520 encoder->private_->verify.input_fifo.data[i] = 0;
1521 }
1522 }
1523 }
Josh Coalson639aeb02002-07-25 05:38:23 +00001524 FLAC__bitbuffer_free(encoder->private_->frame);
1525}
1526
Josh Coalsonf1eff452002-07-31 07:05:33 +00001527FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001528{
Josh Coalson77e3f312001-06-23 03:03:24 +00001529 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00001530 unsigned i, channel;
1531
1532 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001533 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1534 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00001535
1536 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001537 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00001538 return true;
1539
1540 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00001541
Josh Coalsonc9c0d132002-10-04 05:29:05 +00001542 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1543 * requires that the input arrays (in our case the integer signals)
1544 * have a buffer of up to 3 zeroes in front (at negative indices) for
1545 * alignment purposes; we use 4 to keep the data well-aligned.
1546 */
Josh Coalson8395d022001-07-12 21:25:22 +00001547
Josh Coalsonfa697a92001-08-16 20:07:29 +00001548 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1549 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1550 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1551 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1552 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001553 }
1554 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001555 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]);
1556 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]);
1557 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1558 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001559 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001560 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001561 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001562 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 +00001563 }
1564 }
1565 for(channel = 0; ok && channel < 2; channel++) {
1566 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001567 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 +00001568 }
1569 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001570 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1571 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 */
1572 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1573 if(encoder->protected_->do_escape_coding)
1574 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 +00001575
1576 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001577 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00001578 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001579 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00001580
1581 return ok;
1582}
1583
Josh Coalsond86e03b2002-08-03 21:56:15 +00001584FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00001585{
1586 const FLAC__byte *buffer;
1587 unsigned bytes;
1588
1589 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1590
1591 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1592
Josh Coalsond86e03b2002-08-03 21:56:15 +00001593 if(encoder->protected_->verify) {
1594 encoder->private_->verify.output.data = buffer;
1595 encoder->private_->verify.output.bytes = bytes;
1596 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1597 encoder->private_->verify.needs_magic_hack = true;
1598 }
1599 else {
1600 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1601 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1602 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1603 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1604 return false;
1605 }
1606 }
1607 }
1608
1609 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1610 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalson5c491a12002-08-01 06:39:40 +00001611 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001612 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001613
1614 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1615
Josh Coalsond86e03b2002-08-03 21:56:15 +00001616 if(samples > 0) {
1617 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1618 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1619 }
1620
Josh Coalson5c491a12002-08-01 06:39:40 +00001621 return true;
1622}
1623
Josh Coalsonf1eff452002-07-31 07:05:33 +00001624FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00001625{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001626 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001627
1628 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00001629 * Accumulate raw signal to the MD5 signature
1630 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00001631 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 +00001632 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00001633 return false;
1634 }
1635
1636 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001637 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001638 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00001639 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001640 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001641 return false;
1642 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001643
1644 /*
1645 * Zero-pad the frame to a byte_boundary
1646 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001647 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001648 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001649 return false;
1650 }
1651
1652 /*
Josh Coalson215af572001-03-27 01:15:58 +00001653 * CRC-16 the whole thing
1654 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001655 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1656 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 +00001657
1658 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001659 * Write it
1660 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001661 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1662 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001663 return false;
1664 }
1665
1666 /*
1667 * Get ready for the next frame
1668 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001669 encoder->private_->current_sample_number = 0;
1670 encoder->private_->current_frame_number++;
1671 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001672
1673 return true;
1674}
1675
Josh Coalsonf1eff452002-07-31 07:05:33 +00001676FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001677{
1678 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001679 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00001680 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001681
1682 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00001683 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00001684 */
1685 if(is_last_frame) {
1686 max_partition_order = 0;
1687 }
1688 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001689 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1690 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001691 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001692 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001693
Josh Coalsonfa697a92001-08-16 20:07:29 +00001694 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 +00001695
Josh Coalson94e02cd2001-01-25 10:41:06 +00001696 /*
1697 * Setup the frame
1698 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001699 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001700 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001701 return false;
1702 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001703 frame_header.blocksize = encoder->protected_->blocksize;
1704 frame_header.sample_rate = encoder->protected_->sample_rate;
1705 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001706 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001707 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001708 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001709 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001710
1711 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001712 * Figure out what channel assignments to try
1713 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001714 if(encoder->protected_->do_mid_side_stereo) {
1715 if(encoder->protected_->loose_mid_side_stereo) {
1716 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001717 do_independent = true;
1718 do_mid_side = true;
1719 }
1720 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001721 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001722 do_mid_side = !do_independent;
1723 }
1724 }
1725 else {
1726 do_independent = true;
1727 do_mid_side = true;
1728 }
1729 }
1730 else {
1731 do_independent = true;
1732 do_mid_side = false;
1733 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001734
Josh Coalson1b689822001-05-31 20:11:02 +00001735 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001736
1737 /*
Josh Coalson82b73242001-03-28 22:17:05 +00001738 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00001739 */
1740 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001741 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001742 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001743 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1744 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00001745 }
Josh Coalson859bc542001-03-27 22:22:27 +00001746 }
1747 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001748 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00001749 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001750 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001751 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1752 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00001753 }
Josh Coalson859bc542001-03-27 22:22:27 +00001754 }
1755
1756 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001757 * First do a normal encoding pass of each independent channel
1758 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001759 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001760 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00001761 if(!
1762 process_subframe_(
1763 encoder,
1764 min_partition_order,
1765 max_partition_order,
1766 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001767 &frame_header,
1768 encoder->private_->subframe_bps[channel],
1769 encoder->private_->integer_signal[channel],
1770 encoder->private_->real_signal[channel],
1771 encoder->private_->subframe_workspace_ptr[channel],
1772 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1773 encoder->private_->residual_workspace[channel],
1774 encoder->private_->best_subframe+channel,
1775 encoder->private_->best_subframe_bits+channel
1776 )
1777 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001778 return false;
1779 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001780 }
1781
1782 /*
1783 * Now do mid and side channels if requested
1784 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001785 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001786 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001787
1788 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00001789 if(!
1790 process_subframe_(
1791 encoder,
1792 min_partition_order,
1793 max_partition_order,
1794 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001795 &frame_header,
1796 encoder->private_->subframe_bps_mid_side[channel],
1797 encoder->private_->integer_signal_mid_side[channel],
1798 encoder->private_->real_signal_mid_side[channel],
1799 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1800 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1801 encoder->private_->residual_workspace_mid_side[channel],
1802 encoder->private_->best_subframe_mid_side+channel,
1803 encoder->private_->best_subframe_bits_mid_side+channel
1804 )
1805 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00001806 return false;
1807 }
1808 }
1809
1810 /*
1811 * Compose the frame bitbuffer
1812 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001813 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00001814 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1815 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001816 FLAC__ChannelAssignment channel_assignment;
1817
Josh Coalsonfa697a92001-08-16 20:07:29 +00001818 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001819
Josh Coalsonfa697a92001-08-16 20:07:29 +00001820 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1821 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 +00001822 }
1823 else {
1824 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1825 unsigned min_bits;
1826 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001827
Josh Coalson1b689822001-05-31 20:11:02 +00001828 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001829
1830 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001831 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1832 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1833 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1834 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 +00001835
Josh Coalson7424d2f2002-11-06 07:10:38 +00001836 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 +00001837 if(bits[ca] < min_bits) {
1838 min_bits = bits[ca];
1839 channel_assignment = ca;
1840 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001841 }
1842 }
1843
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001844 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001845
Josh Coalsonaec256b2002-03-12 16:19:54 +00001846 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001847 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001848 return false;
1849 }
1850
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001851 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001852 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001853 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1854 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001855 break;
1856 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001857 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1858 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001859 break;
1860 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001861 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1862 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001863 break;
1864 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001865 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1866 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001867 break;
1868 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001869 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001870 }
Josh Coalson82b73242001-03-28 22:17:05 +00001871
1872 switch(channel_assignment) {
1873 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001874 left_bps = encoder->private_->subframe_bps [0];
1875 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001876 break;
1877 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001878 left_bps = encoder->private_->subframe_bps [0];
1879 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001880 break;
1881 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001882 left_bps = encoder->private_->subframe_bps_mid_side[1];
1883 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001884 break;
1885 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001886 left_bps = encoder->private_->subframe_bps_mid_side[0];
1887 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001888 break;
1889 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001890 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00001891 }
1892
1893 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00001894 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001895 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00001896 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001897 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001898 }
1899 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001900 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001901 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001902 return false;
1903 }
1904
Josh Coalsonfa697a92001-08-16 20:07:29 +00001905 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001906 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 +00001907 /* the above function sets the state for us in case of an error */
1908 return false;
1909 }
1910 }
1911 }
1912
Josh Coalsonfa697a92001-08-16 20:07:29 +00001913 if(encoder->protected_->loose_mid_side_stereo) {
1914 encoder->private_->loose_mid_side_stereo_frame_count++;
1915 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1916 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001917 }
1918
Josh Coalsonfa697a92001-08-16 20:07:29 +00001919 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001920
Josh Coalson94e02cd2001-01-25 10:41:06 +00001921 return true;
1922}
1923
Josh Coalson6fe72f72002-08-20 04:01:59 +00001924FLAC__bool process_subframe_(
1925 FLAC__StreamEncoder *encoder,
1926 unsigned min_partition_order,
1927 unsigned max_partition_order,
1928 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001929 const FLAC__FrameHeader *frame_header,
1930 unsigned subframe_bps,
1931 const FLAC__int32 integer_signal[],
1932 const FLAC__real real_signal[],
1933 FLAC__Subframe *subframe[2],
1934 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1935 FLAC__int32 *residual[2],
1936 unsigned *best_subframe,
1937 unsigned *best_bits
1938)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001939{
Josh Coalson77e3f312001-06-23 03:03:24 +00001940 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1941 FLAC__real lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001942 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 +00001943 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001944 unsigned min_lpc_order, max_lpc_order, lpc_order;
1945 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1946 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1947 unsigned rice_parameter;
1948 unsigned _candidate_bits, _best_bits;
1949 unsigned _best_subframe;
1950
1951 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1952 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001953 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
1954 _best_bits = UINT_MAX;
1955 else
1956 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001957
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001958 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1959 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001960 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 +00001961 /* check for constant subframe */
1962 if(!encoder->private_->disable_constant_subframes && fixed_residual_bits_per_sample[1] == 0.0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001963 /* 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 +00001964 unsigned i;
1965 signal_is_constant = true;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001966 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1967 if(integer_signal[0] != integer_signal[i]) {
1968 signal_is_constant = false;
1969 break;
1970 }
1971 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001972 }
1973 if(signal_is_constant) {
1974 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1975 if(_candidate_bits < _best_bits) {
1976 _best_subframe = !_best_subframe;
1977 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001978 }
1979 }
1980 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001981 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
1982 /* encode fixed */
1983 if(encoder->protected_->do_exhaustive_model_search) {
1984 min_fixed_order = 0;
1985 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00001986 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001987 else {
1988 min_fixed_order = max_fixed_order = guess_fixed_order;
1989 }
1990 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1991 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1992 continue; /* don't even try */
1993 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 */
1994#ifndef FLAC__SYMMETRIC_RICE
1995 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
1996#endif
1997 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1998#ifdef DEBUG_VERBOSE
1999 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2000#endif
2001 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2002 }
2003 _candidate_bits =
2004 evaluate_fixed_subframe_(
2005 encoder,
2006 integer_signal,
2007 residual[!_best_subframe],
2008 encoder->private_->abs_residual,
2009 encoder->private_->abs_residual_partition_sums,
2010 encoder->private_->raw_bits_per_partition,
2011 frame_header->blocksize,
2012 subframe_bps,
2013 fixed_order,
2014 rice_parameter,
2015 min_partition_order,
2016 max_partition_order,
2017 precompute_partition_sums,
2018 encoder->protected_->do_escape_coding,
2019 encoder->protected_->rice_parameter_search_dist,
2020 subframe[!_best_subframe],
2021 partitioned_rice_contents[!_best_subframe]
2022 );
2023 if(_candidate_bits < _best_bits) {
2024 _best_subframe = !_best_subframe;
2025 _best_bits = _candidate_bits;
2026 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002027 }
2028 }
2029
2030 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002031 if(encoder->protected_->max_lpc_order > 0) {
2032 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002033 max_lpc_order = frame_header->blocksize-1;
2034 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002035 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002036 if(max_lpc_order > 0) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002037 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002038 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2039 if(autoc[0] != 0.0) {
Josh Coalson8084b052001-11-01 00:27:29 +00002040 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002041 if(encoder->protected_->do_exhaustive_model_search) {
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002042 min_lpc_order = 1;
2043 }
2044 else {
Josh Coalson82b73242001-03-28 22:17:05 +00002045 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 +00002046 min_lpc_order = max_lpc_order = guess_lpc_order;
2047 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002048 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2049 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 +00002050 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002051 continue; /* don't even try */
2052 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 +00002053#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002054 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00002055#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002056 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002057#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002058 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2059#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002060 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002061 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002062 if(encoder->protected_->do_qlp_coeff_prec_search) {
2063 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2064 /* ensure a 32-bit datapath throughout for 16bps or less */
2065 if(subframe_bps <= 16)
2066 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2067 else
2068 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2069 }
2070 else {
2071 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2072 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002073 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 +00002074 _candidate_bits =
2075 evaluate_lpc_subframe_(
2076 encoder,
2077 integer_signal,
2078 residual[!_best_subframe],
2079 encoder->private_->abs_residual,
2080 encoder->private_->abs_residual_partition_sums,
2081 encoder->private_->raw_bits_per_partition,
2082 encoder->private_->lp_coeff[lpc_order-1],
2083 frame_header->blocksize,
2084 subframe_bps,
2085 lpc_order,
2086 qlp_coeff_precision,
2087 rice_parameter,
2088 min_partition_order,
2089 max_partition_order,
2090 precompute_partition_sums,
2091 encoder->protected_->do_escape_coding,
2092 encoder->protected_->rice_parameter_search_dist,
2093 subframe[!_best_subframe],
2094 partitioned_rice_contents[!_best_subframe]
2095 );
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002096 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2097 if(_candidate_bits < _best_bits) {
2098 _best_subframe = !_best_subframe;
2099 _best_bits = _candidate_bits;
2100 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002101 }
2102 }
2103 }
2104 }
2105 }
2106 }
2107 }
2108 }
2109
Josh Coalson72695802002-10-11 06:25:16 +00002110 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2111 if(_best_bits == UINT_MAX) {
2112 FLAC__ASSERT(_best_subframe == 0);
2113 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2114 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002115
Josh Coalson94e02cd2001-01-25 10:41:06 +00002116 *best_subframe = _best_subframe;
2117 *best_bits = _best_bits;
2118
2119 return true;
2120}
2121
Josh Coalson6fe72f72002-08-20 04:01:59 +00002122FLAC__bool add_subframe_(
2123 FLAC__StreamEncoder *encoder,
2124 const FLAC__FrameHeader *frame_header,
2125 unsigned subframe_bps,
2126 const FLAC__Subframe *subframe,
2127 FLAC__BitBuffer *frame
2128)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002129{
2130 switch(subframe->type) {
2131 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002132//@@@@fprintf(stderr,"@@@@ add CONSTANT, bps=%u\n",subframe_bps);
Josh Coalson82b73242001-03-28 22:17:05 +00002133 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002134 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002135 return false;
2136 }
2137 break;
2138 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002139//@@@@fprintf(stderr,"@@@@ add FIXED, bps=%u, order=%u\n",subframe_bps,subframe->data.fixed.order);
Josh Coalson82b73242001-03-28 22:17:05 +00002140 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 +00002141 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002142 return false;
2143 }
2144 break;
2145 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002146//@@@@fprintf(stderr,"@@@@ add LPC, bps=%u, order=%u, prec=%u, shift=%d\n",subframe_bps,subframe->data.lpc.order,subframe->data.lpc.qlp_coeff_precision,subframe->data.lpc.quantization_level);
Josh Coalson82b73242001-03-28 22:17:05 +00002147 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 +00002148 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002149 return false;
2150 }
2151 break;
2152 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002153//@@@@fprintf(stderr,"@@@@ add VERBATIM, bps=%u\n",subframe_bps);
Josh Coalson82b73242001-03-28 22:17:05 +00002154 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002155 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002156 return false;
2157 }
2158 break;
2159 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002160 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002161 }
2162
2163 return true;
2164}
2165
Josh Coalson6fe72f72002-08-20 04:01:59 +00002166unsigned evaluate_constant_subframe_(
2167 const FLAC__int32 signal,
2168 unsigned subframe_bps,
2169 FLAC__Subframe *subframe
2170)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002171{
2172 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2173 subframe->data.constant.value = signal;
2174
Josh Coalson82b73242001-03-28 22:17:05 +00002175 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 +00002176}
2177
Josh Coalson6fe72f72002-08-20 04:01:59 +00002178unsigned evaluate_fixed_subframe_(
2179 FLAC__StreamEncoder *encoder,
2180 const FLAC__int32 signal[],
2181 FLAC__int32 residual[],
2182 FLAC__uint32 abs_residual[],
2183 FLAC__uint64 abs_residual_partition_sums[],
2184 unsigned raw_bits_per_partition[],
2185 unsigned blocksize,
2186 unsigned subframe_bps,
2187 unsigned order,
2188 unsigned rice_parameter,
2189 unsigned min_partition_order,
2190 unsigned max_partition_order,
2191 FLAC__bool precompute_partition_sums,
2192 FLAC__bool do_escape_coding,
2193 unsigned rice_parameter_search_dist,
2194 FLAC__Subframe *subframe,
2195 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2196)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002197{
2198 unsigned i, residual_bits;
2199 const unsigned residual_samples = blocksize - order;
2200
2201 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2202
2203 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2204
2205 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002206 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002207 subframe->data.fixed.residual = residual;
2208
Josh Coalson6fe72f72002-08-20 04:01:59 +00002209 residual_bits =
2210 find_best_partition_order_(
2211 encoder->private_,
2212 residual,
2213 abs_residual,
2214 abs_residual_partition_sums,
2215 raw_bits_per_partition,
2216 residual_samples,
2217 order,
2218 rice_parameter,
2219 min_partition_order,
2220 max_partition_order,
2221 precompute_partition_sums,
2222 do_escape_coding,
2223 rice_parameter_search_dist,
2224 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2225 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002226
2227 subframe->data.fixed.order = order;
2228 for(i = 0; i < order; i++)
2229 subframe->data.fixed.warmup[i] = signal[i];
2230
Josh Coalson82b73242001-03-28 22:17:05 +00002231 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 +00002232}
2233
Josh Coalson6fe72f72002-08-20 04:01:59 +00002234unsigned evaluate_lpc_subframe_(
2235 FLAC__StreamEncoder *encoder,
2236 const FLAC__int32 signal[],
2237 FLAC__int32 residual[],
2238 FLAC__uint32 abs_residual[],
2239 FLAC__uint64 abs_residual_partition_sums[],
2240 unsigned raw_bits_per_partition[],
2241 const FLAC__real lp_coeff[],
2242 unsigned blocksize,
2243 unsigned subframe_bps,
2244 unsigned order,
2245 unsigned qlp_coeff_precision,
2246 unsigned rice_parameter,
2247 unsigned min_partition_order,
2248 unsigned max_partition_order,
2249 FLAC__bool precompute_partition_sums,
2250 FLAC__bool do_escape_coding,
2251 unsigned rice_parameter_search_dist,
2252 FLAC__Subframe *subframe,
2253 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2254)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002255{
Josh Coalson77e3f312001-06-23 03:03:24 +00002256 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002257 unsigned i, residual_bits;
2258 int quantization, ret;
2259 const unsigned residual_samples = blocksize - order;
2260
Josh Coalson20ac2c12002-08-30 05:47:14 +00002261 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2262 if(subframe_bps <= 16) {
2263 FLAC__ASSERT(order > 0);
2264 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2265 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2266 }
2267
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002268 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002269 if(ret != 0)
2270 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2271
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00002272 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2273 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2274 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2275 else
2276 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 +00002277 else
2278 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 +00002279
2280 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2281
2282 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002283 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002284 subframe->data.lpc.residual = residual;
2285
Josh Coalson6fe72f72002-08-20 04:01:59 +00002286 residual_bits =
2287 find_best_partition_order_(
2288 encoder->private_,
2289 residual,
2290 abs_residual,
2291 abs_residual_partition_sums,
2292 raw_bits_per_partition,
2293 residual_samples,
2294 order,
2295 rice_parameter,
2296 min_partition_order,
2297 max_partition_order,
2298 precompute_partition_sums,
2299 do_escape_coding,
2300 rice_parameter_search_dist,
2301 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2302 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002303
2304 subframe->data.lpc.order = order;
2305 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2306 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00002307 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002308 for(i = 0; i < order; i++)
2309 subframe->data.lpc.warmup[i] = signal[i];
2310
Josh Coalson82b73242001-03-28 22:17:05 +00002311 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 +00002312}
2313
Josh Coalson6fe72f72002-08-20 04:01:59 +00002314unsigned evaluate_verbatim_subframe_(
2315 const FLAC__int32 signal[],
2316 unsigned blocksize,
2317 unsigned subframe_bps,
2318 FLAC__Subframe *subframe
2319)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002320{
2321 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2322
2323 subframe->data.verbatim.data = signal;
2324
Josh Coalson82b73242001-03-28 22:17:05 +00002325 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 +00002326}
2327
Josh Coalson6fe72f72002-08-20 04:01:59 +00002328unsigned find_best_partition_order_(
2329 FLAC__StreamEncoderPrivate *private_,
2330 const FLAC__int32 residual[],
2331 FLAC__uint32 abs_residual[],
2332 FLAC__uint64 abs_residual_partition_sums[],
2333 unsigned raw_bits_per_partition[],
2334 unsigned residual_samples,
2335 unsigned predictor_order,
2336 unsigned rice_parameter,
2337 unsigned min_partition_order,
2338 unsigned max_partition_order,
2339 FLAC__bool precompute_partition_sums,
2340 FLAC__bool do_escape_coding,
2341 unsigned rice_parameter_search_dist,
2342 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2343)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002344{
Josh Coalson77e3f312001-06-23 03:03:24 +00002345 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002346 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00002347 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00002348 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002349 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002350
Josh Coalson2051dd42001-04-12 22:22:34 +00002351 /* compute abs(residual) for use later */
2352 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2353 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00002354 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00002355 }
2356
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002357 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 +00002358 min_partition_order = min(min_partition_order, max_partition_order);
2359
Josh Coalson8395d022001-07-12 21:25:22 +00002360 if(precompute_partition_sums) {
2361 int partition_order;
2362 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002363
Josh Coalsonf1eff452002-07-31 07:05:33 +00002364 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 +00002365
2366 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00002367 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 +00002368
2369 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2370#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002371 if(!
2372 set_partitioned_rice_with_precompute_(
2373 residual,
2374 abs_residual_partition_sums+sum,
2375 raw_bits_per_partition+sum,
2376 residual_samples,
2377 predictor_order,
2378 rice_parameter,
2379 rice_parameter_search_dist,
2380 (unsigned)partition_order,
2381 do_escape_coding,
2382 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2383 &residual_bits
2384 )
2385 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002386#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002387 if(!
2388 set_partitioned_rice_with_precompute_(
2389 abs_residual,
2390 abs_residual_partition_sums+sum,
2391 raw_bits_per_partition+sum,
2392 residual_samples,
2393 predictor_order,
2394 rice_parameter,
2395 rice_parameter_search_dist,
2396 (unsigned)partition_order,
2397 do_escape_coding,
2398 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2399 &residual_bits
2400 )
2401 )
Josh Coalson8395d022001-07-12 21:25:22 +00002402#endif
2403 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002404 FLAC__ASSERT(best_residual_bits != 0);
2405 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002406 }
2407 sum += 1u << partition_order;
2408 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2409 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002410 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002411 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002412 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00002413 }
2414 }
Josh Coalson8395d022001-07-12 21:25:22 +00002415 else {
2416 unsigned partition_order;
2417 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2418#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002419 if(!
2420 set_partitioned_rice_(
2421 abs_residual,
2422 residual,
2423 residual_samples,
2424 predictor_order,
2425 rice_parameter,
2426 rice_parameter_search_dist,
2427 partition_order,
2428 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2429 &residual_bits
2430 )
2431 )
Josh Coalson8395d022001-07-12 21:25:22 +00002432#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002433 if(!
2434 set_partitioned_rice_(
2435 abs_residual,
2436 residual_samples,
2437 predictor_order,
2438 rice_parameter,
2439 rice_parameter_search_dist,
2440 partition_order,
2441 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2442 &residual_bits
2443 )
2444 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002445#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002446 {
2447 FLAC__ASSERT(best_residual_bits != 0);
2448 break;
2449 }
2450 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2451 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002452 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002453 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002454 }
2455 }
2456 }
2457
Josh Coalsona37ba462002-08-19 21:36:39 +00002458 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00002459 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00002460 * it is const to the outside world.
2461 */
2462 {
2463 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2464 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2465 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2466 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2467 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002468
2469 return best_residual_bits;
2470}
2471
Josh Coalson6fe72f72002-08-20 04:01:59 +00002472void precompute_partition_info_sums_(
2473 const FLAC__uint32 abs_residual[],
2474 FLAC__uint64 abs_residual_partition_sums[],
2475 unsigned residual_samples,
2476 unsigned predictor_order,
2477 unsigned min_partition_order,
2478 unsigned max_partition_order
2479)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002480{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002481 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002482 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002483 const unsigned blocksize = residual_samples + predictor_order;
2484
Josh Coalsonaef013c2001-04-24 01:25:42 +00002485 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002486 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002487 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00002488 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002489 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002490 const unsigned partitions = 1u << partition_order;
2491 const unsigned default_partition_samples = blocksize >> partition_order;
2492
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002493 FLAC__ASSERT(default_partition_samples > predictor_order);
2494
2495 for(partition = residual_sample = 0; partition < partitions; partition++) {
2496 partition_samples = default_partition_samples;
2497 if(partition == 0)
2498 partition_samples -= predictor_order;
2499 abs_residual_partition_sum = 0;
2500 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2501 abs_r = abs_residual[residual_sample];
2502 abs_residual_partition_sum += abs_r;
2503 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002504 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002505 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002506 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002507 to_partition = partitions;
2508 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002509 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00002510
Josh Coalson8395d022001-07-12 21:25:22 +00002511 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00002512 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002513 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002514 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002515 const unsigned partitions = 1u << partition_order;
2516 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00002517 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00002518 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002519 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00002520 from_partition++;
2521 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002522 }
2523 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002524}
Josh Coalson8395d022001-07-12 21:25:22 +00002525
Josh Coalson6fe72f72002-08-20 04:01:59 +00002526void precompute_partition_info_escapes_(
2527 const FLAC__int32 residual[],
2528 unsigned raw_bits_per_partition[],
2529 unsigned residual_samples,
2530 unsigned predictor_order,
2531 unsigned min_partition_order,
2532 unsigned max_partition_order
2533)
Josh Coalson8395d022001-07-12 21:25:22 +00002534{
2535 int partition_order;
2536 unsigned from_partition, to_partition = 0;
2537 const unsigned blocksize = residual_samples + predictor_order;
2538
2539 /* first do max_partition_order */
2540 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2541 FLAC__int32 r, residual_partition_min, residual_partition_max;
2542 unsigned silog2_min, silog2_max;
2543 unsigned partition, partition_sample, partition_samples, residual_sample;
2544 const unsigned partitions = 1u << partition_order;
2545 const unsigned default_partition_samples = blocksize >> partition_order;
2546
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002547 FLAC__ASSERT(default_partition_samples > predictor_order);
2548
2549 for(partition = residual_sample = 0; partition < partitions; partition++) {
2550 partition_samples = default_partition_samples;
2551 if(partition == 0)
2552 partition_samples -= predictor_order;
2553 residual_partition_min = residual_partition_max = 0;
2554 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2555 r = residual[residual_sample];
2556 if(r < residual_partition_min)
2557 residual_partition_min = r;
2558 else if(r > residual_partition_max)
2559 residual_partition_max = r;
2560 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00002561 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002562 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2563 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2564 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00002565 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002566 to_partition = partitions;
2567 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002568 }
2569
2570 /* now merge partitions for lower orders */
2571 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2572 unsigned m;
2573 unsigned i;
2574 const unsigned partitions = 1u << partition_order;
2575 for(i = 0; i < partitions; i++) {
2576 m = raw_bits_per_partition[from_partition];
2577 from_partition++;
2578 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2579 from_partition++;
2580 to_partition++;
2581 }
2582 }
2583}
Josh Coalson94e02cd2001-01-25 10:41:06 +00002584
Josh Coalson352e0f62001-03-20 22:55:50 +00002585#ifdef VARIABLE_RICE_BITS
2586#undef VARIABLE_RICE_BITS
2587#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002588#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00002589#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00002590#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002591
Josh Coalson8395d022001-07-12 21:25:22 +00002592#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002593FLAC__bool set_partitioned_rice_(
2594 const FLAC__uint32 abs_residual[],
2595 const FLAC__int32 residual[],
2596 const unsigned residual_samples,
2597 const unsigned predictor_order,
2598 const unsigned suggested_rice_parameter,
2599 const unsigned rice_parameter_search_dist,
2600 const unsigned partition_order,
2601 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2602 unsigned *bits
2603)
Josh Coalson8395d022001-07-12 21:25:22 +00002604#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002605FLAC__bool set_partitioned_rice_(
2606 const FLAC__uint32 abs_residual[],
2607 const unsigned residual_samples,
2608 const unsigned predictor_order,
2609 const unsigned suggested_rice_parameter,
2610 const unsigned rice_parameter_search_dist,
2611 const unsigned partition_order,
2612 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2613 unsigned *bits
2614)
Josh Coalson8395d022001-07-12 21:25:22 +00002615#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002616{
Josh Coalson034dfab2001-04-27 19:10:23 +00002617 unsigned rice_parameter, partition_bits;
2618#ifndef NO_RICE_SEARCH
2619 unsigned best_partition_bits;
2620 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2621#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002622 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002623 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002624
Josh Coalson1b689822001-05-31 20:11:02 +00002625 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00002626
Josh Coalsona37ba462002-08-19 21:36:39 +00002627 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2628 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002629
Josh Coalson94e02cd2001-01-25 10:41:06 +00002630 if(partition_order == 0) {
2631 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00002632
Josh Coalson034dfab2001-04-27 19:10:23 +00002633#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00002634 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002635 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00002636 min_rice_parameter = 0;
2637 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002638 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2639 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00002640 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002641#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002642 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2643#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00002644 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002645 }
2646 }
2647 else
2648 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2649
2650 best_partition_bits = 0xffffffff;
2651 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2652#endif
2653#ifdef VARIABLE_RICE_BITS
2654#ifdef FLAC__SYMMETRIC_RICE
2655 partition_bits = (2+rice_parameter) * residual_samples;
2656#else
2657 const unsigned rice_parameter_estimate = rice_parameter-1;
2658 partition_bits = (1+rice_parameter) * residual_samples;
2659#endif
2660#else
2661 partition_bits = 0;
2662#endif
2663 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2664 for(i = 0; i < residual_samples; i++) {
2665#ifdef VARIABLE_RICE_BITS
2666#ifdef FLAC__SYMMETRIC_RICE
2667 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2668#else
2669 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2670#endif
2671#else
2672 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2673#endif
2674 }
2675#ifndef NO_RICE_SEARCH
2676 if(partition_bits < best_partition_bits) {
2677 best_rice_parameter = rice_parameter;
2678 best_partition_bits = partition_bits;
2679 }
2680 }
2681#endif
2682 parameters[0] = best_rice_parameter;
2683 bits_ += best_partition_bits;
2684 }
2685 else {
2686 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002687 unsigned partition_samples;
2688 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00002689 const unsigned partitions = 1u << partition_order;
2690 for(partition = residual_sample = 0; partition < partitions; partition++) {
2691 partition_samples = (residual_samples+predictor_order) >> partition_order;
2692 if(partition == 0) {
2693 if(partition_samples <= predictor_order)
2694 return false;
2695 else
2696 partition_samples -= predictor_order;
2697 }
2698 mean = 0;
2699 save_residual_sample = residual_sample;
2700 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002701 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00002702 residual_sample = save_residual_sample;
2703#ifdef FLAC__SYMMETRIC_RICE
2704 mean += partition_samples >> 1; /* for rounding effect */
2705 mean /= partition_samples;
2706
2707 /* calc rice_parameter = floor(log2(mean)) */
2708 rice_parameter = 0;
2709 mean>>=1;
2710 while(mean) {
2711 rice_parameter++;
2712 mean >>= 1;
2713 }
2714#else
2715 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002716 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00002717 ;
2718#endif
2719 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002720#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002721 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2722#endif
2723 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2724 }
2725
2726#ifndef NO_RICE_SEARCH
2727 if(rice_parameter_search_dist) {
2728 if(rice_parameter < rice_parameter_search_dist)
2729 min_rice_parameter = 0;
2730 else
2731 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2732 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2733 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002734#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002735 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2736#endif
2737 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2738 }
2739 }
2740 else
2741 min_rice_parameter = max_rice_parameter = rice_parameter;
2742
2743 best_partition_bits = 0xffffffff;
2744 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2745#endif
2746#ifdef VARIABLE_RICE_BITS
2747#ifdef FLAC__SYMMETRIC_RICE
2748 partition_bits = (2+rice_parameter) * partition_samples;
2749#else
2750 const unsigned rice_parameter_estimate = rice_parameter-1;
2751 partition_bits = (1+rice_parameter) * partition_samples;
2752#endif
2753#else
2754 partition_bits = 0;
2755#endif
2756 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2757 save_residual_sample = residual_sample;
2758 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2759#ifdef VARIABLE_RICE_BITS
2760#ifdef FLAC__SYMMETRIC_RICE
2761 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2762#else
2763 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2764#endif
2765#else
2766 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2767#endif
2768 }
2769#ifndef NO_RICE_SEARCH
2770 if(rice_parameter != max_rice_parameter)
2771 residual_sample = save_residual_sample;
2772 if(partition_bits < best_partition_bits) {
2773 best_rice_parameter = rice_parameter;
2774 best_partition_bits = partition_bits;
2775 }
2776 }
2777#endif
2778 parameters[partition] = best_rice_parameter;
2779 bits_ += best_partition_bits;
2780 }
2781 }
2782
2783 *bits = bits_;
2784 return true;
2785}
2786
2787#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002788FLAC__bool set_partitioned_rice_with_precompute_(
2789 const FLAC__int32 residual[],
2790 const FLAC__uint64 abs_residual_partition_sums[],
2791 const unsigned raw_bits_per_partition[],
2792 const unsigned residual_samples,
2793 const unsigned predictor_order,
2794 const unsigned suggested_rice_parameter,
2795 const unsigned rice_parameter_search_dist,
2796 const unsigned partition_order,
2797 const FLAC__bool search_for_escapes,
2798 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2799 unsigned *bits
2800)
Josh Coalson8395d022001-07-12 21:25:22 +00002801#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002802FLAC__bool set_partitioned_rice_with_precompute_(
2803 const FLAC__uint32 abs_residual[],
2804 const FLAC__uint64 abs_residual_partition_sums[],
2805 const unsigned raw_bits_per_partition[],
2806 const unsigned residual_samples,
2807 const unsigned predictor_order,
2808 const unsigned suggested_rice_parameter,
2809 const unsigned rice_parameter_search_dist,
2810 const unsigned partition_order,
2811 const FLAC__bool search_for_escapes,
2812 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2813 unsigned *bits
2814)
Josh Coalson8395d022001-07-12 21:25:22 +00002815#endif
2816{
2817 unsigned rice_parameter, partition_bits;
2818#ifndef NO_RICE_SEARCH
2819 unsigned best_partition_bits;
2820 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2821#endif
2822 unsigned flat_bits;
2823 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002824 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002825
2826 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2827
Josh Coalsona37ba462002-08-19 21:36:39 +00002828 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2829 parameters = partitioned_rice_contents->parameters;
2830 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002831
Josh Coalson8395d022001-07-12 21:25:22 +00002832 if(partition_order == 0) {
2833 unsigned i;
2834
2835#ifndef NO_RICE_SEARCH
2836 if(rice_parameter_search_dist) {
2837 if(suggested_rice_parameter < rice_parameter_search_dist)
2838 min_rice_parameter = 0;
2839 else
2840 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2841 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2842 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002843#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002844 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2845#endif
2846 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2847 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002848 }
2849 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002850 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00002851
Josh Coalson034dfab2001-04-27 19:10:23 +00002852 best_partition_bits = 0xffffffff;
2853 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2854#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002855#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002856#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00002857 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002858#else
Josh Coalson352e0f62001-03-20 22:55:50 +00002859 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00002860 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002861#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002862#else
2863 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002864#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00002865 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00002866 for(i = 0; i < residual_samples; i++) {
2867#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002868#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00002869 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002870#else
Josh Coalson2051dd42001-04-12 22:22:34 +00002871 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00002872#endif
2873#else
Josh Coalson2051dd42001-04-12 22:22:34 +00002874 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 +00002875#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00002876 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002877#ifndef NO_RICE_SEARCH
2878 if(partition_bits < best_partition_bits) {
2879 best_rice_parameter = rice_parameter;
2880 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00002881 }
2882 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002883#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002884 if(search_for_escapes) {
2885 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;
2886 if(flat_bits <= best_partition_bits) {
2887 raw_bits[0] = raw_bits_per_partition[0];
2888 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2889 best_partition_bits = flat_bits;
2890 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002891 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002892 parameters[0] = best_rice_parameter;
2893 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002894 }
2895 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00002896 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002897 unsigned partition_samples;
2898 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002899 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00002900 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002901 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00002902 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002903 if(partition_samples <= predictor_order)
2904 return false;
2905 else
2906 partition_samples -= predictor_order;
2907 }
Josh Coalson05d20792001-06-29 23:12:26 +00002908 mean = abs_residual_partition_sums[partition];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002909#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson05d20792001-06-29 23:12:26 +00002910 mean += partition_samples >> 1; /* for rounding effect */
2911 mean /= partition_samples;
2912
Josh Coalson034dfab2001-04-27 19:10:23 +00002913 /* calc rice_parameter = floor(log2(mean)) */
2914 rice_parameter = 0;
2915 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002916 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002917 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002918 mean >>= 1;
2919 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00002920#else
Josh Coalson05d20792001-06-29 23:12:26 +00002921 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002922 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00002923 ;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002924#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002925 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002926#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002927 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2928#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002929 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002930 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002931
Josh Coalson034dfab2001-04-27 19:10:23 +00002932#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00002933 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002934 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00002935 min_rice_parameter = 0;
2936 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002937 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2938 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00002939 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002940#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002941 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2942#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00002943 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002944 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002945 }
2946 else
2947 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00002948
Josh Coalson034dfab2001-04-27 19:10:23 +00002949 best_partition_bits = 0xffffffff;
2950 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2951#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002952#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002953#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00002954 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002955#else
Josh Coalson034dfab2001-04-27 19:10:23 +00002956 const unsigned rice_parameter_estimate = rice_parameter-1;
2957 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002958#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002959#else
2960 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002961#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002962 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00002963 save_residual_sample = residual_sample;
2964 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00002965#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002966#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson4dacd192001-06-06 21:11:44 +00002967 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002968#else
Josh Coalson4dacd192001-06-06 21:11:44 +00002969 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00002970#endif
2971#else
Josh Coalson4dacd192001-06-06 21:11:44 +00002972 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 +00002973#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002974 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002975#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00002976 if(rice_parameter != max_rice_parameter)
2977 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00002978 if(partition_bits < best_partition_bits) {
2979 best_rice_parameter = rice_parameter;
2980 best_partition_bits = partition_bits;
2981 }
Josh Coalson2051dd42001-04-12 22:22:34 +00002982 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002983#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002984 if(search_for_escapes) {
2985 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;
2986 if(flat_bits <= best_partition_bits) {
2987 raw_bits[partition] = raw_bits_per_partition[partition];
2988 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2989 best_partition_bits = flat_bits;
2990 }
Josh Coalson2051dd42001-04-12 22:22:34 +00002991 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002992 parameters[partition] = best_rice_parameter;
2993 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002994 }
2995 }
2996
2997 *bits = bits_;
2998 return true;
2999}
Josh Coalson859bc542001-03-27 22:22:27 +00003000
Josh Coalsonf1eff452002-07-31 07:05:33 +00003001unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00003002{
3003 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00003004 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00003005
3006 for(i = 0; i < samples && !(x&1); i++)
3007 x |= signal[i];
3008
3009 if(x == 0) {
3010 shift = 0;
3011 }
3012 else {
3013 for(shift = 0; !(x&1); shift++)
3014 x >>= 1;
3015 }
3016
3017 if(shift > 0) {
3018 for(i = 0; i < samples; i++)
3019 signal[i] >>= shift;
3020 }
3021
3022 return shift;
3023}
Josh Coalsond86e03b2002-08-03 21:56:15 +00003024
3025void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3026{
3027 unsigned channel;
3028
3029 for(channel = 0; channel < channels; channel++)
3030 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3031
3032 fifo->tail += wide_samples;
3033
3034 FLAC__ASSERT(fifo->tail <= fifo->size);
3035}
3036
3037void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3038{
3039 unsigned channel;
3040 unsigned sample, wide_sample;
3041 unsigned tail = fifo->tail;
3042
3043 sample = input_offset * channels;
3044 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3045 for(channel = 0; channel < channels; channel++)
3046 fifo->data[channel][tail] = input[sample++];
3047 tail++;
3048 }
3049 fifo->tail = tail;
3050
3051 FLAC__ASSERT(fifo->tail <= fifo->size);
3052}
3053
3054FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3055{
3056 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3057 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3058 (void)decoder;
3059
3060 if(encoder->private_->verify.needs_magic_hack) {
3061 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3062 *bytes = FLAC__STREAM_SYNC_LENGTH;
3063 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3064 encoder->private_->verify.needs_magic_hack = false;
3065 }
3066 else {
3067 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00003068 /*
3069 * If we get here, a FIFO underflow has occurred,
3070 * which means there is a bug somewhere.
3071 */
3072 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003073 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3074 }
3075 else if(encoded_bytes < *bytes)
3076 *bytes = encoded_bytes;
3077 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3078 encoder->private_->verify.output.data += *bytes;
3079 encoder->private_->verify.output.bytes -= *bytes;
3080 }
3081
3082 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3083}
3084
3085FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3086{
3087 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3088 unsigned channel;
3089 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3090 const unsigned blocksize = frame->header.blocksize;
3091 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3092
3093 for(channel = 0; channel < channels; channel++) {
3094 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3095 unsigned i, sample = 0;
3096 FLAC__int32 expect = 0, got = 0;
3097
3098 for(i = 0; i < blocksize; i++) {
3099 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3100 sample = i;
3101 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3102 got = (FLAC__int32)buffer[channel][i];
3103 break;
3104 }
3105 }
3106 FLAC__ASSERT(i < blocksize);
3107 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3108 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00003109 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003110 encoder->private_->verify.error_stats.channel = channel;
3111 encoder->private_->verify.error_stats.sample = sample;
3112 encoder->private_->verify.error_stats.expected = expect;
3113 encoder->private_->verify.error_stats.got = got;
3114 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3115 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3116 }
3117 }
3118 /* dequeue the frame from the fifo */
3119 for(channel = 0; channel < channels; channel++) {
3120 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3121 }
3122 encoder->private_->verify.input_fifo.tail -= blocksize;
3123 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3124}
3125
3126void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3127{
3128 (void)decoder, (void)metadata, (void)client_data;
3129}
3130
3131void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3132{
3133 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3134 (void)decoder, (void)status;
3135 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3136}