blob: 6cc7f5b5684dc6203435914b236752d70350f29d [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 Coalsonbf0f52c2006-04-25 06:38:43 +000032/*@@@@@@*/
Josh Coalson81cbb8e2006-05-01 06:17:24 +000033#undef WINDOW_DEBUG_OUTPUT
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000034
Josh Coalsonb1ec7962006-05-24 04:41:36 +000035#if HAVE_CONFIG_H
36# include <config.h>
37#endif
38
Josh Coalson6b21f662006-09-13 01:42:27 +000039#if defined _MSC_VER || defined __MINGW32__
40#include <io.h> /* for _setmode() */
41#include <fcntl.h> /* for _O_BINARY */
42#endif
43#if defined __CYGWIN__ || defined __EMX__
44#include <io.h> /* for setmode(), O_BINARY */
45#include <fcntl.h> /* for _O_BINARY */
46#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +000047#include <limits.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000048#include <stdio.h>
49#include <stdlib.h> /* for malloc() */
50#include <string.h> /* for memcpy() */
Josh Coalson6b21f662006-09-13 01:42:27 +000051#include <sys/types.h> /* for off_t */
52#if defined _MSC_VER || defined __MINGW32__
53/*@@@ [2G limit] hacks for MSVC6 */
54#define fseeko fseek
55#define ftello ftell
56#endif
Josh Coalson1b689822001-05-31 20:11:02 +000057#include "FLAC/assert.h"
Josh Coalsond86e03b2002-08-03 21:56:15 +000058#include "FLAC/stream_decoder.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000059#include "protected/stream_encoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000060#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000061#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000062#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000063#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000064#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000065#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000066#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000067#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000068#include "private/memory.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000069#include "private/stream_encoder_framing.h"
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000070#include "private/window.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000071
72#ifdef min
73#undef min
74#endif
75#define min(x,y) ((x)<(y)?(x):(y))
76
77#ifdef max
78#undef max
79#endif
80#define max(x,y) ((x)>(y)?(x):(y))
81
Josh Coalsond86e03b2002-08-03 21:56:15 +000082typedef struct {
83 FLAC__int32 *data[FLAC__MAX_CHANNELS];
84 unsigned size; /* of each data[] in samples */
85 unsigned tail;
86} verify_input_fifo;
87
88typedef struct {
89 const FLAC__byte *data;
90 unsigned capacity;
91 unsigned bytes;
92} verify_output;
93
94typedef enum {
95 ENCODER_IN_MAGIC = 0,
96 ENCODER_IN_METADATA = 1,
97 ENCODER_IN_AUDIO = 2
98} EncoderStateHint;
99
Josh Coalson0a15c142001-06-13 17:59:57 +0000100/***********************************************************************
101 *
102 * Private class method prototypes
103 *
104 ***********************************************************************/
105
Josh Coalsonf1eff452002-07-31 07:05:33 +0000106static void set_defaults_(FLAC__StreamEncoder *encoder);
107static void free_(FLAC__StreamEncoder *encoder);
108static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
Josh Coalsond86e03b2002-08-03 21:56:15 +0000109static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
Josh Coalson6b21f662006-09-13 01:42:27 +0000110static FLAC__StreamEncoderWriteStatus write_frame_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples);
111static void update_metadata_(const FLAC__StreamEncoder *encoder);
Josh Coalsonf1eff452002-07-31 07:05:33 +0000112static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
113static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000114
115static FLAC__bool process_subframe_(
116 FLAC__StreamEncoder *encoder,
117 unsigned min_partition_order,
118 unsigned max_partition_order,
119 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000120 const FLAC__FrameHeader *frame_header,
121 unsigned subframe_bps,
122 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000123#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000124 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000125#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000126 FLAC__Subframe *subframe[2],
127 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
128 FLAC__int32 *residual[2],
129 unsigned *best_subframe,
130 unsigned *best_bits
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000131#ifdef WINDOW_DEBUG_OUTPUT
132 ,unsigned subframe_number
133#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000134);
135
136static FLAC__bool add_subframe_(
137 FLAC__StreamEncoder *encoder,
138 const FLAC__FrameHeader *frame_header,
139 unsigned subframe_bps,
140 const FLAC__Subframe *subframe,
141 FLAC__BitBuffer *frame
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000142#ifdef WINDOW_DEBUG_OUTPUT
143,unsigned subframe_bits
144#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000145);
146
147static unsigned evaluate_constant_subframe_(
148 const FLAC__int32 signal,
149 unsigned subframe_bps,
150 FLAC__Subframe *subframe
151);
152
153static unsigned evaluate_fixed_subframe_(
154 FLAC__StreamEncoder *encoder,
155 const FLAC__int32 signal[],
156 FLAC__int32 residual[],
157 FLAC__uint32 abs_residual[],
158 FLAC__uint64 abs_residual_partition_sums[],
159 unsigned raw_bits_per_partition[],
160 unsigned blocksize,
161 unsigned subframe_bps,
162 unsigned order,
163 unsigned rice_parameter,
164 unsigned min_partition_order,
165 unsigned max_partition_order,
166 FLAC__bool precompute_partition_sums,
167 FLAC__bool do_escape_coding,
168 unsigned rice_parameter_search_dist,
169 FLAC__Subframe *subframe,
170 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
171);
172
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000173#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000174static unsigned evaluate_lpc_subframe_(
175 FLAC__StreamEncoder *encoder,
176 const FLAC__int32 signal[],
177 FLAC__int32 residual[],
178 FLAC__uint32 abs_residual[],
179 FLAC__uint64 abs_residual_partition_sums[],
180 unsigned raw_bits_per_partition[],
181 const FLAC__real lp_coeff[],
182 unsigned blocksize,
183 unsigned subframe_bps,
184 unsigned order,
185 unsigned qlp_coeff_precision,
186 unsigned rice_parameter,
187 unsigned min_partition_order,
188 unsigned max_partition_order,
189 FLAC__bool precompute_partition_sums,
190 FLAC__bool do_escape_coding,
191 unsigned rice_parameter_search_dist,
192 FLAC__Subframe *subframe,
193 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000194#ifdef WINDOW_DEBUG_OUTPUT
195 ,unsigned frame_number
196 ,unsigned subframe_number
197 ,FLAC__ApodizationSpecification aspec
198#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000199);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000200#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000201
202static unsigned evaluate_verbatim_subframe_(
203 const FLAC__int32 signal[],
204 unsigned blocksize,
205 unsigned subframe_bps,
206 FLAC__Subframe *subframe
207);
208
209static unsigned find_best_partition_order_(
210 struct FLAC__StreamEncoderPrivate *private_,
211 const FLAC__int32 residual[],
212 FLAC__uint32 abs_residual[],
213 FLAC__uint64 abs_residual_partition_sums[],
214 unsigned raw_bits_per_partition[],
215 unsigned residual_samples,
216 unsigned predictor_order,
217 unsigned rice_parameter,
218 unsigned min_partition_order,
219 unsigned max_partition_order,
220 FLAC__bool precompute_partition_sums,
221 FLAC__bool do_escape_coding,
222 unsigned rice_parameter_search_dist,
223 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
224);
225
226static void precompute_partition_info_sums_(
227 const FLAC__uint32 abs_residual[],
228 FLAC__uint64 abs_residual_partition_sums[],
229 unsigned residual_samples,
230 unsigned predictor_order,
231 unsigned min_partition_order,
232 unsigned max_partition_order
233);
234
235static void precompute_partition_info_escapes_(
236 const FLAC__int32 residual[],
237 unsigned raw_bits_per_partition[],
238 unsigned residual_samples,
239 unsigned predictor_order,
240 unsigned min_partition_order,
241 unsigned max_partition_order
242);
243
Josh Coalson8395d022001-07-12 21:25:22 +0000244#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000245static FLAC__bool set_partitioned_rice_(
246 const FLAC__uint32 abs_residual[],
247 const FLAC__int32 residual[],
248 const unsigned residual_samples,
249 const unsigned predictor_order,
250 const unsigned suggested_rice_parameter,
251 const unsigned rice_parameter_search_dist,
252 const unsigned partition_order,
253 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
254 unsigned *bits
255);
256
257static FLAC__bool set_partitioned_rice_with_precompute_(
258 const FLAC__int32 residual[],
259 const FLAC__uint64 abs_residual_partition_sums[],
260 const unsigned raw_bits_per_partition[],
261 const unsigned residual_samples,
262 const unsigned predictor_order,
263 const unsigned suggested_rice_parameter,
264 const unsigned rice_parameter_search_dist,
265 const unsigned partition_order,
266 const FLAC__bool search_for_escapes,
267 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
268 unsigned *bits
269);
Josh Coalson8395d022001-07-12 21:25:22 +0000270#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000271static FLAC__bool set_partitioned_rice_(
272 const FLAC__uint32 abs_residual[],
273 const unsigned residual_samples,
274 const unsigned predictor_order,
275 const unsigned suggested_rice_parameter,
276 const unsigned rice_parameter_search_dist,
277 const unsigned partition_order,
278 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
279 unsigned *bits
280);
281
282static FLAC__bool set_partitioned_rice_with_precompute_(
283 const FLAC__uint32 abs_residual[],
284 const FLAC__uint64 abs_residual_partition_sums[],
285 const unsigned raw_bits_per_partition[],
286 const unsigned residual_samples,
287 const unsigned predictor_order,
288 const unsigned suggested_rice_parameter,
289 const unsigned rice_parameter_search_dist,
290 const unsigned partition_order,
291 const FLAC__bool search_for_escapes,
292 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
293 unsigned *bits
294);
Josh Coalson0a15c142001-06-13 17:59:57 +0000295#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000296
Josh Coalsonf1eff452002-07-31 07:05:33 +0000297static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000298
Josh Coalsond86e03b2002-08-03 21:56:15 +0000299/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000300static void append_to_verify_fifo_(
301 verify_input_fifo *fifo,
302 const FLAC__int32 * const input[],
303 unsigned input_offset,
304 unsigned channels,
305 unsigned wide_samples
306);
307
308static void append_to_verify_fifo_interleaved_(
309 verify_input_fifo *fifo,
310 const FLAC__int32 input[],
311 unsigned input_offset,
312 unsigned channels,
313 unsigned wide_samples
314);
315
Josh Coalson6b21f662006-09-13 01:42:27 +0000316static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
317static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
318static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
319static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000320
Josh Coalson6b21f662006-09-13 01:42:27 +0000321static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
322static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
323static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
324static FILE *get_binary_stdout_();
Josh Coalson6fe72f72002-08-20 04:01:59 +0000325
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000326#ifdef WINDOW_DEBUG_OUTPUT
327static const char * const winstr[] = {
328 "bartlett",
329 "bartlett_hann",
330 "blackman",
331 "blackman_harris_4term_92db_sidelobe",
332 "connes",
333 "flattop",
334 "gauss",
335 "hamming",
336 "hann",
337 "kaiser_bessel",
338 "nuttall",
339 "rectangular",
340 "triangle",
341 "tukey",
342 "welch"
343};
344#endif
Josh Coalson0a15c142001-06-13 17:59:57 +0000345
346/***********************************************************************
347 *
348 * Private class data
349 *
350 ***********************************************************************/
351
352typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000353 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000354 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
355 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000356#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000357 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
358 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000359 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
360 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000361#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000362 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
363 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
Josh Coalson77e3f312001-06-23 03:03:24 +0000364 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
365 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000366 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
367 FLAC__Subframe subframe_workspace_mid_side[2][2];
368 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
369 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000370 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
371 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
372 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
373 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000374 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000375 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000376 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000377 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000378 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000379 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
Josh Coalson8395d022001-07-12 21:25:22 +0000380 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
Josh Coalsonaec256b2002-03-12 16:19:54 +0000381 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000382 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
383 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000384 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalson6b21f662006-09-13 01:42:27 +0000385 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
386 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000387 unsigned current_sample_number;
388 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000389 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000390 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000391#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000392 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000393#else
394 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
395#endif
396#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000397 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
Josh Coalson7446e182005-01-26 04:04:38 +0000398 void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
399 void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
400 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000401#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000402 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
403 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
404 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
405 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +0000406 FLAC__bool disable_constant_subframes;
407 FLAC__bool disable_fixed_subframes;
408 FLAC__bool disable_verbatim_subframes;
Josh Coalson6b21f662006-09-13 01:42:27 +0000409 FLAC__StreamEncoderSeekCallback seek_callback;
410 FLAC__StreamEncoderTellCallback tell_callback;
Josh Coalson681c2932002-08-01 08:19:37 +0000411 FLAC__StreamEncoderWriteCallback write_callback;
412 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000413 FLAC__StreamEncoderProgressCallback progress_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000414 void *client_data;
Josh Coalson6b21f662006-09-13 01:42:27 +0000415 unsigned first_seekpoint_to_check;
416 FILE *file; /* only used when encoding to a file */
417 FLAC__uint64 bytes_written;
418 FLAC__uint64 samples_written;
419 unsigned frames_written;
420 unsigned total_frames_estimate;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000421 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000422 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
423 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000424#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000425 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
426 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000427 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
428 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000429#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000430 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
431 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
432 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000433 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000434 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000435 /*
436 * These fields have been moved here from private function local
437 * declarations merely to save stack space during encoding.
438 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000439#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000440 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000441#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000442 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000443 /*
444 * The data for the verify section
445 */
446 struct {
447 FLAC__StreamDecoder *decoder;
448 EncoderStateHint state_hint;
449 FLAC__bool needs_magic_hack;
450 verify_input_fifo input_fifo;
451 verify_output output;
452 struct {
453 FLAC__uint64 absolute_sample;
454 unsigned frame_number;
455 unsigned channel;
456 unsigned sample;
457 FLAC__int32 expected;
458 FLAC__int32 got;
459 } error_stats;
460 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000461 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000462} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000463
Josh Coalson0a15c142001-06-13 17:59:57 +0000464/***********************************************************************
465 *
466 * Public static class data
467 *
468 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000469
Josh Coalson6afed9f2002-10-16 22:29:47 +0000470FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000471 "FLAC__STREAM_ENCODER_OK",
Josh Coalson6b21f662006-09-13 01:42:27 +0000472 "FLAC__STREAM_ENCODER_UNINITIALIZED",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000473 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
474 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson6b21f662006-09-13 01:42:27 +0000475 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
476 "FLAC__STREAM_ENCODER_IO_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000477 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson6b21f662006-09-13 01:42:27 +0000478 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
479};
480
481FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
482 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
483 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
484 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
485 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
486 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
487 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
488 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
489 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
490 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
491 "FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH",
492 "FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_SAMPLE_SIZE_MISMATCH",
493 "FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE",
494 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
495 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
496 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
497 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000498};
499
Josh Coalson6afed9f2002-10-16 22:29:47 +0000500FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000501 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
502 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000503};
504
Josh Coalson6b21f662006-09-13 01:42:27 +0000505FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
506 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
507 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
508 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
509};
510
511FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
512 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
513 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
514 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
515};
516
Josh Coalson0a15c142001-06-13 17:59:57 +0000517/***********************************************************************
518 *
519 * Class constructor/destructor
520 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000521 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000522FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000523{
Josh Coalson0a15c142001-06-13 17:59:57 +0000524 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000525 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000526
Josh Coalson0a15c142001-06-13 17:59:57 +0000527 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000528
Josh Coalsonea7155f2002-10-18 05:49:19 +0000529 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000530 if(encoder == 0) {
531 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000532 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000533
Josh Coalsonea7155f2002-10-18 05:49:19 +0000534 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000535 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000536 free(encoder);
537 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000538 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000539
Josh Coalsonea7155f2002-10-18 05:49:19 +0000540 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000541 if(encoder->private_ == 0) {
542 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000543 free(encoder);
544 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000545 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000546
Josh Coalsonaec256b2002-03-12 16:19:54 +0000547 encoder->private_->frame = FLAC__bitbuffer_new();
548 if(encoder->private_->frame == 0) {
549 free(encoder->private_);
550 free(encoder->protected_);
551 free(encoder);
552 return 0;
553 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000554
Josh Coalson6b21f662006-09-13 01:42:27 +0000555 encoder->private_->file = 0;
556
Josh Coalsonf1eff452002-07-31 07:05:33 +0000557 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000558
Josh Coalson3262b0d2002-08-14 20:58:42 +0000559 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000560
561 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
562 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
563 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
564 }
565 for(i = 0; i < 2; i++) {
566 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
567 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
568 }
569 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000570 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
571 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[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 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
575 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 +0000576 }
577
578 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000579 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
580 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000581 }
582 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000583 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
584 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 +0000585 }
586 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000587 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000588
Josh Coalsonfa697a92001-08-16 20:07:29 +0000589 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000590
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000591 return encoder;
592}
593
Josh Coalson6afed9f2002-10-16 22:29:47 +0000594FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000595{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000596 unsigned i;
597
Josh Coalsonf1eff452002-07-31 07:05:33 +0000598 FLAC__ASSERT(0 != encoder);
599 FLAC__ASSERT(0 != encoder->protected_);
600 FLAC__ASSERT(0 != encoder->private_);
601 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000602
Josh Coalson3262b0d2002-08-14 20:58:42 +0000603 encoder->private_->is_being_deleted = true;
604
605 FLAC__stream_encoder_finish(encoder);
606
Josh Coalson4fa90592002-12-04 07:01:37 +0000607 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000608 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000609
610 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000611 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
612 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000613 }
614 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000615 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
616 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 +0000617 }
618 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000619 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000620
Josh Coalsonaec256b2002-03-12 16:19:54 +0000621 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000622 free(encoder->private_);
623 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000624 free(encoder);
625}
626
Josh Coalson0a15c142001-06-13 17:59:57 +0000627/***********************************************************************
628 *
629 * Public class methods
630 *
631 ***********************************************************************/
632
Josh Coalson6b21f662006-09-13 01:42:27 +0000633FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000634{
635 unsigned i;
Josh Coalson3957c472006-09-24 16:25:42 +0000636 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000637
Josh Coalsonf1eff452002-07-31 07:05:33 +0000638 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000639
Josh Coalsonfa697a92001-08-16 20:07:29 +0000640 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000641 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000642
Josh Coalson6b21f662006-09-13 01:42:27 +0000643 if(0 == write_callback || (seek_callback && 0 == tell_callback))
644 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000645
Josh Coalsonfa697a92001-08-16 20:07:29 +0000646 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000647 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000648
Josh Coalsonfa697a92001-08-16 20:07:29 +0000649 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
Josh Coalson6b21f662006-09-13 01:42:27 +0000650 return FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000651
Josh Coalsonfa697a92001-08-16 20:07:29 +0000652 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
Josh Coalson6b21f662006-09-13 01:42:27 +0000653 return FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000654
Josh Coalsonfa697a92001-08-16 20:07:29 +0000655 if(encoder->protected_->bits_per_sample >= 32)
Josh Coalson6b21f662006-09-13 01:42:27 +0000656 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 +0000657
Josh Coalson76c68bc2002-05-17 06:22:02 +0000658 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 +0000659 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000660
Josh Coalson0833f342002-07-15 05:31:55 +0000661 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000662 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000663
Josh Coalsonfa697a92001-08-16 20:07:29 +0000664 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000665 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000666
Josh Coalson20ac2c12002-08-30 05:47:14 +0000667 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000668 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000669
Josh Coalsonfa697a92001-08-16 20:07:29 +0000670 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000671 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000672
Josh Coalsonfa697a92001-08-16 20:07:29 +0000673 if(encoder->protected_->qlp_coeff_precision == 0) {
674 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000675 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
676 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000677 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 +0000678 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000679 else if(encoder->protected_->bits_per_sample == 16) {
680 if(encoder->protected_->blocksize <= 192)
681 encoder->protected_->qlp_coeff_precision = 7;
682 else if(encoder->protected_->blocksize <= 384)
683 encoder->protected_->qlp_coeff_precision = 8;
684 else if(encoder->protected_->blocksize <= 576)
685 encoder->protected_->qlp_coeff_precision = 9;
686 else if(encoder->protected_->blocksize <= 1152)
687 encoder->protected_->qlp_coeff_precision = 10;
688 else if(encoder->protected_->blocksize <= 2304)
689 encoder->protected_->qlp_coeff_precision = 11;
690 else if(encoder->protected_->blocksize <= 4608)
691 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000692 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000693 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000694 }
695 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000696 if(encoder->protected_->blocksize <= 384)
697 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
698 else if(encoder->protected_->blocksize <= 1152)
699 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
700 else
701 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000702 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000703 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000704 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000705 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 +0000706 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000707
Josh Coalsonfa697a92001-08-16 20:07:29 +0000708 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000709 if(
710 encoder->protected_->blocksize != 192 &&
711 encoder->protected_->blocksize != 576 &&
712 encoder->protected_->blocksize != 1152 &&
713 encoder->protected_->blocksize != 2304 &&
714 encoder->protected_->blocksize != 4608 &&
715 encoder->protected_->blocksize != 256 &&
716 encoder->protected_->blocksize != 512 &&
717 encoder->protected_->blocksize != 1024 &&
718 encoder->protected_->blocksize != 2048 &&
719 encoder->protected_->blocksize != 4096 &&
720 encoder->protected_->blocksize != 8192 &&
721 encoder->protected_->blocksize != 16384
722 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000723 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000724 if(
725 encoder->protected_->sample_rate != 8000 &&
726 encoder->protected_->sample_rate != 16000 &&
727 encoder->protected_->sample_rate != 22050 &&
728 encoder->protected_->sample_rate != 24000 &&
729 encoder->protected_->sample_rate != 32000 &&
730 encoder->protected_->sample_rate != 44100 &&
731 encoder->protected_->sample_rate != 48000 &&
732 encoder->protected_->sample_rate != 96000
733 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000734 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000735 if(
736 encoder->protected_->bits_per_sample != 8 &&
737 encoder->protected_->bits_per_sample != 12 &&
738 encoder->protected_->bits_per_sample != 16 &&
739 encoder->protected_->bits_per_sample != 20 &&
740 encoder->protected_->bits_per_sample != 24
741 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000742 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000743 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000744 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000745 }
746
Josh Coalsonfa697a92001-08-16 20:07:29 +0000747 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
748 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
749 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
750 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000751
Josh Coalson66075c12002-06-01 05:39:38 +0000752 /* validate metadata */
753 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000754 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000755 metadata_has_seektable = false;
756 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000757 metadata_picture_has_type1 = false;
758 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000759 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000760 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
761 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000762 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000763 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000764 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000765 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000766 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000767 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000768 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000769 }
Josh Coalson3957c472006-09-24 16:25:42 +0000770 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000771 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000772 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000773 metadata_has_vorbis_comment = true;
774 }
Josh Coalson3957c472006-09-24 16:25:42 +0000775 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
776 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 +0000777 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000778 }
Josh Coalson3957c472006-09-24 16:25:42 +0000779 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
780 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000781 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000782 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
783 if(metadata_picture_has_type1) /* there should only be 1 per stream */
784 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
785 metadata_picture_has_type1 = true;
786 /* standard icon must be 32x32 pixel PNG */
787 if(
788 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
789 (
790 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
791 m->data.picture.width != 32 ||
792 m->data.picture.height != 32
793 )
794 )
795 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
796 }
797 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
798 if(metadata_picture_has_type2) /* there should only be 1 per stream */
799 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
800 metadata_picture_has_type2 = true;
801 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000802 }
Josh Coalson66075c12002-06-01 05:39:38 +0000803 }
804
Josh Coalsonfa697a92001-08-16 20:07:29 +0000805 encoder->private_->input_capacity = 0;
806 for(i = 0; i < encoder->protected_->channels; i++) {
807 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000808#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000809 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000810#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000811 }
812 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000813 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000814#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000815 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000816#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000817 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000818#ifndef FLAC__INTEGER_ONLY_LIBRARY
819 for(i = 0; i < encoder->protected_->num_apodizations; i++)
820 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
821 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
822#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000823 for(i = 0; i < encoder->protected_->channels; i++) {
824 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
825 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
826 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000827 }
828 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000829 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
830 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
831 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000832 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000833 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
834 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
835 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000836#ifndef FLAC__INTEGER_ONLY_LIBRARY
837 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
838#else
839 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
840 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
841 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
842 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
843 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
844 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
845 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);
846#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000847 if(encoder->private_->loose_mid_side_stereo_frames == 0)
848 encoder->private_->loose_mid_side_stereo_frames = 1;
849 encoder->private_->loose_mid_side_stereo_frame_count = 0;
850 encoder->private_->current_sample_number = 0;
851 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000852
Josh Coalsonfa697a92001-08-16 20:07:29 +0000853 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
854 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? */
855 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000856
Josh Coalsoncf30f502001-05-23 20:57:44 +0000857 /*
858 * get the CPU info and set the function pointers
859 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000860 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000861 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000862#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000863 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000864#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000865 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000866#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000867 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000868 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 +0000869 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000870#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000871 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000872#ifndef FLAC__INTEGER_ONLY_LIBRARY
873# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000874 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000875# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000876 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000877# ifdef FLAC__HAS_NASM
878# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000879 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000880 if(encoder->protected_->max_lpc_order < 4)
881 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
882 else if(encoder->protected_->max_lpc_order < 8)
883 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
884 else if(encoder->protected_->max_lpc_order < 12)
885 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000886 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000887 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000888 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000889 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000890# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000891 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000892 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000893 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000894 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000895 if(encoder->private_->cpuinfo.data.ia32.mmx) {
896 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
897 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 +0000898 }
899 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000900 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
901 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 +0000902 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000903 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
904 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
905# endif /* FLAC__HAS_NASM */
906# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000907 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000908# endif /* !FLAC__NO_ASM */
909#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000910 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000911 if(encoder->private_->use_wide_by_block) {
912 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000913 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000914
Josh Coalson8395d022001-07-12 21:25:22 +0000915 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000916 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 +0000917
Josh Coalson6b21f662006-09-13 01:42:27 +0000918 /* set state to OK; from here on, errors are fatal and we'll override the state then */
919 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
920
921 encoder->private_->write_callback = write_callback;
922 encoder->private_->seek_callback = seek_callback;
923 encoder->private_->tell_callback = tell_callback;
924 encoder->private_->metadata_callback = metadata_callback;
925 encoder->private_->client_data = client_data;
926
Josh Coalsonf1eff452002-07-31 07:05:33 +0000927 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000928 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +0000929 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000930 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000931
Josh Coalson6b21f662006-09-13 01:42:27 +0000932 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
933 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
934 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
935 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000936
937 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000938 * Set up the verify stuff if necessary
939 */
940 if(encoder->protected_->verify) {
941 /*
942 * First, set up the fifo which will hold the
943 * original signal to compare against
944 */
945 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
946 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalson6b21f662006-09-13 01:42:27 +0000947 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
948 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
949 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
950 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000951 }
952 encoder->private_->verify.input_fifo.tail = 0;
953
954 /*
955 * Now set up a stream decoder for verification
956 */
957 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
Josh Coalson6b21f662006-09-13 01:42:27 +0000958 if(0 == encoder->private_->verify.decoder) {
959 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
960 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
961 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000962
Josh Coalson6b21f662006-09-13 01:42:27 +0000963 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) {
964 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
965 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
966 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000967 }
Josh Coalson589f8c72002-08-07 23:54:55 +0000968 encoder->private_->verify.error_stats.absolute_sample = 0;
969 encoder->private_->verify.error_stats.frame_number = 0;
970 encoder->private_->verify.error_stats.channel = 0;
971 encoder->private_->verify.error_stats.sample = 0;
972 encoder->private_->verify.error_stats.expected = 0;
973 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000974
975 /*
Josh Coalson6b21f662006-09-13 01:42:27 +0000976 * These must be done before we write any metadata, because that
977 * calls the write_callback, which uses these values.
978 */
979 encoder->private_->first_seekpoint_to_check = 0;
980 encoder->private_->samples_written = 0;
981 encoder->protected_->streaminfo_offset = 0;
982 encoder->protected_->seektable_offset = 0;
983 encoder->protected_->audio_offset = 0;
984
985 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000986 * write the stream header
987 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000988 if(encoder->protected_->verify)
989 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson6b21f662006-09-13 01:42:27 +0000990 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
991 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
992 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
993 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000994 if(!write_bitbuffer_(encoder, 0)) {
995 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +0000996 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000997 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000998
Josh Coalson5c491a12002-08-01 06:39:40 +0000999 /*
1000 * write the STREAMINFO metadata block
1001 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001002 if(encoder->protected_->verify)
1003 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +00001004 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1005 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1006 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1007 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1008 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1009 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1010 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1011 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1012 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1013 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1014 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1015 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 +00001016 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +00001017 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1018 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1019 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1020 }
1021 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1022 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1023 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1024 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001025 if(!write_bitbuffer_(encoder, 0)) {
1026 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001027 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001028 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001029
Josh Coalson5c491a12002-08-01 06:39:40 +00001030 /*
1031 * Now that the STREAMINFO block is written, we can init this to an
1032 * absurdly-high value...
1033 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001034 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 +00001035 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001036 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001037
Josh Coalson5c491a12002-08-01 06:39:40 +00001038 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001039 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1040 * if not, we will write an empty one (FLAC__add_metadata_block()
1041 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001042 *
1043 * WATCHOUT: libOggFLAC depends on us to write this block after the
1044 * STREAMINFO since that's what the mapping requires. (In the case
Josh Coalson8ddf7fb2004-12-30 00:58:50 +00001045 * that metadata_has_vorbis_comment is true it will have already
Josh Coalson69cfda72004-09-10 00:38:21 +00001046 * insured that the metadata list is properly ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001047 */
1048 if(!metadata_has_vorbis_comment) {
1049 FLAC__StreamMetadata vorbis_comment;
1050 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1051 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1052 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1053 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1054 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1055 vorbis_comment.data.vorbis_comment.num_comments = 0;
1056 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001057 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1058 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1059 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1060 }
1061 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1062 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1063 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1064 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001065 if(!write_bitbuffer_(encoder, 0)) {
1066 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001067 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001068 }
1069 }
1070
1071 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001072 * write the user's metadata blocks
1073 */
1074 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1075 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001076 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1077 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1078 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1079 }
1080 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1081 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1082 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1083 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001084 if(!write_bitbuffer_(encoder, 0)) {
1085 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001086 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001087 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001088 }
1089
Josh Coalson6b21f662006-09-13 01:42:27 +00001090 /* now that all the metadata is written, we save the stream offset */
1091 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 */
1092 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1093 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1094 }
1095
Josh Coalsond86e03b2002-08-03 21:56:15 +00001096 if(encoder->protected_->verify)
1097 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1098
Josh Coalson6b21f662006-09-13 01:42:27 +00001099 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1100}
1101
1102FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data)
1103{
1104 FLAC__StreamEncoderInitStatus init_status;
1105
1106 FLAC__ASSERT(0 != encoder);
1107 FLAC__ASSERT(0 != file);
1108
1109 /*
1110 * To make sure that our file does not go unclosed after an error, we
1111 * must assign the FILE pointer before any further error can occur in
1112 * this routine.
1113 */
1114 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1115 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1116
1117 /* double protection */
1118 if(file == 0) {
1119 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1120 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1121 }
1122
1123 if(file == stdout)
1124 file = get_binary_stdout_(); /* just to be safe */
1125
1126 encoder->private_->file = file;
1127
1128 encoder->private_->progress_callback = progress_callback;
1129 encoder->private_->bytes_written = 0;
1130 encoder->private_->samples_written = 0;
1131 encoder->private_->frames_written = 0;
1132
1133 init_status = FLAC__stream_encoder_init_stream(encoder, file_write_callback_, file_seek_callback_, file_tell_callback_, /*metadata_callback=*/0, client_data);
1134 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1135 /* the above function sets the state for us in case of an error */
1136 return init_status;
1137 }
1138
1139 {
1140 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1141
1142 FLAC__ASSERT(blocksize != 0);
1143 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1144 }
1145
1146 return init_status;
1147}
1148
1149FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data)
1150{
1151 FILE *file;
1152
1153 FLAC__ASSERT(0 != encoder);
1154
1155 /*
1156 * To make sure that our file does not go unclosed after an error, we
1157 * have to do the same entrance checks here that are later performed
1158 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1159 */
1160 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1161 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1162
1163 file = filename? fopen(filename, "w+b") : stdout;
1164
1165 if(file == 0) {
1166 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1167 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1168 }
1169
1170 return FLAC__stream_encoder_init_FILE(encoder, file, progress_callback, client_data);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001171}
1172
Josh Coalson6afed9f2002-10-16 22:29:47 +00001173FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001174{
Josh Coalsonf1eff452002-07-31 07:05:33 +00001175 FLAC__ASSERT(0 != encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001176 FLAC__ASSERT(0 != encoder->private_);
1177 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001178
Josh Coalsonfa697a92001-08-16 20:07:29 +00001179 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001180 return;
Josh Coalson2b245f22002-08-07 17:10:50 +00001181
Josh Coalson3262b0d2002-08-14 20:58:42 +00001182 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001183 if(encoder->private_->current_sample_number != 0) {
1184 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1185 process_frame_(encoder, true); /* true => is last frame */
1186 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001187 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001188
Josh Coalson6b21f662006-09-13 01:42:27 +00001189 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001190
Josh Coalson3262b0d2002-08-14 20:58:42 +00001191 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001192 if(encoder->private_->seek_callback)
1193 update_metadata_(encoder);
1194 if(encoder->private_->metadata_callback)
1195 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
Josh Coalson2b245f22002-08-07 17:10:50 +00001196 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001197
Josh Coalsond86e03b2002-08-03 21:56:15 +00001198 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
1199 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1200
Josh Coalson6b21f662006-09-13 01:42:27 +00001201 if(0 != encoder->private_->file) {
1202 if(encoder->private_->file != stdout)
1203 fclose(encoder->private_->file);
1204 encoder->private_->file = 0;
1205 }
1206
Josh Coalsonf1eff452002-07-31 07:05:33 +00001207 free_(encoder);
1208 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001209
Josh Coalsonfa697a92001-08-16 20:07:29 +00001210 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001211}
1212
Josh Coalson6afed9f2002-10-16 22:29:47 +00001213FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001214{
1215 FLAC__ASSERT(0 != encoder);
1216 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1217 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001218#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001219 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001220#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001221 return true;
1222}
1223
Josh Coalson6afed9f2002-10-16 22:29:47 +00001224FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001225{
Josh Coalson92031602002-07-24 06:02:11 +00001226 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001227 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001228 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001229 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001230 return true;
1231}
1232
Josh Coalson6afed9f2002-10-16 22:29:47 +00001233FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001234{
Josh Coalson92031602002-07-24 06:02:11 +00001235 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001236 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001237 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001238 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001239 return true;
1240}
1241
Josh Coalson6afed9f2002-10-16 22:29:47 +00001242FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001243{
Josh Coalson92031602002-07-24 06:02:11 +00001244 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001245 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001246 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001247 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001248 return true;
1249}
1250
Josh Coalson6afed9f2002-10-16 22:29:47 +00001251FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001252{
Josh Coalson92031602002-07-24 06:02:11 +00001253 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001254 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001255 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001256 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001257 return true;
1258}
1259
Josh Coalson6afed9f2002-10-16 22:29:47 +00001260FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001261{
Josh Coalson92031602002-07-24 06:02:11 +00001262 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001263 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001264 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001265 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001266 return true;
1267}
1268
Josh Coalson6afed9f2002-10-16 22:29:47 +00001269FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001270{
Josh Coalson92031602002-07-24 06:02:11 +00001271 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001272 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001273 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001274 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001275 return true;
1276}
1277
Josh Coalson6afed9f2002-10-16 22:29:47 +00001278FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001279{
Josh Coalson92031602002-07-24 06:02:11 +00001280 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001281 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001282 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001283 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001284 return true;
1285}
1286
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001287FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1288{
1289 FLAC__ASSERT(0 != encoder);
1290 FLAC__ASSERT(0 != specification);
1291 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1292 return false;
1293#ifdef FLAC__INTEGER_ONLY_LIBRARY
1294 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1295#else
1296 encoder->protected_->num_apodizations = 0;
1297 while(1) {
1298 const char *s = strchr(specification, ';');
1299 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1300 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1301 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1302 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1303 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1304 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1305 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1306 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1307 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1308 else if(n==6 && 0 == strncmp("connes" , specification, n))
1309 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1310 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1311 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1312 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1313 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1314 if (stddev > 0.0 && stddev <= 0.5) {
1315 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1316 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1317 }
1318 }
1319 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1320 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1321 else if(n==4 && 0 == strncmp("hann" , specification, n))
1322 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1323 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1324 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1325 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1326 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1327 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1328 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1329 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1330 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1331 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1332 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1333 if (p >= 0.0 && p <= 1.0) {
1334 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1335 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1336 }
1337 }
1338 else if(n==5 && 0 == strncmp("welch" , specification, n))
1339 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1340 if (encoder->protected_->num_apodizations == 32)
1341 break;
1342 if (s)
1343 specification = s+1;
1344 else
1345 break;
1346 }
1347 if(encoder->protected_->num_apodizations == 0) {
1348 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001349 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1350 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001351 }
1352#ifdef WINDOW_DEBUG_OUTPUT
1353{unsigned n;for(n=0;n<encoder->protected_->num_apodizations;n++)fprintf(stderr,"@@@@@@ parsed apodization[%zu]: %s\n",n,winstr[encoder->protected_->apodizations[n].type]);}
1354#endif
1355#endif
1356 return true;
1357}
1358
Josh Coalson6afed9f2002-10-16 22:29:47 +00001359FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001360{
Josh Coalson92031602002-07-24 06:02:11 +00001361 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001362 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001363 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001364 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001365 return true;
1366}
1367
Josh Coalson6afed9f2002-10-16 22:29:47 +00001368FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001369{
Josh Coalson92031602002-07-24 06:02:11 +00001370 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001371 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001372 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001373 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001374 return true;
1375}
1376
Josh Coalson6afed9f2002-10-16 22:29:47 +00001377FLAC_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 +00001378{
Josh Coalson92031602002-07-24 06:02:11 +00001379 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001380 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001381 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001382 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001383 return true;
1384}
1385
Josh Coalson6afed9f2002-10-16 22:29:47 +00001386FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001387{
Josh Coalson92031602002-07-24 06:02:11 +00001388 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001389 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001390 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001391#if 0
1392 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001393 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001394#else
1395 (void)value;
1396#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001397 return true;
1398}
1399
Josh Coalson6afed9f2002-10-16 22:29:47 +00001400FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001401{
Josh Coalson92031602002-07-24 06:02:11 +00001402 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001403 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001404 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001405 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001406 return true;
1407}
1408
Josh Coalson6afed9f2002-10-16 22:29:47 +00001409FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001410{
Josh Coalson92031602002-07-24 06:02:11 +00001411 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001412 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001413 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001414 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001415 return true;
1416}
1417
Josh Coalson6afed9f2002-10-16 22:29:47 +00001418FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001419{
Josh Coalson92031602002-07-24 06:02:11 +00001420 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001421 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001422 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001423 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001424 return true;
1425}
1426
Josh Coalson6afed9f2002-10-16 22:29:47 +00001427FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001428{
Josh Coalson92031602002-07-24 06:02:11 +00001429 FLAC__ASSERT(0 != encoder);
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 Coalson680e3aa2002-08-01 07:32:17 +00001432#if 0
1433 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001434 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001435#else
1436 (void)value;
1437#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001438 return true;
1439}
1440
Josh Coalson6afed9f2002-10-16 22:29:47 +00001441FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001442{
Josh Coalson92031602002-07-24 06:02:11 +00001443 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001444 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001445 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001446 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001447 return true;
1448}
1449
Josh Coalson6afed9f2002-10-16 22:29:47 +00001450FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001451{
Josh Coalson92031602002-07-24 06:02:11 +00001452 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001453 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001454 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001455 encoder->protected_->metadata = metadata;
1456 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson6b21f662006-09-13 01:42:27 +00001457 if(0 != metadata && num_blocks > 0) {
1458 unsigned i;
1459 for(i = 0; i < num_blocks; i++) {
1460 if(0 != metadata[i] && metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
1461 encoder->private_->seek_table = &metadata[i]->data.seek_table;
1462 break; /* take only the first one */
1463 }
1464 }
1465 }
Josh Coalson00e53872001-06-16 07:32:25 +00001466 return true;
1467}
1468
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001469/*
1470 * These three functions are not static, but not publically exposed in
1471 * include/FLAC/ either. They are used by the test suite.
1472 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001473FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001474{
1475 FLAC__ASSERT(0 != encoder);
1476 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1477 return false;
1478 encoder->private_->disable_constant_subframes = value;
1479 return true;
1480}
1481
Josh Coalson6afed9f2002-10-16 22:29:47 +00001482FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001483{
1484 FLAC__ASSERT(0 != encoder);
1485 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1486 return false;
1487 encoder->private_->disable_fixed_subframes = value;
1488 return true;
1489}
1490
Josh Coalson6afed9f2002-10-16 22:29:47 +00001491FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001492{
1493 FLAC__ASSERT(0 != encoder);
1494 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1495 return false;
1496 encoder->private_->disable_verbatim_subframes = value;
1497 return true;
1498}
1499
Josh Coalson6afed9f2002-10-16 22:29:47 +00001500FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001501{
Josh Coalson92031602002-07-24 06:02:11 +00001502 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001503 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001504}
1505
Josh Coalson6afed9f2002-10-16 22:29:47 +00001506FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001507{
1508 FLAC__ASSERT(0 != encoder);
1509 if(encoder->protected_->verify)
1510 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1511 else
1512 return FLAC__STREAM_DECODER_UNINITIALIZED;
1513}
1514
Josh Coalson02954222002-11-08 06:16:31 +00001515FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1516{
1517 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1518 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1519 else
Josh Coalson807140d2003-09-24 22:10:51 +00001520 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001521}
1522
Josh Coalson6afed9f2002-10-16 22:29:47 +00001523FLAC_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 +00001524{
1525 FLAC__ASSERT(0 != encoder);
1526 if(0 != absolute_sample)
1527 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1528 if(0 != frame_number)
1529 *frame_number = encoder->private_->verify.error_stats.frame_number;
1530 if(0 != channel)
1531 *channel = encoder->private_->verify.error_stats.channel;
1532 if(0 != sample)
1533 *sample = encoder->private_->verify.error_stats.sample;
1534 if(0 != expected)
1535 *expected = encoder->private_->verify.error_stats.expected;
1536 if(0 != got)
1537 *got = encoder->private_->verify.error_stats.got;
1538}
1539
Josh Coalson6afed9f2002-10-16 22:29:47 +00001540FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001541{
1542 FLAC__ASSERT(0 != encoder);
1543 return encoder->protected_->verify;
1544}
1545
Josh Coalson6afed9f2002-10-16 22:29:47 +00001546FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001547{
Josh Coalson92031602002-07-24 06:02:11 +00001548 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001549 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001550}
1551
Josh Coalson6afed9f2002-10-16 22:29:47 +00001552FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001553{
Josh Coalson92031602002-07-24 06:02:11 +00001554 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001555 return encoder->protected_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001556}
1557
Josh Coalson6afed9f2002-10-16 22:29:47 +00001558FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001559{
Josh Coalson92031602002-07-24 06:02:11 +00001560 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001561 return encoder->protected_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001562}
1563
Josh Coalson6afed9f2002-10-16 22:29:47 +00001564FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001565{
Josh Coalson92031602002-07-24 06:02:11 +00001566 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001567 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001568}
1569
Josh Coalson6afed9f2002-10-16 22:29:47 +00001570FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001571{
Josh Coalson92031602002-07-24 06:02:11 +00001572 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001573 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001574}
1575
Josh Coalson6afed9f2002-10-16 22:29:47 +00001576FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001577{
Josh Coalson92031602002-07-24 06:02:11 +00001578 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001579 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001580}
1581
Josh Coalson6afed9f2002-10-16 22:29:47 +00001582FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001583{
Josh Coalson92031602002-07-24 06:02:11 +00001584 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001585 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001586}
1587
Josh Coalson6afed9f2002-10-16 22:29:47 +00001588FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001589{
Josh Coalson92031602002-07-24 06:02:11 +00001590 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001591 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001592}
1593
Josh Coalson6afed9f2002-10-16 22:29:47 +00001594FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001595{
Josh Coalson92031602002-07-24 06:02:11 +00001596 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001597 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001598}
1599
Josh Coalson6afed9f2002-10-16 22:29:47 +00001600FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001601{
Josh Coalson92031602002-07-24 06:02:11 +00001602 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001603 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001604}
1605
Josh Coalson6afed9f2002-10-16 22:29:47 +00001606FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001607{
Josh Coalson92031602002-07-24 06:02:11 +00001608 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001609 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001610}
1611
Josh Coalson6afed9f2002-10-16 22:29:47 +00001612FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001613{
Josh Coalson92031602002-07-24 06:02:11 +00001614 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001615 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001616}
1617
Josh Coalson6afed9f2002-10-16 22:29:47 +00001618FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001619{
Josh Coalson92031602002-07-24 06:02:11 +00001620 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001621 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001622}
1623
Josh Coalson6afed9f2002-10-16 22:29:47 +00001624FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001625{
Josh Coalson92031602002-07-24 06:02:11 +00001626 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001627 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001628}
1629
Josh Coalson6afed9f2002-10-16 22:29:47 +00001630FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001631{
Josh Coalson92031602002-07-24 06:02:11 +00001632 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001633 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001634}
1635
Josh Coalson6afed9f2002-10-16 22:29:47 +00001636FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001637{
1638 FLAC__ASSERT(0 != encoder);
1639 return encoder->protected_->total_samples_estimate;
1640}
1641
Josh Coalson6afed9f2002-10-16 22:29:47 +00001642FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001643{
1644 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001645 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001646 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001647
Josh Coalsonf1eff452002-07-31 07:05:33 +00001648 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001649 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001650
1651 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001652 /*
1653 * we have several flavors of the same basic loop, optimized for
1654 * different conditions:
1655 */
1656 if(encoder->protected_->max_lpc_order > 0) {
1657 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1658 /*
1659 * stereo coding: unroll channel loop
1660 * with LPC: calculate floating point version of signal
1661 */
1662 do {
1663 if(encoder->protected_->verify)
1664 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 +00001665
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001666 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1667 x = mid = side = buffer[0][j];
1668 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001669#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001670 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001671#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001672 x = buffer[1][j];
1673 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001674#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001675 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001676#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001677 mid += x;
1678 side -= x;
1679 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1680 encoder->private_->integer_signal_mid_side[1][i] = side;
1681 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001682#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001683 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1684 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001685#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001686 encoder->private_->current_sample_number++;
1687 }
1688 if(i == blocksize) {
1689 if(!process_frame_(encoder, false)) /* false => not last frame */
1690 return false;
1691 }
1692 } while(j < samples);
1693 }
1694 else {
1695 /*
1696 * independent channel coding: buffer each channel in inner loop
1697 * with LPC: calculate floating point version of signal
1698 */
1699 do {
1700 if(encoder->protected_->verify)
1701 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1702
1703 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1704 for(channel = 0; channel < channels; channel++) {
1705 x = buffer[channel][j];
1706 encoder->private_->integer_signal[channel][i] = x;
1707#ifndef FLAC__INTEGER_ONLY_LIBRARY
1708 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1709#endif
1710 }
1711 encoder->private_->current_sample_number++;
1712 }
1713 if(i == blocksize) {
1714 if(!process_frame_(encoder, false)) /* false => not last frame */
1715 return false;
1716 }
1717 } while(j < samples);
1718 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001719 }
1720 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001721 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1722 /*
1723 * stereo coding: unroll channel loop
1724 * without LPC: no need to calculate floating point version of signal
1725 */
1726 do {
1727 if(encoder->protected_->verify)
1728 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 +00001729
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001730 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1731 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1732 x = buffer[1][j];
1733 encoder->private_->integer_signal[1][i] = x;
1734 mid += x;
1735 side -= x;
1736 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1737 encoder->private_->integer_signal_mid_side[1][i] = side;
1738 encoder->private_->integer_signal_mid_side[0][i] = mid;
1739 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001740 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001741 if(i == blocksize) {
1742 if(!process_frame_(encoder, false)) /* false => not last frame */
1743 return false;
1744 }
1745 } while(j < samples);
1746 }
1747 else {
1748 /*
1749 * independent channel coding: buffer each channel in inner loop
1750 * without LPC: no need to calculate floating point version of signal
1751 */
1752 do {
1753 if(encoder->protected_->verify)
1754 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1755
1756 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1757 for(channel = 0; channel < channels; channel++)
1758 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1759 encoder->private_->current_sample_number++;
1760 }
1761 if(i == blocksize) {
1762 if(!process_frame_(encoder, false)) /* false => not last frame */
1763 return false;
1764 }
1765 } while(j < samples);
1766 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001767 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001768
1769 return true;
1770}
1771
Josh Coalson6afed9f2002-10-16 22:29:47 +00001772FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001773{
1774 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001775 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001776 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001777
Josh Coalsonf1eff452002-07-31 07:05:33 +00001778 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001779 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001780
1781 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001782 /*
1783 * we have several flavors of the same basic loop, optimized for
1784 * different conditions:
1785 */
1786 if(encoder->protected_->max_lpc_order > 0) {
1787 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1788 /*
1789 * stereo coding: unroll channel loop
1790 * with LPC: calculate floating point version of signal
1791 */
1792 do {
1793 if(encoder->protected_->verify)
1794 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 +00001795
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001796 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1797 x = mid = side = buffer[k++];
1798 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001799#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001800 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001801#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001802 x = buffer[k++];
1803 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001804#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001805 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001806#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001807 mid += x;
1808 side -= x;
1809 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1810 encoder->private_->integer_signal_mid_side[1][i] = side;
1811 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001812#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001813 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1814 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001815#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001816 encoder->private_->current_sample_number++;
1817 }
1818 if(i == blocksize) {
1819 if(!process_frame_(encoder, false)) /* false => not last frame */
1820 return false;
1821 }
1822 } while(j < samples);
1823 }
1824 else {
1825 /*
1826 * independent channel coding: buffer each channel in inner loop
1827 * with LPC: calculate floating point version of signal
1828 */
1829 do {
1830 if(encoder->protected_->verify)
1831 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1832
1833 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1834 for(channel = 0; channel < channels; channel++) {
1835 x = buffer[k++];
1836 encoder->private_->integer_signal[channel][i] = x;
1837#ifndef FLAC__INTEGER_ONLY_LIBRARY
1838 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1839#endif
1840 }
1841 encoder->private_->current_sample_number++;
1842 }
1843 if(i == blocksize) {
1844 if(!process_frame_(encoder, false)) /* false => not last frame */
1845 return false;
1846 }
1847 } while(j < samples);
1848 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001849 }
1850 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001851 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1852 /*
1853 * stereo coding: unroll channel loop
1854 * without LPC: no need to calculate floating point version of signal
1855 */
1856 do {
1857 if(encoder->protected_->verify)
1858 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 +00001859
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001860 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1861 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00001862 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001863 encoder->private_->integer_signal[1][i] = x;
1864 mid += x;
1865 side -= x;
1866 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1867 encoder->private_->integer_signal_mid_side[1][i] = side;
1868 encoder->private_->integer_signal_mid_side[0][i] = mid;
1869 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001870 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001871 if(i == blocksize) {
1872 if(!process_frame_(encoder, false)) /* false => not last frame */
1873 return false;
1874 }
1875 } while(j < samples);
1876 }
1877 else {
1878 /*
1879 * independent channel coding: buffer each channel in inner loop
1880 * without LPC: no need to calculate floating point version of signal
1881 */
1882 do {
1883 if(encoder->protected_->verify)
1884 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1885
1886 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1887 for(channel = 0; channel < channels; channel++)
1888 encoder->private_->integer_signal[channel][i] = buffer[k++];
1889 encoder->private_->current_sample_number++;
1890 }
1891 if(i == blocksize) {
1892 if(!process_frame_(encoder, false)) /* false => not last frame */
1893 return false;
1894 }
1895 } while(j < samples);
1896 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001897 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001898
1899 return true;
1900}
1901
Josh Coalsonf1eff452002-07-31 07:05:33 +00001902/***********************************************************************
1903 *
1904 * Private class methods
1905 *
1906 ***********************************************************************/
1907
1908void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00001909{
1910 FLAC__ASSERT(0 != encoder);
1911
Josh Coalson47c7b142005-01-29 06:08:58 +00001912#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1913 encoder->protected_->verify = true;
1914#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00001915 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001916#endif
Josh Coalson92031602002-07-24 06:02:11 +00001917 encoder->protected_->streamable_subset = true;
1918 encoder->protected_->do_mid_side_stereo = false;
1919 encoder->protected_->loose_mid_side_stereo = false;
1920 encoder->protected_->channels = 2;
1921 encoder->protected_->bits_per_sample = 16;
1922 encoder->protected_->sample_rate = 44100;
1923 encoder->protected_->blocksize = 1152;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001924#ifndef FLAC__INTEGER_ONLY_LIBRARY
1925 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001926 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1927 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001928#endif
Josh Coalson92031602002-07-24 06:02:11 +00001929 encoder->protected_->max_lpc_order = 0;
1930 encoder->protected_->qlp_coeff_precision = 0;
1931 encoder->protected_->do_qlp_coeff_prec_search = false;
1932 encoder->protected_->do_exhaustive_model_search = false;
1933 encoder->protected_->do_escape_coding = false;
1934 encoder->protected_->min_residual_partition_order = 0;
1935 encoder->protected_->max_residual_partition_order = 0;
1936 encoder->protected_->rice_parameter_search_dist = 0;
1937 encoder->protected_->total_samples_estimate = 0;
1938 encoder->protected_->metadata = 0;
1939 encoder->protected_->num_metadata_blocks = 0;
1940
Josh Coalson6b21f662006-09-13 01:42:27 +00001941 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001942 encoder->private_->disable_constant_subframes = false;
1943 encoder->private_->disable_fixed_subframes = false;
1944 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson92031602002-07-24 06:02:11 +00001945 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001946 encoder->private_->seek_callback = 0;
1947 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00001948 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001949 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00001950 encoder->private_->client_data = 0;
1951}
1952
Josh Coalsonf1eff452002-07-31 07:05:33 +00001953void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00001954{
1955 unsigned i, channel;
1956
Josh Coalsonf1eff452002-07-31 07:05:33 +00001957 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00001958 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001959 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001960 free(encoder->private_->integer_signal_unaligned[i]);
1961 encoder->private_->integer_signal_unaligned[i] = 0;
1962 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001963#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00001964 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001965 free(encoder->private_->real_signal_unaligned[i]);
1966 encoder->private_->real_signal_unaligned[i] = 0;
1967 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001968#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001969 }
1970 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001971 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001972 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1973 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1974 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001975#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00001976 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001977 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1978 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1979 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001980#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001981 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001982#ifndef FLAC__INTEGER_ONLY_LIBRARY
1983 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
1984 if(0 != encoder->private_->window_unaligned[i]) {
1985 free(encoder->private_->window_unaligned[i]);
1986 encoder->private_->window_unaligned[i] = 0;
1987 }
1988 }
1989 if(0 != encoder->private_->windowed_signal_unaligned) {
1990 free(encoder->private_->windowed_signal_unaligned);
1991 encoder->private_->windowed_signal_unaligned = 0;
1992 }
1993#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001994 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1995 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001996 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001997 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1998 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1999 }
2000 }
2001 }
2002 for(channel = 0; channel < 2; channel++) {
2003 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002004 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002005 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2006 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2007 }
2008 }
2009 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002010 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002011 free(encoder->private_->abs_residual_unaligned);
2012 encoder->private_->abs_residual_unaligned = 0;
2013 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002014 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002015 free(encoder->private_->abs_residual_partition_sums_unaligned);
2016 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2017 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002018 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002019 free(encoder->private_->raw_bits_per_partition_unaligned);
2020 encoder->private_->raw_bits_per_partition_unaligned = 0;
2021 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002022 if(encoder->protected_->verify) {
2023 for(i = 0; i < encoder->protected_->channels; i++) {
2024 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2025 free(encoder->private_->verify.input_fifo.data[i]);
2026 encoder->private_->verify.input_fifo.data[i] = 0;
2027 }
2028 }
2029 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002030 FLAC__bitbuffer_free(encoder->private_->frame);
2031}
2032
Josh Coalsonf1eff452002-07-31 07:05:33 +00002033FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002034{
Josh Coalson77e3f312001-06-23 03:03:24 +00002035 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002036 unsigned i, channel;
2037
2038 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002039 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2040 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002041
2042 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002043 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002044 return true;
2045
2046 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002047
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002048 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2049 * requires that the input arrays (in our case the integer signals)
2050 * have a buffer of up to 3 zeroes in front (at negative indices) for
2051 * alignment purposes; we use 4 to keep the data well-aligned.
2052 */
Josh Coalson8395d022001-07-12 21:25:22 +00002053
Josh Coalsonfa697a92001-08-16 20:07:29 +00002054 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2055 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 +00002056#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002057 if(encoder->protected_->max_lpc_order > 0)
2058 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 +00002059#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002060 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2061 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00002062 }
2063 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002064 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 +00002065#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002066 if(encoder->protected_->max_lpc_order > 0)
2067 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 +00002068#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002069 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2070 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00002071 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002072#ifndef FLAC__INTEGER_ONLY_LIBRARY
2073 if(ok && encoder->protected_->max_lpc_order > 0) {
2074 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2075 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2076 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2077 }
2078#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002079 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002080 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002081 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 +00002082 }
2083 }
2084 for(channel = 0; ok && channel < 2; channel++) {
2085 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002086 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 +00002087 }
2088 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002089 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
2090 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 */
2091 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2092 if(encoder->protected_->do_escape_coding)
2093 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 +00002094
2095 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00002096 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00002097 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002098 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00002099
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002100#ifndef FLAC__INTEGER_ONLY_LIBRARY
2101 if(ok && encoder->protected_->max_lpc_order > 0) {
2102 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2103 switch(encoder->protected_->apodizations[i].type) {
2104 case FLAC__APODIZATION_BARTLETT:
2105 FLAC__window_bartlett(encoder->private_->window[i], new_size);
2106 break;
2107 case FLAC__APODIZATION_BARTLETT_HANN:
2108 FLAC__window_bartlett_hann(encoder->private_->window[i], new_size);
2109 break;
2110 case FLAC__APODIZATION_BLACKMAN:
2111 FLAC__window_blackman(encoder->private_->window[i], new_size);
2112 break;
2113 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2114 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_size);
2115 break;
2116 case FLAC__APODIZATION_CONNES:
2117 FLAC__window_connes(encoder->private_->window[i], new_size);
2118 break;
2119 case FLAC__APODIZATION_FLATTOP:
2120 FLAC__window_flattop(encoder->private_->window[i], new_size);
2121 break;
2122 case FLAC__APODIZATION_GAUSS:
2123 FLAC__window_gauss(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2124 break;
2125 case FLAC__APODIZATION_HAMMING:
2126 FLAC__window_hamming(encoder->private_->window[i], new_size);
2127 break;
2128 case FLAC__APODIZATION_HANN:
2129 FLAC__window_hann(encoder->private_->window[i], new_size);
2130 break;
2131 case FLAC__APODIZATION_KAISER_BESSEL:
2132 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_size);
2133 break;
2134 case FLAC__APODIZATION_NUTTALL:
2135 FLAC__window_nuttall(encoder->private_->window[i], new_size);
2136 break;
2137 case FLAC__APODIZATION_RECTANGLE:
2138 FLAC__window_rectangle(encoder->private_->window[i], new_size);
2139 break;
2140 case FLAC__APODIZATION_TRIANGLE:
2141 FLAC__window_triangle(encoder->private_->window[i], new_size);
2142 break;
2143 case FLAC__APODIZATION_TUKEY:
2144 FLAC__window_tukey(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.tukey.p);
2145 break;
2146 case FLAC__APODIZATION_WELCH:
2147 FLAC__window_welch(encoder->private_->window[i], new_size);
2148 break;
2149 default:
2150 FLAC__ASSERT(0);
2151 /* double protection */
Josh Coalsondf598452006-04-28 00:13:34 +00002152 FLAC__window_hann(encoder->private_->window[i], new_size);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002153 break;
2154 }
2155 }
2156 }
2157#endif
2158
Josh Coalson0a15c142001-06-13 17:59:57 +00002159 return ok;
2160}
2161
Josh Coalsond86e03b2002-08-03 21:56:15 +00002162FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00002163{
2164 const FLAC__byte *buffer;
2165 unsigned bytes;
2166
2167 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2168
2169 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2170
Josh Coalsond86e03b2002-08-03 21:56:15 +00002171 if(encoder->protected_->verify) {
2172 encoder->private_->verify.output.data = buffer;
2173 encoder->private_->verify.output.bytes = bytes;
2174 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2175 encoder->private_->verify.needs_magic_hack = true;
2176 }
2177 else {
2178 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2179 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2180 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2181 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2182 return false;
2183 }
2184 }
2185 }
2186
Josh Coalson6b21f662006-09-13 01:42:27 +00002187 if(write_frame_(encoder, buffer, bytes, samples) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002188 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002189 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002190 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002191 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002192
2193 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2194
Josh Coalsond86e03b2002-08-03 21:56:15 +00002195 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002196 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2197 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 +00002198 }
2199
Josh Coalson5c491a12002-08-01 06:39:40 +00002200 return true;
2201}
2202
Josh Coalson6b21f662006-09-13 01:42:27 +00002203FLAC__StreamEncoderWriteStatus write_frame_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples)
2204{
2205 FLAC__StreamEncoderWriteStatus status;
2206 FLAC__uint64 output_position = 0;
2207
2208 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2209 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2210 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2211 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2212 }
2213
2214 /*
2215 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2216 */
2217 if(samples == 0) {
2218 FLAC__MetadataType type = (buffer[0] & 0x7f);
2219 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2220 encoder->protected_->streaminfo_offset = output_position;
2221 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2222 encoder->protected_->seektable_offset = output_position;
2223 }
2224
2225 /*
2226 * Mark the current seek point if hit (if audio_offset == 0 that
2227 * means we're still writing metadata and haven't hit the first
2228 * frame yet)
2229 */
2230 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2231 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2232 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2233 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2234 FLAC__uint64 test_sample;
2235 unsigned i;
2236 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2237 test_sample = encoder->private_->seek_table->points[i].sample_number;
2238 if(test_sample > frame_last_sample) {
2239 break;
2240 }
2241 else if(test_sample >= frame_first_sample) {
2242 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2243 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2244 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2245 encoder->private_->first_seekpoint_to_check++;
2246 /* DO NOT: "break;" and here's why:
2247 * The seektable template may contain more than one target
2248 * sample for any given frame; we will keep looping, generating
2249 * duplicate seekpoints for them, and we'll clean it up later,
2250 * just before writing the seektable back to the metadata.
2251 */
2252 }
2253 else {
2254 encoder->private_->first_seekpoint_to_check++;
2255 }
2256 }
2257 }
2258
2259 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2260
2261 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2262 encoder->private_->bytes_written += bytes;
2263 encoder->private_->samples_written += samples;
2264 /* we keep a high watermark on the number of frames written because
2265 * when the encoder goes back to write metadata, 'current_frame'
2266 * will drop back to 0.
2267 */
2268 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2269 }
2270 else
2271 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2272
2273 return status;
2274}
2275
2276/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2277void update_metadata_(const FLAC__StreamEncoder *encoder)
2278{
2279 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2280 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2281 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2282 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2283 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2284 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2285 FLAC__StreamEncoderSeekStatus seek_status;
2286
2287 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2288
2289 /* All this is based on intimate knowledge of the stream header
2290 * layout, but a change to the header format that would break this
2291 * would also break all streams encoded in the previous format.
2292 */
2293
2294 /*
2295 * Write MD5 signature
2296 */
2297 {
2298 const unsigned md5_offset =
2299 FLAC__STREAM_METADATA_HEADER_LENGTH +
2300 (
2301 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2302 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2303 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2304 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2305 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2306 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2307 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2308 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2309 ) / 8;
2310
2311 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2312 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2313 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2314 return;
2315 }
2316 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2317 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2318 return;
2319 }
2320 }
2321
2322 /*
2323 * Write total samples
2324 */
2325 {
2326 const unsigned total_samples_byte_offset =
2327 FLAC__STREAM_METADATA_HEADER_LENGTH +
2328 (
2329 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2330 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2331 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2332 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2333 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2334 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2335 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2336 - 4
2337 ) / 8;
2338
2339 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2340 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2341 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2342 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2343 b[4] = (FLAC__byte)(samples & 0xFF);
2344 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) {
2345 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2346 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2347 return;
2348 }
2349 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2350 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2351 return;
2352 }
2353 }
2354
2355 /*
2356 * Write min/max framesize
2357 */
2358 {
2359 const unsigned min_framesize_offset =
2360 FLAC__STREAM_METADATA_HEADER_LENGTH +
2361 (
2362 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2363 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2364 ) / 8;
2365
2366 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2367 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2368 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2369 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2370 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2371 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2372 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) {
2373 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2374 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2375 return;
2376 }
2377 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2378 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2379 return;
2380 }
2381 }
2382
2383 /*
2384 * Write seektable
2385 */
2386 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2387 unsigned i;
2388
2389 FLAC__format_seektable_sort(encoder->private_->seek_table);
2390
2391 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2392
2393 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) {
2394 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2395 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2396 return;
2397 }
2398
2399 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2400 FLAC__uint64 xx;
2401 unsigned x;
2402 xx = encoder->private_->seek_table->points[i].sample_number;
2403 b[7] = (FLAC__byte)xx; xx >>= 8;
2404 b[6] = (FLAC__byte)xx; xx >>= 8;
2405 b[5] = (FLAC__byte)xx; xx >>= 8;
2406 b[4] = (FLAC__byte)xx; xx >>= 8;
2407 b[3] = (FLAC__byte)xx; xx >>= 8;
2408 b[2] = (FLAC__byte)xx; xx >>= 8;
2409 b[1] = (FLAC__byte)xx; xx >>= 8;
2410 b[0] = (FLAC__byte)xx; xx >>= 8;
2411 xx = encoder->private_->seek_table->points[i].stream_offset;
2412 b[15] = (FLAC__byte)xx; xx >>= 8;
2413 b[14] = (FLAC__byte)xx; xx >>= 8;
2414 b[13] = (FLAC__byte)xx; xx >>= 8;
2415 b[12] = (FLAC__byte)xx; xx >>= 8;
2416 b[11] = (FLAC__byte)xx; xx >>= 8;
2417 b[10] = (FLAC__byte)xx; xx >>= 8;
2418 b[9] = (FLAC__byte)xx; xx >>= 8;
2419 b[8] = (FLAC__byte)xx; xx >>= 8;
2420 x = encoder->private_->seek_table->points[i].frame_samples;
2421 b[17] = (FLAC__byte)x; x >>= 8;
2422 b[16] = (FLAC__byte)x; x >>= 8;
2423 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2424 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2425 return;
2426 }
2427 }
2428 }
2429}
2430
Josh Coalsonf1eff452002-07-31 07:05:33 +00002431FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00002432{
Josh Coalsonfa697a92001-08-16 20:07:29 +00002433 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002434
2435 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002436 * Accumulate raw signal to the MD5 signature
2437 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00002438 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 +00002439 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002440 return false;
2441 }
2442
2443 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002444 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002445 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00002446 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002447 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002448 return false;
2449 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002450
2451 /*
2452 * Zero-pad the frame to a byte_boundary
2453 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002454 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002455 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002456 return false;
2457 }
2458
2459 /*
Josh Coalson215af572001-03-27 01:15:58 +00002460 * CRC-16 the whole thing
2461 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002462 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2463 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 +00002464
2465 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002466 * Write it
2467 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00002468 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
2469 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002470 return false;
2471 }
2472
2473 /*
2474 * Get ready for the next frame
2475 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002476 encoder->private_->current_sample_number = 0;
2477 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00002478 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002479
2480 return true;
2481}
2482
Josh Coalsonf1eff452002-07-31 07:05:33 +00002483FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002484{
2485 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002486 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002487 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002488
2489 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00002490 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00002491 */
2492 if(is_last_frame) {
2493 max_partition_order = 0;
2494 }
2495 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002496 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2497 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002498 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002499 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002500
Josh Coalsonfa697a92001-08-16 20:07:29 +00002501 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 +00002502
Josh Coalson94e02cd2001-01-25 10:41:06 +00002503 /*
2504 * Setup the frame
2505 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002506 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002507 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002508 return false;
2509 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002510 frame_header.blocksize = encoder->protected_->blocksize;
2511 frame_header.sample_rate = encoder->protected_->sample_rate;
2512 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002513 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002514 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002515 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002516 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002517
2518 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002519 * Figure out what channel assignments to try
2520 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002521 if(encoder->protected_->do_mid_side_stereo) {
2522 if(encoder->protected_->loose_mid_side_stereo) {
2523 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002524 do_independent = true;
2525 do_mid_side = true;
2526 }
2527 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002528 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002529 do_mid_side = !do_independent;
2530 }
2531 }
2532 else {
2533 do_independent = true;
2534 do_mid_side = true;
2535 }
2536 }
2537 else {
2538 do_independent = true;
2539 do_mid_side = false;
2540 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002541
Josh Coalson1b689822001-05-31 20:11:02 +00002542 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002543
2544 /*
Josh Coalson82b73242001-03-28 22:17:05 +00002545 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00002546 */
2547 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002548 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002549 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002550 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
2551 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00002552 }
Josh Coalson859bc542001-03-27 22:22:27 +00002553 }
2554 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002555 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00002556 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002557 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002558 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
2559 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00002560 }
Josh Coalson859bc542001-03-27 22:22:27 +00002561 }
2562
2563 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002564 * First do a normal encoding pass of each independent channel
2565 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002566 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002567 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002568 if(!
2569 process_subframe_(
2570 encoder,
2571 min_partition_order,
2572 max_partition_order,
2573 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002574 &frame_header,
2575 encoder->private_->subframe_bps[channel],
2576 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002577#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002578 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002579#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002580 encoder->private_->subframe_workspace_ptr[channel],
2581 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
2582 encoder->private_->residual_workspace[channel],
2583 encoder->private_->best_subframe+channel,
2584 encoder->private_->best_subframe_bits+channel
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002585#ifdef WINDOW_DEBUG_OUTPUT
2586 ,channel
2587#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002588 )
2589 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002590 return false;
2591 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002592 }
2593
2594 /*
2595 * Now do mid and side channels if requested
2596 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002597 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002598 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002599
2600 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002601 if(!
2602 process_subframe_(
2603 encoder,
2604 min_partition_order,
2605 max_partition_order,
2606 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002607 &frame_header,
2608 encoder->private_->subframe_bps_mid_side[channel],
2609 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002610#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002611 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002612#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002613 encoder->private_->subframe_workspace_ptr_mid_side[channel],
2614 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
2615 encoder->private_->residual_workspace_mid_side[channel],
2616 encoder->private_->best_subframe_mid_side+channel,
2617 encoder->private_->best_subframe_bits_mid_side+channel
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002618#ifdef WINDOW_DEBUG_OUTPUT
2619 ,channel
2620#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002621 )
2622 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00002623 return false;
2624 }
2625 }
2626
2627 /*
2628 * Compose the frame bitbuffer
2629 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002630 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00002631 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
2632 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002633 FLAC__ChannelAssignment channel_assignment;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002634#ifdef WINDOW_DEBUG_OUTPUT
2635 unsigned left_bits = 0, right_bits = 0;
2636#endif
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002637
Josh Coalsonfa697a92001-08-16 20:07:29 +00002638 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002639
Josh Coalsonfa697a92001-08-16 20:07:29 +00002640 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
2641 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 +00002642 }
2643 else {
2644 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
2645 unsigned min_bits;
2646 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002647
Josh Coalson1b689822001-05-31 20:11:02 +00002648 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002649
2650 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002651 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
2652 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
2653 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
2654 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 +00002655
Josh Coalson7424d2f2002-11-06 07:10:38 +00002656 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 +00002657 if(bits[ca] < min_bits) {
2658 min_bits = bits[ca];
2659 channel_assignment = ca;
2660 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002661 }
2662 }
2663
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002664 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002665
Josh Coalson3e7a96e2004-07-23 05:18:22 +00002666 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002667 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002668 return false;
2669 }
2670
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002671 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002672 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002673 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2674 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002675#ifdef WINDOW_DEBUG_OUTPUT
2676 left_bits = encoder->private_->best_subframe_bits [0];
2677 right_bits = encoder->private_->best_subframe_bits [1];
2678#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002679 break;
2680 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002681 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2682 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002683#ifdef WINDOW_DEBUG_OUTPUT
2684 left_bits = encoder->private_->best_subframe_bits [0];
2685 right_bits = encoder->private_->best_subframe_bits_mid_side [1];
2686#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002687 break;
2688 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002689 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2690 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002691#ifdef WINDOW_DEBUG_OUTPUT
2692 left_bits = encoder->private_->best_subframe_bits_mid_side [1];
2693 right_bits = encoder->private_->best_subframe_bits [1];
2694#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002695 break;
2696 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002697 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
2698 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002699#ifdef WINDOW_DEBUG_OUTPUT
2700 left_bits = encoder->private_->best_subframe_bits_mid_side [0];
2701 right_bits = encoder->private_->best_subframe_bits_mid_side [1];
2702#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002703 break;
2704 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002705 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002706 }
Josh Coalson82b73242001-03-28 22:17:05 +00002707
2708 switch(channel_assignment) {
2709 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002710 left_bps = encoder->private_->subframe_bps [0];
2711 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00002712 break;
2713 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002714 left_bps = encoder->private_->subframe_bps [0];
2715 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00002716 break;
2717 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002718 left_bps = encoder->private_->subframe_bps_mid_side[1];
2719 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00002720 break;
2721 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002722 left_bps = encoder->private_->subframe_bps_mid_side[0];
2723 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00002724 break;
2725 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002726 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00002727 }
2728
2729 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002730#ifdef WINDOW_DEBUG_OUTPUT
2731 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame, left_bits))
2732 return false;
2733 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame, right_bits))
2734 return false;
2735#else
Josh Coalsonf1eff452002-07-31 07:05:33 +00002736 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00002737 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00002738 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00002739 return false;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002740#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002741 }
2742 else {
Josh Coalson3e7a96e2004-07-23 05:18:22 +00002743 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002744 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002745 return false;
2746 }
2747
Josh Coalsonfa697a92001-08-16 20:07:29 +00002748 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00002749#ifdef WINDOW_DEBUG_OUTPUT
2750 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame, encoder->private_->best_subframe_bits[channel]))
2751#else
2752 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame))
2753#endif
2754 {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002755 /* the above function sets the state for us in case of an error */
2756 return false;
2757 }
2758 }
2759 }
2760
Josh Coalsonfa697a92001-08-16 20:07:29 +00002761 if(encoder->protected_->loose_mid_side_stereo) {
2762 encoder->private_->loose_mid_side_stereo_frame_count++;
2763 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
2764 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002765 }
2766
Josh Coalsonfa697a92001-08-16 20:07:29 +00002767 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002768
Josh Coalson94e02cd2001-01-25 10:41:06 +00002769 return true;
2770}
2771
Josh Coalson6fe72f72002-08-20 04:01:59 +00002772FLAC__bool process_subframe_(
2773 FLAC__StreamEncoder *encoder,
2774 unsigned min_partition_order,
2775 unsigned max_partition_order,
2776 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002777 const FLAC__FrameHeader *frame_header,
2778 unsigned subframe_bps,
2779 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002780#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002781 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002782#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002783 FLAC__Subframe *subframe[2],
2784 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
2785 FLAC__int32 *residual[2],
2786 unsigned *best_subframe,
2787 unsigned *best_bits
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002788#ifdef WINDOW_DEBUG_OUTPUT
2789 ,unsigned subframe_number
2790#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002791)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002792{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002793#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002794 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002795#else
2796 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2797#endif
2798#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002799 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002800 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 +00002801 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002802 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002803 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002804#endif
2805 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002806 unsigned rice_parameter;
2807 unsigned _candidate_bits, _best_bits;
2808 unsigned _best_subframe;
2809
2810 /* verbatim subframe is the baseline against which we measure other compressed subframes */
2811 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002812 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
2813 _best_bits = UINT_MAX;
2814 else
2815 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002816
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002817 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
2818 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002819 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 +00002820 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002821 if(
2822 !encoder->private_->disable_constant_subframes &&
2823#ifndef FLAC__INTEGER_ONLY_LIBRARY
2824 fixed_residual_bits_per_sample[1] == 0.0
2825#else
2826 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
2827#endif
2828 ) {
2829 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002830 unsigned i;
2831 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002832 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002833 if(integer_signal[0] != integer_signal[i]) {
2834 signal_is_constant = false;
2835 break;
2836 }
2837 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002838 }
2839 if(signal_is_constant) {
2840 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
2841 if(_candidate_bits < _best_bits) {
2842 _best_subframe = !_best_subframe;
2843 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002844 }
2845 }
2846 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002847 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
2848 /* encode fixed */
2849 if(encoder->protected_->do_exhaustive_model_search) {
2850 min_fixed_order = 0;
2851 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00002852 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002853 else {
2854 min_fixed_order = max_fixed_order = guess_fixed_order;
2855 }
2856 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002857#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002858 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002859 continue; /* don't even try */
2860 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 +00002861#else
2862 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
2863 continue; /* don't even try */
2864 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 */
2865#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002866 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002867 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2868#ifdef DEBUG_VERBOSE
2869 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2870#endif
2871 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2872 }
2873 _candidate_bits =
2874 evaluate_fixed_subframe_(
2875 encoder,
2876 integer_signal,
2877 residual[!_best_subframe],
2878 encoder->private_->abs_residual,
2879 encoder->private_->abs_residual_partition_sums,
2880 encoder->private_->raw_bits_per_partition,
2881 frame_header->blocksize,
2882 subframe_bps,
2883 fixed_order,
2884 rice_parameter,
2885 min_partition_order,
2886 max_partition_order,
2887 precompute_partition_sums,
2888 encoder->protected_->do_escape_coding,
2889 encoder->protected_->rice_parameter_search_dist,
2890 subframe[!_best_subframe],
2891 partitioned_rice_contents[!_best_subframe]
2892 );
2893 if(_candidate_bits < _best_bits) {
2894 _best_subframe = !_best_subframe;
2895 _best_bits = _candidate_bits;
2896 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002897 }
2898 }
2899
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002900#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00002901 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002902 if(encoder->protected_->max_lpc_order > 0) {
2903 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002904 max_lpc_order = frame_header->blocksize-1;
2905 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002906 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002907 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002908 unsigned a;
2909 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00002910 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002911 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2912 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2913 if(autoc[0] != 0.0) {
2914 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2915 if(encoder->protected_->do_exhaustive_model_search) {
2916 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002917 }
2918 else {
Josh Coalsondf598452006-04-28 00:13:34 +00002919 const unsigned guess_lpc_order =
2920 FLAC__lpc_compute_best_order(
2921 lpc_error,
2922 max_lpc_order,
2923 frame_header->blocksize,
2924 subframe_bps + (
2925 encoder->protected_->do_qlp_coeff_prec_search?
2926 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
2927 encoder->protected_->qlp_coeff_precision
2928 )
2929 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002930 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002931 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002932 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2933 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2934 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
2935 continue; /* don't even try */
2936 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2937 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2938 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00002939#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002940 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 +00002941#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002942 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2943 }
2944 if(encoder->protected_->do_qlp_coeff_prec_search) {
2945 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00002946 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
2947 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002948 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00002949 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
2950 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002951 else
2952 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2953 }
2954 else {
2955 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2956 }
2957 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2958 _candidate_bits =
2959 evaluate_lpc_subframe_(
2960 encoder,
2961 integer_signal,
2962 residual[!_best_subframe],
2963 encoder->private_->abs_residual,
2964 encoder->private_->abs_residual_partition_sums,
2965 encoder->private_->raw_bits_per_partition,
2966 encoder->private_->lp_coeff[lpc_order-1],
2967 frame_header->blocksize,
2968 subframe_bps,
2969 lpc_order,
2970 qlp_coeff_precision,
2971 rice_parameter,
2972 min_partition_order,
2973 max_partition_order,
2974 precompute_partition_sums,
2975 encoder->protected_->do_escape_coding,
2976 encoder->protected_->rice_parameter_search_dist,
2977 subframe[!_best_subframe],
2978 partitioned_rice_contents[!_best_subframe]
2979#ifdef WINDOW_DEBUG_OUTPUT
2980 ,frame_header->number.frame_number
2981 ,subframe_number
2982 ,encoder->protected_->apodizations[a]
2983#endif
2984 );
2985 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2986 if(_candidate_bits < _best_bits) {
2987 _best_subframe = !_best_subframe;
2988 _best_bits = _candidate_bits;
2989 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002990 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002991 }
2992 }
2993 }
2994 }
2995 }
2996 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002997#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00002998 }
2999 }
3000
Josh Coalson72695802002-10-11 06:25:16 +00003001 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
3002 if(_best_bits == UINT_MAX) {
3003 FLAC__ASSERT(_best_subframe == 0);
3004 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
3005 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003006
Josh Coalson94e02cd2001-01-25 10:41:06 +00003007 *best_subframe = _best_subframe;
3008 *best_bits = _best_bits;
3009
3010 return true;
3011}
3012
Josh Coalson6fe72f72002-08-20 04:01:59 +00003013FLAC__bool add_subframe_(
3014 FLAC__StreamEncoder *encoder,
3015 const FLAC__FrameHeader *frame_header,
3016 unsigned subframe_bps,
3017 const FLAC__Subframe *subframe,
3018 FLAC__BitBuffer *frame
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003019#ifdef WINDOW_DEBUG_OUTPUT
3020,unsigned subframe_bits
3021#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003022)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003023{
3024 switch(subframe->type) {
3025 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003026 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003027 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003028 return false;
3029 }
3030 break;
3031 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00003032 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 +00003033 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003034 return false;
3035 }
3036 break;
3037 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003038#ifdef WINDOW_DEBUG_OUTPUT
3039 fprintf(stderr, "WIN:\tframe=%u\tsubframe=?\torder=%u\twindow=%s\tbits=%u\n", frame_header->number.frame_number, subframe->data.lpc.order, subframe->data.lpc.window_type, subframe_bits);
3040#endif
Josh Coalson82b73242001-03-28 22:17:05 +00003041 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 +00003042 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003043 return false;
3044 }
3045 break;
3046 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00003047 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003048 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003049 return false;
3050 }
3051 break;
3052 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003053 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003054 }
3055
3056 return true;
3057}
3058
Josh Coalson6fe72f72002-08-20 04:01:59 +00003059unsigned evaluate_constant_subframe_(
3060 const FLAC__int32 signal,
3061 unsigned subframe_bps,
3062 FLAC__Subframe *subframe
3063)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003064{
3065 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3066 subframe->data.constant.value = signal;
3067
Josh Coalson82b73242001-03-28 22:17:05 +00003068 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 +00003069}
3070
Josh Coalson6fe72f72002-08-20 04:01:59 +00003071unsigned evaluate_fixed_subframe_(
3072 FLAC__StreamEncoder *encoder,
3073 const FLAC__int32 signal[],
3074 FLAC__int32 residual[],
3075 FLAC__uint32 abs_residual[],
3076 FLAC__uint64 abs_residual_partition_sums[],
3077 unsigned raw_bits_per_partition[],
3078 unsigned blocksize,
3079 unsigned subframe_bps,
3080 unsigned order,
3081 unsigned rice_parameter,
3082 unsigned min_partition_order,
3083 unsigned max_partition_order,
3084 FLAC__bool precompute_partition_sums,
3085 FLAC__bool do_escape_coding,
3086 unsigned rice_parameter_search_dist,
3087 FLAC__Subframe *subframe,
3088 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3089)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003090{
3091 unsigned i, residual_bits;
3092 const unsigned residual_samples = blocksize - order;
3093
3094 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3095
3096 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3097
3098 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003099 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003100 subframe->data.fixed.residual = residual;
3101
Josh Coalson6fe72f72002-08-20 04:01:59 +00003102 residual_bits =
3103 find_best_partition_order_(
3104 encoder->private_,
3105 residual,
3106 abs_residual,
3107 abs_residual_partition_sums,
3108 raw_bits_per_partition,
3109 residual_samples,
3110 order,
3111 rice_parameter,
3112 min_partition_order,
3113 max_partition_order,
3114 precompute_partition_sums,
3115 do_escape_coding,
3116 rice_parameter_search_dist,
3117 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3118 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003119
3120 subframe->data.fixed.order = order;
3121 for(i = 0; i < order; i++)
3122 subframe->data.fixed.warmup[i] = signal[i];
3123
Josh Coalson82b73242001-03-28 22:17:05 +00003124 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 +00003125}
3126
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003127#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003128unsigned evaluate_lpc_subframe_(
3129 FLAC__StreamEncoder *encoder,
3130 const FLAC__int32 signal[],
3131 FLAC__int32 residual[],
3132 FLAC__uint32 abs_residual[],
3133 FLAC__uint64 abs_residual_partition_sums[],
3134 unsigned raw_bits_per_partition[],
3135 const FLAC__real lp_coeff[],
3136 unsigned blocksize,
3137 unsigned subframe_bps,
3138 unsigned order,
3139 unsigned qlp_coeff_precision,
3140 unsigned rice_parameter,
3141 unsigned min_partition_order,
3142 unsigned max_partition_order,
3143 FLAC__bool precompute_partition_sums,
3144 FLAC__bool do_escape_coding,
3145 unsigned rice_parameter_search_dist,
3146 FLAC__Subframe *subframe,
3147 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003148#ifdef WINDOW_DEBUG_OUTPUT
3149 ,unsigned frame_number
3150 ,unsigned subframe_number
3151 ,FLAC__ApodizationSpecification aspec
3152#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003153)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003154{
Josh Coalson77e3f312001-06-23 03:03:24 +00003155 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003156 unsigned i, residual_bits;
3157 int quantization, ret;
3158 const unsigned residual_samples = blocksize - order;
3159
Josh Coalson20ac2c12002-08-30 05:47:14 +00003160 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3161 if(subframe_bps <= 16) {
3162 FLAC__ASSERT(order > 0);
3163 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3164 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3165 }
3166
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003167#ifdef WINDOW_DEBUG_OUTPUT
3168 if (aspec.type == FLAC__APODIZATION_GAUSS)
3169 snprintf(subframe->data.lpc.window_type, sizeof subframe->data.lpc.window_type, "%s(%0.5f)", winstr[aspec.type], aspec.parameters.gauss.stddev);
3170 else if (aspec.type == FLAC__APODIZATION_TUKEY)
3171 snprintf(subframe->data.lpc.window_type, sizeof subframe->data.lpc.window_type, "%s(%0.5f)", winstr[aspec.type], aspec.parameters.tukey.p);
3172 else
3173 strncpy(subframe->data.lpc.window_type, winstr[aspec.type], sizeof subframe->data.lpc.window_type);
3174#endif
3175
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003176 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003177 if(ret != 0)
3178 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3179
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003180 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3181 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3182 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3183 else
3184 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 +00003185 else
3186 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 +00003187
3188 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3189
3190 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003191 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003192 subframe->data.lpc.residual = residual;
3193
Josh Coalson6fe72f72002-08-20 04:01:59 +00003194 residual_bits =
3195 find_best_partition_order_(
3196 encoder->private_,
3197 residual,
3198 abs_residual,
3199 abs_residual_partition_sums,
3200 raw_bits_per_partition,
3201 residual_samples,
3202 order,
3203 rice_parameter,
3204 min_partition_order,
3205 max_partition_order,
3206 precompute_partition_sums,
3207 do_escape_coding,
3208 rice_parameter_search_dist,
3209 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3210 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003211
3212 subframe->data.lpc.order = order;
3213 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3214 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003215 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003216 for(i = 0; i < order; i++)
3217 subframe->data.lpc.warmup[i] = signal[i];
3218
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003219#ifdef WINDOW_DEBUG_OUTPUT
3220 fprintf(stderr, "SWIN:\tframe=%u\tsubframe=%u\torder=%u\twindow=%s\tbits=%u\n", frame_number, subframe_number, order, subframe->data.lpc.window_type, 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);
3221#endif
Josh Coalson82b73242001-03-28 22:17:05 +00003222 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 +00003223}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003224#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003225
Josh Coalson6fe72f72002-08-20 04:01:59 +00003226unsigned evaluate_verbatim_subframe_(
3227 const FLAC__int32 signal[],
3228 unsigned blocksize,
3229 unsigned subframe_bps,
3230 FLAC__Subframe *subframe
3231)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003232{
3233 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3234
3235 subframe->data.verbatim.data = signal;
3236
Josh Coalson82b73242001-03-28 22:17:05 +00003237 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 +00003238}
3239
Josh Coalson6fe72f72002-08-20 04:01:59 +00003240unsigned find_best_partition_order_(
3241 FLAC__StreamEncoderPrivate *private_,
3242 const FLAC__int32 residual[],
3243 FLAC__uint32 abs_residual[],
3244 FLAC__uint64 abs_residual_partition_sums[],
3245 unsigned raw_bits_per_partition[],
3246 unsigned residual_samples,
3247 unsigned predictor_order,
3248 unsigned rice_parameter,
3249 unsigned min_partition_order,
3250 unsigned max_partition_order,
3251 FLAC__bool precompute_partition_sums,
3252 FLAC__bool do_escape_coding,
3253 unsigned rice_parameter_search_dist,
3254 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3255)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003256{
Josh Coalson77e3f312001-06-23 03:03:24 +00003257 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003258 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00003259 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00003260 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003261 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003262
Josh Coalson2051dd42001-04-12 22:22:34 +00003263 /* compute abs(residual) for use later */
3264 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3265 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00003266 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00003267 }
3268
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003269 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 +00003270 min_partition_order = min(min_partition_order, max_partition_order);
3271
Josh Coalson8395d022001-07-12 21:25:22 +00003272 if(precompute_partition_sums) {
3273 int partition_order;
3274 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003275
Josh Coalsonf1eff452002-07-31 07:05:33 +00003276 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 +00003277
3278 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00003279 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 +00003280
3281 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3282#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003283 if(!
3284 set_partitioned_rice_with_precompute_(
3285 residual,
3286 abs_residual_partition_sums+sum,
3287 raw_bits_per_partition+sum,
3288 residual_samples,
3289 predictor_order,
3290 rice_parameter,
3291 rice_parameter_search_dist,
3292 (unsigned)partition_order,
3293 do_escape_coding,
3294 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3295 &residual_bits
3296 )
3297 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003298#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003299 if(!
3300 set_partitioned_rice_with_precompute_(
3301 abs_residual,
3302 abs_residual_partition_sums+sum,
3303 raw_bits_per_partition+sum,
3304 residual_samples,
3305 predictor_order,
3306 rice_parameter,
3307 rice_parameter_search_dist,
3308 (unsigned)partition_order,
3309 do_escape_coding,
3310 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3311 &residual_bits
3312 )
3313 )
Josh Coalson8395d022001-07-12 21:25:22 +00003314#endif
3315 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003316 FLAC__ASSERT(best_residual_bits != 0);
3317 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003318 }
3319 sum += 1u << partition_order;
3320 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3321 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003322 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003323 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003324 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003325 }
3326 }
Josh Coalson8395d022001-07-12 21:25:22 +00003327 else {
3328 unsigned partition_order;
3329 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3330#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003331 if(!
3332 set_partitioned_rice_(
3333 abs_residual,
3334 residual,
3335 residual_samples,
3336 predictor_order,
3337 rice_parameter,
3338 rice_parameter_search_dist,
3339 partition_order,
3340 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3341 &residual_bits
3342 )
3343 )
Josh Coalson8395d022001-07-12 21:25:22 +00003344#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003345 if(!
3346 set_partitioned_rice_(
3347 abs_residual,
3348 residual_samples,
3349 predictor_order,
3350 rice_parameter,
3351 rice_parameter_search_dist,
3352 partition_order,
3353 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3354 &residual_bits
3355 )
3356 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003357#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003358 {
3359 FLAC__ASSERT(best_residual_bits != 0);
3360 break;
3361 }
3362 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3363 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003364 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003365 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003366 }
3367 }
3368 }
3369
Josh Coalsona37ba462002-08-19 21:36:39 +00003370 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003371 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003372 * it is const to the outside world.
3373 */
3374 {
3375 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3376 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3377 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3378 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3379 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003380
3381 return best_residual_bits;
3382}
3383
Josh Coalson6fe72f72002-08-20 04:01:59 +00003384void precompute_partition_info_sums_(
3385 const FLAC__uint32 abs_residual[],
3386 FLAC__uint64 abs_residual_partition_sums[],
3387 unsigned residual_samples,
3388 unsigned predictor_order,
3389 unsigned min_partition_order,
3390 unsigned max_partition_order
3391)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003392{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003393 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003394 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003395 const unsigned blocksize = residual_samples + predictor_order;
3396
Josh Coalsonaef013c2001-04-24 01:25:42 +00003397 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003398 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003399 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00003400 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003401 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003402 const unsigned partitions = 1u << partition_order;
3403 const unsigned default_partition_samples = blocksize >> partition_order;
3404
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003405 FLAC__ASSERT(default_partition_samples > predictor_order);
3406
3407 for(partition = residual_sample = 0; partition < partitions; partition++) {
3408 partition_samples = default_partition_samples;
3409 if(partition == 0)
3410 partition_samples -= predictor_order;
3411 abs_residual_partition_sum = 0;
3412 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3413 abs_r = abs_residual[residual_sample];
3414 abs_residual_partition_sum += abs_r;
3415 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003416 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003417 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003418 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003419 to_partition = partitions;
3420 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003421 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00003422
Josh Coalson8395d022001-07-12 21:25:22 +00003423 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00003424 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003425 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003426 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003427 const unsigned partitions = 1u << partition_order;
3428 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00003429 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003430 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003431 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003432 from_partition++;
3433 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003434 }
3435 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003436}
Josh Coalson8395d022001-07-12 21:25:22 +00003437
Josh Coalson6fe72f72002-08-20 04:01:59 +00003438void precompute_partition_info_escapes_(
3439 const FLAC__int32 residual[],
3440 unsigned raw_bits_per_partition[],
3441 unsigned residual_samples,
3442 unsigned predictor_order,
3443 unsigned min_partition_order,
3444 unsigned max_partition_order
3445)
Josh Coalson8395d022001-07-12 21:25:22 +00003446{
3447 int partition_order;
3448 unsigned from_partition, to_partition = 0;
3449 const unsigned blocksize = residual_samples + predictor_order;
3450
3451 /* first do max_partition_order */
3452 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3453 FLAC__int32 r, residual_partition_min, residual_partition_max;
3454 unsigned silog2_min, silog2_max;
3455 unsigned partition, partition_sample, partition_samples, residual_sample;
3456 const unsigned partitions = 1u << partition_order;
3457 const unsigned default_partition_samples = blocksize >> partition_order;
3458
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003459 FLAC__ASSERT(default_partition_samples > predictor_order);
3460
3461 for(partition = residual_sample = 0; partition < partitions; partition++) {
3462 partition_samples = default_partition_samples;
3463 if(partition == 0)
3464 partition_samples -= predictor_order;
3465 residual_partition_min = residual_partition_max = 0;
3466 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3467 r = residual[residual_sample];
3468 if(r < residual_partition_min)
3469 residual_partition_min = r;
3470 else if(r > residual_partition_max)
3471 residual_partition_max = r;
3472 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00003473 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003474 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
3475 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
3476 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00003477 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003478 to_partition = partitions;
Josh Coalson03f54be2006-05-18 00:04:14 +00003479 break; /*@@@@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00003480 }
3481
3482 /* now merge partitions for lower orders */
3483 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3484 unsigned m;
3485 unsigned i;
3486 const unsigned partitions = 1u << partition_order;
3487 for(i = 0; i < partitions; i++) {
3488 m = raw_bits_per_partition[from_partition];
3489 from_partition++;
3490 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
3491 from_partition++;
3492 to_partition++;
3493 }
3494 }
3495}
Josh Coalson94e02cd2001-01-25 10:41:06 +00003496
Josh Coalson352e0f62001-03-20 22:55:50 +00003497#ifdef VARIABLE_RICE_BITS
3498#undef VARIABLE_RICE_BITS
3499#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003500#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003501#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00003502#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003503
Josh Coalson8395d022001-07-12 21:25:22 +00003504#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003505FLAC__bool set_partitioned_rice_(
3506 const FLAC__uint32 abs_residual[],
3507 const FLAC__int32 residual[],
3508 const unsigned residual_samples,
3509 const unsigned predictor_order,
3510 const unsigned suggested_rice_parameter,
3511 const unsigned rice_parameter_search_dist,
3512 const unsigned partition_order,
3513 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3514 unsigned *bits
3515)
Josh Coalson8395d022001-07-12 21:25:22 +00003516#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003517FLAC__bool set_partitioned_rice_(
3518 const FLAC__uint32 abs_residual[],
3519 const unsigned residual_samples,
3520 const unsigned predictor_order,
3521 const unsigned suggested_rice_parameter,
3522 const unsigned rice_parameter_search_dist,
3523 const unsigned partition_order,
3524 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3525 unsigned *bits
3526)
Josh Coalson8395d022001-07-12 21:25:22 +00003527#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003528{
Josh Coalson034dfab2001-04-27 19:10:23 +00003529 unsigned rice_parameter, partition_bits;
3530#ifndef NO_RICE_SEARCH
3531 unsigned best_partition_bits;
3532 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3533#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003534 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003535 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003536
Josh Coalson1b689822001-05-31 20:11:02 +00003537 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00003538
Josh Coalsona37ba462002-08-19 21:36:39 +00003539 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3540 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003541
Josh Coalson94e02cd2001-01-25 10:41:06 +00003542 if(partition_order == 0) {
3543 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00003544
Josh Coalson034dfab2001-04-27 19:10:23 +00003545#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003546 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003547 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003548 min_rice_parameter = 0;
3549 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003550 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3551 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003552 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003553#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003554 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3555#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003556 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003557 }
3558 }
3559 else
3560 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3561
3562 best_partition_bits = 0xffffffff;
3563 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3564#endif
3565#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003566 const unsigned rice_parameter_estimate = rice_parameter-1;
3567 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00003568#else
3569 partition_bits = 0;
3570#endif
3571 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3572 for(i = 0; i < residual_samples; i++) {
3573#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003574 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00003575#else
3576 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3577#endif
3578 }
3579#ifndef NO_RICE_SEARCH
3580 if(partition_bits < best_partition_bits) {
3581 best_rice_parameter = rice_parameter;
3582 best_partition_bits = partition_bits;
3583 }
3584 }
3585#endif
3586 parameters[0] = best_rice_parameter;
3587 bits_ += best_partition_bits;
3588 }
3589 else {
3590 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003591 unsigned partition_samples;
3592 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00003593 const unsigned partitions = 1u << partition_order;
3594 for(partition = residual_sample = 0; partition < partitions; partition++) {
3595 partition_samples = (residual_samples+predictor_order) >> partition_order;
3596 if(partition == 0) {
3597 if(partition_samples <= predictor_order)
3598 return false;
3599 else
3600 partition_samples -= predictor_order;
3601 }
3602 mean = 0;
3603 save_residual_sample = residual_sample;
3604 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003605 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00003606 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00003607 /* we are basically calculating the size in bits of the
3608 * average residual magnitude in the partition:
3609 * rice_parameter = floor(log2(mean/partition_samples))
3610 * 'mean' is not a good name for the variable, it is
3611 * actually the sum of magnitudes of all residual values
3612 * in the partition, so the actual mean is
3613 * mean/partition_samples
3614 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003615 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00003616 ;
Josh Coalson8395d022001-07-12 21:25:22 +00003617 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003618#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003619 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3620#endif
3621 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3622 }
3623
3624#ifndef NO_RICE_SEARCH
3625 if(rice_parameter_search_dist) {
3626 if(rice_parameter < rice_parameter_search_dist)
3627 min_rice_parameter = 0;
3628 else
3629 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3630 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3631 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003632#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003633 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3634#endif
3635 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3636 }
3637 }
3638 else
3639 min_rice_parameter = max_rice_parameter = rice_parameter;
3640
3641 best_partition_bits = 0xffffffff;
3642 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3643#endif
3644#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003645 const unsigned rice_parameter_estimate = rice_parameter-1;
3646 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00003647#else
3648 partition_bits = 0;
3649#endif
3650 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3651 save_residual_sample = residual_sample;
3652 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3653#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003654 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00003655#else
3656 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3657#endif
3658 }
3659#ifndef NO_RICE_SEARCH
3660 if(rice_parameter != max_rice_parameter)
3661 residual_sample = save_residual_sample;
3662 if(partition_bits < best_partition_bits) {
3663 best_rice_parameter = rice_parameter;
3664 best_partition_bits = partition_bits;
3665 }
3666 }
3667#endif
3668 parameters[partition] = best_rice_parameter;
3669 bits_ += best_partition_bits;
3670 }
3671 }
3672
3673 *bits = bits_;
3674 return true;
3675}
3676
3677#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003678FLAC__bool set_partitioned_rice_with_precompute_(
3679 const FLAC__int32 residual[],
3680 const FLAC__uint64 abs_residual_partition_sums[],
3681 const unsigned raw_bits_per_partition[],
3682 const unsigned residual_samples,
3683 const unsigned predictor_order,
3684 const unsigned suggested_rice_parameter,
3685 const unsigned rice_parameter_search_dist,
3686 const unsigned partition_order,
3687 const FLAC__bool search_for_escapes,
3688 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3689 unsigned *bits
3690)
Josh Coalson8395d022001-07-12 21:25:22 +00003691#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003692FLAC__bool set_partitioned_rice_with_precompute_(
3693 const FLAC__uint32 abs_residual[],
3694 const FLAC__uint64 abs_residual_partition_sums[],
3695 const unsigned raw_bits_per_partition[],
3696 const unsigned residual_samples,
3697 const unsigned predictor_order,
3698 const unsigned suggested_rice_parameter,
3699 const unsigned rice_parameter_search_dist,
3700 const unsigned partition_order,
3701 const FLAC__bool search_for_escapes,
3702 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3703 unsigned *bits
3704)
Josh Coalson8395d022001-07-12 21:25:22 +00003705#endif
3706{
3707 unsigned rice_parameter, partition_bits;
3708#ifndef NO_RICE_SEARCH
3709 unsigned best_partition_bits;
3710 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3711#endif
3712 unsigned flat_bits;
3713 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003714 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003715
3716 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3717
Josh Coalsona37ba462002-08-19 21:36:39 +00003718 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3719 parameters = partitioned_rice_contents->parameters;
3720 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003721
Josh Coalson8395d022001-07-12 21:25:22 +00003722 if(partition_order == 0) {
3723 unsigned i;
3724
3725#ifndef NO_RICE_SEARCH
3726 if(rice_parameter_search_dist) {
3727 if(suggested_rice_parameter < rice_parameter_search_dist)
3728 min_rice_parameter = 0;
3729 else
3730 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3731 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3732 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003733#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003734 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3735#endif
3736 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3737 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003738 }
3739 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003740 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00003741
Josh Coalson034dfab2001-04-27 19:10:23 +00003742 best_partition_bits = 0xffffffff;
3743 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3744#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003745#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003746 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00003747 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00003748#else
3749 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003750#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00003751 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00003752 for(i = 0; i < residual_samples; i++) {
3753#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00003754 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00003755#else
Josh Coalson2051dd42001-04-12 22:22:34 +00003756 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 +00003757#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00003758 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003759#ifndef NO_RICE_SEARCH
3760 if(partition_bits < best_partition_bits) {
3761 best_rice_parameter = rice_parameter;
3762 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00003763 }
3764 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003765#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003766 if(search_for_escapes) {
3767 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;
3768 if(flat_bits <= best_partition_bits) {
3769 raw_bits[0] = raw_bits_per_partition[0];
3770 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3771 best_partition_bits = flat_bits;
3772 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003773 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003774 parameters[0] = best_rice_parameter;
3775 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003776 }
3777 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00003778 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003779 unsigned partition_samples;
3780 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003781 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00003782 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003783 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00003784 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003785 if(partition_samples <= predictor_order)
3786 return false;
3787 else
3788 partition_samples -= predictor_order;
3789 }
Josh Coalson05d20792001-06-29 23:12:26 +00003790 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00003791 /* we are basically calculating the size in bits of the
3792 * average residual magnitude in the partition:
3793 * rice_parameter = floor(log2(mean/partition_samples))
3794 * 'mean' is not a good name for the variable, it is
3795 * actually the sum of magnitudes of all residual values
3796 * in the partition, so the actual mean is
3797 * mean/partition_samples
3798 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003799 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00003800 ;
Josh Coalson8395d022001-07-12 21:25:22 +00003801 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003802#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003803 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3804#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003805 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003806 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003807
Josh Coalson034dfab2001-04-27 19:10:23 +00003808#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003809 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003810 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003811 min_rice_parameter = 0;
3812 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003813 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3814 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003815 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003816#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003817 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3818#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003819 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003820 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003821 }
3822 else
3823 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00003824
Josh Coalson034dfab2001-04-27 19:10:23 +00003825 best_partition_bits = 0xffffffff;
3826 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3827#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003828#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00003829 const unsigned rice_parameter_estimate = rice_parameter-1;
3830 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00003831#else
3832 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003833#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003834 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00003835 save_residual_sample = residual_sample;
3836 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00003837#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00003838 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00003839#else
Josh Coalson4dacd192001-06-06 21:11:44 +00003840 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 +00003841#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003842 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003843#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00003844 if(rice_parameter != max_rice_parameter)
3845 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00003846 if(partition_bits < best_partition_bits) {
3847 best_rice_parameter = rice_parameter;
3848 best_partition_bits = partition_bits;
3849 }
Josh Coalson2051dd42001-04-12 22:22:34 +00003850 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003851#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003852 if(search_for_escapes) {
3853 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;
3854 if(flat_bits <= best_partition_bits) {
3855 raw_bits[partition] = raw_bits_per_partition[partition];
3856 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3857 best_partition_bits = flat_bits;
3858 }
Josh Coalson2051dd42001-04-12 22:22:34 +00003859 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003860 parameters[partition] = best_rice_parameter;
3861 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003862 }
3863 }
3864
3865 *bits = bits_;
3866 return true;
3867}
Josh Coalson859bc542001-03-27 22:22:27 +00003868
Josh Coalsonf1eff452002-07-31 07:05:33 +00003869unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00003870{
3871 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00003872 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00003873
3874 for(i = 0; i < samples && !(x&1); i++)
3875 x |= signal[i];
3876
3877 if(x == 0) {
3878 shift = 0;
3879 }
3880 else {
3881 for(shift = 0; !(x&1); shift++)
3882 x >>= 1;
3883 }
3884
3885 if(shift > 0) {
3886 for(i = 0; i < samples; i++)
3887 signal[i] >>= shift;
3888 }
3889
3890 return shift;
3891}
Josh Coalsond86e03b2002-08-03 21:56:15 +00003892
3893void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3894{
3895 unsigned channel;
3896
3897 for(channel = 0; channel < channels; channel++)
3898 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3899
3900 fifo->tail += wide_samples;
3901
3902 FLAC__ASSERT(fifo->tail <= fifo->size);
3903}
3904
3905void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3906{
3907 unsigned channel;
3908 unsigned sample, wide_sample;
3909 unsigned tail = fifo->tail;
3910
3911 sample = input_offset * channels;
3912 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3913 for(channel = 0; channel < channels; channel++)
3914 fifo->data[channel][tail] = input[sample++];
3915 tail++;
3916 }
3917 fifo->tail = tail;
3918
3919 FLAC__ASSERT(fifo->tail <= fifo->size);
3920}
3921
3922FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3923{
3924 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3925 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3926 (void)decoder;
3927
3928 if(encoder->private_->verify.needs_magic_hack) {
3929 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3930 *bytes = FLAC__STREAM_SYNC_LENGTH;
3931 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3932 encoder->private_->verify.needs_magic_hack = false;
3933 }
3934 else {
3935 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00003936 /*
3937 * If we get here, a FIFO underflow has occurred,
3938 * which means there is a bug somewhere.
3939 */
3940 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003941 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3942 }
3943 else if(encoded_bytes < *bytes)
3944 *bytes = encoded_bytes;
3945 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3946 encoder->private_->verify.output.data += *bytes;
3947 encoder->private_->verify.output.bytes -= *bytes;
3948 }
3949
3950 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3951}
3952
3953FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3954{
3955 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3956 unsigned channel;
3957 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3958 const unsigned blocksize = frame->header.blocksize;
3959 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3960
3961 for(channel = 0; channel < channels; channel++) {
3962 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3963 unsigned i, sample = 0;
3964 FLAC__int32 expect = 0, got = 0;
3965
3966 for(i = 0; i < blocksize; i++) {
3967 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3968 sample = i;
3969 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3970 got = (FLAC__int32)buffer[channel][i];
3971 break;
3972 }
3973 }
3974 FLAC__ASSERT(i < blocksize);
3975 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3976 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00003977 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003978 encoder->private_->verify.error_stats.channel = channel;
3979 encoder->private_->verify.error_stats.sample = sample;
3980 encoder->private_->verify.error_stats.expected = expect;
3981 encoder->private_->verify.error_stats.got = got;
3982 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3983 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3984 }
3985 }
3986 /* dequeue the frame from the fifo */
3987 for(channel = 0; channel < channels; channel++) {
3988 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3989 }
3990 encoder->private_->verify.input_fifo.tail -= blocksize;
3991 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3992}
3993
3994void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3995{
3996 (void)decoder, (void)metadata, (void)client_data;
3997}
3998
3999void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4000{
4001 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4002 (void)decoder, (void)status;
4003 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4004}
Josh Coalson6b21f662006-09-13 01:42:27 +00004005
4006FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4007{
4008 (void)client_data;
4009
4010 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4011 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4012 else
4013 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4014}
4015
4016FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4017{
4018 off_t offset;
4019
4020 (void)client_data;
4021
4022 offset = ftello(encoder->private_->file);
4023
4024 if(offset < 0) {
4025 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4026 }
4027 else {
4028 *absolute_byte_offset = (FLAC__uint64)offset;
4029 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4030 }
4031}
4032
4033#ifdef FLAC__VALGRIND_TESTING
4034static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4035{
4036 size_t ret = fwrite(ptr, size, nmemb, stream);
4037 if(!ferror(stream))
4038 fflush(stream);
4039 return ret;
4040}
4041#else
4042#define local__fwrite fwrite
4043#endif
4044
4045FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
4046{
4047 (void)client_data, (void)samples, (void)current_frame;
4048
4049 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4050 if(0 != encoder->private_->progress_callback && samples > 0)
4051 encoder->private_->progress_callback(encoder, encoder->private_->bytes_written, encoder->private_->samples_written, encoder->private_->frames_written, encoder->private_->total_frames_estimate, encoder->private_->client_data);
4052 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4053 }
4054 else
4055 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4056}
4057
4058/*
4059 * This will forcibly set stdout to binary mode (for OSes that require it)
4060 */
4061FILE *get_binary_stdout_()
4062{
4063 /* if something breaks here it is probably due to the presence or
4064 * absence of an underscore before the identifiers 'setmode',
4065 * 'fileno', and/or 'O_BINARY'; check your system header files.
4066 */
4067#if defined _MSC_VER || defined __MINGW32__
4068 _setmode(_fileno(stdout), _O_BINARY);
4069#elif defined __CYGWIN__
4070 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
4071 setmode(_fileno(stdout), _O_BINARY);
4072#elif defined __EMX__
4073 setmode(fileno(stdout), O_BINARY);
4074#endif
4075
4076 return stdout;
4077}