blob: cf95d44062236f2decc27da981be1a31fa0c3853 [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson0395dac2006-04-25 06:59:33 +00002 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003 *
Josh Coalsonafd81072003-01-31 23:34:56 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00007 *
Josh Coalsonafd81072003-01-31 23:34:56 +00008 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000010 *
Josh Coalsonafd81072003-01-31 23:34:56 +000011 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000030 */
31
Josh Coalsonb1ec7962006-05-24 04:41:36 +000032#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
Josh Coalson6b21f662006-09-13 01:42:27 +000036#if defined _MSC_VER || defined __MINGW32__
37#include <io.h> /* for _setmode() */
38#include <fcntl.h> /* for _O_BINARY */
39#endif
40#if defined __CYGWIN__ || defined __EMX__
41#include <io.h> /* for setmode(), O_BINARY */
42#include <fcntl.h> /* for _O_BINARY */
43#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +000044#include <limits.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000045#include <stdio.h>
46#include <stdlib.h> /* for malloc() */
47#include <string.h> /* for memcpy() */
Josh Coalson6b21f662006-09-13 01:42:27 +000048#include <sys/types.h> /* for off_t */
49#if defined _MSC_VER || defined __MINGW32__
Josh Coalson825e72c2006-10-03 01:16:59 +000050#if _MSC_VER <= 1200 /* @@@ [2G limit] */
Josh Coalson6b21f662006-09-13 01:42:27 +000051#define fseeko fseek
52#define ftello ftell
53#endif
Josh Coalson825e72c2006-10-03 01:16:59 +000054#endif
Josh Coalson1b689822001-05-31 20:11:02 +000055#include "FLAC/assert.h"
Josh Coalsond86e03b2002-08-03 21:56:15 +000056#include "FLAC/stream_decoder.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000057#include "protected/stream_encoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000058#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000059#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000060#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000061#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000062#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000063#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000064#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000065#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000066#include "private/memory.h"
Josh Coalson8da98c82006-10-15 04:24:05 +000067#ifdef FLAC__HAS_OGG
68#include "private/ogg_helper.h"
69#endif
Josh Coalsonb7023aa2002-08-17 15:23:43 +000070#include "private/stream_encoder_framing.h"
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000071#include "private/window.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000072
73#ifdef min
74#undef min
75#endif
76#define min(x,y) ((x)<(y)?(x):(y))
77
78#ifdef max
79#undef max
80#endif
81#define max(x,y) ((x)>(y)?(x):(y))
82
Josh Coalsond86e03b2002-08-03 21:56:15 +000083typedef struct {
84 FLAC__int32 *data[FLAC__MAX_CHANNELS];
85 unsigned size; /* of each data[] in samples */
86 unsigned tail;
87} verify_input_fifo;
88
89typedef struct {
90 const FLAC__byte *data;
91 unsigned capacity;
92 unsigned bytes;
93} verify_output;
94
95typedef enum {
96 ENCODER_IN_MAGIC = 0,
97 ENCODER_IN_METADATA = 1,
98 ENCODER_IN_AUDIO = 2
99} EncoderStateHint;
100
Josh Coalson0a15c142001-06-13 17:59:57 +0000101/***********************************************************************
102 *
103 * Private class method prototypes
104 *
105 ***********************************************************************/
106
Josh Coalsonf1eff452002-07-31 07:05:33 +0000107static void set_defaults_(FLAC__StreamEncoder *encoder);
108static void free_(FLAC__StreamEncoder *encoder);
109static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
Josh Coalsond86e03b2002-08-03 21:56:15 +0000110static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
Josh Coalson8da98c82006-10-15 04:24:05 +0000111static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples);
Josh Coalson6b21f662006-09-13 01:42:27 +0000112static void update_metadata_(const FLAC__StreamEncoder *encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +0000113static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
Josh Coalsonf1eff452002-07-31 07:05:33 +0000114static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
115static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000116
117static FLAC__bool process_subframe_(
118 FLAC__StreamEncoder *encoder,
119 unsigned min_partition_order,
120 unsigned max_partition_order,
121 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000122 const FLAC__FrameHeader *frame_header,
123 unsigned subframe_bps,
124 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000125#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000126 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000127#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000128 FLAC__Subframe *subframe[2],
129 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
130 FLAC__int32 *residual[2],
131 unsigned *best_subframe,
132 unsigned *best_bits
133);
134
135static FLAC__bool add_subframe_(
136 FLAC__StreamEncoder *encoder,
137 const FLAC__FrameHeader *frame_header,
138 unsigned subframe_bps,
139 const FLAC__Subframe *subframe,
140 FLAC__BitBuffer *frame
141);
142
143static unsigned evaluate_constant_subframe_(
144 const FLAC__int32 signal,
145 unsigned subframe_bps,
146 FLAC__Subframe *subframe
147);
148
149static unsigned evaluate_fixed_subframe_(
150 FLAC__StreamEncoder *encoder,
151 const FLAC__int32 signal[],
152 FLAC__int32 residual[],
153 FLAC__uint32 abs_residual[],
154 FLAC__uint64 abs_residual_partition_sums[],
155 unsigned raw_bits_per_partition[],
156 unsigned blocksize,
157 unsigned subframe_bps,
158 unsigned order,
159 unsigned rice_parameter,
160 unsigned min_partition_order,
161 unsigned max_partition_order,
162 FLAC__bool precompute_partition_sums,
163 FLAC__bool do_escape_coding,
164 unsigned rice_parameter_search_dist,
165 FLAC__Subframe *subframe,
166 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
167);
168
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000169#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000170static unsigned evaluate_lpc_subframe_(
171 FLAC__StreamEncoder *encoder,
172 const FLAC__int32 signal[],
173 FLAC__int32 residual[],
174 FLAC__uint32 abs_residual[],
175 FLAC__uint64 abs_residual_partition_sums[],
176 unsigned raw_bits_per_partition[],
177 const FLAC__real lp_coeff[],
178 unsigned blocksize,
179 unsigned subframe_bps,
180 unsigned order,
181 unsigned qlp_coeff_precision,
182 unsigned rice_parameter,
183 unsigned min_partition_order,
184 unsigned max_partition_order,
185 FLAC__bool precompute_partition_sums,
186 FLAC__bool do_escape_coding,
187 unsigned rice_parameter_search_dist,
188 FLAC__Subframe *subframe,
189 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
190);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000191#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000192
193static unsigned evaluate_verbatim_subframe_(
194 const FLAC__int32 signal[],
195 unsigned blocksize,
196 unsigned subframe_bps,
197 FLAC__Subframe *subframe
198);
199
200static unsigned find_best_partition_order_(
201 struct FLAC__StreamEncoderPrivate *private_,
202 const FLAC__int32 residual[],
203 FLAC__uint32 abs_residual[],
204 FLAC__uint64 abs_residual_partition_sums[],
205 unsigned raw_bits_per_partition[],
206 unsigned residual_samples,
207 unsigned predictor_order,
208 unsigned rice_parameter,
209 unsigned min_partition_order,
210 unsigned max_partition_order,
211 FLAC__bool precompute_partition_sums,
212 FLAC__bool do_escape_coding,
213 unsigned rice_parameter_search_dist,
214 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
215);
216
217static void precompute_partition_info_sums_(
218 const FLAC__uint32 abs_residual[],
219 FLAC__uint64 abs_residual_partition_sums[],
220 unsigned residual_samples,
221 unsigned predictor_order,
222 unsigned min_partition_order,
223 unsigned max_partition_order
224);
225
226static void precompute_partition_info_escapes_(
227 const FLAC__int32 residual[],
228 unsigned raw_bits_per_partition[],
229 unsigned residual_samples,
230 unsigned predictor_order,
231 unsigned min_partition_order,
232 unsigned max_partition_order
233);
234
Josh Coalson8395d022001-07-12 21:25:22 +0000235#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000236static FLAC__bool set_partitioned_rice_(
237 const FLAC__uint32 abs_residual[],
238 const FLAC__int32 residual[],
239 const unsigned residual_samples,
240 const unsigned predictor_order,
241 const unsigned suggested_rice_parameter,
242 const unsigned rice_parameter_search_dist,
243 const unsigned partition_order,
244 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
245 unsigned *bits
246);
247
248static FLAC__bool set_partitioned_rice_with_precompute_(
249 const FLAC__int32 residual[],
250 const FLAC__uint64 abs_residual_partition_sums[],
251 const unsigned raw_bits_per_partition[],
252 const unsigned residual_samples,
253 const unsigned predictor_order,
254 const unsigned suggested_rice_parameter,
255 const unsigned rice_parameter_search_dist,
256 const unsigned partition_order,
257 const FLAC__bool search_for_escapes,
258 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
259 unsigned *bits
260);
Josh Coalson8395d022001-07-12 21:25:22 +0000261#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000262static FLAC__bool set_partitioned_rice_(
263 const FLAC__uint32 abs_residual[],
264 const unsigned residual_samples,
265 const unsigned predictor_order,
266 const unsigned suggested_rice_parameter,
267 const unsigned rice_parameter_search_dist,
268 const unsigned partition_order,
269 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
270 unsigned *bits
271);
272
273static FLAC__bool set_partitioned_rice_with_precompute_(
274 const FLAC__uint32 abs_residual[],
275 const FLAC__uint64 abs_residual_partition_sums[],
276 const unsigned raw_bits_per_partition[],
277 const unsigned residual_samples,
278 const unsigned predictor_order,
279 const unsigned suggested_rice_parameter,
280 const unsigned rice_parameter_search_dist,
281 const unsigned partition_order,
282 const FLAC__bool search_for_escapes,
283 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
284 unsigned *bits
285);
Josh Coalson0a15c142001-06-13 17:59:57 +0000286#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000287
Josh Coalsonf1eff452002-07-31 07:05:33 +0000288static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000289
Josh Coalsond86e03b2002-08-03 21:56:15 +0000290/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000291static void append_to_verify_fifo_(
292 verify_input_fifo *fifo,
293 const FLAC__int32 * const input[],
294 unsigned input_offset,
295 unsigned channels,
296 unsigned wide_samples
297);
298
299static void append_to_verify_fifo_interleaved_(
300 verify_input_fifo *fifo,
301 const FLAC__int32 input[],
302 unsigned input_offset,
303 unsigned channels,
304 unsigned wide_samples
305);
306
Josh Coalson6b21f662006-09-13 01:42:27 +0000307static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
308static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
309static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
310static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000311
Josh Coalson8da98c82006-10-15 04:24:05 +0000312static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
Josh Coalson6b21f662006-09-13 01:42:27 +0000313static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
314static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
315static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
316static FILE *get_binary_stdout_();
Josh Coalson6fe72f72002-08-20 04:01:59 +0000317
Josh Coalson0a15c142001-06-13 17:59:57 +0000318
319/***********************************************************************
320 *
321 * Private class data
322 *
323 ***********************************************************************/
324
325typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000326 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000327 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
328 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 +0000329#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000330 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
331 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 +0000332 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
333 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000334#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000335 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
336 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 +0000337 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
338 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000339 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
340 FLAC__Subframe subframe_workspace_mid_side[2][2];
341 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
342 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000343 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
344 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
345 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
346 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000347 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000348 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000349 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000350 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000351 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000352 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 +0000353 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
Josh Coalsonaec256b2002-03-12 16:19:54 +0000354 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000355 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
356 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000357 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalson6b21f662006-09-13 01:42:27 +0000358 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
359 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000360 unsigned current_sample_number;
361 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000362 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000363 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000364#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000365 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 +0000366#else
367 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
368#endif
369#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000370 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
Josh Coalson7446e182005-01-26 04:04:38 +0000371 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[]);
372 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[]);
373 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 +0000374#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000375 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
376 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
377 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
378 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +0000379 FLAC__bool disable_constant_subframes;
380 FLAC__bool disable_fixed_subframes;
381 FLAC__bool disable_verbatim_subframes;
Josh Coalson8da98c82006-10-15 04:24:05 +0000382#if FLAC__HAS_OGG
383 FLAC__bool is_ogg;
384#endif
385 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
Josh Coalson6b21f662006-09-13 01:42:27 +0000386 FLAC__StreamEncoderSeekCallback seek_callback;
387 FLAC__StreamEncoderTellCallback tell_callback;
Josh Coalson681c2932002-08-01 08:19:37 +0000388 FLAC__StreamEncoderWriteCallback write_callback;
389 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000390 FLAC__StreamEncoderProgressCallback progress_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000391 void *client_data;
Josh Coalson6b21f662006-09-13 01:42:27 +0000392 unsigned first_seekpoint_to_check;
393 FILE *file; /* only used when encoding to a file */
394 FLAC__uint64 bytes_written;
395 FLAC__uint64 samples_written;
396 unsigned frames_written;
397 unsigned total_frames_estimate;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000398 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000399 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
400 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000401#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000402 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
403 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000404 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
405 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000406#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000407 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
408 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
409 FLAC__uint32 *abs_residual_unaligned;
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",
470 "FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH",
Josh Coalson6b21f662006-09-13 01:42:27 +0000471 "FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE",
472 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
473 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
474 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
475 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000476};
477
Josh Coalson8da98c82006-10-15 04:24:05 +0000478FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
479 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
480 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
481 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
482 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
483};
484
Josh Coalson6afed9f2002-10-16 22:29:47 +0000485FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000486 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
487 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000488};
489
Josh Coalson6b21f662006-09-13 01:42:27 +0000490FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
491 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
492 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
493 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
494};
495
496FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
497 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
498 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
499 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
500};
501
Josh Coalson0a15c142001-06-13 17:59:57 +0000502/***********************************************************************
503 *
504 * Class constructor/destructor
505 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000506 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000507FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000508{
Josh Coalson0a15c142001-06-13 17:59:57 +0000509 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000510 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000511
Josh Coalson0a15c142001-06-13 17:59:57 +0000512 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000513
Josh Coalsonea7155f2002-10-18 05:49:19 +0000514 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000515 if(encoder == 0) {
516 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000517 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000518
Josh Coalsonea7155f2002-10-18 05:49:19 +0000519 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000520 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000521 free(encoder);
522 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000523 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000524
Josh Coalsonea7155f2002-10-18 05:49:19 +0000525 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000526 if(encoder->private_ == 0) {
527 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000528 free(encoder);
529 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000530 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000531
Josh Coalsonaec256b2002-03-12 16:19:54 +0000532 encoder->private_->frame = FLAC__bitbuffer_new();
533 if(encoder->private_->frame == 0) {
534 free(encoder->private_);
535 free(encoder->protected_);
536 free(encoder);
537 return 0;
538 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000539
Josh Coalson6b21f662006-09-13 01:42:27 +0000540 encoder->private_->file = 0;
541
Josh Coalsonf1eff452002-07-31 07:05:33 +0000542 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000543
Josh Coalson3262b0d2002-08-14 20:58:42 +0000544 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000545
546 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
547 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
548 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
549 }
550 for(i = 0; i < 2; i++) {
551 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
552 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
553 }
554 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000555 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
556 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000557 }
558 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000559 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
560 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 +0000561 }
562
563 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000564 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
565 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000566 }
567 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000568 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
569 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 +0000570 }
571 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000572 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000573
Josh Coalsonfa697a92001-08-16 20:07:29 +0000574 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000575
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000576 return encoder;
577}
578
Josh Coalson6afed9f2002-10-16 22:29:47 +0000579FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000580{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000581 unsigned i;
582
Josh Coalsonf1eff452002-07-31 07:05:33 +0000583 FLAC__ASSERT(0 != encoder);
584 FLAC__ASSERT(0 != encoder->protected_);
585 FLAC__ASSERT(0 != encoder->private_);
586 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000587
Josh Coalson3262b0d2002-08-14 20:58:42 +0000588 encoder->private_->is_being_deleted = true;
589
590 FLAC__stream_encoder_finish(encoder);
591
Josh Coalson4fa90592002-12-04 07:01:37 +0000592 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000593 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000594
595 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000596 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
597 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000598 }
599 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000600 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
601 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 +0000602 }
603 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000604 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000605
Josh Coalsonaec256b2002-03-12 16:19:54 +0000606 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000607 free(encoder->private_);
608 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000609 free(encoder);
610}
611
Josh Coalson0a15c142001-06-13 17:59:57 +0000612/***********************************************************************
613 *
614 * Public class methods
615 *
616 ***********************************************************************/
617
Josh Coalson8da98c82006-10-15 04:24:05 +0000618static FLAC__StreamEncoderInitStatus init_stream_internal_(
619 FLAC__StreamEncoder *encoder,
620 FLAC__StreamEncoderReadCallback read_callback,
621 FLAC__StreamEncoderWriteCallback write_callback,
622 FLAC__StreamEncoderSeekCallback seek_callback,
623 FLAC__StreamEncoderTellCallback tell_callback,
624 FLAC__StreamEncoderMetadataCallback metadata_callback,
625 void *client_data,
626 FLAC__bool is_ogg
627)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000628{
629 unsigned i;
Josh Coalson3957c472006-09-24 16:25:42 +0000630 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000631
Josh Coalsonf1eff452002-07-31 07:05:33 +0000632 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000633
Josh Coalsonfa697a92001-08-16 20:07:29 +0000634 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000635 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000636
Josh Coalson8da98c82006-10-15 04:24:05 +0000637#ifndef FLAC__HAS_OGG
638 if(is_ogg)
639 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
640#endif
641
Josh Coalson6b21f662006-09-13 01:42:27 +0000642 if(0 == write_callback || (seek_callback && 0 == tell_callback))
643 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000644
Josh Coalsonfa697a92001-08-16 20:07:29 +0000645 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000646 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000647
Josh Coalsonfa697a92001-08-16 20:07:29 +0000648 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
Josh Coalson6b21f662006-09-13 01:42:27 +0000649 return FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000650
Josh Coalsonfa697a92001-08-16 20:07:29 +0000651 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
Josh Coalson6b21f662006-09-13 01:42:27 +0000652 return FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000653
Josh Coalsonfa697a92001-08-16 20:07:29 +0000654 if(encoder->protected_->bits_per_sample >= 32)
Josh Coalson6b21f662006-09-13 01:42:27 +0000655 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 +0000656
Josh Coalson76c68bc2002-05-17 06:22:02 +0000657 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 +0000658 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000659
Josh Coalson0833f342002-07-15 05:31:55 +0000660 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000661 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000662
Josh Coalsonfa697a92001-08-16 20:07:29 +0000663 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000664 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000665
Josh Coalson20ac2c12002-08-30 05:47:14 +0000666 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000667 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000668
Josh Coalsonfa697a92001-08-16 20:07:29 +0000669 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000670 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000671
Josh Coalsonfa697a92001-08-16 20:07:29 +0000672 if(encoder->protected_->qlp_coeff_precision == 0) {
673 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000674 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
675 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000676 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 +0000677 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000678 else if(encoder->protected_->bits_per_sample == 16) {
679 if(encoder->protected_->blocksize <= 192)
680 encoder->protected_->qlp_coeff_precision = 7;
681 else if(encoder->protected_->blocksize <= 384)
682 encoder->protected_->qlp_coeff_precision = 8;
683 else if(encoder->protected_->blocksize <= 576)
684 encoder->protected_->qlp_coeff_precision = 9;
685 else if(encoder->protected_->blocksize <= 1152)
686 encoder->protected_->qlp_coeff_precision = 10;
687 else if(encoder->protected_->blocksize <= 2304)
688 encoder->protected_->qlp_coeff_precision = 11;
689 else if(encoder->protected_->blocksize <= 4608)
690 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000691 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000692 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000693 }
694 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000695 if(encoder->protected_->blocksize <= 384)
696 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
697 else if(encoder->protected_->blocksize <= 1152)
698 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
699 else
700 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000701 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000702 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000703 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000704 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 +0000705 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000706
Josh Coalsonfa697a92001-08-16 20:07:29 +0000707 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000708 if(
709 encoder->protected_->blocksize != 192 &&
710 encoder->protected_->blocksize != 576 &&
711 encoder->protected_->blocksize != 1152 &&
712 encoder->protected_->blocksize != 2304 &&
713 encoder->protected_->blocksize != 4608 &&
714 encoder->protected_->blocksize != 256 &&
715 encoder->protected_->blocksize != 512 &&
716 encoder->protected_->blocksize != 1024 &&
717 encoder->protected_->blocksize != 2048 &&
718 encoder->protected_->blocksize != 4096 &&
719 encoder->protected_->blocksize != 8192 &&
720 encoder->protected_->blocksize != 16384
721 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000722 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000723 if(
724 encoder->protected_->sample_rate != 8000 &&
725 encoder->protected_->sample_rate != 16000 &&
726 encoder->protected_->sample_rate != 22050 &&
727 encoder->protected_->sample_rate != 24000 &&
728 encoder->protected_->sample_rate != 32000 &&
729 encoder->protected_->sample_rate != 44100 &&
730 encoder->protected_->sample_rate != 48000 &&
731 encoder->protected_->sample_rate != 96000
732 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000733 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000734 if(
735 encoder->protected_->bits_per_sample != 8 &&
736 encoder->protected_->bits_per_sample != 12 &&
737 encoder->protected_->bits_per_sample != 16 &&
738 encoder->protected_->bits_per_sample != 20 &&
739 encoder->protected_->bits_per_sample != 24
740 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000741 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000742 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000743 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsond0edb972006-10-07 06:50:08 +0000744 if(
745 encoder->protected_->sample_rate <= 48000 &&
746 (
747 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
748 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
749 )
750 ) {
751 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
752 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000753 }
754
Josh Coalsonfa697a92001-08-16 20:07:29 +0000755 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
756 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
757 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
758 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000759
Josh Coalson8da98c82006-10-15 04:24:05 +0000760#if FLAC__HAS_OGG
761 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
762 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
763 unsigned i;
764 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
765 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
766 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
767 for( ; i > 0; i--)
768 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
769 encoder->protected_->metadata[0] = vc;
770 break;
771 }
772 }
773 }
774#endif
775 /* keep track of any SEEKTABLE block */
776 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
777 unsigned i;
778 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
779 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
780 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
781 break; /* take only the first one */
782 }
783 }
784 }
785
Josh Coalson66075c12002-06-01 05:39:38 +0000786 /* validate metadata */
787 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000788 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000789 metadata_has_seektable = false;
790 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000791 metadata_picture_has_type1 = false;
792 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000793 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000794 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
795 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000796 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000797 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000798 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000799 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000800 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000801 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000802 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000803 }
Josh Coalson3957c472006-09-24 16:25:42 +0000804 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000805 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000806 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000807 metadata_has_vorbis_comment = true;
808 }
Josh Coalson3957c472006-09-24 16:25:42 +0000809 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
810 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 +0000811 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000812 }
Josh Coalson3957c472006-09-24 16:25:42 +0000813 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
814 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000815 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000816 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
817 if(metadata_picture_has_type1) /* there should only be 1 per stream */
818 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
819 metadata_picture_has_type1 = true;
820 /* standard icon must be 32x32 pixel PNG */
821 if(
822 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
823 (
824 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
825 m->data.picture.width != 32 ||
826 m->data.picture.height != 32
827 )
828 )
829 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
830 }
831 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
832 if(metadata_picture_has_type2) /* there should only be 1 per stream */
833 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
834 metadata_picture_has_type2 = true;
835 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000836 }
Josh Coalson66075c12002-06-01 05:39:38 +0000837 }
838
Josh Coalsonfa697a92001-08-16 20:07:29 +0000839 encoder->private_->input_capacity = 0;
840 for(i = 0; i < encoder->protected_->channels; i++) {
841 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000842#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000843 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000844#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000845 }
846 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000847 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000848#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000849 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000850#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000851 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000852#ifndef FLAC__INTEGER_ONLY_LIBRARY
853 for(i = 0; i < encoder->protected_->num_apodizations; i++)
854 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
855 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
856#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000857 for(i = 0; i < encoder->protected_->channels; i++) {
858 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
859 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
860 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000861 }
862 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000863 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
864 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
865 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000866 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000867 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
868 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
869 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000870#ifndef FLAC__INTEGER_ONLY_LIBRARY
871 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
872#else
873 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
874 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
875 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
876 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
877 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
878 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
879 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);
880#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000881 if(encoder->private_->loose_mid_side_stereo_frames == 0)
882 encoder->private_->loose_mid_side_stereo_frames = 1;
883 encoder->private_->loose_mid_side_stereo_frame_count = 0;
884 encoder->private_->current_sample_number = 0;
885 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000886
Josh Coalsonfa697a92001-08-16 20:07:29 +0000887 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
888 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? */
889 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000890
Josh Coalsoncf30f502001-05-23 20:57:44 +0000891 /*
892 * get the CPU info and set the function pointers
893 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000894 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000895 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000896#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000897 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000898#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000899 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000900#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000901 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000902 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 +0000903 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000904#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000905 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000906#ifndef FLAC__INTEGER_ONLY_LIBRARY
907# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000908 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000909# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000910 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000911# ifdef FLAC__HAS_NASM
912# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000913 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000914 if(encoder->protected_->max_lpc_order < 4)
915 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
916 else if(encoder->protected_->max_lpc_order < 8)
917 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
918 else if(encoder->protected_->max_lpc_order < 12)
919 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000920 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000921 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000922 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000923 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000924# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000925 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000926 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000927 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000928 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000929 if(encoder->private_->cpuinfo.data.ia32.mmx) {
930 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
931 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 +0000932 }
933 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000934 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
935 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 +0000936 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000937 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
938 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
939# endif /* FLAC__HAS_NASM */
940# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000941 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000942# endif /* !FLAC__NO_ASM */
943#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000944 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000945 if(encoder->private_->use_wide_by_block) {
946 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000947 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000948
Josh Coalson8395d022001-07-12 21:25:22 +0000949 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000950 encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
Josh Coalsoneef56702001-03-30 00:45:22 +0000951
Josh Coalson6b21f662006-09-13 01:42:27 +0000952 /* set state to OK; from here on, errors are fatal and we'll override the state then */
953 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
954
Josh Coalson8da98c82006-10-15 04:24:05 +0000955#if FLAC__HAS_OGG
956 encoder->private_->is_ogg = is_ogg;
957 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
958 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
959 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
960 }
961#endif
962
963 encoder->private_->read_callback = read_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000964 encoder->private_->write_callback = write_callback;
965 encoder->private_->seek_callback = seek_callback;
966 encoder->private_->tell_callback = tell_callback;
967 encoder->private_->metadata_callback = metadata_callback;
968 encoder->private_->client_data = client_data;
969
Josh Coalsonf1eff452002-07-31 07:05:33 +0000970 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000971 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +0000972 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000973 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000974
Josh Coalson6b21f662006-09-13 01:42:27 +0000975 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
976 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
977 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
978 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000979
980 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000981 * Set up the verify stuff if necessary
982 */
983 if(encoder->protected_->verify) {
984 /*
985 * First, set up the fifo which will hold the
986 * original signal to compare against
987 */
988 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
989 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalson6b21f662006-09-13 01:42:27 +0000990 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
991 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
992 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
993 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000994 }
995 encoder->private_->verify.input_fifo.tail = 0;
996
997 /*
998 * Now set up a stream decoder for verification
999 */
1000 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
Josh Coalson6b21f662006-09-13 01:42:27 +00001001 if(0 == encoder->private_->verify.decoder) {
1002 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1003 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1004 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001005
Josh Coalson6b21f662006-09-13 01:42:27 +00001006 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) {
1007 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1008 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1009 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001010 }
Josh Coalson589f8c72002-08-07 23:54:55 +00001011 encoder->private_->verify.error_stats.absolute_sample = 0;
1012 encoder->private_->verify.error_stats.frame_number = 0;
1013 encoder->private_->verify.error_stats.channel = 0;
1014 encoder->private_->verify.error_stats.sample = 0;
1015 encoder->private_->verify.error_stats.expected = 0;
1016 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001017
1018 /*
Josh Coalson6b21f662006-09-13 01:42:27 +00001019 * These must be done before we write any metadata, because that
1020 * calls the write_callback, which uses these values.
1021 */
1022 encoder->private_->first_seekpoint_to_check = 0;
1023 encoder->private_->samples_written = 0;
1024 encoder->protected_->streaminfo_offset = 0;
1025 encoder->protected_->seektable_offset = 0;
1026 encoder->protected_->audio_offset = 0;
1027
1028 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001029 * write the stream header
1030 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001031 if(encoder->protected_->verify)
1032 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson6b21f662006-09-13 01:42:27 +00001033 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1034 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1035 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1036 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001037 if(!write_bitbuffer_(encoder, 0)) {
1038 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001039 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001040 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001041
Josh Coalson5c491a12002-08-01 06:39:40 +00001042 /*
1043 * write the STREAMINFO metadata block
1044 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001045 if(encoder->protected_->verify)
1046 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +00001047 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1048 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1049 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1050 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1051 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1052 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1053 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1054 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1055 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1056 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1057 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1058 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 +00001059 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +00001060 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1061 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1062 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1063 }
1064 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1065 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1066 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1067 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001068 if(!write_bitbuffer_(encoder, 0)) {
1069 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001070 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001071 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001072
Josh Coalson5c491a12002-08-01 06:39:40 +00001073 /*
1074 * Now that the STREAMINFO block is written, we can init this to an
1075 * absurdly-high value...
1076 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001077 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 +00001078 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001079 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001080
Josh Coalson5c491a12002-08-01 06:39:40 +00001081 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001082 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1083 * if not, we will write an empty one (FLAC__add_metadata_block()
1084 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001085 *
Josh Coalson8da98c82006-10-15 04:24:05 +00001086 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1087 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1088 * true it will have already insured that the metadata list is properly
1089 * ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001090 */
1091 if(!metadata_has_vorbis_comment) {
1092 FLAC__StreamMetadata vorbis_comment;
1093 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1094 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1095 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1096 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1097 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1098 vorbis_comment.data.vorbis_comment.num_comments = 0;
1099 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001100 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1101 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1102 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1103 }
1104 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1105 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1106 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1107 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001108 if(!write_bitbuffer_(encoder, 0)) {
1109 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001110 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001111 }
1112 }
1113
1114 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001115 * write the user's metadata blocks
1116 */
1117 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1118 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001119 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1120 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1121 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1122 }
1123 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 Coalsond86e03b2002-08-03 21:56:15 +00001127 if(!write_bitbuffer_(encoder, 0)) {
1128 /* 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,
1227 is_ogg? file_read_callback_ : 0,
1228 file_write_callback_,
1229 file_seek_callback_,
1230 file_tell_callback_,
1231 /*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 Coalson6afed9f2002-10-16 22:29:47 +00001320FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001321{
Josh Coalsonf1eff452002-07-31 07:05:33 +00001322 FLAC__ASSERT(0 != encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001323 FLAC__ASSERT(0 != encoder->private_);
1324 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001325
Josh Coalsonfa697a92001-08-16 20:07:29 +00001326 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001327 return;
Josh Coalson2b245f22002-08-07 17:10:50 +00001328
Josh Coalson3262b0d2002-08-14 20:58:42 +00001329 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001330 if(encoder->private_->current_sample_number != 0) {
1331 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1332 process_frame_(encoder, true); /* true => is last frame */
1333 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001334 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001335
Josh Coalson6b21f662006-09-13 01:42:27 +00001336 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001337
Josh Coalson3262b0d2002-08-14 20:58:42 +00001338 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson8da98c82006-10-15 04:24:05 +00001339 if(encoder->private_->seek_callback) {
1340#if FLAC__HAS_OGG
1341 if(encoder->private_->is_ogg)
1342 update_ogg_metadata_(encoder);
1343 else
1344#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00001345 update_metadata_(encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001346 }
Josh Coalson6b21f662006-09-13 01:42:27 +00001347 if(encoder->private_->metadata_callback)
1348 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
Josh Coalson2b245f22002-08-07 17:10:50 +00001349 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001350
Josh Coalsond86e03b2002-08-03 21:56:15 +00001351 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
1352 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1353
Josh Coalson6b21f662006-09-13 01:42:27 +00001354 if(0 != encoder->private_->file) {
1355 if(encoder->private_->file != stdout)
1356 fclose(encoder->private_->file);
1357 encoder->private_->file = 0;
1358 }
1359
Josh Coalson8da98c82006-10-15 04:24:05 +00001360#if FLAC__HAS_OGG
1361 if(encoder->private_->is_ogg)
1362 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1363#endif
1364
Josh Coalsonf1eff452002-07-31 07:05:33 +00001365 free_(encoder);
1366 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001367
Josh Coalsonfa697a92001-08-16 20:07:29 +00001368 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001369}
1370
Josh Coalson8da98c82006-10-15 04:24:05 +00001371FLAC_API FLAC__bool FLAC__stream_encoder_set_serial_number(FLAC__StreamEncoder *encoder, long value)
1372{
1373 FLAC__ASSERT(0 != encoder);
1374 FLAC__ASSERT(0 != encoder->private_);
1375 FLAC__ASSERT(0 != encoder->protected_);
1376 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1377 return false;
1378#ifdef FLAC__HAS_OGG
1379 /* can't check encoder->private_->is_ogg since that's not set until init time */
1380 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1381 return true;
1382#else
1383 (void)value;
1384 return false;
1385#endif
1386}
1387
Josh Coalson6afed9f2002-10-16 22:29:47 +00001388FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001389{
1390 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001391 FLAC__ASSERT(0 != encoder->private_);
1392 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001393 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1394 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001395#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001396 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001397#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001398 return true;
1399}
1400
Josh Coalson6afed9f2002-10-16 22:29:47 +00001401FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001402{
Josh Coalson92031602002-07-24 06:02:11 +00001403 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001404 FLAC__ASSERT(0 != encoder->private_);
1405 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001406 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001407 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001408 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001409 return true;
1410}
1411
Josh Coalson6afed9f2002-10-16 22:29:47 +00001412FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001413{
Josh Coalson92031602002-07-24 06:02:11 +00001414 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001415 FLAC__ASSERT(0 != encoder->private_);
1416 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001417 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001418 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001419 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001420 return true;
1421}
1422
Josh Coalson6afed9f2002-10-16 22:29:47 +00001423FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001424{
Josh Coalson92031602002-07-24 06:02:11 +00001425 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001426 FLAC__ASSERT(0 != encoder->private_);
1427 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001428 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001429 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001430 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001431 return true;
1432}
1433
Josh Coalson6afed9f2002-10-16 22:29:47 +00001434FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001435{
Josh Coalson92031602002-07-24 06:02:11 +00001436 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001437 FLAC__ASSERT(0 != encoder->private_);
1438 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001439 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001440 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001441 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001442 return true;
1443}
1444
Josh Coalson6afed9f2002-10-16 22:29:47 +00001445FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001446{
Josh Coalson92031602002-07-24 06:02:11 +00001447 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001448 FLAC__ASSERT(0 != encoder->private_);
1449 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001450 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001451 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001452 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001453 return true;
1454}
1455
Josh Coalson6afed9f2002-10-16 22:29:47 +00001456FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001457{
Josh Coalson92031602002-07-24 06:02:11 +00001458 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001459 FLAC__ASSERT(0 != encoder->private_);
1460 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001461 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001462 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001463 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001464 return true;
1465}
1466
Josh Coalson6afed9f2002-10-16 22:29:47 +00001467FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001468{
Josh Coalson92031602002-07-24 06:02:11 +00001469 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001470 FLAC__ASSERT(0 != encoder->private_);
1471 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001472 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001473 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001474 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001475 return true;
1476}
1477
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001478FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1479{
1480 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001481 FLAC__ASSERT(0 != encoder->private_);
1482 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001483 FLAC__ASSERT(0 != specification);
1484 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1485 return false;
1486#ifdef FLAC__INTEGER_ONLY_LIBRARY
1487 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1488#else
1489 encoder->protected_->num_apodizations = 0;
1490 while(1) {
1491 const char *s = strchr(specification, ';');
1492 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1493 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1494 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1495 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1496 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1497 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1498 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1499 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1500 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1501 else if(n==6 && 0 == strncmp("connes" , specification, n))
1502 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1503 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1504 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1505 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1506 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1507 if (stddev > 0.0 && stddev <= 0.5) {
1508 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1509 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1510 }
1511 }
1512 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1513 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1514 else if(n==4 && 0 == strncmp("hann" , specification, n))
1515 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1516 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1517 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1518 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1519 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1520 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1521 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1522 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1523 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1524 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1525 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1526 if (p >= 0.0 && p <= 1.0) {
1527 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1528 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1529 }
1530 }
1531 else if(n==5 && 0 == strncmp("welch" , specification, n))
1532 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1533 if (encoder->protected_->num_apodizations == 32)
1534 break;
1535 if (s)
1536 specification = s+1;
1537 else
1538 break;
1539 }
1540 if(encoder->protected_->num_apodizations == 0) {
1541 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001542 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1543 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001544 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001545#endif
1546 return true;
1547}
1548
Josh Coalson6afed9f2002-10-16 22:29:47 +00001549FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001550{
Josh Coalson92031602002-07-24 06:02:11 +00001551 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001552 FLAC__ASSERT(0 != encoder->private_);
1553 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001554 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001555 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001556 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001557 return true;
1558}
1559
Josh Coalson6afed9f2002-10-16 22:29:47 +00001560FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001561{
Josh Coalson92031602002-07-24 06:02:11 +00001562 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001563 FLAC__ASSERT(0 != encoder->private_);
1564 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001565 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001566 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001567 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001568 return true;
1569}
1570
Josh Coalson6afed9f2002-10-16 22:29:47 +00001571FLAC_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 +00001572{
Josh Coalson92031602002-07-24 06:02:11 +00001573 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001574 FLAC__ASSERT(0 != encoder->private_);
1575 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001576 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001577 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001578 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001579 return true;
1580}
1581
Josh Coalson6afed9f2002-10-16 22:29:47 +00001582FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001583{
Josh Coalson92031602002-07-24 06:02:11 +00001584 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001585 FLAC__ASSERT(0 != encoder->private_);
1586 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001587 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001588 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001589#if 0
1590 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001591 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001592#else
1593 (void)value;
1594#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001595 return true;
1596}
1597
Josh Coalson6afed9f2002-10-16 22:29:47 +00001598FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001599{
Josh Coalson92031602002-07-24 06:02:11 +00001600 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001601 FLAC__ASSERT(0 != encoder->private_);
1602 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001603 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001604 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001605 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001606 return true;
1607}
1608
Josh Coalson6afed9f2002-10-16 22:29:47 +00001609FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001610{
Josh Coalson92031602002-07-24 06:02:11 +00001611 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001612 FLAC__ASSERT(0 != encoder->private_);
1613 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001614 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001615 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001616 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001617 return true;
1618}
1619
Josh Coalson6afed9f2002-10-16 22:29:47 +00001620FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001621{
Josh Coalson92031602002-07-24 06:02:11 +00001622 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001623 FLAC__ASSERT(0 != encoder->private_);
1624 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001625 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001626 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001627 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001628 return true;
1629}
1630
Josh Coalson6afed9f2002-10-16 22:29:47 +00001631FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001632{
Josh Coalson92031602002-07-24 06:02:11 +00001633 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001634 FLAC__ASSERT(0 != encoder->private_);
1635 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001636 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001637 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001638#if 0
1639 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001640 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001641#else
1642 (void)value;
1643#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001644 return true;
1645}
1646
Josh Coalson6afed9f2002-10-16 22:29:47 +00001647FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001648{
Josh Coalson92031602002-07-24 06:02:11 +00001649 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001650 FLAC__ASSERT(0 != encoder->private_);
1651 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001652 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001653 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001654 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001655 return true;
1656}
1657
Josh Coalson6afed9f2002-10-16 22:29:47 +00001658FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001659{
Josh Coalson92031602002-07-24 06:02:11 +00001660 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001661 FLAC__ASSERT(0 != encoder->private_);
1662 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001663 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001664 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001665 encoder->protected_->metadata = metadata;
1666 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson8da98c82006-10-15 04:24:05 +00001667#if FLAC__HAS_OGG
1668 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1669 return false;
1670#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001671 return true;
1672}
1673
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001674/*
1675 * These three functions are not static, but not publically exposed in
1676 * include/FLAC/ either. They are used by the test suite.
1677 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001678FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001679{
1680 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001681 FLAC__ASSERT(0 != encoder->private_);
1682 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001683 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1684 return false;
1685 encoder->private_->disable_constant_subframes = value;
1686 return true;
1687}
1688
Josh Coalson6afed9f2002-10-16 22:29:47 +00001689FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001690{
1691 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001692 FLAC__ASSERT(0 != encoder->private_);
1693 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001694 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1695 return false;
1696 encoder->private_->disable_fixed_subframes = value;
1697 return true;
1698}
1699
Josh Coalson6afed9f2002-10-16 22:29:47 +00001700FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001701{
1702 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001703 FLAC__ASSERT(0 != encoder->private_);
1704 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001705 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1706 return false;
1707 encoder->private_->disable_verbatim_subframes = value;
1708 return true;
1709}
1710
Josh Coalson6afed9f2002-10-16 22:29:47 +00001711FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001712{
Josh Coalson92031602002-07-24 06:02:11 +00001713 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001714 FLAC__ASSERT(0 != encoder->private_);
1715 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001716 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001717}
1718
Josh Coalson6afed9f2002-10-16 22:29:47 +00001719FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001720{
1721 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001722 FLAC__ASSERT(0 != encoder->private_);
1723 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001724 if(encoder->protected_->verify)
1725 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1726 else
1727 return FLAC__STREAM_DECODER_UNINITIALIZED;
1728}
1729
Josh Coalson02954222002-11-08 06:16:31 +00001730FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1731{
Josh Coalson8da98c82006-10-15 04:24:05 +00001732 FLAC__ASSERT(0 != encoder);
1733 FLAC__ASSERT(0 != encoder->private_);
1734 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson02954222002-11-08 06:16:31 +00001735 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1736 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1737 else
Josh Coalson807140d2003-09-24 22:10:51 +00001738 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001739}
1740
Josh Coalson6afed9f2002-10-16 22:29:47 +00001741FLAC_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 +00001742{
1743 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001744 FLAC__ASSERT(0 != encoder->private_);
1745 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson589f8c72002-08-07 23:54:55 +00001746 if(0 != absolute_sample)
1747 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1748 if(0 != frame_number)
1749 *frame_number = encoder->private_->verify.error_stats.frame_number;
1750 if(0 != channel)
1751 *channel = encoder->private_->verify.error_stats.channel;
1752 if(0 != sample)
1753 *sample = encoder->private_->verify.error_stats.sample;
1754 if(0 != expected)
1755 *expected = encoder->private_->verify.error_stats.expected;
1756 if(0 != got)
1757 *got = encoder->private_->verify.error_stats.got;
1758}
1759
Josh Coalson6afed9f2002-10-16 22:29:47 +00001760FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001761{
1762 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001763 FLAC__ASSERT(0 != encoder->private_);
1764 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001765 return encoder->protected_->verify;
1766}
1767
Josh Coalson6afed9f2002-10-16 22:29:47 +00001768FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001769{
Josh Coalson92031602002-07-24 06:02:11 +00001770 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001771 FLAC__ASSERT(0 != encoder->private_);
1772 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001773 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001774}
1775
Josh Coalson6afed9f2002-10-16 22:29:47 +00001776FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001777{
Josh Coalson92031602002-07-24 06:02:11 +00001778 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001779 FLAC__ASSERT(0 != encoder->private_);
1780 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001781 return encoder->protected_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001782}
1783
Josh Coalson6afed9f2002-10-16 22:29:47 +00001784FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001785{
Josh Coalson92031602002-07-24 06:02:11 +00001786 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001787 FLAC__ASSERT(0 != encoder->private_);
1788 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001789 return encoder->protected_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001790}
1791
Josh Coalson6afed9f2002-10-16 22:29:47 +00001792FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001793{
Josh Coalson92031602002-07-24 06:02:11 +00001794 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001795 FLAC__ASSERT(0 != encoder->private_);
1796 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001797 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001798}
1799
Josh Coalson6afed9f2002-10-16 22:29:47 +00001800FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001801{
Josh Coalson92031602002-07-24 06:02:11 +00001802 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001803 FLAC__ASSERT(0 != encoder->private_);
1804 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001805 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001806}
1807
Josh Coalson6afed9f2002-10-16 22:29:47 +00001808FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001809{
Josh Coalson92031602002-07-24 06:02:11 +00001810 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001811 FLAC__ASSERT(0 != encoder->private_);
1812 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001813 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001814}
1815
Josh Coalson6afed9f2002-10-16 22:29:47 +00001816FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001817{
Josh Coalson92031602002-07-24 06:02:11 +00001818 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001819 FLAC__ASSERT(0 != encoder->private_);
1820 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001821 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001822}
1823
Josh Coalson6afed9f2002-10-16 22:29:47 +00001824FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001825{
Josh Coalson92031602002-07-24 06:02:11 +00001826 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001827 FLAC__ASSERT(0 != encoder->private_);
1828 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001829 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001830}
1831
Josh Coalson6afed9f2002-10-16 22:29:47 +00001832FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(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_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001838}
1839
Josh Coalson6afed9f2002-10-16 22:29:47 +00001840FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(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_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001846}
1847
Josh Coalson6afed9f2002-10-16 22:29:47 +00001848FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +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_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001854}
1855
Josh Coalson6afed9f2002-10-16 22:29:47 +00001856FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(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_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001862}
1863
Josh Coalson6afed9f2002-10-16 22:29:47 +00001864FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(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_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001870}
1871
Josh Coalson6afed9f2002-10-16 22:29:47 +00001872FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001873{
Josh Coalson92031602002-07-24 06:02:11 +00001874 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001875 FLAC__ASSERT(0 != encoder->private_);
1876 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001877 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001878}
1879
Josh Coalson6afed9f2002-10-16 22:29:47 +00001880FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001881{
Josh Coalson92031602002-07-24 06:02:11 +00001882 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001883 FLAC__ASSERT(0 != encoder->private_);
1884 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001885 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001886}
1887
Josh Coalson6afed9f2002-10-16 22:29:47 +00001888FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001889{
1890 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001891 FLAC__ASSERT(0 != encoder->private_);
1892 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001893 return encoder->protected_->total_samples_estimate;
1894}
1895
Josh Coalson6afed9f2002-10-16 22:29:47 +00001896FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001897{
1898 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001899 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001900 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001901
Josh Coalsonf1eff452002-07-31 07:05:33 +00001902 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001903 FLAC__ASSERT(0 != encoder->private_);
1904 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001905 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001906
1907 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001908 /*
1909 * we have several flavors of the same basic loop, optimized for
1910 * different conditions:
1911 */
1912 if(encoder->protected_->max_lpc_order > 0) {
1913 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1914 /*
1915 * stereo coding: unroll channel loop
1916 * with LPC: calculate floating point version of signal
1917 */
1918 do {
1919 if(encoder->protected_->verify)
1920 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00001921
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001922 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1923 x = mid = side = buffer[0][j];
1924 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001925#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001926 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001927#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001928 x = buffer[1][j];
1929 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001930#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001931 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001932#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001933 mid += x;
1934 side -= x;
1935 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1936 encoder->private_->integer_signal_mid_side[1][i] = side;
1937 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001938#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001939 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1940 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001941#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001942 encoder->private_->current_sample_number++;
1943 }
1944 if(i == blocksize) {
1945 if(!process_frame_(encoder, false)) /* false => not last frame */
1946 return false;
1947 }
1948 } while(j < samples);
1949 }
1950 else {
1951 /*
1952 * independent channel coding: buffer each channel in inner loop
1953 * with LPC: calculate floating point version of signal
1954 */
1955 do {
1956 if(encoder->protected_->verify)
1957 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1958
1959 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1960 for(channel = 0; channel < channels; channel++) {
1961 x = buffer[channel][j];
1962 encoder->private_->integer_signal[channel][i] = x;
1963#ifndef FLAC__INTEGER_ONLY_LIBRARY
1964 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1965#endif
1966 }
1967 encoder->private_->current_sample_number++;
1968 }
1969 if(i == blocksize) {
1970 if(!process_frame_(encoder, false)) /* false => not last frame */
1971 return false;
1972 }
1973 } while(j < samples);
1974 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001975 }
1976 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001977 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1978 /*
1979 * stereo coding: unroll channel loop
1980 * without LPC: no need to calculate floating point version of signal
1981 */
1982 do {
1983 if(encoder->protected_->verify)
1984 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00001985
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001986 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1987 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1988 x = buffer[1][j];
1989 encoder->private_->integer_signal[1][i] = x;
1990 mid += x;
1991 side -= x;
1992 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1993 encoder->private_->integer_signal_mid_side[1][i] = side;
1994 encoder->private_->integer_signal_mid_side[0][i] = mid;
1995 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001996 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001997 if(i == blocksize) {
1998 if(!process_frame_(encoder, false)) /* false => not last frame */
1999 return false;
2000 }
2001 } while(j < samples);
2002 }
2003 else {
2004 /*
2005 * independent channel coding: buffer each channel in inner loop
2006 * without LPC: no need to calculate floating point version of signal
2007 */
2008 do {
2009 if(encoder->protected_->verify)
2010 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2011
2012 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2013 for(channel = 0; channel < channels; channel++)
2014 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2015 encoder->private_->current_sample_number++;
2016 }
2017 if(i == blocksize) {
2018 if(!process_frame_(encoder, false)) /* false => not last frame */
2019 return false;
2020 }
2021 } while(j < samples);
2022 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002023 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002024
2025 return true;
2026}
2027
Josh Coalson6afed9f2002-10-16 22:29:47 +00002028FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002029{
2030 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00002031 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002032 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002033
Josh Coalsonf1eff452002-07-31 07:05:33 +00002034 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002035 FLAC__ASSERT(0 != encoder->private_);
2036 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002037 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002038
2039 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002040 /*
2041 * we have several flavors of the same basic loop, optimized for
2042 * different conditions:
2043 */
2044 if(encoder->protected_->max_lpc_order > 0) {
2045 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2046 /*
2047 * stereo coding: unroll channel loop
2048 * with LPC: calculate floating point version of signal
2049 */
2050 do {
2051 if(encoder->protected_->verify)
2052 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00002053
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002054 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2055 x = mid = side = buffer[k++];
2056 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002057#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002058 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002059#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002060 x = buffer[k++];
2061 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002062#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002063 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002064#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002065 mid += x;
2066 side -= x;
2067 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2068 encoder->private_->integer_signal_mid_side[1][i] = side;
2069 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002070#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002071 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2072 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002073#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002074 encoder->private_->current_sample_number++;
2075 }
2076 if(i == blocksize) {
2077 if(!process_frame_(encoder, false)) /* false => not last frame */
2078 return false;
2079 }
2080 } while(j < samples);
2081 }
2082 else {
2083 /*
2084 * independent channel coding: buffer each channel in inner loop
2085 * with LPC: calculate floating point version of signal
2086 */
2087 do {
2088 if(encoder->protected_->verify)
2089 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2090
2091 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2092 for(channel = 0; channel < channels; channel++) {
2093 x = buffer[k++];
2094 encoder->private_->integer_signal[channel][i] = x;
2095#ifndef FLAC__INTEGER_ONLY_LIBRARY
2096 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2097#endif
2098 }
2099 encoder->private_->current_sample_number++;
2100 }
2101 if(i == blocksize) {
2102 if(!process_frame_(encoder, false)) /* false => not last frame */
2103 return false;
2104 }
2105 } while(j < samples);
2106 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002107 }
2108 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002109 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2110 /*
2111 * stereo coding: unroll channel loop
2112 * without LPC: no need to calculate floating point version of signal
2113 */
2114 do {
2115 if(encoder->protected_->verify)
2116 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00002117
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002118 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2119 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00002120 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002121 encoder->private_->integer_signal[1][i] = x;
2122 mid += x;
2123 side -= x;
2124 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2125 encoder->private_->integer_signal_mid_side[1][i] = side;
2126 encoder->private_->integer_signal_mid_side[0][i] = mid;
2127 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002128 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002129 if(i == blocksize) {
2130 if(!process_frame_(encoder, false)) /* false => not last frame */
2131 return false;
2132 }
2133 } while(j < samples);
2134 }
2135 else {
2136 /*
2137 * independent channel coding: buffer each channel in inner loop
2138 * without LPC: no need to calculate floating point version of signal
2139 */
2140 do {
2141 if(encoder->protected_->verify)
2142 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2143
2144 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2145 for(channel = 0; channel < channels; channel++)
2146 encoder->private_->integer_signal[channel][i] = buffer[k++];
2147 encoder->private_->current_sample_number++;
2148 }
2149 if(i == blocksize) {
2150 if(!process_frame_(encoder, false)) /* false => not last frame */
2151 return false;
2152 }
2153 } while(j < samples);
2154 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002155 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002156
2157 return true;
2158}
2159
Josh Coalsonf1eff452002-07-31 07:05:33 +00002160/***********************************************************************
2161 *
2162 * Private class methods
2163 *
2164 ***********************************************************************/
2165
2166void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00002167{
2168 FLAC__ASSERT(0 != encoder);
2169
Josh Coalson47c7b142005-01-29 06:08:58 +00002170#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2171 encoder->protected_->verify = true;
2172#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00002173 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00002174#endif
Josh Coalson92031602002-07-24 06:02:11 +00002175 encoder->protected_->streamable_subset = true;
2176 encoder->protected_->do_mid_side_stereo = false;
2177 encoder->protected_->loose_mid_side_stereo = false;
2178 encoder->protected_->channels = 2;
2179 encoder->protected_->bits_per_sample = 16;
2180 encoder->protected_->sample_rate = 44100;
2181 encoder->protected_->blocksize = 1152;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002182#ifndef FLAC__INTEGER_ONLY_LIBRARY
2183 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00002184 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2185 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002186#endif
Josh Coalson92031602002-07-24 06:02:11 +00002187 encoder->protected_->max_lpc_order = 0;
2188 encoder->protected_->qlp_coeff_precision = 0;
2189 encoder->protected_->do_qlp_coeff_prec_search = false;
2190 encoder->protected_->do_exhaustive_model_search = false;
2191 encoder->protected_->do_escape_coding = false;
2192 encoder->protected_->min_residual_partition_order = 0;
2193 encoder->protected_->max_residual_partition_order = 0;
2194 encoder->protected_->rice_parameter_search_dist = 0;
2195 encoder->protected_->total_samples_estimate = 0;
2196 encoder->protected_->metadata = 0;
2197 encoder->protected_->num_metadata_blocks = 0;
2198
Josh Coalson6b21f662006-09-13 01:42:27 +00002199 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002200 encoder->private_->disable_constant_subframes = false;
2201 encoder->private_->disable_fixed_subframes = false;
2202 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002203#if FLAC__HAS_OGG
2204 encoder->private_->is_ogg = false;
2205#endif
2206 encoder->private_->read_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002207 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002208 encoder->private_->seek_callback = 0;
2209 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002210 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002211 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002212 encoder->private_->client_data = 0;
Josh Coalson8da98c82006-10-15 04:24:05 +00002213
2214#if FLAC__HAS_OGG
2215 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2216#endif
Josh Coalson92031602002-07-24 06:02:11 +00002217}
2218
Josh Coalsonf1eff452002-07-31 07:05:33 +00002219void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00002220{
2221 unsigned i, channel;
2222
Josh Coalsonf1eff452002-07-31 07:05:33 +00002223 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00002224 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002225 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002226 free(encoder->private_->integer_signal_unaligned[i]);
2227 encoder->private_->integer_signal_unaligned[i] = 0;
2228 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002229#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002230 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002231 free(encoder->private_->real_signal_unaligned[i]);
2232 encoder->private_->real_signal_unaligned[i] = 0;
2233 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002234#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002235 }
2236 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002237 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002238 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2239 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2240 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002241#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002242 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002243 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2244 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2245 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002246#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002247 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002248#ifndef FLAC__INTEGER_ONLY_LIBRARY
2249 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2250 if(0 != encoder->private_->window_unaligned[i]) {
2251 free(encoder->private_->window_unaligned[i]);
2252 encoder->private_->window_unaligned[i] = 0;
2253 }
2254 }
2255 if(0 != encoder->private_->windowed_signal_unaligned) {
2256 free(encoder->private_->windowed_signal_unaligned);
2257 encoder->private_->windowed_signal_unaligned = 0;
2258 }
2259#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002260 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2261 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002262 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002263 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2264 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2265 }
2266 }
2267 }
2268 for(channel = 0; channel < 2; channel++) {
2269 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002270 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002271 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2272 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2273 }
2274 }
2275 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002276 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002277 free(encoder->private_->abs_residual_unaligned);
2278 encoder->private_->abs_residual_unaligned = 0;
2279 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002280 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002281 free(encoder->private_->abs_residual_partition_sums_unaligned);
2282 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2283 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002284 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002285 free(encoder->private_->raw_bits_per_partition_unaligned);
2286 encoder->private_->raw_bits_per_partition_unaligned = 0;
2287 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002288 if(encoder->protected_->verify) {
2289 for(i = 0; i < encoder->protected_->channels; i++) {
2290 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2291 free(encoder->private_->verify.input_fifo.data[i]);
2292 encoder->private_->verify.input_fifo.data[i] = 0;
2293 }
2294 }
2295 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002296 FLAC__bitbuffer_free(encoder->private_->frame);
2297}
2298
Josh Coalsonf1eff452002-07-31 07:05:33 +00002299FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002300{
Josh Coalson77e3f312001-06-23 03:03:24 +00002301 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002302 unsigned i, channel;
2303
2304 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002305 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2306 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002307
2308 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002309 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002310 return true;
2311
2312 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002313
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002314 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2315 * requires that the input arrays (in our case the integer signals)
2316 * have a buffer of up to 3 zeroes in front (at negative indices) for
2317 * alignment purposes; we use 4 to keep the data well-aligned.
2318 */
Josh Coalson8395d022001-07-12 21:25:22 +00002319
Josh Coalsonfa697a92001-08-16 20:07:29 +00002320 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2321 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002322#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002323 if(encoder->protected_->max_lpc_order > 0)
2324 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002325#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002326 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2327 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00002328 }
2329 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002330 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002331#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002332 if(encoder->protected_->max_lpc_order > 0)
2333 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002334#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002335 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2336 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00002337 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002338#ifndef FLAC__INTEGER_ONLY_LIBRARY
2339 if(ok && encoder->protected_->max_lpc_order > 0) {
2340 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2341 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2342 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2343 }
2344#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002345 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002346 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002347 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
Josh Coalson0a15c142001-06-13 17:59:57 +00002348 }
2349 }
2350 for(channel = 0; ok && channel < 2; channel++) {
2351 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002352 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
Josh Coalson0a15c142001-06-13 17:59:57 +00002353 }
2354 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002355 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
2356 if(encoder->private_->precompute_partition_sums || encoder->protected_->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
2357 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2358 if(encoder->protected_->do_escape_coding)
2359 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
Josh Coalson0a15c142001-06-13 17:59:57 +00002360
2361 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00002362 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00002363 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002364 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00002365
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002366#ifndef FLAC__INTEGER_ONLY_LIBRARY
2367 if(ok && encoder->protected_->max_lpc_order > 0) {
2368 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2369 switch(encoder->protected_->apodizations[i].type) {
2370 case FLAC__APODIZATION_BARTLETT:
2371 FLAC__window_bartlett(encoder->private_->window[i], new_size);
2372 break;
2373 case FLAC__APODIZATION_BARTLETT_HANN:
2374 FLAC__window_bartlett_hann(encoder->private_->window[i], new_size);
2375 break;
2376 case FLAC__APODIZATION_BLACKMAN:
2377 FLAC__window_blackman(encoder->private_->window[i], new_size);
2378 break;
2379 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2380 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_size);
2381 break;
2382 case FLAC__APODIZATION_CONNES:
2383 FLAC__window_connes(encoder->private_->window[i], new_size);
2384 break;
2385 case FLAC__APODIZATION_FLATTOP:
2386 FLAC__window_flattop(encoder->private_->window[i], new_size);
2387 break;
2388 case FLAC__APODIZATION_GAUSS:
2389 FLAC__window_gauss(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2390 break;
2391 case FLAC__APODIZATION_HAMMING:
2392 FLAC__window_hamming(encoder->private_->window[i], new_size);
2393 break;
2394 case FLAC__APODIZATION_HANN:
2395 FLAC__window_hann(encoder->private_->window[i], new_size);
2396 break;
2397 case FLAC__APODIZATION_KAISER_BESSEL:
2398 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_size);
2399 break;
2400 case FLAC__APODIZATION_NUTTALL:
2401 FLAC__window_nuttall(encoder->private_->window[i], new_size);
2402 break;
2403 case FLAC__APODIZATION_RECTANGLE:
2404 FLAC__window_rectangle(encoder->private_->window[i], new_size);
2405 break;
2406 case FLAC__APODIZATION_TRIANGLE:
2407 FLAC__window_triangle(encoder->private_->window[i], new_size);
2408 break;
2409 case FLAC__APODIZATION_TUKEY:
2410 FLAC__window_tukey(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.tukey.p);
2411 break;
2412 case FLAC__APODIZATION_WELCH:
2413 FLAC__window_welch(encoder->private_->window[i], new_size);
2414 break;
2415 default:
2416 FLAC__ASSERT(0);
2417 /* double protection */
Josh Coalsondf598452006-04-28 00:13:34 +00002418 FLAC__window_hann(encoder->private_->window[i], new_size);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002419 break;
2420 }
2421 }
2422 }
2423#endif
2424
Josh Coalson0a15c142001-06-13 17:59:57 +00002425 return ok;
2426}
2427
Josh Coalsond86e03b2002-08-03 21:56:15 +00002428FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00002429{
2430 const FLAC__byte *buffer;
2431 unsigned bytes;
2432
2433 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2434
2435 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2436
Josh Coalsond86e03b2002-08-03 21:56:15 +00002437 if(encoder->protected_->verify) {
2438 encoder->private_->verify.output.data = buffer;
2439 encoder->private_->verify.output.bytes = bytes;
2440 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2441 encoder->private_->verify.needs_magic_hack = true;
2442 }
2443 else {
2444 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2445 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2446 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2447 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2448 return false;
2449 }
2450 }
2451 }
2452
Josh Coalson6b21f662006-09-13 01:42:27 +00002453 if(write_frame_(encoder, buffer, bytes, samples) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002454 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002455 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002456 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002457 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002458
2459 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2460
Josh Coalsond86e03b2002-08-03 21:56:15 +00002461 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002462 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2463 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 +00002464 }
2465
Josh Coalson5c491a12002-08-01 06:39:40 +00002466 return true;
2467}
2468
Josh Coalson8da98c82006-10-15 04:24:05 +00002469FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples)
Josh Coalson6b21f662006-09-13 01:42:27 +00002470{
2471 FLAC__StreamEncoderWriteStatus status;
2472 FLAC__uint64 output_position = 0;
2473
2474 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2475 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2476 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2477 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2478 }
2479
2480 /*
2481 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2482 */
2483 if(samples == 0) {
2484 FLAC__MetadataType type = (buffer[0] & 0x7f);
2485 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2486 encoder->protected_->streaminfo_offset = output_position;
2487 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2488 encoder->protected_->seektable_offset = output_position;
2489 }
2490
2491 /*
2492 * Mark the current seek point if hit (if audio_offset == 0 that
2493 * means we're still writing metadata and haven't hit the first
2494 * frame yet)
2495 */
2496 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2497 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2498 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2499 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2500 FLAC__uint64 test_sample;
2501 unsigned i;
2502 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2503 test_sample = encoder->private_->seek_table->points[i].sample_number;
2504 if(test_sample > frame_last_sample) {
2505 break;
2506 }
2507 else if(test_sample >= frame_first_sample) {
2508 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2509 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2510 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2511 encoder->private_->first_seekpoint_to_check++;
2512 /* DO NOT: "break;" and here's why:
2513 * The seektable template may contain more than one target
2514 * sample for any given frame; we will keep looping, generating
2515 * duplicate seekpoints for them, and we'll clean it up later,
2516 * just before writing the seektable back to the metadata.
2517 */
2518 }
2519 else {
2520 encoder->private_->first_seekpoint_to_check++;
2521 }
2522 }
2523 }
2524
Josh Coalson8da98c82006-10-15 04:24:05 +00002525#if FLAC__HAS_OGG
2526 if(encoder->private_->is_ogg) {
2527 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2528 &encoder->protected_->ogg_encoder_aspect,
2529 FLAC__stream_encoder_get_total_samples_estimate(encoder),
2530 buffer,
2531 bytes,
2532 samples,
2533 encoder->private_->current_frame_number,
2534 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2535 encoder,
2536 encoder->private_->client_data
2537 );
2538 }
2539 else
2540#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00002541 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2542
2543 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2544 encoder->private_->bytes_written += bytes;
2545 encoder->private_->samples_written += samples;
2546 /* we keep a high watermark on the number of frames written because
2547 * when the encoder goes back to write metadata, 'current_frame'
2548 * will drop back to 0.
2549 */
2550 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2551 }
2552 else
2553 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2554
2555 return status;
2556}
2557
2558/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2559void update_metadata_(const FLAC__StreamEncoder *encoder)
2560{
2561 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2562 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2563 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2564 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2565 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2566 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2567 FLAC__StreamEncoderSeekStatus seek_status;
2568
2569 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2570
2571 /* All this is based on intimate knowledge of the stream header
2572 * layout, but a change to the header format that would break this
2573 * would also break all streams encoded in the previous format.
2574 */
2575
2576 /*
2577 * Write MD5 signature
2578 */
2579 {
2580 const unsigned md5_offset =
2581 FLAC__STREAM_METADATA_HEADER_LENGTH +
2582 (
2583 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2584 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2585 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2586 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2587 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2588 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2589 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2590 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2591 ) / 8;
2592
2593 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2594 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2595 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2596 return;
2597 }
2598 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2599 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2600 return;
2601 }
2602 }
2603
2604 /*
2605 * Write total samples
2606 */
2607 {
2608 const unsigned total_samples_byte_offset =
2609 FLAC__STREAM_METADATA_HEADER_LENGTH +
2610 (
2611 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2612 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2613 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2614 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2615 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2616 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2617 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2618 - 4
2619 ) / 8;
2620
2621 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2622 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2623 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2624 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2625 b[4] = (FLAC__byte)(samples & 0xFF);
2626 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) {
2627 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2628 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2629 return;
2630 }
2631 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2632 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2633 return;
2634 }
2635 }
2636
2637 /*
2638 * Write min/max framesize
2639 */
2640 {
2641 const unsigned min_framesize_offset =
2642 FLAC__STREAM_METADATA_HEADER_LENGTH +
2643 (
2644 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2645 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2646 ) / 8;
2647
2648 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2649 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2650 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2651 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2652 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2653 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2654 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) {
2655 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2656 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2657 return;
2658 }
2659 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2660 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2661 return;
2662 }
2663 }
2664
2665 /*
2666 * Write seektable
2667 */
2668 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2669 unsigned i;
2670
2671 FLAC__format_seektable_sort(encoder->private_->seek_table);
2672
2673 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2674
2675 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) {
2676 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2677 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2678 return;
2679 }
2680
2681 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2682 FLAC__uint64 xx;
2683 unsigned x;
2684 xx = encoder->private_->seek_table->points[i].sample_number;
2685 b[7] = (FLAC__byte)xx; xx >>= 8;
2686 b[6] = (FLAC__byte)xx; xx >>= 8;
2687 b[5] = (FLAC__byte)xx; xx >>= 8;
2688 b[4] = (FLAC__byte)xx; xx >>= 8;
2689 b[3] = (FLAC__byte)xx; xx >>= 8;
2690 b[2] = (FLAC__byte)xx; xx >>= 8;
2691 b[1] = (FLAC__byte)xx; xx >>= 8;
2692 b[0] = (FLAC__byte)xx; xx >>= 8;
2693 xx = encoder->private_->seek_table->points[i].stream_offset;
2694 b[15] = (FLAC__byte)xx; xx >>= 8;
2695 b[14] = (FLAC__byte)xx; xx >>= 8;
2696 b[13] = (FLAC__byte)xx; xx >>= 8;
2697 b[12] = (FLAC__byte)xx; xx >>= 8;
2698 b[11] = (FLAC__byte)xx; xx >>= 8;
2699 b[10] = (FLAC__byte)xx; xx >>= 8;
2700 b[9] = (FLAC__byte)xx; xx >>= 8;
2701 b[8] = (FLAC__byte)xx; xx >>= 8;
2702 x = encoder->private_->seek_table->points[i].frame_samples;
2703 b[17] = (FLAC__byte)x; x >>= 8;
2704 b[16] = (FLAC__byte)x; x >>= 8;
2705 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2706 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2707 return;
2708 }
2709 }
2710 }
2711}
2712
Josh Coalson8da98c82006-10-15 04:24:05 +00002713/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2714void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2715{
2716 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2717 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2718 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2719 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2720 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2721 ogg_page page;
2722
2723 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2724
2725 /* All this is based on intimate knowledge of the stream header
2726 * layout, but a change to the header format that would break this
2727 * would also break all streams encoded in the previous format.
2728 */
2729
2730 /**
2731 ** Write STREAMINFO stats
2732 **/
2733 simple_ogg_page__init(&page);
2734 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2735 simple_ogg_page__clear(&page);
2736 return; /* state already set */
2737 }
2738
2739 /*
2740 * Write MD5 signature
2741 */
2742 {
2743 const unsigned md5_offset =
2744 FLAC__STREAM_METADATA_HEADER_LENGTH +
2745 (
2746 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2747 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2748 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2749 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2750 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2751 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2752 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2753 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2754 ) / 8;
2755
2756 if(md5_offset + 16 > (unsigned)page.body_len) {
2757 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2758 simple_ogg_page__clear(&page);
2759 return;
2760 }
2761 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2762 }
2763
2764 /*
2765 * Write total samples
2766 */
2767 {
2768 const unsigned total_samples_byte_offset =
2769 FLAC__STREAM_METADATA_HEADER_LENGTH +
2770 (
2771 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2772 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2773 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2774 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2775 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2776 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2777 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2778 - 4
2779 ) / 8;
2780
2781 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2782 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2783 simple_ogg_page__clear(&page);
2784 return;
2785 }
2786 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2787 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2788 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2789 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2790 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2791 b[4] = (FLAC__byte)(samples & 0xFF);
2792 memcpy(page.body + total_samples_byte_offset, b, 5);
2793 }
2794
2795 /*
2796 * Write min/max framesize
2797 */
2798 {
2799 const unsigned min_framesize_offset =
2800 FLAC__STREAM_METADATA_HEADER_LENGTH +
2801 (
2802 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2803 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2804 ) / 8;
2805
2806 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2807 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2808 simple_ogg_page__clear(&page);
2809 return;
2810 }
2811 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2812 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2813 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2814 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2815 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2816 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2817 memcpy(page.body + min_framesize_offset, b, 6);
2818 }
2819 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2820 simple_ogg_page__clear(&page);
2821 return; /* state already set */
2822 }
2823 simple_ogg_page__clear(&page);
2824
2825 /*
2826 * Write seektable
2827 */
2828 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2829 unsigned i;
2830 FLAC__byte *p;
2831
2832 FLAC__format_seektable_sort(encoder->private_->seek_table);
2833
2834 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2835
2836 simple_ogg_page__init(&page);
2837 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2838 simple_ogg_page__clear(&page);
2839 return; /* state already set */
2840 }
2841
2842 if(FLAC__STREAM_METADATA_HEADER_LENGTH + (18*encoder->private_->seek_table->num_points) > (unsigned)page.body_len) {
2843 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2844 simple_ogg_page__clear(&page);
2845 return;
2846 }
2847
2848 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
2849 FLAC__uint64 xx;
2850 unsigned x;
2851 xx = encoder->private_->seek_table->points[i].sample_number;
2852 b[7] = (FLAC__byte)xx; xx >>= 8;
2853 b[6] = (FLAC__byte)xx; xx >>= 8;
2854 b[5] = (FLAC__byte)xx; xx >>= 8;
2855 b[4] = (FLAC__byte)xx; xx >>= 8;
2856 b[3] = (FLAC__byte)xx; xx >>= 8;
2857 b[2] = (FLAC__byte)xx; xx >>= 8;
2858 b[1] = (FLAC__byte)xx; xx >>= 8;
2859 b[0] = (FLAC__byte)xx; xx >>= 8;
2860 xx = encoder->private_->seek_table->points[i].stream_offset;
2861 b[15] = (FLAC__byte)xx; xx >>= 8;
2862 b[14] = (FLAC__byte)xx; xx >>= 8;
2863 b[13] = (FLAC__byte)xx; xx >>= 8;
2864 b[12] = (FLAC__byte)xx; xx >>= 8;
2865 b[11] = (FLAC__byte)xx; xx >>= 8;
2866 b[10] = (FLAC__byte)xx; xx >>= 8;
2867 b[9] = (FLAC__byte)xx; xx >>= 8;
2868 b[8] = (FLAC__byte)xx; xx >>= 8;
2869 x = encoder->private_->seek_table->points[i].frame_samples;
2870 b[17] = (FLAC__byte)x; x >>= 8;
2871 b[16] = (FLAC__byte)x; x >>= 8;
2872 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2873 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2874 simple_ogg_page__clear(&page);
2875 return;
2876 }
2877 memcpy(p, b, 18);
2878 }
2879
2880 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2881 simple_ogg_page__clear(&page);
2882 return; /* state already set */
2883 }
2884 simple_ogg_page__clear(&page);
2885 }
2886}
2887
Josh Coalsonf1eff452002-07-31 07:05:33 +00002888FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00002889{
Josh Coalsonfa697a92001-08-16 20:07:29 +00002890 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002891
2892 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002893 * Accumulate raw signal to the MD5 signature
2894 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00002895 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 +00002896 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002897 return false;
2898 }
2899
2900 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002901 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002902 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00002903 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002904 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002905 return false;
2906 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002907
2908 /*
2909 * Zero-pad the frame to a byte_boundary
2910 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002911 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002912 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002913 return false;
2914 }
2915
2916 /*
Josh Coalson215af572001-03-27 01:15:58 +00002917 * CRC-16 the whole thing
2918 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002919 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2920 FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
Josh Coalson215af572001-03-27 01:15:58 +00002921
2922 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002923 * Write it
2924 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00002925 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
2926 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002927 return false;
2928 }
2929
2930 /*
2931 * Get ready for the next frame
2932 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002933 encoder->private_->current_sample_number = 0;
2934 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00002935 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002936
2937 return true;
2938}
2939
Josh Coalsonf1eff452002-07-31 07:05:33 +00002940FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002941{
2942 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002943 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002944 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002945
2946 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00002947 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00002948 */
2949 if(is_last_frame) {
2950 max_partition_order = 0;
2951 }
2952 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002953 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2954 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002955 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002956 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002957
Josh Coalsonfa697a92001-08-16 20:07:29 +00002958 precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
Josh Coalson8395d022001-07-12 21:25:22 +00002959
Josh Coalson94e02cd2001-01-25 10:41:06 +00002960 /*
2961 * Setup the frame
2962 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002963 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002964 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002965 return false;
2966 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002967 frame_header.blocksize = encoder->protected_->blocksize;
2968 frame_header.sample_rate = encoder->protected_->sample_rate;
2969 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002970 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002971 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002972 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002973 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002974
2975 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002976 * Figure out what channel assignments to try
2977 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002978 if(encoder->protected_->do_mid_side_stereo) {
2979 if(encoder->protected_->loose_mid_side_stereo) {
2980 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002981 do_independent = true;
2982 do_mid_side = true;
2983 }
2984 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002985 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002986 do_mid_side = !do_independent;
2987 }
2988 }
2989 else {
2990 do_independent = true;
2991 do_mid_side = true;
2992 }
2993 }
2994 else {
2995 do_independent = true;
2996 do_mid_side = false;
2997 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002998
Josh Coalson1b689822001-05-31 20:11:02 +00002999 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003000
3001 /*
Josh Coalson82b73242001-03-28 22:17:05 +00003002 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00003003 */
3004 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003005 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003006 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003007 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3008 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00003009 }
Josh Coalson859bc542001-03-27 22:22:27 +00003010 }
3011 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003012 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00003013 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003014 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003015 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3016 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00003017 }
Josh Coalson859bc542001-03-27 22:22:27 +00003018 }
3019
3020 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003021 * First do a normal encoding pass of each independent channel
3022 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003023 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003024 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003025 if(!
3026 process_subframe_(
3027 encoder,
3028 min_partition_order,
3029 max_partition_order,
3030 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003031 &frame_header,
3032 encoder->private_->subframe_bps[channel],
3033 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003034#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003035 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003036#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003037 encoder->private_->subframe_workspace_ptr[channel],
3038 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3039 encoder->private_->residual_workspace[channel],
3040 encoder->private_->best_subframe+channel,
3041 encoder->private_->best_subframe_bits+channel
3042 )
3043 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003044 return false;
3045 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003046 }
3047
3048 /*
3049 * Now do mid and side channels if requested
3050 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003051 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003052 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003053
3054 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003055 if(!
3056 process_subframe_(
3057 encoder,
3058 min_partition_order,
3059 max_partition_order,
3060 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003061 &frame_header,
3062 encoder->private_->subframe_bps_mid_side[channel],
3063 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003064#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003065 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003066#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003067 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3068 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3069 encoder->private_->residual_workspace_mid_side[channel],
3070 encoder->private_->best_subframe_mid_side+channel,
3071 encoder->private_->best_subframe_bits_mid_side+channel
3072 )
3073 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00003074 return false;
3075 }
3076 }
3077
3078 /*
3079 * Compose the frame bitbuffer
3080 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003081 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00003082 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3083 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003084 FLAC__ChannelAssignment channel_assignment;
3085
Josh Coalsonfa697a92001-08-16 20:07:29 +00003086 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003087
Josh Coalsonfa697a92001-08-16 20:07:29 +00003088 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3089 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 +00003090 }
3091 else {
3092 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3093 unsigned min_bits;
3094 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003095
Josh Coalson1b689822001-05-31 20:11:02 +00003096 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003097
3098 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003099 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3100 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3101 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3102 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 +00003103
Josh Coalson7424d2f2002-11-06 07:10:38 +00003104 for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003105 if(bits[ca] < min_bits) {
3106 min_bits = bits[ca];
3107 channel_assignment = ca;
3108 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003109 }
3110 }
3111
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003112 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003113
Josh Coalsond0edb972006-10-07 06:50:08 +00003114 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003115 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003116 return false;
3117 }
3118
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003119 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003120 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003121 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3122 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003123 break;
3124 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003125 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3126 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003127 break;
3128 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003129 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3130 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003131 break;
3132 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003133 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3134 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003135 break;
3136 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003137 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003138 }
Josh Coalson82b73242001-03-28 22:17:05 +00003139
3140 switch(channel_assignment) {
3141 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003142 left_bps = encoder->private_->subframe_bps [0];
3143 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003144 break;
3145 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003146 left_bps = encoder->private_->subframe_bps [0];
3147 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003148 break;
3149 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003150 left_bps = encoder->private_->subframe_bps_mid_side[1];
3151 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003152 break;
3153 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003154 left_bps = encoder->private_->subframe_bps_mid_side[0];
3155 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003156 break;
3157 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003158 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00003159 }
3160
3161 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00003162 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003163 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00003164 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003165 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003166 }
3167 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00003168 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003169 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003170 return false;
3171 }
3172
Josh Coalsonfa697a92001-08-16 20:07:29 +00003173 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson369a6da2006-10-10 00:37:48 +00003174 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003175 /* the above function sets the state for us in case of an error */
3176 return false;
3177 }
3178 }
3179 }
3180
Josh Coalsonfa697a92001-08-16 20:07:29 +00003181 if(encoder->protected_->loose_mid_side_stereo) {
3182 encoder->private_->loose_mid_side_stereo_frame_count++;
3183 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3184 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003185 }
3186
Josh Coalsonfa697a92001-08-16 20:07:29 +00003187 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003188
Josh Coalson94e02cd2001-01-25 10:41:06 +00003189 return true;
3190}
3191
Josh Coalson6fe72f72002-08-20 04:01:59 +00003192FLAC__bool process_subframe_(
3193 FLAC__StreamEncoder *encoder,
3194 unsigned min_partition_order,
3195 unsigned max_partition_order,
3196 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003197 const FLAC__FrameHeader *frame_header,
3198 unsigned subframe_bps,
3199 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003200#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003201 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003202#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003203 FLAC__Subframe *subframe[2],
3204 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3205 FLAC__int32 *residual[2],
3206 unsigned *best_subframe,
3207 unsigned *best_bits
3208)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003209{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003210#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003211 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003212#else
3213 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3214#endif
3215#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003216 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003217 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 +00003218 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003219 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003220 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003221#endif
3222 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003223 unsigned rice_parameter;
3224 unsigned _candidate_bits, _best_bits;
3225 unsigned _best_subframe;
3226
3227 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3228 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003229 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3230 _best_bits = UINT_MAX;
3231 else
3232 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003233
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003234 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3235 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003236 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 +00003237 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003238 if(
3239 !encoder->private_->disable_constant_subframes &&
3240#ifndef FLAC__INTEGER_ONLY_LIBRARY
3241 fixed_residual_bits_per_sample[1] == 0.0
3242#else
3243 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3244#endif
3245 ) {
3246 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003247 unsigned i;
3248 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003249 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003250 if(integer_signal[0] != integer_signal[i]) {
3251 signal_is_constant = false;
3252 break;
3253 }
3254 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003255 }
3256 if(signal_is_constant) {
3257 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
3258 if(_candidate_bits < _best_bits) {
3259 _best_subframe = !_best_subframe;
3260 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003261 }
3262 }
3263 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003264 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3265 /* encode fixed */
3266 if(encoder->protected_->do_exhaustive_model_search) {
3267 min_fixed_order = 0;
3268 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00003269 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003270 else {
3271 min_fixed_order = max_fixed_order = guess_fixed_order;
3272 }
3273 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003274#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003275 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003276 continue; /* don't even try */
3277 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 +00003278#else
3279 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3280 continue; /* don't even try */
3281 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 */
3282#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003283 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003284 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3285#ifdef DEBUG_VERBOSE
3286 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3287#endif
3288 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3289 }
3290 _candidate_bits =
3291 evaluate_fixed_subframe_(
3292 encoder,
3293 integer_signal,
3294 residual[!_best_subframe],
3295 encoder->private_->abs_residual,
3296 encoder->private_->abs_residual_partition_sums,
3297 encoder->private_->raw_bits_per_partition,
3298 frame_header->blocksize,
3299 subframe_bps,
3300 fixed_order,
3301 rice_parameter,
3302 min_partition_order,
3303 max_partition_order,
3304 precompute_partition_sums,
3305 encoder->protected_->do_escape_coding,
3306 encoder->protected_->rice_parameter_search_dist,
3307 subframe[!_best_subframe],
3308 partitioned_rice_contents[!_best_subframe]
3309 );
3310 if(_candidate_bits < _best_bits) {
3311 _best_subframe = !_best_subframe;
3312 _best_bits = _candidate_bits;
3313 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003314 }
3315 }
3316
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003317#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00003318 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003319 if(encoder->protected_->max_lpc_order > 0) {
3320 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003321 max_lpc_order = frame_header->blocksize-1;
3322 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00003323 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003324 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003325 unsigned a;
3326 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00003327 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003328 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3329 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3330 if(autoc[0] != 0.0) {
3331 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3332 if(encoder->protected_->do_exhaustive_model_search) {
3333 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003334 }
3335 else {
Josh Coalsondf598452006-04-28 00:13:34 +00003336 const unsigned guess_lpc_order =
3337 FLAC__lpc_compute_best_order(
3338 lpc_error,
3339 max_lpc_order,
3340 frame_header->blocksize,
3341 subframe_bps + (
3342 encoder->protected_->do_qlp_coeff_prec_search?
3343 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3344 encoder->protected_->qlp_coeff_precision
3345 )
3346 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003347 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003348 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003349 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3350 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3351 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3352 continue; /* don't even try */
3353 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3354 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3355 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00003356#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003357 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 +00003358#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003359 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3360 }
3361 if(encoder->protected_->do_qlp_coeff_prec_search) {
3362 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00003363 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3364 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003365 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00003366 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3367 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003368 else
3369 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3370 }
3371 else {
3372 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3373 }
3374 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3375 _candidate_bits =
3376 evaluate_lpc_subframe_(
3377 encoder,
3378 integer_signal,
3379 residual[!_best_subframe],
3380 encoder->private_->abs_residual,
3381 encoder->private_->abs_residual_partition_sums,
3382 encoder->private_->raw_bits_per_partition,
3383 encoder->private_->lp_coeff[lpc_order-1],
3384 frame_header->blocksize,
3385 subframe_bps,
3386 lpc_order,
3387 qlp_coeff_precision,
3388 rice_parameter,
3389 min_partition_order,
3390 max_partition_order,
3391 precompute_partition_sums,
3392 encoder->protected_->do_escape_coding,
3393 encoder->protected_->rice_parameter_search_dist,
3394 subframe[!_best_subframe],
3395 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003396 );
3397 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3398 if(_candidate_bits < _best_bits) {
3399 _best_subframe = !_best_subframe;
3400 _best_bits = _candidate_bits;
3401 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00003402 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003403 }
3404 }
3405 }
3406 }
3407 }
3408 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003409#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00003410 }
3411 }
3412
Josh Coalson72695802002-10-11 06:25:16 +00003413 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3414 if(_best_bits == UINT_MAX) {
3415 FLAC__ASSERT(_best_subframe == 0);
3416 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3417 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003418
Josh Coalson94e02cd2001-01-25 10:41:06 +00003419 *best_subframe = _best_subframe;
3420 *best_bits = _best_bits;
3421
3422 return true;
3423}
3424
Josh Coalson6fe72f72002-08-20 04:01:59 +00003425FLAC__bool add_subframe_(
3426 FLAC__StreamEncoder *encoder,
3427 const FLAC__FrameHeader *frame_header,
3428 unsigned subframe_bps,
3429 const FLAC__Subframe *subframe,
3430 FLAC__BitBuffer *frame
3431)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003432{
3433 switch(subframe->type) {
3434 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003435 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003436 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003437 return false;
3438 }
3439 break;
3440 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00003441 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003442 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003443 return false;
3444 }
3445 break;
3446 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00003447 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003448 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003449 return false;
3450 }
3451 break;
3452 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00003453 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003454 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003455 return false;
3456 }
3457 break;
3458 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003459 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003460 }
3461
3462 return true;
3463}
3464
Josh Coalson6fe72f72002-08-20 04:01:59 +00003465unsigned evaluate_constant_subframe_(
3466 const FLAC__int32 signal,
3467 unsigned subframe_bps,
3468 FLAC__Subframe *subframe
3469)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003470{
3471 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3472 subframe->data.constant.value = signal;
3473
Josh Coalson82b73242001-03-28 22:17:05 +00003474 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003475}
3476
Josh Coalson6fe72f72002-08-20 04:01:59 +00003477unsigned evaluate_fixed_subframe_(
3478 FLAC__StreamEncoder *encoder,
3479 const FLAC__int32 signal[],
3480 FLAC__int32 residual[],
3481 FLAC__uint32 abs_residual[],
3482 FLAC__uint64 abs_residual_partition_sums[],
3483 unsigned raw_bits_per_partition[],
3484 unsigned blocksize,
3485 unsigned subframe_bps,
3486 unsigned order,
3487 unsigned rice_parameter,
3488 unsigned min_partition_order,
3489 unsigned max_partition_order,
3490 FLAC__bool precompute_partition_sums,
3491 FLAC__bool do_escape_coding,
3492 unsigned rice_parameter_search_dist,
3493 FLAC__Subframe *subframe,
3494 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3495)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003496{
3497 unsigned i, residual_bits;
3498 const unsigned residual_samples = blocksize - order;
3499
3500 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3501
3502 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3503
3504 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003505 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003506 subframe->data.fixed.residual = residual;
3507
Josh Coalson6fe72f72002-08-20 04:01:59 +00003508 residual_bits =
3509 find_best_partition_order_(
3510 encoder->private_,
3511 residual,
3512 abs_residual,
3513 abs_residual_partition_sums,
3514 raw_bits_per_partition,
3515 residual_samples,
3516 order,
3517 rice_parameter,
3518 min_partition_order,
3519 max_partition_order,
3520 precompute_partition_sums,
3521 do_escape_coding,
3522 rice_parameter_search_dist,
3523 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3524 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003525
3526 subframe->data.fixed.order = order;
3527 for(i = 0; i < order; i++)
3528 subframe->data.fixed.warmup[i] = signal[i];
3529
Josh Coalson82b73242001-03-28 22:17:05 +00003530 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003531}
3532
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003533#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003534unsigned evaluate_lpc_subframe_(
3535 FLAC__StreamEncoder *encoder,
3536 const FLAC__int32 signal[],
3537 FLAC__int32 residual[],
3538 FLAC__uint32 abs_residual[],
3539 FLAC__uint64 abs_residual_partition_sums[],
3540 unsigned raw_bits_per_partition[],
3541 const FLAC__real lp_coeff[],
3542 unsigned blocksize,
3543 unsigned subframe_bps,
3544 unsigned order,
3545 unsigned qlp_coeff_precision,
3546 unsigned rice_parameter,
3547 unsigned min_partition_order,
3548 unsigned max_partition_order,
3549 FLAC__bool precompute_partition_sums,
3550 FLAC__bool do_escape_coding,
3551 unsigned rice_parameter_search_dist,
3552 FLAC__Subframe *subframe,
3553 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3554)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003555{
Josh Coalson77e3f312001-06-23 03:03:24 +00003556 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003557 unsigned i, residual_bits;
3558 int quantization, ret;
3559 const unsigned residual_samples = blocksize - order;
3560
Josh Coalson20ac2c12002-08-30 05:47:14 +00003561 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3562 if(subframe_bps <= 16) {
3563 FLAC__ASSERT(order > 0);
3564 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3565 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3566 }
3567
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003568 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003569 if(ret != 0)
3570 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3571
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003572 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3573 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3574 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3575 else
3576 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 +00003577 else
3578 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 +00003579
3580 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3581
3582 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003583 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003584 subframe->data.lpc.residual = residual;
3585
Josh Coalson6fe72f72002-08-20 04:01:59 +00003586 residual_bits =
3587 find_best_partition_order_(
3588 encoder->private_,
3589 residual,
3590 abs_residual,
3591 abs_residual_partition_sums,
3592 raw_bits_per_partition,
3593 residual_samples,
3594 order,
3595 rice_parameter,
3596 min_partition_order,
3597 max_partition_order,
3598 precompute_partition_sums,
3599 do_escape_coding,
3600 rice_parameter_search_dist,
3601 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3602 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003603
3604 subframe->data.lpc.order = order;
3605 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3606 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003607 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003608 for(i = 0; i < order; i++)
3609 subframe->data.lpc.warmup[i] = signal[i];
3610
Josh Coalson82b73242001-03-28 22:17:05 +00003611 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003612}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003613#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003614
Josh Coalson6fe72f72002-08-20 04:01:59 +00003615unsigned evaluate_verbatim_subframe_(
3616 const FLAC__int32 signal[],
3617 unsigned blocksize,
3618 unsigned subframe_bps,
3619 FLAC__Subframe *subframe
3620)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003621{
3622 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3623
3624 subframe->data.verbatim.data = signal;
3625
Josh Coalson82b73242001-03-28 22:17:05 +00003626 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003627}
3628
Josh Coalson6fe72f72002-08-20 04:01:59 +00003629unsigned find_best_partition_order_(
3630 FLAC__StreamEncoderPrivate *private_,
3631 const FLAC__int32 residual[],
3632 FLAC__uint32 abs_residual[],
3633 FLAC__uint64 abs_residual_partition_sums[],
3634 unsigned raw_bits_per_partition[],
3635 unsigned residual_samples,
3636 unsigned predictor_order,
3637 unsigned rice_parameter,
3638 unsigned min_partition_order,
3639 unsigned max_partition_order,
3640 FLAC__bool precompute_partition_sums,
3641 FLAC__bool do_escape_coding,
3642 unsigned rice_parameter_search_dist,
3643 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3644)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003645{
Josh Coalson77e3f312001-06-23 03:03:24 +00003646 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003647 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00003648 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00003649 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003650 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003651
Josh Coalson2051dd42001-04-12 22:22:34 +00003652 /* compute abs(residual) for use later */
3653 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3654 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00003655 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00003656 }
3657
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003658 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 +00003659 min_partition_order = min(min_partition_order, max_partition_order);
3660
Josh Coalson8395d022001-07-12 21:25:22 +00003661 if(precompute_partition_sums) {
3662 int partition_order;
3663 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003664
Josh Coalsonf1eff452002-07-31 07:05:33 +00003665 precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
Josh Coalson8395d022001-07-12 21:25:22 +00003666
3667 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00003668 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
Josh Coalson8395d022001-07-12 21:25:22 +00003669
3670 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3671#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003672 if(!
3673 set_partitioned_rice_with_precompute_(
3674 residual,
3675 abs_residual_partition_sums+sum,
3676 raw_bits_per_partition+sum,
3677 residual_samples,
3678 predictor_order,
3679 rice_parameter,
3680 rice_parameter_search_dist,
3681 (unsigned)partition_order,
3682 do_escape_coding,
3683 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3684 &residual_bits
3685 )
3686 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003687#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003688 if(!
3689 set_partitioned_rice_with_precompute_(
3690 abs_residual,
3691 abs_residual_partition_sums+sum,
3692 raw_bits_per_partition+sum,
3693 residual_samples,
3694 predictor_order,
3695 rice_parameter,
3696 rice_parameter_search_dist,
3697 (unsigned)partition_order,
3698 do_escape_coding,
3699 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3700 &residual_bits
3701 )
3702 )
Josh Coalson8395d022001-07-12 21:25:22 +00003703#endif
3704 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003705 FLAC__ASSERT(best_residual_bits != 0);
3706 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003707 }
3708 sum += 1u << partition_order;
3709 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3710 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003711 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003712 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003713 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003714 }
3715 }
Josh Coalson8395d022001-07-12 21:25:22 +00003716 else {
3717 unsigned partition_order;
3718 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3719#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003720 if(!
3721 set_partitioned_rice_(
3722 abs_residual,
3723 residual,
3724 residual_samples,
3725 predictor_order,
3726 rice_parameter,
3727 rice_parameter_search_dist,
3728 partition_order,
3729 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3730 &residual_bits
3731 )
3732 )
Josh Coalson8395d022001-07-12 21:25:22 +00003733#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003734 if(!
3735 set_partitioned_rice_(
3736 abs_residual,
3737 residual_samples,
3738 predictor_order,
3739 rice_parameter,
3740 rice_parameter_search_dist,
3741 partition_order,
3742 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3743 &residual_bits
3744 )
3745 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003746#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003747 {
3748 FLAC__ASSERT(best_residual_bits != 0);
3749 break;
3750 }
3751 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3752 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003753 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003754 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003755 }
3756 }
3757 }
3758
Josh Coalsona37ba462002-08-19 21:36:39 +00003759 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003760 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003761 * it is const to the outside world.
3762 */
3763 {
3764 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3765 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3766 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3767 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3768 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003769
3770 return best_residual_bits;
3771}
3772
Josh Coalson6fe72f72002-08-20 04:01:59 +00003773void precompute_partition_info_sums_(
3774 const FLAC__uint32 abs_residual[],
3775 FLAC__uint64 abs_residual_partition_sums[],
3776 unsigned residual_samples,
3777 unsigned predictor_order,
3778 unsigned min_partition_order,
3779 unsigned max_partition_order
3780)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003781{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003782 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003783 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003784 const unsigned blocksize = residual_samples + predictor_order;
3785
Josh Coalsonaef013c2001-04-24 01:25:42 +00003786 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003787 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003788 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00003789 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003790 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003791 const unsigned partitions = 1u << partition_order;
3792 const unsigned default_partition_samples = blocksize >> partition_order;
3793
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003794 FLAC__ASSERT(default_partition_samples > predictor_order);
3795
3796 for(partition = residual_sample = 0; partition < partitions; partition++) {
3797 partition_samples = default_partition_samples;
3798 if(partition == 0)
3799 partition_samples -= predictor_order;
3800 abs_residual_partition_sum = 0;
3801 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3802 abs_r = abs_residual[residual_sample];
3803 abs_residual_partition_sum += abs_r;
3804 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003805 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003806 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003807 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003808 to_partition = partitions;
3809 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003810 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00003811
Josh Coalson8395d022001-07-12 21:25:22 +00003812 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00003813 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003814 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003815 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003816 const unsigned partitions = 1u << partition_order;
3817 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00003818 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003819 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003820 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003821 from_partition++;
3822 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003823 }
3824 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003825}
Josh Coalson8395d022001-07-12 21:25:22 +00003826
Josh Coalson6fe72f72002-08-20 04:01:59 +00003827void precompute_partition_info_escapes_(
3828 const FLAC__int32 residual[],
3829 unsigned raw_bits_per_partition[],
3830 unsigned residual_samples,
3831 unsigned predictor_order,
3832 unsigned min_partition_order,
3833 unsigned max_partition_order
3834)
Josh Coalson8395d022001-07-12 21:25:22 +00003835{
3836 int partition_order;
3837 unsigned from_partition, to_partition = 0;
3838 const unsigned blocksize = residual_samples + predictor_order;
3839
3840 /* first do max_partition_order */
3841 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3842 FLAC__int32 r, residual_partition_min, residual_partition_max;
3843 unsigned silog2_min, silog2_max;
3844 unsigned partition, partition_sample, partition_samples, residual_sample;
3845 const unsigned partitions = 1u << partition_order;
3846 const unsigned default_partition_samples = blocksize >> partition_order;
3847
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003848 FLAC__ASSERT(default_partition_samples > predictor_order);
3849
3850 for(partition = residual_sample = 0; partition < partitions; partition++) {
3851 partition_samples = default_partition_samples;
3852 if(partition == 0)
3853 partition_samples -= predictor_order;
3854 residual_partition_min = residual_partition_max = 0;
3855 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3856 r = residual[residual_sample];
3857 if(r < residual_partition_min)
3858 residual_partition_min = r;
3859 else if(r > residual_partition_max)
3860 residual_partition_max = r;
3861 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00003862 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003863 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
3864 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
3865 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00003866 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003867 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00003868 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00003869 }
3870
3871 /* now merge partitions for lower orders */
3872 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3873 unsigned m;
3874 unsigned i;
3875 const unsigned partitions = 1u << partition_order;
3876 for(i = 0; i < partitions; i++) {
3877 m = raw_bits_per_partition[from_partition];
3878 from_partition++;
3879 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
3880 from_partition++;
3881 to_partition++;
3882 }
3883 }
3884}
Josh Coalson94e02cd2001-01-25 10:41:06 +00003885
Josh Coalson352e0f62001-03-20 22:55:50 +00003886#ifdef VARIABLE_RICE_BITS
3887#undef VARIABLE_RICE_BITS
3888#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003889#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003890#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00003891#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003892
Josh Coalson8395d022001-07-12 21:25:22 +00003893#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003894FLAC__bool set_partitioned_rice_(
3895 const FLAC__uint32 abs_residual[],
3896 const FLAC__int32 residual[],
3897 const unsigned residual_samples,
3898 const unsigned predictor_order,
3899 const unsigned suggested_rice_parameter,
3900 const unsigned rice_parameter_search_dist,
3901 const unsigned partition_order,
3902 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3903 unsigned *bits
3904)
Josh Coalson8395d022001-07-12 21:25:22 +00003905#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003906FLAC__bool set_partitioned_rice_(
3907 const FLAC__uint32 abs_residual[],
3908 const unsigned residual_samples,
3909 const unsigned predictor_order,
3910 const unsigned suggested_rice_parameter,
3911 const unsigned rice_parameter_search_dist,
3912 const unsigned partition_order,
3913 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3914 unsigned *bits
3915)
Josh Coalson8395d022001-07-12 21:25:22 +00003916#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003917{
Josh Coalson034dfab2001-04-27 19:10:23 +00003918 unsigned rice_parameter, partition_bits;
3919#ifndef NO_RICE_SEARCH
3920 unsigned best_partition_bits;
3921 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3922#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003923 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003924 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003925
Josh Coalson1b689822001-05-31 20:11:02 +00003926 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00003927
Josh Coalsona37ba462002-08-19 21:36:39 +00003928 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3929 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003930
Josh Coalson94e02cd2001-01-25 10:41:06 +00003931 if(partition_order == 0) {
3932 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00003933
Josh Coalson034dfab2001-04-27 19:10:23 +00003934#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003935 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003936 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003937 min_rice_parameter = 0;
3938 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003939 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3940 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003941 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003942#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003943 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3944#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003945 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003946 }
3947 }
3948 else
3949 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3950
3951 best_partition_bits = 0xffffffff;
3952 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3953#endif
3954#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003955 const unsigned rice_parameter_estimate = rice_parameter-1;
3956 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00003957#else
3958 partition_bits = 0;
3959#endif
3960 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3961 for(i = 0; i < residual_samples; i++) {
3962#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003963 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00003964#else
3965 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3966#endif
3967 }
3968#ifndef NO_RICE_SEARCH
3969 if(partition_bits < best_partition_bits) {
3970 best_rice_parameter = rice_parameter;
3971 best_partition_bits = partition_bits;
3972 }
3973 }
3974#endif
3975 parameters[0] = best_rice_parameter;
3976 bits_ += best_partition_bits;
3977 }
3978 else {
3979 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003980 unsigned partition_samples;
3981 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00003982 const unsigned partitions = 1u << partition_order;
3983 for(partition = residual_sample = 0; partition < partitions; partition++) {
3984 partition_samples = (residual_samples+predictor_order) >> partition_order;
3985 if(partition == 0) {
3986 if(partition_samples <= predictor_order)
3987 return false;
3988 else
3989 partition_samples -= predictor_order;
3990 }
3991 mean = 0;
3992 save_residual_sample = residual_sample;
3993 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003994 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00003995 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00003996 /* we are basically calculating the size in bits of the
3997 * average residual magnitude in the partition:
3998 * rice_parameter = floor(log2(mean/partition_samples))
3999 * 'mean' is not a good name for the variable, it is
4000 * actually the sum of magnitudes of all residual values
4001 * in the partition, so the actual mean is
4002 * mean/partition_samples
4003 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004004 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00004005 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004006 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004007#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004008 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4009#endif
4010 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4011 }
4012
4013#ifndef NO_RICE_SEARCH
4014 if(rice_parameter_search_dist) {
4015 if(rice_parameter < rice_parameter_search_dist)
4016 min_rice_parameter = 0;
4017 else
4018 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4019 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4020 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004021#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004022 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4023#endif
4024 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4025 }
4026 }
4027 else
4028 min_rice_parameter = max_rice_parameter = rice_parameter;
4029
4030 best_partition_bits = 0xffffffff;
4031 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4032#endif
4033#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004034 const unsigned rice_parameter_estimate = rice_parameter-1;
4035 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004036#else
4037 partition_bits = 0;
4038#endif
4039 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4040 save_residual_sample = residual_sample;
4041 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4042#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004043 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004044#else
4045 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4046#endif
4047 }
4048#ifndef NO_RICE_SEARCH
4049 if(rice_parameter != max_rice_parameter)
4050 residual_sample = save_residual_sample;
4051 if(partition_bits < best_partition_bits) {
4052 best_rice_parameter = rice_parameter;
4053 best_partition_bits = partition_bits;
4054 }
4055 }
4056#endif
4057 parameters[partition] = best_rice_parameter;
4058 bits_ += best_partition_bits;
4059 }
4060 }
4061
4062 *bits = bits_;
4063 return true;
4064}
4065
4066#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004067FLAC__bool set_partitioned_rice_with_precompute_(
4068 const FLAC__int32 residual[],
4069 const FLAC__uint64 abs_residual_partition_sums[],
4070 const unsigned raw_bits_per_partition[],
4071 const unsigned residual_samples,
4072 const unsigned predictor_order,
4073 const unsigned suggested_rice_parameter,
4074 const unsigned rice_parameter_search_dist,
4075 const unsigned partition_order,
4076 const FLAC__bool search_for_escapes,
4077 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4078 unsigned *bits
4079)
Josh Coalson8395d022001-07-12 21:25:22 +00004080#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004081FLAC__bool set_partitioned_rice_with_precompute_(
4082 const FLAC__uint32 abs_residual[],
4083 const FLAC__uint64 abs_residual_partition_sums[],
4084 const unsigned raw_bits_per_partition[],
4085 const unsigned residual_samples,
4086 const unsigned predictor_order,
4087 const unsigned suggested_rice_parameter,
4088 const unsigned rice_parameter_search_dist,
4089 const unsigned partition_order,
4090 const FLAC__bool search_for_escapes,
4091 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4092 unsigned *bits
4093)
Josh Coalson8395d022001-07-12 21:25:22 +00004094#endif
4095{
4096 unsigned rice_parameter, partition_bits;
4097#ifndef NO_RICE_SEARCH
4098 unsigned best_partition_bits;
4099 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4100#endif
4101 unsigned flat_bits;
4102 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004103 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004104
4105 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4106
Josh Coalsona37ba462002-08-19 21:36:39 +00004107 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4108 parameters = partitioned_rice_contents->parameters;
4109 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004110
Josh Coalson8395d022001-07-12 21:25:22 +00004111 if(partition_order == 0) {
4112 unsigned i;
4113
4114#ifndef NO_RICE_SEARCH
4115 if(rice_parameter_search_dist) {
4116 if(suggested_rice_parameter < rice_parameter_search_dist)
4117 min_rice_parameter = 0;
4118 else
4119 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4120 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4121 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004122#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004123 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4124#endif
4125 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4126 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004127 }
4128 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004129 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00004130
Josh Coalson034dfab2001-04-27 19:10:23 +00004131 best_partition_bits = 0xffffffff;
4132 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4133#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004134#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004135 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00004136 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004137#else
4138 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004139#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004140 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00004141 for(i = 0; i < residual_samples; i++) {
4142#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00004143 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004144#else
Josh Coalson2051dd42001-04-12 22:22:34 +00004145 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00004146#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004147 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004148#ifndef NO_RICE_SEARCH
4149 if(partition_bits < best_partition_bits) {
4150 best_rice_parameter = rice_parameter;
4151 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00004152 }
4153 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004154#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004155 if(search_for_escapes) {
4156 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
4157 if(flat_bits <= best_partition_bits) {
4158 raw_bits[0] = raw_bits_per_partition[0];
4159 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4160 best_partition_bits = flat_bits;
4161 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004162 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004163 parameters[0] = best_rice_parameter;
4164 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004165 }
4166 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00004167 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004168 unsigned partition_samples;
4169 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004170 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00004171 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004172 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00004173 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004174 if(partition_samples <= predictor_order)
4175 return false;
4176 else
4177 partition_samples -= predictor_order;
4178 }
Josh Coalson05d20792001-06-29 23:12:26 +00004179 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004180 /* we are basically calculating the size in bits of the
4181 * average residual magnitude in the partition:
4182 * rice_parameter = floor(log2(mean/partition_samples))
4183 * 'mean' is not a good name for the variable, it is
4184 * actually the sum of magnitudes of all residual values
4185 * in the partition, so the actual mean is
4186 * mean/partition_samples
4187 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004188 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00004189 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004190 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004191#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004192 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4193#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004194 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004195 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004196
Josh Coalson034dfab2001-04-27 19:10:23 +00004197#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004198 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004199 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004200 min_rice_parameter = 0;
4201 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004202 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4203 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004204 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004205#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004206 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4207#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004208 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004209 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004210 }
4211 else
4212 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00004213
Josh Coalson034dfab2001-04-27 19:10:23 +00004214 best_partition_bits = 0xffffffff;
4215 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4216#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004217#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00004218 const unsigned rice_parameter_estimate = rice_parameter-1;
4219 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004220#else
4221 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004222#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004223 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00004224 save_residual_sample = residual_sample;
4225 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00004226#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00004227 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004228#else
Josh Coalson4dacd192001-06-06 21:11:44 +00004229 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00004230#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004231 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004232#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00004233 if(rice_parameter != max_rice_parameter)
4234 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00004235 if(partition_bits < best_partition_bits) {
4236 best_rice_parameter = rice_parameter;
4237 best_partition_bits = partition_bits;
4238 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004239 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004240#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004241 if(search_for_escapes) {
4242 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
4243 if(flat_bits <= best_partition_bits) {
4244 raw_bits[partition] = raw_bits_per_partition[partition];
4245 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4246 best_partition_bits = flat_bits;
4247 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004248 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004249 parameters[partition] = best_rice_parameter;
4250 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004251 }
4252 }
4253
4254 *bits = bits_;
4255 return true;
4256}
Josh Coalson859bc542001-03-27 22:22:27 +00004257
Josh Coalsonf1eff452002-07-31 07:05:33 +00004258unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00004259{
4260 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00004261 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00004262
4263 for(i = 0; i < samples && !(x&1); i++)
4264 x |= signal[i];
4265
4266 if(x == 0) {
4267 shift = 0;
4268 }
4269 else {
4270 for(shift = 0; !(x&1); shift++)
4271 x >>= 1;
4272 }
4273
4274 if(shift > 0) {
4275 for(i = 0; i < samples; i++)
4276 signal[i] >>= shift;
4277 }
4278
4279 return shift;
4280}
Josh Coalsond86e03b2002-08-03 21:56:15 +00004281
4282void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4283{
4284 unsigned channel;
4285
4286 for(channel = 0; channel < channels; channel++)
4287 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4288
4289 fifo->tail += wide_samples;
4290
4291 FLAC__ASSERT(fifo->tail <= fifo->size);
4292}
4293
4294void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4295{
4296 unsigned channel;
4297 unsigned sample, wide_sample;
4298 unsigned tail = fifo->tail;
4299
4300 sample = input_offset * channels;
4301 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4302 for(channel = 0; channel < channels; channel++)
4303 fifo->data[channel][tail] = input[sample++];
4304 tail++;
4305 }
4306 fifo->tail = tail;
4307
4308 FLAC__ASSERT(fifo->tail <= fifo->size);
4309}
4310
4311FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
4312{
4313 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4314 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
4315 (void)decoder;
4316
4317 if(encoder->private_->verify.needs_magic_hack) {
4318 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4319 *bytes = FLAC__STREAM_SYNC_LENGTH;
4320 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4321 encoder->private_->verify.needs_magic_hack = false;
4322 }
4323 else {
4324 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00004325 /*
4326 * If we get here, a FIFO underflow has occurred,
4327 * which means there is a bug somewhere.
4328 */
4329 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004330 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4331 }
4332 else if(encoded_bytes < *bytes)
4333 *bytes = encoded_bytes;
4334 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4335 encoder->private_->verify.output.data += *bytes;
4336 encoder->private_->verify.output.bytes -= *bytes;
4337 }
4338
4339 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4340}
4341
4342FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4343{
4344 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4345 unsigned channel;
4346 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
4347 const unsigned blocksize = frame->header.blocksize;
4348 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4349
4350 for(channel = 0; channel < channels; channel++) {
4351 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4352 unsigned i, sample = 0;
4353 FLAC__int32 expect = 0, got = 0;
4354
4355 for(i = 0; i < blocksize; i++) {
4356 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4357 sample = i;
4358 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4359 got = (FLAC__int32)buffer[channel][i];
4360 break;
4361 }
4362 }
4363 FLAC__ASSERT(i < blocksize);
4364 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4365 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00004366 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004367 encoder->private_->verify.error_stats.channel = channel;
4368 encoder->private_->verify.error_stats.sample = sample;
4369 encoder->private_->verify.error_stats.expected = expect;
4370 encoder->private_->verify.error_stats.got = got;
4371 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4372 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4373 }
4374 }
4375 /* dequeue the frame from the fifo */
4376 for(channel = 0; channel < channels; channel++) {
4377 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
4378 }
4379 encoder->private_->verify.input_fifo.tail -= blocksize;
4380 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4381}
4382
4383void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4384{
4385 (void)decoder, (void)metadata, (void)client_data;
4386}
4387
4388void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4389{
4390 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4391 (void)decoder, (void)status;
4392 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4393}
Josh Coalson6b21f662006-09-13 01:42:27 +00004394
Josh Coalson8da98c82006-10-15 04:24:05 +00004395FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
4396{
4397 (void)client_data;
4398
4399 *bytes = (unsigned)fread(buffer, 1, *bytes, encoder->private_->file);
4400 if (*bytes == 0) {
4401 if (feof(encoder->private_->file))
4402 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4403 else if (ferror(encoder->private_->file))
4404 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4405 }
4406 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4407}
4408
Josh Coalson6b21f662006-09-13 01:42:27 +00004409FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4410{
4411 (void)client_data;
4412
4413 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4414 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4415 else
4416 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4417}
4418
4419FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4420{
4421 off_t offset;
4422
4423 (void)client_data;
4424
4425 offset = ftello(encoder->private_->file);
4426
4427 if(offset < 0) {
4428 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4429 }
4430 else {
4431 *absolute_byte_offset = (FLAC__uint64)offset;
4432 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4433 }
4434}
4435
4436#ifdef FLAC__VALGRIND_TESTING
4437static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4438{
4439 size_t ret = fwrite(ptr, size, nmemb, stream);
4440 if(!ferror(stream))
4441 fflush(stream);
4442 return ret;
4443}
4444#else
4445#define local__fwrite fwrite
4446#endif
4447
4448FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
4449{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004450 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00004451
4452 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson8da98c82006-10-15 04:24:05 +00004453 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4454#if FLAC__HAS_OGG
4455 /* We would like to be able to use 'samples > 0' in the
4456 * clause here but currently because of the nature of our
4457 * Ogg writing implementation, 'samples' is always 0 (see
4458 * ogg_encoder_aspect.c). The downside is extra progress
4459 * callbacks.
4460 */
4461 encoder->private_->is_ogg? true :
4462#endif
4463 samples > 0
4464 );
4465 if(call_it) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004466 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4467 * because at this point in the callback chain, the stats
4468 * have not been updated. Only after we return and control
4469 * gets back to write_frame_() are the stats updated
4470 */
4471 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);
4472 }
Josh Coalson6b21f662006-09-13 01:42:27 +00004473 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4474 }
4475 else
4476 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4477}
4478
4479/*
4480 * This will forcibly set stdout to binary mode (for OSes that require it)
4481 */
4482FILE *get_binary_stdout_()
4483{
4484 /* if something breaks here it is probably due to the presence or
4485 * absence of an underscore before the identifiers 'setmode',
4486 * 'fileno', and/or 'O_BINARY'; check your system header files.
4487 */
4488#if defined _MSC_VER || defined __MINGW32__
4489 _setmode(_fileno(stdout), _O_BINARY);
4490#elif defined __CYGWIN__
4491 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4492 setmode(_fileno(stdout), _O_BINARY);
4493#elif defined __EMX__
4494 setmode(fileno(stdout), O_BINARY);
4495#endif
4496
4497 return stdout;
4498}