blob: 0a27caeece08727724b530c635c6ba8c2788bca9 [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 Coalsonb1ec7962006-05-24 04:41:36 +000032#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
Josh Coalson6b21f662006-09-13 01:42:27 +000036#if defined _MSC_VER || defined __MINGW32__
37#include <io.h> /* for _setmode() */
38#include <fcntl.h> /* for _O_BINARY */
39#endif
40#if defined __CYGWIN__ || defined __EMX__
41#include <io.h> /* for setmode(), O_BINARY */
42#include <fcntl.h> /* for _O_BINARY */
43#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +000044#include <limits.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000045#include <stdio.h>
46#include <stdlib.h> /* for malloc() */
47#include <string.h> /* for memcpy() */
Josh Coalson6b21f662006-09-13 01:42:27 +000048#include <sys/types.h> /* for off_t */
49#if defined _MSC_VER || defined __MINGW32__
Josh Coalson825e72c2006-10-03 01:16:59 +000050#if _MSC_VER <= 1200 /* @@@ [2G limit] */
Josh Coalson6b21f662006-09-13 01:42:27 +000051#define fseeko fseek
52#define ftello ftell
53#endif
Josh Coalson825e72c2006-10-03 01:16:59 +000054#endif
Josh Coalson1b689822001-05-31 20:11:02 +000055#include "FLAC/assert.h"
Josh Coalsond86e03b2002-08-03 21:56:15 +000056#include "FLAC/stream_decoder.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000057#include "protected/stream_encoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000058#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000059#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000060#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000061#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000062#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000063#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000064#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000065#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000066#include "private/memory.h"
Josh Coalson8da98c82006-10-15 04:24:05 +000067#ifdef FLAC__HAS_OGG
68#include "private/ogg_helper.h"
69#endif
Josh Coalsonb7023aa2002-08-17 15:23:43 +000070#include "private/stream_encoder_framing.h"
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000071#include "private/window.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000072
73#ifdef min
74#undef min
75#endif
76#define min(x,y) ((x)<(y)?(x):(y))
77
78#ifdef max
79#undef max
80#endif
81#define max(x,y) ((x)>(y)?(x):(y))
82
Josh Coalsond86e03b2002-08-03 21:56:15 +000083typedef struct {
84 FLAC__int32 *data[FLAC__MAX_CHANNELS];
85 unsigned size; /* of each data[] in samples */
86 unsigned tail;
87} verify_input_fifo;
88
89typedef struct {
90 const FLAC__byte *data;
91 unsigned capacity;
92 unsigned bytes;
93} verify_output;
94
95typedef enum {
96 ENCODER_IN_MAGIC = 0,
97 ENCODER_IN_METADATA = 1,
98 ENCODER_IN_AUDIO = 2
99} EncoderStateHint;
100
Josh Coalson425609c2006-11-03 16:08:52 +0000101static struct CompressionLevels {
102 FLAC__bool do_mid_side_stereo;
103 FLAC__bool loose_mid_side_stereo;
104 const char *apodization;
105 unsigned max_lpc_order;
106 unsigned qlp_coeff_precision;
107 FLAC__bool do_qlp_coeff_prec_search;
108 FLAC__bool do_escape_coding;
109 FLAC__bool do_exhaustive_model_search;
110 unsigned min_residual_partition_order;
111 unsigned max_residual_partition_order;
112 unsigned rice_parameter_search_dist;
113} compression_levels_[] = {
114 { false, false, "tukey(0.5)", 0, 0, false, false, false, 2, 2, 0 },
115 { true , true , "tukey(0.5)", 0, 0, false, false, false, 2, 2, 0 },
116 { true , false, "tukey(0.5)", 0, 0, false, false, false, 0, 3, 0 },
117 { false, false, "tukey(0.5)", 6, 0, false, false, false, 3, 3, 0 },
118 { true , true , "tukey(0.5)", 8, 0, false, false, false, 3, 3, 0 },
119 { true , false, "tukey(0.5)", 8, 0, false, false, false, 3, 3, 0 },
120 { true , false, "tukey(0.5)", 8, 0, false, false, false, 0, 4, 0 },
121 { true , false, "tukey(0.5)", 8, 0, false, false, true , 0, 6, 0 },
122 { true , false, "tukey(0.5)", 12, 0, false, false, true , 0, 6, 0 }
123};
124
125
Josh Coalson0a15c142001-06-13 17:59:57 +0000126/***********************************************************************
127 *
128 * Private class method prototypes
129 *
130 ***********************************************************************/
131
Josh Coalsonf1eff452002-07-31 07:05:33 +0000132static void set_defaults_(FLAC__StreamEncoder *encoder);
133static void free_(FLAC__StreamEncoder *encoder);
Josh Coalson85aaed82006-11-09 01:19:13 +0000134static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +0000135static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
Josh Coalson352feb52006-10-15 17:08:52 +0000136static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples);
Josh Coalson6b21f662006-09-13 01:42:27 +0000137static void update_metadata_(const FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000138#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +0000139static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000140#endif
Josh Coalson85aaed82006-11-09 01:19:13 +0000141static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
142static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000143
144static FLAC__bool process_subframe_(
145 FLAC__StreamEncoder *encoder,
146 unsigned min_partition_order,
147 unsigned max_partition_order,
148 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000149 const FLAC__FrameHeader *frame_header,
150 unsigned subframe_bps,
151 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000152#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000153 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000154#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000155 FLAC__Subframe *subframe[2],
156 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
157 FLAC__int32 *residual[2],
158 unsigned *best_subframe,
159 unsigned *best_bits
160);
161
162static FLAC__bool add_subframe_(
163 FLAC__StreamEncoder *encoder,
164 const FLAC__FrameHeader *frame_header,
165 unsigned subframe_bps,
166 const FLAC__Subframe *subframe,
167 FLAC__BitBuffer *frame
168);
169
170static unsigned evaluate_constant_subframe_(
171 const FLAC__int32 signal,
172 unsigned subframe_bps,
173 FLAC__Subframe *subframe
174);
175
176static unsigned evaluate_fixed_subframe_(
177 FLAC__StreamEncoder *encoder,
178 const FLAC__int32 signal[],
179 FLAC__int32 residual[],
180 FLAC__uint32 abs_residual[],
181 FLAC__uint64 abs_residual_partition_sums[],
182 unsigned raw_bits_per_partition[],
183 unsigned blocksize,
184 unsigned subframe_bps,
185 unsigned order,
186 unsigned rice_parameter,
187 unsigned min_partition_order,
188 unsigned max_partition_order,
189 FLAC__bool precompute_partition_sums,
190 FLAC__bool do_escape_coding,
191 unsigned rice_parameter_search_dist,
192 FLAC__Subframe *subframe,
193 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
194);
195
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000196#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000197static unsigned evaluate_lpc_subframe_(
198 FLAC__StreamEncoder *encoder,
199 const FLAC__int32 signal[],
200 FLAC__int32 residual[],
201 FLAC__uint32 abs_residual[],
202 FLAC__uint64 abs_residual_partition_sums[],
203 unsigned raw_bits_per_partition[],
204 const FLAC__real lp_coeff[],
205 unsigned blocksize,
206 unsigned subframe_bps,
207 unsigned order,
208 unsigned qlp_coeff_precision,
209 unsigned rice_parameter,
210 unsigned min_partition_order,
211 unsigned max_partition_order,
212 FLAC__bool precompute_partition_sums,
213 FLAC__bool do_escape_coding,
214 unsigned rice_parameter_search_dist,
215 FLAC__Subframe *subframe,
216 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
217);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000218#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000219
220static unsigned evaluate_verbatim_subframe_(
221 const FLAC__int32 signal[],
222 unsigned blocksize,
223 unsigned subframe_bps,
224 FLAC__Subframe *subframe
225);
226
227static unsigned find_best_partition_order_(
228 struct FLAC__StreamEncoderPrivate *private_,
229 const FLAC__int32 residual[],
230 FLAC__uint32 abs_residual[],
231 FLAC__uint64 abs_residual_partition_sums[],
232 unsigned raw_bits_per_partition[],
233 unsigned residual_samples,
234 unsigned predictor_order,
235 unsigned rice_parameter,
236 unsigned min_partition_order,
237 unsigned max_partition_order,
238 FLAC__bool precompute_partition_sums,
239 FLAC__bool do_escape_coding,
240 unsigned rice_parameter_search_dist,
241 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
242);
243
244static void precompute_partition_info_sums_(
245 const FLAC__uint32 abs_residual[],
246 FLAC__uint64 abs_residual_partition_sums[],
247 unsigned residual_samples,
248 unsigned predictor_order,
249 unsigned min_partition_order,
250 unsigned max_partition_order
251);
252
253static void precompute_partition_info_escapes_(
254 const FLAC__int32 residual[],
255 unsigned raw_bits_per_partition[],
256 unsigned residual_samples,
257 unsigned predictor_order,
258 unsigned min_partition_order,
259 unsigned max_partition_order
260);
261
Josh Coalson8395d022001-07-12 21:25:22 +0000262#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000263static FLAC__bool set_partitioned_rice_(
264 const FLAC__uint32 abs_residual[],
265 const FLAC__int32 residual[],
266 const unsigned residual_samples,
267 const unsigned predictor_order,
268 const unsigned suggested_rice_parameter,
269 const unsigned rice_parameter_search_dist,
270 const unsigned partition_order,
271 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
272 unsigned *bits
273);
274
275static FLAC__bool set_partitioned_rice_with_precompute_(
276 const FLAC__int32 residual[],
277 const FLAC__uint64 abs_residual_partition_sums[],
278 const unsigned raw_bits_per_partition[],
279 const unsigned residual_samples,
280 const unsigned predictor_order,
281 const unsigned suggested_rice_parameter,
282 const unsigned rice_parameter_search_dist,
283 const unsigned partition_order,
284 const FLAC__bool search_for_escapes,
285 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
286 unsigned *bits
287);
Josh Coalson8395d022001-07-12 21:25:22 +0000288#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000289static FLAC__bool set_partitioned_rice_(
290 const FLAC__uint32 abs_residual[],
291 const unsigned residual_samples,
292 const unsigned predictor_order,
293 const unsigned suggested_rice_parameter,
294 const unsigned rice_parameter_search_dist,
295 const unsigned partition_order,
296 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
297 unsigned *bits
298);
299
300static FLAC__bool set_partitioned_rice_with_precompute_(
301 const FLAC__uint32 abs_residual[],
302 const FLAC__uint64 abs_residual_partition_sums[],
303 const unsigned raw_bits_per_partition[],
304 const unsigned residual_samples,
305 const unsigned predictor_order,
306 const unsigned suggested_rice_parameter,
307 const unsigned rice_parameter_search_dist,
308 const unsigned partition_order,
309 const FLAC__bool search_for_escapes,
310 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
311 unsigned *bits
312);
Josh Coalson0a15c142001-06-13 17:59:57 +0000313#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000314
Josh Coalsonf1eff452002-07-31 07:05:33 +0000315static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000316
Josh Coalsond86e03b2002-08-03 21:56:15 +0000317/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000318static void append_to_verify_fifo_(
319 verify_input_fifo *fifo,
320 const FLAC__int32 * const input[],
321 unsigned input_offset,
322 unsigned channels,
323 unsigned wide_samples
324);
325
326static void append_to_verify_fifo_interleaved_(
327 verify_input_fifo *fifo,
328 const FLAC__int32 input[],
329 unsigned input_offset,
330 unsigned channels,
331 unsigned wide_samples
332);
333
Josh Coalson8065a2d2006-10-15 08:32:56 +0000334static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
Josh Coalson6b21f662006-09-13 01:42:27 +0000335static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
336static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
337static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000338
Josh Coalson8065a2d2006-10-15 08:32:56 +0000339static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
Josh Coalson6b21f662006-09-13 01:42:27 +0000340static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
341static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
Josh Coalson352feb52006-10-15 17:08:52 +0000342static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
Josh Coalson6b21f662006-09-13 01:42:27 +0000343static FILE *get_binary_stdout_();
Josh Coalson6fe72f72002-08-20 04:01:59 +0000344
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 Coalson6b21f662006-09-13 01:42:27 +0000385 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
386 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000387 unsigned current_sample_number;
388 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000389 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000390 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000391#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000392 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 +0000393#else
394 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
395#endif
396#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000397 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
Josh Coalson7446e182005-01-26 04:04:38 +0000398 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[]);
399 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[]);
400 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 +0000401#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000402 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
403 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
404 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
405 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 +0000406 FLAC__bool disable_constant_subframes;
407 FLAC__bool disable_fixed_subframes;
408 FLAC__bool disable_verbatim_subframes;
Josh Coalson8da98c82006-10-15 04:24:05 +0000409#if FLAC__HAS_OGG
410 FLAC__bool is_ogg;
411#endif
412 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
Josh Coalson6b21f662006-09-13 01:42:27 +0000413 FLAC__StreamEncoderSeekCallback seek_callback;
414 FLAC__StreamEncoderTellCallback tell_callback;
Josh Coalson681c2932002-08-01 08:19:37 +0000415 FLAC__StreamEncoderWriteCallback write_callback;
416 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000417 FLAC__StreamEncoderProgressCallback progress_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000418 void *client_data;
Josh Coalson6b21f662006-09-13 01:42:27 +0000419 unsigned first_seekpoint_to_check;
420 FILE *file; /* only used when encoding to a file */
421 FLAC__uint64 bytes_written;
422 FLAC__uint64 samples_written;
423 unsigned frames_written;
424 unsigned total_frames_estimate;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000425 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000426 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
427 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000428#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000429 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
430 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000431 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
432 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000433#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000434 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
435 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
436 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000437 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000438 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000439 /*
440 * These fields have been moved here from private function local
441 * declarations merely to save stack space during encoding.
442 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000443#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000444 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000445#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000446 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000447 /*
448 * The data for the verify section
449 */
450 struct {
451 FLAC__StreamDecoder *decoder;
452 EncoderStateHint state_hint;
453 FLAC__bool needs_magic_hack;
454 verify_input_fifo input_fifo;
455 verify_output output;
456 struct {
457 FLAC__uint64 absolute_sample;
458 unsigned frame_number;
459 unsigned channel;
460 unsigned sample;
461 FLAC__int32 expected;
462 FLAC__int32 got;
463 } error_stats;
464 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000465 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000466} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000467
Josh Coalson0a15c142001-06-13 17:59:57 +0000468/***********************************************************************
469 *
470 * Public static class data
471 *
472 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000473
Josh Coalson6afed9f2002-10-16 22:29:47 +0000474FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000475 "FLAC__STREAM_ENCODER_OK",
Josh Coalson6b21f662006-09-13 01:42:27 +0000476 "FLAC__STREAM_ENCODER_UNINITIALIZED",
Josh Coalson8da98c82006-10-15 04:24:05 +0000477 "FLAC__STREAM_ENCODER_OGG_ERROR",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000478 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
479 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson6b21f662006-09-13 01:42:27 +0000480 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
481 "FLAC__STREAM_ENCODER_IO_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000482 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson6b21f662006-09-13 01:42:27 +0000483 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
484};
485
486FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
487 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
488 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
Josh Coalson8da98c82006-10-15 04:24:05 +0000489 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
Josh Coalson6b21f662006-09-13 01:42:27 +0000490 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
491 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
492 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
493 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
494 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
495 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
496 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
Josh Coalson6b21f662006-09-13 01:42:27 +0000497 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
498 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
499 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
500 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000501};
502
Josh Coalson8da98c82006-10-15 04:24:05 +0000503FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
504 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
505 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
506 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
507 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
508};
509
Josh Coalson6afed9f2002-10-16 22:29:47 +0000510FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000511 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
512 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000513};
514
Josh Coalson6b21f662006-09-13 01:42:27 +0000515FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
516 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
517 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
518 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
519};
520
521FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
522 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
523 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
524 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
525};
526
Josh Coalson0a15c142001-06-13 17:59:57 +0000527/***********************************************************************
528 *
529 * Class constructor/destructor
530 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000531 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000532FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000533{
Josh Coalson0a15c142001-06-13 17:59:57 +0000534 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000535 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000536
Josh Coalson0a15c142001-06-13 17:59:57 +0000537 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000538
Josh Coalsonea7155f2002-10-18 05:49:19 +0000539 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000540 if(encoder == 0) {
541 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000542 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000543
Josh Coalsonea7155f2002-10-18 05:49:19 +0000544 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000545 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000546 free(encoder);
547 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000548 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000549
Josh Coalsonea7155f2002-10-18 05:49:19 +0000550 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000551 if(encoder->private_ == 0) {
552 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000553 free(encoder);
554 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000555 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000556
Josh Coalsonaec256b2002-03-12 16:19:54 +0000557 encoder->private_->frame = FLAC__bitbuffer_new();
558 if(encoder->private_->frame == 0) {
559 free(encoder->private_);
560 free(encoder->protected_);
561 free(encoder);
562 return 0;
563 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000564
Josh Coalson6b21f662006-09-13 01:42:27 +0000565 encoder->private_->file = 0;
566
Josh Coalsonf1eff452002-07-31 07:05:33 +0000567 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000568
Josh Coalson3262b0d2002-08-14 20:58:42 +0000569 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000570
571 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
572 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
573 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
574 }
575 for(i = 0; i < 2; i++) {
576 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
577 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
578 }
579 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000580 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
581 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000582 }
583 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000584 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
585 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 +0000586 }
587
588 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000589 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
590 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000591 }
592 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000593 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
594 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 +0000595 }
596 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000597 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000598
Josh Coalsonfa697a92001-08-16 20:07:29 +0000599 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000600
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000601 return encoder;
602}
603
Josh Coalson6afed9f2002-10-16 22:29:47 +0000604FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000605{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000606 unsigned i;
607
Josh Coalsonf1eff452002-07-31 07:05:33 +0000608 FLAC__ASSERT(0 != encoder);
609 FLAC__ASSERT(0 != encoder->protected_);
610 FLAC__ASSERT(0 != encoder->private_);
611 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000612
Josh Coalson3262b0d2002-08-14 20:58:42 +0000613 encoder->private_->is_being_deleted = true;
614
615 FLAC__stream_encoder_finish(encoder);
616
Josh Coalson4fa90592002-12-04 07:01:37 +0000617 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000618 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000619
620 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000621 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
622 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000623 }
624 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000625 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
626 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 +0000627 }
628 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000629 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000630
Josh Coalsonaec256b2002-03-12 16:19:54 +0000631 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000632 free(encoder->private_);
633 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000634 free(encoder);
635}
636
Josh Coalson0a15c142001-06-13 17:59:57 +0000637/***********************************************************************
638 *
639 * Public class methods
640 *
641 ***********************************************************************/
642
Josh Coalson8da98c82006-10-15 04:24:05 +0000643static FLAC__StreamEncoderInitStatus init_stream_internal_(
644 FLAC__StreamEncoder *encoder,
645 FLAC__StreamEncoderReadCallback read_callback,
646 FLAC__StreamEncoderWriteCallback write_callback,
647 FLAC__StreamEncoderSeekCallback seek_callback,
648 FLAC__StreamEncoderTellCallback tell_callback,
649 FLAC__StreamEncoderMetadataCallback metadata_callback,
650 void *client_data,
651 FLAC__bool is_ogg
652)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000653{
654 unsigned i;
Josh Coalson3957c472006-09-24 16:25:42 +0000655 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000656
Josh Coalsonf1eff452002-07-31 07:05:33 +0000657 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000658
Josh Coalsonfa697a92001-08-16 20:07:29 +0000659 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000660 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000661
Josh Coalson8da98c82006-10-15 04:24:05 +0000662#ifndef FLAC__HAS_OGG
663 if(is_ogg)
664 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
665#endif
666
Josh Coalson6b21f662006-09-13 01:42:27 +0000667 if(0 == write_callback || (seek_callback && 0 == tell_callback))
668 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000669
Josh Coalsonfa697a92001-08-16 20:07:29 +0000670 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000671 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000672
Josh Coalson425609c2006-11-03 16:08:52 +0000673 if(encoder->protected_->channels != 2) {
674 encoder->protected_->do_mid_side_stereo = false;
675 encoder->protected_->loose_mid_side_stereo = false;
676 }
677 else if(!encoder->protected_->do_mid_side_stereo)
678 encoder->protected_->loose_mid_side_stereo = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000679
Josh Coalsonfa697a92001-08-16 20:07:29 +0000680 if(encoder->protected_->bits_per_sample >= 32)
Josh Coalson6b21f662006-09-13 01:42:27 +0000681 encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000682
Josh Coalson76c68bc2002-05-17 06:22:02 +0000683 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000684 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000685
Josh Coalson0833f342002-07-15 05:31:55 +0000686 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000687 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000688
Josh Coalson425609c2006-11-03 16:08:52 +0000689 if(encoder->protected_->blocksize == 0) {
690 if(encoder->protected_->max_lpc_order == 0)
691 encoder->protected_->blocksize = 1152;
692 else
693 encoder->protected_->blocksize = 4608;
694 }
695
Josh Coalsonfa697a92001-08-16 20:07:29 +0000696 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000697 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000698
Josh Coalson20ac2c12002-08-30 05:47:14 +0000699 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000700 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000701
Josh Coalsonfa697a92001-08-16 20:07:29 +0000702 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000703 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000704
Josh Coalsonfa697a92001-08-16 20:07:29 +0000705 if(encoder->protected_->qlp_coeff_precision == 0) {
706 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000707 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
708 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000709 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 +0000710 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000711 else if(encoder->protected_->bits_per_sample == 16) {
712 if(encoder->protected_->blocksize <= 192)
713 encoder->protected_->qlp_coeff_precision = 7;
714 else if(encoder->protected_->blocksize <= 384)
715 encoder->protected_->qlp_coeff_precision = 8;
716 else if(encoder->protected_->blocksize <= 576)
717 encoder->protected_->qlp_coeff_precision = 9;
718 else if(encoder->protected_->blocksize <= 1152)
719 encoder->protected_->qlp_coeff_precision = 10;
720 else if(encoder->protected_->blocksize <= 2304)
721 encoder->protected_->qlp_coeff_precision = 11;
722 else if(encoder->protected_->blocksize <= 4608)
723 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000724 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000725 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000726 }
727 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000728 if(encoder->protected_->blocksize <= 384)
729 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
730 else if(encoder->protected_->blocksize <= 1152)
731 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
732 else
733 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000734 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000735 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000736 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000737 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
Josh Coalson6b21f662006-09-13 01:42:27 +0000738 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000739
Josh Coalsonfa697a92001-08-16 20:07:29 +0000740 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000741 if(
742 encoder->protected_->blocksize != 192 &&
743 encoder->protected_->blocksize != 576 &&
744 encoder->protected_->blocksize != 1152 &&
745 encoder->protected_->blocksize != 2304 &&
746 encoder->protected_->blocksize != 4608 &&
747 encoder->protected_->blocksize != 256 &&
748 encoder->protected_->blocksize != 512 &&
749 encoder->protected_->blocksize != 1024 &&
750 encoder->protected_->blocksize != 2048 &&
751 encoder->protected_->blocksize != 4096 &&
752 encoder->protected_->blocksize != 8192 &&
753 encoder->protected_->blocksize != 16384
754 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000755 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000756 if(
757 encoder->protected_->sample_rate != 8000 &&
758 encoder->protected_->sample_rate != 16000 &&
759 encoder->protected_->sample_rate != 22050 &&
760 encoder->protected_->sample_rate != 24000 &&
761 encoder->protected_->sample_rate != 32000 &&
762 encoder->protected_->sample_rate != 44100 &&
763 encoder->protected_->sample_rate != 48000 &&
764 encoder->protected_->sample_rate != 96000
765 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000766 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000767 if(
768 encoder->protected_->bits_per_sample != 8 &&
769 encoder->protected_->bits_per_sample != 12 &&
770 encoder->protected_->bits_per_sample != 16 &&
771 encoder->protected_->bits_per_sample != 20 &&
772 encoder->protected_->bits_per_sample != 24
773 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000774 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000775 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000776 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsond0edb972006-10-07 06:50:08 +0000777 if(
778 encoder->protected_->sample_rate <= 48000 &&
779 (
780 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
781 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
782 )
783 ) {
784 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
785 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000786 }
787
Josh Coalsonfa697a92001-08-16 20:07:29 +0000788 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
789 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
790 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
791 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000792
Josh Coalson8da98c82006-10-15 04:24:05 +0000793#if FLAC__HAS_OGG
794 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
795 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
796 unsigned i;
797 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
798 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
799 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
800 for( ; i > 0; i--)
801 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
802 encoder->protected_->metadata[0] = vc;
803 break;
804 }
805 }
806 }
807#endif
808 /* keep track of any SEEKTABLE block */
809 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
810 unsigned i;
811 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
812 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
813 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
814 break; /* take only the first one */
815 }
816 }
817 }
818
Josh Coalson66075c12002-06-01 05:39:38 +0000819 /* validate metadata */
820 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000821 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000822 metadata_has_seektable = false;
823 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000824 metadata_picture_has_type1 = false;
825 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000826 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000827 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
828 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000829 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000830 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000831 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000832 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000833 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000834 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000835 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000836 }
Josh Coalson3957c472006-09-24 16:25:42 +0000837 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000838 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000839 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000840 metadata_has_vorbis_comment = true;
841 }
Josh Coalson3957c472006-09-24 16:25:42 +0000842 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
843 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
Josh Coalson6b21f662006-09-13 01:42:27 +0000844 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000845 }
Josh Coalson3957c472006-09-24 16:25:42 +0000846 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
847 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000848 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000849 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
850 if(metadata_picture_has_type1) /* there should only be 1 per stream */
851 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
852 metadata_picture_has_type1 = true;
853 /* standard icon must be 32x32 pixel PNG */
854 if(
855 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
856 (
857 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
858 m->data.picture.width != 32 ||
859 m->data.picture.height != 32
860 )
861 )
862 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
863 }
864 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
865 if(metadata_picture_has_type2) /* there should only be 1 per stream */
866 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
867 metadata_picture_has_type2 = true;
868 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000869 }
Josh Coalson66075c12002-06-01 05:39:38 +0000870 }
871
Josh Coalsonfa697a92001-08-16 20:07:29 +0000872 encoder->private_->input_capacity = 0;
873 for(i = 0; i < encoder->protected_->channels; i++) {
874 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000875#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000876 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000877#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000878 }
879 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000880 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000881#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000882 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000883#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000884 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000885#ifndef FLAC__INTEGER_ONLY_LIBRARY
886 for(i = 0; i < encoder->protected_->num_apodizations; i++)
887 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
888 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
889#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000890 for(i = 0; i < encoder->protected_->channels; i++) {
891 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
892 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
893 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000894 }
895 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000896 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
897 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
898 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000899 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000900 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
901 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
902 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000903#ifndef FLAC__INTEGER_ONLY_LIBRARY
904 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
905#else
906 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
907 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
908 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
909 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
910 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
911 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
912 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);
913#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000914 if(encoder->private_->loose_mid_side_stereo_frames == 0)
915 encoder->private_->loose_mid_side_stereo_frames = 1;
916 encoder->private_->loose_mid_side_stereo_frame_count = 0;
917 encoder->private_->current_sample_number = 0;
918 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000919
Josh Coalsonfa697a92001-08-16 20:07:29 +0000920 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
921 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? */
922 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000923
Josh Coalsoncf30f502001-05-23 20:57:44 +0000924 /*
925 * get the CPU info and set the function pointers
926 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000927 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000928 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000929#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000930 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000931#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000932 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000933#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000934 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000935 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 +0000936 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000937#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000938 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000939#ifndef FLAC__INTEGER_ONLY_LIBRARY
940# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000941 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000942# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000943 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000944# ifdef FLAC__HAS_NASM
945# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000946 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000947 if(encoder->protected_->max_lpc_order < 4)
948 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
949 else if(encoder->protected_->max_lpc_order < 8)
950 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
951 else if(encoder->protected_->max_lpc_order < 12)
952 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000953 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000954 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000955 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000956 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000957# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000958 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000959 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000960 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000961 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000962 if(encoder->private_->cpuinfo.data.ia32.mmx) {
963 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
964 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 +0000965 }
966 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000967 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
968 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 +0000969 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000970 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
971 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
972# endif /* FLAC__HAS_NASM */
973# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000974 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000975# endif /* !FLAC__NO_ASM */
976#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000977 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000978 if(encoder->private_->use_wide_by_block) {
979 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000980 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000981
Josh Coalson8395d022001-07-12 21:25:22 +0000982 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000983 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 +0000984
Josh Coalson6b21f662006-09-13 01:42:27 +0000985 /* set state to OK; from here on, errors are fatal and we'll override the state then */
986 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
987
Josh Coalson8da98c82006-10-15 04:24:05 +0000988#if FLAC__HAS_OGG
989 encoder->private_->is_ogg = is_ogg;
990 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
991 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
992 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
993 }
994#endif
995
996 encoder->private_->read_callback = read_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000997 encoder->private_->write_callback = write_callback;
998 encoder->private_->seek_callback = seek_callback;
999 encoder->private_->tell_callback = tell_callback;
1000 encoder->private_->metadata_callback = metadata_callback;
1001 encoder->private_->client_data = client_data;
1002
Josh Coalsonf1eff452002-07-31 07:05:33 +00001003 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001004 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001005 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001006 }
Josh Coalsonaec256b2002-03-12 16:19:54 +00001007
Josh Coalson6b21f662006-09-13 01:42:27 +00001008 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
1009 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1010 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1011 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001012
1013 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +00001014 * Set up the verify stuff if necessary
1015 */
1016 if(encoder->protected_->verify) {
1017 /*
1018 * First, set up the fifo which will hold the
1019 * original signal to compare against
1020 */
1021 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
1022 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001023 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
1024 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1025 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1026 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001027 }
1028 encoder->private_->verify.input_fifo.tail = 0;
1029
1030 /*
1031 * Now set up a stream decoder for verification
1032 */
1033 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
Josh Coalson6b21f662006-09-13 01:42:27 +00001034 if(0 == encoder->private_->verify.decoder) {
1035 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1036 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1037 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001038
Josh Coalson6b21f662006-09-13 01:42:27 +00001039 if(FLAC__stream_decoder_init_stream(encoder->private_->verify.decoder, verify_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, verify_write_callback_, verify_metadata_callback_, verify_error_callback_, /*client_data=*/encoder) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
1040 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1041 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1042 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001043 }
Josh Coalson589f8c72002-08-07 23:54:55 +00001044 encoder->private_->verify.error_stats.absolute_sample = 0;
1045 encoder->private_->verify.error_stats.frame_number = 0;
1046 encoder->private_->verify.error_stats.channel = 0;
1047 encoder->private_->verify.error_stats.sample = 0;
1048 encoder->private_->verify.error_stats.expected = 0;
1049 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001050
1051 /*
Josh Coalson6b21f662006-09-13 01:42:27 +00001052 * These must be done before we write any metadata, because that
1053 * calls the write_callback, which uses these values.
1054 */
1055 encoder->private_->first_seekpoint_to_check = 0;
1056 encoder->private_->samples_written = 0;
1057 encoder->protected_->streaminfo_offset = 0;
1058 encoder->protected_->seektable_offset = 0;
1059 encoder->protected_->audio_offset = 0;
1060
1061 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001062 * write the stream header
1063 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001064 if(encoder->protected_->verify)
1065 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson6b21f662006-09-13 01:42:27 +00001066 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1067 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1068 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1069 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001070 if(!write_bitbuffer_(encoder, 0)) {
1071 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001072 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001073 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001074
Josh Coalson5c491a12002-08-01 06:39:40 +00001075 /*
1076 * write the STREAMINFO metadata block
1077 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001078 if(encoder->protected_->verify)
1079 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +00001080 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1081 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1082 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1083 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1084 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1085 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1086 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1087 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1088 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1089 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1090 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1091 memset(encoder->private_->streaminfo.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 +00001092 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +00001093 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1094 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1095 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1096 }
1097 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1098 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1099 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1100 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001101 if(!write_bitbuffer_(encoder, 0)) {
1102 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001103 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001104 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001105
Josh Coalson5c491a12002-08-01 06:39:40 +00001106 /*
1107 * Now that the STREAMINFO block is written, we can init this to an
1108 * absurdly-high value...
1109 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001110 encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +00001111 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001112 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001113
Josh Coalson5c491a12002-08-01 06:39:40 +00001114 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001115 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1116 * if not, we will write an empty one (FLAC__add_metadata_block()
1117 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001118 *
Josh Coalson8da98c82006-10-15 04:24:05 +00001119 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1120 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1121 * true it will have already insured that the metadata list is properly
1122 * ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001123 */
1124 if(!metadata_has_vorbis_comment) {
1125 FLAC__StreamMetadata vorbis_comment;
1126 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1127 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1128 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1129 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1130 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1131 vorbis_comment.data.vorbis_comment.num_comments = 0;
1132 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001133 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1134 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1135 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1136 }
1137 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1138 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1139 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1140 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001141 if(!write_bitbuffer_(encoder, 0)) {
1142 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001143 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001144 }
1145 }
1146
1147 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001148 * write the user's metadata blocks
1149 */
1150 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1151 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001152 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1153 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1154 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1155 }
1156 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1157 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1158 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1159 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001160 if(!write_bitbuffer_(encoder, 0)) {
1161 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001162 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001163 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001164 }
1165
Josh Coalson6b21f662006-09-13 01:42:27 +00001166 /* now that all the metadata is written, we save the stream offset */
1167 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
1168 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1169 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1170 }
1171
Josh Coalsond86e03b2002-08-03 21:56:15 +00001172 if(encoder->protected_->verify)
1173 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1174
Josh Coalson6b21f662006-09-13 01:42:27 +00001175 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1176}
1177
Josh Coalson8da98c82006-10-15 04:24:05 +00001178FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1179 FLAC__StreamEncoder *encoder,
1180 FLAC__StreamEncoderWriteCallback write_callback,
1181 FLAC__StreamEncoderSeekCallback seek_callback,
1182 FLAC__StreamEncoderTellCallback tell_callback,
1183 FLAC__StreamEncoderMetadataCallback metadata_callback,
1184 void *client_data
1185)
1186{
1187 return init_stream_internal_(
1188 encoder,
1189 /*read_callback=*/0,
1190 write_callback,
1191 seek_callback,
1192 tell_callback,
1193 metadata_callback,
1194 client_data,
1195 /*is_ogg=*/false
1196 );
1197}
1198
1199FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1200 FLAC__StreamEncoder *encoder,
1201 FLAC__StreamEncoderReadCallback read_callback,
1202 FLAC__StreamEncoderWriteCallback write_callback,
1203 FLAC__StreamEncoderSeekCallback seek_callback,
1204 FLAC__StreamEncoderTellCallback tell_callback,
1205 FLAC__StreamEncoderMetadataCallback metadata_callback,
1206 void *client_data
1207)
1208{
1209 return init_stream_internal_(
1210 encoder,
1211 read_callback,
1212 write_callback,
1213 seek_callback,
1214 tell_callback,
1215 metadata_callback,
1216 client_data,
1217 /*is_ogg=*/true
1218 );
1219}
1220
1221static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1222 FLAC__StreamEncoder *encoder,
1223 FILE *file,
1224 FLAC__StreamEncoderProgressCallback progress_callback,
1225 void *client_data,
1226 FLAC__bool is_ogg
1227)
Josh Coalson6b21f662006-09-13 01:42:27 +00001228{
1229 FLAC__StreamEncoderInitStatus init_status;
1230
1231 FLAC__ASSERT(0 != encoder);
1232 FLAC__ASSERT(0 != file);
1233
Josh Coalson6b21f662006-09-13 01:42:27 +00001234 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1235 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1236
1237 /* double protection */
1238 if(file == 0) {
1239 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1240 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1241 }
1242
Josh Coalson92f7fa92006-10-09 05:34:21 +00001243 /*
1244 * To make sure that our file does not go unclosed after an error, we
1245 * must assign the FILE pointer before any further error can occur in
1246 * this routine.
1247 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001248 if(file == stdout)
1249 file = get_binary_stdout_(); /* just to be safe */
1250
1251 encoder->private_->file = file;
1252
1253 encoder->private_->progress_callback = progress_callback;
1254 encoder->private_->bytes_written = 0;
1255 encoder->private_->samples_written = 0;
1256 encoder->private_->frames_written = 0;
1257
Josh Coalson8da98c82006-10-15 04:24:05 +00001258 init_status = init_stream_internal_(
1259 encoder,
1260 is_ogg? file_read_callback_ : 0,
1261 file_write_callback_,
1262 file_seek_callback_,
1263 file_tell_callback_,
1264 /*metadata_callback=*/0,
1265 client_data,
1266 is_ogg
1267 );
Josh Coalson6b21f662006-09-13 01:42:27 +00001268 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1269 /* the above function sets the state for us in case of an error */
1270 return init_status;
1271 }
1272
1273 {
1274 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1275
1276 FLAC__ASSERT(blocksize != 0);
1277 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1278 }
1279
1280 return init_status;
1281}
Josh Coalson8da98c82006-10-15 04:24:05 +00001282
1283FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1284 FLAC__StreamEncoder *encoder,
1285 FILE *file,
1286 FLAC__StreamEncoderProgressCallback progress_callback,
1287 void *client_data
1288)
1289{
1290 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1291}
1292
1293FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1294 FLAC__StreamEncoder *encoder,
1295 FILE *file,
1296 FLAC__StreamEncoderProgressCallback progress_callback,
1297 void *client_data
1298)
1299{
1300 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1301}
Josh Coalson6b21f662006-09-13 01:42:27 +00001302
Josh Coalson8da98c82006-10-15 04:24:05 +00001303static FLAC__StreamEncoderInitStatus init_file_internal_(
1304 FLAC__StreamEncoder *encoder,
1305 const char *filename,
1306 FLAC__StreamEncoderProgressCallback progress_callback,
1307 void *client_data,
1308 FLAC__bool is_ogg
1309)
Josh Coalson6b21f662006-09-13 01:42:27 +00001310{
1311 FILE *file;
1312
1313 FLAC__ASSERT(0 != encoder);
1314
1315 /*
1316 * To make sure that our file does not go unclosed after an error, we
1317 * have to do the same entrance checks here that are later performed
1318 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1319 */
1320 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1321 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1322
1323 file = filename? fopen(filename, "w+b") : stdout;
1324
1325 if(file == 0) {
1326 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1327 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1328 }
1329
Josh Coalson8da98c82006-10-15 04:24:05 +00001330 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1331}
1332
1333FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1334 FLAC__StreamEncoder *encoder,
1335 const char *filename,
1336 FLAC__StreamEncoderProgressCallback progress_callback,
1337 void *client_data
1338)
1339{
1340 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1341}
1342
1343FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1344 FLAC__StreamEncoder *encoder,
1345 const char *filename,
1346 FLAC__StreamEncoderProgressCallback progress_callback,
1347 void *client_data
1348)
1349{
1350 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001351}
1352
Josh Coalson6afed9f2002-10-16 22:29:47 +00001353FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001354{
Josh Coalsonf1eff452002-07-31 07:05:33 +00001355 FLAC__ASSERT(0 != encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001356 FLAC__ASSERT(0 != encoder->private_);
1357 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001358
Josh Coalsonfa697a92001-08-16 20:07:29 +00001359 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001360 return;
Josh Coalson2b245f22002-08-07 17:10:50 +00001361
Josh Coalson3262b0d2002-08-14 20:58:42 +00001362 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001363 if(encoder->private_->current_sample_number != 0) {
1364 encoder->protected_->blocksize = encoder->private_->current_sample_number;
Josh Coalson85aaed82006-11-09 01:19:13 +00001365 process_frame_(encoder, /*is_fractional_block=*/true);
Josh Coalson2b245f22002-08-07 17:10:50 +00001366 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001367 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001368
Josh Coalson6b21f662006-09-13 01:42:27 +00001369 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001370
Josh Coalson3262b0d2002-08-14 20:58:42 +00001371 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson8da98c82006-10-15 04:24:05 +00001372 if(encoder->private_->seek_callback) {
1373#if FLAC__HAS_OGG
1374 if(encoder->private_->is_ogg)
1375 update_ogg_metadata_(encoder);
1376 else
1377#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00001378 update_metadata_(encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001379 }
Josh Coalson6b21f662006-09-13 01:42:27 +00001380 if(encoder->private_->metadata_callback)
1381 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
Josh Coalson2b245f22002-08-07 17:10:50 +00001382 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001383
Josh Coalsond86e03b2002-08-03 21:56:15 +00001384 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
1385 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1386
Josh Coalson6b21f662006-09-13 01:42:27 +00001387 if(0 != encoder->private_->file) {
1388 if(encoder->private_->file != stdout)
1389 fclose(encoder->private_->file);
1390 encoder->private_->file = 0;
1391 }
1392
Josh Coalson8da98c82006-10-15 04:24:05 +00001393#if FLAC__HAS_OGG
1394 if(encoder->private_->is_ogg)
1395 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1396#endif
1397
Josh Coalsonf1eff452002-07-31 07:05:33 +00001398 free_(encoder);
1399 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001400
Josh Coalsonfa697a92001-08-16 20:07:29 +00001401 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001402}
1403
Josh Coalson71d5c252006-10-15 06:04:55 +00001404FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
Josh Coalson8da98c82006-10-15 04:24:05 +00001405{
1406 FLAC__ASSERT(0 != encoder);
1407 FLAC__ASSERT(0 != encoder->private_);
1408 FLAC__ASSERT(0 != encoder->protected_);
1409 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1410 return false;
1411#ifdef FLAC__HAS_OGG
1412 /* can't check encoder->private_->is_ogg since that's not set until init time */
1413 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1414 return true;
1415#else
1416 (void)value;
1417 return false;
1418#endif
1419}
1420
Josh Coalson6afed9f2002-10-16 22:29:47 +00001421FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001422{
1423 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001424 FLAC__ASSERT(0 != encoder->private_);
1425 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001426 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1427 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001428#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001429 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001430#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001431 return true;
1432}
1433
Josh Coalson6afed9f2002-10-16 22:29:47 +00001434FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001435{
Josh Coalson92031602002-07-24 06:02:11 +00001436 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001437 FLAC__ASSERT(0 != encoder->private_);
1438 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001439 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001440 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001441 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001442 return true;
1443}
1444
Josh Coalson6afed9f2002-10-16 22:29:47 +00001445FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001446{
Josh Coalson92031602002-07-24 06:02:11 +00001447 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001448 FLAC__ASSERT(0 != encoder->private_);
1449 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001450 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001451 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001452 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001453 return true;
1454}
1455
Josh Coalson6afed9f2002-10-16 22:29:47 +00001456FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001457{
Josh Coalson92031602002-07-24 06:02:11 +00001458 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001459 FLAC__ASSERT(0 != encoder->private_);
1460 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001461 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001462 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001463 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001464 return true;
1465}
1466
Josh Coalson6afed9f2002-10-16 22:29:47 +00001467FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001468{
Josh Coalson92031602002-07-24 06:02:11 +00001469 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001470 FLAC__ASSERT(0 != encoder->private_);
1471 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001472 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001473 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001474 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001475 return true;
1476}
1477
Josh Coalson425609c2006-11-03 16:08:52 +00001478FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1479{
1480 FLAC__bool ok = true;
1481 FLAC__ASSERT(0 != encoder);
1482 FLAC__ASSERT(0 != encoder->private_);
1483 FLAC__ASSERT(0 != encoder->protected_);
1484 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1485 return false;
1486 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1487 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1488 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1489 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
1490 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1491 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1492 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1493 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1494 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1495 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1496 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1497 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1498 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1499 return ok;
1500}
1501
Josh Coalson6afed9f2002-10-16 22:29:47 +00001502FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001503{
Josh Coalson92031602002-07-24 06:02:11 +00001504 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001505 FLAC__ASSERT(0 != encoder->private_);
1506 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001507 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001508 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001509 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001510 return true;
1511}
1512
Josh Coalson425609c2006-11-03 16:08:52 +00001513FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1514{
1515 FLAC__ASSERT(0 != encoder);
1516 FLAC__ASSERT(0 != encoder->private_);
1517 FLAC__ASSERT(0 != encoder->protected_);
1518 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1519 return false;
1520 encoder->protected_->do_mid_side_stereo = value;
1521 return true;
1522}
1523
1524FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1525{
1526 FLAC__ASSERT(0 != encoder);
1527 FLAC__ASSERT(0 != encoder->private_);
1528 FLAC__ASSERT(0 != encoder->protected_);
1529 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1530 return false;
1531 encoder->protected_->loose_mid_side_stereo = value;
1532 return true;
1533}
1534
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001535FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1536{
1537 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001538 FLAC__ASSERT(0 != encoder->private_);
1539 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001540 FLAC__ASSERT(0 != specification);
1541 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1542 return false;
1543#ifdef FLAC__INTEGER_ONLY_LIBRARY
1544 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1545#else
1546 encoder->protected_->num_apodizations = 0;
1547 while(1) {
1548 const char *s = strchr(specification, ';');
1549 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1550 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1551 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1552 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1553 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1554 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1555 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1556 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1557 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1558 else if(n==6 && 0 == strncmp("connes" , specification, n))
1559 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1560 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1561 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1562 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1563 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1564 if (stddev > 0.0 && stddev <= 0.5) {
1565 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1566 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1567 }
1568 }
1569 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1570 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1571 else if(n==4 && 0 == strncmp("hann" , specification, n))
1572 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1573 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1574 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1575 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1576 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1577 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1578 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1579 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1580 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1581 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1582 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1583 if (p >= 0.0 && p <= 1.0) {
1584 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1585 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1586 }
1587 }
1588 else if(n==5 && 0 == strncmp("welch" , specification, n))
1589 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1590 if (encoder->protected_->num_apodizations == 32)
1591 break;
1592 if (s)
1593 specification = s+1;
1594 else
1595 break;
1596 }
1597 if(encoder->protected_->num_apodizations == 0) {
1598 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001599 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1600 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001601 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001602#endif
1603 return true;
1604}
1605
Josh Coalson6afed9f2002-10-16 22:29:47 +00001606FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001607{
Josh Coalson92031602002-07-24 06:02:11 +00001608 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001609 FLAC__ASSERT(0 != encoder->private_);
1610 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001611 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001612 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001613 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001614 return true;
1615}
1616
Josh Coalson6afed9f2002-10-16 22:29:47 +00001617FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001618{
Josh Coalson92031602002-07-24 06:02:11 +00001619 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001620 FLAC__ASSERT(0 != encoder->private_);
1621 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001622 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001623 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001624 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001625 return true;
1626}
1627
Josh Coalson6afed9f2002-10-16 22:29:47 +00001628FLAC_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 +00001629{
Josh Coalson92031602002-07-24 06:02:11 +00001630 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001631 FLAC__ASSERT(0 != encoder->private_);
1632 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001633 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001634 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001635 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001636 return true;
1637}
1638
Josh Coalson6afed9f2002-10-16 22:29:47 +00001639FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001640{
Josh Coalson92031602002-07-24 06:02:11 +00001641 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001642 FLAC__ASSERT(0 != encoder->private_);
1643 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001644 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001645 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001646#if 0
1647 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001648 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001649#else
1650 (void)value;
1651#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001652 return true;
1653}
1654
Josh Coalson6afed9f2002-10-16 22:29:47 +00001655FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001656{
Josh Coalson92031602002-07-24 06:02:11 +00001657 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001658 FLAC__ASSERT(0 != encoder->private_);
1659 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001660 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001661 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001662 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001663 return true;
1664}
1665
Josh Coalson6afed9f2002-10-16 22:29:47 +00001666FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001667{
Josh Coalson92031602002-07-24 06:02:11 +00001668 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001669 FLAC__ASSERT(0 != encoder->private_);
1670 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001671 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001672 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001673 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001674 return true;
1675}
1676
Josh Coalson6afed9f2002-10-16 22:29:47 +00001677FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001678{
Josh Coalson92031602002-07-24 06:02:11 +00001679 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001680 FLAC__ASSERT(0 != encoder->private_);
1681 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001682 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001683 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001684 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001685 return true;
1686}
1687
Josh Coalson6afed9f2002-10-16 22:29:47 +00001688FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001689{
Josh Coalson92031602002-07-24 06:02:11 +00001690 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001691 FLAC__ASSERT(0 != encoder->private_);
1692 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001693 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001694 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001695#if 0
1696 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001697 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001698#else
1699 (void)value;
1700#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001701 return true;
1702}
1703
Josh Coalson6afed9f2002-10-16 22:29:47 +00001704FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001705{
Josh Coalson92031602002-07-24 06:02:11 +00001706 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001707 FLAC__ASSERT(0 != encoder->private_);
1708 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001709 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001710 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001711 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001712 return true;
1713}
1714
Josh Coalson6afed9f2002-10-16 22:29:47 +00001715FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001716{
Josh Coalson92031602002-07-24 06:02:11 +00001717 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001718 FLAC__ASSERT(0 != encoder->private_);
1719 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001720 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001721 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001722 encoder->protected_->metadata = metadata;
1723 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson8da98c82006-10-15 04:24:05 +00001724#if FLAC__HAS_OGG
1725 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1726 return false;
1727#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001728 return true;
1729}
1730
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001731/*
1732 * These three functions are not static, but not publically exposed in
1733 * include/FLAC/ either. They are used by the test suite.
1734 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001735FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001736{
1737 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001738 FLAC__ASSERT(0 != encoder->private_);
1739 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001740 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1741 return false;
1742 encoder->private_->disable_constant_subframes = value;
1743 return true;
1744}
1745
Josh Coalson6afed9f2002-10-16 22:29:47 +00001746FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001747{
1748 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001749 FLAC__ASSERT(0 != encoder->private_);
1750 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001751 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1752 return false;
1753 encoder->private_->disable_fixed_subframes = value;
1754 return true;
1755}
1756
Josh Coalson6afed9f2002-10-16 22:29:47 +00001757FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001758{
1759 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001760 FLAC__ASSERT(0 != encoder->private_);
1761 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001762 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1763 return false;
1764 encoder->private_->disable_verbatim_subframes = value;
1765 return true;
1766}
1767
Josh Coalson6afed9f2002-10-16 22:29:47 +00001768FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001769{
Josh Coalson92031602002-07-24 06:02:11 +00001770 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001771 FLAC__ASSERT(0 != encoder->private_);
1772 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001773 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001774}
1775
Josh Coalson6afed9f2002-10-16 22:29:47 +00001776FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001777{
1778 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001779 FLAC__ASSERT(0 != encoder->private_);
1780 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001781 if(encoder->protected_->verify)
1782 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1783 else
1784 return FLAC__STREAM_DECODER_UNINITIALIZED;
1785}
1786
Josh Coalson02954222002-11-08 06:16:31 +00001787FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1788{
Josh Coalson8da98c82006-10-15 04:24:05 +00001789 FLAC__ASSERT(0 != encoder);
1790 FLAC__ASSERT(0 != encoder->private_);
1791 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson02954222002-11-08 06:16:31 +00001792 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1793 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1794 else
Josh Coalson807140d2003-09-24 22:10:51 +00001795 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001796}
1797
Josh Coalson6afed9f2002-10-16 22:29:47 +00001798FLAC_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 +00001799{
1800 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001801 FLAC__ASSERT(0 != encoder->private_);
1802 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson589f8c72002-08-07 23:54:55 +00001803 if(0 != absolute_sample)
1804 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1805 if(0 != frame_number)
1806 *frame_number = encoder->private_->verify.error_stats.frame_number;
1807 if(0 != channel)
1808 *channel = encoder->private_->verify.error_stats.channel;
1809 if(0 != sample)
1810 *sample = encoder->private_->verify.error_stats.sample;
1811 if(0 != expected)
1812 *expected = encoder->private_->verify.error_stats.expected;
1813 if(0 != got)
1814 *got = encoder->private_->verify.error_stats.got;
1815}
1816
Josh Coalson6afed9f2002-10-16 22:29:47 +00001817FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001818{
1819 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001820 FLAC__ASSERT(0 != encoder->private_);
1821 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001822 return encoder->protected_->verify;
1823}
1824
Josh Coalson6afed9f2002-10-16 22:29:47 +00001825FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001826{
Josh Coalson92031602002-07-24 06:02:11 +00001827 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001828 FLAC__ASSERT(0 != encoder->private_);
1829 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001830 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001831}
1832
Josh Coalson6afed9f2002-10-16 22:29:47 +00001833FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001834{
Josh Coalson92031602002-07-24 06:02:11 +00001835 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001836 FLAC__ASSERT(0 != encoder->private_);
1837 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001838 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001839}
1840
Josh Coalson6afed9f2002-10-16 22:29:47 +00001841FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001842{
Josh Coalson92031602002-07-24 06:02:11 +00001843 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001844 FLAC__ASSERT(0 != encoder->private_);
1845 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001846 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001847}
1848
Josh Coalson6afed9f2002-10-16 22:29:47 +00001849FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001850{
Josh Coalson92031602002-07-24 06:02:11 +00001851 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001852 FLAC__ASSERT(0 != encoder->private_);
1853 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001854 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001855}
1856
Josh Coalson6afed9f2002-10-16 22:29:47 +00001857FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001858{
Josh Coalson92031602002-07-24 06:02:11 +00001859 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001860 FLAC__ASSERT(0 != encoder->private_);
1861 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001862 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001863}
1864
Josh Coalson425609c2006-11-03 16:08:52 +00001865FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1866{
1867 FLAC__ASSERT(0 != encoder);
1868 FLAC__ASSERT(0 != encoder->private_);
1869 FLAC__ASSERT(0 != encoder->protected_);
1870 return encoder->protected_->do_mid_side_stereo;
1871}
1872
1873FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1874{
1875 FLAC__ASSERT(0 != encoder);
1876 FLAC__ASSERT(0 != encoder->private_);
1877 FLAC__ASSERT(0 != encoder->protected_);
1878 return encoder->protected_->loose_mid_side_stereo;
1879}
1880
Josh Coalson6afed9f2002-10-16 22:29:47 +00001881FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001882{
Josh Coalson92031602002-07-24 06:02:11 +00001883 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001884 FLAC__ASSERT(0 != encoder->private_);
1885 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001886 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001887}
1888
Josh Coalson6afed9f2002-10-16 22:29:47 +00001889FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001890{
Josh Coalson92031602002-07-24 06:02:11 +00001891 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001892 FLAC__ASSERT(0 != encoder->private_);
1893 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001894 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001895}
1896
Josh Coalson6afed9f2002-10-16 22:29:47 +00001897FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001898{
Josh Coalson92031602002-07-24 06:02:11 +00001899 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001900 FLAC__ASSERT(0 != encoder->private_);
1901 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001902 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001903}
1904
Josh Coalson6afed9f2002-10-16 22:29:47 +00001905FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001906{
Josh Coalson92031602002-07-24 06:02:11 +00001907 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001908 FLAC__ASSERT(0 != encoder->private_);
1909 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001910 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001911}
1912
Josh Coalson6afed9f2002-10-16 22:29:47 +00001913FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001914{
Josh Coalson92031602002-07-24 06:02:11 +00001915 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001916 FLAC__ASSERT(0 != encoder->private_);
1917 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001918 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001919}
1920
Josh Coalson6afed9f2002-10-16 22:29:47 +00001921FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001922{
Josh Coalson92031602002-07-24 06:02:11 +00001923 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001924 FLAC__ASSERT(0 != encoder->private_);
1925 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001926 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001927}
1928
Josh Coalson6afed9f2002-10-16 22:29:47 +00001929FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001930{
Josh Coalson92031602002-07-24 06:02:11 +00001931 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001932 FLAC__ASSERT(0 != encoder->private_);
1933 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001934 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001935}
1936
Josh Coalson6afed9f2002-10-16 22:29:47 +00001937FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001938{
Josh Coalson92031602002-07-24 06:02:11 +00001939 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001940 FLAC__ASSERT(0 != encoder->private_);
1941 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001942 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001943}
1944
Josh Coalson6afed9f2002-10-16 22:29:47 +00001945FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001946{
1947 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001948 FLAC__ASSERT(0 != encoder->private_);
1949 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001950 return encoder->protected_->total_samples_estimate;
1951}
1952
Josh Coalson6afed9f2002-10-16 22:29:47 +00001953FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001954{
1955 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001956 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001957 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001958
Josh Coalsonf1eff452002-07-31 07:05:33 +00001959 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001960 FLAC__ASSERT(0 != encoder->private_);
1961 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001962 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001963
1964 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001965 /*
1966 * we have several flavors of the same basic loop, optimized for
1967 * different conditions:
1968 */
1969 if(encoder->protected_->max_lpc_order > 0) {
1970 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1971 /*
1972 * stereo coding: unroll channel loop
1973 * with LPC: calculate floating point version of signal
1974 */
1975 do {
1976 if(encoder->protected_->verify)
1977 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 +00001978
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001979 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1980 x = mid = side = buffer[0][j];
1981 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001982#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001983 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001984#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001985 x = buffer[1][j];
1986 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001987#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001988 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001989#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001990 mid += x;
1991 side -= x;
1992 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1993 encoder->private_->integer_signal_mid_side[1][i] = side;
1994 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001995#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001996 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1997 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001998#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001999 encoder->private_->current_sample_number++;
2000 }
2001 if(i == blocksize) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002002 if(!process_frame_(encoder, /*is_fractional_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002003 return false;
2004 }
2005 } while(j < samples);
2006 }
2007 else {
2008 /*
2009 * independent channel coding: buffer each channel in inner loop
2010 * with LPC: calculate floating point version of signal
2011 */
2012 do {
2013 if(encoder->protected_->verify)
2014 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2015
2016 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2017 for(channel = 0; channel < channels; channel++) {
2018 x = buffer[channel][j];
2019 encoder->private_->integer_signal[channel][i] = x;
2020#ifndef FLAC__INTEGER_ONLY_LIBRARY
2021 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2022#endif
2023 }
2024 encoder->private_->current_sample_number++;
2025 }
2026 if(i == blocksize) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002027 if(!process_frame_(encoder, /*is_fractional_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002028 return false;
2029 }
2030 } while(j < samples);
2031 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002032 }
2033 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002034 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2035 /*
2036 * stereo coding: unroll channel loop
2037 * without LPC: no need to calculate floating point version of signal
2038 */
2039 do {
2040 if(encoder->protected_->verify)
2041 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 +00002042
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002043 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2044 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
2045 x = buffer[1][j];
2046 encoder->private_->integer_signal[1][i] = x;
2047 mid += x;
2048 side -= x;
2049 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2050 encoder->private_->integer_signal_mid_side[1][i] = side;
2051 encoder->private_->integer_signal_mid_side[0][i] = mid;
2052 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002053 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002054 if(i == blocksize) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002055 if(!process_frame_(encoder, /*is_fractional_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002056 return false;
2057 }
2058 } while(j < samples);
2059 }
2060 else {
2061 /*
2062 * independent channel coding: buffer each channel in inner loop
2063 * without LPC: no need to calculate floating point version of signal
2064 */
2065 do {
2066 if(encoder->protected_->verify)
2067 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2068
2069 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2070 for(channel = 0; channel < channels; channel++)
2071 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2072 encoder->private_->current_sample_number++;
2073 }
2074 if(i == blocksize) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002075 if(!process_frame_(encoder, /*is_fractional_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002076 return false;
2077 }
2078 } while(j < samples);
2079 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002080 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002081
2082 return true;
2083}
2084
Josh Coalson6afed9f2002-10-16 22:29:47 +00002085FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002086{
2087 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00002088 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002089 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002090
Josh Coalsonf1eff452002-07-31 07:05:33 +00002091 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002092 FLAC__ASSERT(0 != encoder->private_);
2093 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002094 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002095
2096 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002097 /*
2098 * we have several flavors of the same basic loop, optimized for
2099 * different conditions:
2100 */
2101 if(encoder->protected_->max_lpc_order > 0) {
2102 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2103 /*
2104 * stereo coding: unroll channel loop
2105 * with LPC: calculate floating point version of signal
2106 */
2107 do {
2108 if(encoder->protected_->verify)
2109 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 +00002110
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002111 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2112 x = mid = side = buffer[k++];
2113 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002114#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002115 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002116#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002117 x = buffer[k++];
2118 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002119#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002120 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002121#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002122 mid += x;
2123 side -= x;
2124 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2125 encoder->private_->integer_signal_mid_side[1][i] = side;
2126 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002127#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002128 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2129 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002130#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002131 encoder->private_->current_sample_number++;
2132 }
2133 if(i == blocksize) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002134 if(!process_frame_(encoder, /*is_fractional_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002135 return false;
2136 }
2137 } while(j < samples);
2138 }
2139 else {
2140 /*
2141 * independent channel coding: buffer each channel in inner loop
2142 * with LPC: calculate floating point version of signal
2143 */
2144 do {
2145 if(encoder->protected_->verify)
2146 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2147
2148 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2149 for(channel = 0; channel < channels; channel++) {
2150 x = buffer[k++];
2151 encoder->private_->integer_signal[channel][i] = x;
2152#ifndef FLAC__INTEGER_ONLY_LIBRARY
2153 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2154#endif
2155 }
2156 encoder->private_->current_sample_number++;
2157 }
2158 if(i == blocksize) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002159 if(!process_frame_(encoder, /*is_fractional_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002160 return false;
2161 }
2162 } while(j < samples);
2163 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002164 }
2165 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002166 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2167 /*
2168 * stereo coding: unroll channel loop
2169 * without LPC: no need to calculate floating point version of signal
2170 */
2171 do {
2172 if(encoder->protected_->verify)
2173 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 +00002174
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002175 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2176 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00002177 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002178 encoder->private_->integer_signal[1][i] = x;
2179 mid += x;
2180 side -= x;
2181 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2182 encoder->private_->integer_signal_mid_side[1][i] = side;
2183 encoder->private_->integer_signal_mid_side[0][i] = mid;
2184 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002185 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002186 if(i == blocksize) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002187 if(!process_frame_(encoder, /*is_fractional_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002188 return false;
2189 }
2190 } while(j < samples);
2191 }
2192 else {
2193 /*
2194 * independent channel coding: buffer each channel in inner loop
2195 * without LPC: no need to calculate floating point version of signal
2196 */
2197 do {
2198 if(encoder->protected_->verify)
2199 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2200
2201 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2202 for(channel = 0; channel < channels; channel++)
2203 encoder->private_->integer_signal[channel][i] = buffer[k++];
2204 encoder->private_->current_sample_number++;
2205 }
2206 if(i == blocksize) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002207 if(!process_frame_(encoder, /*is_fractional_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002208 return false;
2209 }
2210 } while(j < samples);
2211 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002212 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002213
2214 return true;
2215}
2216
Josh Coalsonf1eff452002-07-31 07:05:33 +00002217/***********************************************************************
2218 *
2219 * Private class methods
2220 *
2221 ***********************************************************************/
2222
2223void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00002224{
2225 FLAC__ASSERT(0 != encoder);
2226
Josh Coalson47c7b142005-01-29 06:08:58 +00002227#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2228 encoder->protected_->verify = true;
2229#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00002230 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00002231#endif
Josh Coalson92031602002-07-24 06:02:11 +00002232 encoder->protected_->streamable_subset = true;
2233 encoder->protected_->do_mid_side_stereo = false;
2234 encoder->protected_->loose_mid_side_stereo = false;
2235 encoder->protected_->channels = 2;
2236 encoder->protected_->bits_per_sample = 16;
2237 encoder->protected_->sample_rate = 44100;
Josh Coalson425609c2006-11-03 16:08:52 +00002238 encoder->protected_->blocksize = 0;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002239#ifndef FLAC__INTEGER_ONLY_LIBRARY
2240 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00002241 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2242 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002243#endif
Josh Coalson92031602002-07-24 06:02:11 +00002244 encoder->protected_->max_lpc_order = 0;
2245 encoder->protected_->qlp_coeff_precision = 0;
2246 encoder->protected_->do_qlp_coeff_prec_search = false;
2247 encoder->protected_->do_exhaustive_model_search = false;
2248 encoder->protected_->do_escape_coding = false;
2249 encoder->protected_->min_residual_partition_order = 0;
2250 encoder->protected_->max_residual_partition_order = 0;
2251 encoder->protected_->rice_parameter_search_dist = 0;
2252 encoder->protected_->total_samples_estimate = 0;
2253 encoder->protected_->metadata = 0;
2254 encoder->protected_->num_metadata_blocks = 0;
2255
Josh Coalson6b21f662006-09-13 01:42:27 +00002256 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002257 encoder->private_->disable_constant_subframes = false;
2258 encoder->private_->disable_fixed_subframes = false;
2259 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002260#if FLAC__HAS_OGG
2261 encoder->private_->is_ogg = false;
2262#endif
2263 encoder->private_->read_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002264 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002265 encoder->private_->seek_callback = 0;
2266 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002267 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002268 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002269 encoder->private_->client_data = 0;
Josh Coalson8da98c82006-10-15 04:24:05 +00002270
2271#if FLAC__HAS_OGG
2272 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2273#endif
Josh Coalson92031602002-07-24 06:02:11 +00002274}
2275
Josh Coalsonf1eff452002-07-31 07:05:33 +00002276void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00002277{
2278 unsigned i, channel;
2279
Josh Coalsonf1eff452002-07-31 07:05:33 +00002280 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00002281 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002282 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002283 free(encoder->private_->integer_signal_unaligned[i]);
2284 encoder->private_->integer_signal_unaligned[i] = 0;
2285 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002286#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002287 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002288 free(encoder->private_->real_signal_unaligned[i]);
2289 encoder->private_->real_signal_unaligned[i] = 0;
2290 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002291#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002292 }
2293 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002294 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002295 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2296 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2297 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002298#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002299 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002300 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2301 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2302 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002303#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002304 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002305#ifndef FLAC__INTEGER_ONLY_LIBRARY
2306 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2307 if(0 != encoder->private_->window_unaligned[i]) {
2308 free(encoder->private_->window_unaligned[i]);
2309 encoder->private_->window_unaligned[i] = 0;
2310 }
2311 }
2312 if(0 != encoder->private_->windowed_signal_unaligned) {
2313 free(encoder->private_->windowed_signal_unaligned);
2314 encoder->private_->windowed_signal_unaligned = 0;
2315 }
2316#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002317 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2318 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002319 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002320 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2321 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2322 }
2323 }
2324 }
2325 for(channel = 0; channel < 2; channel++) {
2326 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002327 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002328 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2329 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2330 }
2331 }
2332 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002333 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002334 free(encoder->private_->abs_residual_unaligned);
2335 encoder->private_->abs_residual_unaligned = 0;
2336 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002337 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002338 free(encoder->private_->abs_residual_partition_sums_unaligned);
2339 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2340 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002341 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002342 free(encoder->private_->raw_bits_per_partition_unaligned);
2343 encoder->private_->raw_bits_per_partition_unaligned = 0;
2344 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002345 if(encoder->protected_->verify) {
2346 for(i = 0; i < encoder->protected_->channels; i++) {
2347 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2348 free(encoder->private_->verify.input_fifo.data[i]);
2349 encoder->private_->verify.input_fifo.data[i] = 0;
2350 }
2351 }
2352 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002353 FLAC__bitbuffer_free(encoder->private_->frame);
2354}
2355
Josh Coalson85aaed82006-11-09 01:19:13 +00002356FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002357{
Josh Coalson77e3f312001-06-23 03:03:24 +00002358 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002359 unsigned i, channel;
2360
Josh Coalson85aaed82006-11-09 01:19:13 +00002361 FLAC__ASSERT(new_blocksize > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002362 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2363 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002364
2365 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalson85aaed82006-11-09 01:19:13 +00002366 if(new_blocksize <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002367 return true;
2368
2369 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002370
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002371 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2372 * requires that the input arrays (in our case the integer signals)
2373 * have a buffer of up to 3 zeroes in front (at negative indices) for
Josh Coalson85aaed82006-11-09 01:19:13 +00002374 * alignment purposes; we use 4 in front to keep the data well-aligned.
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002375 */
Josh Coalson8395d022001-07-12 21:25:22 +00002376
Josh Coalsonfa697a92001-08-16 20:07:29 +00002377 for(i = 0; ok && i < encoder->protected_->channels; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002378 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002379 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2380 encoder->private_->integer_signal[i] += 4;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002381#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002382 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson85aaed82006-11-09 01:19:13 +00002383 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002384#endif
Josh Coalson85aaed82006-11-09 01:19:13 +00002385 }
2386 for(i = 0; ok && i < 2; i++) {
2387 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002388 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2389 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson85aaed82006-11-09 01:19:13 +00002390#ifndef FLAC__INTEGER_ONLY_LIBRARY
2391 if(encoder->protected_->max_lpc_order > 0)
2392 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
2393#endif
Josh Coalson0a15c142001-06-13 17:59:57 +00002394 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002395#ifndef FLAC__INTEGER_ONLY_LIBRARY
2396 if(ok && encoder->protected_->max_lpc_order > 0) {
2397 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
Josh Coalson85aaed82006-11-09 01:19:13 +00002398 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2399 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002400 }
2401#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002402 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002403 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002404 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
Josh Coalson0a15c142001-06-13 17:59:57 +00002405 }
2406 }
2407 for(channel = 0; ok && channel < 2; channel++) {
2408 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002409 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
Josh Coalson0a15c142001-06-13 17:59:57 +00002410 }
2411 }
Josh Coalson85aaed82006-11-09 01:19:13 +00002412 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_blocksize, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002413 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 */
Josh Coalson85aaed82006-11-09 01:19:13 +00002414 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002415 if(encoder->protected_->do_escape_coding)
Josh Coalson85aaed82006-11-09 01:19:13 +00002416 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
Josh Coalson0a15c142001-06-13 17:59:57 +00002417
Josh Coalson85aaed82006-11-09 01:19:13 +00002418 /* now adjust the windows if the blocksize has changed */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002419#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson85aaed82006-11-09 01:19:13 +00002420 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002421 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2422 switch(encoder->protected_->apodizations[i].type) {
2423 case FLAC__APODIZATION_BARTLETT:
Josh Coalson85aaed82006-11-09 01:19:13 +00002424 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002425 break;
2426 case FLAC__APODIZATION_BARTLETT_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002427 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002428 break;
2429 case FLAC__APODIZATION_BLACKMAN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002430 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002431 break;
2432 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002433 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002434 break;
2435 case FLAC__APODIZATION_CONNES:
Josh Coalson85aaed82006-11-09 01:19:13 +00002436 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002437 break;
2438 case FLAC__APODIZATION_FLATTOP:
Josh Coalson85aaed82006-11-09 01:19:13 +00002439 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002440 break;
2441 case FLAC__APODIZATION_GAUSS:
Josh Coalson85aaed82006-11-09 01:19:13 +00002442 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002443 break;
2444 case FLAC__APODIZATION_HAMMING:
Josh Coalson85aaed82006-11-09 01:19:13 +00002445 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002446 break;
2447 case FLAC__APODIZATION_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002448 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002449 break;
2450 case FLAC__APODIZATION_KAISER_BESSEL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002451 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002452 break;
2453 case FLAC__APODIZATION_NUTTALL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002454 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002455 break;
2456 case FLAC__APODIZATION_RECTANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002457 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002458 break;
2459 case FLAC__APODIZATION_TRIANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002460 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002461 break;
2462 case FLAC__APODIZATION_TUKEY:
Josh Coalson85aaed82006-11-09 01:19:13 +00002463 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002464 break;
2465 case FLAC__APODIZATION_WELCH:
Josh Coalson85aaed82006-11-09 01:19:13 +00002466 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002467 break;
2468 default:
2469 FLAC__ASSERT(0);
2470 /* double protection */
Josh Coalson85aaed82006-11-09 01:19:13 +00002471 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002472 break;
2473 }
2474 }
2475 }
2476#endif
2477
Josh Coalson85aaed82006-11-09 01:19:13 +00002478 if(ok)
2479 encoder->private_->input_capacity = new_blocksize;
2480 else
2481 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2482
Josh Coalson0a15c142001-06-13 17:59:57 +00002483 return ok;
2484}
2485
Josh Coalsond86e03b2002-08-03 21:56:15 +00002486FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00002487{
2488 const FLAC__byte *buffer;
Josh Coalson352feb52006-10-15 17:08:52 +00002489 size_t bytes;
Josh Coalson5c491a12002-08-01 06:39:40 +00002490
2491 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2492
2493 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2494
Josh Coalsond86e03b2002-08-03 21:56:15 +00002495 if(encoder->protected_->verify) {
2496 encoder->private_->verify.output.data = buffer;
2497 encoder->private_->verify.output.bytes = bytes;
2498 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2499 encoder->private_->verify.needs_magic_hack = true;
2500 }
2501 else {
2502 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2503 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2504 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2505 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2506 return false;
2507 }
2508 }
2509 }
2510
Josh Coalson6b21f662006-09-13 01:42:27 +00002511 if(write_frame_(encoder, buffer, bytes, samples) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002512 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002513 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002514 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002515 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002516
2517 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2518
Josh Coalsond86e03b2002-08-03 21:56:15 +00002519 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002520 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2521 encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00002522 }
2523
Josh Coalson5c491a12002-08-01 06:39:40 +00002524 return true;
2525}
2526
Josh Coalson352feb52006-10-15 17:08:52 +00002527FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples)
Josh Coalson6b21f662006-09-13 01:42:27 +00002528{
2529 FLAC__StreamEncoderWriteStatus status;
2530 FLAC__uint64 output_position = 0;
2531
2532 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2533 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2534 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2535 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2536 }
2537
2538 /*
2539 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2540 */
2541 if(samples == 0) {
2542 FLAC__MetadataType type = (buffer[0] & 0x7f);
2543 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2544 encoder->protected_->streaminfo_offset = output_position;
2545 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2546 encoder->protected_->seektable_offset = output_position;
2547 }
2548
2549 /*
2550 * Mark the current seek point if hit (if audio_offset == 0 that
2551 * means we're still writing metadata and haven't hit the first
2552 * frame yet)
2553 */
2554 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2555 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2556 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2557 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2558 FLAC__uint64 test_sample;
2559 unsigned i;
2560 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2561 test_sample = encoder->private_->seek_table->points[i].sample_number;
2562 if(test_sample > frame_last_sample) {
2563 break;
2564 }
2565 else if(test_sample >= frame_first_sample) {
2566 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2567 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2568 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2569 encoder->private_->first_seekpoint_to_check++;
2570 /* DO NOT: "break;" and here's why:
2571 * The seektable template may contain more than one target
2572 * sample for any given frame; we will keep looping, generating
2573 * duplicate seekpoints for them, and we'll clean it up later,
2574 * just before writing the seektable back to the metadata.
2575 */
2576 }
2577 else {
2578 encoder->private_->first_seekpoint_to_check++;
2579 }
2580 }
2581 }
2582
Josh Coalson8da98c82006-10-15 04:24:05 +00002583#if FLAC__HAS_OGG
2584 if(encoder->private_->is_ogg) {
2585 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2586 &encoder->protected_->ogg_encoder_aspect,
2587 FLAC__stream_encoder_get_total_samples_estimate(encoder),
2588 buffer,
2589 bytes,
2590 samples,
2591 encoder->private_->current_frame_number,
2592 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2593 encoder,
2594 encoder->private_->client_data
2595 );
2596 }
2597 else
2598#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00002599 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2600
2601 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2602 encoder->private_->bytes_written += bytes;
2603 encoder->private_->samples_written += samples;
2604 /* we keep a high watermark on the number of frames written because
2605 * when the encoder goes back to write metadata, 'current_frame'
2606 * will drop back to 0.
2607 */
2608 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2609 }
2610 else
2611 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2612
2613 return status;
2614}
2615
2616/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2617void update_metadata_(const FLAC__StreamEncoder *encoder)
2618{
2619 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2620 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2621 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2622 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2623 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2624 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2625 FLAC__StreamEncoderSeekStatus seek_status;
2626
2627 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2628
2629 /* All this is based on intimate knowledge of the stream header
2630 * layout, but a change to the header format that would break this
2631 * would also break all streams encoded in the previous format.
2632 */
2633
2634 /*
2635 * Write MD5 signature
2636 */
2637 {
2638 const unsigned md5_offset =
2639 FLAC__STREAM_METADATA_HEADER_LENGTH +
2640 (
2641 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2642 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2643 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2644 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2645 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2646 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2647 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2648 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2649 ) / 8;
2650
2651 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2652 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2653 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2654 return;
2655 }
2656 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2657 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2658 return;
2659 }
2660 }
2661
2662 /*
2663 * Write total samples
2664 */
2665 {
2666 const unsigned total_samples_byte_offset =
2667 FLAC__STREAM_METADATA_HEADER_LENGTH +
2668 (
2669 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2670 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2671 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2672 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2673 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2674 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2675 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2676 - 4
2677 ) / 8;
2678
2679 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2680 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2681 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2682 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2683 b[4] = (FLAC__byte)(samples & 0xFF);
2684 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + total_samples_byte_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2685 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2686 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2687 return;
2688 }
2689 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2690 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2691 return;
2692 }
2693 }
2694
2695 /*
2696 * Write min/max framesize
2697 */
2698 {
2699 const unsigned min_framesize_offset =
2700 FLAC__STREAM_METADATA_HEADER_LENGTH +
2701 (
2702 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2703 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2704 ) / 8;
2705
2706 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2707 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2708 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2709 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2710 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2711 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2712 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + min_framesize_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2713 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2714 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2715 return;
2716 }
2717 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2718 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2719 return;
2720 }
2721 }
2722
2723 /*
2724 * Write seektable
2725 */
2726 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2727 unsigned i;
2728
2729 FLAC__format_seektable_sort(encoder->private_->seek_table);
2730
2731 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2732
2733 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2734 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2735 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2736 return;
2737 }
2738
2739 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2740 FLAC__uint64 xx;
2741 unsigned x;
2742 xx = encoder->private_->seek_table->points[i].sample_number;
2743 b[7] = (FLAC__byte)xx; xx >>= 8;
2744 b[6] = (FLAC__byte)xx; xx >>= 8;
2745 b[5] = (FLAC__byte)xx; xx >>= 8;
2746 b[4] = (FLAC__byte)xx; xx >>= 8;
2747 b[3] = (FLAC__byte)xx; xx >>= 8;
2748 b[2] = (FLAC__byte)xx; xx >>= 8;
2749 b[1] = (FLAC__byte)xx; xx >>= 8;
2750 b[0] = (FLAC__byte)xx; xx >>= 8;
2751 xx = encoder->private_->seek_table->points[i].stream_offset;
2752 b[15] = (FLAC__byte)xx; xx >>= 8;
2753 b[14] = (FLAC__byte)xx; xx >>= 8;
2754 b[13] = (FLAC__byte)xx; xx >>= 8;
2755 b[12] = (FLAC__byte)xx; xx >>= 8;
2756 b[11] = (FLAC__byte)xx; xx >>= 8;
2757 b[10] = (FLAC__byte)xx; xx >>= 8;
2758 b[9] = (FLAC__byte)xx; xx >>= 8;
2759 b[8] = (FLAC__byte)xx; xx >>= 8;
2760 x = encoder->private_->seek_table->points[i].frame_samples;
2761 b[17] = (FLAC__byte)x; x >>= 8;
2762 b[16] = (FLAC__byte)x; x >>= 8;
2763 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2764 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2765 return;
2766 }
2767 }
2768 }
2769}
2770
Josh Coalson15b8eb82006-10-15 05:15:55 +00002771#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00002772/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2773void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2774{
2775 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2776 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2777 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2778 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2779 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2780 ogg_page page;
2781
2782 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2783
2784 /* All this is based on intimate knowledge of the stream header
2785 * layout, but a change to the header format that would break this
2786 * would also break all streams encoded in the previous format.
2787 */
2788
2789 /**
2790 ** Write STREAMINFO stats
2791 **/
2792 simple_ogg_page__init(&page);
2793 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2794 simple_ogg_page__clear(&page);
2795 return; /* state already set */
2796 }
2797
2798 /*
2799 * Write MD5 signature
2800 */
2801 {
2802 const unsigned md5_offset =
2803 FLAC__STREAM_METADATA_HEADER_LENGTH +
2804 (
2805 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2806 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2807 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2808 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2809 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2810 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2811 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2812 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2813 ) / 8;
2814
2815 if(md5_offset + 16 > (unsigned)page.body_len) {
2816 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2817 simple_ogg_page__clear(&page);
2818 return;
2819 }
2820 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2821 }
2822
2823 /*
2824 * Write total samples
2825 */
2826 {
2827 const unsigned total_samples_byte_offset =
2828 FLAC__STREAM_METADATA_HEADER_LENGTH +
2829 (
2830 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2831 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2832 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2833 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2834 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2835 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2836 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2837 - 4
2838 ) / 8;
2839
2840 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2841 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2842 simple_ogg_page__clear(&page);
2843 return;
2844 }
2845 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2846 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2847 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2848 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2849 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2850 b[4] = (FLAC__byte)(samples & 0xFF);
2851 memcpy(page.body + total_samples_byte_offset, b, 5);
2852 }
2853
2854 /*
2855 * Write min/max framesize
2856 */
2857 {
2858 const unsigned min_framesize_offset =
2859 FLAC__STREAM_METADATA_HEADER_LENGTH +
2860 (
2861 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2862 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2863 ) / 8;
2864
2865 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2866 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2867 simple_ogg_page__clear(&page);
2868 return;
2869 }
2870 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2871 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2872 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2873 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2874 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2875 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2876 memcpy(page.body + min_framesize_offset, b, 6);
2877 }
2878 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2879 simple_ogg_page__clear(&page);
2880 return; /* state already set */
2881 }
2882 simple_ogg_page__clear(&page);
2883
2884 /*
2885 * Write seektable
2886 */
2887 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2888 unsigned i;
2889 FLAC__byte *p;
2890
2891 FLAC__format_seektable_sort(encoder->private_->seek_table);
2892
2893 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2894
2895 simple_ogg_page__init(&page);
2896 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2897 simple_ogg_page__clear(&page);
2898 return; /* state already set */
2899 }
2900
2901 if(FLAC__STREAM_METADATA_HEADER_LENGTH + (18*encoder->private_->seek_table->num_points) > (unsigned)page.body_len) {
2902 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2903 simple_ogg_page__clear(&page);
2904 return;
2905 }
2906
2907 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
2908 FLAC__uint64 xx;
2909 unsigned x;
2910 xx = encoder->private_->seek_table->points[i].sample_number;
2911 b[7] = (FLAC__byte)xx; xx >>= 8;
2912 b[6] = (FLAC__byte)xx; xx >>= 8;
2913 b[5] = (FLAC__byte)xx; xx >>= 8;
2914 b[4] = (FLAC__byte)xx; xx >>= 8;
2915 b[3] = (FLAC__byte)xx; xx >>= 8;
2916 b[2] = (FLAC__byte)xx; xx >>= 8;
2917 b[1] = (FLAC__byte)xx; xx >>= 8;
2918 b[0] = (FLAC__byte)xx; xx >>= 8;
2919 xx = encoder->private_->seek_table->points[i].stream_offset;
2920 b[15] = (FLAC__byte)xx; xx >>= 8;
2921 b[14] = (FLAC__byte)xx; xx >>= 8;
2922 b[13] = (FLAC__byte)xx; xx >>= 8;
2923 b[12] = (FLAC__byte)xx; xx >>= 8;
2924 b[11] = (FLAC__byte)xx; xx >>= 8;
2925 b[10] = (FLAC__byte)xx; xx >>= 8;
2926 b[9] = (FLAC__byte)xx; xx >>= 8;
2927 b[8] = (FLAC__byte)xx; xx >>= 8;
2928 x = encoder->private_->seek_table->points[i].frame_samples;
2929 b[17] = (FLAC__byte)x; x >>= 8;
2930 b[16] = (FLAC__byte)x; x >>= 8;
2931 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2932 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2933 simple_ogg_page__clear(&page);
2934 return;
2935 }
2936 memcpy(p, b, 18);
2937 }
2938
2939 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2940 simple_ogg_page__clear(&page);
2941 return; /* state already set */
2942 }
2943 simple_ogg_page__clear(&page);
2944 }
2945}
Josh Coalson15b8eb82006-10-15 05:15:55 +00002946#endif
Josh Coalson8da98c82006-10-15 04:24:05 +00002947
Josh Coalson85aaed82006-11-09 01:19:13 +00002948FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
Josh Coalson0a15c142001-06-13 17:59:57 +00002949{
Josh Coalsonfa697a92001-08-16 20:07:29 +00002950 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002951
2952 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002953 * Accumulate raw signal to the MD5 signature
2954 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00002955 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 +00002956 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002957 return false;
2958 }
2959
2960 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002961 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002962 */
Josh Coalson85aaed82006-11-09 01:19:13 +00002963 if(!process_subframes_(encoder, is_fractional_block)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002964 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002965 return false;
2966 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002967
2968 /*
2969 * Zero-pad the frame to a byte_boundary
2970 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002971 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002972 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002973 return false;
2974 }
2975
2976 /*
Josh Coalson215af572001-03-27 01:15:58 +00002977 * CRC-16 the whole thing
2978 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002979 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2980 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 +00002981
2982 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002983 * Write it
2984 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00002985 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
2986 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002987 return false;
2988 }
2989
2990 /*
2991 * Get ready for the next frame
2992 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002993 encoder->private_->current_sample_number = 0;
2994 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00002995 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002996
2997 return true;
2998}
2999
Josh Coalson85aaed82006-11-09 01:19:13 +00003000FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003001{
3002 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003003 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003004 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003005
3006 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00003007 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00003008 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003009 if(is_fractional_block) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003010 max_partition_order = 0;
3011 }
3012 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003013 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
3014 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003015 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003016 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003017
Josh Coalsonfa697a92001-08-16 20:07:29 +00003018 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 +00003019
Josh Coalson94e02cd2001-01-25 10:41:06 +00003020 /*
3021 * Setup the frame
3022 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003023 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003024 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003025 return false;
3026 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00003027 frame_header.blocksize = encoder->protected_->blocksize;
3028 frame_header.sample_rate = encoder->protected_->sample_rate;
3029 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003030 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003031 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003032 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003033 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003034
3035 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003036 * Figure out what channel assignments to try
3037 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003038 if(encoder->protected_->do_mid_side_stereo) {
3039 if(encoder->protected_->loose_mid_side_stereo) {
3040 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003041 do_independent = true;
3042 do_mid_side = true;
3043 }
3044 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003045 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003046 do_mid_side = !do_independent;
3047 }
3048 }
3049 else {
3050 do_independent = true;
3051 do_mid_side = true;
3052 }
3053 }
3054 else {
3055 do_independent = true;
3056 do_mid_side = false;
3057 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003058
Josh Coalson1b689822001-05-31 20:11:02 +00003059 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003060
3061 /*
Josh Coalson82b73242001-03-28 22:17:05 +00003062 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00003063 */
3064 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003065 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003066 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003067 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3068 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00003069 }
Josh Coalson859bc542001-03-27 22:22:27 +00003070 }
3071 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003072 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00003073 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003074 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003075 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3076 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00003077 }
Josh Coalson859bc542001-03-27 22:22:27 +00003078 }
3079
3080 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003081 * First do a normal encoding pass of each independent channel
3082 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003083 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003084 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003085 if(!
3086 process_subframe_(
3087 encoder,
3088 min_partition_order,
3089 max_partition_order,
3090 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003091 &frame_header,
3092 encoder->private_->subframe_bps[channel],
3093 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003094#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003095 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003096#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003097 encoder->private_->subframe_workspace_ptr[channel],
3098 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3099 encoder->private_->residual_workspace[channel],
3100 encoder->private_->best_subframe+channel,
3101 encoder->private_->best_subframe_bits+channel
3102 )
3103 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003104 return false;
3105 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003106 }
3107
3108 /*
3109 * Now do mid and side channels if requested
3110 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003111 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003112 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003113
3114 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003115 if(!
3116 process_subframe_(
3117 encoder,
3118 min_partition_order,
3119 max_partition_order,
3120 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003121 &frame_header,
3122 encoder->private_->subframe_bps_mid_side[channel],
3123 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003124#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003125 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003126#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003127 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3128 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3129 encoder->private_->residual_workspace_mid_side[channel],
3130 encoder->private_->best_subframe_mid_side+channel,
3131 encoder->private_->best_subframe_bits_mid_side+channel
3132 )
3133 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00003134 return false;
3135 }
3136 }
3137
3138 /*
3139 * Compose the frame bitbuffer
3140 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003141 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00003142 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3143 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003144 FLAC__ChannelAssignment channel_assignment;
3145
Josh Coalsonfa697a92001-08-16 20:07:29 +00003146 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003147
Josh Coalsonfa697a92001-08-16 20:07:29 +00003148 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3149 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 +00003150 }
3151 else {
3152 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3153 unsigned min_bits;
3154 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003155
Josh Coalson1b689822001-05-31 20:11:02 +00003156 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003157
3158 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003159 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3160 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3161 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3162 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 +00003163
Josh Coalson7424d2f2002-11-06 07:10:38 +00003164 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 +00003165 if(bits[ca] < min_bits) {
3166 min_bits = bits[ca];
3167 channel_assignment = ca;
3168 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003169 }
3170 }
3171
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003172 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003173
Josh Coalsond0edb972006-10-07 06:50:08 +00003174 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003175 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003176 return false;
3177 }
3178
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003179 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003180 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003181 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3182 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003183 break;
3184 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003185 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3186 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003187 break;
3188 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003189 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3190 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003191 break;
3192 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003193 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3194 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003195 break;
3196 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003197 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003198 }
Josh Coalson82b73242001-03-28 22:17:05 +00003199
3200 switch(channel_assignment) {
3201 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003202 left_bps = encoder->private_->subframe_bps [0];
3203 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003204 break;
3205 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003206 left_bps = encoder->private_->subframe_bps [0];
3207 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003208 break;
3209 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003210 left_bps = encoder->private_->subframe_bps_mid_side[1];
3211 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003212 break;
3213 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003214 left_bps = encoder->private_->subframe_bps_mid_side[0];
3215 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003216 break;
3217 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003218 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00003219 }
3220
3221 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00003222 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003223 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00003224 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003225 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003226 }
3227 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00003228 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003229 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003230 return false;
3231 }
3232
Josh Coalsonfa697a92001-08-16 20:07:29 +00003233 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson369a6da2006-10-10 00:37:48 +00003234 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003235 /* the above function sets the state for us in case of an error */
3236 return false;
3237 }
3238 }
3239 }
3240
Josh Coalsonfa697a92001-08-16 20:07:29 +00003241 if(encoder->protected_->loose_mid_side_stereo) {
3242 encoder->private_->loose_mid_side_stereo_frame_count++;
3243 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3244 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003245 }
3246
Josh Coalsonfa697a92001-08-16 20:07:29 +00003247 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003248
Josh Coalson94e02cd2001-01-25 10:41:06 +00003249 return true;
3250}
3251
Josh Coalson6fe72f72002-08-20 04:01:59 +00003252FLAC__bool process_subframe_(
3253 FLAC__StreamEncoder *encoder,
3254 unsigned min_partition_order,
3255 unsigned max_partition_order,
3256 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003257 const FLAC__FrameHeader *frame_header,
3258 unsigned subframe_bps,
3259 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003260#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003261 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003262#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003263 FLAC__Subframe *subframe[2],
3264 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3265 FLAC__int32 *residual[2],
3266 unsigned *best_subframe,
3267 unsigned *best_bits
3268)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003269{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003270#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003271 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003272#else
3273 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3274#endif
3275#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003276 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003277 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 +00003278 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003279 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003280 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003281#endif
3282 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003283 unsigned rice_parameter;
3284 unsigned _candidate_bits, _best_bits;
3285 unsigned _best_subframe;
3286
3287 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3288 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003289 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3290 _best_bits = UINT_MAX;
3291 else
3292 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003293
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003294 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3295 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003296 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 +00003297 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003298 if(
3299 !encoder->private_->disable_constant_subframes &&
3300#ifndef FLAC__INTEGER_ONLY_LIBRARY
3301 fixed_residual_bits_per_sample[1] == 0.0
3302#else
3303 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3304#endif
3305 ) {
3306 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003307 unsigned i;
3308 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003309 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003310 if(integer_signal[0] != integer_signal[i]) {
3311 signal_is_constant = false;
3312 break;
3313 }
3314 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003315 }
3316 if(signal_is_constant) {
3317 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
3318 if(_candidate_bits < _best_bits) {
3319 _best_subframe = !_best_subframe;
3320 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003321 }
3322 }
3323 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003324 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3325 /* encode fixed */
3326 if(encoder->protected_->do_exhaustive_model_search) {
3327 min_fixed_order = 0;
3328 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00003329 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003330 else {
3331 min_fixed_order = max_fixed_order = guess_fixed_order;
3332 }
3333 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003334#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003335 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003336 continue; /* don't even try */
3337 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 +00003338#else
3339 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3340 continue; /* don't even try */
3341 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 */
3342#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003343 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003344 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3345#ifdef DEBUG_VERBOSE
3346 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3347#endif
3348 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3349 }
3350 _candidate_bits =
3351 evaluate_fixed_subframe_(
3352 encoder,
3353 integer_signal,
3354 residual[!_best_subframe],
3355 encoder->private_->abs_residual,
3356 encoder->private_->abs_residual_partition_sums,
3357 encoder->private_->raw_bits_per_partition,
3358 frame_header->blocksize,
3359 subframe_bps,
3360 fixed_order,
3361 rice_parameter,
3362 min_partition_order,
3363 max_partition_order,
3364 precompute_partition_sums,
3365 encoder->protected_->do_escape_coding,
3366 encoder->protected_->rice_parameter_search_dist,
3367 subframe[!_best_subframe],
3368 partitioned_rice_contents[!_best_subframe]
3369 );
3370 if(_candidate_bits < _best_bits) {
3371 _best_subframe = !_best_subframe;
3372 _best_bits = _candidate_bits;
3373 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003374 }
3375 }
3376
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003377#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00003378 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003379 if(encoder->protected_->max_lpc_order > 0) {
3380 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003381 max_lpc_order = frame_header->blocksize-1;
3382 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00003383 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003384 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003385 unsigned a;
3386 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00003387 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003388 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3389 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3390 if(autoc[0] != 0.0) {
3391 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3392 if(encoder->protected_->do_exhaustive_model_search) {
3393 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003394 }
3395 else {
Josh Coalsondf598452006-04-28 00:13:34 +00003396 const unsigned guess_lpc_order =
3397 FLAC__lpc_compute_best_order(
3398 lpc_error,
3399 max_lpc_order,
3400 frame_header->blocksize,
3401 subframe_bps + (
3402 encoder->protected_->do_qlp_coeff_prec_search?
3403 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3404 encoder->protected_->qlp_coeff_precision
3405 )
3406 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003407 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003408 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003409 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3410 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3411 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3412 continue; /* don't even try */
3413 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3414 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3415 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00003416#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003417 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 +00003418#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003419 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3420 }
3421 if(encoder->protected_->do_qlp_coeff_prec_search) {
3422 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00003423 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3424 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003425 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00003426 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3427 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003428 else
3429 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3430 }
3431 else {
3432 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3433 }
3434 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3435 _candidate_bits =
3436 evaluate_lpc_subframe_(
3437 encoder,
3438 integer_signal,
3439 residual[!_best_subframe],
3440 encoder->private_->abs_residual,
3441 encoder->private_->abs_residual_partition_sums,
3442 encoder->private_->raw_bits_per_partition,
3443 encoder->private_->lp_coeff[lpc_order-1],
3444 frame_header->blocksize,
3445 subframe_bps,
3446 lpc_order,
3447 qlp_coeff_precision,
3448 rice_parameter,
3449 min_partition_order,
3450 max_partition_order,
3451 precompute_partition_sums,
3452 encoder->protected_->do_escape_coding,
3453 encoder->protected_->rice_parameter_search_dist,
3454 subframe[!_best_subframe],
3455 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003456 );
3457 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3458 if(_candidate_bits < _best_bits) {
3459 _best_subframe = !_best_subframe;
3460 _best_bits = _candidate_bits;
3461 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00003462 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003463 }
3464 }
3465 }
3466 }
3467 }
3468 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003469#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00003470 }
3471 }
3472
Josh Coalson72695802002-10-11 06:25:16 +00003473 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3474 if(_best_bits == UINT_MAX) {
3475 FLAC__ASSERT(_best_subframe == 0);
3476 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3477 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003478
Josh Coalson94e02cd2001-01-25 10:41:06 +00003479 *best_subframe = _best_subframe;
3480 *best_bits = _best_bits;
3481
3482 return true;
3483}
3484
Josh Coalson6fe72f72002-08-20 04:01:59 +00003485FLAC__bool add_subframe_(
3486 FLAC__StreamEncoder *encoder,
3487 const FLAC__FrameHeader *frame_header,
3488 unsigned subframe_bps,
3489 const FLAC__Subframe *subframe,
3490 FLAC__BitBuffer *frame
3491)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003492{
3493 switch(subframe->type) {
3494 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003495 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003496 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003497 return false;
3498 }
3499 break;
3500 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00003501 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003502 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003503 return false;
3504 }
3505 break;
3506 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00003507 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003508 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003509 return false;
3510 }
3511 break;
3512 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00003513 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003514 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003515 return false;
3516 }
3517 break;
3518 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003519 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003520 }
3521
3522 return true;
3523}
3524
Josh Coalson6fe72f72002-08-20 04:01:59 +00003525unsigned evaluate_constant_subframe_(
3526 const FLAC__int32 signal,
3527 unsigned subframe_bps,
3528 FLAC__Subframe *subframe
3529)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003530{
3531 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3532 subframe->data.constant.value = signal;
3533
Josh Coalson82b73242001-03-28 22:17:05 +00003534 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 +00003535}
3536
Josh Coalson6fe72f72002-08-20 04:01:59 +00003537unsigned evaluate_fixed_subframe_(
3538 FLAC__StreamEncoder *encoder,
3539 const FLAC__int32 signal[],
3540 FLAC__int32 residual[],
3541 FLAC__uint32 abs_residual[],
3542 FLAC__uint64 abs_residual_partition_sums[],
3543 unsigned raw_bits_per_partition[],
3544 unsigned blocksize,
3545 unsigned subframe_bps,
3546 unsigned order,
3547 unsigned rice_parameter,
3548 unsigned min_partition_order,
3549 unsigned max_partition_order,
3550 FLAC__bool precompute_partition_sums,
3551 FLAC__bool do_escape_coding,
3552 unsigned rice_parameter_search_dist,
3553 FLAC__Subframe *subframe,
3554 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3555)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003556{
3557 unsigned i, residual_bits;
3558 const unsigned residual_samples = blocksize - order;
3559
3560 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3561
3562 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3563
3564 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003565 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003566 subframe->data.fixed.residual = residual;
3567
Josh Coalson6fe72f72002-08-20 04:01:59 +00003568 residual_bits =
3569 find_best_partition_order_(
3570 encoder->private_,
3571 residual,
3572 abs_residual,
3573 abs_residual_partition_sums,
3574 raw_bits_per_partition,
3575 residual_samples,
3576 order,
3577 rice_parameter,
3578 min_partition_order,
3579 max_partition_order,
3580 precompute_partition_sums,
3581 do_escape_coding,
3582 rice_parameter_search_dist,
3583 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3584 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003585
3586 subframe->data.fixed.order = order;
3587 for(i = 0; i < order; i++)
3588 subframe->data.fixed.warmup[i] = signal[i];
3589
Josh Coalson82b73242001-03-28 22:17:05 +00003590 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 +00003591}
3592
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003593#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003594unsigned evaluate_lpc_subframe_(
3595 FLAC__StreamEncoder *encoder,
3596 const FLAC__int32 signal[],
3597 FLAC__int32 residual[],
3598 FLAC__uint32 abs_residual[],
3599 FLAC__uint64 abs_residual_partition_sums[],
3600 unsigned raw_bits_per_partition[],
3601 const FLAC__real lp_coeff[],
3602 unsigned blocksize,
3603 unsigned subframe_bps,
3604 unsigned order,
3605 unsigned qlp_coeff_precision,
3606 unsigned rice_parameter,
3607 unsigned min_partition_order,
3608 unsigned max_partition_order,
3609 FLAC__bool precompute_partition_sums,
3610 FLAC__bool do_escape_coding,
3611 unsigned rice_parameter_search_dist,
3612 FLAC__Subframe *subframe,
3613 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3614)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003615{
Josh Coalson77e3f312001-06-23 03:03:24 +00003616 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003617 unsigned i, residual_bits;
3618 int quantization, ret;
3619 const unsigned residual_samples = blocksize - order;
3620
Josh Coalson20ac2c12002-08-30 05:47:14 +00003621 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3622 if(subframe_bps <= 16) {
3623 FLAC__ASSERT(order > 0);
3624 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3625 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3626 }
3627
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003628 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003629 if(ret != 0)
3630 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3631
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003632 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3633 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3634 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3635 else
3636 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 +00003637 else
3638 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 +00003639
3640 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3641
3642 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003643 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003644 subframe->data.lpc.residual = residual;
3645
Josh Coalson6fe72f72002-08-20 04:01:59 +00003646 residual_bits =
3647 find_best_partition_order_(
3648 encoder->private_,
3649 residual,
3650 abs_residual,
3651 abs_residual_partition_sums,
3652 raw_bits_per_partition,
3653 residual_samples,
3654 order,
3655 rice_parameter,
3656 min_partition_order,
3657 max_partition_order,
3658 precompute_partition_sums,
3659 do_escape_coding,
3660 rice_parameter_search_dist,
3661 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3662 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003663
3664 subframe->data.lpc.order = order;
3665 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3666 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003667 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003668 for(i = 0; i < order; i++)
3669 subframe->data.lpc.warmup[i] = signal[i];
3670
Josh Coalson82b73242001-03-28 22:17:05 +00003671 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 +00003672}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003673#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003674
Josh Coalson6fe72f72002-08-20 04:01:59 +00003675unsigned evaluate_verbatim_subframe_(
3676 const FLAC__int32 signal[],
3677 unsigned blocksize,
3678 unsigned subframe_bps,
3679 FLAC__Subframe *subframe
3680)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003681{
3682 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3683
3684 subframe->data.verbatim.data = signal;
3685
Josh Coalson82b73242001-03-28 22:17:05 +00003686 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 +00003687}
3688
Josh Coalson6fe72f72002-08-20 04:01:59 +00003689unsigned find_best_partition_order_(
3690 FLAC__StreamEncoderPrivate *private_,
3691 const FLAC__int32 residual[],
3692 FLAC__uint32 abs_residual[],
3693 FLAC__uint64 abs_residual_partition_sums[],
3694 unsigned raw_bits_per_partition[],
3695 unsigned residual_samples,
3696 unsigned predictor_order,
3697 unsigned rice_parameter,
3698 unsigned min_partition_order,
3699 unsigned max_partition_order,
3700 FLAC__bool precompute_partition_sums,
3701 FLAC__bool do_escape_coding,
3702 unsigned rice_parameter_search_dist,
3703 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3704)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003705{
Josh Coalson77e3f312001-06-23 03:03:24 +00003706 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003707 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00003708 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00003709 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003710 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003711
Josh Coalson2051dd42001-04-12 22:22:34 +00003712 /* compute abs(residual) for use later */
3713 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3714 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00003715 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00003716 }
3717
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003718 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 +00003719 min_partition_order = min(min_partition_order, max_partition_order);
3720
Josh Coalson8395d022001-07-12 21:25:22 +00003721 if(precompute_partition_sums) {
3722 int partition_order;
3723 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003724
Josh Coalsonf1eff452002-07-31 07:05:33 +00003725 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 +00003726
3727 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00003728 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 +00003729
3730 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3731#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003732 if(!
3733 set_partitioned_rice_with_precompute_(
3734 residual,
3735 abs_residual_partition_sums+sum,
3736 raw_bits_per_partition+sum,
3737 residual_samples,
3738 predictor_order,
3739 rice_parameter,
3740 rice_parameter_search_dist,
3741 (unsigned)partition_order,
3742 do_escape_coding,
3743 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3744 &residual_bits
3745 )
3746 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003747#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003748 if(!
3749 set_partitioned_rice_with_precompute_(
3750 abs_residual,
3751 abs_residual_partition_sums+sum,
3752 raw_bits_per_partition+sum,
3753 residual_samples,
3754 predictor_order,
3755 rice_parameter,
3756 rice_parameter_search_dist,
3757 (unsigned)partition_order,
3758 do_escape_coding,
3759 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3760 &residual_bits
3761 )
3762 )
Josh Coalson8395d022001-07-12 21:25:22 +00003763#endif
3764 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003765 FLAC__ASSERT(best_residual_bits != 0);
3766 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003767 }
3768 sum += 1u << partition_order;
3769 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3770 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003771 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003772 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003773 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003774 }
3775 }
Josh Coalson8395d022001-07-12 21:25:22 +00003776 else {
3777 unsigned partition_order;
3778 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3779#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003780 if(!
3781 set_partitioned_rice_(
3782 abs_residual,
3783 residual,
3784 residual_samples,
3785 predictor_order,
3786 rice_parameter,
3787 rice_parameter_search_dist,
3788 partition_order,
3789 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3790 &residual_bits
3791 )
3792 )
Josh Coalson8395d022001-07-12 21:25:22 +00003793#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003794 if(!
3795 set_partitioned_rice_(
3796 abs_residual,
3797 residual_samples,
3798 predictor_order,
3799 rice_parameter,
3800 rice_parameter_search_dist,
3801 partition_order,
3802 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3803 &residual_bits
3804 )
3805 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003806#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003807 {
3808 FLAC__ASSERT(best_residual_bits != 0);
3809 break;
3810 }
3811 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3812 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003813 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003814 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003815 }
3816 }
3817 }
3818
Josh Coalsona37ba462002-08-19 21:36:39 +00003819 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003820 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003821 * it is const to the outside world.
3822 */
3823 {
3824 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3825 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3826 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3827 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3828 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003829
3830 return best_residual_bits;
3831}
3832
Josh Coalson6fe72f72002-08-20 04:01:59 +00003833void precompute_partition_info_sums_(
3834 const FLAC__uint32 abs_residual[],
3835 FLAC__uint64 abs_residual_partition_sums[],
3836 unsigned residual_samples,
3837 unsigned predictor_order,
3838 unsigned min_partition_order,
3839 unsigned max_partition_order
3840)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003841{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003842 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003843 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003844 const unsigned blocksize = residual_samples + predictor_order;
3845
Josh Coalsonaef013c2001-04-24 01:25:42 +00003846 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003847 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003848 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00003849 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003850 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003851 const unsigned partitions = 1u << partition_order;
3852 const unsigned default_partition_samples = blocksize >> partition_order;
3853
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003854 FLAC__ASSERT(default_partition_samples > predictor_order);
3855
3856 for(partition = residual_sample = 0; partition < partitions; partition++) {
3857 partition_samples = default_partition_samples;
3858 if(partition == 0)
3859 partition_samples -= predictor_order;
3860 abs_residual_partition_sum = 0;
3861 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3862 abs_r = abs_residual[residual_sample];
3863 abs_residual_partition_sum += abs_r;
3864 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003865 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003866 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003867 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003868 to_partition = partitions;
3869 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003870 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00003871
Josh Coalson8395d022001-07-12 21:25:22 +00003872 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00003873 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003874 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003875 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003876 const unsigned partitions = 1u << partition_order;
3877 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00003878 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003879 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003880 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003881 from_partition++;
3882 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003883 }
3884 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003885}
Josh Coalson8395d022001-07-12 21:25:22 +00003886
Josh Coalson6fe72f72002-08-20 04:01:59 +00003887void precompute_partition_info_escapes_(
3888 const FLAC__int32 residual[],
3889 unsigned raw_bits_per_partition[],
3890 unsigned residual_samples,
3891 unsigned predictor_order,
3892 unsigned min_partition_order,
3893 unsigned max_partition_order
3894)
Josh Coalson8395d022001-07-12 21:25:22 +00003895{
3896 int partition_order;
3897 unsigned from_partition, to_partition = 0;
3898 const unsigned blocksize = residual_samples + predictor_order;
3899
3900 /* first do max_partition_order */
3901 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3902 FLAC__int32 r, residual_partition_min, residual_partition_max;
3903 unsigned silog2_min, silog2_max;
3904 unsigned partition, partition_sample, partition_samples, residual_sample;
3905 const unsigned partitions = 1u << partition_order;
3906 const unsigned default_partition_samples = blocksize >> partition_order;
3907
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003908 FLAC__ASSERT(default_partition_samples > predictor_order);
3909
3910 for(partition = residual_sample = 0; partition < partitions; partition++) {
3911 partition_samples = default_partition_samples;
3912 if(partition == 0)
3913 partition_samples -= predictor_order;
3914 residual_partition_min = residual_partition_max = 0;
3915 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3916 r = residual[residual_sample];
3917 if(r < residual_partition_min)
3918 residual_partition_min = r;
3919 else if(r > residual_partition_max)
3920 residual_partition_max = r;
3921 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00003922 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003923 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
3924 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
3925 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00003926 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003927 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00003928 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00003929 }
3930
3931 /* now merge partitions for lower orders */
3932 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3933 unsigned m;
3934 unsigned i;
3935 const unsigned partitions = 1u << partition_order;
3936 for(i = 0; i < partitions; i++) {
3937 m = raw_bits_per_partition[from_partition];
3938 from_partition++;
3939 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
3940 from_partition++;
3941 to_partition++;
3942 }
3943 }
3944}
Josh Coalson94e02cd2001-01-25 10:41:06 +00003945
Josh Coalson352e0f62001-03-20 22:55:50 +00003946#ifdef VARIABLE_RICE_BITS
3947#undef VARIABLE_RICE_BITS
3948#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003949#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003950#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00003951#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003952
Josh Coalson8395d022001-07-12 21:25:22 +00003953#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003954FLAC__bool set_partitioned_rice_(
3955 const FLAC__uint32 abs_residual[],
3956 const FLAC__int32 residual[],
3957 const unsigned residual_samples,
3958 const unsigned predictor_order,
3959 const unsigned suggested_rice_parameter,
3960 const unsigned rice_parameter_search_dist,
3961 const unsigned partition_order,
3962 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3963 unsigned *bits
3964)
Josh Coalson8395d022001-07-12 21:25:22 +00003965#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003966FLAC__bool set_partitioned_rice_(
3967 const FLAC__uint32 abs_residual[],
3968 const unsigned residual_samples,
3969 const unsigned predictor_order,
3970 const unsigned suggested_rice_parameter,
3971 const unsigned rice_parameter_search_dist,
3972 const unsigned partition_order,
3973 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3974 unsigned *bits
3975)
Josh Coalson8395d022001-07-12 21:25:22 +00003976#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003977{
Josh Coalson034dfab2001-04-27 19:10:23 +00003978 unsigned rice_parameter, partition_bits;
3979#ifndef NO_RICE_SEARCH
3980 unsigned best_partition_bits;
3981 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3982#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003983 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003984 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003985
Josh Coalson1b689822001-05-31 20:11:02 +00003986 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00003987
Josh Coalsona37ba462002-08-19 21:36:39 +00003988 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3989 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003990
Josh Coalson94e02cd2001-01-25 10:41:06 +00003991 if(partition_order == 0) {
3992 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00003993
Josh Coalson034dfab2001-04-27 19:10:23 +00003994#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003995 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003996 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003997 min_rice_parameter = 0;
3998 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003999 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4000 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004001 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004002#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004003 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4004#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004005 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004006 }
4007 }
4008 else
4009 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4010
4011 best_partition_bits = 0xffffffff;
4012 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4013#endif
4014#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004015 const unsigned rice_parameter_estimate = rice_parameter-1;
4016 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004017#else
4018 partition_bits = 0;
4019#endif
4020 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4021 for(i = 0; i < residual_samples; i++) {
4022#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004023 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004024#else
4025 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4026#endif
4027 }
4028#ifndef NO_RICE_SEARCH
4029 if(partition_bits < best_partition_bits) {
4030 best_rice_parameter = rice_parameter;
4031 best_partition_bits = partition_bits;
4032 }
4033 }
4034#endif
4035 parameters[0] = best_rice_parameter;
4036 bits_ += best_partition_bits;
4037 }
4038 else {
4039 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004040 unsigned partition_samples;
4041 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00004042 const unsigned partitions = 1u << partition_order;
4043 for(partition = residual_sample = 0; partition < partitions; partition++) {
4044 partition_samples = (residual_samples+predictor_order) >> partition_order;
4045 if(partition == 0) {
4046 if(partition_samples <= predictor_order)
4047 return false;
4048 else
4049 partition_samples -= predictor_order;
4050 }
4051 mean = 0;
4052 save_residual_sample = residual_sample;
4053 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004054 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00004055 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004056 /* we are basically calculating the size in bits of the
4057 * average residual magnitude in the partition:
4058 * rice_parameter = floor(log2(mean/partition_samples))
4059 * 'mean' is not a good name for the variable, it is
4060 * actually the sum of magnitudes of all residual values
4061 * in the partition, so the actual mean is
4062 * mean/partition_samples
4063 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004064 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00004065 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004066 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004067#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004068 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4069#endif
4070 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4071 }
4072
4073#ifndef NO_RICE_SEARCH
4074 if(rice_parameter_search_dist) {
4075 if(rice_parameter < rice_parameter_search_dist)
4076 min_rice_parameter = 0;
4077 else
4078 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4079 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4080 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004081#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004082 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4083#endif
4084 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4085 }
4086 }
4087 else
4088 min_rice_parameter = max_rice_parameter = rice_parameter;
4089
4090 best_partition_bits = 0xffffffff;
4091 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4092#endif
4093#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004094 const unsigned rice_parameter_estimate = rice_parameter-1;
4095 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004096#else
4097 partition_bits = 0;
4098#endif
4099 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4100 save_residual_sample = residual_sample;
4101 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4102#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004103 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004104#else
4105 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4106#endif
4107 }
4108#ifndef NO_RICE_SEARCH
4109 if(rice_parameter != max_rice_parameter)
4110 residual_sample = save_residual_sample;
4111 if(partition_bits < best_partition_bits) {
4112 best_rice_parameter = rice_parameter;
4113 best_partition_bits = partition_bits;
4114 }
4115 }
4116#endif
4117 parameters[partition] = best_rice_parameter;
4118 bits_ += best_partition_bits;
4119 }
4120 }
4121
4122 *bits = bits_;
4123 return true;
4124}
4125
4126#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004127FLAC__bool set_partitioned_rice_with_precompute_(
4128 const FLAC__int32 residual[],
4129 const FLAC__uint64 abs_residual_partition_sums[],
4130 const unsigned raw_bits_per_partition[],
4131 const unsigned residual_samples,
4132 const unsigned predictor_order,
4133 const unsigned suggested_rice_parameter,
4134 const unsigned rice_parameter_search_dist,
4135 const unsigned partition_order,
4136 const FLAC__bool search_for_escapes,
4137 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4138 unsigned *bits
4139)
Josh Coalson8395d022001-07-12 21:25:22 +00004140#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004141FLAC__bool set_partitioned_rice_with_precompute_(
4142 const FLAC__uint32 abs_residual[],
4143 const FLAC__uint64 abs_residual_partition_sums[],
4144 const unsigned raw_bits_per_partition[],
4145 const unsigned residual_samples,
4146 const unsigned predictor_order,
4147 const unsigned suggested_rice_parameter,
4148 const unsigned rice_parameter_search_dist,
4149 const unsigned partition_order,
4150 const FLAC__bool search_for_escapes,
4151 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4152 unsigned *bits
4153)
Josh Coalson8395d022001-07-12 21:25:22 +00004154#endif
4155{
4156 unsigned rice_parameter, partition_bits;
4157#ifndef NO_RICE_SEARCH
4158 unsigned best_partition_bits;
4159 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4160#endif
4161 unsigned flat_bits;
4162 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004163 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004164
4165 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4166
Josh Coalsona37ba462002-08-19 21:36:39 +00004167 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4168 parameters = partitioned_rice_contents->parameters;
4169 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004170
Josh Coalson8395d022001-07-12 21:25:22 +00004171 if(partition_order == 0) {
4172 unsigned i;
4173
4174#ifndef NO_RICE_SEARCH
4175 if(rice_parameter_search_dist) {
4176 if(suggested_rice_parameter < rice_parameter_search_dist)
4177 min_rice_parameter = 0;
4178 else
4179 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4180 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4181 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004182#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004183 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4184#endif
4185 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4186 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004187 }
4188 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004189 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00004190
Josh Coalson034dfab2001-04-27 19:10:23 +00004191 best_partition_bits = 0xffffffff;
4192 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4193#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004194#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004195 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00004196 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004197#else
4198 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004199#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004200 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00004201 for(i = 0; i < residual_samples; i++) {
4202#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00004203 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004204#else
Josh Coalson2051dd42001-04-12 22:22:34 +00004205 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 +00004206#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004207 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004208#ifndef NO_RICE_SEARCH
4209 if(partition_bits < best_partition_bits) {
4210 best_rice_parameter = rice_parameter;
4211 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00004212 }
4213 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004214#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004215 if(search_for_escapes) {
4216 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;
4217 if(flat_bits <= best_partition_bits) {
4218 raw_bits[0] = raw_bits_per_partition[0];
4219 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4220 best_partition_bits = flat_bits;
4221 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004222 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004223 parameters[0] = best_rice_parameter;
4224 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004225 }
4226 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00004227 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004228 unsigned partition_samples;
4229 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004230 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00004231 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004232 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00004233 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004234 if(partition_samples <= predictor_order)
4235 return false;
4236 else
4237 partition_samples -= predictor_order;
4238 }
Josh Coalson05d20792001-06-29 23:12:26 +00004239 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004240 /* we are basically calculating the size in bits of the
4241 * average residual magnitude in the partition:
4242 * rice_parameter = floor(log2(mean/partition_samples))
4243 * 'mean' is not a good name for the variable, it is
4244 * actually the sum of magnitudes of all residual values
4245 * in the partition, so the actual mean is
4246 * mean/partition_samples
4247 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004248 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00004249 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004250 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004251#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004252 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4253#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004254 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004255 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004256
Josh Coalson034dfab2001-04-27 19:10:23 +00004257#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004258 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004259 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004260 min_rice_parameter = 0;
4261 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004262 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4263 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004264 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004265#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004266 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4267#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004268 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004269 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004270 }
4271 else
4272 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00004273
Josh Coalson034dfab2001-04-27 19:10:23 +00004274 best_partition_bits = 0xffffffff;
4275 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4276#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004277#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00004278 const unsigned rice_parameter_estimate = rice_parameter-1;
4279 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004280#else
4281 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004282#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004283 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00004284 save_residual_sample = residual_sample;
4285 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00004286#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00004287 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004288#else
Josh Coalson4dacd192001-06-06 21:11:44 +00004289 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 +00004290#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004291 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004292#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00004293 if(rice_parameter != max_rice_parameter)
4294 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00004295 if(partition_bits < best_partition_bits) {
4296 best_rice_parameter = rice_parameter;
4297 best_partition_bits = partition_bits;
4298 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004299 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004300#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004301 if(search_for_escapes) {
4302 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;
4303 if(flat_bits <= best_partition_bits) {
4304 raw_bits[partition] = raw_bits_per_partition[partition];
4305 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4306 best_partition_bits = flat_bits;
4307 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004308 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004309 parameters[partition] = best_rice_parameter;
4310 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004311 }
4312 }
4313
4314 *bits = bits_;
4315 return true;
4316}
Josh Coalson859bc542001-03-27 22:22:27 +00004317
Josh Coalsonf1eff452002-07-31 07:05:33 +00004318unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00004319{
4320 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00004321 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00004322
4323 for(i = 0; i < samples && !(x&1); i++)
4324 x |= signal[i];
4325
4326 if(x == 0) {
4327 shift = 0;
4328 }
4329 else {
4330 for(shift = 0; !(x&1); shift++)
4331 x >>= 1;
4332 }
4333
4334 if(shift > 0) {
4335 for(i = 0; i < samples; i++)
4336 signal[i] >>= shift;
4337 }
4338
4339 return shift;
4340}
Josh Coalsond86e03b2002-08-03 21:56:15 +00004341
4342void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4343{
4344 unsigned channel;
4345
4346 for(channel = 0; channel < channels; channel++)
4347 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4348
4349 fifo->tail += wide_samples;
4350
4351 FLAC__ASSERT(fifo->tail <= fifo->size);
4352}
4353
4354void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4355{
4356 unsigned channel;
4357 unsigned sample, wide_sample;
4358 unsigned tail = fifo->tail;
4359
4360 sample = input_offset * channels;
4361 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4362 for(channel = 0; channel < channels; channel++)
4363 fifo->data[channel][tail] = input[sample++];
4364 tail++;
4365 }
4366 fifo->tail = tail;
4367
4368 FLAC__ASSERT(fifo->tail <= fifo->size);
4369}
4370
Josh Coalson8065a2d2006-10-15 08:32:56 +00004371FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalsond86e03b2002-08-03 21:56:15 +00004372{
4373 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
Josh Coalson8065a2d2006-10-15 08:32:56 +00004374 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004375 (void)decoder;
4376
4377 if(encoder->private_->verify.needs_magic_hack) {
4378 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4379 *bytes = FLAC__STREAM_SYNC_LENGTH;
4380 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4381 encoder->private_->verify.needs_magic_hack = false;
4382 }
4383 else {
4384 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00004385 /*
4386 * If we get here, a FIFO underflow has occurred,
4387 * which means there is a bug somewhere.
4388 */
4389 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004390 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4391 }
4392 else if(encoded_bytes < *bytes)
4393 *bytes = encoded_bytes;
4394 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4395 encoder->private_->verify.output.data += *bytes;
4396 encoder->private_->verify.output.bytes -= *bytes;
4397 }
4398
4399 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4400}
4401
4402FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4403{
4404 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4405 unsigned channel;
4406 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
4407 const unsigned blocksize = frame->header.blocksize;
4408 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4409
4410 for(channel = 0; channel < channels; channel++) {
4411 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4412 unsigned i, sample = 0;
4413 FLAC__int32 expect = 0, got = 0;
4414
4415 for(i = 0; i < blocksize; i++) {
4416 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4417 sample = i;
4418 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4419 got = (FLAC__int32)buffer[channel][i];
4420 break;
4421 }
4422 }
4423 FLAC__ASSERT(i < blocksize);
4424 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4425 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00004426 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004427 encoder->private_->verify.error_stats.channel = channel;
4428 encoder->private_->verify.error_stats.sample = sample;
4429 encoder->private_->verify.error_stats.expected = expect;
4430 encoder->private_->verify.error_stats.got = got;
4431 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4432 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4433 }
4434 }
4435 /* dequeue the frame from the fifo */
4436 for(channel = 0; channel < channels; channel++) {
4437 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
4438 }
4439 encoder->private_->verify.input_fifo.tail -= blocksize;
4440 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4441}
4442
4443void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4444{
4445 (void)decoder, (void)metadata, (void)client_data;
4446}
4447
4448void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4449{
4450 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4451 (void)decoder, (void)status;
4452 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4453}
Josh Coalson6b21f662006-09-13 01:42:27 +00004454
Josh Coalson8065a2d2006-10-15 08:32:56 +00004455FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalson8da98c82006-10-15 04:24:05 +00004456{
4457 (void)client_data;
4458
Josh Coalson8065a2d2006-10-15 08:32:56 +00004459 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
Josh Coalson8da98c82006-10-15 04:24:05 +00004460 if (*bytes == 0) {
4461 if (feof(encoder->private_->file))
4462 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4463 else if (ferror(encoder->private_->file))
4464 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4465 }
4466 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4467}
4468
Josh Coalson6b21f662006-09-13 01:42:27 +00004469FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4470{
4471 (void)client_data;
4472
4473 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4474 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4475 else
4476 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4477}
4478
4479FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4480{
4481 off_t offset;
4482
4483 (void)client_data;
4484
4485 offset = ftello(encoder->private_->file);
4486
4487 if(offset < 0) {
4488 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4489 }
4490 else {
4491 *absolute_byte_offset = (FLAC__uint64)offset;
4492 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4493 }
4494}
4495
4496#ifdef FLAC__VALGRIND_TESTING
4497static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4498{
4499 size_t ret = fwrite(ptr, size, nmemb, stream);
4500 if(!ferror(stream))
4501 fflush(stream);
4502 return ret;
4503}
4504#else
4505#define local__fwrite fwrite
4506#endif
4507
Josh Coalson352feb52006-10-15 17:08:52 +00004508FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
Josh Coalson6b21f662006-09-13 01:42:27 +00004509{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004510 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00004511
4512 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson8da98c82006-10-15 04:24:05 +00004513 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4514#if FLAC__HAS_OGG
4515 /* We would like to be able to use 'samples > 0' in the
4516 * clause here but currently because of the nature of our
4517 * Ogg writing implementation, 'samples' is always 0 (see
4518 * ogg_encoder_aspect.c). The downside is extra progress
4519 * callbacks.
4520 */
4521 encoder->private_->is_ogg? true :
4522#endif
4523 samples > 0
4524 );
4525 if(call_it) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004526 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4527 * because at this point in the callback chain, the stats
4528 * have not been updated. Only after we return and control
4529 * gets back to write_frame_() are the stats updated
4530 */
4531 encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data);
4532 }
Josh Coalson6b21f662006-09-13 01:42:27 +00004533 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4534 }
4535 else
4536 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4537}
4538
4539/*
4540 * This will forcibly set stdout to binary mode (for OSes that require it)
4541 */
4542FILE *get_binary_stdout_()
4543{
4544 /* if something breaks here it is probably due to the presence or
4545 * absence of an underscore before the identifiers 'setmode',
4546 * 'fileno', and/or 'O_BINARY'; check your system header files.
4547 */
4548#if defined _MSC_VER || defined __MINGW32__
4549 _setmode(_fileno(stdout), _O_BINARY);
4550#elif defined __CYGWIN__
4551 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4552 setmode(_fileno(stdout), _O_BINARY);
4553#elif defined __EMX__
4554 setmode(fileno(stdout), O_BINARY);
4555#endif
4556
4557 return stdout;
4558}