blob: 40b44b5d36c3b79a4c0fd6fc434f90f5b6706f05 [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++) {
Josh Coalsone4869382002-11-15 05:41:48 +0000662 if(encoder->protected_->metadata[i]->type >= FLAC__METADATA_TYPE_UNDEFINED)
663 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
664 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson66075c12002-06-01 05:39:38 +0000665 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
666 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000667 if(metadata_has_seektable) /* only one is allowed */
668 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
669 metadata_has_seektable = true;
Josh Coalson0833f342002-07-15 05:31:55 +0000670 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
Josh Coalson66075c12002-06-01 05:39:38 +0000671 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
672 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000673 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
674 if(metadata_has_vorbis_comment) /* only one is allowed */
675 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
676 metadata_has_vorbis_comment = true;
677 }
Josh Coalsone4869382002-11-15 05:41:48 +0000678 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
679 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, /*check_cd_da_subset=*/false, /*violation=*/0))
680 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
681 }
Josh Coalson66075c12002-06-01 05:39:38 +0000682 }
683
Josh Coalsonfa697a92001-08-16 20:07:29 +0000684 encoder->private_->input_capacity = 0;
685 for(i = 0; i < encoder->protected_->channels; i++) {
686 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
687 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000688 }
689 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000690 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
691 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000692 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000693 for(i = 0; i < encoder->protected_->channels; i++) {
694 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
695 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
696 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000697 }
698 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000699 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
700 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
701 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000702 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000703 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
704 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
705 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
706 encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize;
707 encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5);
708 if(encoder->private_->loose_mid_side_stereo_frames == 0)
709 encoder->private_->loose_mid_side_stereo_frames = 1;
710 encoder->private_->loose_mid_side_stereo_frame_count = 0;
711 encoder->private_->current_sample_number = 0;
712 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000713
Josh Coalsonfa697a92001-08-16 20:07:29 +0000714 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
715 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? */
716 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000717
Josh Coalsoncf30f502001-05-23 20:57:44 +0000718 /*
719 * get the CPU info and set the function pointers
720 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000721 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000722 /* first default to the non-asm routines */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000723 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
724 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
725 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000726 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 +0000727 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000728 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000729#ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000730 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalsoncf30f502001-05-23 20:57:44 +0000731#ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000732 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000733#ifdef FLAC__HAS_NASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000734 if(0 && encoder->private_->cpuinfo.data.ia32.sse) {
735 if(encoder->protected_->max_lpc_order < 4)
736 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
737 else if(encoder->protected_->max_lpc_order < 8)
738 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
739 else if(encoder->protected_->max_lpc_order < 12)
740 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000741 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000742 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000743 }
Josh Coalson395938e2001-11-15 21:53:25 +0000744 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000745 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000746 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000747 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
748 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
749 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
750 if(encoder->private_->cpuinfo.data.ia32.mmx) {
751 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
752 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 +0000753 }
754 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000755 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
756 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000757 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000758#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000759#endif
Josh Coalson021ad3b2001-07-18 00:25:52 +0000760 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000761#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000762 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000763 if(encoder->private_->use_wide_by_block) {
764 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000765 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000766
Josh Coalson8395d022001-07-12 21:25:22 +0000767 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000768 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 +0000769
Josh Coalsonf1eff452002-07-31 07:05:33 +0000770 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000771 /* the above function sets the state for us in case of an error */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000772 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000773 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000774
775 if(!FLAC__bitbuffer_init(encoder->private_->frame))
776 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000777
778 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000779 * Set up the verify stuff if necessary
780 */
781 if(encoder->protected_->verify) {
782 /*
783 * First, set up the fifo which will hold the
784 * original signal to compare against
785 */
786 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
787 for(i = 0; i < encoder->protected_->channels; i++) {
788 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
789 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
790 }
791 encoder->private_->verify.input_fifo.tail = 0;
792
793 /*
794 * Now set up a stream decoder for verification
795 */
796 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
797 if(0 == encoder->private_->verify.decoder)
798 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
799
800 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
801 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
802 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
803 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
804 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
805 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
806 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
807 }
Josh Coalson589f8c72002-08-07 23:54:55 +0000808 encoder->private_->verify.error_stats.absolute_sample = 0;
809 encoder->private_->verify.error_stats.frame_number = 0;
810 encoder->private_->verify.error_stats.channel = 0;
811 encoder->private_->verify.error_stats.sample = 0;
812 encoder->private_->verify.error_stats.expected = 0;
813 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000814
815 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000816 * write the stream header
817 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000818 if(encoder->protected_->verify)
819 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000820 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000821 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000822 if(!write_bitbuffer_(encoder, 0)) {
823 /* the above function sets the state for us in case of an error */
824 return encoder->protected_->state;
825 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000826
Josh Coalson5c491a12002-08-01 06:39:40 +0000827 /*
828 * write the STREAMINFO metadata block
829 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000830 if(encoder->protected_->verify)
831 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000832 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000833 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000834 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
835 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
836 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
837 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
838 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
839 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
840 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
841 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
842 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
843 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
844 MD5Init(&encoder->private_->md5context);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000845 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
846 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000847 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000848 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000849 if(!write_bitbuffer_(encoder, 0)) {
850 /* the above function sets the state for us in case of an error */
851 return encoder->protected_->state;
852 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000853
Josh Coalson5c491a12002-08-01 06:39:40 +0000854 /*
855 * Now that the STREAMINFO block is written, we can init this to an
856 * absurdly-high value...
857 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000858 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 +0000859 /* ... and clear this to 0 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000860 encoder->private_->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000861
Josh Coalson5c491a12002-08-01 06:39:40 +0000862 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000863 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
864 * if not, we will write an empty one (FLAC__add_metadata_block()
865 * automatically supplies the vendor string).
866 */
867 if(!metadata_has_vorbis_comment) {
868 FLAC__StreamMetadata vorbis_comment;
869 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
870 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
871 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
872 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
873 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
874 vorbis_comment.data.vorbis_comment.num_comments = 0;
875 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson7424d2f2002-11-06 07:10:38 +0000876 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
877 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000878 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
879 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
880 if(!write_bitbuffer_(encoder, 0)) {
881 /* the above function sets the state for us in case of an error */
882 return encoder->protected_->state;
883 }
884 }
885
886 /*
Josh Coalson5c491a12002-08-01 06:39:40 +0000887 * write the user's metadata blocks
888 */
889 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
890 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000891 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
892 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +0000893 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
894 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000895 if(!write_bitbuffer_(encoder, 0)) {
896 /* the above function sets the state for us in case of an error */
897 return encoder->protected_->state;
898 }
Josh Coalson5c491a12002-08-01 06:39:40 +0000899 }
900
Josh Coalsond86e03b2002-08-03 21:56:15 +0000901 if(encoder->protected_->verify)
902 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
903
Josh Coalsonfa697a92001-08-16 20:07:29 +0000904 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000905}
906
Josh Coalson6afed9f2002-10-16 22:29:47 +0000907FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000908{
Josh Coalsonf1eff452002-07-31 07:05:33 +0000909 FLAC__ASSERT(0 != encoder);
Josh Coalson2b245f22002-08-07 17:10:50 +0000910
Josh Coalsonfa697a92001-08-16 20:07:29 +0000911 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000912 return;
Josh Coalson2b245f22002-08-07 17:10:50 +0000913
Josh Coalson3262b0d2002-08-14 20:58:42 +0000914 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +0000915 if(encoder->private_->current_sample_number != 0) {
916 encoder->protected_->blocksize = encoder->private_->current_sample_number;
917 process_frame_(encoder, true); /* true => is last frame */
918 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000919 }
Josh Coalson2b245f22002-08-07 17:10:50 +0000920
Josh Coalsonfa697a92001-08-16 20:07:29 +0000921 MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +0000922
Josh Coalson3262b0d2002-08-14 20:58:42 +0000923 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +0000924 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
925 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000926
Josh Coalsond86e03b2002-08-03 21:56:15 +0000927 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
928 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
929
Josh Coalsonf1eff452002-07-31 07:05:33 +0000930 free_(encoder);
931 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000932
Josh Coalsonfa697a92001-08-16 20:07:29 +0000933 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000934}
935
Josh Coalson6afed9f2002-10-16 22:29:47 +0000936FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000937{
938 FLAC__ASSERT(0 != encoder);
939 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
940 return false;
941 encoder->protected_->verify = value;
942 return true;
943}
944
Josh Coalson6afed9f2002-10-16 22:29:47 +0000945FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000946{
Josh Coalson92031602002-07-24 06:02:11 +0000947 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000948 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000949 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000950 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000951 return true;
952}
953
Josh Coalson6afed9f2002-10-16 22:29:47 +0000954FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000955{
Josh Coalson92031602002-07-24 06:02:11 +0000956 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000957 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000958 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000959 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000960 return true;
961}
962
Josh Coalson6afed9f2002-10-16 22:29:47 +0000963FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000964{
Josh Coalson92031602002-07-24 06:02:11 +0000965 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000966 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000967 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000968 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000969 return true;
970}
971
Josh Coalson6afed9f2002-10-16 22:29:47 +0000972FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000973{
Josh Coalson92031602002-07-24 06:02:11 +0000974 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000975 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000976 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000977 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000978 return true;
979}
980
Josh Coalson6afed9f2002-10-16 22:29:47 +0000981FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000982{
Josh Coalson92031602002-07-24 06:02:11 +0000983 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000984 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000985 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000986 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000987 return true;
988}
989
Josh Coalson6afed9f2002-10-16 22:29:47 +0000990FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000991{
Josh Coalson92031602002-07-24 06:02:11 +0000992 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000993 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000994 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000995 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000996 return true;
997}
998
Josh Coalson6afed9f2002-10-16 22:29:47 +0000999FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001000{
Josh Coalson92031602002-07-24 06:02:11 +00001001 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001002 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001003 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001004 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001005 return true;
1006}
1007
Josh Coalson6afed9f2002-10-16 22:29:47 +00001008FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001009{
Josh Coalson92031602002-07-24 06:02:11 +00001010 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001011 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001012 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001013 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001014 return true;
1015}
1016
Josh Coalson6afed9f2002-10-16 22:29:47 +00001017FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001018{
Josh Coalson92031602002-07-24 06:02:11 +00001019 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001020 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001021 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001022 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001023 return true;
1024}
1025
Josh Coalson6afed9f2002-10-16 22:29:47 +00001026FLAC_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 +00001027{
Josh Coalson92031602002-07-24 06:02:11 +00001028 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001029 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001030 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001031 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001032 return true;
1033}
1034
Josh Coalson6afed9f2002-10-16 22:29:47 +00001035FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001036{
Josh Coalson92031602002-07-24 06:02:11 +00001037 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001038 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001039 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001040#if 0
1041 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001042 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001043#else
1044 (void)value;
1045#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001046 return true;
1047}
1048
Josh Coalson6afed9f2002-10-16 22:29:47 +00001049FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001050{
Josh Coalson92031602002-07-24 06:02:11 +00001051 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001052 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001053 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001054 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001055 return true;
1056}
1057
Josh Coalson6afed9f2002-10-16 22:29:47 +00001058FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001059{
Josh Coalson92031602002-07-24 06:02:11 +00001060 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001061 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001062 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001063 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001064 return true;
1065}
1066
Josh Coalson6afed9f2002-10-16 22:29:47 +00001067FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001068{
Josh Coalson92031602002-07-24 06:02:11 +00001069 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001070 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001071 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001072 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001073 return true;
1074}
1075
Josh Coalson6afed9f2002-10-16 22:29:47 +00001076FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001077{
Josh Coalson92031602002-07-24 06:02:11 +00001078 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001079 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001080 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001081#if 0
1082 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001083 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001084#else
1085 (void)value;
1086#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001087 return true;
1088}
1089
Josh Coalson6afed9f2002-10-16 22:29:47 +00001090FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001091{
Josh Coalson92031602002-07-24 06:02:11 +00001092 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001093 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001094 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001095 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001096 return true;
1097}
1098
Josh Coalson6afed9f2002-10-16 22:29:47 +00001099FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001100{
Josh Coalson92031602002-07-24 06:02:11 +00001101 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001102 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001103 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001104 encoder->protected_->metadata = metadata;
1105 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson00e53872001-06-16 07:32:25 +00001106 return true;
1107}
1108
Josh Coalson6afed9f2002-10-16 22:29:47 +00001109FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001110{
Josh Coalson92031602002-07-24 06:02:11 +00001111 FLAC__ASSERT(0 != encoder);
1112 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001113 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001114 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001115 encoder->private_->write_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001116 return true;
1117}
1118
Josh Coalson6afed9f2002-10-16 22:29:47 +00001119FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001120{
Josh Coalson92031602002-07-24 06:02:11 +00001121 FLAC__ASSERT(0 != encoder);
1122 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001123 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001124 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001125 encoder->private_->metadata_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001126 return true;
1127}
1128
Josh Coalson6afed9f2002-10-16 22:29:47 +00001129FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +00001130{
Josh Coalson92031602002-07-24 06:02:11 +00001131 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001132 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001133 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001134 encoder->private_->client_data = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001135 return true;
1136}
1137
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001138/*
1139 * These three functions are not static, but not publically exposed in
1140 * include/FLAC/ either. They are used by the test suite.
1141 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001142FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001143{
1144 FLAC__ASSERT(0 != encoder);
1145 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1146 return false;
1147 encoder->private_->disable_constant_subframes = value;
1148 return true;
1149}
1150
Josh Coalson6afed9f2002-10-16 22:29:47 +00001151FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001152{
1153 FLAC__ASSERT(0 != encoder);
1154 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1155 return false;
1156 encoder->private_->disable_fixed_subframes = value;
1157 return true;
1158}
1159
Josh Coalson6afed9f2002-10-16 22:29:47 +00001160FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001161{
1162 FLAC__ASSERT(0 != encoder);
1163 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1164 return false;
1165 encoder->private_->disable_verbatim_subframes = value;
1166 return true;
1167}
1168
Josh Coalson6afed9f2002-10-16 22:29:47 +00001169FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001170{
Josh Coalson92031602002-07-24 06:02:11 +00001171 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001172 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001173}
1174
Josh Coalson6afed9f2002-10-16 22:29:47 +00001175FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001176{
1177 FLAC__ASSERT(0 != encoder);
1178 if(encoder->protected_->verify)
1179 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1180 else
1181 return FLAC__STREAM_DECODER_UNINITIALIZED;
1182}
1183
Josh Coalson02954222002-11-08 06:16:31 +00001184FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1185{
1186 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1187 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1188 else
1189 return FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(encoder->private_->verify.decoder)];
1190}
1191
Josh Coalson6afed9f2002-10-16 22:29:47 +00001192FLAC_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 +00001193{
1194 FLAC__ASSERT(0 != encoder);
1195 if(0 != absolute_sample)
1196 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1197 if(0 != frame_number)
1198 *frame_number = encoder->private_->verify.error_stats.frame_number;
1199 if(0 != channel)
1200 *channel = encoder->private_->verify.error_stats.channel;
1201 if(0 != sample)
1202 *sample = encoder->private_->verify.error_stats.sample;
1203 if(0 != expected)
1204 *expected = encoder->private_->verify.error_stats.expected;
1205 if(0 != got)
1206 *got = encoder->private_->verify.error_stats.got;
1207}
1208
Josh Coalson6afed9f2002-10-16 22:29:47 +00001209FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001210{
1211 FLAC__ASSERT(0 != encoder);
1212 return encoder->protected_->verify;
1213}
1214
Josh Coalson6afed9f2002-10-16 22:29:47 +00001215FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(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_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001219}
1220
Josh Coalson6afed9f2002-10-16 22:29:47 +00001221FLAC_API FLAC__bool FLAC__stream_encoder_get_do_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_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001225}
1226
Josh Coalson6afed9f2002-10-16 22:29:47 +00001227FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(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_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001231}
1232
Josh Coalson6afed9f2002-10-16 22:29:47 +00001233FLAC_API unsigned FLAC__stream_encoder_get_channels(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_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001237}
1238
Josh Coalson6afed9f2002-10-16 22:29:47 +00001239FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(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_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001243}
1244
Josh Coalson6afed9f2002-10-16 22:29:47 +00001245FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(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_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001249}
1250
Josh Coalson6afed9f2002-10-16 22:29:47 +00001251FLAC_API unsigned FLAC__stream_encoder_get_blocksize(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_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001255}
1256
Josh Coalson6afed9f2002-10-16 22:29:47 +00001257FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(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_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001261}
1262
Josh Coalson6afed9f2002-10-16 22:29:47 +00001263FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(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_->qlp_coeff_precision;
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_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +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_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001273}
1274
Josh Coalson6afed9f2002-10-16 22:29:47 +00001275FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +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_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001279}
1280
Josh Coalson6afed9f2002-10-16 22:29:47 +00001281FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(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_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001285}
1286
Josh Coalson6afed9f2002-10-16 22:29:47 +00001287FLAC_API unsigned FLAC__stream_encoder_get_min_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_->min_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_max_residual_partition_order(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_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001297}
1298
Josh Coalson6afed9f2002-10-16 22:29:47 +00001299FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001300{
Josh Coalson92031602002-07-24 06:02:11 +00001301 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001302 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001303}
1304
Josh Coalson6afed9f2002-10-16 22:29:47 +00001305FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001306{
1307 FLAC__ASSERT(0 != encoder);
1308 return encoder->protected_->total_samples_estimate;
1309}
1310
Josh Coalson6afed9f2002-10-16 22:29:47 +00001311FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001312{
1313 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001314 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001315 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001316
Josh Coalsonf1eff452002-07-31 07:05:33 +00001317 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001318 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001319
1320 j = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001321 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001322 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001323 if(encoder->protected_->verify)
1324 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1325
Josh Coalsonfa697a92001-08-16 20:07:29 +00001326 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001327 x = mid = side = buffer[0][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001328 encoder->private_->integer_signal[0][i] = x;
1329 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001330 x = buffer[1][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001331 encoder->private_->integer_signal[1][i] = x;
1332 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001333 mid += x;
1334 side -= x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001335 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001336 encoder->private_->integer_signal_mid_side[1][i] = side;
1337 encoder->private_->integer_signal_mid_side[0][i] = mid;
1338 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1339 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1340 encoder->private_->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001341 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001342 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001343 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001344 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001345 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001346 } while(j < samples);
1347 }
1348 else {
1349 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001350 if(encoder->protected_->verify)
1351 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1352
Josh Coalsonfa697a92001-08-16 20:07:29 +00001353 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001354 for(channel = 0; channel < channels; channel++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001355 x = buffer[channel][j];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001356 encoder->private_->integer_signal[channel][i] = x;
1357 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001358 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001359 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001360 }
1361 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001362 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001363 return false;
1364 }
1365 } while(j < samples);
1366 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001367
1368 return true;
1369}
1370
Josh Coalson6afed9f2002-10-16 22:29:47 +00001371FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001372{
1373 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001374 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001375 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001376
Josh Coalsonf1eff452002-07-31 07:05:33 +00001377 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001378 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001379
1380 j = k = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001381 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001382 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001383 if(encoder->protected_->verify)
1384 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1385
Josh Coalsonfa697a92001-08-16 20:07:29 +00001386 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001387 x = mid = side = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001388 encoder->private_->integer_signal[0][i] = x;
1389 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson57ba6f42002-06-07 05:27:37 +00001390 x = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001391 encoder->private_->integer_signal[1][i] = x;
1392 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001393 mid += x;
1394 side -= x;
1395 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001396 encoder->private_->integer_signal_mid_side[1][i] = side;
1397 encoder->private_->integer_signal_mid_side[0][i] = mid;
1398 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1399 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1400 encoder->private_->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001401 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001402 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001403 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001404 return false;
1405 }
1406 } while(j < samples);
1407 }
1408 else {
1409 do {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001410 if(encoder->protected_->verify)
1411 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1412
Josh Coalsonfa697a92001-08-16 20:07:29 +00001413 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +00001414 for(channel = 0; channel < channels; channel++) {
Josh Coalson57ba6f42002-06-07 05:27:37 +00001415 x = buffer[k++];
Josh Coalsonfa697a92001-08-16 20:07:29 +00001416 encoder->private_->integer_signal[channel][i] = x;
1417 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +00001418 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001419 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001420 }
1421 if(i == blocksize) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001422 if(!process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +00001423 return false;
1424 }
1425 } while(j < samples);
1426 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001427
1428 return true;
1429}
1430
Josh Coalsonf1eff452002-07-31 07:05:33 +00001431/***********************************************************************
1432 *
1433 * Private class methods
1434 *
1435 ***********************************************************************/
1436
1437void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00001438{
1439 FLAC__ASSERT(0 != encoder);
1440
Josh Coalsond86e03b2002-08-03 21:56:15 +00001441 encoder->protected_->verify = false;
Josh Coalson92031602002-07-24 06:02:11 +00001442 encoder->protected_->streamable_subset = true;
1443 encoder->protected_->do_mid_side_stereo = false;
1444 encoder->protected_->loose_mid_side_stereo = false;
1445 encoder->protected_->channels = 2;
1446 encoder->protected_->bits_per_sample = 16;
1447 encoder->protected_->sample_rate = 44100;
1448 encoder->protected_->blocksize = 1152;
1449 encoder->protected_->max_lpc_order = 0;
1450 encoder->protected_->qlp_coeff_precision = 0;
1451 encoder->protected_->do_qlp_coeff_prec_search = false;
1452 encoder->protected_->do_exhaustive_model_search = false;
1453 encoder->protected_->do_escape_coding = false;
1454 encoder->protected_->min_residual_partition_order = 0;
1455 encoder->protected_->max_residual_partition_order = 0;
1456 encoder->protected_->rice_parameter_search_dist = 0;
1457 encoder->protected_->total_samples_estimate = 0;
1458 encoder->protected_->metadata = 0;
1459 encoder->protected_->num_metadata_blocks = 0;
1460
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001461 encoder->private_->disable_constant_subframes = false;
1462 encoder->private_->disable_fixed_subframes = false;
1463 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson92031602002-07-24 06:02:11 +00001464 encoder->private_->write_callback = 0;
1465 encoder->private_->metadata_callback = 0;
1466 encoder->private_->client_data = 0;
1467}
1468
Josh Coalsonf1eff452002-07-31 07:05:33 +00001469void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00001470{
1471 unsigned i, channel;
1472
Josh Coalsonf1eff452002-07-31 07:05:33 +00001473 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00001474 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001475 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001476 free(encoder->private_->integer_signal_unaligned[i]);
1477 encoder->private_->integer_signal_unaligned[i] = 0;
1478 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001479 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001480 free(encoder->private_->real_signal_unaligned[i]);
1481 encoder->private_->real_signal_unaligned[i] = 0;
1482 }
1483 }
1484 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001485 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001486 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1487 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1488 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001489 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001490 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1491 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1492 }
1493 }
1494 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1495 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001496 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001497 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1498 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1499 }
1500 }
1501 }
1502 for(channel = 0; channel < 2; channel++) {
1503 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001504 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001505 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1506 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1507 }
1508 }
1509 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001510 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001511 free(encoder->private_->abs_residual_unaligned);
1512 encoder->private_->abs_residual_unaligned = 0;
1513 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001514 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001515 free(encoder->private_->abs_residual_partition_sums_unaligned);
1516 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1517 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001518 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001519 free(encoder->private_->raw_bits_per_partition_unaligned);
1520 encoder->private_->raw_bits_per_partition_unaligned = 0;
1521 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001522 if(encoder->protected_->verify) {
1523 for(i = 0; i < encoder->protected_->channels; i++) {
1524 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1525 free(encoder->private_->verify.input_fifo.data[i]);
1526 encoder->private_->verify.input_fifo.data[i] = 0;
1527 }
1528 }
1529 }
Josh Coalson639aeb02002-07-25 05:38:23 +00001530 FLAC__bitbuffer_free(encoder->private_->frame);
1531}
1532
Josh Coalsonf1eff452002-07-31 07:05:33 +00001533FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001534{
Josh Coalson77e3f312001-06-23 03:03:24 +00001535 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00001536 unsigned i, channel;
1537
1538 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001539 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1540 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00001541
1542 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001543 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00001544 return true;
1545
1546 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00001547
Josh Coalsonc9c0d132002-10-04 05:29:05 +00001548 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1549 * requires that the input arrays (in our case the integer signals)
1550 * have a buffer of up to 3 zeroes in front (at negative indices) for
1551 * alignment purposes; we use 4 to keep the data well-aligned.
1552 */
Josh Coalson8395d022001-07-12 21:25:22 +00001553
Josh Coalsonfa697a92001-08-16 20:07:29 +00001554 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1555 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1556 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1557 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1558 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001559 }
1560 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001561 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]);
1562 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]);
1563 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1564 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001565 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001566 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001567 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001568 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 +00001569 }
1570 }
1571 for(channel = 0; ok && channel < 2; channel++) {
1572 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001573 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 +00001574 }
1575 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001576 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1577 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 */
1578 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1579 if(encoder->protected_->do_escape_coding)
1580 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 +00001581
1582 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001583 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00001584 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001585 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00001586
1587 return ok;
1588}
1589
Josh Coalsond86e03b2002-08-03 21:56:15 +00001590FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00001591{
1592 const FLAC__byte *buffer;
1593 unsigned bytes;
1594
1595 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1596
1597 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1598
Josh Coalsond86e03b2002-08-03 21:56:15 +00001599 if(encoder->protected_->verify) {
1600 encoder->private_->verify.output.data = buffer;
1601 encoder->private_->verify.output.bytes = bytes;
1602 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1603 encoder->private_->verify.needs_magic_hack = true;
1604 }
1605 else {
1606 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1607 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1608 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1609 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1610 return false;
1611 }
1612 }
1613 }
1614
1615 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1616 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalson5c491a12002-08-01 06:39:40 +00001617 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001618 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001619
1620 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1621
Josh Coalsond86e03b2002-08-03 21:56:15 +00001622 if(samples > 0) {
1623 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1624 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1625 }
1626
Josh Coalson5c491a12002-08-01 06:39:40 +00001627 return true;
1628}
1629
Josh Coalsonf1eff452002-07-31 07:05:33 +00001630FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00001631{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001632 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001633
1634 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00001635 * Accumulate raw signal to the MD5 signature
1636 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00001637 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 +00001638 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00001639 return false;
1640 }
1641
1642 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001643 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001644 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00001645 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001646 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001647 return false;
1648 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001649
1650 /*
1651 * Zero-pad the frame to a byte_boundary
1652 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001653 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001654 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001655 return false;
1656 }
1657
1658 /*
Josh Coalson215af572001-03-27 01:15:58 +00001659 * CRC-16 the whole thing
1660 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001661 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1662 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 +00001663
1664 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001665 * Write it
1666 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001667 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1668 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001669 return false;
1670 }
1671
1672 /*
1673 * Get ready for the next frame
1674 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001675 encoder->private_->current_sample_number = 0;
1676 encoder->private_->current_frame_number++;
1677 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001678
1679 return true;
1680}
1681
Josh Coalsonf1eff452002-07-31 07:05:33 +00001682FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001683{
1684 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001685 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00001686 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001687
1688 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00001689 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00001690 */
1691 if(is_last_frame) {
1692 max_partition_order = 0;
1693 }
1694 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001695 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1696 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001697 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001698 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001699
Josh Coalsonfa697a92001-08-16 20:07:29 +00001700 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 +00001701
Josh Coalson94e02cd2001-01-25 10:41:06 +00001702 /*
1703 * Setup the frame
1704 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001705 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001706 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001707 return false;
1708 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001709 frame_header.blocksize = encoder->protected_->blocksize;
1710 frame_header.sample_rate = encoder->protected_->sample_rate;
1711 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001712 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001713 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001714 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001715 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001716
1717 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001718 * Figure out what channel assignments to try
1719 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001720 if(encoder->protected_->do_mid_side_stereo) {
1721 if(encoder->protected_->loose_mid_side_stereo) {
1722 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001723 do_independent = true;
1724 do_mid_side = true;
1725 }
1726 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001727 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001728 do_mid_side = !do_independent;
1729 }
1730 }
1731 else {
1732 do_independent = true;
1733 do_mid_side = true;
1734 }
1735 }
1736 else {
1737 do_independent = true;
1738 do_mid_side = false;
1739 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001740
Josh Coalson1b689822001-05-31 20:11:02 +00001741 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001742
1743 /*
Josh Coalson82b73242001-03-28 22:17:05 +00001744 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00001745 */
1746 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001747 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001748 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001749 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1750 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00001751 }
Josh Coalson859bc542001-03-27 22:22:27 +00001752 }
1753 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001754 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00001755 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001756 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001757 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1758 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00001759 }
Josh Coalson859bc542001-03-27 22:22:27 +00001760 }
1761
1762 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001763 * First do a normal encoding pass of each independent channel
1764 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001765 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001766 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00001767 if(!
1768 process_subframe_(
1769 encoder,
1770 min_partition_order,
1771 max_partition_order,
1772 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001773 &frame_header,
1774 encoder->private_->subframe_bps[channel],
1775 encoder->private_->integer_signal[channel],
1776 encoder->private_->real_signal[channel],
1777 encoder->private_->subframe_workspace_ptr[channel],
1778 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1779 encoder->private_->residual_workspace[channel],
1780 encoder->private_->best_subframe+channel,
1781 encoder->private_->best_subframe_bits+channel
1782 )
1783 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001784 return false;
1785 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001786 }
1787
1788 /*
1789 * Now do mid and side channels if requested
1790 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001791 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001792 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001793
1794 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00001795 if(!
1796 process_subframe_(
1797 encoder,
1798 min_partition_order,
1799 max_partition_order,
1800 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001801 &frame_header,
1802 encoder->private_->subframe_bps_mid_side[channel],
1803 encoder->private_->integer_signal_mid_side[channel],
1804 encoder->private_->real_signal_mid_side[channel],
1805 encoder->private_->subframe_workspace_ptr_mid_side[channel],
1806 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
1807 encoder->private_->residual_workspace_mid_side[channel],
1808 encoder->private_->best_subframe_mid_side+channel,
1809 encoder->private_->best_subframe_bits_mid_side+channel
1810 )
1811 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00001812 return false;
1813 }
1814 }
1815
1816 /*
1817 * Compose the frame bitbuffer
1818 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001819 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00001820 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1821 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001822 FLAC__ChannelAssignment channel_assignment;
1823
Josh Coalsonfa697a92001-08-16 20:07:29 +00001824 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001825
Josh Coalsonfa697a92001-08-16 20:07:29 +00001826 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
1827 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 +00001828 }
1829 else {
1830 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1831 unsigned min_bits;
1832 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001833
Josh Coalson1b689822001-05-31 20:11:02 +00001834 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001835
1836 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001837 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
1838 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
1839 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
1840 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 +00001841
Josh Coalson7424d2f2002-11-06 07:10:38 +00001842 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 +00001843 if(bits[ca] < min_bits) {
1844 min_bits = bits[ca];
1845 channel_assignment = ca;
1846 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001847 }
1848 }
1849
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001850 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001851
Josh Coalsonaec256b2002-03-12 16:19:54 +00001852 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001853 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001854 return false;
1855 }
1856
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001857 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001858 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001859 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1860 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001861 break;
1862 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001863 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
1864 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001865 break;
1866 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001867 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
1868 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001869 break;
1870 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001871 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
1872 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001873 break;
1874 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001875 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001876 }
Josh Coalson82b73242001-03-28 22:17:05 +00001877
1878 switch(channel_assignment) {
1879 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001880 left_bps = encoder->private_->subframe_bps [0];
1881 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001882 break;
1883 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001884 left_bps = encoder->private_->subframe_bps [0];
1885 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001886 break;
1887 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001888 left_bps = encoder->private_->subframe_bps_mid_side[1];
1889 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001890 break;
1891 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001892 left_bps = encoder->private_->subframe_bps_mid_side[0];
1893 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001894 break;
1895 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001896 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00001897 }
1898
1899 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00001900 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001901 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00001902 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001903 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001904 }
1905 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001906 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001907 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001908 return false;
1909 }
1910
Josh Coalsonfa697a92001-08-16 20:07:29 +00001911 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001912 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 +00001913 /* the above function sets the state for us in case of an error */
1914 return false;
1915 }
1916 }
1917 }
1918
Josh Coalsonfa697a92001-08-16 20:07:29 +00001919 if(encoder->protected_->loose_mid_side_stereo) {
1920 encoder->private_->loose_mid_side_stereo_frame_count++;
1921 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
1922 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001923 }
1924
Josh Coalsonfa697a92001-08-16 20:07:29 +00001925 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001926
Josh Coalson94e02cd2001-01-25 10:41:06 +00001927 return true;
1928}
1929
Josh Coalson6fe72f72002-08-20 04:01:59 +00001930FLAC__bool process_subframe_(
1931 FLAC__StreamEncoder *encoder,
1932 unsigned min_partition_order,
1933 unsigned max_partition_order,
1934 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001935 const FLAC__FrameHeader *frame_header,
1936 unsigned subframe_bps,
1937 const FLAC__int32 integer_signal[],
1938 const FLAC__real real_signal[],
1939 FLAC__Subframe *subframe[2],
1940 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
1941 FLAC__int32 *residual[2],
1942 unsigned *best_subframe,
1943 unsigned *best_bits
1944)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001945{
Josh Coalson77e3f312001-06-23 03:03:24 +00001946 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1947 FLAC__real lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001948 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 +00001949 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001950 unsigned min_lpc_order, max_lpc_order, lpc_order;
1951 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1952 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1953 unsigned rice_parameter;
1954 unsigned _candidate_bits, _best_bits;
1955 unsigned _best_subframe;
1956
1957 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1958 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001959 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
1960 _best_bits = UINT_MAX;
1961 else
1962 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001963
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001964 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1965 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001966 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 +00001967 /* check for constant subframe */
1968 if(!encoder->private_->disable_constant_subframes && fixed_residual_bits_per_sample[1] == 0.0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001969 /* 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 +00001970 unsigned i;
1971 signal_is_constant = true;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001972 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1973 if(integer_signal[0] != integer_signal[i]) {
1974 signal_is_constant = false;
1975 break;
1976 }
1977 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001978 }
1979 if(signal_is_constant) {
1980 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
1981 if(_candidate_bits < _best_bits) {
1982 _best_subframe = !_best_subframe;
1983 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001984 }
1985 }
1986 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001987 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
1988 /* encode fixed */
1989 if(encoder->protected_->do_exhaustive_model_search) {
1990 min_fixed_order = 0;
1991 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00001992 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001993 else {
1994 min_fixed_order = max_fixed_order = guess_fixed_order;
1995 }
1996 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
1997 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
1998 continue; /* don't even try */
1999 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 */
2000#ifndef FLAC__SYMMETRIC_RICE
2001 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2002#endif
2003 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2004#ifdef DEBUG_VERBOSE
2005 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2006#endif
2007 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2008 }
2009 _candidate_bits =
2010 evaluate_fixed_subframe_(
2011 encoder,
2012 integer_signal,
2013 residual[!_best_subframe],
2014 encoder->private_->abs_residual,
2015 encoder->private_->abs_residual_partition_sums,
2016 encoder->private_->raw_bits_per_partition,
2017 frame_header->blocksize,
2018 subframe_bps,
2019 fixed_order,
2020 rice_parameter,
2021 min_partition_order,
2022 max_partition_order,
2023 precompute_partition_sums,
2024 encoder->protected_->do_escape_coding,
2025 encoder->protected_->rice_parameter_search_dist,
2026 subframe[!_best_subframe],
2027 partitioned_rice_contents[!_best_subframe]
2028 );
2029 if(_candidate_bits < _best_bits) {
2030 _best_subframe = !_best_subframe;
2031 _best_bits = _candidate_bits;
2032 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002033 }
2034 }
2035
2036 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002037 if(encoder->protected_->max_lpc_order > 0) {
2038 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002039 max_lpc_order = frame_header->blocksize-1;
2040 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002041 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002042 if(max_lpc_order > 0) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002043 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002044 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2045 if(autoc[0] != 0.0) {
Josh Coalson8084b052001-11-01 00:27:29 +00002046 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002047 if(encoder->protected_->do_exhaustive_model_search) {
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002048 min_lpc_order = 1;
2049 }
2050 else {
Josh Coalson82b73242001-03-28 22:17:05 +00002051 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 +00002052 min_lpc_order = max_lpc_order = guess_lpc_order;
2053 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002054 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2055 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 +00002056 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002057 continue; /* don't even try */
2058 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 +00002059#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002060 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00002061#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002062 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002063#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002064 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2065#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002066 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002067 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002068 if(encoder->protected_->do_qlp_coeff_prec_search) {
2069 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2070 /* ensure a 32-bit datapath throughout for 16bps or less */
2071 if(subframe_bps <= 16)
2072 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2073 else
2074 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2075 }
2076 else {
2077 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2078 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002079 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 +00002080 _candidate_bits =
2081 evaluate_lpc_subframe_(
2082 encoder,
2083 integer_signal,
2084 residual[!_best_subframe],
2085 encoder->private_->abs_residual,
2086 encoder->private_->abs_residual_partition_sums,
2087 encoder->private_->raw_bits_per_partition,
2088 encoder->private_->lp_coeff[lpc_order-1],
2089 frame_header->blocksize,
2090 subframe_bps,
2091 lpc_order,
2092 qlp_coeff_precision,
2093 rice_parameter,
2094 min_partition_order,
2095 max_partition_order,
2096 precompute_partition_sums,
2097 encoder->protected_->do_escape_coding,
2098 encoder->protected_->rice_parameter_search_dist,
2099 subframe[!_best_subframe],
2100 partitioned_rice_contents[!_best_subframe]
2101 );
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002102 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2103 if(_candidate_bits < _best_bits) {
2104 _best_subframe = !_best_subframe;
2105 _best_bits = _candidate_bits;
2106 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002107 }
2108 }
2109 }
2110 }
2111 }
2112 }
2113 }
2114 }
2115
Josh Coalson72695802002-10-11 06:25:16 +00002116 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2117 if(_best_bits == UINT_MAX) {
2118 FLAC__ASSERT(_best_subframe == 0);
2119 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2120 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002121
Josh Coalson94e02cd2001-01-25 10:41:06 +00002122 *best_subframe = _best_subframe;
2123 *best_bits = _best_bits;
2124
2125 return true;
2126}
2127
Josh Coalson6fe72f72002-08-20 04:01:59 +00002128FLAC__bool add_subframe_(
2129 FLAC__StreamEncoder *encoder,
2130 const FLAC__FrameHeader *frame_header,
2131 unsigned subframe_bps,
2132 const FLAC__Subframe *subframe,
2133 FLAC__BitBuffer *frame
2134)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002135{
2136 switch(subframe->type) {
2137 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002138//@@@@fprintf(stderr,"@@@@ add CONSTANT, bps=%u\n",subframe_bps);
Josh Coalson82b73242001-03-28 22:17:05 +00002139 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002140 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002141 return false;
2142 }
2143 break;
2144 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002145//@@@@fprintf(stderr,"@@@@ add FIXED, bps=%u, order=%u\n",subframe_bps,subframe->data.fixed.order);
Josh Coalson82b73242001-03-28 22:17:05 +00002146 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 +00002147 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002148 return false;
2149 }
2150 break;
2151 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002152//@@@@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 +00002153 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 +00002154 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002155 return false;
2156 }
2157 break;
2158 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002159//@@@@fprintf(stderr,"@@@@ add VERBATIM, bps=%u\n",subframe_bps);
Josh Coalson82b73242001-03-28 22:17:05 +00002160 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002161 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002162 return false;
2163 }
2164 break;
2165 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002166 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002167 }
2168
2169 return true;
2170}
2171
Josh Coalson6fe72f72002-08-20 04:01:59 +00002172unsigned evaluate_constant_subframe_(
2173 const FLAC__int32 signal,
2174 unsigned subframe_bps,
2175 FLAC__Subframe *subframe
2176)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002177{
2178 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2179 subframe->data.constant.value = signal;
2180
Josh Coalson82b73242001-03-28 22:17:05 +00002181 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 +00002182}
2183
Josh Coalson6fe72f72002-08-20 04:01:59 +00002184unsigned evaluate_fixed_subframe_(
2185 FLAC__StreamEncoder *encoder,
2186 const FLAC__int32 signal[],
2187 FLAC__int32 residual[],
2188 FLAC__uint32 abs_residual[],
2189 FLAC__uint64 abs_residual_partition_sums[],
2190 unsigned raw_bits_per_partition[],
2191 unsigned blocksize,
2192 unsigned subframe_bps,
2193 unsigned order,
2194 unsigned rice_parameter,
2195 unsigned min_partition_order,
2196 unsigned max_partition_order,
2197 FLAC__bool precompute_partition_sums,
2198 FLAC__bool do_escape_coding,
2199 unsigned rice_parameter_search_dist,
2200 FLAC__Subframe *subframe,
2201 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2202)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002203{
2204 unsigned i, residual_bits;
2205 const unsigned residual_samples = blocksize - order;
2206
2207 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2208
2209 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2210
2211 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002212 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002213 subframe->data.fixed.residual = residual;
2214
Josh Coalson6fe72f72002-08-20 04:01:59 +00002215 residual_bits =
2216 find_best_partition_order_(
2217 encoder->private_,
2218 residual,
2219 abs_residual,
2220 abs_residual_partition_sums,
2221 raw_bits_per_partition,
2222 residual_samples,
2223 order,
2224 rice_parameter,
2225 min_partition_order,
2226 max_partition_order,
2227 precompute_partition_sums,
2228 do_escape_coding,
2229 rice_parameter_search_dist,
2230 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2231 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002232
2233 subframe->data.fixed.order = order;
2234 for(i = 0; i < order; i++)
2235 subframe->data.fixed.warmup[i] = signal[i];
2236
Josh Coalson82b73242001-03-28 22:17:05 +00002237 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 +00002238}
2239
Josh Coalson6fe72f72002-08-20 04:01:59 +00002240unsigned evaluate_lpc_subframe_(
2241 FLAC__StreamEncoder *encoder,
2242 const FLAC__int32 signal[],
2243 FLAC__int32 residual[],
2244 FLAC__uint32 abs_residual[],
2245 FLAC__uint64 abs_residual_partition_sums[],
2246 unsigned raw_bits_per_partition[],
2247 const FLAC__real lp_coeff[],
2248 unsigned blocksize,
2249 unsigned subframe_bps,
2250 unsigned order,
2251 unsigned qlp_coeff_precision,
2252 unsigned rice_parameter,
2253 unsigned min_partition_order,
2254 unsigned max_partition_order,
2255 FLAC__bool precompute_partition_sums,
2256 FLAC__bool do_escape_coding,
2257 unsigned rice_parameter_search_dist,
2258 FLAC__Subframe *subframe,
2259 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2260)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002261{
Josh Coalson77e3f312001-06-23 03:03:24 +00002262 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002263 unsigned i, residual_bits;
2264 int quantization, ret;
2265 const unsigned residual_samples = blocksize - order;
2266
Josh Coalson20ac2c12002-08-30 05:47:14 +00002267 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2268 if(subframe_bps <= 16) {
2269 FLAC__ASSERT(order > 0);
2270 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2271 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2272 }
2273
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002274 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002275 if(ret != 0)
2276 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2277
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00002278 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2279 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2280 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2281 else
2282 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 +00002283 else
2284 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 +00002285
2286 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2287
2288 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002289 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002290 subframe->data.lpc.residual = residual;
2291
Josh Coalson6fe72f72002-08-20 04:01:59 +00002292 residual_bits =
2293 find_best_partition_order_(
2294 encoder->private_,
2295 residual,
2296 abs_residual,
2297 abs_residual_partition_sums,
2298 raw_bits_per_partition,
2299 residual_samples,
2300 order,
2301 rice_parameter,
2302 min_partition_order,
2303 max_partition_order,
2304 precompute_partition_sums,
2305 do_escape_coding,
2306 rice_parameter_search_dist,
2307 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2308 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002309
2310 subframe->data.lpc.order = order;
2311 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2312 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00002313 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002314 for(i = 0; i < order; i++)
2315 subframe->data.lpc.warmup[i] = signal[i];
2316
Josh Coalson82b73242001-03-28 22:17:05 +00002317 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 +00002318}
2319
Josh Coalson6fe72f72002-08-20 04:01:59 +00002320unsigned evaluate_verbatim_subframe_(
2321 const FLAC__int32 signal[],
2322 unsigned blocksize,
2323 unsigned subframe_bps,
2324 FLAC__Subframe *subframe
2325)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002326{
2327 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2328
2329 subframe->data.verbatim.data = signal;
2330
Josh Coalson82b73242001-03-28 22:17:05 +00002331 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 +00002332}
2333
Josh Coalson6fe72f72002-08-20 04:01:59 +00002334unsigned find_best_partition_order_(
2335 FLAC__StreamEncoderPrivate *private_,
2336 const FLAC__int32 residual[],
2337 FLAC__uint32 abs_residual[],
2338 FLAC__uint64 abs_residual_partition_sums[],
2339 unsigned raw_bits_per_partition[],
2340 unsigned residual_samples,
2341 unsigned predictor_order,
2342 unsigned rice_parameter,
2343 unsigned min_partition_order,
2344 unsigned max_partition_order,
2345 FLAC__bool precompute_partition_sums,
2346 FLAC__bool do_escape_coding,
2347 unsigned rice_parameter_search_dist,
2348 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2349)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002350{
Josh Coalson77e3f312001-06-23 03:03:24 +00002351 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002352 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00002353 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00002354 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002355 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002356
Josh Coalson2051dd42001-04-12 22:22:34 +00002357 /* compute abs(residual) for use later */
2358 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2359 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00002360 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00002361 }
2362
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002363 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 +00002364 min_partition_order = min(min_partition_order, max_partition_order);
2365
Josh Coalson8395d022001-07-12 21:25:22 +00002366 if(precompute_partition_sums) {
2367 int partition_order;
2368 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002369
Josh Coalsonf1eff452002-07-31 07:05:33 +00002370 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 +00002371
2372 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00002373 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 +00002374
2375 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2376#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002377 if(!
2378 set_partitioned_rice_with_precompute_(
2379 residual,
2380 abs_residual_partition_sums+sum,
2381 raw_bits_per_partition+sum,
2382 residual_samples,
2383 predictor_order,
2384 rice_parameter,
2385 rice_parameter_search_dist,
2386 (unsigned)partition_order,
2387 do_escape_coding,
2388 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2389 &residual_bits
2390 )
2391 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002392#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002393 if(!
2394 set_partitioned_rice_with_precompute_(
2395 abs_residual,
2396 abs_residual_partition_sums+sum,
2397 raw_bits_per_partition+sum,
2398 residual_samples,
2399 predictor_order,
2400 rice_parameter,
2401 rice_parameter_search_dist,
2402 (unsigned)partition_order,
2403 do_escape_coding,
2404 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2405 &residual_bits
2406 )
2407 )
Josh Coalson8395d022001-07-12 21:25:22 +00002408#endif
2409 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002410 FLAC__ASSERT(best_residual_bits != 0);
2411 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002412 }
2413 sum += 1u << partition_order;
2414 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2415 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002416 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002417 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002418 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00002419 }
2420 }
Josh Coalson8395d022001-07-12 21:25:22 +00002421 else {
2422 unsigned partition_order;
2423 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2424#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002425 if(!
2426 set_partitioned_rice_(
2427 abs_residual,
2428 residual,
2429 residual_samples,
2430 predictor_order,
2431 rice_parameter,
2432 rice_parameter_search_dist,
2433 partition_order,
2434 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2435 &residual_bits
2436 )
2437 )
Josh Coalson8395d022001-07-12 21:25:22 +00002438#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002439 if(!
2440 set_partitioned_rice_(
2441 abs_residual,
2442 residual_samples,
2443 predictor_order,
2444 rice_parameter,
2445 rice_parameter_search_dist,
2446 partition_order,
2447 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2448 &residual_bits
2449 )
2450 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002451#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002452 {
2453 FLAC__ASSERT(best_residual_bits != 0);
2454 break;
2455 }
2456 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2457 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002458 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002459 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002460 }
2461 }
2462 }
2463
Josh Coalsona37ba462002-08-19 21:36:39 +00002464 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00002465 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00002466 * it is const to the outside world.
2467 */
2468 {
2469 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2470 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2471 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2472 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2473 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002474
2475 return best_residual_bits;
2476}
2477
Josh Coalson6fe72f72002-08-20 04:01:59 +00002478void precompute_partition_info_sums_(
2479 const FLAC__uint32 abs_residual[],
2480 FLAC__uint64 abs_residual_partition_sums[],
2481 unsigned residual_samples,
2482 unsigned predictor_order,
2483 unsigned min_partition_order,
2484 unsigned max_partition_order
2485)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002486{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002487 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002488 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002489 const unsigned blocksize = residual_samples + predictor_order;
2490
Josh Coalsonaef013c2001-04-24 01:25:42 +00002491 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002492 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002493 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00002494 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002495 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002496 const unsigned partitions = 1u << partition_order;
2497 const unsigned default_partition_samples = blocksize >> partition_order;
2498
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002499 FLAC__ASSERT(default_partition_samples > predictor_order);
2500
2501 for(partition = residual_sample = 0; partition < partitions; partition++) {
2502 partition_samples = default_partition_samples;
2503 if(partition == 0)
2504 partition_samples -= predictor_order;
2505 abs_residual_partition_sum = 0;
2506 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2507 abs_r = abs_residual[residual_sample];
2508 abs_residual_partition_sum += abs_r;
2509 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002510 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002511 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002512 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002513 to_partition = partitions;
2514 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002515 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00002516
Josh Coalson8395d022001-07-12 21:25:22 +00002517 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00002518 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002519 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002520 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002521 const unsigned partitions = 1u << partition_order;
2522 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00002523 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00002524 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002525 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00002526 from_partition++;
2527 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002528 }
2529 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002530}
Josh Coalson8395d022001-07-12 21:25:22 +00002531
Josh Coalson6fe72f72002-08-20 04:01:59 +00002532void precompute_partition_info_escapes_(
2533 const FLAC__int32 residual[],
2534 unsigned raw_bits_per_partition[],
2535 unsigned residual_samples,
2536 unsigned predictor_order,
2537 unsigned min_partition_order,
2538 unsigned max_partition_order
2539)
Josh Coalson8395d022001-07-12 21:25:22 +00002540{
2541 int partition_order;
2542 unsigned from_partition, to_partition = 0;
2543 const unsigned blocksize = residual_samples + predictor_order;
2544
2545 /* first do max_partition_order */
2546 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2547 FLAC__int32 r, residual_partition_min, residual_partition_max;
2548 unsigned silog2_min, silog2_max;
2549 unsigned partition, partition_sample, partition_samples, residual_sample;
2550 const unsigned partitions = 1u << partition_order;
2551 const unsigned default_partition_samples = blocksize >> partition_order;
2552
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002553 FLAC__ASSERT(default_partition_samples > predictor_order);
2554
2555 for(partition = residual_sample = 0; partition < partitions; partition++) {
2556 partition_samples = default_partition_samples;
2557 if(partition == 0)
2558 partition_samples -= predictor_order;
2559 residual_partition_min = residual_partition_max = 0;
2560 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2561 r = residual[residual_sample];
2562 if(r < residual_partition_min)
2563 residual_partition_min = r;
2564 else if(r > residual_partition_max)
2565 residual_partition_max = r;
2566 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00002567 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002568 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2569 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2570 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00002571 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002572 to_partition = partitions;
2573 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002574 }
2575
2576 /* now merge partitions for lower orders */
2577 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2578 unsigned m;
2579 unsigned i;
2580 const unsigned partitions = 1u << partition_order;
2581 for(i = 0; i < partitions; i++) {
2582 m = raw_bits_per_partition[from_partition];
2583 from_partition++;
2584 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2585 from_partition++;
2586 to_partition++;
2587 }
2588 }
2589}
Josh Coalson94e02cd2001-01-25 10:41:06 +00002590
Josh Coalson352e0f62001-03-20 22:55:50 +00002591#ifdef VARIABLE_RICE_BITS
2592#undef VARIABLE_RICE_BITS
2593#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002594#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00002595#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00002596#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002597
Josh Coalson8395d022001-07-12 21:25:22 +00002598#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002599FLAC__bool set_partitioned_rice_(
2600 const FLAC__uint32 abs_residual[],
2601 const FLAC__int32 residual[],
2602 const unsigned residual_samples,
2603 const unsigned predictor_order,
2604 const unsigned suggested_rice_parameter,
2605 const unsigned rice_parameter_search_dist,
2606 const unsigned partition_order,
2607 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2608 unsigned *bits
2609)
Josh Coalson8395d022001-07-12 21:25:22 +00002610#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002611FLAC__bool set_partitioned_rice_(
2612 const FLAC__uint32 abs_residual[],
2613 const unsigned residual_samples,
2614 const unsigned predictor_order,
2615 const unsigned suggested_rice_parameter,
2616 const unsigned rice_parameter_search_dist,
2617 const unsigned partition_order,
2618 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2619 unsigned *bits
2620)
Josh Coalson8395d022001-07-12 21:25:22 +00002621#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002622{
Josh Coalson034dfab2001-04-27 19:10:23 +00002623 unsigned rice_parameter, partition_bits;
2624#ifndef NO_RICE_SEARCH
2625 unsigned best_partition_bits;
2626 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2627#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002628 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002629 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002630
Josh Coalson1b689822001-05-31 20:11:02 +00002631 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00002632
Josh Coalsona37ba462002-08-19 21:36:39 +00002633 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2634 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002635
Josh Coalson94e02cd2001-01-25 10:41:06 +00002636 if(partition_order == 0) {
2637 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00002638
Josh Coalson034dfab2001-04-27 19:10:23 +00002639#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00002640 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002641 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00002642 min_rice_parameter = 0;
2643 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002644 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2645 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00002646 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002647#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002648 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2649#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00002650 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002651 }
2652 }
2653 else
2654 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2655
2656 best_partition_bits = 0xffffffff;
2657 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2658#endif
2659#ifdef VARIABLE_RICE_BITS
2660#ifdef FLAC__SYMMETRIC_RICE
2661 partition_bits = (2+rice_parameter) * residual_samples;
2662#else
2663 const unsigned rice_parameter_estimate = rice_parameter-1;
2664 partition_bits = (1+rice_parameter) * residual_samples;
2665#endif
2666#else
2667 partition_bits = 0;
2668#endif
2669 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2670 for(i = 0; i < residual_samples; i++) {
2671#ifdef VARIABLE_RICE_BITS
2672#ifdef FLAC__SYMMETRIC_RICE
2673 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2674#else
2675 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2676#endif
2677#else
2678 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2679#endif
2680 }
2681#ifndef NO_RICE_SEARCH
2682 if(partition_bits < best_partition_bits) {
2683 best_rice_parameter = rice_parameter;
2684 best_partition_bits = partition_bits;
2685 }
2686 }
2687#endif
2688 parameters[0] = best_rice_parameter;
2689 bits_ += best_partition_bits;
2690 }
2691 else {
2692 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002693 unsigned partition_samples;
2694 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00002695 const unsigned partitions = 1u << partition_order;
2696 for(partition = residual_sample = 0; partition < partitions; partition++) {
2697 partition_samples = (residual_samples+predictor_order) >> partition_order;
2698 if(partition == 0) {
2699 if(partition_samples <= predictor_order)
2700 return false;
2701 else
2702 partition_samples -= predictor_order;
2703 }
2704 mean = 0;
2705 save_residual_sample = residual_sample;
2706 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002707 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00002708 residual_sample = save_residual_sample;
2709#ifdef FLAC__SYMMETRIC_RICE
2710 mean += partition_samples >> 1; /* for rounding effect */
2711 mean /= partition_samples;
2712
2713 /* calc rice_parameter = floor(log2(mean)) */
2714 rice_parameter = 0;
2715 mean>>=1;
2716 while(mean) {
2717 rice_parameter++;
2718 mean >>= 1;
2719 }
2720#else
2721 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002722 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00002723 ;
2724#endif
2725 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002726#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002727 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2728#endif
2729 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2730 }
2731
2732#ifndef NO_RICE_SEARCH
2733 if(rice_parameter_search_dist) {
2734 if(rice_parameter < rice_parameter_search_dist)
2735 min_rice_parameter = 0;
2736 else
2737 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2738 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2739 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002740#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002741 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2742#endif
2743 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2744 }
2745 }
2746 else
2747 min_rice_parameter = max_rice_parameter = rice_parameter;
2748
2749 best_partition_bits = 0xffffffff;
2750 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2751#endif
2752#ifdef VARIABLE_RICE_BITS
2753#ifdef FLAC__SYMMETRIC_RICE
2754 partition_bits = (2+rice_parameter) * partition_samples;
2755#else
2756 const unsigned rice_parameter_estimate = rice_parameter-1;
2757 partition_bits = (1+rice_parameter) * partition_samples;
2758#endif
2759#else
2760 partition_bits = 0;
2761#endif
2762 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2763 save_residual_sample = residual_sample;
2764 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2765#ifdef VARIABLE_RICE_BITS
2766#ifdef FLAC__SYMMETRIC_RICE
2767 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
2768#else
2769 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
2770#endif
2771#else
2772 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2773#endif
2774 }
2775#ifndef NO_RICE_SEARCH
2776 if(rice_parameter != max_rice_parameter)
2777 residual_sample = save_residual_sample;
2778 if(partition_bits < best_partition_bits) {
2779 best_rice_parameter = rice_parameter;
2780 best_partition_bits = partition_bits;
2781 }
2782 }
2783#endif
2784 parameters[partition] = best_rice_parameter;
2785 bits_ += best_partition_bits;
2786 }
2787 }
2788
2789 *bits = bits_;
2790 return true;
2791}
2792
2793#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002794FLAC__bool set_partitioned_rice_with_precompute_(
2795 const FLAC__int32 residual[],
2796 const FLAC__uint64 abs_residual_partition_sums[],
2797 const unsigned raw_bits_per_partition[],
2798 const unsigned residual_samples,
2799 const unsigned predictor_order,
2800 const unsigned suggested_rice_parameter,
2801 const unsigned rice_parameter_search_dist,
2802 const unsigned partition_order,
2803 const FLAC__bool search_for_escapes,
2804 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2805 unsigned *bits
2806)
Josh Coalson8395d022001-07-12 21:25:22 +00002807#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002808FLAC__bool set_partitioned_rice_with_precompute_(
2809 const FLAC__uint32 abs_residual[],
2810 const FLAC__uint64 abs_residual_partition_sums[],
2811 const unsigned raw_bits_per_partition[],
2812 const unsigned residual_samples,
2813 const unsigned predictor_order,
2814 const unsigned suggested_rice_parameter,
2815 const unsigned rice_parameter_search_dist,
2816 const unsigned partition_order,
2817 const FLAC__bool search_for_escapes,
2818 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2819 unsigned *bits
2820)
Josh Coalson8395d022001-07-12 21:25:22 +00002821#endif
2822{
2823 unsigned rice_parameter, partition_bits;
2824#ifndef NO_RICE_SEARCH
2825 unsigned best_partition_bits;
2826 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2827#endif
2828 unsigned flat_bits;
2829 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002830 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002831
2832 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2833
Josh Coalsona37ba462002-08-19 21:36:39 +00002834 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2835 parameters = partitioned_rice_contents->parameters;
2836 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002837
Josh Coalson8395d022001-07-12 21:25:22 +00002838 if(partition_order == 0) {
2839 unsigned i;
2840
2841#ifndef NO_RICE_SEARCH
2842 if(rice_parameter_search_dist) {
2843 if(suggested_rice_parameter < rice_parameter_search_dist)
2844 min_rice_parameter = 0;
2845 else
2846 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2847 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2848 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002849#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002850 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2851#endif
2852 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2853 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002854 }
2855 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002856 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00002857
Josh Coalson034dfab2001-04-27 19:10:23 +00002858 best_partition_bits = 0xffffffff;
2859 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2860#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002861#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002862#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00002863 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002864#else
Josh Coalson352e0f62001-03-20 22:55:50 +00002865 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00002866 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002867#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002868#else
2869 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002870#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00002871 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00002872 for(i = 0; i < residual_samples; i++) {
2873#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002874#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00002875 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002876#else
Josh Coalson2051dd42001-04-12 22:22:34 +00002877 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00002878#endif
2879#else
Josh Coalson2051dd42001-04-12 22:22:34 +00002880 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 +00002881#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00002882 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002883#ifndef NO_RICE_SEARCH
2884 if(partition_bits < best_partition_bits) {
2885 best_rice_parameter = rice_parameter;
2886 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00002887 }
2888 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002889#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002890 if(search_for_escapes) {
2891 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;
2892 if(flat_bits <= best_partition_bits) {
2893 raw_bits[0] = raw_bits_per_partition[0];
2894 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2895 best_partition_bits = flat_bits;
2896 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002897 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002898 parameters[0] = best_rice_parameter;
2899 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002900 }
2901 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00002902 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002903 unsigned partition_samples;
2904 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002905 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00002906 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002907 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00002908 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002909 if(partition_samples <= predictor_order)
2910 return false;
2911 else
2912 partition_samples -= predictor_order;
2913 }
Josh Coalson05d20792001-06-29 23:12:26 +00002914 mean = abs_residual_partition_sums[partition];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002915#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson05d20792001-06-29 23:12:26 +00002916 mean += partition_samples >> 1; /* for rounding effect */
2917 mean /= partition_samples;
2918
Josh Coalson034dfab2001-04-27 19:10:23 +00002919 /* calc rice_parameter = floor(log2(mean)) */
2920 rice_parameter = 0;
2921 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002922 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002923 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002924 mean >>= 1;
2925 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00002926#else
Josh Coalson05d20792001-06-29 23:12:26 +00002927 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002928 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00002929 ;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002930#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002931 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002932#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002933 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2934#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002935 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002936 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002937
Josh Coalson034dfab2001-04-27 19:10:23 +00002938#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00002939 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002940 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00002941 min_rice_parameter = 0;
2942 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002943 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2944 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00002945 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002946#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002947 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2948#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00002949 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002950 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002951 }
2952 else
2953 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00002954
Josh Coalson034dfab2001-04-27 19:10:23 +00002955 best_partition_bits = 0xffffffff;
2956 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2957#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002958#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002959#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00002960 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002961#else
Josh Coalson034dfab2001-04-27 19:10:23 +00002962 const unsigned rice_parameter_estimate = rice_parameter-1;
2963 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00002964#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002965#else
2966 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002967#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002968 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00002969 save_residual_sample = residual_sample;
2970 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00002971#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002972#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson4dacd192001-06-06 21:11:44 +00002973 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002974#else
Josh Coalson4dacd192001-06-06 21:11:44 +00002975 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00002976#endif
2977#else
Josh Coalson4dacd192001-06-06 21:11:44 +00002978 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 +00002979#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002980 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002981#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00002982 if(rice_parameter != max_rice_parameter)
2983 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00002984 if(partition_bits < best_partition_bits) {
2985 best_rice_parameter = rice_parameter;
2986 best_partition_bits = partition_bits;
2987 }
Josh Coalson2051dd42001-04-12 22:22:34 +00002988 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002989#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002990 if(search_for_escapes) {
2991 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;
2992 if(flat_bits <= best_partition_bits) {
2993 raw_bits[partition] = raw_bits_per_partition[partition];
2994 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2995 best_partition_bits = flat_bits;
2996 }
Josh Coalson2051dd42001-04-12 22:22:34 +00002997 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002998 parameters[partition] = best_rice_parameter;
2999 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003000 }
3001 }
3002
3003 *bits = bits_;
3004 return true;
3005}
Josh Coalson859bc542001-03-27 22:22:27 +00003006
Josh Coalsonf1eff452002-07-31 07:05:33 +00003007unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00003008{
3009 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00003010 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00003011
3012 for(i = 0; i < samples && !(x&1); i++)
3013 x |= signal[i];
3014
3015 if(x == 0) {
3016 shift = 0;
3017 }
3018 else {
3019 for(shift = 0; !(x&1); shift++)
3020 x >>= 1;
3021 }
3022
3023 if(shift > 0) {
3024 for(i = 0; i < samples; i++)
3025 signal[i] >>= shift;
3026 }
3027
3028 return shift;
3029}
Josh Coalsond86e03b2002-08-03 21:56:15 +00003030
3031void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3032{
3033 unsigned channel;
3034
3035 for(channel = 0; channel < channels; channel++)
3036 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3037
3038 fifo->tail += wide_samples;
3039
3040 FLAC__ASSERT(fifo->tail <= fifo->size);
3041}
3042
3043void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3044{
3045 unsigned channel;
3046 unsigned sample, wide_sample;
3047 unsigned tail = fifo->tail;
3048
3049 sample = input_offset * channels;
3050 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3051 for(channel = 0; channel < channels; channel++)
3052 fifo->data[channel][tail] = input[sample++];
3053 tail++;
3054 }
3055 fifo->tail = tail;
3056
3057 FLAC__ASSERT(fifo->tail <= fifo->size);
3058}
3059
3060FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3061{
3062 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3063 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3064 (void)decoder;
3065
3066 if(encoder->private_->verify.needs_magic_hack) {
3067 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3068 *bytes = FLAC__STREAM_SYNC_LENGTH;
3069 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3070 encoder->private_->verify.needs_magic_hack = false;
3071 }
3072 else {
3073 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00003074 /*
3075 * If we get here, a FIFO underflow has occurred,
3076 * which means there is a bug somewhere.
3077 */
3078 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003079 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3080 }
3081 else if(encoded_bytes < *bytes)
3082 *bytes = encoded_bytes;
3083 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3084 encoder->private_->verify.output.data += *bytes;
3085 encoder->private_->verify.output.bytes -= *bytes;
3086 }
3087
3088 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3089}
3090
3091FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3092{
3093 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3094 unsigned channel;
3095 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3096 const unsigned blocksize = frame->header.blocksize;
3097 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3098
3099 for(channel = 0; channel < channels; channel++) {
3100 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3101 unsigned i, sample = 0;
3102 FLAC__int32 expect = 0, got = 0;
3103
3104 for(i = 0; i < blocksize; i++) {
3105 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3106 sample = i;
3107 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3108 got = (FLAC__int32)buffer[channel][i];
3109 break;
3110 }
3111 }
3112 FLAC__ASSERT(i < blocksize);
3113 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3114 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00003115 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003116 encoder->private_->verify.error_stats.channel = channel;
3117 encoder->private_->verify.error_stats.sample = sample;
3118 encoder->private_->verify.error_stats.expected = expect;
3119 encoder->private_->verify.error_stats.got = got;
3120 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3121 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3122 }
3123 }
3124 /* dequeue the frame from the fifo */
3125 for(channel = 0; channel < channels; channel++) {
3126 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3127 }
3128 encoder->private_->verify.input_fifo.tail -= blocksize;
3129 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3130}
3131
3132void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3133{
3134 (void)decoder, (void)metadata, (void)client_data;
3135}
3136
3137void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3138{
3139 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3140 (void)decoder, (void)status;
3141 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3142}