blob: 968ae2b3f965ad4c8f64b3fd6e4d217654982aa1 [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;
105 const char *apodization;
106 unsigned max_lpc_order;
107 unsigned qlp_coeff_precision;
108 FLAC__bool do_qlp_coeff_prec_search;
109 FLAC__bool do_escape_coding;
110 FLAC__bool do_exhaustive_model_search;
111 unsigned min_residual_partition_order;
112 unsigned max_residual_partition_order;
113 unsigned rice_parameter_search_dist;
114} compression_levels_[] = {
115 { false, false, "tukey(0.5)", 0, 0, false, false, false, 2, 2, 0 },
116 { true , true , "tukey(0.5)", 0, 0, false, false, false, 2, 2, 0 },
117 { true , false, "tukey(0.5)", 0, 0, false, false, false, 0, 3, 0 },
118 { false, false, "tukey(0.5)", 6, 0, false, false, false, 3, 3, 0 },
119 { true , true , "tukey(0.5)", 8, 0, false, false, false, 3, 3, 0 },
120 { true , false, "tukey(0.5)", 8, 0, false, false, false, 3, 3, 0 },
121 { true , false, "tukey(0.5)", 8, 0, false, false, false, 0, 4, 0 },
122 { true , false, "tukey(0.5)", 8, 0, false, false, true , 0, 6, 0 },
123 { true , false, "tukey(0.5)", 12, 0, false, false, true , 0, 6, 0 }
124};
125
126
Josh Coalson0a15c142001-06-13 17:59:57 +0000127/***********************************************************************
128 *
129 * Private class method prototypes
130 *
131 ***********************************************************************/
132
Josh Coalsonf1eff452002-07-31 07:05:33 +0000133static void set_defaults_(FLAC__StreamEncoder *encoder);
134static void free_(FLAC__StreamEncoder *encoder);
Josh Coalson85aaed82006-11-09 01:19:13 +0000135static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
Josh Coalson49f2f162006-11-09 16:54:52 +0000136static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
137static 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 +0000138static void update_metadata_(const FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000139#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +0000140static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000141#endif
Josh Coalson49f2f162006-11-09 16:54:52 +0000142static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
Josh Coalson85aaed82006-11-09 01:19:13 +0000143static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000144
145static FLAC__bool process_subframe_(
146 FLAC__StreamEncoder *encoder,
147 unsigned min_partition_order,
148 unsigned max_partition_order,
149 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000150 const FLAC__FrameHeader *frame_header,
151 unsigned subframe_bps,
152 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000153#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000154 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000155#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000156 FLAC__Subframe *subframe[2],
157 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
158 FLAC__int32 *residual[2],
159 unsigned *best_subframe,
160 unsigned *best_bits
161);
162
163static FLAC__bool add_subframe_(
164 FLAC__StreamEncoder *encoder,
165 const FLAC__FrameHeader *frame_header,
166 unsigned subframe_bps,
167 const FLAC__Subframe *subframe,
168 FLAC__BitBuffer *frame
169);
170
171static unsigned evaluate_constant_subframe_(
172 const FLAC__int32 signal,
173 unsigned subframe_bps,
174 FLAC__Subframe *subframe
175);
176
177static unsigned evaluate_fixed_subframe_(
178 FLAC__StreamEncoder *encoder,
179 const FLAC__int32 signal[],
180 FLAC__int32 residual[],
181 FLAC__uint32 abs_residual[],
182 FLAC__uint64 abs_residual_partition_sums[],
183 unsigned raw_bits_per_partition[],
184 unsigned blocksize,
185 unsigned subframe_bps,
186 unsigned order,
187 unsigned rice_parameter,
188 unsigned min_partition_order,
189 unsigned max_partition_order,
190 FLAC__bool precompute_partition_sums,
191 FLAC__bool do_escape_coding,
192 unsigned rice_parameter_search_dist,
193 FLAC__Subframe *subframe,
194 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
195);
196
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000197#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000198static unsigned evaluate_lpc_subframe_(
199 FLAC__StreamEncoder *encoder,
200 const FLAC__int32 signal[],
201 FLAC__int32 residual[],
202 FLAC__uint32 abs_residual[],
203 FLAC__uint64 abs_residual_partition_sums[],
204 unsigned raw_bits_per_partition[],
205 const FLAC__real lp_coeff[],
206 unsigned blocksize,
207 unsigned subframe_bps,
208 unsigned order,
209 unsigned qlp_coeff_precision,
210 unsigned rice_parameter,
211 unsigned min_partition_order,
212 unsigned max_partition_order,
213 FLAC__bool precompute_partition_sums,
214 FLAC__bool do_escape_coding,
215 unsigned rice_parameter_search_dist,
216 FLAC__Subframe *subframe,
217 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
218);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000219#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000220
221static unsigned evaluate_verbatim_subframe_(
222 const FLAC__int32 signal[],
223 unsigned blocksize,
224 unsigned subframe_bps,
225 FLAC__Subframe *subframe
226);
227
228static unsigned find_best_partition_order_(
229 struct FLAC__StreamEncoderPrivate *private_,
230 const FLAC__int32 residual[],
231 FLAC__uint32 abs_residual[],
232 FLAC__uint64 abs_residual_partition_sums[],
233 unsigned raw_bits_per_partition[],
234 unsigned residual_samples,
235 unsigned predictor_order,
236 unsigned rice_parameter,
237 unsigned min_partition_order,
238 unsigned max_partition_order,
239 FLAC__bool precompute_partition_sums,
240 FLAC__bool do_escape_coding,
241 unsigned rice_parameter_search_dist,
242 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
243);
244
245static void precompute_partition_info_sums_(
246 const FLAC__uint32 abs_residual[],
247 FLAC__uint64 abs_residual_partition_sums[],
248 unsigned residual_samples,
249 unsigned predictor_order,
250 unsigned min_partition_order,
251 unsigned max_partition_order
252);
253
254static void precompute_partition_info_escapes_(
255 const FLAC__int32 residual[],
256 unsigned raw_bits_per_partition[],
257 unsigned residual_samples,
258 unsigned predictor_order,
259 unsigned min_partition_order,
260 unsigned max_partition_order
261);
262
Josh Coalson8395d022001-07-12 21:25:22 +0000263#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000264static FLAC__bool set_partitioned_rice_(
265 const FLAC__uint32 abs_residual[],
266 const FLAC__int32 residual[],
267 const unsigned residual_samples,
268 const unsigned predictor_order,
269 const unsigned suggested_rice_parameter,
270 const unsigned rice_parameter_search_dist,
271 const unsigned partition_order,
272 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
273 unsigned *bits
274);
275
276static FLAC__bool set_partitioned_rice_with_precompute_(
277 const FLAC__int32 residual[],
278 const FLAC__uint64 abs_residual_partition_sums[],
279 const unsigned raw_bits_per_partition[],
280 const unsigned residual_samples,
281 const unsigned predictor_order,
282 const unsigned suggested_rice_parameter,
283 const unsigned rice_parameter_search_dist,
284 const unsigned partition_order,
285 const FLAC__bool search_for_escapes,
286 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
287 unsigned *bits
288);
Josh Coalson8395d022001-07-12 21:25:22 +0000289#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000290static FLAC__bool set_partitioned_rice_(
291 const FLAC__uint32 abs_residual[],
292 const unsigned residual_samples,
293 const unsigned predictor_order,
294 const unsigned suggested_rice_parameter,
295 const unsigned rice_parameter_search_dist,
296 const unsigned partition_order,
297 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
298 unsigned *bits
299);
300
301static FLAC__bool set_partitioned_rice_with_precompute_(
302 const FLAC__uint32 abs_residual[],
303 const FLAC__uint64 abs_residual_partition_sums[],
304 const unsigned raw_bits_per_partition[],
305 const unsigned residual_samples,
306 const unsigned predictor_order,
307 const unsigned suggested_rice_parameter,
308 const unsigned rice_parameter_search_dist,
309 const unsigned partition_order,
310 const FLAC__bool search_for_escapes,
311 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
312 unsigned *bits
313);
Josh Coalson0a15c142001-06-13 17:59:57 +0000314#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000315
Josh Coalsonf1eff452002-07-31 07:05:33 +0000316static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000317
Josh Coalsond86e03b2002-08-03 21:56:15 +0000318/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000319static void append_to_verify_fifo_(
320 verify_input_fifo *fifo,
321 const FLAC__int32 * const input[],
322 unsigned input_offset,
323 unsigned channels,
324 unsigned wide_samples
325);
326
327static void append_to_verify_fifo_interleaved_(
328 verify_input_fifo *fifo,
329 const FLAC__int32 input[],
330 unsigned input_offset,
331 unsigned channels,
332 unsigned wide_samples
333);
334
Josh Coalson8065a2d2006-10-15 08:32:56 +0000335static 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 +0000336static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
337static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
338static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000339
Josh Coalson8065a2d2006-10-15 08:32:56 +0000340static 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 +0000341static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
342static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
Josh Coalson352feb52006-10-15 17:08:52 +0000343static 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 +0000344static FILE *get_binary_stdout_();
Josh Coalson6fe72f72002-08-20 04:01:59 +0000345
Josh Coalson0a15c142001-06-13 17:59:57 +0000346
347/***********************************************************************
348 *
349 * Private class data
350 *
351 ***********************************************************************/
352
353typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000354 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000355 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
356 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 +0000357#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000358 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
359 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 +0000360 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
361 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000362#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000363 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
364 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 +0000365 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
366 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000367 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
368 FLAC__Subframe subframe_workspace_mid_side[2][2];
369 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
370 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000371 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
372 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
373 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
374 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000375 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000376 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000377 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000378 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000379 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000380 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 +0000381 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 +0000382 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000383 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
384 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000385 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalson6b21f662006-09-13 01:42:27 +0000386 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
387 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000388 unsigned current_sample_number;
389 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000390 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000391 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000392#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000393 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 +0000394#else
395 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
396#endif
397#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000398 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
Josh Coalson7446e182005-01-26 04:04:38 +0000399 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[]);
400 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[]);
401 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 +0000402#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000403 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
404 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
405 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
406 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 +0000407 FLAC__bool disable_constant_subframes;
408 FLAC__bool disable_fixed_subframes;
409 FLAC__bool disable_verbatim_subframes;
Josh Coalson8da98c82006-10-15 04:24:05 +0000410#if FLAC__HAS_OGG
411 FLAC__bool is_ogg;
412#endif
413 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
Josh Coalson6b21f662006-09-13 01:42:27 +0000414 FLAC__StreamEncoderSeekCallback seek_callback;
415 FLAC__StreamEncoderTellCallback tell_callback;
Josh Coalson681c2932002-08-01 08:19:37 +0000416 FLAC__StreamEncoderWriteCallback write_callback;
417 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000418 FLAC__StreamEncoderProgressCallback progress_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000419 void *client_data;
Josh Coalson6b21f662006-09-13 01:42:27 +0000420 unsigned first_seekpoint_to_check;
421 FILE *file; /* only used when encoding to a file */
422 FLAC__uint64 bytes_written;
423 FLAC__uint64 samples_written;
424 unsigned frames_written;
425 unsigned total_frames_estimate;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000426 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000427 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
428 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000429#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000430 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
431 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000432 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
433 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000434#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000435 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
436 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
437 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000438 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000439 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000440 /*
441 * These fields have been moved here from private function local
442 * declarations merely to save stack space during encoding.
443 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000444#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000445 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000446#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000447 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000448 /*
449 * The data for the verify section
450 */
451 struct {
452 FLAC__StreamDecoder *decoder;
453 EncoderStateHint state_hint;
454 FLAC__bool needs_magic_hack;
455 verify_input_fifo input_fifo;
456 verify_output output;
457 struct {
458 FLAC__uint64 absolute_sample;
459 unsigned frame_number;
460 unsigned channel;
461 unsigned sample;
462 FLAC__int32 expected;
463 FLAC__int32 got;
464 } error_stats;
465 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000466 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000467} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000468
Josh Coalson0a15c142001-06-13 17:59:57 +0000469/***********************************************************************
470 *
471 * Public static class data
472 *
473 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000474
Josh Coalson6afed9f2002-10-16 22:29:47 +0000475FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000476 "FLAC__STREAM_ENCODER_OK",
Josh Coalson6b21f662006-09-13 01:42:27 +0000477 "FLAC__STREAM_ENCODER_UNINITIALIZED",
Josh Coalson8da98c82006-10-15 04:24:05 +0000478 "FLAC__STREAM_ENCODER_OGG_ERROR",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000479 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
480 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson6b21f662006-09-13 01:42:27 +0000481 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
482 "FLAC__STREAM_ENCODER_IO_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000483 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson6b21f662006-09-13 01:42:27 +0000484 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
485};
486
487FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
488 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
489 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
Josh Coalson8da98c82006-10-15 04:24:05 +0000490 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
Josh Coalson6b21f662006-09-13 01:42:27 +0000491 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
492 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
493 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
494 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
495 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
496 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
497 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
Josh Coalson6b21f662006-09-13 01:42:27 +0000498 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
499 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
500 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
501 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000502};
503
Josh Coalson8da98c82006-10-15 04:24:05 +0000504FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
505 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
506 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
507 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
508 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
509};
510
Josh Coalson6afed9f2002-10-16 22:29:47 +0000511FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000512 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
513 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000514};
515
Josh Coalson6b21f662006-09-13 01:42:27 +0000516FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
517 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
518 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
519 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
520};
521
522FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
523 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
524 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
525 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
526};
527
Josh Coalson49f2f162006-11-09 16:54:52 +0000528/* Number of samples that will be overread to watch for end of stream. By
529 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
530 * always try to read blocksize+1 samples before encoding a block, so that
531 * even if the stream has a total sample count that is an integral multiple
532 * of the blocksize, we will still notice when we are encoding the last
533 * block. This is needed, for example, to correctly set the end-of-stream
534 * marker in Ogg FLAC.
535 *
536 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
537 * not really any reason to change it.
538 */
539static const unsigned OVERREAD_ = 1;
540
Josh Coalson0a15c142001-06-13 17:59:57 +0000541/***********************************************************************
542 *
543 * Class constructor/destructor
544 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000545 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000546FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000547{
Josh Coalson0a15c142001-06-13 17:59:57 +0000548 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000549 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000550
Josh Coalson0a15c142001-06-13 17:59:57 +0000551 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000552
Josh Coalsonea7155f2002-10-18 05:49:19 +0000553 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000554 if(encoder == 0) {
555 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000556 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000557
Josh Coalsonea7155f2002-10-18 05:49:19 +0000558 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000559 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000560 free(encoder);
561 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000562 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000563
Josh Coalsonea7155f2002-10-18 05:49:19 +0000564 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000565 if(encoder->private_ == 0) {
566 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000567 free(encoder);
568 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000569 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000570
Josh Coalsonaec256b2002-03-12 16:19:54 +0000571 encoder->private_->frame = FLAC__bitbuffer_new();
572 if(encoder->private_->frame == 0) {
573 free(encoder->private_);
574 free(encoder->protected_);
575 free(encoder);
576 return 0;
577 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000578
Josh Coalson6b21f662006-09-13 01:42:27 +0000579 encoder->private_->file = 0;
580
Josh Coalsonf1eff452002-07-31 07:05:33 +0000581 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000582
Josh Coalson3262b0d2002-08-14 20:58:42 +0000583 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000584
585 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
586 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
587 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
588 }
589 for(i = 0; i < 2; i++) {
590 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
591 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
592 }
593 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000594 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
595 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000596 }
597 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000598 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
599 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 +0000600 }
601
602 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000603 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
604 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000605 }
606 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000607 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
608 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 +0000609 }
610 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000611 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000612
Josh Coalsonfa697a92001-08-16 20:07:29 +0000613 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000614
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000615 return encoder;
616}
617
Josh Coalson6afed9f2002-10-16 22:29:47 +0000618FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000619{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000620 unsigned i;
621
Josh Coalsonf1eff452002-07-31 07:05:33 +0000622 FLAC__ASSERT(0 != encoder);
623 FLAC__ASSERT(0 != encoder->protected_);
624 FLAC__ASSERT(0 != encoder->private_);
625 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000626
Josh Coalson3262b0d2002-08-14 20:58:42 +0000627 encoder->private_->is_being_deleted = true;
628
Josh Coalsona5862262006-11-09 06:58:26 +0000629 (void)FLAC__stream_encoder_finish(encoder);
Josh Coalson3262b0d2002-08-14 20:58:42 +0000630
Josh Coalson4fa90592002-12-04 07:01:37 +0000631 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000632 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000633
634 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000635 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
636 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000637 }
638 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000639 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
640 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 +0000641 }
642 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000643 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000644
Josh Coalsonaec256b2002-03-12 16:19:54 +0000645 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000646 free(encoder->private_);
647 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000648 free(encoder);
649}
650
Josh Coalson0a15c142001-06-13 17:59:57 +0000651/***********************************************************************
652 *
653 * Public class methods
654 *
655 ***********************************************************************/
656
Josh Coalson8da98c82006-10-15 04:24:05 +0000657static FLAC__StreamEncoderInitStatus init_stream_internal_(
658 FLAC__StreamEncoder *encoder,
659 FLAC__StreamEncoderReadCallback read_callback,
660 FLAC__StreamEncoderWriteCallback write_callback,
661 FLAC__StreamEncoderSeekCallback seek_callback,
662 FLAC__StreamEncoderTellCallback tell_callback,
663 FLAC__StreamEncoderMetadataCallback metadata_callback,
664 void *client_data,
665 FLAC__bool is_ogg
666)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000667{
668 unsigned i;
Josh Coalson3957c472006-09-24 16:25:42 +0000669 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000670
Josh Coalsonf1eff452002-07-31 07:05:33 +0000671 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000672
Josh Coalsonfa697a92001-08-16 20:07:29 +0000673 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000674 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000675
Josh Coalsonf1ac7d92006-11-16 07:20:09 +0000676#if !FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +0000677 if(is_ogg)
678 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
679#endif
680
Josh Coalson6b21f662006-09-13 01:42:27 +0000681 if(0 == write_callback || (seek_callback && 0 == tell_callback))
682 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000683
Josh Coalsonfa697a92001-08-16 20:07:29 +0000684 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000685 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000686
Josh Coalson425609c2006-11-03 16:08:52 +0000687 if(encoder->protected_->channels != 2) {
688 encoder->protected_->do_mid_side_stereo = false;
689 encoder->protected_->loose_mid_side_stereo = false;
690 }
691 else if(!encoder->protected_->do_mid_side_stereo)
692 encoder->protected_->loose_mid_side_stereo = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000693
Josh Coalsonfa697a92001-08-16 20:07:29 +0000694 if(encoder->protected_->bits_per_sample >= 32)
Josh Coalson6b21f662006-09-13 01:42:27 +0000695 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 +0000696
Josh Coalson76c68bc2002-05-17 06:22:02 +0000697 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 +0000698 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000699
Josh Coalson0833f342002-07-15 05:31:55 +0000700 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000701 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000702
Josh Coalson425609c2006-11-03 16:08:52 +0000703 if(encoder->protected_->blocksize == 0) {
704 if(encoder->protected_->max_lpc_order == 0)
705 encoder->protected_->blocksize = 1152;
706 else
707 encoder->protected_->blocksize = 4608;
708 }
709
Josh Coalsonfa697a92001-08-16 20:07:29 +0000710 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000711 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000712
Josh Coalson20ac2c12002-08-30 05:47:14 +0000713 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000714 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000715
Josh Coalsonfa697a92001-08-16 20:07:29 +0000716 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000717 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000718
Josh Coalsonfa697a92001-08-16 20:07:29 +0000719 if(encoder->protected_->qlp_coeff_precision == 0) {
720 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000721 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
722 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000723 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 +0000724 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000725 else if(encoder->protected_->bits_per_sample == 16) {
726 if(encoder->protected_->blocksize <= 192)
727 encoder->protected_->qlp_coeff_precision = 7;
728 else if(encoder->protected_->blocksize <= 384)
729 encoder->protected_->qlp_coeff_precision = 8;
730 else if(encoder->protected_->blocksize <= 576)
731 encoder->protected_->qlp_coeff_precision = 9;
732 else if(encoder->protected_->blocksize <= 1152)
733 encoder->protected_->qlp_coeff_precision = 10;
734 else if(encoder->protected_->blocksize <= 2304)
735 encoder->protected_->qlp_coeff_precision = 11;
736 else if(encoder->protected_->blocksize <= 4608)
737 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000738 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000739 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000740 }
741 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000742 if(encoder->protected_->blocksize <= 384)
743 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
744 else if(encoder->protected_->blocksize <= 1152)
745 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
746 else
747 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000748 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000749 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000750 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000751 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 +0000752 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000753
Josh Coalsonfa697a92001-08-16 20:07:29 +0000754 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000755 if(
756 encoder->protected_->blocksize != 192 &&
757 encoder->protected_->blocksize != 576 &&
758 encoder->protected_->blocksize != 1152 &&
759 encoder->protected_->blocksize != 2304 &&
760 encoder->protected_->blocksize != 4608 &&
761 encoder->protected_->blocksize != 256 &&
762 encoder->protected_->blocksize != 512 &&
763 encoder->protected_->blocksize != 1024 &&
764 encoder->protected_->blocksize != 2048 &&
765 encoder->protected_->blocksize != 4096 &&
766 encoder->protected_->blocksize != 8192 &&
767 encoder->protected_->blocksize != 16384
768 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000769 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000770 if(
771 encoder->protected_->sample_rate != 8000 &&
772 encoder->protected_->sample_rate != 16000 &&
773 encoder->protected_->sample_rate != 22050 &&
774 encoder->protected_->sample_rate != 24000 &&
775 encoder->protected_->sample_rate != 32000 &&
776 encoder->protected_->sample_rate != 44100 &&
777 encoder->protected_->sample_rate != 48000 &&
778 encoder->protected_->sample_rate != 96000
779 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000780 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000781 if(
782 encoder->protected_->bits_per_sample != 8 &&
783 encoder->protected_->bits_per_sample != 12 &&
784 encoder->protected_->bits_per_sample != 16 &&
785 encoder->protected_->bits_per_sample != 20 &&
786 encoder->protected_->bits_per_sample != 24
787 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000788 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000789 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000790 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsond0edb972006-10-07 06:50:08 +0000791 if(
792 encoder->protected_->sample_rate <= 48000 &&
793 (
794 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
795 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
796 )
797 ) {
798 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
799 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000800 }
801
Josh Coalsonfa697a92001-08-16 20:07:29 +0000802 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
803 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
804 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
805 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000806
Josh Coalson8da98c82006-10-15 04:24:05 +0000807#if FLAC__HAS_OGG
808 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
809 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
810 unsigned i;
811 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
812 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
813 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
814 for( ; i > 0; i--)
815 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
816 encoder->protected_->metadata[0] = vc;
817 break;
818 }
819 }
820 }
821#endif
822 /* keep track of any SEEKTABLE block */
823 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
824 unsigned i;
825 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
826 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
827 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
828 break; /* take only the first one */
829 }
830 }
831 }
832
Josh Coalson66075c12002-06-01 05:39:38 +0000833 /* validate metadata */
834 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000835 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000836 metadata_has_seektable = false;
837 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000838 metadata_picture_has_type1 = false;
839 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000840 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000841 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
842 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000843 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000844 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000845 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000846 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000847 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000848 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000849 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000850 }
Josh Coalson3957c472006-09-24 16:25:42 +0000851 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000852 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000853 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000854 metadata_has_vorbis_comment = true;
855 }
Josh Coalson3957c472006-09-24 16:25:42 +0000856 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
857 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 +0000858 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000859 }
Josh Coalson3957c472006-09-24 16:25:42 +0000860 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
861 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000862 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000863 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
864 if(metadata_picture_has_type1) /* there should only be 1 per stream */
865 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
866 metadata_picture_has_type1 = true;
867 /* standard icon must be 32x32 pixel PNG */
868 if(
869 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
870 (
871 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
872 m->data.picture.width != 32 ||
873 m->data.picture.height != 32
874 )
875 )
876 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
877 }
878 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
879 if(metadata_picture_has_type2) /* there should only be 1 per stream */
880 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
881 metadata_picture_has_type2 = true;
882 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000883 }
Josh Coalson66075c12002-06-01 05:39:38 +0000884 }
885
Josh Coalsonfa697a92001-08-16 20:07:29 +0000886 encoder->private_->input_capacity = 0;
887 for(i = 0; i < encoder->protected_->channels; i++) {
888 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000889#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000890 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000891#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000892 }
893 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000894 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000895#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000896 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000897#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000898 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000899#ifndef FLAC__INTEGER_ONLY_LIBRARY
900 for(i = 0; i < encoder->protected_->num_apodizations; i++)
901 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
902 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
903#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000904 for(i = 0; i < encoder->protected_->channels; i++) {
905 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
906 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
907 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000908 }
909 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000910 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
911 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
912 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000913 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000914 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
915 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
916 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000917#ifndef FLAC__INTEGER_ONLY_LIBRARY
918 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
919#else
920 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
921 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
922 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
923 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
924 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
925 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
926 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);
927#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000928 if(encoder->private_->loose_mid_side_stereo_frames == 0)
929 encoder->private_->loose_mid_side_stereo_frames = 1;
930 encoder->private_->loose_mid_side_stereo_frame_count = 0;
931 encoder->private_->current_sample_number = 0;
932 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000933
Josh Coalsonfa697a92001-08-16 20:07:29 +0000934 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
935 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? */
936 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000937
Josh Coalsoncf30f502001-05-23 20:57:44 +0000938 /*
939 * get the CPU info and set the function pointers
940 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000941 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000942 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000943#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000944 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000945#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000946 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000947#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000948 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000949 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 +0000950 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000951#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000952 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000953#ifndef FLAC__INTEGER_ONLY_LIBRARY
954# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000955 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000956# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000957 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000958# ifdef FLAC__HAS_NASM
959# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000960 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000961 if(encoder->protected_->max_lpc_order < 4)
962 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
963 else if(encoder->protected_->max_lpc_order < 8)
964 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
965 else if(encoder->protected_->max_lpc_order < 12)
966 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000967 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000968 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000969 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000970 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000971# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000972 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000973 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000974 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000975 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000976 if(encoder->private_->cpuinfo.data.ia32.mmx) {
977 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
978 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 +0000979 }
980 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000981 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
982 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 +0000983 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000984 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
985 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
986# endif /* FLAC__HAS_NASM */
987# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000988 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000989# endif /* !FLAC__NO_ASM */
990#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000991 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000992 if(encoder->private_->use_wide_by_block) {
993 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000994 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000995
Josh Coalson8395d022001-07-12 21:25:22 +0000996 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000997 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 +0000998
Josh Coalson6b21f662006-09-13 01:42:27 +0000999 /* set state to OK; from here on, errors are fatal and we'll override the state then */
1000 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
1001
Josh Coalson8da98c82006-10-15 04:24:05 +00001002#if FLAC__HAS_OGG
1003 encoder->private_->is_ogg = is_ogg;
1004 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
1005 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
1006 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1007 }
1008#endif
1009
1010 encoder->private_->read_callback = read_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +00001011 encoder->private_->write_callback = write_callback;
1012 encoder->private_->seek_callback = seek_callback;
1013 encoder->private_->tell_callback = tell_callback;
1014 encoder->private_->metadata_callback = metadata_callback;
1015 encoder->private_->client_data = client_data;
1016
Josh Coalsonf1eff452002-07-31 07:05:33 +00001017 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001018 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001019 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001020 }
Josh Coalsonaec256b2002-03-12 16:19:54 +00001021
Josh Coalson6b21f662006-09-13 01:42:27 +00001022 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
1023 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1024 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1025 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001026
1027 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +00001028 * Set up the verify stuff if necessary
1029 */
1030 if(encoder->protected_->verify) {
1031 /*
1032 * First, set up the fifo which will hold the
1033 * original signal to compare against
1034 */
Josh Coalson49f2f162006-11-09 16:54:52 +00001035 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001036 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001037 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
1038 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1039 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1040 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001041 }
1042 encoder->private_->verify.input_fifo.tail = 0;
1043
1044 /*
1045 * Now set up a stream decoder for verification
1046 */
1047 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
Josh Coalson6b21f662006-09-13 01:42:27 +00001048 if(0 == encoder->private_->verify.decoder) {
1049 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1050 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1051 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001052
Josh Coalson6b21f662006-09-13 01:42:27 +00001053 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) {
1054 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1055 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1056 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001057 }
Josh Coalson589f8c72002-08-07 23:54:55 +00001058 encoder->private_->verify.error_stats.absolute_sample = 0;
1059 encoder->private_->verify.error_stats.frame_number = 0;
1060 encoder->private_->verify.error_stats.channel = 0;
1061 encoder->private_->verify.error_stats.sample = 0;
1062 encoder->private_->verify.error_stats.expected = 0;
1063 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001064
1065 /*
Josh Coalson6b21f662006-09-13 01:42:27 +00001066 * These must be done before we write any metadata, because that
1067 * calls the write_callback, which uses these values.
1068 */
1069 encoder->private_->first_seekpoint_to_check = 0;
1070 encoder->private_->samples_written = 0;
1071 encoder->protected_->streaminfo_offset = 0;
1072 encoder->protected_->seektable_offset = 0;
1073 encoder->protected_->audio_offset = 0;
1074
1075 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001076 * write the stream header
1077 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001078 if(encoder->protected_->verify)
1079 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson6b21f662006-09-13 01:42:27 +00001080 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1081 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1082 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1083 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001084 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001085 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001086 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001087 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001088
Josh Coalson5c491a12002-08-01 06:39:40 +00001089 /*
1090 * write the STREAMINFO metadata block
1091 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001092 if(encoder->protected_->verify)
1093 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +00001094 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1095 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1096 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1097 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1098 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1099 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1100 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1101 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1102 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1103 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1104 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1105 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 +00001106 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +00001107 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1108 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1109 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1110 }
1111 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1112 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1113 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1114 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001115 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001116 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001117 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001118 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001119
Josh Coalson5c491a12002-08-01 06:39:40 +00001120 /*
1121 * Now that the STREAMINFO block is written, we can init this to an
1122 * absurdly-high value...
1123 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001124 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 +00001125 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001126 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001127
Josh Coalson5c491a12002-08-01 06:39:40 +00001128 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001129 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1130 * if not, we will write an empty one (FLAC__add_metadata_block()
1131 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001132 *
Josh Coalson8da98c82006-10-15 04:24:05 +00001133 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1134 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1135 * true it will have already insured that the metadata list is properly
1136 * ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001137 */
1138 if(!metadata_has_vorbis_comment) {
1139 FLAC__StreamMetadata vorbis_comment;
1140 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1141 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1142 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1143 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1144 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1145 vorbis_comment.data.vorbis_comment.num_comments = 0;
1146 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001147 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1148 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1149 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1150 }
1151 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1152 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1153 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1154 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001155 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001156 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001157 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001158 }
1159 }
1160
1161 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001162 * write the user's metadata blocks
1163 */
1164 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1165 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001166 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1167 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1168 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1169 }
1170 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1171 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1172 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1173 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001174 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001175 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001176 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001177 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001178 }
1179
Josh Coalson6b21f662006-09-13 01:42:27 +00001180 /* now that all the metadata is written, we save the stream offset */
1181 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 */
1182 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1183 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1184 }
1185
Josh Coalsond86e03b2002-08-03 21:56:15 +00001186 if(encoder->protected_->verify)
1187 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1188
Josh Coalson6b21f662006-09-13 01:42:27 +00001189 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1190}
1191
Josh Coalson8da98c82006-10-15 04:24:05 +00001192FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1193 FLAC__StreamEncoder *encoder,
1194 FLAC__StreamEncoderWriteCallback write_callback,
1195 FLAC__StreamEncoderSeekCallback seek_callback,
1196 FLAC__StreamEncoderTellCallback tell_callback,
1197 FLAC__StreamEncoderMetadataCallback metadata_callback,
1198 void *client_data
1199)
1200{
1201 return init_stream_internal_(
1202 encoder,
1203 /*read_callback=*/0,
1204 write_callback,
1205 seek_callback,
1206 tell_callback,
1207 metadata_callback,
1208 client_data,
1209 /*is_ogg=*/false
1210 );
1211}
1212
1213FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1214 FLAC__StreamEncoder *encoder,
1215 FLAC__StreamEncoderReadCallback read_callback,
1216 FLAC__StreamEncoderWriteCallback write_callback,
1217 FLAC__StreamEncoderSeekCallback seek_callback,
1218 FLAC__StreamEncoderTellCallback tell_callback,
1219 FLAC__StreamEncoderMetadataCallback metadata_callback,
1220 void *client_data
1221)
1222{
1223 return init_stream_internal_(
1224 encoder,
1225 read_callback,
1226 write_callback,
1227 seek_callback,
1228 tell_callback,
1229 metadata_callback,
1230 client_data,
1231 /*is_ogg=*/true
1232 );
1233}
1234
1235static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1236 FLAC__StreamEncoder *encoder,
1237 FILE *file,
1238 FLAC__StreamEncoderProgressCallback progress_callback,
1239 void *client_data,
1240 FLAC__bool is_ogg
1241)
Josh Coalson6b21f662006-09-13 01:42:27 +00001242{
1243 FLAC__StreamEncoderInitStatus init_status;
1244
1245 FLAC__ASSERT(0 != encoder);
1246 FLAC__ASSERT(0 != file);
1247
Josh Coalson6b21f662006-09-13 01:42:27 +00001248 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1249 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1250
1251 /* double protection */
1252 if(file == 0) {
1253 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1254 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1255 }
1256
Josh Coalson92f7fa92006-10-09 05:34:21 +00001257 /*
1258 * To make sure that our file does not go unclosed after an error, we
1259 * must assign the FILE pointer before any further error can occur in
1260 * this routine.
1261 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001262 if(file == stdout)
1263 file = get_binary_stdout_(); /* just to be safe */
1264
1265 encoder->private_->file = file;
1266
1267 encoder->private_->progress_callback = progress_callback;
1268 encoder->private_->bytes_written = 0;
1269 encoder->private_->samples_written = 0;
1270 encoder->private_->frames_written = 0;
1271
Josh Coalson8da98c82006-10-15 04:24:05 +00001272 init_status = init_stream_internal_(
1273 encoder,
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001274 encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
Josh Coalson8da98c82006-10-15 04:24:05 +00001275 file_write_callback_,
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001276 encoder->private_->file == stdout? 0 : file_seek_callback_,
1277 encoder->private_->file == stdout? 0 : file_tell_callback_,
Josh Coalson8da98c82006-10-15 04:24:05 +00001278 /*metadata_callback=*/0,
1279 client_data,
1280 is_ogg
1281 );
Josh Coalson6b21f662006-09-13 01:42:27 +00001282 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1283 /* the above function sets the state for us in case of an error */
1284 return init_status;
1285 }
1286
1287 {
1288 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1289
1290 FLAC__ASSERT(blocksize != 0);
1291 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1292 }
1293
1294 return init_status;
1295}
Josh Coalson8da98c82006-10-15 04:24:05 +00001296
1297FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1298 FLAC__StreamEncoder *encoder,
1299 FILE *file,
1300 FLAC__StreamEncoderProgressCallback progress_callback,
1301 void *client_data
1302)
1303{
1304 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1305}
1306
1307FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1308 FLAC__StreamEncoder *encoder,
1309 FILE *file,
1310 FLAC__StreamEncoderProgressCallback progress_callback,
1311 void *client_data
1312)
1313{
1314 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1315}
Josh Coalson6b21f662006-09-13 01:42:27 +00001316
Josh Coalson8da98c82006-10-15 04:24:05 +00001317static FLAC__StreamEncoderInitStatus init_file_internal_(
1318 FLAC__StreamEncoder *encoder,
1319 const char *filename,
1320 FLAC__StreamEncoderProgressCallback progress_callback,
1321 void *client_data,
1322 FLAC__bool is_ogg
1323)
Josh Coalson6b21f662006-09-13 01:42:27 +00001324{
1325 FILE *file;
1326
1327 FLAC__ASSERT(0 != encoder);
1328
1329 /*
1330 * To make sure that our file does not go unclosed after an error, we
1331 * have to do the same entrance checks here that are later performed
1332 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1333 */
1334 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1335 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1336
1337 file = filename? fopen(filename, "w+b") : stdout;
1338
1339 if(file == 0) {
1340 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1341 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1342 }
1343
Josh Coalson8da98c82006-10-15 04:24:05 +00001344 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1345}
1346
1347FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1348 FLAC__StreamEncoder *encoder,
1349 const char *filename,
1350 FLAC__StreamEncoderProgressCallback progress_callback,
1351 void *client_data
1352)
1353{
1354 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1355}
1356
1357FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1358 FLAC__StreamEncoder *encoder,
1359 const char *filename,
1360 FLAC__StreamEncoderProgressCallback progress_callback,
1361 void *client_data
1362)
1363{
1364 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001365}
1366
Josh Coalsona5862262006-11-09 06:58:26 +00001367FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001368{
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001369 FLAC__bool error = false;
Josh Coalsona5862262006-11-09 06:58:26 +00001370
Josh Coalsonf1eff452002-07-31 07:05:33 +00001371 FLAC__ASSERT(0 != encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001372 FLAC__ASSERT(0 != encoder->private_);
1373 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001374
Josh Coalsonfa697a92001-08-16 20:07:29 +00001375 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsona5862262006-11-09 06:58:26 +00001376 return true;
Josh Coalson2b245f22002-08-07 17:10:50 +00001377
Josh Coalson3262b0d2002-08-14 20:58:42 +00001378 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001379 if(encoder->private_->current_sample_number != 0) {
Josh Coalson49f2f162006-11-09 16:54:52 +00001380 const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
Josh Coalson2b245f22002-08-07 17:10:50 +00001381 encoder->protected_->blocksize = encoder->private_->current_sample_number;
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001382 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1383 error = true;
Josh Coalson2b245f22002-08-07 17:10:50 +00001384 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001385 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001386
Josh Coalson6b21f662006-09-13 01:42:27 +00001387 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001388
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001389 if(!encoder->private_->is_being_deleted) {
1390 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1391 if(encoder->private_->seek_callback) {
Josh Coalson8da98c82006-10-15 04:24:05 +00001392#if FLAC__HAS_OGG
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001393 if(encoder->private_->is_ogg)
1394 update_ogg_metadata_(encoder);
1395 else
Josh Coalson8da98c82006-10-15 04:24:05 +00001396#endif
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001397 update_metadata_(encoder);
Josh Coalson0a15c142001-06-13 17:59:57 +00001398
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001399 /* check if an error occurred while updating metadata */
1400 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1401 error = true;
1402 }
1403 if(encoder->private_->metadata_callback)
1404 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1405 }
1406
1407 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1408 if(!error)
1409 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1410 error = true;
1411 }
1412 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001413
Josh Coalson6b21f662006-09-13 01:42:27 +00001414 if(0 != encoder->private_->file) {
1415 if(encoder->private_->file != stdout)
1416 fclose(encoder->private_->file);
1417 encoder->private_->file = 0;
1418 }
1419
Josh Coalson8da98c82006-10-15 04:24:05 +00001420#if FLAC__HAS_OGG
1421 if(encoder->private_->is_ogg)
1422 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1423#endif
1424
Josh Coalsonf1eff452002-07-31 07:05:33 +00001425 free_(encoder);
1426 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001427
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001428 if(!error)
Josh Coalsona5862262006-11-09 06:58:26 +00001429 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1430
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001431 return !error;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001432}
1433
Josh Coalson71d5c252006-10-15 06:04:55 +00001434FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
Josh Coalson8da98c82006-10-15 04:24:05 +00001435{
1436 FLAC__ASSERT(0 != encoder);
1437 FLAC__ASSERT(0 != encoder->private_);
1438 FLAC__ASSERT(0 != encoder->protected_);
1439 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1440 return false;
Josh Coalsonf1ac7d92006-11-16 07:20:09 +00001441#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00001442 /* can't check encoder->private_->is_ogg since that's not set until init time */
1443 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1444 return true;
1445#else
1446 (void)value;
1447 return false;
1448#endif
1449}
1450
Josh Coalson6afed9f2002-10-16 22:29:47 +00001451FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001452{
1453 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001454 FLAC__ASSERT(0 != encoder->private_);
1455 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001456 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1457 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001458#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001459 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001460#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001461 return true;
1462}
1463
Josh Coalson6afed9f2002-10-16 22:29:47 +00001464FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001465{
Josh Coalson92031602002-07-24 06:02:11 +00001466 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001467 FLAC__ASSERT(0 != encoder->private_);
1468 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001469 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001470 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001471 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001472 return true;
1473}
1474
Josh Coalson6afed9f2002-10-16 22:29:47 +00001475FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001476{
Josh Coalson92031602002-07-24 06:02:11 +00001477 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001478 FLAC__ASSERT(0 != encoder->private_);
1479 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001480 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001481 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001482 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001483 return true;
1484}
1485
Josh Coalson6afed9f2002-10-16 22:29:47 +00001486FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001487{
Josh Coalson92031602002-07-24 06:02:11 +00001488 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001489 FLAC__ASSERT(0 != encoder->private_);
1490 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001491 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001492 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001493 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001494 return true;
1495}
1496
Josh Coalson6afed9f2002-10-16 22:29:47 +00001497FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001498{
Josh Coalson92031602002-07-24 06:02:11 +00001499 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001500 FLAC__ASSERT(0 != encoder->private_);
1501 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001502 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001503 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001504 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001505 return true;
1506}
1507
Josh Coalson425609c2006-11-03 16:08:52 +00001508FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1509{
1510 FLAC__bool ok = true;
1511 FLAC__ASSERT(0 != encoder);
1512 FLAC__ASSERT(0 != encoder->private_);
1513 FLAC__ASSERT(0 != encoder->protected_);
1514 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1515 return false;
1516 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1517 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1518 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1519 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
1520 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1521 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1522 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1523 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1524 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1525 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1526 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1527 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1528 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1529 return ok;
1530}
1531
Josh Coalson6afed9f2002-10-16 22:29:47 +00001532FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001533{
Josh Coalson92031602002-07-24 06:02:11 +00001534 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001535 FLAC__ASSERT(0 != encoder->private_);
1536 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001537 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001538 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001539 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001540 return true;
1541}
1542
Josh Coalson425609c2006-11-03 16:08:52 +00001543FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1544{
1545 FLAC__ASSERT(0 != encoder);
1546 FLAC__ASSERT(0 != encoder->private_);
1547 FLAC__ASSERT(0 != encoder->protected_);
1548 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1549 return false;
1550 encoder->protected_->do_mid_side_stereo = value;
1551 return true;
1552}
1553
1554FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1555{
1556 FLAC__ASSERT(0 != encoder);
1557 FLAC__ASSERT(0 != encoder->private_);
1558 FLAC__ASSERT(0 != encoder->protected_);
1559 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1560 return false;
1561 encoder->protected_->loose_mid_side_stereo = value;
1562 return true;
1563}
1564
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001565FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1566{
1567 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001568 FLAC__ASSERT(0 != encoder->private_);
1569 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001570 FLAC__ASSERT(0 != specification);
1571 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1572 return false;
1573#ifdef FLAC__INTEGER_ONLY_LIBRARY
1574 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1575#else
1576 encoder->protected_->num_apodizations = 0;
1577 while(1) {
1578 const char *s = strchr(specification, ';');
1579 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1580 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1581 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1582 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1583 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1584 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1585 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1586 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1587 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1588 else if(n==6 && 0 == strncmp("connes" , specification, n))
1589 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1590 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1591 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1592 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1593 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1594 if (stddev > 0.0 && stddev <= 0.5) {
1595 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1596 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1597 }
1598 }
1599 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1600 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1601 else if(n==4 && 0 == strncmp("hann" , specification, n))
1602 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1603 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1604 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1605 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1606 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1607 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1608 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1609 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1610 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1611 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1612 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1613 if (p >= 0.0 && p <= 1.0) {
1614 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1615 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1616 }
1617 }
1618 else if(n==5 && 0 == strncmp("welch" , specification, n))
1619 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1620 if (encoder->protected_->num_apodizations == 32)
1621 break;
1622 if (s)
1623 specification = s+1;
1624 else
1625 break;
1626 }
1627 if(encoder->protected_->num_apodizations == 0) {
1628 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001629 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1630 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001631 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001632#endif
1633 return true;
1634}
1635
Josh Coalson6afed9f2002-10-16 22:29:47 +00001636FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001637{
Josh Coalson92031602002-07-24 06:02:11 +00001638 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001639 FLAC__ASSERT(0 != encoder->private_);
1640 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001641 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001642 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001643 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001644 return true;
1645}
1646
Josh Coalson6afed9f2002-10-16 22:29:47 +00001647FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001648{
Josh Coalson92031602002-07-24 06:02:11 +00001649 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001650 FLAC__ASSERT(0 != encoder->private_);
1651 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001652 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001653 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001654 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001655 return true;
1656}
1657
Josh Coalson6afed9f2002-10-16 22:29:47 +00001658FLAC_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 +00001659{
Josh Coalson92031602002-07-24 06:02:11 +00001660 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001661 FLAC__ASSERT(0 != encoder->private_);
1662 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001663 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001664 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001665 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001666 return true;
1667}
1668
Josh Coalson6afed9f2002-10-16 22:29:47 +00001669FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001670{
Josh Coalson92031602002-07-24 06:02:11 +00001671 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001672 FLAC__ASSERT(0 != encoder->private_);
1673 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001674 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001675 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001676#if 0
1677 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001678 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001679#else
1680 (void)value;
1681#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001682 return true;
1683}
1684
Josh Coalson6afed9f2002-10-16 22:29:47 +00001685FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001686{
Josh Coalson92031602002-07-24 06:02:11 +00001687 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001688 FLAC__ASSERT(0 != encoder->private_);
1689 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001690 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001691 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001692 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001693 return true;
1694}
1695
Josh Coalson6afed9f2002-10-16 22:29:47 +00001696FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001697{
Josh Coalson92031602002-07-24 06:02:11 +00001698 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001699 FLAC__ASSERT(0 != encoder->private_);
1700 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001701 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001702 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001703 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001704 return true;
1705}
1706
Josh Coalson6afed9f2002-10-16 22:29:47 +00001707FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001708{
Josh Coalson92031602002-07-24 06:02:11 +00001709 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001710 FLAC__ASSERT(0 != encoder->private_);
1711 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001712 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001713 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001714 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001715 return true;
1716}
1717
Josh Coalson6afed9f2002-10-16 22:29:47 +00001718FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001719{
Josh Coalson92031602002-07-24 06:02:11 +00001720 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001721 FLAC__ASSERT(0 != encoder->private_);
1722 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001723 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001724 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001725#if 0
1726 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001727 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001728#else
1729 (void)value;
1730#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001731 return true;
1732}
1733
Josh Coalson6afed9f2002-10-16 22:29:47 +00001734FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001735{
Josh Coalson92031602002-07-24 06:02:11 +00001736 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001737 FLAC__ASSERT(0 != encoder->private_);
1738 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001739 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001740 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001741 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001742 return true;
1743}
1744
Josh Coalson6afed9f2002-10-16 22:29:47 +00001745FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001746{
Josh Coalson92031602002-07-24 06:02:11 +00001747 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001748 FLAC__ASSERT(0 != encoder->private_);
1749 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001750 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001751 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001752 encoder->protected_->metadata = metadata;
1753 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson8da98c82006-10-15 04:24:05 +00001754#if FLAC__HAS_OGG
1755 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1756 return false;
1757#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001758 return true;
1759}
1760
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001761/*
1762 * These three functions are not static, but not publically exposed in
1763 * include/FLAC/ either. They are used by the test suite.
1764 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001765FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001766{
1767 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001768 FLAC__ASSERT(0 != encoder->private_);
1769 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001770 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1771 return false;
1772 encoder->private_->disable_constant_subframes = value;
1773 return true;
1774}
1775
Josh Coalson6afed9f2002-10-16 22:29:47 +00001776FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001777{
1778 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001779 FLAC__ASSERT(0 != encoder->private_);
1780 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001781 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1782 return false;
1783 encoder->private_->disable_fixed_subframes = value;
1784 return true;
1785}
1786
Josh Coalson6afed9f2002-10-16 22:29:47 +00001787FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001788{
1789 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001790 FLAC__ASSERT(0 != encoder->private_);
1791 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001792 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1793 return false;
1794 encoder->private_->disable_verbatim_subframes = value;
1795 return true;
1796}
1797
Josh Coalson6afed9f2002-10-16 22:29:47 +00001798FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001799{
Josh Coalson92031602002-07-24 06:02:11 +00001800 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001801 FLAC__ASSERT(0 != encoder->private_);
1802 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001803 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001804}
1805
Josh Coalson6afed9f2002-10-16 22:29:47 +00001806FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001807{
1808 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001809 FLAC__ASSERT(0 != encoder->private_);
1810 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001811 if(encoder->protected_->verify)
1812 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1813 else
1814 return FLAC__STREAM_DECODER_UNINITIALIZED;
1815}
1816
Josh Coalson02954222002-11-08 06:16:31 +00001817FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1818{
Josh Coalson8da98c82006-10-15 04:24:05 +00001819 FLAC__ASSERT(0 != encoder);
1820 FLAC__ASSERT(0 != encoder->private_);
1821 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson02954222002-11-08 06:16:31 +00001822 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1823 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1824 else
Josh Coalson807140d2003-09-24 22:10:51 +00001825 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001826}
1827
Josh Coalson6afed9f2002-10-16 22:29:47 +00001828FLAC_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 +00001829{
1830 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001831 FLAC__ASSERT(0 != encoder->private_);
1832 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson589f8c72002-08-07 23:54:55 +00001833 if(0 != absolute_sample)
1834 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1835 if(0 != frame_number)
1836 *frame_number = encoder->private_->verify.error_stats.frame_number;
1837 if(0 != channel)
1838 *channel = encoder->private_->verify.error_stats.channel;
1839 if(0 != sample)
1840 *sample = encoder->private_->verify.error_stats.sample;
1841 if(0 != expected)
1842 *expected = encoder->private_->verify.error_stats.expected;
1843 if(0 != got)
1844 *got = encoder->private_->verify.error_stats.got;
1845}
1846
Josh Coalson6afed9f2002-10-16 22:29:47 +00001847FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001848{
1849 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001850 FLAC__ASSERT(0 != encoder->private_);
1851 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001852 return encoder->protected_->verify;
1853}
1854
Josh Coalson6afed9f2002-10-16 22:29:47 +00001855FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001856{
Josh Coalson92031602002-07-24 06:02:11 +00001857 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001858 FLAC__ASSERT(0 != encoder->private_);
1859 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001860 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001861}
1862
Josh Coalson6afed9f2002-10-16 22:29:47 +00001863FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001864{
Josh Coalson92031602002-07-24 06:02:11 +00001865 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001866 FLAC__ASSERT(0 != encoder->private_);
1867 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001868 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001869}
1870
Josh Coalson6afed9f2002-10-16 22:29:47 +00001871FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001872{
Josh Coalson92031602002-07-24 06:02:11 +00001873 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001874 FLAC__ASSERT(0 != encoder->private_);
1875 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001876 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001877}
1878
Josh Coalson6afed9f2002-10-16 22:29:47 +00001879FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001880{
Josh Coalson92031602002-07-24 06:02:11 +00001881 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001882 FLAC__ASSERT(0 != encoder->private_);
1883 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001884 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001885}
1886
Josh Coalson6afed9f2002-10-16 22:29:47 +00001887FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001888{
Josh Coalson92031602002-07-24 06:02:11 +00001889 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001890 FLAC__ASSERT(0 != encoder->private_);
1891 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001892 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001893}
1894
Josh Coalson425609c2006-11-03 16:08:52 +00001895FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1896{
1897 FLAC__ASSERT(0 != encoder);
1898 FLAC__ASSERT(0 != encoder->private_);
1899 FLAC__ASSERT(0 != encoder->protected_);
1900 return encoder->protected_->do_mid_side_stereo;
1901}
1902
1903FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1904{
1905 FLAC__ASSERT(0 != encoder);
1906 FLAC__ASSERT(0 != encoder->private_);
1907 FLAC__ASSERT(0 != encoder->protected_);
1908 return encoder->protected_->loose_mid_side_stereo;
1909}
1910
Josh Coalson6afed9f2002-10-16 22:29:47 +00001911FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001912{
Josh Coalson92031602002-07-24 06:02:11 +00001913 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001914 FLAC__ASSERT(0 != encoder->private_);
1915 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001916 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001917}
1918
Josh Coalson6afed9f2002-10-16 22:29:47 +00001919FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001920{
Josh Coalson92031602002-07-24 06:02:11 +00001921 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001922 FLAC__ASSERT(0 != encoder->private_);
1923 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001924 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001925}
1926
Josh Coalson6afed9f2002-10-16 22:29:47 +00001927FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001928{
Josh Coalson92031602002-07-24 06:02:11 +00001929 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001930 FLAC__ASSERT(0 != encoder->private_);
1931 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001932 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001933}
1934
Josh Coalson6afed9f2002-10-16 22:29:47 +00001935FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001936{
Josh Coalson92031602002-07-24 06:02:11 +00001937 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001938 FLAC__ASSERT(0 != encoder->private_);
1939 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001940 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001941}
1942
Josh Coalson6afed9f2002-10-16 22:29:47 +00001943FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001944{
Josh Coalson92031602002-07-24 06:02:11 +00001945 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001946 FLAC__ASSERT(0 != encoder->private_);
1947 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001948 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001949}
1950
Josh Coalson6afed9f2002-10-16 22:29:47 +00001951FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001952{
Josh Coalson92031602002-07-24 06:02:11 +00001953 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001954 FLAC__ASSERT(0 != encoder->private_);
1955 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001956 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001957}
1958
Josh Coalson6afed9f2002-10-16 22:29:47 +00001959FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001960{
Josh Coalson92031602002-07-24 06:02:11 +00001961 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001962 FLAC__ASSERT(0 != encoder->private_);
1963 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001964 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001965}
1966
Josh Coalson6afed9f2002-10-16 22:29:47 +00001967FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001968{
Josh Coalson92031602002-07-24 06:02:11 +00001969 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001970 FLAC__ASSERT(0 != encoder->private_);
1971 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001972 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001973}
1974
Josh Coalson6afed9f2002-10-16 22:29:47 +00001975FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001976{
1977 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001978 FLAC__ASSERT(0 != encoder->private_);
1979 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001980 return encoder->protected_->total_samples_estimate;
1981}
1982
Josh Coalson6afed9f2002-10-16 22:29:47 +00001983FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001984{
1985 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001986 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001987 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001988
Josh Coalsonf1eff452002-07-31 07:05:33 +00001989 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001990 FLAC__ASSERT(0 != encoder->private_);
1991 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001992 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001993
1994 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001995 /*
1996 * we have several flavors of the same basic loop, optimized for
1997 * different conditions:
1998 */
1999 if(encoder->protected_->max_lpc_order > 0) {
2000 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2001 /*
2002 * stereo coding: unroll channel loop
2003 * with LPC: calculate floating point version of signal
2004 */
2005 do {
2006 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002007 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 +00002008
Josh Coalson49f2f162006-11-09 16:54:52 +00002009 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2010 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002011 x = mid = side = buffer[0][j];
2012 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002013#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002014 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002015#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002016 x = buffer[1][j];
2017 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002018#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002019 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002020#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002021 mid += x;
2022 side -= x;
2023 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2024 encoder->private_->integer_signal_mid_side[1][i] = side;
2025 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002026#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002027 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2028 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002029#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002030 encoder->private_->current_sample_number++;
2031 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002032 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2033 if(i > blocksize) {
2034 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002035 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002036 /* move unprocessed overread samples to beginnings of arrays */
2037 FLAC__ASSERT(i == blocksize+OVERREAD_);
2038 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2039 i--;
2040 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2041 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2042 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2043 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2044#ifndef FLAC__INTEGER_ONLY_LIBRARY
2045 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2046 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2047 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2048 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2049#endif
2050 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002051 }
2052 } while(j < samples);
2053 }
2054 else {
2055 /*
2056 * independent channel coding: buffer each channel in inner loop
2057 * with LPC: calculate floating point version of signal
2058 */
2059 do {
2060 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002061 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 +00002062
Josh Coalson49f2f162006-11-09 16:54:52 +00002063 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2064 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002065 for(channel = 0; channel < channels; channel++) {
2066 x = buffer[channel][j];
2067 encoder->private_->integer_signal[channel][i] = x;
2068#ifndef FLAC__INTEGER_ONLY_LIBRARY
2069 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2070#endif
2071 }
2072 encoder->private_->current_sample_number++;
2073 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002074 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2075 if(i > blocksize) {
2076 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002077 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002078 /* move unprocessed overread samples to beginnings of arrays */
2079 FLAC__ASSERT(i == blocksize+OVERREAD_);
2080 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2081 i--;
2082 for(channel = 0; channel < channels; channel++) {
2083 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2084#ifndef FLAC__INTEGER_ONLY_LIBRARY
2085 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2086#endif
2087 }
2088 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002089 }
2090 } while(j < samples);
2091 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002092 }
2093 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002094 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2095 /*
2096 * stereo coding: unroll channel loop
2097 * without LPC: no need to calculate floating point version of signal
2098 */
2099 do {
2100 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002101 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 +00002102
Josh Coalson49f2f162006-11-09 16:54:52 +00002103 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2104 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002105 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
2106 x = buffer[1][j];
2107 encoder->private_->integer_signal[1][i] = x;
2108 mid += x;
2109 side -= x;
2110 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2111 encoder->private_->integer_signal_mid_side[1][i] = side;
2112 encoder->private_->integer_signal_mid_side[0][i] = mid;
2113 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002114 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002115 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2116 if(i > blocksize) {
2117 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002118 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002119 /* move unprocessed overread samples to beginnings of arrays */
2120 FLAC__ASSERT(i == blocksize+OVERREAD_);
2121 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2122 i--;
2123 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2124 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2125 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2126 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2127 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002128 }
2129 } while(j < samples);
2130 }
2131 else {
2132 /*
2133 * independent channel coding: buffer each channel in inner loop
2134 * without LPC: no need to calculate floating point version of signal
2135 */
2136 do {
2137 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002138 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 +00002139
Josh Coalson49f2f162006-11-09 16:54:52 +00002140 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2141 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002142 for(channel = 0; channel < channels; channel++)
2143 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2144 encoder->private_->current_sample_number++;
2145 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002146 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2147 if(i > blocksize) {
2148 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002149 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002150 /* move unprocessed overread samples to beginnings of arrays */
2151 FLAC__ASSERT(i == blocksize+OVERREAD_);
2152 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2153 i--;
2154 for(channel = 0; channel < channels; channel++)
2155 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2156 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002157 }
2158 } while(j < samples);
2159 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002160 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002161
2162 return true;
2163}
2164
Josh Coalson6afed9f2002-10-16 22:29:47 +00002165FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002166{
2167 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00002168 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002169 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002170
Josh Coalsonf1eff452002-07-31 07:05:33 +00002171 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002172 FLAC__ASSERT(0 != encoder->private_);
2173 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002174 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002175
2176 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002177 /*
2178 * we have several flavors of the same basic loop, optimized for
2179 * different conditions:
2180 */
2181 if(encoder->protected_->max_lpc_order > 0) {
2182 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2183 /*
2184 * stereo coding: unroll channel loop
2185 * with LPC: calculate floating point version of signal
2186 */
2187 do {
2188 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002189 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 +00002190
Josh Coalson49f2f162006-11-09 16:54:52 +00002191 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2192 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002193 x = mid = side = buffer[k++];
2194 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002195#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002196 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002197#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002198 x = buffer[k++];
2199 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002200#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002201 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002202#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002203 mid += x;
2204 side -= x;
2205 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2206 encoder->private_->integer_signal_mid_side[1][i] = side;
2207 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002208#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002209 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2210 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002211#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002212 encoder->private_->current_sample_number++;
2213 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002214 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2215 if(i > blocksize) {
2216 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002217 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002218 /* move unprocessed overread samples to beginnings of arrays */
2219 FLAC__ASSERT(i == blocksize+OVERREAD_);
2220 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2221 i--;
2222 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2223 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2224 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2225 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2226#ifndef FLAC__INTEGER_ONLY_LIBRARY
2227 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2228 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2229 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2230 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2231#endif
2232 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002233 }
2234 } while(j < samples);
2235 }
2236 else {
2237 /*
2238 * independent channel coding: buffer each channel in inner loop
2239 * with LPC: calculate floating point version of signal
2240 */
2241 do {
2242 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002243 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 +00002244
Josh Coalson49f2f162006-11-09 16:54:52 +00002245 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2246 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002247 for(channel = 0; channel < channels; channel++) {
2248 x = buffer[k++];
2249 encoder->private_->integer_signal[channel][i] = x;
2250#ifndef FLAC__INTEGER_ONLY_LIBRARY
2251 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2252#endif
2253 }
2254 encoder->private_->current_sample_number++;
2255 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002256 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2257 if(i > blocksize) {
2258 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002259 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002260 /* move unprocessed overread samples to beginnings of arrays */
2261 FLAC__ASSERT(i == blocksize+OVERREAD_);
2262 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2263 i--;
2264 for(channel = 0; channel < channels; channel++) {
2265 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2266#ifndef FLAC__INTEGER_ONLY_LIBRARY
2267 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2268#endif
2269 }
2270 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002271 }
2272 } while(j < samples);
2273 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002274 }
2275 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002276 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2277 /*
2278 * stereo coding: unroll channel loop
2279 * without LPC: no need to calculate floating point version of signal
2280 */
2281 do {
2282 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002283 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 +00002284
Josh Coalson49f2f162006-11-09 16:54:52 +00002285 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2286 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002287 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00002288 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002289 encoder->private_->integer_signal[1][i] = x;
2290 mid += x;
2291 side -= x;
2292 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2293 encoder->private_->integer_signal_mid_side[1][i] = side;
2294 encoder->private_->integer_signal_mid_side[0][i] = mid;
2295 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002296 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002297 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2298 if(i > blocksize) {
2299 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002300 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002301 /* move unprocessed overread samples to beginnings of arrays */
2302 FLAC__ASSERT(i == blocksize+OVERREAD_);
2303 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2304 i--;
2305 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2306 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2307 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2308 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2309 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002310 }
2311 } while(j < samples);
2312 }
2313 else {
2314 /*
2315 * independent channel coding: buffer each channel in inner loop
2316 * without LPC: no need to calculate floating point version of signal
2317 */
2318 do {
2319 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002320 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 +00002321
Josh Coalson49f2f162006-11-09 16:54:52 +00002322 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2323 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002324 for(channel = 0; channel < channels; channel++)
2325 encoder->private_->integer_signal[channel][i] = buffer[k++];
2326 encoder->private_->current_sample_number++;
2327 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002328 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2329 if(i > blocksize) {
2330 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002331 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002332 /* move unprocessed overread samples to beginnings of arrays */
2333 FLAC__ASSERT(i == blocksize+OVERREAD_);
2334 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2335 i--;
2336 for(channel = 0; channel < channels; channel++)
2337 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2338 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002339 }
2340 } while(j < samples);
2341 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002342 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002343
2344 return true;
2345}
2346
Josh Coalsonf1eff452002-07-31 07:05:33 +00002347/***********************************************************************
2348 *
2349 * Private class methods
2350 *
2351 ***********************************************************************/
2352
2353void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00002354{
2355 FLAC__ASSERT(0 != encoder);
2356
Josh Coalson47c7b142005-01-29 06:08:58 +00002357#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2358 encoder->protected_->verify = true;
2359#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00002360 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00002361#endif
Josh Coalson92031602002-07-24 06:02:11 +00002362 encoder->protected_->streamable_subset = true;
2363 encoder->protected_->do_mid_side_stereo = false;
2364 encoder->protected_->loose_mid_side_stereo = false;
2365 encoder->protected_->channels = 2;
2366 encoder->protected_->bits_per_sample = 16;
2367 encoder->protected_->sample_rate = 44100;
Josh Coalson425609c2006-11-03 16:08:52 +00002368 encoder->protected_->blocksize = 0;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002369#ifndef FLAC__INTEGER_ONLY_LIBRARY
2370 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00002371 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2372 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002373#endif
Josh Coalson92031602002-07-24 06:02:11 +00002374 encoder->protected_->max_lpc_order = 0;
2375 encoder->protected_->qlp_coeff_precision = 0;
2376 encoder->protected_->do_qlp_coeff_prec_search = false;
2377 encoder->protected_->do_exhaustive_model_search = false;
2378 encoder->protected_->do_escape_coding = false;
2379 encoder->protected_->min_residual_partition_order = 0;
2380 encoder->protected_->max_residual_partition_order = 0;
2381 encoder->protected_->rice_parameter_search_dist = 0;
2382 encoder->protected_->total_samples_estimate = 0;
2383 encoder->protected_->metadata = 0;
2384 encoder->protected_->num_metadata_blocks = 0;
2385
Josh Coalson6b21f662006-09-13 01:42:27 +00002386 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002387 encoder->private_->disable_constant_subframes = false;
2388 encoder->private_->disable_fixed_subframes = false;
2389 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002390#if FLAC__HAS_OGG
2391 encoder->private_->is_ogg = false;
2392#endif
2393 encoder->private_->read_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002394 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002395 encoder->private_->seek_callback = 0;
2396 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002397 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002398 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002399 encoder->private_->client_data = 0;
Josh Coalson8da98c82006-10-15 04:24:05 +00002400
2401#if FLAC__HAS_OGG
2402 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2403#endif
Josh Coalson92031602002-07-24 06:02:11 +00002404}
2405
Josh Coalsonf1eff452002-07-31 07:05:33 +00002406void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00002407{
2408 unsigned i, channel;
2409
Josh Coalsonf1eff452002-07-31 07:05:33 +00002410 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00002411 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002412 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002413 free(encoder->private_->integer_signal_unaligned[i]);
2414 encoder->private_->integer_signal_unaligned[i] = 0;
2415 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002416#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002417 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002418 free(encoder->private_->real_signal_unaligned[i]);
2419 encoder->private_->real_signal_unaligned[i] = 0;
2420 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002421#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002422 }
2423 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002424 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002425 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2426 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2427 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002428#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002429 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002430 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2431 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2432 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002433#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002434 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002435#ifndef FLAC__INTEGER_ONLY_LIBRARY
2436 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2437 if(0 != encoder->private_->window_unaligned[i]) {
2438 free(encoder->private_->window_unaligned[i]);
2439 encoder->private_->window_unaligned[i] = 0;
2440 }
2441 }
2442 if(0 != encoder->private_->windowed_signal_unaligned) {
2443 free(encoder->private_->windowed_signal_unaligned);
2444 encoder->private_->windowed_signal_unaligned = 0;
2445 }
2446#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002447 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2448 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002449 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002450 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2451 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2452 }
2453 }
2454 }
2455 for(channel = 0; channel < 2; channel++) {
2456 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002457 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002458 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2459 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2460 }
2461 }
2462 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002463 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002464 free(encoder->private_->abs_residual_unaligned);
2465 encoder->private_->abs_residual_unaligned = 0;
2466 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002467 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002468 free(encoder->private_->abs_residual_partition_sums_unaligned);
2469 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2470 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002471 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002472 free(encoder->private_->raw_bits_per_partition_unaligned);
2473 encoder->private_->raw_bits_per_partition_unaligned = 0;
2474 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002475 if(encoder->protected_->verify) {
2476 for(i = 0; i < encoder->protected_->channels; i++) {
2477 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2478 free(encoder->private_->verify.input_fifo.data[i]);
2479 encoder->private_->verify.input_fifo.data[i] = 0;
2480 }
2481 }
2482 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002483 FLAC__bitbuffer_free(encoder->private_->frame);
2484}
2485
Josh Coalson85aaed82006-11-09 01:19:13 +00002486FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002487{
Josh Coalson77e3f312001-06-23 03:03:24 +00002488 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002489 unsigned i, channel;
2490
Josh Coalson85aaed82006-11-09 01:19:13 +00002491 FLAC__ASSERT(new_blocksize > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002492 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2493 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002494
2495 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalson85aaed82006-11-09 01:19:13 +00002496 if(new_blocksize <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002497 return true;
2498
2499 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002500
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002501 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2502 * requires that the input arrays (in our case the integer signals)
2503 * have a buffer of up to 3 zeroes in front (at negative indices) for
Josh Coalson85aaed82006-11-09 01:19:13 +00002504 * alignment purposes; we use 4 in front to keep the data well-aligned.
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002505 */
Josh Coalson8395d022001-07-12 21:25:22 +00002506
Josh Coalsonfa697a92001-08-16 20:07:29 +00002507 for(i = 0; ok && i < encoder->protected_->channels; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002508 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 +00002509 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2510 encoder->private_->integer_signal[i] += 4;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002511#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002512 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002513 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 +00002514#endif
Josh Coalson85aaed82006-11-09 01:19:13 +00002515 }
2516 for(i = 0; ok && i < 2; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002517 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 +00002518 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2519 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson85aaed82006-11-09 01:19:13 +00002520#ifndef FLAC__INTEGER_ONLY_LIBRARY
2521 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002522 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 +00002523#endif
Josh Coalson0a15c142001-06-13 17:59:57 +00002524 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002525#ifndef FLAC__INTEGER_ONLY_LIBRARY
2526 if(ok && encoder->protected_->max_lpc_order > 0) {
2527 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
Josh Coalson85aaed82006-11-09 01:19:13 +00002528 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2529 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 +00002530 }
2531#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002532 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002533 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002534 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 +00002535 }
2536 }
2537 for(channel = 0; ok && channel < 2; channel++) {
2538 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002539 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 +00002540 }
2541 }
Josh Coalson85aaed82006-11-09 01:19:13 +00002542 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 +00002543 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 +00002544 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 +00002545 if(encoder->protected_->do_escape_coding)
Josh Coalson85aaed82006-11-09 01:19:13 +00002546 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 +00002547
Josh Coalson85aaed82006-11-09 01:19:13 +00002548 /* now adjust the windows if the blocksize has changed */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002549#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson85aaed82006-11-09 01:19:13 +00002550 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002551 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2552 switch(encoder->protected_->apodizations[i].type) {
2553 case FLAC__APODIZATION_BARTLETT:
Josh Coalson85aaed82006-11-09 01:19:13 +00002554 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002555 break;
2556 case FLAC__APODIZATION_BARTLETT_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002557 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002558 break;
2559 case FLAC__APODIZATION_BLACKMAN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002560 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002561 break;
2562 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002563 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002564 break;
2565 case FLAC__APODIZATION_CONNES:
Josh Coalson85aaed82006-11-09 01:19:13 +00002566 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002567 break;
2568 case FLAC__APODIZATION_FLATTOP:
Josh Coalson85aaed82006-11-09 01:19:13 +00002569 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002570 break;
2571 case FLAC__APODIZATION_GAUSS:
Josh Coalson85aaed82006-11-09 01:19:13 +00002572 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002573 break;
2574 case FLAC__APODIZATION_HAMMING:
Josh Coalson85aaed82006-11-09 01:19:13 +00002575 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002576 break;
2577 case FLAC__APODIZATION_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002578 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002579 break;
2580 case FLAC__APODIZATION_KAISER_BESSEL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002581 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002582 break;
2583 case FLAC__APODIZATION_NUTTALL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002584 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002585 break;
2586 case FLAC__APODIZATION_RECTANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002587 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002588 break;
2589 case FLAC__APODIZATION_TRIANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002590 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002591 break;
2592 case FLAC__APODIZATION_TUKEY:
Josh Coalson85aaed82006-11-09 01:19:13 +00002593 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002594 break;
2595 case FLAC__APODIZATION_WELCH:
Josh Coalson85aaed82006-11-09 01:19:13 +00002596 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002597 break;
2598 default:
2599 FLAC__ASSERT(0);
2600 /* double protection */
Josh Coalson85aaed82006-11-09 01:19:13 +00002601 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002602 break;
2603 }
2604 }
2605 }
2606#endif
2607
Josh Coalson85aaed82006-11-09 01:19:13 +00002608 if(ok)
2609 encoder->private_->input_capacity = new_blocksize;
2610 else
2611 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2612
Josh Coalson0a15c142001-06-13 17:59:57 +00002613 return ok;
2614}
2615
Josh Coalson49f2f162006-11-09 16:54:52 +00002616FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
Josh Coalson5c491a12002-08-01 06:39:40 +00002617{
2618 const FLAC__byte *buffer;
Josh Coalson352feb52006-10-15 17:08:52 +00002619 size_t bytes;
Josh Coalson5c491a12002-08-01 06:39:40 +00002620
2621 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2622
2623 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2624
Josh Coalsond86e03b2002-08-03 21:56:15 +00002625 if(encoder->protected_->verify) {
2626 encoder->private_->verify.output.data = buffer;
2627 encoder->private_->verify.output.bytes = bytes;
2628 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2629 encoder->private_->verify.needs_magic_hack = true;
2630 }
2631 else {
2632 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2633 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2634 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2635 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2636 return false;
2637 }
2638 }
2639 }
2640
Josh Coalson49f2f162006-11-09 16:54:52 +00002641 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002642 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002643 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002644 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002645 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002646
2647 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2648
Josh Coalsond86e03b2002-08-03 21:56:15 +00002649 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002650 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2651 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 +00002652 }
2653
Josh Coalson5c491a12002-08-01 06:39:40 +00002654 return true;
2655}
2656
Josh Coalson49f2f162006-11-09 16:54:52 +00002657FLAC__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 +00002658{
2659 FLAC__StreamEncoderWriteStatus status;
2660 FLAC__uint64 output_position = 0;
2661
2662 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2663 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2664 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2665 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2666 }
2667
2668 /*
2669 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2670 */
2671 if(samples == 0) {
2672 FLAC__MetadataType type = (buffer[0] & 0x7f);
2673 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2674 encoder->protected_->streaminfo_offset = output_position;
2675 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2676 encoder->protected_->seektable_offset = output_position;
2677 }
2678
2679 /*
2680 * Mark the current seek point if hit (if audio_offset == 0 that
2681 * means we're still writing metadata and haven't hit the first
2682 * frame yet)
2683 */
2684 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2685 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2686 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2687 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2688 FLAC__uint64 test_sample;
2689 unsigned i;
2690 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2691 test_sample = encoder->private_->seek_table->points[i].sample_number;
2692 if(test_sample > frame_last_sample) {
2693 break;
2694 }
2695 else if(test_sample >= frame_first_sample) {
2696 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2697 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2698 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2699 encoder->private_->first_seekpoint_to_check++;
2700 /* DO NOT: "break;" and here's why:
2701 * The seektable template may contain more than one target
2702 * sample for any given frame; we will keep looping, generating
2703 * duplicate seekpoints for them, and we'll clean it up later,
2704 * just before writing the seektable back to the metadata.
2705 */
2706 }
2707 else {
2708 encoder->private_->first_seekpoint_to_check++;
2709 }
2710 }
2711 }
2712
Josh Coalson8da98c82006-10-15 04:24:05 +00002713#if FLAC__HAS_OGG
2714 if(encoder->private_->is_ogg) {
2715 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2716 &encoder->protected_->ogg_encoder_aspect,
Josh Coalson8da98c82006-10-15 04:24:05 +00002717 buffer,
2718 bytes,
2719 samples,
2720 encoder->private_->current_frame_number,
Josh Coalson49f2f162006-11-09 16:54:52 +00002721 is_last_block,
Josh Coalson8da98c82006-10-15 04:24:05 +00002722 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2723 encoder,
2724 encoder->private_->client_data
2725 );
2726 }
2727 else
2728#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00002729 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2730
2731 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2732 encoder->private_->bytes_written += bytes;
2733 encoder->private_->samples_written += samples;
2734 /* we keep a high watermark on the number of frames written because
2735 * when the encoder goes back to write metadata, 'current_frame'
2736 * will drop back to 0.
2737 */
2738 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2739 }
2740 else
2741 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2742
2743 return status;
2744}
2745
2746/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2747void update_metadata_(const FLAC__StreamEncoder *encoder)
2748{
2749 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2750 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2751 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2752 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2753 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2754 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2755 FLAC__StreamEncoderSeekStatus seek_status;
2756
2757 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2758
2759 /* All this is based on intimate knowledge of the stream header
2760 * layout, but a change to the header format that would break this
2761 * would also break all streams encoded in the previous format.
2762 */
2763
2764 /*
2765 * Write MD5 signature
2766 */
2767 {
2768 const unsigned md5_offset =
2769 FLAC__STREAM_METADATA_HEADER_LENGTH +
2770 (
2771 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2772 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2773 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2774 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2775 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2776 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2777 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2778 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2779 ) / 8;
2780
2781 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2782 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2783 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2784 return;
2785 }
2786 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2787 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2788 return;
2789 }
2790 }
2791
2792 /*
2793 * Write total samples
2794 */
2795 {
2796 const unsigned total_samples_byte_offset =
2797 FLAC__STREAM_METADATA_HEADER_LENGTH +
2798 (
2799 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2800 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2801 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2802 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2803 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2804 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2805 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2806 - 4
2807 ) / 8;
2808
2809 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2810 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2811 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2812 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2813 b[4] = (FLAC__byte)(samples & 0xFF);
2814 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) {
2815 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2816 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2817 return;
2818 }
2819 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2820 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2821 return;
2822 }
2823 }
2824
2825 /*
2826 * Write min/max framesize
2827 */
2828 {
2829 const unsigned min_framesize_offset =
2830 FLAC__STREAM_METADATA_HEADER_LENGTH +
2831 (
2832 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2833 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2834 ) / 8;
2835
2836 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2837 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2838 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2839 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2840 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2841 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2842 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) {
2843 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2844 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2845 return;
2846 }
2847 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2848 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2849 return;
2850 }
2851 }
2852
2853 /*
2854 * Write seektable
2855 */
2856 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2857 unsigned i;
2858
2859 FLAC__format_seektable_sort(encoder->private_->seek_table);
2860
2861 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2862
2863 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) {
2864 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2865 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2866 return;
2867 }
2868
2869 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2870 FLAC__uint64 xx;
2871 unsigned x;
2872 xx = encoder->private_->seek_table->points[i].sample_number;
2873 b[7] = (FLAC__byte)xx; xx >>= 8;
2874 b[6] = (FLAC__byte)xx; xx >>= 8;
2875 b[5] = (FLAC__byte)xx; xx >>= 8;
2876 b[4] = (FLAC__byte)xx; xx >>= 8;
2877 b[3] = (FLAC__byte)xx; xx >>= 8;
2878 b[2] = (FLAC__byte)xx; xx >>= 8;
2879 b[1] = (FLAC__byte)xx; xx >>= 8;
2880 b[0] = (FLAC__byte)xx; xx >>= 8;
2881 xx = encoder->private_->seek_table->points[i].stream_offset;
2882 b[15] = (FLAC__byte)xx; xx >>= 8;
2883 b[14] = (FLAC__byte)xx; xx >>= 8;
2884 b[13] = (FLAC__byte)xx; xx >>= 8;
2885 b[12] = (FLAC__byte)xx; xx >>= 8;
2886 b[11] = (FLAC__byte)xx; xx >>= 8;
2887 b[10] = (FLAC__byte)xx; xx >>= 8;
2888 b[9] = (FLAC__byte)xx; xx >>= 8;
2889 b[8] = (FLAC__byte)xx; xx >>= 8;
2890 x = encoder->private_->seek_table->points[i].frame_samples;
2891 b[17] = (FLAC__byte)x; x >>= 8;
2892 b[16] = (FLAC__byte)x; x >>= 8;
2893 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2894 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2895 return;
2896 }
2897 }
2898 }
2899}
2900
Josh Coalson15b8eb82006-10-15 05:15:55 +00002901#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00002902/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2903void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2904{
Josh Coalsonc986d132006-11-15 08:53:32 +00002905 /* the # of bytes in the 1st packet that precede the STREAMINFO */
2906 static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2907 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2908 FLAC__OGG_MAPPING_MAGIC_LENGTH +
2909 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2910 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2911 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2912 FLAC__STREAM_SYNC_LENGTH
2913 ;
Josh Coalson8da98c82006-10-15 04:24:05 +00002914 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2915 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2916 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2917 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2918 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2919 ogg_page page;
2920
2921 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
Josh Coalsoncf1422e2006-11-16 01:32:25 +00002922 FLAC__ASSERT(0 != encoder->private_->seek_callback);
2923
2924 /* Pre-check that client supports seeking, since we don't want the
2925 * ogg_helper code to ever have to deal with this condition.
2926 */
2927 if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2928 return;
Josh Coalson8da98c82006-10-15 04:24:05 +00002929
2930 /* All this is based on intimate knowledge of the stream header
2931 * layout, but a change to the header format that would break this
2932 * would also break all streams encoded in the previous format.
2933 */
2934
2935 /**
2936 ** Write STREAMINFO stats
2937 **/
2938 simple_ogg_page__init(&page);
2939 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2940 simple_ogg_page__clear(&page);
2941 return; /* state already set */
2942 }
2943
2944 /*
2945 * Write MD5 signature
2946 */
2947 {
2948 const unsigned md5_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002949 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002950 FLAC__STREAM_METADATA_HEADER_LENGTH +
2951 (
2952 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2953 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2954 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2955 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2956 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2957 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2958 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2959 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2960 ) / 8;
2961
2962 if(md5_offset + 16 > (unsigned)page.body_len) {
2963 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2964 simple_ogg_page__clear(&page);
2965 return;
2966 }
2967 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2968 }
2969
2970 /*
2971 * Write total samples
2972 */
2973 {
2974 const unsigned total_samples_byte_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002975 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002976 FLAC__STREAM_METADATA_HEADER_LENGTH +
2977 (
2978 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2979 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2980 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2981 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2982 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2983 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2984 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2985 - 4
2986 ) / 8;
2987
2988 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2989 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2990 simple_ogg_page__clear(&page);
2991 return;
2992 }
2993 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2994 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2995 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2996 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2997 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2998 b[4] = (FLAC__byte)(samples & 0xFF);
2999 memcpy(page.body + total_samples_byte_offset, b, 5);
3000 }
3001
3002 /*
3003 * Write min/max framesize
3004 */
3005 {
3006 const unsigned min_framesize_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00003007 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00003008 FLAC__STREAM_METADATA_HEADER_LENGTH +
3009 (
3010 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
3011 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
3012 ) / 8;
3013
3014 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
3015 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3016 simple_ogg_page__clear(&page);
3017 return;
3018 }
3019 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
3020 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
3021 b[2] = (FLAC__byte)(min_framesize & 0xFF);
3022 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
3023 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
3024 b[5] = (FLAC__byte)(max_framesize & 0xFF);
3025 memcpy(page.body + min_framesize_offset, b, 6);
3026 }
3027 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3028 simple_ogg_page__clear(&page);
3029 return; /* state already set */
3030 }
3031 simple_ogg_page__clear(&page);
3032
3033 /*
3034 * Write seektable
3035 */
3036 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
3037 unsigned i;
3038 FLAC__byte *p;
3039
3040 FLAC__format_seektable_sort(encoder->private_->seek_table);
3041
3042 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3043
3044 simple_ogg_page__init(&page);
3045 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3046 simple_ogg_page__clear(&page);
3047 return; /* state already set */
3048 }
3049
Josh Coalsonc986d132006-11-15 08:53:32 +00003050 if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
Josh Coalson8da98c82006-10-15 04:24:05 +00003051 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3052 simple_ogg_page__clear(&page);
3053 return;
3054 }
3055
3056 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3057 FLAC__uint64 xx;
3058 unsigned x;
3059 xx = encoder->private_->seek_table->points[i].sample_number;
3060 b[7] = (FLAC__byte)xx; xx >>= 8;
3061 b[6] = (FLAC__byte)xx; xx >>= 8;
3062 b[5] = (FLAC__byte)xx; xx >>= 8;
3063 b[4] = (FLAC__byte)xx; xx >>= 8;
3064 b[3] = (FLAC__byte)xx; xx >>= 8;
3065 b[2] = (FLAC__byte)xx; xx >>= 8;
3066 b[1] = (FLAC__byte)xx; xx >>= 8;
3067 b[0] = (FLAC__byte)xx; xx >>= 8;
3068 xx = encoder->private_->seek_table->points[i].stream_offset;
3069 b[15] = (FLAC__byte)xx; xx >>= 8;
3070 b[14] = (FLAC__byte)xx; xx >>= 8;
3071 b[13] = (FLAC__byte)xx; xx >>= 8;
3072 b[12] = (FLAC__byte)xx; xx >>= 8;
3073 b[11] = (FLAC__byte)xx; xx >>= 8;
3074 b[10] = (FLAC__byte)xx; xx >>= 8;
3075 b[9] = (FLAC__byte)xx; xx >>= 8;
3076 b[8] = (FLAC__byte)xx; xx >>= 8;
3077 x = encoder->private_->seek_table->points[i].frame_samples;
3078 b[17] = (FLAC__byte)x; x >>= 8;
3079 b[16] = (FLAC__byte)x; x >>= 8;
Josh Coalson8da98c82006-10-15 04:24:05 +00003080 memcpy(p, b, 18);
3081 }
3082
3083 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3084 simple_ogg_page__clear(&page);
3085 return; /* state already set */
3086 }
3087 simple_ogg_page__clear(&page);
3088 }
3089}
Josh Coalson15b8eb82006-10-15 05:15:55 +00003090#endif
Josh Coalson8da98c82006-10-15 04:24:05 +00003091
Josh Coalson49f2f162006-11-09 16:54:52 +00003092FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
Josh Coalson0a15c142001-06-13 17:59:57 +00003093{
Josh Coalsonfa697a92001-08-16 20:07:29 +00003094 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003095
3096 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003097 * Accumulate raw signal to the MD5 signature
3098 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00003099 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 +00003100 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003101 return false;
3102 }
3103
3104 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003105 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003106 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003107 if(!process_subframes_(encoder, is_fractional_block)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003108 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003109 return false;
3110 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003111
3112 /*
3113 * Zero-pad the frame to a byte_boundary
3114 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003115 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003116 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003117 return false;
3118 }
3119
3120 /*
Josh Coalson215af572001-03-27 01:15:58 +00003121 * CRC-16 the whole thing
3122 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003123 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
3124 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 +00003125
3126 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003127 * Write it
3128 */
Josh Coalson49f2f162006-11-09 16:54:52 +00003129 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00003130 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003131 return false;
3132 }
3133
3134 /*
3135 * Get ready for the next frame
3136 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003137 encoder->private_->current_sample_number = 0;
3138 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00003139 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003140
3141 return true;
3142}
3143
Josh Coalson85aaed82006-11-09 01:19:13 +00003144FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003145{
3146 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003147 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003148 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003149
3150 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00003151 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00003152 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003153 if(is_fractional_block) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003154 max_partition_order = 0;
3155 }
3156 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003157 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
3158 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003159 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003160 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003161
Josh Coalsonfa697a92001-08-16 20:07:29 +00003162 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 +00003163
Josh Coalson94e02cd2001-01-25 10:41:06 +00003164 /*
3165 * Setup the frame
3166 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00003167 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003168 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003169 return false;
3170 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00003171 frame_header.blocksize = encoder->protected_->blocksize;
3172 frame_header.sample_rate = encoder->protected_->sample_rate;
3173 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003174 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003175 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003176 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003177 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003178
3179 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003180 * Figure out what channel assignments to try
3181 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003182 if(encoder->protected_->do_mid_side_stereo) {
3183 if(encoder->protected_->loose_mid_side_stereo) {
3184 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003185 do_independent = true;
3186 do_mid_side = true;
3187 }
3188 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003189 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003190 do_mid_side = !do_independent;
3191 }
3192 }
3193 else {
3194 do_independent = true;
3195 do_mid_side = true;
3196 }
3197 }
3198 else {
3199 do_independent = true;
3200 do_mid_side = false;
3201 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003202
Josh Coalson1b689822001-05-31 20:11:02 +00003203 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003204
3205 /*
Josh Coalson82b73242001-03-28 22:17:05 +00003206 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00003207 */
3208 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003209 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003210 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003211 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3212 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00003213 }
Josh Coalson859bc542001-03-27 22:22:27 +00003214 }
3215 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003216 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00003217 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003218 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003219 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3220 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00003221 }
Josh Coalson859bc542001-03-27 22:22:27 +00003222 }
3223
3224 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003225 * First do a normal encoding pass of each independent channel
3226 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003227 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003228 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003229 if(!
3230 process_subframe_(
3231 encoder,
3232 min_partition_order,
3233 max_partition_order,
3234 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003235 &frame_header,
3236 encoder->private_->subframe_bps[channel],
3237 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003238#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003239 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003240#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003241 encoder->private_->subframe_workspace_ptr[channel],
3242 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3243 encoder->private_->residual_workspace[channel],
3244 encoder->private_->best_subframe+channel,
3245 encoder->private_->best_subframe_bits+channel
3246 )
3247 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003248 return false;
3249 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003250 }
3251
3252 /*
3253 * Now do mid and side channels if requested
3254 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003255 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003256 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003257
3258 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003259 if(!
3260 process_subframe_(
3261 encoder,
3262 min_partition_order,
3263 max_partition_order,
3264 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003265 &frame_header,
3266 encoder->private_->subframe_bps_mid_side[channel],
3267 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003268#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003269 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003270#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003271 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3272 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3273 encoder->private_->residual_workspace_mid_side[channel],
3274 encoder->private_->best_subframe_mid_side+channel,
3275 encoder->private_->best_subframe_bits_mid_side+channel
3276 )
3277 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00003278 return false;
3279 }
3280 }
3281
3282 /*
3283 * Compose the frame bitbuffer
3284 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003285 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00003286 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3287 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003288 FLAC__ChannelAssignment channel_assignment;
3289
Josh Coalsonfa697a92001-08-16 20:07:29 +00003290 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003291
Josh Coalsonfa697a92001-08-16 20:07:29 +00003292 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3293 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 +00003294 }
3295 else {
3296 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3297 unsigned min_bits;
3298 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003299
Josh Coalson1b689822001-05-31 20:11:02 +00003300 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003301
3302 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003303 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3304 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3305 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3306 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 +00003307
Josh Coalson7424d2f2002-11-06 07:10:38 +00003308 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 +00003309 if(bits[ca] < min_bits) {
3310 min_bits = bits[ca];
3311 channel_assignment = ca;
3312 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003313 }
3314 }
3315
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003316 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003317
Josh Coalsond0edb972006-10-07 06:50:08 +00003318 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003319 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003320 return false;
3321 }
3322
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003323 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003324 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003325 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3326 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003327 break;
3328 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003329 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3330 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003331 break;
3332 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003333 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3334 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003335 break;
3336 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003337 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3338 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003339 break;
3340 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003341 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003342 }
Josh Coalson82b73242001-03-28 22:17:05 +00003343
3344 switch(channel_assignment) {
3345 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003346 left_bps = encoder->private_->subframe_bps [0];
3347 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003348 break;
3349 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003350 left_bps = encoder->private_->subframe_bps [0];
3351 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003352 break;
3353 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003354 left_bps = encoder->private_->subframe_bps_mid_side[1];
3355 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003356 break;
3357 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003358 left_bps = encoder->private_->subframe_bps_mid_side[0];
3359 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003360 break;
3361 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003362 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00003363 }
3364
3365 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00003366 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003367 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00003368 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003369 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003370 }
3371 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00003372 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003373 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003374 return false;
3375 }
3376
Josh Coalsonfa697a92001-08-16 20:07:29 +00003377 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson369a6da2006-10-10 00:37:48 +00003378 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 +00003379 /* the above function sets the state for us in case of an error */
3380 return false;
3381 }
3382 }
3383 }
3384
Josh Coalsonfa697a92001-08-16 20:07:29 +00003385 if(encoder->protected_->loose_mid_side_stereo) {
3386 encoder->private_->loose_mid_side_stereo_frame_count++;
3387 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3388 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003389 }
3390
Josh Coalsonfa697a92001-08-16 20:07:29 +00003391 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003392
Josh Coalson94e02cd2001-01-25 10:41:06 +00003393 return true;
3394}
3395
Josh Coalson6fe72f72002-08-20 04:01:59 +00003396FLAC__bool process_subframe_(
3397 FLAC__StreamEncoder *encoder,
3398 unsigned min_partition_order,
3399 unsigned max_partition_order,
3400 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003401 const FLAC__FrameHeader *frame_header,
3402 unsigned subframe_bps,
3403 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003404#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003405 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003406#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003407 FLAC__Subframe *subframe[2],
3408 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3409 FLAC__int32 *residual[2],
3410 unsigned *best_subframe,
3411 unsigned *best_bits
3412)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003413{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003414#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003415 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003416#else
3417 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3418#endif
3419#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003420 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003421 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 +00003422 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003423 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003424 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003425#endif
3426 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003427 unsigned rice_parameter;
3428 unsigned _candidate_bits, _best_bits;
3429 unsigned _best_subframe;
3430
3431 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3432 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003433 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3434 _best_bits = UINT_MAX;
3435 else
3436 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003437
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003438 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3439 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003440 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 +00003441 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003442 if(
3443 !encoder->private_->disable_constant_subframes &&
3444#ifndef FLAC__INTEGER_ONLY_LIBRARY
3445 fixed_residual_bits_per_sample[1] == 0.0
3446#else
3447 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3448#endif
3449 ) {
3450 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003451 unsigned i;
3452 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003453 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003454 if(integer_signal[0] != integer_signal[i]) {
3455 signal_is_constant = false;
3456 break;
3457 }
3458 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003459 }
3460 if(signal_is_constant) {
3461 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
3462 if(_candidate_bits < _best_bits) {
3463 _best_subframe = !_best_subframe;
3464 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003465 }
3466 }
3467 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003468 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3469 /* encode fixed */
3470 if(encoder->protected_->do_exhaustive_model_search) {
3471 min_fixed_order = 0;
3472 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00003473 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003474 else {
3475 min_fixed_order = max_fixed_order = guess_fixed_order;
3476 }
3477 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003478#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003479 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003480 continue; /* don't even try */
3481 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 +00003482#else
3483 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3484 continue; /* don't even try */
3485 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 */
3486#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003487 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003488 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3489#ifdef DEBUG_VERBOSE
3490 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3491#endif
3492 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3493 }
3494 _candidate_bits =
3495 evaluate_fixed_subframe_(
3496 encoder,
3497 integer_signal,
3498 residual[!_best_subframe],
3499 encoder->private_->abs_residual,
3500 encoder->private_->abs_residual_partition_sums,
3501 encoder->private_->raw_bits_per_partition,
3502 frame_header->blocksize,
3503 subframe_bps,
3504 fixed_order,
3505 rice_parameter,
3506 min_partition_order,
3507 max_partition_order,
3508 precompute_partition_sums,
3509 encoder->protected_->do_escape_coding,
3510 encoder->protected_->rice_parameter_search_dist,
3511 subframe[!_best_subframe],
3512 partitioned_rice_contents[!_best_subframe]
3513 );
3514 if(_candidate_bits < _best_bits) {
3515 _best_subframe = !_best_subframe;
3516 _best_bits = _candidate_bits;
3517 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003518 }
3519 }
3520
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003521#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00003522 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003523 if(encoder->protected_->max_lpc_order > 0) {
3524 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003525 max_lpc_order = frame_header->blocksize-1;
3526 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00003527 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003528 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003529 unsigned a;
3530 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00003531 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003532 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3533 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3534 if(autoc[0] != 0.0) {
3535 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3536 if(encoder->protected_->do_exhaustive_model_search) {
3537 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003538 }
3539 else {
Josh Coalsondf598452006-04-28 00:13:34 +00003540 const unsigned guess_lpc_order =
3541 FLAC__lpc_compute_best_order(
3542 lpc_error,
3543 max_lpc_order,
3544 frame_header->blocksize,
3545 subframe_bps + (
3546 encoder->protected_->do_qlp_coeff_prec_search?
3547 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3548 encoder->protected_->qlp_coeff_precision
3549 )
3550 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003551 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003552 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003553 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3554 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3555 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3556 continue; /* don't even try */
3557 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3558 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3559 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00003560#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003561 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 +00003562#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003563 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3564 }
3565 if(encoder->protected_->do_qlp_coeff_prec_search) {
3566 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00003567 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3568 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003569 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00003570 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3571 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003572 else
3573 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3574 }
3575 else {
3576 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3577 }
3578 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3579 _candidate_bits =
3580 evaluate_lpc_subframe_(
3581 encoder,
3582 integer_signal,
3583 residual[!_best_subframe],
3584 encoder->private_->abs_residual,
3585 encoder->private_->abs_residual_partition_sums,
3586 encoder->private_->raw_bits_per_partition,
3587 encoder->private_->lp_coeff[lpc_order-1],
3588 frame_header->blocksize,
3589 subframe_bps,
3590 lpc_order,
3591 qlp_coeff_precision,
3592 rice_parameter,
3593 min_partition_order,
3594 max_partition_order,
3595 precompute_partition_sums,
3596 encoder->protected_->do_escape_coding,
3597 encoder->protected_->rice_parameter_search_dist,
3598 subframe[!_best_subframe],
3599 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003600 );
3601 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3602 if(_candidate_bits < _best_bits) {
3603 _best_subframe = !_best_subframe;
3604 _best_bits = _candidate_bits;
3605 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00003606 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003607 }
3608 }
3609 }
3610 }
3611 }
3612 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003613#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00003614 }
3615 }
3616
Josh Coalson72695802002-10-11 06:25:16 +00003617 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3618 if(_best_bits == UINT_MAX) {
3619 FLAC__ASSERT(_best_subframe == 0);
3620 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3621 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003622
Josh Coalson94e02cd2001-01-25 10:41:06 +00003623 *best_subframe = _best_subframe;
3624 *best_bits = _best_bits;
3625
3626 return true;
3627}
3628
Josh Coalson6fe72f72002-08-20 04:01:59 +00003629FLAC__bool add_subframe_(
3630 FLAC__StreamEncoder *encoder,
3631 const FLAC__FrameHeader *frame_header,
3632 unsigned subframe_bps,
3633 const FLAC__Subframe *subframe,
3634 FLAC__BitBuffer *frame
3635)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003636{
3637 switch(subframe->type) {
3638 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003639 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003640 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003641 return false;
3642 }
3643 break;
3644 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00003645 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 +00003646 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003647 return false;
3648 }
3649 break;
3650 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00003651 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 +00003652 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003653 return false;
3654 }
3655 break;
3656 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00003657 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003658 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003659 return false;
3660 }
3661 break;
3662 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003663 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003664 }
3665
3666 return true;
3667}
3668
Josh Coalson6fe72f72002-08-20 04:01:59 +00003669unsigned evaluate_constant_subframe_(
3670 const FLAC__int32 signal,
3671 unsigned subframe_bps,
3672 FLAC__Subframe *subframe
3673)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003674{
3675 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3676 subframe->data.constant.value = signal;
3677
Josh Coalson82b73242001-03-28 22:17:05 +00003678 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 +00003679}
3680
Josh Coalson6fe72f72002-08-20 04:01:59 +00003681unsigned evaluate_fixed_subframe_(
3682 FLAC__StreamEncoder *encoder,
3683 const FLAC__int32 signal[],
3684 FLAC__int32 residual[],
3685 FLAC__uint32 abs_residual[],
3686 FLAC__uint64 abs_residual_partition_sums[],
3687 unsigned raw_bits_per_partition[],
3688 unsigned blocksize,
3689 unsigned subframe_bps,
3690 unsigned order,
3691 unsigned rice_parameter,
3692 unsigned min_partition_order,
3693 unsigned max_partition_order,
3694 FLAC__bool precompute_partition_sums,
3695 FLAC__bool do_escape_coding,
3696 unsigned rice_parameter_search_dist,
3697 FLAC__Subframe *subframe,
3698 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3699)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003700{
3701 unsigned i, residual_bits;
3702 const unsigned residual_samples = blocksize - order;
3703
3704 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3705
3706 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3707
3708 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003709 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003710 subframe->data.fixed.residual = residual;
3711
Josh Coalson6fe72f72002-08-20 04:01:59 +00003712 residual_bits =
3713 find_best_partition_order_(
3714 encoder->private_,
3715 residual,
3716 abs_residual,
3717 abs_residual_partition_sums,
3718 raw_bits_per_partition,
3719 residual_samples,
3720 order,
3721 rice_parameter,
3722 min_partition_order,
3723 max_partition_order,
3724 precompute_partition_sums,
3725 do_escape_coding,
3726 rice_parameter_search_dist,
3727 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3728 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003729
3730 subframe->data.fixed.order = order;
3731 for(i = 0; i < order; i++)
3732 subframe->data.fixed.warmup[i] = signal[i];
3733
Josh Coalson82b73242001-03-28 22:17:05 +00003734 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 +00003735}
3736
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003737#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003738unsigned evaluate_lpc_subframe_(
3739 FLAC__StreamEncoder *encoder,
3740 const FLAC__int32 signal[],
3741 FLAC__int32 residual[],
3742 FLAC__uint32 abs_residual[],
3743 FLAC__uint64 abs_residual_partition_sums[],
3744 unsigned raw_bits_per_partition[],
3745 const FLAC__real lp_coeff[],
3746 unsigned blocksize,
3747 unsigned subframe_bps,
3748 unsigned order,
3749 unsigned qlp_coeff_precision,
3750 unsigned rice_parameter,
3751 unsigned min_partition_order,
3752 unsigned max_partition_order,
3753 FLAC__bool precompute_partition_sums,
3754 FLAC__bool do_escape_coding,
3755 unsigned rice_parameter_search_dist,
3756 FLAC__Subframe *subframe,
3757 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3758)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003759{
Josh Coalson77e3f312001-06-23 03:03:24 +00003760 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003761 unsigned i, residual_bits;
3762 int quantization, ret;
3763 const unsigned residual_samples = blocksize - order;
3764
Josh Coalson20ac2c12002-08-30 05:47:14 +00003765 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3766 if(subframe_bps <= 16) {
3767 FLAC__ASSERT(order > 0);
3768 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3769 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3770 }
3771
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003772 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003773 if(ret != 0)
3774 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3775
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003776 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3777 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3778 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3779 else
3780 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 +00003781 else
3782 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 +00003783
3784 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3785
3786 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003787 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003788 subframe->data.lpc.residual = residual;
3789
Josh Coalson6fe72f72002-08-20 04:01:59 +00003790 residual_bits =
3791 find_best_partition_order_(
3792 encoder->private_,
3793 residual,
3794 abs_residual,
3795 abs_residual_partition_sums,
3796 raw_bits_per_partition,
3797 residual_samples,
3798 order,
3799 rice_parameter,
3800 min_partition_order,
3801 max_partition_order,
3802 precompute_partition_sums,
3803 do_escape_coding,
3804 rice_parameter_search_dist,
3805 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3806 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003807
3808 subframe->data.lpc.order = order;
3809 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3810 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003811 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003812 for(i = 0; i < order; i++)
3813 subframe->data.lpc.warmup[i] = signal[i];
3814
Josh Coalson82b73242001-03-28 22:17:05 +00003815 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 +00003816}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003817#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003818
Josh Coalson6fe72f72002-08-20 04:01:59 +00003819unsigned evaluate_verbatim_subframe_(
3820 const FLAC__int32 signal[],
3821 unsigned blocksize,
3822 unsigned subframe_bps,
3823 FLAC__Subframe *subframe
3824)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003825{
3826 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3827
3828 subframe->data.verbatim.data = signal;
3829
Josh Coalson82b73242001-03-28 22:17:05 +00003830 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 +00003831}
3832
Josh Coalson6fe72f72002-08-20 04:01:59 +00003833unsigned find_best_partition_order_(
3834 FLAC__StreamEncoderPrivate *private_,
3835 const FLAC__int32 residual[],
3836 FLAC__uint32 abs_residual[],
3837 FLAC__uint64 abs_residual_partition_sums[],
3838 unsigned raw_bits_per_partition[],
3839 unsigned residual_samples,
3840 unsigned predictor_order,
3841 unsigned rice_parameter,
3842 unsigned min_partition_order,
3843 unsigned max_partition_order,
3844 FLAC__bool precompute_partition_sums,
3845 FLAC__bool do_escape_coding,
3846 unsigned rice_parameter_search_dist,
3847 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3848)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003849{
Josh Coalson77e3f312001-06-23 03:03:24 +00003850 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003851 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00003852 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00003853 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003854 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003855
Josh Coalson2051dd42001-04-12 22:22:34 +00003856 /* compute abs(residual) for use later */
3857 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3858 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00003859 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00003860 }
3861
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003862 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 +00003863 min_partition_order = min(min_partition_order, max_partition_order);
3864
Josh Coalson8395d022001-07-12 21:25:22 +00003865 if(precompute_partition_sums) {
3866 int partition_order;
3867 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003868
Josh Coalsonf1eff452002-07-31 07:05:33 +00003869 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 +00003870
3871 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00003872 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 +00003873
3874 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3875#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003876 if(!
3877 set_partitioned_rice_with_precompute_(
3878 residual,
3879 abs_residual_partition_sums+sum,
3880 raw_bits_per_partition+sum,
3881 residual_samples,
3882 predictor_order,
3883 rice_parameter,
3884 rice_parameter_search_dist,
3885 (unsigned)partition_order,
3886 do_escape_coding,
3887 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3888 &residual_bits
3889 )
3890 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003891#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003892 if(!
3893 set_partitioned_rice_with_precompute_(
3894 abs_residual,
3895 abs_residual_partition_sums+sum,
3896 raw_bits_per_partition+sum,
3897 residual_samples,
3898 predictor_order,
3899 rice_parameter,
3900 rice_parameter_search_dist,
3901 (unsigned)partition_order,
3902 do_escape_coding,
3903 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3904 &residual_bits
3905 )
3906 )
Josh Coalson8395d022001-07-12 21:25:22 +00003907#endif
3908 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003909 FLAC__ASSERT(best_residual_bits != 0);
3910 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003911 }
3912 sum += 1u << partition_order;
3913 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3914 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003915 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003916 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003917 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003918 }
3919 }
Josh Coalson8395d022001-07-12 21:25:22 +00003920 else {
3921 unsigned partition_order;
3922 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3923#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003924 if(!
3925 set_partitioned_rice_(
3926 abs_residual,
3927 residual,
3928 residual_samples,
3929 predictor_order,
3930 rice_parameter,
3931 rice_parameter_search_dist,
3932 partition_order,
3933 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3934 &residual_bits
3935 )
3936 )
Josh Coalson8395d022001-07-12 21:25:22 +00003937#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003938 if(!
3939 set_partitioned_rice_(
3940 abs_residual,
3941 residual_samples,
3942 predictor_order,
3943 rice_parameter,
3944 rice_parameter_search_dist,
3945 partition_order,
3946 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3947 &residual_bits
3948 )
3949 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003950#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003951 {
3952 FLAC__ASSERT(best_residual_bits != 0);
3953 break;
3954 }
3955 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3956 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003957 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003958 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003959 }
3960 }
3961 }
3962
Josh Coalsona37ba462002-08-19 21:36:39 +00003963 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003964 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003965 * it is const to the outside world.
3966 */
3967 {
3968 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3969 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3970 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3971 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3972 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003973
3974 return best_residual_bits;
3975}
3976
Josh Coalson6fe72f72002-08-20 04:01:59 +00003977void precompute_partition_info_sums_(
3978 const FLAC__uint32 abs_residual[],
3979 FLAC__uint64 abs_residual_partition_sums[],
3980 unsigned residual_samples,
3981 unsigned predictor_order,
3982 unsigned min_partition_order,
3983 unsigned max_partition_order
3984)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003985{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003986 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003987 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003988 const unsigned blocksize = residual_samples + predictor_order;
3989
Josh Coalsonaef013c2001-04-24 01:25:42 +00003990 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003991 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003992 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00003993 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003994 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003995 const unsigned partitions = 1u << partition_order;
3996 const unsigned default_partition_samples = blocksize >> partition_order;
3997
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003998 FLAC__ASSERT(default_partition_samples > predictor_order);
3999
4000 for(partition = residual_sample = 0; partition < partitions; partition++) {
4001 partition_samples = default_partition_samples;
4002 if(partition == 0)
4003 partition_samples -= predictor_order;
4004 abs_residual_partition_sum = 0;
4005 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4006 abs_r = abs_residual[residual_sample];
4007 abs_residual_partition_sum += abs_r;
4008 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004009 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004010 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004011 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004012 to_partition = partitions;
4013 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004014 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00004015
Josh Coalson8395d022001-07-12 21:25:22 +00004016 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00004017 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004018 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00004019 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004020 const unsigned partitions = 1u << partition_order;
4021 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00004022 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00004023 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00004024 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00004025 from_partition++;
4026 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004027 }
4028 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00004029}
Josh Coalson8395d022001-07-12 21:25:22 +00004030
Josh Coalson6fe72f72002-08-20 04:01:59 +00004031void precompute_partition_info_escapes_(
4032 const FLAC__int32 residual[],
4033 unsigned raw_bits_per_partition[],
4034 unsigned residual_samples,
4035 unsigned predictor_order,
4036 unsigned min_partition_order,
4037 unsigned max_partition_order
4038)
Josh Coalson8395d022001-07-12 21:25:22 +00004039{
4040 int partition_order;
4041 unsigned from_partition, to_partition = 0;
4042 const unsigned blocksize = residual_samples + predictor_order;
4043
4044 /* first do max_partition_order */
4045 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
4046 FLAC__int32 r, residual_partition_min, residual_partition_max;
4047 unsigned silog2_min, silog2_max;
4048 unsigned partition, partition_sample, partition_samples, residual_sample;
4049 const unsigned partitions = 1u << partition_order;
4050 const unsigned default_partition_samples = blocksize >> partition_order;
4051
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004052 FLAC__ASSERT(default_partition_samples > predictor_order);
4053
4054 for(partition = residual_sample = 0; partition < partitions; partition++) {
4055 partition_samples = default_partition_samples;
4056 if(partition == 0)
4057 partition_samples -= predictor_order;
4058 residual_partition_min = residual_partition_max = 0;
4059 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
4060 r = residual[residual_sample];
4061 if(r < residual_partition_min)
4062 residual_partition_min = r;
4063 else if(r > residual_partition_max)
4064 residual_partition_max = r;
4065 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00004066 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004067 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
4068 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
4069 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00004070 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004071 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00004072 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00004073 }
4074
4075 /* now merge partitions for lower orders */
4076 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4077 unsigned m;
4078 unsigned i;
4079 const unsigned partitions = 1u << partition_order;
4080 for(i = 0; i < partitions; i++) {
4081 m = raw_bits_per_partition[from_partition];
4082 from_partition++;
4083 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
4084 from_partition++;
4085 to_partition++;
4086 }
4087 }
4088}
Josh Coalson94e02cd2001-01-25 10:41:06 +00004089
Josh Coalson352e0f62001-03-20 22:55:50 +00004090#ifdef VARIABLE_RICE_BITS
4091#undef VARIABLE_RICE_BITS
4092#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004093#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004094#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00004095#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004096
Josh Coalson8395d022001-07-12 21:25:22 +00004097#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004098FLAC__bool set_partitioned_rice_(
4099 const FLAC__uint32 abs_residual[],
4100 const FLAC__int32 residual[],
4101 const unsigned residual_samples,
4102 const unsigned predictor_order,
4103 const unsigned suggested_rice_parameter,
4104 const unsigned rice_parameter_search_dist,
4105 const unsigned partition_order,
4106 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4107 unsigned *bits
4108)
Josh Coalson8395d022001-07-12 21:25:22 +00004109#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004110FLAC__bool set_partitioned_rice_(
4111 const FLAC__uint32 abs_residual[],
4112 const unsigned residual_samples,
4113 const unsigned predictor_order,
4114 const unsigned suggested_rice_parameter,
4115 const unsigned rice_parameter_search_dist,
4116 const unsigned partition_order,
4117 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4118 unsigned *bits
4119)
Josh Coalson8395d022001-07-12 21:25:22 +00004120#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00004121{
Josh Coalson034dfab2001-04-27 19:10:23 +00004122 unsigned rice_parameter, partition_bits;
4123#ifndef NO_RICE_SEARCH
4124 unsigned best_partition_bits;
4125 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4126#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00004127 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004128 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004129
Josh Coalson1b689822001-05-31 20:11:02 +00004130 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00004131
Josh Coalsona37ba462002-08-19 21:36:39 +00004132 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4133 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004134
Josh Coalson94e02cd2001-01-25 10:41:06 +00004135 if(partition_order == 0) {
4136 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00004137
Josh Coalson034dfab2001-04-27 19:10:23 +00004138#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004139 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004140 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004141 min_rice_parameter = 0;
4142 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004143 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4144 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004145 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004146#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004147 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4148#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004149 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004150 }
4151 }
4152 else
4153 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
4154
4155 best_partition_bits = 0xffffffff;
4156 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4157#endif
4158#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004159 const unsigned rice_parameter_estimate = rice_parameter-1;
4160 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004161#else
4162 partition_bits = 0;
4163#endif
4164 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4165 for(i = 0; i < residual_samples; i++) {
4166#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004167 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004168#else
4169 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4170#endif
4171 }
4172#ifndef NO_RICE_SEARCH
4173 if(partition_bits < best_partition_bits) {
4174 best_rice_parameter = rice_parameter;
4175 best_partition_bits = partition_bits;
4176 }
4177 }
4178#endif
4179 parameters[0] = best_rice_parameter;
4180 bits_ += best_partition_bits;
4181 }
4182 else {
4183 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004184 unsigned partition_samples;
4185 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00004186 const unsigned partitions = 1u << partition_order;
4187 for(partition = residual_sample = 0; partition < partitions; partition++) {
4188 partition_samples = (residual_samples+predictor_order) >> partition_order;
4189 if(partition == 0) {
4190 if(partition_samples <= predictor_order)
4191 return false;
4192 else
4193 partition_samples -= predictor_order;
4194 }
4195 mean = 0;
4196 save_residual_sample = residual_sample;
4197 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004198 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00004199 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004200 /* we are basically calculating the size in bits of the
4201 * average residual magnitude in the partition:
4202 * rice_parameter = floor(log2(mean/partition_samples))
4203 * 'mean' is not a good name for the variable, it is
4204 * actually the sum of magnitudes of all residual values
4205 * in the partition, so the actual mean is
4206 * mean/partition_samples
4207 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004208 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00004209 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004210 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004211#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004212 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4213#endif
4214 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4215 }
4216
4217#ifndef NO_RICE_SEARCH
4218 if(rice_parameter_search_dist) {
4219 if(rice_parameter < rice_parameter_search_dist)
4220 min_rice_parameter = 0;
4221 else
4222 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4223 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4224 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004225#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004226 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4227#endif
4228 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4229 }
4230 }
4231 else
4232 min_rice_parameter = max_rice_parameter = rice_parameter;
4233
4234 best_partition_bits = 0xffffffff;
4235 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4236#endif
4237#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004238 const unsigned rice_parameter_estimate = rice_parameter-1;
4239 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004240#else
4241 partition_bits = 0;
4242#endif
4243 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4244 save_residual_sample = residual_sample;
4245 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4246#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004247 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004248#else
4249 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4250#endif
4251 }
4252#ifndef NO_RICE_SEARCH
4253 if(rice_parameter != max_rice_parameter)
4254 residual_sample = save_residual_sample;
4255 if(partition_bits < best_partition_bits) {
4256 best_rice_parameter = rice_parameter;
4257 best_partition_bits = partition_bits;
4258 }
4259 }
4260#endif
4261 parameters[partition] = best_rice_parameter;
4262 bits_ += best_partition_bits;
4263 }
4264 }
4265
4266 *bits = bits_;
4267 return true;
4268}
4269
4270#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004271FLAC__bool set_partitioned_rice_with_precompute_(
4272 const FLAC__int32 residual[],
4273 const FLAC__uint64 abs_residual_partition_sums[],
4274 const unsigned raw_bits_per_partition[],
4275 const unsigned residual_samples,
4276 const unsigned predictor_order,
4277 const unsigned suggested_rice_parameter,
4278 const unsigned rice_parameter_search_dist,
4279 const unsigned partition_order,
4280 const FLAC__bool search_for_escapes,
4281 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4282 unsigned *bits
4283)
Josh Coalson8395d022001-07-12 21:25:22 +00004284#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004285FLAC__bool set_partitioned_rice_with_precompute_(
4286 const FLAC__uint32 abs_residual[],
4287 const FLAC__uint64 abs_residual_partition_sums[],
4288 const unsigned raw_bits_per_partition[],
4289 const unsigned residual_samples,
4290 const unsigned predictor_order,
4291 const unsigned suggested_rice_parameter,
4292 const unsigned rice_parameter_search_dist,
4293 const unsigned partition_order,
4294 const FLAC__bool search_for_escapes,
4295 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4296 unsigned *bits
4297)
Josh Coalson8395d022001-07-12 21:25:22 +00004298#endif
4299{
4300 unsigned rice_parameter, partition_bits;
4301#ifndef NO_RICE_SEARCH
4302 unsigned best_partition_bits;
4303 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4304#endif
4305 unsigned flat_bits;
4306 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004307 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004308
4309 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4310
Josh Coalsona37ba462002-08-19 21:36:39 +00004311 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4312 parameters = partitioned_rice_contents->parameters;
4313 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004314
Josh Coalson8395d022001-07-12 21:25:22 +00004315 if(partition_order == 0) {
4316 unsigned i;
4317
4318#ifndef NO_RICE_SEARCH
4319 if(rice_parameter_search_dist) {
4320 if(suggested_rice_parameter < rice_parameter_search_dist)
4321 min_rice_parameter = 0;
4322 else
4323 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4324 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4325 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004326#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004327 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4328#endif
4329 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4330 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004331 }
4332 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004333 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00004334
Josh Coalson034dfab2001-04-27 19:10:23 +00004335 best_partition_bits = 0xffffffff;
4336 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4337#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004338#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004339 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00004340 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004341#else
4342 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004343#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004344 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00004345 for(i = 0; i < residual_samples; i++) {
4346#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00004347 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004348#else
Josh Coalson2051dd42001-04-12 22:22:34 +00004349 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 +00004350#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004351 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004352#ifndef NO_RICE_SEARCH
4353 if(partition_bits < best_partition_bits) {
4354 best_rice_parameter = rice_parameter;
4355 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00004356 }
4357 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004358#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004359 if(search_for_escapes) {
4360 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;
4361 if(flat_bits <= best_partition_bits) {
4362 raw_bits[0] = raw_bits_per_partition[0];
4363 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4364 best_partition_bits = flat_bits;
4365 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004366 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004367 parameters[0] = best_rice_parameter;
4368 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004369 }
4370 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00004371 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004372 unsigned partition_samples;
4373 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004374 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00004375 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004376 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00004377 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004378 if(partition_samples <= predictor_order)
4379 return false;
4380 else
4381 partition_samples -= predictor_order;
4382 }
Josh Coalson05d20792001-06-29 23:12:26 +00004383 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004384 /* we are basically calculating the size in bits of the
4385 * average residual magnitude in the partition:
4386 * rice_parameter = floor(log2(mean/partition_samples))
4387 * 'mean' is not a good name for the variable, it is
4388 * actually the sum of magnitudes of all residual values
4389 * in the partition, so the actual mean is
4390 * mean/partition_samples
4391 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004392 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00004393 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004394 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004395#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004396 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4397#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004398 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004399 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004400
Josh Coalson034dfab2001-04-27 19:10:23 +00004401#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004402 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004403 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004404 min_rice_parameter = 0;
4405 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004406 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4407 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004408 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004409#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004410 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4411#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004412 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004413 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004414 }
4415 else
4416 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00004417
Josh Coalson034dfab2001-04-27 19:10:23 +00004418 best_partition_bits = 0xffffffff;
4419 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4420#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004421#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00004422 const unsigned rice_parameter_estimate = rice_parameter-1;
4423 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004424#else
4425 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004426#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004427 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00004428 save_residual_sample = residual_sample;
4429 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00004430#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00004431 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004432#else
Josh Coalson4dacd192001-06-06 21:11:44 +00004433 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 +00004434#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004435 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004436#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00004437 if(rice_parameter != max_rice_parameter)
4438 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00004439 if(partition_bits < best_partition_bits) {
4440 best_rice_parameter = rice_parameter;
4441 best_partition_bits = partition_bits;
4442 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004443 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004444#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004445 if(search_for_escapes) {
4446 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;
4447 if(flat_bits <= best_partition_bits) {
4448 raw_bits[partition] = raw_bits_per_partition[partition];
4449 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4450 best_partition_bits = flat_bits;
4451 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004452 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004453 parameters[partition] = best_rice_parameter;
4454 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004455 }
4456 }
4457
4458 *bits = bits_;
4459 return true;
4460}
Josh Coalson859bc542001-03-27 22:22:27 +00004461
Josh Coalsonf1eff452002-07-31 07:05:33 +00004462unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00004463{
4464 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00004465 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00004466
4467 for(i = 0; i < samples && !(x&1); i++)
4468 x |= signal[i];
4469
4470 if(x == 0) {
4471 shift = 0;
4472 }
4473 else {
4474 for(shift = 0; !(x&1); shift++)
4475 x >>= 1;
4476 }
4477
4478 if(shift > 0) {
4479 for(i = 0; i < samples; i++)
4480 signal[i] >>= shift;
4481 }
4482
4483 return shift;
4484}
Josh Coalsond86e03b2002-08-03 21:56:15 +00004485
4486void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4487{
4488 unsigned channel;
4489
4490 for(channel = 0; channel < channels; channel++)
4491 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4492
4493 fifo->tail += wide_samples;
4494
4495 FLAC__ASSERT(fifo->tail <= fifo->size);
4496}
4497
4498void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4499{
4500 unsigned channel;
4501 unsigned sample, wide_sample;
4502 unsigned tail = fifo->tail;
4503
4504 sample = input_offset * channels;
4505 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4506 for(channel = 0; channel < channels; channel++)
4507 fifo->data[channel][tail] = input[sample++];
4508 tail++;
4509 }
4510 fifo->tail = tail;
4511
4512 FLAC__ASSERT(fifo->tail <= fifo->size);
4513}
4514
Josh Coalson8065a2d2006-10-15 08:32:56 +00004515FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalsond86e03b2002-08-03 21:56:15 +00004516{
4517 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
Josh Coalson8065a2d2006-10-15 08:32:56 +00004518 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004519 (void)decoder;
4520
4521 if(encoder->private_->verify.needs_magic_hack) {
4522 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4523 *bytes = FLAC__STREAM_SYNC_LENGTH;
4524 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4525 encoder->private_->verify.needs_magic_hack = false;
4526 }
4527 else {
4528 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00004529 /*
4530 * If we get here, a FIFO underflow has occurred,
4531 * which means there is a bug somewhere.
4532 */
4533 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004534 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4535 }
4536 else if(encoded_bytes < *bytes)
4537 *bytes = encoded_bytes;
4538 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4539 encoder->private_->verify.output.data += *bytes;
4540 encoder->private_->verify.output.bytes -= *bytes;
4541 }
4542
4543 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4544}
4545
4546FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4547{
4548 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4549 unsigned channel;
Josh Coalson49f2f162006-11-09 16:54:52 +00004550 const unsigned channels = frame->header.channels;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004551 const unsigned blocksize = frame->header.blocksize;
4552 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4553
Josh Coalson49f2f162006-11-09 16:54:52 +00004554 (void)decoder;
4555
Josh Coalsond86e03b2002-08-03 21:56:15 +00004556 for(channel = 0; channel < channels; channel++) {
4557 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4558 unsigned i, sample = 0;
4559 FLAC__int32 expect = 0, got = 0;
4560
4561 for(i = 0; i < blocksize; i++) {
4562 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4563 sample = i;
4564 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4565 got = (FLAC__int32)buffer[channel][i];
4566 break;
4567 }
4568 }
4569 FLAC__ASSERT(i < blocksize);
4570 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4571 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00004572 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004573 encoder->private_->verify.error_stats.channel = channel;
4574 encoder->private_->verify.error_stats.sample = sample;
4575 encoder->private_->verify.error_stats.expected = expect;
4576 encoder->private_->verify.error_stats.got = got;
4577 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4578 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4579 }
4580 }
4581 /* dequeue the frame from the fifo */
Josh Coalsond86e03b2002-08-03 21:56:15 +00004582 encoder->private_->verify.input_fifo.tail -= blocksize;
Josh Coalson49f2f162006-11-09 16:54:52 +00004583 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
Josh Coalsonb7b57ef2006-11-09 07:06:33 +00004584 for(channel = 0; channel < channels; channel++)
4585 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 +00004586 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4587}
4588
4589void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4590{
4591 (void)decoder, (void)metadata, (void)client_data;
4592}
4593
4594void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4595{
4596 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4597 (void)decoder, (void)status;
4598 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4599}
Josh Coalson6b21f662006-09-13 01:42:27 +00004600
Josh Coalson8065a2d2006-10-15 08:32:56 +00004601FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalson8da98c82006-10-15 04:24:05 +00004602{
4603 (void)client_data;
4604
Josh Coalson8065a2d2006-10-15 08:32:56 +00004605 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
Josh Coalson8da98c82006-10-15 04:24:05 +00004606 if (*bytes == 0) {
4607 if (feof(encoder->private_->file))
4608 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4609 else if (ferror(encoder->private_->file))
4610 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4611 }
4612 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4613}
4614
Josh Coalson6b21f662006-09-13 01:42:27 +00004615FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4616{
4617 (void)client_data;
4618
4619 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4620 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4621 else
4622 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4623}
4624
4625FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4626{
4627 off_t offset;
4628
4629 (void)client_data;
4630
4631 offset = ftello(encoder->private_->file);
4632
4633 if(offset < 0) {
4634 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4635 }
4636 else {
4637 *absolute_byte_offset = (FLAC__uint64)offset;
4638 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4639 }
4640}
4641
4642#ifdef FLAC__VALGRIND_TESTING
4643static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4644{
4645 size_t ret = fwrite(ptr, size, nmemb, stream);
4646 if(!ferror(stream))
4647 fflush(stream);
4648 return ret;
4649}
4650#else
4651#define local__fwrite fwrite
4652#endif
4653
Josh Coalson352feb52006-10-15 17:08:52 +00004654FLAC__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 +00004655{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004656 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00004657
4658 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson8da98c82006-10-15 04:24:05 +00004659 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4660#if FLAC__HAS_OGG
4661 /* We would like to be able to use 'samples > 0' in the
4662 * clause here but currently because of the nature of our
4663 * Ogg writing implementation, 'samples' is always 0 (see
4664 * ogg_encoder_aspect.c). The downside is extra progress
4665 * callbacks.
4666 */
4667 encoder->private_->is_ogg? true :
4668#endif
4669 samples > 0
4670 );
4671 if(call_it) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004672 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4673 * because at this point in the callback chain, the stats
4674 * have not been updated. Only after we return and control
4675 * gets back to write_frame_() are the stats updated
4676 */
4677 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);
4678 }
Josh Coalson6b21f662006-09-13 01:42:27 +00004679 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4680 }
4681 else
4682 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4683}
4684
4685/*
4686 * This will forcibly set stdout to binary mode (for OSes that require it)
4687 */
4688FILE *get_binary_stdout_()
4689{
4690 /* if something breaks here it is probably due to the presence or
4691 * absence of an underscore before the identifiers 'setmode',
4692 * 'fileno', and/or 'O_BINARY'; check your system header files.
4693 */
4694#if defined _MSC_VER || defined __MINGW32__
4695 _setmode(_fileno(stdout), _O_BINARY);
4696#elif defined __CYGWIN__
4697 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4698 setmode(_fileno(stdout), _O_BINARY);
4699#elif defined __EMX__
4700 setmode(fileno(stdout), O_BINARY);
4701#endif
4702
4703 return stdout;
4704}