blob: 1c0ba964fd365c1eb4531d6481ae6d9cc4d60aea [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson0395dac2006-04-25 06:59:33 +00002 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003 *
Josh Coalsonafd81072003-01-31 23:34:56 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00007 *
Josh Coalsonafd81072003-01-31 23:34:56 +00008 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000010 *
Josh Coalsonafd81072003-01-31 23:34:56 +000011 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000030 */
31
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000032/*@@@@@@*/
Josh Coalson81cbb8e2006-05-01 06:17:24 +000033#undef WINDOW_DEBUG_OUTPUT
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000034
Josh Coalsone6b3bbe2002-10-08 06:03:25 +000035#include <limits.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000036#include <stdio.h>
37#include <stdlib.h> /* for malloc() */
38#include <string.h> /* for memcpy() */
Josh Coalson1b689822001-05-31 20:11:02 +000039#include "FLAC/assert.h"
Josh Coalsond86e03b2002-08-03 21:56:15 +000040#include "FLAC/stream_decoder.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000041#include "protected/stream_encoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000042#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000043#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000044#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000045#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000046#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000047#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000048#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000049#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000050#include "private/memory.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000051#include "private/stream_encoder_framing.h"
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000052#include "private/window.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000053
Josh Coalson5e31be12002-12-04 07:07:35 +000054#ifdef HAVE_CONFIG_H
55#include <config.h>
56#endif
57
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000058#ifdef min
59#undef min
60#endif
61#define min(x,y) ((x)<(y)?(x):(y))
62
63#ifdef max
64#undef max
65#endif
66#define max(x,y) ((x)>(y)?(x):(y))
67
Josh Coalsond86e03b2002-08-03 21:56:15 +000068typedef struct {
69 FLAC__int32 *data[FLAC__MAX_CHANNELS];
70 unsigned size; /* of each data[] in samples */
71 unsigned tail;
72} verify_input_fifo;
73
74typedef struct {
75 const FLAC__byte *data;
76 unsigned capacity;
77 unsigned bytes;
78} verify_output;
79
80typedef enum {
81 ENCODER_IN_MAGIC = 0,
82 ENCODER_IN_METADATA = 1,
83 ENCODER_IN_AUDIO = 2
84} EncoderStateHint;
85
Josh Coalson0a15c142001-06-13 17:59:57 +000086/***********************************************************************
87 *
88 * Private class method prototypes
89 *
90 ***********************************************************************/
91
Josh Coalsonf1eff452002-07-31 07:05:33 +000092static void set_defaults_(FLAC__StreamEncoder *encoder);
93static void free_(FLAC__StreamEncoder *encoder);
94static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
Josh Coalsond86e03b2002-08-03 21:56:15 +000095static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
Josh Coalsonf1eff452002-07-31 07:05:33 +000096static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
97static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson6fe72f72002-08-20 04:01:59 +000098
99static FLAC__bool process_subframe_(
100 FLAC__StreamEncoder *encoder,
101 unsigned min_partition_order,
102 unsigned max_partition_order,
103 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000104 const FLAC__FrameHeader *frame_header,
105 unsigned subframe_bps,
106 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000107#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000108 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000109#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000110 FLAC__Subframe *subframe[2],
111 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
112 FLAC__int32 *residual[2],
113 unsigned *best_subframe,
114 unsigned *best_bits
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000115#ifdef WINDOW_DEBUG_OUTPUT
116 ,unsigned subframe_number
117#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000118);
119
120static FLAC__bool add_subframe_(
121 FLAC__StreamEncoder *encoder,
122 const FLAC__FrameHeader *frame_header,
123 unsigned subframe_bps,
124 const FLAC__Subframe *subframe,
125 FLAC__BitBuffer *frame
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000126#ifdef WINDOW_DEBUG_OUTPUT
127,unsigned subframe_bits
128#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000129);
130
131static unsigned evaluate_constant_subframe_(
132 const FLAC__int32 signal,
133 unsigned subframe_bps,
134 FLAC__Subframe *subframe
135);
136
137static unsigned evaluate_fixed_subframe_(
138 FLAC__StreamEncoder *encoder,
139 const FLAC__int32 signal[],
140 FLAC__int32 residual[],
141 FLAC__uint32 abs_residual[],
142 FLAC__uint64 abs_residual_partition_sums[],
143 unsigned raw_bits_per_partition[],
144 unsigned blocksize,
145 unsigned subframe_bps,
146 unsigned order,
147 unsigned rice_parameter,
148 unsigned min_partition_order,
149 unsigned max_partition_order,
150 FLAC__bool precompute_partition_sums,
151 FLAC__bool do_escape_coding,
152 unsigned rice_parameter_search_dist,
153 FLAC__Subframe *subframe,
154 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
155);
156
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000157#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000158static unsigned evaluate_lpc_subframe_(
159 FLAC__StreamEncoder *encoder,
160 const FLAC__int32 signal[],
161 FLAC__int32 residual[],
162 FLAC__uint32 abs_residual[],
163 FLAC__uint64 abs_residual_partition_sums[],
164 unsigned raw_bits_per_partition[],
165 const FLAC__real lp_coeff[],
166 unsigned blocksize,
167 unsigned subframe_bps,
168 unsigned order,
169 unsigned qlp_coeff_precision,
170 unsigned rice_parameter,
171 unsigned min_partition_order,
172 unsigned max_partition_order,
173 FLAC__bool precompute_partition_sums,
174 FLAC__bool do_escape_coding,
175 unsigned rice_parameter_search_dist,
176 FLAC__Subframe *subframe,
177 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000178#ifdef WINDOW_DEBUG_OUTPUT
179 ,unsigned frame_number
180 ,unsigned subframe_number
181 ,FLAC__ApodizationSpecification aspec
182#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000183);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000184#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000185
186static unsigned evaluate_verbatim_subframe_(
187 const FLAC__int32 signal[],
188 unsigned blocksize,
189 unsigned subframe_bps,
190 FLAC__Subframe *subframe
191);
192
193static unsigned find_best_partition_order_(
194 struct FLAC__StreamEncoderPrivate *private_,
195 const FLAC__int32 residual[],
196 FLAC__uint32 abs_residual[],
197 FLAC__uint64 abs_residual_partition_sums[],
198 unsigned raw_bits_per_partition[],
199 unsigned residual_samples,
200 unsigned predictor_order,
201 unsigned rice_parameter,
202 unsigned min_partition_order,
203 unsigned max_partition_order,
204 FLAC__bool precompute_partition_sums,
205 FLAC__bool do_escape_coding,
206 unsigned rice_parameter_search_dist,
207 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
208);
209
210static void precompute_partition_info_sums_(
211 const FLAC__uint32 abs_residual[],
212 FLAC__uint64 abs_residual_partition_sums[],
213 unsigned residual_samples,
214 unsigned predictor_order,
215 unsigned min_partition_order,
216 unsigned max_partition_order
217);
218
219static void precompute_partition_info_escapes_(
220 const FLAC__int32 residual[],
221 unsigned raw_bits_per_partition[],
222 unsigned residual_samples,
223 unsigned predictor_order,
224 unsigned min_partition_order,
225 unsigned max_partition_order
226);
227
Josh Coalson8395d022001-07-12 21:25:22 +0000228#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000229static FLAC__bool set_partitioned_rice_(
230 const FLAC__uint32 abs_residual[],
231 const FLAC__int32 residual[],
232 const unsigned residual_samples,
233 const unsigned predictor_order,
234 const unsigned suggested_rice_parameter,
235 const unsigned rice_parameter_search_dist,
236 const unsigned partition_order,
237 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
238 unsigned *bits
239);
240
241static FLAC__bool set_partitioned_rice_with_precompute_(
242 const FLAC__int32 residual[],
243 const FLAC__uint64 abs_residual_partition_sums[],
244 const unsigned raw_bits_per_partition[],
245 const unsigned residual_samples,
246 const unsigned predictor_order,
247 const unsigned suggested_rice_parameter,
248 const unsigned rice_parameter_search_dist,
249 const unsigned partition_order,
250 const FLAC__bool search_for_escapes,
251 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
252 unsigned *bits
253);
Josh Coalson8395d022001-07-12 21:25:22 +0000254#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000255static FLAC__bool set_partitioned_rice_(
256 const FLAC__uint32 abs_residual[],
257 const unsigned residual_samples,
258 const unsigned predictor_order,
259 const unsigned suggested_rice_parameter,
260 const unsigned rice_parameter_search_dist,
261 const unsigned partition_order,
262 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
263 unsigned *bits
264);
265
266static FLAC__bool set_partitioned_rice_with_precompute_(
267 const FLAC__uint32 abs_residual[],
268 const FLAC__uint64 abs_residual_partition_sums[],
269 const unsigned raw_bits_per_partition[],
270 const unsigned residual_samples,
271 const unsigned predictor_order,
272 const unsigned suggested_rice_parameter,
273 const unsigned rice_parameter_search_dist,
274 const unsigned partition_order,
275 const FLAC__bool search_for_escapes,
276 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
277 unsigned *bits
278);
Josh Coalson0a15c142001-06-13 17:59:57 +0000279#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000280
Josh Coalsonf1eff452002-07-31 07:05:33 +0000281static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000282
Josh Coalsond86e03b2002-08-03 21:56:15 +0000283/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000284static void append_to_verify_fifo_(
285 verify_input_fifo *fifo,
286 const FLAC__int32 * const input[],
287 unsigned input_offset,
288 unsigned channels,
289 unsigned wide_samples
290);
291
292static void append_to_verify_fifo_interleaved_(
293 verify_input_fifo *fifo,
294 const FLAC__int32 input[],
295 unsigned input_offset,
296 unsigned channels,
297 unsigned wide_samples
298);
299
300static FLAC__StreamDecoderReadStatus verify_read_callback_(
301 const FLAC__StreamDecoder *decoder,
302 FLAC__byte buffer[],
303 unsigned *bytes,
304 void *client_data
305);
306
307static FLAC__StreamDecoderWriteStatus verify_write_callback_(
308 const FLAC__StreamDecoder *decoder,
309 const FLAC__Frame *frame,
310 const FLAC__int32 * const buffer[],
311 void *client_data
312);
313
314static void verify_metadata_callback_(
315 const FLAC__StreamDecoder *decoder,
316 const FLAC__StreamMetadata *metadata,
317 void *client_data
318);
319
320static void verify_error_callback_(
321 const FLAC__StreamDecoder *decoder,
322 FLAC__StreamDecoderErrorStatus status,
323 void *client_data
324);
325
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000326#ifdef WINDOW_DEBUG_OUTPUT
327static const char * const winstr[] = {
328 "bartlett",
329 "bartlett_hann",
330 "blackman",
331 "blackman_harris_4term_92db_sidelobe",
332 "connes",
333 "flattop",
334 "gauss",
335 "hamming",
336 "hann",
337 "kaiser_bessel",
338 "nuttall",
339 "rectangular",
340 "triangle",
341 "tukey",
342 "welch"
343};
344#endif
Josh Coalson0a15c142001-06-13 17:59:57 +0000345
346/***********************************************************************
347 *
348 * Private class data
349 *
350 ***********************************************************************/
351
352typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000353 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000354 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
355 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000356#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000357 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
358 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000359 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
360 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000361#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000362 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
363 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 +0000364 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
365 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000366 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
367 FLAC__Subframe subframe_workspace_mid_side[2][2];
368 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
369 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000370 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
371 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
372 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
373 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000374 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000375 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000376 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000377 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000378 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000379 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 +0000380 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 +0000381 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000382 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
383 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000384 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsoncc682512002-06-08 04:53:42 +0000385 FLAC__StreamMetadata metadata;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000386 unsigned current_sample_number;
387 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000388 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000389 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000390#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000391 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000392#else
393 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
394#endif
395#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000396 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
Josh Coalson7446e182005-01-26 04:04:38 +0000397 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[]);
398 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[]);
399 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 Coalson5f2b46d2004-11-09 01:34:01 +0000400#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000401 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
402 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
403 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
404 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 +0000405 FLAC__bool disable_constant_subframes;
406 FLAC__bool disable_fixed_subframes;
407 FLAC__bool disable_verbatim_subframes;
Josh Coalson681c2932002-08-01 08:19:37 +0000408 FLAC__StreamEncoderWriteCallback write_callback;
409 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000410 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000411 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000412 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
413 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000414#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000415 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
416 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000417 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
418 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000419#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000420 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
421 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
422 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000423 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000424 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000425 /*
426 * These fields have been moved here from private function local
427 * declarations merely to save stack space during encoding.
428 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000429#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000430 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000431#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000432 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000433 /*
434 * The data for the verify section
435 */
436 struct {
437 FLAC__StreamDecoder *decoder;
438 EncoderStateHint state_hint;
439 FLAC__bool needs_magic_hack;
440 verify_input_fifo input_fifo;
441 verify_output output;
442 struct {
443 FLAC__uint64 absolute_sample;
444 unsigned frame_number;
445 unsigned channel;
446 unsigned sample;
447 FLAC__int32 expected;
448 FLAC__int32 got;
449 } error_stats;
450 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000451 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000452} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000453
Josh Coalson0a15c142001-06-13 17:59:57 +0000454/***********************************************************************
455 *
456 * Public static class data
457 *
458 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000459
Josh Coalson6afed9f2002-10-16 22:29:47 +0000460FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000461 "FLAC__STREAM_ENCODER_OK",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000462 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
463 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson00e53872001-06-16 07:32:25 +0000464 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
Josh Coalson0a15c142001-06-13 17:59:57 +0000465 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
466 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
467 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
468 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
Josh Coalson20ac2c12002-08-30 05:47:14 +0000469 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
Josh Coalson0a15c142001-06-13 17:59:57 +0000470 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
471 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
472 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
473 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
474 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
475 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
476 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson66075c12002-06-01 05:39:38 +0000477 "FLAC__STREAM_ENCODER_INVALID_METADATA",
Josh Coalson0a15c142001-06-13 17:59:57 +0000478 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
479 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
480 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
481 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
482 "FLAC__STREAM_ENCODER_UNINITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000483};
484
Josh Coalson6afed9f2002-10-16 22:29:47 +0000485FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000486 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
487 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000488};
489
Josh Coalson0a15c142001-06-13 17:59:57 +0000490/***********************************************************************
491 *
492 * Class constructor/destructor
493 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000494 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000495FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000496{
Josh Coalson0a15c142001-06-13 17:59:57 +0000497 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000498 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000499
Josh Coalson0a15c142001-06-13 17:59:57 +0000500 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000501
Josh Coalsonea7155f2002-10-18 05:49:19 +0000502 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000503 if(encoder == 0) {
504 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000505 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000506
Josh Coalsonea7155f2002-10-18 05:49:19 +0000507 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000508 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000509 free(encoder);
510 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000511 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000512
Josh Coalsonea7155f2002-10-18 05:49:19 +0000513 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000514 if(encoder->private_ == 0) {
515 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000516 free(encoder);
517 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000518 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000519
Josh Coalsonaec256b2002-03-12 16:19:54 +0000520 encoder->private_->frame = FLAC__bitbuffer_new();
521 if(encoder->private_->frame == 0) {
522 free(encoder->private_);
523 free(encoder->protected_);
524 free(encoder);
525 return 0;
526 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000527
Josh Coalsonf1eff452002-07-31 07:05:33 +0000528 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000529
Josh Coalson3262b0d2002-08-14 20:58:42 +0000530 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000531
532 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
533 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
534 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
535 }
536 for(i = 0; i < 2; i++) {
537 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
538 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
539 }
540 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000541 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
542 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000543 }
544 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000545 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
546 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 +0000547 }
548
549 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000550 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
551 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000552 }
553 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000554 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
555 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 +0000556 }
557 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000558 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000559
Josh Coalsonfa697a92001-08-16 20:07:29 +0000560 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000561
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000562 return encoder;
563}
564
Josh Coalson6afed9f2002-10-16 22:29:47 +0000565FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000566{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000567 unsigned i;
568
Josh Coalsonf1eff452002-07-31 07:05:33 +0000569 FLAC__ASSERT(0 != encoder);
570 FLAC__ASSERT(0 != encoder->protected_);
571 FLAC__ASSERT(0 != encoder->private_);
572 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000573
Josh Coalson3262b0d2002-08-14 20:58:42 +0000574 encoder->private_->is_being_deleted = true;
575
576 FLAC__stream_encoder_finish(encoder);
577
Josh Coalson4fa90592002-12-04 07:01:37 +0000578 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000579 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000580
581 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000582 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
583 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000584 }
585 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000586 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
587 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 +0000588 }
589 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000590 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000591
Josh Coalsonaec256b2002-03-12 16:19:54 +0000592 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000593 free(encoder->private_);
594 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000595 free(encoder);
596}
597
Josh Coalson0a15c142001-06-13 17:59:57 +0000598/***********************************************************************
599 *
600 * Public class methods
601 *
602 ***********************************************************************/
603
Josh Coalson6afed9f2002-10-16 22:29:47 +0000604FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000605{
606 unsigned i;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000607 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000608
Josh Coalsonf1eff452002-07-31 07:05:33 +0000609 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000610
Josh Coalsonfa697a92001-08-16 20:07:29 +0000611 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
612 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000613
Josh Coalsonfa697a92001-08-16 20:07:29 +0000614 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000615
Josh Coalsonfa697a92001-08-16 20:07:29 +0000616 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
617 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000618
Josh Coalsonfa697a92001-08-16 20:07:29 +0000619 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
620 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000621
Josh Coalsonfa697a92001-08-16 20:07:29 +0000622 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
623 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000624
Josh Coalsonfa697a92001-08-16 20:07:29 +0000625 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
626 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000627
Josh Coalsonfa697a92001-08-16 20:07:29 +0000628 if(encoder->protected_->bits_per_sample >= 32)
629 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 +0000630
Josh Coalson76c68bc2002-05-17 06:22:02 +0000631 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 +0000632 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000633
Josh Coalson0833f342002-07-15 05:31:55 +0000634 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000635 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000636
Josh Coalsonfa697a92001-08-16 20:07:29 +0000637 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
638 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000639
Josh Coalson20ac2c12002-08-30 05:47:14 +0000640 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
641 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
642
Josh Coalsonfa697a92001-08-16 20:07:29 +0000643 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
644 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000645
Josh Coalsonfa697a92001-08-16 20:07:29 +0000646 if(encoder->protected_->qlp_coeff_precision == 0) {
647 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000648 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
649 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000650 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 +0000651 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000652 else if(encoder->protected_->bits_per_sample == 16) {
653 if(encoder->protected_->blocksize <= 192)
654 encoder->protected_->qlp_coeff_precision = 7;
655 else if(encoder->protected_->blocksize <= 384)
656 encoder->protected_->qlp_coeff_precision = 8;
657 else if(encoder->protected_->blocksize <= 576)
658 encoder->protected_->qlp_coeff_precision = 9;
659 else if(encoder->protected_->blocksize <= 1152)
660 encoder->protected_->qlp_coeff_precision = 10;
661 else if(encoder->protected_->blocksize <= 2304)
662 encoder->protected_->qlp_coeff_precision = 11;
663 else if(encoder->protected_->blocksize <= 4608)
664 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000665 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000666 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000667 }
668 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000669 if(encoder->protected_->blocksize <= 384)
670 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
671 else if(encoder->protected_->blocksize <= 1152)
672 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
673 else
674 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000675 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000676 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000677 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000678 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 +0000679 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000680
Josh Coalsonfa697a92001-08-16 20:07:29 +0000681 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000682 if(
683 encoder->protected_->blocksize != 192 &&
684 encoder->protected_->blocksize != 576 &&
685 encoder->protected_->blocksize != 1152 &&
686 encoder->protected_->blocksize != 2304 &&
687 encoder->protected_->blocksize != 4608 &&
688 encoder->protected_->blocksize != 256 &&
689 encoder->protected_->blocksize != 512 &&
690 encoder->protected_->blocksize != 1024 &&
691 encoder->protected_->blocksize != 2048 &&
692 encoder->protected_->blocksize != 4096 &&
693 encoder->protected_->blocksize != 8192 &&
694 encoder->protected_->blocksize != 16384
695 )
Josh Coalsonfa697a92001-08-16 20:07:29 +0000696 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000697 if(
698 encoder->protected_->sample_rate != 8000 &&
699 encoder->protected_->sample_rate != 16000 &&
700 encoder->protected_->sample_rate != 22050 &&
701 encoder->protected_->sample_rate != 24000 &&
702 encoder->protected_->sample_rate != 32000 &&
703 encoder->protected_->sample_rate != 44100 &&
704 encoder->protected_->sample_rate != 48000 &&
705 encoder->protected_->sample_rate != 96000
706 )
707 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
708 if(
709 encoder->protected_->bits_per_sample != 8 &&
710 encoder->protected_->bits_per_sample != 12 &&
711 encoder->protected_->bits_per_sample != 16 &&
712 encoder->protected_->bits_per_sample != 20 &&
713 encoder->protected_->bits_per_sample != 24
714 )
715 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000716 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000717 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000718 }
719
Josh Coalsonfa697a92001-08-16 20:07:29 +0000720 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
721 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
722 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
723 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000724
Josh Coalson66075c12002-06-01 05:39:38 +0000725 /* validate metadata */
726 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
727 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000728 metadata_has_seektable = false;
729 metadata_has_vorbis_comment = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000730 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalsond0609472003-01-10 05:37:13 +0000731 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson66075c12002-06-01 05:39:38 +0000732 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
733 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000734 if(metadata_has_seektable) /* only one is allowed */
735 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
736 metadata_has_seektable = true;
Josh Coalson0833f342002-07-15 05:31:55 +0000737 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
Josh Coalson66075c12002-06-01 05:39:38 +0000738 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
739 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000740 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
741 if(metadata_has_vorbis_comment) /* only one is allowed */
742 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
743 metadata_has_vorbis_comment = true;
744 }
Josh Coalsone4869382002-11-15 05:41:48 +0000745 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000746 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
Josh Coalsone4869382002-11-15 05:41:48 +0000747 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
748 }
Josh Coalson66075c12002-06-01 05:39:38 +0000749 }
750
Josh Coalsonfa697a92001-08-16 20:07:29 +0000751 encoder->private_->input_capacity = 0;
752 for(i = 0; i < encoder->protected_->channels; i++) {
753 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000754#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000755 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000756#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000757 }
758 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000759 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000760#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000761 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000762#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000763 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000764#ifndef FLAC__INTEGER_ONLY_LIBRARY
765 for(i = 0; i < encoder->protected_->num_apodizations; i++)
766 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
767 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
768#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000769 for(i = 0; i < encoder->protected_->channels; i++) {
770 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
771 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
772 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000773 }
774 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000775 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
776 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
777 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000778 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000779 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
780 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
781 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000782#ifndef FLAC__INTEGER_ONLY_LIBRARY
783 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
784#else
785 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
786 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
787 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
788 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
789 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
790 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
791 encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF);
792#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000793 if(encoder->private_->loose_mid_side_stereo_frames == 0)
794 encoder->private_->loose_mid_side_stereo_frames = 1;
795 encoder->private_->loose_mid_side_stereo_frame_count = 0;
796 encoder->private_->current_sample_number = 0;
797 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000798
Josh Coalsonfa697a92001-08-16 20:07:29 +0000799 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
800 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? */
801 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000802
Josh Coalsoncf30f502001-05-23 20:57:44 +0000803 /*
804 * get the CPU info and set the function pointers
805 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000806 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000807 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000808#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000809 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000810#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000811 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000812#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000813 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000814 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 +0000815 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000816#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000817 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000818#ifndef FLAC__INTEGER_ONLY_LIBRARY
819# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000820 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000821# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000822 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000823# ifdef FLAC__HAS_NASM
824# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000825 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000826 if(encoder->protected_->max_lpc_order < 4)
827 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
828 else if(encoder->protected_->max_lpc_order < 8)
829 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
830 else if(encoder->protected_->max_lpc_order < 12)
831 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000832 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000833 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000834 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000835 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000836# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000837 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000838 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000839 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000840 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000841 if(encoder->private_->cpuinfo.data.ia32.mmx) {
842 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
843 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 +0000844 }
845 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000846 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
847 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 +0000848 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000849 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
850 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
851# endif /* FLAC__HAS_NASM */
852# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000853 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000854# endif /* !FLAC__NO_ASM */
855#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000856 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000857 if(encoder->private_->use_wide_by_block) {
858 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000859 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000860
Josh Coalson8395d022001-07-12 21:25:22 +0000861 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000862 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 +0000863
Josh Coalsonf1eff452002-07-31 07:05:33 +0000864 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000865 /* the above function sets the state for us in case of an error */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000866 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000867 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000868
869 if(!FLAC__bitbuffer_init(encoder->private_->frame))
870 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000871
872 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000873 * Set up the verify stuff if necessary
874 */
875 if(encoder->protected_->verify) {
876 /*
877 * First, set up the fifo which will hold the
878 * original signal to compare against
879 */
880 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
881 for(i = 0; i < encoder->protected_->channels; i++) {
882 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
883 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
884 }
885 encoder->private_->verify.input_fifo.tail = 0;
886
887 /*
888 * Now set up a stream decoder for verification
889 */
890 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
891 if(0 == encoder->private_->verify.decoder)
892 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
893
894 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
895 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
896 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
897 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
898 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
899 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
900 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
901 }
Josh Coalson589f8c72002-08-07 23:54:55 +0000902 encoder->private_->verify.error_stats.absolute_sample = 0;
903 encoder->private_->verify.error_stats.frame_number = 0;
904 encoder->private_->verify.error_stats.channel = 0;
905 encoder->private_->verify.error_stats.sample = 0;
906 encoder->private_->verify.error_stats.expected = 0;
907 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000908
909 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000910 * write the stream header
911 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000912 if(encoder->protected_->verify)
913 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000914 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000915 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000916 if(!write_bitbuffer_(encoder, 0)) {
917 /* the above function sets the state for us in case of an error */
918 return encoder->protected_->state;
919 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000920
Josh Coalson5c491a12002-08-01 06:39:40 +0000921 /*
922 * write the STREAMINFO metadata block
923 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000924 if(encoder->protected_->verify)
925 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000926 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000927 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000928 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
929 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
930 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
931 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
932 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
933 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
934 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
935 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
936 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
937 memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000938 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000939 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
940 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000941 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000942 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000943 if(!write_bitbuffer_(encoder, 0)) {
944 /* the above function sets the state for us in case of an error */
945 return encoder->protected_->state;
946 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000947
Josh Coalson5c491a12002-08-01 06:39:40 +0000948 /*
949 * Now that the STREAMINFO block is written, we can init this to an
950 * absurdly-high value...
951 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000952 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 +0000953 /* ... and clear this to 0 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000954 encoder->private_->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000955
Josh Coalson5c491a12002-08-01 06:39:40 +0000956 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000957 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
958 * if not, we will write an empty one (FLAC__add_metadata_block()
959 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +0000960 *
961 * WATCHOUT: libOggFLAC depends on us to write this block after the
962 * STREAMINFO since that's what the mapping requires. (In the case
Josh Coalson8ddf7fb2004-12-30 00:58:50 +0000963 * that metadata_has_vorbis_comment is true it will have already
Josh Coalson69cfda72004-09-10 00:38:21 +0000964 * insured that the metadata list is properly ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000965 */
966 if(!metadata_has_vorbis_comment) {
967 FLAC__StreamMetadata vorbis_comment;
968 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
969 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
970 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
971 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
972 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
973 vorbis_comment.data.vorbis_comment.num_comments = 0;
974 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson7424d2f2002-11-06 07:10:38 +0000975 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
976 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000977 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
978 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
979 if(!write_bitbuffer_(encoder, 0)) {
980 /* the above function sets the state for us in case of an error */
981 return encoder->protected_->state;
982 }
983 }
984
985 /*
Josh Coalson5c491a12002-08-01 06:39:40 +0000986 * write the user's metadata blocks
987 */
988 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
989 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000990 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
991 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +0000992 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
993 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000994 if(!write_bitbuffer_(encoder, 0)) {
995 /* the above function sets the state for us in case of an error */
996 return encoder->protected_->state;
997 }
Josh Coalson5c491a12002-08-01 06:39:40 +0000998 }
999
Josh Coalsond86e03b2002-08-03 21:56:15 +00001000 if(encoder->protected_->verify)
1001 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1002
Josh Coalsonfa697a92001-08-16 20:07:29 +00001003 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001004}
1005
Josh Coalson6afed9f2002-10-16 22:29:47 +00001006FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001007{
Josh Coalsonf1eff452002-07-31 07:05:33 +00001008 FLAC__ASSERT(0 != encoder);
Josh Coalson2b245f22002-08-07 17:10:50 +00001009
Josh Coalsonfa697a92001-08-16 20:07:29 +00001010 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001011 return;
Josh Coalson2b245f22002-08-07 17:10:50 +00001012
Josh Coalson3262b0d2002-08-14 20:58:42 +00001013 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001014 if(encoder->private_->current_sample_number != 0) {
1015 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1016 process_frame_(encoder, true); /* true => is last frame */
1017 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001018 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001019
Josh Coalson3e7a96e2004-07-23 05:18:22 +00001020 FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001021
Josh Coalson3262b0d2002-08-14 20:58:42 +00001022 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001023 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
1024 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001025
Josh Coalsond86e03b2002-08-03 21:56:15 +00001026 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
1027 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1028
Josh Coalsonf1eff452002-07-31 07:05:33 +00001029 free_(encoder);
1030 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001031
Josh Coalsonfa697a92001-08-16 20:07:29 +00001032 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001033}
1034
Josh Coalson6afed9f2002-10-16 22:29:47 +00001035FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001036{
1037 FLAC__ASSERT(0 != encoder);
1038 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1039 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001040#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001041 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001042#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001043 return true;
1044}
1045
Josh Coalson6afed9f2002-10-16 22:29:47 +00001046FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001047{
Josh Coalson92031602002-07-24 06:02:11 +00001048 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001049 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001050 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001051 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001052 return true;
1053}
1054
Josh Coalson6afed9f2002-10-16 22:29:47 +00001055FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001056{
Josh Coalson92031602002-07-24 06:02:11 +00001057 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001058 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001059 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001060 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001061 return true;
1062}
1063
Josh Coalson6afed9f2002-10-16 22:29:47 +00001064FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001065{
Josh Coalson92031602002-07-24 06:02:11 +00001066 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001067 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001068 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001069 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001070 return true;
1071}
1072
Josh Coalson6afed9f2002-10-16 22:29:47 +00001073FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001074{
Josh Coalson92031602002-07-24 06:02:11 +00001075 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001076 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001077 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001078 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001079 return true;
1080}
1081
Josh Coalson6afed9f2002-10-16 22:29:47 +00001082FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001083{
Josh Coalson92031602002-07-24 06:02:11 +00001084 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001085 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001086 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001087 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001088 return true;
1089}
1090
Josh Coalson6afed9f2002-10-16 22:29:47 +00001091FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001092{
Josh Coalson92031602002-07-24 06:02:11 +00001093 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001094 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001095 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001096 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001097 return true;
1098}
1099
Josh Coalson6afed9f2002-10-16 22:29:47 +00001100FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001101{
Josh Coalson92031602002-07-24 06:02:11 +00001102 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001103 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001104 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001105 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001106 return true;
1107}
1108
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001109FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1110{
1111 FLAC__ASSERT(0 != encoder);
1112 FLAC__ASSERT(0 != specification);
1113 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1114 return false;
1115#ifdef FLAC__INTEGER_ONLY_LIBRARY
1116 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1117#else
1118 encoder->protected_->num_apodizations = 0;
1119 while(1) {
1120 const char *s = strchr(specification, ';');
1121 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1122 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1123 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1124 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1125 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1126 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1127 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1128 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1129 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1130 else if(n==6 && 0 == strncmp("connes" , specification, n))
1131 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1132 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1133 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1134 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1135 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1136 if (stddev > 0.0 && stddev <= 0.5) {
1137 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1138 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1139 }
1140 }
1141 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1142 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1143 else if(n==4 && 0 == strncmp("hann" , specification, n))
1144 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1145 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1146 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1147 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1148 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1149 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1150 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1151 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1152 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1153 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1154 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1155 if (p >= 0.0 && p <= 1.0) {
1156 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1157 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1158 }
1159 }
1160 else if(n==5 && 0 == strncmp("welch" , specification, n))
1161 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1162 if (encoder->protected_->num_apodizations == 32)
1163 break;
1164 if (s)
1165 specification = s+1;
1166 else
1167 break;
1168 }
1169 if(encoder->protected_->num_apodizations == 0) {
1170 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001171 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1172 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001173 }
1174#ifdef WINDOW_DEBUG_OUTPUT
1175{unsigned n;for(n=0;n<encoder->protected_->num_apodizations;n++)fprintf(stderr,"@@@@@@ parsed apodization[%zu]: %s\n",n,winstr[encoder->protected_->apodizations[n].type]);}
1176#endif
1177#endif
1178 return true;
1179}
1180
Josh Coalson6afed9f2002-10-16 22:29:47 +00001181FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001182{
Josh Coalson92031602002-07-24 06:02:11 +00001183 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001184 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001185 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001186 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001187 return true;
1188}
1189
Josh Coalson6afed9f2002-10-16 22:29:47 +00001190FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001191{
Josh Coalson92031602002-07-24 06:02:11 +00001192 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001193 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001194 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001195 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001196 return true;
1197}
1198
Josh Coalson6afed9f2002-10-16 22:29:47 +00001199FLAC_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 +00001200{
Josh Coalson92031602002-07-24 06:02:11 +00001201 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001202 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001203 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001204 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001205 return true;
1206}
1207
Josh Coalson6afed9f2002-10-16 22:29:47 +00001208FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001209{
Josh Coalson92031602002-07-24 06:02:11 +00001210 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001211 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001212 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001213#if 0
1214 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001215 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001216#else
1217 (void)value;
1218#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001219 return true;
1220}
1221
Josh Coalson6afed9f2002-10-16 22:29:47 +00001222FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001223{
Josh Coalson92031602002-07-24 06:02:11 +00001224 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001225 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001226 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001227 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001228 return true;
1229}
1230
Josh Coalson6afed9f2002-10-16 22:29:47 +00001231FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001232{
Josh Coalson92031602002-07-24 06:02:11 +00001233 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001234 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001235 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001236 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001237 return true;
1238}
1239
Josh Coalson6afed9f2002-10-16 22:29:47 +00001240FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001241{
Josh Coalson92031602002-07-24 06:02:11 +00001242 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001243 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001244 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001245 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001246 return true;
1247}
1248
Josh Coalson6afed9f2002-10-16 22:29:47 +00001249FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001250{
Josh Coalson92031602002-07-24 06:02:11 +00001251 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001252 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001253 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001254#if 0
1255 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001256 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001257#else
1258 (void)value;
1259#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001260 return true;
1261}
1262
Josh Coalson6afed9f2002-10-16 22:29:47 +00001263FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001264{
Josh Coalson92031602002-07-24 06:02:11 +00001265 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001266 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001267 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001268 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001269 return true;
1270}
1271
Josh Coalson6afed9f2002-10-16 22:29:47 +00001272FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001273{
Josh Coalson92031602002-07-24 06:02:11 +00001274 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001275 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001276 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001277 encoder->protected_->metadata = metadata;
1278 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson00e53872001-06-16 07:32:25 +00001279 return true;
1280}
1281
Josh Coalson6afed9f2002-10-16 22:29:47 +00001282FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001283{
Josh Coalson92031602002-07-24 06:02:11 +00001284 FLAC__ASSERT(0 != encoder);
1285 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001286 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001287 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001288 encoder->private_->write_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001289 return true;
1290}
1291
Josh Coalson6afed9f2002-10-16 22:29:47 +00001292FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001293{
Josh Coalson92031602002-07-24 06:02:11 +00001294 FLAC__ASSERT(0 != encoder);
1295 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001296 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001297 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001298 encoder->private_->metadata_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001299 return true;
1300}
1301
Josh Coalson6afed9f2002-10-16 22:29:47 +00001302FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +00001303{
Josh Coalson92031602002-07-24 06:02:11 +00001304 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001305 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001306 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001307 encoder->private_->client_data = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001308 return true;
1309}
1310
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001311/*
1312 * These three functions are not static, but not publically exposed in
1313 * include/FLAC/ either. They are used by the test suite.
1314 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001315FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001316{
1317 FLAC__ASSERT(0 != encoder);
1318 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1319 return false;
1320 encoder->private_->disable_constant_subframes = value;
1321 return true;
1322}
1323
Josh Coalson6afed9f2002-10-16 22:29:47 +00001324FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001325{
1326 FLAC__ASSERT(0 != encoder);
1327 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1328 return false;
1329 encoder->private_->disable_fixed_subframes = value;
1330 return true;
1331}
1332
Josh Coalson6afed9f2002-10-16 22:29:47 +00001333FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001334{
1335 FLAC__ASSERT(0 != encoder);
1336 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1337 return false;
1338 encoder->private_->disable_verbatim_subframes = value;
1339 return true;
1340}
1341
Josh Coalson6afed9f2002-10-16 22:29:47 +00001342FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001343{
Josh Coalson92031602002-07-24 06:02:11 +00001344 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001345 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001346}
1347
Josh Coalson6afed9f2002-10-16 22:29:47 +00001348FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001349{
1350 FLAC__ASSERT(0 != encoder);
1351 if(encoder->protected_->verify)
1352 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1353 else
1354 return FLAC__STREAM_DECODER_UNINITIALIZED;
1355}
1356
Josh Coalson02954222002-11-08 06:16:31 +00001357FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1358{
1359 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1360 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1361 else
Josh Coalson807140d2003-09-24 22:10:51 +00001362 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001363}
1364
Josh Coalson6afed9f2002-10-16 22:29:47 +00001365FLAC_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 +00001366{
1367 FLAC__ASSERT(0 != encoder);
1368 if(0 != absolute_sample)
1369 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1370 if(0 != frame_number)
1371 *frame_number = encoder->private_->verify.error_stats.frame_number;
1372 if(0 != channel)
1373 *channel = encoder->private_->verify.error_stats.channel;
1374 if(0 != sample)
1375 *sample = encoder->private_->verify.error_stats.sample;
1376 if(0 != expected)
1377 *expected = encoder->private_->verify.error_stats.expected;
1378 if(0 != got)
1379 *got = encoder->private_->verify.error_stats.got;
1380}
1381
Josh Coalson6afed9f2002-10-16 22:29:47 +00001382FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001383{
1384 FLAC__ASSERT(0 != encoder);
1385 return encoder->protected_->verify;
1386}
1387
Josh Coalson6afed9f2002-10-16 22:29:47 +00001388FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001389{
Josh Coalson92031602002-07-24 06:02:11 +00001390 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001391 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001392}
1393
Josh Coalson6afed9f2002-10-16 22:29:47 +00001394FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001395{
Josh Coalson92031602002-07-24 06:02:11 +00001396 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001397 return encoder->protected_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001398}
1399
Josh Coalson6afed9f2002-10-16 22:29:47 +00001400FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001401{
Josh Coalson92031602002-07-24 06:02:11 +00001402 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001403 return encoder->protected_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001404}
1405
Josh Coalson6afed9f2002-10-16 22:29:47 +00001406FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001407{
Josh Coalson92031602002-07-24 06:02:11 +00001408 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001409 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001410}
1411
Josh Coalson6afed9f2002-10-16 22:29:47 +00001412FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001413{
Josh Coalson92031602002-07-24 06:02:11 +00001414 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001415 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001416}
1417
Josh Coalson6afed9f2002-10-16 22:29:47 +00001418FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001419{
Josh Coalson92031602002-07-24 06:02:11 +00001420 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001421 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001422}
1423
Josh Coalson6afed9f2002-10-16 22:29:47 +00001424FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001425{
Josh Coalson92031602002-07-24 06:02:11 +00001426 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001427 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001428}
1429
Josh Coalson6afed9f2002-10-16 22:29:47 +00001430FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001431{
Josh Coalson92031602002-07-24 06:02:11 +00001432 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001433 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001434}
1435
Josh Coalson6afed9f2002-10-16 22:29:47 +00001436FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001437{
Josh Coalson92031602002-07-24 06:02:11 +00001438 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001439 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001440}
1441
Josh Coalson6afed9f2002-10-16 22:29:47 +00001442FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001443{
Josh Coalson92031602002-07-24 06:02:11 +00001444 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001445 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001446}
1447
Josh Coalson6afed9f2002-10-16 22:29:47 +00001448FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001449{
Josh Coalson92031602002-07-24 06:02:11 +00001450 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001451 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001452}
1453
Josh Coalson6afed9f2002-10-16 22:29:47 +00001454FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001455{
Josh Coalson92031602002-07-24 06:02:11 +00001456 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001457 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001458}
1459
Josh Coalson6afed9f2002-10-16 22:29:47 +00001460FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001461{
Josh Coalson92031602002-07-24 06:02:11 +00001462 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001463 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001464}
1465
Josh Coalson6afed9f2002-10-16 22:29:47 +00001466FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001467{
Josh Coalson92031602002-07-24 06:02:11 +00001468 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001469 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001470}
1471
Josh Coalson6afed9f2002-10-16 22:29:47 +00001472FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001473{
Josh Coalson92031602002-07-24 06:02:11 +00001474 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001475 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001476}
1477
Josh Coalson6afed9f2002-10-16 22:29:47 +00001478FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001479{
1480 FLAC__ASSERT(0 != encoder);
1481 return encoder->protected_->total_samples_estimate;
1482}
1483
Josh Coalson6afed9f2002-10-16 22:29:47 +00001484FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001485{
1486 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001487 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001488 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001489
Josh Coalsonf1eff452002-07-31 07:05:33 +00001490 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001491 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001492
1493 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001494 /*
1495 * we have several flavors of the same basic loop, optimized for
1496 * different conditions:
1497 */
1498 if(encoder->protected_->max_lpc_order > 0) {
1499 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1500 /*
1501 * stereo coding: unroll channel loop
1502 * with LPC: calculate floating point version of signal
1503 */
1504 do {
1505 if(encoder->protected_->verify)
1506 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00001507
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001508 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1509 x = mid = side = buffer[0][j];
1510 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001511#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001512 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001513#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001514 x = buffer[1][j];
1515 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001516#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001517 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001518#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001519 mid += x;
1520 side -= x;
1521 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1522 encoder->private_->integer_signal_mid_side[1][i] = side;
1523 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001524#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001525 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1526 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001527#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001528 encoder->private_->current_sample_number++;
1529 }
1530 if(i == blocksize) {
1531 if(!process_frame_(encoder, false)) /* false => not last frame */
1532 return false;
1533 }
1534 } while(j < samples);
1535 }
1536 else {
1537 /*
1538 * independent channel coding: buffer each channel in inner loop
1539 * with LPC: calculate floating point version of signal
1540 */
1541 do {
1542 if(encoder->protected_->verify)
1543 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1544
1545 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1546 for(channel = 0; channel < channels; channel++) {
1547 x = buffer[channel][j];
1548 encoder->private_->integer_signal[channel][i] = x;
1549#ifndef FLAC__INTEGER_ONLY_LIBRARY
1550 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1551#endif
1552 }
1553 encoder->private_->current_sample_number++;
1554 }
1555 if(i == blocksize) {
1556 if(!process_frame_(encoder, false)) /* false => not last frame */
1557 return false;
1558 }
1559 } while(j < samples);
1560 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001561 }
1562 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001563 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1564 /*
1565 * stereo coding: unroll channel loop
1566 * without LPC: no need to calculate floating point version of signal
1567 */
1568 do {
1569 if(encoder->protected_->verify)
1570 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00001571
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001572 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1573 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1574 x = buffer[1][j];
1575 encoder->private_->integer_signal[1][i] = x;
1576 mid += x;
1577 side -= x;
1578 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1579 encoder->private_->integer_signal_mid_side[1][i] = side;
1580 encoder->private_->integer_signal_mid_side[0][i] = mid;
1581 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001582 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001583 if(i == blocksize) {
1584 if(!process_frame_(encoder, false)) /* false => not last frame */
1585 return false;
1586 }
1587 } while(j < samples);
1588 }
1589 else {
1590 /*
1591 * independent channel coding: buffer each channel in inner loop
1592 * without LPC: no need to calculate floating point version of signal
1593 */
1594 do {
1595 if(encoder->protected_->verify)
1596 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1597
1598 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1599 for(channel = 0; channel < channels; channel++)
1600 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1601 encoder->private_->current_sample_number++;
1602 }
1603 if(i == blocksize) {
1604 if(!process_frame_(encoder, false)) /* false => not last frame */
1605 return false;
1606 }
1607 } while(j < samples);
1608 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001609 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001610
1611 return true;
1612}
1613
Josh Coalson6afed9f2002-10-16 22:29:47 +00001614FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001615{
1616 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001617 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001618 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001619
Josh Coalsonf1eff452002-07-31 07:05:33 +00001620 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001621 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001622
1623 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001624 /*
1625 * we have several flavors of the same basic loop, optimized for
1626 * different conditions:
1627 */
1628 if(encoder->protected_->max_lpc_order > 0) {
1629 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1630 /*
1631 * stereo coding: unroll channel loop
1632 * with LPC: calculate floating point version of signal
1633 */
1634 do {
1635 if(encoder->protected_->verify)
1636 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00001637
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001638 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1639 x = mid = side = buffer[k++];
1640 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001641#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001642 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001643#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001644 x = buffer[k++];
1645 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001646#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001647 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001648#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001649 mid += x;
1650 side -= x;
1651 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1652 encoder->private_->integer_signal_mid_side[1][i] = side;
1653 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001654#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001655 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1656 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001657#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001658 encoder->private_->current_sample_number++;
1659 }
1660 if(i == blocksize) {
1661 if(!process_frame_(encoder, false)) /* false => not last frame */
1662 return false;
1663 }
1664 } while(j < samples);
1665 }
1666 else {
1667 /*
1668 * independent channel coding: buffer each channel in inner loop
1669 * with LPC: calculate floating point version of signal
1670 */
1671 do {
1672 if(encoder->protected_->verify)
1673 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1674
1675 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1676 for(channel = 0; channel < channels; channel++) {
1677 x = buffer[k++];
1678 encoder->private_->integer_signal[channel][i] = x;
1679#ifndef FLAC__INTEGER_ONLY_LIBRARY
1680 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1681#endif
1682 }
1683 encoder->private_->current_sample_number++;
1684 }
1685 if(i == blocksize) {
1686 if(!process_frame_(encoder, false)) /* false => not last frame */
1687 return false;
1688 }
1689 } while(j < samples);
1690 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001691 }
1692 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001693 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1694 /*
1695 * stereo coding: unroll channel loop
1696 * without LPC: no need to calculate floating point version of signal
1697 */
1698 do {
1699 if(encoder->protected_->verify)
1700 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00001701
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001702 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1703 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00001704 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001705 encoder->private_->integer_signal[1][i] = x;
1706 mid += x;
1707 side -= x;
1708 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1709 encoder->private_->integer_signal_mid_side[1][i] = side;
1710 encoder->private_->integer_signal_mid_side[0][i] = mid;
1711 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001712 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001713 if(i == blocksize) {
1714 if(!process_frame_(encoder, false)) /* false => not last frame */
1715 return false;
1716 }
1717 } while(j < samples);
1718 }
1719 else {
1720 /*
1721 * independent channel coding: buffer each channel in inner loop
1722 * without LPC: no need to calculate floating point version of signal
1723 */
1724 do {
1725 if(encoder->protected_->verify)
1726 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1727
1728 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1729 for(channel = 0; channel < channels; channel++)
1730 encoder->private_->integer_signal[channel][i] = buffer[k++];
1731 encoder->private_->current_sample_number++;
1732 }
1733 if(i == blocksize) {
1734 if(!process_frame_(encoder, false)) /* false => not last frame */
1735 return false;
1736 }
1737 } while(j < samples);
1738 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001739 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001740
1741 return true;
1742}
1743
Josh Coalsonf1eff452002-07-31 07:05:33 +00001744/***********************************************************************
1745 *
1746 * Private class methods
1747 *
1748 ***********************************************************************/
1749
1750void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00001751{
1752 FLAC__ASSERT(0 != encoder);
1753
Josh Coalson47c7b142005-01-29 06:08:58 +00001754#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1755 encoder->protected_->verify = true;
1756#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00001757 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001758#endif
Josh Coalson92031602002-07-24 06:02:11 +00001759 encoder->protected_->streamable_subset = true;
1760 encoder->protected_->do_mid_side_stereo = false;
1761 encoder->protected_->loose_mid_side_stereo = false;
1762 encoder->protected_->channels = 2;
1763 encoder->protected_->bits_per_sample = 16;
1764 encoder->protected_->sample_rate = 44100;
1765 encoder->protected_->blocksize = 1152;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001766#ifndef FLAC__INTEGER_ONLY_LIBRARY
1767 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001768 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1769 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001770#endif
Josh Coalson92031602002-07-24 06:02:11 +00001771 encoder->protected_->max_lpc_order = 0;
1772 encoder->protected_->qlp_coeff_precision = 0;
1773 encoder->protected_->do_qlp_coeff_prec_search = false;
1774 encoder->protected_->do_exhaustive_model_search = false;
1775 encoder->protected_->do_escape_coding = false;
1776 encoder->protected_->min_residual_partition_order = 0;
1777 encoder->protected_->max_residual_partition_order = 0;
1778 encoder->protected_->rice_parameter_search_dist = 0;
1779 encoder->protected_->total_samples_estimate = 0;
1780 encoder->protected_->metadata = 0;
1781 encoder->protected_->num_metadata_blocks = 0;
1782
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001783 encoder->private_->disable_constant_subframes = false;
1784 encoder->private_->disable_fixed_subframes = false;
1785 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson92031602002-07-24 06:02:11 +00001786 encoder->private_->write_callback = 0;
1787 encoder->private_->metadata_callback = 0;
1788 encoder->private_->client_data = 0;
1789}
1790
Josh Coalsonf1eff452002-07-31 07:05:33 +00001791void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00001792{
1793 unsigned i, channel;
1794
Josh Coalsonf1eff452002-07-31 07:05:33 +00001795 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00001796 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001797 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001798 free(encoder->private_->integer_signal_unaligned[i]);
1799 encoder->private_->integer_signal_unaligned[i] = 0;
1800 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001801#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00001802 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001803 free(encoder->private_->real_signal_unaligned[i]);
1804 encoder->private_->real_signal_unaligned[i] = 0;
1805 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001806#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001807 }
1808 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001809 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001810 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1811 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1812 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001813#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00001814 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001815 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1816 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1817 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001818#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001819 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001820#ifndef FLAC__INTEGER_ONLY_LIBRARY
1821 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
1822 if(0 != encoder->private_->window_unaligned[i]) {
1823 free(encoder->private_->window_unaligned[i]);
1824 encoder->private_->window_unaligned[i] = 0;
1825 }
1826 }
1827 if(0 != encoder->private_->windowed_signal_unaligned) {
1828 free(encoder->private_->windowed_signal_unaligned);
1829 encoder->private_->windowed_signal_unaligned = 0;
1830 }
1831#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001832 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1833 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001834 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001835 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1836 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1837 }
1838 }
1839 }
1840 for(channel = 0; channel < 2; channel++) {
1841 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001842 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001843 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1844 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1845 }
1846 }
1847 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001848 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001849 free(encoder->private_->abs_residual_unaligned);
1850 encoder->private_->abs_residual_unaligned = 0;
1851 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001852 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001853 free(encoder->private_->abs_residual_partition_sums_unaligned);
1854 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1855 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001856 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001857 free(encoder->private_->raw_bits_per_partition_unaligned);
1858 encoder->private_->raw_bits_per_partition_unaligned = 0;
1859 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001860 if(encoder->protected_->verify) {
1861 for(i = 0; i < encoder->protected_->channels; i++) {
1862 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1863 free(encoder->private_->verify.input_fifo.data[i]);
1864 encoder->private_->verify.input_fifo.data[i] = 0;
1865 }
1866 }
1867 }
Josh Coalson639aeb02002-07-25 05:38:23 +00001868 FLAC__bitbuffer_free(encoder->private_->frame);
1869}
1870
Josh Coalsonf1eff452002-07-31 07:05:33 +00001871FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001872{
Josh Coalson77e3f312001-06-23 03:03:24 +00001873 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00001874 unsigned i, channel;
1875
1876 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001877 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1878 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00001879
1880 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001881 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00001882 return true;
1883
1884 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00001885
Josh Coalsonc9c0d132002-10-04 05:29:05 +00001886 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1887 * requires that the input arrays (in our case the integer signals)
1888 * have a buffer of up to 3 zeroes in front (at negative indices) for
1889 * alignment purposes; we use 4 to keep the data well-aligned.
1890 */
Josh Coalson8395d022001-07-12 21:25:22 +00001891
Josh Coalsonfa697a92001-08-16 20:07:29 +00001892 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1893 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001894#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001895 if(encoder->protected_->max_lpc_order > 0)
1896 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001897#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00001898 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1899 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001900 }
1901 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001902 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001903#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001904 if(encoder->protected_->max_lpc_order > 0)
1905 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001906#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00001907 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1908 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001909 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001910#ifndef FLAC__INTEGER_ONLY_LIBRARY
1911 if(ok && encoder->protected_->max_lpc_order > 0) {
1912 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
1913 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
1914 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
1915 }
1916#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00001917 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001918 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001919 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 +00001920 }
1921 }
1922 for(channel = 0; ok && channel < 2; channel++) {
1923 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001924 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 +00001925 }
1926 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001927 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1928 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 */
1929 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1930 if(encoder->protected_->do_escape_coding)
1931 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 +00001932
1933 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001934 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00001935 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001936 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00001937
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001938#ifndef FLAC__INTEGER_ONLY_LIBRARY
1939 if(ok && encoder->protected_->max_lpc_order > 0) {
1940 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
1941 switch(encoder->protected_->apodizations[i].type) {
1942 case FLAC__APODIZATION_BARTLETT:
1943 FLAC__window_bartlett(encoder->private_->window[i], new_size);
1944 break;
1945 case FLAC__APODIZATION_BARTLETT_HANN:
1946 FLAC__window_bartlett_hann(encoder->private_->window[i], new_size);
1947 break;
1948 case FLAC__APODIZATION_BLACKMAN:
1949 FLAC__window_blackman(encoder->private_->window[i], new_size);
1950 break;
1951 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
1952 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_size);
1953 break;
1954 case FLAC__APODIZATION_CONNES:
1955 FLAC__window_connes(encoder->private_->window[i], new_size);
1956 break;
1957 case FLAC__APODIZATION_FLATTOP:
1958 FLAC__window_flattop(encoder->private_->window[i], new_size);
1959 break;
1960 case FLAC__APODIZATION_GAUSS:
1961 FLAC__window_gauss(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.gauss.stddev);
1962 break;
1963 case FLAC__APODIZATION_HAMMING:
1964 FLAC__window_hamming(encoder->private_->window[i], new_size);
1965 break;
1966 case FLAC__APODIZATION_HANN:
1967 FLAC__window_hann(encoder->private_->window[i], new_size);
1968 break;
1969 case FLAC__APODIZATION_KAISER_BESSEL:
1970 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_size);
1971 break;
1972 case FLAC__APODIZATION_NUTTALL:
1973 FLAC__window_nuttall(encoder->private_->window[i], new_size);
1974 break;
1975 case FLAC__APODIZATION_RECTANGLE:
1976 FLAC__window_rectangle(encoder->private_->window[i], new_size);
1977 break;
1978 case FLAC__APODIZATION_TRIANGLE:
1979 FLAC__window_triangle(encoder->private_->window[i], new_size);
1980 break;
1981 case FLAC__APODIZATION_TUKEY:
1982 FLAC__window_tukey(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.tukey.p);
1983 break;
1984 case FLAC__APODIZATION_WELCH:
1985 FLAC__window_welch(encoder->private_->window[i], new_size);
1986 break;
1987 default:
1988 FLAC__ASSERT(0);
1989 /* double protection */
Josh Coalsondf598452006-04-28 00:13:34 +00001990 FLAC__window_hann(encoder->private_->window[i], new_size);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001991 break;
1992 }
1993 }
1994 }
1995#endif
1996
Josh Coalson0a15c142001-06-13 17:59:57 +00001997 return ok;
1998}
1999
Josh Coalsond86e03b2002-08-03 21:56:15 +00002000FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00002001{
2002 const FLAC__byte *buffer;
2003 unsigned bytes;
2004
2005 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2006
2007 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2008
Josh Coalsond86e03b2002-08-03 21:56:15 +00002009 if(encoder->protected_->verify) {
2010 encoder->private_->verify.output.data = buffer;
2011 encoder->private_->verify.output.bytes = bytes;
2012 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2013 encoder->private_->verify.needs_magic_hack = true;
2014 }
2015 else {
2016 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2017 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2018 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2019 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2020 return false;
2021 }
2022 }
2023 }
2024
2025 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002026 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalsond86e03b2002-08-03 21:56:15 +00002027 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalson5c491a12002-08-01 06:39:40 +00002028 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002029 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002030
2031 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2032
Josh Coalsond86e03b2002-08-03 21:56:15 +00002033 if(samples > 0) {
2034 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
2035 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
2036 }
2037
Josh Coalson5c491a12002-08-01 06:39:40 +00002038 return true;
2039}
2040
Josh Coalsonf1eff452002-07-31 07:05:33 +00002041FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00002042{
Josh Coalsonfa697a92001-08-16 20:07:29 +00002043 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002044
2045 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002046 * Accumulate raw signal to the MD5 signature
2047 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00002048 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 +00002049 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002050 return false;
2051 }
2052
2053 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002054 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002055 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00002056 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002057 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002058 return false;
2059 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002060
2061 /*
2062 * Zero-pad the frame to a byte_boundary
2063 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002064 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002065 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002066 return false;
2067 }
2068
2069 /*
Josh Coalson215af572001-03-27 01:15:58 +00002070 * CRC-16 the whole thing
2071 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002072 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2073 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 +00002074
2075 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002076 * Write it
2077 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00002078 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
2079 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002080 return false;
2081 }
2082
2083 /*
2084 * Get ready for the next frame
2085 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002086 encoder->private_->current_sample_number = 0;
2087 encoder->private_->current_frame_number++;
2088 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002089
2090 return true;
2091}
2092
Josh Coalsonf1eff452002-07-31 07:05:33 +00002093FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002094{
2095 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002096 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002097 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002098
2099 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00002100 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00002101 */
2102 if(is_last_frame) {
2103 max_partition_order = 0;
2104 }
2105 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002106 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2107 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002108 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002109 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002110
Josh Coalsonfa697a92001-08-16 20:07:29 +00002111 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 +00002112
Josh Coalson94e02cd2001-01-25 10:41:06 +00002113 /*
2114 * Setup the frame
2115 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002116 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002117 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002118 return false;
2119 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002120 frame_header.blocksize = encoder->protected_->blocksize;
2121 frame_header.sample_rate = encoder->protected_->sample_rate;
2122 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002123 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002124 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002125 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002126 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002127
2128 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002129 * Figure out what channel assignments to try
2130 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002131 if(encoder->protected_->do_mid_side_stereo) {
2132 if(encoder->protected_->loose_mid_side_stereo) {
2133 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002134 do_independent = true;
2135 do_mid_side = true;
2136 }
2137 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002138 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002139 do_mid_side = !do_independent;
2140 }
2141 }
2142 else {
2143 do_independent = true;
2144 do_mid_side = true;
2145 }
2146 }
2147 else {
2148 do_independent = true;
2149 do_mid_side = false;
2150 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002151
Josh Coalson1b689822001-05-31 20:11:02 +00002152 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002153
2154 /*
Josh Coalson82b73242001-03-28 22:17:05 +00002155 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00002156 */
2157 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002158 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002159 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002160 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
2161 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00002162 }
Josh Coalson859bc542001-03-27 22:22:27 +00002163 }
2164 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002165 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00002166 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002167 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002168 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
2169 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00002170 }
Josh Coalson859bc542001-03-27 22:22:27 +00002171 }
2172
2173 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002174 * First do a normal encoding pass of each independent channel
2175 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002176 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002177 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002178 if(!
2179 process_subframe_(
2180 encoder,
2181 min_partition_order,
2182 max_partition_order,
2183 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002184 &frame_header,
2185 encoder->private_->subframe_bps[channel],
2186 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002187#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002188 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002189#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002190 encoder->private_->subframe_workspace_ptr[channel],
2191 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
2192 encoder->private_->residual_workspace[channel],
2193 encoder->private_->best_subframe+channel,
2194 encoder->private_->best_subframe_bits+channel
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002195#ifdef WINDOW_DEBUG_OUTPUT
2196 ,channel
2197#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002198 )
2199 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002200 return false;
2201 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002202 }
2203
2204 /*
2205 * Now do mid and side channels if requested
2206 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002207 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002208 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002209
2210 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002211 if(!
2212 process_subframe_(
2213 encoder,
2214 min_partition_order,
2215 max_partition_order,
2216 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002217 &frame_header,
2218 encoder->private_->subframe_bps_mid_side[channel],
2219 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002220#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002221 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002222#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002223 encoder->private_->subframe_workspace_ptr_mid_side[channel],
2224 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
2225 encoder->private_->residual_workspace_mid_side[channel],
2226 encoder->private_->best_subframe_mid_side+channel,
2227 encoder->private_->best_subframe_bits_mid_side+channel
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002228#ifdef WINDOW_DEBUG_OUTPUT
2229 ,channel
2230#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002231 )
2232 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00002233 return false;
2234 }
2235 }
2236
2237 /*
2238 * Compose the frame bitbuffer
2239 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002240 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00002241 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
2242 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002243 FLAC__ChannelAssignment channel_assignment;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002244#ifdef WINDOW_DEBUG_OUTPUT
2245 unsigned left_bits = 0, right_bits = 0;
2246#endif
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002247
Josh Coalsonfa697a92001-08-16 20:07:29 +00002248 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002249
Josh Coalsonfa697a92001-08-16 20:07:29 +00002250 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
2251 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 +00002252 }
2253 else {
2254 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
2255 unsigned min_bits;
2256 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002257
Josh Coalson1b689822001-05-31 20:11:02 +00002258 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002259
2260 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002261 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
2262 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
2263 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
2264 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 +00002265
Josh Coalson7424d2f2002-11-06 07:10:38 +00002266 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 +00002267 if(bits[ca] < min_bits) {
2268 min_bits = bits[ca];
2269 channel_assignment = ca;
2270 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002271 }
2272 }
2273
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002274 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002275
Josh Coalson3e7a96e2004-07-23 05:18:22 +00002276 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002277 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002278 return false;
2279 }
2280
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002281 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002282 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002283 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2284 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002285#ifdef WINDOW_DEBUG_OUTPUT
2286 left_bits = encoder->private_->best_subframe_bits [0];
2287 right_bits = encoder->private_->best_subframe_bits [1];
2288#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002289 break;
2290 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002291 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2292 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002293#ifdef WINDOW_DEBUG_OUTPUT
2294 left_bits = encoder->private_->best_subframe_bits [0];
2295 right_bits = encoder->private_->best_subframe_bits_mid_side [1];
2296#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002297 break;
2298 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002299 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2300 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002301#ifdef WINDOW_DEBUG_OUTPUT
2302 left_bits = encoder->private_->best_subframe_bits_mid_side [1];
2303 right_bits = encoder->private_->best_subframe_bits [1];
2304#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002305 break;
2306 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002307 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
2308 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002309#ifdef WINDOW_DEBUG_OUTPUT
2310 left_bits = encoder->private_->best_subframe_bits_mid_side [0];
2311 right_bits = encoder->private_->best_subframe_bits_mid_side [1];
2312#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002313 break;
2314 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002315 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002316 }
Josh Coalson82b73242001-03-28 22:17:05 +00002317
2318 switch(channel_assignment) {
2319 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002320 left_bps = encoder->private_->subframe_bps [0];
2321 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00002322 break;
2323 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002324 left_bps = encoder->private_->subframe_bps [0];
2325 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00002326 break;
2327 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002328 left_bps = encoder->private_->subframe_bps_mid_side[1];
2329 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00002330 break;
2331 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002332 left_bps = encoder->private_->subframe_bps_mid_side[0];
2333 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00002334 break;
2335 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002336 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00002337 }
2338
2339 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002340#ifdef WINDOW_DEBUG_OUTPUT
2341 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame, left_bits))
2342 return false;
2343 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame, right_bits))
2344 return false;
2345#else
Josh Coalsonf1eff452002-07-31 07:05:33 +00002346 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00002347 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00002348 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00002349 return false;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002350#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002351 }
2352 else {
Josh Coalson3e7a96e2004-07-23 05:18:22 +00002353 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002354 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002355 return false;
2356 }
2357
Josh Coalsonfa697a92001-08-16 20:07:29 +00002358 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00002359#ifdef WINDOW_DEBUG_OUTPUT
2360 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame, encoder->private_->best_subframe_bits[channel]))
2361#else
2362 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame))
2363#endif
2364 {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002365 /* the above function sets the state for us in case of an error */
2366 return false;
2367 }
2368 }
2369 }
2370
Josh Coalsonfa697a92001-08-16 20:07:29 +00002371 if(encoder->protected_->loose_mid_side_stereo) {
2372 encoder->private_->loose_mid_side_stereo_frame_count++;
2373 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
2374 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002375 }
2376
Josh Coalsonfa697a92001-08-16 20:07:29 +00002377 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002378
Josh Coalson94e02cd2001-01-25 10:41:06 +00002379 return true;
2380}
2381
Josh Coalson6fe72f72002-08-20 04:01:59 +00002382FLAC__bool process_subframe_(
2383 FLAC__StreamEncoder *encoder,
2384 unsigned min_partition_order,
2385 unsigned max_partition_order,
2386 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002387 const FLAC__FrameHeader *frame_header,
2388 unsigned subframe_bps,
2389 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002390#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002391 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002392#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002393 FLAC__Subframe *subframe[2],
2394 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
2395 FLAC__int32 *residual[2],
2396 unsigned *best_subframe,
2397 unsigned *best_bits
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002398#ifdef WINDOW_DEBUG_OUTPUT
2399 ,unsigned subframe_number
2400#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002401)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002402{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002403#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002404 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002405#else
2406 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2407#endif
2408#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002409 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002410 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 Coalson09758432004-10-20 00:21:50 +00002411 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002412 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002413 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002414#endif
2415 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002416 unsigned rice_parameter;
2417 unsigned _candidate_bits, _best_bits;
2418 unsigned _best_subframe;
2419
2420 /* verbatim subframe is the baseline against which we measure other compressed subframes */
2421 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002422 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
2423 _best_bits = UINT_MAX;
2424 else
2425 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002426
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002427 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
2428 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002429 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 +00002430 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002431 if(
2432 !encoder->private_->disable_constant_subframes &&
2433#ifndef FLAC__INTEGER_ONLY_LIBRARY
2434 fixed_residual_bits_per_sample[1] == 0.0
2435#else
2436 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
2437#endif
2438 ) {
2439 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002440 unsigned i;
2441 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002442 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002443 if(integer_signal[0] != integer_signal[i]) {
2444 signal_is_constant = false;
2445 break;
2446 }
2447 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002448 }
2449 if(signal_is_constant) {
2450 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
2451 if(_candidate_bits < _best_bits) {
2452 _best_subframe = !_best_subframe;
2453 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002454 }
2455 }
2456 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002457 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
2458 /* encode fixed */
2459 if(encoder->protected_->do_exhaustive_model_search) {
2460 min_fixed_order = 0;
2461 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00002462 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002463 else {
2464 min_fixed_order = max_fixed_order = guess_fixed_order;
2465 }
2466 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002467#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002468 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002469 continue; /* don't even try */
2470 rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002471#else
2472 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
2473 continue; /* don't even try */
2474 rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (unsigned)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */
2475#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002476 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002477 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2478#ifdef DEBUG_VERBOSE
2479 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2480#endif
2481 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2482 }
2483 _candidate_bits =
2484 evaluate_fixed_subframe_(
2485 encoder,
2486 integer_signal,
2487 residual[!_best_subframe],
2488 encoder->private_->abs_residual,
2489 encoder->private_->abs_residual_partition_sums,
2490 encoder->private_->raw_bits_per_partition,
2491 frame_header->blocksize,
2492 subframe_bps,
2493 fixed_order,
2494 rice_parameter,
2495 min_partition_order,
2496 max_partition_order,
2497 precompute_partition_sums,
2498 encoder->protected_->do_escape_coding,
2499 encoder->protected_->rice_parameter_search_dist,
2500 subframe[!_best_subframe],
2501 partitioned_rice_contents[!_best_subframe]
2502 );
2503 if(_candidate_bits < _best_bits) {
2504 _best_subframe = !_best_subframe;
2505 _best_bits = _candidate_bits;
2506 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002507 }
2508 }
2509
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002510#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00002511 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002512 if(encoder->protected_->max_lpc_order > 0) {
2513 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002514 max_lpc_order = frame_header->blocksize-1;
2515 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002516 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002517 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002518 unsigned a;
2519 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00002520 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002521 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2522 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2523 if(autoc[0] != 0.0) {
2524 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2525 if(encoder->protected_->do_exhaustive_model_search) {
2526 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002527 }
2528 else {
Josh Coalsondf598452006-04-28 00:13:34 +00002529 const unsigned guess_lpc_order =
2530 FLAC__lpc_compute_best_order(
2531 lpc_error,
2532 max_lpc_order,
2533 frame_header->blocksize,
2534 subframe_bps + (
2535 encoder->protected_->do_qlp_coeff_prec_search?
2536 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
2537 encoder->protected_->qlp_coeff_precision
2538 )
2539 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002540 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002541 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002542 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2543 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2544 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
2545 continue; /* don't even try */
2546 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2547 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2548 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00002549#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002550 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
Josh Coalsondf598452006-04-28 00:13:34 +00002551#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002552 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2553 }
2554 if(encoder->protected_->do_qlp_coeff_prec_search) {
2555 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00002556 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
2557 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002558 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00002559 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
2560 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002561 else
2562 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2563 }
2564 else {
2565 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2566 }
2567 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2568 _candidate_bits =
2569 evaluate_lpc_subframe_(
2570 encoder,
2571 integer_signal,
2572 residual[!_best_subframe],
2573 encoder->private_->abs_residual,
2574 encoder->private_->abs_residual_partition_sums,
2575 encoder->private_->raw_bits_per_partition,
2576 encoder->private_->lp_coeff[lpc_order-1],
2577 frame_header->blocksize,
2578 subframe_bps,
2579 lpc_order,
2580 qlp_coeff_precision,
2581 rice_parameter,
2582 min_partition_order,
2583 max_partition_order,
2584 precompute_partition_sums,
2585 encoder->protected_->do_escape_coding,
2586 encoder->protected_->rice_parameter_search_dist,
2587 subframe[!_best_subframe],
2588 partitioned_rice_contents[!_best_subframe]
2589#ifdef WINDOW_DEBUG_OUTPUT
2590 ,frame_header->number.frame_number
2591 ,subframe_number
2592 ,encoder->protected_->apodizations[a]
2593#endif
2594 );
2595 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2596 if(_candidate_bits < _best_bits) {
2597 _best_subframe = !_best_subframe;
2598 _best_bits = _candidate_bits;
2599 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002600 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002601 }
2602 }
2603 }
2604 }
2605 }
2606 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002607#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00002608 }
2609 }
2610
Josh Coalson72695802002-10-11 06:25:16 +00002611 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2612 if(_best_bits == UINT_MAX) {
2613 FLAC__ASSERT(_best_subframe == 0);
2614 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2615 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002616
Josh Coalson94e02cd2001-01-25 10:41:06 +00002617 *best_subframe = _best_subframe;
2618 *best_bits = _best_bits;
2619
2620 return true;
2621}
2622
Josh Coalson6fe72f72002-08-20 04:01:59 +00002623FLAC__bool add_subframe_(
2624 FLAC__StreamEncoder *encoder,
2625 const FLAC__FrameHeader *frame_header,
2626 unsigned subframe_bps,
2627 const FLAC__Subframe *subframe,
2628 FLAC__BitBuffer *frame
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002629#ifdef WINDOW_DEBUG_OUTPUT
2630,unsigned subframe_bits
2631#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002632)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002633{
2634 switch(subframe->type) {
2635 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00002636 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002637 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002638 return false;
2639 }
2640 break;
2641 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00002642 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 +00002643 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002644 return false;
2645 }
2646 break;
2647 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002648#ifdef WINDOW_DEBUG_OUTPUT
2649 fprintf(stderr, "WIN:\tframe=%u\tsubframe=?\torder=%u\twindow=%s\tbits=%u\n", frame_header->number.frame_number, subframe->data.lpc.order, subframe->data.lpc.window_type, subframe_bits);
2650#endif
Josh Coalson82b73242001-03-28 22:17:05 +00002651 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 +00002652 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002653 return false;
2654 }
2655 break;
2656 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00002657 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002658 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002659 return false;
2660 }
2661 break;
2662 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002663 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002664 }
2665
2666 return true;
2667}
2668
Josh Coalson6fe72f72002-08-20 04:01:59 +00002669unsigned evaluate_constant_subframe_(
2670 const FLAC__int32 signal,
2671 unsigned subframe_bps,
2672 FLAC__Subframe *subframe
2673)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002674{
2675 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2676 subframe->data.constant.value = signal;
2677
Josh Coalson82b73242001-03-28 22:17:05 +00002678 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 +00002679}
2680
Josh Coalson6fe72f72002-08-20 04:01:59 +00002681unsigned evaluate_fixed_subframe_(
2682 FLAC__StreamEncoder *encoder,
2683 const FLAC__int32 signal[],
2684 FLAC__int32 residual[],
2685 FLAC__uint32 abs_residual[],
2686 FLAC__uint64 abs_residual_partition_sums[],
2687 unsigned raw_bits_per_partition[],
2688 unsigned blocksize,
2689 unsigned subframe_bps,
2690 unsigned order,
2691 unsigned rice_parameter,
2692 unsigned min_partition_order,
2693 unsigned max_partition_order,
2694 FLAC__bool precompute_partition_sums,
2695 FLAC__bool do_escape_coding,
2696 unsigned rice_parameter_search_dist,
2697 FLAC__Subframe *subframe,
2698 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2699)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002700{
2701 unsigned i, residual_bits;
2702 const unsigned residual_samples = blocksize - order;
2703
2704 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2705
2706 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2707
2708 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002709 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002710 subframe->data.fixed.residual = residual;
2711
Josh Coalson6fe72f72002-08-20 04:01:59 +00002712 residual_bits =
2713 find_best_partition_order_(
2714 encoder->private_,
2715 residual,
2716 abs_residual,
2717 abs_residual_partition_sums,
2718 raw_bits_per_partition,
2719 residual_samples,
2720 order,
2721 rice_parameter,
2722 min_partition_order,
2723 max_partition_order,
2724 precompute_partition_sums,
2725 do_escape_coding,
2726 rice_parameter_search_dist,
2727 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2728 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002729
2730 subframe->data.fixed.order = order;
2731 for(i = 0; i < order; i++)
2732 subframe->data.fixed.warmup[i] = signal[i];
2733
Josh Coalson82b73242001-03-28 22:17:05 +00002734 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 +00002735}
2736
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002737#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002738unsigned evaluate_lpc_subframe_(
2739 FLAC__StreamEncoder *encoder,
2740 const FLAC__int32 signal[],
2741 FLAC__int32 residual[],
2742 FLAC__uint32 abs_residual[],
2743 FLAC__uint64 abs_residual_partition_sums[],
2744 unsigned raw_bits_per_partition[],
2745 const FLAC__real lp_coeff[],
2746 unsigned blocksize,
2747 unsigned subframe_bps,
2748 unsigned order,
2749 unsigned qlp_coeff_precision,
2750 unsigned rice_parameter,
2751 unsigned min_partition_order,
2752 unsigned max_partition_order,
2753 FLAC__bool precompute_partition_sums,
2754 FLAC__bool do_escape_coding,
2755 unsigned rice_parameter_search_dist,
2756 FLAC__Subframe *subframe,
2757 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002758#ifdef WINDOW_DEBUG_OUTPUT
2759 ,unsigned frame_number
2760 ,unsigned subframe_number
2761 ,FLAC__ApodizationSpecification aspec
2762#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002763)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002764{
Josh Coalson77e3f312001-06-23 03:03:24 +00002765 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002766 unsigned i, residual_bits;
2767 int quantization, ret;
2768 const unsigned residual_samples = blocksize - order;
2769
Josh Coalson20ac2c12002-08-30 05:47:14 +00002770 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2771 if(subframe_bps <= 16) {
2772 FLAC__ASSERT(order > 0);
2773 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2774 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2775 }
2776
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002777#ifdef WINDOW_DEBUG_OUTPUT
2778 if (aspec.type == FLAC__APODIZATION_GAUSS)
2779 snprintf(subframe->data.lpc.window_type, sizeof subframe->data.lpc.window_type, "%s(%0.5f)", winstr[aspec.type], aspec.parameters.gauss.stddev);
2780 else if (aspec.type == FLAC__APODIZATION_TUKEY)
2781 snprintf(subframe->data.lpc.window_type, sizeof subframe->data.lpc.window_type, "%s(%0.5f)", winstr[aspec.type], aspec.parameters.tukey.p);
2782 else
2783 strncpy(subframe->data.lpc.window_type, winstr[aspec.type], sizeof subframe->data.lpc.window_type);
2784#endif
2785
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002786 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002787 if(ret != 0)
2788 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2789
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00002790 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2791 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2792 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2793 else
2794 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 +00002795 else
2796 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 +00002797
2798 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2799
2800 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002801 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002802 subframe->data.lpc.residual = residual;
2803
Josh Coalson6fe72f72002-08-20 04:01:59 +00002804 residual_bits =
2805 find_best_partition_order_(
2806 encoder->private_,
2807 residual,
2808 abs_residual,
2809 abs_residual_partition_sums,
2810 raw_bits_per_partition,
2811 residual_samples,
2812 order,
2813 rice_parameter,
2814 min_partition_order,
2815 max_partition_order,
2816 precompute_partition_sums,
2817 do_escape_coding,
2818 rice_parameter_search_dist,
2819 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2820 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002821
2822 subframe->data.lpc.order = order;
2823 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2824 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00002825 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002826 for(i = 0; i < order; i++)
2827 subframe->data.lpc.warmup[i] = signal[i];
2828
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002829#ifdef WINDOW_DEBUG_OUTPUT
2830 fprintf(stderr, "SWIN:\tframe=%u\tsubframe=%u\torder=%u\twindow=%s\tbits=%u\n", frame_number, subframe_number, order, subframe->data.lpc.window_type, 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);
2831#endif
Josh Coalson82b73242001-03-28 22:17:05 +00002832 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 +00002833}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002834#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002835
Josh Coalson6fe72f72002-08-20 04:01:59 +00002836unsigned evaluate_verbatim_subframe_(
2837 const FLAC__int32 signal[],
2838 unsigned blocksize,
2839 unsigned subframe_bps,
2840 FLAC__Subframe *subframe
2841)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002842{
2843 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2844
2845 subframe->data.verbatim.data = signal;
2846
Josh Coalson82b73242001-03-28 22:17:05 +00002847 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 +00002848}
2849
Josh Coalson6fe72f72002-08-20 04:01:59 +00002850unsigned find_best_partition_order_(
2851 FLAC__StreamEncoderPrivate *private_,
2852 const FLAC__int32 residual[],
2853 FLAC__uint32 abs_residual[],
2854 FLAC__uint64 abs_residual_partition_sums[],
2855 unsigned raw_bits_per_partition[],
2856 unsigned residual_samples,
2857 unsigned predictor_order,
2858 unsigned rice_parameter,
2859 unsigned min_partition_order,
2860 unsigned max_partition_order,
2861 FLAC__bool precompute_partition_sums,
2862 FLAC__bool do_escape_coding,
2863 unsigned rice_parameter_search_dist,
2864 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2865)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002866{
Josh Coalson77e3f312001-06-23 03:03:24 +00002867 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002868 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00002869 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00002870 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002871 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002872
Josh Coalson2051dd42001-04-12 22:22:34 +00002873 /* compute abs(residual) for use later */
2874 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2875 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00002876 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00002877 }
2878
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002879 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 +00002880 min_partition_order = min(min_partition_order, max_partition_order);
2881
Josh Coalson8395d022001-07-12 21:25:22 +00002882 if(precompute_partition_sums) {
2883 int partition_order;
2884 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002885
Josh Coalsonf1eff452002-07-31 07:05:33 +00002886 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 +00002887
2888 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00002889 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 +00002890
2891 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2892#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002893 if(!
2894 set_partitioned_rice_with_precompute_(
2895 residual,
2896 abs_residual_partition_sums+sum,
2897 raw_bits_per_partition+sum,
2898 residual_samples,
2899 predictor_order,
2900 rice_parameter,
2901 rice_parameter_search_dist,
2902 (unsigned)partition_order,
2903 do_escape_coding,
2904 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2905 &residual_bits
2906 )
2907 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002908#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002909 if(!
2910 set_partitioned_rice_with_precompute_(
2911 abs_residual,
2912 abs_residual_partition_sums+sum,
2913 raw_bits_per_partition+sum,
2914 residual_samples,
2915 predictor_order,
2916 rice_parameter,
2917 rice_parameter_search_dist,
2918 (unsigned)partition_order,
2919 do_escape_coding,
2920 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2921 &residual_bits
2922 )
2923 )
Josh Coalson8395d022001-07-12 21:25:22 +00002924#endif
2925 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002926 FLAC__ASSERT(best_residual_bits != 0);
2927 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002928 }
2929 sum += 1u << partition_order;
2930 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2931 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002932 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002933 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002934 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00002935 }
2936 }
Josh Coalson8395d022001-07-12 21:25:22 +00002937 else {
2938 unsigned partition_order;
2939 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2940#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002941 if(!
2942 set_partitioned_rice_(
2943 abs_residual,
2944 residual,
2945 residual_samples,
2946 predictor_order,
2947 rice_parameter,
2948 rice_parameter_search_dist,
2949 partition_order,
2950 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2951 &residual_bits
2952 )
2953 )
Josh Coalson8395d022001-07-12 21:25:22 +00002954#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002955 if(!
2956 set_partitioned_rice_(
2957 abs_residual,
2958 residual_samples,
2959 predictor_order,
2960 rice_parameter,
2961 rice_parameter_search_dist,
2962 partition_order,
2963 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2964 &residual_bits
2965 )
2966 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002967#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002968 {
2969 FLAC__ASSERT(best_residual_bits != 0);
2970 break;
2971 }
2972 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2973 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002974 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002975 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002976 }
2977 }
2978 }
2979
Josh Coalsona37ba462002-08-19 21:36:39 +00002980 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00002981 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00002982 * it is const to the outside world.
2983 */
2984 {
2985 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2986 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2987 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2988 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2989 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002990
2991 return best_residual_bits;
2992}
2993
Josh Coalson6fe72f72002-08-20 04:01:59 +00002994void precompute_partition_info_sums_(
2995 const FLAC__uint32 abs_residual[],
2996 FLAC__uint64 abs_residual_partition_sums[],
2997 unsigned residual_samples,
2998 unsigned predictor_order,
2999 unsigned min_partition_order,
3000 unsigned max_partition_order
3001)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003002{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003003 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003004 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003005 const unsigned blocksize = residual_samples + predictor_order;
3006
Josh Coalsonaef013c2001-04-24 01:25:42 +00003007 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003008 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003009 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00003010 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003011 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003012 const unsigned partitions = 1u << partition_order;
3013 const unsigned default_partition_samples = blocksize >> partition_order;
3014
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003015 FLAC__ASSERT(default_partition_samples > predictor_order);
3016
3017 for(partition = residual_sample = 0; partition < partitions; partition++) {
3018 partition_samples = default_partition_samples;
3019 if(partition == 0)
3020 partition_samples -= predictor_order;
3021 abs_residual_partition_sum = 0;
3022 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3023 abs_r = abs_residual[residual_sample];
3024 abs_residual_partition_sum += abs_r;
3025 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003026 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003027 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003028 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003029 to_partition = partitions;
3030 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003031 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00003032
Josh Coalson8395d022001-07-12 21:25:22 +00003033 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00003034 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003035 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003036 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003037 const unsigned partitions = 1u << partition_order;
3038 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00003039 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003040 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003041 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003042 from_partition++;
3043 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003044 }
3045 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003046}
Josh Coalson8395d022001-07-12 21:25:22 +00003047
Josh Coalson6fe72f72002-08-20 04:01:59 +00003048void precompute_partition_info_escapes_(
3049 const FLAC__int32 residual[],
3050 unsigned raw_bits_per_partition[],
3051 unsigned residual_samples,
3052 unsigned predictor_order,
3053 unsigned min_partition_order,
3054 unsigned max_partition_order
3055)
Josh Coalson8395d022001-07-12 21:25:22 +00003056{
3057 int partition_order;
3058 unsigned from_partition, to_partition = 0;
3059 const unsigned blocksize = residual_samples + predictor_order;
3060
3061 /* first do max_partition_order */
3062 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3063 FLAC__int32 r, residual_partition_min, residual_partition_max;
3064 unsigned silog2_min, silog2_max;
3065 unsigned partition, partition_sample, partition_samples, residual_sample;
3066 const unsigned partitions = 1u << partition_order;
3067 const unsigned default_partition_samples = blocksize >> partition_order;
3068
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003069 FLAC__ASSERT(default_partition_samples > predictor_order);
3070
3071 for(partition = residual_sample = 0; partition < partitions; partition++) {
3072 partition_samples = default_partition_samples;
3073 if(partition == 0)
3074 partition_samples -= predictor_order;
3075 residual_partition_min = residual_partition_max = 0;
3076 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3077 r = residual[residual_sample];
3078 if(r < residual_partition_min)
3079 residual_partition_min = r;
3080 else if(r > residual_partition_max)
3081 residual_partition_max = r;
3082 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00003083 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003084 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
3085 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
3086 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00003087 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003088 to_partition = partitions;
3089 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003090 }
3091
3092 /* now merge partitions for lower orders */
3093 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3094 unsigned m;
3095 unsigned i;
3096 const unsigned partitions = 1u << partition_order;
3097 for(i = 0; i < partitions; i++) {
3098 m = raw_bits_per_partition[from_partition];
3099 from_partition++;
3100 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
3101 from_partition++;
3102 to_partition++;
3103 }
3104 }
3105}
Josh Coalson94e02cd2001-01-25 10:41:06 +00003106
Josh Coalson352e0f62001-03-20 22:55:50 +00003107#ifdef VARIABLE_RICE_BITS
3108#undef VARIABLE_RICE_BITS
3109#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003110#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003111#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00003112#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003113
Josh Coalson8395d022001-07-12 21:25:22 +00003114#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003115FLAC__bool set_partitioned_rice_(
3116 const FLAC__uint32 abs_residual[],
3117 const FLAC__int32 residual[],
3118 const unsigned residual_samples,
3119 const unsigned predictor_order,
3120 const unsigned suggested_rice_parameter,
3121 const unsigned rice_parameter_search_dist,
3122 const unsigned partition_order,
3123 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3124 unsigned *bits
3125)
Josh Coalson8395d022001-07-12 21:25:22 +00003126#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003127FLAC__bool set_partitioned_rice_(
3128 const FLAC__uint32 abs_residual[],
3129 const unsigned residual_samples,
3130 const unsigned predictor_order,
3131 const unsigned suggested_rice_parameter,
3132 const unsigned rice_parameter_search_dist,
3133 const unsigned partition_order,
3134 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3135 unsigned *bits
3136)
Josh Coalson8395d022001-07-12 21:25:22 +00003137#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003138{
Josh Coalson034dfab2001-04-27 19:10:23 +00003139 unsigned rice_parameter, partition_bits;
3140#ifndef NO_RICE_SEARCH
3141 unsigned best_partition_bits;
3142 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3143#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003144 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003145 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003146
Josh Coalson1b689822001-05-31 20:11:02 +00003147 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00003148
Josh Coalsona37ba462002-08-19 21:36:39 +00003149 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3150 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003151
Josh Coalson94e02cd2001-01-25 10:41:06 +00003152 if(partition_order == 0) {
3153 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00003154
Josh Coalson034dfab2001-04-27 19:10:23 +00003155#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003156 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003157 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003158 min_rice_parameter = 0;
3159 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003160 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3161 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003162 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003163#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003164 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3165#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003166 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003167 }
3168 }
3169 else
3170 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3171
3172 best_partition_bits = 0xffffffff;
3173 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3174#endif
3175#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003176 const unsigned rice_parameter_estimate = rice_parameter-1;
3177 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00003178#else
3179 partition_bits = 0;
3180#endif
3181 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3182 for(i = 0; i < residual_samples; i++) {
3183#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003184 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00003185#else
3186 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3187#endif
3188 }
3189#ifndef NO_RICE_SEARCH
3190 if(partition_bits < best_partition_bits) {
3191 best_rice_parameter = rice_parameter;
3192 best_partition_bits = partition_bits;
3193 }
3194 }
3195#endif
3196 parameters[0] = best_rice_parameter;
3197 bits_ += best_partition_bits;
3198 }
3199 else {
3200 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003201 unsigned partition_samples;
3202 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00003203 const unsigned partitions = 1u << partition_order;
3204 for(partition = residual_sample = 0; partition < partitions; partition++) {
3205 partition_samples = (residual_samples+predictor_order) >> partition_order;
3206 if(partition == 0) {
3207 if(partition_samples <= predictor_order)
3208 return false;
3209 else
3210 partition_samples -= predictor_order;
3211 }
3212 mean = 0;
3213 save_residual_sample = residual_sample;
3214 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003215 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00003216 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00003217 /* we are basically calculating the size in bits of the
3218 * average residual magnitude in the partition:
3219 * rice_parameter = floor(log2(mean/partition_samples))
3220 * 'mean' is not a good name for the variable, it is
3221 * actually the sum of magnitudes of all residual values
3222 * in the partition, so the actual mean is
3223 * mean/partition_samples
3224 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003225 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00003226 ;
Josh Coalson8395d022001-07-12 21:25:22 +00003227 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003228#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003229 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3230#endif
3231 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3232 }
3233
3234#ifndef NO_RICE_SEARCH
3235 if(rice_parameter_search_dist) {
3236 if(rice_parameter < rice_parameter_search_dist)
3237 min_rice_parameter = 0;
3238 else
3239 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3240 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3241 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003242#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003243 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3244#endif
3245 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3246 }
3247 }
3248 else
3249 min_rice_parameter = max_rice_parameter = rice_parameter;
3250
3251 best_partition_bits = 0xffffffff;
3252 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3253#endif
3254#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003255 const unsigned rice_parameter_estimate = rice_parameter-1;
3256 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00003257#else
3258 partition_bits = 0;
3259#endif
3260 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3261 save_residual_sample = residual_sample;
3262 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3263#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003264 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00003265#else
3266 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3267#endif
3268 }
3269#ifndef NO_RICE_SEARCH
3270 if(rice_parameter != max_rice_parameter)
3271 residual_sample = save_residual_sample;
3272 if(partition_bits < best_partition_bits) {
3273 best_rice_parameter = rice_parameter;
3274 best_partition_bits = partition_bits;
3275 }
3276 }
3277#endif
3278 parameters[partition] = best_rice_parameter;
3279 bits_ += best_partition_bits;
3280 }
3281 }
3282
3283 *bits = bits_;
3284 return true;
3285}
3286
3287#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003288FLAC__bool set_partitioned_rice_with_precompute_(
3289 const FLAC__int32 residual[],
3290 const FLAC__uint64 abs_residual_partition_sums[],
3291 const unsigned raw_bits_per_partition[],
3292 const unsigned residual_samples,
3293 const unsigned predictor_order,
3294 const unsigned suggested_rice_parameter,
3295 const unsigned rice_parameter_search_dist,
3296 const unsigned partition_order,
3297 const FLAC__bool search_for_escapes,
3298 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3299 unsigned *bits
3300)
Josh Coalson8395d022001-07-12 21:25:22 +00003301#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003302FLAC__bool set_partitioned_rice_with_precompute_(
3303 const FLAC__uint32 abs_residual[],
3304 const FLAC__uint64 abs_residual_partition_sums[],
3305 const unsigned raw_bits_per_partition[],
3306 const unsigned residual_samples,
3307 const unsigned predictor_order,
3308 const unsigned suggested_rice_parameter,
3309 const unsigned rice_parameter_search_dist,
3310 const unsigned partition_order,
3311 const FLAC__bool search_for_escapes,
3312 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3313 unsigned *bits
3314)
Josh Coalson8395d022001-07-12 21:25:22 +00003315#endif
3316{
3317 unsigned rice_parameter, partition_bits;
3318#ifndef NO_RICE_SEARCH
3319 unsigned best_partition_bits;
3320 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3321#endif
3322 unsigned flat_bits;
3323 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003324 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003325
3326 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3327
Josh Coalsona37ba462002-08-19 21:36:39 +00003328 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3329 parameters = partitioned_rice_contents->parameters;
3330 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003331
Josh Coalson8395d022001-07-12 21:25:22 +00003332 if(partition_order == 0) {
3333 unsigned i;
3334
3335#ifndef NO_RICE_SEARCH
3336 if(rice_parameter_search_dist) {
3337 if(suggested_rice_parameter < rice_parameter_search_dist)
3338 min_rice_parameter = 0;
3339 else
3340 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3341 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3342 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003343#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003344 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3345#endif
3346 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3347 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003348 }
3349 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003350 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00003351
Josh Coalson034dfab2001-04-27 19:10:23 +00003352 best_partition_bits = 0xffffffff;
3353 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3354#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003355#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003356 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00003357 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00003358#else
3359 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003360#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00003361 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00003362 for(i = 0; i < residual_samples; i++) {
3363#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00003364 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00003365#else
Josh Coalson2051dd42001-04-12 22:22:34 +00003366 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 +00003367#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00003368 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003369#ifndef NO_RICE_SEARCH
3370 if(partition_bits < best_partition_bits) {
3371 best_rice_parameter = rice_parameter;
3372 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00003373 }
3374 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003375#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003376 if(search_for_escapes) {
3377 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;
3378 if(flat_bits <= best_partition_bits) {
3379 raw_bits[0] = raw_bits_per_partition[0];
3380 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3381 best_partition_bits = flat_bits;
3382 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003383 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003384 parameters[0] = best_rice_parameter;
3385 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003386 }
3387 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00003388 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003389 unsigned partition_samples;
3390 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003391 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00003392 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003393 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00003394 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003395 if(partition_samples <= predictor_order)
3396 return false;
3397 else
3398 partition_samples -= predictor_order;
3399 }
Josh Coalson05d20792001-06-29 23:12:26 +00003400 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00003401 /* we are basically calculating the size in bits of the
3402 * average residual magnitude in the partition:
3403 * rice_parameter = floor(log2(mean/partition_samples))
3404 * 'mean' is not a good name for the variable, it is
3405 * actually the sum of magnitudes of all residual values
3406 * in the partition, so the actual mean is
3407 * mean/partition_samples
3408 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003409 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00003410 ;
Josh Coalson8395d022001-07-12 21:25:22 +00003411 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003412#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003413 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3414#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003415 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003416 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003417
Josh Coalson034dfab2001-04-27 19:10:23 +00003418#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003419 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003420 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003421 min_rice_parameter = 0;
3422 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003423 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3424 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003425 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003426#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003427 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3428#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003429 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003430 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003431 }
3432 else
3433 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00003434
Josh Coalson034dfab2001-04-27 19:10:23 +00003435 best_partition_bits = 0xffffffff;
3436 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3437#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003438#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00003439 const unsigned rice_parameter_estimate = rice_parameter-1;
3440 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00003441#else
3442 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003443#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003444 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00003445 save_residual_sample = residual_sample;
3446 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00003447#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00003448 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00003449#else
Josh Coalson4dacd192001-06-06 21:11:44 +00003450 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 +00003451#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003452 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003453#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00003454 if(rice_parameter != max_rice_parameter)
3455 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00003456 if(partition_bits < best_partition_bits) {
3457 best_rice_parameter = rice_parameter;
3458 best_partition_bits = partition_bits;
3459 }
Josh Coalson2051dd42001-04-12 22:22:34 +00003460 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003461#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003462 if(search_for_escapes) {
3463 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;
3464 if(flat_bits <= best_partition_bits) {
3465 raw_bits[partition] = raw_bits_per_partition[partition];
3466 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3467 best_partition_bits = flat_bits;
3468 }
Josh Coalson2051dd42001-04-12 22:22:34 +00003469 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003470 parameters[partition] = best_rice_parameter;
3471 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003472 }
3473 }
3474
3475 *bits = bits_;
3476 return true;
3477}
Josh Coalson859bc542001-03-27 22:22:27 +00003478
Josh Coalsonf1eff452002-07-31 07:05:33 +00003479unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00003480{
3481 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00003482 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00003483
3484 for(i = 0; i < samples && !(x&1); i++)
3485 x |= signal[i];
3486
3487 if(x == 0) {
3488 shift = 0;
3489 }
3490 else {
3491 for(shift = 0; !(x&1); shift++)
3492 x >>= 1;
3493 }
3494
3495 if(shift > 0) {
3496 for(i = 0; i < samples; i++)
3497 signal[i] >>= shift;
3498 }
3499
3500 return shift;
3501}
Josh Coalsond86e03b2002-08-03 21:56:15 +00003502
3503void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3504{
3505 unsigned channel;
3506
3507 for(channel = 0; channel < channels; channel++)
3508 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3509
3510 fifo->tail += wide_samples;
3511
3512 FLAC__ASSERT(fifo->tail <= fifo->size);
3513}
3514
3515void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3516{
3517 unsigned channel;
3518 unsigned sample, wide_sample;
3519 unsigned tail = fifo->tail;
3520
3521 sample = input_offset * channels;
3522 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3523 for(channel = 0; channel < channels; channel++)
3524 fifo->data[channel][tail] = input[sample++];
3525 tail++;
3526 }
3527 fifo->tail = tail;
3528
3529 FLAC__ASSERT(fifo->tail <= fifo->size);
3530}
3531
3532FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3533{
3534 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3535 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3536 (void)decoder;
3537
3538 if(encoder->private_->verify.needs_magic_hack) {
3539 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3540 *bytes = FLAC__STREAM_SYNC_LENGTH;
3541 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3542 encoder->private_->verify.needs_magic_hack = false;
3543 }
3544 else {
3545 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00003546 /*
3547 * If we get here, a FIFO underflow has occurred,
3548 * which means there is a bug somewhere.
3549 */
3550 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003551 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3552 }
3553 else if(encoded_bytes < *bytes)
3554 *bytes = encoded_bytes;
3555 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3556 encoder->private_->verify.output.data += *bytes;
3557 encoder->private_->verify.output.bytes -= *bytes;
3558 }
3559
3560 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3561}
3562
3563FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3564{
3565 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3566 unsigned channel;
3567 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3568 const unsigned blocksize = frame->header.blocksize;
3569 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3570
3571 for(channel = 0; channel < channels; channel++) {
3572 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3573 unsigned i, sample = 0;
3574 FLAC__int32 expect = 0, got = 0;
3575
3576 for(i = 0; i < blocksize; i++) {
3577 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3578 sample = i;
3579 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3580 got = (FLAC__int32)buffer[channel][i];
3581 break;
3582 }
3583 }
3584 FLAC__ASSERT(i < blocksize);
3585 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3586 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00003587 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003588 encoder->private_->verify.error_stats.channel = channel;
3589 encoder->private_->verify.error_stats.sample = sample;
3590 encoder->private_->verify.error_stats.expected = expect;
3591 encoder->private_->verify.error_stats.got = got;
3592 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3593 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3594 }
3595 }
3596 /* dequeue the frame from the fifo */
3597 for(channel = 0; channel < channels; channel++) {
3598 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3599 }
3600 encoder->private_->verify.input_fifo.tail -= blocksize;
3601 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3602}
3603
3604void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3605{
3606 (void)decoder, (void)metadata, (void)client_data;
3607}
3608
3609void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3610{
3611 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3612 (void)decoder, (void)status;
3613 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3614}