blob: 8c3c99003b95368c09971d4e7128042fe8384111 [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 Coalson15b8eb82006-10-15 05:15:55 +0000113#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +0000114static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000115#endif
Josh Coalsonf1eff452002-07-31 07:05:33 +0000116static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
117static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000118
119static FLAC__bool process_subframe_(
120 FLAC__StreamEncoder *encoder,
121 unsigned min_partition_order,
122 unsigned max_partition_order,
123 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000124 const FLAC__FrameHeader *frame_header,
125 unsigned subframe_bps,
126 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000127#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000128 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000129#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000130 FLAC__Subframe *subframe[2],
131 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
132 FLAC__int32 *residual[2],
133 unsigned *best_subframe,
134 unsigned *best_bits
135);
136
137static FLAC__bool add_subframe_(
138 FLAC__StreamEncoder *encoder,
139 const FLAC__FrameHeader *frame_header,
140 unsigned subframe_bps,
141 const FLAC__Subframe *subframe,
142 FLAC__BitBuffer *frame
143);
144
145static unsigned evaluate_constant_subframe_(
146 const FLAC__int32 signal,
147 unsigned subframe_bps,
148 FLAC__Subframe *subframe
149);
150
151static unsigned evaluate_fixed_subframe_(
152 FLAC__StreamEncoder *encoder,
153 const FLAC__int32 signal[],
154 FLAC__int32 residual[],
155 FLAC__uint32 abs_residual[],
156 FLAC__uint64 abs_residual_partition_sums[],
157 unsigned raw_bits_per_partition[],
158 unsigned blocksize,
159 unsigned subframe_bps,
160 unsigned order,
161 unsigned rice_parameter,
162 unsigned min_partition_order,
163 unsigned max_partition_order,
164 FLAC__bool precompute_partition_sums,
165 FLAC__bool do_escape_coding,
166 unsigned rice_parameter_search_dist,
167 FLAC__Subframe *subframe,
168 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
169);
170
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000171#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000172static unsigned evaluate_lpc_subframe_(
173 FLAC__StreamEncoder *encoder,
174 const FLAC__int32 signal[],
175 FLAC__int32 residual[],
176 FLAC__uint32 abs_residual[],
177 FLAC__uint64 abs_residual_partition_sums[],
178 unsigned raw_bits_per_partition[],
179 const FLAC__real lp_coeff[],
180 unsigned blocksize,
181 unsigned subframe_bps,
182 unsigned order,
183 unsigned qlp_coeff_precision,
184 unsigned rice_parameter,
185 unsigned min_partition_order,
186 unsigned max_partition_order,
187 FLAC__bool precompute_partition_sums,
188 FLAC__bool do_escape_coding,
189 unsigned rice_parameter_search_dist,
190 FLAC__Subframe *subframe,
191 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
192);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000193#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000194
195static unsigned evaluate_verbatim_subframe_(
196 const FLAC__int32 signal[],
197 unsigned blocksize,
198 unsigned subframe_bps,
199 FLAC__Subframe *subframe
200);
201
202static unsigned find_best_partition_order_(
203 struct FLAC__StreamEncoderPrivate *private_,
204 const FLAC__int32 residual[],
205 FLAC__uint32 abs_residual[],
206 FLAC__uint64 abs_residual_partition_sums[],
207 unsigned raw_bits_per_partition[],
208 unsigned residual_samples,
209 unsigned predictor_order,
210 unsigned rice_parameter,
211 unsigned min_partition_order,
212 unsigned max_partition_order,
213 FLAC__bool precompute_partition_sums,
214 FLAC__bool do_escape_coding,
215 unsigned rice_parameter_search_dist,
216 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
217);
218
219static void precompute_partition_info_sums_(
220 const FLAC__uint32 abs_residual[],
221 FLAC__uint64 abs_residual_partition_sums[],
222 unsigned residual_samples,
223 unsigned predictor_order,
224 unsigned min_partition_order,
225 unsigned max_partition_order
226);
227
228static void precompute_partition_info_escapes_(
229 const FLAC__int32 residual[],
230 unsigned raw_bits_per_partition[],
231 unsigned residual_samples,
232 unsigned predictor_order,
233 unsigned min_partition_order,
234 unsigned max_partition_order
235);
236
Josh Coalson8395d022001-07-12 21:25:22 +0000237#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000238static FLAC__bool set_partitioned_rice_(
239 const FLAC__uint32 abs_residual[],
240 const FLAC__int32 residual[],
241 const unsigned residual_samples,
242 const unsigned predictor_order,
243 const unsigned suggested_rice_parameter,
244 const unsigned rice_parameter_search_dist,
245 const unsigned partition_order,
246 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
247 unsigned *bits
248);
249
250static FLAC__bool set_partitioned_rice_with_precompute_(
251 const FLAC__int32 residual[],
252 const FLAC__uint64 abs_residual_partition_sums[],
253 const unsigned raw_bits_per_partition[],
254 const unsigned residual_samples,
255 const unsigned predictor_order,
256 const unsigned suggested_rice_parameter,
257 const unsigned rice_parameter_search_dist,
258 const unsigned partition_order,
259 const FLAC__bool search_for_escapes,
260 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
261 unsigned *bits
262);
Josh Coalson8395d022001-07-12 21:25:22 +0000263#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000264static FLAC__bool set_partitioned_rice_(
265 const FLAC__uint32 abs_residual[],
266 const unsigned residual_samples,
267 const unsigned predictor_order,
268 const unsigned suggested_rice_parameter,
269 const unsigned rice_parameter_search_dist,
270 const unsigned partition_order,
271 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
272 unsigned *bits
273);
274
275static FLAC__bool set_partitioned_rice_with_precompute_(
276 const FLAC__uint32 abs_residual[],
277 const FLAC__uint64 abs_residual_partition_sums[],
278 const unsigned raw_bits_per_partition[],
279 const unsigned residual_samples,
280 const unsigned predictor_order,
281 const unsigned suggested_rice_parameter,
282 const unsigned rice_parameter_search_dist,
283 const unsigned partition_order,
284 const FLAC__bool search_for_escapes,
285 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
286 unsigned *bits
287);
Josh Coalson0a15c142001-06-13 17:59:57 +0000288#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000289
Josh Coalsonf1eff452002-07-31 07:05:33 +0000290static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000291
Josh Coalsond86e03b2002-08-03 21:56:15 +0000292/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000293static void append_to_verify_fifo_(
294 verify_input_fifo *fifo,
295 const FLAC__int32 * const input[],
296 unsigned input_offset,
297 unsigned channels,
298 unsigned wide_samples
299);
300
301static void append_to_verify_fifo_interleaved_(
302 verify_input_fifo *fifo,
303 const FLAC__int32 input[],
304 unsigned input_offset,
305 unsigned channels,
306 unsigned wide_samples
307);
308
Josh Coalson6b21f662006-09-13 01:42:27 +0000309static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
310static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
311static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
312static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000313
Josh Coalson8da98c82006-10-15 04:24:05 +0000314static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
Josh Coalson6b21f662006-09-13 01:42:27 +0000315static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
316static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
317static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
318static FILE *get_binary_stdout_();
Josh Coalson6fe72f72002-08-20 04:01:59 +0000319
Josh Coalson0a15c142001-06-13 17:59:57 +0000320
321/***********************************************************************
322 *
323 * Private class data
324 *
325 ***********************************************************************/
326
327typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000328 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000329 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
330 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 +0000331#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000332 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
333 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 +0000334 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
335 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000336#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000337 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
338 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 +0000339 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
340 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000341 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
342 FLAC__Subframe subframe_workspace_mid_side[2][2];
343 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
344 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000345 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
346 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
347 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
348 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000349 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000350 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000351 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000352 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000353 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000354 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
Josh Coalson8395d022001-07-12 21:25:22 +0000355 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
Josh Coalsonaec256b2002-03-12 16:19:54 +0000356 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000357 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
358 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000359 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalson6b21f662006-09-13 01:42:27 +0000360 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
361 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000362 unsigned current_sample_number;
363 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000364 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000365 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000366#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000367 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000368#else
369 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
370#endif
371#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000372 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
Josh Coalson7446e182005-01-26 04:04:38 +0000373 void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
374 void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
375 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000376#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000377 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
378 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
379 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
380 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 +0000381 FLAC__bool disable_constant_subframes;
382 FLAC__bool disable_fixed_subframes;
383 FLAC__bool disable_verbatim_subframes;
Josh Coalson8da98c82006-10-15 04:24:05 +0000384#if FLAC__HAS_OGG
385 FLAC__bool is_ogg;
386#endif
387 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
Josh Coalson6b21f662006-09-13 01:42:27 +0000388 FLAC__StreamEncoderSeekCallback seek_callback;
389 FLAC__StreamEncoderTellCallback tell_callback;
Josh Coalson681c2932002-08-01 08:19:37 +0000390 FLAC__StreamEncoderWriteCallback write_callback;
391 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000392 FLAC__StreamEncoderProgressCallback progress_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000393 void *client_data;
Josh Coalson6b21f662006-09-13 01:42:27 +0000394 unsigned first_seekpoint_to_check;
395 FILE *file; /* only used when encoding to a file */
396 FLAC__uint64 bytes_written;
397 FLAC__uint64 samples_written;
398 unsigned frames_written;
399 unsigned total_frames_estimate;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000400 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000401 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
402 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000403#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000404 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
405 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000406 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
407 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000408#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000409 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
410 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
411 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000412 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000413 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000414 /*
415 * These fields have been moved here from private function local
416 * declarations merely to save stack space during encoding.
417 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000418#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000419 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000420#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000421 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000422 /*
423 * The data for the verify section
424 */
425 struct {
426 FLAC__StreamDecoder *decoder;
427 EncoderStateHint state_hint;
428 FLAC__bool needs_magic_hack;
429 verify_input_fifo input_fifo;
430 verify_output output;
431 struct {
432 FLAC__uint64 absolute_sample;
433 unsigned frame_number;
434 unsigned channel;
435 unsigned sample;
436 FLAC__int32 expected;
437 FLAC__int32 got;
438 } error_stats;
439 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000440 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000441} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000442
Josh Coalson0a15c142001-06-13 17:59:57 +0000443/***********************************************************************
444 *
445 * Public static class data
446 *
447 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000448
Josh Coalson6afed9f2002-10-16 22:29:47 +0000449FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000450 "FLAC__STREAM_ENCODER_OK",
Josh Coalson6b21f662006-09-13 01:42:27 +0000451 "FLAC__STREAM_ENCODER_UNINITIALIZED",
Josh Coalson8da98c82006-10-15 04:24:05 +0000452 "FLAC__STREAM_ENCODER_OGG_ERROR",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000453 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
454 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson6b21f662006-09-13 01:42:27 +0000455 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
456 "FLAC__STREAM_ENCODER_IO_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000457 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson6b21f662006-09-13 01:42:27 +0000458 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
459};
460
461FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
462 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
463 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
Josh Coalson8da98c82006-10-15 04:24:05 +0000464 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
Josh Coalson6b21f662006-09-13 01:42:27 +0000465 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
466 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
467 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
468 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
469 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
470 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
471 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
472 "FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH",
Josh Coalson6b21f662006-09-13 01:42:27 +0000473 "FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE",
474 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
475 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
476 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
477 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000478};
479
Josh Coalson8da98c82006-10-15 04:24:05 +0000480FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
481 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
482 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
483 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
484 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
485};
486
Josh Coalson6afed9f2002-10-16 22:29:47 +0000487FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000488 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
489 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000490};
491
Josh Coalson6b21f662006-09-13 01:42:27 +0000492FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
493 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
494 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
495 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
496};
497
498FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
499 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
500 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
501 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
502};
503
Josh Coalson0a15c142001-06-13 17:59:57 +0000504/***********************************************************************
505 *
506 * Class constructor/destructor
507 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000508 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000509FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000510{
Josh Coalson0a15c142001-06-13 17:59:57 +0000511 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000512 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000513
Josh Coalson0a15c142001-06-13 17:59:57 +0000514 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000515
Josh Coalsonea7155f2002-10-18 05:49:19 +0000516 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000517 if(encoder == 0) {
518 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000519 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000520
Josh Coalsonea7155f2002-10-18 05:49:19 +0000521 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000522 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000523 free(encoder);
524 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000525 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000526
Josh Coalsonea7155f2002-10-18 05:49:19 +0000527 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000528 if(encoder->private_ == 0) {
529 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000530 free(encoder);
531 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000532 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000533
Josh Coalsonaec256b2002-03-12 16:19:54 +0000534 encoder->private_->frame = FLAC__bitbuffer_new();
535 if(encoder->private_->frame == 0) {
536 free(encoder->private_);
537 free(encoder->protected_);
538 free(encoder);
539 return 0;
540 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000541
Josh Coalson6b21f662006-09-13 01:42:27 +0000542 encoder->private_->file = 0;
543
Josh Coalsonf1eff452002-07-31 07:05:33 +0000544 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000545
Josh Coalson3262b0d2002-08-14 20:58:42 +0000546 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000547
548 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
549 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
550 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
551 }
552 for(i = 0; i < 2; i++) {
553 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
554 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
555 }
556 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000557 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
558 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000559 }
560 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000561 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
562 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 +0000563 }
564
565 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000566 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
567 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000568 }
569 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000570 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
571 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 +0000572 }
573 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000574 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000575
Josh Coalsonfa697a92001-08-16 20:07:29 +0000576 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000577
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000578 return encoder;
579}
580
Josh Coalson6afed9f2002-10-16 22:29:47 +0000581FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000582{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000583 unsigned i;
584
Josh Coalsonf1eff452002-07-31 07:05:33 +0000585 FLAC__ASSERT(0 != encoder);
586 FLAC__ASSERT(0 != encoder->protected_);
587 FLAC__ASSERT(0 != encoder->private_);
588 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000589
Josh Coalson3262b0d2002-08-14 20:58:42 +0000590 encoder->private_->is_being_deleted = true;
591
592 FLAC__stream_encoder_finish(encoder);
593
Josh Coalson4fa90592002-12-04 07:01:37 +0000594 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000595 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000596
597 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000598 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
599 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000600 }
601 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000602 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
603 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 +0000604 }
605 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000606 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000607
Josh Coalsonaec256b2002-03-12 16:19:54 +0000608 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000609 free(encoder->private_);
610 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000611 free(encoder);
612}
613
Josh Coalson0a15c142001-06-13 17:59:57 +0000614/***********************************************************************
615 *
616 * Public class methods
617 *
618 ***********************************************************************/
619
Josh Coalson8da98c82006-10-15 04:24:05 +0000620static FLAC__StreamEncoderInitStatus init_stream_internal_(
621 FLAC__StreamEncoder *encoder,
622 FLAC__StreamEncoderReadCallback read_callback,
623 FLAC__StreamEncoderWriteCallback write_callback,
624 FLAC__StreamEncoderSeekCallback seek_callback,
625 FLAC__StreamEncoderTellCallback tell_callback,
626 FLAC__StreamEncoderMetadataCallback metadata_callback,
627 void *client_data,
628 FLAC__bool is_ogg
629)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000630{
631 unsigned i;
Josh Coalson3957c472006-09-24 16:25:42 +0000632 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000633
Josh Coalsonf1eff452002-07-31 07:05:33 +0000634 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000635
Josh Coalsonfa697a92001-08-16 20:07:29 +0000636 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000637 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000638
Josh Coalson8da98c82006-10-15 04:24:05 +0000639#ifndef FLAC__HAS_OGG
640 if(is_ogg)
641 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
642#endif
643
Josh Coalson6b21f662006-09-13 01:42:27 +0000644 if(0 == write_callback || (seek_callback && 0 == tell_callback))
645 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000646
Josh Coalsonfa697a92001-08-16 20:07:29 +0000647 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000648 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000649
Josh Coalsonfa697a92001-08-16 20:07:29 +0000650 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
Josh Coalson6b21f662006-09-13 01:42:27 +0000651 return FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000652
Josh Coalsonfa697a92001-08-16 20:07:29 +0000653 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
Josh Coalson6b21f662006-09-13 01:42:27 +0000654 return FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000655
Josh Coalsonfa697a92001-08-16 20:07:29 +0000656 if(encoder->protected_->bits_per_sample >= 32)
Josh Coalson6b21f662006-09-13 01:42:27 +0000657 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 +0000658
Josh Coalson76c68bc2002-05-17 06:22:02 +0000659 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 +0000660 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000661
Josh Coalson0833f342002-07-15 05:31:55 +0000662 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000663 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000664
Josh Coalsonfa697a92001-08-16 20:07:29 +0000665 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000666 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000667
Josh Coalson20ac2c12002-08-30 05:47:14 +0000668 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000669 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000670
Josh Coalsonfa697a92001-08-16 20:07:29 +0000671 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000672 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000673
Josh Coalsonfa697a92001-08-16 20:07:29 +0000674 if(encoder->protected_->qlp_coeff_precision == 0) {
675 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000676 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
677 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000678 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 +0000679 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000680 else if(encoder->protected_->bits_per_sample == 16) {
681 if(encoder->protected_->blocksize <= 192)
682 encoder->protected_->qlp_coeff_precision = 7;
683 else if(encoder->protected_->blocksize <= 384)
684 encoder->protected_->qlp_coeff_precision = 8;
685 else if(encoder->protected_->blocksize <= 576)
686 encoder->protected_->qlp_coeff_precision = 9;
687 else if(encoder->protected_->blocksize <= 1152)
688 encoder->protected_->qlp_coeff_precision = 10;
689 else if(encoder->protected_->blocksize <= 2304)
690 encoder->protected_->qlp_coeff_precision = 11;
691 else if(encoder->protected_->blocksize <= 4608)
692 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000693 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000694 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000695 }
696 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000697 if(encoder->protected_->blocksize <= 384)
698 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
699 else if(encoder->protected_->blocksize <= 1152)
700 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
701 else
702 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 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000705 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000706 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 +0000707 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000708
Josh Coalsonfa697a92001-08-16 20:07:29 +0000709 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000710 if(
711 encoder->protected_->blocksize != 192 &&
712 encoder->protected_->blocksize != 576 &&
713 encoder->protected_->blocksize != 1152 &&
714 encoder->protected_->blocksize != 2304 &&
715 encoder->protected_->blocksize != 4608 &&
716 encoder->protected_->blocksize != 256 &&
717 encoder->protected_->blocksize != 512 &&
718 encoder->protected_->blocksize != 1024 &&
719 encoder->protected_->blocksize != 2048 &&
720 encoder->protected_->blocksize != 4096 &&
721 encoder->protected_->blocksize != 8192 &&
722 encoder->protected_->blocksize != 16384
723 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000724 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000725 if(
726 encoder->protected_->sample_rate != 8000 &&
727 encoder->protected_->sample_rate != 16000 &&
728 encoder->protected_->sample_rate != 22050 &&
729 encoder->protected_->sample_rate != 24000 &&
730 encoder->protected_->sample_rate != 32000 &&
731 encoder->protected_->sample_rate != 44100 &&
732 encoder->protected_->sample_rate != 48000 &&
733 encoder->protected_->sample_rate != 96000
734 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000735 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000736 if(
737 encoder->protected_->bits_per_sample != 8 &&
738 encoder->protected_->bits_per_sample != 12 &&
739 encoder->protected_->bits_per_sample != 16 &&
740 encoder->protected_->bits_per_sample != 20 &&
741 encoder->protected_->bits_per_sample != 24
742 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000743 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000744 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000745 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsond0edb972006-10-07 06:50:08 +0000746 if(
747 encoder->protected_->sample_rate <= 48000 &&
748 (
749 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
750 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
751 )
752 ) {
753 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
754 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000755 }
756
Josh Coalsonfa697a92001-08-16 20:07:29 +0000757 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
758 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
759 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
760 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000761
Josh Coalson8da98c82006-10-15 04:24:05 +0000762#if FLAC__HAS_OGG
763 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
764 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
765 unsigned i;
766 for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
767 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
768 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
769 for( ; i > 0; i--)
770 encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
771 encoder->protected_->metadata[0] = vc;
772 break;
773 }
774 }
775 }
776#endif
777 /* keep track of any SEEKTABLE block */
778 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
779 unsigned i;
780 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
781 if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
782 encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
783 break; /* take only the first one */
784 }
785 }
786 }
787
Josh Coalson66075c12002-06-01 05:39:38 +0000788 /* validate metadata */
789 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000790 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000791 metadata_has_seektable = false;
792 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000793 metadata_picture_has_type1 = false;
794 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000795 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000796 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
797 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000798 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000799 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000800 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000801 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000802 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000803 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000804 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000805 }
Josh Coalson3957c472006-09-24 16:25:42 +0000806 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000807 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000808 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000809 metadata_has_vorbis_comment = true;
810 }
Josh Coalson3957c472006-09-24 16:25:42 +0000811 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
812 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 +0000813 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000814 }
Josh Coalson3957c472006-09-24 16:25:42 +0000815 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
816 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000817 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000818 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
819 if(metadata_picture_has_type1) /* there should only be 1 per stream */
820 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
821 metadata_picture_has_type1 = true;
822 /* standard icon must be 32x32 pixel PNG */
823 if(
824 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
825 (
826 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
827 m->data.picture.width != 32 ||
828 m->data.picture.height != 32
829 )
830 )
831 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
832 }
833 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
834 if(metadata_picture_has_type2) /* there should only be 1 per stream */
835 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
836 metadata_picture_has_type2 = true;
837 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000838 }
Josh Coalson66075c12002-06-01 05:39:38 +0000839 }
840
Josh Coalsonfa697a92001-08-16 20:07:29 +0000841 encoder->private_->input_capacity = 0;
842 for(i = 0; i < encoder->protected_->channels; i++) {
843 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000844#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000845 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000846#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000847 }
848 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000849 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000850#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000851 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000852#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000853 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000854#ifndef FLAC__INTEGER_ONLY_LIBRARY
855 for(i = 0; i < encoder->protected_->num_apodizations; i++)
856 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
857 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
858#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000859 for(i = 0; i < encoder->protected_->channels; i++) {
860 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
861 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
862 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000863 }
864 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000865 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
866 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
867 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000868 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000869 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
870 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
871 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000872#ifndef FLAC__INTEGER_ONLY_LIBRARY
873 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
874#else
875 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
876 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
877 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
878 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
879 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
880 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
881 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);
882#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000883 if(encoder->private_->loose_mid_side_stereo_frames == 0)
884 encoder->private_->loose_mid_side_stereo_frames = 1;
885 encoder->private_->loose_mid_side_stereo_frame_count = 0;
886 encoder->private_->current_sample_number = 0;
887 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000888
Josh Coalsonfa697a92001-08-16 20:07:29 +0000889 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
890 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? */
891 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000892
Josh Coalsoncf30f502001-05-23 20:57:44 +0000893 /*
894 * get the CPU info and set the function pointers
895 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000896 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000897 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000898#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000899 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000900#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000901 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000902#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000903 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000904 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 +0000905 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000906#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000907 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000908#ifndef FLAC__INTEGER_ONLY_LIBRARY
909# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000910 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000911# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000912 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000913# ifdef FLAC__HAS_NASM
914# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000915 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000916 if(encoder->protected_->max_lpc_order < 4)
917 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
918 else if(encoder->protected_->max_lpc_order < 8)
919 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
920 else if(encoder->protected_->max_lpc_order < 12)
921 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000922 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000923 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000924 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000925 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000926# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000927 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000928 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000929 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000930 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000931 if(encoder->private_->cpuinfo.data.ia32.mmx) {
932 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
933 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 +0000934 }
935 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000936 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
937 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 +0000938 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000939 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
940 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
941# endif /* FLAC__HAS_NASM */
942# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000943 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000944# endif /* !FLAC__NO_ASM */
945#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000946 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000947 if(encoder->private_->use_wide_by_block) {
948 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000949 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000950
Josh Coalson8395d022001-07-12 21:25:22 +0000951 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000952 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 +0000953
Josh Coalson6b21f662006-09-13 01:42:27 +0000954 /* set state to OK; from here on, errors are fatal and we'll override the state then */
955 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
956
Josh Coalson8da98c82006-10-15 04:24:05 +0000957#if FLAC__HAS_OGG
958 encoder->private_->is_ogg = is_ogg;
959 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
960 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
961 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
962 }
963#endif
964
965 encoder->private_->read_callback = read_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000966 encoder->private_->write_callback = write_callback;
967 encoder->private_->seek_callback = seek_callback;
968 encoder->private_->tell_callback = tell_callback;
969 encoder->private_->metadata_callback = metadata_callback;
970 encoder->private_->client_data = client_data;
971
Josh Coalsonf1eff452002-07-31 07:05:33 +0000972 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000973 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +0000974 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000975 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000976
Josh Coalson6b21f662006-09-13 01:42:27 +0000977 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
978 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
979 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
980 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000981
982 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000983 * Set up the verify stuff if necessary
984 */
985 if(encoder->protected_->verify) {
986 /*
987 * First, set up the fifo which will hold the
988 * original signal to compare against
989 */
990 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
991 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalson6b21f662006-09-13 01:42:27 +0000992 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
993 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
994 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
995 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000996 }
997 encoder->private_->verify.input_fifo.tail = 0;
998
999 /*
1000 * Now set up a stream decoder for verification
1001 */
1002 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
Josh Coalson6b21f662006-09-13 01:42:27 +00001003 if(0 == encoder->private_->verify.decoder) {
1004 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1005 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1006 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001007
Josh Coalson6b21f662006-09-13 01:42:27 +00001008 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) {
1009 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1010 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1011 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001012 }
Josh Coalson589f8c72002-08-07 23:54:55 +00001013 encoder->private_->verify.error_stats.absolute_sample = 0;
1014 encoder->private_->verify.error_stats.frame_number = 0;
1015 encoder->private_->verify.error_stats.channel = 0;
1016 encoder->private_->verify.error_stats.sample = 0;
1017 encoder->private_->verify.error_stats.expected = 0;
1018 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001019
1020 /*
Josh Coalson6b21f662006-09-13 01:42:27 +00001021 * These must be done before we write any metadata, because that
1022 * calls the write_callback, which uses these values.
1023 */
1024 encoder->private_->first_seekpoint_to_check = 0;
1025 encoder->private_->samples_written = 0;
1026 encoder->protected_->streaminfo_offset = 0;
1027 encoder->protected_->seektable_offset = 0;
1028 encoder->protected_->audio_offset = 0;
1029
1030 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001031 * write the stream header
1032 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001033 if(encoder->protected_->verify)
1034 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson6b21f662006-09-13 01:42:27 +00001035 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
1036 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1037 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1038 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001039 if(!write_bitbuffer_(encoder, 0)) {
1040 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001041 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001042 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001043
Josh Coalson5c491a12002-08-01 06:39:40 +00001044 /*
1045 * write the STREAMINFO metadata block
1046 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001047 if(encoder->protected_->verify)
1048 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +00001049 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1050 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1051 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1052 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1053 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1054 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1055 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1056 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1057 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1058 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1059 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1060 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 +00001061 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +00001062 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1063 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1064 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1065 }
1066 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1067 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1068 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1069 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001070 if(!write_bitbuffer_(encoder, 0)) {
1071 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001072 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001073 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001074
Josh Coalson5c491a12002-08-01 06:39:40 +00001075 /*
1076 * Now that the STREAMINFO block is written, we can init this to an
1077 * absurdly-high value...
1078 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001079 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 +00001080 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001081 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001082
Josh Coalson5c491a12002-08-01 06:39:40 +00001083 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001084 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1085 * if not, we will write an empty one (FLAC__add_metadata_block()
1086 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001087 *
Josh Coalson8da98c82006-10-15 04:24:05 +00001088 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1089 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1090 * true it will have already insured that the metadata list is properly
1091 * ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001092 */
1093 if(!metadata_has_vorbis_comment) {
1094 FLAC__StreamMetadata vorbis_comment;
1095 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1096 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1097 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1098 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1099 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1100 vorbis_comment.data.vorbis_comment.num_comments = 0;
1101 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001102 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1103 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1104 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1105 }
1106 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1107 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1108 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1109 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001110 if(!write_bitbuffer_(encoder, 0)) {
1111 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001112 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001113 }
1114 }
1115
1116 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001117 * write the user's metadata blocks
1118 */
1119 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1120 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001121 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1122 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1123 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1124 }
1125 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1126 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1127 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1128 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001129 if(!write_bitbuffer_(encoder, 0)) {
1130 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001131 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001132 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001133 }
1134
Josh Coalson6b21f662006-09-13 01:42:27 +00001135 /* now that all the metadata is written, we save the stream offset */
1136 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 */
1137 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1138 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1139 }
1140
Josh Coalsond86e03b2002-08-03 21:56:15 +00001141 if(encoder->protected_->verify)
1142 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1143
Josh Coalson6b21f662006-09-13 01:42:27 +00001144 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1145}
1146
Josh Coalson8da98c82006-10-15 04:24:05 +00001147FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1148 FLAC__StreamEncoder *encoder,
1149 FLAC__StreamEncoderWriteCallback write_callback,
1150 FLAC__StreamEncoderSeekCallback seek_callback,
1151 FLAC__StreamEncoderTellCallback tell_callback,
1152 FLAC__StreamEncoderMetadataCallback metadata_callback,
1153 void *client_data
1154)
1155{
1156 return init_stream_internal_(
1157 encoder,
1158 /*read_callback=*/0,
1159 write_callback,
1160 seek_callback,
1161 tell_callback,
1162 metadata_callback,
1163 client_data,
1164 /*is_ogg=*/false
1165 );
1166}
1167
1168FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1169 FLAC__StreamEncoder *encoder,
1170 FLAC__StreamEncoderReadCallback read_callback,
1171 FLAC__StreamEncoderWriteCallback write_callback,
1172 FLAC__StreamEncoderSeekCallback seek_callback,
1173 FLAC__StreamEncoderTellCallback tell_callback,
1174 FLAC__StreamEncoderMetadataCallback metadata_callback,
1175 void *client_data
1176)
1177{
1178 return init_stream_internal_(
1179 encoder,
1180 read_callback,
1181 write_callback,
1182 seek_callback,
1183 tell_callback,
1184 metadata_callback,
1185 client_data,
1186 /*is_ogg=*/true
1187 );
1188}
1189
1190static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1191 FLAC__StreamEncoder *encoder,
1192 FILE *file,
1193 FLAC__StreamEncoderProgressCallback progress_callback,
1194 void *client_data,
1195 FLAC__bool is_ogg
1196)
Josh Coalson6b21f662006-09-13 01:42:27 +00001197{
1198 FLAC__StreamEncoderInitStatus init_status;
1199
1200 FLAC__ASSERT(0 != encoder);
1201 FLAC__ASSERT(0 != file);
1202
Josh Coalson6b21f662006-09-13 01:42:27 +00001203 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1204 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1205
1206 /* double protection */
1207 if(file == 0) {
1208 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1209 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1210 }
1211
Josh Coalson92f7fa92006-10-09 05:34:21 +00001212 /*
1213 * To make sure that our file does not go unclosed after an error, we
1214 * must assign the FILE pointer before any further error can occur in
1215 * this routine.
1216 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001217 if(file == stdout)
1218 file = get_binary_stdout_(); /* just to be safe */
1219
1220 encoder->private_->file = file;
1221
1222 encoder->private_->progress_callback = progress_callback;
1223 encoder->private_->bytes_written = 0;
1224 encoder->private_->samples_written = 0;
1225 encoder->private_->frames_written = 0;
1226
Josh Coalson8da98c82006-10-15 04:24:05 +00001227 init_status = init_stream_internal_(
1228 encoder,
1229 is_ogg? file_read_callback_ : 0,
1230 file_write_callback_,
1231 file_seek_callback_,
1232 file_tell_callback_,
1233 /*metadata_callback=*/0,
1234 client_data,
1235 is_ogg
1236 );
Josh Coalson6b21f662006-09-13 01:42:27 +00001237 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1238 /* the above function sets the state for us in case of an error */
1239 return init_status;
1240 }
1241
1242 {
1243 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1244
1245 FLAC__ASSERT(blocksize != 0);
1246 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1247 }
1248
1249 return init_status;
1250}
Josh Coalson8da98c82006-10-15 04:24:05 +00001251
1252FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1253 FLAC__StreamEncoder *encoder,
1254 FILE *file,
1255 FLAC__StreamEncoderProgressCallback progress_callback,
1256 void *client_data
1257)
1258{
1259 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1260}
1261
1262FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1263 FLAC__StreamEncoder *encoder,
1264 FILE *file,
1265 FLAC__StreamEncoderProgressCallback progress_callback,
1266 void *client_data
1267)
1268{
1269 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1270}
Josh Coalson6b21f662006-09-13 01:42:27 +00001271
Josh Coalson8da98c82006-10-15 04:24:05 +00001272static FLAC__StreamEncoderInitStatus init_file_internal_(
1273 FLAC__StreamEncoder *encoder,
1274 const char *filename,
1275 FLAC__StreamEncoderProgressCallback progress_callback,
1276 void *client_data,
1277 FLAC__bool is_ogg
1278)
Josh Coalson6b21f662006-09-13 01:42:27 +00001279{
1280 FILE *file;
1281
1282 FLAC__ASSERT(0 != encoder);
1283
1284 /*
1285 * To make sure that our file does not go unclosed after an error, we
1286 * have to do the same entrance checks here that are later performed
1287 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1288 */
1289 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1290 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1291
1292 file = filename? fopen(filename, "w+b") : stdout;
1293
1294 if(file == 0) {
1295 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1296 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1297 }
1298
Josh Coalson8da98c82006-10-15 04:24:05 +00001299 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1300}
1301
1302FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1303 FLAC__StreamEncoder *encoder,
1304 const char *filename,
1305 FLAC__StreamEncoderProgressCallback progress_callback,
1306 void *client_data
1307)
1308{
1309 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1310}
1311
1312FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1313 FLAC__StreamEncoder *encoder,
1314 const char *filename,
1315 FLAC__StreamEncoderProgressCallback progress_callback,
1316 void *client_data
1317)
1318{
1319 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001320}
1321
Josh Coalson6afed9f2002-10-16 22:29:47 +00001322FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001323{
Josh Coalsonf1eff452002-07-31 07:05:33 +00001324 FLAC__ASSERT(0 != encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001325 FLAC__ASSERT(0 != encoder->private_);
1326 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001327
Josh Coalsonfa697a92001-08-16 20:07:29 +00001328 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001329 return;
Josh Coalson2b245f22002-08-07 17:10:50 +00001330
Josh Coalson3262b0d2002-08-14 20:58:42 +00001331 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001332 if(encoder->private_->current_sample_number != 0) {
1333 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1334 process_frame_(encoder, true); /* true => is last frame */
1335 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001336 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001337
Josh Coalson6b21f662006-09-13 01:42:27 +00001338 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001339
Josh Coalson3262b0d2002-08-14 20:58:42 +00001340 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson8da98c82006-10-15 04:24:05 +00001341 if(encoder->private_->seek_callback) {
1342#if FLAC__HAS_OGG
1343 if(encoder->private_->is_ogg)
1344 update_ogg_metadata_(encoder);
1345 else
1346#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00001347 update_metadata_(encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001348 }
Josh Coalson6b21f662006-09-13 01:42:27 +00001349 if(encoder->private_->metadata_callback)
1350 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
Josh Coalson2b245f22002-08-07 17:10:50 +00001351 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001352
Josh Coalsond86e03b2002-08-03 21:56:15 +00001353 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
1354 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1355
Josh Coalson6b21f662006-09-13 01:42:27 +00001356 if(0 != encoder->private_->file) {
1357 if(encoder->private_->file != stdout)
1358 fclose(encoder->private_->file);
1359 encoder->private_->file = 0;
1360 }
1361
Josh Coalson8da98c82006-10-15 04:24:05 +00001362#if FLAC__HAS_OGG
1363 if(encoder->private_->is_ogg)
1364 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1365#endif
1366
Josh Coalsonf1eff452002-07-31 07:05:33 +00001367 free_(encoder);
1368 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001369
Josh Coalsonfa697a92001-08-16 20:07:29 +00001370 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001371}
1372
Josh Coalson71d5c252006-10-15 06:04:55 +00001373FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
Josh Coalson8da98c82006-10-15 04:24:05 +00001374{
1375 FLAC__ASSERT(0 != encoder);
1376 FLAC__ASSERT(0 != encoder->private_);
1377 FLAC__ASSERT(0 != encoder->protected_);
1378 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1379 return false;
1380#ifdef FLAC__HAS_OGG
1381 /* can't check encoder->private_->is_ogg since that's not set until init time */
1382 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1383 return true;
1384#else
1385 (void)value;
1386 return false;
1387#endif
1388}
1389
Josh Coalson6afed9f2002-10-16 22:29:47 +00001390FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001391{
1392 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001393 FLAC__ASSERT(0 != encoder->private_);
1394 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001395 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1396 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001397#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001398 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001399#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001400 return true;
1401}
1402
Josh Coalson6afed9f2002-10-16 22:29:47 +00001403FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001404{
Josh Coalson92031602002-07-24 06:02:11 +00001405 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001406 FLAC__ASSERT(0 != encoder->private_);
1407 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001408 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001409 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001410 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001411 return true;
1412}
1413
Josh Coalson6afed9f2002-10-16 22:29:47 +00001414FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001415{
Josh Coalson92031602002-07-24 06:02:11 +00001416 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001417 FLAC__ASSERT(0 != encoder->private_);
1418 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001419 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001420 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001421 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001422 return true;
1423}
1424
Josh Coalson6afed9f2002-10-16 22:29:47 +00001425FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001426{
Josh Coalson92031602002-07-24 06:02:11 +00001427 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001428 FLAC__ASSERT(0 != encoder->private_);
1429 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001430 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001431 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001432 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001433 return true;
1434}
1435
Josh Coalson6afed9f2002-10-16 22:29:47 +00001436FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001437{
Josh Coalson92031602002-07-24 06:02:11 +00001438 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001439 FLAC__ASSERT(0 != encoder->private_);
1440 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001441 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001442 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001443 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001444 return true;
1445}
1446
Josh Coalson6afed9f2002-10-16 22:29:47 +00001447FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001448{
Josh Coalson92031602002-07-24 06:02:11 +00001449 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001450 FLAC__ASSERT(0 != encoder->private_);
1451 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001452 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001453 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001454 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001455 return true;
1456}
1457
Josh Coalson6afed9f2002-10-16 22:29:47 +00001458FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001459{
Josh Coalson92031602002-07-24 06:02:11 +00001460 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001461 FLAC__ASSERT(0 != encoder->private_);
1462 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001463 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001464 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001465 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001466 return true;
1467}
1468
Josh Coalson6afed9f2002-10-16 22:29:47 +00001469FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001470{
Josh Coalson92031602002-07-24 06:02:11 +00001471 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001472 FLAC__ASSERT(0 != encoder->private_);
1473 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001474 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001475 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001476 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001477 return true;
1478}
1479
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001480FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1481{
1482 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001483 FLAC__ASSERT(0 != encoder->private_);
1484 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001485 FLAC__ASSERT(0 != specification);
1486 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1487 return false;
1488#ifdef FLAC__INTEGER_ONLY_LIBRARY
1489 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1490#else
1491 encoder->protected_->num_apodizations = 0;
1492 while(1) {
1493 const char *s = strchr(specification, ';');
1494 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1495 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1496 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1497 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1498 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1499 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1500 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1501 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1502 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1503 else if(n==6 && 0 == strncmp("connes" , specification, n))
1504 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1505 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1506 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1507 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1508 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1509 if (stddev > 0.0 && stddev <= 0.5) {
1510 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1511 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1512 }
1513 }
1514 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1515 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1516 else if(n==4 && 0 == strncmp("hann" , specification, n))
1517 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1518 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1519 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1520 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1521 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1522 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1523 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1524 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1525 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1526 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1527 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1528 if (p >= 0.0 && p <= 1.0) {
1529 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1530 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1531 }
1532 }
1533 else if(n==5 && 0 == strncmp("welch" , specification, n))
1534 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1535 if (encoder->protected_->num_apodizations == 32)
1536 break;
1537 if (s)
1538 specification = s+1;
1539 else
1540 break;
1541 }
1542 if(encoder->protected_->num_apodizations == 0) {
1543 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001544 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1545 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001546 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001547#endif
1548 return true;
1549}
1550
Josh Coalson6afed9f2002-10-16 22:29:47 +00001551FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001552{
Josh Coalson92031602002-07-24 06:02:11 +00001553 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001554 FLAC__ASSERT(0 != encoder->private_);
1555 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001556 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001557 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001558 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001559 return true;
1560}
1561
Josh Coalson6afed9f2002-10-16 22:29:47 +00001562FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001563{
Josh Coalson92031602002-07-24 06:02:11 +00001564 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001565 FLAC__ASSERT(0 != encoder->private_);
1566 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001567 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001568 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001569 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001570 return true;
1571}
1572
Josh Coalson6afed9f2002-10-16 22:29:47 +00001573FLAC_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 +00001574{
Josh Coalson92031602002-07-24 06:02:11 +00001575 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001576 FLAC__ASSERT(0 != encoder->private_);
1577 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001578 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001579 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001580 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001581 return true;
1582}
1583
Josh Coalson6afed9f2002-10-16 22:29:47 +00001584FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001585{
Josh Coalson92031602002-07-24 06:02:11 +00001586 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001587 FLAC__ASSERT(0 != encoder->private_);
1588 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001589 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001590 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001591#if 0
1592 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001593 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001594#else
1595 (void)value;
1596#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001597 return true;
1598}
1599
Josh Coalson6afed9f2002-10-16 22:29:47 +00001600FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001601{
Josh Coalson92031602002-07-24 06:02:11 +00001602 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001603 FLAC__ASSERT(0 != encoder->private_);
1604 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001605 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001606 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001607 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001608 return true;
1609}
1610
Josh Coalson6afed9f2002-10-16 22:29:47 +00001611FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001612{
Josh Coalson92031602002-07-24 06:02:11 +00001613 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001614 FLAC__ASSERT(0 != encoder->private_);
1615 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001616 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001617 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001618 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001619 return true;
1620}
1621
Josh Coalson6afed9f2002-10-16 22:29:47 +00001622FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001623{
Josh Coalson92031602002-07-24 06:02:11 +00001624 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001625 FLAC__ASSERT(0 != encoder->private_);
1626 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001627 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001628 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001629 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001630 return true;
1631}
1632
Josh Coalson6afed9f2002-10-16 22:29:47 +00001633FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001634{
Josh Coalson92031602002-07-24 06:02:11 +00001635 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001636 FLAC__ASSERT(0 != encoder->private_);
1637 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001638 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001639 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001640#if 0
1641 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001642 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001643#else
1644 (void)value;
1645#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001646 return true;
1647}
1648
Josh Coalson6afed9f2002-10-16 22:29:47 +00001649FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001650{
Josh Coalson92031602002-07-24 06:02:11 +00001651 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001652 FLAC__ASSERT(0 != encoder->private_);
1653 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001654 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001655 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001656 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001657 return true;
1658}
1659
Josh Coalson6afed9f2002-10-16 22:29:47 +00001660FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001661{
Josh Coalson92031602002-07-24 06:02:11 +00001662 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001663 FLAC__ASSERT(0 != encoder->private_);
1664 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001665 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001666 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001667 encoder->protected_->metadata = metadata;
1668 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson8da98c82006-10-15 04:24:05 +00001669#if FLAC__HAS_OGG
1670 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1671 return false;
1672#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001673 return true;
1674}
1675
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001676/*
1677 * These three functions are not static, but not publically exposed in
1678 * include/FLAC/ either. They are used by the test suite.
1679 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001680FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001681{
1682 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001683 FLAC__ASSERT(0 != encoder->private_);
1684 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001685 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1686 return false;
1687 encoder->private_->disable_constant_subframes = value;
1688 return true;
1689}
1690
Josh Coalson6afed9f2002-10-16 22:29:47 +00001691FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001692{
1693 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001694 FLAC__ASSERT(0 != encoder->private_);
1695 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001696 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1697 return false;
1698 encoder->private_->disable_fixed_subframes = value;
1699 return true;
1700}
1701
Josh Coalson6afed9f2002-10-16 22:29:47 +00001702FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001703{
1704 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001705 FLAC__ASSERT(0 != encoder->private_);
1706 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001707 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1708 return false;
1709 encoder->private_->disable_verbatim_subframes = value;
1710 return true;
1711}
1712
Josh Coalson6afed9f2002-10-16 22:29:47 +00001713FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001714{
Josh Coalson92031602002-07-24 06:02:11 +00001715 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001716 FLAC__ASSERT(0 != encoder->private_);
1717 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001718 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001719}
1720
Josh Coalson6afed9f2002-10-16 22:29:47 +00001721FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001722{
1723 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001724 FLAC__ASSERT(0 != encoder->private_);
1725 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001726 if(encoder->protected_->verify)
1727 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1728 else
1729 return FLAC__STREAM_DECODER_UNINITIALIZED;
1730}
1731
Josh Coalson02954222002-11-08 06:16:31 +00001732FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1733{
Josh Coalson8da98c82006-10-15 04:24:05 +00001734 FLAC__ASSERT(0 != encoder);
1735 FLAC__ASSERT(0 != encoder->private_);
1736 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson02954222002-11-08 06:16:31 +00001737 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1738 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1739 else
Josh Coalson807140d2003-09-24 22:10:51 +00001740 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001741}
1742
Josh Coalson6afed9f2002-10-16 22:29:47 +00001743FLAC_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 +00001744{
1745 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001746 FLAC__ASSERT(0 != encoder->private_);
1747 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson589f8c72002-08-07 23:54:55 +00001748 if(0 != absolute_sample)
1749 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1750 if(0 != frame_number)
1751 *frame_number = encoder->private_->verify.error_stats.frame_number;
1752 if(0 != channel)
1753 *channel = encoder->private_->verify.error_stats.channel;
1754 if(0 != sample)
1755 *sample = encoder->private_->verify.error_stats.sample;
1756 if(0 != expected)
1757 *expected = encoder->private_->verify.error_stats.expected;
1758 if(0 != got)
1759 *got = encoder->private_->verify.error_stats.got;
1760}
1761
Josh Coalson6afed9f2002-10-16 22:29:47 +00001762FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001763{
1764 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001765 FLAC__ASSERT(0 != encoder->private_);
1766 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001767 return encoder->protected_->verify;
1768}
1769
Josh Coalson6afed9f2002-10-16 22:29:47 +00001770FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001771{
Josh Coalson92031602002-07-24 06:02:11 +00001772 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001773 FLAC__ASSERT(0 != encoder->private_);
1774 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001775 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001776}
1777
Josh Coalson6afed9f2002-10-16 22:29:47 +00001778FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001779{
Josh Coalson92031602002-07-24 06:02:11 +00001780 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001781 FLAC__ASSERT(0 != encoder->private_);
1782 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001783 return encoder->protected_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001784}
1785
Josh Coalson6afed9f2002-10-16 22:29:47 +00001786FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001787{
Josh Coalson92031602002-07-24 06:02:11 +00001788 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001789 FLAC__ASSERT(0 != encoder->private_);
1790 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001791 return encoder->protected_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001792}
1793
Josh Coalson6afed9f2002-10-16 22:29:47 +00001794FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001795{
Josh Coalson92031602002-07-24 06:02:11 +00001796 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001797 FLAC__ASSERT(0 != encoder->private_);
1798 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001799 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001800}
1801
Josh Coalson6afed9f2002-10-16 22:29:47 +00001802FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001803{
Josh Coalson92031602002-07-24 06:02:11 +00001804 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001805 FLAC__ASSERT(0 != encoder->private_);
1806 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001807 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001808}
1809
Josh Coalson6afed9f2002-10-16 22:29:47 +00001810FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001811{
Josh Coalson92031602002-07-24 06:02:11 +00001812 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001813 FLAC__ASSERT(0 != encoder->private_);
1814 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001815 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001816}
1817
Josh Coalson6afed9f2002-10-16 22:29:47 +00001818FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001819{
Josh Coalson92031602002-07-24 06:02:11 +00001820 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001821 FLAC__ASSERT(0 != encoder->private_);
1822 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001823 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001824}
1825
Josh Coalson6afed9f2002-10-16 22:29:47 +00001826FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001827{
Josh Coalson92031602002-07-24 06:02:11 +00001828 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001829 FLAC__ASSERT(0 != encoder->private_);
1830 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001831 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001832}
1833
Josh Coalson6afed9f2002-10-16 22:29:47 +00001834FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001835{
Josh Coalson92031602002-07-24 06:02:11 +00001836 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001837 FLAC__ASSERT(0 != encoder->private_);
1838 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001839 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001840}
1841
Josh Coalson6afed9f2002-10-16 22:29:47 +00001842FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001843{
Josh Coalson92031602002-07-24 06:02:11 +00001844 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001845 FLAC__ASSERT(0 != encoder->private_);
1846 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001847 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001848}
1849
Josh Coalson6afed9f2002-10-16 22:29:47 +00001850FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001851{
Josh Coalson92031602002-07-24 06:02:11 +00001852 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001853 FLAC__ASSERT(0 != encoder->private_);
1854 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001855 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001856}
1857
Josh Coalson6afed9f2002-10-16 22:29:47 +00001858FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001859{
Josh Coalson92031602002-07-24 06:02:11 +00001860 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001861 FLAC__ASSERT(0 != encoder->private_);
1862 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001863 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001864}
1865
Josh Coalson6afed9f2002-10-16 22:29:47 +00001866FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001867{
Josh Coalson92031602002-07-24 06:02:11 +00001868 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001869 FLAC__ASSERT(0 != encoder->private_);
1870 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001871 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001872}
1873
Josh Coalson6afed9f2002-10-16 22:29:47 +00001874FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001875{
Josh Coalson92031602002-07-24 06:02:11 +00001876 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001877 FLAC__ASSERT(0 != encoder->private_);
1878 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001879 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001880}
1881
Josh Coalson6afed9f2002-10-16 22:29:47 +00001882FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001883{
Josh Coalson92031602002-07-24 06:02:11 +00001884 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001885 FLAC__ASSERT(0 != encoder->private_);
1886 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001887 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001888}
1889
Josh Coalson6afed9f2002-10-16 22:29:47 +00001890FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001891{
1892 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001893 FLAC__ASSERT(0 != encoder->private_);
1894 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001895 return encoder->protected_->total_samples_estimate;
1896}
1897
Josh Coalson6afed9f2002-10-16 22:29:47 +00001898FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001899{
1900 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001901 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001902 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001903
Josh Coalsonf1eff452002-07-31 07:05:33 +00001904 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001905 FLAC__ASSERT(0 != encoder->private_);
1906 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001907 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001908
1909 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001910 /*
1911 * we have several flavors of the same basic loop, optimized for
1912 * different conditions:
1913 */
1914 if(encoder->protected_->max_lpc_order > 0) {
1915 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1916 /*
1917 * stereo coding: unroll channel loop
1918 * with LPC: calculate floating point version of signal
1919 */
1920 do {
1921 if(encoder->protected_->verify)
1922 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 +00001923
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001924 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1925 x = mid = side = buffer[0][j];
1926 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001927#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001928 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001929#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001930 x = buffer[1][j];
1931 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001932#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001933 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001934#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001935 mid += x;
1936 side -= x;
1937 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1938 encoder->private_->integer_signal_mid_side[1][i] = side;
1939 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001940#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001941 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1942 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001943#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001944 encoder->private_->current_sample_number++;
1945 }
1946 if(i == blocksize) {
1947 if(!process_frame_(encoder, false)) /* false => not last frame */
1948 return false;
1949 }
1950 } while(j < samples);
1951 }
1952 else {
1953 /*
1954 * independent channel coding: buffer each channel in inner loop
1955 * with LPC: calculate floating point version of signal
1956 */
1957 do {
1958 if(encoder->protected_->verify)
1959 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1960
1961 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1962 for(channel = 0; channel < channels; channel++) {
1963 x = buffer[channel][j];
1964 encoder->private_->integer_signal[channel][i] = x;
1965#ifndef FLAC__INTEGER_ONLY_LIBRARY
1966 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1967#endif
1968 }
1969 encoder->private_->current_sample_number++;
1970 }
1971 if(i == blocksize) {
1972 if(!process_frame_(encoder, false)) /* false => not last frame */
1973 return false;
1974 }
1975 } while(j < samples);
1976 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001977 }
1978 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001979 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1980 /*
1981 * stereo coding: unroll channel loop
1982 * without LPC: no need to calculate floating point version of signal
1983 */
1984 do {
1985 if(encoder->protected_->verify)
1986 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 +00001987
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001988 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1989 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1990 x = buffer[1][j];
1991 encoder->private_->integer_signal[1][i] = x;
1992 mid += x;
1993 side -= x;
1994 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1995 encoder->private_->integer_signal_mid_side[1][i] = side;
1996 encoder->private_->integer_signal_mid_side[0][i] = mid;
1997 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001998 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001999 if(i == blocksize) {
2000 if(!process_frame_(encoder, false)) /* false => not last frame */
2001 return false;
2002 }
2003 } while(j < samples);
2004 }
2005 else {
2006 /*
2007 * independent channel coding: buffer each channel in inner loop
2008 * without LPC: no need to calculate floating point version of signal
2009 */
2010 do {
2011 if(encoder->protected_->verify)
2012 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2013
2014 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2015 for(channel = 0; channel < channels; channel++)
2016 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
2017 encoder->private_->current_sample_number++;
2018 }
2019 if(i == blocksize) {
2020 if(!process_frame_(encoder, false)) /* false => not last frame */
2021 return false;
2022 }
2023 } while(j < samples);
2024 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002025 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002026
2027 return true;
2028}
2029
Josh Coalson6afed9f2002-10-16 22:29:47 +00002030FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002031{
2032 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00002033 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002034 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002035
Josh Coalsonf1eff452002-07-31 07:05:33 +00002036 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002037 FLAC__ASSERT(0 != encoder->private_);
2038 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002039 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002040
2041 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002042 /*
2043 * we have several flavors of the same basic loop, optimized for
2044 * different conditions:
2045 */
2046 if(encoder->protected_->max_lpc_order > 0) {
2047 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2048 /*
2049 * stereo coding: unroll channel loop
2050 * with LPC: calculate floating point version of signal
2051 */
2052 do {
2053 if(encoder->protected_->verify)
2054 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 +00002055
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002056 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2057 x = mid = side = buffer[k++];
2058 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002059#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002060 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002061#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002062 x = buffer[k++];
2063 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002064#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002065 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002066#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002067 mid += x;
2068 side -= x;
2069 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2070 encoder->private_->integer_signal_mid_side[1][i] = side;
2071 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002072#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002073 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
2074 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002075#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002076 encoder->private_->current_sample_number++;
2077 }
2078 if(i == blocksize) {
2079 if(!process_frame_(encoder, false)) /* false => not last frame */
2080 return false;
2081 }
2082 } while(j < samples);
2083 }
2084 else {
2085 /*
2086 * independent channel coding: buffer each channel in inner loop
2087 * with LPC: calculate floating point version of signal
2088 */
2089 do {
2090 if(encoder->protected_->verify)
2091 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2092
2093 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2094 for(channel = 0; channel < channels; channel++) {
2095 x = buffer[k++];
2096 encoder->private_->integer_signal[channel][i] = x;
2097#ifndef FLAC__INTEGER_ONLY_LIBRARY
2098 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
2099#endif
2100 }
2101 encoder->private_->current_sample_number++;
2102 }
2103 if(i == blocksize) {
2104 if(!process_frame_(encoder, false)) /* false => not last frame */
2105 return false;
2106 }
2107 } while(j < samples);
2108 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002109 }
2110 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002111 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2112 /*
2113 * stereo coding: unroll channel loop
2114 * without LPC: no need to calculate floating point version of signal
2115 */
2116 do {
2117 if(encoder->protected_->verify)
2118 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 +00002119
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002120 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2121 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00002122 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002123 encoder->private_->integer_signal[1][i] = x;
2124 mid += x;
2125 side -= x;
2126 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2127 encoder->private_->integer_signal_mid_side[1][i] = side;
2128 encoder->private_->integer_signal_mid_side[0][i] = mid;
2129 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00002130 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002131 if(i == blocksize) {
2132 if(!process_frame_(encoder, false)) /* false => not last frame */
2133 return false;
2134 }
2135 } while(j < samples);
2136 }
2137 else {
2138 /*
2139 * independent channel coding: buffer each channel in inner loop
2140 * without LPC: no need to calculate floating point version of signal
2141 */
2142 do {
2143 if(encoder->protected_->verify)
2144 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
2145
2146 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
2147 for(channel = 0; channel < channels; channel++)
2148 encoder->private_->integer_signal[channel][i] = buffer[k++];
2149 encoder->private_->current_sample_number++;
2150 }
2151 if(i == blocksize) {
2152 if(!process_frame_(encoder, false)) /* false => not last frame */
2153 return false;
2154 }
2155 } while(j < samples);
2156 }
Josh Coalsonaa255362001-05-31 06:17:41 +00002157 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002158
2159 return true;
2160}
2161
Josh Coalsonf1eff452002-07-31 07:05:33 +00002162/***********************************************************************
2163 *
2164 * Private class methods
2165 *
2166 ***********************************************************************/
2167
2168void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00002169{
2170 FLAC__ASSERT(0 != encoder);
2171
Josh Coalson47c7b142005-01-29 06:08:58 +00002172#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2173 encoder->protected_->verify = true;
2174#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00002175 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00002176#endif
Josh Coalson92031602002-07-24 06:02:11 +00002177 encoder->protected_->streamable_subset = true;
2178 encoder->protected_->do_mid_side_stereo = false;
2179 encoder->protected_->loose_mid_side_stereo = false;
2180 encoder->protected_->channels = 2;
2181 encoder->protected_->bits_per_sample = 16;
2182 encoder->protected_->sample_rate = 44100;
2183 encoder->protected_->blocksize = 1152;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002184#ifndef FLAC__INTEGER_ONLY_LIBRARY
2185 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00002186 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2187 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002188#endif
Josh Coalson92031602002-07-24 06:02:11 +00002189 encoder->protected_->max_lpc_order = 0;
2190 encoder->protected_->qlp_coeff_precision = 0;
2191 encoder->protected_->do_qlp_coeff_prec_search = false;
2192 encoder->protected_->do_exhaustive_model_search = false;
2193 encoder->protected_->do_escape_coding = false;
2194 encoder->protected_->min_residual_partition_order = 0;
2195 encoder->protected_->max_residual_partition_order = 0;
2196 encoder->protected_->rice_parameter_search_dist = 0;
2197 encoder->protected_->total_samples_estimate = 0;
2198 encoder->protected_->metadata = 0;
2199 encoder->protected_->num_metadata_blocks = 0;
2200
Josh Coalson6b21f662006-09-13 01:42:27 +00002201 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002202 encoder->private_->disable_constant_subframes = false;
2203 encoder->private_->disable_fixed_subframes = false;
2204 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002205#if FLAC__HAS_OGG
2206 encoder->private_->is_ogg = false;
2207#endif
2208 encoder->private_->read_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002209 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002210 encoder->private_->seek_callback = 0;
2211 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002212 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002213 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002214 encoder->private_->client_data = 0;
Josh Coalson8da98c82006-10-15 04:24:05 +00002215
2216#if FLAC__HAS_OGG
2217 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2218#endif
Josh Coalson92031602002-07-24 06:02:11 +00002219}
2220
Josh Coalsonf1eff452002-07-31 07:05:33 +00002221void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00002222{
2223 unsigned i, channel;
2224
Josh Coalsonf1eff452002-07-31 07:05:33 +00002225 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00002226 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002227 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002228 free(encoder->private_->integer_signal_unaligned[i]);
2229 encoder->private_->integer_signal_unaligned[i] = 0;
2230 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002231#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002232 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002233 free(encoder->private_->real_signal_unaligned[i]);
2234 encoder->private_->real_signal_unaligned[i] = 0;
2235 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002236#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002237 }
2238 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002239 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002240 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2241 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2242 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002243#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002244 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002245 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2246 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2247 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002248#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002249 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002250#ifndef FLAC__INTEGER_ONLY_LIBRARY
2251 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2252 if(0 != encoder->private_->window_unaligned[i]) {
2253 free(encoder->private_->window_unaligned[i]);
2254 encoder->private_->window_unaligned[i] = 0;
2255 }
2256 }
2257 if(0 != encoder->private_->windowed_signal_unaligned) {
2258 free(encoder->private_->windowed_signal_unaligned);
2259 encoder->private_->windowed_signal_unaligned = 0;
2260 }
2261#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002262 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2263 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002264 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002265 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2266 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2267 }
2268 }
2269 }
2270 for(channel = 0; channel < 2; channel++) {
2271 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002272 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002273 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2274 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2275 }
2276 }
2277 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002278 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002279 free(encoder->private_->abs_residual_unaligned);
2280 encoder->private_->abs_residual_unaligned = 0;
2281 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002282 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002283 free(encoder->private_->abs_residual_partition_sums_unaligned);
2284 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2285 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002286 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002287 free(encoder->private_->raw_bits_per_partition_unaligned);
2288 encoder->private_->raw_bits_per_partition_unaligned = 0;
2289 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002290 if(encoder->protected_->verify) {
2291 for(i = 0; i < encoder->protected_->channels; i++) {
2292 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2293 free(encoder->private_->verify.input_fifo.data[i]);
2294 encoder->private_->verify.input_fifo.data[i] = 0;
2295 }
2296 }
2297 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002298 FLAC__bitbuffer_free(encoder->private_->frame);
2299}
2300
Josh Coalsonf1eff452002-07-31 07:05:33 +00002301FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002302{
Josh Coalson77e3f312001-06-23 03:03:24 +00002303 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002304 unsigned i, channel;
2305
2306 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002307 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2308 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002309
2310 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002311 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002312 return true;
2313
2314 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002315
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002316 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2317 * requires that the input arrays (in our case the integer signals)
2318 * have a buffer of up to 3 zeroes in front (at negative indices) for
2319 * alignment purposes; we use 4 to keep the data well-aligned.
2320 */
Josh Coalson8395d022001-07-12 21:25:22 +00002321
Josh Coalsonfa697a92001-08-16 20:07:29 +00002322 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2323 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 +00002324#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002325 if(encoder->protected_->max_lpc_order > 0)
2326 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 +00002327#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002328 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2329 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00002330 }
2331 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002332 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 +00002333#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002334 if(encoder->protected_->max_lpc_order > 0)
2335 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 +00002336#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002337 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2338 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00002339 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002340#ifndef FLAC__INTEGER_ONLY_LIBRARY
2341 if(ok && encoder->protected_->max_lpc_order > 0) {
2342 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2343 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2344 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2345 }
2346#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002347 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002348 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002349 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 +00002350 }
2351 }
2352 for(channel = 0; ok && channel < 2; channel++) {
2353 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002354 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 +00002355 }
2356 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002357 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
2358 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 */
2359 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2360 if(encoder->protected_->do_escape_coding)
2361 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 +00002362
2363 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00002364 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00002365 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002366 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00002367
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002368#ifndef FLAC__INTEGER_ONLY_LIBRARY
2369 if(ok && encoder->protected_->max_lpc_order > 0) {
2370 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2371 switch(encoder->protected_->apodizations[i].type) {
2372 case FLAC__APODIZATION_BARTLETT:
2373 FLAC__window_bartlett(encoder->private_->window[i], new_size);
2374 break;
2375 case FLAC__APODIZATION_BARTLETT_HANN:
2376 FLAC__window_bartlett_hann(encoder->private_->window[i], new_size);
2377 break;
2378 case FLAC__APODIZATION_BLACKMAN:
2379 FLAC__window_blackman(encoder->private_->window[i], new_size);
2380 break;
2381 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2382 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_size);
2383 break;
2384 case FLAC__APODIZATION_CONNES:
2385 FLAC__window_connes(encoder->private_->window[i], new_size);
2386 break;
2387 case FLAC__APODIZATION_FLATTOP:
2388 FLAC__window_flattop(encoder->private_->window[i], new_size);
2389 break;
2390 case FLAC__APODIZATION_GAUSS:
2391 FLAC__window_gauss(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2392 break;
2393 case FLAC__APODIZATION_HAMMING:
2394 FLAC__window_hamming(encoder->private_->window[i], new_size);
2395 break;
2396 case FLAC__APODIZATION_HANN:
2397 FLAC__window_hann(encoder->private_->window[i], new_size);
2398 break;
2399 case FLAC__APODIZATION_KAISER_BESSEL:
2400 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_size);
2401 break;
2402 case FLAC__APODIZATION_NUTTALL:
2403 FLAC__window_nuttall(encoder->private_->window[i], new_size);
2404 break;
2405 case FLAC__APODIZATION_RECTANGLE:
2406 FLAC__window_rectangle(encoder->private_->window[i], new_size);
2407 break;
2408 case FLAC__APODIZATION_TRIANGLE:
2409 FLAC__window_triangle(encoder->private_->window[i], new_size);
2410 break;
2411 case FLAC__APODIZATION_TUKEY:
2412 FLAC__window_tukey(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.tukey.p);
2413 break;
2414 case FLAC__APODIZATION_WELCH:
2415 FLAC__window_welch(encoder->private_->window[i], new_size);
2416 break;
2417 default:
2418 FLAC__ASSERT(0);
2419 /* double protection */
Josh Coalsondf598452006-04-28 00:13:34 +00002420 FLAC__window_hann(encoder->private_->window[i], new_size);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002421 break;
2422 }
2423 }
2424 }
2425#endif
2426
Josh Coalson0a15c142001-06-13 17:59:57 +00002427 return ok;
2428}
2429
Josh Coalsond86e03b2002-08-03 21:56:15 +00002430FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00002431{
2432 const FLAC__byte *buffer;
2433 unsigned bytes;
2434
2435 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2436
2437 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2438
Josh Coalsond86e03b2002-08-03 21:56:15 +00002439 if(encoder->protected_->verify) {
2440 encoder->private_->verify.output.data = buffer;
2441 encoder->private_->verify.output.bytes = bytes;
2442 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2443 encoder->private_->verify.needs_magic_hack = true;
2444 }
2445 else {
2446 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2447 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2448 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2449 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2450 return false;
2451 }
2452 }
2453 }
2454
Josh Coalson6b21f662006-09-13 01:42:27 +00002455 if(write_frame_(encoder, buffer, bytes, samples) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002456 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002457 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002458 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002459 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002460
2461 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2462
Josh Coalsond86e03b2002-08-03 21:56:15 +00002463 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002464 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2465 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 +00002466 }
2467
Josh Coalson5c491a12002-08-01 06:39:40 +00002468 return true;
2469}
2470
Josh Coalson8da98c82006-10-15 04:24:05 +00002471FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples)
Josh Coalson6b21f662006-09-13 01:42:27 +00002472{
2473 FLAC__StreamEncoderWriteStatus status;
2474 FLAC__uint64 output_position = 0;
2475
2476 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2477 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2478 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2479 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2480 }
2481
2482 /*
2483 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2484 */
2485 if(samples == 0) {
2486 FLAC__MetadataType type = (buffer[0] & 0x7f);
2487 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2488 encoder->protected_->streaminfo_offset = output_position;
2489 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2490 encoder->protected_->seektable_offset = output_position;
2491 }
2492
2493 /*
2494 * Mark the current seek point if hit (if audio_offset == 0 that
2495 * means we're still writing metadata and haven't hit the first
2496 * frame yet)
2497 */
2498 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2499 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2500 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2501 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2502 FLAC__uint64 test_sample;
2503 unsigned i;
2504 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2505 test_sample = encoder->private_->seek_table->points[i].sample_number;
2506 if(test_sample > frame_last_sample) {
2507 break;
2508 }
2509 else if(test_sample >= frame_first_sample) {
2510 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2511 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2512 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2513 encoder->private_->first_seekpoint_to_check++;
2514 /* DO NOT: "break;" and here's why:
2515 * The seektable template may contain more than one target
2516 * sample for any given frame; we will keep looping, generating
2517 * duplicate seekpoints for them, and we'll clean it up later,
2518 * just before writing the seektable back to the metadata.
2519 */
2520 }
2521 else {
2522 encoder->private_->first_seekpoint_to_check++;
2523 }
2524 }
2525 }
2526
Josh Coalson8da98c82006-10-15 04:24:05 +00002527#if FLAC__HAS_OGG
2528 if(encoder->private_->is_ogg) {
2529 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2530 &encoder->protected_->ogg_encoder_aspect,
2531 FLAC__stream_encoder_get_total_samples_estimate(encoder),
2532 buffer,
2533 bytes,
2534 samples,
2535 encoder->private_->current_frame_number,
2536 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2537 encoder,
2538 encoder->private_->client_data
2539 );
2540 }
2541 else
2542#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00002543 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2544
2545 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2546 encoder->private_->bytes_written += bytes;
2547 encoder->private_->samples_written += samples;
2548 /* we keep a high watermark on the number of frames written because
2549 * when the encoder goes back to write metadata, 'current_frame'
2550 * will drop back to 0.
2551 */
2552 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2553 }
2554 else
2555 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2556
2557 return status;
2558}
2559
2560/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2561void update_metadata_(const FLAC__StreamEncoder *encoder)
2562{
2563 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2564 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2565 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2566 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2567 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2568 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2569 FLAC__StreamEncoderSeekStatus seek_status;
2570
2571 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2572
2573 /* All this is based on intimate knowledge of the stream header
2574 * layout, but a change to the header format that would break this
2575 * would also break all streams encoded in the previous format.
2576 */
2577
2578 /*
2579 * Write MD5 signature
2580 */
2581 {
2582 const unsigned md5_offset =
2583 FLAC__STREAM_METADATA_HEADER_LENGTH +
2584 (
2585 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2586 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2587 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2588 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2589 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2590 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2591 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2592 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2593 ) / 8;
2594
2595 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2596 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2597 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2598 return;
2599 }
2600 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2601 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2602 return;
2603 }
2604 }
2605
2606 /*
2607 * Write total samples
2608 */
2609 {
2610 const unsigned total_samples_byte_offset =
2611 FLAC__STREAM_METADATA_HEADER_LENGTH +
2612 (
2613 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2614 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2615 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2616 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2617 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2618 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2619 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2620 - 4
2621 ) / 8;
2622
2623 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2624 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2625 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2626 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2627 b[4] = (FLAC__byte)(samples & 0xFF);
2628 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) {
2629 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2630 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2631 return;
2632 }
2633 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2634 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2635 return;
2636 }
2637 }
2638
2639 /*
2640 * Write min/max framesize
2641 */
2642 {
2643 const unsigned min_framesize_offset =
2644 FLAC__STREAM_METADATA_HEADER_LENGTH +
2645 (
2646 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2647 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2648 ) / 8;
2649
2650 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2651 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2652 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2653 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2654 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2655 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2656 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) {
2657 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2658 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2659 return;
2660 }
2661 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2662 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2663 return;
2664 }
2665 }
2666
2667 /*
2668 * Write seektable
2669 */
2670 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2671 unsigned i;
2672
2673 FLAC__format_seektable_sort(encoder->private_->seek_table);
2674
2675 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2676
2677 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) {
2678 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2679 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2680 return;
2681 }
2682
2683 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2684 FLAC__uint64 xx;
2685 unsigned x;
2686 xx = encoder->private_->seek_table->points[i].sample_number;
2687 b[7] = (FLAC__byte)xx; xx >>= 8;
2688 b[6] = (FLAC__byte)xx; xx >>= 8;
2689 b[5] = (FLAC__byte)xx; xx >>= 8;
2690 b[4] = (FLAC__byte)xx; xx >>= 8;
2691 b[3] = (FLAC__byte)xx; xx >>= 8;
2692 b[2] = (FLAC__byte)xx; xx >>= 8;
2693 b[1] = (FLAC__byte)xx; xx >>= 8;
2694 b[0] = (FLAC__byte)xx; xx >>= 8;
2695 xx = encoder->private_->seek_table->points[i].stream_offset;
2696 b[15] = (FLAC__byte)xx; xx >>= 8;
2697 b[14] = (FLAC__byte)xx; xx >>= 8;
2698 b[13] = (FLAC__byte)xx; xx >>= 8;
2699 b[12] = (FLAC__byte)xx; xx >>= 8;
2700 b[11] = (FLAC__byte)xx; xx >>= 8;
2701 b[10] = (FLAC__byte)xx; xx >>= 8;
2702 b[9] = (FLAC__byte)xx; xx >>= 8;
2703 b[8] = (FLAC__byte)xx; xx >>= 8;
2704 x = encoder->private_->seek_table->points[i].frame_samples;
2705 b[17] = (FLAC__byte)x; x >>= 8;
2706 b[16] = (FLAC__byte)x; x >>= 8;
2707 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2708 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2709 return;
2710 }
2711 }
2712 }
2713}
2714
Josh Coalson15b8eb82006-10-15 05:15:55 +00002715#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00002716/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2717void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2718{
2719 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2720 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2721 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2722 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2723 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2724 ogg_page page;
2725
2726 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2727
2728 /* All this is based on intimate knowledge of the stream header
2729 * layout, but a change to the header format that would break this
2730 * would also break all streams encoded in the previous format.
2731 */
2732
2733 /**
2734 ** Write STREAMINFO stats
2735 **/
2736 simple_ogg_page__init(&page);
2737 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2738 simple_ogg_page__clear(&page);
2739 return; /* state already set */
2740 }
2741
2742 /*
2743 * Write MD5 signature
2744 */
2745 {
2746 const unsigned md5_offset =
2747 FLAC__STREAM_METADATA_HEADER_LENGTH +
2748 (
2749 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2750 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2751 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2752 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2753 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2754 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2755 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2756 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2757 ) / 8;
2758
2759 if(md5_offset + 16 > (unsigned)page.body_len) {
2760 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2761 simple_ogg_page__clear(&page);
2762 return;
2763 }
2764 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2765 }
2766
2767 /*
2768 * Write total samples
2769 */
2770 {
2771 const unsigned total_samples_byte_offset =
2772 FLAC__STREAM_METADATA_HEADER_LENGTH +
2773 (
2774 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2775 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2776 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2777 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2778 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2779 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2780 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2781 - 4
2782 ) / 8;
2783
2784 if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2785 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2786 simple_ogg_page__clear(&page);
2787 return;
2788 }
2789 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2790 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2791 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2792 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2793 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2794 b[4] = (FLAC__byte)(samples & 0xFF);
2795 memcpy(page.body + total_samples_byte_offset, b, 5);
2796 }
2797
2798 /*
2799 * Write min/max framesize
2800 */
2801 {
2802 const unsigned min_framesize_offset =
2803 FLAC__STREAM_METADATA_HEADER_LENGTH +
2804 (
2805 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2806 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2807 ) / 8;
2808
2809 if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2810 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2811 simple_ogg_page__clear(&page);
2812 return;
2813 }
2814 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2815 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2816 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2817 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2818 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2819 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2820 memcpy(page.body + min_framesize_offset, b, 6);
2821 }
2822 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2823 simple_ogg_page__clear(&page);
2824 return; /* state already set */
2825 }
2826 simple_ogg_page__clear(&page);
2827
2828 /*
2829 * Write seektable
2830 */
2831 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2832 unsigned i;
2833 FLAC__byte *p;
2834
2835 FLAC__format_seektable_sort(encoder->private_->seek_table);
2836
2837 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2838
2839 simple_ogg_page__init(&page);
2840 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2841 simple_ogg_page__clear(&page);
2842 return; /* state already set */
2843 }
2844
2845 if(FLAC__STREAM_METADATA_HEADER_LENGTH + (18*encoder->private_->seek_table->num_points) > (unsigned)page.body_len) {
2846 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2847 simple_ogg_page__clear(&page);
2848 return;
2849 }
2850
2851 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
2852 FLAC__uint64 xx;
2853 unsigned x;
2854 xx = encoder->private_->seek_table->points[i].sample_number;
2855 b[7] = (FLAC__byte)xx; xx >>= 8;
2856 b[6] = (FLAC__byte)xx; xx >>= 8;
2857 b[5] = (FLAC__byte)xx; xx >>= 8;
2858 b[4] = (FLAC__byte)xx; xx >>= 8;
2859 b[3] = (FLAC__byte)xx; xx >>= 8;
2860 b[2] = (FLAC__byte)xx; xx >>= 8;
2861 b[1] = (FLAC__byte)xx; xx >>= 8;
2862 b[0] = (FLAC__byte)xx; xx >>= 8;
2863 xx = encoder->private_->seek_table->points[i].stream_offset;
2864 b[15] = (FLAC__byte)xx; xx >>= 8;
2865 b[14] = (FLAC__byte)xx; xx >>= 8;
2866 b[13] = (FLAC__byte)xx; xx >>= 8;
2867 b[12] = (FLAC__byte)xx; xx >>= 8;
2868 b[11] = (FLAC__byte)xx; xx >>= 8;
2869 b[10] = (FLAC__byte)xx; xx >>= 8;
2870 b[9] = (FLAC__byte)xx; xx >>= 8;
2871 b[8] = (FLAC__byte)xx; xx >>= 8;
2872 x = encoder->private_->seek_table->points[i].frame_samples;
2873 b[17] = (FLAC__byte)x; x >>= 8;
2874 b[16] = (FLAC__byte)x; x >>= 8;
2875 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2876 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2877 simple_ogg_page__clear(&page);
2878 return;
2879 }
2880 memcpy(p, b, 18);
2881 }
2882
2883 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2884 simple_ogg_page__clear(&page);
2885 return; /* state already set */
2886 }
2887 simple_ogg_page__clear(&page);
2888 }
2889}
Josh Coalson15b8eb82006-10-15 05:15:55 +00002890#endif
Josh Coalson8da98c82006-10-15 04:24:05 +00002891
Josh Coalsonf1eff452002-07-31 07:05:33 +00002892FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00002893{
Josh Coalsonfa697a92001-08-16 20:07:29 +00002894 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002895
2896 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002897 * Accumulate raw signal to the MD5 signature
2898 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00002899 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 +00002900 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002901 return false;
2902 }
2903
2904 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002905 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002906 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00002907 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002908 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002909 return false;
2910 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002911
2912 /*
2913 * Zero-pad the frame to a byte_boundary
2914 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002915 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002916 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002917 return false;
2918 }
2919
2920 /*
Josh Coalson215af572001-03-27 01:15:58 +00002921 * CRC-16 the whole thing
2922 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002923 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2924 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 +00002925
2926 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002927 * Write it
2928 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00002929 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
2930 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002931 return false;
2932 }
2933
2934 /*
2935 * Get ready for the next frame
2936 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002937 encoder->private_->current_sample_number = 0;
2938 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00002939 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002940
2941 return true;
2942}
2943
Josh Coalsonf1eff452002-07-31 07:05:33 +00002944FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002945{
2946 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002947 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002948 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002949
2950 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00002951 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00002952 */
2953 if(is_last_frame) {
2954 max_partition_order = 0;
2955 }
2956 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002957 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2958 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002959 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002960 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002961
Josh Coalsonfa697a92001-08-16 20:07:29 +00002962 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 +00002963
Josh Coalson94e02cd2001-01-25 10:41:06 +00002964 /*
2965 * Setup the frame
2966 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002967 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002968 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002969 return false;
2970 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002971 frame_header.blocksize = encoder->protected_->blocksize;
2972 frame_header.sample_rate = encoder->protected_->sample_rate;
2973 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002974 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002975 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002976 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002977 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002978
2979 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002980 * Figure out what channel assignments to try
2981 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002982 if(encoder->protected_->do_mid_side_stereo) {
2983 if(encoder->protected_->loose_mid_side_stereo) {
2984 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002985 do_independent = true;
2986 do_mid_side = true;
2987 }
2988 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002989 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002990 do_mid_side = !do_independent;
2991 }
2992 }
2993 else {
2994 do_independent = true;
2995 do_mid_side = true;
2996 }
2997 }
2998 else {
2999 do_independent = true;
3000 do_mid_side = false;
3001 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003002
Josh Coalson1b689822001-05-31 20:11:02 +00003003 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003004
3005 /*
Josh Coalson82b73242001-03-28 22:17:05 +00003006 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00003007 */
3008 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003009 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003010 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003011 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3012 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00003013 }
Josh Coalson859bc542001-03-27 22:22:27 +00003014 }
3015 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003016 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00003017 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003018 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00003019 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3020 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00003021 }
Josh Coalson859bc542001-03-27 22:22:27 +00003022 }
3023
3024 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003025 * First do a normal encoding pass of each independent channel
3026 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003027 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003028 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003029 if(!
3030 process_subframe_(
3031 encoder,
3032 min_partition_order,
3033 max_partition_order,
3034 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003035 &frame_header,
3036 encoder->private_->subframe_bps[channel],
3037 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003038#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003039 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003040#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003041 encoder->private_->subframe_workspace_ptr[channel],
3042 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3043 encoder->private_->residual_workspace[channel],
3044 encoder->private_->best_subframe+channel,
3045 encoder->private_->best_subframe_bits+channel
3046 )
3047 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003048 return false;
3049 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003050 }
3051
3052 /*
3053 * Now do mid and side channels if requested
3054 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003055 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003056 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003057
3058 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003059 if(!
3060 process_subframe_(
3061 encoder,
3062 min_partition_order,
3063 max_partition_order,
3064 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003065 &frame_header,
3066 encoder->private_->subframe_bps_mid_side[channel],
3067 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003068#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003069 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003070#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003071 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3072 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3073 encoder->private_->residual_workspace_mid_side[channel],
3074 encoder->private_->best_subframe_mid_side+channel,
3075 encoder->private_->best_subframe_bits_mid_side+channel
3076 )
3077 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00003078 return false;
3079 }
3080 }
3081
3082 /*
3083 * Compose the frame bitbuffer
3084 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003085 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00003086 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
3087 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003088 FLAC__ChannelAssignment channel_assignment;
3089
Josh Coalsonfa697a92001-08-16 20:07:29 +00003090 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003091
Josh Coalsonfa697a92001-08-16 20:07:29 +00003092 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3093 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 +00003094 }
3095 else {
3096 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3097 unsigned min_bits;
3098 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003099
Josh Coalson1b689822001-05-31 20:11:02 +00003100 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003101
3102 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003103 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3104 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3105 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3106 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 +00003107
Josh Coalson7424d2f2002-11-06 07:10:38 +00003108 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 +00003109 if(bits[ca] < min_bits) {
3110 min_bits = bits[ca];
3111 channel_assignment = ca;
3112 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003113 }
3114 }
3115
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003116 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003117
Josh Coalsond0edb972006-10-07 06:50:08 +00003118 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003119 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003120 return false;
3121 }
3122
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003123 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003124 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
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 [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003127 break;
3128 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003129 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3130 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003131 break;
3132 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003133 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3134 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003135 break;
3136 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003137 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3138 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003139 break;
3140 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003141 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003142 }
Josh Coalson82b73242001-03-28 22:17:05 +00003143
3144 switch(channel_assignment) {
3145 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003146 left_bps = encoder->private_->subframe_bps [0];
3147 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003148 break;
3149 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003150 left_bps = encoder->private_->subframe_bps [0];
3151 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003152 break;
3153 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003154 left_bps = encoder->private_->subframe_bps_mid_side[1];
3155 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003156 break;
3157 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003158 left_bps = encoder->private_->subframe_bps_mid_side[0];
3159 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003160 break;
3161 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003162 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00003163 }
3164
3165 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00003166 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003167 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00003168 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003169 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003170 }
3171 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00003172 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003173 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003174 return false;
3175 }
3176
Josh Coalsonfa697a92001-08-16 20:07:29 +00003177 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson369a6da2006-10-10 00:37:48 +00003178 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 +00003179 /* the above function sets the state for us in case of an error */
3180 return false;
3181 }
3182 }
3183 }
3184
Josh Coalsonfa697a92001-08-16 20:07:29 +00003185 if(encoder->protected_->loose_mid_side_stereo) {
3186 encoder->private_->loose_mid_side_stereo_frame_count++;
3187 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3188 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003189 }
3190
Josh Coalsonfa697a92001-08-16 20:07:29 +00003191 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003192
Josh Coalson94e02cd2001-01-25 10:41:06 +00003193 return true;
3194}
3195
Josh Coalson6fe72f72002-08-20 04:01:59 +00003196FLAC__bool process_subframe_(
3197 FLAC__StreamEncoder *encoder,
3198 unsigned min_partition_order,
3199 unsigned max_partition_order,
3200 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003201 const FLAC__FrameHeader *frame_header,
3202 unsigned subframe_bps,
3203 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003204#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003205 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003206#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003207 FLAC__Subframe *subframe[2],
3208 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3209 FLAC__int32 *residual[2],
3210 unsigned *best_subframe,
3211 unsigned *best_bits
3212)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003213{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003214#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003215 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003216#else
3217 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3218#endif
3219#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003220 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003221 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 +00003222 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003223 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003224 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003225#endif
3226 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003227 unsigned rice_parameter;
3228 unsigned _candidate_bits, _best_bits;
3229 unsigned _best_subframe;
3230
3231 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3232 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003233 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3234 _best_bits = UINT_MAX;
3235 else
3236 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003237
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003238 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
3239 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003240 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 +00003241 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003242 if(
3243 !encoder->private_->disable_constant_subframes &&
3244#ifndef FLAC__INTEGER_ONLY_LIBRARY
3245 fixed_residual_bits_per_sample[1] == 0.0
3246#else
3247 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3248#endif
3249 ) {
3250 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003251 unsigned i;
3252 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003253 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003254 if(integer_signal[0] != integer_signal[i]) {
3255 signal_is_constant = false;
3256 break;
3257 }
3258 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003259 }
3260 if(signal_is_constant) {
3261 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
3262 if(_candidate_bits < _best_bits) {
3263 _best_subframe = !_best_subframe;
3264 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003265 }
3266 }
3267 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003268 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3269 /* encode fixed */
3270 if(encoder->protected_->do_exhaustive_model_search) {
3271 min_fixed_order = 0;
3272 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00003273 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003274 else {
3275 min_fixed_order = max_fixed_order = guess_fixed_order;
3276 }
3277 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003278#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00003279 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003280 continue; /* don't even try */
3281 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 +00003282#else
3283 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3284 continue; /* don't even try */
3285 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 */
3286#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003287 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003288 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3289#ifdef DEBUG_VERBOSE
3290 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3291#endif
3292 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3293 }
3294 _candidate_bits =
3295 evaluate_fixed_subframe_(
3296 encoder,
3297 integer_signal,
3298 residual[!_best_subframe],
3299 encoder->private_->abs_residual,
3300 encoder->private_->abs_residual_partition_sums,
3301 encoder->private_->raw_bits_per_partition,
3302 frame_header->blocksize,
3303 subframe_bps,
3304 fixed_order,
3305 rice_parameter,
3306 min_partition_order,
3307 max_partition_order,
3308 precompute_partition_sums,
3309 encoder->protected_->do_escape_coding,
3310 encoder->protected_->rice_parameter_search_dist,
3311 subframe[!_best_subframe],
3312 partitioned_rice_contents[!_best_subframe]
3313 );
3314 if(_candidate_bits < _best_bits) {
3315 _best_subframe = !_best_subframe;
3316 _best_bits = _candidate_bits;
3317 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003318 }
3319 }
3320
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003321#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00003322 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003323 if(encoder->protected_->max_lpc_order > 0) {
3324 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003325 max_lpc_order = frame_header->blocksize-1;
3326 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00003327 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003328 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003329 unsigned a;
3330 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00003331 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003332 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3333 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3334 if(autoc[0] != 0.0) {
3335 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3336 if(encoder->protected_->do_exhaustive_model_search) {
3337 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003338 }
3339 else {
Josh Coalsondf598452006-04-28 00:13:34 +00003340 const unsigned guess_lpc_order =
3341 FLAC__lpc_compute_best_order(
3342 lpc_error,
3343 max_lpc_order,
3344 frame_header->blocksize,
3345 subframe_bps + (
3346 encoder->protected_->do_qlp_coeff_prec_search?
3347 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3348 encoder->protected_->qlp_coeff_precision
3349 )
3350 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003351 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003352 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003353 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3354 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3355 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3356 continue; /* don't even try */
3357 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3358 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3359 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00003360#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003361 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 +00003362#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003363 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3364 }
3365 if(encoder->protected_->do_qlp_coeff_prec_search) {
3366 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00003367 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3368 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003369 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00003370 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3371 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003372 else
3373 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3374 }
3375 else {
3376 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3377 }
3378 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3379 _candidate_bits =
3380 evaluate_lpc_subframe_(
3381 encoder,
3382 integer_signal,
3383 residual[!_best_subframe],
3384 encoder->private_->abs_residual,
3385 encoder->private_->abs_residual_partition_sums,
3386 encoder->private_->raw_bits_per_partition,
3387 encoder->private_->lp_coeff[lpc_order-1],
3388 frame_header->blocksize,
3389 subframe_bps,
3390 lpc_order,
3391 qlp_coeff_precision,
3392 rice_parameter,
3393 min_partition_order,
3394 max_partition_order,
3395 precompute_partition_sums,
3396 encoder->protected_->do_escape_coding,
3397 encoder->protected_->rice_parameter_search_dist,
3398 subframe[!_best_subframe],
3399 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003400 );
3401 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3402 if(_candidate_bits < _best_bits) {
3403 _best_subframe = !_best_subframe;
3404 _best_bits = _candidate_bits;
3405 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00003406 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003407 }
3408 }
3409 }
3410 }
3411 }
3412 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003413#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00003414 }
3415 }
3416
Josh Coalson72695802002-10-11 06:25:16 +00003417 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3418 if(_best_bits == UINT_MAX) {
3419 FLAC__ASSERT(_best_subframe == 0);
3420 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3421 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003422
Josh Coalson94e02cd2001-01-25 10:41:06 +00003423 *best_subframe = _best_subframe;
3424 *best_bits = _best_bits;
3425
3426 return true;
3427}
3428
Josh Coalson6fe72f72002-08-20 04:01:59 +00003429FLAC__bool add_subframe_(
3430 FLAC__StreamEncoder *encoder,
3431 const FLAC__FrameHeader *frame_header,
3432 unsigned subframe_bps,
3433 const FLAC__Subframe *subframe,
3434 FLAC__BitBuffer *frame
3435)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003436{
3437 switch(subframe->type) {
3438 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003439 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003440 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003441 return false;
3442 }
3443 break;
3444 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00003445 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 +00003446 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003447 return false;
3448 }
3449 break;
3450 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00003451 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 +00003452 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003453 return false;
3454 }
3455 break;
3456 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00003457 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003458 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003459 return false;
3460 }
3461 break;
3462 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003463 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003464 }
3465
3466 return true;
3467}
3468
Josh Coalson6fe72f72002-08-20 04:01:59 +00003469unsigned evaluate_constant_subframe_(
3470 const FLAC__int32 signal,
3471 unsigned subframe_bps,
3472 FLAC__Subframe *subframe
3473)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003474{
3475 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3476 subframe->data.constant.value = signal;
3477
Josh Coalson82b73242001-03-28 22:17:05 +00003478 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 +00003479}
3480
Josh Coalson6fe72f72002-08-20 04:01:59 +00003481unsigned evaluate_fixed_subframe_(
3482 FLAC__StreamEncoder *encoder,
3483 const FLAC__int32 signal[],
3484 FLAC__int32 residual[],
3485 FLAC__uint32 abs_residual[],
3486 FLAC__uint64 abs_residual_partition_sums[],
3487 unsigned raw_bits_per_partition[],
3488 unsigned blocksize,
3489 unsigned subframe_bps,
3490 unsigned order,
3491 unsigned rice_parameter,
3492 unsigned min_partition_order,
3493 unsigned max_partition_order,
3494 FLAC__bool precompute_partition_sums,
3495 FLAC__bool do_escape_coding,
3496 unsigned rice_parameter_search_dist,
3497 FLAC__Subframe *subframe,
3498 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3499)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003500{
3501 unsigned i, residual_bits;
3502 const unsigned residual_samples = blocksize - order;
3503
3504 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3505
3506 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3507
3508 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003509 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003510 subframe->data.fixed.residual = residual;
3511
Josh Coalson6fe72f72002-08-20 04:01:59 +00003512 residual_bits =
3513 find_best_partition_order_(
3514 encoder->private_,
3515 residual,
3516 abs_residual,
3517 abs_residual_partition_sums,
3518 raw_bits_per_partition,
3519 residual_samples,
3520 order,
3521 rice_parameter,
3522 min_partition_order,
3523 max_partition_order,
3524 precompute_partition_sums,
3525 do_escape_coding,
3526 rice_parameter_search_dist,
3527 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3528 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003529
3530 subframe->data.fixed.order = order;
3531 for(i = 0; i < order; i++)
3532 subframe->data.fixed.warmup[i] = signal[i];
3533
Josh Coalson82b73242001-03-28 22:17:05 +00003534 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 +00003535}
3536
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003537#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003538unsigned evaluate_lpc_subframe_(
3539 FLAC__StreamEncoder *encoder,
3540 const FLAC__int32 signal[],
3541 FLAC__int32 residual[],
3542 FLAC__uint32 abs_residual[],
3543 FLAC__uint64 abs_residual_partition_sums[],
3544 unsigned raw_bits_per_partition[],
3545 const FLAC__real lp_coeff[],
3546 unsigned blocksize,
3547 unsigned subframe_bps,
3548 unsigned order,
3549 unsigned qlp_coeff_precision,
3550 unsigned rice_parameter,
3551 unsigned min_partition_order,
3552 unsigned max_partition_order,
3553 FLAC__bool precompute_partition_sums,
3554 FLAC__bool do_escape_coding,
3555 unsigned rice_parameter_search_dist,
3556 FLAC__Subframe *subframe,
3557 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3558)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003559{
Josh Coalson77e3f312001-06-23 03:03:24 +00003560 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003561 unsigned i, residual_bits;
3562 int quantization, ret;
3563 const unsigned residual_samples = blocksize - order;
3564
Josh Coalson20ac2c12002-08-30 05:47:14 +00003565 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3566 if(subframe_bps <= 16) {
3567 FLAC__ASSERT(order > 0);
3568 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3569 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3570 }
3571
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003572 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003573 if(ret != 0)
3574 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3575
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003576 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3577 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3578 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3579 else
3580 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 +00003581 else
3582 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 +00003583
3584 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3585
3586 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003587 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003588 subframe->data.lpc.residual = residual;
3589
Josh Coalson6fe72f72002-08-20 04:01:59 +00003590 residual_bits =
3591 find_best_partition_order_(
3592 encoder->private_,
3593 residual,
3594 abs_residual,
3595 abs_residual_partition_sums,
3596 raw_bits_per_partition,
3597 residual_samples,
3598 order,
3599 rice_parameter,
3600 min_partition_order,
3601 max_partition_order,
3602 precompute_partition_sums,
3603 do_escape_coding,
3604 rice_parameter_search_dist,
3605 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3606 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003607
3608 subframe->data.lpc.order = order;
3609 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3610 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003611 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003612 for(i = 0; i < order; i++)
3613 subframe->data.lpc.warmup[i] = signal[i];
3614
Josh Coalson82b73242001-03-28 22:17:05 +00003615 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 +00003616}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003617#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003618
Josh Coalson6fe72f72002-08-20 04:01:59 +00003619unsigned evaluate_verbatim_subframe_(
3620 const FLAC__int32 signal[],
3621 unsigned blocksize,
3622 unsigned subframe_bps,
3623 FLAC__Subframe *subframe
3624)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003625{
3626 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3627
3628 subframe->data.verbatim.data = signal;
3629
Josh Coalson82b73242001-03-28 22:17:05 +00003630 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 +00003631}
3632
Josh Coalson6fe72f72002-08-20 04:01:59 +00003633unsigned find_best_partition_order_(
3634 FLAC__StreamEncoderPrivate *private_,
3635 const FLAC__int32 residual[],
3636 FLAC__uint32 abs_residual[],
3637 FLAC__uint64 abs_residual_partition_sums[],
3638 unsigned raw_bits_per_partition[],
3639 unsigned residual_samples,
3640 unsigned predictor_order,
3641 unsigned rice_parameter,
3642 unsigned min_partition_order,
3643 unsigned max_partition_order,
3644 FLAC__bool precompute_partition_sums,
3645 FLAC__bool do_escape_coding,
3646 unsigned rice_parameter_search_dist,
3647 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3648)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003649{
Josh Coalson77e3f312001-06-23 03:03:24 +00003650 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003651 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00003652 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00003653 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003654 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003655
Josh Coalson2051dd42001-04-12 22:22:34 +00003656 /* compute abs(residual) for use later */
3657 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3658 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00003659 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00003660 }
3661
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003662 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 +00003663 min_partition_order = min(min_partition_order, max_partition_order);
3664
Josh Coalson8395d022001-07-12 21:25:22 +00003665 if(precompute_partition_sums) {
3666 int partition_order;
3667 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003668
Josh Coalsonf1eff452002-07-31 07:05:33 +00003669 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 +00003670
3671 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00003672 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 +00003673
3674 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3675#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003676 if(!
3677 set_partitioned_rice_with_precompute_(
3678 residual,
3679 abs_residual_partition_sums+sum,
3680 raw_bits_per_partition+sum,
3681 residual_samples,
3682 predictor_order,
3683 rice_parameter,
3684 rice_parameter_search_dist,
3685 (unsigned)partition_order,
3686 do_escape_coding,
3687 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3688 &residual_bits
3689 )
3690 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003691#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003692 if(!
3693 set_partitioned_rice_with_precompute_(
3694 abs_residual,
3695 abs_residual_partition_sums+sum,
3696 raw_bits_per_partition+sum,
3697 residual_samples,
3698 predictor_order,
3699 rice_parameter,
3700 rice_parameter_search_dist,
3701 (unsigned)partition_order,
3702 do_escape_coding,
3703 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3704 &residual_bits
3705 )
3706 )
Josh Coalson8395d022001-07-12 21:25:22 +00003707#endif
3708 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003709 FLAC__ASSERT(best_residual_bits != 0);
3710 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003711 }
3712 sum += 1u << partition_order;
3713 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3714 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003715 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003716 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003717 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003718 }
3719 }
Josh Coalson8395d022001-07-12 21:25:22 +00003720 else {
3721 unsigned partition_order;
3722 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3723#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003724 if(!
3725 set_partitioned_rice_(
3726 abs_residual,
3727 residual,
3728 residual_samples,
3729 predictor_order,
3730 rice_parameter,
3731 rice_parameter_search_dist,
3732 partition_order,
3733 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3734 &residual_bits
3735 )
3736 )
Josh Coalson8395d022001-07-12 21:25:22 +00003737#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003738 if(!
3739 set_partitioned_rice_(
3740 abs_residual,
3741 residual_samples,
3742 predictor_order,
3743 rice_parameter,
3744 rice_parameter_search_dist,
3745 partition_order,
3746 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3747 &residual_bits
3748 )
3749 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003750#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003751 {
3752 FLAC__ASSERT(best_residual_bits != 0);
3753 break;
3754 }
3755 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3756 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003757 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003758 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003759 }
3760 }
3761 }
3762
Josh Coalsona37ba462002-08-19 21:36:39 +00003763 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003764 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003765 * it is const to the outside world.
3766 */
3767 {
3768 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3769 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3770 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3771 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3772 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003773
3774 return best_residual_bits;
3775}
3776
Josh Coalson6fe72f72002-08-20 04:01:59 +00003777void precompute_partition_info_sums_(
3778 const FLAC__uint32 abs_residual[],
3779 FLAC__uint64 abs_residual_partition_sums[],
3780 unsigned residual_samples,
3781 unsigned predictor_order,
3782 unsigned min_partition_order,
3783 unsigned max_partition_order
3784)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003785{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003786 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003787 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003788 const unsigned blocksize = residual_samples + predictor_order;
3789
Josh Coalsonaef013c2001-04-24 01:25:42 +00003790 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003791 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003792 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00003793 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003794 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003795 const unsigned partitions = 1u << partition_order;
3796 const unsigned default_partition_samples = blocksize >> partition_order;
3797
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003798 FLAC__ASSERT(default_partition_samples > predictor_order);
3799
3800 for(partition = residual_sample = 0; partition < partitions; partition++) {
3801 partition_samples = default_partition_samples;
3802 if(partition == 0)
3803 partition_samples -= predictor_order;
3804 abs_residual_partition_sum = 0;
3805 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3806 abs_r = abs_residual[residual_sample];
3807 abs_residual_partition_sum += abs_r;
3808 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003809 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003810 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003811 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003812 to_partition = partitions;
3813 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003814 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00003815
Josh Coalson8395d022001-07-12 21:25:22 +00003816 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00003817 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003818 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003819 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003820 const unsigned partitions = 1u << partition_order;
3821 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00003822 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003823 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003824 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003825 from_partition++;
3826 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003827 }
3828 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003829}
Josh Coalson8395d022001-07-12 21:25:22 +00003830
Josh Coalson6fe72f72002-08-20 04:01:59 +00003831void precompute_partition_info_escapes_(
3832 const FLAC__int32 residual[],
3833 unsigned raw_bits_per_partition[],
3834 unsigned residual_samples,
3835 unsigned predictor_order,
3836 unsigned min_partition_order,
3837 unsigned max_partition_order
3838)
Josh Coalson8395d022001-07-12 21:25:22 +00003839{
3840 int partition_order;
3841 unsigned from_partition, to_partition = 0;
3842 const unsigned blocksize = residual_samples + predictor_order;
3843
3844 /* first do max_partition_order */
3845 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3846 FLAC__int32 r, residual_partition_min, residual_partition_max;
3847 unsigned silog2_min, silog2_max;
3848 unsigned partition, partition_sample, partition_samples, residual_sample;
3849 const unsigned partitions = 1u << partition_order;
3850 const unsigned default_partition_samples = blocksize >> partition_order;
3851
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003852 FLAC__ASSERT(default_partition_samples > predictor_order);
3853
3854 for(partition = residual_sample = 0; partition < partitions; partition++) {
3855 partition_samples = default_partition_samples;
3856 if(partition == 0)
3857 partition_samples -= predictor_order;
3858 residual_partition_min = residual_partition_max = 0;
3859 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3860 r = residual[residual_sample];
3861 if(r < residual_partition_min)
3862 residual_partition_min = r;
3863 else if(r > residual_partition_max)
3864 residual_partition_max = r;
3865 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00003866 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003867 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
3868 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
3869 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00003870 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003871 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00003872 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00003873 }
3874
3875 /* now merge partitions for lower orders */
3876 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3877 unsigned m;
3878 unsigned i;
3879 const unsigned partitions = 1u << partition_order;
3880 for(i = 0; i < partitions; i++) {
3881 m = raw_bits_per_partition[from_partition];
3882 from_partition++;
3883 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
3884 from_partition++;
3885 to_partition++;
3886 }
3887 }
3888}
Josh Coalson94e02cd2001-01-25 10:41:06 +00003889
Josh Coalson352e0f62001-03-20 22:55:50 +00003890#ifdef VARIABLE_RICE_BITS
3891#undef VARIABLE_RICE_BITS
3892#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003893#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003894#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00003895#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003896
Josh Coalson8395d022001-07-12 21:25:22 +00003897#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003898FLAC__bool set_partitioned_rice_(
3899 const FLAC__uint32 abs_residual[],
3900 const FLAC__int32 residual[],
3901 const unsigned residual_samples,
3902 const unsigned predictor_order,
3903 const unsigned suggested_rice_parameter,
3904 const unsigned rice_parameter_search_dist,
3905 const unsigned partition_order,
3906 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3907 unsigned *bits
3908)
Josh Coalson8395d022001-07-12 21:25:22 +00003909#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003910FLAC__bool set_partitioned_rice_(
3911 const FLAC__uint32 abs_residual[],
3912 const unsigned residual_samples,
3913 const unsigned predictor_order,
3914 const unsigned suggested_rice_parameter,
3915 const unsigned rice_parameter_search_dist,
3916 const unsigned partition_order,
3917 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3918 unsigned *bits
3919)
Josh Coalson8395d022001-07-12 21:25:22 +00003920#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003921{
Josh Coalson034dfab2001-04-27 19:10:23 +00003922 unsigned rice_parameter, partition_bits;
3923#ifndef NO_RICE_SEARCH
3924 unsigned best_partition_bits;
3925 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3926#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003927 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003928 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003929
Josh Coalson1b689822001-05-31 20:11:02 +00003930 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00003931
Josh Coalsona37ba462002-08-19 21:36:39 +00003932 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3933 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003934
Josh Coalson94e02cd2001-01-25 10:41:06 +00003935 if(partition_order == 0) {
3936 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00003937
Josh Coalson034dfab2001-04-27 19:10:23 +00003938#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003939 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003940 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003941 min_rice_parameter = 0;
3942 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003943 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3944 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003945 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003946#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003947 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3948#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003949 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003950 }
3951 }
3952 else
3953 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3954
3955 best_partition_bits = 0xffffffff;
3956 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3957#endif
3958#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003959 const unsigned rice_parameter_estimate = rice_parameter-1;
3960 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00003961#else
3962 partition_bits = 0;
3963#endif
3964 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3965 for(i = 0; i < residual_samples; i++) {
3966#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003967 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00003968#else
3969 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3970#endif
3971 }
3972#ifndef NO_RICE_SEARCH
3973 if(partition_bits < best_partition_bits) {
3974 best_rice_parameter = rice_parameter;
3975 best_partition_bits = partition_bits;
3976 }
3977 }
3978#endif
3979 parameters[0] = best_rice_parameter;
3980 bits_ += best_partition_bits;
3981 }
3982 else {
3983 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003984 unsigned partition_samples;
3985 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00003986 const unsigned partitions = 1u << partition_order;
3987 for(partition = residual_sample = 0; partition < partitions; partition++) {
3988 partition_samples = (residual_samples+predictor_order) >> partition_order;
3989 if(partition == 0) {
3990 if(partition_samples <= predictor_order)
3991 return false;
3992 else
3993 partition_samples -= predictor_order;
3994 }
3995 mean = 0;
3996 save_residual_sample = residual_sample;
3997 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003998 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00003999 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004000 /* we are basically calculating the size in bits of the
4001 * average residual magnitude in the partition:
4002 * rice_parameter = floor(log2(mean/partition_samples))
4003 * 'mean' is not a good name for the variable, it is
4004 * actually the sum of magnitudes of all residual values
4005 * in the partition, so the actual mean is
4006 * mean/partition_samples
4007 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004008 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00004009 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004010 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004011#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004012 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4013#endif
4014 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4015 }
4016
4017#ifndef NO_RICE_SEARCH
4018 if(rice_parameter_search_dist) {
4019 if(rice_parameter < rice_parameter_search_dist)
4020 min_rice_parameter = 0;
4021 else
4022 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4023 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
4024 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004025#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004026 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4027#endif
4028 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4029 }
4030 }
4031 else
4032 min_rice_parameter = max_rice_parameter = rice_parameter;
4033
4034 best_partition_bits = 0xffffffff;
4035 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4036#endif
4037#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004038 const unsigned rice_parameter_estimate = rice_parameter-1;
4039 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00004040#else
4041 partition_bits = 0;
4042#endif
4043 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
4044 save_residual_sample = residual_sample;
4045 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
4046#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00004047 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00004048#else
4049 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
4050#endif
4051 }
4052#ifndef NO_RICE_SEARCH
4053 if(rice_parameter != max_rice_parameter)
4054 residual_sample = save_residual_sample;
4055 if(partition_bits < best_partition_bits) {
4056 best_rice_parameter = rice_parameter;
4057 best_partition_bits = partition_bits;
4058 }
4059 }
4060#endif
4061 parameters[partition] = best_rice_parameter;
4062 bits_ += best_partition_bits;
4063 }
4064 }
4065
4066 *bits = bits_;
4067 return true;
4068}
4069
4070#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00004071FLAC__bool set_partitioned_rice_with_precompute_(
4072 const FLAC__int32 residual[],
4073 const FLAC__uint64 abs_residual_partition_sums[],
4074 const unsigned raw_bits_per_partition[],
4075 const unsigned residual_samples,
4076 const unsigned predictor_order,
4077 const unsigned suggested_rice_parameter,
4078 const unsigned rice_parameter_search_dist,
4079 const unsigned partition_order,
4080 const FLAC__bool search_for_escapes,
4081 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4082 unsigned *bits
4083)
Josh Coalson8395d022001-07-12 21:25:22 +00004084#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00004085FLAC__bool set_partitioned_rice_with_precompute_(
4086 const FLAC__uint32 abs_residual[],
4087 const FLAC__uint64 abs_residual_partition_sums[],
4088 const unsigned raw_bits_per_partition[],
4089 const unsigned residual_samples,
4090 const unsigned predictor_order,
4091 const unsigned suggested_rice_parameter,
4092 const unsigned rice_parameter_search_dist,
4093 const unsigned partition_order,
4094 const FLAC__bool search_for_escapes,
4095 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4096 unsigned *bits
4097)
Josh Coalson8395d022001-07-12 21:25:22 +00004098#endif
4099{
4100 unsigned rice_parameter, partition_bits;
4101#ifndef NO_RICE_SEARCH
4102 unsigned best_partition_bits;
4103 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
4104#endif
4105 unsigned flat_bits;
4106 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004107 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004108
4109 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
4110
Josh Coalsona37ba462002-08-19 21:36:39 +00004111 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
4112 parameters = partitioned_rice_contents->parameters;
4113 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004114
Josh Coalson8395d022001-07-12 21:25:22 +00004115 if(partition_order == 0) {
4116 unsigned i;
4117
4118#ifndef NO_RICE_SEARCH
4119 if(rice_parameter_search_dist) {
4120 if(suggested_rice_parameter < rice_parameter_search_dist)
4121 min_rice_parameter = 0;
4122 else
4123 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4124 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
4125 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004126#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004127 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4128#endif
4129 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4130 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004131 }
4132 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004133 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00004134
Josh Coalson034dfab2001-04-27 19:10:23 +00004135 best_partition_bits = 0xffffffff;
4136 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4137#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004138#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00004139 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00004140 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004141#else
4142 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004143#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004144 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00004145 for(i = 0; i < residual_samples; i++) {
4146#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00004147 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004148#else
Josh Coalson2051dd42001-04-12 22:22:34 +00004149 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 +00004150#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00004151 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004152#ifndef NO_RICE_SEARCH
4153 if(partition_bits < best_partition_bits) {
4154 best_rice_parameter = rice_parameter;
4155 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00004156 }
4157 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004158#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004159 if(search_for_escapes) {
4160 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;
4161 if(flat_bits <= best_partition_bits) {
4162 raw_bits[0] = raw_bits_per_partition[0];
4163 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4164 best_partition_bits = flat_bits;
4165 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004166 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004167 parameters[0] = best_rice_parameter;
4168 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004169 }
4170 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00004171 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004172 unsigned partition_samples;
4173 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004174 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00004175 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004176 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00004177 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004178 if(partition_samples <= predictor_order)
4179 return false;
4180 else
4181 partition_samples -= predictor_order;
4182 }
Josh Coalson05d20792001-06-29 23:12:26 +00004183 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004184 /* we are basically calculating the size in bits of the
4185 * average residual magnitude in the partition:
4186 * rice_parameter = floor(log2(mean/partition_samples))
4187 * 'mean' is not a good name for the variable, it is
4188 * actually the sum of magnitudes of all residual values
4189 * in the partition, so the actual mean is
4190 * mean/partition_samples
4191 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004192 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00004193 ;
Josh Coalson8395d022001-07-12 21:25:22 +00004194 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004195#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004196 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4197#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004198 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004199 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004200
Josh Coalson034dfab2001-04-27 19:10:23 +00004201#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004202 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004203 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004204 min_rice_parameter = 0;
4205 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004206 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4207 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00004208 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00004209#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00004210 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4211#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00004212 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004213 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004214 }
4215 else
4216 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00004217
Josh Coalson034dfab2001-04-27 19:10:23 +00004218 best_partition_bits = 0xffffffff;
4219 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4220#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00004221#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00004222 const unsigned rice_parameter_estimate = rice_parameter-1;
4223 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00004224#else
4225 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004226#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004227 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00004228 save_residual_sample = residual_sample;
4229 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00004230#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00004231 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004232#else
Josh Coalson4dacd192001-06-06 21:11:44 +00004233 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 +00004234#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004235 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004236#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00004237 if(rice_parameter != max_rice_parameter)
4238 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00004239 if(partition_bits < best_partition_bits) {
4240 best_rice_parameter = rice_parameter;
4241 best_partition_bits = partition_bits;
4242 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004243 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004244#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004245 if(search_for_escapes) {
4246 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;
4247 if(flat_bits <= best_partition_bits) {
4248 raw_bits[partition] = raw_bits_per_partition[partition];
4249 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
4250 best_partition_bits = flat_bits;
4251 }
Josh Coalson2051dd42001-04-12 22:22:34 +00004252 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004253 parameters[partition] = best_rice_parameter;
4254 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004255 }
4256 }
4257
4258 *bits = bits_;
4259 return true;
4260}
Josh Coalson859bc542001-03-27 22:22:27 +00004261
Josh Coalsonf1eff452002-07-31 07:05:33 +00004262unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00004263{
4264 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00004265 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00004266
4267 for(i = 0; i < samples && !(x&1); i++)
4268 x |= signal[i];
4269
4270 if(x == 0) {
4271 shift = 0;
4272 }
4273 else {
4274 for(shift = 0; !(x&1); shift++)
4275 x >>= 1;
4276 }
4277
4278 if(shift > 0) {
4279 for(i = 0; i < samples; i++)
4280 signal[i] >>= shift;
4281 }
4282
4283 return shift;
4284}
Josh Coalsond86e03b2002-08-03 21:56:15 +00004285
4286void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4287{
4288 unsigned channel;
4289
4290 for(channel = 0; channel < channels; channel++)
4291 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4292
4293 fifo->tail += wide_samples;
4294
4295 FLAC__ASSERT(fifo->tail <= fifo->size);
4296}
4297
4298void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
4299{
4300 unsigned channel;
4301 unsigned sample, wide_sample;
4302 unsigned tail = fifo->tail;
4303
4304 sample = input_offset * channels;
4305 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4306 for(channel = 0; channel < channels; channel++)
4307 fifo->data[channel][tail] = input[sample++];
4308 tail++;
4309 }
4310 fifo->tail = tail;
4311
4312 FLAC__ASSERT(fifo->tail <= fifo->size);
4313}
4314
4315FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
4316{
4317 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4318 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
4319 (void)decoder;
4320
4321 if(encoder->private_->verify.needs_magic_hack) {
4322 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4323 *bytes = FLAC__STREAM_SYNC_LENGTH;
4324 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4325 encoder->private_->verify.needs_magic_hack = false;
4326 }
4327 else {
4328 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00004329 /*
4330 * If we get here, a FIFO underflow has occurred,
4331 * which means there is a bug somewhere.
4332 */
4333 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004334 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4335 }
4336 else if(encoded_bytes < *bytes)
4337 *bytes = encoded_bytes;
4338 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4339 encoder->private_->verify.output.data += *bytes;
4340 encoder->private_->verify.output.bytes -= *bytes;
4341 }
4342
4343 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4344}
4345
4346FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4347{
4348 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
4349 unsigned channel;
4350 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
4351 const unsigned blocksize = frame->header.blocksize;
4352 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
4353
4354 for(channel = 0; channel < channels; channel++) {
4355 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
4356 unsigned i, sample = 0;
4357 FLAC__int32 expect = 0, got = 0;
4358
4359 for(i = 0; i < blocksize; i++) {
4360 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4361 sample = i;
4362 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4363 got = (FLAC__int32)buffer[channel][i];
4364 break;
4365 }
4366 }
4367 FLAC__ASSERT(i < blocksize);
4368 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4369 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00004370 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004371 encoder->private_->verify.error_stats.channel = channel;
4372 encoder->private_->verify.error_stats.sample = sample;
4373 encoder->private_->verify.error_stats.expected = expect;
4374 encoder->private_->verify.error_stats.got = got;
4375 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4376 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4377 }
4378 }
4379 /* dequeue the frame from the fifo */
4380 for(channel = 0; channel < channels; channel++) {
4381 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
4382 }
4383 encoder->private_->verify.input_fifo.tail -= blocksize;
4384 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4385}
4386
4387void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4388{
4389 (void)decoder, (void)metadata, (void)client_data;
4390}
4391
4392void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4393{
4394 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4395 (void)decoder, (void)status;
4396 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4397}
Josh Coalson6b21f662006-09-13 01:42:27 +00004398
Josh Coalson8da98c82006-10-15 04:24:05 +00004399FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
4400{
4401 (void)client_data;
4402
4403 *bytes = (unsigned)fread(buffer, 1, *bytes, encoder->private_->file);
4404 if (*bytes == 0) {
4405 if (feof(encoder->private_->file))
4406 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4407 else if (ferror(encoder->private_->file))
4408 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4409 }
4410 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4411}
4412
Josh Coalson6b21f662006-09-13 01:42:27 +00004413FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4414{
4415 (void)client_data;
4416
4417 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4418 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4419 else
4420 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4421}
4422
4423FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4424{
4425 off_t offset;
4426
4427 (void)client_data;
4428
4429 offset = ftello(encoder->private_->file);
4430
4431 if(offset < 0) {
4432 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4433 }
4434 else {
4435 *absolute_byte_offset = (FLAC__uint64)offset;
4436 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4437 }
4438}
4439
4440#ifdef FLAC__VALGRIND_TESTING
4441static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4442{
4443 size_t ret = fwrite(ptr, size, nmemb, stream);
4444 if(!ferror(stream))
4445 fflush(stream);
4446 return ret;
4447}
4448#else
4449#define local__fwrite fwrite
4450#endif
4451
4452FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
4453{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004454 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00004455
4456 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson8da98c82006-10-15 04:24:05 +00004457 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4458#if FLAC__HAS_OGG
4459 /* We would like to be able to use 'samples > 0' in the
4460 * clause here but currently because of the nature of our
4461 * Ogg writing implementation, 'samples' is always 0 (see
4462 * ogg_encoder_aspect.c). The downside is extra progress
4463 * callbacks.
4464 */
4465 encoder->private_->is_ogg? true :
4466#endif
4467 samples > 0
4468 );
4469 if(call_it) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004470 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4471 * because at this point in the callback chain, the stats
4472 * have not been updated. Only after we return and control
4473 * gets back to write_frame_() are the stats updated
4474 */
4475 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);
4476 }
Josh Coalson6b21f662006-09-13 01:42:27 +00004477 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4478 }
4479 else
4480 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4481}
4482
4483/*
4484 * This will forcibly set stdout to binary mode (for OSes that require it)
4485 */
4486FILE *get_binary_stdout_()
4487{
4488 /* if something breaks here it is probably due to the presence or
4489 * absence of an underscore before the identifiers 'setmode',
4490 * 'fileno', and/or 'O_BINARY'; check your system header files.
4491 */
4492#if defined _MSC_VER || defined __MINGW32__
4493 _setmode(_fileno(stdout), _O_BINARY);
4494#elif defined __CYGWIN__
4495 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4496 setmode(_fileno(stdout), _O_BINARY);
4497#elif defined __EMX__
4498 setmode(fileno(stdout), O_BINARY);
4499#endif
4500
4501 return stdout;
4502}