blob: 0c2e536ada438db9c21901a535b84a28266f7ba0 [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 Coalson49f2f162006-11-09 16:54:52 +0000135static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
136static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block);
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 Coalson49f2f162006-11-09 16:54:52 +0000141static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
Josh Coalson85aaed82006-11-09 01:19:13 +0000142static 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 Coalson49f2f162006-11-09 16:54:52 +0000527/* Number of samples that will be overread to watch for end of stream. By
528 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
529 * always try to read blocksize+1 samples before encoding a block, so that
530 * even if the stream has a total sample count that is an integral multiple
531 * of the blocksize, we will still notice when we are encoding the last
532 * block. This is needed, for example, to correctly set the end-of-stream
533 * marker in Ogg FLAC.
534 *
535 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
536 * not really any reason to change it.
537 */
538static const unsigned OVERREAD_ = 1;
539
Josh Coalson0a15c142001-06-13 17:59:57 +0000540/***********************************************************************
541 *
542 * Class constructor/destructor
543 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000544 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000545FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000546{
Josh Coalson0a15c142001-06-13 17:59:57 +0000547 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000548 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000549
Josh Coalson0a15c142001-06-13 17:59:57 +0000550 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000551
Josh Coalsonea7155f2002-10-18 05:49:19 +0000552 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000553 if(encoder == 0) {
554 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000555 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000556
Josh Coalsonea7155f2002-10-18 05:49:19 +0000557 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000558 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000559 free(encoder);
560 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000561 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000562
Josh Coalsonea7155f2002-10-18 05:49:19 +0000563 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000564 if(encoder->private_ == 0) {
565 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000566 free(encoder);
567 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000568 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000569
Josh Coalsonaec256b2002-03-12 16:19:54 +0000570 encoder->private_->frame = FLAC__bitbuffer_new();
571 if(encoder->private_->frame == 0) {
572 free(encoder->private_);
573 free(encoder->protected_);
574 free(encoder);
575 return 0;
576 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000577
Josh Coalson6b21f662006-09-13 01:42:27 +0000578 encoder->private_->file = 0;
579
Josh Coalsonf1eff452002-07-31 07:05:33 +0000580 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000581
Josh Coalson3262b0d2002-08-14 20:58:42 +0000582 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000583
584 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
585 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
586 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
587 }
588 for(i = 0; i < 2; i++) {
589 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
590 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
591 }
592 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000593 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
594 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[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 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
598 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 +0000599 }
600
601 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000602 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
603 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000604 }
605 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000606 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
607 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 +0000608 }
609 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000610 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000611
Josh Coalsonfa697a92001-08-16 20:07:29 +0000612 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000613
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000614 return encoder;
615}
616
Josh Coalson6afed9f2002-10-16 22:29:47 +0000617FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000618{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000619 unsigned i;
620
Josh Coalsonf1eff452002-07-31 07:05:33 +0000621 FLAC__ASSERT(0 != encoder);
622 FLAC__ASSERT(0 != encoder->protected_);
623 FLAC__ASSERT(0 != encoder->private_);
624 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000625
Josh Coalson3262b0d2002-08-14 20:58:42 +0000626 encoder->private_->is_being_deleted = true;
627
Josh Coalsona5862262006-11-09 06:58:26 +0000628 (void)FLAC__stream_encoder_finish(encoder);
Josh Coalson3262b0d2002-08-14 20:58:42 +0000629
Josh Coalson4fa90592002-12-04 07:01:37 +0000630 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000631 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000632
633 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000634 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
635 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000636 }
637 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000638 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
639 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 +0000640 }
641 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000642 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000643
Josh Coalsonaec256b2002-03-12 16:19:54 +0000644 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000645 free(encoder->private_);
646 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000647 free(encoder);
648}
649
Josh Coalson0a15c142001-06-13 17:59:57 +0000650/***********************************************************************
651 *
652 * Public class methods
653 *
654 ***********************************************************************/
655
Josh Coalson8da98c82006-10-15 04:24:05 +0000656static FLAC__StreamEncoderInitStatus init_stream_internal_(
657 FLAC__StreamEncoder *encoder,
658 FLAC__StreamEncoderReadCallback read_callback,
659 FLAC__StreamEncoderWriteCallback write_callback,
660 FLAC__StreamEncoderSeekCallback seek_callback,
661 FLAC__StreamEncoderTellCallback tell_callback,
662 FLAC__StreamEncoderMetadataCallback metadata_callback,
663 void *client_data,
664 FLAC__bool is_ogg
665)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000666{
667 unsigned i;
Josh Coalson3957c472006-09-24 16:25:42 +0000668 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000669
Josh Coalsonf1eff452002-07-31 07:05:33 +0000670 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000671
Josh Coalsonfa697a92001-08-16 20:07:29 +0000672 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000673 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000674
Josh Coalson8da98c82006-10-15 04:24:05 +0000675#ifndef FLAC__HAS_OGG
676 if(is_ogg)
677 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
678#endif
679
Josh Coalson6b21f662006-09-13 01:42:27 +0000680 if(0 == write_callback || (seek_callback && 0 == tell_callback))
681 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000682
Josh Coalsonfa697a92001-08-16 20:07:29 +0000683 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000684 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000685
Josh Coalson425609c2006-11-03 16:08:52 +0000686 if(encoder->protected_->channels != 2) {
687 encoder->protected_->do_mid_side_stereo = false;
688 encoder->protected_->loose_mid_side_stereo = false;
689 }
690 else if(!encoder->protected_->do_mid_side_stereo)
691 encoder->protected_->loose_mid_side_stereo = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000692
Josh Coalsonfa697a92001-08-16 20:07:29 +0000693 if(encoder->protected_->bits_per_sample >= 32)
Josh Coalson6b21f662006-09-13 01:42:27 +0000694 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 +0000695
Josh Coalson76c68bc2002-05-17 06:22:02 +0000696 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 +0000697 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000698
Josh Coalson0833f342002-07-15 05:31:55 +0000699 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000700 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000701
Josh Coalson425609c2006-11-03 16:08:52 +0000702 if(encoder->protected_->blocksize == 0) {
703 if(encoder->protected_->max_lpc_order == 0)
704 encoder->protected_->blocksize = 1152;
705 else
706 encoder->protected_->blocksize = 4608;
707 }
708
Josh Coalsonfa697a92001-08-16 20:07:29 +0000709 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000710 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000711
Josh Coalson20ac2c12002-08-30 05:47:14 +0000712 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000713 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000714
Josh Coalsonfa697a92001-08-16 20:07:29 +0000715 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000716 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000717
Josh Coalsonfa697a92001-08-16 20:07:29 +0000718 if(encoder->protected_->qlp_coeff_precision == 0) {
719 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000720 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
721 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000722 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 +0000723 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000724 else if(encoder->protected_->bits_per_sample == 16) {
725 if(encoder->protected_->blocksize <= 192)
726 encoder->protected_->qlp_coeff_precision = 7;
727 else if(encoder->protected_->blocksize <= 384)
728 encoder->protected_->qlp_coeff_precision = 8;
729 else if(encoder->protected_->blocksize <= 576)
730 encoder->protected_->qlp_coeff_precision = 9;
731 else if(encoder->protected_->blocksize <= 1152)
732 encoder->protected_->qlp_coeff_precision = 10;
733 else if(encoder->protected_->blocksize <= 2304)
734 encoder->protected_->qlp_coeff_precision = 11;
735 else if(encoder->protected_->blocksize <= 4608)
736 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000737 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000738 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000739 }
740 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000741 if(encoder->protected_->blocksize <= 384)
742 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
743 else if(encoder->protected_->blocksize <= 1152)
744 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
745 else
746 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000747 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000748 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000749 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000750 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 +0000751 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000752
Josh Coalsonfa697a92001-08-16 20:07:29 +0000753 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000754 if(
755 encoder->protected_->blocksize != 192 &&
756 encoder->protected_->blocksize != 576 &&
757 encoder->protected_->blocksize != 1152 &&
758 encoder->protected_->blocksize != 2304 &&
759 encoder->protected_->blocksize != 4608 &&
760 encoder->protected_->blocksize != 256 &&
761 encoder->protected_->blocksize != 512 &&
762 encoder->protected_->blocksize != 1024 &&
763 encoder->protected_->blocksize != 2048 &&
764 encoder->protected_->blocksize != 4096 &&
765 encoder->protected_->blocksize != 8192 &&
766 encoder->protected_->blocksize != 16384
767 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000768 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000769 if(
770 encoder->protected_->sample_rate != 8000 &&
771 encoder->protected_->sample_rate != 16000 &&
772 encoder->protected_->sample_rate != 22050 &&
773 encoder->protected_->sample_rate != 24000 &&
774 encoder->protected_->sample_rate != 32000 &&
775 encoder->protected_->sample_rate != 44100 &&
776 encoder->protected_->sample_rate != 48000 &&
777 encoder->protected_->sample_rate != 96000
778 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000779 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000780 if(
781 encoder->protected_->bits_per_sample != 8 &&
782 encoder->protected_->bits_per_sample != 12 &&
783 encoder->protected_->bits_per_sample != 16 &&
784 encoder->protected_->bits_per_sample != 20 &&
785 encoder->protected_->bits_per_sample != 24
786 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000787 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000788 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000789 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsond0edb972006-10-07 06:50:08 +0000790 if(
791 encoder->protected_->sample_rate <= 48000 &&
792 (
793 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
794 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
795 )
796 ) {
797 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
798 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000799 }
800
Josh Coalsonfa697a92001-08-16 20:07:29 +0000801 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
802 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
803 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
804 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000805
Josh Coalson8da98c82006-10-15 04:24:05 +0000806#if FLAC__HAS_OGG
807 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
808 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
809 unsigned i;
810 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
811 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
812 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
813 for( ; i > 0; i--)
814 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
815 encoder->protected_->metadata[0] = vc;
816 break;
817 }
818 }
819 }
820#endif
821 /* keep track of any SEEKTABLE block */
822 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
823 unsigned i;
824 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
825 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
826 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
827 break; /* take only the first one */
828 }
829 }
830 }
831
Josh Coalson66075c12002-06-01 05:39:38 +0000832 /* validate metadata */
833 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000834 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000835 metadata_has_seektable = false;
836 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000837 metadata_picture_has_type1 = false;
838 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000839 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000840 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
841 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000842 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000843 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000844 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000845 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000846 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000847 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000848 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000849 }
Josh Coalson3957c472006-09-24 16:25:42 +0000850 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000851 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000852 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000853 metadata_has_vorbis_comment = true;
854 }
Josh Coalson3957c472006-09-24 16:25:42 +0000855 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
856 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 +0000857 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000858 }
Josh Coalson3957c472006-09-24 16:25:42 +0000859 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
860 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000861 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000862 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
863 if(metadata_picture_has_type1) /* there should only be 1 per stream */
864 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
865 metadata_picture_has_type1 = true;
866 /* standard icon must be 32x32 pixel PNG */
867 if(
868 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
869 (
870 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
871 m->data.picture.width != 32 ||
872 m->data.picture.height != 32
873 )
874 )
875 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
876 }
877 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
878 if(metadata_picture_has_type2) /* there should only be 1 per stream */
879 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
880 metadata_picture_has_type2 = true;
881 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000882 }
Josh Coalson66075c12002-06-01 05:39:38 +0000883 }
884
Josh Coalsonfa697a92001-08-16 20:07:29 +0000885 encoder->private_->input_capacity = 0;
886 for(i = 0; i < encoder->protected_->channels; i++) {
887 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000888#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000889 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000890#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000891 }
892 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000893 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000894#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000895 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000896#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000897 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000898#ifndef FLAC__INTEGER_ONLY_LIBRARY
899 for(i = 0; i < encoder->protected_->num_apodizations; i++)
900 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
901 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
902#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000903 for(i = 0; i < encoder->protected_->channels; i++) {
904 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
905 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
906 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000907 }
908 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000909 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
910 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
911 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000912 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000913 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
914 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
915 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000916#ifndef FLAC__INTEGER_ONLY_LIBRARY
917 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
918#else
919 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
920 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
921 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
922 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
923 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
924 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
925 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);
926#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000927 if(encoder->private_->loose_mid_side_stereo_frames == 0)
928 encoder->private_->loose_mid_side_stereo_frames = 1;
929 encoder->private_->loose_mid_side_stereo_frame_count = 0;
930 encoder->private_->current_sample_number = 0;
931 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000932
Josh Coalsonfa697a92001-08-16 20:07:29 +0000933 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
934 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? */
935 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000936
Josh Coalsoncf30f502001-05-23 20:57:44 +0000937 /*
938 * get the CPU info and set the function pointers
939 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000940 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000941 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000942#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000943 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000944#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000945 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000946#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000947 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000948 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 +0000949 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000950#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000951 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000952#ifndef FLAC__INTEGER_ONLY_LIBRARY
953# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000954 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000955# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000956 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000957# ifdef FLAC__HAS_NASM
958# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000959 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000960 if(encoder->protected_->max_lpc_order < 4)
961 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
962 else if(encoder->protected_->max_lpc_order < 8)
963 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
964 else if(encoder->protected_->max_lpc_order < 12)
965 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000966 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000967 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000968 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000969 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000970# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000971 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000972 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000973 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000974 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000975 if(encoder->private_->cpuinfo.data.ia32.mmx) {
976 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
977 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 +0000978 }
979 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000980 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
981 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 +0000982 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000983 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
984 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
985# endif /* FLAC__HAS_NASM */
986# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000987 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000988# endif /* !FLAC__NO_ASM */
989#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000990 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000991 if(encoder->private_->use_wide_by_block) {
992 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000993 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000994
Josh Coalson8395d022001-07-12 21:25:22 +0000995 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000996 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 +0000997
Josh Coalson6b21f662006-09-13 01:42:27 +0000998 /* set state to OK; from here on, errors are fatal and we'll override the state then */
999 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
1000
Josh Coalson8da98c82006-10-15 04:24:05 +00001001#if FLAC__HAS_OGG
1002 encoder->private_->is_ogg = is_ogg;
1003 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
1004 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
1005 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1006 }
1007#endif
1008
1009 encoder->private_->read_callback = read_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +00001010 encoder->private_->write_callback = write_callback;
1011 encoder->private_->seek_callback = seek_callback;
1012 encoder->private_->tell_callback = tell_callback;
1013 encoder->private_->metadata_callback = metadata_callback;
1014 encoder->private_->client_data = client_data;
1015
Josh Coalsonf1eff452002-07-31 07:05:33 +00001016 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001017 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001018 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001019 }
Josh Coalsonaec256b2002-03-12 16:19:54 +00001020
Josh Coalson6b21f662006-09-13 01:42:27 +00001021 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
1022 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1023 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1024 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001025
1026 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +00001027 * Set up the verify stuff if necessary
1028 */
1029 if(encoder->protected_->verify) {
1030 /*
1031 * First, set up the fifo which will hold the
1032 * original signal to compare against
1033 */
Josh Coalson49f2f162006-11-09 16:54:52 +00001034 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001035 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001036 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
1037 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1038 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1039 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001040 }
1041 encoder->private_->verify.input_fifo.tail = 0;
1042
1043 /*
1044 * Now set up a stream decoder for verification
1045 */
1046 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
Josh Coalson6b21f662006-09-13 01:42:27 +00001047 if(0 == encoder->private_->verify.decoder) {
1048 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1049 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1050 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001051
Josh Coalson6b21f662006-09-13 01:42:27 +00001052 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) {
1053 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1054 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1055 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001056 }
Josh Coalson589f8c72002-08-07 23:54:55 +00001057 encoder->private_->verify.error_stats.absolute_sample = 0;
1058 encoder->private_->verify.error_stats.frame_number = 0;
1059 encoder->private_->verify.error_stats.channel = 0;
1060 encoder->private_->verify.error_stats.sample = 0;
1061 encoder->private_->verify.error_stats.expected = 0;
1062 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001063
1064 /*
Josh Coalson6b21f662006-09-13 01:42:27 +00001065 * These must be done before we write any metadata, because that
1066 * calls the write_callback, which uses these values.
1067 */
1068 encoder->private_->first_seekpoint_to_check = 0;
1069 encoder->private_->samples_written = 0;
1070 encoder->protected_->streaminfo_offset = 0;
1071 encoder->protected_->seektable_offset = 0;
1072 encoder->protected_->audio_offset = 0;
1073
1074 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001075 * write the stream header
1076 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001077 if(encoder->protected_->verify)
1078 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson6b21f662006-09-13 01:42:27 +00001079 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1080 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1081 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1082 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001083 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001084 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001085 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001086 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001087
Josh Coalson5c491a12002-08-01 06:39:40 +00001088 /*
1089 * write the STREAMINFO metadata block
1090 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001091 if(encoder->protected_->verify)
1092 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +00001093 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1094 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1095 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1096 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1097 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1098 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1099 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1100 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1101 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1102 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1103 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1104 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 +00001105 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +00001106 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1107 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1108 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1109 }
1110 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1111 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1112 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1113 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001114 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001115 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001116 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001117 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001118
Josh Coalson5c491a12002-08-01 06:39:40 +00001119 /*
1120 * Now that the STREAMINFO block is written, we can init this to an
1121 * absurdly-high value...
1122 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001123 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 +00001124 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001125 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001126
Josh Coalson5c491a12002-08-01 06:39:40 +00001127 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001128 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1129 * if not, we will write an empty one (FLAC__add_metadata_block()
1130 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001131 *
Josh Coalson8da98c82006-10-15 04:24:05 +00001132 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1133 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1134 * true it will have already insured that the metadata list is properly
1135 * ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001136 */
1137 if(!metadata_has_vorbis_comment) {
1138 FLAC__StreamMetadata vorbis_comment;
1139 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1140 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1141 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1142 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1143 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1144 vorbis_comment.data.vorbis_comment.num_comments = 0;
1145 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001146 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1147 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1148 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1149 }
1150 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1151 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1152 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1153 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001154 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001155 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001156 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001157 }
1158 }
1159
1160 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001161 * write the user's metadata blocks
1162 */
1163 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1164 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001165 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1166 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1167 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1168 }
1169 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1170 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1171 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1172 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001173 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001174 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001175 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001176 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001177 }
1178
Josh Coalson6b21f662006-09-13 01:42:27 +00001179 /* now that all the metadata is written, we save the stream offset */
1180 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 */
1181 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1182 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1183 }
1184
Josh Coalsond86e03b2002-08-03 21:56:15 +00001185 if(encoder->protected_->verify)
1186 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1187
Josh Coalson6b21f662006-09-13 01:42:27 +00001188 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1189}
1190
Josh Coalson8da98c82006-10-15 04:24:05 +00001191FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1192 FLAC__StreamEncoder *encoder,
1193 FLAC__StreamEncoderWriteCallback write_callback,
1194 FLAC__StreamEncoderSeekCallback seek_callback,
1195 FLAC__StreamEncoderTellCallback tell_callback,
1196 FLAC__StreamEncoderMetadataCallback metadata_callback,
1197 void *client_data
1198)
1199{
1200 return init_stream_internal_(
1201 encoder,
1202 /*read_callback=*/0,
1203 write_callback,
1204 seek_callback,
1205 tell_callback,
1206 metadata_callback,
1207 client_data,
1208 /*is_ogg=*/false
1209 );
1210}
1211
1212FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1213 FLAC__StreamEncoder *encoder,
1214 FLAC__StreamEncoderReadCallback read_callback,
1215 FLAC__StreamEncoderWriteCallback write_callback,
1216 FLAC__StreamEncoderSeekCallback seek_callback,
1217 FLAC__StreamEncoderTellCallback tell_callback,
1218 FLAC__StreamEncoderMetadataCallback metadata_callback,
1219 void *client_data
1220)
1221{
1222 return init_stream_internal_(
1223 encoder,
1224 read_callback,
1225 write_callback,
1226 seek_callback,
1227 tell_callback,
1228 metadata_callback,
1229 client_data,
1230 /*is_ogg=*/true
1231 );
1232}
1233
1234static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1235 FLAC__StreamEncoder *encoder,
1236 FILE *file,
1237 FLAC__StreamEncoderProgressCallback progress_callback,
1238 void *client_data,
1239 FLAC__bool is_ogg
1240)
Josh Coalson6b21f662006-09-13 01:42:27 +00001241{
1242 FLAC__StreamEncoderInitStatus init_status;
1243
1244 FLAC__ASSERT(0 != encoder);
1245 FLAC__ASSERT(0 != file);
1246
Josh Coalson6b21f662006-09-13 01:42:27 +00001247 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1248 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1249
1250 /* double protection */
1251 if(file == 0) {
1252 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1253 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1254 }
1255
Josh Coalson92f7fa92006-10-09 05:34:21 +00001256 /*
1257 * To make sure that our file does not go unclosed after an error, we
1258 * must assign the FILE pointer before any further error can occur in
1259 * this routine.
1260 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001261 if(file == stdout)
1262 file = get_binary_stdout_(); /* just to be safe */
1263
1264 encoder->private_->file = file;
1265
1266 encoder->private_->progress_callback = progress_callback;
1267 encoder->private_->bytes_written = 0;
1268 encoder->private_->samples_written = 0;
1269 encoder->private_->frames_written = 0;
1270
Josh Coalson8da98c82006-10-15 04:24:05 +00001271 init_status = init_stream_internal_(
1272 encoder,
1273 is_ogg? file_read_callback_ : 0,
1274 file_write_callback_,
1275 file_seek_callback_,
1276 file_tell_callback_,
1277 /*metadata_callback=*/0,
1278 client_data,
1279 is_ogg
1280 );
Josh Coalson6b21f662006-09-13 01:42:27 +00001281 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1282 /* the above function sets the state for us in case of an error */
1283 return init_status;
1284 }
1285
1286 {
1287 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1288
1289 FLAC__ASSERT(blocksize != 0);
1290 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1291 }
1292
1293 return init_status;
1294}
Josh Coalson8da98c82006-10-15 04:24:05 +00001295
1296FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1297 FLAC__StreamEncoder *encoder,
1298 FILE *file,
1299 FLAC__StreamEncoderProgressCallback progress_callback,
1300 void *client_data
1301)
1302{
1303 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1304}
1305
1306FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1307 FLAC__StreamEncoder *encoder,
1308 FILE *file,
1309 FLAC__StreamEncoderProgressCallback progress_callback,
1310 void *client_data
1311)
1312{
1313 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1314}
Josh Coalson6b21f662006-09-13 01:42:27 +00001315
Josh Coalson8da98c82006-10-15 04:24:05 +00001316static FLAC__StreamEncoderInitStatus init_file_internal_(
1317 FLAC__StreamEncoder *encoder,
1318 const char *filename,
1319 FLAC__StreamEncoderProgressCallback progress_callback,
1320 void *client_data,
1321 FLAC__bool is_ogg
1322)
Josh Coalson6b21f662006-09-13 01:42:27 +00001323{
1324 FILE *file;
1325
1326 FLAC__ASSERT(0 != encoder);
1327
1328 /*
1329 * To make sure that our file does not go unclosed after an error, we
1330 * have to do the same entrance checks here that are later performed
1331 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1332 */
1333 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1334 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1335
1336 file = filename? fopen(filename, "w+b") : stdout;
1337
1338 if(file == 0) {
1339 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1340 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1341 }
1342
Josh Coalson8da98c82006-10-15 04:24:05 +00001343 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1344}
1345
1346FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1347 FLAC__StreamEncoder *encoder,
1348 const char *filename,
1349 FLAC__StreamEncoderProgressCallback progress_callback,
1350 void *client_data
1351)
1352{
1353 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1354}
1355
1356FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1357 FLAC__StreamEncoder *encoder,
1358 const char *filename,
1359 FLAC__StreamEncoderProgressCallback progress_callback,
1360 void *client_data
1361)
1362{
1363 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001364}
1365
Josh Coalsona5862262006-11-09 06:58:26 +00001366FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001367{
Josh Coalsona5862262006-11-09 06:58:26 +00001368 FLAC__bool verify_mismatch = false;
1369
Josh Coalsonf1eff452002-07-31 07:05:33 +00001370 FLAC__ASSERT(0 != encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001371 FLAC__ASSERT(0 != encoder->private_);
1372 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001373
Josh Coalsonfa697a92001-08-16 20:07:29 +00001374 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsona5862262006-11-09 06:58:26 +00001375 return true;
Josh Coalson2b245f22002-08-07 17:10:50 +00001376
Josh Coalson3262b0d2002-08-14 20:58:42 +00001377 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001378 if(encoder->private_->current_sample_number != 0) {
Josh Coalson49f2f162006-11-09 16:54:52 +00001379 const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
Josh Coalson2b245f22002-08-07 17:10:50 +00001380 encoder->protected_->blocksize = encoder->private_->current_sample_number;
Josh Coalson49f2f162006-11-09 16:54:52 +00001381 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true) && encoder->protected_->state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
Josh Coalsona5862262006-11-09 06:58:26 +00001382 verify_mismatch = true;
Josh Coalson2b245f22002-08-07 17:10:50 +00001383 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001384 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001385
Josh Coalson6b21f662006-09-13 01:42:27 +00001386 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001387
Josh Coalson3262b0d2002-08-14 20:58:42 +00001388 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson8da98c82006-10-15 04:24:05 +00001389 if(encoder->private_->seek_callback) {
1390#if FLAC__HAS_OGG
1391 if(encoder->private_->is_ogg)
1392 update_ogg_metadata_(encoder);
1393 else
1394#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00001395 update_metadata_(encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001396 }
Josh Coalson6b21f662006-09-13 01:42:27 +00001397 if(encoder->private_->metadata_callback)
1398 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
Josh Coalson2b245f22002-08-07 17:10:50 +00001399 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001400
Josh Coalsona5862262006-11-09 06:58:26 +00001401 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder))
1402 verify_mismatch = true;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001403
Josh Coalson6b21f662006-09-13 01:42:27 +00001404 if(0 != encoder->private_->file) {
1405 if(encoder->private_->file != stdout)
1406 fclose(encoder->private_->file);
1407 encoder->private_->file = 0;
1408 }
1409
Josh Coalson8da98c82006-10-15 04:24:05 +00001410#if FLAC__HAS_OGG
1411 if(encoder->private_->is_ogg)
1412 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1413#endif
1414
Josh Coalsonf1eff452002-07-31 07:05:33 +00001415 free_(encoder);
1416 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001417
Josh Coalsona5862262006-11-09 06:58:26 +00001418 /* change the state to uninitialized unless there was a verify mismatch */
1419 if(verify_mismatch)
1420 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1421 else
1422 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1423
1424 return !verify_mismatch;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001425}
1426
Josh Coalson71d5c252006-10-15 06:04:55 +00001427FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
Josh Coalson8da98c82006-10-15 04:24:05 +00001428{
1429 FLAC__ASSERT(0 != encoder);
1430 FLAC__ASSERT(0 != encoder->private_);
1431 FLAC__ASSERT(0 != encoder->protected_);
1432 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1433 return false;
1434#ifdef FLAC__HAS_OGG
1435 /* can't check encoder->private_->is_ogg since that's not set until init time */
1436 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1437 return true;
1438#else
1439 (void)value;
1440 return false;
1441#endif
1442}
1443
Josh Coalson6afed9f2002-10-16 22:29:47 +00001444FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001445{
1446 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001447 FLAC__ASSERT(0 != encoder->private_);
1448 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001449 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1450 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001451#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001452 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001453#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001454 return true;
1455}
1456
Josh Coalson6afed9f2002-10-16 22:29:47 +00001457FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001458{
Josh Coalson92031602002-07-24 06:02:11 +00001459 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001460 FLAC__ASSERT(0 != encoder->private_);
1461 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001462 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001463 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001464 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001465 return true;
1466}
1467
Josh Coalson6afed9f2002-10-16 22:29:47 +00001468FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001469{
Josh Coalson92031602002-07-24 06:02:11 +00001470 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001471 FLAC__ASSERT(0 != encoder->private_);
1472 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001473 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001474 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001475 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001476 return true;
1477}
1478
Josh Coalson6afed9f2002-10-16 22:29:47 +00001479FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001480{
Josh Coalson92031602002-07-24 06:02:11 +00001481 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001482 FLAC__ASSERT(0 != encoder->private_);
1483 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001484 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001485 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001486 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001487 return true;
1488}
1489
Josh Coalson6afed9f2002-10-16 22:29:47 +00001490FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001491{
Josh Coalson92031602002-07-24 06:02:11 +00001492 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001493 FLAC__ASSERT(0 != encoder->private_);
1494 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001495 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001496 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001497 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001498 return true;
1499}
1500
Josh Coalson425609c2006-11-03 16:08:52 +00001501FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1502{
1503 FLAC__bool ok = true;
1504 FLAC__ASSERT(0 != encoder);
1505 FLAC__ASSERT(0 != encoder->private_);
1506 FLAC__ASSERT(0 != encoder->protected_);
1507 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1508 return false;
1509 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1510 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1511 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1512 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
1513 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1514 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1515 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1516 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1517 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1518 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1519 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1520 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1521 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1522 return ok;
1523}
1524
Josh Coalson6afed9f2002-10-16 22:29:47 +00001525FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001526{
Josh Coalson92031602002-07-24 06:02:11 +00001527 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001528 FLAC__ASSERT(0 != encoder->private_);
1529 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001530 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001531 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001532 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001533 return true;
1534}
1535
Josh Coalson425609c2006-11-03 16:08:52 +00001536FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1537{
1538 FLAC__ASSERT(0 != encoder);
1539 FLAC__ASSERT(0 != encoder->private_);
1540 FLAC__ASSERT(0 != encoder->protected_);
1541 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1542 return false;
1543 encoder->protected_->do_mid_side_stereo = value;
1544 return true;
1545}
1546
1547FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1548{
1549 FLAC__ASSERT(0 != encoder);
1550 FLAC__ASSERT(0 != encoder->private_);
1551 FLAC__ASSERT(0 != encoder->protected_);
1552 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1553 return false;
1554 encoder->protected_->loose_mid_side_stereo = value;
1555 return true;
1556}
1557
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001558FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1559{
1560 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001561 FLAC__ASSERT(0 != encoder->private_);
1562 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001563 FLAC__ASSERT(0 != specification);
1564 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1565 return false;
1566#ifdef FLAC__INTEGER_ONLY_LIBRARY
1567 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1568#else
1569 encoder->protected_->num_apodizations = 0;
1570 while(1) {
1571 const char *s = strchr(specification, ';');
1572 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1573 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1574 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1575 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1576 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1577 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1578 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1579 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1580 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1581 else if(n==6 && 0 == strncmp("connes" , specification, n))
1582 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1583 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1584 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1585 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1586 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1587 if (stddev > 0.0 && stddev <= 0.5) {
1588 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1589 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1590 }
1591 }
1592 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1593 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1594 else if(n==4 && 0 == strncmp("hann" , specification, n))
1595 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1596 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1597 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1598 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1599 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1600 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1601 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1602 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1603 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1604 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1605 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1606 if (p >= 0.0 && p <= 1.0) {
1607 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1608 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1609 }
1610 }
1611 else if(n==5 && 0 == strncmp("welch" , specification, n))
1612 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1613 if (encoder->protected_->num_apodizations == 32)
1614 break;
1615 if (s)
1616 specification = s+1;
1617 else
1618 break;
1619 }
1620 if(encoder->protected_->num_apodizations == 0) {
1621 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001622 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1623 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001624 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001625#endif
1626 return true;
1627}
1628
Josh Coalson6afed9f2002-10-16 22:29:47 +00001629FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001630{
Josh Coalson92031602002-07-24 06:02:11 +00001631 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001632 FLAC__ASSERT(0 != encoder->private_);
1633 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001634 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001635 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001636 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001637 return true;
1638}
1639
Josh Coalson6afed9f2002-10-16 22:29:47 +00001640FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001641{
Josh Coalson92031602002-07-24 06:02:11 +00001642 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001643 FLAC__ASSERT(0 != encoder->private_);
1644 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001645 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001646 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001647 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001648 return true;
1649}
1650
Josh Coalson6afed9f2002-10-16 22:29:47 +00001651FLAC_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 +00001652{
Josh Coalson92031602002-07-24 06:02:11 +00001653 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001654 FLAC__ASSERT(0 != encoder->private_);
1655 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001656 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001657 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001658 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001659 return true;
1660}
1661
Josh Coalson6afed9f2002-10-16 22:29:47 +00001662FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001663{
Josh Coalson92031602002-07-24 06:02:11 +00001664 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001665 FLAC__ASSERT(0 != encoder->private_);
1666 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001667 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001668 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001669#if 0
1670 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001671 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001672#else
1673 (void)value;
1674#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001675 return true;
1676}
1677
Josh Coalson6afed9f2002-10-16 22:29:47 +00001678FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001679{
Josh Coalson92031602002-07-24 06:02:11 +00001680 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001681 FLAC__ASSERT(0 != encoder->private_);
1682 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001683 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001684 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001685 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001686 return true;
1687}
1688
Josh Coalson6afed9f2002-10-16 22:29:47 +00001689FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001690{
Josh Coalson92031602002-07-24 06:02:11 +00001691 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001692 FLAC__ASSERT(0 != encoder->private_);
1693 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001694 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001695 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001696 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001697 return true;
1698}
1699
Josh Coalson6afed9f2002-10-16 22:29:47 +00001700FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001701{
Josh Coalson92031602002-07-24 06:02:11 +00001702 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001703 FLAC__ASSERT(0 != encoder->private_);
1704 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001705 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001706 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001707 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001708 return true;
1709}
1710
Josh Coalson6afed9f2002-10-16 22:29:47 +00001711FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001712{
Josh Coalson92031602002-07-24 06:02:11 +00001713 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001714 FLAC__ASSERT(0 != encoder->private_);
1715 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001716 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001717 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001718#if 0
1719 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001720 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001721#else
1722 (void)value;
1723#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001724 return true;
1725}
1726
Josh Coalson6afed9f2002-10-16 22:29:47 +00001727FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001728{
Josh Coalson92031602002-07-24 06:02:11 +00001729 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001730 FLAC__ASSERT(0 != encoder->private_);
1731 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001732 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001733 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001734 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001735 return true;
1736}
1737
Josh Coalson6afed9f2002-10-16 22:29:47 +00001738FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001739{
Josh Coalson92031602002-07-24 06:02:11 +00001740 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001741 FLAC__ASSERT(0 != encoder->private_);
1742 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001743 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001744 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001745 encoder->protected_->metadata = metadata;
1746 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson8da98c82006-10-15 04:24:05 +00001747#if FLAC__HAS_OGG
1748 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1749 return false;
1750#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001751 return true;
1752}
1753
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001754/*
1755 * These three functions are not static, but not publically exposed in
1756 * include/FLAC/ either. They are used by the test suite.
1757 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001758FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001759{
1760 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001761 FLAC__ASSERT(0 != encoder->private_);
1762 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001763 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1764 return false;
1765 encoder->private_->disable_constant_subframes = value;
1766 return true;
1767}
1768
Josh Coalson6afed9f2002-10-16 22:29:47 +00001769FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001770{
1771 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001772 FLAC__ASSERT(0 != encoder->private_);
1773 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001774 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1775 return false;
1776 encoder->private_->disable_fixed_subframes = value;
1777 return true;
1778}
1779
Josh Coalson6afed9f2002-10-16 22:29:47 +00001780FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001781{
1782 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001783 FLAC__ASSERT(0 != encoder->private_);
1784 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001785 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1786 return false;
1787 encoder->private_->disable_verbatim_subframes = value;
1788 return true;
1789}
1790
Josh Coalson6afed9f2002-10-16 22:29:47 +00001791FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001792{
Josh Coalson92031602002-07-24 06:02:11 +00001793 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001794 FLAC__ASSERT(0 != encoder->private_);
1795 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001796 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001797}
1798
Josh Coalson6afed9f2002-10-16 22:29:47 +00001799FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001800{
1801 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001802 FLAC__ASSERT(0 != encoder->private_);
1803 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001804 if(encoder->protected_->verify)
1805 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1806 else
1807 return FLAC__STREAM_DECODER_UNINITIALIZED;
1808}
1809
Josh Coalson02954222002-11-08 06:16:31 +00001810FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1811{
Josh Coalson8da98c82006-10-15 04:24:05 +00001812 FLAC__ASSERT(0 != encoder);
1813 FLAC__ASSERT(0 != encoder->private_);
1814 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson02954222002-11-08 06:16:31 +00001815 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1816 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1817 else
Josh Coalson807140d2003-09-24 22:10:51 +00001818 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001819}
1820
Josh Coalson6afed9f2002-10-16 22:29:47 +00001821FLAC_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 +00001822{
1823 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001824 FLAC__ASSERT(0 != encoder->private_);
1825 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson589f8c72002-08-07 23:54:55 +00001826 if(0 != absolute_sample)
1827 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1828 if(0 != frame_number)
1829 *frame_number = encoder->private_->verify.error_stats.frame_number;
1830 if(0 != channel)
1831 *channel = encoder->private_->verify.error_stats.channel;
1832 if(0 != sample)
1833 *sample = encoder->private_->verify.error_stats.sample;
1834 if(0 != expected)
1835 *expected = encoder->private_->verify.error_stats.expected;
1836 if(0 != got)
1837 *got = encoder->private_->verify.error_stats.got;
1838}
1839
Josh Coalson6afed9f2002-10-16 22:29:47 +00001840FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001841{
1842 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001843 FLAC__ASSERT(0 != encoder->private_);
1844 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001845 return encoder->protected_->verify;
1846}
1847
Josh Coalson6afed9f2002-10-16 22:29:47 +00001848FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001849{
Josh Coalson92031602002-07-24 06:02:11 +00001850 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001851 FLAC__ASSERT(0 != encoder->private_);
1852 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001853 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001854}
1855
Josh Coalson6afed9f2002-10-16 22:29:47 +00001856FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001857{
Josh Coalson92031602002-07-24 06:02:11 +00001858 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001859 FLAC__ASSERT(0 != encoder->private_);
1860 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001861 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001862}
1863
Josh Coalson6afed9f2002-10-16 22:29:47 +00001864FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001865{
Josh Coalson92031602002-07-24 06:02:11 +00001866 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001867 FLAC__ASSERT(0 != encoder->private_);
1868 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001869 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001870}
1871
Josh Coalson6afed9f2002-10-16 22:29:47 +00001872FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001873{
Josh Coalson92031602002-07-24 06:02:11 +00001874 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001875 FLAC__ASSERT(0 != encoder->private_);
1876 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001877 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001878}
1879
Josh Coalson6afed9f2002-10-16 22:29:47 +00001880FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001881{
Josh Coalson92031602002-07-24 06:02:11 +00001882 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001883 FLAC__ASSERT(0 != encoder->private_);
1884 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001885 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001886}
1887
Josh Coalson425609c2006-11-03 16:08:52 +00001888FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1889{
1890 FLAC__ASSERT(0 != encoder);
1891 FLAC__ASSERT(0 != encoder->private_);
1892 FLAC__ASSERT(0 != encoder->protected_);
1893 return encoder->protected_->do_mid_side_stereo;
1894}
1895
1896FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1897{
1898 FLAC__ASSERT(0 != encoder);
1899 FLAC__ASSERT(0 != encoder->private_);
1900 FLAC__ASSERT(0 != encoder->protected_);
1901 return encoder->protected_->loose_mid_side_stereo;
1902}
1903
Josh Coalson6afed9f2002-10-16 22:29:47 +00001904FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001905{
Josh Coalson92031602002-07-24 06:02:11 +00001906 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001907 FLAC__ASSERT(0 != encoder->private_);
1908 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001909 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001910}
1911
Josh Coalson6afed9f2002-10-16 22:29:47 +00001912FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001913{
Josh Coalson92031602002-07-24 06:02:11 +00001914 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001915 FLAC__ASSERT(0 != encoder->private_);
1916 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001917 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001918}
1919
Josh Coalson6afed9f2002-10-16 22:29:47 +00001920FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001921{
Josh Coalson92031602002-07-24 06:02:11 +00001922 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001923 FLAC__ASSERT(0 != encoder->private_);
1924 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001925 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001926}
1927
Josh Coalson6afed9f2002-10-16 22:29:47 +00001928FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001929{
Josh Coalson92031602002-07-24 06:02:11 +00001930 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001931 FLAC__ASSERT(0 != encoder->private_);
1932 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001933 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001934}
1935
Josh Coalson6afed9f2002-10-16 22:29:47 +00001936FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001937{
Josh Coalson92031602002-07-24 06:02:11 +00001938 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001939 FLAC__ASSERT(0 != encoder->private_);
1940 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001941 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001942}
1943
Josh Coalson6afed9f2002-10-16 22:29:47 +00001944FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001945{
Josh Coalson92031602002-07-24 06:02:11 +00001946 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001947 FLAC__ASSERT(0 != encoder->private_);
1948 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001949 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001950}
1951
Josh Coalson6afed9f2002-10-16 22:29:47 +00001952FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001953{
Josh Coalson92031602002-07-24 06:02:11 +00001954 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001955 FLAC__ASSERT(0 != encoder->private_);
1956 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001957 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001958}
1959
Josh Coalson6afed9f2002-10-16 22:29:47 +00001960FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001961{
Josh Coalson92031602002-07-24 06:02:11 +00001962 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001963 FLAC__ASSERT(0 != encoder->private_);
1964 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001965 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001966}
1967
Josh Coalson6afed9f2002-10-16 22:29:47 +00001968FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001969{
1970 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001971 FLAC__ASSERT(0 != encoder->private_);
1972 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001973 return encoder->protected_->total_samples_estimate;
1974}
1975
Josh Coalson6afed9f2002-10-16 22:29:47 +00001976FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001977{
1978 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001979 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001980 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001981
Josh Coalsonf1eff452002-07-31 07:05:33 +00001982 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001983 FLAC__ASSERT(0 != encoder->private_);
1984 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001985 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001986
1987 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001988 /*
1989 * we have several flavors of the same basic loop, optimized for
1990 * different conditions:
1991 */
1992 if(encoder->protected_->max_lpc_order > 0) {
1993 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1994 /*
1995 * stereo coding: unroll channel loop
1996 * with LPC: calculate floating point version of signal
1997 */
1998 do {
1999 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002000 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00002001
Josh Coalson49f2f162006-11-09 16:54:52 +00002002 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2003 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002004 x = mid = side = buffer[0][j];
2005 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002006#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002007 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002008#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002009 x = buffer[1][j];
2010 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002011#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002012 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002013#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002014 mid += x;
2015 side -= x;
2016 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2017 encoder->private_->integer_signal_mid_side[1][i] = side;
2018 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002019#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002020 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2021 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002022#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002023 encoder->private_->current_sample_number++;
2024 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002025 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2026 if(i > blocksize) {
2027 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002028 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002029 /* move unprocessed overread samples to beginnings of arrays */
2030 FLAC__ASSERT(i == blocksize+OVERREAD_);
2031 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2032 i--;
2033 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2034 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2035 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2036 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2037#ifndef FLAC__INTEGER_ONLY_LIBRARY
2038 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2039 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2040 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2041 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2042#endif
2043 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002044 }
2045 } while(j < samples);
2046 }
2047 else {
2048 /*
2049 * independent channel coding: buffer each channel in inner loop
2050 * with LPC: calculate floating point version of signal
2051 */
2052 do {
2053 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002054 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002055
Josh Coalson49f2f162006-11-09 16:54:52 +00002056 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2057 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002058 for(channel = 0; channel < channels; channel++) {
2059 x = buffer[channel][j];
2060 encoder->private_->integer_signal[channel][i] = x;
2061#ifndef FLAC__INTEGER_ONLY_LIBRARY
2062 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2063#endif
2064 }
2065 encoder->private_->current_sample_number++;
2066 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002067 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2068 if(i > blocksize) {
2069 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002070 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002071 /* move unprocessed overread samples to beginnings of arrays */
2072 FLAC__ASSERT(i == blocksize+OVERREAD_);
2073 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2074 i--;
2075 for(channel = 0; channel < channels; channel++) {
2076 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2077#ifndef FLAC__INTEGER_ONLY_LIBRARY
2078 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2079#endif
2080 }
2081 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002082 }
2083 } while(j < samples);
2084 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002085 }
2086 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002087 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2088 /*
2089 * stereo coding: unroll channel loop
2090 * without LPC: no need to calculate floating point version of signal
2091 */
2092 do {
2093 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002094 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00002095
Josh Coalson49f2f162006-11-09 16:54:52 +00002096 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2097 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002098 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
2099 x = buffer[1][j];
2100 encoder->private_->integer_signal[1][i] = x;
2101 mid += x;
2102 side -= x;
2103 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2104 encoder->private_->integer_signal_mid_side[1][i] = side;
2105 encoder->private_->integer_signal_mid_side[0][i] = mid;
2106 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002107 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002108 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2109 if(i > blocksize) {
2110 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002111 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002112 /* move unprocessed overread samples to beginnings of arrays */
2113 FLAC__ASSERT(i == blocksize+OVERREAD_);
2114 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2115 i--;
2116 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2117 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2118 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2119 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2120 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002121 }
2122 } while(j < samples);
2123 }
2124 else {
2125 /*
2126 * independent channel coding: buffer each channel in inner loop
2127 * without LPC: no need to calculate floating point version of signal
2128 */
2129 do {
2130 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002131 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002132
Josh Coalson49f2f162006-11-09 16:54:52 +00002133 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2134 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002135 for(channel = 0; channel < channels; channel++)
2136 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2137 encoder->private_->current_sample_number++;
2138 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002139 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2140 if(i > blocksize) {
2141 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002142 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002143 /* move unprocessed overread samples to beginnings of arrays */
2144 FLAC__ASSERT(i == blocksize+OVERREAD_);
2145 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2146 i--;
2147 for(channel = 0; channel < channels; channel++)
2148 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2149 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002150 }
2151 } while(j < samples);
2152 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002153 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002154
2155 return true;
2156}
2157
Josh Coalson6afed9f2002-10-16 22:29:47 +00002158FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002159{
2160 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00002161 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002162 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002163
Josh Coalsonf1eff452002-07-31 07:05:33 +00002164 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002165 FLAC__ASSERT(0 != encoder->private_);
2166 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002167 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002168
2169 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002170 /*
2171 * we have several flavors of the same basic loop, optimized for
2172 * different conditions:
2173 */
2174 if(encoder->protected_->max_lpc_order > 0) {
2175 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2176 /*
2177 * stereo coding: unroll channel loop
2178 * with LPC: calculate floating point version of signal
2179 */
2180 do {
2181 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002182 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00002183
Josh Coalson49f2f162006-11-09 16:54:52 +00002184 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2185 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002186 x = mid = side = buffer[k++];
2187 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002188#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002189 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002190#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002191 x = buffer[k++];
2192 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002193#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002194 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002195#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002196 mid += x;
2197 side -= x;
2198 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2199 encoder->private_->integer_signal_mid_side[1][i] = side;
2200 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002201#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002202 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2203 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002204#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002205 encoder->private_->current_sample_number++;
2206 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002207 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2208 if(i > blocksize) {
2209 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002210 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002211 /* move unprocessed overread samples to beginnings of arrays */
2212 FLAC__ASSERT(i == blocksize+OVERREAD_);
2213 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2214 i--;
2215 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2216 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2217 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2218 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2219#ifndef FLAC__INTEGER_ONLY_LIBRARY
2220 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2221 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2222 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2223 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2224#endif
2225 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002226 }
2227 } while(j < samples);
2228 }
2229 else {
2230 /*
2231 * independent channel coding: buffer each channel in inner loop
2232 * with LPC: calculate floating point version of signal
2233 */
2234 do {
2235 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002236 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002237
Josh Coalson49f2f162006-11-09 16:54:52 +00002238 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2239 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002240 for(channel = 0; channel < channels; channel++) {
2241 x = buffer[k++];
2242 encoder->private_->integer_signal[channel][i] = x;
2243#ifndef FLAC__INTEGER_ONLY_LIBRARY
2244 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2245#endif
2246 }
2247 encoder->private_->current_sample_number++;
2248 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002249 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2250 if(i > blocksize) {
2251 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002252 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002253 /* move unprocessed overread samples to beginnings of arrays */
2254 FLAC__ASSERT(i == blocksize+OVERREAD_);
2255 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2256 i--;
2257 for(channel = 0; channel < channels; channel++) {
2258 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2259#ifndef FLAC__INTEGER_ONLY_LIBRARY
2260 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2261#endif
2262 }
2263 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002264 }
2265 } while(j < samples);
2266 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002267 }
2268 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002269 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2270 /*
2271 * stereo coding: unroll channel loop
2272 * without LPC: no need to calculate floating point version of signal
2273 */
2274 do {
2275 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002276 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00002277
Josh Coalson49f2f162006-11-09 16:54:52 +00002278 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2279 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002280 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00002281 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002282 encoder->private_->integer_signal[1][i] = x;
2283 mid += x;
2284 side -= x;
2285 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2286 encoder->private_->integer_signal_mid_side[1][i] = side;
2287 encoder->private_->integer_signal_mid_side[0][i] = mid;
2288 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002289 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002290 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2291 if(i > blocksize) {
2292 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002293 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002294 /* move unprocessed overread samples to beginnings of arrays */
2295 FLAC__ASSERT(i == blocksize+OVERREAD_);
2296 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2297 i--;
2298 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2299 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2300 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2301 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2302 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002303 }
2304 } while(j < samples);
2305 }
2306 else {
2307 /*
2308 * independent channel coding: buffer each channel in inner loop
2309 * without LPC: no need to calculate floating point version of signal
2310 */
2311 do {
2312 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002313 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+1-encoder->private_->current_sample_number, samples-j));
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002314
Josh Coalson49f2f162006-11-09 16:54:52 +00002315 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2316 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002317 for(channel = 0; channel < channels; channel++)
2318 encoder->private_->integer_signal[channel][i] = buffer[k++];
2319 encoder->private_->current_sample_number++;
2320 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002321 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2322 if(i > blocksize) {
2323 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002324 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002325 /* move unprocessed overread samples to beginnings of arrays */
2326 FLAC__ASSERT(i == blocksize+OVERREAD_);
2327 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2328 i--;
2329 for(channel = 0; channel < channels; channel++)
2330 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2331 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002332 }
2333 } while(j < samples);
2334 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002335 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002336
2337 return true;
2338}
2339
Josh Coalsonf1eff452002-07-31 07:05:33 +00002340/***********************************************************************
2341 *
2342 * Private class methods
2343 *
2344 ***********************************************************************/
2345
2346void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00002347{
2348 FLAC__ASSERT(0 != encoder);
2349
Josh Coalson47c7b142005-01-29 06:08:58 +00002350#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2351 encoder->protected_->verify = true;
2352#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00002353 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00002354#endif
Josh Coalson92031602002-07-24 06:02:11 +00002355 encoder->protected_->streamable_subset = true;
2356 encoder->protected_->do_mid_side_stereo = false;
2357 encoder->protected_->loose_mid_side_stereo = false;
2358 encoder->protected_->channels = 2;
2359 encoder->protected_->bits_per_sample = 16;
2360 encoder->protected_->sample_rate = 44100;
Josh Coalson425609c2006-11-03 16:08:52 +00002361 encoder->protected_->blocksize = 0;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002362#ifndef FLAC__INTEGER_ONLY_LIBRARY
2363 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00002364 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2365 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002366#endif
Josh Coalson92031602002-07-24 06:02:11 +00002367 encoder->protected_->max_lpc_order = 0;
2368 encoder->protected_->qlp_coeff_precision = 0;
2369 encoder->protected_->do_qlp_coeff_prec_search = false;
2370 encoder->protected_->do_exhaustive_model_search = false;
2371 encoder->protected_->do_escape_coding = false;
2372 encoder->protected_->min_residual_partition_order = 0;
2373 encoder->protected_->max_residual_partition_order = 0;
2374 encoder->protected_->rice_parameter_search_dist = 0;
2375 encoder->protected_->total_samples_estimate = 0;
2376 encoder->protected_->metadata = 0;
2377 encoder->protected_->num_metadata_blocks = 0;
2378
Josh Coalson6b21f662006-09-13 01:42:27 +00002379 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002380 encoder->private_->disable_constant_subframes = false;
2381 encoder->private_->disable_fixed_subframes = false;
2382 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002383#if FLAC__HAS_OGG
2384 encoder->private_->is_ogg = false;
2385#endif
2386 encoder->private_->read_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002387 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002388 encoder->private_->seek_callback = 0;
2389 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002390 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002391 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002392 encoder->private_->client_data = 0;
Josh Coalson8da98c82006-10-15 04:24:05 +00002393
2394#if FLAC__HAS_OGG
2395 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2396#endif
Josh Coalson92031602002-07-24 06:02:11 +00002397}
2398
Josh Coalsonf1eff452002-07-31 07:05:33 +00002399void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00002400{
2401 unsigned i, channel;
2402
Josh Coalsonf1eff452002-07-31 07:05:33 +00002403 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00002404 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002405 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002406 free(encoder->private_->integer_signal_unaligned[i]);
2407 encoder->private_->integer_signal_unaligned[i] = 0;
2408 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002409#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002410 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002411 free(encoder->private_->real_signal_unaligned[i]);
2412 encoder->private_->real_signal_unaligned[i] = 0;
2413 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002414#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002415 }
2416 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002417 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002418 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2419 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2420 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002421#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002422 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002423 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2424 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2425 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002426#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002427 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002428#ifndef FLAC__INTEGER_ONLY_LIBRARY
2429 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2430 if(0 != encoder->private_->window_unaligned[i]) {
2431 free(encoder->private_->window_unaligned[i]);
2432 encoder->private_->window_unaligned[i] = 0;
2433 }
2434 }
2435 if(0 != encoder->private_->windowed_signal_unaligned) {
2436 free(encoder->private_->windowed_signal_unaligned);
2437 encoder->private_->windowed_signal_unaligned = 0;
2438 }
2439#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002440 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2441 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002442 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002443 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2444 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2445 }
2446 }
2447 }
2448 for(channel = 0; channel < 2; channel++) {
2449 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002450 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002451 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2452 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2453 }
2454 }
2455 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002456 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002457 free(encoder->private_->abs_residual_unaligned);
2458 encoder->private_->abs_residual_unaligned = 0;
2459 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002460 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002461 free(encoder->private_->abs_residual_partition_sums_unaligned);
2462 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2463 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002464 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002465 free(encoder->private_->raw_bits_per_partition_unaligned);
2466 encoder->private_->raw_bits_per_partition_unaligned = 0;
2467 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002468 if(encoder->protected_->verify) {
2469 for(i = 0; i < encoder->protected_->channels; i++) {
2470 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2471 free(encoder->private_->verify.input_fifo.data[i]);
2472 encoder->private_->verify.input_fifo.data[i] = 0;
2473 }
2474 }
2475 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002476 FLAC__bitbuffer_free(encoder->private_->frame);
2477}
2478
Josh Coalson85aaed82006-11-09 01:19:13 +00002479FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002480{
Josh Coalson77e3f312001-06-23 03:03:24 +00002481 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002482 unsigned i, channel;
2483
Josh Coalson85aaed82006-11-09 01:19:13 +00002484 FLAC__ASSERT(new_blocksize > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002485 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2486 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002487
2488 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalson85aaed82006-11-09 01:19:13 +00002489 if(new_blocksize <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002490 return true;
2491
2492 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002493
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002494 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2495 * requires that the input arrays (in our case the integer signals)
2496 * have a buffer of up to 3 zeroes in front (at negative indices) for
Josh Coalson85aaed82006-11-09 01:19:13 +00002497 * alignment purposes; we use 4 in front to keep the data well-aligned.
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002498 */
Josh Coalson8395d022001-07-12 21:25:22 +00002499
Josh Coalsonfa697a92001-08-16 20:07:29 +00002500 for(i = 0; ok && i < encoder->protected_->channels; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002501 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002502 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2503 encoder->private_->integer_signal[i] += 4;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002504#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002505 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002506 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002507#endif
Josh Coalson85aaed82006-11-09 01:19:13 +00002508 }
2509 for(i = 0; ok && i < 2; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002510 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002511 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2512 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson85aaed82006-11-09 01:19:13 +00002513#ifndef FLAC__INTEGER_ONLY_LIBRARY
2514 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002515 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
Josh Coalson85aaed82006-11-09 01:19:13 +00002516#endif
Josh Coalson0a15c142001-06-13 17:59:57 +00002517 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002518#ifndef FLAC__INTEGER_ONLY_LIBRARY
2519 if(ok && encoder->protected_->max_lpc_order > 0) {
2520 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
Josh Coalson85aaed82006-11-09 01:19:13 +00002521 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2522 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 +00002523 }
2524#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002525 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002526 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002527 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 +00002528 }
2529 }
2530 for(channel = 0; ok && channel < 2; channel++) {
2531 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002532 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 +00002533 }
2534 }
Josh Coalson85aaed82006-11-09 01:19:13 +00002535 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 +00002536 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 +00002537 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 +00002538 if(encoder->protected_->do_escape_coding)
Josh Coalson85aaed82006-11-09 01:19:13 +00002539 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 +00002540
Josh Coalson85aaed82006-11-09 01:19:13 +00002541 /* now adjust the windows if the blocksize has changed */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002542#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson85aaed82006-11-09 01:19:13 +00002543 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002544 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2545 switch(encoder->protected_->apodizations[i].type) {
2546 case FLAC__APODIZATION_BARTLETT:
Josh Coalson85aaed82006-11-09 01:19:13 +00002547 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002548 break;
2549 case FLAC__APODIZATION_BARTLETT_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002550 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002551 break;
2552 case FLAC__APODIZATION_BLACKMAN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002553 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002554 break;
2555 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002556 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002557 break;
2558 case FLAC__APODIZATION_CONNES:
Josh Coalson85aaed82006-11-09 01:19:13 +00002559 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002560 break;
2561 case FLAC__APODIZATION_FLATTOP:
Josh Coalson85aaed82006-11-09 01:19:13 +00002562 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002563 break;
2564 case FLAC__APODIZATION_GAUSS:
Josh Coalson85aaed82006-11-09 01:19:13 +00002565 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002566 break;
2567 case FLAC__APODIZATION_HAMMING:
Josh Coalson85aaed82006-11-09 01:19:13 +00002568 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002569 break;
2570 case FLAC__APODIZATION_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002571 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002572 break;
2573 case FLAC__APODIZATION_KAISER_BESSEL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002574 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002575 break;
2576 case FLAC__APODIZATION_NUTTALL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002577 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002578 break;
2579 case FLAC__APODIZATION_RECTANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002580 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002581 break;
2582 case FLAC__APODIZATION_TRIANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002583 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002584 break;
2585 case FLAC__APODIZATION_TUKEY:
Josh Coalson85aaed82006-11-09 01:19:13 +00002586 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002587 break;
2588 case FLAC__APODIZATION_WELCH:
Josh Coalson85aaed82006-11-09 01:19:13 +00002589 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002590 break;
2591 default:
2592 FLAC__ASSERT(0);
2593 /* double protection */
Josh Coalson85aaed82006-11-09 01:19:13 +00002594 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002595 break;
2596 }
2597 }
2598 }
2599#endif
2600
Josh Coalson85aaed82006-11-09 01:19:13 +00002601 if(ok)
2602 encoder->private_->input_capacity = new_blocksize;
2603 else
2604 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2605
Josh Coalson0a15c142001-06-13 17:59:57 +00002606 return ok;
2607}
2608
Josh Coalson49f2f162006-11-09 16:54:52 +00002609FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
Josh Coalson5c491a12002-08-01 06:39:40 +00002610{
2611 const FLAC__byte *buffer;
Josh Coalson352feb52006-10-15 17:08:52 +00002612 size_t bytes;
Josh Coalson5c491a12002-08-01 06:39:40 +00002613
2614 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2615
2616 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2617
Josh Coalsond86e03b2002-08-03 21:56:15 +00002618 if(encoder->protected_->verify) {
2619 encoder->private_->verify.output.data = buffer;
2620 encoder->private_->verify.output.bytes = bytes;
2621 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2622 encoder->private_->verify.needs_magic_hack = true;
2623 }
2624 else {
2625 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2626 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2627 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2628 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2629 return false;
2630 }
2631 }
2632 }
2633
Josh Coalson49f2f162006-11-09 16:54:52 +00002634 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002635 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002636 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002637 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002638 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002639
2640 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2641
Josh Coalsond86e03b2002-08-03 21:56:15 +00002642 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002643 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2644 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 +00002645 }
2646
Josh Coalson5c491a12002-08-01 06:39:40 +00002647 return true;
2648}
2649
Josh Coalson49f2f162006-11-09 16:54:52 +00002650FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
Josh Coalson6b21f662006-09-13 01:42:27 +00002651{
2652 FLAC__StreamEncoderWriteStatus status;
2653 FLAC__uint64 output_position = 0;
2654
2655 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2656 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2657 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2658 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2659 }
2660
2661 /*
2662 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2663 */
2664 if(samples == 0) {
2665 FLAC__MetadataType type = (buffer[0] & 0x7f);
2666 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2667 encoder->protected_->streaminfo_offset = output_position;
2668 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2669 encoder->protected_->seektable_offset = output_position;
2670 }
2671
2672 /*
2673 * Mark the current seek point if hit (if audio_offset == 0 that
2674 * means we're still writing metadata and haven't hit the first
2675 * frame yet)
2676 */
2677 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2678 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2679 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2680 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2681 FLAC__uint64 test_sample;
2682 unsigned i;
2683 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2684 test_sample = encoder->private_->seek_table->points[i].sample_number;
2685 if(test_sample > frame_last_sample) {
2686 break;
2687 }
2688 else if(test_sample >= frame_first_sample) {
2689 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2690 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2691 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2692 encoder->private_->first_seekpoint_to_check++;
2693 /* DO NOT: "break;" and here's why:
2694 * The seektable template may contain more than one target
2695 * sample for any given frame; we will keep looping, generating
2696 * duplicate seekpoints for them, and we'll clean it up later,
2697 * just before writing the seektable back to the metadata.
2698 */
2699 }
2700 else {
2701 encoder->private_->first_seekpoint_to_check++;
2702 }
2703 }
2704 }
2705
Josh Coalson8da98c82006-10-15 04:24:05 +00002706#if FLAC__HAS_OGG
2707 if(encoder->private_->is_ogg) {
2708 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2709 &encoder->protected_->ogg_encoder_aspect,
2710 FLAC__stream_encoder_get_total_samples_estimate(encoder),
2711 buffer,
2712 bytes,
2713 samples,
2714 encoder->private_->current_frame_number,
Josh Coalson49f2f162006-11-09 16:54:52 +00002715 is_last_block,
Josh Coalson8da98c82006-10-15 04:24:05 +00002716 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2717 encoder,
2718 encoder->private_->client_data
2719 );
2720 }
2721 else
2722#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00002723 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2724
2725 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2726 encoder->private_->bytes_written += bytes;
2727 encoder->private_->samples_written += samples;
2728 /* we keep a high watermark on the number of frames written because
2729 * when the encoder goes back to write metadata, 'current_frame'
2730 * will drop back to 0.
2731 */
2732 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2733 }
2734 else
2735 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2736
2737 return status;
2738}
2739
2740/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2741void update_metadata_(const FLAC__StreamEncoder *encoder)
2742{
2743 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2744 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2745 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2746 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2747 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2748 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2749 FLAC__StreamEncoderSeekStatus seek_status;
2750
2751 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2752
2753 /* All this is based on intimate knowledge of the stream header
2754 * layout, but a change to the header format that would break this
2755 * would also break all streams encoded in the previous format.
2756 */
2757
2758 /*
2759 * Write MD5 signature
2760 */
2761 {
2762 const unsigned md5_offset =
2763 FLAC__STREAM_METADATA_HEADER_LENGTH +
2764 (
2765 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2766 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2767 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2768 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2769 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2770 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2771 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2772 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2773 ) / 8;
2774
2775 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2776 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2777 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2778 return;
2779 }
2780 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2781 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2782 return;
2783 }
2784 }
2785
2786 /*
2787 * Write total samples
2788 */
2789 {
2790 const unsigned total_samples_byte_offset =
2791 FLAC__STREAM_METADATA_HEADER_LENGTH +
2792 (
2793 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2794 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2795 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2796 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2797 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2798 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2799 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2800 - 4
2801 ) / 8;
2802
2803 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2804 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2805 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2806 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2807 b[4] = (FLAC__byte)(samples & 0xFF);
2808 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) {
2809 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2810 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2811 return;
2812 }
2813 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2814 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2815 return;
2816 }
2817 }
2818
2819 /*
2820 * Write min/max framesize
2821 */
2822 {
2823 const unsigned min_framesize_offset =
2824 FLAC__STREAM_METADATA_HEADER_LENGTH +
2825 (
2826 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2827 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2828 ) / 8;
2829
2830 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2831 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2832 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2833 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2834 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2835 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2836 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) {
2837 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2838 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2839 return;
2840 }
2841 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2842 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2843 return;
2844 }
2845 }
2846
2847 /*
2848 * Write seektable
2849 */
2850 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2851 unsigned i;
2852
2853 FLAC__format_seektable_sort(encoder->private_->seek_table);
2854
2855 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2856
2857 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) {
2858 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2859 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2860 return;
2861 }
2862
2863 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2864 FLAC__uint64 xx;
2865 unsigned x;
2866 xx = encoder->private_->seek_table->points[i].sample_number;
2867 b[7] = (FLAC__byte)xx; xx >>= 8;
2868 b[6] = (FLAC__byte)xx; xx >>= 8;
2869 b[5] = (FLAC__byte)xx; xx >>= 8;
2870 b[4] = (FLAC__byte)xx; xx >>= 8;
2871 b[3] = (FLAC__byte)xx; xx >>= 8;
2872 b[2] = (FLAC__byte)xx; xx >>= 8;
2873 b[1] = (FLAC__byte)xx; xx >>= 8;
2874 b[0] = (FLAC__byte)xx; xx >>= 8;
2875 xx = encoder->private_->seek_table->points[i].stream_offset;
2876 b[15] = (FLAC__byte)xx; xx >>= 8;
2877 b[14] = (FLAC__byte)xx; xx >>= 8;
2878 b[13] = (FLAC__byte)xx; xx >>= 8;
2879 b[12] = (FLAC__byte)xx; xx >>= 8;
2880 b[11] = (FLAC__byte)xx; xx >>= 8;
2881 b[10] = (FLAC__byte)xx; xx >>= 8;
2882 b[9] = (FLAC__byte)xx; xx >>= 8;
2883 b[8] = (FLAC__byte)xx; xx >>= 8;
2884 x = encoder->private_->seek_table->points[i].frame_samples;
2885 b[17] = (FLAC__byte)x; x >>= 8;
2886 b[16] = (FLAC__byte)x; x >>= 8;
2887 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2888 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2889 return;
2890 }
2891 }
2892 }
2893}
2894
Josh Coalson15b8eb82006-10-15 05:15:55 +00002895#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00002896/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2897void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2898{
2899 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2900 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2901 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2902 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2903 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2904 ogg_page page;
2905
2906 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2907
2908 /* All this is based on intimate knowledge of the stream header
2909 * layout, but a change to the header format that would break this
2910 * would also break all streams encoded in the previous format.
2911 */
2912
2913 /**
2914 ** Write STREAMINFO stats
2915 **/
2916 simple_ogg_page__init(&page);
2917 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2918 simple_ogg_page__clear(&page);
2919 return; /* state already set */
2920 }
2921
2922 /*
2923 * Write MD5 signature
2924 */
2925 {
2926 const unsigned md5_offset =
2927 FLAC__STREAM_METADATA_HEADER_LENGTH +
2928 (
2929 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2930 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2931 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2932 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2933 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2934 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2935 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2936 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2937 ) / 8;
2938
2939 if(md5_offset + 16 > (unsigned)page.body_len) {
2940 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2941 simple_ogg_page__clear(&page);
2942 return;
2943 }
2944 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2945 }
2946
2947 /*
2948 * Write total samples
2949 */
2950 {
2951 const unsigned total_samples_byte_offset =
2952 FLAC__STREAM_METADATA_HEADER_LENGTH +
2953 (
2954 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2955 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2956 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2957 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2958 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2959 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2960 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2961 - 4
2962 ) / 8;
2963
2964 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2965 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2966 simple_ogg_page__clear(&page);
2967 return;
2968 }
2969 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2970 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2971 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2972 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2973 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2974 b[4] = (FLAC__byte)(samples & 0xFF);
2975 memcpy(page.body + total_samples_byte_offset, b, 5);
2976 }
2977
2978 /*
2979 * Write min/max framesize
2980 */
2981 {
2982 const unsigned min_framesize_offset =
2983 FLAC__STREAM_METADATA_HEADER_LENGTH +
2984 (
2985 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2986 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2987 ) / 8;
2988
2989 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2990 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2991 simple_ogg_page__clear(&page);
2992 return;
2993 }
2994 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2995 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2996 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2997 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2998 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2999 b[5] = (FLAC__byte)(max_framesize & 0xFF);
3000 memcpy(page.body + min_framesize_offset, b, 6);
3001 }
3002 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3003 simple_ogg_page__clear(&page);
3004 return; /* state already set */
3005 }
3006 simple_ogg_page__clear(&page);
3007
3008 /*
3009 * Write seektable
3010 */
3011 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
3012 unsigned i;
3013 FLAC__byte *p;
3014
3015 FLAC__format_seektable_sort(encoder->private_->seek_table);
3016
3017 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3018
3019 simple_ogg_page__init(&page);
3020 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3021 simple_ogg_page__clear(&page);
3022 return; /* state already set */
3023 }
3024
3025 if(FLAC__STREAM_METADATA_HEADER_LENGTH + (18*encoder->private_->seek_table->num_points) > (unsigned)page.body_len) {
3026 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3027 simple_ogg_page__clear(&page);
3028 return;
3029 }
3030
3031 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3032 FLAC__uint64 xx;
3033 unsigned x;
3034 xx = encoder->private_->seek_table->points[i].sample_number;
3035 b[7] = (FLAC__byte)xx; xx >>= 8;
3036 b[6] = (FLAC__byte)xx; xx >>= 8;
3037 b[5] = (FLAC__byte)xx; xx >>= 8;
3038 b[4] = (FLAC__byte)xx; xx >>= 8;
3039 b[3] = (FLAC__byte)xx; xx >>= 8;
3040 b[2] = (FLAC__byte)xx; xx >>= 8;
3041 b[1] = (FLAC__byte)xx; xx >>= 8;
3042 b[0] = (FLAC__byte)xx; xx >>= 8;
3043 xx = encoder->private_->seek_table->points[i].stream_offset;
3044 b[15] = (FLAC__byte)xx; xx >>= 8;
3045 b[14] = (FLAC__byte)xx; xx >>= 8;
3046 b[13] = (FLAC__byte)xx; xx >>= 8;
3047 b[12] = (FLAC__byte)xx; xx >>= 8;
3048 b[11] = (FLAC__byte)xx; xx >>= 8;
3049 b[10] = (FLAC__byte)xx; xx >>= 8;
3050 b[9] = (FLAC__byte)xx; xx >>= 8;
3051 b[8] = (FLAC__byte)xx; xx >>= 8;
3052 x = encoder->private_->seek_table->points[i].frame_samples;
3053 b[17] = (FLAC__byte)x; x >>= 8;
3054 b[16] = (FLAC__byte)x; x >>= 8;
3055 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
3056 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
3057 simple_ogg_page__clear(&page);
3058 return;
3059 }
3060 memcpy(p, b, 18);
3061 }
3062
3063 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3064 simple_ogg_page__clear(&page);
3065 return; /* state already set */
3066 }
3067 simple_ogg_page__clear(&page);
3068 }
3069}
Josh Coalson15b8eb82006-10-15 05:15:55 +00003070#endif
Josh Coalson8da98c82006-10-15 04:24:05 +00003071
Josh Coalson49f2f162006-11-09 16:54:52 +00003072FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
Josh Coalson0a15c142001-06-13 17:59:57 +00003073{
Josh Coalsonfa697a92001-08-16 20:07:29 +00003074 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003075
3076 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003077 * Accumulate raw signal to the MD5 signature
3078 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00003079 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 +00003080 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003081 return false;
3082 }
3083
3084 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003085 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003086 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003087 if(!process_subframes_(encoder, is_fractional_block)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003088 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003089 return false;
3090 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003091
3092 /*
3093 * Zero-pad the frame to a byte_boundary
3094 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003095 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003096 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003097 return false;
3098 }
3099
3100 /*
Josh Coalson215af572001-03-27 01:15:58 +00003101 * CRC-16 the whole thing
3102 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003103 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
3104 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 +00003105
3106 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003107 * Write it
3108 */
Josh Coalson49f2f162006-11-09 16:54:52 +00003109 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00003110 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003111 return false;
3112 }
3113
3114 /*
3115 * Get ready for the next frame
3116 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003117 encoder->private_->current_sample_number = 0;
3118 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00003119 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003120
3121 return true;
3122}
3123
Josh Coalson85aaed82006-11-09 01:19:13 +00003124FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003125{
3126 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003127 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003128 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003129
3130 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00003131 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00003132 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003133 if(is_fractional_block) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003134 max_partition_order = 0;
3135 }
3136 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003137 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
3138 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003139 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003140 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003141
Josh Coalsonfa697a92001-08-16 20:07:29 +00003142 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 +00003143
Josh Coalson94e02cd2001-01-25 10:41:06 +00003144 /*
3145 * Setup the frame
3146 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003147 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003148 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003149 return false;
3150 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00003151 frame_header.blocksize = encoder->protected_->blocksize;
3152 frame_header.sample_rate = encoder->protected_->sample_rate;
3153 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003154 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003155 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003156 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003157 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003158
3159 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003160 * Figure out what channel assignments to try
3161 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003162 if(encoder->protected_->do_mid_side_stereo) {
3163 if(encoder->protected_->loose_mid_side_stereo) {
3164 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003165 do_independent = true;
3166 do_mid_side = true;
3167 }
3168 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003169 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003170 do_mid_side = !do_independent;
3171 }
3172 }
3173 else {
3174 do_independent = true;
3175 do_mid_side = true;
3176 }
3177 }
3178 else {
3179 do_independent = true;
3180 do_mid_side = false;
3181 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003182
Josh Coalson1b689822001-05-31 20:11:02 +00003183 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003184
3185 /*
Josh Coalson82b73242001-03-28 22:17:05 +00003186 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00003187 */
3188 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003189 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003190 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003191 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3192 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00003193 }
Josh Coalson859bc542001-03-27 22:22:27 +00003194 }
3195 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003196 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00003197 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003198 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003199 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3200 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00003201 }
Josh Coalson859bc542001-03-27 22:22:27 +00003202 }
3203
3204 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003205 * First do a normal encoding pass of each independent channel
3206 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003207 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003208 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003209 if(!
3210 process_subframe_(
3211 encoder,
3212 min_partition_order,
3213 max_partition_order,
3214 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003215 &frame_header,
3216 encoder->private_->subframe_bps[channel],
3217 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003218#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003219 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003220#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003221 encoder->private_->subframe_workspace_ptr[channel],
3222 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3223 encoder->private_->residual_workspace[channel],
3224 encoder->private_->best_subframe+channel,
3225 encoder->private_->best_subframe_bits+channel
3226 )
3227 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003228 return false;
3229 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003230 }
3231
3232 /*
3233 * Now do mid and side channels if requested
3234 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003235 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003236 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003237
3238 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003239 if(!
3240 process_subframe_(
3241 encoder,
3242 min_partition_order,
3243 max_partition_order,
3244 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003245 &frame_header,
3246 encoder->private_->subframe_bps_mid_side[channel],
3247 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003248#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003249 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003250#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003251 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3252 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3253 encoder->private_->residual_workspace_mid_side[channel],
3254 encoder->private_->best_subframe_mid_side+channel,
3255 encoder->private_->best_subframe_bits_mid_side+channel
3256 )
3257 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00003258 return false;
3259 }
3260 }
3261
3262 /*
3263 * Compose the frame bitbuffer
3264 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003265 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00003266 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3267 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003268 FLAC__ChannelAssignment channel_assignment;
3269
Josh Coalsonfa697a92001-08-16 20:07:29 +00003270 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003271
Josh Coalsonfa697a92001-08-16 20:07:29 +00003272 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3273 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 +00003274 }
3275 else {
3276 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3277 unsigned min_bits;
3278 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003279
Josh Coalson1b689822001-05-31 20:11:02 +00003280 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003281
3282 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003283 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3284 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3285 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3286 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 +00003287
Josh Coalson7424d2f2002-11-06 07:10:38 +00003288 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 +00003289 if(bits[ca] < min_bits) {
3290 min_bits = bits[ca];
3291 channel_assignment = ca;
3292 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003293 }
3294 }
3295
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003296 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003297
Josh Coalsond0edb972006-10-07 06:50:08 +00003298 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003299 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003300 return false;
3301 }
3302
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003303 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003304 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003305 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3306 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003307 break;
3308 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003309 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3310 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003311 break;
3312 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003313 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3314 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003315 break;
3316 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003317 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3318 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003319 break;
3320 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003321 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003322 }
Josh Coalson82b73242001-03-28 22:17:05 +00003323
3324 switch(channel_assignment) {
3325 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003326 left_bps = encoder->private_->subframe_bps [0];
3327 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003328 break;
3329 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003330 left_bps = encoder->private_->subframe_bps [0];
3331 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003332 break;
3333 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003334 left_bps = encoder->private_->subframe_bps_mid_side[1];
3335 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003336 break;
3337 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003338 left_bps = encoder->private_->subframe_bps_mid_side[0];
3339 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003340 break;
3341 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003342 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00003343 }
3344
3345 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00003346 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003347 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00003348 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003349 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003350 }
3351 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00003352 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003353 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003354 return false;
3355 }
3356
Josh Coalsonfa697a92001-08-16 20:07:29 +00003357 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson369a6da2006-10-10 00:37:48 +00003358 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 +00003359 /* the above function sets the state for us in case of an error */
3360 return false;
3361 }
3362 }
3363 }
3364
Josh Coalsonfa697a92001-08-16 20:07:29 +00003365 if(encoder->protected_->loose_mid_side_stereo) {
3366 encoder->private_->loose_mid_side_stereo_frame_count++;
3367 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3368 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003369 }
3370
Josh Coalsonfa697a92001-08-16 20:07:29 +00003371 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003372
Josh Coalson94e02cd2001-01-25 10:41:06 +00003373 return true;
3374}
3375
Josh Coalson6fe72f72002-08-20 04:01:59 +00003376FLAC__bool process_subframe_(
3377 FLAC__StreamEncoder *encoder,
3378 unsigned min_partition_order,
3379 unsigned max_partition_order,
3380 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003381 const FLAC__FrameHeader *frame_header,
3382 unsigned subframe_bps,
3383 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003384#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003385 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003386#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003387 FLAC__Subframe *subframe[2],
3388 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3389 FLAC__int32 *residual[2],
3390 unsigned *best_subframe,
3391 unsigned *best_bits
3392)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003393{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003394#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003395 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003396#else
3397 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3398#endif
3399#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003400 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003401 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 +00003402 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003403 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003404 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003405#endif
3406 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003407 unsigned rice_parameter;
3408 unsigned _candidate_bits, _best_bits;
3409 unsigned _best_subframe;
3410
3411 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3412 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003413 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3414 _best_bits = UINT_MAX;
3415 else
3416 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003417
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003418 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3419 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003420 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 +00003421 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003422 if(
3423 !encoder->private_->disable_constant_subframes &&
3424#ifndef FLAC__INTEGER_ONLY_LIBRARY
3425 fixed_residual_bits_per_sample[1] == 0.0
3426#else
3427 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3428#endif
3429 ) {
3430 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003431 unsigned i;
3432 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003433 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003434 if(integer_signal[0] != integer_signal[i]) {
3435 signal_is_constant = false;
3436 break;
3437 }
3438 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003439 }
3440 if(signal_is_constant) {
3441 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
3442 if(_candidate_bits < _best_bits) {
3443 _best_subframe = !_best_subframe;
3444 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003445 }
3446 }
3447 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003448 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3449 /* encode fixed */
3450 if(encoder->protected_->do_exhaustive_model_search) {
3451 min_fixed_order = 0;
3452 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00003453 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003454 else {
3455 min_fixed_order = max_fixed_order = guess_fixed_order;
3456 }
3457 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003458#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003459 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003460 continue; /* don't even try */
3461 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 +00003462#else
3463 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3464 continue; /* don't even try */
3465 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 */
3466#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003467 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003468 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3469#ifdef DEBUG_VERBOSE
3470 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3471#endif
3472 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3473 }
3474 _candidate_bits =
3475 evaluate_fixed_subframe_(
3476 encoder,
3477 integer_signal,
3478 residual[!_best_subframe],
3479 encoder->private_->abs_residual,
3480 encoder->private_->abs_residual_partition_sums,
3481 encoder->private_->raw_bits_per_partition,
3482 frame_header->blocksize,
3483 subframe_bps,
3484 fixed_order,
3485 rice_parameter,
3486 min_partition_order,
3487 max_partition_order,
3488 precompute_partition_sums,
3489 encoder->protected_->do_escape_coding,
3490 encoder->protected_->rice_parameter_search_dist,
3491 subframe[!_best_subframe],
3492 partitioned_rice_contents[!_best_subframe]
3493 );
3494 if(_candidate_bits < _best_bits) {
3495 _best_subframe = !_best_subframe;
3496 _best_bits = _candidate_bits;
3497 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003498 }
3499 }
3500
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003501#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00003502 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003503 if(encoder->protected_->max_lpc_order > 0) {
3504 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003505 max_lpc_order = frame_header->blocksize-1;
3506 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00003507 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003508 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003509 unsigned a;
3510 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00003511 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003512 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3513 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3514 if(autoc[0] != 0.0) {
3515 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3516 if(encoder->protected_->do_exhaustive_model_search) {
3517 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003518 }
3519 else {
Josh Coalsondf598452006-04-28 00:13:34 +00003520 const unsigned guess_lpc_order =
3521 FLAC__lpc_compute_best_order(
3522 lpc_error,
3523 max_lpc_order,
3524 frame_header->blocksize,
3525 subframe_bps + (
3526 encoder->protected_->do_qlp_coeff_prec_search?
3527 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3528 encoder->protected_->qlp_coeff_precision
3529 )
3530 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003531 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003532 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003533 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3534 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3535 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3536 continue; /* don't even try */
3537 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3538 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3539 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00003540#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003541 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 +00003542#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003543 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3544 }
3545 if(encoder->protected_->do_qlp_coeff_prec_search) {
3546 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00003547 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3548 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003549 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00003550 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3551 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003552 else
3553 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3554 }
3555 else {
3556 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3557 }
3558 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3559 _candidate_bits =
3560 evaluate_lpc_subframe_(
3561 encoder,
3562 integer_signal,
3563 residual[!_best_subframe],
3564 encoder->private_->abs_residual,
3565 encoder->private_->abs_residual_partition_sums,
3566 encoder->private_->raw_bits_per_partition,
3567 encoder->private_->lp_coeff[lpc_order-1],
3568 frame_header->blocksize,
3569 subframe_bps,
3570 lpc_order,
3571 qlp_coeff_precision,
3572 rice_parameter,
3573 min_partition_order,
3574 max_partition_order,
3575 precompute_partition_sums,
3576 encoder->protected_->do_escape_coding,
3577 encoder->protected_->rice_parameter_search_dist,
3578 subframe[!_best_subframe],
3579 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003580 );
3581 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3582 if(_candidate_bits < _best_bits) {
3583 _best_subframe = !_best_subframe;
3584 _best_bits = _candidate_bits;
3585 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00003586 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003587 }
3588 }
3589 }
3590 }
3591 }
3592 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003593#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00003594 }
3595 }
3596
Josh Coalson72695802002-10-11 06:25:16 +00003597 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3598 if(_best_bits == UINT_MAX) {
3599 FLAC__ASSERT(_best_subframe == 0);
3600 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3601 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003602
Josh Coalson94e02cd2001-01-25 10:41:06 +00003603 *best_subframe = _best_subframe;
3604 *best_bits = _best_bits;
3605
3606 return true;
3607}
3608
Josh Coalson6fe72f72002-08-20 04:01:59 +00003609FLAC__bool add_subframe_(
3610 FLAC__StreamEncoder *encoder,
3611 const FLAC__FrameHeader *frame_header,
3612 unsigned subframe_bps,
3613 const FLAC__Subframe *subframe,
3614 FLAC__BitBuffer *frame
3615)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003616{
3617 switch(subframe->type) {
3618 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003619 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003620 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003621 return false;
3622 }
3623 break;
3624 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00003625 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 +00003626 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003627 return false;
3628 }
3629 break;
3630 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00003631 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 +00003632 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003633 return false;
3634 }
3635 break;
3636 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00003637 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003638 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003639 return false;
3640 }
3641 break;
3642 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003643 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003644 }
3645
3646 return true;
3647}
3648
Josh Coalson6fe72f72002-08-20 04:01:59 +00003649unsigned evaluate_constant_subframe_(
3650 const FLAC__int32 signal,
3651 unsigned subframe_bps,
3652 FLAC__Subframe *subframe
3653)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003654{
3655 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3656 subframe->data.constant.value = signal;
3657
Josh Coalson82b73242001-03-28 22:17:05 +00003658 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 +00003659}
3660
Josh Coalson6fe72f72002-08-20 04:01:59 +00003661unsigned evaluate_fixed_subframe_(
3662 FLAC__StreamEncoder *encoder,
3663 const FLAC__int32 signal[],
3664 FLAC__int32 residual[],
3665 FLAC__uint32 abs_residual[],
3666 FLAC__uint64 abs_residual_partition_sums[],
3667 unsigned raw_bits_per_partition[],
3668 unsigned blocksize,
3669 unsigned subframe_bps,
3670 unsigned order,
3671 unsigned rice_parameter,
3672 unsigned min_partition_order,
3673 unsigned max_partition_order,
3674 FLAC__bool precompute_partition_sums,
3675 FLAC__bool do_escape_coding,
3676 unsigned rice_parameter_search_dist,
3677 FLAC__Subframe *subframe,
3678 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3679)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003680{
3681 unsigned i, residual_bits;
3682 const unsigned residual_samples = blocksize - order;
3683
3684 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3685
3686 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3687
3688 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003689 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003690 subframe->data.fixed.residual = residual;
3691
Josh Coalson6fe72f72002-08-20 04:01:59 +00003692 residual_bits =
3693 find_best_partition_order_(
3694 encoder->private_,
3695 residual,
3696 abs_residual,
3697 abs_residual_partition_sums,
3698 raw_bits_per_partition,
3699 residual_samples,
3700 order,
3701 rice_parameter,
3702 min_partition_order,
3703 max_partition_order,
3704 precompute_partition_sums,
3705 do_escape_coding,
3706 rice_parameter_search_dist,
3707 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3708 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003709
3710 subframe->data.fixed.order = order;
3711 for(i = 0; i < order; i++)
3712 subframe->data.fixed.warmup[i] = signal[i];
3713
Josh Coalson82b73242001-03-28 22:17:05 +00003714 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 +00003715}
3716
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003717#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003718unsigned evaluate_lpc_subframe_(
3719 FLAC__StreamEncoder *encoder,
3720 const FLAC__int32 signal[],
3721 FLAC__int32 residual[],
3722 FLAC__uint32 abs_residual[],
3723 FLAC__uint64 abs_residual_partition_sums[],
3724 unsigned raw_bits_per_partition[],
3725 const FLAC__real lp_coeff[],
3726 unsigned blocksize,
3727 unsigned subframe_bps,
3728 unsigned order,
3729 unsigned qlp_coeff_precision,
3730 unsigned rice_parameter,
3731 unsigned min_partition_order,
3732 unsigned max_partition_order,
3733 FLAC__bool precompute_partition_sums,
3734 FLAC__bool do_escape_coding,
3735 unsigned rice_parameter_search_dist,
3736 FLAC__Subframe *subframe,
3737 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3738)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003739{
Josh Coalson77e3f312001-06-23 03:03:24 +00003740 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003741 unsigned i, residual_bits;
3742 int quantization, ret;
3743 const unsigned residual_samples = blocksize - order;
3744
Josh Coalson20ac2c12002-08-30 05:47:14 +00003745 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3746 if(subframe_bps <= 16) {
3747 FLAC__ASSERT(order > 0);
3748 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3749 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3750 }
3751
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003752 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003753 if(ret != 0)
3754 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3755
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003756 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3757 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3758 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3759 else
3760 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 +00003761 else
3762 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 +00003763
3764 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3765
3766 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003767 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003768 subframe->data.lpc.residual = residual;
3769
Josh Coalson6fe72f72002-08-20 04:01:59 +00003770 residual_bits =
3771 find_best_partition_order_(
3772 encoder->private_,
3773 residual,
3774 abs_residual,
3775 abs_residual_partition_sums,
3776 raw_bits_per_partition,
3777 residual_samples,
3778 order,
3779 rice_parameter,
3780 min_partition_order,
3781 max_partition_order,
3782 precompute_partition_sums,
3783 do_escape_coding,
3784 rice_parameter_search_dist,
3785 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3786 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003787
3788 subframe->data.lpc.order = order;
3789 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3790 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003791 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003792 for(i = 0; i < order; i++)
3793 subframe->data.lpc.warmup[i] = signal[i];
3794
Josh Coalson82b73242001-03-28 22:17:05 +00003795 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 +00003796}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003797#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003798
Josh Coalson6fe72f72002-08-20 04:01:59 +00003799unsigned evaluate_verbatim_subframe_(
3800 const FLAC__int32 signal[],
3801 unsigned blocksize,
3802 unsigned subframe_bps,
3803 FLAC__Subframe *subframe
3804)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003805{
3806 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3807
3808 subframe->data.verbatim.data = signal;
3809
Josh Coalson82b73242001-03-28 22:17:05 +00003810 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 +00003811}
3812
Josh Coalson6fe72f72002-08-20 04:01:59 +00003813unsigned find_best_partition_order_(
3814 FLAC__StreamEncoderPrivate *private_,
3815 const FLAC__int32 residual[],
3816 FLAC__uint32 abs_residual[],
3817 FLAC__uint64 abs_residual_partition_sums[],
3818 unsigned raw_bits_per_partition[],
3819 unsigned residual_samples,
3820 unsigned predictor_order,
3821 unsigned rice_parameter,
3822 unsigned min_partition_order,
3823 unsigned max_partition_order,
3824 FLAC__bool precompute_partition_sums,
3825 FLAC__bool do_escape_coding,
3826 unsigned rice_parameter_search_dist,
3827 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3828)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003829{
Josh Coalson77e3f312001-06-23 03:03:24 +00003830 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003831 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00003832 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00003833 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003834 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003835
Josh Coalson2051dd42001-04-12 22:22:34 +00003836 /* compute abs(residual) for use later */
3837 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3838 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00003839 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00003840 }
3841
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003842 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 +00003843 min_partition_order = min(min_partition_order, max_partition_order);
3844
Josh Coalson8395d022001-07-12 21:25:22 +00003845 if(precompute_partition_sums) {
3846 int partition_order;
3847 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003848
Josh Coalsonf1eff452002-07-31 07:05:33 +00003849 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 +00003850
3851 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00003852 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 +00003853
3854 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3855#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003856 if(!
3857 set_partitioned_rice_with_precompute_(
3858 residual,
3859 abs_residual_partition_sums+sum,
3860 raw_bits_per_partition+sum,
3861 residual_samples,
3862 predictor_order,
3863 rice_parameter,
3864 rice_parameter_search_dist,
3865 (unsigned)partition_order,
3866 do_escape_coding,
3867 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3868 &residual_bits
3869 )
3870 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003871#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003872 if(!
3873 set_partitioned_rice_with_precompute_(
3874 abs_residual,
3875 abs_residual_partition_sums+sum,
3876 raw_bits_per_partition+sum,
3877 residual_samples,
3878 predictor_order,
3879 rice_parameter,
3880 rice_parameter_search_dist,
3881 (unsigned)partition_order,
3882 do_escape_coding,
3883 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3884 &residual_bits
3885 )
3886 )
Josh Coalson8395d022001-07-12 21:25:22 +00003887#endif
3888 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003889 FLAC__ASSERT(best_residual_bits != 0);
3890 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003891 }
3892 sum += 1u << partition_order;
3893 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3894 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003895 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003896 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003897 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003898 }
3899 }
Josh Coalson8395d022001-07-12 21:25:22 +00003900 else {
3901 unsigned partition_order;
3902 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3903#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003904 if(!
3905 set_partitioned_rice_(
3906 abs_residual,
3907 residual,
3908 residual_samples,
3909 predictor_order,
3910 rice_parameter,
3911 rice_parameter_search_dist,
3912 partition_order,
3913 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3914 &residual_bits
3915 )
3916 )
Josh Coalson8395d022001-07-12 21:25:22 +00003917#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003918 if(!
3919 set_partitioned_rice_(
3920 abs_residual,
3921 residual_samples,
3922 predictor_order,
3923 rice_parameter,
3924 rice_parameter_search_dist,
3925 partition_order,
3926 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3927 &residual_bits
3928 )
3929 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003930#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003931 {
3932 FLAC__ASSERT(best_residual_bits != 0);
3933 break;
3934 }
3935 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3936 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003937 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003938 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003939 }
3940 }
3941 }
3942
Josh Coalsona37ba462002-08-19 21:36:39 +00003943 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003944 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003945 * it is const to the outside world.
3946 */
3947 {
3948 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3949 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3950 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3951 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3952 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003953
3954 return best_residual_bits;
3955}
3956
Josh Coalson6fe72f72002-08-20 04:01:59 +00003957void precompute_partition_info_sums_(
3958 const FLAC__uint32 abs_residual[],
3959 FLAC__uint64 abs_residual_partition_sums[],
3960 unsigned residual_samples,
3961 unsigned predictor_order,
3962 unsigned min_partition_order,
3963 unsigned max_partition_order
3964)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003965{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003966 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003967 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003968 const unsigned blocksize = residual_samples + predictor_order;
3969
Josh Coalsonaef013c2001-04-24 01:25:42 +00003970 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003971 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003972 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00003973 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003974 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003975 const unsigned partitions = 1u << partition_order;
3976 const unsigned default_partition_samples = blocksize >> partition_order;
3977
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003978 FLAC__ASSERT(default_partition_samples > predictor_order);
3979
3980 for(partition = residual_sample = 0; partition < partitions; partition++) {
3981 partition_samples = default_partition_samples;
3982 if(partition == 0)
3983 partition_samples -= predictor_order;
3984 abs_residual_partition_sum = 0;
3985 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3986 abs_r = abs_residual[residual_sample];
3987 abs_residual_partition_sum += abs_r;
3988 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003989 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003990 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003991 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003992 to_partition = partitions;
3993 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003994 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00003995
Josh Coalson8395d022001-07-12 21:25:22 +00003996 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00003997 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003998 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003999 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004000 const unsigned partitions = 1u << partition_order;
4001 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00004002 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00004003 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00004004 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00004005 from_partition++;
4006 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004007 }
4008 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00004009}
Josh Coalson8395d022001-07-12 21:25:22 +00004010
Josh Coalson6fe72f72002-08-20 04:01:59 +00004011void precompute_partition_info_escapes_(
4012 const FLAC__int32 residual[],
4013 unsigned raw_bits_per_partition[],
4014 unsigned residual_samples,
4015 unsigned predictor_order,
4016 unsigned min_partition_order,
4017 unsigned max_partition_order
4018)
Josh Coalson8395d022001-07-12 21:25:22 +00004019{
4020 int partition_order;
4021 unsigned from_partition, to_partition = 0;
4022 const unsigned blocksize = residual_samples + predictor_order;
4023
4024 /* first do max_partition_order */
4025 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
4026 FLAC__int32 r, residual_partition_min, residual_partition_max;
4027 unsigned silog2_min, silog2_max;
4028 unsigned partition, partition_sample, partition_samples, residual_sample;
4029 const unsigned partitions = 1u << partition_order;
4030 const unsigned default_partition_samples = blocksize >> partition_order;
4031
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004032 FLAC__ASSERT(default_partition_samples > predictor_order);
4033
4034 for(partition = residual_sample = 0; partition < partitions; partition++) {
4035 partition_samples = default_partition_samples;
4036 if(partition == 0)
4037 partition_samples -= predictor_order;
4038 residual_partition_min = residual_partition_max = 0;
4039 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4040 r = residual[residual_sample];
4041 if(r < residual_partition_min)
4042 residual_partition_min = r;
4043 else if(r > residual_partition_max)
4044 residual_partition_max = r;
4045 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00004046 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004047 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
4048 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
4049 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00004050 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004051 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00004052 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00004053 }
4054
4055 /* now merge partitions for lower orders */
4056 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4057 unsigned m;
4058 unsigned i;
4059 const unsigned partitions = 1u << partition_order;
4060 for(i = 0; i < partitions; i++) {
4061 m = raw_bits_per_partition[from_partition];
4062 from_partition++;
4063 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
4064 from_partition++;
4065 to_partition++;
4066 }
4067 }
4068}
Josh Coalson94e02cd2001-01-25 10:41:06 +00004069
Josh Coalson352e0f62001-03-20 22:55:50 +00004070#ifdef VARIABLE_RICE_BITS
4071#undef VARIABLE_RICE_BITS
4072#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004073#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004074#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00004075#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004076
Josh Coalson8395d022001-07-12 21:25:22 +00004077#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004078FLAC__bool set_partitioned_rice_(
4079 const FLAC__uint32 abs_residual[],
4080 const FLAC__int32 residual[],
4081 const unsigned residual_samples,
4082 const unsigned predictor_order,
4083 const unsigned suggested_rice_parameter,
4084 const unsigned rice_parameter_search_dist,
4085 const unsigned partition_order,
4086 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4087 unsigned *bits
4088)
Josh Coalson8395d022001-07-12 21:25:22 +00004089#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004090FLAC__bool set_partitioned_rice_(
4091 const FLAC__uint32 abs_residual[],
4092 const unsigned residual_samples,
4093 const unsigned predictor_order,
4094 const unsigned suggested_rice_parameter,
4095 const unsigned rice_parameter_search_dist,
4096 const unsigned partition_order,
4097 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4098 unsigned *bits
4099)
Josh Coalson8395d022001-07-12 21:25:22 +00004100#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00004101{
Josh Coalson034dfab2001-04-27 19:10:23 +00004102 unsigned rice_parameter, partition_bits;
4103#ifndef NO_RICE_SEARCH
4104 unsigned best_partition_bits;
4105 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4106#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00004107 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004108 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004109
Josh Coalson1b689822001-05-31 20:11:02 +00004110 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00004111
Josh Coalsona37ba462002-08-19 21:36:39 +00004112 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4113 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004114
Josh Coalson94e02cd2001-01-25 10:41:06 +00004115 if(partition_order == 0) {
4116 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00004117
Josh Coalson034dfab2001-04-27 19:10:23 +00004118#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004119 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004120 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004121 min_rice_parameter = 0;
4122 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004123 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4124 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004125 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004126#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004127 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4128#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004129 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004130 }
4131 }
4132 else
4133 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4134
4135 best_partition_bits = 0xffffffff;
4136 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4137#endif
4138#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004139 const unsigned rice_parameter_estimate = rice_parameter-1;
4140 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004141#else
4142 partition_bits = 0;
4143#endif
4144 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4145 for(i = 0; i < residual_samples; i++) {
4146#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004147 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004148#else
4149 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4150#endif
4151 }
4152#ifndef NO_RICE_SEARCH
4153 if(partition_bits < best_partition_bits) {
4154 best_rice_parameter = rice_parameter;
4155 best_partition_bits = partition_bits;
4156 }
4157 }
4158#endif
4159 parameters[0] = best_rice_parameter;
4160 bits_ += best_partition_bits;
4161 }
4162 else {
4163 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004164 unsigned partition_samples;
4165 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00004166 const unsigned partitions = 1u << partition_order;
4167 for(partition = residual_sample = 0; partition < partitions; partition++) {
4168 partition_samples = (residual_samples+predictor_order) >> partition_order;
4169 if(partition == 0) {
4170 if(partition_samples <= predictor_order)
4171 return false;
4172 else
4173 partition_samples -= predictor_order;
4174 }
4175 mean = 0;
4176 save_residual_sample = residual_sample;
4177 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004178 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00004179 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004180 /* we are basically calculating the size in bits of the
4181 * average residual magnitude in the partition:
4182 * rice_parameter = floor(log2(mean/partition_samples))
4183 * 'mean' is not a good name for the variable, it is
4184 * actually the sum of magnitudes of all residual values
4185 * in the partition, so the actual mean is
4186 * mean/partition_samples
4187 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004188 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00004189 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004190 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004191#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004192 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4193#endif
4194 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4195 }
4196
4197#ifndef NO_RICE_SEARCH
4198 if(rice_parameter_search_dist) {
4199 if(rice_parameter < rice_parameter_search_dist)
4200 min_rice_parameter = 0;
4201 else
4202 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4203 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4204 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004205#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004206 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4207#endif
4208 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4209 }
4210 }
4211 else
4212 min_rice_parameter = max_rice_parameter = rice_parameter;
4213
4214 best_partition_bits = 0xffffffff;
4215 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4216#endif
4217#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004218 const unsigned rice_parameter_estimate = rice_parameter-1;
4219 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004220#else
4221 partition_bits = 0;
4222#endif
4223 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4224 save_residual_sample = residual_sample;
4225 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4226#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004227 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004228#else
4229 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4230#endif
4231 }
4232#ifndef NO_RICE_SEARCH
4233 if(rice_parameter != max_rice_parameter)
4234 residual_sample = save_residual_sample;
4235 if(partition_bits < best_partition_bits) {
4236 best_rice_parameter = rice_parameter;
4237 best_partition_bits = partition_bits;
4238 }
4239 }
4240#endif
4241 parameters[partition] = best_rice_parameter;
4242 bits_ += best_partition_bits;
4243 }
4244 }
4245
4246 *bits = bits_;
4247 return true;
4248}
4249
4250#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004251FLAC__bool set_partitioned_rice_with_precompute_(
4252 const FLAC__int32 residual[],
4253 const FLAC__uint64 abs_residual_partition_sums[],
4254 const unsigned raw_bits_per_partition[],
4255 const unsigned residual_samples,
4256 const unsigned predictor_order,
4257 const unsigned suggested_rice_parameter,
4258 const unsigned rice_parameter_search_dist,
4259 const unsigned partition_order,
4260 const FLAC__bool search_for_escapes,
4261 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4262 unsigned *bits
4263)
Josh Coalson8395d022001-07-12 21:25:22 +00004264#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004265FLAC__bool set_partitioned_rice_with_precompute_(
4266 const FLAC__uint32 abs_residual[],
4267 const FLAC__uint64 abs_residual_partition_sums[],
4268 const unsigned raw_bits_per_partition[],
4269 const unsigned residual_samples,
4270 const unsigned predictor_order,
4271 const unsigned suggested_rice_parameter,
4272 const unsigned rice_parameter_search_dist,
4273 const unsigned partition_order,
4274 const FLAC__bool search_for_escapes,
4275 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4276 unsigned *bits
4277)
Josh Coalson8395d022001-07-12 21:25:22 +00004278#endif
4279{
4280 unsigned rice_parameter, partition_bits;
4281#ifndef NO_RICE_SEARCH
4282 unsigned best_partition_bits;
4283 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4284#endif
4285 unsigned flat_bits;
4286 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004287 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004288
4289 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4290
Josh Coalsona37ba462002-08-19 21:36:39 +00004291 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4292 parameters = partitioned_rice_contents->parameters;
4293 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004294
Josh Coalson8395d022001-07-12 21:25:22 +00004295 if(partition_order == 0) {
4296 unsigned i;
4297
4298#ifndef NO_RICE_SEARCH
4299 if(rice_parameter_search_dist) {
4300 if(suggested_rice_parameter < rice_parameter_search_dist)
4301 min_rice_parameter = 0;
4302 else
4303 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4304 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4305 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004306#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004307 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4308#endif
4309 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4310 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004311 }
4312 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004313 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00004314
Josh Coalson034dfab2001-04-27 19:10:23 +00004315 best_partition_bits = 0xffffffff;
4316 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4317#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004318#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004319 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00004320 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004321#else
4322 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004323#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004324 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00004325 for(i = 0; i < residual_samples; i++) {
4326#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00004327 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004328#else
Josh Coalson2051dd42001-04-12 22:22:34 +00004329 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 +00004330#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004331 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004332#ifndef NO_RICE_SEARCH
4333 if(partition_bits < best_partition_bits) {
4334 best_rice_parameter = rice_parameter;
4335 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00004336 }
4337 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004338#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004339 if(search_for_escapes) {
4340 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;
4341 if(flat_bits <= best_partition_bits) {
4342 raw_bits[0] = raw_bits_per_partition[0];
4343 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4344 best_partition_bits = flat_bits;
4345 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004346 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004347 parameters[0] = best_rice_parameter;
4348 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004349 }
4350 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00004351 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004352 unsigned partition_samples;
4353 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004354 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00004355 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004356 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00004357 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004358 if(partition_samples <= predictor_order)
4359 return false;
4360 else
4361 partition_samples -= predictor_order;
4362 }
Josh Coalson05d20792001-06-29 23:12:26 +00004363 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004364 /* we are basically calculating the size in bits of the
4365 * average residual magnitude in the partition:
4366 * rice_parameter = floor(log2(mean/partition_samples))
4367 * 'mean' is not a good name for the variable, it is
4368 * actually the sum of magnitudes of all residual values
4369 * in the partition, so the actual mean is
4370 * mean/partition_samples
4371 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004372 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00004373 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004374 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004375#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004376 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4377#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004378 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004379 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004380
Josh Coalson034dfab2001-04-27 19:10:23 +00004381#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004382 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004383 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004384 min_rice_parameter = 0;
4385 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004386 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4387 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004388 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004389#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004390 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4391#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004392 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004393 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004394 }
4395 else
4396 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00004397
Josh Coalson034dfab2001-04-27 19:10:23 +00004398 best_partition_bits = 0xffffffff;
4399 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4400#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004401#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00004402 const unsigned rice_parameter_estimate = rice_parameter-1;
4403 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004404#else
4405 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004406#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004407 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00004408 save_residual_sample = residual_sample;
4409 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00004410#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00004411 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004412#else
Josh Coalson4dacd192001-06-06 21:11:44 +00004413 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 +00004414#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004415 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004416#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00004417 if(rice_parameter != max_rice_parameter)
4418 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00004419 if(partition_bits < best_partition_bits) {
4420 best_rice_parameter = rice_parameter;
4421 best_partition_bits = partition_bits;
4422 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004423 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004424#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004425 if(search_for_escapes) {
4426 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;
4427 if(flat_bits <= best_partition_bits) {
4428 raw_bits[partition] = raw_bits_per_partition[partition];
4429 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4430 best_partition_bits = flat_bits;
4431 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004432 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004433 parameters[partition] = best_rice_parameter;
4434 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004435 }
4436 }
4437
4438 *bits = bits_;
4439 return true;
4440}
Josh Coalson859bc542001-03-27 22:22:27 +00004441
Josh Coalsonf1eff452002-07-31 07:05:33 +00004442unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00004443{
4444 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00004445 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00004446
4447 for(i = 0; i < samples && !(x&1); i++)
4448 x |= signal[i];
4449
4450 if(x == 0) {
4451 shift = 0;
4452 }
4453 else {
4454 for(shift = 0; !(x&1); shift++)
4455 x >>= 1;
4456 }
4457
4458 if(shift > 0) {
4459 for(i = 0; i < samples; i++)
4460 signal[i] >>= shift;
4461 }
4462
4463 return shift;
4464}
Josh Coalsond86e03b2002-08-03 21:56:15 +00004465
4466void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4467{
4468 unsigned channel;
4469
4470 for(channel = 0; channel < channels; channel++)
4471 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4472
4473 fifo->tail += wide_samples;
4474
4475 FLAC__ASSERT(fifo->tail <= fifo->size);
4476}
4477
4478void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4479{
4480 unsigned channel;
4481 unsigned sample, wide_sample;
4482 unsigned tail = fifo->tail;
4483
4484 sample = input_offset * channels;
4485 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4486 for(channel = 0; channel < channels; channel++)
4487 fifo->data[channel][tail] = input[sample++];
4488 tail++;
4489 }
4490 fifo->tail = tail;
4491
4492 FLAC__ASSERT(fifo->tail <= fifo->size);
4493}
4494
Josh Coalson8065a2d2006-10-15 08:32:56 +00004495FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalsond86e03b2002-08-03 21:56:15 +00004496{
4497 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
Josh Coalson8065a2d2006-10-15 08:32:56 +00004498 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004499 (void)decoder;
4500
4501 if(encoder->private_->verify.needs_magic_hack) {
4502 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4503 *bytes = FLAC__STREAM_SYNC_LENGTH;
4504 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4505 encoder->private_->verify.needs_magic_hack = false;
4506 }
4507 else {
4508 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00004509 /*
4510 * If we get here, a FIFO underflow has occurred,
4511 * which means there is a bug somewhere.
4512 */
4513 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004514 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4515 }
4516 else if(encoded_bytes < *bytes)
4517 *bytes = encoded_bytes;
4518 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4519 encoder->private_->verify.output.data += *bytes;
4520 encoder->private_->verify.output.bytes -= *bytes;
4521 }
4522
4523 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4524}
4525
4526FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4527{
4528 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4529 unsigned channel;
Josh Coalson49f2f162006-11-09 16:54:52 +00004530 const unsigned channels = frame->header.channels;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004531 const unsigned blocksize = frame->header.blocksize;
4532 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4533
Josh Coalson49f2f162006-11-09 16:54:52 +00004534 (void)decoder;
4535
Josh Coalsond86e03b2002-08-03 21:56:15 +00004536 for(channel = 0; channel < channels; channel++) {
4537 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4538 unsigned i, sample = 0;
4539 FLAC__int32 expect = 0, got = 0;
4540
4541 for(i = 0; i < blocksize; i++) {
4542 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4543 sample = i;
4544 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4545 got = (FLAC__int32)buffer[channel][i];
4546 break;
4547 }
4548 }
4549 FLAC__ASSERT(i < blocksize);
4550 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4551 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00004552 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004553 encoder->private_->verify.error_stats.channel = channel;
4554 encoder->private_->verify.error_stats.sample = sample;
4555 encoder->private_->verify.error_stats.expected = expect;
4556 encoder->private_->verify.error_stats.got = got;
4557 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4558 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4559 }
4560 }
4561 /* dequeue the frame from the fifo */
Josh Coalsond86e03b2002-08-03 21:56:15 +00004562 encoder->private_->verify.input_fifo.tail -= blocksize;
Josh Coalson49f2f162006-11-09 16:54:52 +00004563 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
Josh Coalsonb7b57ef2006-11-09 07:06:33 +00004564 for(channel = 0; channel < channels; channel++)
4565 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail * sizeof(encoder->private_->verify.input_fifo.data[0][0]));
Josh Coalsond86e03b2002-08-03 21:56:15 +00004566 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4567}
4568
4569void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4570{
4571 (void)decoder, (void)metadata, (void)client_data;
4572}
4573
4574void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4575{
4576 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4577 (void)decoder, (void)status;
4578 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4579}
Josh Coalson6b21f662006-09-13 01:42:27 +00004580
Josh Coalson8065a2d2006-10-15 08:32:56 +00004581FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalson8da98c82006-10-15 04:24:05 +00004582{
4583 (void)client_data;
4584
Josh Coalson8065a2d2006-10-15 08:32:56 +00004585 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
Josh Coalson8da98c82006-10-15 04:24:05 +00004586 if (*bytes == 0) {
4587 if (feof(encoder->private_->file))
4588 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4589 else if (ferror(encoder->private_->file))
4590 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4591 }
4592 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4593}
4594
Josh Coalson6b21f662006-09-13 01:42:27 +00004595FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4596{
4597 (void)client_data;
4598
4599 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4600 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4601 else
4602 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4603}
4604
4605FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4606{
4607 off_t offset;
4608
4609 (void)client_data;
4610
4611 offset = ftello(encoder->private_->file);
4612
4613 if(offset < 0) {
4614 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4615 }
4616 else {
4617 *absolute_byte_offset = (FLAC__uint64)offset;
4618 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4619 }
4620}
4621
4622#ifdef FLAC__VALGRIND_TESTING
4623static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4624{
4625 size_t ret = fwrite(ptr, size, nmemb, stream);
4626 if(!ferror(stream))
4627 fflush(stream);
4628 return ret;
4629}
4630#else
4631#define local__fwrite fwrite
4632#endif
4633
Josh Coalson352feb52006-10-15 17:08:52 +00004634FLAC__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 +00004635{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004636 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00004637
4638 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson8da98c82006-10-15 04:24:05 +00004639 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4640#if FLAC__HAS_OGG
4641 /* We would like to be able to use 'samples > 0' in the
4642 * clause here but currently because of the nature of our
4643 * Ogg writing implementation, 'samples' is always 0 (see
4644 * ogg_encoder_aspect.c). The downside is extra progress
4645 * callbacks.
4646 */
4647 encoder->private_->is_ogg? true :
4648#endif
4649 samples > 0
4650 );
4651 if(call_it) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004652 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4653 * because at this point in the callback chain, the stats
4654 * have not been updated. Only after we return and control
4655 * gets back to write_frame_() are the stats updated
4656 */
4657 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);
4658 }
Josh Coalson6b21f662006-09-13 01:42:27 +00004659 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4660 }
4661 else
4662 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4663}
4664
4665/*
4666 * This will forcibly set stdout to binary mode (for OSes that require it)
4667 */
4668FILE *get_binary_stdout_()
4669{
4670 /* if something breaks here it is probably due to the presence or
4671 * absence of an underscore before the identifiers 'setmode',
4672 * 'fileno', and/or 'O_BINARY'; check your system header files.
4673 */
4674#if defined _MSC_VER || defined __MINGW32__
4675 _setmode(_fileno(stdout), _O_BINARY);
4676#elif defined __CYGWIN__
4677 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4678 setmode(_fileno(stdout), _O_BINARY);
4679#elif defined __EMX__
4680 setmode(fileno(stdout), O_BINARY);
4681#endif
4682
4683 return stdout;
4684}