blob: 314df26504b8b528066975d4fbcd23c11d2ca6b2 [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson95643902004-01-17 04:14:43 +00002 * Copyright (C) 2000,2001,2002,2003,2004 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 Coalsone6b3bbe2002-10-08 06:03:25 +000032#include <limits.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000033#include <stdio.h>
34#include <stdlib.h> /* for malloc() */
35#include <string.h> /* for memcpy() */
Josh Coalson1b689822001-05-31 20:11:02 +000036#include "FLAC/assert.h"
Josh Coalsond86e03b2002-08-03 21:56:15 +000037#include "FLAC/stream_decoder.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000038#include "protected/stream_encoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000039#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000040#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000041#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000042#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000043#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000044#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000045#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000046#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000047#include "private/memory.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000048#include "private/stream_encoder_framing.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000049
Josh Coalson5e31be12002-12-04 07:07:35 +000050#ifdef HAVE_CONFIG_H
51#include <config.h>
52#endif
53
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000054#ifdef min
55#undef min
56#endif
57#define min(x,y) ((x)<(y)?(x):(y))
58
59#ifdef max
60#undef max
61#endif
62#define max(x,y) ((x)>(y)?(x):(y))
63
Josh Coalsond86e03b2002-08-03 21:56:15 +000064typedef struct {
65 FLAC__int32 *data[FLAC__MAX_CHANNELS];
66 unsigned size; /* of each data[] in samples */
67 unsigned tail;
68} verify_input_fifo;
69
70typedef struct {
71 const FLAC__byte *data;
72 unsigned capacity;
73 unsigned bytes;
74} verify_output;
75
76typedef enum {
77 ENCODER_IN_MAGIC = 0,
78 ENCODER_IN_METADATA = 1,
79 ENCODER_IN_AUDIO = 2
80} EncoderStateHint;
81
Josh Coalson0a15c142001-06-13 17:59:57 +000082/***********************************************************************
83 *
84 * Private class method prototypes
85 *
86 ***********************************************************************/
87
Josh Coalsonf1eff452002-07-31 07:05:33 +000088static void set_defaults_(FLAC__StreamEncoder *encoder);
89static void free_(FLAC__StreamEncoder *encoder);
90static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
Josh Coalsond86e03b2002-08-03 21:56:15 +000091static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
Josh Coalsonf1eff452002-07-31 07:05:33 +000092static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
93static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson6fe72f72002-08-20 04:01:59 +000094
95static FLAC__bool process_subframe_(
96 FLAC__StreamEncoder *encoder,
97 unsigned min_partition_order,
98 unsigned max_partition_order,
99 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000100 const FLAC__FrameHeader *frame_header,
101 unsigned subframe_bps,
102 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000103#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000104 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000105#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000106 FLAC__Subframe *subframe[2],
107 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
108 FLAC__int32 *residual[2],
109 unsigned *best_subframe,
110 unsigned *best_bits
111);
112
113static FLAC__bool add_subframe_(
114 FLAC__StreamEncoder *encoder,
115 const FLAC__FrameHeader *frame_header,
116 unsigned subframe_bps,
117 const FLAC__Subframe *subframe,
118 FLAC__BitBuffer *frame
119);
120
121static unsigned evaluate_constant_subframe_(
122 const FLAC__int32 signal,
123 unsigned subframe_bps,
124 FLAC__Subframe *subframe
125);
126
127static unsigned evaluate_fixed_subframe_(
128 FLAC__StreamEncoder *encoder,
129 const FLAC__int32 signal[],
130 FLAC__int32 residual[],
131 FLAC__uint32 abs_residual[],
132 FLAC__uint64 abs_residual_partition_sums[],
133 unsigned raw_bits_per_partition[],
134 unsigned blocksize,
135 unsigned subframe_bps,
136 unsigned order,
137 unsigned rice_parameter,
138 unsigned min_partition_order,
139 unsigned max_partition_order,
140 FLAC__bool precompute_partition_sums,
141 FLAC__bool do_escape_coding,
142 unsigned rice_parameter_search_dist,
143 FLAC__Subframe *subframe,
144 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
145);
146
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000147#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +0000148static unsigned evaluate_lpc_subframe_(
149 FLAC__StreamEncoder *encoder,
150 const FLAC__int32 signal[],
151 FLAC__int32 residual[],
152 FLAC__uint32 abs_residual[],
153 FLAC__uint64 abs_residual_partition_sums[],
154 unsigned raw_bits_per_partition[],
155 const FLAC__real lp_coeff[],
156 unsigned blocksize,
157 unsigned subframe_bps,
158 unsigned order,
159 unsigned qlp_coeff_precision,
160 unsigned rice_parameter,
161 unsigned min_partition_order,
162 unsigned max_partition_order,
163 FLAC__bool precompute_partition_sums,
164 FLAC__bool do_escape_coding,
165 unsigned rice_parameter_search_dist,
166 FLAC__Subframe *subframe,
167 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
168);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000169#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000170
171static unsigned evaluate_verbatim_subframe_(
172 const FLAC__int32 signal[],
173 unsigned blocksize,
174 unsigned subframe_bps,
175 FLAC__Subframe *subframe
176);
177
178static unsigned find_best_partition_order_(
179 struct FLAC__StreamEncoderPrivate *private_,
180 const FLAC__int32 residual[],
181 FLAC__uint32 abs_residual[],
182 FLAC__uint64 abs_residual_partition_sums[],
183 unsigned raw_bits_per_partition[],
184 unsigned residual_samples,
185 unsigned predictor_order,
186 unsigned rice_parameter,
187 unsigned min_partition_order,
188 unsigned max_partition_order,
189 FLAC__bool precompute_partition_sums,
190 FLAC__bool do_escape_coding,
191 unsigned rice_parameter_search_dist,
192 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
193);
194
195static void precompute_partition_info_sums_(
196 const FLAC__uint32 abs_residual[],
197 FLAC__uint64 abs_residual_partition_sums[],
198 unsigned residual_samples,
199 unsigned predictor_order,
200 unsigned min_partition_order,
201 unsigned max_partition_order
202);
203
204static void precompute_partition_info_escapes_(
205 const FLAC__int32 residual[],
206 unsigned raw_bits_per_partition[],
207 unsigned residual_samples,
208 unsigned predictor_order,
209 unsigned min_partition_order,
210 unsigned max_partition_order
211);
212
Josh Coalson8395d022001-07-12 21:25:22 +0000213#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +0000214static FLAC__bool set_partitioned_rice_(
215 const FLAC__uint32 abs_residual[],
216 const FLAC__int32 residual[],
217 const unsigned residual_samples,
218 const unsigned predictor_order,
219 const unsigned suggested_rice_parameter,
220 const unsigned rice_parameter_search_dist,
221 const unsigned partition_order,
222 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
223 unsigned *bits
224);
225
226static FLAC__bool set_partitioned_rice_with_precompute_(
227 const FLAC__int32 residual[],
228 const FLAC__uint64 abs_residual_partition_sums[],
229 const unsigned raw_bits_per_partition[],
230 const unsigned residual_samples,
231 const unsigned predictor_order,
232 const unsigned suggested_rice_parameter,
233 const unsigned rice_parameter_search_dist,
234 const unsigned partition_order,
235 const FLAC__bool search_for_escapes,
236 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
237 unsigned *bits
238);
Josh Coalson8395d022001-07-12 21:25:22 +0000239#else
Josh Coalson6fe72f72002-08-20 04:01:59 +0000240static FLAC__bool set_partitioned_rice_(
241 const FLAC__uint32 abs_residual[],
242 const unsigned residual_samples,
243 const unsigned predictor_order,
244 const unsigned suggested_rice_parameter,
245 const unsigned rice_parameter_search_dist,
246 const unsigned partition_order,
247 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
248 unsigned *bits
249);
250
251static FLAC__bool set_partitioned_rice_with_precompute_(
252 const FLAC__uint32 abs_residual[],
253 const FLAC__uint64 abs_residual_partition_sums[],
254 const unsigned raw_bits_per_partition[],
255 const unsigned residual_samples,
256 const unsigned predictor_order,
257 const unsigned suggested_rice_parameter,
258 const unsigned rice_parameter_search_dist,
259 const unsigned partition_order,
260 const FLAC__bool search_for_escapes,
261 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
262 unsigned *bits
263);
Josh Coalson0a15c142001-06-13 17:59:57 +0000264#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000265
Josh Coalsonf1eff452002-07-31 07:05:33 +0000266static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000267
Josh Coalsond86e03b2002-08-03 21:56:15 +0000268/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000269static void append_to_verify_fifo_(
270 verify_input_fifo *fifo,
271 const FLAC__int32 * const input[],
272 unsigned input_offset,
273 unsigned channels,
274 unsigned wide_samples
275);
276
277static void append_to_verify_fifo_interleaved_(
278 verify_input_fifo *fifo,
279 const FLAC__int32 input[],
280 unsigned input_offset,
281 unsigned channels,
282 unsigned wide_samples
283);
284
285static FLAC__StreamDecoderReadStatus verify_read_callback_(
286 const FLAC__StreamDecoder *decoder,
287 FLAC__byte buffer[],
288 unsigned *bytes,
289 void *client_data
290);
291
292static FLAC__StreamDecoderWriteStatus verify_write_callback_(
293 const FLAC__StreamDecoder *decoder,
294 const FLAC__Frame *frame,
295 const FLAC__int32 * const buffer[],
296 void *client_data
297);
298
299static void verify_metadata_callback_(
300 const FLAC__StreamDecoder *decoder,
301 const FLAC__StreamMetadata *metadata,
302 void *client_data
303);
304
305static void verify_error_callback_(
306 const FLAC__StreamDecoder *decoder,
307 FLAC__StreamDecoderErrorStatus status,
308 void *client_data
309);
310
Josh Coalson0a15c142001-06-13 17:59:57 +0000311
312/***********************************************************************
313 *
314 * Private class data
315 *
316 ***********************************************************************/
317
318typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +0000319 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000320 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
321 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 +0000322#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000323 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
324 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000325#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000326 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
327 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 +0000328 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
329 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000330 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
331 FLAC__Subframe subframe_workspace_mid_side[2][2];
332 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
333 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000334 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
335 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
336 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
337 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Josh Coalson8395d022001-07-12 21:25:22 +0000338 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000339 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +0000340 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000341 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +0000342 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000343 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 +0000344 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 +0000345 FLAC__BitBuffer *frame; /* the current frame being worked on */
Josh Coalson8395d022001-07-12 21:25:22 +0000346 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
347 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000348 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsoncc682512002-06-08 04:53:42 +0000349 FLAC__StreamMetadata metadata;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000350 unsigned current_sample_number;
351 unsigned current_frame_number;
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000352 struct FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000353 FLAC__CPUInfo cpuinfo;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000354#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +0000355 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 +0000356#else
357 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
358#endif
359#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000360 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
361 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[]);
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000362 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[]);
Josh Coalson77e3f312001-06-23 03:03:24 +0000363 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 +0000364#endif
Josh Coalson3262b0d2002-08-14 20:58:42 +0000365 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
366 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
367 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
368 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 +0000369 FLAC__bool disable_constant_subframes;
370 FLAC__bool disable_fixed_subframes;
371 FLAC__bool disable_verbatim_subframes;
Josh Coalson681c2932002-08-01 08:19:37 +0000372 FLAC__StreamEncoderWriteCallback write_callback;
373 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000374 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000375 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000376 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
377 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000378#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson77e3f312001-06-23 03:03:24 +0000379 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
380 FLAC__real *real_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000381#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000382 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
383 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
384 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000385 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000386 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000387 /*
388 * These fields have been moved here from private function local
389 * declarations merely to save stack space during encoding.
390 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000391#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000392 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000393#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000394 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000395 /*
396 * The data for the verify section
397 */
398 struct {
399 FLAC__StreamDecoder *decoder;
400 EncoderStateHint state_hint;
401 FLAC__bool needs_magic_hack;
402 verify_input_fifo input_fifo;
403 verify_output output;
404 struct {
405 FLAC__uint64 absolute_sample;
406 unsigned frame_number;
407 unsigned channel;
408 unsigned sample;
409 FLAC__int32 expected;
410 FLAC__int32 got;
411 } error_stats;
412 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000413 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000414} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000415
Josh Coalson0a15c142001-06-13 17:59:57 +0000416/***********************************************************************
417 *
418 * Public static class data
419 *
420 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000421
Josh Coalson6afed9f2002-10-16 22:29:47 +0000422FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000423 "FLAC__STREAM_ENCODER_OK",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000424 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
425 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson00e53872001-06-16 07:32:25 +0000426 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
Josh Coalson0a15c142001-06-13 17:59:57 +0000427 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
428 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
429 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
430 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
Josh Coalson20ac2c12002-08-30 05:47:14 +0000431 "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
Josh Coalson0a15c142001-06-13 17:59:57 +0000432 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
433 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
434 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
435 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
436 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
437 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
438 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson66075c12002-06-01 05:39:38 +0000439 "FLAC__STREAM_ENCODER_INVALID_METADATA",
Josh Coalson0a15c142001-06-13 17:59:57 +0000440 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
441 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
442 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
443 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
444 "FLAC__STREAM_ENCODER_UNINITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000445};
446
Josh Coalson6afed9f2002-10-16 22:29:47 +0000447FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000448 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
449 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000450};
451
Josh Coalson0a15c142001-06-13 17:59:57 +0000452/***********************************************************************
453 *
454 * Class constructor/destructor
455 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000456 */
Josh Coalson6afed9f2002-10-16 22:29:47 +0000457FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000458{
Josh Coalson0a15c142001-06-13 17:59:57 +0000459 FLAC__StreamEncoder *encoder;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000460 unsigned i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000461
Josh Coalson0a15c142001-06-13 17:59:57 +0000462 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000463
Josh Coalsonea7155f2002-10-18 05:49:19 +0000464 encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000465 if(encoder == 0) {
466 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000467 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000468
Josh Coalsonea7155f2002-10-18 05:49:19 +0000469 encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000470 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000471 free(encoder);
472 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000473 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000474
Josh Coalsonea7155f2002-10-18 05:49:19 +0000475 encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000476 if(encoder->private_ == 0) {
477 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000478 free(encoder);
479 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000480 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000481
Josh Coalsonaec256b2002-03-12 16:19:54 +0000482 encoder->private_->frame = FLAC__bitbuffer_new();
483 if(encoder->private_->frame == 0) {
484 free(encoder->private_);
485 free(encoder->protected_);
486 free(encoder);
487 return 0;
488 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000489
Josh Coalsonf1eff452002-07-31 07:05:33 +0000490 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000491
Josh Coalson3262b0d2002-08-14 20:58:42 +0000492 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000493
494 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
495 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
496 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
497 }
498 for(i = 0; i < 2; i++) {
499 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
500 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
501 }
502 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000503 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
504 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000505 }
506 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000507 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
508 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 +0000509 }
510
511 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000512 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
513 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000514 }
515 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000516 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
517 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 +0000518 }
519 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000520 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000521
Josh Coalsonfa697a92001-08-16 20:07:29 +0000522 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000523
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000524 return encoder;
525}
526
Josh Coalson6afed9f2002-10-16 22:29:47 +0000527FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000528{
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000529 unsigned i;
530
Josh Coalsonf1eff452002-07-31 07:05:33 +0000531 FLAC__ASSERT(0 != encoder);
532 FLAC__ASSERT(0 != encoder->protected_);
533 FLAC__ASSERT(0 != encoder->private_);
534 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000535
Josh Coalson3262b0d2002-08-14 20:58:42 +0000536 encoder->private_->is_being_deleted = true;
537
538 FLAC__stream_encoder_finish(encoder);
539
Josh Coalson4fa90592002-12-04 07:01:37 +0000540 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000541 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000542
543 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000544 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
545 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000546 }
547 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000548 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
549 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 +0000550 }
551 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000552 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000553
Josh Coalsonaec256b2002-03-12 16:19:54 +0000554 FLAC__bitbuffer_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000555 free(encoder->private_);
556 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000557 free(encoder);
558}
559
Josh Coalson0a15c142001-06-13 17:59:57 +0000560/***********************************************************************
561 *
562 * Public class methods
563 *
564 ***********************************************************************/
565
Josh Coalson6afed9f2002-10-16 22:29:47 +0000566FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000567{
568 unsigned i;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000569 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000570
Josh Coalsonf1eff452002-07-31 07:05:33 +0000571 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000572
Josh Coalsonfa697a92001-08-16 20:07:29 +0000573 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
574 return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000575
Josh Coalsonfa697a92001-08-16 20:07:29 +0000576 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000577
Josh Coalsonfa697a92001-08-16 20:07:29 +0000578 if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
579 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000580
Josh Coalsonfa697a92001-08-16 20:07:29 +0000581 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
582 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000583
Josh Coalsonfa697a92001-08-16 20:07:29 +0000584 if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
585 return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000586
Josh Coalsonfa697a92001-08-16 20:07:29 +0000587 if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
588 return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000589
Josh Coalsonfa697a92001-08-16 20:07:29 +0000590 if(encoder->protected_->bits_per_sample >= 32)
591 encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000592
Josh Coalson76c68bc2002-05-17 06:22:02 +0000593 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000594 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000595
Josh Coalson0833f342002-07-15 05:31:55 +0000596 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000597 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000598
Josh Coalsonfa697a92001-08-16 20:07:29 +0000599 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
600 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000601
Josh Coalson20ac2c12002-08-30 05:47:14 +0000602 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
603 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
604
Josh Coalsonfa697a92001-08-16 20:07:29 +0000605 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
606 return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000607
Josh Coalsonfa697a92001-08-16 20:07:29 +0000608 if(encoder->protected_->qlp_coeff_precision == 0) {
609 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000610 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
611 /* @@@ until then we'll make a guess */
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000612 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 +0000613 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000614 else if(encoder->protected_->bits_per_sample == 16) {
615 if(encoder->protected_->blocksize <= 192)
616 encoder->protected_->qlp_coeff_precision = 7;
617 else if(encoder->protected_->blocksize <= 384)
618 encoder->protected_->qlp_coeff_precision = 8;
619 else if(encoder->protected_->blocksize <= 576)
620 encoder->protected_->qlp_coeff_precision = 9;
621 else if(encoder->protected_->blocksize <= 1152)
622 encoder->protected_->qlp_coeff_precision = 10;
623 else if(encoder->protected_->blocksize <= 2304)
624 encoder->protected_->qlp_coeff_precision = 11;
625 else if(encoder->protected_->blocksize <= 4608)
626 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000627 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000628 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000629 }
630 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000631 if(encoder->protected_->blocksize <= 384)
632 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
633 else if(encoder->protected_->blocksize <= 1152)
634 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
635 else
636 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000637 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000638 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000639 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000640 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000641 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000642
Josh Coalsonfa697a92001-08-16 20:07:29 +0000643 if(encoder->protected_->streamable_subset) {
Josh Coalson20ac2c12002-08-30 05:47:14 +0000644 if(
645 encoder->protected_->blocksize != 192 &&
646 encoder->protected_->blocksize != 576 &&
647 encoder->protected_->blocksize != 1152 &&
648 encoder->protected_->blocksize != 2304 &&
649 encoder->protected_->blocksize != 4608 &&
650 encoder->protected_->blocksize != 256 &&
651 encoder->protected_->blocksize != 512 &&
652 encoder->protected_->blocksize != 1024 &&
653 encoder->protected_->blocksize != 2048 &&
654 encoder->protected_->blocksize != 4096 &&
655 encoder->protected_->blocksize != 8192 &&
656 encoder->protected_->blocksize != 16384
657 )
Josh Coalsonfa697a92001-08-16 20:07:29 +0000658 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000659 if(
660 encoder->protected_->sample_rate != 8000 &&
661 encoder->protected_->sample_rate != 16000 &&
662 encoder->protected_->sample_rate != 22050 &&
663 encoder->protected_->sample_rate != 24000 &&
664 encoder->protected_->sample_rate != 32000 &&
665 encoder->protected_->sample_rate != 44100 &&
666 encoder->protected_->sample_rate != 48000 &&
667 encoder->protected_->sample_rate != 96000
668 )
669 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
670 if(
671 encoder->protected_->bits_per_sample != 8 &&
672 encoder->protected_->bits_per_sample != 12 &&
673 encoder->protected_->bits_per_sample != 16 &&
674 encoder->protected_->bits_per_sample != 20 &&
675 encoder->protected_->bits_per_sample != 24
676 )
677 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000678 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000679 return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000680 }
681
Josh Coalsonfa697a92001-08-16 20:07:29 +0000682 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
683 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
684 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
685 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000686
Josh Coalson66075c12002-06-01 05:39:38 +0000687 /* validate metadata */
688 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
689 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000690 metadata_has_seektable = false;
691 metadata_has_vorbis_comment = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000692 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalsond0609472003-01-10 05:37:13 +0000693 if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson66075c12002-06-01 05:39:38 +0000694 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
695 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000696 if(metadata_has_seektable) /* only one is allowed */
697 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
698 metadata_has_seektable = true;
Josh Coalson0833f342002-07-15 05:31:55 +0000699 if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
Josh Coalson66075c12002-06-01 05:39:38 +0000700 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
701 }
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000702 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
703 if(metadata_has_vorbis_comment) /* only one is allowed */
704 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
705 metadata_has_vorbis_comment = true;
706 }
Josh Coalsone4869382002-11-15 05:41:48 +0000707 else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000708 if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
Josh Coalsone4869382002-11-15 05:41:48 +0000709 return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
710 }
Josh Coalson66075c12002-06-01 05:39:38 +0000711 }
712
Josh Coalsonfa697a92001-08-16 20:07:29 +0000713 encoder->private_->input_capacity = 0;
714 for(i = 0; i < encoder->protected_->channels; i++) {
715 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000716#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000717 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000718#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000719 }
720 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000721 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000722#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000723 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000724#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000725 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000726 for(i = 0; i < encoder->protected_->channels; i++) {
727 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
728 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
729 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000730 }
731 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000732 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
733 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
734 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000735 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000736 encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
737 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
738 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000739#ifndef FLAC__INTEGER_ONLY_LIBRARY
740 encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
741#else
742 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
743 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
744 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
745 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
746 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
747 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
748 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);
749#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000750 if(encoder->private_->loose_mid_side_stereo_frames == 0)
751 encoder->private_->loose_mid_side_stereo_frames = 1;
752 encoder->private_->loose_mid_side_stereo_frame_count = 0;
753 encoder->private_->current_sample_number = 0;
754 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000755
Josh Coalsonfa697a92001-08-16 20:07:29 +0000756 encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
757 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? */
758 encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
Josh Coalson8395d022001-07-12 21:25:22 +0000759
Josh Coalsoncf30f502001-05-23 20:57:44 +0000760 /*
761 * get the CPU info and set the function pointers
762 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000763 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000764 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000765#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000766 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000767#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000768 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000769#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000770 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000771 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 +0000772 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000773#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000774 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000775#ifndef FLAC__INTEGER_ONLY_LIBRARY
776# ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000777 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000778# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000779 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000780# ifdef FLAC__HAS_NASM
781# ifdef FLAC__SSE_OS
Josh Coalson48cbe662002-12-30 23:38:14 +0000782 if(encoder->private_->cpuinfo.data.ia32.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000783 if(encoder->protected_->max_lpc_order < 4)
784 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
785 else if(encoder->protected_->max_lpc_order < 8)
786 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
787 else if(encoder->protected_->max_lpc_order < 12)
788 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000789 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000790 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000791 }
Josh Coalson48cbe662002-12-30 23:38:14 +0000792 else
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000793# endif /* FLAC__SSE_OS */
Josh Coalson48cbe662002-12-30 23:38:14 +0000794 if(encoder->private_->cpuinfo.data.ia32._3dnow)
Josh Coalsonfa697a92001-08-16 20:07:29 +0000795 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
Josh Coalsonaa255362001-05-31 06:17:41 +0000796 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000797 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000798 if(encoder->private_->cpuinfo.data.ia32.mmx) {
799 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
800 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 +0000801 }
802 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000803 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
804 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 +0000805 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000806 if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
807 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
808# endif /* FLAC__HAS_NASM */
809# endif /* FLAC__CPU_IA32 */
Josh Coalson021ad3b2001-07-18 00:25:52 +0000810 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000811# endif /* !FLAC__NO_ASM */
812#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson8395d022001-07-12 21:25:22 +0000813 /* finally override based on wide-ness if necessary */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000814 if(encoder->private_->use_wide_by_block) {
815 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson8395d022001-07-12 21:25:22 +0000816 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000817
Josh Coalson8395d022001-07-12 21:25:22 +0000818 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000819 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 +0000820
Josh Coalsonf1eff452002-07-31 07:05:33 +0000821 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000822 /* the above function sets the state for us in case of an error */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000823 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000824 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000825
826 if(!FLAC__bitbuffer_init(encoder->private_->frame))
827 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000828
829 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +0000830 * Set up the verify stuff if necessary
831 */
832 if(encoder->protected_->verify) {
833 /*
834 * First, set up the fifo which will hold the
835 * original signal to compare against
836 */
837 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
838 for(i = 0; i < encoder->protected_->channels; i++) {
839 if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
840 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
841 }
842 encoder->private_->verify.input_fifo.tail = 0;
843
844 /*
845 * Now set up a stream decoder for verification
846 */
847 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
848 if(0 == encoder->private_->verify.decoder)
849 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
850
851 FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
852 FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
853 FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
854 FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
855 FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
856 if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
857 return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
858 }
Josh Coalson589f8c72002-08-07 23:54:55 +0000859 encoder->private_->verify.error_stats.absolute_sample = 0;
860 encoder->private_->verify.error_stats.frame_number = 0;
861 encoder->private_->verify.error_stats.channel = 0;
862 encoder->private_->verify.error_stats.sample = 0;
863 encoder->private_->verify.error_stats.expected = 0;
864 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000865
866 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000867 * write the stream header
868 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000869 if(encoder->protected_->verify)
870 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000871 if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000872 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000873 if(!write_bitbuffer_(encoder, 0)) {
874 /* the above function sets the state for us in case of an error */
875 return encoder->protected_->state;
876 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000877
Josh Coalson5c491a12002-08-01 06:39:40 +0000878 /*
879 * write the STREAMINFO metadata block
880 */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000881 if(encoder->protected_->verify)
882 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000883 encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000884 encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000885 encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
886 encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
887 encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
888 encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
889 encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
890 encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
891 encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
892 encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
893 encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
894 memset(encoder->private_->metadata.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 +0000895 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000896 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
897 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000898 if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
Josh Coalsonfa697a92001-08-16 20:07:29 +0000899 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000900 if(!write_bitbuffer_(encoder, 0)) {
901 /* the above function sets the state for us in case of an error */
902 return encoder->protected_->state;
903 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000904
Josh Coalson5c491a12002-08-01 06:39:40 +0000905 /*
906 * Now that the STREAMINFO block is written, we can init this to an
907 * absurdly-high value...
908 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000909 encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000910 /* ... and clear this to 0 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000911 encoder->private_->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000912
Josh Coalson5c491a12002-08-01 06:39:40 +0000913 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000914 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
915 * if not, we will write an empty one (FLAC__add_metadata_block()
916 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +0000917 *
918 * WATCHOUT: libOggFLAC depends on us to write this block after the
919 * STREAMINFO since that's what the mapping requires. (In the case
Josh Coalson8ddf7fb2004-12-30 00:58:50 +0000920 * that metadata_has_vorbis_comment is true it will have already
Josh Coalson69cfda72004-09-10 00:38:21 +0000921 * insured that the metadata list is properly ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000922 */
923 if(!metadata_has_vorbis_comment) {
924 FLAC__StreamMetadata vorbis_comment;
925 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
926 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
927 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
928 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
929 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
930 vorbis_comment.data.vorbis_comment.num_comments = 0;
931 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson7424d2f2002-11-06 07:10:38 +0000932 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
933 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000934 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
935 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
936 if(!write_bitbuffer_(encoder, 0)) {
937 /* the above function sets the state for us in case of an error */
938 return encoder->protected_->state;
939 }
940 }
941
942 /*
Josh Coalson5c491a12002-08-01 06:39:40 +0000943 * write the user's metadata blocks
944 */
945 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
946 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson7424d2f2002-11-06 07:10:38 +0000947 if(!FLAC__bitbuffer_clear(encoder->private_->frame))
948 return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +0000949 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
950 return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000951 if(!write_bitbuffer_(encoder, 0)) {
952 /* the above function sets the state for us in case of an error */
953 return encoder->protected_->state;
954 }
Josh Coalson5c491a12002-08-01 06:39:40 +0000955 }
956
Josh Coalsond86e03b2002-08-03 21:56:15 +0000957 if(encoder->protected_->verify)
958 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
959
Josh Coalsonfa697a92001-08-16 20:07:29 +0000960 return encoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000961}
962
Josh Coalson6afed9f2002-10-16 22:29:47 +0000963FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000964{
Josh Coalsonf1eff452002-07-31 07:05:33 +0000965 FLAC__ASSERT(0 != encoder);
Josh Coalson2b245f22002-08-07 17:10:50 +0000966
Josh Coalsonfa697a92001-08-16 20:07:29 +0000967 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000968 return;
Josh Coalson2b245f22002-08-07 17:10:50 +0000969
Josh Coalson3262b0d2002-08-14 20:58:42 +0000970 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +0000971 if(encoder->private_->current_sample_number != 0) {
972 encoder->protected_->blocksize = encoder->private_->current_sample_number;
973 process_frame_(encoder, true); /* true => is last frame */
974 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000975 }
Josh Coalson2b245f22002-08-07 17:10:50 +0000976
Josh Coalson3e7a96e2004-07-23 05:18:22 +0000977 FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +0000978
Josh Coalson3262b0d2002-08-14 20:58:42 +0000979 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +0000980 encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
981 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000982
Josh Coalsond86e03b2002-08-03 21:56:15 +0000983 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
984 FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
985
Josh Coalsonf1eff452002-07-31 07:05:33 +0000986 free_(encoder);
987 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000988
Josh Coalsonfa697a92001-08-16 20:07:29 +0000989 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000990}
991
Josh Coalson6afed9f2002-10-16 22:29:47 +0000992FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000993{
994 FLAC__ASSERT(0 != encoder);
995 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
996 return false;
997 encoder->protected_->verify = value;
998 return true;
999}
1000
Josh Coalson6afed9f2002-10-16 22:29:47 +00001001FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001002{
Josh Coalson92031602002-07-24 06:02:11 +00001003 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001004 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001005 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001006 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001007 return true;
1008}
1009
Josh Coalson6afed9f2002-10-16 22:29:47 +00001010FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001011{
Josh Coalson92031602002-07-24 06:02:11 +00001012 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001013 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001014 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001015 encoder->protected_->do_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001016 return true;
1017}
1018
Josh Coalson6afed9f2002-10-16 22:29:47 +00001019FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001020{
Josh Coalson92031602002-07-24 06:02:11 +00001021 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001022 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001023 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001024 encoder->protected_->loose_mid_side_stereo = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001025 return true;
1026}
1027
Josh Coalson6afed9f2002-10-16 22:29:47 +00001028FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001029{
Josh Coalson92031602002-07-24 06:02:11 +00001030 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001031 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001032 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001033 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001034 return true;
1035}
1036
Josh Coalson6afed9f2002-10-16 22:29:47 +00001037FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001038{
Josh Coalson92031602002-07-24 06:02:11 +00001039 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001040 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001041 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001042 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001043 return true;
1044}
1045
Josh Coalson6afed9f2002-10-16 22:29:47 +00001046FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001047{
Josh Coalson92031602002-07-24 06:02:11 +00001048 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001049 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001050 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001051 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001052 return true;
1053}
1054
Josh Coalson6afed9f2002-10-16 22:29:47 +00001055FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001056{
Josh Coalson92031602002-07-24 06:02:11 +00001057 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001058 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001059 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001060 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001061 return true;
1062}
1063
Josh Coalson6afed9f2002-10-16 22:29:47 +00001064FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001065{
Josh Coalson92031602002-07-24 06:02:11 +00001066 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001067 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001068 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001069 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001070 return true;
1071}
1072
Josh Coalson6afed9f2002-10-16 22:29:47 +00001073FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001074{
Josh Coalson92031602002-07-24 06:02:11 +00001075 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001076 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001077 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001078 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001079 return true;
1080}
1081
Josh Coalson6afed9f2002-10-16 22:29:47 +00001082FLAC_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 +00001083{
Josh Coalson92031602002-07-24 06:02:11 +00001084 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001085 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001086 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001087 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001088 return true;
1089}
1090
Josh Coalson6afed9f2002-10-16 22:29:47 +00001091FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001092{
Josh Coalson92031602002-07-24 06:02:11 +00001093 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001094 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001095 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001096#if 0
1097 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001098 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001099#else
1100 (void)value;
1101#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001102 return true;
1103}
1104
Josh Coalson6afed9f2002-10-16 22:29:47 +00001105FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001106{
Josh Coalson92031602002-07-24 06:02:11 +00001107 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001108 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001109 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001110 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001111 return true;
1112}
1113
Josh Coalson6afed9f2002-10-16 22:29:47 +00001114FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001115{
Josh Coalson92031602002-07-24 06:02:11 +00001116 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001117 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001118 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001119 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001120 return true;
1121}
1122
Josh Coalson6afed9f2002-10-16 22:29:47 +00001123FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001124{
Josh Coalson92031602002-07-24 06:02:11 +00001125 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001126 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001127 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001128 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001129 return true;
1130}
1131
Josh Coalson6afed9f2002-10-16 22:29:47 +00001132FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +00001133{
Josh Coalson92031602002-07-24 06:02:11 +00001134 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001135 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001136 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001137#if 0
1138 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001139 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001140#else
1141 (void)value;
1142#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001143 return true;
1144}
1145
Josh Coalson6afed9f2002-10-16 22:29:47 +00001146FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001147{
Josh Coalson92031602002-07-24 06:02:11 +00001148 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001149 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001150 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001151 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001152 return true;
1153}
1154
Josh Coalson6afed9f2002-10-16 22:29:47 +00001155FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001156{
Josh Coalson92031602002-07-24 06:02:11 +00001157 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001158 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001159 return false;
Josh Coalson66075c12002-06-01 05:39:38 +00001160 encoder->protected_->metadata = metadata;
1161 encoder->protected_->num_metadata_blocks = num_blocks;
Josh Coalson00e53872001-06-16 07:32:25 +00001162 return true;
1163}
1164
Josh Coalson6afed9f2002-10-16 22:29:47 +00001165FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001166{
Josh Coalson92031602002-07-24 06:02:11 +00001167 FLAC__ASSERT(0 != encoder);
1168 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001169 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001170 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001171 encoder->private_->write_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001172 return true;
1173}
1174
Josh Coalson6afed9f2002-10-16 22:29:47 +00001175FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +00001176{
Josh Coalson92031602002-07-24 06:02:11 +00001177 FLAC__ASSERT(0 != encoder);
1178 FLAC__ASSERT(0 != value);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001179 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001180 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001181 encoder->private_->metadata_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001182 return true;
1183}
1184
Josh Coalson6afed9f2002-10-16 22:29:47 +00001185FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +00001186{
Josh Coalson92031602002-07-24 06:02:11 +00001187 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001188 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001189 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001190 encoder->private_->client_data = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001191 return true;
1192}
1193
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001194/*
1195 * These three functions are not static, but not publically exposed in
1196 * include/FLAC/ either. They are used by the test suite.
1197 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001198FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001199{
1200 FLAC__ASSERT(0 != encoder);
1201 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1202 return false;
1203 encoder->private_->disable_constant_subframes = value;
1204 return true;
1205}
1206
Josh Coalson6afed9f2002-10-16 22:29:47 +00001207FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001208{
1209 FLAC__ASSERT(0 != encoder);
1210 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1211 return false;
1212 encoder->private_->disable_fixed_subframes = value;
1213 return true;
1214}
1215
Josh Coalson6afed9f2002-10-16 22:29:47 +00001216FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001217{
1218 FLAC__ASSERT(0 != encoder);
1219 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1220 return false;
1221 encoder->private_->disable_verbatim_subframes = value;
1222 return true;
1223}
1224
Josh Coalson6afed9f2002-10-16 22:29:47 +00001225FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001226{
Josh Coalson92031602002-07-24 06:02:11 +00001227 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001228 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001229}
1230
Josh Coalson6afed9f2002-10-16 22:29:47 +00001231FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001232{
1233 FLAC__ASSERT(0 != encoder);
1234 if(encoder->protected_->verify)
1235 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1236 else
1237 return FLAC__STREAM_DECODER_UNINITIALIZED;
1238}
1239
Josh Coalson02954222002-11-08 06:16:31 +00001240FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1241{
1242 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1243 return FLAC__StreamEncoderStateString[encoder->protected_->state];
1244 else
Josh Coalson807140d2003-09-24 22:10:51 +00001245 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00001246}
1247
Josh Coalson6afed9f2002-10-16 22:29:47 +00001248FLAC_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 +00001249{
1250 FLAC__ASSERT(0 != encoder);
1251 if(0 != absolute_sample)
1252 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1253 if(0 != frame_number)
1254 *frame_number = encoder->private_->verify.error_stats.frame_number;
1255 if(0 != channel)
1256 *channel = encoder->private_->verify.error_stats.channel;
1257 if(0 != sample)
1258 *sample = encoder->private_->verify.error_stats.sample;
1259 if(0 != expected)
1260 *expected = encoder->private_->verify.error_stats.expected;
1261 if(0 != got)
1262 *got = encoder->private_->verify.error_stats.got;
1263}
1264
Josh Coalson6afed9f2002-10-16 22:29:47 +00001265FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001266{
1267 FLAC__ASSERT(0 != encoder);
1268 return encoder->protected_->verify;
1269}
1270
Josh Coalson6afed9f2002-10-16 22:29:47 +00001271FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001272{
Josh Coalson92031602002-07-24 06:02:11 +00001273 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001274 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00001275}
1276
Josh Coalson6afed9f2002-10-16 22:29:47 +00001277FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001278{
Josh Coalson92031602002-07-24 06:02:11 +00001279 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001280 return encoder->protected_->do_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001281}
1282
Josh Coalson6afed9f2002-10-16 22:29:47 +00001283FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001284{
Josh Coalson92031602002-07-24 06:02:11 +00001285 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001286 return encoder->protected_->loose_mid_side_stereo;
Josh Coalson0a15c142001-06-13 17:59:57 +00001287}
1288
Josh Coalson6afed9f2002-10-16 22:29:47 +00001289FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001290{
Josh Coalson92031602002-07-24 06:02:11 +00001291 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001292 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00001293}
1294
Josh Coalson6afed9f2002-10-16 22:29:47 +00001295FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001296{
Josh Coalson92031602002-07-24 06:02:11 +00001297 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001298 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00001299}
1300
Josh Coalson6afed9f2002-10-16 22:29:47 +00001301FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001302{
Josh Coalson92031602002-07-24 06:02:11 +00001303 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001304 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00001305}
1306
Josh Coalson6afed9f2002-10-16 22:29:47 +00001307FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001308{
Josh Coalson92031602002-07-24 06:02:11 +00001309 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001310 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001311}
1312
Josh Coalson6afed9f2002-10-16 22:29:47 +00001313FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001314{
Josh Coalson92031602002-07-24 06:02:11 +00001315 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001316 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001317}
1318
Josh Coalson6afed9f2002-10-16 22:29:47 +00001319FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001320{
Josh Coalson92031602002-07-24 06:02:11 +00001321 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001322 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00001323}
1324
Josh Coalson6afed9f2002-10-16 22:29:47 +00001325FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001326{
Josh Coalson92031602002-07-24 06:02:11 +00001327 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001328 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001329}
1330
Josh Coalson6afed9f2002-10-16 22:29:47 +00001331FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00001332{
Josh Coalson92031602002-07-24 06:02:11 +00001333 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001334 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00001335}
1336
Josh Coalson6afed9f2002-10-16 22:29:47 +00001337FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001338{
Josh Coalson92031602002-07-24 06:02:11 +00001339 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001340 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00001341}
1342
Josh Coalson6afed9f2002-10-16 22:29:47 +00001343FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001344{
Josh Coalson92031602002-07-24 06:02:11 +00001345 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001346 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001347}
1348
Josh Coalson6afed9f2002-10-16 22:29:47 +00001349FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001350{
Josh Coalson92031602002-07-24 06:02:11 +00001351 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001352 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00001353}
1354
Josh Coalson6afed9f2002-10-16 22:29:47 +00001355FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001356{
Josh Coalson92031602002-07-24 06:02:11 +00001357 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001358 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00001359}
1360
Josh Coalson6afed9f2002-10-16 22:29:47 +00001361FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00001362{
1363 FLAC__ASSERT(0 != encoder);
1364 return encoder->protected_->total_samples_estimate;
1365}
1366
Josh Coalson6afed9f2002-10-16 22:29:47 +00001367FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001368{
1369 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001370 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001371 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001372
Josh Coalsonf1eff452002-07-31 07:05:33 +00001373 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001374 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001375
1376 j = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001377 /*
1378 * we have several flavors of the same basic loop, optimized for
1379 * different conditions:
1380 */
1381 if(encoder->protected_->max_lpc_order > 0) {
1382 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1383 /*
1384 * stereo coding: unroll channel loop
1385 * with LPC: calculate floating point version of signal
1386 */
1387 do {
1388 if(encoder->protected_->verify)
1389 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 +00001390
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001391 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1392 x = mid = side = buffer[0][j];
1393 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001394#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001395 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001396#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001397 x = buffer[1][j];
1398 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001399#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001400 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001401#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001402 mid += x;
1403 side -= x;
1404 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1405 encoder->private_->integer_signal_mid_side[1][i] = side;
1406 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001407#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001408 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1409 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001410#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001411 encoder->private_->current_sample_number++;
1412 }
1413 if(i == blocksize) {
1414 if(!process_frame_(encoder, false)) /* false => not last frame */
1415 return false;
1416 }
1417 } while(j < samples);
1418 }
1419 else {
1420 /*
1421 * independent channel coding: buffer each channel in inner loop
1422 * with LPC: calculate floating point version of signal
1423 */
1424 do {
1425 if(encoder->protected_->verify)
1426 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1427
1428 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1429 for(channel = 0; channel < channels; channel++) {
1430 x = buffer[channel][j];
1431 encoder->private_->integer_signal[channel][i] = x;
1432#ifndef FLAC__INTEGER_ONLY_LIBRARY
1433 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1434#endif
1435 }
1436 encoder->private_->current_sample_number++;
1437 }
1438 if(i == blocksize) {
1439 if(!process_frame_(encoder, false)) /* false => not last frame */
1440 return false;
1441 }
1442 } while(j < samples);
1443 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001444 }
1445 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001446 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1447 /*
1448 * stereo coding: unroll channel loop
1449 * without LPC: no need to calculate floating point version of signal
1450 */
1451 do {
1452 if(encoder->protected_->verify)
1453 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 +00001454
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001455 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1456 encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1457 x = buffer[1][j];
1458 encoder->private_->integer_signal[1][i] = x;
1459 mid += x;
1460 side -= x;
1461 mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1462 encoder->private_->integer_signal_mid_side[1][i] = side;
1463 encoder->private_->integer_signal_mid_side[0][i] = mid;
1464 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001465 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001466 if(i == blocksize) {
1467 if(!process_frame_(encoder, false)) /* false => not last frame */
1468 return false;
1469 }
1470 } while(j < samples);
1471 }
1472 else {
1473 /*
1474 * independent channel coding: buffer each channel in inner loop
1475 * without LPC: no need to calculate floating point version of signal
1476 */
1477 do {
1478 if(encoder->protected_->verify)
1479 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1480
1481 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1482 for(channel = 0; channel < channels; channel++)
1483 encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1484 encoder->private_->current_sample_number++;
1485 }
1486 if(i == blocksize) {
1487 if(!process_frame_(encoder, false)) /* false => not last frame */
1488 return false;
1489 }
1490 } while(j < samples);
1491 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001492 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001493
1494 return true;
1495}
1496
Josh Coalson6afed9f2002-10-16 22:29:47 +00001497FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001498{
1499 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00001500 FLAC__int32 x, mid, side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001501 const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001502
Josh Coalsonf1eff452002-07-31 07:05:33 +00001503 FLAC__ASSERT(0 != encoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001504 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001505
1506 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001507 /*
1508 * we have several flavors of the same basic loop, optimized for
1509 * different conditions:
1510 */
1511 if(encoder->protected_->max_lpc_order > 0) {
1512 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1513 /*
1514 * stereo coding: unroll channel loop
1515 * with LPC: calculate floating point version of signal
1516 */
1517 do {
1518 if(encoder->protected_->verify)
1519 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 +00001520
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001521 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1522 x = mid = side = buffer[k++];
1523 encoder->private_->integer_signal[0][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001524#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001525 encoder->private_->real_signal[0][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001526#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001527 x = buffer[k++];
1528 encoder->private_->integer_signal[1][i] = x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001529#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001530 encoder->private_->real_signal[1][i] = (FLAC__real)x;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001531#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001532 mid += x;
1533 side -= x;
1534 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1535 encoder->private_->integer_signal_mid_side[1][i] = side;
1536 encoder->private_->integer_signal_mid_side[0][i] = mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001537#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001538 encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1539 encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001540#endif
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001541 encoder->private_->current_sample_number++;
1542 }
1543 if(i == blocksize) {
1544 if(!process_frame_(encoder, false)) /* false => not last frame */
1545 return false;
1546 }
1547 } while(j < samples);
1548 }
1549 else {
1550 /*
1551 * independent channel coding: buffer each channel in inner loop
1552 * with LPC: calculate floating point version of signal
1553 */
1554 do {
1555 if(encoder->protected_->verify)
1556 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1557
1558 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1559 for(channel = 0; channel < channels; channel++) {
1560 x = buffer[k++];
1561 encoder->private_->integer_signal[channel][i] = x;
1562#ifndef FLAC__INTEGER_ONLY_LIBRARY
1563 encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1564#endif
1565 }
1566 encoder->private_->current_sample_number++;
1567 }
1568 if(i == blocksize) {
1569 if(!process_frame_(encoder, false)) /* false => not last frame */
1570 return false;
1571 }
1572 } while(j < samples);
1573 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001574 }
1575 else {
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001576 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1577 /*
1578 * stereo coding: unroll channel loop
1579 * without LPC: no need to calculate floating point version of signal
1580 */
1581 do {
1582 if(encoder->protected_->verify)
1583 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 +00001584
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001585 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1586 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
Josh Coalson57ba6f42002-06-07 05:27:37 +00001587 x = buffer[k++];
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001588 encoder->private_->integer_signal[1][i] = x;
1589 mid += x;
1590 side -= x;
1591 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1592 encoder->private_->integer_signal_mid_side[1][i] = side;
1593 encoder->private_->integer_signal_mid_side[0][i] = mid;
1594 encoder->private_->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +00001595 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001596 if(i == blocksize) {
1597 if(!process_frame_(encoder, false)) /* false => not last frame */
1598 return false;
1599 }
1600 } while(j < samples);
1601 }
1602 else {
1603 /*
1604 * independent channel coding: buffer each channel in inner loop
1605 * without LPC: no need to calculate floating point version of signal
1606 */
1607 do {
1608 if(encoder->protected_->verify)
1609 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1610
1611 for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1612 for(channel = 0; channel < channels; channel++)
1613 encoder->private_->integer_signal[channel][i] = buffer[k++];
1614 encoder->private_->current_sample_number++;
1615 }
1616 if(i == blocksize) {
1617 if(!process_frame_(encoder, false)) /* false => not last frame */
1618 return false;
1619 }
1620 } while(j < samples);
1621 }
Josh Coalsonaa255362001-05-31 06:17:41 +00001622 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001623
1624 return true;
1625}
1626
Josh Coalsonf1eff452002-07-31 07:05:33 +00001627/***********************************************************************
1628 *
1629 * Private class methods
1630 *
1631 ***********************************************************************/
1632
1633void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00001634{
1635 FLAC__ASSERT(0 != encoder);
1636
Josh Coalsond86e03b2002-08-03 21:56:15 +00001637 encoder->protected_->verify = false;
Josh Coalson92031602002-07-24 06:02:11 +00001638 encoder->protected_->streamable_subset = true;
1639 encoder->protected_->do_mid_side_stereo = false;
1640 encoder->protected_->loose_mid_side_stereo = false;
1641 encoder->protected_->channels = 2;
1642 encoder->protected_->bits_per_sample = 16;
1643 encoder->protected_->sample_rate = 44100;
1644 encoder->protected_->blocksize = 1152;
1645 encoder->protected_->max_lpc_order = 0;
1646 encoder->protected_->qlp_coeff_precision = 0;
1647 encoder->protected_->do_qlp_coeff_prec_search = false;
1648 encoder->protected_->do_exhaustive_model_search = false;
1649 encoder->protected_->do_escape_coding = false;
1650 encoder->protected_->min_residual_partition_order = 0;
1651 encoder->protected_->max_residual_partition_order = 0;
1652 encoder->protected_->rice_parameter_search_dist = 0;
1653 encoder->protected_->total_samples_estimate = 0;
1654 encoder->protected_->metadata = 0;
1655 encoder->protected_->num_metadata_blocks = 0;
1656
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001657 encoder->private_->disable_constant_subframes = false;
1658 encoder->private_->disable_fixed_subframes = false;
1659 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson92031602002-07-24 06:02:11 +00001660 encoder->private_->write_callback = 0;
1661 encoder->private_->metadata_callback = 0;
1662 encoder->private_->client_data = 0;
1663}
1664
Josh Coalsonf1eff452002-07-31 07:05:33 +00001665void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00001666{
1667 unsigned i, channel;
1668
Josh Coalsonf1eff452002-07-31 07:05:33 +00001669 FLAC__ASSERT(0 != encoder);
Josh Coalson639aeb02002-07-25 05:38:23 +00001670 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001671 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001672 free(encoder->private_->integer_signal_unaligned[i]);
1673 encoder->private_->integer_signal_unaligned[i] = 0;
1674 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001675#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00001676 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001677 free(encoder->private_->real_signal_unaligned[i]);
1678 encoder->private_->real_signal_unaligned[i] = 0;
1679 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001680#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001681 }
1682 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001683 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001684 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
1685 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
1686 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001687#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00001688 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001689 free(encoder->private_->real_signal_mid_side_unaligned[i]);
1690 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
1691 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001692#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00001693 }
1694 for(channel = 0; channel < encoder->protected_->channels; channel++) {
1695 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001696 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001697 free(encoder->private_->residual_workspace_unaligned[channel][i]);
1698 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
1699 }
1700 }
1701 }
1702 for(channel = 0; channel < 2; channel++) {
1703 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00001704 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001705 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
1706 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
1707 }
1708 }
1709 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001710 if(0 != encoder->private_->abs_residual_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001711 free(encoder->private_->abs_residual_unaligned);
1712 encoder->private_->abs_residual_unaligned = 0;
1713 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001714 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001715 free(encoder->private_->abs_residual_partition_sums_unaligned);
1716 encoder->private_->abs_residual_partition_sums_unaligned = 0;
1717 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00001718 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00001719 free(encoder->private_->raw_bits_per_partition_unaligned);
1720 encoder->private_->raw_bits_per_partition_unaligned = 0;
1721 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001722 if(encoder->protected_->verify) {
1723 for(i = 0; i < encoder->protected_->channels; i++) {
1724 if(0 != encoder->private_->verify.input_fifo.data[i]) {
1725 free(encoder->private_->verify.input_fifo.data[i]);
1726 encoder->private_->verify.input_fifo.data[i] = 0;
1727 }
1728 }
1729 }
Josh Coalson639aeb02002-07-25 05:38:23 +00001730 FLAC__bitbuffer_free(encoder->private_->frame);
1731}
1732
Josh Coalsonf1eff452002-07-31 07:05:33 +00001733FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001734{
Josh Coalson77e3f312001-06-23 03:03:24 +00001735 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +00001736 unsigned i, channel;
1737
1738 FLAC__ASSERT(new_size > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001739 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1740 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00001741
1742 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001743 if(new_size <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00001744 return true;
1745
1746 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00001747
Josh Coalsonc9c0d132002-10-04 05:29:05 +00001748 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1749 * requires that the input arrays (in our case the integer signals)
1750 * have a buffer of up to 3 zeroes in front (at negative indices) for
1751 * alignment purposes; we use 4 to keep the data well-aligned.
1752 */
Josh Coalson8395d022001-07-12 21:25:22 +00001753
Josh Coalsonfa697a92001-08-16 20:07:29 +00001754 for(i = 0; ok && i < encoder->protected_->channels; i++) {
1755 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 +00001756#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001757 if(encoder->protected_->max_lpc_order > 0)
1758 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 +00001759#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00001760 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1761 encoder->private_->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001762 }
1763 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001764 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 +00001765#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonc549f0f2004-12-30 03:47:49 +00001766 if(encoder->protected_->max_lpc_order > 0)
1767 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 +00001768#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00001769 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1770 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +00001771 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001772 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001773 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001774 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 +00001775 }
1776 }
1777 for(channel = 0; ok && channel < 2; channel++) {
1778 for(i = 0; ok && i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001779 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 +00001780 }
1781 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001782 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1783 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 */
1784 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1785 if(encoder->protected_->do_escape_coding)
1786 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 +00001787
1788 if(ok)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001789 encoder->private_->input_capacity = new_size;
Josh Coalson0a15c142001-06-13 17:59:57 +00001790 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001791 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson0a15c142001-06-13 17:59:57 +00001792
1793 return ok;
1794}
1795
Josh Coalsond86e03b2002-08-03 21:56:15 +00001796FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
Josh Coalson5c491a12002-08-01 06:39:40 +00001797{
1798 const FLAC__byte *buffer;
1799 unsigned bytes;
1800
1801 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1802
1803 FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
1804
Josh Coalsond86e03b2002-08-03 21:56:15 +00001805 if(encoder->protected_->verify) {
1806 encoder->private_->verify.output.data = buffer;
1807 encoder->private_->verify.output.bytes = bytes;
1808 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
1809 encoder->private_->verify.needs_magic_hack = true;
1810 }
1811 else {
1812 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1813 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1814 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1815 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1816 return false;
1817 }
1818 }
1819 }
1820
1821 if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalsondd190232002-12-29 09:30:23 +00001822 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001823 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalson5c491a12002-08-01 06:39:40 +00001824 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001825 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001826
1827 FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1828
Josh Coalsond86e03b2002-08-03 21:56:15 +00001829 if(samples > 0) {
1830 encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1831 encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
1832 }
1833
Josh Coalson5c491a12002-08-01 06:39:40 +00001834 return true;
1835}
1836
Josh Coalsonf1eff452002-07-31 07:05:33 +00001837FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +00001838{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001839 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001840
1841 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00001842 * Accumulate raw signal to the MD5 signature
1843 */
Josh Coalson57ba6f42002-06-07 05:27:37 +00001844 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 +00001845 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00001846 return false;
1847 }
1848
1849 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001850 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001851 */
Josh Coalsonf1eff452002-07-31 07:05:33 +00001852 if(!process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001853 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001854 return false;
1855 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001856
1857 /*
1858 * Zero-pad the frame to a byte_boundary
1859 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001860 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001861 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001862 return false;
1863 }
1864
1865 /*
Josh Coalson215af572001-03-27 01:15:58 +00001866 * CRC-16 the whole thing
1867 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001868 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1869 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 +00001870
1871 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001872 * Write it
1873 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001874 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
1875 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001876 return false;
1877 }
1878
1879 /*
1880 * Get ready for the next frame
1881 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001882 encoder->private_->current_sample_number = 0;
1883 encoder->private_->current_frame_number++;
1884 encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001885
1886 return true;
1887}
1888
Josh Coalsonf1eff452002-07-31 07:05:33 +00001889FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001890{
1891 FLAC__FrameHeader frame_header;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001892 unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00001893 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001894
1895 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00001896 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00001897 */
1898 if(is_last_frame) {
1899 max_partition_order = 0;
1900 }
1901 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001902 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
1903 max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001904 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001905 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001906
Josh Coalsonfa697a92001-08-16 20:07:29 +00001907 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 +00001908
Josh Coalson94e02cd2001-01-25 10:41:06 +00001909 /*
1910 * Setup the frame
1911 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001912 if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001913 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001914 return false;
1915 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001916 frame_header.blocksize = encoder->protected_->blocksize;
1917 frame_header.sample_rate = encoder->protected_->sample_rate;
1918 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001919 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001920 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001921 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001922 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001923
1924 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001925 * Figure out what channel assignments to try
1926 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001927 if(encoder->protected_->do_mid_side_stereo) {
1928 if(encoder->protected_->loose_mid_side_stereo) {
1929 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001930 do_independent = true;
1931 do_mid_side = true;
1932 }
1933 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001934 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001935 do_mid_side = !do_independent;
1936 }
1937 }
1938 else {
1939 do_independent = true;
1940 do_mid_side = true;
1941 }
1942 }
1943 else {
1944 do_independent = true;
1945 do_mid_side = false;
1946 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001947
Josh Coalson1b689822001-05-31 20:11:02 +00001948 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001949
1950 /*
Josh Coalson82b73242001-03-28 22:17:05 +00001951 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00001952 */
1953 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001954 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001955 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001956 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
1957 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00001958 }
Josh Coalson859bc542001-03-27 22:22:27 +00001959 }
1960 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001961 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00001962 for(channel = 0; channel < 2; channel++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00001963 const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001964 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1965 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00001966 }
Josh Coalson859bc542001-03-27 22:22:27 +00001967 }
1968
1969 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001970 * First do a normal encoding pass of each independent channel
1971 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001972 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001973 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00001974 if(!
1975 process_subframe_(
1976 encoder,
1977 min_partition_order,
1978 max_partition_order,
1979 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00001980 &frame_header,
1981 encoder->private_->subframe_bps[channel],
1982 encoder->private_->integer_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001983#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00001984 encoder->private_->real_signal[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001985#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00001986 encoder->private_->subframe_workspace_ptr[channel],
1987 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1988 encoder->private_->residual_workspace[channel],
1989 encoder->private_->best_subframe+channel,
1990 encoder->private_->best_subframe_bits+channel
1991 )
1992 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001993 return false;
1994 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001995 }
1996
1997 /*
1998 * Now do mid and side channels if requested
1999 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002000 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002001 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002002
2003 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002004 if(!
2005 process_subframe_(
2006 encoder,
2007 min_partition_order,
2008 max_partition_order,
2009 precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002010 &frame_header,
2011 encoder->private_->subframe_bps_mid_side[channel],
2012 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002013#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002014 encoder->private_->real_signal_mid_side[channel],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002015#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002016 encoder->private_->subframe_workspace_ptr_mid_side[channel],
2017 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
2018 encoder->private_->residual_workspace_mid_side[channel],
2019 encoder->private_->best_subframe_mid_side+channel,
2020 encoder->private_->best_subframe_bits_mid_side+channel
2021 )
2022 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00002023 return false;
2024 }
2025 }
2026
2027 /*
2028 * Compose the frame bitbuffer
2029 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002030 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00002031 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
2032 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002033 FLAC__ChannelAssignment channel_assignment;
2034
Josh Coalsonfa697a92001-08-16 20:07:29 +00002035 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002036
Josh Coalsonfa697a92001-08-16 20:07:29 +00002037 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
2038 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 +00002039 }
2040 else {
2041 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
2042 unsigned min_bits;
2043 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002044
Josh Coalson1b689822001-05-31 20:11:02 +00002045 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002046
2047 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002048 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
2049 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
2050 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
2051 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 +00002052
Josh Coalson7424d2f2002-11-06 07:10:38 +00002053 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 +00002054 if(bits[ca] < min_bits) {
2055 min_bits = bits[ca];
2056 channel_assignment = ca;
2057 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002058 }
2059 }
2060
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002061 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002062
Josh Coalson3e7a96e2004-07-23 05:18:22 +00002063 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002064 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002065 return false;
2066 }
2067
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002068 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002069 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002070 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2071 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002072 break;
2073 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002074 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
2075 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002076 break;
2077 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002078 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
2079 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002080 break;
2081 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002082 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
2083 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002084 break;
2085 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002086 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002087 }
Josh Coalson82b73242001-03-28 22:17:05 +00002088
2089 switch(channel_assignment) {
2090 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002091 left_bps = encoder->private_->subframe_bps [0];
2092 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00002093 break;
2094 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002095 left_bps = encoder->private_->subframe_bps [0];
2096 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00002097 break;
2098 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002099 left_bps = encoder->private_->subframe_bps_mid_side[1];
2100 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00002101 break;
2102 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002103 left_bps = encoder->private_->subframe_bps_mid_side[0];
2104 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00002105 break;
2106 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002107 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00002108 }
2109
2110 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonf1eff452002-07-31 07:05:33 +00002111 if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00002112 return false;
Josh Coalsonf1eff452002-07-31 07:05:33 +00002113 if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00002114 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002115 }
2116 else {
Josh Coalson3e7a96e2004-07-23 05:18:22 +00002117 if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002118 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002119 return false;
2120 }
2121
Josh Coalsonfa697a92001-08-16 20:07:29 +00002122 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002123 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 +00002124 /* the above function sets the state for us in case of an error */
2125 return false;
2126 }
2127 }
2128 }
2129
Josh Coalsonfa697a92001-08-16 20:07:29 +00002130 if(encoder->protected_->loose_mid_side_stereo) {
2131 encoder->private_->loose_mid_side_stereo_frame_count++;
2132 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
2133 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002134 }
2135
Josh Coalsonfa697a92001-08-16 20:07:29 +00002136 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002137
Josh Coalson94e02cd2001-01-25 10:41:06 +00002138 return true;
2139}
2140
Josh Coalson6fe72f72002-08-20 04:01:59 +00002141FLAC__bool process_subframe_(
2142 FLAC__StreamEncoder *encoder,
2143 unsigned min_partition_order,
2144 unsigned max_partition_order,
2145 FLAC__bool precompute_partition_sums,
Josh Coalson6fe72f72002-08-20 04:01:59 +00002146 const FLAC__FrameHeader *frame_header,
2147 unsigned subframe_bps,
2148 const FLAC__int32 integer_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002149#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002150 const FLAC__real real_signal[],
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002151#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00002152 FLAC__Subframe *subframe[2],
2153 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
2154 FLAC__int32 *residual[2],
2155 unsigned *best_subframe,
2156 unsigned *best_bits
2157)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002158{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002159#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002160 FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002161#else
2162 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
2163#endif
2164#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002165 FLAC__double lpc_residual_bits_per_sample;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002166 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 +00002167 FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002168 unsigned min_lpc_order, max_lpc_order, lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002169 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002170#endif
2171 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002172 unsigned rice_parameter;
2173 unsigned _candidate_bits, _best_bits;
2174 unsigned _best_subframe;
2175
2176 /* verbatim subframe is the baseline against which we measure other compressed subframes */
2177 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002178 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
2179 _best_bits = UINT_MAX;
2180 else
2181 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002182
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002183 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
2184 unsigned signal_is_constant = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002185 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 +00002186 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002187 if(
2188 !encoder->private_->disable_constant_subframes &&
2189#ifndef FLAC__INTEGER_ONLY_LIBRARY
2190 fixed_residual_bits_per_sample[1] == 0.0
2191#else
2192 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
2193#endif
2194 ) {
2195 /* the above means it's possible all samples are the same value; now double-check it: */
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002196 unsigned i;
2197 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002198 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00002199 if(integer_signal[0] != integer_signal[i]) {
2200 signal_is_constant = false;
2201 break;
2202 }
2203 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002204 }
2205 if(signal_is_constant) {
2206 _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
2207 if(_candidate_bits < _best_bits) {
2208 _best_subframe = !_best_subframe;
2209 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002210 }
2211 }
2212 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002213 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
2214 /* encode fixed */
2215 if(encoder->protected_->do_exhaustive_model_search) {
2216 min_fixed_order = 0;
2217 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00002218 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002219 else {
2220 min_fixed_order = max_fixed_order = guess_fixed_order;
2221 }
2222 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002223#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson09758432004-10-20 00:21:50 +00002224 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002225 continue; /* don't even try */
2226 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 +00002227#else
2228 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
2229 continue; /* don't even try */
2230 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 */
2231#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002232#ifndef FLAC__SYMMETRIC_RICE
2233 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2234#endif
2235 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2236#ifdef DEBUG_VERBOSE
2237 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2238#endif
2239 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2240 }
2241 _candidate_bits =
2242 evaluate_fixed_subframe_(
2243 encoder,
2244 integer_signal,
2245 residual[!_best_subframe],
2246 encoder->private_->abs_residual,
2247 encoder->private_->abs_residual_partition_sums,
2248 encoder->private_->raw_bits_per_partition,
2249 frame_header->blocksize,
2250 subframe_bps,
2251 fixed_order,
2252 rice_parameter,
2253 min_partition_order,
2254 max_partition_order,
2255 precompute_partition_sums,
2256 encoder->protected_->do_escape_coding,
2257 encoder->protected_->rice_parameter_search_dist,
2258 subframe[!_best_subframe],
2259 partitioned_rice_contents[!_best_subframe]
2260 );
2261 if(_candidate_bits < _best_bits) {
2262 _best_subframe = !_best_subframe;
2263 _best_bits = _candidate_bits;
2264 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002265 }
2266 }
2267
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002268#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00002269 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00002270 if(encoder->protected_->max_lpc_order > 0) {
2271 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002272 max_lpc_order = frame_header->blocksize-1;
2273 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00002274 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002275 if(max_lpc_order > 0) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002276 encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002277 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2278 if(autoc[0] != 0.0) {
Josh Coalson8084b052001-11-01 00:27:29 +00002279 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002280 if(encoder->protected_->do_exhaustive_model_search) {
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002281 min_lpc_order = 1;
2282 }
2283 else {
Josh Coalson82b73242001-03-28 22:17:05 +00002284 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002285 min_lpc_order = max_lpc_order = guess_lpc_order;
2286 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002287 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2288 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
Josh Coalson09758432004-10-20 00:21:50 +00002289 if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002290 continue; /* don't even try */
2291 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
Josh Coalsonbb6712e2001-04-24 22:54:07 +00002292#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002293 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00002294#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002295 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002296#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002297 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2298#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00002299 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002300 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002301 if(encoder->protected_->do_qlp_coeff_prec_search) {
2302 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2303 /* ensure a 32-bit datapath throughout for 16bps or less */
2304 if(subframe_bps <= 16)
2305 max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2306 else
2307 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
2308 }
2309 else {
2310 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2311 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002312 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00002313 _candidate_bits =
2314 evaluate_lpc_subframe_(
2315 encoder,
2316 integer_signal,
2317 residual[!_best_subframe],
2318 encoder->private_->abs_residual,
2319 encoder->private_->abs_residual_partition_sums,
2320 encoder->private_->raw_bits_per_partition,
2321 encoder->private_->lp_coeff[lpc_order-1],
2322 frame_header->blocksize,
2323 subframe_bps,
2324 lpc_order,
2325 qlp_coeff_precision,
2326 rice_parameter,
2327 min_partition_order,
2328 max_partition_order,
2329 precompute_partition_sums,
2330 encoder->protected_->do_escape_coding,
2331 encoder->protected_->rice_parameter_search_dist,
2332 subframe[!_best_subframe],
2333 partitioned_rice_contents[!_best_subframe]
2334 );
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00002335 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2336 if(_candidate_bits < _best_bits) {
2337 _best_subframe = !_best_subframe;
2338 _best_bits = _candidate_bits;
2339 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002340 }
2341 }
2342 }
2343 }
2344 }
2345 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002346#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00002347 }
2348 }
2349
Josh Coalson72695802002-10-11 06:25:16 +00002350 /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2351 if(_best_bits == UINT_MAX) {
2352 FLAC__ASSERT(_best_subframe == 0);
2353 _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2354 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002355
Josh Coalson94e02cd2001-01-25 10:41:06 +00002356 *best_subframe = _best_subframe;
2357 *best_bits = _best_bits;
2358
2359 return true;
2360}
2361
Josh Coalson6fe72f72002-08-20 04:01:59 +00002362FLAC__bool add_subframe_(
2363 FLAC__StreamEncoder *encoder,
2364 const FLAC__FrameHeader *frame_header,
2365 unsigned subframe_bps,
2366 const FLAC__Subframe *subframe,
2367 FLAC__BitBuffer *frame
2368)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002369{
2370 switch(subframe->type) {
2371 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00002372 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002373 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002374 return false;
2375 }
2376 break;
2377 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00002378 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002379 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002380 return false;
2381 }
2382 break;
2383 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00002384 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002385 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002386 return false;
2387 }
2388 break;
2389 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00002390 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00002391 encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002392 return false;
2393 }
2394 break;
2395 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002396 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002397 }
2398
2399 return true;
2400}
2401
Josh Coalson6fe72f72002-08-20 04:01:59 +00002402unsigned evaluate_constant_subframe_(
2403 const FLAC__int32 signal,
2404 unsigned subframe_bps,
2405 FLAC__Subframe *subframe
2406)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002407{
2408 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2409 subframe->data.constant.value = signal;
2410
Josh Coalson82b73242001-03-28 22:17:05 +00002411 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 +00002412}
2413
Josh Coalson6fe72f72002-08-20 04:01:59 +00002414unsigned evaluate_fixed_subframe_(
2415 FLAC__StreamEncoder *encoder,
2416 const FLAC__int32 signal[],
2417 FLAC__int32 residual[],
2418 FLAC__uint32 abs_residual[],
2419 FLAC__uint64 abs_residual_partition_sums[],
2420 unsigned raw_bits_per_partition[],
2421 unsigned blocksize,
2422 unsigned subframe_bps,
2423 unsigned order,
2424 unsigned rice_parameter,
2425 unsigned min_partition_order,
2426 unsigned max_partition_order,
2427 FLAC__bool precompute_partition_sums,
2428 FLAC__bool do_escape_coding,
2429 unsigned rice_parameter_search_dist,
2430 FLAC__Subframe *subframe,
2431 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2432)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002433{
2434 unsigned i, residual_bits;
2435 const unsigned residual_samples = blocksize - order;
2436
2437 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2438
2439 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
2440
2441 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002442 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002443 subframe->data.fixed.residual = residual;
2444
Josh Coalson6fe72f72002-08-20 04:01:59 +00002445 residual_bits =
2446 find_best_partition_order_(
2447 encoder->private_,
2448 residual,
2449 abs_residual,
2450 abs_residual_partition_sums,
2451 raw_bits_per_partition,
2452 residual_samples,
2453 order,
2454 rice_parameter,
2455 min_partition_order,
2456 max_partition_order,
2457 precompute_partition_sums,
2458 do_escape_coding,
2459 rice_parameter_search_dist,
2460 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2461 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002462
2463 subframe->data.fixed.order = order;
2464 for(i = 0; i < order; i++)
2465 subframe->data.fixed.warmup[i] = signal[i];
2466
Josh Coalson82b73242001-03-28 22:17:05 +00002467 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 +00002468}
2469
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002470#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson6fe72f72002-08-20 04:01:59 +00002471unsigned evaluate_lpc_subframe_(
2472 FLAC__StreamEncoder *encoder,
2473 const FLAC__int32 signal[],
2474 FLAC__int32 residual[],
2475 FLAC__uint32 abs_residual[],
2476 FLAC__uint64 abs_residual_partition_sums[],
2477 unsigned raw_bits_per_partition[],
2478 const FLAC__real lp_coeff[],
2479 unsigned blocksize,
2480 unsigned subframe_bps,
2481 unsigned order,
2482 unsigned qlp_coeff_precision,
2483 unsigned rice_parameter,
2484 unsigned min_partition_order,
2485 unsigned max_partition_order,
2486 FLAC__bool precompute_partition_sums,
2487 FLAC__bool do_escape_coding,
2488 unsigned rice_parameter_search_dist,
2489 FLAC__Subframe *subframe,
2490 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2491)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002492{
Josh Coalson77e3f312001-06-23 03:03:24 +00002493 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00002494 unsigned i, residual_bits;
2495 int quantization, ret;
2496 const unsigned residual_samples = blocksize - order;
2497
Josh Coalson20ac2c12002-08-30 05:47:14 +00002498 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
2499 if(subframe_bps <= 16) {
2500 FLAC__ASSERT(order > 0);
2501 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
2502 qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
2503 }
2504
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002505 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002506 if(ret != 0)
2507 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
2508
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00002509 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2510 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
2511 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
2512 else
2513 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 +00002514 else
2515 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 +00002516
2517 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
2518
2519 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00002520 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002521 subframe->data.lpc.residual = residual;
2522
Josh Coalson6fe72f72002-08-20 04:01:59 +00002523 residual_bits =
2524 find_best_partition_order_(
2525 encoder->private_,
2526 residual,
2527 abs_residual,
2528 abs_residual_partition_sums,
2529 raw_bits_per_partition,
2530 residual_samples,
2531 order,
2532 rice_parameter,
2533 min_partition_order,
2534 max_partition_order,
2535 precompute_partition_sums,
2536 do_escape_coding,
2537 rice_parameter_search_dist,
2538 &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
2539 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00002540
2541 subframe->data.lpc.order = order;
2542 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
2543 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00002544 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00002545 for(i = 0; i < order; i++)
2546 subframe->data.lpc.warmup[i] = signal[i];
2547
Josh Coalson82b73242001-03-28 22:17:05 +00002548 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 +00002549}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002550#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002551
Josh Coalson6fe72f72002-08-20 04:01:59 +00002552unsigned evaluate_verbatim_subframe_(
2553 const FLAC__int32 signal[],
2554 unsigned blocksize,
2555 unsigned subframe_bps,
2556 FLAC__Subframe *subframe
2557)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002558{
2559 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2560
2561 subframe->data.verbatim.data = signal;
2562
Josh Coalson82b73242001-03-28 22:17:05 +00002563 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 +00002564}
2565
Josh Coalson6fe72f72002-08-20 04:01:59 +00002566unsigned find_best_partition_order_(
2567 FLAC__StreamEncoderPrivate *private_,
2568 const FLAC__int32 residual[],
2569 FLAC__uint32 abs_residual[],
2570 FLAC__uint64 abs_residual_partition_sums[],
2571 unsigned raw_bits_per_partition[],
2572 unsigned residual_samples,
2573 unsigned predictor_order,
2574 unsigned rice_parameter,
2575 unsigned min_partition_order,
2576 unsigned max_partition_order,
2577 FLAC__bool precompute_partition_sums,
2578 FLAC__bool do_escape_coding,
2579 unsigned rice_parameter_search_dist,
2580 FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
2581)
Josh Coalson94e02cd2001-01-25 10:41:06 +00002582{
Josh Coalson77e3f312001-06-23 03:03:24 +00002583 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002584 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00002585 unsigned residual_sample;
Josh Coalson8084b052001-11-01 00:27:29 +00002586 unsigned best_parameters_index = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002587 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002588
Josh Coalson2051dd42001-04-12 22:22:34 +00002589 /* compute abs(residual) for use later */
2590 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2591 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00002592 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00002593 }
2594
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002595 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 +00002596 min_partition_order = min(min_partition_order, max_partition_order);
2597
Josh Coalson8395d022001-07-12 21:25:22 +00002598 if(precompute_partition_sums) {
2599 int partition_order;
2600 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002601
Josh Coalsonf1eff452002-07-31 07:05:33 +00002602 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 +00002603
2604 if(do_escape_coding)
Josh Coalsonf1eff452002-07-31 07:05:33 +00002605 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 +00002606
2607 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2608#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002609 if(!
2610 set_partitioned_rice_with_precompute_(
2611 residual,
2612 abs_residual_partition_sums+sum,
2613 raw_bits_per_partition+sum,
2614 residual_samples,
2615 predictor_order,
2616 rice_parameter,
2617 rice_parameter_search_dist,
2618 (unsigned)partition_order,
2619 do_escape_coding,
2620 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2621 &residual_bits
2622 )
2623 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002624#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002625 if(!
2626 set_partitioned_rice_with_precompute_(
2627 abs_residual,
2628 abs_residual_partition_sums+sum,
2629 raw_bits_per_partition+sum,
2630 residual_samples,
2631 predictor_order,
2632 rice_parameter,
2633 rice_parameter_search_dist,
2634 (unsigned)partition_order,
2635 do_escape_coding,
2636 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2637 &residual_bits
2638 )
2639 )
Josh Coalson8395d022001-07-12 21:25:22 +00002640#endif
2641 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002642 FLAC__ASSERT(best_residual_bits != 0);
2643 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002644 }
2645 sum += 1u << partition_order;
2646 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2647 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002648 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002649 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002650 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00002651 }
2652 }
Josh Coalson8395d022001-07-12 21:25:22 +00002653 else {
2654 unsigned partition_order;
2655 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2656#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002657 if(!
2658 set_partitioned_rice_(
2659 abs_residual,
2660 residual,
2661 residual_samples,
2662 predictor_order,
2663 rice_parameter,
2664 rice_parameter_search_dist,
2665 partition_order,
2666 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2667 &residual_bits
2668 )
2669 )
Josh Coalson8395d022001-07-12 21:25:22 +00002670#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002671 if(!
2672 set_partitioned_rice_(
2673 abs_residual,
2674 residual_samples,
2675 predictor_order,
2676 rice_parameter,
2677 rice_parameter_search_dist,
2678 partition_order,
2679 &private_->partitioned_rice_contents_extra[!best_parameters_index],
2680 &residual_bits
2681 )
2682 )
Josh Coalsonafcd8772001-04-18 22:59:25 +00002683#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002684 {
2685 FLAC__ASSERT(best_residual_bits != 0);
2686 break;
2687 }
2688 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2689 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00002690 best_parameters_index = !best_parameters_index;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002691 best_partitioned_rice->order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00002692 }
2693 }
2694 }
2695
Josh Coalsona37ba462002-08-19 21:36:39 +00002696 /*
Josh Coalson20ac2c12002-08-30 05:47:14 +00002697 * We are allowed to de-const the pointer based on our special knowledge;
Josh Coalsona37ba462002-08-19 21:36:39 +00002698 * it is const to the outside world.
2699 */
2700 {
2701 FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2702 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2703 memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2704 memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2705 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002706
2707 return best_residual_bits;
2708}
2709
Josh Coalson6fe72f72002-08-20 04:01:59 +00002710void precompute_partition_info_sums_(
2711 const FLAC__uint32 abs_residual[],
2712 FLAC__uint64 abs_residual_partition_sums[],
2713 unsigned residual_samples,
2714 unsigned predictor_order,
2715 unsigned min_partition_order,
2716 unsigned max_partition_order
2717)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002718{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002719 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002720 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002721 const unsigned blocksize = residual_samples + predictor_order;
2722
Josh Coalsonaef013c2001-04-24 01:25:42 +00002723 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002724 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002725 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00002726 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002727 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002728 const unsigned partitions = 1u << partition_order;
2729 const unsigned default_partition_samples = blocksize >> partition_order;
2730
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002731 FLAC__ASSERT(default_partition_samples > predictor_order);
2732
2733 for(partition = residual_sample = 0; partition < partitions; partition++) {
2734 partition_samples = default_partition_samples;
2735 if(partition == 0)
2736 partition_samples -= predictor_order;
2737 abs_residual_partition_sum = 0;
2738 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2739 abs_r = abs_residual[residual_sample];
2740 abs_residual_partition_sum += abs_r;
2741 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002742 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002743 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002744 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002745 to_partition = partitions;
2746 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002747 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00002748
Josh Coalson8395d022001-07-12 21:25:22 +00002749 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00002750 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002751 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002752 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002753 const unsigned partitions = 1u << partition_order;
2754 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00002755 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00002756 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00002757 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00002758 from_partition++;
2759 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00002760 }
2761 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00002762}
Josh Coalson8395d022001-07-12 21:25:22 +00002763
Josh Coalson6fe72f72002-08-20 04:01:59 +00002764void precompute_partition_info_escapes_(
2765 const FLAC__int32 residual[],
2766 unsigned raw_bits_per_partition[],
2767 unsigned residual_samples,
2768 unsigned predictor_order,
2769 unsigned min_partition_order,
2770 unsigned max_partition_order
2771)
Josh Coalson8395d022001-07-12 21:25:22 +00002772{
2773 int partition_order;
2774 unsigned from_partition, to_partition = 0;
2775 const unsigned blocksize = residual_samples + predictor_order;
2776
2777 /* first do max_partition_order */
2778 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2779 FLAC__int32 r, residual_partition_min, residual_partition_max;
2780 unsigned silog2_min, silog2_max;
2781 unsigned partition, partition_sample, partition_samples, residual_sample;
2782 const unsigned partitions = 1u << partition_order;
2783 const unsigned default_partition_samples = blocksize >> partition_order;
2784
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002785 FLAC__ASSERT(default_partition_samples > predictor_order);
2786
2787 for(partition = residual_sample = 0; partition < partitions; partition++) {
2788 partition_samples = default_partition_samples;
2789 if(partition == 0)
2790 partition_samples -= predictor_order;
2791 residual_partition_min = residual_partition_max = 0;
2792 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2793 r = residual[residual_sample];
2794 if(r < residual_partition_min)
2795 residual_partition_min = r;
2796 else if(r > residual_partition_max)
2797 residual_partition_max = r;
2798 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00002799 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002800 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2801 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2802 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00002803 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002804 to_partition = partitions;
2805 break;
Josh Coalson8395d022001-07-12 21:25:22 +00002806 }
2807
2808 /* now merge partitions for lower orders */
2809 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2810 unsigned m;
2811 unsigned i;
2812 const unsigned partitions = 1u << partition_order;
2813 for(i = 0; i < partitions; i++) {
2814 m = raw_bits_per_partition[from_partition];
2815 from_partition++;
2816 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
2817 from_partition++;
2818 to_partition++;
2819 }
2820 }
2821}
Josh Coalson94e02cd2001-01-25 10:41:06 +00002822
Josh Coalson352e0f62001-03-20 22:55:50 +00002823#ifdef VARIABLE_RICE_BITS
2824#undef VARIABLE_RICE_BITS
2825#endif
Josh Coalson8395d022001-07-12 21:25:22 +00002826#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00002827#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00002828#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00002829
Josh Coalson8395d022001-07-12 21:25:22 +00002830#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00002831FLAC__bool set_partitioned_rice_(
2832 const FLAC__uint32 abs_residual[],
2833 const FLAC__int32 residual[],
2834 const unsigned residual_samples,
2835 const unsigned predictor_order,
2836 const unsigned suggested_rice_parameter,
2837 const unsigned rice_parameter_search_dist,
2838 const unsigned partition_order,
2839 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2840 unsigned *bits
2841)
Josh Coalson8395d022001-07-12 21:25:22 +00002842#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00002843FLAC__bool set_partitioned_rice_(
2844 const FLAC__uint32 abs_residual[],
2845 const unsigned residual_samples,
2846 const unsigned predictor_order,
2847 const unsigned suggested_rice_parameter,
2848 const unsigned rice_parameter_search_dist,
2849 const unsigned partition_order,
2850 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2851 unsigned *bits
2852)
Josh Coalson8395d022001-07-12 21:25:22 +00002853#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002854{
Josh Coalson034dfab2001-04-27 19:10:23 +00002855 unsigned rice_parameter, partition_bits;
2856#ifndef NO_RICE_SEARCH
2857 unsigned best_partition_bits;
2858 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2859#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00002860 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002861 unsigned *parameters;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002862
Josh Coalson1b689822001-05-31 20:11:02 +00002863 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00002864
Josh Coalsona37ba462002-08-19 21:36:39 +00002865 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2866 parameters = partitioned_rice_contents->parameters;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002867
Josh Coalson94e02cd2001-01-25 10:41:06 +00002868 if(partition_order == 0) {
2869 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00002870
Josh Coalson034dfab2001-04-27 19:10:23 +00002871#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00002872 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00002873 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00002874 min_rice_parameter = 0;
2875 else
Josh Coalson034dfab2001-04-27 19:10:23 +00002876 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2877 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00002878 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002879#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002880 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2881#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00002882 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00002883 }
2884 }
2885 else
2886 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2887
2888 best_partition_bits = 0xffffffff;
2889 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2890#endif
2891#ifdef VARIABLE_RICE_BITS
2892#ifdef FLAC__SYMMETRIC_RICE
2893 partition_bits = (2+rice_parameter) * residual_samples;
2894#else
2895 const unsigned rice_parameter_estimate = rice_parameter-1;
2896 partition_bits = (1+rice_parameter) * residual_samples;
2897#endif
2898#else
2899 partition_bits = 0;
2900#endif
2901 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2902 for(i = 0; i < residual_samples; i++) {
2903#ifdef VARIABLE_RICE_BITS
2904#ifdef FLAC__SYMMETRIC_RICE
2905 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2906#else
2907 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2908#endif
2909#else
2910 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2911#endif
2912 }
2913#ifndef NO_RICE_SEARCH
2914 if(partition_bits < best_partition_bits) {
2915 best_rice_parameter = rice_parameter;
2916 best_partition_bits = partition_bits;
2917 }
2918 }
2919#endif
2920 parameters[0] = best_rice_parameter;
2921 bits_ += best_partition_bits;
2922 }
2923 else {
2924 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002925 unsigned partition_samples;
2926 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00002927 const unsigned partitions = 1u << partition_order;
2928 for(partition = residual_sample = 0; partition < partitions; partition++) {
2929 partition_samples = (residual_samples+predictor_order) >> partition_order;
2930 if(partition == 0) {
2931 if(partition_samples <= predictor_order)
2932 return false;
2933 else
2934 partition_samples -= predictor_order;
2935 }
2936 mean = 0;
2937 save_residual_sample = residual_sample;
2938 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002939 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00002940 residual_sample = save_residual_sample;
2941#ifdef FLAC__SYMMETRIC_RICE
2942 mean += partition_samples >> 1; /* for rounding effect */
2943 mean /= partition_samples;
2944
2945 /* calc rice_parameter = floor(log2(mean)) */
2946 rice_parameter = 0;
2947 mean>>=1;
2948 while(mean) {
2949 rice_parameter++;
2950 mean >>= 1;
2951 }
2952#else
2953 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00002954 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00002955 ;
2956#endif
2957 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002958#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002959 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2960#endif
2961 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2962 }
2963
2964#ifndef NO_RICE_SEARCH
2965 if(rice_parameter_search_dist) {
2966 if(rice_parameter < rice_parameter_search_dist)
2967 min_rice_parameter = 0;
2968 else
2969 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2970 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2971 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00002972#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00002973 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2974#endif
2975 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2976 }
2977 }
2978 else
2979 min_rice_parameter = max_rice_parameter = rice_parameter;
2980
2981 best_partition_bits = 0xffffffff;
2982 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2983#endif
2984#ifdef VARIABLE_RICE_BITS
2985#ifdef FLAC__SYMMETRIC_RICE
2986 partition_bits = (2+rice_parameter) * partition_samples;
2987#else
2988 const unsigned rice_parameter_estimate = rice_parameter-1;
2989 partition_bits = (1+rice_parameter) * partition_samples;
2990#endif
2991#else
2992 partition_bits = 0;
2993#endif
2994 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2995 save_residual_sample = residual_sample;
2996 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
2997#ifdef VARIABLE_RICE_BITS
2998#ifdef FLAC__SYMMETRIC_RICE
2999 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
3000#else
3001 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3002#endif
3003#else
3004 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3005#endif
3006 }
3007#ifndef NO_RICE_SEARCH
3008 if(rice_parameter != max_rice_parameter)
3009 residual_sample = save_residual_sample;
3010 if(partition_bits < best_partition_bits) {
3011 best_rice_parameter = rice_parameter;
3012 best_partition_bits = partition_bits;
3013 }
3014 }
3015#endif
3016 parameters[partition] = best_rice_parameter;
3017 bits_ += best_partition_bits;
3018 }
3019 }
3020
3021 *bits = bits_;
3022 return true;
3023}
3024
3025#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalson6fe72f72002-08-20 04:01:59 +00003026FLAC__bool set_partitioned_rice_with_precompute_(
3027 const FLAC__int32 residual[],
3028 const FLAC__uint64 abs_residual_partition_sums[],
3029 const unsigned raw_bits_per_partition[],
3030 const unsigned residual_samples,
3031 const unsigned predictor_order,
3032 const unsigned suggested_rice_parameter,
3033 const unsigned rice_parameter_search_dist,
3034 const unsigned partition_order,
3035 const FLAC__bool search_for_escapes,
3036 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3037 unsigned *bits
3038)
Josh Coalson8395d022001-07-12 21:25:22 +00003039#else
Josh Coalson6fe72f72002-08-20 04:01:59 +00003040FLAC__bool set_partitioned_rice_with_precompute_(
3041 const FLAC__uint32 abs_residual[],
3042 const FLAC__uint64 abs_residual_partition_sums[],
3043 const unsigned raw_bits_per_partition[],
3044 const unsigned residual_samples,
3045 const unsigned predictor_order,
3046 const unsigned suggested_rice_parameter,
3047 const unsigned rice_parameter_search_dist,
3048 const unsigned partition_order,
3049 const FLAC__bool search_for_escapes,
3050 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3051 unsigned *bits
3052)
Josh Coalson8395d022001-07-12 21:25:22 +00003053#endif
3054{
3055 unsigned rice_parameter, partition_bits;
3056#ifndef NO_RICE_SEARCH
3057 unsigned best_partition_bits;
3058 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3059#endif
3060 unsigned flat_bits;
3061 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003062 unsigned *parameters, *raw_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003063
3064 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3065
Josh Coalsona37ba462002-08-19 21:36:39 +00003066 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3067 parameters = partitioned_rice_contents->parameters;
3068 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003069
Josh Coalson8395d022001-07-12 21:25:22 +00003070 if(partition_order == 0) {
3071 unsigned i;
3072
3073#ifndef NO_RICE_SEARCH
3074 if(rice_parameter_search_dist) {
3075 if(suggested_rice_parameter < rice_parameter_search_dist)
3076 min_rice_parameter = 0;
3077 else
3078 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3079 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3080 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003081#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003082 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3083#endif
3084 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3085 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003086 }
3087 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003088 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00003089
Josh Coalson034dfab2001-04-27 19:10:23 +00003090 best_partition_bits = 0xffffffff;
3091 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3092#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003093#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00003094#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00003095 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00003096#else
Josh Coalson352e0f62001-03-20 22:55:50 +00003097 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00003098 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00003099#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003100#else
3101 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003102#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00003103 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00003104 for(i = 0; i < residual_samples; i++) {
3105#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00003106#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00003107 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003108#else
Josh Coalson2051dd42001-04-12 22:22:34 +00003109 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00003110#endif
3111#else
Josh Coalson2051dd42001-04-12 22:22:34 +00003112 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 +00003113#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00003114 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003115#ifndef NO_RICE_SEARCH
3116 if(partition_bits < best_partition_bits) {
3117 best_rice_parameter = rice_parameter;
3118 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00003119 }
3120 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003121#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003122 if(search_for_escapes) {
3123 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;
3124 if(flat_bits <= best_partition_bits) {
3125 raw_bits[0] = raw_bits_per_partition[0];
3126 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3127 best_partition_bits = flat_bits;
3128 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003129 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003130 parameters[0] = best_rice_parameter;
3131 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003132 }
3133 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00003134 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003135 unsigned partition_samples;
3136 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003137 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00003138 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003139 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00003140 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003141 if(partition_samples <= predictor_order)
3142 return false;
3143 else
3144 partition_samples -= predictor_order;
3145 }
Josh Coalson05d20792001-06-29 23:12:26 +00003146 mean = abs_residual_partition_sums[partition];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00003147#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson05d20792001-06-29 23:12:26 +00003148 mean += partition_samples >> 1; /* for rounding effect */
3149 mean /= partition_samples;
3150
Josh Coalson034dfab2001-04-27 19:10:23 +00003151 /* calc rice_parameter = floor(log2(mean)) */
3152 rice_parameter = 0;
3153 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00003154 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003155 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00003156 mean >>= 1;
3157 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00003158#else
Josh Coalson05d20792001-06-29 23:12:26 +00003159 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003160 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00003161 ;
Josh Coalsonb9433f92001-03-17 01:07:00 +00003162#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003163 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003164#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003165 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3166#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003167 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003168 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003169
Josh Coalson034dfab2001-04-27 19:10:23 +00003170#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00003171 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00003172 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00003173 min_rice_parameter = 0;
3174 else
Josh Coalson034dfab2001-04-27 19:10:23 +00003175 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3176 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00003177 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalson31209492001-07-18 23:43:01 +00003178#ifdef DEBUG_VERBOSE
Josh Coalson8395d022001-07-12 21:25:22 +00003179 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3180#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00003181 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00003182 }
Josh Coalson60f77d72001-04-25 02:16:36 +00003183 }
3184 else
3185 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00003186
Josh Coalson034dfab2001-04-27 19:10:23 +00003187 best_partition_bits = 0xffffffff;
3188 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3189#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00003190#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00003191#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00003192 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00003193#else
Josh Coalson034dfab2001-04-27 19:10:23 +00003194 const unsigned rice_parameter_estimate = rice_parameter-1;
3195 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00003196#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003197#else
3198 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003199#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003200 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00003201 save_residual_sample = residual_sample;
3202 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00003203#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00003204#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson4dacd192001-06-06 21:11:44 +00003205 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003206#else
Josh Coalson4dacd192001-06-06 21:11:44 +00003207 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00003208#endif
3209#else
Josh Coalson4dacd192001-06-06 21:11:44 +00003210 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 +00003211#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00003212 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003213#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00003214 if(rice_parameter != max_rice_parameter)
3215 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00003216 if(partition_bits < best_partition_bits) {
3217 best_rice_parameter = rice_parameter;
3218 best_partition_bits = partition_bits;
3219 }
Josh Coalson2051dd42001-04-12 22:22:34 +00003220 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003221#endif
Josh Coalson8395d022001-07-12 21:25:22 +00003222 if(search_for_escapes) {
3223 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;
3224 if(flat_bits <= best_partition_bits) {
3225 raw_bits[partition] = raw_bits_per_partition[partition];
3226 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3227 best_partition_bits = flat_bits;
3228 }
Josh Coalson2051dd42001-04-12 22:22:34 +00003229 }
Josh Coalson034dfab2001-04-27 19:10:23 +00003230 parameters[partition] = best_rice_parameter;
3231 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003232 }
3233 }
3234
3235 *bits = bits_;
3236 return true;
3237}
Josh Coalson859bc542001-03-27 22:22:27 +00003238
Josh Coalsonf1eff452002-07-31 07:05:33 +00003239unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00003240{
3241 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00003242 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00003243
3244 for(i = 0; i < samples && !(x&1); i++)
3245 x |= signal[i];
3246
3247 if(x == 0) {
3248 shift = 0;
3249 }
3250 else {
3251 for(shift = 0; !(x&1); shift++)
3252 x >>= 1;
3253 }
3254
3255 if(shift > 0) {
3256 for(i = 0; i < samples; i++)
3257 signal[i] >>= shift;
3258 }
3259
3260 return shift;
3261}
Josh Coalsond86e03b2002-08-03 21:56:15 +00003262
3263void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3264{
3265 unsigned channel;
3266
3267 for(channel = 0; channel < channels; channel++)
3268 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
3269
3270 fifo->tail += wide_samples;
3271
3272 FLAC__ASSERT(fifo->tail <= fifo->size);
3273}
3274
3275void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
3276{
3277 unsigned channel;
3278 unsigned sample, wide_sample;
3279 unsigned tail = fifo->tail;
3280
3281 sample = input_offset * channels;
3282 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
3283 for(channel = 0; channel < channels; channel++)
3284 fifo->data[channel][tail] = input[sample++];
3285 tail++;
3286 }
3287 fifo->tail = tail;
3288
3289 FLAC__ASSERT(fifo->tail <= fifo->size);
3290}
3291
3292FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3293{
3294 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3295 const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
3296 (void)decoder;
3297
3298 if(encoder->private_->verify.needs_magic_hack) {
3299 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
3300 *bytes = FLAC__STREAM_SYNC_LENGTH;
3301 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
3302 encoder->private_->verify.needs_magic_hack = false;
3303 }
3304 else {
3305 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00003306 /*
3307 * If we get here, a FIFO underflow has occurred,
3308 * which means there is a bug somewhere.
3309 */
3310 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003311 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3312 }
3313 else if(encoded_bytes < *bytes)
3314 *bytes = encoded_bytes;
3315 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
3316 encoder->private_->verify.output.data += *bytes;
3317 encoder->private_->verify.output.bytes -= *bytes;
3318 }
3319
3320 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3321}
3322
3323FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
3324{
3325 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3326 unsigned channel;
3327 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3328 const unsigned blocksize = frame->header.blocksize;
3329 const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3330
3331 for(channel = 0; channel < channels; channel++) {
3332 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3333 unsigned i, sample = 0;
3334 FLAC__int32 expect = 0, got = 0;
3335
3336 for(i = 0; i < blocksize; i++) {
3337 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
3338 sample = i;
3339 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
3340 got = (FLAC__int32)buffer[channel][i];
3341 break;
3342 }
3343 }
3344 FLAC__ASSERT(i < blocksize);
3345 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3346 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Josh Coalson5f39e9f2002-08-21 05:27:01 +00003347 encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00003348 encoder->private_->verify.error_stats.channel = channel;
3349 encoder->private_->verify.error_stats.sample = sample;
3350 encoder->private_->verify.error_stats.expected = expect;
3351 encoder->private_->verify.error_stats.got = got;
3352 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
3353 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3354 }
3355 }
3356 /* dequeue the frame from the fifo */
3357 for(channel = 0; channel < channels; channel++) {
3358 memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3359 }
3360 encoder->private_->verify.input_fifo.tail -= blocksize;
3361 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3362}
3363
3364void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
3365{
3366 (void)decoder, (void)metadata, (void)client_data;
3367}
3368
3369void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
3370{
3371 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3372 (void)decoder, (void)status;
3373 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3374}