blob: 164723e5e31f58cc8c91712ae6f39ef1546ea1cd [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 */
Josh Coalson2beca732006-11-21 01:51:58 +000049#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
50#if _MSC_VER <= 1200 || defined __BORLANDC__ /* @@@ [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 Coalsonf1ac7d92006-11-16 07:20:09 +000067#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +000068#include "private/ogg_helper.h"
Josh Coalsonc986d132006-11-15 08:53:32 +000069#include "private/ogg_mapping.h"
Josh Coalson8da98c82006-10-15 04:24:05 +000070#endif
Josh Coalsonb7023aa2002-08-17 15:23:43 +000071#include "private/stream_encoder_framing.h"
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000072#include "private/window.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000073
74#ifdef min
75#undef min
76#endif
77#define min(x,y) ((x)<(y)?(x):(y))
78
79#ifdef max
80#undef max
81#endif
82#define max(x,y) ((x)>(y)?(x):(y))
83
Josh Coalsond86e03b2002-08-03 21:56:15 +000084typedef struct {
85 FLAC__int32 *data[FLAC__MAX_CHANNELS];
86 unsigned size; /* of each data[] in samples */
87 unsigned tail;
88} verify_input_fifo;
89
90typedef struct {
91 const FLAC__byte *data;
92 unsigned capacity;
93 unsigned bytes;
94} verify_output;
95
96typedef enum {
97 ENCODER_IN_MAGIC = 0,
98 ENCODER_IN_METADATA = 1,
99 ENCODER_IN_AUDIO = 2
100} EncoderStateHint;
101
Josh Coalson425609c2006-11-03 16:08:52 +0000102static struct CompressionLevels {
103 FLAC__bool do_mid_side_stereo;
104 FLAC__bool loose_mid_side_stereo;
Josh Coalson425609c2006-11-03 16:08:52 +0000105 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_[] = {
Josh Coalson4e8fe852006-12-05 01:36:46 +0000114 { false, false, 0, 0, false, false, false, 2, 2, 0 },
115 { true , true , 0, 0, false, false, false, 2, 2, 0 },
116 { true , false, 0, 0, false, false, false, 0, 3, 0 },
117 { false, false, 6, 0, false, false, false, 3, 3, 0 },
118 { true , true , 8, 0, false, false, false, 3, 3, 0 },
119 { true , false, 8, 0, false, false, false, 3, 3, 0 },
120 { true , false, 8, 0, false, false, false, 0, 4, 0 },
121 { true , false, 8, 0, false, false, true , 0, 6, 0 },
122 { true , false, 12, 0, false, false, true , 0, 6, 0 }
Josh Coalson425609c2006-11-03 16:08:52 +0000123};
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 Coalsonf1ac7d92006-11-16 07:20:09 +0000675#if !FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +0000676 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,
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001273 encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
Josh Coalson8da98c82006-10-15 04:24:05 +00001274 file_write_callback_,
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001275 encoder->private_->file == stdout? 0 : file_seek_callback_,
1276 encoder->private_->file == stdout? 0 : file_tell_callback_,
Josh Coalson8da98c82006-10-15 04:24:05 +00001277 /*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 Coalsoncf1422e2006-11-16 01:32:25 +00001368 FLAC__bool error = false;
Josh Coalsona5862262006-11-09 06:58:26 +00001369
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 Coalsoncf1422e2006-11-16 01:32:25 +00001381 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1382 error = 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 Coalsoncf1422e2006-11-16 01:32:25 +00001388 if(!encoder->private_->is_being_deleted) {
1389 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1390 if(encoder->private_->seek_callback) {
Josh Coalson8da98c82006-10-15 04:24:05 +00001391#if FLAC__HAS_OGG
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001392 if(encoder->private_->is_ogg)
1393 update_ogg_metadata_(encoder);
1394 else
Josh Coalson8da98c82006-10-15 04:24:05 +00001395#endif
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001396 update_metadata_(encoder);
Josh Coalson0a15c142001-06-13 17:59:57 +00001397
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001398 /* check if an error occurred while updating metadata */
1399 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1400 error = true;
1401 }
1402 if(encoder->private_->metadata_callback)
1403 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1404 }
1405
1406 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1407 if(!error)
1408 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1409 error = true;
1410 }
1411 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001412
Josh Coalson6b21f662006-09-13 01:42:27 +00001413 if(0 != encoder->private_->file) {
1414 if(encoder->private_->file != stdout)
1415 fclose(encoder->private_->file);
1416 encoder->private_->file = 0;
1417 }
1418
Josh Coalson8da98c82006-10-15 04:24:05 +00001419#if FLAC__HAS_OGG
1420 if(encoder->private_->is_ogg)
1421 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1422#endif
1423
Josh Coalsonf1eff452002-07-31 07:05:33 +00001424 free_(encoder);
1425 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001426
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001427 if(!error)
Josh Coalsona5862262006-11-09 06:58:26 +00001428 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1429
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001430 return !error;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001431}
1432
Josh Coalson71d5c252006-10-15 06:04:55 +00001433FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
Josh Coalson8da98c82006-10-15 04:24:05 +00001434{
1435 FLAC__ASSERT(0 != encoder);
1436 FLAC__ASSERT(0 != encoder->private_);
1437 FLAC__ASSERT(0 != encoder->protected_);
1438 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1439 return false;
Josh Coalsonf1ac7d92006-11-16 07:20:09 +00001440#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00001441 /* can't check encoder->private_->is_ogg since that's not set until init time */
1442 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1443 return true;
1444#else
1445 (void)value;
1446 return false;
1447#endif
1448}
1449
Josh Coalson6afed9f2002-10-16 22:29:47 +00001450FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001451{
1452 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001453 FLAC__ASSERT(0 != encoder->private_);
1454 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001455 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1456 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001457#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001458 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001459#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001460 return true;
1461}
1462
Josh Coalson6afed9f2002-10-16 22:29:47 +00001463FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001464{
Josh Coalson92031602002-07-24 06:02:11 +00001465 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001466 FLAC__ASSERT(0 != encoder->private_);
1467 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001468 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001469 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001470 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001471 return true;
1472}
1473
Josh Coalson6afed9f2002-10-16 22:29:47 +00001474FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001475{
Josh Coalson92031602002-07-24 06:02:11 +00001476 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001477 FLAC__ASSERT(0 != encoder->private_);
1478 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001479 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001480 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001481 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001482 return true;
1483}
1484
Josh Coalson6afed9f2002-10-16 22:29:47 +00001485FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001486{
Josh Coalson92031602002-07-24 06:02:11 +00001487 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001488 FLAC__ASSERT(0 != encoder->private_);
1489 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001490 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001491 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001492 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001493 return true;
1494}
1495
Josh Coalson6afed9f2002-10-16 22:29:47 +00001496FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001497{
Josh Coalson92031602002-07-24 06:02:11 +00001498 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001499 FLAC__ASSERT(0 != encoder->private_);
1500 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001501 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001502 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001503 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001504 return true;
1505}
1506
Josh Coalson425609c2006-11-03 16:08:52 +00001507FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1508{
1509 FLAC__bool ok = true;
1510 FLAC__ASSERT(0 != encoder);
1511 FLAC__ASSERT(0 != encoder->private_);
1512 FLAC__ASSERT(0 != encoder->protected_);
1513 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1514 return false;
1515 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1516 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1517 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1518 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
Josh Coalson4e8fe852006-12-05 01:36:46 +00001519#if 0
1520 /* was: */
Josh Coalson425609c2006-11-03 16:08:52 +00001521 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
Josh Coalson4e8fe852006-12-05 01:36:46 +00001522 /* but it's too hard to specify the string in a locale-specific way */
1523#else
1524 encoder->protected_->num_apodizations = 1;
1525 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1526 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1527#endif
Josh Coalson425609c2006-11-03 16:08:52 +00001528 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1529 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1530 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1531 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1532 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1533 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1534 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1535 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1536 return ok;
1537}
1538
Josh Coalson6afed9f2002-10-16 22:29:47 +00001539FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001540{
Josh Coalson92031602002-07-24 06:02:11 +00001541 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001542 FLAC__ASSERT(0 != encoder->private_);
1543 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001544 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001545 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001546 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001547 return true;
1548}
1549
Josh Coalson425609c2006-11-03 16:08:52 +00001550FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1551{
1552 FLAC__ASSERT(0 != encoder);
1553 FLAC__ASSERT(0 != encoder->private_);
1554 FLAC__ASSERT(0 != encoder->protected_);
1555 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1556 return false;
1557 encoder->protected_->do_mid_side_stereo = value;
1558 return true;
1559}
1560
1561FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1562{
1563 FLAC__ASSERT(0 != encoder);
1564 FLAC__ASSERT(0 != encoder->private_);
1565 FLAC__ASSERT(0 != encoder->protected_);
1566 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1567 return false;
1568 encoder->protected_->loose_mid_side_stereo = value;
1569 return true;
1570}
1571
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001572FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1573{
1574 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001575 FLAC__ASSERT(0 != encoder->private_);
1576 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001577 FLAC__ASSERT(0 != specification);
1578 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1579 return false;
1580#ifdef FLAC__INTEGER_ONLY_LIBRARY
1581 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1582#else
1583 encoder->protected_->num_apodizations = 0;
1584 while(1) {
1585 const char *s = strchr(specification, ';');
1586 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1587 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1588 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1589 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1590 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1591 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1592 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1593 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1594 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1595 else if(n==6 && 0 == strncmp("connes" , specification, n))
1596 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1597 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1598 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1599 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1600 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1601 if (stddev > 0.0 && stddev <= 0.5) {
1602 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1603 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1604 }
1605 }
1606 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1607 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1608 else if(n==4 && 0 == strncmp("hann" , specification, n))
1609 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1610 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1611 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1612 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1613 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1614 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1615 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1616 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1617 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1618 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1619 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1620 if (p >= 0.0 && p <= 1.0) {
1621 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1622 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1623 }
1624 }
1625 else if(n==5 && 0 == strncmp("welch" , specification, n))
1626 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1627 if (encoder->protected_->num_apodizations == 32)
1628 break;
1629 if (s)
1630 specification = s+1;
1631 else
1632 break;
1633 }
1634 if(encoder->protected_->num_apodizations == 0) {
1635 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001636 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1637 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001638 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001639#endif
1640 return true;
1641}
1642
Josh Coalson6afed9f2002-10-16 22:29:47 +00001643FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001644{
Josh Coalson92031602002-07-24 06:02:11 +00001645 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001646 FLAC__ASSERT(0 != encoder->private_);
1647 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001648 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001649 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001650 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001651 return true;
1652}
1653
Josh Coalson6afed9f2002-10-16 22:29:47 +00001654FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001655{
Josh Coalson92031602002-07-24 06:02:11 +00001656 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001657 FLAC__ASSERT(0 != encoder->private_);
1658 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001659 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001660 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001661 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001662 return true;
1663}
1664
Josh Coalson6afed9f2002-10-16 22:29:47 +00001665FLAC_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 +00001666{
Josh Coalson92031602002-07-24 06:02:11 +00001667 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001668 FLAC__ASSERT(0 != encoder->private_);
1669 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001670 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001671 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001672 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001673 return true;
1674}
1675
Josh Coalson6afed9f2002-10-16 22:29:47 +00001676FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001677{
Josh Coalson92031602002-07-24 06:02:11 +00001678 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001679 FLAC__ASSERT(0 != encoder->private_);
1680 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001681 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001682 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001683#if 0
1684 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001685 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001686#else
1687 (void)value;
1688#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001689 return true;
1690}
1691
Josh Coalson6afed9f2002-10-16 22:29:47 +00001692FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001693{
Josh Coalson92031602002-07-24 06:02:11 +00001694 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001695 FLAC__ASSERT(0 != encoder->private_);
1696 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001697 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001698 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001699 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001700 return true;
1701}
1702
Josh Coalson6afed9f2002-10-16 22:29:47 +00001703FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001704{
Josh Coalson92031602002-07-24 06:02:11 +00001705 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001706 FLAC__ASSERT(0 != encoder->private_);
1707 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001708 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001709 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001710 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001711 return true;
1712}
1713
Josh Coalson6afed9f2002-10-16 22:29:47 +00001714FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001715{
Josh Coalson92031602002-07-24 06:02:11 +00001716 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001717 FLAC__ASSERT(0 != encoder->private_);
1718 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001719 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001720 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001721 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001722 return true;
1723}
1724
Josh Coalson6afed9f2002-10-16 22:29:47 +00001725FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001726{
Josh Coalson92031602002-07-24 06:02:11 +00001727 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001728 FLAC__ASSERT(0 != encoder->private_);
1729 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001730 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001731 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001732#if 0
1733 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001734 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001735#else
1736 (void)value;
1737#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001738 return true;
1739}
1740
Josh Coalson6afed9f2002-10-16 22:29:47 +00001741FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001742{
Josh Coalson92031602002-07-24 06:02:11 +00001743 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001744 FLAC__ASSERT(0 != encoder->private_);
1745 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001746 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001747 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001748 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001749 return true;
1750}
1751
Josh Coalson6afed9f2002-10-16 22:29:47 +00001752FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001753{
Josh Coalson92031602002-07-24 06:02:11 +00001754 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001755 FLAC__ASSERT(0 != encoder->private_);
1756 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001757 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001758 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001759 encoder->protected_->metadata = metadata;
1760 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson8da98c82006-10-15 04:24:05 +00001761#if FLAC__HAS_OGG
1762 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1763 return false;
1764#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001765 return true;
1766}
1767
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001768/*
1769 * These three functions are not static, but not publically exposed in
1770 * include/FLAC/ either. They are used by the test suite.
1771 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001772FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001773{
1774 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001775 FLAC__ASSERT(0 != encoder->private_);
1776 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001777 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1778 return false;
1779 encoder->private_->disable_constant_subframes = value;
1780 return true;
1781}
1782
Josh Coalson6afed9f2002-10-16 22:29:47 +00001783FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001784{
1785 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001786 FLAC__ASSERT(0 != encoder->private_);
1787 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001788 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1789 return false;
1790 encoder->private_->disable_fixed_subframes = value;
1791 return true;
1792}
1793
Josh Coalson6afed9f2002-10-16 22:29:47 +00001794FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001795{
1796 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001797 FLAC__ASSERT(0 != encoder->private_);
1798 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001799 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1800 return false;
1801 encoder->private_->disable_verbatim_subframes = value;
1802 return true;
1803}
1804
Josh Coalson6afed9f2002-10-16 22:29:47 +00001805FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001806{
Josh Coalson92031602002-07-24 06:02:11 +00001807 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001808 FLAC__ASSERT(0 != encoder->private_);
1809 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001810 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001811}
1812
Josh Coalson6afed9f2002-10-16 22:29:47 +00001813FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001814{
1815 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001816 FLAC__ASSERT(0 != encoder->private_);
1817 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001818 if(encoder->protected_->verify)
1819 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1820 else
1821 return FLAC__STREAM_DECODER_UNINITIALIZED;
1822}
1823
Josh Coalson02954222002-11-08 06:16:31 +00001824FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1825{
Josh Coalson8da98c82006-10-15 04:24:05 +00001826 FLAC__ASSERT(0 != encoder);
1827 FLAC__ASSERT(0 != encoder->private_);
1828 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson02954222002-11-08 06:16:31 +00001829 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1830 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1831 else
Josh Coalson807140d2003-09-24 22:10:51 +00001832 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001833}
1834
Josh Coalson6afed9f2002-10-16 22:29:47 +00001835FLAC_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 +00001836{
1837 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001838 FLAC__ASSERT(0 != encoder->private_);
1839 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson589f8c72002-08-07 23:54:55 +00001840 if(0 != absolute_sample)
1841 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1842 if(0 != frame_number)
1843 *frame_number = encoder->private_->verify.error_stats.frame_number;
1844 if(0 != channel)
1845 *channel = encoder->private_->verify.error_stats.channel;
1846 if(0 != sample)
1847 *sample = encoder->private_->verify.error_stats.sample;
1848 if(0 != expected)
1849 *expected = encoder->private_->verify.error_stats.expected;
1850 if(0 != got)
1851 *got = encoder->private_->verify.error_stats.got;
1852}
1853
Josh Coalson6afed9f2002-10-16 22:29:47 +00001854FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001855{
1856 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001857 FLAC__ASSERT(0 != encoder->private_);
1858 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001859 return encoder->protected_->verify;
1860}
1861
Josh Coalson6afed9f2002-10-16 22:29:47 +00001862FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001863{
Josh Coalson92031602002-07-24 06:02:11 +00001864 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001865 FLAC__ASSERT(0 != encoder->private_);
1866 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001867 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001868}
1869
Josh Coalson6afed9f2002-10-16 22:29:47 +00001870FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001871{
Josh Coalson92031602002-07-24 06:02:11 +00001872 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001873 FLAC__ASSERT(0 != encoder->private_);
1874 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001875 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001876}
1877
Josh Coalson6afed9f2002-10-16 22:29:47 +00001878FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001879{
Josh Coalson92031602002-07-24 06:02:11 +00001880 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001881 FLAC__ASSERT(0 != encoder->private_);
1882 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001883 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001884}
1885
Josh Coalson6afed9f2002-10-16 22:29:47 +00001886FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001887{
Josh Coalson92031602002-07-24 06:02:11 +00001888 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001889 FLAC__ASSERT(0 != encoder->private_);
1890 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001891 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001892}
1893
Josh Coalson6afed9f2002-10-16 22:29:47 +00001894FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001895{
Josh Coalson92031602002-07-24 06:02:11 +00001896 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001897 FLAC__ASSERT(0 != encoder->private_);
1898 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001899 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001900}
1901
Josh Coalson425609c2006-11-03 16:08:52 +00001902FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1903{
1904 FLAC__ASSERT(0 != encoder);
1905 FLAC__ASSERT(0 != encoder->private_);
1906 FLAC__ASSERT(0 != encoder->protected_);
1907 return encoder->protected_->do_mid_side_stereo;
1908}
1909
1910FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1911{
1912 FLAC__ASSERT(0 != encoder);
1913 FLAC__ASSERT(0 != encoder->private_);
1914 FLAC__ASSERT(0 != encoder->protected_);
1915 return encoder->protected_->loose_mid_side_stereo;
1916}
1917
Josh Coalson6afed9f2002-10-16 22:29:47 +00001918FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001919{
Josh Coalson92031602002-07-24 06:02:11 +00001920 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001921 FLAC__ASSERT(0 != encoder->private_);
1922 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001923 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001924}
1925
Josh Coalson6afed9f2002-10-16 22:29:47 +00001926FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001927{
Josh Coalson92031602002-07-24 06:02:11 +00001928 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001929 FLAC__ASSERT(0 != encoder->private_);
1930 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001931 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001932}
1933
Josh Coalson6afed9f2002-10-16 22:29:47 +00001934FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001935{
Josh Coalson92031602002-07-24 06:02:11 +00001936 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001937 FLAC__ASSERT(0 != encoder->private_);
1938 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001939 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001940}
1941
Josh Coalson6afed9f2002-10-16 22:29:47 +00001942FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001943{
Josh Coalson92031602002-07-24 06:02:11 +00001944 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001945 FLAC__ASSERT(0 != encoder->private_);
1946 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001947 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001948}
1949
Josh Coalson6afed9f2002-10-16 22:29:47 +00001950FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001951{
Josh Coalson92031602002-07-24 06:02:11 +00001952 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001953 FLAC__ASSERT(0 != encoder->private_);
1954 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001955 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001956}
1957
Josh Coalson6afed9f2002-10-16 22:29:47 +00001958FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001959{
Josh Coalson92031602002-07-24 06:02:11 +00001960 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001961 FLAC__ASSERT(0 != encoder->private_);
1962 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001963 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001964}
1965
Josh Coalson6afed9f2002-10-16 22:29:47 +00001966FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001967{
Josh Coalson92031602002-07-24 06:02:11 +00001968 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001969 FLAC__ASSERT(0 != encoder->private_);
1970 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001971 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001972}
1973
Josh Coalson6afed9f2002-10-16 22:29:47 +00001974FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001975{
Josh Coalson92031602002-07-24 06:02:11 +00001976 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001977 FLAC__ASSERT(0 != encoder->private_);
1978 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001979 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001980}
1981
Josh Coalson6afed9f2002-10-16 22:29:47 +00001982FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001983{
1984 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001985 FLAC__ASSERT(0 != encoder->private_);
1986 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001987 return encoder->protected_->total_samples_estimate;
1988}
1989
Josh Coalson6afed9f2002-10-16 22:29:47 +00001990FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001991{
1992 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001993 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001994 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001995
Josh Coalsonf1eff452002-07-31 07:05:33 +00001996 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001997 FLAC__ASSERT(0 != encoder->private_);
1998 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001999 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002000
2001 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002002 /*
2003 * we have several flavors of the same basic loop, optimized for
2004 * different conditions:
2005 */
2006 if(encoder->protected_->max_lpc_order > 0) {
2007 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2008 /*
2009 * stereo coding: unroll channel loop
2010 * with LPC: calculate floating point version of signal
2011 */
2012 do {
2013 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002014 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 +00002015
Josh Coalson49f2f162006-11-09 16:54:52 +00002016 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2017 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002018 x = mid = side = buffer[0][j];
2019 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002020#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002021 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002022#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002023 x = buffer[1][j];
2024 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002025#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002026 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002027#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002028 mid += x;
2029 side -= x;
2030 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2031 encoder->private_->integer_signal_mid_side[1][i] = side;
2032 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002033#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002034 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2035 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002036#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002037 encoder->private_->current_sample_number++;
2038 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002039 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2040 if(i > blocksize) {
2041 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002042 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002043 /* move unprocessed overread samples to beginnings of arrays */
2044 FLAC__ASSERT(i == blocksize+OVERREAD_);
2045 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2046 i--;
2047 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2048 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2049 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2050 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2051#ifndef FLAC__INTEGER_ONLY_LIBRARY
2052 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2053 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2054 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2055 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2056#endif
2057 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002058 }
2059 } while(j < samples);
2060 }
2061 else {
2062 /*
2063 * independent channel coding: buffer each channel in inner loop
2064 * with LPC: calculate floating point version of signal
2065 */
2066 do {
2067 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002068 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 +00002069
Josh Coalson49f2f162006-11-09 16:54:52 +00002070 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2071 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002072 for(channel = 0; channel < channels; channel++) {
2073 x = buffer[channel][j];
2074 encoder->private_->integer_signal[channel][i] = x;
2075#ifndef FLAC__INTEGER_ONLY_LIBRARY
2076 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2077#endif
2078 }
2079 encoder->private_->current_sample_number++;
2080 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002081 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2082 if(i > blocksize) {
2083 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002084 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002085 /* move unprocessed overread samples to beginnings of arrays */
2086 FLAC__ASSERT(i == blocksize+OVERREAD_);
2087 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2088 i--;
2089 for(channel = 0; channel < channels; channel++) {
2090 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2091#ifndef FLAC__INTEGER_ONLY_LIBRARY
2092 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2093#endif
2094 }
2095 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002096 }
2097 } while(j < samples);
2098 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002099 }
2100 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002101 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2102 /*
2103 * stereo coding: unroll channel loop
2104 * without LPC: no need to calculate floating point version of signal
2105 */
2106 do {
2107 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002108 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 +00002109
Josh Coalson49f2f162006-11-09 16:54:52 +00002110 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2111 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002112 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
2113 x = buffer[1][j];
2114 encoder->private_->integer_signal[1][i] = x;
2115 mid += x;
2116 side -= x;
2117 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2118 encoder->private_->integer_signal_mid_side[1][i] = side;
2119 encoder->private_->integer_signal_mid_side[0][i] = mid;
2120 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002121 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002122 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2123 if(i > blocksize) {
2124 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002125 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002126 /* move unprocessed overread samples to beginnings of arrays */
2127 FLAC__ASSERT(i == blocksize+OVERREAD_);
2128 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2129 i--;
2130 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2131 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2132 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2133 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2134 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002135 }
2136 } while(j < samples);
2137 }
2138 else {
2139 /*
2140 * independent channel coding: buffer each channel in inner loop
2141 * without LPC: no need to calculate floating point version of signal
2142 */
2143 do {
2144 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002145 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 +00002146
Josh Coalson49f2f162006-11-09 16:54:52 +00002147 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2148 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002149 for(channel = 0; channel < channels; channel++)
2150 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2151 encoder->private_->current_sample_number++;
2152 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002153 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2154 if(i > blocksize) {
2155 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002156 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002157 /* move unprocessed overread samples to beginnings of arrays */
2158 FLAC__ASSERT(i == blocksize+OVERREAD_);
2159 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2160 i--;
2161 for(channel = 0; channel < channels; channel++)
2162 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2163 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002164 }
2165 } while(j < samples);
2166 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002167 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002168
2169 return true;
2170}
2171
Josh Coalson6afed9f2002-10-16 22:29:47 +00002172FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002173{
2174 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00002175 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002176 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002177
Josh Coalsonf1eff452002-07-31 07:05:33 +00002178 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002179 FLAC__ASSERT(0 != encoder->private_);
2180 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002181 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002182
2183 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002184 /*
2185 * we have several flavors of the same basic loop, optimized for
2186 * different conditions:
2187 */
2188 if(encoder->protected_->max_lpc_order > 0) {
2189 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2190 /*
2191 * stereo coding: unroll channel loop
2192 * with LPC: calculate floating point version of signal
2193 */
2194 do {
2195 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002196 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 +00002197
Josh Coalson49f2f162006-11-09 16:54:52 +00002198 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2199 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002200 x = mid = side = buffer[k++];
2201 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002202#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002203 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002204#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002205 x = buffer[k++];
2206 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002207#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002208 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002209#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002210 mid += x;
2211 side -= x;
2212 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2213 encoder->private_->integer_signal_mid_side[1][i] = side;
2214 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002215#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002216 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2217 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002218#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002219 encoder->private_->current_sample_number++;
2220 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002221 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2222 if(i > blocksize) {
2223 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002224 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002225 /* move unprocessed overread samples to beginnings of arrays */
2226 FLAC__ASSERT(i == blocksize+OVERREAD_);
2227 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2228 i--;
2229 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2230 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2231 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2232 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2233#ifndef FLAC__INTEGER_ONLY_LIBRARY
2234 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2235 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2236 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2237 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2238#endif
2239 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002240 }
2241 } while(j < samples);
2242 }
2243 else {
2244 /*
2245 * independent channel coding: buffer each channel in inner loop
2246 * with LPC: calculate floating point version of signal
2247 */
2248 do {
2249 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002250 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 +00002251
Josh Coalson49f2f162006-11-09 16:54:52 +00002252 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2253 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002254 for(channel = 0; channel < channels; channel++) {
2255 x = buffer[k++];
2256 encoder->private_->integer_signal[channel][i] = x;
2257#ifndef FLAC__INTEGER_ONLY_LIBRARY
2258 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2259#endif
2260 }
2261 encoder->private_->current_sample_number++;
2262 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002263 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2264 if(i > blocksize) {
2265 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002266 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002267 /* move unprocessed overread samples to beginnings of arrays */
2268 FLAC__ASSERT(i == blocksize+OVERREAD_);
2269 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2270 i--;
2271 for(channel = 0; channel < channels; channel++) {
2272 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2273#ifndef FLAC__INTEGER_ONLY_LIBRARY
2274 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2275#endif
2276 }
2277 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002278 }
2279 } while(j < samples);
2280 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002281 }
2282 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002283 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2284 /*
2285 * stereo coding: unroll channel loop
2286 * without LPC: no need to calculate floating point version of signal
2287 */
2288 do {
2289 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002290 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 +00002291
Josh Coalson49f2f162006-11-09 16:54:52 +00002292 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2293 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002294 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00002295 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002296 encoder->private_->integer_signal[1][i] = x;
2297 mid += x;
2298 side -= x;
2299 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2300 encoder->private_->integer_signal_mid_side[1][i] = side;
2301 encoder->private_->integer_signal_mid_side[0][i] = mid;
2302 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002303 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002304 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2305 if(i > blocksize) {
2306 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002307 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002308 /* move unprocessed overread samples to beginnings of arrays */
2309 FLAC__ASSERT(i == blocksize+OVERREAD_);
2310 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2311 i--;
2312 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2313 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2314 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2315 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2316 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002317 }
2318 } while(j < samples);
2319 }
2320 else {
2321 /*
2322 * independent channel coding: buffer each channel in inner loop
2323 * without LPC: no need to calculate floating point version of signal
2324 */
2325 do {
2326 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002327 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 +00002328
Josh Coalson49f2f162006-11-09 16:54:52 +00002329 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2330 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002331 for(channel = 0; channel < channels; channel++)
2332 encoder->private_->integer_signal[channel][i] = buffer[k++];
2333 encoder->private_->current_sample_number++;
2334 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002335 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2336 if(i > blocksize) {
2337 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002338 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002339 /* move unprocessed overread samples to beginnings of arrays */
2340 FLAC__ASSERT(i == blocksize+OVERREAD_);
2341 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2342 i--;
2343 for(channel = 0; channel < channels; channel++)
2344 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2345 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002346 }
2347 } while(j < samples);
2348 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002349 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002350
2351 return true;
2352}
2353
Josh Coalsonf1eff452002-07-31 07:05:33 +00002354/***********************************************************************
2355 *
2356 * Private class methods
2357 *
2358 ***********************************************************************/
2359
2360void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00002361{
2362 FLAC__ASSERT(0 != encoder);
2363
Josh Coalson47c7b142005-01-29 06:08:58 +00002364#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2365 encoder->protected_->verify = true;
2366#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00002367 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00002368#endif
Josh Coalson92031602002-07-24 06:02:11 +00002369 encoder->protected_->streamable_subset = true;
2370 encoder->protected_->do_mid_side_stereo = false;
2371 encoder->protected_->loose_mid_side_stereo = false;
2372 encoder->protected_->channels = 2;
2373 encoder->protected_->bits_per_sample = 16;
2374 encoder->protected_->sample_rate = 44100;
Josh Coalson425609c2006-11-03 16:08:52 +00002375 encoder->protected_->blocksize = 0;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002376#ifndef FLAC__INTEGER_ONLY_LIBRARY
2377 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00002378 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2379 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002380#endif
Josh Coalson92031602002-07-24 06:02:11 +00002381 encoder->protected_->max_lpc_order = 0;
2382 encoder->protected_->qlp_coeff_precision = 0;
2383 encoder->protected_->do_qlp_coeff_prec_search = false;
2384 encoder->protected_->do_exhaustive_model_search = false;
2385 encoder->protected_->do_escape_coding = false;
2386 encoder->protected_->min_residual_partition_order = 0;
2387 encoder->protected_->max_residual_partition_order = 0;
2388 encoder->protected_->rice_parameter_search_dist = 0;
2389 encoder->protected_->total_samples_estimate = 0;
2390 encoder->protected_->metadata = 0;
2391 encoder->protected_->num_metadata_blocks = 0;
2392
Josh Coalson6b21f662006-09-13 01:42:27 +00002393 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002394 encoder->private_->disable_constant_subframes = false;
2395 encoder->private_->disable_fixed_subframes = false;
2396 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002397#if FLAC__HAS_OGG
2398 encoder->private_->is_ogg = false;
2399#endif
2400 encoder->private_->read_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002401 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002402 encoder->private_->seek_callback = 0;
2403 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002404 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002405 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002406 encoder->private_->client_data = 0;
Josh Coalson8da98c82006-10-15 04:24:05 +00002407
2408#if FLAC__HAS_OGG
2409 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2410#endif
Josh Coalson92031602002-07-24 06:02:11 +00002411}
2412
Josh Coalsonf1eff452002-07-31 07:05:33 +00002413void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00002414{
2415 unsigned i, channel;
2416
Josh Coalsonf1eff452002-07-31 07:05:33 +00002417 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00002418 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002419 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002420 free(encoder->private_->integer_signal_unaligned[i]);
2421 encoder->private_->integer_signal_unaligned[i] = 0;
2422 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002423#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002424 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002425 free(encoder->private_->real_signal_unaligned[i]);
2426 encoder->private_->real_signal_unaligned[i] = 0;
2427 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002428#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002429 }
2430 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002431 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002432 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2433 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2434 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002435#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002436 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002437 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2438 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2439 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002440#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002441 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002442#ifndef FLAC__INTEGER_ONLY_LIBRARY
2443 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2444 if(0 != encoder->private_->window_unaligned[i]) {
2445 free(encoder->private_->window_unaligned[i]);
2446 encoder->private_->window_unaligned[i] = 0;
2447 }
2448 }
2449 if(0 != encoder->private_->windowed_signal_unaligned) {
2450 free(encoder->private_->windowed_signal_unaligned);
2451 encoder->private_->windowed_signal_unaligned = 0;
2452 }
2453#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002454 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2455 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002456 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002457 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2458 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2459 }
2460 }
2461 }
2462 for(channel = 0; channel < 2; channel++) {
2463 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002464 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002465 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2466 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2467 }
2468 }
2469 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002470 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002471 free(encoder->private_->abs_residual_unaligned);
2472 encoder->private_->abs_residual_unaligned = 0;
2473 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002474 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002475 free(encoder->private_->abs_residual_partition_sums_unaligned);
2476 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2477 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002478 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002479 free(encoder->private_->raw_bits_per_partition_unaligned);
2480 encoder->private_->raw_bits_per_partition_unaligned = 0;
2481 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002482 if(encoder->protected_->verify) {
2483 for(i = 0; i < encoder->protected_->channels; i++) {
2484 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2485 free(encoder->private_->verify.input_fifo.data[i]);
2486 encoder->private_->verify.input_fifo.data[i] = 0;
2487 }
2488 }
2489 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002490 FLAC__bitbuffer_free(encoder->private_->frame);
2491}
2492
Josh Coalson85aaed82006-11-09 01:19:13 +00002493FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002494{
Josh Coalson77e3f312001-06-23 03:03:24 +00002495 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002496 unsigned i, channel;
2497
Josh Coalson85aaed82006-11-09 01:19:13 +00002498 FLAC__ASSERT(new_blocksize > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002499 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2500 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002501
2502 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalson85aaed82006-11-09 01:19:13 +00002503 if(new_blocksize <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002504 return true;
2505
2506 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002507
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002508 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2509 * requires that the input arrays (in our case the integer signals)
2510 * have a buffer of up to 3 zeroes in front (at negative indices) for
Josh Coalson85aaed82006-11-09 01:19:13 +00002511 * alignment purposes; we use 4 in front to keep the data well-aligned.
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002512 */
Josh Coalson8395d022001-07-12 21:25:22 +00002513
Josh Coalsonfa697a92001-08-16 20:07:29 +00002514 for(i = 0; ok && i < encoder->protected_->channels; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002515 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 +00002516 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2517 encoder->private_->integer_signal[i] += 4;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002518#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002519 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002520 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 +00002521#endif
Josh Coalson85aaed82006-11-09 01:19:13 +00002522 }
2523 for(i = 0; ok && i < 2; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002524 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 +00002525 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2526 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson85aaed82006-11-09 01:19:13 +00002527#ifndef FLAC__INTEGER_ONLY_LIBRARY
2528 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002529 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 +00002530#endif
Josh Coalson0a15c142001-06-13 17:59:57 +00002531 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002532#ifndef FLAC__INTEGER_ONLY_LIBRARY
2533 if(ok && encoder->protected_->max_lpc_order > 0) {
2534 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
Josh Coalson85aaed82006-11-09 01:19:13 +00002535 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2536 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 +00002537 }
2538#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002539 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002540 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002541 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 +00002542 }
2543 }
2544 for(channel = 0; ok && channel < 2; channel++) {
2545 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002546 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 +00002547 }
2548 }
Josh Coalson85aaed82006-11-09 01:19:13 +00002549 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 +00002550 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 +00002551 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 +00002552 if(encoder->protected_->do_escape_coding)
Josh Coalson85aaed82006-11-09 01:19:13 +00002553 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 +00002554
Josh Coalson85aaed82006-11-09 01:19:13 +00002555 /* now adjust the windows if the blocksize has changed */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002556#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson85aaed82006-11-09 01:19:13 +00002557 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002558 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2559 switch(encoder->protected_->apodizations[i].type) {
2560 case FLAC__APODIZATION_BARTLETT:
Josh Coalson85aaed82006-11-09 01:19:13 +00002561 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002562 break;
2563 case FLAC__APODIZATION_BARTLETT_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002564 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002565 break;
2566 case FLAC__APODIZATION_BLACKMAN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002567 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002568 break;
2569 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002570 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002571 break;
2572 case FLAC__APODIZATION_CONNES:
Josh Coalson85aaed82006-11-09 01:19:13 +00002573 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002574 break;
2575 case FLAC__APODIZATION_FLATTOP:
Josh Coalson85aaed82006-11-09 01:19:13 +00002576 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002577 break;
2578 case FLAC__APODIZATION_GAUSS:
Josh Coalson85aaed82006-11-09 01:19:13 +00002579 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002580 break;
2581 case FLAC__APODIZATION_HAMMING:
Josh Coalson85aaed82006-11-09 01:19:13 +00002582 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002583 break;
2584 case FLAC__APODIZATION_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002585 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002586 break;
2587 case FLAC__APODIZATION_KAISER_BESSEL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002588 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002589 break;
2590 case FLAC__APODIZATION_NUTTALL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002591 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002592 break;
2593 case FLAC__APODIZATION_RECTANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002594 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002595 break;
2596 case FLAC__APODIZATION_TRIANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002597 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002598 break;
2599 case FLAC__APODIZATION_TUKEY:
Josh Coalson85aaed82006-11-09 01:19:13 +00002600 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002601 break;
2602 case FLAC__APODIZATION_WELCH:
Josh Coalson85aaed82006-11-09 01:19:13 +00002603 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002604 break;
2605 default:
2606 FLAC__ASSERT(0);
2607 /* double protection */
Josh Coalson85aaed82006-11-09 01:19:13 +00002608 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002609 break;
2610 }
2611 }
2612 }
2613#endif
2614
Josh Coalson85aaed82006-11-09 01:19:13 +00002615 if(ok)
2616 encoder->private_->input_capacity = new_blocksize;
2617 else
2618 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2619
Josh Coalson0a15c142001-06-13 17:59:57 +00002620 return ok;
2621}
2622
Josh Coalson49f2f162006-11-09 16:54:52 +00002623FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
Josh Coalson5c491a12002-08-01 06:39:40 +00002624{
2625 const FLAC__byte *buffer;
Josh Coalson352feb52006-10-15 17:08:52 +00002626 size_t bytes;
Josh Coalson5c491a12002-08-01 06:39:40 +00002627
2628 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2629
2630 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2631
Josh Coalsond86e03b2002-08-03 21:56:15 +00002632 if(encoder->protected_->verify) {
2633 encoder->private_->verify.output.data = buffer;
2634 encoder->private_->verify.output.bytes = bytes;
2635 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2636 encoder->private_->verify.needs_magic_hack = true;
2637 }
2638 else {
2639 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2640 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2641 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2642 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2643 return false;
2644 }
2645 }
2646 }
2647
Josh Coalson49f2f162006-11-09 16:54:52 +00002648 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002649 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002650 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002651 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002652 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002653
2654 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2655
Josh Coalsond86e03b2002-08-03 21:56:15 +00002656 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002657 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2658 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 +00002659 }
2660
Josh Coalson5c491a12002-08-01 06:39:40 +00002661 return true;
2662}
2663
Josh Coalson49f2f162006-11-09 16:54:52 +00002664FLAC__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 +00002665{
2666 FLAC__StreamEncoderWriteStatus status;
2667 FLAC__uint64 output_position = 0;
2668
2669 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2670 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2671 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2672 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2673 }
2674
2675 /*
2676 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2677 */
2678 if(samples == 0) {
2679 FLAC__MetadataType type = (buffer[0] & 0x7f);
2680 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2681 encoder->protected_->streaminfo_offset = output_position;
2682 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2683 encoder->protected_->seektable_offset = output_position;
2684 }
2685
2686 /*
2687 * Mark the current seek point if hit (if audio_offset == 0 that
2688 * means we're still writing metadata and haven't hit the first
2689 * frame yet)
2690 */
2691 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2692 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2693 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2694 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2695 FLAC__uint64 test_sample;
2696 unsigned i;
2697 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2698 test_sample = encoder->private_->seek_table->points[i].sample_number;
2699 if(test_sample > frame_last_sample) {
2700 break;
2701 }
2702 else if(test_sample >= frame_first_sample) {
2703 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2704 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2705 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2706 encoder->private_->first_seekpoint_to_check++;
2707 /* DO NOT: "break;" and here's why:
2708 * The seektable template may contain more than one target
2709 * sample for any given frame; we will keep looping, generating
2710 * duplicate seekpoints for them, and we'll clean it up later,
2711 * just before writing the seektable back to the metadata.
2712 */
2713 }
2714 else {
2715 encoder->private_->first_seekpoint_to_check++;
2716 }
2717 }
2718 }
2719
Josh Coalson8da98c82006-10-15 04:24:05 +00002720#if FLAC__HAS_OGG
2721 if(encoder->private_->is_ogg) {
2722 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2723 &encoder->protected_->ogg_encoder_aspect,
Josh Coalson8da98c82006-10-15 04:24:05 +00002724 buffer,
2725 bytes,
2726 samples,
2727 encoder->private_->current_frame_number,
Josh Coalson49f2f162006-11-09 16:54:52 +00002728 is_last_block,
Josh Coalson8da98c82006-10-15 04:24:05 +00002729 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2730 encoder,
2731 encoder->private_->client_data
2732 );
2733 }
2734 else
2735#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00002736 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2737
2738 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2739 encoder->private_->bytes_written += bytes;
2740 encoder->private_->samples_written += samples;
2741 /* we keep a high watermark on the number of frames written because
2742 * when the encoder goes back to write metadata, 'current_frame'
2743 * will drop back to 0.
2744 */
2745 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2746 }
2747 else
2748 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2749
2750 return status;
2751}
2752
2753/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2754void update_metadata_(const FLAC__StreamEncoder *encoder)
2755{
2756 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2757 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2758 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2759 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2760 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2761 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2762 FLAC__StreamEncoderSeekStatus seek_status;
2763
2764 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2765
2766 /* All this is based on intimate knowledge of the stream header
2767 * layout, but a change to the header format that would break this
2768 * would also break all streams encoded in the previous format.
2769 */
2770
2771 /*
2772 * Write MD5 signature
2773 */
2774 {
2775 const unsigned md5_offset =
2776 FLAC__STREAM_METADATA_HEADER_LENGTH +
2777 (
2778 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2779 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2780 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2781 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2782 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2783 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2784 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2785 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2786 ) / 8;
2787
2788 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2789 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2790 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2791 return;
2792 }
2793 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2794 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2795 return;
2796 }
2797 }
2798
2799 /*
2800 * Write total samples
2801 */
2802 {
2803 const unsigned total_samples_byte_offset =
2804 FLAC__STREAM_METADATA_HEADER_LENGTH +
2805 (
2806 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2807 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2808 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2809 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2810 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2811 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2812 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2813 - 4
2814 ) / 8;
2815
2816 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2817 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2818 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2819 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2820 b[4] = (FLAC__byte)(samples & 0xFF);
2821 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) {
2822 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2823 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2824 return;
2825 }
2826 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2827 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2828 return;
2829 }
2830 }
2831
2832 /*
2833 * Write min/max framesize
2834 */
2835 {
2836 const unsigned min_framesize_offset =
2837 FLAC__STREAM_METADATA_HEADER_LENGTH +
2838 (
2839 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2840 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2841 ) / 8;
2842
2843 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2844 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2845 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2846 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2847 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2848 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2849 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) {
2850 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2851 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2852 return;
2853 }
2854 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2855 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2856 return;
2857 }
2858 }
2859
2860 /*
2861 * Write seektable
2862 */
2863 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2864 unsigned i;
2865
2866 FLAC__format_seektable_sort(encoder->private_->seek_table);
2867
2868 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2869
2870 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) {
2871 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2872 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2873 return;
2874 }
2875
2876 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2877 FLAC__uint64 xx;
2878 unsigned x;
2879 xx = encoder->private_->seek_table->points[i].sample_number;
2880 b[7] = (FLAC__byte)xx; xx >>= 8;
2881 b[6] = (FLAC__byte)xx; xx >>= 8;
2882 b[5] = (FLAC__byte)xx; xx >>= 8;
2883 b[4] = (FLAC__byte)xx; xx >>= 8;
2884 b[3] = (FLAC__byte)xx; xx >>= 8;
2885 b[2] = (FLAC__byte)xx; xx >>= 8;
2886 b[1] = (FLAC__byte)xx; xx >>= 8;
2887 b[0] = (FLAC__byte)xx; xx >>= 8;
2888 xx = encoder->private_->seek_table->points[i].stream_offset;
2889 b[15] = (FLAC__byte)xx; xx >>= 8;
2890 b[14] = (FLAC__byte)xx; xx >>= 8;
2891 b[13] = (FLAC__byte)xx; xx >>= 8;
2892 b[12] = (FLAC__byte)xx; xx >>= 8;
2893 b[11] = (FLAC__byte)xx; xx >>= 8;
2894 b[10] = (FLAC__byte)xx; xx >>= 8;
2895 b[9] = (FLAC__byte)xx; xx >>= 8;
2896 b[8] = (FLAC__byte)xx; xx >>= 8;
2897 x = encoder->private_->seek_table->points[i].frame_samples;
2898 b[17] = (FLAC__byte)x; x >>= 8;
2899 b[16] = (FLAC__byte)x; x >>= 8;
2900 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2901 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2902 return;
2903 }
2904 }
2905 }
2906}
2907
Josh Coalson15b8eb82006-10-15 05:15:55 +00002908#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00002909/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2910void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2911{
Josh Coalsonc986d132006-11-15 08:53:32 +00002912 /* the # of bytes in the 1st packet that precede the STREAMINFO */
2913 static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2914 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2915 FLAC__OGG_MAPPING_MAGIC_LENGTH +
2916 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2917 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2918 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2919 FLAC__STREAM_SYNC_LENGTH
2920 ;
Josh Coalson8da98c82006-10-15 04:24:05 +00002921 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2922 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2923 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2924 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2925 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2926 ogg_page page;
2927
2928 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
Josh Coalsoncf1422e2006-11-16 01:32:25 +00002929 FLAC__ASSERT(0 != encoder->private_->seek_callback);
2930
2931 /* Pre-check that client supports seeking, since we don't want the
2932 * ogg_helper code to ever have to deal with this condition.
2933 */
2934 if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2935 return;
Josh Coalson8da98c82006-10-15 04:24:05 +00002936
2937 /* All this is based on intimate knowledge of the stream header
2938 * layout, but a change to the header format that would break this
2939 * would also break all streams encoded in the previous format.
2940 */
2941
2942 /**
2943 ** Write STREAMINFO stats
2944 **/
2945 simple_ogg_page__init(&page);
2946 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2947 simple_ogg_page__clear(&page);
2948 return; /* state already set */
2949 }
2950
2951 /*
2952 * Write MD5 signature
2953 */
2954 {
2955 const unsigned md5_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002956 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002957 FLAC__STREAM_METADATA_HEADER_LENGTH +
2958 (
2959 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2960 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2961 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2962 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2963 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2964 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2965 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2966 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2967 ) / 8;
2968
2969 if(md5_offset + 16 > (unsigned)page.body_len) {
2970 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2971 simple_ogg_page__clear(&page);
2972 return;
2973 }
2974 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2975 }
2976
2977 /*
2978 * Write total samples
2979 */
2980 {
2981 const unsigned total_samples_byte_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002982 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002983 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 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2988 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2989 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2990 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2991 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2992 - 4
2993 ) / 8;
2994
2995 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2996 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2997 simple_ogg_page__clear(&page);
2998 return;
2999 }
3000 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
3001 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
3002 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
3003 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
3004 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
3005 b[4] = (FLAC__byte)(samples & 0xFF);
3006 memcpy(page.body + total_samples_byte_offset, b, 5);
3007 }
3008
3009 /*
3010 * Write min/max framesize
3011 */
3012 {
3013 const unsigned min_framesize_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00003014 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00003015 FLAC__STREAM_METADATA_HEADER_LENGTH +
3016 (
3017 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
3018 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
3019 ) / 8;
3020
3021 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
3022 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3023 simple_ogg_page__clear(&page);
3024 return;
3025 }
3026 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
3027 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
3028 b[2] = (FLAC__byte)(min_framesize & 0xFF);
3029 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
3030 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
3031 b[5] = (FLAC__byte)(max_framesize & 0xFF);
3032 memcpy(page.body + min_framesize_offset, b, 6);
3033 }
3034 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3035 simple_ogg_page__clear(&page);
3036 return; /* state already set */
3037 }
3038 simple_ogg_page__clear(&page);
3039
3040 /*
3041 * Write seektable
3042 */
3043 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
3044 unsigned i;
3045 FLAC__byte *p;
3046
3047 FLAC__format_seektable_sort(encoder->private_->seek_table);
3048
3049 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3050
3051 simple_ogg_page__init(&page);
3052 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3053 simple_ogg_page__clear(&page);
3054 return; /* state already set */
3055 }
3056
Josh Coalsonc986d132006-11-15 08:53:32 +00003057 if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
Josh Coalson8da98c82006-10-15 04:24:05 +00003058 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3059 simple_ogg_page__clear(&page);
3060 return;
3061 }
3062
3063 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3064 FLAC__uint64 xx;
3065 unsigned x;
3066 xx = encoder->private_->seek_table->points[i].sample_number;
3067 b[7] = (FLAC__byte)xx; xx >>= 8;
3068 b[6] = (FLAC__byte)xx; xx >>= 8;
3069 b[5] = (FLAC__byte)xx; xx >>= 8;
3070 b[4] = (FLAC__byte)xx; xx >>= 8;
3071 b[3] = (FLAC__byte)xx; xx >>= 8;
3072 b[2] = (FLAC__byte)xx; xx >>= 8;
3073 b[1] = (FLAC__byte)xx; xx >>= 8;
3074 b[0] = (FLAC__byte)xx; xx >>= 8;
3075 xx = encoder->private_->seek_table->points[i].stream_offset;
3076 b[15] = (FLAC__byte)xx; xx >>= 8;
3077 b[14] = (FLAC__byte)xx; xx >>= 8;
3078 b[13] = (FLAC__byte)xx; xx >>= 8;
3079 b[12] = (FLAC__byte)xx; xx >>= 8;
3080 b[11] = (FLAC__byte)xx; xx >>= 8;
3081 b[10] = (FLAC__byte)xx; xx >>= 8;
3082 b[9] = (FLAC__byte)xx; xx >>= 8;
3083 b[8] = (FLAC__byte)xx; xx >>= 8;
3084 x = encoder->private_->seek_table->points[i].frame_samples;
3085 b[17] = (FLAC__byte)x; x >>= 8;
3086 b[16] = (FLAC__byte)x; x >>= 8;
Josh Coalson8da98c82006-10-15 04:24:05 +00003087 memcpy(p, b, 18);
3088 }
3089
3090 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3091 simple_ogg_page__clear(&page);
3092 return; /* state already set */
3093 }
3094 simple_ogg_page__clear(&page);
3095 }
3096}
Josh Coalson15b8eb82006-10-15 05:15:55 +00003097#endif
Josh Coalson8da98c82006-10-15 04:24:05 +00003098
Josh Coalson49f2f162006-11-09 16:54:52 +00003099FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
Josh Coalson0a15c142001-06-13 17:59:57 +00003100{
Josh Coalsonfa697a92001-08-16 20:07:29 +00003101 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003102
3103 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003104 * Accumulate raw signal to the MD5 signature
3105 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00003106 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 +00003107 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003108 return false;
3109 }
3110
3111 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003112 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003113 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003114 if(!process_subframes_(encoder, is_fractional_block)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003115 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003116 return false;
3117 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003118
3119 /*
3120 * Zero-pad the frame to a byte_boundary
3121 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003122 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003123 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003124 return false;
3125 }
3126
3127 /*
Josh Coalson215af572001-03-27 01:15:58 +00003128 * CRC-16 the whole thing
3129 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003130 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
3131 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 +00003132
3133 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003134 * Write it
3135 */
Josh Coalson49f2f162006-11-09 16:54:52 +00003136 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00003137 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003138 return false;
3139 }
3140
3141 /*
3142 * Get ready for the next frame
3143 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003144 encoder->private_->current_sample_number = 0;
3145 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00003146 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003147
3148 return true;
3149}
3150
Josh Coalson85aaed82006-11-09 01:19:13 +00003151FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003152{
3153 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003154 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003155 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003156
3157 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00003158 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00003159 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003160 if(is_fractional_block) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003161 max_partition_order = 0;
3162 }
3163 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003164 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
3165 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003166 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003167 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003168
Josh Coalsonfa697a92001-08-16 20:07:29 +00003169 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 +00003170
Josh Coalson94e02cd2001-01-25 10:41:06 +00003171 /*
3172 * Setup the frame
3173 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003174 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003175 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003176 return false;
3177 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00003178 frame_header.blocksize = encoder->protected_->blocksize;
3179 frame_header.sample_rate = encoder->protected_->sample_rate;
3180 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003181 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003182 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003183 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003184 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003185
3186 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003187 * Figure out what channel assignments to try
3188 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003189 if(encoder->protected_->do_mid_side_stereo) {
3190 if(encoder->protected_->loose_mid_side_stereo) {
3191 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003192 do_independent = true;
3193 do_mid_side = true;
3194 }
3195 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003196 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003197 do_mid_side = !do_independent;
3198 }
3199 }
3200 else {
3201 do_independent = true;
3202 do_mid_side = true;
3203 }
3204 }
3205 else {
3206 do_independent = true;
3207 do_mid_side = false;
3208 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003209
Josh Coalson1b689822001-05-31 20:11:02 +00003210 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003211
3212 /*
Josh Coalson82b73242001-03-28 22:17:05 +00003213 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00003214 */
3215 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003216 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003217 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003218 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3219 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00003220 }
Josh Coalson859bc542001-03-27 22:22:27 +00003221 }
3222 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003223 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00003224 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003225 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003226 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3227 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00003228 }
Josh Coalson859bc542001-03-27 22:22:27 +00003229 }
3230
3231 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003232 * First do a normal encoding pass of each independent channel
3233 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003234 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003235 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003236 if(!
3237 process_subframe_(
3238 encoder,
3239 min_partition_order,
3240 max_partition_order,
3241 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003242 &frame_header,
3243 encoder->private_->subframe_bps[channel],
3244 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003245#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003246 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003247#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003248 encoder->private_->subframe_workspace_ptr[channel],
3249 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3250 encoder->private_->residual_workspace[channel],
3251 encoder->private_->best_subframe+channel,
3252 encoder->private_->best_subframe_bits+channel
3253 )
3254 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003255 return false;
3256 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003257 }
3258
3259 /*
3260 * Now do mid and side channels if requested
3261 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003262 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003263 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003264
3265 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003266 if(!
3267 process_subframe_(
3268 encoder,
3269 min_partition_order,
3270 max_partition_order,
3271 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003272 &frame_header,
3273 encoder->private_->subframe_bps_mid_side[channel],
3274 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003275#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003276 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003277#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003278 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3279 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3280 encoder->private_->residual_workspace_mid_side[channel],
3281 encoder->private_->best_subframe_mid_side+channel,
3282 encoder->private_->best_subframe_bits_mid_side+channel
3283 )
3284 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00003285 return false;
3286 }
3287 }
3288
3289 /*
3290 * Compose the frame bitbuffer
3291 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003292 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00003293 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3294 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003295 FLAC__ChannelAssignment channel_assignment;
3296
Josh Coalsonfa697a92001-08-16 20:07:29 +00003297 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003298
Josh Coalsonfa697a92001-08-16 20:07:29 +00003299 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3300 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 +00003301 }
3302 else {
3303 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3304 unsigned min_bits;
3305 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003306
Josh Coalson1b689822001-05-31 20:11:02 +00003307 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003308
3309 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003310 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3311 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3312 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3313 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 +00003314
Josh Coalson7424d2f2002-11-06 07:10:38 +00003315 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 +00003316 if(bits[ca] < min_bits) {
3317 min_bits = bits[ca];
3318 channel_assignment = ca;
3319 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003320 }
3321 }
3322
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003323 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003324
Josh Coalsond0edb972006-10-07 06:50:08 +00003325 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003326 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003327 return false;
3328 }
3329
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003330 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003331 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003332 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3333 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003334 break;
3335 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003336 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3337 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003338 break;
3339 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003340 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3341 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003342 break;
3343 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003344 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3345 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003346 break;
3347 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003348 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003349 }
Josh Coalson82b73242001-03-28 22:17:05 +00003350
3351 switch(channel_assignment) {
3352 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003353 left_bps = encoder->private_->subframe_bps [0];
3354 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003355 break;
3356 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003357 left_bps = encoder->private_->subframe_bps [0];
3358 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003359 break;
3360 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003361 left_bps = encoder->private_->subframe_bps_mid_side[1];
3362 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003363 break;
3364 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003365 left_bps = encoder->private_->subframe_bps_mid_side[0];
3366 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003367 break;
3368 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003369 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00003370 }
3371
3372 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00003373 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003374 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00003375 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003376 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003377 }
3378 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00003379 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003380 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003381 return false;
3382 }
3383
Josh Coalsonfa697a92001-08-16 20:07:29 +00003384 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson369a6da2006-10-10 00:37:48 +00003385 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 +00003386 /* the above function sets the state for us in case of an error */
3387 return false;
3388 }
3389 }
3390 }
3391
Josh Coalsonfa697a92001-08-16 20:07:29 +00003392 if(encoder->protected_->loose_mid_side_stereo) {
3393 encoder->private_->loose_mid_side_stereo_frame_count++;
3394 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3395 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003396 }
3397
Josh Coalsonfa697a92001-08-16 20:07:29 +00003398 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003399
Josh Coalson94e02cd2001-01-25 10:41:06 +00003400 return true;
3401}
3402
Josh Coalson6fe72f72002-08-20 04:01:59 +00003403FLAC__bool process_subframe_(
3404 FLAC__StreamEncoder *encoder,
3405 unsigned min_partition_order,
3406 unsigned max_partition_order,
3407 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003408 const FLAC__FrameHeader *frame_header,
3409 unsigned subframe_bps,
3410 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003411#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003412 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003413#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003414 FLAC__Subframe *subframe[2],
3415 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3416 FLAC__int32 *residual[2],
3417 unsigned *best_subframe,
3418 unsigned *best_bits
3419)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003420{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003421#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003422 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003423#else
3424 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3425#endif
3426#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003427 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003428 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 +00003429 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003430 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003431 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003432#endif
3433 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003434 unsigned rice_parameter;
3435 unsigned _candidate_bits, _best_bits;
3436 unsigned _best_subframe;
3437
3438 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3439 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003440 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3441 _best_bits = UINT_MAX;
3442 else
3443 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003444
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003445 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3446 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003447 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 +00003448 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003449 if(
3450 !encoder->private_->disable_constant_subframes &&
3451#ifndef FLAC__INTEGER_ONLY_LIBRARY
3452 fixed_residual_bits_per_sample[1] == 0.0
3453#else
3454 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3455#endif
3456 ) {
3457 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003458 unsigned i;
3459 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003460 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003461 if(integer_signal[0] != integer_signal[i]) {
3462 signal_is_constant = false;
3463 break;
3464 }
3465 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003466 }
3467 if(signal_is_constant) {
3468 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
3469 if(_candidate_bits < _best_bits) {
3470 _best_subframe = !_best_subframe;
3471 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003472 }
3473 }
3474 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003475 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3476 /* encode fixed */
3477 if(encoder->protected_->do_exhaustive_model_search) {
3478 min_fixed_order = 0;
3479 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00003480 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003481 else {
3482 min_fixed_order = max_fixed_order = guess_fixed_order;
3483 }
3484 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003485#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003486 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003487 continue; /* don't even try */
3488 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 +00003489#else
3490 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3491 continue; /* don't even try */
3492 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 */
3493#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003494 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003495 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3496#ifdef DEBUG_VERBOSE
3497 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3498#endif
3499 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3500 }
3501 _candidate_bits =
3502 evaluate_fixed_subframe_(
3503 encoder,
3504 integer_signal,
3505 residual[!_best_subframe],
3506 encoder->private_->abs_residual,
3507 encoder->private_->abs_residual_partition_sums,
3508 encoder->private_->raw_bits_per_partition,
3509 frame_header->blocksize,
3510 subframe_bps,
3511 fixed_order,
3512 rice_parameter,
3513 min_partition_order,
3514 max_partition_order,
3515 precompute_partition_sums,
3516 encoder->protected_->do_escape_coding,
3517 encoder->protected_->rice_parameter_search_dist,
3518 subframe[!_best_subframe],
3519 partitioned_rice_contents[!_best_subframe]
3520 );
3521 if(_candidate_bits < _best_bits) {
3522 _best_subframe = !_best_subframe;
3523 _best_bits = _candidate_bits;
3524 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003525 }
3526 }
3527
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003528#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00003529 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003530 if(encoder->protected_->max_lpc_order > 0) {
3531 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003532 max_lpc_order = frame_header->blocksize-1;
3533 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00003534 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003535 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003536 unsigned a;
3537 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00003538 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003539 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3540 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3541 if(autoc[0] != 0.0) {
Josh Coalson32b9bae2006-11-27 16:27:41 +00003542 FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003543 if(encoder->protected_->do_exhaustive_model_search) {
3544 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003545 }
3546 else {
Josh Coalsondf598452006-04-28 00:13:34 +00003547 const unsigned guess_lpc_order =
3548 FLAC__lpc_compute_best_order(
3549 lpc_error,
3550 max_lpc_order,
3551 frame_header->blocksize,
3552 subframe_bps + (
3553 encoder->protected_->do_qlp_coeff_prec_search?
3554 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3555 encoder->protected_->qlp_coeff_precision
3556 )
3557 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003558 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003559 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003560 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3561 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3562 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3563 continue; /* don't even try */
3564 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3565 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3566 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00003567#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003568 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 +00003569#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003570 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3571 }
3572 if(encoder->protected_->do_qlp_coeff_prec_search) {
3573 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00003574 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3575 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003576 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00003577 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3578 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003579 else
3580 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3581 }
3582 else {
3583 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3584 }
3585 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3586 _candidate_bits =
3587 evaluate_lpc_subframe_(
3588 encoder,
3589 integer_signal,
3590 residual[!_best_subframe],
3591 encoder->private_->abs_residual,
3592 encoder->private_->abs_residual_partition_sums,
3593 encoder->private_->raw_bits_per_partition,
3594 encoder->private_->lp_coeff[lpc_order-1],
3595 frame_header->blocksize,
3596 subframe_bps,
3597 lpc_order,
3598 qlp_coeff_precision,
3599 rice_parameter,
3600 min_partition_order,
3601 max_partition_order,
3602 precompute_partition_sums,
3603 encoder->protected_->do_escape_coding,
3604 encoder->protected_->rice_parameter_search_dist,
3605 subframe[!_best_subframe],
3606 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003607 );
3608 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3609 if(_candidate_bits < _best_bits) {
3610 _best_subframe = !_best_subframe;
3611 _best_bits = _candidate_bits;
3612 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00003613 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003614 }
3615 }
3616 }
3617 }
3618 }
3619 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003620#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00003621 }
3622 }
3623
Josh Coalson72695802002-10-11 06:25:16 +00003624 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3625 if(_best_bits == UINT_MAX) {
3626 FLAC__ASSERT(_best_subframe == 0);
3627 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3628 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003629
Josh Coalson94e02cd2001-01-25 10:41:06 +00003630 *best_subframe = _best_subframe;
3631 *best_bits = _best_bits;
3632
3633 return true;
3634}
3635
Josh Coalson6fe72f72002-08-20 04:01:59 +00003636FLAC__bool add_subframe_(
3637 FLAC__StreamEncoder *encoder,
3638 const FLAC__FrameHeader *frame_header,
3639 unsigned subframe_bps,
3640 const FLAC__Subframe *subframe,
3641 FLAC__BitBuffer *frame
3642)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003643{
3644 switch(subframe->type) {
3645 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003646 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003647 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003648 return false;
3649 }
3650 break;
3651 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00003652 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 +00003653 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003654 return false;
3655 }
3656 break;
3657 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00003658 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 +00003659 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003660 return false;
3661 }
3662 break;
3663 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00003664 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003665 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003666 return false;
3667 }
3668 break;
3669 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003670 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003671 }
3672
3673 return true;
3674}
3675
Josh Coalson6fe72f72002-08-20 04:01:59 +00003676unsigned evaluate_constant_subframe_(
3677 const FLAC__int32 signal,
3678 unsigned subframe_bps,
3679 FLAC__Subframe *subframe
3680)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003681{
3682 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3683 subframe->data.constant.value = signal;
3684
Josh Coalson82b73242001-03-28 22:17:05 +00003685 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 +00003686}
3687
Josh Coalson6fe72f72002-08-20 04:01:59 +00003688unsigned evaluate_fixed_subframe_(
3689 FLAC__StreamEncoder *encoder,
3690 const FLAC__int32 signal[],
3691 FLAC__int32 residual[],
3692 FLAC__uint32 abs_residual[],
3693 FLAC__uint64 abs_residual_partition_sums[],
3694 unsigned raw_bits_per_partition[],
3695 unsigned blocksize,
3696 unsigned subframe_bps,
3697 unsigned order,
3698 unsigned rice_parameter,
3699 unsigned min_partition_order,
3700 unsigned max_partition_order,
3701 FLAC__bool precompute_partition_sums,
3702 FLAC__bool do_escape_coding,
3703 unsigned rice_parameter_search_dist,
3704 FLAC__Subframe *subframe,
3705 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3706)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003707{
3708 unsigned i, residual_bits;
3709 const unsigned residual_samples = blocksize - order;
3710
3711 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3712
3713 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3714
3715 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003716 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003717 subframe->data.fixed.residual = residual;
3718
Josh Coalson6fe72f72002-08-20 04:01:59 +00003719 residual_bits =
3720 find_best_partition_order_(
3721 encoder->private_,
3722 residual,
3723 abs_residual,
3724 abs_residual_partition_sums,
3725 raw_bits_per_partition,
3726 residual_samples,
3727 order,
3728 rice_parameter,
3729 min_partition_order,
3730 max_partition_order,
3731 precompute_partition_sums,
3732 do_escape_coding,
3733 rice_parameter_search_dist,
3734 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3735 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003736
3737 subframe->data.fixed.order = order;
3738 for(i = 0; i < order; i++)
3739 subframe->data.fixed.warmup[i] = signal[i];
3740
Josh Coalson82b73242001-03-28 22:17:05 +00003741 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 +00003742}
3743
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003744#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003745unsigned evaluate_lpc_subframe_(
3746 FLAC__StreamEncoder *encoder,
3747 const FLAC__int32 signal[],
3748 FLAC__int32 residual[],
3749 FLAC__uint32 abs_residual[],
3750 FLAC__uint64 abs_residual_partition_sums[],
3751 unsigned raw_bits_per_partition[],
3752 const FLAC__real lp_coeff[],
3753 unsigned blocksize,
3754 unsigned subframe_bps,
3755 unsigned order,
3756 unsigned qlp_coeff_precision,
3757 unsigned rice_parameter,
3758 unsigned min_partition_order,
3759 unsigned max_partition_order,
3760 FLAC__bool precompute_partition_sums,
3761 FLAC__bool do_escape_coding,
3762 unsigned rice_parameter_search_dist,
3763 FLAC__Subframe *subframe,
3764 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3765)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003766{
Josh Coalson77e3f312001-06-23 03:03:24 +00003767 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003768 unsigned i, residual_bits;
3769 int quantization, ret;
3770 const unsigned residual_samples = blocksize - order;
3771
Josh Coalson20ac2c12002-08-30 05:47:14 +00003772 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3773 if(subframe_bps <= 16) {
3774 FLAC__ASSERT(order > 0);
3775 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3776 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3777 }
3778
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003779 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003780 if(ret != 0)
3781 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3782
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003783 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3784 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3785 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3786 else
3787 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 +00003788 else
3789 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 +00003790
3791 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3792
3793 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003794 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003795 subframe->data.lpc.residual = residual;
3796
Josh Coalson6fe72f72002-08-20 04:01:59 +00003797 residual_bits =
3798 find_best_partition_order_(
3799 encoder->private_,
3800 residual,
3801 abs_residual,
3802 abs_residual_partition_sums,
3803 raw_bits_per_partition,
3804 residual_samples,
3805 order,
3806 rice_parameter,
3807 min_partition_order,
3808 max_partition_order,
3809 precompute_partition_sums,
3810 do_escape_coding,
3811 rice_parameter_search_dist,
3812 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3813 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003814
3815 subframe->data.lpc.order = order;
3816 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3817 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003818 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003819 for(i = 0; i < order; i++)
3820 subframe->data.lpc.warmup[i] = signal[i];
3821
Josh Coalson82b73242001-03-28 22:17:05 +00003822 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 +00003823}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003824#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003825
Josh Coalson6fe72f72002-08-20 04:01:59 +00003826unsigned evaluate_verbatim_subframe_(
3827 const FLAC__int32 signal[],
3828 unsigned blocksize,
3829 unsigned subframe_bps,
3830 FLAC__Subframe *subframe
3831)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003832{
3833 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3834
3835 subframe->data.verbatim.data = signal;
3836
Josh Coalson82b73242001-03-28 22:17:05 +00003837 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 +00003838}
3839
Josh Coalson6fe72f72002-08-20 04:01:59 +00003840unsigned find_best_partition_order_(
3841 FLAC__StreamEncoderPrivate *private_,
3842 const FLAC__int32 residual[],
3843 FLAC__uint32 abs_residual[],
3844 FLAC__uint64 abs_residual_partition_sums[],
3845 unsigned raw_bits_per_partition[],
3846 unsigned residual_samples,
3847 unsigned predictor_order,
3848 unsigned rice_parameter,
3849 unsigned min_partition_order,
3850 unsigned max_partition_order,
3851 FLAC__bool precompute_partition_sums,
3852 FLAC__bool do_escape_coding,
3853 unsigned rice_parameter_search_dist,
3854 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3855)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003856{
Josh Coalson77e3f312001-06-23 03:03:24 +00003857 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003858 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00003859 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00003860 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003861 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003862
Josh Coalson2051dd42001-04-12 22:22:34 +00003863 /* compute abs(residual) for use later */
3864 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3865 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00003866 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00003867 }
3868
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003869 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 +00003870 min_partition_order = min(min_partition_order, max_partition_order);
3871
Josh Coalson8395d022001-07-12 21:25:22 +00003872 if(precompute_partition_sums) {
3873 int partition_order;
3874 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003875
Josh Coalsonf1eff452002-07-31 07:05:33 +00003876 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 +00003877
3878 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00003879 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 +00003880
3881 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3882#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003883 if(!
3884 set_partitioned_rice_with_precompute_(
3885 residual,
3886 abs_residual_partition_sums+sum,
3887 raw_bits_per_partition+sum,
3888 residual_samples,
3889 predictor_order,
3890 rice_parameter,
3891 rice_parameter_search_dist,
3892 (unsigned)partition_order,
3893 do_escape_coding,
3894 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3895 &residual_bits
3896 )
3897 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003898#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003899 if(!
3900 set_partitioned_rice_with_precompute_(
3901 abs_residual,
3902 abs_residual_partition_sums+sum,
3903 raw_bits_per_partition+sum,
3904 residual_samples,
3905 predictor_order,
3906 rice_parameter,
3907 rice_parameter_search_dist,
3908 (unsigned)partition_order,
3909 do_escape_coding,
3910 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3911 &residual_bits
3912 )
3913 )
Josh Coalson8395d022001-07-12 21:25:22 +00003914#endif
3915 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003916 FLAC__ASSERT(best_residual_bits != 0);
3917 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003918 }
3919 sum += 1u << partition_order;
3920 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3921 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003922 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003923 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003924 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003925 }
3926 }
Josh Coalson8395d022001-07-12 21:25:22 +00003927 else {
3928 unsigned partition_order;
3929 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3930#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003931 if(!
3932 set_partitioned_rice_(
3933 abs_residual,
3934 residual,
3935 residual_samples,
3936 predictor_order,
3937 rice_parameter,
3938 rice_parameter_search_dist,
3939 partition_order,
3940 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3941 &residual_bits
3942 )
3943 )
Josh Coalson8395d022001-07-12 21:25:22 +00003944#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003945 if(!
3946 set_partitioned_rice_(
3947 abs_residual,
3948 residual_samples,
3949 predictor_order,
3950 rice_parameter,
3951 rice_parameter_search_dist,
3952 partition_order,
3953 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3954 &residual_bits
3955 )
3956 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003957#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003958 {
3959 FLAC__ASSERT(best_residual_bits != 0);
3960 break;
3961 }
3962 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3963 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003964 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003965 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003966 }
3967 }
3968 }
3969
Josh Coalsona37ba462002-08-19 21:36:39 +00003970 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003971 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003972 * it is const to the outside world.
3973 */
3974 {
3975 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3976 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3977 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3978 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3979 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003980
3981 return best_residual_bits;
3982}
3983
Josh Coalson6fe72f72002-08-20 04:01:59 +00003984void precompute_partition_info_sums_(
3985 const FLAC__uint32 abs_residual[],
3986 FLAC__uint64 abs_residual_partition_sums[],
3987 unsigned residual_samples,
3988 unsigned predictor_order,
3989 unsigned min_partition_order,
3990 unsigned max_partition_order
3991)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003992{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003993 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003994 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003995 const unsigned blocksize = residual_samples + predictor_order;
3996
Josh Coalsonaef013c2001-04-24 01:25:42 +00003997 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003998 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003999 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00004000 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00004001 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004002 const unsigned partitions = 1u << partition_order;
4003 const unsigned default_partition_samples = blocksize >> partition_order;
4004
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004005 FLAC__ASSERT(default_partition_samples > predictor_order);
4006
4007 for(partition = residual_sample = 0; partition < partitions; partition++) {
4008 partition_samples = default_partition_samples;
4009 if(partition == 0)
4010 partition_samples -= predictor_order;
4011 abs_residual_partition_sum = 0;
4012 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4013 abs_r = abs_residual[residual_sample];
4014 abs_residual_partition_sum += abs_r;
4015 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004016 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004017 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004018 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004019 to_partition = partitions;
4020 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004021 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00004022
Josh Coalson8395d022001-07-12 21:25:22 +00004023 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00004024 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004025 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00004026 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004027 const unsigned partitions = 1u << partition_order;
4028 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00004029 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00004030 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00004031 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00004032 from_partition++;
4033 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004034 }
4035 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00004036}
Josh Coalson8395d022001-07-12 21:25:22 +00004037
Josh Coalson6fe72f72002-08-20 04:01:59 +00004038void precompute_partition_info_escapes_(
4039 const FLAC__int32 residual[],
4040 unsigned raw_bits_per_partition[],
4041 unsigned residual_samples,
4042 unsigned predictor_order,
4043 unsigned min_partition_order,
4044 unsigned max_partition_order
4045)
Josh Coalson8395d022001-07-12 21:25:22 +00004046{
4047 int partition_order;
4048 unsigned from_partition, to_partition = 0;
4049 const unsigned blocksize = residual_samples + predictor_order;
4050
4051 /* first do max_partition_order */
4052 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
4053 FLAC__int32 r, residual_partition_min, residual_partition_max;
4054 unsigned silog2_min, silog2_max;
4055 unsigned partition, partition_sample, partition_samples, residual_sample;
4056 const unsigned partitions = 1u << partition_order;
4057 const unsigned default_partition_samples = blocksize >> partition_order;
4058
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004059 FLAC__ASSERT(default_partition_samples > predictor_order);
4060
4061 for(partition = residual_sample = 0; partition < partitions; partition++) {
4062 partition_samples = default_partition_samples;
4063 if(partition == 0)
4064 partition_samples -= predictor_order;
4065 residual_partition_min = residual_partition_max = 0;
4066 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4067 r = residual[residual_sample];
4068 if(r < residual_partition_min)
4069 residual_partition_min = r;
4070 else if(r > residual_partition_max)
4071 residual_partition_max = r;
4072 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00004073 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004074 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
4075 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
4076 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00004077 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004078 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00004079 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00004080 }
4081
4082 /* now merge partitions for lower orders */
4083 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4084 unsigned m;
4085 unsigned i;
4086 const unsigned partitions = 1u << partition_order;
4087 for(i = 0; i < partitions; i++) {
4088 m = raw_bits_per_partition[from_partition];
4089 from_partition++;
4090 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
4091 from_partition++;
4092 to_partition++;
4093 }
4094 }
4095}
Josh Coalson94e02cd2001-01-25 10:41:06 +00004096
Josh Coalson352e0f62001-03-20 22:55:50 +00004097#ifdef VARIABLE_RICE_BITS
4098#undef VARIABLE_RICE_BITS
4099#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004100#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004101#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00004102#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004103
Josh Coalson8395d022001-07-12 21:25:22 +00004104#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004105FLAC__bool set_partitioned_rice_(
4106 const FLAC__uint32 abs_residual[],
4107 const FLAC__int32 residual[],
4108 const unsigned residual_samples,
4109 const unsigned predictor_order,
4110 const unsigned suggested_rice_parameter,
4111 const unsigned rice_parameter_search_dist,
4112 const unsigned partition_order,
4113 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4114 unsigned *bits
4115)
Josh Coalson8395d022001-07-12 21:25:22 +00004116#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004117FLAC__bool set_partitioned_rice_(
4118 const FLAC__uint32 abs_residual[],
4119 const unsigned residual_samples,
4120 const unsigned predictor_order,
4121 const unsigned suggested_rice_parameter,
4122 const unsigned rice_parameter_search_dist,
4123 const unsigned partition_order,
4124 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4125 unsigned *bits
4126)
Josh Coalson8395d022001-07-12 21:25:22 +00004127#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00004128{
Josh Coalson034dfab2001-04-27 19:10:23 +00004129 unsigned rice_parameter, partition_bits;
4130#ifndef NO_RICE_SEARCH
4131 unsigned best_partition_bits;
4132 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4133#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00004134 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004135 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004136
Josh Coalson1b689822001-05-31 20:11:02 +00004137 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00004138
Josh Coalsona37ba462002-08-19 21:36:39 +00004139 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4140 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004141
Josh Coalson94e02cd2001-01-25 10:41:06 +00004142 if(partition_order == 0) {
4143 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00004144
Josh Coalson034dfab2001-04-27 19:10:23 +00004145#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004146 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004147 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004148 min_rice_parameter = 0;
4149 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004150 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4151 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004152 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004153#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004154 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4155#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004156 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004157 }
4158 }
4159 else
4160 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4161
4162 best_partition_bits = 0xffffffff;
4163 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4164#endif
4165#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004166 const unsigned rice_parameter_estimate = rice_parameter-1;
4167 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004168#else
4169 partition_bits = 0;
4170#endif
4171 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4172 for(i = 0; i < residual_samples; i++) {
4173#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004174 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004175#else
4176 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4177#endif
4178 }
4179#ifndef NO_RICE_SEARCH
4180 if(partition_bits < best_partition_bits) {
4181 best_rice_parameter = rice_parameter;
4182 best_partition_bits = partition_bits;
4183 }
4184 }
4185#endif
4186 parameters[0] = best_rice_parameter;
4187 bits_ += best_partition_bits;
4188 }
4189 else {
4190 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004191 unsigned partition_samples;
4192 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00004193 const unsigned partitions = 1u << partition_order;
4194 for(partition = residual_sample = 0; partition < partitions; partition++) {
4195 partition_samples = (residual_samples+predictor_order) >> partition_order;
4196 if(partition == 0) {
4197 if(partition_samples <= predictor_order)
4198 return false;
4199 else
4200 partition_samples -= predictor_order;
4201 }
4202 mean = 0;
4203 save_residual_sample = residual_sample;
4204 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004205 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00004206 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004207 /* we are basically calculating the size in bits of the
4208 * average residual magnitude in the partition:
4209 * rice_parameter = floor(log2(mean/partition_samples))
4210 * 'mean' is not a good name for the variable, it is
4211 * actually the sum of magnitudes of all residual values
4212 * in the partition, so the actual mean is
4213 * mean/partition_samples
4214 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004215 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00004216 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004217 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004218#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004219 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4220#endif
4221 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4222 }
4223
4224#ifndef NO_RICE_SEARCH
4225 if(rice_parameter_search_dist) {
4226 if(rice_parameter < rice_parameter_search_dist)
4227 min_rice_parameter = 0;
4228 else
4229 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4230 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4231 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004232#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004233 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4234#endif
4235 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4236 }
4237 }
4238 else
4239 min_rice_parameter = max_rice_parameter = rice_parameter;
4240
4241 best_partition_bits = 0xffffffff;
4242 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4243#endif
4244#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004245 const unsigned rice_parameter_estimate = rice_parameter-1;
4246 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004247#else
4248 partition_bits = 0;
4249#endif
4250 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4251 save_residual_sample = residual_sample;
4252 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4253#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004254 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004255#else
4256 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4257#endif
4258 }
4259#ifndef NO_RICE_SEARCH
4260 if(rice_parameter != max_rice_parameter)
4261 residual_sample = save_residual_sample;
4262 if(partition_bits < best_partition_bits) {
4263 best_rice_parameter = rice_parameter;
4264 best_partition_bits = partition_bits;
4265 }
4266 }
4267#endif
4268 parameters[partition] = best_rice_parameter;
4269 bits_ += best_partition_bits;
4270 }
4271 }
4272
4273 *bits = bits_;
4274 return true;
4275}
4276
4277#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004278FLAC__bool set_partitioned_rice_with_precompute_(
4279 const FLAC__int32 residual[],
4280 const FLAC__uint64 abs_residual_partition_sums[],
4281 const unsigned raw_bits_per_partition[],
4282 const unsigned residual_samples,
4283 const unsigned predictor_order,
4284 const unsigned suggested_rice_parameter,
4285 const unsigned rice_parameter_search_dist,
4286 const unsigned partition_order,
4287 const FLAC__bool search_for_escapes,
4288 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4289 unsigned *bits
4290)
Josh Coalson8395d022001-07-12 21:25:22 +00004291#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004292FLAC__bool set_partitioned_rice_with_precompute_(
4293 const FLAC__uint32 abs_residual[],
4294 const FLAC__uint64 abs_residual_partition_sums[],
4295 const unsigned raw_bits_per_partition[],
4296 const unsigned residual_samples,
4297 const unsigned predictor_order,
4298 const unsigned suggested_rice_parameter,
4299 const unsigned rice_parameter_search_dist,
4300 const unsigned partition_order,
4301 const FLAC__bool search_for_escapes,
4302 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4303 unsigned *bits
4304)
Josh Coalson8395d022001-07-12 21:25:22 +00004305#endif
4306{
4307 unsigned rice_parameter, partition_bits;
4308#ifndef NO_RICE_SEARCH
4309 unsigned best_partition_bits;
4310 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4311#endif
4312 unsigned flat_bits;
4313 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004314 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004315
4316 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4317
Josh Coalsona37ba462002-08-19 21:36:39 +00004318 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4319 parameters = partitioned_rice_contents->parameters;
4320 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004321
Josh Coalson8395d022001-07-12 21:25:22 +00004322 if(partition_order == 0) {
4323 unsigned i;
4324
4325#ifndef NO_RICE_SEARCH
4326 if(rice_parameter_search_dist) {
4327 if(suggested_rice_parameter < rice_parameter_search_dist)
4328 min_rice_parameter = 0;
4329 else
4330 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4331 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4332 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004333#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004334 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4335#endif
4336 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4337 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004338 }
4339 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004340 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00004341
Josh Coalson034dfab2001-04-27 19:10:23 +00004342 best_partition_bits = 0xffffffff;
4343 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4344#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004345#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004346 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00004347 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004348#else
4349 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004350#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004351 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00004352 for(i = 0; i < residual_samples; i++) {
4353#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00004354 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004355#else
Josh Coalson2051dd42001-04-12 22:22:34 +00004356 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 +00004357#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004358 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004359#ifndef NO_RICE_SEARCH
4360 if(partition_bits < best_partition_bits) {
4361 best_rice_parameter = rice_parameter;
4362 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00004363 }
4364 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004365#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004366 if(search_for_escapes) {
4367 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;
4368 if(flat_bits <= best_partition_bits) {
4369 raw_bits[0] = raw_bits_per_partition[0];
4370 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4371 best_partition_bits = flat_bits;
4372 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004373 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004374 parameters[0] = best_rice_parameter;
4375 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004376 }
4377 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00004378 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004379 unsigned partition_samples;
4380 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004381 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00004382 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004383 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00004384 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004385 if(partition_samples <= predictor_order)
4386 return false;
4387 else
4388 partition_samples -= predictor_order;
4389 }
Josh Coalson05d20792001-06-29 23:12:26 +00004390 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004391 /* we are basically calculating the size in bits of the
4392 * average residual magnitude in the partition:
4393 * rice_parameter = floor(log2(mean/partition_samples))
4394 * 'mean' is not a good name for the variable, it is
4395 * actually the sum of magnitudes of all residual values
4396 * in the partition, so the actual mean is
4397 * mean/partition_samples
4398 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004399 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00004400 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004401 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004402#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004403 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4404#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004405 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004406 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004407
Josh Coalson034dfab2001-04-27 19:10:23 +00004408#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004409 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004410 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004411 min_rice_parameter = 0;
4412 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004413 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4414 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004415 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004416#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004417 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4418#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004419 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004420 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004421 }
4422 else
4423 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00004424
Josh Coalson034dfab2001-04-27 19:10:23 +00004425 best_partition_bits = 0xffffffff;
4426 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4427#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004428#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00004429 const unsigned rice_parameter_estimate = rice_parameter-1;
4430 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004431#else
4432 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004433#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004434 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00004435 save_residual_sample = residual_sample;
4436 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00004437#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00004438 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004439#else
Josh Coalson4dacd192001-06-06 21:11:44 +00004440 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 +00004441#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004442 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004443#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00004444 if(rice_parameter != max_rice_parameter)
4445 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00004446 if(partition_bits < best_partition_bits) {
4447 best_rice_parameter = rice_parameter;
4448 best_partition_bits = partition_bits;
4449 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004450 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004451#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004452 if(search_for_escapes) {
4453 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;
4454 if(flat_bits <= best_partition_bits) {
4455 raw_bits[partition] = raw_bits_per_partition[partition];
4456 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4457 best_partition_bits = flat_bits;
4458 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004459 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004460 parameters[partition] = best_rice_parameter;
4461 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004462 }
4463 }
4464
4465 *bits = bits_;
4466 return true;
4467}
Josh Coalson859bc542001-03-27 22:22:27 +00004468
Josh Coalsonf1eff452002-07-31 07:05:33 +00004469unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00004470{
4471 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00004472 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00004473
4474 for(i = 0; i < samples && !(x&1); i++)
4475 x |= signal[i];
4476
4477 if(x == 0) {
4478 shift = 0;
4479 }
4480 else {
4481 for(shift = 0; !(x&1); shift++)
4482 x >>= 1;
4483 }
4484
4485 if(shift > 0) {
4486 for(i = 0; i < samples; i++)
4487 signal[i] >>= shift;
4488 }
4489
4490 return shift;
4491}
Josh Coalsond86e03b2002-08-03 21:56:15 +00004492
4493void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4494{
4495 unsigned channel;
4496
4497 for(channel = 0; channel < channels; channel++)
4498 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4499
4500 fifo->tail += wide_samples;
4501
4502 FLAC__ASSERT(fifo->tail <= fifo->size);
4503}
4504
4505void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4506{
4507 unsigned channel;
4508 unsigned sample, wide_sample;
4509 unsigned tail = fifo->tail;
4510
4511 sample = input_offset * channels;
4512 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4513 for(channel = 0; channel < channels; channel++)
4514 fifo->data[channel][tail] = input[sample++];
4515 tail++;
4516 }
4517 fifo->tail = tail;
4518
4519 FLAC__ASSERT(fifo->tail <= fifo->size);
4520}
4521
Josh Coalson8065a2d2006-10-15 08:32:56 +00004522FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalsond86e03b2002-08-03 21:56:15 +00004523{
4524 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
Josh Coalson8065a2d2006-10-15 08:32:56 +00004525 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004526 (void)decoder;
4527
4528 if(encoder->private_->verify.needs_magic_hack) {
4529 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4530 *bytes = FLAC__STREAM_SYNC_LENGTH;
4531 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4532 encoder->private_->verify.needs_magic_hack = false;
4533 }
4534 else {
4535 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00004536 /*
4537 * If we get here, a FIFO underflow has occurred,
4538 * which means there is a bug somewhere.
4539 */
4540 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004541 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4542 }
4543 else if(encoded_bytes < *bytes)
4544 *bytes = encoded_bytes;
4545 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4546 encoder->private_->verify.output.data += *bytes;
4547 encoder->private_->verify.output.bytes -= *bytes;
4548 }
4549
4550 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4551}
4552
4553FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4554{
4555 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4556 unsigned channel;
Josh Coalson49f2f162006-11-09 16:54:52 +00004557 const unsigned channels = frame->header.channels;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004558 const unsigned blocksize = frame->header.blocksize;
4559 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4560
Josh Coalson49f2f162006-11-09 16:54:52 +00004561 (void)decoder;
4562
Josh Coalsond86e03b2002-08-03 21:56:15 +00004563 for(channel = 0; channel < channels; channel++) {
4564 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4565 unsigned i, sample = 0;
4566 FLAC__int32 expect = 0, got = 0;
4567
4568 for(i = 0; i < blocksize; i++) {
4569 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4570 sample = i;
4571 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4572 got = (FLAC__int32)buffer[channel][i];
4573 break;
4574 }
4575 }
4576 FLAC__ASSERT(i < blocksize);
4577 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4578 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00004579 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004580 encoder->private_->verify.error_stats.channel = channel;
4581 encoder->private_->verify.error_stats.sample = sample;
4582 encoder->private_->verify.error_stats.expected = expect;
4583 encoder->private_->verify.error_stats.got = got;
4584 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4585 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4586 }
4587 }
4588 /* dequeue the frame from the fifo */
Josh Coalsond86e03b2002-08-03 21:56:15 +00004589 encoder->private_->verify.input_fifo.tail -= blocksize;
Josh Coalson49f2f162006-11-09 16:54:52 +00004590 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
Josh Coalsonb7b57ef2006-11-09 07:06:33 +00004591 for(channel = 0; channel < channels; channel++)
4592 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 +00004593 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4594}
4595
4596void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4597{
4598 (void)decoder, (void)metadata, (void)client_data;
4599}
4600
4601void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4602{
4603 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4604 (void)decoder, (void)status;
4605 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4606}
Josh Coalson6b21f662006-09-13 01:42:27 +00004607
Josh Coalson8065a2d2006-10-15 08:32:56 +00004608FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalson8da98c82006-10-15 04:24:05 +00004609{
4610 (void)client_data;
4611
Josh Coalson8065a2d2006-10-15 08:32:56 +00004612 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
Josh Coalson8da98c82006-10-15 04:24:05 +00004613 if (*bytes == 0) {
4614 if (feof(encoder->private_->file))
4615 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4616 else if (ferror(encoder->private_->file))
4617 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4618 }
4619 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4620}
4621
Josh Coalson6b21f662006-09-13 01:42:27 +00004622FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4623{
4624 (void)client_data;
4625
4626 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4627 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4628 else
4629 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4630}
4631
4632FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4633{
4634 off_t offset;
4635
4636 (void)client_data;
4637
4638 offset = ftello(encoder->private_->file);
4639
4640 if(offset < 0) {
4641 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4642 }
4643 else {
4644 *absolute_byte_offset = (FLAC__uint64)offset;
4645 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4646 }
4647}
4648
4649#ifdef FLAC__VALGRIND_TESTING
4650static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4651{
4652 size_t ret = fwrite(ptr, size, nmemb, stream);
4653 if(!ferror(stream))
4654 fflush(stream);
4655 return ret;
4656}
4657#else
4658#define local__fwrite fwrite
4659#endif
4660
Josh Coalson352feb52006-10-15 17:08:52 +00004661FLAC__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 +00004662{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004663 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00004664
4665 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson8da98c82006-10-15 04:24:05 +00004666 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4667#if FLAC__HAS_OGG
4668 /* We would like to be able to use 'samples > 0' in the
4669 * clause here but currently because of the nature of our
4670 * Ogg writing implementation, 'samples' is always 0 (see
4671 * ogg_encoder_aspect.c). The downside is extra progress
4672 * callbacks.
4673 */
4674 encoder->private_->is_ogg? true :
4675#endif
4676 samples > 0
4677 );
4678 if(call_it) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004679 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4680 * because at this point in the callback chain, the stats
4681 * have not been updated. Only after we return and control
4682 * gets back to write_frame_() are the stats updated
4683 */
4684 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);
4685 }
Josh Coalson6b21f662006-09-13 01:42:27 +00004686 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4687 }
4688 else
4689 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4690}
4691
4692/*
4693 * This will forcibly set stdout to binary mode (for OSes that require it)
4694 */
4695FILE *get_binary_stdout_()
4696{
4697 /* if something breaks here it is probably due to the presence or
4698 * absence of an underscore before the identifiers 'setmode',
4699 * 'fileno', and/or 'O_BINARY'; check your system header files.
4700 */
4701#if defined _MSC_VER || defined __MINGW32__
4702 _setmode(_fileno(stdout), _O_BINARY);
4703#elif defined __CYGWIN__
4704 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4705 setmode(_fileno(stdout), _O_BINARY);
4706#elif defined __EMX__
4707 setmode(fileno(stdout), O_BINARY);
4708#endif
4709
4710 return stdout;
4711}