blob: fdb0abd052dc98ee2f0796f4bace9da9021f57df [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalsone74bd952007-02-02 06:58:19 +00002 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 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__
Josh Coalson37d9fb12007-01-31 05:37:20 +000050#if _MSC_VER <= 1600 || 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 Coalson423f8042007-01-28 17:40:26 +000058#include "private/bitwriter.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
Josh Coalson14b184c2007-03-12 05:08:21 +000074#ifndef FLaC__INLINE
75#define FLaC__INLINE
76#endif
77
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000078#ifdef min
79#undef min
80#endif
81#define min(x,y) ((x)<(y)?(x):(y))
82
83#ifdef max
84#undef max
85#endif
86#define max(x,y) ((x)>(y)?(x):(y))
87
Josh Coalsonce1d07c2007-01-23 05:00:46 +000088/* Exact Rice codeword length calculation is off by default. The simple
89 * (and fast) estimation (of how many bits a residual value will be
90 * encoded with) in this encoder is very good, almost always yielding
91 * compression within 0.1% of exact calculation.
92 */
93#undef EXACT_RICE_BITS_CALCULATION
94/* Rice parameter searching is off by default. The simple (and fast)
95 * parameter estimation in this encoder is very good, almost always
96 * yielding compression within 0.1% of the optimal parameters.
97 */
98#undef ENABLE_RICE_PARAMETER_SEARCH
99
Josh Coalsondf4e40f2007-02-04 05:49:44 +0000100
Josh Coalsond86e03b2002-08-03 21:56:15 +0000101typedef struct {
102 FLAC__int32 *data[FLAC__MAX_CHANNELS];
103 unsigned size; /* of each data[] in samples */
104 unsigned tail;
105} verify_input_fifo;
106
107typedef struct {
108 const FLAC__byte *data;
109 unsigned capacity;
110 unsigned bytes;
111} verify_output;
112
113typedef enum {
114 ENCODER_IN_MAGIC = 0,
115 ENCODER_IN_METADATA = 1,
116 ENCODER_IN_AUDIO = 2
117} EncoderStateHint;
118
Josh Coalson425609c2006-11-03 16:08:52 +0000119static struct CompressionLevels {
120 FLAC__bool do_mid_side_stereo;
121 FLAC__bool loose_mid_side_stereo;
Josh Coalson425609c2006-11-03 16:08:52 +0000122 unsigned max_lpc_order;
123 unsigned qlp_coeff_precision;
124 FLAC__bool do_qlp_coeff_prec_search;
125 FLAC__bool do_escape_coding;
126 FLAC__bool do_exhaustive_model_search;
127 unsigned min_residual_partition_order;
128 unsigned max_residual_partition_order;
129 unsigned rice_parameter_search_dist;
130} compression_levels_[] = {
Josh Coalsond83553d2007-02-06 04:48:26 +0000131 { false, false, 0, 0, false, false, false, 0, 3, 0 },
132 { true , true , 0, 0, false, false, false, 0, 3, 0 },
Josh Coalson4e8fe852006-12-05 01:36:46 +0000133 { true , false, 0, 0, false, false, false, 0, 3, 0 },
Josh Coalsond83553d2007-02-06 04:48:26 +0000134 { false, false, 6, 0, false, false, false, 0, 4, 0 },
135 { true , true , 8, 0, false, false, false, 0, 4, 0 },
136 { true , false, 8, 0, false, false, false, 0, 5, 0 },
137 { true , false, 8, 0, false, false, false, 0, 6, 0 },
Josh Coalson4e8fe852006-12-05 01:36:46 +0000138 { true , false, 8, 0, false, false, true , 0, 6, 0 },
139 { true , false, 12, 0, false, false, true , 0, 6, 0 }
Josh Coalson425609c2006-11-03 16:08:52 +0000140};
141
142
Josh Coalson0a15c142001-06-13 17:59:57 +0000143/***********************************************************************
144 *
145 * Private class method prototypes
146 *
147 ***********************************************************************/
148
Josh Coalsonf1eff452002-07-31 07:05:33 +0000149static void set_defaults_(FLAC__StreamEncoder *encoder);
150static void free_(FLAC__StreamEncoder *encoder);
Josh Coalson85aaed82006-11-09 01:19:13 +0000151static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
Josh Coalson49f2f162006-11-09 16:54:52 +0000152static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
153static 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 +0000154static void update_metadata_(const FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000155#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +0000156static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000157#endif
Josh Coalson49f2f162006-11-09 16:54:52 +0000158static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
Josh Coalson85aaed82006-11-09 01:19:13 +0000159static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000160
161static FLAC__bool process_subframe_(
162 FLAC__StreamEncoder *encoder,
163 unsigned min_partition_order,
164 unsigned max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000165 const FLAC__FrameHeader *frame_header,
166 unsigned subframe_bps,
167 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000168#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000169 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000170#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000171 FLAC__Subframe *subframe[2],
172 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
173 FLAC__int32 *residual[2],
174 unsigned *best_subframe,
175 unsigned *best_bits
176);
177
178static FLAC__bool add_subframe_(
179 FLAC__StreamEncoder *encoder,
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000180 unsigned blocksize,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000181 unsigned subframe_bps,
182 const FLAC__Subframe *subframe,
Josh Coalson423f8042007-01-28 17:40:26 +0000183 FLAC__BitWriter *frame
Josh Coalson6fe72f72002-08-20 04:01:59 +0000184);
185
186static unsigned evaluate_constant_subframe_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000187 FLAC__StreamEncoder *encoder,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000188 const FLAC__int32 signal,
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000189 unsigned blocksize,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000190 unsigned subframe_bps,
191 FLAC__Subframe *subframe
192);
193
194static unsigned evaluate_fixed_subframe_(
195 FLAC__StreamEncoder *encoder,
196 const FLAC__int32 signal[],
197 FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000198 FLAC__uint64 abs_residual_partition_sums[],
199 unsigned raw_bits_per_partition[],
200 unsigned blocksize,
201 unsigned subframe_bps,
202 unsigned order,
203 unsigned rice_parameter,
204 unsigned min_partition_order,
205 unsigned max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000206 FLAC__bool do_escape_coding,
207 unsigned rice_parameter_search_dist,
208 FLAC__Subframe *subframe,
209 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
210);
211
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000212#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000213static unsigned evaluate_lpc_subframe_(
214 FLAC__StreamEncoder *encoder,
215 const FLAC__int32 signal[],
216 FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000217 FLAC__uint64 abs_residual_partition_sums[],
218 unsigned raw_bits_per_partition[],
219 const FLAC__real lp_coeff[],
220 unsigned blocksize,
221 unsigned subframe_bps,
222 unsigned order,
223 unsigned qlp_coeff_precision,
224 unsigned rice_parameter,
225 unsigned min_partition_order,
226 unsigned max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000227 FLAC__bool do_escape_coding,
228 unsigned rice_parameter_search_dist,
229 FLAC__Subframe *subframe,
230 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
231);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000232#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000233
234static unsigned evaluate_verbatim_subframe_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000235 FLAC__StreamEncoder *encoder,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000236 const FLAC__int32 signal[],
237 unsigned blocksize,
238 unsigned subframe_bps,
239 FLAC__Subframe *subframe
240);
241
242static unsigned find_best_partition_order_(
243 struct FLAC__StreamEncoderPrivate *private_,
244 const FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000245 FLAC__uint64 abs_residual_partition_sums[],
246 unsigned raw_bits_per_partition[],
247 unsigned residual_samples,
248 unsigned predictor_order,
249 unsigned rice_parameter,
250 unsigned min_partition_order,
251 unsigned max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000252 FLAC__bool do_escape_coding,
253 unsigned rice_parameter_search_dist,
254 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
255);
256
257static void precompute_partition_info_sums_(
Josh Coalson1109e7f2007-01-24 04:26:15 +0000258 const FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000259 FLAC__uint64 abs_residual_partition_sums[],
260 unsigned residual_samples,
261 unsigned predictor_order,
262 unsigned min_partition_order,
263 unsigned max_partition_order
264);
265
266static void precompute_partition_info_escapes_(
267 const FLAC__int32 residual[],
268 unsigned raw_bits_per_partition[],
269 unsigned residual_samples,
270 unsigned predictor_order,
271 unsigned min_partition_order,
272 unsigned max_partition_order
273);
274
Josh Coalson6fe72f72002-08-20 04:01:59 +0000275static FLAC__bool set_partitioned_rice_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000276#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson6fe72f72002-08-20 04:01:59 +0000277 const FLAC__int32 residual[],
Josh Coalson0a15c142001-06-13 17:59:57 +0000278#endif
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000279 const FLAC__uint64 abs_residual_partition_sums[],
280 const unsigned raw_bits_per_partition[],
281 const unsigned residual_samples,
282 const unsigned predictor_order,
283 const unsigned suggested_rice_parameter,
284 const unsigned rice_parameter_search_dist,
285 const unsigned partition_order,
286 const FLAC__bool search_for_escapes,
287 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
288 unsigned *bits
289);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000290
Josh Coalsonf1eff452002-07-31 07:05:33 +0000291static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000292
Josh Coalsond86e03b2002-08-03 21:56:15 +0000293/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000294static void append_to_verify_fifo_(
295 verify_input_fifo *fifo,
296 const FLAC__int32 * const input[],
297 unsigned input_offset,
298 unsigned channels,
299 unsigned wide_samples
300);
301
302static void append_to_verify_fifo_interleaved_(
303 verify_input_fifo *fifo,
304 const FLAC__int32 input[],
305 unsigned input_offset,
306 unsigned channels,
307 unsigned wide_samples
308);
309
Josh Coalson8065a2d2006-10-15 08:32:56 +0000310static 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 +0000311static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
312static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
313static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000314
Josh Coalson8065a2d2006-10-15 08:32:56 +0000315static 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 +0000316static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
317static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
Josh Coalson352feb52006-10-15 17:08:52 +0000318static 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 Coalsone3ec2ad2007-01-31 03:53:22 +0000319static FILE *get_binary_stdout_(void);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000320
Josh Coalson0a15c142001-06-13 17:59:57 +0000321
322/***********************************************************************
323 *
324 * Private class data
325 *
326 ***********************************************************************/
327
328typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000329 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000330 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
331 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 +0000332#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000333 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
334 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 +0000335 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
336 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000337#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000338 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
339 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 +0000340 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
341 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000342 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
343 FLAC__Subframe subframe_workspace_mid_side[2][2];
344 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
345 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000346 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
347 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
348 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
349 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000350 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index (0 or 1) into 2nd dimension of the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000351 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000352 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000353 unsigned best_subframe_bits_mid_side[2];
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000354 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 +0000355 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
Josh Coalson423f8042007-01-28 17:40:26 +0000356 FLAC__BitWriter *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000357 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
358 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000359 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalson6b21f662006-09-13 01:42:27 +0000360 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
361 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000362 unsigned current_sample_number;
363 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000364 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000365 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000366#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000367 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 +0000368#else
369 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
370#endif
371#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000372 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
Josh Coalson7446e182005-01-26 04:04:38 +0000373 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[]);
374 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[]);
375 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 +0000376#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000377 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
378 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
379 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +0000380 FLAC__bool disable_constant_subframes;
381 FLAC__bool disable_fixed_subframes;
382 FLAC__bool disable_verbatim_subframes;
Josh Coalson8da98c82006-10-15 04:24:05 +0000383#if FLAC__HAS_OGG
384 FLAC__bool is_ogg;
385#endif
386 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
Josh Coalson6b21f662006-09-13 01:42:27 +0000387 FLAC__StreamEncoderSeekCallback seek_callback;
388 FLAC__StreamEncoderTellCallback tell_callback;
Josh Coalson681c2932002-08-01 08:19:37 +0000389 FLAC__StreamEncoderWriteCallback write_callback;
390 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000391 FLAC__StreamEncoderProgressCallback progress_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000392 void *client_data;
Josh Coalson6b21f662006-09-13 01:42:27 +0000393 unsigned first_seekpoint_to_check;
394 FILE *file; /* only used when encoding to a file */
395 FLAC__uint64 bytes_written;
396 FLAC__uint64 samples_written;
397 unsigned frames_written;
398 unsigned total_frames_estimate;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000399 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000400 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
401 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000402#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000403 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
404 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000405 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
406 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000407#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000408 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
409 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000410 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000411 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000412 /*
413 * These fields have been moved here from private function local
414 * declarations merely to save stack space during encoding.
415 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000416#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000417 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000418#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000419 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000420 /*
421 * The data for the verify section
422 */
423 struct {
424 FLAC__StreamDecoder *decoder;
425 EncoderStateHint state_hint;
426 FLAC__bool needs_magic_hack;
427 verify_input_fifo input_fifo;
428 verify_output output;
429 struct {
430 FLAC__uint64 absolute_sample;
431 unsigned frame_number;
432 unsigned channel;
433 unsigned sample;
434 FLAC__int32 expected;
435 FLAC__int32 got;
436 } error_stats;
437 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000438 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000439} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000440
Josh Coalson0a15c142001-06-13 17:59:57 +0000441/***********************************************************************
442 *
443 * Public static class data
444 *
445 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000446
Josh Coalson6afed9f2002-10-16 22:29:47 +0000447FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000448 "FLAC__STREAM_ENCODER_OK",
Josh Coalson6b21f662006-09-13 01:42:27 +0000449 "FLAC__STREAM_ENCODER_UNINITIALIZED",
Josh Coalson8da98c82006-10-15 04:24:05 +0000450 "FLAC__STREAM_ENCODER_OGG_ERROR",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000451 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
452 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson6b21f662006-09-13 01:42:27 +0000453 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
454 "FLAC__STREAM_ENCODER_IO_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000455 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson6b21f662006-09-13 01:42:27 +0000456 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
457};
458
459FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
460 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
461 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
Josh Coalson8da98c82006-10-15 04:24:05 +0000462 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
Josh Coalson6b21f662006-09-13 01:42:27 +0000463 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
464 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
465 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
466 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
467 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
468 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
469 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
Josh Coalson6b21f662006-09-13 01:42:27 +0000470 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
471 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
472 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
473 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000474};
475
Josh Coalson8da98c82006-10-15 04:24:05 +0000476FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
477 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
478 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
479 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
480 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
481};
482
Josh Coalson6afed9f2002-10-16 22:29:47 +0000483FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000484 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
485 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000486};
487
Josh Coalson6b21f662006-09-13 01:42:27 +0000488FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
489 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
490 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
491 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
492};
493
494FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
495 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
496 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
497 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
498};
499
Josh Coalson49f2f162006-11-09 16:54:52 +0000500/* Number of samples that will be overread to watch for end of stream. By
501 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
502 * always try to read blocksize+1 samples before encoding a block, so that
503 * even if the stream has a total sample count that is an integral multiple
504 * of the blocksize, we will still notice when we are encoding the last
505 * block. This is needed, for example, to correctly set the end-of-stream
506 * marker in Ogg FLAC.
507 *
508 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
509 * not really any reason to change it.
510 */
511static const unsigned OVERREAD_ = 1;
512
Josh Coalson0a15c142001-06-13 17:59:57 +0000513/***********************************************************************
514 *
515 * Class constructor/destructor
516 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000517 */
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000518FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000519{
Josh Coalson0a15c142001-06-13 17:59:57 +0000520 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000521 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000522
Josh Coalson0a15c142001-06-13 17:59:57 +0000523 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000524
Josh Coalsonea7155f2002-10-18 05:49:19 +0000525 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000526 if(encoder == 0) {
527 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000528 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000529
Josh Coalsonea7155f2002-10-18 05:49:19 +0000530 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000531 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000532 free(encoder);
533 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000534 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000535
Josh Coalsonea7155f2002-10-18 05:49:19 +0000536 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000537 if(encoder->private_ == 0) {
538 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000539 free(encoder);
540 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000541 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000542
Josh Coalson423f8042007-01-28 17:40:26 +0000543 encoder->private_->frame = FLAC__bitwriter_new();
Josh Coalsonaec256b2002-03-12 16:19:54 +0000544 if(encoder->private_->frame == 0) {
545 free(encoder->private_);
546 free(encoder->protected_);
547 free(encoder);
548 return 0;
549 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000550
Josh Coalson6b21f662006-09-13 01:42:27 +0000551 encoder->private_->file = 0;
552
Josh Coalsonf1eff452002-07-31 07:05:33 +0000553 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000554
Josh Coalson3262b0d2002-08-14 20:58:42 +0000555 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000556
557 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
558 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
559 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
560 }
561 for(i = 0; i < 2; i++) {
562 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
563 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
564 }
565 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000566 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
567 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000568 }
569 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000570 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
571 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 +0000572 }
573
574 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000575 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
576 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000577 }
578 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000579 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
580 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 +0000581 }
582 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000583 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000584
Josh Coalsonfa697a92001-08-16 20:07:29 +0000585 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000586
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000587 return encoder;
588}
589
Josh Coalson6afed9f2002-10-16 22:29:47 +0000590FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000591{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000592 unsigned i;
593
Josh Coalsonf1eff452002-07-31 07:05:33 +0000594 FLAC__ASSERT(0 != encoder);
595 FLAC__ASSERT(0 != encoder->protected_);
596 FLAC__ASSERT(0 != encoder->private_);
597 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000598
Josh Coalson3262b0d2002-08-14 20:58:42 +0000599 encoder->private_->is_being_deleted = true;
600
Josh Coalsona5862262006-11-09 06:58:26 +0000601 (void)FLAC__stream_encoder_finish(encoder);
Josh Coalson3262b0d2002-08-14 20:58:42 +0000602
Josh Coalson4fa90592002-12-04 07:01:37 +0000603 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000604 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000605
606 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000607 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
608 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[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_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
612 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 +0000613 }
614 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000615 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000616
Josh Coalson423f8042007-01-28 17:40:26 +0000617 FLAC__bitwriter_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000618 free(encoder->private_);
619 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000620 free(encoder);
621}
622
Josh Coalson0a15c142001-06-13 17:59:57 +0000623/***********************************************************************
624 *
625 * Public class methods
626 *
627 ***********************************************************************/
628
Josh Coalson8da98c82006-10-15 04:24:05 +0000629static FLAC__StreamEncoderInitStatus init_stream_internal_(
630 FLAC__StreamEncoder *encoder,
631 FLAC__StreamEncoderReadCallback read_callback,
632 FLAC__StreamEncoderWriteCallback write_callback,
633 FLAC__StreamEncoderSeekCallback seek_callback,
634 FLAC__StreamEncoderTellCallback tell_callback,
635 FLAC__StreamEncoderMetadataCallback metadata_callback,
636 void *client_data,
637 FLAC__bool is_ogg
638)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000639{
640 unsigned i;
Josh Coalson3957c472006-09-24 16:25:42 +0000641 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000642
Josh Coalsonf1eff452002-07-31 07:05:33 +0000643 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000644
Josh Coalsonfa697a92001-08-16 20:07:29 +0000645 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000646 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000647
Josh Coalsonf1ac7d92006-11-16 07:20:09 +0000648#if !FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +0000649 if(is_ogg)
650 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
651#endif
652
Josh Coalson6b21f662006-09-13 01:42:27 +0000653 if(0 == write_callback || (seek_callback && 0 == tell_callback))
654 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000655
Josh Coalsonfa697a92001-08-16 20:07:29 +0000656 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000657 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000658
Josh Coalson425609c2006-11-03 16:08:52 +0000659 if(encoder->protected_->channels != 2) {
660 encoder->protected_->do_mid_side_stereo = false;
661 encoder->protected_->loose_mid_side_stereo = false;
662 }
663 else if(!encoder->protected_->do_mid_side_stereo)
664 encoder->protected_->loose_mid_side_stereo = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000665
Josh Coalsonfa697a92001-08-16 20:07:29 +0000666 if(encoder->protected_->bits_per_sample >= 32)
Josh Coalson6b21f662006-09-13 01:42:27 +0000667 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 +0000668
Josh Coalson76c68bc2002-05-17 06:22:02 +0000669 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 +0000670 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000671
Josh Coalson0833f342002-07-15 05:31:55 +0000672 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000673 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000674
Josh Coalson425609c2006-11-03 16:08:52 +0000675 if(encoder->protected_->blocksize == 0) {
676 if(encoder->protected_->max_lpc_order == 0)
677 encoder->protected_->blocksize = 1152;
678 else
Josh Coalsondc2b03b2007-02-06 05:02:46 +0000679 encoder->protected_->blocksize = 4096;
Josh Coalson425609c2006-11-03 16:08:52 +0000680 }
681
Josh Coalsonfa697a92001-08-16 20:07:29 +0000682 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000683 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000684
Josh Coalson20ac2c12002-08-30 05:47:14 +0000685 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000686 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000687
Josh Coalsonfa697a92001-08-16 20:07:29 +0000688 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000689 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000690
Josh Coalsonfa697a92001-08-16 20:07:29 +0000691 if(encoder->protected_->qlp_coeff_precision == 0) {
692 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000693 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
694 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000695 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 +0000696 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000697 else if(encoder->protected_->bits_per_sample == 16) {
698 if(encoder->protected_->blocksize <= 192)
699 encoder->protected_->qlp_coeff_precision = 7;
700 else if(encoder->protected_->blocksize <= 384)
701 encoder->protected_->qlp_coeff_precision = 8;
702 else if(encoder->protected_->blocksize <= 576)
703 encoder->protected_->qlp_coeff_precision = 9;
704 else if(encoder->protected_->blocksize <= 1152)
705 encoder->protected_->qlp_coeff_precision = 10;
706 else if(encoder->protected_->blocksize <= 2304)
707 encoder->protected_->qlp_coeff_precision = 11;
708 else if(encoder->protected_->blocksize <= 4608)
709 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000710 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000711 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000712 }
713 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000714 if(encoder->protected_->blocksize <= 384)
715 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
716 else if(encoder->protected_->blocksize <= 1152)
717 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
718 else
719 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000720 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000721 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000722 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000723 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 +0000724 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000725
Josh Coalsonfa697a92001-08-16 20:07:29 +0000726 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000727 if(
728 encoder->protected_->blocksize != 192 &&
729 encoder->protected_->blocksize != 576 &&
730 encoder->protected_->blocksize != 1152 &&
731 encoder->protected_->blocksize != 2304 &&
732 encoder->protected_->blocksize != 4608 &&
733 encoder->protected_->blocksize != 256 &&
734 encoder->protected_->blocksize != 512 &&
735 encoder->protected_->blocksize != 1024 &&
736 encoder->protected_->blocksize != 2048 &&
737 encoder->protected_->blocksize != 4096 &&
738 encoder->protected_->blocksize != 8192 &&
739 encoder->protected_->blocksize != 16384
740 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000741 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000742 if(
743 encoder->protected_->sample_rate != 8000 &&
744 encoder->protected_->sample_rate != 16000 &&
745 encoder->protected_->sample_rate != 22050 &&
746 encoder->protected_->sample_rate != 24000 &&
747 encoder->protected_->sample_rate != 32000 &&
748 encoder->protected_->sample_rate != 44100 &&
749 encoder->protected_->sample_rate != 48000 &&
750 encoder->protected_->sample_rate != 96000
751 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000752 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000753 if(
754 encoder->protected_->bits_per_sample != 8 &&
755 encoder->protected_->bits_per_sample != 12 &&
756 encoder->protected_->bits_per_sample != 16 &&
757 encoder->protected_->bits_per_sample != 20 &&
758 encoder->protected_->bits_per_sample != 24
759 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000760 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000761 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000762 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsond0edb972006-10-07 06:50:08 +0000763 if(
764 encoder->protected_->sample_rate <= 48000 &&
765 (
766 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
767 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
768 )
769 ) {
770 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
771 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000772 }
773
Josh Coalsonfa697a92001-08-16 20:07:29 +0000774 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
775 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
776 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
777 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000778
Josh Coalson8da98c82006-10-15 04:24:05 +0000779#if FLAC__HAS_OGG
780 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
781 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
782 unsigned i;
783 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
784 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
785 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
786 for( ; i > 0; i--)
787 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
788 encoder->protected_->metadata[0] = vc;
789 break;
790 }
791 }
792 }
793#endif
794 /* keep track of any SEEKTABLE block */
795 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
796 unsigned i;
797 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
798 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
799 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
800 break; /* take only the first one */
801 }
802 }
803 }
804
Josh Coalson66075c12002-06-01 05:39:38 +0000805 /* validate metadata */
806 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000807 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000808 metadata_has_seektable = false;
809 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000810 metadata_picture_has_type1 = false;
811 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000812 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000813 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
814 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000815 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000816 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000817 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000818 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000819 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000820 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000821 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000822 }
Josh Coalson3957c472006-09-24 16:25:42 +0000823 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000824 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000825 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000826 metadata_has_vorbis_comment = true;
827 }
Josh Coalson3957c472006-09-24 16:25:42 +0000828 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
829 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 +0000830 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000831 }
Josh Coalson3957c472006-09-24 16:25:42 +0000832 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
833 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000834 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000835 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
836 if(metadata_picture_has_type1) /* there should only be 1 per stream */
837 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
838 metadata_picture_has_type1 = true;
839 /* standard icon must be 32x32 pixel PNG */
840 if(
841 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
842 (
843 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
844 m->data.picture.width != 32 ||
845 m->data.picture.height != 32
846 )
847 )
848 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
849 }
850 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
851 if(metadata_picture_has_type2) /* there should only be 1 per stream */
852 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
853 metadata_picture_has_type2 = true;
854 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000855 }
Josh Coalson66075c12002-06-01 05:39:38 +0000856 }
857
Josh Coalsonfa697a92001-08-16 20:07:29 +0000858 encoder->private_->input_capacity = 0;
859 for(i = 0; i < encoder->protected_->channels; i++) {
860 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000861#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000862 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000863#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000864 }
865 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000866 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000867#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000868 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000869#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000870 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000871#ifndef FLAC__INTEGER_ONLY_LIBRARY
872 for(i = 0; i < encoder->protected_->num_apodizations; i++)
873 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
874 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
875#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000876 for(i = 0; i < encoder->protected_->channels; i++) {
877 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
878 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
879 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000880 }
881 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000882 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
883 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
884 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000885 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000886 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
887 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000888#ifndef FLAC__INTEGER_ONLY_LIBRARY
889 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
890#else
891 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
892 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
893 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
894 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
895 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
896 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
897 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);
898#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000899 if(encoder->private_->loose_mid_side_stereo_frames == 0)
900 encoder->private_->loose_mid_side_stereo_frames = 1;
901 encoder->private_->loose_mid_side_stereo_frame_count = 0;
902 encoder->private_->current_sample_number = 0;
903 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000904
Josh Coalsonfa697a92001-08-16 20:07:29 +0000905 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
906 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? */
907 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000908
Josh Coalsoncf30f502001-05-23 20:57:44 +0000909 /*
910 * get the CPU info and set the function pointers
911 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000912 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000913 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000914#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000915 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000916#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000917 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000918#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000919 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000920 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 +0000921 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000922#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000923 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000924#ifndef FLAC__INTEGER_ONLY_LIBRARY
925# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000926 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000927# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000928 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000929# ifdef FLAC__HAS_NASM
Josh Coalson48cbe662002-12-30 23:38:14 +0000930 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000931 if(encoder->protected_->max_lpc_order < 4)
932 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
933 else if(encoder->protected_->max_lpc_order < 8)
934 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
935 else if(encoder->protected_->max_lpc_order < 12)
936 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000937 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000938 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000939 }
Josh Coalson38483802007-03-03 01:47:53 +0000940 else if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000941 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000942 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000943 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000944 if(encoder->private_->cpuinfo.data.ia32.mmx) {
945 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
946 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 +0000947 }
948 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000949 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
950 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 +0000951 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000952 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
953 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
954# endif /* FLAC__HAS_NASM */
955# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000956 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000957# endif /* !FLAC__NO_ASM */
958#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000959 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000960 if(encoder->private_->use_wide_by_block) {
961 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000962 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000963
Josh Coalson6b21f662006-09-13 01:42:27 +0000964 /* set state to OK; from here on, errors are fatal and we'll override the state then */
965 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
966
Josh Coalson8da98c82006-10-15 04:24:05 +0000967#if FLAC__HAS_OGG
968 encoder->private_->is_ogg = is_ogg;
969 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
970 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
971 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
972 }
973#endif
974
975 encoder->private_->read_callback = read_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000976 encoder->private_->write_callback = write_callback;
977 encoder->private_->seek_callback = seek_callback;
978 encoder->private_->tell_callback = tell_callback;
979 encoder->private_->metadata_callback = metadata_callback;
980 encoder->private_->client_data = client_data;
981
Josh Coalsonf1eff452002-07-31 07:05:33 +0000982 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000983 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +0000984 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000985 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000986
Josh Coalson423f8042007-01-28 17:40:26 +0000987 if(!FLAC__bitwriter_init(encoder->private_->frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +0000988 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
989 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
990 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000991
992 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000993 * Set up the verify stuff if necessary
994 */
995 if(encoder->protected_->verify) {
996 /*
997 * First, set up the fifo which will hold the
998 * original signal to compare against
999 */
Josh Coalson49f2f162006-11-09 16:54:52 +00001000 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001001 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001002 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
1003 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1004 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1005 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001006 }
1007 encoder->private_->verify.input_fifo.tail = 0;
1008
1009 /*
1010 * Now set up a stream decoder for verification
1011 */
1012 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
Josh Coalson6b21f662006-09-13 01:42:27 +00001013 if(0 == encoder->private_->verify.decoder) {
1014 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1015 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1016 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001017
Josh Coalson6b21f662006-09-13 01:42:27 +00001018 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) {
1019 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1020 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1021 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001022 }
Josh Coalson589f8c72002-08-07 23:54:55 +00001023 encoder->private_->verify.error_stats.absolute_sample = 0;
1024 encoder->private_->verify.error_stats.frame_number = 0;
1025 encoder->private_->verify.error_stats.channel = 0;
1026 encoder->private_->verify.error_stats.sample = 0;
1027 encoder->private_->verify.error_stats.expected = 0;
1028 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001029
1030 /*
Josh Coalson6b21f662006-09-13 01:42:27 +00001031 * These must be done before we write any metadata, because that
1032 * calls the write_callback, which uses these values.
1033 */
1034 encoder->private_->first_seekpoint_to_check = 0;
1035 encoder->private_->samples_written = 0;
1036 encoder->protected_->streaminfo_offset = 0;
1037 encoder->protected_->seektable_offset = 0;
1038 encoder->protected_->audio_offset = 0;
1039
1040 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001041 * write the stream header
1042 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001043 if(encoder->protected_->verify)
1044 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson423f8042007-01-28 17:40:26 +00001045 if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001046 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1047 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1048 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001049 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001050 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001051 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001052 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001053
Josh Coalson5c491a12002-08-01 06:39:40 +00001054 /*
1055 * write the STREAMINFO metadata block
1056 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001057 if(encoder->protected_->verify)
1058 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +00001059 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1060 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1061 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1062 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1063 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1064 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1065 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1066 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1067 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1068 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1069 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1070 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 +00001071 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +00001072 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1073 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1074 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1075 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001076 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001077 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001078 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001079 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001080
Josh Coalson5c491a12002-08-01 06:39:40 +00001081 /*
1082 * Now that the STREAMINFO block is written, we can init this to an
1083 * absurdly-high value...
1084 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001085 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 +00001086 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001087 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001088
Josh Coalson5c491a12002-08-01 06:39:40 +00001089 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001090 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1091 * if not, we will write an empty one (FLAC__add_metadata_block()
1092 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001093 *
Josh Coalson8da98c82006-10-15 04:24:05 +00001094 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1095 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1096 * true it will have already insured that the metadata list is properly
1097 * ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001098 */
1099 if(!metadata_has_vorbis_comment) {
1100 FLAC__StreamMetadata vorbis_comment;
1101 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1102 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1103 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1104 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1105 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1106 vorbis_comment.data.vorbis_comment.num_comments = 0;
1107 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001108 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1109 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1110 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1111 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001112 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001113 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001114 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001115 }
1116 }
1117
1118 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001119 * write the user's metadata blocks
1120 */
1121 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1122 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001123 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1124 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1125 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1126 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001127 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001128 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001129 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001130 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001131 }
1132
Josh Coalson6b21f662006-09-13 01:42:27 +00001133 /* now that all the metadata is written, we save the stream offset */
1134 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 */
1135 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1136 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1137 }
1138
Josh Coalsond86e03b2002-08-03 21:56:15 +00001139 if(encoder->protected_->verify)
1140 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1141
Josh Coalson6b21f662006-09-13 01:42:27 +00001142 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1143}
1144
Josh Coalson8da98c82006-10-15 04:24:05 +00001145FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1146 FLAC__StreamEncoder *encoder,
1147 FLAC__StreamEncoderWriteCallback write_callback,
1148 FLAC__StreamEncoderSeekCallback seek_callback,
1149 FLAC__StreamEncoderTellCallback tell_callback,
1150 FLAC__StreamEncoderMetadataCallback metadata_callback,
1151 void *client_data
1152)
1153{
1154 return init_stream_internal_(
1155 encoder,
1156 /*read_callback=*/0,
1157 write_callback,
1158 seek_callback,
1159 tell_callback,
1160 metadata_callback,
1161 client_data,
1162 /*is_ogg=*/false
1163 );
1164}
1165
1166FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1167 FLAC__StreamEncoder *encoder,
1168 FLAC__StreamEncoderReadCallback read_callback,
1169 FLAC__StreamEncoderWriteCallback write_callback,
1170 FLAC__StreamEncoderSeekCallback seek_callback,
1171 FLAC__StreamEncoderTellCallback tell_callback,
1172 FLAC__StreamEncoderMetadataCallback metadata_callback,
1173 void *client_data
1174)
1175{
1176 return init_stream_internal_(
1177 encoder,
1178 read_callback,
1179 write_callback,
1180 seek_callback,
1181 tell_callback,
1182 metadata_callback,
1183 client_data,
1184 /*is_ogg=*/true
1185 );
1186}
1187
1188static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1189 FLAC__StreamEncoder *encoder,
1190 FILE *file,
1191 FLAC__StreamEncoderProgressCallback progress_callback,
1192 void *client_data,
1193 FLAC__bool is_ogg
1194)
Josh Coalson6b21f662006-09-13 01:42:27 +00001195{
1196 FLAC__StreamEncoderInitStatus init_status;
1197
1198 FLAC__ASSERT(0 != encoder);
1199 FLAC__ASSERT(0 != file);
1200
Josh Coalson6b21f662006-09-13 01:42:27 +00001201 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1202 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1203
1204 /* double protection */
1205 if(file == 0) {
1206 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1207 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1208 }
1209
Josh Coalson92f7fa92006-10-09 05:34:21 +00001210 /*
1211 * To make sure that our file does not go unclosed after an error, we
1212 * must assign the FILE pointer before any further error can occur in
1213 * this routine.
1214 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001215 if(file == stdout)
1216 file = get_binary_stdout_(); /* just to be safe */
1217
1218 encoder->private_->file = file;
1219
1220 encoder->private_->progress_callback = progress_callback;
1221 encoder->private_->bytes_written = 0;
1222 encoder->private_->samples_written = 0;
1223 encoder->private_->frames_written = 0;
1224
Josh Coalson8da98c82006-10-15 04:24:05 +00001225 init_status = init_stream_internal_(
1226 encoder,
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001227 encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
Josh Coalson8da98c82006-10-15 04:24:05 +00001228 file_write_callback_,
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001229 encoder->private_->file == stdout? 0 : file_seek_callback_,
1230 encoder->private_->file == stdout? 0 : file_tell_callback_,
Josh Coalson8da98c82006-10-15 04:24:05 +00001231 /*metadata_callback=*/0,
1232 client_data,
1233 is_ogg
1234 );
Josh Coalson6b21f662006-09-13 01:42:27 +00001235 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1236 /* the above function sets the state for us in case of an error */
1237 return init_status;
1238 }
1239
1240 {
1241 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1242
1243 FLAC__ASSERT(blocksize != 0);
1244 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1245 }
1246
1247 return init_status;
1248}
Josh Coalson8da98c82006-10-15 04:24:05 +00001249
1250FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1251 FLAC__StreamEncoder *encoder,
1252 FILE *file,
1253 FLAC__StreamEncoderProgressCallback progress_callback,
1254 void *client_data
1255)
1256{
1257 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1258}
1259
1260FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1261 FLAC__StreamEncoder *encoder,
1262 FILE *file,
1263 FLAC__StreamEncoderProgressCallback progress_callback,
1264 void *client_data
1265)
1266{
1267 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1268}
Josh Coalson6b21f662006-09-13 01:42:27 +00001269
Josh Coalson8da98c82006-10-15 04:24:05 +00001270static FLAC__StreamEncoderInitStatus init_file_internal_(
1271 FLAC__StreamEncoder *encoder,
1272 const char *filename,
1273 FLAC__StreamEncoderProgressCallback progress_callback,
1274 void *client_data,
1275 FLAC__bool is_ogg
1276)
Josh Coalson6b21f662006-09-13 01:42:27 +00001277{
1278 FILE *file;
1279
1280 FLAC__ASSERT(0 != encoder);
1281
1282 /*
1283 * To make sure that our file does not go unclosed after an error, we
1284 * have to do the same entrance checks here that are later performed
1285 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1286 */
1287 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1288 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1289
1290 file = filename? fopen(filename, "w+b") : stdout;
1291
1292 if(file == 0) {
1293 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1294 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1295 }
1296
Josh Coalson8da98c82006-10-15 04:24:05 +00001297 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1298}
1299
1300FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1301 FLAC__StreamEncoder *encoder,
1302 const char *filename,
1303 FLAC__StreamEncoderProgressCallback progress_callback,
1304 void *client_data
1305)
1306{
1307 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1308}
1309
1310FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1311 FLAC__StreamEncoder *encoder,
1312 const char *filename,
1313 FLAC__StreamEncoderProgressCallback progress_callback,
1314 void *client_data
1315)
1316{
1317 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001318}
1319
Josh Coalsona5862262006-11-09 06:58:26 +00001320FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001321{
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001322 FLAC__bool error = false;
Josh Coalsona5862262006-11-09 06:58:26 +00001323
Josh Coalsonf1eff452002-07-31 07:05:33 +00001324 FLAC__ASSERT(0 != encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001325 FLAC__ASSERT(0 != encoder->private_);
1326 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001327
Josh Coalsonfa697a92001-08-16 20:07:29 +00001328 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsona5862262006-11-09 06:58:26 +00001329 return true;
Josh Coalson2b245f22002-08-07 17:10:50 +00001330
Josh Coalson3262b0d2002-08-14 20:58:42 +00001331 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001332 if(encoder->private_->current_sample_number != 0) {
Josh Coalson49f2f162006-11-09 16:54:52 +00001333 const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
Josh Coalson2b245f22002-08-07 17:10:50 +00001334 encoder->protected_->blocksize = encoder->private_->current_sample_number;
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001335 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1336 error = true;
Josh Coalson2b245f22002-08-07 17:10:50 +00001337 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001338 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001339
Josh Coalson6b21f662006-09-13 01:42:27 +00001340 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001341
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001342 if(!encoder->private_->is_being_deleted) {
1343 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1344 if(encoder->private_->seek_callback) {
Josh Coalson8da98c82006-10-15 04:24:05 +00001345#if FLAC__HAS_OGG
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001346 if(encoder->private_->is_ogg)
1347 update_ogg_metadata_(encoder);
1348 else
Josh Coalson8da98c82006-10-15 04:24:05 +00001349#endif
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001350 update_metadata_(encoder);
Josh Coalson0a15c142001-06-13 17:59:57 +00001351
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001352 /* check if an error occurred while updating metadata */
1353 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1354 error = true;
1355 }
1356 if(encoder->private_->metadata_callback)
1357 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1358 }
1359
1360 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1361 if(!error)
1362 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1363 error = true;
1364 }
1365 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001366
Josh Coalson6b21f662006-09-13 01:42:27 +00001367 if(0 != encoder->private_->file) {
1368 if(encoder->private_->file != stdout)
1369 fclose(encoder->private_->file);
1370 encoder->private_->file = 0;
1371 }
1372
Josh Coalson8da98c82006-10-15 04:24:05 +00001373#if FLAC__HAS_OGG
1374 if(encoder->private_->is_ogg)
1375 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1376#endif
1377
Josh Coalsonf1eff452002-07-31 07:05:33 +00001378 free_(encoder);
1379 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001380
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001381 if(!error)
Josh Coalsona5862262006-11-09 06:58:26 +00001382 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1383
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001384 return !error;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001385}
1386
Josh Coalson71d5c252006-10-15 06:04:55 +00001387FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
Josh Coalson8da98c82006-10-15 04:24:05 +00001388{
1389 FLAC__ASSERT(0 != encoder);
1390 FLAC__ASSERT(0 != encoder->private_);
1391 FLAC__ASSERT(0 != encoder->protected_);
1392 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1393 return false;
Josh Coalsonf1ac7d92006-11-16 07:20:09 +00001394#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00001395 /* can't check encoder->private_->is_ogg since that's not set until init time */
1396 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1397 return true;
1398#else
1399 (void)value;
1400 return false;
1401#endif
1402}
1403
Josh Coalson6afed9f2002-10-16 22:29:47 +00001404FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001405{
1406 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001407 FLAC__ASSERT(0 != encoder->private_);
1408 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001409 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1410 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001411#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001412 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001413#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001414 return true;
1415}
1416
Josh Coalson6afed9f2002-10-16 22:29:47 +00001417FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001418{
Josh Coalson92031602002-07-24 06:02:11 +00001419 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001420 FLAC__ASSERT(0 != encoder->private_);
1421 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001422 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001423 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001424 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001425 return true;
1426}
1427
Josh Coalson6afed9f2002-10-16 22:29:47 +00001428FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001429{
Josh Coalson92031602002-07-24 06:02:11 +00001430 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001431 FLAC__ASSERT(0 != encoder->private_);
1432 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001433 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001434 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001435 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001436 return true;
1437}
1438
Josh Coalson6afed9f2002-10-16 22:29:47 +00001439FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001440{
Josh Coalson92031602002-07-24 06:02:11 +00001441 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001442 FLAC__ASSERT(0 != encoder->private_);
1443 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001444 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001445 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001446 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001447 return true;
1448}
1449
Josh Coalson6afed9f2002-10-16 22:29:47 +00001450FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001451{
Josh Coalson92031602002-07-24 06:02:11 +00001452 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001453 FLAC__ASSERT(0 != encoder->private_);
1454 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001455 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001456 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001457 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001458 return true;
1459}
1460
Josh Coalson425609c2006-11-03 16:08:52 +00001461FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1462{
1463 FLAC__bool ok = true;
1464 FLAC__ASSERT(0 != encoder);
1465 FLAC__ASSERT(0 != encoder->private_);
1466 FLAC__ASSERT(0 != encoder->protected_);
1467 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1468 return false;
1469 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1470 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1471 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1472 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
Josh Coalson4e8fe852006-12-05 01:36:46 +00001473#if 0
1474 /* was: */
Josh Coalson425609c2006-11-03 16:08:52 +00001475 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
Josh Coalson4e8fe852006-12-05 01:36:46 +00001476 /* but it's too hard to specify the string in a locale-specific way */
1477#else
1478 encoder->protected_->num_apodizations = 1;
1479 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1480 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1481#endif
Josh Coalson425609c2006-11-03 16:08:52 +00001482 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1483 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1484 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1485 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1486 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1487 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1488 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1489 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1490 return ok;
1491}
1492
Josh Coalson6afed9f2002-10-16 22:29:47 +00001493FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001494{
Josh Coalson92031602002-07-24 06:02:11 +00001495 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001496 FLAC__ASSERT(0 != encoder->private_);
1497 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001498 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001499 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001500 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001501 return true;
1502}
1503
Josh Coalson425609c2006-11-03 16:08:52 +00001504FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1505{
1506 FLAC__ASSERT(0 != encoder);
1507 FLAC__ASSERT(0 != encoder->private_);
1508 FLAC__ASSERT(0 != encoder->protected_);
1509 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1510 return false;
1511 encoder->protected_->do_mid_side_stereo = value;
1512 return true;
1513}
1514
1515FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1516{
1517 FLAC__ASSERT(0 != encoder);
1518 FLAC__ASSERT(0 != encoder->private_);
1519 FLAC__ASSERT(0 != encoder->protected_);
1520 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1521 return false;
1522 encoder->protected_->loose_mid_side_stereo = value;
1523 return true;
1524}
1525
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001526FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1527{
1528 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001529 FLAC__ASSERT(0 != encoder->private_);
1530 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001531 FLAC__ASSERT(0 != specification);
1532 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1533 return false;
1534#ifdef FLAC__INTEGER_ONLY_LIBRARY
1535 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1536#else
1537 encoder->protected_->num_apodizations = 0;
1538 while(1) {
1539 const char *s = strchr(specification, ';');
1540 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1541 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1542 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1543 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1544 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1545 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1546 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1547 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1548 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1549 else if(n==6 && 0 == strncmp("connes" , specification, n))
1550 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1551 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1552 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1553 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1554 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1555 if (stddev > 0.0 && stddev <= 0.5) {
1556 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1557 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1558 }
1559 }
1560 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1561 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1562 else if(n==4 && 0 == strncmp("hann" , specification, n))
1563 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1564 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1565 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1566 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1567 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1568 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1569 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1570 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1571 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1572 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1573 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1574 if (p >= 0.0 && p <= 1.0) {
1575 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1576 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1577 }
1578 }
1579 else if(n==5 && 0 == strncmp("welch" , specification, n))
1580 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1581 if (encoder->protected_->num_apodizations == 32)
1582 break;
1583 if (s)
1584 specification = s+1;
1585 else
1586 break;
1587 }
1588 if(encoder->protected_->num_apodizations == 0) {
1589 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001590 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1591 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001592 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001593#endif
1594 return true;
1595}
1596
Josh Coalson6afed9f2002-10-16 22:29:47 +00001597FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001598{
Josh Coalson92031602002-07-24 06:02:11 +00001599 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001600 FLAC__ASSERT(0 != encoder->private_);
1601 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001602 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001603 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001604 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001605 return true;
1606}
1607
Josh Coalson6afed9f2002-10-16 22:29:47 +00001608FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001609{
Josh Coalson92031602002-07-24 06:02:11 +00001610 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001611 FLAC__ASSERT(0 != encoder->private_);
1612 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001613 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001614 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001615 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001616 return true;
1617}
1618
Josh Coalson6afed9f2002-10-16 22:29:47 +00001619FLAC_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 +00001620{
Josh Coalson92031602002-07-24 06:02:11 +00001621 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001622 FLAC__ASSERT(0 != encoder->private_);
1623 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001624 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001625 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001626 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001627 return true;
1628}
1629
Josh Coalson6afed9f2002-10-16 22:29:47 +00001630FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001631{
Josh Coalson92031602002-07-24 06:02:11 +00001632 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001633 FLAC__ASSERT(0 != encoder->private_);
1634 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001635 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001636 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001637#if 0
1638 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001639 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001640#else
1641 (void)value;
1642#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001643 return true;
1644}
1645
Josh Coalson6afed9f2002-10-16 22:29:47 +00001646FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001647{
Josh Coalson92031602002-07-24 06:02:11 +00001648 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001649 FLAC__ASSERT(0 != encoder->private_);
1650 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001651 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001652 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001653 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001654 return true;
1655}
1656
Josh Coalson6afed9f2002-10-16 22:29:47 +00001657FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001658{
Josh Coalson92031602002-07-24 06:02:11 +00001659 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001660 FLAC__ASSERT(0 != encoder->private_);
1661 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001662 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001663 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001664 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001665 return true;
1666}
1667
Josh Coalson6afed9f2002-10-16 22:29:47 +00001668FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001669{
Josh Coalson92031602002-07-24 06:02:11 +00001670 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001671 FLAC__ASSERT(0 != encoder->private_);
1672 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001673 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001674 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001675 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001676 return true;
1677}
1678
Josh Coalson6afed9f2002-10-16 22:29:47 +00001679FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001680{
Josh Coalson92031602002-07-24 06:02:11 +00001681 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001682 FLAC__ASSERT(0 != encoder->private_);
1683 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001684 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001685 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001686#if 0
1687 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001688 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001689#else
1690 (void)value;
1691#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001692 return true;
1693}
1694
Josh Coalson6afed9f2002-10-16 22:29:47 +00001695FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001696{
Josh Coalson92031602002-07-24 06:02:11 +00001697 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001698 FLAC__ASSERT(0 != encoder->private_);
1699 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001700 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001701 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001702 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001703 return true;
1704}
1705
Josh Coalson6afed9f2002-10-16 22:29:47 +00001706FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001707{
Josh Coalson92031602002-07-24 06:02:11 +00001708 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001709 FLAC__ASSERT(0 != encoder->private_);
1710 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001711 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001712 return false;
Josh Coalson1c034072007-01-29 08:27:25 +00001713 if(0 == metadata)
1714 num_blocks = 0;
1715 if(0 == num_blocks)
1716 metadata = 0;
1717 /* realloc() does not do exactly what we want so... */
1718 if(encoder->protected_->metadata) {
1719 free(encoder->protected_->metadata);
1720 encoder->protected_->metadata = 0;
1721 encoder->protected_->num_metadata_blocks = 0;
1722 }
1723 if(num_blocks) {
1724 FLAC__StreamMetadata **m;
1725 if(0 == (m = (FLAC__StreamMetadata**)malloc(sizeof(m[0]) * num_blocks)))
1726 return false;
1727 memcpy(m, metadata, sizeof(m[0]) * num_blocks);
1728 encoder->protected_->metadata = m;
1729 encoder->protected_->num_metadata_blocks = num_blocks;
1730 }
Josh Coalson8da98c82006-10-15 04:24:05 +00001731#if FLAC__HAS_OGG
1732 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1733 return false;
1734#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001735 return true;
1736}
1737
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001738/*
1739 * These three functions are not static, but not publically exposed in
1740 * include/FLAC/ either. They are used by the test suite.
1741 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001742FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001743{
1744 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001745 FLAC__ASSERT(0 != encoder->private_);
1746 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001747 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1748 return false;
1749 encoder->private_->disable_constant_subframes = value;
1750 return true;
1751}
1752
Josh Coalson6afed9f2002-10-16 22:29:47 +00001753FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001754{
1755 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001756 FLAC__ASSERT(0 != encoder->private_);
1757 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001758 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1759 return false;
1760 encoder->private_->disable_fixed_subframes = value;
1761 return true;
1762}
1763
Josh Coalson6afed9f2002-10-16 22:29:47 +00001764FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001765{
1766 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001767 FLAC__ASSERT(0 != encoder->private_);
1768 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001769 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1770 return false;
1771 encoder->private_->disable_verbatim_subframes = value;
1772 return true;
1773}
1774
Josh Coalson6afed9f2002-10-16 22:29:47 +00001775FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001776{
Josh Coalson92031602002-07-24 06:02:11 +00001777 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001778 FLAC__ASSERT(0 != encoder->private_);
1779 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001780 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001781}
1782
Josh Coalson6afed9f2002-10-16 22:29:47 +00001783FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001784{
1785 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001786 FLAC__ASSERT(0 != encoder->private_);
1787 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001788 if(encoder->protected_->verify)
1789 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1790 else
1791 return FLAC__STREAM_DECODER_UNINITIALIZED;
1792}
1793
Josh Coalson02954222002-11-08 06:16:31 +00001794FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1795{
Josh Coalson8da98c82006-10-15 04:24:05 +00001796 FLAC__ASSERT(0 != encoder);
1797 FLAC__ASSERT(0 != encoder->private_);
1798 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson02954222002-11-08 06:16:31 +00001799 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1800 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1801 else
Josh Coalson807140d2003-09-24 22:10:51 +00001802 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001803}
1804
Josh Coalson6afed9f2002-10-16 22:29:47 +00001805FLAC_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 +00001806{
1807 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001808 FLAC__ASSERT(0 != encoder->private_);
1809 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson589f8c72002-08-07 23:54:55 +00001810 if(0 != absolute_sample)
1811 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1812 if(0 != frame_number)
1813 *frame_number = encoder->private_->verify.error_stats.frame_number;
1814 if(0 != channel)
1815 *channel = encoder->private_->verify.error_stats.channel;
1816 if(0 != sample)
1817 *sample = encoder->private_->verify.error_stats.sample;
1818 if(0 != expected)
1819 *expected = encoder->private_->verify.error_stats.expected;
1820 if(0 != got)
1821 *got = encoder->private_->verify.error_stats.got;
1822}
1823
Josh Coalson6afed9f2002-10-16 22:29:47 +00001824FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001825{
1826 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001827 FLAC__ASSERT(0 != encoder->private_);
1828 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001829 return encoder->protected_->verify;
1830}
1831
Josh Coalson6afed9f2002-10-16 22:29:47 +00001832FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001833{
Josh Coalson92031602002-07-24 06:02:11 +00001834 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001835 FLAC__ASSERT(0 != encoder->private_);
1836 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001837 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001838}
1839
Josh Coalson6afed9f2002-10-16 22:29:47 +00001840FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001841{
Josh Coalson92031602002-07-24 06:02:11 +00001842 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001843 FLAC__ASSERT(0 != encoder->private_);
1844 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001845 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001846}
1847
Josh Coalson6afed9f2002-10-16 22:29:47 +00001848FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001849{
Josh Coalson92031602002-07-24 06:02:11 +00001850 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001851 FLAC__ASSERT(0 != encoder->private_);
1852 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001853 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001854}
1855
Josh Coalson6afed9f2002-10-16 22:29:47 +00001856FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001857{
Josh Coalson92031602002-07-24 06:02:11 +00001858 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001859 FLAC__ASSERT(0 != encoder->private_);
1860 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001861 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001862}
1863
Josh Coalson6afed9f2002-10-16 22:29:47 +00001864FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001865{
Josh Coalson92031602002-07-24 06:02:11 +00001866 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001867 FLAC__ASSERT(0 != encoder->private_);
1868 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001869 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001870}
1871
Josh Coalson425609c2006-11-03 16:08:52 +00001872FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1873{
1874 FLAC__ASSERT(0 != encoder);
1875 FLAC__ASSERT(0 != encoder->private_);
1876 FLAC__ASSERT(0 != encoder->protected_);
1877 return encoder->protected_->do_mid_side_stereo;
1878}
1879
1880FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1881{
1882 FLAC__ASSERT(0 != encoder);
1883 FLAC__ASSERT(0 != encoder->private_);
1884 FLAC__ASSERT(0 != encoder->protected_);
1885 return encoder->protected_->loose_mid_side_stereo;
1886}
1887
Josh Coalson6afed9f2002-10-16 22:29:47 +00001888FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001889{
Josh Coalson92031602002-07-24 06:02:11 +00001890 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001891 FLAC__ASSERT(0 != encoder->private_);
1892 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001893 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001894}
1895
Josh Coalson6afed9f2002-10-16 22:29:47 +00001896FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001897{
Josh Coalson92031602002-07-24 06:02:11 +00001898 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001899 FLAC__ASSERT(0 != encoder->private_);
1900 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001901 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001902}
1903
Josh Coalson6afed9f2002-10-16 22:29:47 +00001904FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001905{
Josh Coalson92031602002-07-24 06:02:11 +00001906 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001907 FLAC__ASSERT(0 != encoder->private_);
1908 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001909 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001910}
1911
Josh Coalson6afed9f2002-10-16 22:29:47 +00001912FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001913{
Josh Coalson92031602002-07-24 06:02:11 +00001914 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001915 FLAC__ASSERT(0 != encoder->private_);
1916 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001917 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001918}
1919
Josh Coalson6afed9f2002-10-16 22:29:47 +00001920FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001921{
Josh Coalson92031602002-07-24 06:02:11 +00001922 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001923 FLAC__ASSERT(0 != encoder->private_);
1924 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001925 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001926}
1927
Josh Coalson6afed9f2002-10-16 22:29:47 +00001928FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001929{
Josh Coalson92031602002-07-24 06:02:11 +00001930 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001931 FLAC__ASSERT(0 != encoder->private_);
1932 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001933 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001934}
1935
Josh Coalson6afed9f2002-10-16 22:29:47 +00001936FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001937{
Josh Coalson92031602002-07-24 06:02:11 +00001938 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001939 FLAC__ASSERT(0 != encoder->private_);
1940 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001941 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001942}
1943
Josh Coalson6afed9f2002-10-16 22:29:47 +00001944FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001945{
Josh Coalson92031602002-07-24 06:02:11 +00001946 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001947 FLAC__ASSERT(0 != encoder->private_);
1948 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001949 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001950}
1951
Josh Coalson6afed9f2002-10-16 22:29:47 +00001952FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001953{
1954 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001955 FLAC__ASSERT(0 != encoder->private_);
1956 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001957 return encoder->protected_->total_samples_estimate;
1958}
1959
Josh Coalson6afed9f2002-10-16 22:29:47 +00001960FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001961{
1962 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001963 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001964 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001965
Josh Coalsonf1eff452002-07-31 07:05:33 +00001966 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001967 FLAC__ASSERT(0 != encoder->private_);
1968 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001969 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001970
1971 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001972 /*
1973 * we have several flavors of the same basic loop, optimized for
1974 * different conditions:
1975 */
1976 if(encoder->protected_->max_lpc_order > 0) {
1977 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1978 /*
1979 * stereo coding: unroll channel loop
1980 * with LPC: calculate floating point version of signal
1981 */
1982 do {
1983 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00001984 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 +00001985
Josh Coalson49f2f162006-11-09 16:54:52 +00001986 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
1987 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001988 x = mid = side = buffer[0][j];
1989 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001990#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001991 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001992#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001993 x = buffer[1][j];
1994 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001995#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001996 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001997#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001998 mid += x;
1999 side -= x;
2000 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2001 encoder->private_->integer_signal_mid_side[1][i] = side;
2002 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002003#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002004 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2005 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002006#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002007 encoder->private_->current_sample_number++;
2008 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002009 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2010 if(i > blocksize) {
2011 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002012 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002013 /* move unprocessed overread samples to beginnings of arrays */
2014 FLAC__ASSERT(i == blocksize+OVERREAD_);
2015 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2016 i--;
2017 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2018 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2019 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2020 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2021#ifndef FLAC__INTEGER_ONLY_LIBRARY
2022 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2023 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2024 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2025 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2026#endif
2027 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002028 }
2029 } while(j < samples);
2030 }
2031 else {
2032 /*
2033 * independent channel coding: buffer each channel in inner loop
2034 * with LPC: calculate floating point version of signal
2035 */
2036 do {
2037 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002038 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 +00002039
Josh Coalson49f2f162006-11-09 16:54:52 +00002040 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2041 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002042 for(channel = 0; channel < channels; channel++) {
2043 x = buffer[channel][j];
2044 encoder->private_->integer_signal[channel][i] = x;
2045#ifndef FLAC__INTEGER_ONLY_LIBRARY
2046 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2047#endif
2048 }
2049 encoder->private_->current_sample_number++;
2050 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002051 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2052 if(i > blocksize) {
2053 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002054 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002055 /* move unprocessed overread samples to beginnings of arrays */
2056 FLAC__ASSERT(i == blocksize+OVERREAD_);
2057 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2058 i--;
2059 for(channel = 0; channel < channels; channel++) {
2060 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2061#ifndef FLAC__INTEGER_ONLY_LIBRARY
2062 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2063#endif
2064 }
2065 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002066 }
2067 } while(j < samples);
2068 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002069 }
2070 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002071 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2072 /*
2073 * stereo coding: unroll channel loop
2074 * without LPC: no need to calculate floating point version of signal
2075 */
2076 do {
2077 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002078 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 +00002079
Josh Coalson49f2f162006-11-09 16:54:52 +00002080 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2081 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002082 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
2083 x = buffer[1][j];
2084 encoder->private_->integer_signal[1][i] = x;
2085 mid += x;
2086 side -= x;
2087 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2088 encoder->private_->integer_signal_mid_side[1][i] = side;
2089 encoder->private_->integer_signal_mid_side[0][i] = mid;
2090 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002091 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002092 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2093 if(i > blocksize) {
2094 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002095 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002096 /* move unprocessed overread samples to beginnings of arrays */
2097 FLAC__ASSERT(i == blocksize+OVERREAD_);
2098 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2099 i--;
2100 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2101 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2102 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2103 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2104 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002105 }
2106 } while(j < samples);
2107 }
2108 else {
2109 /*
2110 * independent channel coding: buffer each channel in inner loop
2111 * without LPC: no need to calculate floating point version of signal
2112 */
2113 do {
2114 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002115 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 +00002116
Josh Coalson49f2f162006-11-09 16:54:52 +00002117 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2118 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002119 for(channel = 0; channel < channels; channel++)
2120 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2121 encoder->private_->current_sample_number++;
2122 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002123 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2124 if(i > blocksize) {
2125 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002126 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002127 /* move unprocessed overread samples to beginnings of arrays */
2128 FLAC__ASSERT(i == blocksize+OVERREAD_);
2129 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2130 i--;
2131 for(channel = 0; channel < channels; channel++)
2132 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2133 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002134 }
2135 } while(j < samples);
2136 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002137 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002138
2139 return true;
2140}
2141
Josh Coalson6afed9f2002-10-16 22:29:47 +00002142FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002143{
2144 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00002145 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002146 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002147
Josh Coalsonf1eff452002-07-31 07:05:33 +00002148 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002149 FLAC__ASSERT(0 != encoder->private_);
2150 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002151 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002152
2153 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002154 /*
2155 * we have several flavors of the same basic loop, optimized for
2156 * different conditions:
2157 */
2158 if(encoder->protected_->max_lpc_order > 0) {
2159 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2160 /*
2161 * stereo coding: unroll channel loop
2162 * with LPC: calculate floating point version of signal
2163 */
2164 do {
2165 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002166 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 +00002167
Josh Coalson49f2f162006-11-09 16:54:52 +00002168 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2169 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002170 x = mid = side = buffer[k++];
2171 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002172#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002173 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002174#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002175 x = buffer[k++];
2176 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002177#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002178 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002179#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002180 mid += x;
2181 side -= x;
2182 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2183 encoder->private_->integer_signal_mid_side[1][i] = side;
2184 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002185#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002186 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2187 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002188#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002189 encoder->private_->current_sample_number++;
2190 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002191 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2192 if(i > blocksize) {
2193 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002194 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002195 /* move unprocessed overread samples to beginnings of arrays */
2196 FLAC__ASSERT(i == blocksize+OVERREAD_);
2197 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2198 i--;
2199 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2200 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2201 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2202 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2203#ifndef FLAC__INTEGER_ONLY_LIBRARY
2204 encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
2205 encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
2206 encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
2207 encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
2208#endif
2209 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002210 }
2211 } while(j < samples);
2212 }
2213 else {
2214 /*
2215 * independent channel coding: buffer each channel in inner loop
2216 * with LPC: calculate floating point version of signal
2217 */
2218 do {
2219 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002220 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 +00002221
Josh Coalson49f2f162006-11-09 16:54:52 +00002222 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2223 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002224 for(channel = 0; channel < channels; channel++) {
2225 x = buffer[k++];
2226 encoder->private_->integer_signal[channel][i] = x;
2227#ifndef FLAC__INTEGER_ONLY_LIBRARY
2228 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2229#endif
2230 }
2231 encoder->private_->current_sample_number++;
2232 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002233 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2234 if(i > blocksize) {
2235 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002236 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002237 /* move unprocessed overread samples to beginnings of arrays */
2238 FLAC__ASSERT(i == blocksize+OVERREAD_);
2239 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2240 i--;
2241 for(channel = 0; channel < channels; channel++) {
2242 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2243#ifndef FLAC__INTEGER_ONLY_LIBRARY
2244 encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
2245#endif
2246 }
2247 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002248 }
2249 } while(j < samples);
2250 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002251 }
2252 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002253 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2254 /*
2255 * stereo coding: unroll channel loop
2256 * without LPC: no need to calculate floating point version of signal
2257 */
2258 do {
2259 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002260 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 +00002261
Josh Coalson49f2f162006-11-09 16:54:52 +00002262 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2263 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002264 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00002265 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002266 encoder->private_->integer_signal[1][i] = x;
2267 mid += x;
2268 side -= x;
2269 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2270 encoder->private_->integer_signal_mid_side[1][i] = side;
2271 encoder->private_->integer_signal_mid_side[0][i] = mid;
2272 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002273 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002274 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2275 if(i > blocksize) {
2276 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002277 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002278 /* move unprocessed overread samples to beginnings of arrays */
2279 FLAC__ASSERT(i == blocksize+OVERREAD_);
2280 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2281 i--;
2282 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
2283 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
2284 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
2285 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
2286 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002287 }
2288 } while(j < samples);
2289 }
2290 else {
2291 /*
2292 * independent channel coding: buffer each channel in inner loop
2293 * without LPC: no need to calculate floating point version of signal
2294 */
2295 do {
2296 if(encoder->protected_->verify)
Josh Coalson49f2f162006-11-09 16:54:52 +00002297 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 +00002298
Josh Coalson49f2f162006-11-09 16:54:52 +00002299 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2300 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002301 for(channel = 0; channel < channels; channel++)
2302 encoder->private_->integer_signal[channel][i] = buffer[k++];
2303 encoder->private_->current_sample_number++;
2304 }
Josh Coalson49f2f162006-11-09 16:54:52 +00002305 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2306 if(i > blocksize) {
2307 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002308 return false;
Josh Coalson49f2f162006-11-09 16:54:52 +00002309 /* move unprocessed overread samples to beginnings of arrays */
2310 FLAC__ASSERT(i == blocksize+OVERREAD_);
2311 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2312 i--;
2313 for(channel = 0; channel < channels; channel++)
2314 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
2315 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002316 }
2317 } while(j < samples);
2318 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002319 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002320
2321 return true;
2322}
2323
Josh Coalsonf1eff452002-07-31 07:05:33 +00002324/***********************************************************************
2325 *
2326 * Private class methods
2327 *
2328 ***********************************************************************/
2329
2330void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00002331{
2332 FLAC__ASSERT(0 != encoder);
2333
Josh Coalson47c7b142005-01-29 06:08:58 +00002334#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2335 encoder->protected_->verify = true;
2336#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00002337 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00002338#endif
Josh Coalson92031602002-07-24 06:02:11 +00002339 encoder->protected_->streamable_subset = true;
2340 encoder->protected_->do_mid_side_stereo = false;
2341 encoder->protected_->loose_mid_side_stereo = false;
2342 encoder->protected_->channels = 2;
2343 encoder->protected_->bits_per_sample = 16;
2344 encoder->protected_->sample_rate = 44100;
Josh Coalson425609c2006-11-03 16:08:52 +00002345 encoder->protected_->blocksize = 0;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002346#ifndef FLAC__INTEGER_ONLY_LIBRARY
2347 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00002348 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2349 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002350#endif
Josh Coalson92031602002-07-24 06:02:11 +00002351 encoder->protected_->max_lpc_order = 0;
2352 encoder->protected_->qlp_coeff_precision = 0;
2353 encoder->protected_->do_qlp_coeff_prec_search = false;
2354 encoder->protected_->do_exhaustive_model_search = false;
2355 encoder->protected_->do_escape_coding = false;
2356 encoder->protected_->min_residual_partition_order = 0;
2357 encoder->protected_->max_residual_partition_order = 0;
2358 encoder->protected_->rice_parameter_search_dist = 0;
2359 encoder->protected_->total_samples_estimate = 0;
2360 encoder->protected_->metadata = 0;
2361 encoder->protected_->num_metadata_blocks = 0;
2362
Josh Coalson6b21f662006-09-13 01:42:27 +00002363 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002364 encoder->private_->disable_constant_subframes = false;
2365 encoder->private_->disable_fixed_subframes = false;
2366 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002367#if FLAC__HAS_OGG
2368 encoder->private_->is_ogg = false;
2369#endif
2370 encoder->private_->read_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002371 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002372 encoder->private_->seek_callback = 0;
2373 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002374 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002375 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002376 encoder->private_->client_data = 0;
Josh Coalson8da98c82006-10-15 04:24:05 +00002377
2378#if FLAC__HAS_OGG
2379 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2380#endif
Josh Coalson92031602002-07-24 06:02:11 +00002381}
2382
Josh Coalsonf1eff452002-07-31 07:05:33 +00002383void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00002384{
2385 unsigned i, channel;
2386
Josh Coalsonf1eff452002-07-31 07:05:33 +00002387 FLAC__ASSERT(0 != encoder);
Josh Coalson1c034072007-01-29 08:27:25 +00002388 if(encoder->protected_->metadata) {
2389 free(encoder->protected_->metadata);
2390 encoder->protected_->metadata = 0;
2391 encoder->protected_->num_metadata_blocks = 0;
2392 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002393 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002394 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002395 free(encoder->private_->integer_signal_unaligned[i]);
2396 encoder->private_->integer_signal_unaligned[i] = 0;
2397 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002398#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002399 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002400 free(encoder->private_->real_signal_unaligned[i]);
2401 encoder->private_->real_signal_unaligned[i] = 0;
2402 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002403#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002404 }
2405 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002406 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002407 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2408 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2409 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002410#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002411 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002412 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2413 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2414 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002415#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002416 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002417#ifndef FLAC__INTEGER_ONLY_LIBRARY
2418 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2419 if(0 != encoder->private_->window_unaligned[i]) {
2420 free(encoder->private_->window_unaligned[i]);
2421 encoder->private_->window_unaligned[i] = 0;
2422 }
2423 }
2424 if(0 != encoder->private_->windowed_signal_unaligned) {
2425 free(encoder->private_->windowed_signal_unaligned);
2426 encoder->private_->windowed_signal_unaligned = 0;
2427 }
2428#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002429 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2430 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002431 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002432 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2433 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2434 }
2435 }
2436 }
2437 for(channel = 0; channel < 2; channel++) {
2438 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002439 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002440 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2441 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2442 }
2443 }
2444 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002445 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002446 free(encoder->private_->abs_residual_partition_sums_unaligned);
2447 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2448 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002449 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002450 free(encoder->private_->raw_bits_per_partition_unaligned);
2451 encoder->private_->raw_bits_per_partition_unaligned = 0;
2452 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002453 if(encoder->protected_->verify) {
2454 for(i = 0; i < encoder->protected_->channels; i++) {
2455 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2456 free(encoder->private_->verify.input_fifo.data[i]);
2457 encoder->private_->verify.input_fifo.data[i] = 0;
2458 }
2459 }
2460 }
Josh Coalson423f8042007-01-28 17:40:26 +00002461 FLAC__bitwriter_free(encoder->private_->frame);
Josh Coalson639aeb02002-07-25 05:38:23 +00002462}
2463
Josh Coalson85aaed82006-11-09 01:19:13 +00002464FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002465{
Josh Coalson77e3f312001-06-23 03:03:24 +00002466 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002467 unsigned i, channel;
2468
Josh Coalson85aaed82006-11-09 01:19:13 +00002469 FLAC__ASSERT(new_blocksize > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002470 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2471 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002472
2473 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalson85aaed82006-11-09 01:19:13 +00002474 if(new_blocksize <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002475 return true;
2476
2477 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002478
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002479 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2480 * requires that the input arrays (in our case the integer signals)
2481 * have a buffer of up to 3 zeroes in front (at negative indices) for
Josh Coalson85aaed82006-11-09 01:19:13 +00002482 * alignment purposes; we use 4 in front to keep the data well-aligned.
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002483 */
Josh Coalson8395d022001-07-12 21:25:22 +00002484
Josh Coalsonfa697a92001-08-16 20:07:29 +00002485 for(i = 0; ok && i < encoder->protected_->channels; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002486 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 +00002487 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2488 encoder->private_->integer_signal[i] += 4;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002489#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002490 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002491 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 +00002492#endif
Josh Coalson85aaed82006-11-09 01:19:13 +00002493 }
2494 for(i = 0; ok && i < 2; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002495 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 +00002496 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2497 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson85aaed82006-11-09 01:19:13 +00002498#ifndef FLAC__INTEGER_ONLY_LIBRARY
2499 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002500 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 +00002501#endif
Josh Coalson0a15c142001-06-13 17:59:57 +00002502 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002503#ifndef FLAC__INTEGER_ONLY_LIBRARY
2504 if(ok && encoder->protected_->max_lpc_order > 0) {
2505 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
Josh Coalson85aaed82006-11-09 01:19:13 +00002506 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2507 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 +00002508 }
2509#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002510 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002511 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002512 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 +00002513 }
2514 }
2515 for(channel = 0; ok && channel < 2; channel++) {
2516 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002517 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 +00002518 }
2519 }
Josh Coalson469ba742007-02-03 02:52:32 +00002520 /* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
2521 /*@@@ new_blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions; would require moving this out into a separate function, then checking its capacity against the need of the current blocksize&min/max_partition_order (and maybe predictor order) */
Josh Coalson1109e7f2007-01-24 04:26:15 +00002522 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 +00002523 if(encoder->protected_->do_escape_coding)
Josh Coalson85aaed82006-11-09 01:19:13 +00002524 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 +00002525
Josh Coalson85aaed82006-11-09 01:19:13 +00002526 /* now adjust the windows if the blocksize has changed */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002527#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson85aaed82006-11-09 01:19:13 +00002528 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002529 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2530 switch(encoder->protected_->apodizations[i].type) {
2531 case FLAC__APODIZATION_BARTLETT:
Josh Coalson85aaed82006-11-09 01:19:13 +00002532 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002533 break;
2534 case FLAC__APODIZATION_BARTLETT_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002535 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002536 break;
2537 case FLAC__APODIZATION_BLACKMAN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002538 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002539 break;
2540 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002541 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002542 break;
2543 case FLAC__APODIZATION_CONNES:
Josh Coalson85aaed82006-11-09 01:19:13 +00002544 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002545 break;
2546 case FLAC__APODIZATION_FLATTOP:
Josh Coalson85aaed82006-11-09 01:19:13 +00002547 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002548 break;
2549 case FLAC__APODIZATION_GAUSS:
Josh Coalson85aaed82006-11-09 01:19:13 +00002550 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002551 break;
2552 case FLAC__APODIZATION_HAMMING:
Josh Coalson85aaed82006-11-09 01:19:13 +00002553 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002554 break;
2555 case FLAC__APODIZATION_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002556 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002557 break;
2558 case FLAC__APODIZATION_KAISER_BESSEL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002559 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002560 break;
2561 case FLAC__APODIZATION_NUTTALL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002562 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002563 break;
2564 case FLAC__APODIZATION_RECTANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002565 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002566 break;
2567 case FLAC__APODIZATION_TRIANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002568 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002569 break;
2570 case FLAC__APODIZATION_TUKEY:
Josh Coalson85aaed82006-11-09 01:19:13 +00002571 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002572 break;
2573 case FLAC__APODIZATION_WELCH:
Josh Coalson85aaed82006-11-09 01:19:13 +00002574 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002575 break;
2576 default:
2577 FLAC__ASSERT(0);
2578 /* double protection */
Josh Coalson85aaed82006-11-09 01:19:13 +00002579 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002580 break;
2581 }
2582 }
2583 }
2584#endif
2585
Josh Coalson85aaed82006-11-09 01:19:13 +00002586 if(ok)
2587 encoder->private_->input_capacity = new_blocksize;
2588 else
2589 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2590
Josh Coalson0a15c142001-06-13 17:59:57 +00002591 return ok;
2592}
2593
Josh Coalson49f2f162006-11-09 16:54:52 +00002594FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
Josh Coalson5c491a12002-08-01 06:39:40 +00002595{
2596 const FLAC__byte *buffer;
Josh Coalson352feb52006-10-15 17:08:52 +00002597 size_t bytes;
Josh Coalson5c491a12002-08-01 06:39:40 +00002598
Josh Coalson423f8042007-01-28 17:40:26 +00002599 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
Josh Coalson5c491a12002-08-01 06:39:40 +00002600
Josh Coalson423f8042007-01-28 17:40:26 +00002601 if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
2602 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2603 return false;
2604 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002605
Josh Coalsond86e03b2002-08-03 21:56:15 +00002606 if(encoder->protected_->verify) {
2607 encoder->private_->verify.output.data = buffer;
2608 encoder->private_->verify.output.bytes = bytes;
2609 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2610 encoder->private_->verify.needs_magic_hack = true;
2611 }
2612 else {
2613 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
Josh Coalson423f8042007-01-28 17:40:26 +00002614 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2615 FLAC__bitwriter_clear(encoder->private_->frame);
Josh Coalsond86e03b2002-08-03 21:56:15 +00002616 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2617 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2618 return false;
2619 }
2620 }
2621 }
2622
Josh Coalson49f2f162006-11-09 16:54:52 +00002623 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalson423f8042007-01-28 17:40:26 +00002624 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2625 FLAC__bitwriter_clear(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002626 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002627 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002628 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002629
Josh Coalson423f8042007-01-28 17:40:26 +00002630 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2631 FLAC__bitwriter_clear(encoder->private_->frame);
Josh Coalson5c491a12002-08-01 06:39:40 +00002632
Josh Coalsond86e03b2002-08-03 21:56:15 +00002633 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002634 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2635 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 +00002636 }
2637
Josh Coalson5c491a12002-08-01 06:39:40 +00002638 return true;
2639}
2640
Josh Coalson49f2f162006-11-09 16:54:52 +00002641FLAC__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 +00002642{
2643 FLAC__StreamEncoderWriteStatus status;
2644 FLAC__uint64 output_position = 0;
2645
2646 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2647 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2648 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2649 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2650 }
2651
2652 /*
2653 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2654 */
2655 if(samples == 0) {
2656 FLAC__MetadataType type = (buffer[0] & 0x7f);
2657 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2658 encoder->protected_->streaminfo_offset = output_position;
2659 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2660 encoder->protected_->seektable_offset = output_position;
2661 }
2662
2663 /*
2664 * Mark the current seek point if hit (if audio_offset == 0 that
2665 * means we're still writing metadata and haven't hit the first
2666 * frame yet)
2667 */
2668 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2669 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2670 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2671 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2672 FLAC__uint64 test_sample;
2673 unsigned i;
2674 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2675 test_sample = encoder->private_->seek_table->points[i].sample_number;
2676 if(test_sample > frame_last_sample) {
2677 break;
2678 }
2679 else if(test_sample >= frame_first_sample) {
2680 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2681 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2682 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2683 encoder->private_->first_seekpoint_to_check++;
2684 /* DO NOT: "break;" and here's why:
2685 * The seektable template may contain more than one target
2686 * sample for any given frame; we will keep looping, generating
2687 * duplicate seekpoints for them, and we'll clean it up later,
2688 * just before writing the seektable back to the metadata.
2689 */
2690 }
2691 else {
2692 encoder->private_->first_seekpoint_to_check++;
2693 }
2694 }
2695 }
2696
Josh Coalson8da98c82006-10-15 04:24:05 +00002697#if FLAC__HAS_OGG
2698 if(encoder->private_->is_ogg) {
2699 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2700 &encoder->protected_->ogg_encoder_aspect,
Josh Coalson8da98c82006-10-15 04:24:05 +00002701 buffer,
2702 bytes,
2703 samples,
2704 encoder->private_->current_frame_number,
Josh Coalson49f2f162006-11-09 16:54:52 +00002705 is_last_block,
Josh Coalson8da98c82006-10-15 04:24:05 +00002706 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2707 encoder,
2708 encoder->private_->client_data
2709 );
2710 }
2711 else
2712#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00002713 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2714
2715 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2716 encoder->private_->bytes_written += bytes;
2717 encoder->private_->samples_written += samples;
2718 /* we keep a high watermark on the number of frames written because
2719 * when the encoder goes back to write metadata, 'current_frame'
2720 * will drop back to 0.
2721 */
2722 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2723 }
2724 else
2725 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2726
2727 return status;
2728}
2729
2730/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2731void update_metadata_(const FLAC__StreamEncoder *encoder)
2732{
2733 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2734 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2735 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2736 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2737 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2738 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2739 FLAC__StreamEncoderSeekStatus seek_status;
2740
2741 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2742
2743 /* All this is based on intimate knowledge of the stream header
2744 * layout, but a change to the header format that would break this
2745 * would also break all streams encoded in the previous format.
2746 */
2747
2748 /*
2749 * Write MD5 signature
2750 */
2751 {
2752 const unsigned md5_offset =
2753 FLAC__STREAM_METADATA_HEADER_LENGTH +
2754 (
2755 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2756 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2757 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2758 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2759 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2760 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2761 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2762 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2763 ) / 8;
2764
2765 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2766 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2767 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2768 return;
2769 }
2770 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2771 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2772 return;
2773 }
2774 }
2775
2776 /*
2777 * Write total samples
2778 */
2779 {
2780 const unsigned total_samples_byte_offset =
2781 FLAC__STREAM_METADATA_HEADER_LENGTH +
2782 (
2783 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2784 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2785 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2786 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2787 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2788 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2789 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2790 - 4
2791 ) / 8;
2792
2793 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2794 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2795 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2796 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2797 b[4] = (FLAC__byte)(samples & 0xFF);
2798 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) {
2799 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2800 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2801 return;
2802 }
2803 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2804 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2805 return;
2806 }
2807 }
2808
2809 /*
2810 * Write min/max framesize
2811 */
2812 {
2813 const unsigned min_framesize_offset =
2814 FLAC__STREAM_METADATA_HEADER_LENGTH +
2815 (
2816 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2817 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2818 ) / 8;
2819
2820 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2821 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2822 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2823 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2824 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2825 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2826 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) {
2827 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2828 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2829 return;
2830 }
2831 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2832 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2833 return;
2834 }
2835 }
2836
2837 /*
2838 * Write seektable
2839 */
2840 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2841 unsigned i;
2842
2843 FLAC__format_seektable_sort(encoder->private_->seek_table);
2844
2845 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2846
2847 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) {
2848 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2849 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2850 return;
2851 }
2852
2853 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2854 FLAC__uint64 xx;
2855 unsigned x;
2856 xx = encoder->private_->seek_table->points[i].sample_number;
2857 b[7] = (FLAC__byte)xx; xx >>= 8;
2858 b[6] = (FLAC__byte)xx; xx >>= 8;
2859 b[5] = (FLAC__byte)xx; xx >>= 8;
2860 b[4] = (FLAC__byte)xx; xx >>= 8;
2861 b[3] = (FLAC__byte)xx; xx >>= 8;
2862 b[2] = (FLAC__byte)xx; xx >>= 8;
2863 b[1] = (FLAC__byte)xx; xx >>= 8;
2864 b[0] = (FLAC__byte)xx; xx >>= 8;
2865 xx = encoder->private_->seek_table->points[i].stream_offset;
2866 b[15] = (FLAC__byte)xx; xx >>= 8;
2867 b[14] = (FLAC__byte)xx; xx >>= 8;
2868 b[13] = (FLAC__byte)xx; xx >>= 8;
2869 b[12] = (FLAC__byte)xx; xx >>= 8;
2870 b[11] = (FLAC__byte)xx; xx >>= 8;
2871 b[10] = (FLAC__byte)xx; xx >>= 8;
2872 b[9] = (FLAC__byte)xx; xx >>= 8;
2873 b[8] = (FLAC__byte)xx; xx >>= 8;
2874 x = encoder->private_->seek_table->points[i].frame_samples;
2875 b[17] = (FLAC__byte)x; x >>= 8;
2876 b[16] = (FLAC__byte)x; x >>= 8;
2877 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2878 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2879 return;
2880 }
2881 }
2882 }
2883}
2884
Josh Coalson15b8eb82006-10-15 05:15:55 +00002885#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00002886/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2887void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2888{
Josh Coalsonc986d132006-11-15 08:53:32 +00002889 /* the # of bytes in the 1st packet that precede the STREAMINFO */
2890 static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2891 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2892 FLAC__OGG_MAPPING_MAGIC_LENGTH +
2893 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2894 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2895 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2896 FLAC__STREAM_SYNC_LENGTH
2897 ;
Josh Coalson8da98c82006-10-15 04:24:05 +00002898 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2899 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2900 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2901 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2902 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2903 ogg_page page;
2904
2905 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
Josh Coalsoncf1422e2006-11-16 01:32:25 +00002906 FLAC__ASSERT(0 != encoder->private_->seek_callback);
2907
2908 /* Pre-check that client supports seeking, since we don't want the
2909 * ogg_helper code to ever have to deal with this condition.
2910 */
2911 if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2912 return;
Josh Coalson8da98c82006-10-15 04:24:05 +00002913
2914 /* All this is based on intimate knowledge of the stream header
2915 * layout, but a change to the header format that would break this
2916 * would also break all streams encoded in the previous format.
2917 */
2918
2919 /**
2920 ** Write STREAMINFO stats
2921 **/
2922 simple_ogg_page__init(&page);
2923 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2924 simple_ogg_page__clear(&page);
2925 return; /* state already set */
2926 }
2927
2928 /*
2929 * Write MD5 signature
2930 */
2931 {
2932 const unsigned md5_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002933 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002934 FLAC__STREAM_METADATA_HEADER_LENGTH +
2935 (
2936 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2937 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2938 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2939 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2940 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2941 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2942 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2943 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2944 ) / 8;
2945
2946 if(md5_offset + 16 > (unsigned)page.body_len) {
2947 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2948 simple_ogg_page__clear(&page);
2949 return;
2950 }
2951 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2952 }
2953
2954 /*
2955 * Write total samples
2956 */
2957 {
2958 const unsigned total_samples_byte_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002959 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002960 FLAC__STREAM_METADATA_HEADER_LENGTH +
2961 (
2962 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2963 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2964 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2965 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2966 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2967 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2968 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2969 - 4
2970 ) / 8;
2971
2972 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2973 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2974 simple_ogg_page__clear(&page);
2975 return;
2976 }
2977 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2978 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2979 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2980 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2981 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2982 b[4] = (FLAC__byte)(samples & 0xFF);
2983 memcpy(page.body + total_samples_byte_offset, b, 5);
2984 }
2985
2986 /*
2987 * Write min/max framesize
2988 */
2989 {
2990 const unsigned min_framesize_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002991 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002992 FLAC__STREAM_METADATA_HEADER_LENGTH +
2993 (
2994 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2995 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2996 ) / 8;
2997
2998 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2999 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3000 simple_ogg_page__clear(&page);
3001 return;
3002 }
3003 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
3004 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
3005 b[2] = (FLAC__byte)(min_framesize & 0xFF);
3006 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
3007 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
3008 b[5] = (FLAC__byte)(max_framesize & 0xFF);
3009 memcpy(page.body + min_framesize_offset, b, 6);
3010 }
3011 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3012 simple_ogg_page__clear(&page);
3013 return; /* state already set */
3014 }
3015 simple_ogg_page__clear(&page);
3016
3017 /*
3018 * Write seektable
3019 */
3020 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
3021 unsigned i;
3022 FLAC__byte *p;
3023
3024 FLAC__format_seektable_sort(encoder->private_->seek_table);
3025
3026 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3027
3028 simple_ogg_page__init(&page);
3029 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3030 simple_ogg_page__clear(&page);
3031 return; /* state already set */
3032 }
3033
Josh Coalsonc986d132006-11-15 08:53:32 +00003034 if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
Josh Coalson8da98c82006-10-15 04:24:05 +00003035 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3036 simple_ogg_page__clear(&page);
3037 return;
3038 }
3039
3040 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3041 FLAC__uint64 xx;
3042 unsigned x;
3043 xx = encoder->private_->seek_table->points[i].sample_number;
3044 b[7] = (FLAC__byte)xx; xx >>= 8;
3045 b[6] = (FLAC__byte)xx; xx >>= 8;
3046 b[5] = (FLAC__byte)xx; xx >>= 8;
3047 b[4] = (FLAC__byte)xx; xx >>= 8;
3048 b[3] = (FLAC__byte)xx; xx >>= 8;
3049 b[2] = (FLAC__byte)xx; xx >>= 8;
3050 b[1] = (FLAC__byte)xx; xx >>= 8;
3051 b[0] = (FLAC__byte)xx; xx >>= 8;
3052 xx = encoder->private_->seek_table->points[i].stream_offset;
3053 b[15] = (FLAC__byte)xx; xx >>= 8;
3054 b[14] = (FLAC__byte)xx; xx >>= 8;
3055 b[13] = (FLAC__byte)xx; xx >>= 8;
3056 b[12] = (FLAC__byte)xx; xx >>= 8;
3057 b[11] = (FLAC__byte)xx; xx >>= 8;
3058 b[10] = (FLAC__byte)xx; xx >>= 8;
3059 b[9] = (FLAC__byte)xx; xx >>= 8;
3060 b[8] = (FLAC__byte)xx; xx >>= 8;
3061 x = encoder->private_->seek_table->points[i].frame_samples;
3062 b[17] = (FLAC__byte)x; x >>= 8;
3063 b[16] = (FLAC__byte)x; x >>= 8;
Josh Coalson8da98c82006-10-15 04:24:05 +00003064 memcpy(p, b, 18);
3065 }
3066
3067 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3068 simple_ogg_page__clear(&page);
3069 return; /* state already set */
3070 }
3071 simple_ogg_page__clear(&page);
3072 }
3073}
Josh Coalson15b8eb82006-10-15 05:15:55 +00003074#endif
Josh Coalson8da98c82006-10-15 04:24:05 +00003075
Josh Coalson49f2f162006-11-09 16:54:52 +00003076FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
Josh Coalson0a15c142001-06-13 17:59:57 +00003077{
Josh Coalson423f8042007-01-28 17:40:26 +00003078 FLAC__uint16 crc;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003079 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003080
3081 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003082 * Accumulate raw signal to the MD5 signature
3083 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00003084 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 +00003085 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003086 return false;
3087 }
3088
3089 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003090 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003091 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003092 if(!process_subframes_(encoder, is_fractional_block)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003093 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003094 return false;
3095 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003096
3097 /*
3098 * Zero-pad the frame to a byte_boundary
3099 */
Josh Coalson423f8042007-01-28 17:40:26 +00003100 if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003101 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003102 return false;
3103 }
3104
3105 /*
Josh Coalson215af572001-03-27 01:15:58 +00003106 * CRC-16 the whole thing
3107 */
Josh Coalson423f8042007-01-28 17:40:26 +00003108 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
3109 if(
3110 !FLAC__bitwriter_get_write_crc16(encoder->private_->frame, &crc) ||
3111 !FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN)
3112 ) {
3113 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3114 return false;
3115 }
Josh Coalson215af572001-03-27 01:15:58 +00003116
3117 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003118 * Write it
3119 */
Josh Coalson49f2f162006-11-09 16:54:52 +00003120 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00003121 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003122 return false;
3123 }
3124
3125 /*
3126 * Get ready for the next frame
3127 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003128 encoder->private_->current_sample_number = 0;
3129 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00003130 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003131
3132 return true;
3133}
3134
Josh Coalson85aaed82006-11-09 01:19:13 +00003135FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003136{
3137 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003138 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson1109e7f2007-01-24 04:26:15 +00003139 FLAC__bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003140
3141 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00003142 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00003143 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003144 if(is_fractional_block) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003145 max_partition_order = 0;
3146 }
3147 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003148 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
3149 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003150 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003151 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003152
3153 /*
3154 * Setup the frame
3155 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003156 frame_header.blocksize = encoder->protected_->blocksize;
3157 frame_header.sample_rate = encoder->protected_->sample_rate;
3158 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003159 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003160 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003161 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003162 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003163
3164 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003165 * Figure out what channel assignments to try
3166 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003167 if(encoder->protected_->do_mid_side_stereo) {
3168 if(encoder->protected_->loose_mid_side_stereo) {
3169 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003170 do_independent = true;
3171 do_mid_side = true;
3172 }
3173 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003174 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003175 do_mid_side = !do_independent;
3176 }
3177 }
3178 else {
3179 do_independent = true;
3180 do_mid_side = true;
3181 }
3182 }
3183 else {
3184 do_independent = true;
3185 do_mid_side = false;
3186 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003187
Josh Coalson1b689822001-05-31 20:11:02 +00003188 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003189
3190 /*
Josh Coalson82b73242001-03-28 22:17:05 +00003191 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00003192 */
3193 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003194 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003195 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003196 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3197 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00003198 }
Josh Coalson859bc542001-03-27 22:22:27 +00003199 }
3200 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003201 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00003202 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003203 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003204 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3205 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00003206 }
Josh Coalson859bc542001-03-27 22:22:27 +00003207 }
3208
3209 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003210 * First do a normal encoding pass of each independent channel
3211 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003212 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003213 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003214 if(!
3215 process_subframe_(
3216 encoder,
3217 min_partition_order,
3218 max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003219 &frame_header,
3220 encoder->private_->subframe_bps[channel],
3221 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003222#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003223 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003224#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003225 encoder->private_->subframe_workspace_ptr[channel],
3226 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3227 encoder->private_->residual_workspace[channel],
3228 encoder->private_->best_subframe+channel,
3229 encoder->private_->best_subframe_bits+channel
3230 )
3231 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003232 return false;
3233 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003234 }
3235
3236 /*
3237 * Now do mid and side channels if requested
3238 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003239 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003240 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003241
3242 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003243 if(!
3244 process_subframe_(
3245 encoder,
3246 min_partition_order,
3247 max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003248 &frame_header,
3249 encoder->private_->subframe_bps_mid_side[channel],
3250 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003251#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003252 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003253#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003254 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3255 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3256 encoder->private_->residual_workspace_mid_side[channel],
3257 encoder->private_->best_subframe_mid_side+channel,
3258 encoder->private_->best_subframe_bits_mid_side+channel
3259 )
3260 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00003261 return false;
3262 }
3263 }
3264
3265 /*
3266 * Compose the frame bitbuffer
3267 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003268 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00003269 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3270 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003271 FLAC__ChannelAssignment channel_assignment;
3272
Josh Coalsonfa697a92001-08-16 20:07:29 +00003273 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003274
Josh Coalsonfa697a92001-08-16 20:07:29 +00003275 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3276 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 +00003277 }
3278 else {
3279 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3280 unsigned min_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003281 int ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003282
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003283 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0);
3284 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE == 1);
3285 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE == 2);
3286 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE == 3);
Josh Coalson1b689822001-05-31 20:11:02 +00003287 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003288
3289 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003290 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3291 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3292 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3293 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 +00003294
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003295 channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
3296 min_bits = bits[channel_assignment];
3297 for(ca = 1; ca <= 3; ca++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003298 if(bits[ca] < min_bits) {
3299 min_bits = bits[ca];
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003300 channel_assignment = (FLAC__ChannelAssignment)ca;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003301 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003302 }
3303 }
3304
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003305 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003306
Josh Coalsond0edb972006-10-07 06:50:08 +00003307 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003308 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003309 return false;
3310 }
3311
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003312 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003313 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003314 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3315 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003316 break;
3317 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003318 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3319 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003320 break;
3321 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003322 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3323 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003324 break;
3325 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003326 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3327 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003328 break;
3329 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003330 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003331 }
Josh Coalson82b73242001-03-28 22:17:05 +00003332
3333 switch(channel_assignment) {
3334 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003335 left_bps = encoder->private_->subframe_bps [0];
3336 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003337 break;
3338 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003339 left_bps = encoder->private_->subframe_bps [0];
3340 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003341 break;
3342 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003343 left_bps = encoder->private_->subframe_bps_mid_side[1];
3344 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003345 break;
3346 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003347 left_bps = encoder->private_->subframe_bps_mid_side[0];
3348 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003349 break;
3350 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003351 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00003352 }
3353
3354 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003355 if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003356 return false;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003357 if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003358 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003359 }
3360 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00003361 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003362 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003363 return false;
3364 }
3365
Josh Coalsonfa697a92001-08-16 20:07:29 +00003366 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003367 if(!add_subframe_(encoder, frame_header.blocksize, 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 +00003368 /* the above function sets the state for us in case of an error */
3369 return false;
3370 }
3371 }
3372 }
3373
Josh Coalsonfa697a92001-08-16 20:07:29 +00003374 if(encoder->protected_->loose_mid_side_stereo) {
3375 encoder->private_->loose_mid_side_stereo_frame_count++;
3376 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3377 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003378 }
3379
Josh Coalsonfa697a92001-08-16 20:07:29 +00003380 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003381
Josh Coalson94e02cd2001-01-25 10:41:06 +00003382 return true;
3383}
3384
Josh Coalson6fe72f72002-08-20 04:01:59 +00003385FLAC__bool process_subframe_(
3386 FLAC__StreamEncoder *encoder,
3387 unsigned min_partition_order,
3388 unsigned max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003389 const FLAC__FrameHeader *frame_header,
3390 unsigned subframe_bps,
3391 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003392#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003393 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003394#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003395 FLAC__Subframe *subframe[2],
3396 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3397 FLAC__int32 *residual[2],
3398 unsigned *best_subframe,
3399 unsigned *best_bits
3400)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003401{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003402#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003403 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003404#else
3405 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3406#endif
3407#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003408 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003409 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 +00003410 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003411 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003412 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003413#endif
3414 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003415 unsigned rice_parameter;
3416 unsigned _candidate_bits, _best_bits;
3417 unsigned _best_subframe;
3418
Josh Coalson423f8042007-01-28 17:40:26 +00003419 FLAC__ASSERT(frame_header->blocksize > 0);
3420
Josh Coalson94e02cd2001-01-25 10:41:06 +00003421 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3422 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003423 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3424 _best_bits = UINT_MAX;
3425 else
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003426 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003427
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003428 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3429 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003430 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 +00003431 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003432 if(
3433 !encoder->private_->disable_constant_subframes &&
3434#ifndef FLAC__INTEGER_ONLY_LIBRARY
3435 fixed_residual_bits_per_sample[1] == 0.0
3436#else
3437 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3438#endif
3439 ) {
3440 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003441 unsigned i;
3442 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003443 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003444 if(integer_signal[0] != integer_signal[i]) {
3445 signal_is_constant = false;
3446 break;
3447 }
3448 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003449 }
3450 if(signal_is_constant) {
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003451 _candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003452 if(_candidate_bits < _best_bits) {
3453 _best_subframe = !_best_subframe;
3454 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003455 }
3456 }
3457 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003458 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3459 /* encode fixed */
3460 if(encoder->protected_->do_exhaustive_model_search) {
3461 min_fixed_order = 0;
3462 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00003463 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003464 else {
3465 min_fixed_order = max_fixed_order = guess_fixed_order;
3466 }
Josh Coalson423f8042007-01-28 17:40:26 +00003467 if(max_fixed_order >= frame_header->blocksize)
3468 max_fixed_order = frame_header->blocksize - 1;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003469 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003470#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003471 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003472 continue; /* don't even try */
3473 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 +00003474#else
3475 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3476 continue; /* don't even try */
3477 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 */
3478#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003479 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003480 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3481#ifdef DEBUG_VERBOSE
3482 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3483#endif
3484 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3485 }
3486 _candidate_bits =
3487 evaluate_fixed_subframe_(
3488 encoder,
3489 integer_signal,
3490 residual[!_best_subframe],
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003491 encoder->private_->abs_residual_partition_sums,
3492 encoder->private_->raw_bits_per_partition,
3493 frame_header->blocksize,
3494 subframe_bps,
3495 fixed_order,
3496 rice_parameter,
3497 min_partition_order,
3498 max_partition_order,
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003499 encoder->protected_->do_escape_coding,
3500 encoder->protected_->rice_parameter_search_dist,
3501 subframe[!_best_subframe],
3502 partitioned_rice_contents[!_best_subframe]
3503 );
3504 if(_candidate_bits < _best_bits) {
3505 _best_subframe = !_best_subframe;
3506 _best_bits = _candidate_bits;
3507 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003508 }
3509 }
3510
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003511#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00003512 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003513 if(encoder->protected_->max_lpc_order > 0) {
3514 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003515 max_lpc_order = frame_header->blocksize-1;
3516 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00003517 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003518 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003519 unsigned a;
3520 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00003521 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003522 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3523 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3524 if(autoc[0] != 0.0) {
Josh Coalson32b9bae2006-11-27 16:27:41 +00003525 FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003526 if(encoder->protected_->do_exhaustive_model_search) {
3527 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003528 }
3529 else {
Josh Coalsondf598452006-04-28 00:13:34 +00003530 const unsigned guess_lpc_order =
3531 FLAC__lpc_compute_best_order(
3532 lpc_error,
3533 max_lpc_order,
3534 frame_header->blocksize,
3535 subframe_bps + (
3536 encoder->protected_->do_qlp_coeff_prec_search?
3537 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3538 encoder->protected_->qlp_coeff_precision
3539 )
3540 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003541 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003542 }
Josh Coalson423f8042007-01-28 17:40:26 +00003543 if(max_lpc_order >= frame_header->blocksize)
3544 max_lpc_order = frame_header->blocksize - 1;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003545 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3546 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3547 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3548 continue; /* don't even try */
3549 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3550 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3551 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00003552#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003553 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 +00003554#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003555 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3556 }
3557 if(encoder->protected_->do_qlp_coeff_prec_search) {
3558 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00003559 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3560 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003561 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00003562 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3563 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003564 else
3565 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3566 }
3567 else {
3568 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3569 }
3570 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3571 _candidate_bits =
3572 evaluate_lpc_subframe_(
3573 encoder,
3574 integer_signal,
3575 residual[!_best_subframe],
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003576 encoder->private_->abs_residual_partition_sums,
3577 encoder->private_->raw_bits_per_partition,
3578 encoder->private_->lp_coeff[lpc_order-1],
3579 frame_header->blocksize,
3580 subframe_bps,
3581 lpc_order,
3582 qlp_coeff_precision,
3583 rice_parameter,
3584 min_partition_order,
3585 max_partition_order,
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003586 encoder->protected_->do_escape_coding,
3587 encoder->protected_->rice_parameter_search_dist,
3588 subframe[!_best_subframe],
3589 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003590 );
3591 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3592 if(_candidate_bits < _best_bits) {
3593 _best_subframe = !_best_subframe;
3594 _best_bits = _candidate_bits;
3595 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00003596 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003597 }
3598 }
3599 }
3600 }
3601 }
3602 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003603#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00003604 }
3605 }
3606
Josh Coalson72695802002-10-11 06:25:16 +00003607 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3608 if(_best_bits == UINT_MAX) {
3609 FLAC__ASSERT(_best_subframe == 0);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003610 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson72695802002-10-11 06:25:16 +00003611 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003612
Josh Coalson94e02cd2001-01-25 10:41:06 +00003613 *best_subframe = _best_subframe;
3614 *best_bits = _best_bits;
3615
3616 return true;
3617}
3618
Josh Coalson6fe72f72002-08-20 04:01:59 +00003619FLAC__bool add_subframe_(
3620 FLAC__StreamEncoder *encoder,
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003621 unsigned blocksize,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003622 unsigned subframe_bps,
3623 const FLAC__Subframe *subframe,
Josh Coalson423f8042007-01-28 17:40:26 +00003624 FLAC__BitWriter *frame
Josh Coalson6fe72f72002-08-20 04:01:59 +00003625)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003626{
3627 switch(subframe->type) {
3628 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003629 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003630 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003631 return false;
3632 }
3633 break;
3634 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003635 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003636 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003637 return false;
3638 }
3639 break;
3640 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003641 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003642 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003643 return false;
3644 }
3645 break;
3646 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003647 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003648 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003649 return false;
3650 }
3651 break;
3652 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003653 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003654 }
3655
3656 return true;
3657}
3658
Josh Coalson469ba742007-02-03 02:52:32 +00003659#define SPOTCHECK_ESTIMATE 0
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003660#if SPOTCHECK_ESTIMATE
3661static void spotcheck_subframe_estimate_(
3662 FLAC__StreamEncoder *encoder,
3663 unsigned blocksize,
3664 unsigned subframe_bps,
3665 const FLAC__Subframe *subframe,
3666 unsigned estimate
3667)
3668{
3669 FLAC__bool ret;
Josh Coalson423f8042007-01-28 17:40:26 +00003670 FLAC__BitWriter *frame = FLAC__bitwriter_new();
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003671 if(frame == 0) {
3672 fprintf(stderr, "EST: can't allocate frame\n");
3673 return;
3674 }
Josh Coalson423f8042007-01-28 17:40:26 +00003675 if(!FLAC__bitwriter_init(frame)) {
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003676 fprintf(stderr, "EST: can't init frame\n");
3677 return;
3678 }
3679 ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
3680 FLAC__ASSERT(ret);
3681 {
Josh Coalson423f8042007-01-28 17:40:26 +00003682 const unsigned actual = FLAC__bitwriter_get_input_bits_unconsumed(frame);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003683 if(estimate != actual)
3684 fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
3685 }
Josh Coalson423f8042007-01-28 17:40:26 +00003686 FLAC__bitwriter_delete(frame);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003687}
3688#endif
3689
Josh Coalson6fe72f72002-08-20 04:01:59 +00003690unsigned evaluate_constant_subframe_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003691 FLAC__StreamEncoder *encoder,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003692 const FLAC__int32 signal,
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003693 unsigned blocksize,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003694 unsigned subframe_bps,
3695 FLAC__Subframe *subframe
3696)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003697{
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003698 unsigned estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003699 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3700 subframe->data.constant.value = signal;
3701
Josh Coalson423f8042007-01-28 17:40:26 +00003702 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003703
3704#if SPOTCHECK_ESTIMATE
3705 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3706#else
3707 (void)encoder, (void)blocksize;
3708#endif
3709
3710 return estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003711}
3712
Josh Coalson6fe72f72002-08-20 04:01:59 +00003713unsigned evaluate_fixed_subframe_(
3714 FLAC__StreamEncoder *encoder,
3715 const FLAC__int32 signal[],
3716 FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003717 FLAC__uint64 abs_residual_partition_sums[],
3718 unsigned raw_bits_per_partition[],
3719 unsigned blocksize,
3720 unsigned subframe_bps,
3721 unsigned order,
3722 unsigned rice_parameter,
3723 unsigned min_partition_order,
3724 unsigned max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003725 FLAC__bool do_escape_coding,
3726 unsigned rice_parameter_search_dist,
3727 FLAC__Subframe *subframe,
3728 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3729)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003730{
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003731 unsigned i, residual_bits, estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003732 const unsigned residual_samples = blocksize - order;
3733
3734 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3735
3736 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3737
3738 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003739 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003740 subframe->data.fixed.residual = residual;
3741
Josh Coalson6fe72f72002-08-20 04:01:59 +00003742 residual_bits =
3743 find_best_partition_order_(
3744 encoder->private_,
3745 residual,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003746 abs_residual_partition_sums,
3747 raw_bits_per_partition,
3748 residual_samples,
3749 order,
3750 rice_parameter,
3751 min_partition_order,
3752 max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003753 do_escape_coding,
3754 rice_parameter_search_dist,
3755 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3756 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003757
3758 subframe->data.fixed.order = order;
3759 for(i = 0; i < order; i++)
3760 subframe->data.fixed.warmup[i] = signal[i];
3761
Josh Coalson423f8042007-01-28 17:40:26 +00003762 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps) + residual_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003763
3764#if SPOTCHECK_ESTIMATE
3765 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3766#endif
3767
3768 return estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003769}
3770
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003771#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003772unsigned evaluate_lpc_subframe_(
3773 FLAC__StreamEncoder *encoder,
3774 const FLAC__int32 signal[],
3775 FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003776 FLAC__uint64 abs_residual_partition_sums[],
3777 unsigned raw_bits_per_partition[],
3778 const FLAC__real lp_coeff[],
3779 unsigned blocksize,
3780 unsigned subframe_bps,
3781 unsigned order,
3782 unsigned qlp_coeff_precision,
3783 unsigned rice_parameter,
3784 unsigned min_partition_order,
3785 unsigned max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003786 FLAC__bool do_escape_coding,
3787 unsigned rice_parameter_search_dist,
3788 FLAC__Subframe *subframe,
3789 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3790)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003791{
Josh Coalson77e3f312001-06-23 03:03:24 +00003792 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003793 unsigned i, residual_bits, estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003794 int quantization, ret;
3795 const unsigned residual_samples = blocksize - order;
3796
Josh Coalson20ac2c12002-08-30 05:47:14 +00003797 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3798 if(subframe_bps <= 16) {
3799 FLAC__ASSERT(order > 0);
3800 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3801 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3802 }
3803
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003804 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003805 if(ret != 0)
3806 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3807
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003808 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3809 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3810 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3811 else
3812 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 +00003813 else
3814 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 +00003815
3816 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3817
3818 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003819 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003820 subframe->data.lpc.residual = residual;
3821
Josh Coalson6fe72f72002-08-20 04:01:59 +00003822 residual_bits =
3823 find_best_partition_order_(
3824 encoder->private_,
3825 residual,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003826 abs_residual_partition_sums,
3827 raw_bits_per_partition,
3828 residual_samples,
3829 order,
3830 rice_parameter,
3831 min_partition_order,
3832 max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003833 do_escape_coding,
3834 rice_parameter_search_dist,
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003835 &subframe->data.lpc.entropy_coding_method.data.partitioned_rice
Josh Coalson6fe72f72002-08-20 04:01:59 +00003836 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003837
3838 subframe->data.lpc.order = order;
3839 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3840 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003841 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003842 for(i = 0; i < order; i++)
3843 subframe->data.lpc.warmup[i] = signal[i];
3844
Josh Coalson423f8042007-01-28 17:40:26 +00003845 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003846
3847#if SPOTCHECK_ESTIMATE
3848 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3849#endif
3850
3851 return estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003852}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003853#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003854
Josh Coalson6fe72f72002-08-20 04:01:59 +00003855unsigned evaluate_verbatim_subframe_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003856 FLAC__StreamEncoder *encoder,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003857 const FLAC__int32 signal[],
3858 unsigned blocksize,
3859 unsigned subframe_bps,
3860 FLAC__Subframe *subframe
3861)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003862{
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003863 unsigned estimate;
3864
Josh Coalson94e02cd2001-01-25 10:41:06 +00003865 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3866
3867 subframe->data.verbatim.data = signal;
3868
Josh Coalson423f8042007-01-28 17:40:26 +00003869 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003870
3871#if SPOTCHECK_ESTIMATE
3872 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3873#else
3874 (void)encoder;
3875#endif
3876
3877 return estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003878}
3879
Josh Coalson6fe72f72002-08-20 04:01:59 +00003880unsigned find_best_partition_order_(
3881 FLAC__StreamEncoderPrivate *private_,
3882 const FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003883 FLAC__uint64 abs_residual_partition_sums[],
3884 unsigned raw_bits_per_partition[],
3885 unsigned residual_samples,
3886 unsigned predictor_order,
3887 unsigned rice_parameter,
3888 unsigned min_partition_order,
3889 unsigned max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003890 FLAC__bool do_escape_coding,
3891 unsigned rice_parameter_search_dist,
3892 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3893)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003894{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003895 unsigned residual_bits, best_residual_bits = 0;
Josh Coalson8084b052001-11-01 00:27:29 +00003896 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003897 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003898
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003899 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 +00003900 min_partition_order = min(min_partition_order, max_partition_order);
3901
Josh Coalson1109e7f2007-01-24 04:26:15 +00003902 precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
3903
3904 if(do_escape_coding)
3905 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
3906
3907 {
Josh Coalson8395d022001-07-12 21:25:22 +00003908 int partition_order;
3909 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003910
Josh Coalson8395d022001-07-12 21:25:22 +00003911 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003912 if(!
Josh Coalson1109e7f2007-01-24 04:26:15 +00003913 set_partitioned_rice_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003914#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson6fe72f72002-08-20 04:01:59 +00003915 residual,
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003916#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003917 abs_residual_partition_sums+sum,
3918 raw_bits_per_partition+sum,
3919 residual_samples,
3920 predictor_order,
3921 rice_parameter,
3922 rice_parameter_search_dist,
3923 (unsigned)partition_order,
3924 do_escape_coding,
3925 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3926 &residual_bits
3927 )
3928 )
Josh Coalson8395d022001-07-12 21:25:22 +00003929 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003930 FLAC__ASSERT(best_residual_bits != 0);
3931 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003932 }
3933 sum += 1u << partition_order;
3934 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3935 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003936 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003937 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003938 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003939 }
3940 }
Josh Coalson8395d022001-07-12 21:25:22 +00003941
Josh Coalsona37ba462002-08-19 21:36:39 +00003942 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003943 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003944 * it is const to the outside world.
3945 */
3946 {
3947 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3948 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3949 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3950 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3951 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003952
3953 return best_residual_bits;
3954}
3955
Josh Coalson6fe72f72002-08-20 04:01:59 +00003956void precompute_partition_info_sums_(
Josh Coalson1109e7f2007-01-24 04:26:15 +00003957 const FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003958 FLAC__uint64 abs_residual_partition_sums[],
3959 unsigned residual_samples,
3960 unsigned predictor_order,
3961 unsigned min_partition_order,
3962 unsigned max_partition_order
3963)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003964{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003965 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003966 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003967 const unsigned blocksize = residual_samples + predictor_order;
3968
Josh Coalsonaef013c2001-04-24 01:25:42 +00003969 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003970 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalson1109e7f2007-01-24 04:26:15 +00003971 FLAC__uint64 abs_residual_partition_sum; /* OPT: can reasonably be FLAC__uint32 for bps <= 17 and maybe higher */
Josh Coalsonaef013c2001-04-24 01:25:42 +00003972 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003973 const unsigned partitions = 1u << partition_order;
3974 const unsigned default_partition_samples = blocksize >> partition_order;
3975
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003976 FLAC__ASSERT(default_partition_samples > predictor_order);
3977
3978 for(partition = residual_sample = 0; partition < partitions; partition++) {
3979 partition_samples = default_partition_samples;
3980 if(partition == 0)
3981 partition_samples -= predictor_order;
3982 abs_residual_partition_sum = 0;
Josh Coalson1109e7f2007-01-24 04:26:15 +00003983 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++, residual_sample++) {
Josh Coalson0edc1492007-03-01 16:23:11 +00003984#if defined _MSC_VER
Josh Coalson423f8042007-01-28 17:40:26 +00003985 /* OPT: abs() may be faster for some compilers */
Josh Coalson1109e7f2007-01-24 04:26:15 +00003986 abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
3987#else
3988 const FLAC__int32 r = residual[residual_sample];
3989 if(r < 0)
3990 abs_residual_partition_sum -= r;
3991 else
3992 abs_residual_partition_sum += r;
3993#endif
3994 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003995 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003996 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003997 to_partition = partitions;
3998 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003999 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00004000
Josh Coalson8395d022001-07-12 21:25:22 +00004001 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00004002 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004003 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00004004 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004005 const unsigned partitions = 1u << partition_order;
4006 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00004007 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00004008 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00004009 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00004010 from_partition++;
4011 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004012 }
4013 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00004014}
Josh Coalson8395d022001-07-12 21:25:22 +00004015
Josh Coalson6fe72f72002-08-20 04:01:59 +00004016void precompute_partition_info_escapes_(
4017 const FLAC__int32 residual[],
4018 unsigned raw_bits_per_partition[],
4019 unsigned residual_samples,
4020 unsigned predictor_order,
4021 unsigned min_partition_order,
4022 unsigned max_partition_order
4023)
Josh Coalson8395d022001-07-12 21:25:22 +00004024{
4025 int partition_order;
4026 unsigned from_partition, to_partition = 0;
4027 const unsigned blocksize = residual_samples + predictor_order;
4028
4029 /* first do max_partition_order */
4030 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalson90c693a2007-02-01 04:24:08 +00004031 FLAC__int32 r;
4032 FLAC__uint32 rmax;
Josh Coalson8395d022001-07-12 21:25:22 +00004033 unsigned partition, partition_sample, partition_samples, residual_sample;
4034 const unsigned partitions = 1u << partition_order;
4035 const unsigned default_partition_samples = blocksize >> partition_order;
4036
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004037 FLAC__ASSERT(default_partition_samples > predictor_order);
4038
4039 for(partition = residual_sample = 0; partition < partitions; partition++) {
4040 partition_samples = default_partition_samples;
4041 if(partition == 0)
4042 partition_samples -= predictor_order;
Josh Coalson90c693a2007-02-01 04:24:08 +00004043 rmax = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004044 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
Josh Coalson90c693a2007-02-01 04:24:08 +00004045 r = residual[residual_sample++];
Josh Coalson8534bbe2007-02-28 01:10:21 +00004046 /* OPT: maybe faster: rmax |= r ^ (r>>31) */
Josh Coalson90c693a2007-02-01 04:24:08 +00004047 if(r < 0)
4048 rmax |= ~r;
4049 else
4050 rmax |= r;
Josh Coalson8395d022001-07-12 21:25:22 +00004051 }
Josh Coalson90c693a2007-02-01 04:24:08 +00004052 /* now we know all residual values are in the range [-rmax-1,rmax] */
4053 raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004054 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004055 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00004056 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00004057 }
4058
4059 /* now merge partitions for lower orders */
4060 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
4061 unsigned m;
4062 unsigned i;
4063 const unsigned partitions = 1u << partition_order;
4064 for(i = 0; i < partitions; i++) {
4065 m = raw_bits_per_partition[from_partition];
4066 from_partition++;
4067 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
4068 from_partition++;
4069 to_partition++;
4070 }
4071 }
4072}
Josh Coalson94e02cd2001-01-25 10:41:06 +00004073
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004074#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson14b184c2007-03-12 05:08:21 +00004075static FLaC__INLINE unsigned count_rice_bits_in_partition_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004076 const unsigned rice_parameter,
4077 const unsigned partition_samples,
4078 const FLAC__int32 *residual
Josh Coalson6fe72f72002-08-20 04:01:59 +00004079)
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004080{
Josh Coalson423f8042007-01-28 17:40:26 +00004081 unsigned i, partition_bits =
4082 FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN +
4083 (1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
4084 ;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004085 for(i = 0; i < partition_samples; i++)
Josh Coalson423f8042007-01-28 17:40:26 +00004086 partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004087 return partition_bits;
4088}
Josh Coalson8395d022001-07-12 21:25:22 +00004089#else
Josh Coalson14b184c2007-03-12 05:08:21 +00004090static FLaC__INLINE unsigned count_rice_bits_in_partition_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004091 const unsigned rice_parameter,
4092 const unsigned partition_samples,
Josh Coalson1109e7f2007-01-24 04:26:15 +00004093 const FLAC__uint64 abs_residual_partition_sum
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004094)
4095{
Josh Coalson1109e7f2007-01-24 04:26:15 +00004096 return
4097 FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN +
4098 (1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
4099 (
4100 rice_parameter?
Josh Coalson423f8042007-01-28 17:40:26 +00004101 (unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
4102 : (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
Josh Coalson1109e7f2007-01-24 04:26:15 +00004103 )
4104 - (partition_samples >> 1)
4105 /* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
Josh Coalson5981e4f2007-03-06 06:51:57 +00004106 * The actual number of bits used is closer to the sum(for all i in the partition) of abs(residual[i])>>(rice_parameter-1)
Josh Coalson1109e7f2007-01-24 04:26:15 +00004107 * By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out.
4108 * So the subtraction term tries to guess how many extra bits were contributed.
4109 * If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample.
4110 */
4111 ;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004112}
4113#endif
4114
Josh Coalson6fe72f72002-08-20 04:01:59 +00004115FLAC__bool set_partitioned_rice_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004116#ifdef EXACT_RICE_BITS_CALCULATION
4117 const FLAC__int32 residual[],
4118#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00004119 const FLAC__uint64 abs_residual_partition_sums[],
4120 const unsigned raw_bits_per_partition[],
4121 const unsigned residual_samples,
4122 const unsigned predictor_order,
4123 const unsigned suggested_rice_parameter,
4124 const unsigned rice_parameter_search_dist,
4125 const unsigned partition_order,
4126 const FLAC__bool search_for_escapes,
4127 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4128 unsigned *bits
4129)
Josh Coalson8395d022001-07-12 21:25:22 +00004130{
4131 unsigned rice_parameter, partition_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004132 unsigned best_partition_bits, best_rice_parameter = 0;
Josh Coalson8395d022001-07-12 21:25:22 +00004133 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004134 unsigned *parameters, *raw_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004135#ifdef ENABLE_RICE_PARAMETER_SEARCH
4136 unsigned min_rice_parameter, max_rice_parameter;
4137#else
4138 (void)rice_parameter_search_dist;
4139#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004140
4141 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4142
Josh Coalsona37ba462002-08-19 21:36:39 +00004143 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4144 parameters = partitioned_rice_contents->parameters;
4145 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004146
Josh Coalson8395d022001-07-12 21:25:22 +00004147 if(partition_order == 0) {
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004148 best_partition_bits = 0xffffffff;
4149#ifdef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalson8395d022001-07-12 21:25:22 +00004150 if(rice_parameter_search_dist) {
4151 if(suggested_rice_parameter < rice_parameter_search_dist)
4152 min_rice_parameter = 0;
4153 else
4154 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4155 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4156 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004157#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004158 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4159#endif
4160 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4161 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004162 }
4163 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004164 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00004165
Josh Coalson034dfab2001-04-27 19:10:23 +00004166 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004167#else
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004168 rice_parameter = suggested_rice_parameter;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004169#endif
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004170#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson1109e7f2007-01-24 04:26:15 +00004171 partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004172#else
Josh Coalson1109e7f2007-01-24 04:26:15 +00004173 partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00004174#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004175 if(partition_bits < best_partition_bits) {
4176 best_rice_parameter = rice_parameter;
4177 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00004178 }
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004179#ifdef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalson352e0f62001-03-20 22:55:50 +00004180 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004181#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004182 if(search_for_escapes) {
Josh Coalson90c693a2007-02-01 04:24:08 +00004183 partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
4184 if(partition_bits <= best_partition_bits) {
Josh Coalson8395d022001-07-12 21:25:22 +00004185 raw_bits[0] = raw_bits_per_partition[0];
4186 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
Josh Coalson90c693a2007-02-01 04:24:08 +00004187 best_partition_bits = partition_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004188 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004189 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004190 parameters[0] = best_rice_parameter;
4191 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004192 }
4193 else {
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004194 unsigned partition, residual_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004195 unsigned partition_samples;
4196 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004197 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00004198 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004199 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00004200 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004201 if(partition_samples <= predictor_order)
4202 return false;
4203 else
4204 partition_samples -= predictor_order;
4205 }
Josh Coalson05d20792001-06-29 23:12:26 +00004206 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004207 /* we are basically calculating the size in bits of the
4208 * average residual magnitude in the partition:
4209 * rice_parameter = floor(log2(mean/partition_samples))
4210 * 'mean' is not a good name for the variable, it is
4211 * actually the sum of magnitudes of all residual values
4212 * in the partition, so the actual mean is
4213 * mean/partition_samples
4214 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004215 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00004216 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004217 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004218#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004219 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4220#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004221 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004222 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004223
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004224 best_partition_bits = 0xffffffff;
4225#ifdef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004226 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004227 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004228 min_rice_parameter = 0;
4229 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004230 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4231 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004232 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004233#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004234 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4235#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004236 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004237 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004238 }
4239 else
4240 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00004241
Josh Coalson034dfab2001-04-27 19:10:23 +00004242 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4243#endif
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004244#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson1109e7f2007-01-24 04:26:15 +00004245 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample);
Josh Coalson034dfab2001-04-27 19:10:23 +00004246#else
Josh Coalson1109e7f2007-01-24 04:26:15 +00004247 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00004248#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004249 if(partition_bits < best_partition_bits) {
4250 best_rice_parameter = rice_parameter;
4251 best_partition_bits = partition_bits;
4252 }
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004253#ifdef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalson2051dd42001-04-12 22:22:34 +00004254 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004255#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004256 if(search_for_escapes) {
Josh Coalson90c693a2007-02-01 04:24:08 +00004257 partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
4258 if(partition_bits <= best_partition_bits) {
Josh Coalson8395d022001-07-12 21:25:22 +00004259 raw_bits[partition] = raw_bits_per_partition[partition];
4260 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
Josh Coalson90c693a2007-02-01 04:24:08 +00004261 best_partition_bits = partition_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004262 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004263 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004264 parameters[partition] = best_rice_parameter;
4265 bits_ += best_partition_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004266 residual_sample += partition_samples;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004267 }
4268 }
4269
4270 *bits = bits_;
4271 return true;
4272}
Josh Coalson859bc542001-03-27 22:22:27 +00004273
Josh Coalsonf1eff452002-07-31 07:05:33 +00004274unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00004275{
4276 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00004277 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00004278
4279 for(i = 0; i < samples && !(x&1); i++)
4280 x |= signal[i];
4281
4282 if(x == 0) {
4283 shift = 0;
4284 }
4285 else {
4286 for(shift = 0; !(x&1); shift++)
4287 x >>= 1;
4288 }
4289
4290 if(shift > 0) {
4291 for(i = 0; i < samples; i++)
4292 signal[i] >>= shift;
4293 }
4294
4295 return shift;
4296}
Josh Coalsond86e03b2002-08-03 21:56:15 +00004297
4298void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4299{
4300 unsigned channel;
4301
4302 for(channel = 0; channel < channels; channel++)
4303 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4304
4305 fifo->tail += wide_samples;
4306
4307 FLAC__ASSERT(fifo->tail <= fifo->size);
4308}
4309
4310void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4311{
4312 unsigned channel;
4313 unsigned sample, wide_sample;
4314 unsigned tail = fifo->tail;
4315
4316 sample = input_offset * channels;
4317 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4318 for(channel = 0; channel < channels; channel++)
4319 fifo->data[channel][tail] = input[sample++];
4320 tail++;
4321 }
4322 fifo->tail = tail;
4323
4324 FLAC__ASSERT(fifo->tail <= fifo->size);
4325}
4326
Josh Coalson8065a2d2006-10-15 08:32:56 +00004327FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalsond86e03b2002-08-03 21:56:15 +00004328{
4329 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
Josh Coalson8065a2d2006-10-15 08:32:56 +00004330 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004331 (void)decoder;
4332
4333 if(encoder->private_->verify.needs_magic_hack) {
4334 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4335 *bytes = FLAC__STREAM_SYNC_LENGTH;
4336 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4337 encoder->private_->verify.needs_magic_hack = false;
4338 }
4339 else {
4340 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00004341 /*
4342 * If we get here, a FIFO underflow has occurred,
4343 * which means there is a bug somewhere.
4344 */
4345 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004346 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4347 }
4348 else if(encoded_bytes < *bytes)
4349 *bytes = encoded_bytes;
4350 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4351 encoder->private_->verify.output.data += *bytes;
4352 encoder->private_->verify.output.bytes -= *bytes;
4353 }
4354
4355 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4356}
4357
4358FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4359{
4360 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4361 unsigned channel;
Josh Coalson49f2f162006-11-09 16:54:52 +00004362 const unsigned channels = frame->header.channels;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004363 const unsigned blocksize = frame->header.blocksize;
4364 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4365
Josh Coalson49f2f162006-11-09 16:54:52 +00004366 (void)decoder;
4367
Josh Coalsond86e03b2002-08-03 21:56:15 +00004368 for(channel = 0; channel < channels; channel++) {
4369 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4370 unsigned i, sample = 0;
4371 FLAC__int32 expect = 0, got = 0;
4372
4373 for(i = 0; i < blocksize; i++) {
4374 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4375 sample = i;
4376 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4377 got = (FLAC__int32)buffer[channel][i];
4378 break;
4379 }
4380 }
4381 FLAC__ASSERT(i < blocksize);
4382 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4383 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00004384 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004385 encoder->private_->verify.error_stats.channel = channel;
4386 encoder->private_->verify.error_stats.sample = sample;
4387 encoder->private_->verify.error_stats.expected = expect;
4388 encoder->private_->verify.error_stats.got = got;
4389 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4390 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4391 }
4392 }
4393 /* dequeue the frame from the fifo */
Josh Coalsond86e03b2002-08-03 21:56:15 +00004394 encoder->private_->verify.input_fifo.tail -= blocksize;
Josh Coalson49f2f162006-11-09 16:54:52 +00004395 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
Josh Coalsonb7b57ef2006-11-09 07:06:33 +00004396 for(channel = 0; channel < channels; channel++)
4397 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 +00004398 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4399}
4400
4401void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4402{
4403 (void)decoder, (void)metadata, (void)client_data;
4404}
4405
4406void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4407{
4408 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4409 (void)decoder, (void)status;
4410 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4411}
Josh Coalson6b21f662006-09-13 01:42:27 +00004412
Josh Coalson8065a2d2006-10-15 08:32:56 +00004413FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalson8da98c82006-10-15 04:24:05 +00004414{
4415 (void)client_data;
4416
Josh Coalson8065a2d2006-10-15 08:32:56 +00004417 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
Josh Coalson8da98c82006-10-15 04:24:05 +00004418 if (*bytes == 0) {
4419 if (feof(encoder->private_->file))
4420 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4421 else if (ferror(encoder->private_->file))
4422 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4423 }
4424 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4425}
4426
Josh Coalson6b21f662006-09-13 01:42:27 +00004427FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4428{
4429 (void)client_data;
4430
4431 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4432 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4433 else
4434 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4435}
4436
4437FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4438{
4439 off_t offset;
4440
4441 (void)client_data;
4442
4443 offset = ftello(encoder->private_->file);
4444
4445 if(offset < 0) {
4446 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4447 }
4448 else {
4449 *absolute_byte_offset = (FLAC__uint64)offset;
4450 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4451 }
4452}
4453
4454#ifdef FLAC__VALGRIND_TESTING
4455static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4456{
4457 size_t ret = fwrite(ptr, size, nmemb, stream);
4458 if(!ferror(stream))
4459 fflush(stream);
4460 return ret;
4461}
4462#else
4463#define local__fwrite fwrite
4464#endif
4465
Josh Coalson352feb52006-10-15 17:08:52 +00004466FLAC__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 +00004467{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004468 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00004469
4470 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson8da98c82006-10-15 04:24:05 +00004471 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4472#if FLAC__HAS_OGG
4473 /* We would like to be able to use 'samples > 0' in the
4474 * clause here but currently because of the nature of our
4475 * Ogg writing implementation, 'samples' is always 0 (see
4476 * ogg_encoder_aspect.c). The downside is extra progress
4477 * callbacks.
4478 */
4479 encoder->private_->is_ogg? true :
4480#endif
4481 samples > 0
4482 );
4483 if(call_it) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004484 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4485 * because at this point in the callback chain, the stats
4486 * have not been updated. Only after we return and control
4487 * gets back to write_frame_() are the stats updated
4488 */
4489 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);
4490 }
Josh Coalson6b21f662006-09-13 01:42:27 +00004491 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4492 }
4493 else
4494 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4495}
4496
4497/*
4498 * This will forcibly set stdout to binary mode (for OSes that require it)
4499 */
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00004500FILE *get_binary_stdout_(void)
Josh Coalson6b21f662006-09-13 01:42:27 +00004501{
4502 /* if something breaks here it is probably due to the presence or
4503 * absence of an underscore before the identifiers 'setmode',
4504 * 'fileno', and/or 'O_BINARY'; check your system header files.
4505 */
4506#if defined _MSC_VER || defined __MINGW32__
4507 _setmode(_fileno(stdout), _O_BINARY);
4508#elif defined __CYGWIN__
4509 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4510 setmode(_fileno(stdout), _O_BINARY);
4511#elif defined __EMX__
4512 setmode(fileno(stdout), O_BINARY);
4513#endif
4514
4515 return stdout;
4516}