blob: 66eb992c089261d60ce3f1b919918777eb48abca [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson0395dac2006-04-25 06:59:33 +00002 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003 *
Josh Coalsonafd81072003-01-31 23:34:56 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00007 *
Josh Coalsonafd81072003-01-31 23:34:56 +00008 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000010 *
Josh Coalsonafd81072003-01-31 23:34:56 +000011 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000030 */
31
Josh Coalsonb1ec7962006-05-24 04:41:36 +000032#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
Josh Coalson6b21f662006-09-13 01:42:27 +000036#if defined _MSC_VER || defined __MINGW32__
37#include <io.h> /* for _setmode() */
38#include <fcntl.h> /* for _O_BINARY */
39#endif
40#if defined __CYGWIN__ || defined __EMX__
41#include <io.h> /* for setmode(), O_BINARY */
42#include <fcntl.h> /* for _O_BINARY */
43#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +000044#include <limits.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000045#include <stdio.h>
46#include <stdlib.h> /* for malloc() */
47#include <string.h> /* for memcpy() */
Josh Coalson6b21f662006-09-13 01:42:27 +000048#include <sys/types.h> /* for off_t */
49#if defined _MSC_VER || defined __MINGW32__
Josh Coalson825e72c2006-10-03 01:16:59 +000050#if _MSC_VER <= 1200 /* @@@ [2G limit] */
Josh Coalson6b21f662006-09-13 01:42:27 +000051#define fseeko fseek
52#define ftello ftell
53#endif
Josh Coalson825e72c2006-10-03 01:16:59 +000054#endif
Josh Coalson1b689822001-05-31 20:11:02 +000055#include "FLAC/assert.h"
Josh Coalsond86e03b2002-08-03 21:56:15 +000056#include "FLAC/stream_decoder.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000057#include "protected/stream_encoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000058#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000059#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000060#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000061#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000062#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000063#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000064#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000065#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000066#include "private/memory.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000067#include "private/stream_encoder_framing.h"
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000068#include "private/window.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000069
70#ifdef min
71#undef min
72#endif
73#define min(x,y) ((x)<(y)?(x):(y))
74
75#ifdef max
76#undef max
77#endif
78#define max(x,y) ((x)>(y)?(x):(y))
79
Josh Coalsond86e03b2002-08-03 21:56:15 +000080typedef struct {
81 FLAC__int32 *data[FLAC__MAX_CHANNELS];
82 unsigned size; /* of each data[] in samples */
83 unsigned tail;
84} verify_input_fifo;
85
86typedef struct {
87 const FLAC__byte *data;
88 unsigned capacity;
89 unsigned bytes;
90} verify_output;
91
92typedef enum {
93 ENCODER_IN_MAGIC = 0,
94 ENCODER_IN_METADATA = 1,
95 ENCODER_IN_AUDIO = 2
96} EncoderStateHint;
97
Josh Coalson0a15c142001-06-13 17:59:57 +000098/***********************************************************************
99 *
100 * Private class method prototypes
101 *
102 ***********************************************************************/
103
Josh Coalsonf1eff452002-07-31 07:05:33 +0000104static void set_defaults_(FLAC__StreamEncoder *encoder);
105static void free_(FLAC__StreamEncoder *encoder);
106static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
Josh Coalsond86e03b2002-08-03 21:56:15 +0000107static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
Josh Coalson6b21f662006-09-13 01:42:27 +0000108static FLAC__StreamEncoderWriteStatus write_frame_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples);
109static void update_metadata_(const FLAC__StreamEncoder *encoder);
Josh Coalsonf1eff452002-07-31 07:05:33 +0000110static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
111static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000112
113static FLAC__bool process_subframe_(
114 FLAC__StreamEncoder *encoder,
115 unsigned min_partition_order,
116 unsigned max_partition_order,
117 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000118 const FLAC__FrameHeader *frame_header,
119 unsigned subframe_bps,
120 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000121#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000122 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000123#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000124 FLAC__Subframe *subframe[2],
125 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
126 FLAC__int32 *residual[2],
127 unsigned *best_subframe,
128 unsigned *best_bits
129);
130
131static FLAC__bool add_subframe_(
132 FLAC__StreamEncoder *encoder,
133 const FLAC__FrameHeader *frame_header,
134 unsigned subframe_bps,
135 const FLAC__Subframe *subframe,
136 FLAC__BitBuffer *frame
137);
138
139static unsigned evaluate_constant_subframe_(
140 const FLAC__int32 signal,
141 unsigned subframe_bps,
142 FLAC__Subframe *subframe
143);
144
145static unsigned evaluate_fixed_subframe_(
146 FLAC__StreamEncoder *encoder,
147 const FLAC__int32 signal[],
148 FLAC__int32 residual[],
149 FLAC__uint32 abs_residual[],
150 FLAC__uint64 abs_residual_partition_sums[],
151 unsigned raw_bits_per_partition[],
152 unsigned blocksize,
153 unsigned subframe_bps,
154 unsigned order,
155 unsigned rice_parameter,
156 unsigned min_partition_order,
157 unsigned max_partition_order,
158 FLAC__bool precompute_partition_sums,
159 FLAC__bool do_escape_coding,
160 unsigned rice_parameter_search_dist,
161 FLAC__Subframe *subframe,
162 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
163);
164
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000165#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000166static unsigned evaluate_lpc_subframe_(
167 FLAC__StreamEncoder *encoder,
168 const FLAC__int32 signal[],
169 FLAC__int32 residual[],
170 FLAC__uint32 abs_residual[],
171 FLAC__uint64 abs_residual_partition_sums[],
172 unsigned raw_bits_per_partition[],
173 const FLAC__real lp_coeff[],
174 unsigned blocksize,
175 unsigned subframe_bps,
176 unsigned order,
177 unsigned qlp_coeff_precision,
178 unsigned rice_parameter,
179 unsigned min_partition_order,
180 unsigned max_partition_order,
181 FLAC__bool precompute_partition_sums,
182 FLAC__bool do_escape_coding,
183 unsigned rice_parameter_search_dist,
184 FLAC__Subframe *subframe,
185 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
186);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000187#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000188
189static unsigned evaluate_verbatim_subframe_(
190 const FLAC__int32 signal[],
191 unsigned blocksize,
192 unsigned subframe_bps,
193 FLAC__Subframe *subframe
194);
195
196static unsigned find_best_partition_order_(
197 struct FLAC__StreamEncoderPrivate *private_,
198 const FLAC__int32 residual[],
199 FLAC__uint32 abs_residual[],
200 FLAC__uint64 abs_residual_partition_sums[],
201 unsigned raw_bits_per_partition[],
202 unsigned residual_samples,
203 unsigned predictor_order,
204 unsigned rice_parameter,
205 unsigned min_partition_order,
206 unsigned max_partition_order,
207 FLAC__bool precompute_partition_sums,
208 FLAC__bool do_escape_coding,
209 unsigned rice_parameter_search_dist,
210 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
211);
212
213static void precompute_partition_info_sums_(
214 const FLAC__uint32 abs_residual[],
215 FLAC__uint64 abs_residual_partition_sums[],
216 unsigned residual_samples,
217 unsigned predictor_order,
218 unsigned min_partition_order,
219 unsigned max_partition_order
220);
221
222static void precompute_partition_info_escapes_(
223 const FLAC__int32 residual[],
224 unsigned raw_bits_per_partition[],
225 unsigned residual_samples,
226 unsigned predictor_order,
227 unsigned min_partition_order,
228 unsigned max_partition_order
229);
230
Josh Coalson8395d022001-07-12 21:25:22 +0000231#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000232static FLAC__bool set_partitioned_rice_(
233 const FLAC__uint32 abs_residual[],
234 const FLAC__int32 residual[],
235 const unsigned residual_samples,
236 const unsigned predictor_order,
237 const unsigned suggested_rice_parameter,
238 const unsigned rice_parameter_search_dist,
239 const unsigned partition_order,
240 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
241 unsigned *bits
242);
243
244static FLAC__bool set_partitioned_rice_with_precompute_(
245 const FLAC__int32 residual[],
246 const FLAC__uint64 abs_residual_partition_sums[],
247 const unsigned raw_bits_per_partition[],
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 const FLAC__bool search_for_escapes,
254 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
255 unsigned *bits
256);
Josh Coalson8395d022001-07-12 21:25:22 +0000257#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000258static FLAC__bool set_partitioned_rice_(
259 const FLAC__uint32 abs_residual[],
260 const unsigned residual_samples,
261 const unsigned predictor_order,
262 const unsigned suggested_rice_parameter,
263 const unsigned rice_parameter_search_dist,
264 const unsigned partition_order,
265 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
266 unsigned *bits
267);
268
269static FLAC__bool set_partitioned_rice_with_precompute_(
270 const FLAC__uint32 abs_residual[],
271 const FLAC__uint64 abs_residual_partition_sums[],
272 const unsigned raw_bits_per_partition[],
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 const FLAC__bool search_for_escapes,
279 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
280 unsigned *bits
281);
Josh Coalson0a15c142001-06-13 17:59:57 +0000282#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000283
Josh Coalsonf1eff452002-07-31 07:05:33 +0000284static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000285
Josh Coalsond86e03b2002-08-03 21:56:15 +0000286/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000287static void append_to_verify_fifo_(
288 verify_input_fifo *fifo,
289 const FLAC__int32 * const input[],
290 unsigned input_offset,
291 unsigned channels,
292 unsigned wide_samples
293);
294
295static void append_to_verify_fifo_interleaved_(
296 verify_input_fifo *fifo,
297 const FLAC__int32 input[],
298 unsigned input_offset,
299 unsigned channels,
300 unsigned wide_samples
301);
302
Josh Coalson6b21f662006-09-13 01:42:27 +0000303static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
304static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
305static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
306static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000307
Josh Coalson6b21f662006-09-13 01:42:27 +0000308static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
309static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
310static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
311static FILE *get_binary_stdout_();
Josh Coalson6fe72f72002-08-20 04:01:59 +0000312
Josh Coalson0a15c142001-06-13 17:59:57 +0000313
314/***********************************************************************
315 *
316 * Private class data
317 *
318 ***********************************************************************/
319
320typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000321 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000322 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
323 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 +0000324#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000325 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
326 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 +0000327 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
328 FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000329#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000330 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
331 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 +0000332 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
333 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000334 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
335 FLAC__Subframe subframe_workspace_mid_side[2][2];
336 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
337 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000338 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
339 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
340 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
341 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000342 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000343 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000344 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000345 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000346 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000347 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 +0000348 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 +0000349 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000350 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
351 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000352 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalson6b21f662006-09-13 01:42:27 +0000353 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
354 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000355 unsigned current_sample_number;
356 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000357 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000358 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000359#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000360 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 +0000361#else
362 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
363#endif
364#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000365 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
Josh Coalson7446e182005-01-26 04:04:38 +0000366 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[]);
367 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[]);
368 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 +0000369#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000370 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
371 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
372 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
373 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 +0000374 FLAC__bool disable_constant_subframes;
375 FLAC__bool disable_fixed_subframes;
376 FLAC__bool disable_verbatim_subframes;
Josh Coalson6b21f662006-09-13 01:42:27 +0000377 FLAC__StreamEncoderSeekCallback seek_callback;
378 FLAC__StreamEncoderTellCallback tell_callback;
Josh Coalson681c2932002-08-01 08:19:37 +0000379 FLAC__StreamEncoderWriteCallback write_callback;
380 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000381 FLAC__StreamEncoderProgressCallback progress_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000382 void *client_data;
Josh Coalson6b21f662006-09-13 01:42:27 +0000383 unsigned first_seekpoint_to_check;
384 FILE *file; /* only used when encoding to a file */
385 FLAC__uint64 bytes_written;
386 FLAC__uint64 samples_written;
387 unsigned frames_written;
388 unsigned total_frames_estimate;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000389 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000390 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
391 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000392#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000393 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
394 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000395 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
396 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000397#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000398 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
399 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
400 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000401 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000402 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000403 /*
404 * These fields have been moved here from private function local
405 * declarations merely to save stack space during encoding.
406 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000407#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000408 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000409#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000410 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000411 /*
412 * The data for the verify section
413 */
414 struct {
415 FLAC__StreamDecoder *decoder;
416 EncoderStateHint state_hint;
417 FLAC__bool needs_magic_hack;
418 verify_input_fifo input_fifo;
419 verify_output output;
420 struct {
421 FLAC__uint64 absolute_sample;
422 unsigned frame_number;
423 unsigned channel;
424 unsigned sample;
425 FLAC__int32 expected;
426 FLAC__int32 got;
427 } error_stats;
428 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000429 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000430} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000431
Josh Coalson0a15c142001-06-13 17:59:57 +0000432/***********************************************************************
433 *
434 * Public static class data
435 *
436 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000437
Josh Coalson6afed9f2002-10-16 22:29:47 +0000438FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000439 "FLAC__STREAM_ENCODER_OK",
Josh Coalson6b21f662006-09-13 01:42:27 +0000440 "FLAC__STREAM_ENCODER_UNINITIALIZED",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000441 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
442 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson6b21f662006-09-13 01:42:27 +0000443 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
444 "FLAC__STREAM_ENCODER_IO_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000445 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson6b21f662006-09-13 01:42:27 +0000446 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
447};
448
449FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
450 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
451 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
452 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
453 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
454 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
455 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
456 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
457 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
458 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
459 "FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH",
Josh Coalson6b21f662006-09-13 01:42:27 +0000460 "FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE",
461 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
462 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
463 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
464 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000465};
466
Josh Coalson6afed9f2002-10-16 22:29:47 +0000467FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000468 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
469 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000470};
471
Josh Coalson6b21f662006-09-13 01:42:27 +0000472FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
473 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
474 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
475 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
476};
477
478FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
479 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
480 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
481 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
482};
483
Josh Coalson0a15c142001-06-13 17:59:57 +0000484/***********************************************************************
485 *
486 * Class constructor/destructor
487 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000488 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000489FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000490{
Josh Coalson0a15c142001-06-13 17:59:57 +0000491 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000492 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000493
Josh Coalson0a15c142001-06-13 17:59:57 +0000494 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000495
Josh Coalsonea7155f2002-10-18 05:49:19 +0000496 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000497 if(encoder == 0) {
498 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000499 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000500
Josh Coalsonea7155f2002-10-18 05:49:19 +0000501 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000502 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000503 free(encoder);
504 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000505 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000506
Josh Coalsonea7155f2002-10-18 05:49:19 +0000507 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000508 if(encoder->private_ == 0) {
509 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000510 free(encoder);
511 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000512 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000513
Josh Coalsonaec256b2002-03-12 16:19:54 +0000514 encoder->private_->frame = FLAC__bitbuffer_new();
515 if(encoder->private_->frame == 0) {
516 free(encoder->private_);
517 free(encoder->protected_);
518 free(encoder);
519 return 0;
520 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000521
Josh Coalson6b21f662006-09-13 01:42:27 +0000522 encoder->private_->file = 0;
523
Josh Coalsonf1eff452002-07-31 07:05:33 +0000524 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000525
Josh Coalson3262b0d2002-08-14 20:58:42 +0000526 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000527
528 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
529 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
530 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
531 }
532 for(i = 0; i < 2; i++) {
533 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
534 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
535 }
536 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000537 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
538 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000539 }
540 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000541 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
542 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 +0000543 }
544
545 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000546 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
547 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000548 }
549 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000550 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
551 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 +0000552 }
553 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000554 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000555
Josh Coalsonfa697a92001-08-16 20:07:29 +0000556 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000557
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000558 return encoder;
559}
560
Josh Coalson6afed9f2002-10-16 22:29:47 +0000561FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000562{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000563 unsigned i;
564
Josh Coalsonf1eff452002-07-31 07:05:33 +0000565 FLAC__ASSERT(0 != encoder);
566 FLAC__ASSERT(0 != encoder->protected_);
567 FLAC__ASSERT(0 != encoder->private_);
568 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000569
Josh Coalson3262b0d2002-08-14 20:58:42 +0000570 encoder->private_->is_being_deleted = true;
571
572 FLAC__stream_encoder_finish(encoder);
573
Josh Coalson4fa90592002-12-04 07:01:37 +0000574 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000575 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000576
577 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000578 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
579 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000580 }
581 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000582 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
583 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 +0000584 }
585 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000586 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000587
Josh Coalsonaec256b2002-03-12 16:19:54 +0000588 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000589 free(encoder->private_);
590 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000591 free(encoder);
592}
593
Josh Coalson0a15c142001-06-13 17:59:57 +0000594/***********************************************************************
595 *
596 * Public class methods
597 *
598 ***********************************************************************/
599
Josh Coalson6b21f662006-09-13 01:42:27 +0000600FLAC_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 +0000601{
602 unsigned i;
Josh Coalson3957c472006-09-24 16:25:42 +0000603 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000604
Josh Coalsonf1eff452002-07-31 07:05:33 +0000605 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000606
Josh Coalsonfa697a92001-08-16 20:07:29 +0000607 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000608 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000609
Josh Coalson6b21f662006-09-13 01:42:27 +0000610 if(0 == write_callback || (seek_callback && 0 == tell_callback))
611 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000612
Josh Coalsonfa697a92001-08-16 20:07:29 +0000613 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000614 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000615
Josh Coalsonfa697a92001-08-16 20:07:29 +0000616 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
Josh Coalson6b21f662006-09-13 01:42:27 +0000617 return FLAC__STREAM_ENCODER_INIT_STATUS_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000618
Josh Coalsonfa697a92001-08-16 20:07:29 +0000619 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
Josh Coalson6b21f662006-09-13 01:42:27 +0000620 return FLAC__STREAM_ENCODER_INIT_STATUS_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000621
Josh Coalsonfa697a92001-08-16 20:07:29 +0000622 if(encoder->protected_->bits_per_sample >= 32)
Josh Coalson6b21f662006-09-13 01:42:27 +0000623 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 +0000624
Josh Coalson76c68bc2002-05-17 06:22:02 +0000625 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 +0000626 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000627
Josh Coalson0833f342002-07-15 05:31:55 +0000628 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000629 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000630
Josh Coalsonfa697a92001-08-16 20:07:29 +0000631 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000632 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000633
Josh Coalson20ac2c12002-08-30 05:47:14 +0000634 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000635 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000636
Josh Coalsonfa697a92001-08-16 20:07:29 +0000637 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000638 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000639
Josh Coalsonfa697a92001-08-16 20:07:29 +0000640 if(encoder->protected_->qlp_coeff_precision == 0) {
641 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000642 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
643 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000644 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 +0000645 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000646 else if(encoder->protected_->bits_per_sample == 16) {
647 if(encoder->protected_->blocksize <= 192)
648 encoder->protected_->qlp_coeff_precision = 7;
649 else if(encoder->protected_->blocksize <= 384)
650 encoder->protected_->qlp_coeff_precision = 8;
651 else if(encoder->protected_->blocksize <= 576)
652 encoder->protected_->qlp_coeff_precision = 9;
653 else if(encoder->protected_->blocksize <= 1152)
654 encoder->protected_->qlp_coeff_precision = 10;
655 else if(encoder->protected_->blocksize <= 2304)
656 encoder->protected_->qlp_coeff_precision = 11;
657 else if(encoder->protected_->blocksize <= 4608)
658 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000659 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000660 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000661 }
662 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000663 if(encoder->protected_->blocksize <= 384)
664 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
665 else if(encoder->protected_->blocksize <= 1152)
666 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
667 else
668 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000669 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000670 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000671 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000672 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 +0000673 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000674
Josh Coalsonfa697a92001-08-16 20:07:29 +0000675 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000676 if(
677 encoder->protected_->blocksize != 192 &&
678 encoder->protected_->blocksize != 576 &&
679 encoder->protected_->blocksize != 1152 &&
680 encoder->protected_->blocksize != 2304 &&
681 encoder->protected_->blocksize != 4608 &&
682 encoder->protected_->blocksize != 256 &&
683 encoder->protected_->blocksize != 512 &&
684 encoder->protected_->blocksize != 1024 &&
685 encoder->protected_->blocksize != 2048 &&
686 encoder->protected_->blocksize != 4096 &&
687 encoder->protected_->blocksize != 8192 &&
688 encoder->protected_->blocksize != 16384
689 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000690 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000691 if(
692 encoder->protected_->sample_rate != 8000 &&
693 encoder->protected_->sample_rate != 16000 &&
694 encoder->protected_->sample_rate != 22050 &&
695 encoder->protected_->sample_rate != 24000 &&
696 encoder->protected_->sample_rate != 32000 &&
697 encoder->protected_->sample_rate != 44100 &&
698 encoder->protected_->sample_rate != 48000 &&
699 encoder->protected_->sample_rate != 96000
700 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000701 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000702 if(
703 encoder->protected_->bits_per_sample != 8 &&
704 encoder->protected_->bits_per_sample != 12 &&
705 encoder->protected_->bits_per_sample != 16 &&
706 encoder->protected_->bits_per_sample != 20 &&
707 encoder->protected_->bits_per_sample != 24
708 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000709 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000710 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000711 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsond0edb972006-10-07 06:50:08 +0000712 if(
713 encoder->protected_->sample_rate <= 48000 &&
714 (
715 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
716 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
717 )
718 ) {
719 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
720 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000721 }
722
Josh Coalsonfa697a92001-08-16 20:07:29 +0000723 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
724 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
725 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
726 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000727
Josh Coalson66075c12002-06-01 05:39:38 +0000728 /* validate metadata */
729 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000730 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000731 metadata_has_seektable = false;
732 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000733 metadata_picture_has_type1 = false;
734 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000735 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000736 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
737 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000738 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000739 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000740 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000741 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000742 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000743 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000744 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000745 }
Josh Coalson3957c472006-09-24 16:25:42 +0000746 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000747 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000748 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000749 metadata_has_vorbis_comment = true;
750 }
Josh Coalson3957c472006-09-24 16:25:42 +0000751 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
752 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 +0000753 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000754 }
Josh Coalson3957c472006-09-24 16:25:42 +0000755 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
756 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000757 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000758 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
759 if(metadata_picture_has_type1) /* there should only be 1 per stream */
760 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
761 metadata_picture_has_type1 = true;
762 /* standard icon must be 32x32 pixel PNG */
763 if(
764 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
765 (
766 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
767 m->data.picture.width != 32 ||
768 m->data.picture.height != 32
769 )
770 )
771 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
772 }
773 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
774 if(metadata_picture_has_type2) /* there should only be 1 per stream */
775 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
776 metadata_picture_has_type2 = true;
777 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000778 }
Josh Coalson66075c12002-06-01 05:39:38 +0000779 }
780
Josh Coalsonfa697a92001-08-16 20:07:29 +0000781 encoder->private_->input_capacity = 0;
782 for(i = 0; i < encoder->protected_->channels; i++) {
783 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000784#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000785 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000786#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000787 }
788 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000789 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000790#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000791 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000792#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000793 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000794#ifndef FLAC__INTEGER_ONLY_LIBRARY
795 for(i = 0; i < encoder->protected_->num_apodizations; i++)
796 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
797 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
798#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000799 for(i = 0; i < encoder->protected_->channels; i++) {
800 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
801 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
802 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000803 }
804 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000805 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
806 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
807 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000808 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000809 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
810 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
811 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000812#ifndef FLAC__INTEGER_ONLY_LIBRARY
813 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
814#else
815 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
816 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
817 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
818 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
819 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
820 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
821 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);
822#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000823 if(encoder->private_->loose_mid_side_stereo_frames == 0)
824 encoder->private_->loose_mid_side_stereo_frames = 1;
825 encoder->private_->loose_mid_side_stereo_frame_count = 0;
826 encoder->private_->current_sample_number = 0;
827 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000828
Josh Coalsonfa697a92001-08-16 20:07:29 +0000829 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
830 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? */
831 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000832
Josh Coalsoncf30f502001-05-23 20:57:44 +0000833 /*
834 * get the CPU info and set the function pointers
835 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000836 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000837 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000838#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000839 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000840#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000841 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000842#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000843 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000844 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 +0000845 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000846#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000847 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000848#ifndef FLAC__INTEGER_ONLY_LIBRARY
849# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000850 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000851# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000852 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000853# ifdef FLAC__HAS_NASM
854# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000855 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000856 if(encoder->protected_->max_lpc_order < 4)
857 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
858 else if(encoder->protected_->max_lpc_order < 8)
859 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
860 else if(encoder->protected_->max_lpc_order < 12)
861 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000862 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000863 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000864 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000865 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000866# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000867 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000868 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000869 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000870 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000871 if(encoder->private_->cpuinfo.data.ia32.mmx) {
872 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
873 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 +0000874 }
875 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000876 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
877 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 +0000878 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000879 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
880 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
881# endif /* FLAC__HAS_NASM */
882# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000883 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000884# endif /* !FLAC__NO_ASM */
885#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000886 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000887 if(encoder->private_->use_wide_by_block) {
888 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000889 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000890
Josh Coalson8395d022001-07-12 21:25:22 +0000891 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000892 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 +0000893
Josh Coalson6b21f662006-09-13 01:42:27 +0000894 /* set state to OK; from here on, errors are fatal and we'll override the state then */
895 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
896
897 encoder->private_->write_callback = write_callback;
898 encoder->private_->seek_callback = seek_callback;
899 encoder->private_->tell_callback = tell_callback;
900 encoder->private_->metadata_callback = metadata_callback;
901 encoder->private_->client_data = client_data;
902
Josh Coalsonf1eff452002-07-31 07:05:33 +0000903 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000904 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +0000905 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000906 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000907
Josh Coalson6b21f662006-09-13 01:42:27 +0000908 if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
909 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
910 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
911 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000912
913 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000914 * Set up the verify stuff if necessary
915 */
916 if(encoder->protected_->verify) {
917 /*
918 * First, set up the fifo which will hold the
919 * original signal to compare against
920 */
921 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
922 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalson6b21f662006-09-13 01:42:27 +0000923 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) {
924 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
925 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
926 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000927 }
928 encoder->private_->verify.input_fifo.tail = 0;
929
930 /*
931 * Now set up a stream decoder for verification
932 */
933 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
Josh Coalson6b21f662006-09-13 01:42:27 +0000934 if(0 == encoder->private_->verify.decoder) {
935 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
936 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
937 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000938
Josh Coalson6b21f662006-09-13 01:42:27 +0000939 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) {
940 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
941 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
942 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000943 }
Josh Coalson589f8c72002-08-07 23:54:55 +0000944 encoder->private_->verify.error_stats.absolute_sample = 0;
945 encoder->private_->verify.error_stats.frame_number = 0;
946 encoder->private_->verify.error_stats.channel = 0;
947 encoder->private_->verify.error_stats.sample = 0;
948 encoder->private_->verify.error_stats.expected = 0;
949 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000950
951 /*
Josh Coalson6b21f662006-09-13 01:42:27 +0000952 * These must be done before we write any metadata, because that
953 * calls the write_callback, which uses these values.
954 */
955 encoder->private_->first_seekpoint_to_check = 0;
956 encoder->private_->samples_written = 0;
957 encoder->protected_->streaminfo_offset = 0;
958 encoder->protected_->seektable_offset = 0;
959 encoder->protected_->audio_offset = 0;
960
961 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000962 * write the stream header
963 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000964 if(encoder->protected_->verify)
965 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson6b21f662006-09-13 01:42:27 +0000966 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
967 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
968 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
969 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000970 if(!write_bitbuffer_(encoder, 0)) {
971 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +0000972 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000973 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000974
Josh Coalson5c491a12002-08-01 06:39:40 +0000975 /*
976 * write the STREAMINFO metadata block
977 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000978 if(encoder->protected_->verify)
979 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +0000980 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
981 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
982 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
983 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
984 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
985 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
986 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
987 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
988 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
989 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
990 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
991 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 +0000992 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +0000993 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
994 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
995 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
996 }
997 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
998 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
999 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1000 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001001 if(!write_bitbuffer_(encoder, 0)) {
1002 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001003 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001004 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001005
Josh Coalson5c491a12002-08-01 06:39:40 +00001006 /*
1007 * Now that the STREAMINFO block is written, we can init this to an
1008 * absurdly-high value...
1009 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001010 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 +00001011 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001012 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001013
Josh Coalson5c491a12002-08-01 06:39:40 +00001014 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001015 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1016 * if not, we will write an empty one (FLAC__add_metadata_block()
1017 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001018 *
1019 * WATCHOUT: libOggFLAC depends on us to write this block after the
1020 * STREAMINFO since that's what the mapping requires. (In the case
Josh Coalson8ddf7fb2004-12-30 00:58:50 +00001021 * that metadata_has_vorbis_comment is true it will have already
Josh Coalson69cfda72004-09-10 00:38:21 +00001022 * insured that the metadata list is properly ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001023 */
1024 if(!metadata_has_vorbis_comment) {
1025 FLAC__StreamMetadata vorbis_comment;
1026 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1027 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1028 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1029 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1030 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1031 vorbis_comment.data.vorbis_comment.num_comments = 0;
1032 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001033 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1034 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1035 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1036 }
1037 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1038 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1039 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1040 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001041 if(!write_bitbuffer_(encoder, 0)) {
1042 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001043 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001044 }
1045 }
1046
1047 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001048 * write the user's metadata blocks
1049 */
1050 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1051 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001052 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1053 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1054 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1055 }
1056 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1057 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1058 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1059 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001060 if(!write_bitbuffer_(encoder, 0)) {
1061 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001062 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001063 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001064 }
1065
Josh Coalson6b21f662006-09-13 01:42:27 +00001066 /* now that all the metadata is written, we save the stream offset */
1067 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 */
1068 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1069 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1070 }
1071
Josh Coalsond86e03b2002-08-03 21:56:15 +00001072 if(encoder->protected_->verify)
1073 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1074
Josh Coalson6b21f662006-09-13 01:42:27 +00001075 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1076}
1077
1078FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data)
1079{
1080 FLAC__StreamEncoderInitStatus init_status;
1081
1082 FLAC__ASSERT(0 != encoder);
1083 FLAC__ASSERT(0 != file);
1084
Josh Coalson6b21f662006-09-13 01:42:27 +00001085 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1086 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1087
1088 /* double protection */
1089 if(file == 0) {
1090 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1091 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1092 }
1093
Josh Coalson92f7fa92006-10-09 05:34:21 +00001094 /*
1095 * To make sure that our file does not go unclosed after an error, we
1096 * must assign the FILE pointer before any further error can occur in
1097 * this routine.
1098 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001099 if(file == stdout)
1100 file = get_binary_stdout_(); /* just to be safe */
1101
1102 encoder->private_->file = file;
1103
1104 encoder->private_->progress_callback = progress_callback;
1105 encoder->private_->bytes_written = 0;
1106 encoder->private_->samples_written = 0;
1107 encoder->private_->frames_written = 0;
1108
1109 init_status = FLAC__stream_encoder_init_stream(encoder, file_write_callback_, file_seek_callback_, file_tell_callback_, /*metadata_callback=*/0, client_data);
1110 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1111 /* the above function sets the state for us in case of an error */
1112 return init_status;
1113 }
1114
1115 {
1116 unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1117
1118 FLAC__ASSERT(blocksize != 0);
1119 encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1120 }
1121
1122 return init_status;
1123}
1124
1125FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data)
1126{
1127 FILE *file;
1128
1129 FLAC__ASSERT(0 != encoder);
1130
1131 /*
1132 * To make sure that our file does not go unclosed after an error, we
1133 * have to do the same entrance checks here that are later performed
1134 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1135 */
1136 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1137 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1138
1139 file = filename? fopen(filename, "w+b") : stdout;
1140
1141 if(file == 0) {
1142 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1143 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1144 }
1145
1146 return FLAC__stream_encoder_init_FILE(encoder, file, progress_callback, client_data);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001147}
1148
Josh Coalson6afed9f2002-10-16 22:29:47 +00001149FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001150{
Josh Coalsonf1eff452002-07-31 07:05:33 +00001151 FLAC__ASSERT(0 != encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001152 FLAC__ASSERT(0 != encoder->private_);
1153 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001154
Josh Coalsonfa697a92001-08-16 20:07:29 +00001155 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001156 return;
Josh Coalson2b245f22002-08-07 17:10:50 +00001157
Josh Coalson3262b0d2002-08-14 20:58:42 +00001158 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001159 if(encoder->private_->current_sample_number != 0) {
1160 encoder->protected_->blocksize = encoder->private_->current_sample_number;
1161 process_frame_(encoder, true); /* true => is last frame */
1162 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001163 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001164
Josh Coalson6b21f662006-09-13 01:42:27 +00001165 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001166
Josh Coalson3262b0d2002-08-14 20:58:42 +00001167 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001168 if(encoder->private_->seek_callback)
1169 update_metadata_(encoder);
1170 if(encoder->private_->metadata_callback)
1171 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
Josh Coalson2b245f22002-08-07 17:10:50 +00001172 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001173
Josh Coalsond86e03b2002-08-03 21:56:15 +00001174 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
1175 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1176
Josh Coalson6b21f662006-09-13 01:42:27 +00001177 if(0 != encoder->private_->file) {
1178 if(encoder->private_->file != stdout)
1179 fclose(encoder->private_->file);
1180 encoder->private_->file = 0;
1181 }
1182
Josh Coalsonf1eff452002-07-31 07:05:33 +00001183 free_(encoder);
1184 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001185
Josh Coalsonfa697a92001-08-16 20:07:29 +00001186 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001187}
1188
Josh Coalson6afed9f2002-10-16 22:29:47 +00001189FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001190{
1191 FLAC__ASSERT(0 != encoder);
1192 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1193 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001194#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001195 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001196#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001197 return true;
1198}
1199
Josh Coalson6afed9f2002-10-16 22:29:47 +00001200FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001201{
Josh Coalson92031602002-07-24 06:02:11 +00001202 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001203 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001204 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001205 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001206 return true;
1207}
1208
Josh Coalson6afed9f2002-10-16 22:29:47 +00001209FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001210{
Josh Coalson92031602002-07-24 06:02:11 +00001211 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001212 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001213 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001214 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001215 return true;
1216}
1217
Josh Coalson6afed9f2002-10-16 22:29:47 +00001218FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001219{
Josh Coalson92031602002-07-24 06:02:11 +00001220 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001221 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001222 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001223 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001224 return true;
1225}
1226
Josh Coalson6afed9f2002-10-16 22:29:47 +00001227FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001228{
Josh Coalson92031602002-07-24 06:02:11 +00001229 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001230 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001231 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001232 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001233 return true;
1234}
1235
Josh Coalson6afed9f2002-10-16 22:29:47 +00001236FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001237{
Josh Coalson92031602002-07-24 06:02:11 +00001238 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001239 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001240 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001241 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001242 return true;
1243}
1244
Josh Coalson6afed9f2002-10-16 22:29:47 +00001245FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001246{
Josh Coalson92031602002-07-24 06:02:11 +00001247 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001248 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001249 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001250 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001251 return true;
1252}
1253
Josh Coalson6afed9f2002-10-16 22:29:47 +00001254FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001255{
Josh Coalson92031602002-07-24 06:02:11 +00001256 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001257 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001258 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001259 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001260 return true;
1261}
1262
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001263FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1264{
1265 FLAC__ASSERT(0 != encoder);
1266 FLAC__ASSERT(0 != specification);
1267 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1268 return false;
1269#ifdef FLAC__INTEGER_ONLY_LIBRARY
1270 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1271#else
1272 encoder->protected_->num_apodizations = 0;
1273 while(1) {
1274 const char *s = strchr(specification, ';');
1275 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1276 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1277 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1278 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1279 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1280 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1281 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1282 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1283 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1284 else if(n==6 && 0 == strncmp("connes" , specification, n))
1285 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1286 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1287 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1288 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1289 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1290 if (stddev > 0.0 && stddev <= 0.5) {
1291 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1292 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1293 }
1294 }
1295 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1296 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1297 else if(n==4 && 0 == strncmp("hann" , specification, n))
1298 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1299 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1300 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1301 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1302 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1303 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1304 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1305 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1306 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1307 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1308 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1309 if (p >= 0.0 && p <= 1.0) {
1310 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1311 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1312 }
1313 }
1314 else if(n==5 && 0 == strncmp("welch" , specification, n))
1315 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1316 if (encoder->protected_->num_apodizations == 32)
1317 break;
1318 if (s)
1319 specification = s+1;
1320 else
1321 break;
1322 }
1323 if(encoder->protected_->num_apodizations == 0) {
1324 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001325 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1326 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001327 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001328#endif
1329 return true;
1330}
1331
Josh Coalson6afed9f2002-10-16 22:29:47 +00001332FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001333{
Josh Coalson92031602002-07-24 06:02:11 +00001334 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001335 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001336 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001337 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001338 return true;
1339}
1340
Josh Coalson6afed9f2002-10-16 22:29:47 +00001341FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001342{
Josh Coalson92031602002-07-24 06:02:11 +00001343 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001344 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001345 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001346 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001347 return true;
1348}
1349
Josh Coalson6afed9f2002-10-16 22:29:47 +00001350FLAC_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 +00001351{
Josh Coalson92031602002-07-24 06:02:11 +00001352 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001353 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001354 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001355 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001356 return true;
1357}
1358
Josh Coalson6afed9f2002-10-16 22:29:47 +00001359FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +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 Coalson8395d022001-07-12 21:25:22 +00001363 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001364#if 0
1365 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001366 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001367#else
1368 (void)value;
1369#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001370 return true;
1371}
1372
Josh Coalson6afed9f2002-10-16 22:29:47 +00001373FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001374{
Josh Coalson92031602002-07-24 06:02:11 +00001375 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001376 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001377 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001378 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001379 return true;
1380}
1381
Josh Coalson6afed9f2002-10-16 22:29:47 +00001382FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001383{
Josh Coalson92031602002-07-24 06:02:11 +00001384 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001385 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001386 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001387 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001388 return true;
1389}
1390
Josh Coalson6afed9f2002-10-16 22:29:47 +00001391FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001392{
Josh Coalson92031602002-07-24 06:02:11 +00001393 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001394 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001395 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001396 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001397 return true;
1398}
1399
Josh Coalson6afed9f2002-10-16 22:29:47 +00001400FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned 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 Coalson680e3aa2002-08-01 07:32:17 +00001405#if 0
1406 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001407 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001408#else
1409 (void)value;
1410#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001411 return true;
1412}
1413
Josh Coalson6afed9f2002-10-16 22:29:47 +00001414FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001415{
Josh Coalson92031602002-07-24 06:02:11 +00001416 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001417 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001418 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001419 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001420 return true;
1421}
1422
Josh Coalson6afed9f2002-10-16 22:29:47 +00001423FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001424{
Josh Coalson92031602002-07-24 06:02:11 +00001425 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001426 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001427 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001428 encoder->protected_->metadata = metadata;
1429 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson6b21f662006-09-13 01:42:27 +00001430 if(0 != metadata && num_blocks > 0) {
1431 unsigned i;
1432 for(i = 0; i < num_blocks; i++) {
1433 if(0 != metadata[i] && metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
1434 encoder->private_->seek_table = &metadata[i]->data.seek_table;
1435 break; /* take only the first one */
1436 }
1437 }
1438 }
Josh Coalson00e53872001-06-16 07:32:25 +00001439 return true;
1440}
1441
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001442/*
1443 * These three functions are not static, but not publically exposed in
1444 * include/FLAC/ either. They are used by the test suite.
1445 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001446FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001447{
1448 FLAC__ASSERT(0 != encoder);
1449 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1450 return false;
1451 encoder->private_->disable_constant_subframes = value;
1452 return true;
1453}
1454
Josh Coalson6afed9f2002-10-16 22:29:47 +00001455FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001456{
1457 FLAC__ASSERT(0 != encoder);
1458 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1459 return false;
1460 encoder->private_->disable_fixed_subframes = value;
1461 return true;
1462}
1463
Josh Coalson6afed9f2002-10-16 22:29:47 +00001464FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001465{
1466 FLAC__ASSERT(0 != encoder);
1467 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1468 return false;
1469 encoder->private_->disable_verbatim_subframes = value;
1470 return true;
1471}
1472
Josh Coalson6afed9f2002-10-16 22:29:47 +00001473FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001474{
Josh Coalson92031602002-07-24 06:02:11 +00001475 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001476 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001477}
1478
Josh Coalson6afed9f2002-10-16 22:29:47 +00001479FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001480{
1481 FLAC__ASSERT(0 != encoder);
1482 if(encoder->protected_->verify)
1483 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1484 else
1485 return FLAC__STREAM_DECODER_UNINITIALIZED;
1486}
1487
Josh Coalson02954222002-11-08 06:16:31 +00001488FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1489{
1490 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1491 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1492 else
Josh Coalson807140d2003-09-24 22:10:51 +00001493 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001494}
1495
Josh Coalson6afed9f2002-10-16 22:29:47 +00001496FLAC_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 +00001497{
1498 FLAC__ASSERT(0 != encoder);
1499 if(0 != absolute_sample)
1500 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1501 if(0 != frame_number)
1502 *frame_number = encoder->private_->verify.error_stats.frame_number;
1503 if(0 != channel)
1504 *channel = encoder->private_->verify.error_stats.channel;
1505 if(0 != sample)
1506 *sample = encoder->private_->verify.error_stats.sample;
1507 if(0 != expected)
1508 *expected = encoder->private_->verify.error_stats.expected;
1509 if(0 != got)
1510 *got = encoder->private_->verify.error_stats.got;
1511}
1512
Josh Coalson6afed9f2002-10-16 22:29:47 +00001513FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001514{
1515 FLAC__ASSERT(0 != encoder);
1516 return encoder->protected_->verify;
1517}
1518
Josh Coalson6afed9f2002-10-16 22:29:47 +00001519FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001520{
Josh Coalson92031602002-07-24 06:02:11 +00001521 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001522 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001523}
1524
Josh Coalson6afed9f2002-10-16 22:29:47 +00001525FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001526{
Josh Coalson92031602002-07-24 06:02:11 +00001527 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001528 return encoder->protected_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001529}
1530
Josh Coalson6afed9f2002-10-16 22:29:47 +00001531FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001532{
Josh Coalson92031602002-07-24 06:02:11 +00001533 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001534 return encoder->protected_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001535}
1536
Josh Coalson6afed9f2002-10-16 22:29:47 +00001537FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001538{
Josh Coalson92031602002-07-24 06:02:11 +00001539 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001540 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001541}
1542
Josh Coalson6afed9f2002-10-16 22:29:47 +00001543FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001544{
Josh Coalson92031602002-07-24 06:02:11 +00001545 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001546 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001547}
1548
Josh Coalson6afed9f2002-10-16 22:29:47 +00001549FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001550{
Josh Coalson92031602002-07-24 06:02:11 +00001551 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001552 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001553}
1554
Josh Coalson6afed9f2002-10-16 22:29:47 +00001555FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001556{
Josh Coalson92031602002-07-24 06:02:11 +00001557 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001558 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001559}
1560
Josh Coalson6afed9f2002-10-16 22:29:47 +00001561FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001562{
Josh Coalson92031602002-07-24 06:02:11 +00001563 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001564 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001565}
1566
Josh Coalson6afed9f2002-10-16 22:29:47 +00001567FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001568{
Josh Coalson92031602002-07-24 06:02:11 +00001569 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001570 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001571}
1572
Josh Coalson6afed9f2002-10-16 22:29:47 +00001573FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001574{
Josh Coalson92031602002-07-24 06:02:11 +00001575 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001576 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001577}
1578
Josh Coalson6afed9f2002-10-16 22:29:47 +00001579FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001580{
Josh Coalson92031602002-07-24 06:02:11 +00001581 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001582 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001583}
1584
Josh Coalson6afed9f2002-10-16 22:29:47 +00001585FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001586{
Josh Coalson92031602002-07-24 06:02:11 +00001587 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001588 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001589}
1590
Josh Coalson6afed9f2002-10-16 22:29:47 +00001591FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001592{
Josh Coalson92031602002-07-24 06:02:11 +00001593 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001594 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001595}
1596
Josh Coalson6afed9f2002-10-16 22:29:47 +00001597FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001598{
Josh Coalson92031602002-07-24 06:02:11 +00001599 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001600 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001601}
1602
Josh Coalson6afed9f2002-10-16 22:29:47 +00001603FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001604{
Josh Coalson92031602002-07-24 06:02:11 +00001605 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001606 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001607}
1608
Josh Coalson6afed9f2002-10-16 22:29:47 +00001609FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001610{
1611 FLAC__ASSERT(0 != encoder);
1612 return encoder->protected_->total_samples_estimate;
1613}
1614
Josh Coalson6afed9f2002-10-16 22:29:47 +00001615FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001616{
1617 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001618 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001619 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001620
Josh Coalsonf1eff452002-07-31 07:05:33 +00001621 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001622 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001623
1624 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001625 /*
1626 * we have several flavors of the same basic loop, optimized for
1627 * different conditions:
1628 */
1629 if(encoder->protected_->max_lpc_order > 0) {
1630 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1631 /*
1632 * stereo coding: unroll channel loop
1633 * with LPC: calculate floating point version of signal
1634 */
1635 do {
1636 if(encoder->protected_->verify)
1637 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 +00001638
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001639 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1640 x = mid = side = buffer[0][j];
1641 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001642#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001643 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001644#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001645 x = buffer[1][j];
1646 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001647#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001648 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001649#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001650 mid += x;
1651 side -= x;
1652 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1653 encoder->private_->integer_signal_mid_side[1][i] = side;
1654 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001655#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001656 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1657 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001658#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001659 encoder->private_->current_sample_number++;
1660 }
1661 if(i == blocksize) {
1662 if(!process_frame_(encoder, false)) /* false => not last frame */
1663 return false;
1664 }
1665 } while(j < samples);
1666 }
1667 else {
1668 /*
1669 * independent channel coding: buffer each channel in inner loop
1670 * with LPC: calculate floating point version of signal
1671 */
1672 do {
1673 if(encoder->protected_->verify)
1674 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1675
1676 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1677 for(channel = 0; channel < channels; channel++) {
1678 x = buffer[channel][j];
1679 encoder->private_->integer_signal[channel][i] = x;
1680#ifndef FLAC__INTEGER_ONLY_LIBRARY
1681 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1682#endif
1683 }
1684 encoder->private_->current_sample_number++;
1685 }
1686 if(i == blocksize) {
1687 if(!process_frame_(encoder, false)) /* false => not last frame */
1688 return false;
1689 }
1690 } while(j < samples);
1691 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001692 }
1693 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001694 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1695 /*
1696 * stereo coding: unroll channel loop
1697 * without LPC: no need to 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));
Josh Coalsond86e03b2002-08-03 21:56:15 +00001702
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001703 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1704 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1705 x = buffer[1][j];
1706 encoder->private_->integer_signal[1][i] = x;
1707 mid += x;
1708 side -= x;
1709 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1710 encoder->private_->integer_signal_mid_side[1][i] = side;
1711 encoder->private_->integer_signal_mid_side[0][i] = mid;
1712 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001713 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001714 if(i == blocksize) {
1715 if(!process_frame_(encoder, false)) /* false => not last frame */
1716 return false;
1717 }
1718 } while(j < samples);
1719 }
1720 else {
1721 /*
1722 * independent channel coding: buffer each channel in inner loop
1723 * without LPC: no need to calculate floating point version of signal
1724 */
1725 do {
1726 if(encoder->protected_->verify)
1727 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1728
1729 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1730 for(channel = 0; channel < channels; channel++)
1731 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1732 encoder->private_->current_sample_number++;
1733 }
1734 if(i == blocksize) {
1735 if(!process_frame_(encoder, false)) /* false => not last frame */
1736 return false;
1737 }
1738 } while(j < samples);
1739 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001740 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001741
1742 return true;
1743}
1744
Josh Coalson6afed9f2002-10-16 22:29:47 +00001745FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001746{
1747 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001748 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001749 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001750
Josh Coalsonf1eff452002-07-31 07:05:33 +00001751 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001752 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001753
1754 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001755 /*
1756 * we have several flavors of the same basic loop, optimized for
1757 * different conditions:
1758 */
1759 if(encoder->protected_->max_lpc_order > 0) {
1760 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1761 /*
1762 * stereo coding: unroll channel loop
1763 * with LPC: calculate floating point version of signal
1764 */
1765 do {
1766 if(encoder->protected_->verify)
1767 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 +00001768
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001769 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1770 x = mid = side = buffer[k++];
1771 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001772#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001773 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001774#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001775 x = buffer[k++];
1776 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001777#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001778 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001779#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001780 mid += x;
1781 side -= x;
1782 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1783 encoder->private_->integer_signal_mid_side[1][i] = side;
1784 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001785#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001786 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1787 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001788#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001789 encoder->private_->current_sample_number++;
1790 }
1791 if(i == blocksize) {
1792 if(!process_frame_(encoder, false)) /* false => not last frame */
1793 return false;
1794 }
1795 } while(j < samples);
1796 }
1797 else {
1798 /*
1799 * independent channel coding: buffer each channel in inner loop
1800 * with LPC: calculate floating point version of signal
1801 */
1802 do {
1803 if(encoder->protected_->verify)
1804 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1805
1806 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1807 for(channel = 0; channel < channels; channel++) {
1808 x = buffer[k++];
1809 encoder->private_->integer_signal[channel][i] = x;
1810#ifndef FLAC__INTEGER_ONLY_LIBRARY
1811 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1812#endif
1813 }
1814 encoder->private_->current_sample_number++;
1815 }
1816 if(i == blocksize) {
1817 if(!process_frame_(encoder, false)) /* false => not last frame */
1818 return false;
1819 }
1820 } while(j < samples);
1821 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001822 }
1823 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001824 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1825 /*
1826 * stereo coding: unroll channel loop
1827 * without LPC: no need to 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));
Josh Coalsond86e03b2002-08-03 21:56:15 +00001832
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001833 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1834 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00001835 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001836 encoder->private_->integer_signal[1][i] = x;
1837 mid += x;
1838 side -= x;
1839 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1840 encoder->private_->integer_signal_mid_side[1][i] = side;
1841 encoder->private_->integer_signal_mid_side[0][i] = mid;
1842 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001843 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001844 if(i == blocksize) {
1845 if(!process_frame_(encoder, false)) /* false => not last frame */
1846 return false;
1847 }
1848 } while(j < samples);
1849 }
1850 else {
1851 /*
1852 * independent channel coding: buffer each channel in inner loop
1853 * without LPC: no need to calculate floating point version of signal
1854 */
1855 do {
1856 if(encoder->protected_->verify)
1857 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1858
1859 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1860 for(channel = 0; channel < channels; channel++)
1861 encoder->private_->integer_signal[channel][i] = buffer[k++];
1862 encoder->private_->current_sample_number++;
1863 }
1864 if(i == blocksize) {
1865 if(!process_frame_(encoder, false)) /* false => not last frame */
1866 return false;
1867 }
1868 } while(j < samples);
1869 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001870 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001871
1872 return true;
1873}
1874
Josh Coalsonf1eff452002-07-31 07:05:33 +00001875/***********************************************************************
1876 *
1877 * Private class methods
1878 *
1879 ***********************************************************************/
1880
1881void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00001882{
1883 FLAC__ASSERT(0 != encoder);
1884
Josh Coalson47c7b142005-01-29 06:08:58 +00001885#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1886 encoder->protected_->verify = true;
1887#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00001888 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001889#endif
Josh Coalson92031602002-07-24 06:02:11 +00001890 encoder->protected_->streamable_subset = true;
1891 encoder->protected_->do_mid_side_stereo = false;
1892 encoder->protected_->loose_mid_side_stereo = false;
1893 encoder->protected_->channels = 2;
1894 encoder->protected_->bits_per_sample = 16;
1895 encoder->protected_->sample_rate = 44100;
1896 encoder->protected_->blocksize = 1152;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001897#ifndef FLAC__INTEGER_ONLY_LIBRARY
1898 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001899 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1900 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001901#endif
Josh Coalson92031602002-07-24 06:02:11 +00001902 encoder->protected_->max_lpc_order = 0;
1903 encoder->protected_->qlp_coeff_precision = 0;
1904 encoder->protected_->do_qlp_coeff_prec_search = false;
1905 encoder->protected_->do_exhaustive_model_search = false;
1906 encoder->protected_->do_escape_coding = false;
1907 encoder->protected_->min_residual_partition_order = 0;
1908 encoder->protected_->max_residual_partition_order = 0;
1909 encoder->protected_->rice_parameter_search_dist = 0;
1910 encoder->protected_->total_samples_estimate = 0;
1911 encoder->protected_->metadata = 0;
1912 encoder->protected_->num_metadata_blocks = 0;
1913
Josh Coalson6b21f662006-09-13 01:42:27 +00001914 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001915 encoder->private_->disable_constant_subframes = false;
1916 encoder->private_->disable_fixed_subframes = false;
1917 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson92031602002-07-24 06:02:11 +00001918 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001919 encoder->private_->seek_callback = 0;
1920 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00001921 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001922 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00001923 encoder->private_->client_data = 0;
1924}
1925
Josh Coalsonf1eff452002-07-31 07:05:33 +00001926void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00001927{
1928 unsigned i, channel;
1929
Josh Coalsonf1eff452002-07-31 07:05:33 +00001930 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00001931 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001932 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001933 free(encoder->private_->integer_signal_unaligned[i]);
1934 encoder->private_->integer_signal_unaligned[i] = 0;
1935 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001936#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00001937 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001938 free(encoder->private_->real_signal_unaligned[i]);
1939 encoder->private_->real_signal_unaligned[i] = 0;
1940 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001941#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001942 }
1943 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001944 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001945 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1946 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1947 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001948#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00001949 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001950 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1951 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1952 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001953#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001954 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001955#ifndef FLAC__INTEGER_ONLY_LIBRARY
1956 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
1957 if(0 != encoder->private_->window_unaligned[i]) {
1958 free(encoder->private_->window_unaligned[i]);
1959 encoder->private_->window_unaligned[i] = 0;
1960 }
1961 }
1962 if(0 != encoder->private_->windowed_signal_unaligned) {
1963 free(encoder->private_->windowed_signal_unaligned);
1964 encoder->private_->windowed_signal_unaligned = 0;
1965 }
1966#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001967 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1968 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001969 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001970 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1971 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1972 }
1973 }
1974 }
1975 for(channel = 0; channel < 2; channel++) {
1976 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001977 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001978 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1979 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1980 }
1981 }
1982 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001983 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001984 free(encoder->private_->abs_residual_unaligned);
1985 encoder->private_->abs_residual_unaligned = 0;
1986 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001987 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001988 free(encoder->private_->abs_residual_partition_sums_unaligned);
1989 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1990 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001991 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001992 free(encoder->private_->raw_bits_per_partition_unaligned);
1993 encoder->private_->raw_bits_per_partition_unaligned = 0;
1994 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001995 if(encoder->protected_->verify) {
1996 for(i = 0; i < encoder->protected_->channels; i++) {
1997 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1998 free(encoder->private_->verify.input_fifo.data[i]);
1999 encoder->private_->verify.input_fifo.data[i] = 0;
2000 }
2001 }
2002 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002003 FLAC__bitbuffer_free(encoder->private_->frame);
2004}
2005
Josh Coalsonf1eff452002-07-31 07:05:33 +00002006FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002007{
Josh Coalson77e3f312001-06-23 03:03:24 +00002008 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00002009 unsigned i, channel;
2010
2011 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002012 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2013 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002014
2015 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002016 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002017 return true;
2018
2019 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002020
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002021 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
2022 * requires that the input arrays (in our case the integer signals)
2023 * have a buffer of up to 3 zeroes in front (at negative indices) for
2024 * alignment purposes; we use 4 to keep the data well-aligned.
2025 */
Josh Coalson8395d022001-07-12 21:25:22 +00002026
Josh Coalsonfa697a92001-08-16 20:07:29 +00002027 for(i = 0; ok && i < encoder->protected_->channels; i++) {
2028 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 +00002029#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002030 if(encoder->protected_->max_lpc_order > 0)
2031 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 +00002032#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002033 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2034 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00002035 }
2036 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002037 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 +00002038#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002039 if(encoder->protected_->max_lpc_order > 0)
2040 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 +00002041#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002042 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2043 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00002044 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002045#ifndef FLAC__INTEGER_ONLY_LIBRARY
2046 if(ok && encoder->protected_->max_lpc_order > 0) {
2047 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2048 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2049 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
2050 }
2051#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002052 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002053 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002054 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 +00002055 }
2056 }
2057 for(channel = 0; ok && channel < 2; channel++) {
2058 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002059 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 +00002060 }
2061 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002062 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
2063 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 */
2064 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2065 if(encoder->protected_->do_escape_coding)
2066 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 +00002067
2068 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00002069 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00002070 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002071 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00002072
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002073#ifndef FLAC__INTEGER_ONLY_LIBRARY
2074 if(ok && encoder->protected_->max_lpc_order > 0) {
2075 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2076 switch(encoder->protected_->apodizations[i].type) {
2077 case FLAC__APODIZATION_BARTLETT:
2078 FLAC__window_bartlett(encoder->private_->window[i], new_size);
2079 break;
2080 case FLAC__APODIZATION_BARTLETT_HANN:
2081 FLAC__window_bartlett_hann(encoder->private_->window[i], new_size);
2082 break;
2083 case FLAC__APODIZATION_BLACKMAN:
2084 FLAC__window_blackman(encoder->private_->window[i], new_size);
2085 break;
2086 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2087 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_size);
2088 break;
2089 case FLAC__APODIZATION_CONNES:
2090 FLAC__window_connes(encoder->private_->window[i], new_size);
2091 break;
2092 case FLAC__APODIZATION_FLATTOP:
2093 FLAC__window_flattop(encoder->private_->window[i], new_size);
2094 break;
2095 case FLAC__APODIZATION_GAUSS:
2096 FLAC__window_gauss(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2097 break;
2098 case FLAC__APODIZATION_HAMMING:
2099 FLAC__window_hamming(encoder->private_->window[i], new_size);
2100 break;
2101 case FLAC__APODIZATION_HANN:
2102 FLAC__window_hann(encoder->private_->window[i], new_size);
2103 break;
2104 case FLAC__APODIZATION_KAISER_BESSEL:
2105 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_size);
2106 break;
2107 case FLAC__APODIZATION_NUTTALL:
2108 FLAC__window_nuttall(encoder->private_->window[i], new_size);
2109 break;
2110 case FLAC__APODIZATION_RECTANGLE:
2111 FLAC__window_rectangle(encoder->private_->window[i], new_size);
2112 break;
2113 case FLAC__APODIZATION_TRIANGLE:
2114 FLAC__window_triangle(encoder->private_->window[i], new_size);
2115 break;
2116 case FLAC__APODIZATION_TUKEY:
2117 FLAC__window_tukey(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.tukey.p);
2118 break;
2119 case FLAC__APODIZATION_WELCH:
2120 FLAC__window_welch(encoder->private_->window[i], new_size);
2121 break;
2122 default:
2123 FLAC__ASSERT(0);
2124 /* double protection */
Josh Coalsondf598452006-04-28 00:13:34 +00002125 FLAC__window_hann(encoder->private_->window[i], new_size);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002126 break;
2127 }
2128 }
2129 }
2130#endif
2131
Josh Coalson0a15c142001-06-13 17:59:57 +00002132 return ok;
2133}
2134
Josh Coalsond86e03b2002-08-03 21:56:15 +00002135FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00002136{
2137 const FLAC__byte *buffer;
2138 unsigned bytes;
2139
2140 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2141
2142 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2143
Josh Coalsond86e03b2002-08-03 21:56:15 +00002144 if(encoder->protected_->verify) {
2145 encoder->private_->verify.output.data = buffer;
2146 encoder->private_->verify.output.bytes = bytes;
2147 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2148 encoder->private_->verify.needs_magic_hack = true;
2149 }
2150 else {
2151 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
2152 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2153 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2154 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2155 return false;
2156 }
2157 }
2158 }
2159
Josh Coalson6b21f662006-09-13 01:42:27 +00002160 if(write_frame_(encoder, buffer, bytes, samples) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00002161 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002162 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002163 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002164 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002165
2166 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2167
Josh Coalsond86e03b2002-08-03 21:56:15 +00002168 if(samples > 0) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002169 encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2170 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 +00002171 }
2172
Josh Coalson5c491a12002-08-01 06:39:40 +00002173 return true;
2174}
2175
Josh Coalson6b21f662006-09-13 01:42:27 +00002176FLAC__StreamEncoderWriteStatus write_frame_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples)
2177{
2178 FLAC__StreamEncoderWriteStatus status;
2179 FLAC__uint64 output_position = 0;
2180
2181 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2182 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2183 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2184 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2185 }
2186
2187 /*
2188 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2189 */
2190 if(samples == 0) {
2191 FLAC__MetadataType type = (buffer[0] & 0x7f);
2192 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2193 encoder->protected_->streaminfo_offset = output_position;
2194 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2195 encoder->protected_->seektable_offset = output_position;
2196 }
2197
2198 /*
2199 * Mark the current seek point if hit (if audio_offset == 0 that
2200 * means we're still writing metadata and haven't hit the first
2201 * frame yet)
2202 */
2203 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2204 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2205 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2206 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2207 FLAC__uint64 test_sample;
2208 unsigned i;
2209 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2210 test_sample = encoder->private_->seek_table->points[i].sample_number;
2211 if(test_sample > frame_last_sample) {
2212 break;
2213 }
2214 else if(test_sample >= frame_first_sample) {
2215 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2216 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2217 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2218 encoder->private_->first_seekpoint_to_check++;
2219 /* DO NOT: "break;" and here's why:
2220 * The seektable template may contain more than one target
2221 * sample for any given frame; we will keep looping, generating
2222 * duplicate seekpoints for them, and we'll clean it up later,
2223 * just before writing the seektable back to the metadata.
2224 */
2225 }
2226 else {
2227 encoder->private_->first_seekpoint_to_check++;
2228 }
2229 }
2230 }
2231
2232 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2233
2234 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2235 encoder->private_->bytes_written += bytes;
2236 encoder->private_->samples_written += samples;
2237 /* we keep a high watermark on the number of frames written because
2238 * when the encoder goes back to write metadata, 'current_frame'
2239 * will drop back to 0.
2240 */
2241 encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2242 }
2243 else
2244 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2245
2246 return status;
2247}
2248
2249/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2250void update_metadata_(const FLAC__StreamEncoder *encoder)
2251{
2252 FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2253 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2254 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2255 const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2256 const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2257 const unsigned bps = metadata->data.stream_info.bits_per_sample;
2258 FLAC__StreamEncoderSeekStatus seek_status;
2259
2260 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2261
2262 /* All this is based on intimate knowledge of the stream header
2263 * layout, but a change to the header format that would break this
2264 * would also break all streams encoded in the previous format.
2265 */
2266
2267 /*
2268 * Write MD5 signature
2269 */
2270 {
2271 const unsigned md5_offset =
2272 FLAC__STREAM_METADATA_HEADER_LENGTH +
2273 (
2274 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2275 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2276 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2277 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2278 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2279 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2280 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2281 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2282 ) / 8;
2283
2284 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2285 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2286 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2287 return;
2288 }
2289 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2290 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2291 return;
2292 }
2293 }
2294
2295 /*
2296 * Write total samples
2297 */
2298 {
2299 const unsigned total_samples_byte_offset =
2300 FLAC__STREAM_METADATA_HEADER_LENGTH +
2301 (
2302 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2303 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2304 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2305 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2306 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2307 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2308 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2309 - 4
2310 ) / 8;
2311
2312 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2313 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2314 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2315 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2316 b[4] = (FLAC__byte)(samples & 0xFF);
2317 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) {
2318 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2319 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2320 return;
2321 }
2322 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2323 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2324 return;
2325 }
2326 }
2327
2328 /*
2329 * Write min/max framesize
2330 */
2331 {
2332 const unsigned min_framesize_offset =
2333 FLAC__STREAM_METADATA_HEADER_LENGTH +
2334 (
2335 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2336 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2337 ) / 8;
2338
2339 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2340 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2341 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2342 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2343 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2344 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2345 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) {
2346 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2347 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2348 return;
2349 }
2350 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2351 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2352 return;
2353 }
2354 }
2355
2356 /*
2357 * Write seektable
2358 */
2359 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2360 unsigned i;
2361
2362 FLAC__format_seektable_sort(encoder->private_->seek_table);
2363
2364 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2365
2366 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) {
2367 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2368 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2369 return;
2370 }
2371
2372 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2373 FLAC__uint64 xx;
2374 unsigned x;
2375 xx = encoder->private_->seek_table->points[i].sample_number;
2376 b[7] = (FLAC__byte)xx; xx >>= 8;
2377 b[6] = (FLAC__byte)xx; xx >>= 8;
2378 b[5] = (FLAC__byte)xx; xx >>= 8;
2379 b[4] = (FLAC__byte)xx; xx >>= 8;
2380 b[3] = (FLAC__byte)xx; xx >>= 8;
2381 b[2] = (FLAC__byte)xx; xx >>= 8;
2382 b[1] = (FLAC__byte)xx; xx >>= 8;
2383 b[0] = (FLAC__byte)xx; xx >>= 8;
2384 xx = encoder->private_->seek_table->points[i].stream_offset;
2385 b[15] = (FLAC__byte)xx; xx >>= 8;
2386 b[14] = (FLAC__byte)xx; xx >>= 8;
2387 b[13] = (FLAC__byte)xx; xx >>= 8;
2388 b[12] = (FLAC__byte)xx; xx >>= 8;
2389 b[11] = (FLAC__byte)xx; xx >>= 8;
2390 b[10] = (FLAC__byte)xx; xx >>= 8;
2391 b[9] = (FLAC__byte)xx; xx >>= 8;
2392 b[8] = (FLAC__byte)xx; xx >>= 8;
2393 x = encoder->private_->seek_table->points[i].frame_samples;
2394 b[17] = (FLAC__byte)x; x >>= 8;
2395 b[16] = (FLAC__byte)x; x >>= 8;
2396 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2397 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2398 return;
2399 }
2400 }
2401 }
2402}
2403
Josh Coalsonf1eff452002-07-31 07:05:33 +00002404FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00002405{
Josh Coalsonfa697a92001-08-16 20:07:29 +00002406 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002407
2408 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002409 * Accumulate raw signal to the MD5 signature
2410 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00002411 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 +00002412 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00002413 return false;
2414 }
2415
2416 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002417 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002418 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00002419 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002420 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002421 return false;
2422 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002423
2424 /*
2425 * Zero-pad the frame to a byte_boundary
2426 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002427 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002428 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002429 return false;
2430 }
2431
2432 /*
Josh Coalson215af572001-03-27 01:15:58 +00002433 * CRC-16 the whole thing
2434 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002435 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
2436 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 +00002437
2438 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002439 * Write it
2440 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00002441 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
2442 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002443 return false;
2444 }
2445
2446 /*
2447 * Get ready for the next frame
2448 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002449 encoder->private_->current_sample_number = 0;
2450 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00002451 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002452
2453 return true;
2454}
2455
Josh Coalsonf1eff452002-07-31 07:05:33 +00002456FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002457{
2458 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002459 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002460 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002461
2462 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00002463 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00002464 */
2465 if(is_last_frame) {
2466 max_partition_order = 0;
2467 }
2468 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002469 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
2470 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002471 }
Josh Coalson60f77d72001-04-25 02:16:36 +00002472 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002473
Josh Coalsonfa697a92001-08-16 20:07:29 +00002474 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 +00002475
Josh Coalson94e02cd2001-01-25 10:41:06 +00002476 /*
2477 * Setup the frame
2478 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002479 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002480 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002481 return false;
2482 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00002483 frame_header.blocksize = encoder->protected_->blocksize;
2484 frame_header.sample_rate = encoder->protected_->sample_rate;
2485 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002486 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002487 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002488 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002489 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002490
2491 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002492 * Figure out what channel assignments to try
2493 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002494 if(encoder->protected_->do_mid_side_stereo) {
2495 if(encoder->protected_->loose_mid_side_stereo) {
2496 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002497 do_independent = true;
2498 do_mid_side = true;
2499 }
2500 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002501 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002502 do_mid_side = !do_independent;
2503 }
2504 }
2505 else {
2506 do_independent = true;
2507 do_mid_side = true;
2508 }
2509 }
2510 else {
2511 do_independent = true;
2512 do_mid_side = false;
2513 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002514
Josh Coalson1b689822001-05-31 20:11:02 +00002515 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002516
2517 /*
Josh Coalson82b73242001-03-28 22:17:05 +00002518 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00002519 */
2520 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002521 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002522 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002523 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
2524 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00002525 }
Josh Coalson859bc542001-03-27 22:22:27 +00002526 }
2527 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002528 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00002529 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002530 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002531 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
2532 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00002533 }
Josh Coalson859bc542001-03-27 22:22:27 +00002534 }
2535
2536 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00002537 * First do a normal encoding pass of each independent channel
2538 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002539 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002540 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002541 if(!
2542 process_subframe_(
2543 encoder,
2544 min_partition_order,
2545 max_partition_order,
2546 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002547 &frame_header,
2548 encoder->private_->subframe_bps[channel],
2549 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002550#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002551 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002552#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002553 encoder->private_->subframe_workspace_ptr[channel],
2554 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
2555 encoder->private_->residual_workspace[channel],
2556 encoder->private_->best_subframe+channel,
2557 encoder->private_->best_subframe_bits+channel
2558 )
2559 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002560 return false;
2561 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002562 }
2563
2564 /*
2565 * Now do mid and side channels if requested
2566 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002567 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002568 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002569
2570 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002571 if(!
2572 process_subframe_(
2573 encoder,
2574 min_partition_order,
2575 max_partition_order,
2576 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002577 &frame_header,
2578 encoder->private_->subframe_bps_mid_side[channel],
2579 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002580#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002581 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002582#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002583 encoder->private_->subframe_workspace_ptr_mid_side[channel],
2584 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
2585 encoder->private_->residual_workspace_mid_side[channel],
2586 encoder->private_->best_subframe_mid_side+channel,
2587 encoder->private_->best_subframe_bits_mid_side+channel
2588 )
2589 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00002590 return false;
2591 }
2592 }
2593
2594 /*
2595 * Compose the frame bitbuffer
2596 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002597 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00002598 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
2599 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002600 FLAC__ChannelAssignment channel_assignment;
2601
Josh Coalsonfa697a92001-08-16 20:07:29 +00002602 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002603
Josh Coalsonfa697a92001-08-16 20:07:29 +00002604 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
2605 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 +00002606 }
2607 else {
2608 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
2609 unsigned min_bits;
2610 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002611
Josh Coalson1b689822001-05-31 20:11:02 +00002612 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002613
2614 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002615 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
2616 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
2617 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
2618 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 +00002619
Josh Coalson7424d2f2002-11-06 07:10:38 +00002620 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 +00002621 if(bits[ca] < min_bits) {
2622 min_bits = bits[ca];
2623 channel_assignment = ca;
2624 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002625 }
2626 }
2627
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002628 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002629
Josh Coalsond0edb972006-10-07 06:50:08 +00002630 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002631 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002632 return false;
2633 }
2634
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002635 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002636 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002637 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2638 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002639 break;
2640 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002641 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2642 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002643 break;
2644 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002645 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2646 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002647 break;
2648 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002649 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
2650 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002651 break;
2652 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002653 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002654 }
Josh Coalson82b73242001-03-28 22:17:05 +00002655
2656 switch(channel_assignment) {
2657 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002658 left_bps = encoder->private_->subframe_bps [0];
2659 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00002660 break;
2661 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002662 left_bps = encoder->private_->subframe_bps [0];
2663 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00002664 break;
2665 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002666 left_bps = encoder->private_->subframe_bps_mid_side[1];
2667 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00002668 break;
2669 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002670 left_bps = encoder->private_->subframe_bps_mid_side[0];
2671 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00002672 break;
2673 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002674 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00002675 }
2676
2677 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00002678 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00002679 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00002680 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00002681 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002682 }
2683 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00002684 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002685 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002686 return false;
2687 }
2688
Josh Coalsonfa697a92001-08-16 20:07:29 +00002689 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson369a6da2006-10-10 00:37:48 +00002690 if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002691 /* the above function sets the state for us in case of an error */
2692 return false;
2693 }
2694 }
2695 }
2696
Josh Coalsonfa697a92001-08-16 20:07:29 +00002697 if(encoder->protected_->loose_mid_side_stereo) {
2698 encoder->private_->loose_mid_side_stereo_frame_count++;
2699 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
2700 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002701 }
2702
Josh Coalsonfa697a92001-08-16 20:07:29 +00002703 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002704
Josh Coalson94e02cd2001-01-25 10:41:06 +00002705 return true;
2706}
2707
Josh Coalson6fe72f72002-08-20 04:01:59 +00002708FLAC__bool process_subframe_(
2709 FLAC__StreamEncoder *encoder,
2710 unsigned min_partition_order,
2711 unsigned max_partition_order,
2712 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002713 const FLAC__FrameHeader *frame_header,
2714 unsigned subframe_bps,
2715 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002716#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002717 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002718#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002719 FLAC__Subframe *subframe[2],
2720 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
2721 FLAC__int32 *residual[2],
2722 unsigned *best_subframe,
2723 unsigned *best_bits
2724)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002725{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002726#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002727 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002728#else
2729 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2730#endif
2731#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002732 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002733 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 +00002734 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002735 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002736 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002737#endif
2738 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002739 unsigned rice_parameter;
2740 unsigned _candidate_bits, _best_bits;
2741 unsigned _best_subframe;
2742
2743 /* verbatim subframe is the baseline against which we measure other compressed subframes */
2744 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002745 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
2746 _best_bits = UINT_MAX;
2747 else
2748 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002749
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002750 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
2751 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002752 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 +00002753 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002754 if(
2755 !encoder->private_->disable_constant_subframes &&
2756#ifndef FLAC__INTEGER_ONLY_LIBRARY
2757 fixed_residual_bits_per_sample[1] == 0.0
2758#else
2759 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
2760#endif
2761 ) {
2762 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002763 unsigned i;
2764 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002765 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002766 if(integer_signal[0] != integer_signal[i]) {
2767 signal_is_constant = false;
2768 break;
2769 }
2770 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002771 }
2772 if(signal_is_constant) {
2773 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
2774 if(_candidate_bits < _best_bits) {
2775 _best_subframe = !_best_subframe;
2776 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002777 }
2778 }
2779 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002780 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
2781 /* encode fixed */
2782 if(encoder->protected_->do_exhaustive_model_search) {
2783 min_fixed_order = 0;
2784 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00002785 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002786 else {
2787 min_fixed_order = max_fixed_order = guess_fixed_order;
2788 }
2789 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002790#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002791 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002792 continue; /* don't even try */
2793 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 +00002794#else
2795 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
2796 continue; /* don't even try */
2797 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 */
2798#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002799 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002800 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2801#ifdef DEBUG_VERBOSE
2802 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2803#endif
2804 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2805 }
2806 _candidate_bits =
2807 evaluate_fixed_subframe_(
2808 encoder,
2809 integer_signal,
2810 residual[!_best_subframe],
2811 encoder->private_->abs_residual,
2812 encoder->private_->abs_residual_partition_sums,
2813 encoder->private_->raw_bits_per_partition,
2814 frame_header->blocksize,
2815 subframe_bps,
2816 fixed_order,
2817 rice_parameter,
2818 min_partition_order,
2819 max_partition_order,
2820 precompute_partition_sums,
2821 encoder->protected_->do_escape_coding,
2822 encoder->protected_->rice_parameter_search_dist,
2823 subframe[!_best_subframe],
2824 partitioned_rice_contents[!_best_subframe]
2825 );
2826 if(_candidate_bits < _best_bits) {
2827 _best_subframe = !_best_subframe;
2828 _best_bits = _candidate_bits;
2829 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002830 }
2831 }
2832
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002833#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00002834 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002835 if(encoder->protected_->max_lpc_order > 0) {
2836 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002837 max_lpc_order = frame_header->blocksize-1;
2838 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002839 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002840 if(max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002841 unsigned a;
2842 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson0abc7352006-05-03 00:13:25 +00002843 FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002844 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2845 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2846 if(autoc[0] != 0.0) {
2847 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2848 if(encoder->protected_->do_exhaustive_model_search) {
2849 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002850 }
2851 else {
Josh Coalsondf598452006-04-28 00:13:34 +00002852 const unsigned guess_lpc_order =
2853 FLAC__lpc_compute_best_order(
2854 lpc_error,
2855 max_lpc_order,
2856 frame_header->blocksize,
2857 subframe_bps + (
2858 encoder->protected_->do_qlp_coeff_prec_search?
2859 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
2860 encoder->protected_->qlp_coeff_precision
2861 )
2862 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002863 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002864 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002865 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2866 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2867 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
2868 continue; /* don't even try */
2869 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2870 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2871 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsondf598452006-04-28 00:13:34 +00002872#ifdef DEBUG_VERBOSE
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002873 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 +00002874#endif
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002875 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2876 }
2877 if(encoder->protected_->do_qlp_coeff_prec_search) {
2878 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsondf598452006-04-28 00:13:34 +00002879 /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
2880 if(subframe_bps <= 17) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002881 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsondf598452006-04-28 00:13:34 +00002882 max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
2883 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002884 else
2885 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2886 }
2887 else {
2888 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2889 }
2890 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2891 _candidate_bits =
2892 evaluate_lpc_subframe_(
2893 encoder,
2894 integer_signal,
2895 residual[!_best_subframe],
2896 encoder->private_->abs_residual,
2897 encoder->private_->abs_residual_partition_sums,
2898 encoder->private_->raw_bits_per_partition,
2899 encoder->private_->lp_coeff[lpc_order-1],
2900 frame_header->blocksize,
2901 subframe_bps,
2902 lpc_order,
2903 qlp_coeff_precision,
2904 rice_parameter,
2905 min_partition_order,
2906 max_partition_order,
2907 precompute_partition_sums,
2908 encoder->protected_->do_escape_coding,
2909 encoder->protected_->rice_parameter_search_dist,
2910 subframe[!_best_subframe],
2911 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002912 );
2913 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2914 if(_candidate_bits < _best_bits) {
2915 _best_subframe = !_best_subframe;
2916 _best_bits = _candidate_bits;
2917 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002918 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002919 }
2920 }
2921 }
2922 }
2923 }
2924 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002925#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00002926 }
2927 }
2928
Josh Coalson72695802002-10-11 06:25:16 +00002929 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2930 if(_best_bits == UINT_MAX) {
2931 FLAC__ASSERT(_best_subframe == 0);
2932 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2933 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002934
Josh Coalson94e02cd2001-01-25 10:41:06 +00002935 *best_subframe = _best_subframe;
2936 *best_bits = _best_bits;
2937
2938 return true;
2939}
2940
Josh Coalson6fe72f72002-08-20 04:01:59 +00002941FLAC__bool add_subframe_(
2942 FLAC__StreamEncoder *encoder,
2943 const FLAC__FrameHeader *frame_header,
2944 unsigned subframe_bps,
2945 const FLAC__Subframe *subframe,
2946 FLAC__BitBuffer *frame
2947)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002948{
2949 switch(subframe->type) {
2950 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00002951 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002952 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002953 return false;
2954 }
2955 break;
2956 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00002957 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 +00002958 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002959 return false;
2960 }
2961 break;
2962 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00002963 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 +00002964 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002965 return false;
2966 }
2967 break;
2968 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00002969 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00002970 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002971 return false;
2972 }
2973 break;
2974 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002975 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002976 }
2977
2978 return true;
2979}
2980
Josh Coalson6fe72f72002-08-20 04:01:59 +00002981unsigned evaluate_constant_subframe_(
2982 const FLAC__int32 signal,
2983 unsigned subframe_bps,
2984 FLAC__Subframe *subframe
2985)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002986{
2987 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2988 subframe->data.constant.value = signal;
2989
Josh Coalson82b73242001-03-28 22:17:05 +00002990 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 +00002991}
2992
Josh Coalson6fe72f72002-08-20 04:01:59 +00002993unsigned evaluate_fixed_subframe_(
2994 FLAC__StreamEncoder *encoder,
2995 const FLAC__int32 signal[],
2996 FLAC__int32 residual[],
2997 FLAC__uint32 abs_residual[],
2998 FLAC__uint64 abs_residual_partition_sums[],
2999 unsigned raw_bits_per_partition[],
3000 unsigned blocksize,
3001 unsigned subframe_bps,
3002 unsigned order,
3003 unsigned rice_parameter,
3004 unsigned min_partition_order,
3005 unsigned max_partition_order,
3006 FLAC__bool precompute_partition_sums,
3007 FLAC__bool do_escape_coding,
3008 unsigned rice_parameter_search_dist,
3009 FLAC__Subframe *subframe,
3010 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3011)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003012{
3013 unsigned i, residual_bits;
3014 const unsigned residual_samples = blocksize - order;
3015
3016 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3017
3018 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3019
3020 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003021 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003022 subframe->data.fixed.residual = residual;
3023
Josh Coalson6fe72f72002-08-20 04:01:59 +00003024 residual_bits =
3025 find_best_partition_order_(
3026 encoder->private_,
3027 residual,
3028 abs_residual,
3029 abs_residual_partition_sums,
3030 raw_bits_per_partition,
3031 residual_samples,
3032 order,
3033 rice_parameter,
3034 min_partition_order,
3035 max_partition_order,
3036 precompute_partition_sums,
3037 do_escape_coding,
3038 rice_parameter_search_dist,
3039 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
3040 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003041
3042 subframe->data.fixed.order = order;
3043 for(i = 0; i < order; i++)
3044 subframe->data.fixed.warmup[i] = signal[i];
3045
Josh Coalson82b73242001-03-28 22:17:05 +00003046 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 +00003047}
3048
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003049#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00003050unsigned evaluate_lpc_subframe_(
3051 FLAC__StreamEncoder *encoder,
3052 const FLAC__int32 signal[],
3053 FLAC__int32 residual[],
3054 FLAC__uint32 abs_residual[],
3055 FLAC__uint64 abs_residual_partition_sums[],
3056 unsigned raw_bits_per_partition[],
3057 const FLAC__real lp_coeff[],
3058 unsigned blocksize,
3059 unsigned subframe_bps,
3060 unsigned order,
3061 unsigned qlp_coeff_precision,
3062 unsigned rice_parameter,
3063 unsigned min_partition_order,
3064 unsigned max_partition_order,
3065 FLAC__bool precompute_partition_sums,
3066 FLAC__bool do_escape_coding,
3067 unsigned rice_parameter_search_dist,
3068 FLAC__Subframe *subframe,
3069 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3070)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003071{
Josh Coalson77e3f312001-06-23 03:03:24 +00003072 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003073 unsigned i, residual_bits;
3074 int quantization, ret;
3075 const unsigned residual_samples = blocksize - order;
3076
Josh Coalson20ac2c12002-08-30 05:47:14 +00003077 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
3078 if(subframe_bps <= 16) {
3079 FLAC__ASSERT(order > 0);
3080 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
3081 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
3082 }
3083
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003084 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003085 if(ret != 0)
3086 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3087
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003088 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3089 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3090 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3091 else
3092 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 +00003093 else
3094 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 +00003095
3096 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3097
3098 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003099 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003100 subframe->data.lpc.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.lpc.order = order;
3121 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3122 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003123 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003124 for(i = 0; i < order; i++)
3125 subframe->data.lpc.warmup[i] = signal[i];
3126
Josh Coalson82b73242001-03-28 22:17:05 +00003127 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 +00003128}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003129#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003130
Josh Coalson6fe72f72002-08-20 04:01:59 +00003131unsigned evaluate_verbatim_subframe_(
3132 const FLAC__int32 signal[],
3133 unsigned blocksize,
3134 unsigned subframe_bps,
3135 FLAC__Subframe *subframe
3136)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003137{
3138 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3139
3140 subframe->data.verbatim.data = signal;
3141
Josh Coalson82b73242001-03-28 22:17:05 +00003142 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 +00003143}
3144
Josh Coalson6fe72f72002-08-20 04:01:59 +00003145unsigned find_best_partition_order_(
3146 FLAC__StreamEncoderPrivate *private_,
3147 const FLAC__int32 residual[],
3148 FLAC__uint32 abs_residual[],
3149 FLAC__uint64 abs_residual_partition_sums[],
3150 unsigned raw_bits_per_partition[],
3151 unsigned residual_samples,
3152 unsigned predictor_order,
3153 unsigned rice_parameter,
3154 unsigned min_partition_order,
3155 unsigned max_partition_order,
3156 FLAC__bool precompute_partition_sums,
3157 FLAC__bool do_escape_coding,
3158 unsigned rice_parameter_search_dist,
3159 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3160)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003161{
Josh Coalson77e3f312001-06-23 03:03:24 +00003162 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003163 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00003164 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00003165 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003166 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003167
Josh Coalson2051dd42001-04-12 22:22:34 +00003168 /* compute abs(residual) for use later */
3169 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
3170 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00003171 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00003172 }
3173
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003174 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 +00003175 min_partition_order = min(min_partition_order, max_partition_order);
3176
Josh Coalson8395d022001-07-12 21:25:22 +00003177 if(precompute_partition_sums) {
3178 int partition_order;
3179 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003180
Josh Coalsonf1eff452002-07-31 07:05:33 +00003181 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 +00003182
3183 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00003184 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 +00003185
3186 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
3187#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003188 if(!
3189 set_partitioned_rice_with_precompute_(
3190 residual,
3191 abs_residual_partition_sums+sum,
3192 raw_bits_per_partition+sum,
3193 residual_samples,
3194 predictor_order,
3195 rice_parameter,
3196 rice_parameter_search_dist,
3197 (unsigned)partition_order,
3198 do_escape_coding,
3199 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3200 &residual_bits
3201 )
3202 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003203#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003204 if(!
3205 set_partitioned_rice_with_precompute_(
3206 abs_residual,
3207 abs_residual_partition_sums+sum,
3208 raw_bits_per_partition+sum,
3209 residual_samples,
3210 predictor_order,
3211 rice_parameter,
3212 rice_parameter_search_dist,
3213 (unsigned)partition_order,
3214 do_escape_coding,
3215 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3216 &residual_bits
3217 )
3218 )
Josh Coalson8395d022001-07-12 21:25:22 +00003219#endif
3220 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003221 FLAC__ASSERT(best_residual_bits != 0);
3222 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003223 }
3224 sum += 1u << partition_order;
3225 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3226 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003227 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003228 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003229 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003230 }
3231 }
Josh Coalson8395d022001-07-12 21:25:22 +00003232 else {
3233 unsigned partition_order;
3234 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
3235#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003236 if(!
3237 set_partitioned_rice_(
3238 abs_residual,
3239 residual,
3240 residual_samples,
3241 predictor_order,
3242 rice_parameter,
3243 rice_parameter_search_dist,
3244 partition_order,
3245 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3246 &residual_bits
3247 )
3248 )
Josh Coalson8395d022001-07-12 21:25:22 +00003249#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003250 if(!
3251 set_partitioned_rice_(
3252 abs_residual,
3253 residual_samples,
3254 predictor_order,
3255 rice_parameter,
3256 rice_parameter_search_dist,
3257 partition_order,
3258 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3259 &residual_bits
3260 )
3261 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00003262#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003263 {
3264 FLAC__ASSERT(best_residual_bits != 0);
3265 break;
3266 }
3267 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3268 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003269 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003270 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003271 }
3272 }
3273 }
3274
Josh Coalsona37ba462002-08-19 21:36:39 +00003275 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00003276 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00003277 * it is const to the outside world.
3278 */
3279 {
3280 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
3281 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
3282 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3283 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
3284 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003285
3286 return best_residual_bits;
3287}
3288
Josh Coalson6fe72f72002-08-20 04:01:59 +00003289void precompute_partition_info_sums_(
3290 const FLAC__uint32 abs_residual[],
3291 FLAC__uint64 abs_residual_partition_sums[],
3292 unsigned residual_samples,
3293 unsigned predictor_order,
3294 unsigned min_partition_order,
3295 unsigned max_partition_order
3296)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003297{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003298 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003299 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003300 const unsigned blocksize = residual_samples + predictor_order;
3301
Josh Coalsonaef013c2001-04-24 01:25:42 +00003302 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003303 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003304 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00003305 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003306 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003307 const unsigned partitions = 1u << partition_order;
3308 const unsigned default_partition_samples = blocksize >> partition_order;
3309
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003310 FLAC__ASSERT(default_partition_samples > predictor_order);
3311
3312 for(partition = residual_sample = 0; partition < partitions; partition++) {
3313 partition_samples = default_partition_samples;
3314 if(partition == 0)
3315 partition_samples -= predictor_order;
3316 abs_residual_partition_sum = 0;
3317 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3318 abs_r = abs_residual[residual_sample];
3319 abs_residual_partition_sum += abs_r;
3320 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003321 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003322 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003323 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003324 to_partition = partitions;
3325 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003326 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00003327
Josh Coalson8395d022001-07-12 21:25:22 +00003328 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00003329 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003330 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003331 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003332 const unsigned partitions = 1u << partition_order;
3333 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00003334 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003335 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00003336 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00003337 from_partition++;
3338 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003339 }
3340 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003341}
Josh Coalson8395d022001-07-12 21:25:22 +00003342
Josh Coalson6fe72f72002-08-20 04:01:59 +00003343void precompute_partition_info_escapes_(
3344 const FLAC__int32 residual[],
3345 unsigned raw_bits_per_partition[],
3346 unsigned residual_samples,
3347 unsigned predictor_order,
3348 unsigned min_partition_order,
3349 unsigned max_partition_order
3350)
Josh Coalson8395d022001-07-12 21:25:22 +00003351{
3352 int partition_order;
3353 unsigned from_partition, to_partition = 0;
3354 const unsigned blocksize = residual_samples + predictor_order;
3355
3356 /* first do max_partition_order */
3357 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
3358 FLAC__int32 r, residual_partition_min, residual_partition_max;
3359 unsigned silog2_min, silog2_max;
3360 unsigned partition, partition_sample, partition_samples, residual_sample;
3361 const unsigned partitions = 1u << partition_order;
3362 const unsigned default_partition_samples = blocksize >> partition_order;
3363
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003364 FLAC__ASSERT(default_partition_samples > predictor_order);
3365
3366 for(partition = residual_sample = 0; partition < partitions; partition++) {
3367 partition_samples = default_partition_samples;
3368 if(partition == 0)
3369 partition_samples -= predictor_order;
3370 residual_partition_min = residual_partition_max = 0;
3371 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
3372 r = residual[residual_sample];
3373 if(r < residual_partition_min)
3374 residual_partition_min = r;
3375 else if(r > residual_partition_max)
3376 residual_partition_max = r;
3377 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00003378 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003379 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
3380 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
3381 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00003382 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003383 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00003384 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00003385 }
3386
3387 /* now merge partitions for lower orders */
3388 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
3389 unsigned m;
3390 unsigned i;
3391 const unsigned partitions = 1u << partition_order;
3392 for(i = 0; i < partitions; i++) {
3393 m = raw_bits_per_partition[from_partition];
3394 from_partition++;
3395 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
3396 from_partition++;
3397 to_partition++;
3398 }
3399 }
3400}
Josh Coalson94e02cd2001-01-25 10:41:06 +00003401
Josh Coalson352e0f62001-03-20 22:55:50 +00003402#ifdef VARIABLE_RICE_BITS
3403#undef VARIABLE_RICE_BITS
3404#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003405#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003406#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00003407#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003408
Josh Coalson8395d022001-07-12 21:25:22 +00003409#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003410FLAC__bool set_partitioned_rice_(
3411 const FLAC__uint32 abs_residual[],
3412 const FLAC__int32 residual[],
3413 const unsigned residual_samples,
3414 const unsigned predictor_order,
3415 const unsigned suggested_rice_parameter,
3416 const unsigned rice_parameter_search_dist,
3417 const unsigned partition_order,
3418 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3419 unsigned *bits
3420)
Josh Coalson8395d022001-07-12 21:25:22 +00003421#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003422FLAC__bool set_partitioned_rice_(
3423 const FLAC__uint32 abs_residual[],
3424 const unsigned residual_samples,
3425 const unsigned predictor_order,
3426 const unsigned suggested_rice_parameter,
3427 const unsigned rice_parameter_search_dist,
3428 const unsigned partition_order,
3429 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3430 unsigned *bits
3431)
Josh Coalson8395d022001-07-12 21:25:22 +00003432#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003433{
Josh Coalson034dfab2001-04-27 19:10:23 +00003434 unsigned rice_parameter, partition_bits;
3435#ifndef NO_RICE_SEARCH
3436 unsigned best_partition_bits;
3437 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3438#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003439 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003440 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003441
Josh Coalson1b689822001-05-31 20:11:02 +00003442 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00003443
Josh Coalsona37ba462002-08-19 21:36:39 +00003444 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3445 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003446
Josh Coalson94e02cd2001-01-25 10:41:06 +00003447 if(partition_order == 0) {
3448 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00003449
Josh Coalson034dfab2001-04-27 19:10:23 +00003450#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003451 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003452 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003453 min_rice_parameter = 0;
3454 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003455 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3456 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003457 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003458#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003459 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3460#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003461 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003462 }
3463 }
3464 else
3465 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3466
3467 best_partition_bits = 0xffffffff;
3468 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3469#endif
3470#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003471 const unsigned rice_parameter_estimate = rice_parameter-1;
3472 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00003473#else
3474 partition_bits = 0;
3475#endif
3476 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3477 for(i = 0; i < residual_samples; i++) {
3478#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003479 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00003480#else
3481 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3482#endif
3483 }
3484#ifndef NO_RICE_SEARCH
3485 if(partition_bits < best_partition_bits) {
3486 best_rice_parameter = rice_parameter;
3487 best_partition_bits = partition_bits;
3488 }
3489 }
3490#endif
3491 parameters[0] = best_rice_parameter;
3492 bits_ += best_partition_bits;
3493 }
3494 else {
3495 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003496 unsigned partition_samples;
3497 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00003498 const unsigned partitions = 1u << partition_order;
3499 for(partition = residual_sample = 0; partition < partitions; partition++) {
3500 partition_samples = (residual_samples+predictor_order) >> partition_order;
3501 if(partition == 0) {
3502 if(partition_samples <= predictor_order)
3503 return false;
3504 else
3505 partition_samples -= predictor_order;
3506 }
3507 mean = 0;
3508 save_residual_sample = residual_sample;
3509 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003510 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00003511 residual_sample = save_residual_sample;
Josh Coalsonf81b6df2005-02-04 01:34:35 +00003512 /* we are basically calculating the size in bits of the
3513 * average residual magnitude in the partition:
3514 * rice_parameter = floor(log2(mean/partition_samples))
3515 * 'mean' is not a good name for the variable, it is
3516 * actually the sum of magnitudes of all residual values
3517 * in the partition, so the actual mean is
3518 * mean/partition_samples
3519 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003520 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00003521 ;
Josh Coalson8395d022001-07-12 21:25:22 +00003522 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003523#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003524 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3525#endif
3526 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3527 }
3528
3529#ifndef NO_RICE_SEARCH
3530 if(rice_parameter_search_dist) {
3531 if(rice_parameter < rice_parameter_search_dist)
3532 min_rice_parameter = 0;
3533 else
3534 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3535 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3536 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003537#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003538 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3539#endif
3540 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3541 }
3542 }
3543 else
3544 min_rice_parameter = max_rice_parameter = rice_parameter;
3545
3546 best_partition_bits = 0xffffffff;
3547 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3548#endif
3549#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003550 const unsigned rice_parameter_estimate = rice_parameter-1;
3551 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson8395d022001-07-12 21:25:22 +00003552#else
3553 partition_bits = 0;
3554#endif
3555 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3556 save_residual_sample = residual_sample;
3557 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3558#ifdef VARIABLE_RICE_BITS
Josh Coalson8395d022001-07-12 21:25:22 +00003559 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalson8395d022001-07-12 21:25:22 +00003560#else
3561 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3562#endif
3563 }
3564#ifndef NO_RICE_SEARCH
3565 if(rice_parameter != max_rice_parameter)
3566 residual_sample = save_residual_sample;
3567 if(partition_bits < best_partition_bits) {
3568 best_rice_parameter = rice_parameter;
3569 best_partition_bits = partition_bits;
3570 }
3571 }
3572#endif
3573 parameters[partition] = best_rice_parameter;
3574 bits_ += best_partition_bits;
3575 }
3576 }
3577
3578 *bits = bits_;
3579 return true;
3580}
3581
3582#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003583FLAC__bool set_partitioned_rice_with_precompute_(
3584 const FLAC__int32 residual[],
3585 const FLAC__uint64 abs_residual_partition_sums[],
3586 const unsigned raw_bits_per_partition[],
3587 const unsigned residual_samples,
3588 const unsigned predictor_order,
3589 const unsigned suggested_rice_parameter,
3590 const unsigned rice_parameter_search_dist,
3591 const unsigned partition_order,
3592 const FLAC__bool search_for_escapes,
3593 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3594 unsigned *bits
3595)
Josh Coalson8395d022001-07-12 21:25:22 +00003596#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003597FLAC__bool set_partitioned_rice_with_precompute_(
3598 const FLAC__uint32 abs_residual[],
3599 const FLAC__uint64 abs_residual_partition_sums[],
3600 const unsigned raw_bits_per_partition[],
3601 const unsigned residual_samples,
3602 const unsigned predictor_order,
3603 const unsigned suggested_rice_parameter,
3604 const unsigned rice_parameter_search_dist,
3605 const unsigned partition_order,
3606 const FLAC__bool search_for_escapes,
3607 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3608 unsigned *bits
3609)
Josh Coalson8395d022001-07-12 21:25:22 +00003610#endif
3611{
3612 unsigned rice_parameter, partition_bits;
3613#ifndef NO_RICE_SEARCH
3614 unsigned best_partition_bits;
3615 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3616#endif
3617 unsigned flat_bits;
3618 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003619 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003620
3621 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3622
Josh Coalsona37ba462002-08-19 21:36:39 +00003623 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3624 parameters = partitioned_rice_contents->parameters;
3625 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003626
Josh Coalson8395d022001-07-12 21:25:22 +00003627 if(partition_order == 0) {
3628 unsigned i;
3629
3630#ifndef NO_RICE_SEARCH
3631 if(rice_parameter_search_dist) {
3632 if(suggested_rice_parameter < rice_parameter_search_dist)
3633 min_rice_parameter = 0;
3634 else
3635 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3636 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3637 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003638#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003639 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3640#endif
3641 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3642 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003643 }
3644 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003645 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00003646
Josh Coalson034dfab2001-04-27 19:10:23 +00003647 best_partition_bits = 0xffffffff;
3648 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3649#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003650#ifdef VARIABLE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00003651 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00003652 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00003653#else
3654 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003655#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00003656 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00003657 for(i = 0; i < residual_samples; i++) {
3658#ifdef VARIABLE_RICE_BITS
Josh Coalson2051dd42001-04-12 22:22:34 +00003659 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00003660#else
Josh Coalson2051dd42001-04-12 22:22:34 +00003661 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 +00003662#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00003663 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003664#ifndef NO_RICE_SEARCH
3665 if(partition_bits < best_partition_bits) {
3666 best_rice_parameter = rice_parameter;
3667 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00003668 }
3669 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003670#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003671 if(search_for_escapes) {
3672 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;
3673 if(flat_bits <= best_partition_bits) {
3674 raw_bits[0] = raw_bits_per_partition[0];
3675 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3676 best_partition_bits = flat_bits;
3677 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003678 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003679 parameters[0] = best_rice_parameter;
3680 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003681 }
3682 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00003683 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003684 unsigned partition_samples;
3685 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003686 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00003687 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003688 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00003689 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003690 if(partition_samples <= predictor_order)
3691 return false;
3692 else
3693 partition_samples -= predictor_order;
3694 }
Josh Coalson05d20792001-06-29 23:12:26 +00003695 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00003696 /* we are basically calculating the size in bits of the
3697 * average residual magnitude in the partition:
3698 * rice_parameter = floor(log2(mean/partition_samples))
3699 * 'mean' is not a good name for the variable, it is
3700 * actually the sum of magnitudes of all residual values
3701 * in the partition, so the actual mean is
3702 * mean/partition_samples
3703 */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003704 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00003705 ;
Josh Coalson8395d022001-07-12 21:25:22 +00003706 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003707#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003708 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3709#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003710 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003711 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003712
Josh Coalson034dfab2001-04-27 19:10:23 +00003713#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003714 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003715 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003716 min_rice_parameter = 0;
3717 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003718 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3719 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003720 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003721#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003722 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3723#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003724 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003725 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003726 }
3727 else
3728 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00003729
Josh Coalson034dfab2001-04-27 19:10:23 +00003730 best_partition_bits = 0xffffffff;
3731 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3732#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003733#ifdef VARIABLE_RICE_BITS
Josh Coalson034dfab2001-04-27 19:10:23 +00003734 const unsigned rice_parameter_estimate = rice_parameter-1;
3735 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalson034dfab2001-04-27 19:10:23 +00003736#else
3737 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003738#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003739 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00003740 save_residual_sample = residual_sample;
3741 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00003742#ifdef VARIABLE_RICE_BITS
Josh Coalson4dacd192001-06-06 21:11:44 +00003743 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00003744#else
Josh Coalson4dacd192001-06-06 21:11:44 +00003745 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 +00003746#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003747 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003748#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00003749 if(rice_parameter != max_rice_parameter)
3750 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00003751 if(partition_bits < best_partition_bits) {
3752 best_rice_parameter = rice_parameter;
3753 best_partition_bits = partition_bits;
3754 }
Josh Coalson2051dd42001-04-12 22:22:34 +00003755 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003756#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003757 if(search_for_escapes) {
3758 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;
3759 if(flat_bits <= best_partition_bits) {
3760 raw_bits[partition] = raw_bits_per_partition[partition];
3761 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3762 best_partition_bits = flat_bits;
3763 }
Josh Coalson2051dd42001-04-12 22:22:34 +00003764 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003765 parameters[partition] = best_rice_parameter;
3766 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003767 }
3768 }
3769
3770 *bits = bits_;
3771 return true;
3772}
Josh Coalson859bc542001-03-27 22:22:27 +00003773
Josh Coalsonf1eff452002-07-31 07:05:33 +00003774unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00003775{
3776 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00003777 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00003778
3779 for(i = 0; i < samples && !(x&1); i++)
3780 x |= signal[i];
3781
3782 if(x == 0) {
3783 shift = 0;
3784 }
3785 else {
3786 for(shift = 0; !(x&1); shift++)
3787 x >>= 1;
3788 }
3789
3790 if(shift > 0) {
3791 for(i = 0; i < samples; i++)
3792 signal[i] >>= shift;
3793 }
3794
3795 return shift;
3796}
Josh Coalsond86e03b2002-08-03 21:56:15 +00003797
3798void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3799{
3800 unsigned channel;
3801
3802 for(channel = 0; channel < channels; channel++)
3803 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3804
3805 fifo->tail += wide_samples;
3806
3807 FLAC__ASSERT(fifo->tail <= fifo->size);
3808}
3809
3810void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3811{
3812 unsigned channel;
3813 unsigned sample, wide_sample;
3814 unsigned tail = fifo->tail;
3815
3816 sample = input_offset * channels;
3817 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3818 for(channel = 0; channel < channels; channel++)
3819 fifo->data[channel][tail] = input[sample++];
3820 tail++;
3821 }
3822 fifo->tail = tail;
3823
3824 FLAC__ASSERT(fifo->tail <= fifo->size);
3825}
3826
3827FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3828{
3829 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3830 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3831 (void)decoder;
3832
3833 if(encoder->private_->verify.needs_magic_hack) {
3834 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3835 *bytes = FLAC__STREAM_SYNC_LENGTH;
3836 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3837 encoder->private_->verify.needs_magic_hack = false;
3838 }
3839 else {
3840 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00003841 /*
3842 * If we get here, a FIFO underflow has occurred,
3843 * which means there is a bug somewhere.
3844 */
3845 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003846 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3847 }
3848 else if(encoded_bytes < *bytes)
3849 *bytes = encoded_bytes;
3850 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3851 encoder->private_->verify.output.data += *bytes;
3852 encoder->private_->verify.output.bytes -= *bytes;
3853 }
3854
3855 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3856}
3857
3858FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3859{
3860 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3861 unsigned channel;
3862 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3863 const unsigned blocksize = frame->header.blocksize;
3864 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3865
3866 for(channel = 0; channel < channels; channel++) {
3867 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3868 unsigned i, sample = 0;
3869 FLAC__int32 expect = 0, got = 0;
3870
3871 for(i = 0; i < blocksize; i++) {
3872 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3873 sample = i;
3874 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3875 got = (FLAC__int32)buffer[channel][i];
3876 break;
3877 }
3878 }
3879 FLAC__ASSERT(i < blocksize);
3880 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3881 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00003882 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003883 encoder->private_->verify.error_stats.channel = channel;
3884 encoder->private_->verify.error_stats.sample = sample;
3885 encoder->private_->verify.error_stats.expected = expect;
3886 encoder->private_->verify.error_stats.got = got;
3887 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3888 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3889 }
3890 }
3891 /* dequeue the frame from the fifo */
3892 for(channel = 0; channel < channels; channel++) {
3893 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3894 }
3895 encoder->private_->verify.input_fifo.tail -= blocksize;
3896 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3897}
3898
3899void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3900{
3901 (void)decoder, (void)metadata, (void)client_data;
3902}
3903
3904void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3905{
3906 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3907 (void)decoder, (void)status;
3908 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3909}
Josh Coalson6b21f662006-09-13 01:42:27 +00003910
3911FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3912{
3913 (void)client_data;
3914
3915 if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
3916 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
3917 else
3918 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
3919}
3920
3921FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3922{
3923 off_t offset;
3924
3925 (void)client_data;
3926
3927 offset = ftello(encoder->private_->file);
3928
3929 if(offset < 0) {
3930 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
3931 }
3932 else {
3933 *absolute_byte_offset = (FLAC__uint64)offset;
3934 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
3935 }
3936}
3937
3938#ifdef FLAC__VALGRIND_TESTING
3939static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
3940{
3941 size_t ret = fwrite(ptr, size, nmemb, stream);
3942 if(!ferror(stream))
3943 fflush(stream);
3944 return ret;
3945}
3946#else
3947#define local__fwrite fwrite
3948#endif
3949
3950FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
3951{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00003952 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00003953
3954 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00003955 if(0 != encoder->private_->progress_callback && samples > 0) {
3956 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
3957 * because at this point in the callback chain, the stats
3958 * have not been updated. Only after we return and control
3959 * gets back to write_frame_() are the stats updated
3960 */
3961 encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data);
3962 }
Josh Coalson6b21f662006-09-13 01:42:27 +00003963 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
3964 }
3965 else
3966 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
3967}
3968
3969/*
3970 * This will forcibly set stdout to binary mode (for OSes that require it)
3971 */
3972FILE *get_binary_stdout_()
3973{
3974 /* if something breaks here it is probably due to the presence or
3975 * absence of an underscore before the identifiers 'setmode',
3976 * 'fileno', and/or 'O_BINARY'; check your system header files.
3977 */
3978#if defined _MSC_VER || defined __MINGW32__
3979 _setmode(_fileno(stdout), _O_BINARY);
3980#elif defined __CYGWIN__
3981 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
3982 setmode(_fileno(stdout), _O_BINARY);
3983#elif defined __EMX__
3984 setmode(fileno(stdout), O_BINARY);
3985#endif
3986
3987 return stdout;
3988}