~ubuntu-branches/ubuntu/karmic/flac/karmic

« back to all changes in this revision

Viewing changes to src/libFLAC/stream_encoder.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc 'HE' Brockschmidt
  • Date: 2008-03-16 18:02:56 UTC
  • mfrom: (1.1.5 upstream) (10 gutsy)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20080316180256-qhf3wk704rp165pm
* Non-maintainer upload.
* Fix gcc-4.3 FTBFS, patch by KiBi (Closes: #455304)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* libFLAC - Free Lossless Audio Codec library
2
 
 * Copyright (C) 2000,2001,2002,2003,2004,2005  Josh Coalson
 
2
 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
3
3
 *
4
4
 * Redistribution and use in source and binary forms, with or without
5
5
 * modification, are permitted provided that the following conditions
29
29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
 */
31
31
 
 
32
#if HAVE_CONFIG_H
 
33
#  include <config.h>
 
34
#endif
 
35
 
 
36
#if defined _MSC_VER || defined __MINGW32__
 
37
#include <io.h> /* for _setmode() */
 
38
#include <fcntl.h> /* for _O_BINARY */
 
39
#endif
 
40
#if defined __CYGWIN__ || defined __EMX__
 
41
#include <io.h> /* for setmode(), O_BINARY */
 
42
#include <fcntl.h> /* for _O_BINARY */
 
43
#endif
32
44
#include <limits.h>
33
45
#include <stdio.h>
34
46
#include <stdlib.h> /* for malloc() */
35
47
#include <string.h> /* for memcpy() */
 
48
#include <sys/types.h> /* for off_t */
 
49
#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
 
50
#if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */
 
51
#define fseeko fseek
 
52
#define ftello ftell
 
53
#endif
 
54
#endif
36
55
#include "FLAC/assert.h"
37
56
#include "FLAC/stream_decoder.h"
 
57
#include "share/alloc.h"
38
58
#include "protected/stream_encoder.h"
39
 
#include "private/bitbuffer.h"
 
59
#include "private/bitwriter.h"
40
60
#include "private/bitmath.h"
41
61
#include "private/crc.h"
42
62
#include "private/cpu.h"
45
65
#include "private/lpc.h"
46
66
#include "private/md5.h"
47
67
#include "private/memory.h"
 
68
#if FLAC__HAS_OGG
 
69
#include "private/ogg_helper.h"
 
70
#include "private/ogg_mapping.h"
 
71
#endif
48
72
#include "private/stream_encoder_framing.h"
 
73
#include "private/window.h"
49
74
 
50
 
#ifdef HAVE_CONFIG_H
51
 
#include <config.h>
 
75
#ifndef FLaC__INLINE
 
76
#define FLaC__INLINE
52
77
#endif
53
78
 
54
79
#ifdef min
61
86
#endif
62
87
#define max(x,y) ((x)>(y)?(x):(y))
63
88
 
 
89
/* Exact Rice codeword length calculation is off by default.  The simple
 
90
 * (and fast) estimation (of how many bits a residual value will be
 
91
 * encoded with) in this encoder is very good, almost always yielding
 
92
 * compression within 0.1% of exact calculation.
 
93
 */
 
94
#undef EXACT_RICE_BITS_CALCULATION
 
95
/* Rice parameter searching is off by default.  The simple (and fast)
 
96
 * parameter estimation in this encoder is very good, almost always
 
97
 * yielding compression within 0.1% of the optimal parameters.
 
98
 */
 
99
#undef ENABLE_RICE_PARAMETER_SEARCH 
 
100
 
 
101
 
64
102
typedef struct {
65
103
        FLAC__int32 *data[FLAC__MAX_CHANNELS];
66
104
        unsigned size; /* of each data[] in samples */
79
117
        ENCODER_IN_AUDIO = 2
80
118
} EncoderStateHint;
81
119
 
 
120
static struct CompressionLevels {
 
121
        FLAC__bool do_mid_side_stereo;
 
122
        FLAC__bool loose_mid_side_stereo;
 
123
        unsigned max_lpc_order;
 
124
        unsigned qlp_coeff_precision;
 
125
        FLAC__bool do_qlp_coeff_prec_search;
 
126
        FLAC__bool do_escape_coding;
 
127
        FLAC__bool do_exhaustive_model_search;
 
128
        unsigned min_residual_partition_order;
 
129
        unsigned max_residual_partition_order;
 
130
        unsigned rice_parameter_search_dist;
 
131
} compression_levels_[] = {
 
132
        { false, false,  0, 0, false, false, false, 0, 3, 0 },
 
133
        { true , true ,  0, 0, false, false, false, 0, 3, 0 },
 
134
        { true , false,  0, 0, false, false, false, 0, 3, 0 },
 
135
        { false, false,  6, 0, false, false, false, 0, 4, 0 },
 
136
        { true , true ,  8, 0, false, false, false, 0, 4, 0 },
 
137
        { true , false,  8, 0, false, false, false, 0, 5, 0 },
 
138
        { true , false,  8, 0, false, false, false, 0, 6, 0 },
 
139
        { true , false,  8, 0, false, false, true , 0, 6, 0 },
 
140
        { true , false, 12, 0, false, false, true , 0, 6, 0 }
 
141
};
 
142
 
 
143
 
82
144
/***********************************************************************
83
145
 *
84
146
 * Private class method prototypes
87
149
 
88
150
static void set_defaults_(FLAC__StreamEncoder *encoder);
89
151
static void free_(FLAC__StreamEncoder *encoder);
90
 
static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
91
 
static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
92
 
static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
93
 
static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
 
152
static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize);
 
153
static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block);
 
154
static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block);
 
155
static void update_metadata_(const FLAC__StreamEncoder *encoder);
 
156
#if FLAC__HAS_OGG
 
157
static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
 
158
#endif
 
159
static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
 
160
static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
94
161
 
95
162
static FLAC__bool process_subframe_(
96
163
        FLAC__StreamEncoder *encoder,
97
164
        unsigned min_partition_order,
98
165
        unsigned max_partition_order,
99
 
        FLAC__bool precompute_partition_sums,
100
166
        const FLAC__FrameHeader *frame_header,
101
167
        unsigned subframe_bps,
102
168
        const FLAC__int32 integer_signal[],
103
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
104
 
        const FLAC__real real_signal[],
105
 
#endif
106
169
        FLAC__Subframe *subframe[2],
107
170
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
108
171
        FLAC__int32 *residual[2],
112
175
 
113
176
static FLAC__bool add_subframe_(
114
177
        FLAC__StreamEncoder *encoder,
115
 
        const FLAC__FrameHeader *frame_header,
 
178
        unsigned blocksize,
116
179
        unsigned subframe_bps,
117
180
        const FLAC__Subframe *subframe,
118
 
        FLAC__BitBuffer *frame
 
181
        FLAC__BitWriter *frame
119
182
);
120
183
 
121
184
static unsigned evaluate_constant_subframe_(
 
185
        FLAC__StreamEncoder *encoder,
122
186
        const FLAC__int32 signal,
 
187
        unsigned blocksize,
123
188
        unsigned subframe_bps,
124
189
        FLAC__Subframe *subframe
125
190
);
128
193
        FLAC__StreamEncoder *encoder,
129
194
        const FLAC__int32 signal[],
130
195
        FLAC__int32 residual[],
131
 
        FLAC__uint32 abs_residual[],
132
196
        FLAC__uint64 abs_residual_partition_sums[],
133
197
        unsigned raw_bits_per_partition[],
134
198
        unsigned blocksize,
135
199
        unsigned subframe_bps,
136
200
        unsigned order,
137
201
        unsigned rice_parameter,
 
202
        unsigned rice_parameter_limit,
138
203
        unsigned min_partition_order,
139
204
        unsigned max_partition_order,
140
 
        FLAC__bool precompute_partition_sums,
141
205
        FLAC__bool do_escape_coding,
142
206
        unsigned rice_parameter_search_dist,
143
207
        FLAC__Subframe *subframe,
149
213
        FLAC__StreamEncoder *encoder,
150
214
        const FLAC__int32 signal[],
151
215
        FLAC__int32 residual[],
152
 
        FLAC__uint32 abs_residual[],
153
216
        FLAC__uint64 abs_residual_partition_sums[],
154
217
        unsigned raw_bits_per_partition[],
155
218
        const FLAC__real lp_coeff[],
158
221
        unsigned order,
159
222
        unsigned qlp_coeff_precision,
160
223
        unsigned rice_parameter,
 
224
        unsigned rice_parameter_limit,
161
225
        unsigned min_partition_order,
162
226
        unsigned max_partition_order,
163
 
        FLAC__bool precompute_partition_sums,
164
227
        FLAC__bool do_escape_coding,
165
228
        unsigned rice_parameter_search_dist,
166
229
        FLAC__Subframe *subframe,
169
232
#endif
170
233
 
171
234
static unsigned evaluate_verbatim_subframe_(
 
235
        FLAC__StreamEncoder *encoder, 
172
236
        const FLAC__int32 signal[],
173
237
        unsigned blocksize,
174
238
        unsigned subframe_bps,
178
242
static unsigned find_best_partition_order_(
179
243
        struct FLAC__StreamEncoderPrivate *private_,
180
244
        const FLAC__int32 residual[],
181
 
        FLAC__uint32 abs_residual[],
182
245
        FLAC__uint64 abs_residual_partition_sums[],
183
246
        unsigned raw_bits_per_partition[],
184
247
        unsigned residual_samples,
185
248
        unsigned predictor_order,
186
249
        unsigned rice_parameter,
 
250
        unsigned rice_parameter_limit,
187
251
        unsigned min_partition_order,
188
252
        unsigned max_partition_order,
189
 
        FLAC__bool precompute_partition_sums,
 
253
        unsigned bps,
190
254
        FLAC__bool do_escape_coding,
191
255
        unsigned rice_parameter_search_dist,
192
 
        FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
 
256
        FLAC__EntropyCodingMethod *best_ecm
193
257
);
194
258
 
195
259
static void precompute_partition_info_sums_(
196
 
        const FLAC__uint32 abs_residual[],
 
260
        const FLAC__int32 residual[],
197
261
        FLAC__uint64 abs_residual_partition_sums[],
198
262
        unsigned residual_samples,
199
263
        unsigned predictor_order,
200
264
        unsigned min_partition_order,
201
 
        unsigned max_partition_order
 
265
        unsigned max_partition_order,
 
266
        unsigned bps
202
267
);
203
268
 
204
269
static void precompute_partition_info_escapes_(
210
275
        unsigned max_partition_order
211
276
);
212
277
 
213
 
#ifdef DONT_ESTIMATE_RICE_BITS
214
 
static 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
 
 
226
 
static 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
 
);
239
 
#else
240
 
static 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
 
 
251
 
static 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
 
);
 
278
static FLAC__bool set_partitioned_rice_(
 
279
#ifdef EXACT_RICE_BITS_CALCULATION
 
280
        const FLAC__int32 residual[],
264
281
#endif
 
282
        const FLAC__uint64 abs_residual_partition_sums[],
 
283
        const unsigned raw_bits_per_partition[],
 
284
        const unsigned residual_samples,
 
285
        const unsigned predictor_order,
 
286
        const unsigned suggested_rice_parameter,
 
287
        const unsigned rice_parameter_limit,
 
288
        const unsigned rice_parameter_search_dist,
 
289
        const unsigned partition_order,
 
290
        const FLAC__bool search_for_escapes,
 
291
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
 
292
        unsigned *bits
 
293
);
265
294
 
266
295
static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
267
296
 
282
311
        unsigned wide_samples
283
312
);
284
313
 
285
 
static FLAC__StreamDecoderReadStatus verify_read_callback_(
286
 
        const FLAC__StreamDecoder *decoder,
287
 
        FLAC__byte buffer[],
288
 
        unsigned *bytes,
289
 
        void *client_data
290
 
);
291
 
 
292
 
static 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
 
 
299
 
static void verify_metadata_callback_(
300
 
        const FLAC__StreamDecoder *decoder,
301
 
        const FLAC__StreamMetadata *metadata,
302
 
        void *client_data
303
 
);
304
 
 
305
 
static void verify_error_callback_(
306
 
        const FLAC__StreamDecoder *decoder,
307
 
        FLAC__StreamDecoderErrorStatus status,
308
 
        void *client_data
309
 
);
 
314
static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
 
315
static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
 
316
static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
 
317
static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
 
318
 
 
319
static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
 
320
static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
 
321
static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
 
322
static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
 
323
static FILE *get_binary_stdout_(void);
310
324
 
311
325
 
312
326
/***********************************************************************
320
334
        FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
321
335
        FLAC__int32 *integer_signal_mid_side[2];          /* the integer version of the mid-side input signal (stereo only) */
322
336
#ifndef FLAC__INTEGER_ONLY_LIBRARY
323
 
        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) */
 
337
        FLAC__real *real_signal[FLAC__MAX_CHANNELS];      /* (@@@ currently unused) the floating-point version of the input signal */
 
338
        FLAC__real *real_signal_mid_side[2];              /* (@@@ currently unused) the floating-point version of the mid-side input signal (stereo only) */
 
339
        FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
 
340
        FLAC__real *windowed_signal;                      /* the integer_signal[] * current window[] */
325
341
#endif
326
342
        unsigned subframe_bps[FLAC__MAX_CHANNELS];        /* the effective bits per sample of the input signal (stream bps - wasted bits) */
327
343
        unsigned subframe_bps_mid_side[2];                /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
335
351
        FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
336
352
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
337
353
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
338
 
        unsigned best_subframe[FLAC__MAX_CHANNELS];       /* index into the above workspaces */
 
354
        unsigned best_subframe[FLAC__MAX_CHANNELS];       /* index (0 or 1) into 2nd dimension of the above workspaces */
339
355
        unsigned best_subframe_mid_side[2];
340
356
        unsigned best_subframe_bits[FLAC__MAX_CHANNELS];  /* size in bits of the best subframe for each channel */
341
357
        unsigned best_subframe_bits_mid_side[2];
342
 
        FLAC__uint32 *abs_residual;                       /* workspace where abs(candidate residual) is stored */
343
358
        FLAC__uint64 *abs_residual_partition_sums;        /* workspace where the sum of abs(candidate residual) for each partition is stored */
344
359
        unsigned *raw_bits_per_partition;                 /* workspace where the sum of silog2(candidate residual) for each partition is stored */
345
 
        FLAC__BitBuffer *frame;                           /* the current frame being worked on */
 
360
        FLAC__BitWriter *frame;                           /* the current frame being worked on */
346
361
        unsigned loose_mid_side_stereo_frames;            /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
347
362
        unsigned loose_mid_side_stereo_frame_count;       /* number of frames using the current channel assignment */
348
363
        FLAC__ChannelAssignment last_channel_assignment;
349
 
        FLAC__StreamMetadata metadata;
 
364
        FLAC__StreamMetadata streaminfo;                  /* scratchpad for STREAMINFO as it is built */
 
365
        FLAC__StreamMetadata_SeekTable *seek_table;       /* pointer into encoder->protected_->metadata_ where the seek table is */
350
366
        unsigned current_sample_number;
351
367
        unsigned current_frame_number;
352
 
        struct FLAC__MD5Context md5context;
 
368
        FLAC__MD5Context md5context;
353
369
        FLAC__CPUInfo cpuinfo;
354
370
#ifndef FLAC__INTEGER_ONLY_LIBRARY
355
371
        unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
365
381
        FLAC__bool use_wide_by_block;          /* use slow 64-bit versions of some functions because of the block size */
366
382
        FLAC__bool use_wide_by_partition;      /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
367
383
        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 */
369
384
        FLAC__bool disable_constant_subframes;
370
385
        FLAC__bool disable_fixed_subframes;
371
386
        FLAC__bool disable_verbatim_subframes;
 
387
#if FLAC__HAS_OGG
 
388
        FLAC__bool is_ogg;
 
389
#endif
 
390
        FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
 
391
        FLAC__StreamEncoderSeekCallback seek_callback;
 
392
        FLAC__StreamEncoderTellCallback tell_callback;
372
393
        FLAC__StreamEncoderWriteCallback write_callback;
373
394
        FLAC__StreamEncoderMetadataCallback metadata_callback;
 
395
        FLAC__StreamEncoderProgressCallback progress_callback;
374
396
        void *client_data;
 
397
        unsigned first_seekpoint_to_check;
 
398
        FILE *file;                            /* only used when encoding to a file */
 
399
        FLAC__uint64 bytes_written;
 
400
        FLAC__uint64 samples_written;
 
401
        unsigned frames_written;
 
402
        unsigned total_frames_estimate;
375
403
        /* unaligned (original) pointers to allocated data */
376
404
        FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
377
405
        FLAC__int32 *integer_signal_mid_side_unaligned[2];
378
406
#ifndef FLAC__INTEGER_ONLY_LIBRARY
379
 
        FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
380
 
        FLAC__real *real_signal_mid_side_unaligned[2];
 
407
        FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) */
 
408
        FLAC__real *real_signal_mid_side_unaligned[2]; /* (@@@ currently unused) */
 
409
        FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
 
410
        FLAC__real *windowed_signal_unaligned;
381
411
#endif
382
412
        FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
383
413
        FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
384
 
        FLAC__uint32 *abs_residual_unaligned;
385
414
        FLAC__uint64 *abs_residual_partition_sums_unaligned;
386
415
        unsigned *raw_bits_per_partition_unaligned;
387
416
        /*
421
450
 
422
451
FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
423
452
        "FLAC__STREAM_ENCODER_OK",
 
453
        "FLAC__STREAM_ENCODER_UNINITIALIZED",
 
454
        "FLAC__STREAM_ENCODER_OGG_ERROR",
424
455
        "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
425
456
        "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
426
 
        "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
427
 
        "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",
431
 
        "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
432
 
        "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",
 
457
        "FLAC__STREAM_ENCODER_CLIENT_ERROR",
 
458
        "FLAC__STREAM_ENCODER_IO_ERROR",
438
459
        "FLAC__STREAM_ENCODER_FRAMING_ERROR",
439
 
        "FLAC__STREAM_ENCODER_INVALID_METADATA",
440
 
        "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"
 
460
        "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
 
461
};
 
462
 
 
463
FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
 
464
        "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
 
465
        "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
 
466
        "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
 
467
        "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
 
468
        "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
 
469
        "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
 
470
        "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
 
471
        "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
 
472
        "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
 
473
        "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
 
474
        "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
 
475
        "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
 
476
        "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
 
477
        "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
 
478
};
 
479
 
 
480
FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
 
481
        "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
 
482
        "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
 
483
        "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
 
484
        "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
445
485
};
446
486
 
447
487
FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
449
489
        "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
450
490
};
451
491
 
 
492
FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
 
493
        "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
 
494
        "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
 
495
        "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
 
496
};
 
497
 
 
498
FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
 
499
        "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
 
500
        "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
 
501
        "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
 
502
};
 
503
 
 
504
/* Number of samples that will be overread to watch for end of stream.  By
 
505
 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
 
506
 * always try to read blocksize+1 samples before encoding a block, so that
 
507
 * even if the stream has a total sample count that is an integral multiple
 
508
 * of the blocksize, we will still notice when we are encoding the last
 
509
 * block.  This is needed, for example, to correctly set the end-of-stream
 
510
 * marker in Ogg FLAC.
 
511
 *
 
512
 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
 
513
 * not really any reason to change it.
 
514
 */
 
515
static const unsigned OVERREAD_ = 1;
 
516
 
452
517
/***********************************************************************
453
518
 *
454
519
 * Class constructor/destructor
455
520
 *
456
521
 */
457
 
FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
 
522
FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
458
523
{
459
524
        FLAC__StreamEncoder *encoder;
460
525
        unsigned i;
479
544
                return 0;
480
545
        }
481
546
 
482
 
        encoder->private_->frame = FLAC__bitbuffer_new();
 
547
        encoder->private_->frame = FLAC__bitwriter_new();
483
548
        if(encoder->private_->frame == 0) {
484
549
                free(encoder->private_);
485
550
                free(encoder->protected_);
487
552
                return 0;
488
553
        }
489
554
 
 
555
        encoder->private_->file = 0;
 
556
 
490
557
        set_defaults_(encoder);
491
558
 
492
559
        encoder->private_->is_being_deleted = false;
535
602
 
536
603
        encoder->private_->is_being_deleted = true;
537
604
 
538
 
        FLAC__stream_encoder_finish(encoder);
 
605
        (void)FLAC__stream_encoder_finish(encoder);
539
606
 
540
607
        if(0 != encoder->private_->verify.decoder)
541
608
                FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
551
618
        for(i = 0; i < 2; i++)
552
619
                FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
553
620
 
554
 
        FLAC__bitbuffer_delete(encoder->private_->frame);
 
621
        FLAC__bitwriter_delete(encoder->private_->frame);
555
622
        free(encoder->private_);
556
623
        free(encoder->protected_);
557
624
        free(encoder);
563
630
 *
564
631
 ***********************************************************************/
565
632
 
566
 
FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
 
633
static FLAC__StreamEncoderInitStatus init_stream_internal_(
 
634
        FLAC__StreamEncoder *encoder,
 
635
        FLAC__StreamEncoderReadCallback read_callback,
 
636
        FLAC__StreamEncoderWriteCallback write_callback,
 
637
        FLAC__StreamEncoderSeekCallback seek_callback,
 
638
        FLAC__StreamEncoderTellCallback tell_callback,
 
639
        FLAC__StreamEncoderMetadataCallback metadata_callback,
 
640
        void *client_data,
 
641
        FLAC__bool is_ogg
 
642
)
567
643
{
568
644
        unsigned i;
569
 
        FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
 
645
        FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
570
646
 
571
647
        FLAC__ASSERT(0 != encoder);
572
648
 
573
649
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
574
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
575
 
 
576
 
        encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
577
 
 
578
 
        if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
579
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
 
650
                return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
 
651
 
 
652
#if !FLAC__HAS_OGG
 
653
        if(is_ogg)
 
654
                return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
 
655
#endif
 
656
 
 
657
        if(0 == write_callback || (seek_callback && 0 == tell_callback))
 
658
                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
580
659
 
581
660
        if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
582
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
583
 
 
584
 
        if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
585
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
586
 
 
587
 
        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;
 
661
                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
 
662
 
 
663
        if(encoder->protected_->channels != 2) {
 
664
                encoder->protected_->do_mid_side_stereo = false;
 
665
                encoder->protected_->loose_mid_side_stereo = false;
 
666
        }
 
667
        else if(!encoder->protected_->do_mid_side_stereo)
 
668
                encoder->protected_->loose_mid_side_stereo = false;
589
669
 
590
670
        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 */
 
671
                encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
592
672
 
593
673
        if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
594
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
 
674
                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
595
675
 
596
676
        if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
597
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
 
677
                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
 
678
 
 
679
        if(encoder->protected_->blocksize == 0) {
 
680
                if(encoder->protected_->max_lpc_order == 0)
 
681
                        encoder->protected_->blocksize = 1152;
 
682
                else
 
683
                        encoder->protected_->blocksize = 4096;
 
684
        }
598
685
 
599
686
        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;
 
687
                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
601
688
 
602
689
        if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
603
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
 
690
                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
604
691
 
605
692
        if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
606
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
 
693
                return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
607
694
 
608
695
        if(encoder->protected_->qlp_coeff_precision == 0) {
609
696
                if(encoder->protected_->bits_per_sample < 16) {
638
725
                FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
639
726
        }
640
727
        else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
641
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
 
728
                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
642
729
 
643
730
        if(encoder->protected_->streamable_subset) {
644
731
                if(
655
742
                        encoder->protected_->blocksize != 8192 &&
656
743
                        encoder->protected_->blocksize != 16384
657
744
                )
658
 
                        return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
659
 
                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;
 
745
                        return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
 
746
                if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate))
 
747
                        return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
670
748
                if(
671
749
                        encoder->protected_->bits_per_sample != 8 &&
672
750
                        encoder->protected_->bits_per_sample != 12 &&
674
752
                        encoder->protected_->bits_per_sample != 20 &&
675
753
                        encoder->protected_->bits_per_sample != 24
676
754
                )
677
 
                        return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
 
755
                        return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
678
756
                if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
679
 
                        return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
 
757
                        return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
 
758
                if(
 
759
                        encoder->protected_->sample_rate <= 48000 &&
 
760
                        (
 
761
                                encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
 
762
                                encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
 
763
                        )
 
764
                ) {
 
765
                        return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
 
766
                }
680
767
        }
681
768
 
682
769
        if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
684
771
        if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
685
772
                encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
686
773
 
 
774
#if FLAC__HAS_OGG
 
775
        /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
 
776
        if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
 
777
                unsigned i;
 
778
                for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
 
779
                        if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
 
780
                                FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
 
781
                                for( ; i > 0; i--)
 
782
                                        encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
 
783
                                encoder->protected_->metadata[0] = vc;
 
784
                                break;
 
785
                        }
 
786
                }
 
787
        }
 
788
#endif
 
789
        /* keep track of any SEEKTABLE block */
 
790
        if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
 
791
                unsigned i;
 
792
                for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
 
793
                        if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
 
794
                                encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
 
795
                                break; /* take only the first one */
 
796
                        }
 
797
                }
 
798
        }
 
799
 
687
800
        /* validate metadata */
688
801
        if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
689
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
 
802
                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
690
803
        metadata_has_seektable = false;
691
804
        metadata_has_vorbis_comment = false;
 
805
        metadata_picture_has_type1 = false;
 
806
        metadata_picture_has_type2 = false;
692
807
        for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
693
 
                if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
694
 
                        return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
695
 
                else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
 
808
                const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
 
809
                if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
 
810
                        return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
 
811
                else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
696
812
                        if(metadata_has_seektable) /* only one is allowed */
697
 
                                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
 
813
                                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
698
814
                        metadata_has_seektable = true;
699
 
                        if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
700
 
                                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
 
815
                        if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
 
816
                                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
701
817
                }
702
 
                else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
 
818
                else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
703
819
                        if(metadata_has_vorbis_comment) /* only one is allowed */
704
 
                                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
 
820
                                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
705
821
                        metadata_has_vorbis_comment = true;
706
822
                }
707
 
                else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
708
 
                        if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
709
 
                                return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
 
823
                else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
 
824
                        if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
 
825
                                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
 
826
                }
 
827
                else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
 
828
                        if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
 
829
                                return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
 
830
                        if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
 
831
                                if(metadata_picture_has_type1) /* there should only be 1 per stream */
 
832
                                        return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
 
833
                                metadata_picture_has_type1 = true;
 
834
                                /* standard icon must be 32x32 pixel PNG */
 
835
                                if(
 
836
                                        m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD && 
 
837
                                        (
 
838
                                                (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
 
839
                                                m->data.picture.width != 32 ||
 
840
                                                m->data.picture.height != 32
 
841
                                        )
 
842
                                )
 
843
                                        return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
 
844
                        }
 
845
                        else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
 
846
                                if(metadata_picture_has_type2) /* there should only be 1 per stream */
 
847
                                        return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
 
848
                                metadata_picture_has_type2 = true;
 
849
                        }
710
850
                }
711
851
        }
712
852
 
723
863
                encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
724
864
#endif
725
865
        }
 
866
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
867
        for(i = 0; i < encoder->protected_->num_apodizations; i++)
 
868
                encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
 
869
        encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
 
870
#endif
726
871
        for(i = 0; i < encoder->protected_->channels; i++) {
727
872
                encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
728
873
                encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
733
878
                encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
734
879
                encoder->private_->best_subframe_mid_side[i] = 0;
735
880
        }
736
 
        encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
737
881
        encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
738
882
        encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
739
883
#ifndef FLAC__INTEGER_ONLY_LIBRARY
778
922
#  ifdef FLAC__CPU_IA32
779
923
                FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
780
924
#   ifdef FLAC__HAS_NASM
781
 
#    ifdef FLAC__SSE_OS
782
925
                if(encoder->private_->cpuinfo.data.ia32.sse) {
783
926
                        if(encoder->protected_->max_lpc_order < 4)
784
927
                                encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
789
932
                        else
790
933
                                encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
791
934
                }
792
 
                else
793
 
#    endif /* FLAC__SSE_OS */
794
 
                if(encoder->private_->cpuinfo.data.ia32._3dnow)
 
935
                else if(encoder->private_->cpuinfo.data.ia32._3dnow)
795
936
                        encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
796
937
                else
797
938
                        encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
815
956
                encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
816
957
        }
817
958
 
818
 
        /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
819
 
        encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
 
959
        /* set state to OK; from here on, errors are fatal and we'll override the state then */
 
960
        encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
 
961
 
 
962
#if FLAC__HAS_OGG
 
963
        encoder->private_->is_ogg = is_ogg;
 
964
        if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
 
965
                encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
 
966
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
967
        }
 
968
#endif
 
969
 
 
970
        encoder->private_->read_callback = read_callback;
 
971
        encoder->private_->write_callback = write_callback;
 
972
        encoder->private_->seek_callback = seek_callback;
 
973
        encoder->private_->tell_callback = tell_callback;
 
974
        encoder->private_->metadata_callback = metadata_callback;
 
975
        encoder->private_->client_data = client_data;
820
976
 
821
977
        if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
822
978
                /* the above function sets the state for us in case of an error */
823
 
                return encoder->protected_->state;
 
979
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
824
980
        }
825
981
 
826
 
        if(!FLAC__bitbuffer_init(encoder->private_->frame))
827
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
 
982
        if(!FLAC__bitwriter_init(encoder->private_->frame)) {
 
983
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
 
984
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
985
        }
828
986
 
829
987
        /*
830
988
         * Set up the verify stuff if necessary
834
992
                 * First, set up the fifo which will hold the
835
993
                 * original signal to compare against
836
994
                 */
837
 
                encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
 
995
                encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
838
996
                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;
 
997
                        if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)safe_malloc_mul_2op_(sizeof(FLAC__int32), /*times*/encoder->private_->verify.input_fifo.size))) {
 
998
                                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
 
999
                                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1000
                        }
841
1001
                }
842
1002
                encoder->private_->verify.input_fifo.tail = 0;
843
1003
 
845
1005
                 * Now set up a stream decoder for verification
846
1006
                 */
847
1007
                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;
 
1008
                if(0 == encoder->private_->verify.decoder) {
 
1009
                        encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
 
1010
                        return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1011
                }
850
1012
 
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;
 
1013
                if(FLAC__stream_decoder_init_stream(encoder->private_->verify.decoder, verify_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, verify_write_callback_, verify_metadata_callback_, verify_error_callback_, /*client_data=*/encoder) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
 
1014
                        encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
 
1015
                        return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1016
                }
858
1017
        }
859
1018
        encoder->private_->verify.error_stats.absolute_sample = 0;
860
1019
        encoder->private_->verify.error_stats.frame_number = 0;
864
1023
        encoder->private_->verify.error_stats.got = 0;
865
1024
 
866
1025
        /*
 
1026
         * These must be done before we write any metadata, because that
 
1027
         * calls the write_callback, which uses these values.
 
1028
         */
 
1029
        encoder->private_->first_seekpoint_to_check = 0;
 
1030
        encoder->private_->samples_written = 0;
 
1031
        encoder->protected_->streaminfo_offset = 0;
 
1032
        encoder->protected_->seektable_offset = 0;
 
1033
        encoder->protected_->audio_offset = 0;
 
1034
 
 
1035
        /*
867
1036
         * write the stream header
868
1037
         */
869
1038
        if(encoder->protected_->verify)
870
1039
                encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
871
 
        if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
872
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
873
 
        if(!write_bitbuffer_(encoder, 0)) {
 
1040
        if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
 
1041
                encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
 
1042
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1043
        }
 
1044
        if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
874
1045
                /* the above function sets the state for us in case of an error */
875
 
                return encoder->protected_->state;
 
1046
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
876
1047
        }
877
1048
 
878
1049
        /*
880
1051
         */
881
1052
        if(encoder->protected_->verify)
882
1053
                encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
883
 
        encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
884
 
        encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
885
 
        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 */
895
 
        FLAC__MD5Init(&encoder->private_->md5context);
896
 
        if(!FLAC__bitbuffer_clear(encoder->private_->frame))
897
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
898
 
        if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
899
 
                return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
900
 
        if(!write_bitbuffer_(encoder, 0)) {
 
1054
        encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
 
1055
        encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
 
1056
        encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
 
1057
        encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
 
1058
        encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
 
1059
        encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
 
1060
        encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
 
1061
        encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
 
1062
        encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
 
1063
        encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
 
1064
        encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
 
1065
        memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
 
1066
        if(encoder->protected_->do_md5)
 
1067
                FLAC__MD5Init(&encoder->private_->md5context);
 
1068
        if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
 
1069
                encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
 
1070
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1071
        }
 
1072
        if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
901
1073
                /* the above function sets the state for us in case of an error */
902
 
                return encoder->protected_->state;
 
1074
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
903
1075
        }
904
1076
 
905
1077
        /*
906
1078
         * Now that the STREAMINFO block is written, we can init this to an
907
1079
         * absurdly-high value...
908
1080
         */
909
 
        encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
 
1081
        encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
910
1082
        /* ... and clear this to 0 */
911
 
        encoder->private_->metadata.data.stream_info.total_samples = 0;
 
1083
        encoder->private_->streaminfo.data.stream_info.total_samples = 0;
912
1084
 
913
1085
        /*
914
1086
         * Check to see if the supplied metadata contains a VORBIS_COMMENT;
915
1087
         * if not, we will write an empty one (FLAC__add_metadata_block()
916
1088
         * automatically supplies the vendor string).
917
1089
         *
918
 
         * WATCHOUT: libOggFLAC depends on us to write this block after the
919
 
         * STREAMINFO since that's what the mapping requires.  (In the case
920
 
         * that metadata_has_vorbis_comment is true it will have already
921
 
         * insured that the metadata list is properly ordered.)
 
1090
         * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
 
1091
         * the STREAMINFO.  (In the case that metadata_has_vorbis_comment is
 
1092
         * true it will have already insured that the metadata list is properly
 
1093
         * ordered.)
922
1094
         */
923
1095
        if(!metadata_has_vorbis_comment) {
924
1096
                FLAC__StreamMetadata vorbis_comment;
929
1101
                vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
930
1102
                vorbis_comment.data.vorbis_comment.num_comments = 0;
931
1103
                vorbis_comment.data.vorbis_comment.comments = 0;
932
 
                if(!FLAC__bitbuffer_clear(encoder->private_->frame))
933
 
                        return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
934
 
                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)) {
 
1104
                if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
 
1105
                        encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
 
1106
                        return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1107
                }
 
1108
                if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
937
1109
                        /* the above function sets the state for us in case of an error */
938
 
                        return encoder->protected_->state;
 
1110
                        return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
939
1111
                }
940
1112
        }
941
1113
 
944
1116
         */
945
1117
        for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
946
1118
                encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
947
 
                if(!FLAC__bitbuffer_clear(encoder->private_->frame))
948
 
                        return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
949
 
                if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
950
 
                        return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
951
 
                if(!write_bitbuffer_(encoder, 0)) {
 
1119
                if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
 
1120
                        encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
 
1121
                        return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1122
                }
 
1123
                if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
952
1124
                        /* the above function sets the state for us in case of an error */
953
 
                        return encoder->protected_->state;
 
1125
                        return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
954
1126
                }
955
1127
        }
956
1128
 
 
1129
        /* now that all the metadata is written, we save the stream offset */
 
1130
        if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
 
1131
                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
1132
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1133
        }
 
1134
 
957
1135
        if(encoder->protected_->verify)
958
1136
                encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
959
1137
 
960
 
        return encoder->protected_->state;
961
 
}
962
 
 
963
 
FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
964
 
{
965
 
        FLAC__ASSERT(0 != encoder);
 
1138
        return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
 
1139
}
 
1140
 
 
1141
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
 
1142
        FLAC__StreamEncoder *encoder,
 
1143
        FLAC__StreamEncoderWriteCallback write_callback,
 
1144
        FLAC__StreamEncoderSeekCallback seek_callback,
 
1145
        FLAC__StreamEncoderTellCallback tell_callback,
 
1146
        FLAC__StreamEncoderMetadataCallback metadata_callback,
 
1147
        void *client_data
 
1148
)
 
1149
{
 
1150
        return init_stream_internal_(
 
1151
                encoder,
 
1152
                /*read_callback=*/0,
 
1153
                write_callback,
 
1154
                seek_callback,
 
1155
                tell_callback,
 
1156
                metadata_callback,
 
1157
                client_data,
 
1158
                /*is_ogg=*/false
 
1159
        );
 
1160
}
 
1161
 
 
1162
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
 
1163
        FLAC__StreamEncoder *encoder,
 
1164
        FLAC__StreamEncoderReadCallback read_callback,
 
1165
        FLAC__StreamEncoderWriteCallback write_callback,
 
1166
        FLAC__StreamEncoderSeekCallback seek_callback,
 
1167
        FLAC__StreamEncoderTellCallback tell_callback,
 
1168
        FLAC__StreamEncoderMetadataCallback metadata_callback,
 
1169
        void *client_data
 
1170
)
 
1171
{
 
1172
        return init_stream_internal_(
 
1173
                encoder,
 
1174
                read_callback,
 
1175
                write_callback,
 
1176
                seek_callback,
 
1177
                tell_callback,
 
1178
                metadata_callback,
 
1179
                client_data,
 
1180
                /*is_ogg=*/true
 
1181
        );
 
1182
}
 
1183
 
 
1184
static FLAC__StreamEncoderInitStatus init_FILE_internal_(
 
1185
        FLAC__StreamEncoder *encoder,
 
1186
        FILE *file,
 
1187
        FLAC__StreamEncoderProgressCallback progress_callback,
 
1188
        void *client_data,
 
1189
        FLAC__bool is_ogg
 
1190
)
 
1191
{
 
1192
        FLAC__StreamEncoderInitStatus init_status;
 
1193
 
 
1194
        FLAC__ASSERT(0 != encoder);
 
1195
        FLAC__ASSERT(0 != file);
 
1196
 
 
1197
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1198
                return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
 
1199
 
 
1200
        /* double protection */
 
1201
        if(file == 0) {
 
1202
                encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
 
1203
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1204
        }
 
1205
 
 
1206
        /*
 
1207
         * To make sure that our file does not go unclosed after an error, we
 
1208
         * must assign the FILE pointer before any further error can occur in
 
1209
         * this routine.
 
1210
         */
 
1211
        if(file == stdout)
 
1212
                file = get_binary_stdout_(); /* just to be safe */
 
1213
 
 
1214
        encoder->private_->file = file;
 
1215
 
 
1216
        encoder->private_->progress_callback = progress_callback;
 
1217
        encoder->private_->bytes_written = 0;
 
1218
        encoder->private_->samples_written = 0;
 
1219
        encoder->private_->frames_written = 0;
 
1220
 
 
1221
        init_status = init_stream_internal_(
 
1222
                encoder,
 
1223
                encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
 
1224
                file_write_callback_,
 
1225
                encoder->private_->file == stdout? 0 : file_seek_callback_,
 
1226
                encoder->private_->file == stdout? 0 : file_tell_callback_,
 
1227
                /*metadata_callback=*/0,
 
1228
                client_data,
 
1229
                is_ogg
 
1230
        );
 
1231
        if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
 
1232
                /* the above function sets the state for us in case of an error */
 
1233
                return init_status;
 
1234
        }
 
1235
 
 
1236
        {
 
1237
                unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
 
1238
 
 
1239
                FLAC__ASSERT(blocksize != 0);
 
1240
                encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
 
1241
        }
 
1242
 
 
1243
        return init_status;
 
1244
}
 
1245
 
 
1246
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
 
1247
        FLAC__StreamEncoder *encoder,
 
1248
        FILE *file,
 
1249
        FLAC__StreamEncoderProgressCallback progress_callback,
 
1250
        void *client_data
 
1251
)
 
1252
{
 
1253
        return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
 
1254
}
 
1255
 
 
1256
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
 
1257
        FLAC__StreamEncoder *encoder,
 
1258
        FILE *file,
 
1259
        FLAC__StreamEncoderProgressCallback progress_callback,
 
1260
        void *client_data
 
1261
)
 
1262
{
 
1263
        return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
 
1264
}
 
1265
 
 
1266
static FLAC__StreamEncoderInitStatus init_file_internal_(
 
1267
        FLAC__StreamEncoder *encoder,
 
1268
        const char *filename,
 
1269
        FLAC__StreamEncoderProgressCallback progress_callback,
 
1270
        void *client_data,
 
1271
        FLAC__bool is_ogg
 
1272
)
 
1273
{
 
1274
        FILE *file;
 
1275
 
 
1276
        FLAC__ASSERT(0 != encoder);
 
1277
 
 
1278
        /*
 
1279
         * To make sure that our file does not go unclosed after an error, we
 
1280
         * have to do the same entrance checks here that are later performed
 
1281
         * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
 
1282
         */
 
1283
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1284
                return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
 
1285
 
 
1286
        file = filename? fopen(filename, "w+b") : stdout;
 
1287
 
 
1288
        if(file == 0) {
 
1289
                encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
 
1290
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
 
1291
        }
 
1292
 
 
1293
        return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
 
1294
}
 
1295
 
 
1296
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
 
1297
        FLAC__StreamEncoder *encoder,
 
1298
        const char *filename,
 
1299
        FLAC__StreamEncoderProgressCallback progress_callback,
 
1300
        void *client_data
 
1301
)
 
1302
{
 
1303
        return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
 
1304
}
 
1305
 
 
1306
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
 
1307
        FLAC__StreamEncoder *encoder,
 
1308
        const char *filename,
 
1309
        FLAC__StreamEncoderProgressCallback progress_callback,
 
1310
        void *client_data
 
1311
)
 
1312
{
 
1313
        return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
 
1314
}
 
1315
 
 
1316
FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
 
1317
{
 
1318
        FLAC__bool error = false;
 
1319
 
 
1320
        FLAC__ASSERT(0 != encoder);
 
1321
        FLAC__ASSERT(0 != encoder->private_);
 
1322
        FLAC__ASSERT(0 != encoder->protected_);
966
1323
 
967
1324
        if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
968
 
                return;
 
1325
                return true;
969
1326
 
970
1327
        if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
971
1328
                if(encoder->private_->current_sample_number != 0) {
 
1329
                        const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
972
1330
                        encoder->protected_->blocksize = encoder->private_->current_sample_number;
973
 
                        process_frame_(encoder, true); /* true => is last frame */
974
 
                }
975
 
        }
976
 
 
977
 
        FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
978
 
 
979
 
        if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
980
 
                encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
981
 
        }
982
 
 
983
 
        if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
984
 
                FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
 
1331
                        if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
 
1332
                                error = true;
 
1333
                }
 
1334
        }
 
1335
 
 
1336
        if(encoder->protected_->do_md5)
 
1337
                FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
 
1338
 
 
1339
        if(!encoder->private_->is_being_deleted) {
 
1340
                if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
 
1341
                        if(encoder->private_->seek_callback) {
 
1342
#if FLAC__HAS_OGG
 
1343
                                if(encoder->private_->is_ogg)
 
1344
                                        update_ogg_metadata_(encoder);
 
1345
                                else
 
1346
#endif
 
1347
                                update_metadata_(encoder);
 
1348
 
 
1349
                                /* check if an error occurred while updating metadata */
 
1350
                                if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
 
1351
                                        error = true;
 
1352
                        }
 
1353
                        if(encoder->private_->metadata_callback)
 
1354
                                encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
 
1355
                }
 
1356
 
 
1357
                if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
 
1358
                        if(!error)
 
1359
                                encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
 
1360
                        error = true;
 
1361
                }
 
1362
        }
 
1363
 
 
1364
        if(0 != encoder->private_->file) {
 
1365
                if(encoder->private_->file != stdout)
 
1366
                        fclose(encoder->private_->file);
 
1367
                encoder->private_->file = 0;
 
1368
        }
 
1369
 
 
1370
#if FLAC__HAS_OGG
 
1371
        if(encoder->private_->is_ogg)
 
1372
                FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
 
1373
#endif
985
1374
 
986
1375
        free_(encoder);
987
1376
        set_defaults_(encoder);
988
1377
 
989
 
        encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
 
1378
        if(!error)
 
1379
                encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
 
1380
 
 
1381
        return !error;
 
1382
}
 
1383
 
 
1384
FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
 
1385
{
 
1386
        FLAC__ASSERT(0 != encoder);
 
1387
        FLAC__ASSERT(0 != encoder->private_);
 
1388
        FLAC__ASSERT(0 != encoder->protected_);
 
1389
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1390
                return false;
 
1391
#if FLAC__HAS_OGG
 
1392
        /* can't check encoder->private_->is_ogg since that's not set until init time */
 
1393
        FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
 
1394
        return true;
 
1395
#else
 
1396
        (void)value;
 
1397
        return false;
 
1398
#endif
990
1399
}
991
1400
 
992
1401
FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
993
1402
{
994
1403
        FLAC__ASSERT(0 != encoder);
 
1404
        FLAC__ASSERT(0 != encoder->private_);
 
1405
        FLAC__ASSERT(0 != encoder->protected_);
995
1406
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
996
1407
                return false;
997
1408
#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1003
1414
FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
1004
1415
{
1005
1416
        FLAC__ASSERT(0 != encoder);
 
1417
        FLAC__ASSERT(0 != encoder->private_);
 
1418
        FLAC__ASSERT(0 != encoder->protected_);
1006
1419
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1007
1420
                return false;
1008
1421
        encoder->protected_->streamable_subset = value;
1009
1422
        return true;
1010
1423
}
1011
1424
 
1012
 
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1013
 
{
1014
 
        FLAC__ASSERT(0 != encoder);
1015
 
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1016
 
                return false;
1017
 
        encoder->protected_->do_mid_side_stereo = value;
1018
 
        return true;
1019
 
}
1020
 
 
1021
 
FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1022
 
{
1023
 
        FLAC__ASSERT(0 != encoder);
1024
 
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1025
 
                return false;
1026
 
        encoder->protected_->loose_mid_side_stereo = value;
 
1425
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value)
 
1426
{
 
1427
        FLAC__ASSERT(0 != encoder);
 
1428
        FLAC__ASSERT(0 != encoder->private_);
 
1429
        FLAC__ASSERT(0 != encoder->protected_);
 
1430
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1431
                return false;
 
1432
        encoder->protected_->do_md5 = value;
1027
1433
        return true;
1028
1434
}
1029
1435
 
1030
1436
FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
1031
1437
{
1032
1438
        FLAC__ASSERT(0 != encoder);
 
1439
        FLAC__ASSERT(0 != encoder->private_);
 
1440
        FLAC__ASSERT(0 != encoder->protected_);
1033
1441
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1034
1442
                return false;
1035
1443
        encoder->protected_->channels = value;
1039
1447
FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
1040
1448
{
1041
1449
        FLAC__ASSERT(0 != encoder);
 
1450
        FLAC__ASSERT(0 != encoder->private_);
 
1451
        FLAC__ASSERT(0 != encoder->protected_);
1042
1452
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1043
1453
                return false;
1044
1454
        encoder->protected_->bits_per_sample = value;
1048
1458
FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1049
1459
{
1050
1460
        FLAC__ASSERT(0 != encoder);
 
1461
        FLAC__ASSERT(0 != encoder->private_);
 
1462
        FLAC__ASSERT(0 != encoder->protected_);
1051
1463
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1052
1464
                return false;
1053
1465
        encoder->protected_->sample_rate = value;
1054
1466
        return true;
1055
1467
}
1056
1468
 
 
1469
FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
 
1470
{
 
1471
        FLAC__bool ok = true;
 
1472
        FLAC__ASSERT(0 != encoder);
 
1473
        FLAC__ASSERT(0 != encoder->private_);
 
1474
        FLAC__ASSERT(0 != encoder->protected_);
 
1475
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1476
                return false;
 
1477
        if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
 
1478
                value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
 
1479
        ok &= FLAC__stream_encoder_set_do_mid_side_stereo          (encoder, compression_levels_[value].do_mid_side_stereo);
 
1480
        ok &= FLAC__stream_encoder_set_loose_mid_side_stereo       (encoder, compression_levels_[value].loose_mid_side_stereo);
 
1481
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
1482
#if 0
 
1483
        /* was: */
 
1484
        ok &= FLAC__stream_encoder_set_apodization                 (encoder, compression_levels_[value].apodization);
 
1485
        /* but it's too hard to specify the string in a locale-specific way */
 
1486
#else
 
1487
        encoder->protected_->num_apodizations = 1;
 
1488
        encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
 
1489
        encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
 
1490
#endif
 
1491
#endif
 
1492
        ok &= FLAC__stream_encoder_set_max_lpc_order               (encoder, compression_levels_[value].max_lpc_order);
 
1493
        ok &= FLAC__stream_encoder_set_qlp_coeff_precision         (encoder, compression_levels_[value].qlp_coeff_precision);
 
1494
        ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search    (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
 
1495
        ok &= FLAC__stream_encoder_set_do_escape_coding            (encoder, compression_levels_[value].do_escape_coding);
 
1496
        ok &= FLAC__stream_encoder_set_do_exhaustive_model_search  (encoder, compression_levels_[value].do_exhaustive_model_search);
 
1497
        ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
 
1498
        ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
 
1499
        ok &= FLAC__stream_encoder_set_rice_parameter_search_dist  (encoder, compression_levels_[value].rice_parameter_search_dist);
 
1500
        return ok;
 
1501
}
 
1502
 
1057
1503
FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1058
1504
{
1059
1505
        FLAC__ASSERT(0 != encoder);
 
1506
        FLAC__ASSERT(0 != encoder->private_);
 
1507
        FLAC__ASSERT(0 != encoder->protected_);
1060
1508
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1061
1509
                return false;
1062
1510
        encoder->protected_->blocksize = value;
1063
1511
        return true;
1064
1512
}
1065
1513
 
 
1514
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
 
1515
{
 
1516
        FLAC__ASSERT(0 != encoder);
 
1517
        FLAC__ASSERT(0 != encoder->private_);
 
1518
        FLAC__ASSERT(0 != encoder->protected_);
 
1519
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1520
                return false;
 
1521
        encoder->protected_->do_mid_side_stereo = value;
 
1522
        return true;
 
1523
}
 
1524
 
 
1525
FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
 
1526
{
 
1527
        FLAC__ASSERT(0 != encoder);
 
1528
        FLAC__ASSERT(0 != encoder->private_);
 
1529
        FLAC__ASSERT(0 != encoder->protected_);
 
1530
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1531
                return false;
 
1532
        encoder->protected_->loose_mid_side_stereo = value;
 
1533
        return true;
 
1534
}
 
1535
 
 
1536
/*@@@@add to tests*/
 
1537
FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
 
1538
{
 
1539
        FLAC__ASSERT(0 != encoder);
 
1540
        FLAC__ASSERT(0 != encoder->private_);
 
1541
        FLAC__ASSERT(0 != encoder->protected_);
 
1542
        FLAC__ASSERT(0 != specification);
 
1543
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1544
                return false;
 
1545
#ifdef FLAC__INTEGER_ONLY_LIBRARY
 
1546
        (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
 
1547
#else
 
1548
        encoder->protected_->num_apodizations = 0;
 
1549
        while(1) {
 
1550
                const char *s = strchr(specification, ';');
 
1551
                const size_t n = s? (size_t)(s - specification) : strlen(specification);
 
1552
                if     (n==8  && 0 == strncmp("bartlett"     , specification, n))
 
1553
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
 
1554
                else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
 
1555
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
 
1556
                else if(n==8  && 0 == strncmp("blackman"     , specification, n))
 
1557
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
 
1558
                else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
 
1559
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
 
1560
                else if(n==6  && 0 == strncmp("connes"       , specification, n))
 
1561
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
 
1562
                else if(n==7  && 0 == strncmp("flattop"      , specification, n))
 
1563
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
 
1564
                else if(n>7   && 0 == strncmp("gauss("       , specification, 6)) {
 
1565
                        FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
 
1566
                        if (stddev > 0.0 && stddev <= 0.5) {
 
1567
                                encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
 
1568
                                encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
 
1569
                        }
 
1570
                }
 
1571
                else if(n==7  && 0 == strncmp("hamming"      , specification, n))
 
1572
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
 
1573
                else if(n==4  && 0 == strncmp("hann"         , specification, n))
 
1574
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
 
1575
                else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
 
1576
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
 
1577
                else if(n==7  && 0 == strncmp("nuttall"      , specification, n))
 
1578
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
 
1579
                else if(n==9  && 0 == strncmp("rectangle"    , specification, n))
 
1580
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
 
1581
                else if(n==8  && 0 == strncmp("triangle"     , specification, n))
 
1582
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
 
1583
                else if(n>7   && 0 == strncmp("tukey("       , specification, 6)) {
 
1584
                        FLAC__real p = (FLAC__real)strtod(specification+6, 0);
 
1585
                        if (p >= 0.0 && p <= 1.0) {
 
1586
                                encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
 
1587
                                encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
 
1588
                        }
 
1589
                }
 
1590
                else if(n==5  && 0 == strncmp("welch"        , specification, n))
 
1591
                        encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
 
1592
                if (encoder->protected_->num_apodizations == 32)
 
1593
                        break;
 
1594
                if (s)
 
1595
                        specification = s+1;
 
1596
                else
 
1597
                        break;
 
1598
        }
 
1599
        if(encoder->protected_->num_apodizations == 0) {
 
1600
                encoder->protected_->num_apodizations = 1;
 
1601
                encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
 
1602
                encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
 
1603
        }
 
1604
#endif
 
1605
        return true;
 
1606
}
 
1607
 
1066
1608
FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1067
1609
{
1068
1610
        FLAC__ASSERT(0 != encoder);
 
1611
        FLAC__ASSERT(0 != encoder->private_);
 
1612
        FLAC__ASSERT(0 != encoder->protected_);
1069
1613
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1070
1614
                return false;
1071
1615
        encoder->protected_->max_lpc_order = value;
1075
1619
FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
1076
1620
{
1077
1621
        FLAC__ASSERT(0 != encoder);
 
1622
        FLAC__ASSERT(0 != encoder->private_);
 
1623
        FLAC__ASSERT(0 != encoder->protected_);
1078
1624
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1079
1625
                return false;
1080
1626
        encoder->protected_->qlp_coeff_precision = value;
1084
1630
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1085
1631
{
1086
1632
        FLAC__ASSERT(0 != encoder);
 
1633
        FLAC__ASSERT(0 != encoder->private_);
 
1634
        FLAC__ASSERT(0 != encoder->protected_);
1087
1635
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1088
1636
                return false;
1089
1637
        encoder->protected_->do_qlp_coeff_prec_search = value;
1093
1641
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
1094
1642
{
1095
1643
        FLAC__ASSERT(0 != encoder);
 
1644
        FLAC__ASSERT(0 != encoder->private_);
 
1645
        FLAC__ASSERT(0 != encoder->protected_);
1096
1646
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1097
1647
                return false;
1098
1648
#if 0
1107
1657
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
1108
1658
{
1109
1659
        FLAC__ASSERT(0 != encoder);
 
1660
        FLAC__ASSERT(0 != encoder->private_);
 
1661
        FLAC__ASSERT(0 != encoder->protected_);
1110
1662
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1111
1663
                return false;
1112
1664
        encoder->protected_->do_exhaustive_model_search = value;
1116
1668
FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1117
1669
{
1118
1670
        FLAC__ASSERT(0 != encoder);
 
1671
        FLAC__ASSERT(0 != encoder->private_);
 
1672
        FLAC__ASSERT(0 != encoder->protected_);
1119
1673
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1120
1674
                return false;
1121
1675
        encoder->protected_->min_residual_partition_order = value;
1125
1679
FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
1126
1680
{
1127
1681
        FLAC__ASSERT(0 != encoder);
 
1682
        FLAC__ASSERT(0 != encoder->private_);
 
1683
        FLAC__ASSERT(0 != encoder->protected_);
1128
1684
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1129
1685
                return false;
1130
1686
        encoder->protected_->max_residual_partition_order = value;
1134
1690
FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
1135
1691
{
1136
1692
        FLAC__ASSERT(0 != encoder);
 
1693
        FLAC__ASSERT(0 != encoder->private_);
 
1694
        FLAC__ASSERT(0 != encoder->protected_);
1137
1695
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1138
1696
                return false;
1139
1697
#if 0
1148
1706
FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
1149
1707
{
1150
1708
        FLAC__ASSERT(0 != encoder);
 
1709
        FLAC__ASSERT(0 != encoder->private_);
 
1710
        FLAC__ASSERT(0 != encoder->protected_);
1151
1711
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1152
1712
                return false;
1153
1713
        encoder->protected_->total_samples_estimate = value;
1157
1717
FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
1158
1718
{
1159
1719
        FLAC__ASSERT(0 != encoder);
1160
 
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1161
 
                return false;
1162
 
        encoder->protected_->metadata = metadata;
1163
 
        encoder->protected_->num_metadata_blocks = num_blocks;
1164
 
        return true;
1165
 
}
1166
 
 
1167
 
FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
1168
 
{
1169
 
        FLAC__ASSERT(0 != encoder);
1170
 
        FLAC__ASSERT(0 != value);
1171
 
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1172
 
                return false;
1173
 
        encoder->private_->write_callback = value;
1174
 
        return true;
1175
 
}
1176
 
 
1177
 
FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
1178
 
{
1179
 
        FLAC__ASSERT(0 != encoder);
1180
 
        FLAC__ASSERT(0 != value);
1181
 
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1182
 
                return false;
1183
 
        encoder->private_->metadata_callback = value;
1184
 
        return true;
1185
 
}
1186
 
 
1187
 
FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
1188
 
{
1189
 
        FLAC__ASSERT(0 != encoder);
1190
 
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1191
 
                return false;
1192
 
        encoder->private_->client_data = value;
 
1720
        FLAC__ASSERT(0 != encoder->private_);
 
1721
        FLAC__ASSERT(0 != encoder->protected_);
 
1722
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
 
1723
                return false;
 
1724
        if(0 == metadata)
 
1725
                num_blocks = 0;
 
1726
        if(0 == num_blocks)
 
1727
                metadata = 0;
 
1728
        /* realloc() does not do exactly what we want so... */
 
1729
        if(encoder->protected_->metadata) {
 
1730
                free(encoder->protected_->metadata);
 
1731
                encoder->protected_->metadata = 0;
 
1732
                encoder->protected_->num_metadata_blocks = 0;
 
1733
        }
 
1734
        if(num_blocks) {
 
1735
                FLAC__StreamMetadata **m;
 
1736
                if(0 == (m = (FLAC__StreamMetadata**)safe_malloc_mul_2op_(sizeof(m[0]), /*times*/num_blocks)))
 
1737
                        return false;
 
1738
                memcpy(m, metadata, sizeof(m[0]) * num_blocks);
 
1739
                encoder->protected_->metadata = m;
 
1740
                encoder->protected_->num_metadata_blocks = num_blocks;
 
1741
        }
 
1742
#if FLAC__HAS_OGG
 
1743
        if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
 
1744
                return false;
 
1745
#endif
1193
1746
        return true;
1194
1747
}
1195
1748
 
1200
1753
FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1201
1754
{
1202
1755
        FLAC__ASSERT(0 != encoder);
 
1756
        FLAC__ASSERT(0 != encoder->private_);
 
1757
        FLAC__ASSERT(0 != encoder->protected_);
1203
1758
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1204
1759
                return false;
1205
1760
        encoder->private_->disable_constant_subframes = value;
1209
1764
FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1210
1765
{
1211
1766
        FLAC__ASSERT(0 != encoder);
 
1767
        FLAC__ASSERT(0 != encoder->private_);
 
1768
        FLAC__ASSERT(0 != encoder->protected_);
1212
1769
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1213
1770
                return false;
1214
1771
        encoder->private_->disable_fixed_subframes = value;
1218
1775
FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
1219
1776
{
1220
1777
        FLAC__ASSERT(0 != encoder);
 
1778
        FLAC__ASSERT(0 != encoder->private_);
 
1779
        FLAC__ASSERT(0 != encoder->protected_);
1221
1780
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1222
1781
                return false;
1223
1782
        encoder->private_->disable_verbatim_subframes = value;
1227
1786
FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
1228
1787
{
1229
1788
        FLAC__ASSERT(0 != encoder);
 
1789
        FLAC__ASSERT(0 != encoder->private_);
 
1790
        FLAC__ASSERT(0 != encoder->protected_);
1230
1791
        return encoder->protected_->state;
1231
1792
}
1232
1793
 
1233
1794
FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
1234
1795
{
1235
1796
        FLAC__ASSERT(0 != encoder);
 
1797
        FLAC__ASSERT(0 != encoder->private_);
 
1798
        FLAC__ASSERT(0 != encoder->protected_);
1236
1799
        if(encoder->protected_->verify)
1237
1800
                return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1238
1801
        else
1241
1804
 
1242
1805
FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
1243
1806
{
 
1807
        FLAC__ASSERT(0 != encoder);
 
1808
        FLAC__ASSERT(0 != encoder->private_);
 
1809
        FLAC__ASSERT(0 != encoder->protected_);
1244
1810
        if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
1245
1811
                return FLAC__StreamEncoderStateString[encoder->protected_->state];
1246
1812
        else
1250
1816
FLAC_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)
1251
1817
{
1252
1818
        FLAC__ASSERT(0 != encoder);
 
1819
        FLAC__ASSERT(0 != encoder->private_);
 
1820
        FLAC__ASSERT(0 != encoder->protected_);
1253
1821
        if(0 != absolute_sample)
1254
1822
                *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
1255
1823
        if(0 != frame_number)
1267
1835
FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1268
1836
{
1269
1837
        FLAC__ASSERT(0 != encoder);
 
1838
        FLAC__ASSERT(0 != encoder->private_);
 
1839
        FLAC__ASSERT(0 != encoder->protected_);
1270
1840
        return encoder->protected_->verify;
1271
1841
}
1272
1842
 
1273
1843
FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1274
1844
{
1275
1845
        FLAC__ASSERT(0 != encoder);
 
1846
        FLAC__ASSERT(0 != encoder->private_);
 
1847
        FLAC__ASSERT(0 != encoder->protected_);
1276
1848
        return encoder->protected_->streamable_subset;
1277
1849
}
1278
1850
 
1279
 
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1280
 
{
1281
 
        FLAC__ASSERT(0 != encoder);
1282
 
        return encoder->protected_->do_mid_side_stereo;
1283
 
}
1284
 
 
1285
 
FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1286
 
{
1287
 
        FLAC__ASSERT(0 != encoder);
1288
 
        return encoder->protected_->loose_mid_side_stereo;
 
1851
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder)
 
1852
{
 
1853
        FLAC__ASSERT(0 != encoder);
 
1854
        FLAC__ASSERT(0 != encoder->private_);
 
1855
        FLAC__ASSERT(0 != encoder->protected_);
 
1856
        return encoder->protected_->do_md5;
1289
1857
}
1290
1858
 
1291
1859
FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1292
1860
{
1293
1861
        FLAC__ASSERT(0 != encoder);
 
1862
        FLAC__ASSERT(0 != encoder->private_);
 
1863
        FLAC__ASSERT(0 != encoder->protected_);
1294
1864
        return encoder->protected_->channels;
1295
1865
}
1296
1866
 
1297
1867
FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1298
1868
{
1299
1869
        FLAC__ASSERT(0 != encoder);
 
1870
        FLAC__ASSERT(0 != encoder->private_);
 
1871
        FLAC__ASSERT(0 != encoder->protected_);
1300
1872
        return encoder->protected_->bits_per_sample;
1301
1873
}
1302
1874
 
1303
1875
FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1304
1876
{
1305
1877
        FLAC__ASSERT(0 != encoder);
 
1878
        FLAC__ASSERT(0 != encoder->private_);
 
1879
        FLAC__ASSERT(0 != encoder->protected_);
1306
1880
        return encoder->protected_->sample_rate;
1307
1881
}
1308
1882
 
1309
1883
FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1310
1884
{
1311
1885
        FLAC__ASSERT(0 != encoder);
 
1886
        FLAC__ASSERT(0 != encoder->private_);
 
1887
        FLAC__ASSERT(0 != encoder->protected_);
1312
1888
        return encoder->protected_->blocksize;
1313
1889
}
1314
1890
 
 
1891
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
 
1892
{
 
1893
        FLAC__ASSERT(0 != encoder);
 
1894
        FLAC__ASSERT(0 != encoder->private_);
 
1895
        FLAC__ASSERT(0 != encoder->protected_);
 
1896
        return encoder->protected_->do_mid_side_stereo;
 
1897
}
 
1898
 
 
1899
FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
 
1900
{
 
1901
        FLAC__ASSERT(0 != encoder);
 
1902
        FLAC__ASSERT(0 != encoder->private_);
 
1903
        FLAC__ASSERT(0 != encoder->protected_);
 
1904
        return encoder->protected_->loose_mid_side_stereo;
 
1905
}
 
1906
 
1315
1907
FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1316
1908
{
1317
1909
        FLAC__ASSERT(0 != encoder);
 
1910
        FLAC__ASSERT(0 != encoder->private_);
 
1911
        FLAC__ASSERT(0 != encoder->protected_);
1318
1912
        return encoder->protected_->max_lpc_order;
1319
1913
}
1320
1914
 
1321
1915
FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1322
1916
{
1323
1917
        FLAC__ASSERT(0 != encoder);
 
1918
        FLAC__ASSERT(0 != encoder->private_);
 
1919
        FLAC__ASSERT(0 != encoder->protected_);
1324
1920
        return encoder->protected_->qlp_coeff_precision;
1325
1921
}
1326
1922
 
1327
1923
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1328
1924
{
1329
1925
        FLAC__ASSERT(0 != encoder);
 
1926
        FLAC__ASSERT(0 != encoder->private_);
 
1927
        FLAC__ASSERT(0 != encoder->protected_);
1330
1928
        return encoder->protected_->do_qlp_coeff_prec_search;
1331
1929
}
1332
1930
 
1333
1931
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1334
1932
{
1335
1933
        FLAC__ASSERT(0 != encoder);
 
1934
        FLAC__ASSERT(0 != encoder->private_);
 
1935
        FLAC__ASSERT(0 != encoder->protected_);
1336
1936
        return encoder->protected_->do_escape_coding;
1337
1937
}
1338
1938
 
1339
1939
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1340
1940
{
1341
1941
        FLAC__ASSERT(0 != encoder);
 
1942
        FLAC__ASSERT(0 != encoder->private_);
 
1943
        FLAC__ASSERT(0 != encoder->protected_);
1342
1944
        return encoder->protected_->do_exhaustive_model_search;
1343
1945
}
1344
1946
 
1345
1947
FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1346
1948
{
1347
1949
        FLAC__ASSERT(0 != encoder);
 
1950
        FLAC__ASSERT(0 != encoder->private_);
 
1951
        FLAC__ASSERT(0 != encoder->protected_);
1348
1952
        return encoder->protected_->min_residual_partition_order;
1349
1953
}
1350
1954
 
1351
1955
FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1352
1956
{
1353
1957
        FLAC__ASSERT(0 != encoder);
 
1958
        FLAC__ASSERT(0 != encoder->private_);
 
1959
        FLAC__ASSERT(0 != encoder->protected_);
1354
1960
        return encoder->protected_->max_residual_partition_order;
1355
1961
}
1356
1962
 
1357
1963
FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1358
1964
{
1359
1965
        FLAC__ASSERT(0 != encoder);
 
1966
        FLAC__ASSERT(0 != encoder->private_);
 
1967
        FLAC__ASSERT(0 != encoder->protected_);
1360
1968
        return encoder->protected_->rice_parameter_search_dist;
1361
1969
}
1362
1970
 
1363
1971
FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1364
1972
{
1365
1973
        FLAC__ASSERT(0 != encoder);
 
1974
        FLAC__ASSERT(0 != encoder->private_);
 
1975
        FLAC__ASSERT(0 != encoder->protected_);
1366
1976
        return encoder->protected_->total_samples_estimate;
1367
1977
}
1368
1978
 
1369
1979
FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1370
1980
{
1371
 
        unsigned i, j, channel;
1372
 
        FLAC__int32 x, mid, side;
 
1981
        unsigned i, j = 0, channel;
1373
1982
        const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1374
1983
 
1375
1984
        FLAC__ASSERT(0 != encoder);
 
1985
        FLAC__ASSERT(0 != encoder->private_);
 
1986
        FLAC__ASSERT(0 != encoder->protected_);
1376
1987
        FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1377
1988
 
1378
 
        j = 0;
1379
 
        /*
1380
 
         * we have several flavors of the same basic loop, optimized for
1381
 
         * different conditions:
1382
 
         */
1383
 
        if(encoder->protected_->max_lpc_order > 0) {
1384
 
                if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1385
 
                        /*
1386
 
                         * stereo coding: unroll channel loop
1387
 
                         * with LPC: calculate floating point version of signal
1388
 
                         */
1389
 
                        do {
1390
 
                                if(encoder->protected_->verify)
1391
 
                                        append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1392
 
 
1393
 
                                for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1394
 
                                        x = mid = side = buffer[0][j];
1395
 
                                        encoder->private_->integer_signal[0][i] = x;
1396
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1397
 
                                        encoder->private_->real_signal[0][i] = (FLAC__real)x;
1398
 
#endif
1399
 
                                        x = buffer[1][j];
1400
 
                                        encoder->private_->integer_signal[1][i] = x;
1401
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1402
 
                                        encoder->private_->real_signal[1][i] = (FLAC__real)x;
1403
 
#endif
1404
 
                                        mid += x;
1405
 
                                        side -= x;
1406
 
                                        mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1407
 
                                        encoder->private_->integer_signal_mid_side[1][i] = side;
1408
 
                                        encoder->private_->integer_signal_mid_side[0][i] = mid;
1409
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1410
 
                                        encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1411
 
                                        encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1412
 
#endif
1413
 
                                        encoder->private_->current_sample_number++;
1414
 
                                }
1415
 
                                if(i == blocksize) {
1416
 
                                        if(!process_frame_(encoder, false)) /* false => not last frame */
1417
 
                                                return false;
1418
 
                                }
1419
 
                        } while(j < samples);
1420
 
                }
1421
 
                else {
1422
 
                        /*
1423
 
                         * independent channel coding: buffer each channel in inner loop
1424
 
                         * with LPC: calculate floating point version of signal
1425
 
                         */
1426
 
                        do {
1427
 
                                if(encoder->protected_->verify)
1428
 
                                        append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1429
 
 
1430
 
                                for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1431
 
                                        for(channel = 0; channel < channels; channel++) {
1432
 
                                                x = buffer[channel][j];
1433
 
                                                encoder->private_->integer_signal[channel][i] = x;
1434
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1435
 
                                                encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1436
 
#endif
1437
 
                                        }
1438
 
                                        encoder->private_->current_sample_number++;
1439
 
                                }
1440
 
                                if(i == blocksize) {
1441
 
                                        if(!process_frame_(encoder, false)) /* false => not last frame */
1442
 
                                                return false;
1443
 
                                }
1444
 
                        } while(j < samples);
1445
 
                }
1446
 
        }
1447
 
        else {
1448
 
                if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1449
 
                        /*
1450
 
                         * stereo coding: unroll channel loop
1451
 
                         * without LPC: no need to calculate floating point version of signal
1452
 
                         */
1453
 
                        do {
1454
 
                                if(encoder->protected_->verify)
1455
 
                                        append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1456
 
 
1457
 
                                for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1458
 
                                        encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1459
 
                                        x = buffer[1][j];
1460
 
                                        encoder->private_->integer_signal[1][i] = x;
1461
 
                                        mid += x;
1462
 
                                        side -= x;
1463
 
                                        mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1464
 
                                        encoder->private_->integer_signal_mid_side[1][i] = side;
1465
 
                                        encoder->private_->integer_signal_mid_side[0][i] = mid;
1466
 
                                        encoder->private_->current_sample_number++;
1467
 
                                }
1468
 
                                if(i == blocksize) {
1469
 
                                        if(!process_frame_(encoder, false)) /* false => not last frame */
1470
 
                                                return false;
1471
 
                                }
1472
 
                        } while(j < samples);
1473
 
                }
1474
 
                else {
1475
 
                        /*
1476
 
                         * independent channel coding: buffer each channel in inner loop
1477
 
                         * without LPC: no need to calculate floating point version of signal
1478
 
                         */
1479
 
                        do {
1480
 
                                if(encoder->protected_->verify)
1481
 
                                        append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1482
 
 
1483
 
                                for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1484
 
                                        for(channel = 0; channel < channels; channel++)
1485
 
                                                encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1486
 
                                        encoder->private_->current_sample_number++;
1487
 
                                }
1488
 
                                if(i == blocksize) {
1489
 
                                        if(!process_frame_(encoder, false)) /* false => not last frame */
1490
 
                                                return false;
1491
 
                                }
1492
 
                        } while(j < samples);
1493
 
                }
1494
 
        }
 
1989
        do {
 
1990
                const unsigned n = min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j);
 
1991
 
 
1992
                if(encoder->protected_->verify)
 
1993
                        append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n);
 
1994
 
 
1995
                for(channel = 0; channel < channels; channel++)
 
1996
                        memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n);
 
1997
 
 
1998
                if(encoder->protected_->do_mid_side_stereo) {
 
1999
                        FLAC__ASSERT(channels == 2);
 
2000
                        /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
 
2001
                        for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
 
2002
                                encoder->private_->integer_signal_mid_side[1][i] = buffer[0][j] - buffer[1][j];
 
2003
                                encoder->private_->integer_signal_mid_side[0][i] = (buffer[0][j] + buffer[1][j]) >> 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
 
2004
                        }
 
2005
                }
 
2006
                else
 
2007
                        j += n;
 
2008
 
 
2009
                encoder->private_->current_sample_number += n;
 
2010
 
 
2011
                /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
 
2012
                if(encoder->private_->current_sample_number > blocksize) {
 
2013
                        FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_);
 
2014
                        FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
 
2015
                        if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
 
2016
                                return false;
 
2017
                        /* move unprocessed overread samples to beginnings of arrays */
 
2018
                        for(channel = 0; channel < channels; channel++)
 
2019
                                encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
 
2020
                        if(encoder->protected_->do_mid_side_stereo) {
 
2021
                                encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
 
2022
                                encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
 
2023
                        }
 
2024
                        encoder->private_->current_sample_number = 1;
 
2025
                }
 
2026
        } while(j < samples);
1495
2027
 
1496
2028
        return true;
1497
2029
}
1503
2035
        const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1504
2036
 
1505
2037
        FLAC__ASSERT(0 != encoder);
 
2038
        FLAC__ASSERT(0 != encoder->private_);
 
2039
        FLAC__ASSERT(0 != encoder->protected_);
1506
2040
        FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1507
2041
 
1508
2042
        j = k = 0;
1510
2044
         * we have several flavors of the same basic loop, optimized for
1511
2045
         * different conditions:
1512
2046
         */
1513
 
        if(encoder->protected_->max_lpc_order > 0) {
1514
 
                if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1515
 
                        /*
1516
 
                         * stereo coding: unroll channel loop
1517
 
                         * with LPC: calculate floating point version of signal
1518
 
                         */
1519
 
                        do {
1520
 
                                if(encoder->protected_->verify)
1521
 
                                        append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1522
 
 
1523
 
                                for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1524
 
                                        x = mid = side = buffer[k++];
1525
 
                                        encoder->private_->integer_signal[0][i] = x;
1526
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1527
 
                                        encoder->private_->real_signal[0][i] = (FLAC__real)x;
1528
 
#endif
1529
 
                                        x = buffer[k++];
1530
 
                                        encoder->private_->integer_signal[1][i] = x;
1531
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1532
 
                                        encoder->private_->real_signal[1][i] = (FLAC__real)x;
1533
 
#endif
1534
 
                                        mid += x;
1535
 
                                        side -= x;
1536
 
                                        mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1537
 
                                        encoder->private_->integer_signal_mid_side[1][i] = side;
1538
 
                                        encoder->private_->integer_signal_mid_side[0][i] = mid;
1539
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1540
 
                                        encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1541
 
                                        encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1542
 
#endif
1543
 
                                        encoder->private_->current_sample_number++;
1544
 
                                }
1545
 
                                if(i == blocksize) {
1546
 
                                        if(!process_frame_(encoder, false)) /* false => not last frame */
1547
 
                                                return false;
1548
 
                                }
1549
 
                        } while(j < samples);
1550
 
                }
1551
 
                else {
1552
 
                        /*
1553
 
                         * independent channel coding: buffer each channel in inner loop
1554
 
                         * with LPC: calculate floating point version of signal
1555
 
                         */
1556
 
                        do {
1557
 
                                if(encoder->protected_->verify)
1558
 
                                        append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1559
 
 
1560
 
                                for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1561
 
                                        for(channel = 0; channel < channels; channel++) {
1562
 
                                                x = buffer[k++];
1563
 
                                                encoder->private_->integer_signal[channel][i] = x;
1564
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1565
 
                                                encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1566
 
#endif
1567
 
                                        }
1568
 
                                        encoder->private_->current_sample_number++;
1569
 
                                }
1570
 
                                if(i == blocksize) {
1571
 
                                        if(!process_frame_(encoder, false)) /* false => not last frame */
1572
 
                                                return false;
1573
 
                                }
1574
 
                        } while(j < samples);
1575
 
                }
 
2047
        if(encoder->protected_->do_mid_side_stereo && channels == 2) {
 
2048
                /*
 
2049
                 * stereo coding: unroll channel loop
 
2050
                 */
 
2051
                do {
 
2052
                        if(encoder->protected_->verify)
 
2053
                                append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
 
2054
 
 
2055
                        /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
 
2056
                        for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
 
2057
                                encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
 
2058
                                x = buffer[k++];
 
2059
                                encoder->private_->integer_signal[1][i] = x;
 
2060
                                mid += x;
 
2061
                                side -= x;
 
2062
                                mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
 
2063
                                encoder->private_->integer_signal_mid_side[1][i] = side;
 
2064
                                encoder->private_->integer_signal_mid_side[0][i] = mid;
 
2065
                        }
 
2066
                        encoder->private_->current_sample_number = i;
 
2067
                        /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
 
2068
                        if(i > blocksize) {
 
2069
                                if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
 
2070
                                        return false;
 
2071
                                /* move unprocessed overread samples to beginnings of arrays */
 
2072
                                FLAC__ASSERT(i == blocksize+OVERREAD_);
 
2073
                                FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
 
2074
                                encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][blocksize];
 
2075
                                encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][blocksize];
 
2076
                                encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
 
2077
                                encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
 
2078
                                encoder->private_->current_sample_number = 1;
 
2079
                        }
 
2080
                } while(j < samples);
1576
2081
        }
1577
2082
        else {
1578
 
                if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1579
 
                        /*
1580
 
                         * stereo coding: unroll channel loop
1581
 
                         * without LPC: no need to calculate floating point version of signal
1582
 
                         */
1583
 
                        do {
1584
 
                                if(encoder->protected_->verify)
1585
 
                                        append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1586
 
 
1587
 
                                for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1588
 
                                        encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
1589
 
                                        x = buffer[k++];
1590
 
                                        encoder->private_->integer_signal[1][i] = x;
1591
 
                                        mid += x;
1592
 
                                        side -= x;
1593
 
                                        mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1594
 
                                        encoder->private_->integer_signal_mid_side[1][i] = side;
1595
 
                                        encoder->private_->integer_signal_mid_side[0][i] = mid;
1596
 
                                        encoder->private_->current_sample_number++;
1597
 
                                }
1598
 
                                if(i == blocksize) {
1599
 
                                        if(!process_frame_(encoder, false)) /* false => not last frame */
1600
 
                                                return false;
1601
 
                                }
1602
 
                        } while(j < samples);
1603
 
                }
1604
 
                else {
1605
 
                        /*
1606
 
                         * independent channel coding: buffer each channel in inner loop
1607
 
                         * without LPC: no need to calculate floating point version of signal
1608
 
                         */
1609
 
                        do {
1610
 
                                if(encoder->protected_->verify)
1611
 
                                        append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1612
 
 
1613
 
                                for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1614
 
                                        for(channel = 0; channel < channels; channel++)
1615
 
                                                encoder->private_->integer_signal[channel][i] = buffer[k++];
1616
 
                                        encoder->private_->current_sample_number++;
1617
 
                                }
1618
 
                                if(i == blocksize) {
1619
 
                                        if(!process_frame_(encoder, false)) /* false => not last frame */
1620
 
                                                return false;
1621
 
                                }
1622
 
                        } while(j < samples);
1623
 
                }
 
2083
                /*
 
2084
                 * independent channel coding: buffer each channel in inner loop
 
2085
                 */
 
2086
                do {
 
2087
                        if(encoder->protected_->verify)
 
2088
                                append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
 
2089
 
 
2090
                        /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
 
2091
                        for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
 
2092
                                for(channel = 0; channel < channels; channel++)
 
2093
                                        encoder->private_->integer_signal[channel][i] = buffer[k++];
 
2094
                        }
 
2095
                        encoder->private_->current_sample_number = i;
 
2096
                        /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
 
2097
                        if(i > blocksize) {
 
2098
                                if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
 
2099
                                        return false;
 
2100
                                /* move unprocessed overread samples to beginnings of arrays */
 
2101
                                FLAC__ASSERT(i == blocksize+OVERREAD_);
 
2102
                                FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
 
2103
                                for(channel = 0; channel < channels; channel++)
 
2104
                                        encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
 
2105
                                encoder->private_->current_sample_number = 1;
 
2106
                        }
 
2107
                } while(j < samples);
1624
2108
        }
1625
2109
 
1626
2110
        return true;
1642
2126
        encoder->protected_->verify = false;
1643
2127
#endif
1644
2128
        encoder->protected_->streamable_subset = true;
 
2129
        encoder->protected_->do_md5 = true;
1645
2130
        encoder->protected_->do_mid_side_stereo = false;
1646
2131
        encoder->protected_->loose_mid_side_stereo = false;
1647
2132
        encoder->protected_->channels = 2;
1648
2133
        encoder->protected_->bits_per_sample = 16;
1649
2134
        encoder->protected_->sample_rate = 44100;
1650
 
        encoder->protected_->blocksize = 1152;
 
2135
        encoder->protected_->blocksize = 0;
 
2136
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
2137
        encoder->protected_->num_apodizations = 1;
 
2138
        encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
 
2139
        encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
 
2140
#endif
1651
2141
        encoder->protected_->max_lpc_order = 0;
1652
2142
        encoder->protected_->qlp_coeff_precision = 0;
1653
2143
        encoder->protected_->do_qlp_coeff_prec_search = false;
1660
2150
        encoder->protected_->metadata = 0;
1661
2151
        encoder->protected_->num_metadata_blocks = 0;
1662
2152
 
 
2153
        encoder->private_->seek_table = 0;
1663
2154
        encoder->private_->disable_constant_subframes = false;
1664
2155
        encoder->private_->disable_fixed_subframes = false;
1665
2156
        encoder->private_->disable_verbatim_subframes = false;
 
2157
#if FLAC__HAS_OGG
 
2158
        encoder->private_->is_ogg = false;
 
2159
#endif
 
2160
        encoder->private_->read_callback = 0;
1666
2161
        encoder->private_->write_callback = 0;
 
2162
        encoder->private_->seek_callback = 0;
 
2163
        encoder->private_->tell_callback = 0;
1667
2164
        encoder->private_->metadata_callback = 0;
 
2165
        encoder->private_->progress_callback = 0;
1668
2166
        encoder->private_->client_data = 0;
 
2167
 
 
2168
#if FLAC__HAS_OGG
 
2169
        FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
 
2170
#endif
1669
2171
}
1670
2172
 
1671
2173
void free_(FLAC__StreamEncoder *encoder)
1673
2175
        unsigned i, channel;
1674
2176
 
1675
2177
        FLAC__ASSERT(0 != encoder);
 
2178
        if(encoder->protected_->metadata) {
 
2179
                free(encoder->protected_->metadata);
 
2180
                encoder->protected_->metadata = 0;
 
2181
                encoder->protected_->num_metadata_blocks = 0;
 
2182
        }
1676
2183
        for(i = 0; i < encoder->protected_->channels; i++) {
1677
2184
                if(0 != encoder->private_->integer_signal_unaligned[i]) {
1678
2185
                        free(encoder->private_->integer_signal_unaligned[i]);
1697
2204
                }
1698
2205
#endif
1699
2206
        }
 
2207
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
2208
        for(i = 0; i < encoder->protected_->num_apodizations; i++) {
 
2209
                if(0 != encoder->private_->window_unaligned[i]) {
 
2210
                        free(encoder->private_->window_unaligned[i]);
 
2211
                        encoder->private_->window_unaligned[i] = 0;
 
2212
                }
 
2213
        }
 
2214
        if(0 != encoder->private_->windowed_signal_unaligned) {
 
2215
                free(encoder->private_->windowed_signal_unaligned);
 
2216
                encoder->private_->windowed_signal_unaligned = 0;
 
2217
        }
 
2218
#endif
1700
2219
        for(channel = 0; channel < encoder->protected_->channels; channel++) {
1701
2220
                for(i = 0; i < 2; i++) {
1702
2221
                        if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
1713
2232
                        }
1714
2233
                }
1715
2234
        }
1716
 
        if(0 != encoder->private_->abs_residual_unaligned) {
1717
 
                free(encoder->private_->abs_residual_unaligned);
1718
 
                encoder->private_->abs_residual_unaligned = 0;
1719
 
        }
1720
2235
        if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
1721
2236
                free(encoder->private_->abs_residual_partition_sums_unaligned);
1722
2237
                encoder->private_->abs_residual_partition_sums_unaligned = 0;
1733
2248
                        }
1734
2249
                }
1735
2250
        }
1736
 
        FLAC__bitbuffer_free(encoder->private_->frame);
 
2251
        FLAC__bitwriter_free(encoder->private_->frame);
1737
2252
}
1738
2253
 
1739
 
FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
 
2254
FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
1740
2255
{
1741
2256
        FLAC__bool ok;
1742
2257
        unsigned i, channel;
1743
2258
 
1744
 
        FLAC__ASSERT(new_size > 0);
 
2259
        FLAC__ASSERT(new_blocksize > 0);
1745
2260
        FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1746
2261
        FLAC__ASSERT(encoder->private_->current_sample_number == 0);
1747
2262
 
1748
2263
        /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
1749
 
        if(new_size <= encoder->private_->input_capacity)
 
2264
        if(new_blocksize <= encoder->private_->input_capacity)
1750
2265
                return true;
1751
2266
 
1752
2267
        ok = true;
1754
2269
        /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1755
2270
         * requires that the input arrays (in our case the integer signals)
1756
2271
         * have a buffer of up to 3 zeroes in front (at negative indices) for
1757
 
         * alignment purposes; we use 4 to keep the data well-aligned.
 
2272
         * alignment purposes; we use 4 in front to keep the data well-aligned.
1758
2273
         */
1759
2274
 
1760
2275
        for(i = 0; ok && i < encoder->protected_->channels; i++) {
1761
 
                ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1762
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1763
 
                if(encoder->protected_->max_lpc_order > 0)
1764
 
                        ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1765
 
#endif
 
2276
                ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1766
2277
                memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1767
2278
                encoder->private_->integer_signal[i] += 4;
 
2279
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
2280
#if 0 /* @@@ currently unused */
 
2281
                if(encoder->protected_->max_lpc_order > 0)
 
2282
                        ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
 
2283
#endif
 
2284
#endif
1768
2285
        }
1769
2286
        for(i = 0; ok && i < 2; i++) {
1770
 
                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]);
1771
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1772
 
                if(encoder->protected_->max_lpc_order > 0)
1773
 
                        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]);
1774
 
#endif
 
2287
                ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
1775
2288
                memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1776
2289
                encoder->private_->integer_signal_mid_side[i] += 4;
1777
 
        }
 
2290
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
2291
#if 0 /* @@@ currently unused */
 
2292
                if(encoder->protected_->max_lpc_order > 0)
 
2293
                        ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
 
2294
#endif
 
2295
#endif
 
2296
        }
 
2297
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
2298
        if(ok && encoder->protected_->max_lpc_order > 0) {
 
2299
                for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
 
2300
                        ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
 
2301
                ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
 
2302
        }
 
2303
#endif
1778
2304
        for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1779
2305
                for(i = 0; ok && i < 2; i++) {
1780
 
                        ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
 
2306
                        ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1781
2307
                }
1782
2308
        }
1783
2309
        for(channel = 0; ok && channel < 2; channel++) {
1784
2310
                for(i = 0; ok && i < 2; i++) {
1785
 
                        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]);
 
2311
                        ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
1786
2312
                }
1787
2313
        }
1788
 
        ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1789
 
        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 */
1790
 
                ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
 
2314
        /* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
 
2315
        /*@@@ new_blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions; would require moving this out into a separate function, then checking its capacity against the need of the current blocksize&min/max_partition_order (and maybe predictor order) */
 
2316
        ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1791
2317
        if(encoder->protected_->do_escape_coding)
1792
 
                ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
 
2318
                ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
 
2319
 
 
2320
        /* now adjust the windows if the blocksize has changed */
 
2321
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
2322
        if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
 
2323
                for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
 
2324
                        switch(encoder->protected_->apodizations[i].type) {
 
2325
                                case FLAC__APODIZATION_BARTLETT:
 
2326
                                        FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
 
2327
                                        break;
 
2328
                                case FLAC__APODIZATION_BARTLETT_HANN:
 
2329
                                        FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
 
2330
                                        break;
 
2331
                                case FLAC__APODIZATION_BLACKMAN:
 
2332
                                        FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
 
2333
                                        break;
 
2334
                                case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
 
2335
                                        FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
 
2336
                                        break;
 
2337
                                case FLAC__APODIZATION_CONNES:
 
2338
                                        FLAC__window_connes(encoder->private_->window[i], new_blocksize);
 
2339
                                        break;
 
2340
                                case FLAC__APODIZATION_FLATTOP:
 
2341
                                        FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
 
2342
                                        break;
 
2343
                                case FLAC__APODIZATION_GAUSS:
 
2344
                                        FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
 
2345
                                        break;
 
2346
                                case FLAC__APODIZATION_HAMMING:
 
2347
                                        FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
 
2348
                                        break;
 
2349
                                case FLAC__APODIZATION_HANN:
 
2350
                                        FLAC__window_hann(encoder->private_->window[i], new_blocksize);
 
2351
                                        break;
 
2352
                                case FLAC__APODIZATION_KAISER_BESSEL:
 
2353
                                        FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
 
2354
                                        break;
 
2355
                                case FLAC__APODIZATION_NUTTALL:
 
2356
                                        FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
 
2357
                                        break;
 
2358
                                case FLAC__APODIZATION_RECTANGLE:
 
2359
                                        FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
 
2360
                                        break;
 
2361
                                case FLAC__APODIZATION_TRIANGLE:
 
2362
                                        FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
 
2363
                                        break;
 
2364
                                case FLAC__APODIZATION_TUKEY:
 
2365
                                        FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
 
2366
                                        break;
 
2367
                                case FLAC__APODIZATION_WELCH:
 
2368
                                        FLAC__window_welch(encoder->private_->window[i], new_blocksize);
 
2369
                                        break;
 
2370
                                default:
 
2371
                                        FLAC__ASSERT(0);
 
2372
                                        /* double protection */
 
2373
                                        FLAC__window_hann(encoder->private_->window[i], new_blocksize);
 
2374
                                        break;
 
2375
                        }
 
2376
                }
 
2377
        }
 
2378
#endif
1793
2379
 
1794
2380
        if(ok)
1795
 
                encoder->private_->input_capacity = new_size;
 
2381
                encoder->private_->input_capacity = new_blocksize;
1796
2382
        else
1797
2383
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1798
2384
 
1799
2385
        return ok;
1800
2386
}
1801
2387
 
1802
 
FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
 
2388
FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
1803
2389
{
1804
2390
        const FLAC__byte *buffer;
1805
 
        unsigned bytes;
1806
 
 
1807
 
        FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1808
 
 
1809
 
        FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
 
2391
        size_t bytes;
 
2392
 
 
2393
        FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
 
2394
 
 
2395
        if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
 
2396
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
 
2397
                return false;
 
2398
        }
1810
2399
 
1811
2400
        if(encoder->protected_->verify) {
1812
2401
                encoder->private_->verify.output.data = buffer;
1816
2405
                }
1817
2406
                else {
1818
2407
                        if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
1819
 
                                FLAC__bitbuffer_release_buffer(encoder->private_->frame);
 
2408
                                FLAC__bitwriter_release_buffer(encoder->private_->frame);
 
2409
                                FLAC__bitwriter_clear(encoder->private_->frame);
1820
2410
                                if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1821
2411
                                        encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1822
2412
                                return false;
1824
2414
                }
1825
2415
        }
1826
2416
 
1827
 
        if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1828
 
                FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1829
 
                encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
 
2417
        if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
 
2418
                FLAC__bitwriter_release_buffer(encoder->private_->frame);
 
2419
                FLAC__bitwriter_clear(encoder->private_->frame);
 
2420
                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1830
2421
                return false;
1831
2422
        }
1832
2423
 
1833
 
        FLAC__bitbuffer_release_buffer(encoder->private_->frame);
 
2424
        FLAC__bitwriter_release_buffer(encoder->private_->frame);
 
2425
        FLAC__bitwriter_clear(encoder->private_->frame);
1834
2426
 
1835
2427
        if(samples > 0) {
1836
 
                encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1837
 
                encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
 
2428
                encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
 
2429
                encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
1838
2430
        }
1839
2431
 
1840
2432
        return true;
1841
2433
}
1842
2434
 
1843
 
FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
1844
 
{
 
2435
FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
 
2436
{
 
2437
        FLAC__StreamEncoderWriteStatus status;
 
2438
        FLAC__uint64 output_position = 0;
 
2439
 
 
2440
        /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
 
2441
        if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
 
2442
                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2443
                return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
 
2444
        }
 
2445
 
 
2446
        /*
 
2447
         * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
 
2448
         */
 
2449
        if(samples == 0) {
 
2450
                FLAC__MetadataType type = (buffer[0] & 0x7f);
 
2451
                if(type == FLAC__METADATA_TYPE_STREAMINFO)
 
2452
                        encoder->protected_->streaminfo_offset = output_position;
 
2453
                else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
 
2454
                        encoder->protected_->seektable_offset = output_position;
 
2455
        }
 
2456
 
 
2457
        /*
 
2458
         * Mark the current seek point if hit (if audio_offset == 0 that
 
2459
         * means we're still writing metadata and haven't hit the first
 
2460
         * frame yet)
 
2461
         */
 
2462
        if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
 
2463
                const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
 
2464
                const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
 
2465
                const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
 
2466
                FLAC__uint64 test_sample;
 
2467
                unsigned i;
 
2468
                for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
 
2469
                        test_sample = encoder->private_->seek_table->points[i].sample_number;
 
2470
                        if(test_sample > frame_last_sample) {
 
2471
                                break;
 
2472
                        }
 
2473
                        else if(test_sample >= frame_first_sample) {
 
2474
                                encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
 
2475
                                encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
 
2476
                                encoder->private_->seek_table->points[i].frame_samples = blocksize;
 
2477
                                encoder->private_->first_seekpoint_to_check++;
 
2478
                                /* DO NOT: "break;" and here's why:
 
2479
                                 * The seektable template may contain more than one target
 
2480
                                 * sample for any given frame; we will keep looping, generating
 
2481
                                 * duplicate seekpoints for them, and we'll clean it up later,
 
2482
                                 * just before writing the seektable back to the metadata.
 
2483
                                 */
 
2484
                        }
 
2485
                        else {
 
2486
                                encoder->private_->first_seekpoint_to_check++;
 
2487
                        }
 
2488
                }
 
2489
        }
 
2490
 
 
2491
#if FLAC__HAS_OGG
 
2492
        if(encoder->private_->is_ogg) {
 
2493
                status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
 
2494
                        &encoder->protected_->ogg_encoder_aspect,
 
2495
                        buffer,
 
2496
                        bytes,
 
2497
                        samples,
 
2498
                        encoder->private_->current_frame_number,
 
2499
                        is_last_block,
 
2500
                        (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
 
2501
                        encoder,
 
2502
                        encoder->private_->client_data
 
2503
                );
 
2504
        }
 
2505
        else
 
2506
#endif
 
2507
        status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
 
2508
 
 
2509
        if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
 
2510
                encoder->private_->bytes_written += bytes;
 
2511
                encoder->private_->samples_written += samples;
 
2512
                /* we keep a high watermark on the number of frames written because
 
2513
                 * when the encoder goes back to write metadata, 'current_frame'
 
2514
                 * will drop back to 0.
 
2515
                 */
 
2516
                encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
 
2517
        }
 
2518
        else
 
2519
                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2520
 
 
2521
        return status;
 
2522
}
 
2523
 
 
2524
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks.  */
 
2525
void update_metadata_(const FLAC__StreamEncoder *encoder)
 
2526
{
 
2527
        FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
 
2528
        const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
 
2529
        const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
 
2530
        const unsigned min_framesize = metadata->data.stream_info.min_framesize;
 
2531
        const unsigned max_framesize = metadata->data.stream_info.max_framesize;
 
2532
        const unsigned bps = metadata->data.stream_info.bits_per_sample;
 
2533
        FLAC__StreamEncoderSeekStatus seek_status;
 
2534
 
 
2535
        FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
 
2536
 
 
2537
        /* All this is based on intimate knowledge of the stream header
 
2538
         * layout, but a change to the header format that would break this
 
2539
         * would also break all streams encoded in the previous format.
 
2540
         */
 
2541
 
 
2542
        /*
 
2543
         * Write MD5 signature
 
2544
         */
 
2545
        {
 
2546
                const unsigned md5_offset =
 
2547
                        FLAC__STREAM_METADATA_HEADER_LENGTH +
 
2548
                        (
 
2549
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
 
2550
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
 
2551
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
 
2552
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
 
2553
                                FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
 
2554
                                FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
 
2555
                                FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
 
2556
                                FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
 
2557
                        ) / 8;
 
2558
 
 
2559
                if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
 
2560
                        if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
 
2561
                                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2562
                        return;
 
2563
                }
 
2564
                if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
 
2565
                        encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2566
                        return;
 
2567
                }
 
2568
        }
 
2569
 
 
2570
        /*
 
2571
         * Write total samples
 
2572
         */
 
2573
        {
 
2574
                const unsigned total_samples_byte_offset =
 
2575
                        FLAC__STREAM_METADATA_HEADER_LENGTH +
 
2576
                        (
 
2577
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
 
2578
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
 
2579
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
 
2580
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
 
2581
                                FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
 
2582
                                FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
 
2583
                                FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
 
2584
                                - 4
 
2585
                        ) / 8;
 
2586
 
 
2587
                b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
 
2588
                b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
 
2589
                b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
 
2590
                b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
 
2591
                b[4] = (FLAC__byte)(samples & 0xFF);
 
2592
                if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + total_samples_byte_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
 
2593
                        if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
 
2594
                                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2595
                        return;
 
2596
                }
 
2597
                if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
 
2598
                        encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2599
                        return;
 
2600
                }
 
2601
        }
 
2602
 
 
2603
        /*
 
2604
         * Write min/max framesize
 
2605
         */
 
2606
        {
 
2607
                const unsigned min_framesize_offset =
 
2608
                        FLAC__STREAM_METADATA_HEADER_LENGTH +
 
2609
                        (
 
2610
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
 
2611
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
 
2612
                        ) / 8;
 
2613
 
 
2614
                b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
 
2615
                b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
 
2616
                b[2] = (FLAC__byte)(min_framesize & 0xFF);
 
2617
                b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
 
2618
                b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
 
2619
                b[5] = (FLAC__byte)(max_framesize & 0xFF);
 
2620
                if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + min_framesize_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
 
2621
                        if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
 
2622
                                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2623
                        return;
 
2624
                }
 
2625
                if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
 
2626
                        encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2627
                        return;
 
2628
                }
 
2629
        }
 
2630
 
 
2631
        /*
 
2632
         * Write seektable
 
2633
         */
 
2634
        if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
 
2635
                unsigned i;
 
2636
 
 
2637
                FLAC__format_seektable_sort(encoder->private_->seek_table);
 
2638
 
 
2639
                FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
 
2640
 
 
2641
                if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
 
2642
                        if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
 
2643
                                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2644
                        return;
 
2645
                }
 
2646
 
 
2647
                for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
 
2648
                        FLAC__uint64 xx;
 
2649
                        unsigned x;
 
2650
                        xx = encoder->private_->seek_table->points[i].sample_number;
 
2651
                        b[7] = (FLAC__byte)xx; xx >>= 8;
 
2652
                        b[6] = (FLAC__byte)xx; xx >>= 8;
 
2653
                        b[5] = (FLAC__byte)xx; xx >>= 8;
 
2654
                        b[4] = (FLAC__byte)xx; xx >>= 8;
 
2655
                        b[3] = (FLAC__byte)xx; xx >>= 8;
 
2656
                        b[2] = (FLAC__byte)xx; xx >>= 8;
 
2657
                        b[1] = (FLAC__byte)xx; xx >>= 8;
 
2658
                        b[0] = (FLAC__byte)xx; xx >>= 8;
 
2659
                        xx = encoder->private_->seek_table->points[i].stream_offset;
 
2660
                        b[15] = (FLAC__byte)xx; xx >>= 8;
 
2661
                        b[14] = (FLAC__byte)xx; xx >>= 8;
 
2662
                        b[13] = (FLAC__byte)xx; xx >>= 8;
 
2663
                        b[12] = (FLAC__byte)xx; xx >>= 8;
 
2664
                        b[11] = (FLAC__byte)xx; xx >>= 8;
 
2665
                        b[10] = (FLAC__byte)xx; xx >>= 8;
 
2666
                        b[9] = (FLAC__byte)xx; xx >>= 8;
 
2667
                        b[8] = (FLAC__byte)xx; xx >>= 8;
 
2668
                        x = encoder->private_->seek_table->points[i].frame_samples;
 
2669
                        b[17] = (FLAC__byte)x; x >>= 8;
 
2670
                        b[16] = (FLAC__byte)x; x >>= 8;
 
2671
                        if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
 
2672
                                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
 
2673
                                return;
 
2674
                        }
 
2675
                }
 
2676
        }
 
2677
}
 
2678
 
 
2679
#if FLAC__HAS_OGG
 
2680
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks.  */
 
2681
void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
 
2682
{
 
2683
        /* the # of bytes in the 1st packet that precede the STREAMINFO */
 
2684
        static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
 
2685
                FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
 
2686
                FLAC__OGG_MAPPING_MAGIC_LENGTH +
 
2687
                FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
 
2688
                FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
 
2689
                FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
 
2690
                FLAC__STREAM_SYNC_LENGTH
 
2691
        ;
 
2692
        FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
 
2693
        const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
 
2694
        const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
 
2695
        const unsigned min_framesize = metadata->data.stream_info.min_framesize;
 
2696
        const unsigned max_framesize = metadata->data.stream_info.max_framesize;
 
2697
        ogg_page page;
 
2698
 
 
2699
        FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
 
2700
        FLAC__ASSERT(0 != encoder->private_->seek_callback);
 
2701
 
 
2702
        /* Pre-check that client supports seeking, since we don't want the
 
2703
         * ogg_helper code to ever have to deal with this condition.
 
2704
         */
 
2705
        if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
 
2706
                return;
 
2707
 
 
2708
        /* All this is based on intimate knowledge of the stream header
 
2709
         * layout, but a change to the header format that would break this
 
2710
         * would also break all streams encoded in the previous format.
 
2711
         */
 
2712
 
 
2713
        /**
 
2714
         ** Write STREAMINFO stats
 
2715
         **/
 
2716
        simple_ogg_page__init(&page);
 
2717
        if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
 
2718
                simple_ogg_page__clear(&page);
 
2719
                return; /* state already set */
 
2720
        }
 
2721
 
 
2722
        /*
 
2723
         * Write MD5 signature
 
2724
         */
 
2725
        {
 
2726
                const unsigned md5_offset =
 
2727
                        FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
 
2728
                        FLAC__STREAM_METADATA_HEADER_LENGTH +
 
2729
                        (
 
2730
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
 
2731
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
 
2732
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
 
2733
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
 
2734
                                FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
 
2735
                                FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
 
2736
                                FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
 
2737
                                FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
 
2738
                        ) / 8;
 
2739
 
 
2740
                if(md5_offset + 16 > (unsigned)page.body_len) {
 
2741
                        encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
 
2742
                        simple_ogg_page__clear(&page);
 
2743
                        return;
 
2744
                }
 
2745
                memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
 
2746
        }
 
2747
 
 
2748
        /*
 
2749
         * Write total samples
 
2750
         */
 
2751
        {
 
2752
                const unsigned total_samples_byte_offset =
 
2753
                        FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
 
2754
                        FLAC__STREAM_METADATA_HEADER_LENGTH +
 
2755
                        (
 
2756
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
 
2757
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
 
2758
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
 
2759
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
 
2760
                                FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
 
2761
                                FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
 
2762
                                FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
 
2763
                                - 4
 
2764
                        ) / 8;
 
2765
 
 
2766
                if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
 
2767
                        encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
 
2768
                        simple_ogg_page__clear(&page);
 
2769
                        return;
 
2770
                }
 
2771
                b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
 
2772
                b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
 
2773
                b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
 
2774
                b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
 
2775
                b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
 
2776
                b[4] = (FLAC__byte)(samples & 0xFF);
 
2777
                memcpy(page.body + total_samples_byte_offset, b, 5);
 
2778
        }
 
2779
 
 
2780
        /*
 
2781
         * Write min/max framesize
 
2782
         */
 
2783
        {
 
2784
                const unsigned min_framesize_offset =
 
2785
                        FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
 
2786
                        FLAC__STREAM_METADATA_HEADER_LENGTH +
 
2787
                        (
 
2788
                                FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
 
2789
                                FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
 
2790
                        ) / 8;
 
2791
 
 
2792
                if(min_framesize_offset + 6 > (unsigned)page.body_len) {
 
2793
                        encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
 
2794
                        simple_ogg_page__clear(&page);
 
2795
                        return;
 
2796
                }
 
2797
                b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
 
2798
                b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
 
2799
                b[2] = (FLAC__byte)(min_framesize & 0xFF);
 
2800
                b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
 
2801
                b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
 
2802
                b[5] = (FLAC__byte)(max_framesize & 0xFF);
 
2803
                memcpy(page.body + min_framesize_offset, b, 6);
 
2804
        }
 
2805
        if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
 
2806
                simple_ogg_page__clear(&page);
 
2807
                return; /* state already set */
 
2808
        }
 
2809
        simple_ogg_page__clear(&page);
 
2810
 
 
2811
        /*
 
2812
         * Write seektable
 
2813
         */
 
2814
        if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
 
2815
                unsigned i;
 
2816
                FLAC__byte *p;
 
2817
 
 
2818
                FLAC__format_seektable_sort(encoder->private_->seek_table);
 
2819
 
 
2820
                FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
 
2821
 
 
2822
                simple_ogg_page__init(&page);
 
2823
                if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
 
2824
                        simple_ogg_page__clear(&page);
 
2825
                        return; /* state already set */
 
2826
                }
 
2827
 
 
2828
                if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
 
2829
                        encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
 
2830
                        simple_ogg_page__clear(&page);
 
2831
                        return;
 
2832
                }
 
2833
 
 
2834
                for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
 
2835
                        FLAC__uint64 xx;
 
2836
                        unsigned x;
 
2837
                        xx = encoder->private_->seek_table->points[i].sample_number;
 
2838
                        b[7] = (FLAC__byte)xx; xx >>= 8;
 
2839
                        b[6] = (FLAC__byte)xx; xx >>= 8;
 
2840
                        b[5] = (FLAC__byte)xx; xx >>= 8;
 
2841
                        b[4] = (FLAC__byte)xx; xx >>= 8;
 
2842
                        b[3] = (FLAC__byte)xx; xx >>= 8;
 
2843
                        b[2] = (FLAC__byte)xx; xx >>= 8;
 
2844
                        b[1] = (FLAC__byte)xx; xx >>= 8;
 
2845
                        b[0] = (FLAC__byte)xx; xx >>= 8;
 
2846
                        xx = encoder->private_->seek_table->points[i].stream_offset;
 
2847
                        b[15] = (FLAC__byte)xx; xx >>= 8;
 
2848
                        b[14] = (FLAC__byte)xx; xx >>= 8;
 
2849
                        b[13] = (FLAC__byte)xx; xx >>= 8;
 
2850
                        b[12] = (FLAC__byte)xx; xx >>= 8;
 
2851
                        b[11] = (FLAC__byte)xx; xx >>= 8;
 
2852
                        b[10] = (FLAC__byte)xx; xx >>= 8;
 
2853
                        b[9] = (FLAC__byte)xx; xx >>= 8;
 
2854
                        b[8] = (FLAC__byte)xx; xx >>= 8;
 
2855
                        x = encoder->private_->seek_table->points[i].frame_samples;
 
2856
                        b[17] = (FLAC__byte)x; x >>= 8;
 
2857
                        b[16] = (FLAC__byte)x; x >>= 8;
 
2858
                        memcpy(p, b, 18);
 
2859
                }
 
2860
 
 
2861
                if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
 
2862
                        simple_ogg_page__clear(&page);
 
2863
                        return; /* state already set */
 
2864
                }
 
2865
                simple_ogg_page__clear(&page);
 
2866
        }
 
2867
}
 
2868
#endif
 
2869
 
 
2870
FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
 
2871
{
 
2872
        FLAC__uint16 crc;
1845
2873
        FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1846
2874
 
1847
2875
        /*
1848
2876
         * Accumulate raw signal to the MD5 signature
1849
2877
         */
1850
 
        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)) {
 
2878
        if(encoder->protected_->do_md5 && !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)) {
1851
2879
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1852
2880
                return false;
1853
2881
        }
1855
2883
        /*
1856
2884
         * Process the frame header and subframes into the frame bitbuffer
1857
2885
         */
1858
 
        if(!process_subframes_(encoder, is_last_frame)) {
 
2886
        if(!process_subframes_(encoder, is_fractional_block)) {
1859
2887
                /* the above function sets the state for us in case of an error */
1860
2888
                return false;
1861
2889
        }
1863
2891
        /*
1864
2892
         * Zero-pad the frame to a byte_boundary
1865
2893
         */
1866
 
        if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
 
2894
        if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) {
1867
2895
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1868
2896
                return false;
1869
2897
        }
1871
2899
        /*
1872
2900
         * CRC-16 the whole thing
1873
2901
         */
1874
 
        FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1875
 
        FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
 
2902
        FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
 
2903
        if(
 
2904
                !FLAC__bitwriter_get_write_crc16(encoder->private_->frame, &crc) ||
 
2905
                !FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN)
 
2906
        ) {
 
2907
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
 
2908
                return false;
 
2909
        }
1876
2910
 
1877
2911
        /*
1878
2912
         * Write it
1879
2913
         */
1880
 
        if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
 
2914
        if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
1881
2915
                /* the above function sets the state for us in case of an error */
1882
2916
                return false;
1883
2917
        }
1887
2921
         */
1888
2922
        encoder->private_->current_sample_number = 0;
1889
2923
        encoder->private_->current_frame_number++;
1890
 
        encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
 
2924
        encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
1891
2925
 
1892
2926
        return true;
1893
2927
}
1894
2928
 
1895
 
FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
 
2929
FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
1896
2930
{
1897
2931
        FLAC__FrameHeader frame_header;
1898
2932
        unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
1899
 
        FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
 
2933
        FLAC__bool do_independent, do_mid_side;
1900
2934
 
1901
2935
        /*
1902
2936
         * Calculate the min,max Rice partition orders
1903
2937
         */
1904
 
        if(is_last_frame) {
 
2938
        if(is_fractional_block) {
1905
2939
                max_partition_order = 0;
1906
2940
        }
1907
2941
        else {
1910
2944
        }
1911
2945
        min_partition_order = min(min_partition_order, max_partition_order);
1912
2946
 
1913
 
        precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
1914
 
 
1915
2947
        /*
1916
2948
         * Setup the frame
1917
2949
         */
1918
 
        if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
1919
 
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1920
 
                return false;
1921
 
        }
1922
2950
        frame_header.blocksize = encoder->protected_->blocksize;
1923
2951
        frame_header.sample_rate = encoder->protected_->sample_rate;
1924
2952
        frame_header.channels = encoder->protected_->channels;
1982
3010
                                        encoder,
1983
3011
                                        min_partition_order,
1984
3012
                                        max_partition_order,
1985
 
                                        precompute_partition_sums,
1986
3013
                                        &frame_header,
1987
3014
                                        encoder->private_->subframe_bps[channel],
1988
3015
                                        encoder->private_->integer_signal[channel],
1989
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1990
 
                                        encoder->private_->real_signal[channel],
1991
 
#endif
1992
3016
                                        encoder->private_->subframe_workspace_ptr[channel],
1993
3017
                                        encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
1994
3018
                                        encoder->private_->residual_workspace[channel],
2012
3036
                                        encoder,
2013
3037
                                        min_partition_order,
2014
3038
                                        max_partition_order,
2015
 
                                        precompute_partition_sums,
2016
3039
                                        &frame_header,
2017
3040
                                        encoder->private_->subframe_bps_mid_side[channel],
2018
3041
                                        encoder->private_->integer_signal_mid_side[channel],
2019
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2020
 
                                        encoder->private_->real_signal_mid_side[channel],
2021
 
#endif
2022
3042
                                        encoder->private_->subframe_workspace_ptr_mid_side[channel],
2023
3043
                                        encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
2024
3044
                                        encoder->private_->residual_workspace_mid_side[channel],
2046
3066
                else {
2047
3067
                        unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
2048
3068
                        unsigned min_bits;
2049
 
                        FLAC__ChannelAssignment ca;
 
3069
                        int ca;
2050
3070
 
 
3071
                        FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0);
 
3072
                        FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE   == 1);
 
3073
                        FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE  == 2);
 
3074
                        FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE    == 3);
2051
3075
                        FLAC__ASSERT(do_independent && do_mid_side);
2052
3076
 
2053
3077
                        /* We have to figure out which channel assignent results in the smallest frame */
2056
3080
                        bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits         [1] + encoder->private_->best_subframe_bits_mid_side[1];
2057
3081
                        bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
2058
3082
 
2059
 
                        for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
 
3083
                        channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
 
3084
                        min_bits = bits[channel_assignment];
 
3085
                        for(ca = 1; ca <= 3; ca++) {
2060
3086
                                if(bits[ca] < min_bits) {
2061
3087
                                        min_bits = bits[ca];
2062
 
                                        channel_assignment = ca;
 
3088
                                        channel_assignment = (FLAC__ChannelAssignment)ca;
2063
3089
                                }
2064
3090
                        }
2065
3091
                }
2066
3092
 
2067
3093
                frame_header.channel_assignment = channel_assignment;
2068
3094
 
2069
 
                if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
 
3095
                if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
2070
3096
                        encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2071
3097
                        return false;
2072
3098
                }
2114
3140
                }
2115
3141
 
2116
3142
                /* note that encoder_add_subframe_ sets the state for us in case of an error */
2117
 
                if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
 
3143
                if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame))
2118
3144
                        return false;
2119
 
                if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
 
3145
                if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame))
2120
3146
                        return false;
2121
3147
        }
2122
3148
        else {
2123
 
                if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
 
3149
                if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
2124
3150
                        encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2125
3151
                        return false;
2126
3152
                }
2127
3153
 
2128
3154
                for(channel = 0; channel < encoder->protected_->channels; channel++) {
2129
 
                        if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
 
3155
                        if(!add_subframe_(encoder, frame_header.blocksize, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
2130
3156
                                /* the above function sets the state for us in case of an error */
2131
3157
                                return false;
2132
3158
                        }
2148
3174
        FLAC__StreamEncoder *encoder,
2149
3175
        unsigned min_partition_order,
2150
3176
        unsigned max_partition_order,
2151
 
        FLAC__bool precompute_partition_sums,
2152
3177
        const FLAC__FrameHeader *frame_header,
2153
3178
        unsigned subframe_bps,
2154
3179
        const FLAC__int32 integer_signal[],
2155
 
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2156
 
        const FLAC__real real_signal[],
2157
 
#endif
2158
3180
        FLAC__Subframe *subframe[2],
2159
3181
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
2160
3182
        FLAC__int32 *residual[2],
2178
3200
        unsigned rice_parameter;
2179
3201
        unsigned _candidate_bits, _best_bits;
2180
3202
        unsigned _best_subframe;
 
3203
        /* only use RICE2 partitions if stream bps > 16 */
 
3204
        const unsigned rice_parameter_limit = FLAC__stream_encoder_get_bits_per_sample(encoder) > 16? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
 
3205
 
 
3206
        FLAC__ASSERT(frame_header->blocksize > 0);
2181
3207
 
2182
3208
        /* verbatim subframe is the baseline against which we measure other compressed subframes */
2183
3209
        _best_subframe = 0;
2184
3210
        if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
2185
3211
                _best_bits = UINT_MAX;
2186
3212
        else
2187
 
                _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
 
3213
                _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2188
3214
 
2189
3215
        if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
2190
3216
                unsigned signal_is_constant = false;
2209
3235
                        }
2210
3236
                }
2211
3237
                if(signal_is_constant) {
2212
 
                        _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
 
3238
                        _candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]);
2213
3239
                        if(_candidate_bits < _best_bits) {
2214
3240
                                _best_subframe = !_best_subframe;
2215
3241
                                _best_bits = _candidate_bits;
2225
3251
                                else {
2226
3252
                                        min_fixed_order = max_fixed_order = guess_fixed_order;
2227
3253
                                }
 
3254
                                if(max_fixed_order >= frame_header->blocksize)
 
3255
                                        max_fixed_order = frame_header->blocksize - 1;
2228
3256
                                for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
2229
3257
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2230
3258
                                        if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
2235
3263
                                                continue; /* don't even try */
2236
3264
                                        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 */
2237
3265
#endif
2238
 
#ifndef FLAC__SYMMETRIC_RICE
2239
3266
                                        rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2240
 
#endif
2241
 
                                        if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
 
3267
                                        if(rice_parameter >= rice_parameter_limit) {
2242
3268
#ifdef DEBUG_VERBOSE
2243
 
                                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
 
3269
                                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, rice_parameter_limit - 1);
2244
3270
#endif
2245
 
                                                rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
 
3271
                                                rice_parameter = rice_parameter_limit - 1;
2246
3272
                                        }
2247
3273
                                        _candidate_bits =
2248
3274
                                                evaluate_fixed_subframe_(
2249
3275
                                                        encoder,
2250
3276
                                                        integer_signal,
2251
3277
                                                        residual[!_best_subframe],
2252
 
                                                        encoder->private_->abs_residual,
2253
3278
                                                        encoder->private_->abs_residual_partition_sums,
2254
3279
                                                        encoder->private_->raw_bits_per_partition,
2255
3280
                                                        frame_header->blocksize,
2256
3281
                                                        subframe_bps,
2257
3282
                                                        fixed_order,
2258
3283
                                                        rice_parameter,
 
3284
                                                        rice_parameter_limit,
2259
3285
                                                        min_partition_order,
2260
3286
                                                        max_partition_order,
2261
 
                                                        precompute_partition_sums,
2262
3287
                                                        encoder->protected_->do_escape_coding,
2263
3288
                                                        encoder->protected_->rice_parameter_search_dist,
2264
3289
                                                        subframe[!_best_subframe],
2279
3304
                                else
2280
3305
                                        max_lpc_order = encoder->protected_->max_lpc_order;
2281
3306
                                if(max_lpc_order > 0) {
2282
 
                                        encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2283
 
                                        /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2284
 
                                        if(autoc[0] != 0.0) {
2285
 
                                                FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2286
 
                                                if(encoder->protected_->do_exhaustive_model_search) {
2287
 
                                                        min_lpc_order = 1;
2288
 
                                                }
2289
 
                                                else {
2290
 
                                                        unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2291
 
                                                        min_lpc_order = max_lpc_order = guess_lpc_order;
2292
 
                                                }
2293
 
                                                for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2294
 
                                                        lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2295
 
                                                        if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
2296
 
                                                                continue; /* don't even try */
2297
 
                                                        rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2298
 
#ifndef FLAC__SYMMETRIC_RICE
2299
 
                                                        rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2300
 
#endif
2301
 
                                                        if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2302
 
#ifdef DEBUG_VERBOSE
2303
 
                                                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2304
 
#endif
2305
 
                                                                rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2306
 
                                                        }
2307
 
                                                        if(encoder->protected_->do_qlp_coeff_prec_search) {
2308
 
                                                                min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2309
 
                                                                /* ensure a 32-bit datapath throughout for 16bps or less */
2310
 
                                                                if(subframe_bps <= 16)
2311
 
                                                                        max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2312
 
                                                                else
2313
 
                                                                        max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
 
3307
                                        unsigned a;
 
3308
                                        for (a = 0; a < encoder->protected_->num_apodizations; a++) {
 
3309
                                                FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
 
3310
                                                encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
 
3311
                                                /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
 
3312
                                                if(autoc[0] != 0.0) {
 
3313
                                                        FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
 
3314
                                                        if(encoder->protected_->do_exhaustive_model_search) {
 
3315
                                                                min_lpc_order = 1;
2314
3316
                                                        }
2315
3317
                                                        else {
2316
 
                                                                min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2317
 
                                                        }
2318
 
                                                        for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2319
 
                                                                _candidate_bits =
2320
 
                                                                        evaluate_lpc_subframe_(
2321
 
                                                                                encoder,
2322
 
                                                                                integer_signal,
2323
 
                                                                                residual[!_best_subframe],
2324
 
                                                                                encoder->private_->abs_residual,
2325
 
                                                                                encoder->private_->abs_residual_partition_sums,
2326
 
                                                                                encoder->private_->raw_bits_per_partition,
2327
 
                                                                                encoder->private_->lp_coeff[lpc_order-1],
 
3318
                                                                const unsigned guess_lpc_order =
 
3319
                                                                        FLAC__lpc_compute_best_order(
 
3320
                                                                                lpc_error,
 
3321
                                                                                max_lpc_order,
2328
3322
                                                                                frame_header->blocksize,
2329
 
                                                                                subframe_bps,
2330
 
                                                                                lpc_order,
2331
 
                                                                                qlp_coeff_precision,
2332
 
                                                                                rice_parameter,
2333
 
                                                                                min_partition_order,
2334
 
                                                                                max_partition_order,
2335
 
                                                                                precompute_partition_sums,
2336
 
                                                                                encoder->protected_->do_escape_coding,
2337
 
                                                                                encoder->protected_->rice_parameter_search_dist,
2338
 
                                                                                subframe[!_best_subframe],
2339
 
                                                                                partitioned_rice_contents[!_best_subframe]
 
3323
                                                                                subframe_bps + (
 
3324
                                                                                        encoder->protected_->do_qlp_coeff_prec_search?
 
3325
                                                                                                FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
 
3326
                                                                                                encoder->protected_->qlp_coeff_precision
 
3327
                                                                                )
2340
3328
                                                                        );
2341
 
                                                                if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2342
 
                                                                        if(_candidate_bits < _best_bits) {
2343
 
                                                                                _best_subframe = !_best_subframe;
2344
 
                                                                                _best_bits = _candidate_bits;
 
3329
                                                                min_lpc_order = max_lpc_order = guess_lpc_order;
 
3330
                                                        }
 
3331
                                                        if(max_lpc_order >= frame_header->blocksize)
 
3332
                                                                max_lpc_order = frame_header->blocksize - 1;
 
3333
                                                        for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
 
3334
                                                                lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
 
3335
                                                                if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
 
3336
                                                                        continue; /* don't even try */
 
3337
                                                                rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
 
3338
                                                                rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
 
3339
                                                                if(rice_parameter >= rice_parameter_limit) {
 
3340
#ifdef DEBUG_VERBOSE
 
3341
                                                                        fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1);
 
3342
#endif
 
3343
                                                                        rice_parameter = rice_parameter_limit - 1;
 
3344
                                                                }
 
3345
                                                                if(encoder->protected_->do_qlp_coeff_prec_search) {
 
3346
                                                                        min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
 
3347
                                                                        /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
 
3348
                                                                        if(subframe_bps <= 17) {
 
3349
                                                                                max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
 
3350
                                                                                max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
 
3351
                                                                        }
 
3352
                                                                        else
 
3353
                                                                                max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
 
3354
                                                                }
 
3355
                                                                else {
 
3356
                                                                        min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
 
3357
                                                                }
 
3358
                                                                for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
 
3359
                                                                        _candidate_bits =
 
3360
                                                                                evaluate_lpc_subframe_(
 
3361
                                                                                        encoder,
 
3362
                                                                                        integer_signal,
 
3363
                                                                                        residual[!_best_subframe],
 
3364
                                                                                        encoder->private_->abs_residual_partition_sums,
 
3365
                                                                                        encoder->private_->raw_bits_per_partition,
 
3366
                                                                                        encoder->private_->lp_coeff[lpc_order-1],
 
3367
                                                                                        frame_header->blocksize,
 
3368
                                                                                        subframe_bps,
 
3369
                                                                                        lpc_order,
 
3370
                                                                                        qlp_coeff_precision,
 
3371
                                                                                        rice_parameter,
 
3372
                                                                                        rice_parameter_limit,
 
3373
                                                                                        min_partition_order,
 
3374
                                                                                        max_partition_order,
 
3375
                                                                                        encoder->protected_->do_escape_coding,
 
3376
                                                                                        encoder->protected_->rice_parameter_search_dist,
 
3377
                                                                                        subframe[!_best_subframe],
 
3378
                                                                                        partitioned_rice_contents[!_best_subframe]
 
3379
                                                                                );
 
3380
                                                                        if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
 
3381
                                                                                if(_candidate_bits < _best_bits) {
 
3382
                                                                                        _best_subframe = !_best_subframe;
 
3383
                                                                                        _best_bits = _candidate_bits;
 
3384
                                                                                }
2345
3385
                                                                        }
2346
3386
                                                                }
2347
3387
                                                        }
2356
3396
        /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
2357
3397
        if(_best_bits == UINT_MAX) {
2358
3398
                FLAC__ASSERT(_best_subframe == 0);
2359
 
                _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
 
3399
                _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
2360
3400
        }
2361
3401
 
2362
3402
        *best_subframe = _best_subframe;
2367
3407
 
2368
3408
FLAC__bool add_subframe_(
2369
3409
        FLAC__StreamEncoder *encoder,
2370
 
        const FLAC__FrameHeader *frame_header,
 
3410
        unsigned blocksize,
2371
3411
        unsigned subframe_bps,
2372
3412
        const FLAC__Subframe *subframe,
2373
 
        FLAC__BitBuffer *frame
 
3413
        FLAC__BitWriter *frame
2374
3414
)
2375
3415
{
2376
3416
        switch(subframe->type) {
2377
3417
                case FLAC__SUBFRAME_TYPE_CONSTANT:
2378
3418
                        if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
2379
 
                                encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
 
3419
                                encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2380
3420
                                return false;
2381
3421
                        }
2382
3422
                        break;
2383
3423
                case FLAC__SUBFRAME_TYPE_FIXED:
2384
 
                        if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
2385
 
                                encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
 
3424
                        if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
 
3425
                                encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2386
3426
                                return false;
2387
3427
                        }
2388
3428
                        break;
2389
3429
                case FLAC__SUBFRAME_TYPE_LPC:
2390
 
                        if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
2391
 
                                encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
 
3430
                        if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
 
3431
                                encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2392
3432
                                return false;
2393
3433
                        }
2394
3434
                        break;
2395
3435
                case FLAC__SUBFRAME_TYPE_VERBATIM:
2396
 
                        if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
2397
 
                                encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
 
3436
                        if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) {
 
3437
                                encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
2398
3438
                                return false;
2399
3439
                        }
2400
3440
                        break;
2405
3445
        return true;
2406
3446
}
2407
3447
 
 
3448
#define SPOTCHECK_ESTIMATE 0
 
3449
#if SPOTCHECK_ESTIMATE
 
3450
static void spotcheck_subframe_estimate_(
 
3451
        FLAC__StreamEncoder *encoder,
 
3452
        unsigned blocksize,
 
3453
        unsigned subframe_bps,
 
3454
        const FLAC__Subframe *subframe,
 
3455
        unsigned estimate
 
3456
)
 
3457
{
 
3458
        FLAC__bool ret;
 
3459
        FLAC__BitWriter *frame = FLAC__bitwriter_new();
 
3460
        if(frame == 0) {
 
3461
                fprintf(stderr, "EST: can't allocate frame\n");
 
3462
                return;
 
3463
        }
 
3464
        if(!FLAC__bitwriter_init(frame)) {
 
3465
                fprintf(stderr, "EST: can't init frame\n");
 
3466
                return;
 
3467
        }
 
3468
        ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
 
3469
        FLAC__ASSERT(ret);
 
3470
        {
 
3471
                const unsigned actual = FLAC__bitwriter_get_input_bits_unconsumed(frame);
 
3472
                if(estimate != actual)
 
3473
                        fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
 
3474
        }
 
3475
        FLAC__bitwriter_delete(frame);
 
3476
}
 
3477
#endif
 
3478
 
2408
3479
unsigned evaluate_constant_subframe_(
 
3480
        FLAC__StreamEncoder *encoder,
2409
3481
        const FLAC__int32 signal,
 
3482
        unsigned blocksize,
2410
3483
        unsigned subframe_bps,
2411
3484
        FLAC__Subframe *subframe
2412
3485
)
2413
3486
{
 
3487
        unsigned estimate;
2414
3488
        subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
2415
3489
        subframe->data.constant.value = signal;
2416
3490
 
2417
 
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
 
3491
        estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps;
 
3492
 
 
3493
#if SPOTCHECK_ESTIMATE
 
3494
        spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
 
3495
#else
 
3496
        (void)encoder, (void)blocksize;
 
3497
#endif
 
3498
 
 
3499
        return estimate;
2418
3500
}
2419
3501
 
2420
3502
unsigned evaluate_fixed_subframe_(
2421
3503
        FLAC__StreamEncoder *encoder,
2422
3504
        const FLAC__int32 signal[],
2423
3505
        FLAC__int32 residual[],
2424
 
        FLAC__uint32 abs_residual[],
2425
3506
        FLAC__uint64 abs_residual_partition_sums[],
2426
3507
        unsigned raw_bits_per_partition[],
2427
3508
        unsigned blocksize,
2428
3509
        unsigned subframe_bps,
2429
3510
        unsigned order,
2430
3511
        unsigned rice_parameter,
 
3512
        unsigned rice_parameter_limit,
2431
3513
        unsigned min_partition_order,
2432
3514
        unsigned max_partition_order,
2433
 
        FLAC__bool precompute_partition_sums,
2434
3515
        FLAC__bool do_escape_coding,
2435
3516
        unsigned rice_parameter_search_dist,
2436
3517
        FLAC__Subframe *subframe,
2437
3518
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
2438
3519
)
2439
3520
{
2440
 
        unsigned i, residual_bits;
 
3521
        unsigned i, residual_bits, estimate;
2441
3522
        const unsigned residual_samples = blocksize - order;
2442
3523
 
2443
3524
        FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
2452
3533
                find_best_partition_order_(
2453
3534
                        encoder->private_,
2454
3535
                        residual,
2455
 
                        abs_residual,
2456
3536
                        abs_residual_partition_sums,
2457
3537
                        raw_bits_per_partition,
2458
3538
                        residual_samples,
2459
3539
                        order,
2460
3540
                        rice_parameter,
 
3541
                        rice_parameter_limit,
2461
3542
                        min_partition_order,
2462
3543
                        max_partition_order,
2463
 
                        precompute_partition_sums,
 
3544
                        subframe_bps,
2464
3545
                        do_escape_coding,
2465
3546
                        rice_parameter_search_dist,
2466
 
                        &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
 
3547
                        &subframe->data.fixed.entropy_coding_method
2467
3548
                );
2468
3549
 
2469
3550
        subframe->data.fixed.order = order;
2470
3551
        for(i = 0; i < order; i++)
2471
3552
                subframe->data.fixed.warmup[i] = signal[i];
2472
3553
 
2473
 
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
 
3554
        estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps) + residual_bits;
 
3555
 
 
3556
#if SPOTCHECK_ESTIMATE
 
3557
        spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
 
3558
#endif
 
3559
 
 
3560
        return estimate;
2474
3561
}
2475
3562
 
2476
3563
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2478
3565
        FLAC__StreamEncoder *encoder,
2479
3566
        const FLAC__int32 signal[],
2480
3567
        FLAC__int32 residual[],
2481
 
        FLAC__uint32 abs_residual[],
2482
3568
        FLAC__uint64 abs_residual_partition_sums[],
2483
3569
        unsigned raw_bits_per_partition[],
2484
3570
        const FLAC__real lp_coeff[],
2487
3573
        unsigned order,
2488
3574
        unsigned qlp_coeff_precision,
2489
3575
        unsigned rice_parameter,
 
3576
        unsigned rice_parameter_limit,
2490
3577
        unsigned min_partition_order,
2491
3578
        unsigned max_partition_order,
2492
 
        FLAC__bool precompute_partition_sums,
2493
3579
        FLAC__bool do_escape_coding,
2494
3580
        unsigned rice_parameter_search_dist,
2495
3581
        FLAC__Subframe *subframe,
2497
3583
)
2498
3584
{
2499
3585
        FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
2500
 
        unsigned i, residual_bits;
 
3586
        unsigned i, residual_bits, estimate;
2501
3587
        int quantization, ret;
2502
3588
        const unsigned residual_samples = blocksize - order;
2503
3589
 
2530
3616
                find_best_partition_order_(
2531
3617
                        encoder->private_,
2532
3618
                        residual,
2533
 
                        abs_residual,
2534
3619
                        abs_residual_partition_sums,
2535
3620
                        raw_bits_per_partition,
2536
3621
                        residual_samples,
2537
3622
                        order,
2538
3623
                        rice_parameter,
 
3624
                        rice_parameter_limit,
2539
3625
                        min_partition_order,
2540
3626
                        max_partition_order,
2541
 
                        precompute_partition_sums,
 
3627
                        subframe_bps,
2542
3628
                        do_escape_coding,
2543
3629
                        rice_parameter_search_dist,
2544
 
                        &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
 
3630
                        &subframe->data.lpc.entropy_coding_method
2545
3631
                );
2546
3632
 
2547
3633
        subframe->data.lpc.order = order;
2551
3637
        for(i = 0; i < order; i++)
2552
3638
                subframe->data.lpc.warmup[i] = signal[i];
2553
3639
 
2554
 
        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;
 
3640
        estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
 
3641
 
 
3642
#if SPOTCHECK_ESTIMATE
 
3643
        spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
 
3644
#endif
 
3645
 
 
3646
        return estimate;
2555
3647
}
2556
3648
#endif
2557
3649
 
2558
3650
unsigned evaluate_verbatim_subframe_(
 
3651
        FLAC__StreamEncoder *encoder,
2559
3652
        const FLAC__int32 signal[],
2560
3653
        unsigned blocksize,
2561
3654
        unsigned subframe_bps,
2562
3655
        FLAC__Subframe *subframe
2563
3656
)
2564
3657
{
 
3658
        unsigned estimate;
 
3659
 
2565
3660
        subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2566
3661
 
2567
3662
        subframe->data.verbatim.data = signal;
2568
3663
 
2569
 
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
 
3664
        estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);
 
3665
 
 
3666
#if SPOTCHECK_ESTIMATE
 
3667
        spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
 
3668
#else
 
3669
        (void)encoder;
 
3670
#endif
 
3671
 
 
3672
        return estimate;
2570
3673
}
2571
3674
 
2572
3675
unsigned find_best_partition_order_(
2573
3676
        FLAC__StreamEncoderPrivate *private_,
2574
3677
        const FLAC__int32 residual[],
2575
 
        FLAC__uint32 abs_residual[],
2576
3678
        FLAC__uint64 abs_residual_partition_sums[],
2577
3679
        unsigned raw_bits_per_partition[],
2578
3680
        unsigned residual_samples,
2579
3681
        unsigned predictor_order,
2580
3682
        unsigned rice_parameter,
 
3683
        unsigned rice_parameter_limit,
2581
3684
        unsigned min_partition_order,
2582
3685
        unsigned max_partition_order,
2583
 
        FLAC__bool precompute_partition_sums,
 
3686
        unsigned bps,
2584
3687
        FLAC__bool do_escape_coding,
2585
3688
        unsigned rice_parameter_search_dist,
2586
 
        FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
 
3689
        FLAC__EntropyCodingMethod *best_ecm
2587
3690
)
2588
3691
{
2589
 
        FLAC__int32 r;
2590
3692
        unsigned residual_bits, best_residual_bits = 0;
2591
 
        unsigned residual_sample;
2592
3693
        unsigned best_parameters_index = 0;
 
3694
        unsigned best_partition_order = 0;
2593
3695
        const unsigned blocksize = residual_samples + predictor_order;
2594
3696
 
2595
 
        /* compute abs(residual) for use later */
2596
 
        for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2597
 
                r = residual[residual_sample];
2598
 
                abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2599
 
        }
2600
 
 
2601
3697
        max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2602
3698
        min_partition_order = min(min_partition_order, max_partition_order);
2603
3699
 
2604
 
        if(precompute_partition_sums) {
 
3700
        precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps);
 
3701
 
 
3702
        if(do_escape_coding)
 
3703
                precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
 
3704
 
 
3705
        {
2605
3706
                int partition_order;
2606
3707
                unsigned sum;
2607
3708
 
2608
 
                precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2609
 
 
2610
 
                if(do_escape_coding)
2611
 
                        precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2612
 
 
2613
3709
                for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2614
 
#ifdef DONT_ESTIMATE_RICE_BITS
2615
3710
                        if(!
2616
 
                                set_partitioned_rice_with_precompute_(
 
3711
                                set_partitioned_rice_(
 
3712
#ifdef EXACT_RICE_BITS_CALCULATION
2617
3713
                                        residual,
2618
 
                                        abs_residual_partition_sums+sum,
2619
 
                                        raw_bits_per_partition+sum,
2620
 
                                        residual_samples,
2621
 
                                        predictor_order,
2622
 
                                        rice_parameter,
2623
 
                                        rice_parameter_search_dist,
2624
 
                                        (unsigned)partition_order,
2625
 
                                        do_escape_coding,
2626
 
                                        &private_->partitioned_rice_contents_extra[!best_parameters_index],
2627
 
                                        &residual_bits
2628
 
                                )
2629
 
                        )
2630
 
#else
2631
 
                        if(!
2632
 
                                set_partitioned_rice_with_precompute_(
2633
 
                                        abs_residual,
2634
 
                                        abs_residual_partition_sums+sum,
2635
 
                                        raw_bits_per_partition+sum,
2636
 
                                        residual_samples,
2637
 
                                        predictor_order,
2638
 
                                        rice_parameter,
2639
 
                                        rice_parameter_search_dist,
2640
 
                                        (unsigned)partition_order,
2641
 
                                        do_escape_coding,
2642
 
                                        &private_->partitioned_rice_contents_extra[!best_parameters_index],
2643
 
                                        &residual_bits
2644
 
                                )
2645
 
                        )
2646
3714
#endif
 
3715
                                        abs_residual_partition_sums+sum,
 
3716
                                        raw_bits_per_partition+sum,
 
3717
                                        residual_samples,
 
3718
                                        predictor_order,
 
3719
                                        rice_parameter,
 
3720
                                        rice_parameter_limit,
 
3721
                                        rice_parameter_search_dist,
 
3722
                                        (unsigned)partition_order,
 
3723
                                        do_escape_coding,
 
3724
                                        &private_->partitioned_rice_contents_extra[!best_parameters_index],
 
3725
                                        &residual_bits
 
3726
                                )
 
3727
                        )
2647
3728
                        {
2648
3729
                                FLAC__ASSERT(best_residual_bits != 0);
2649
3730
                                break;
2652
3733
                        if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2653
3734
                                best_residual_bits = residual_bits;
2654
3735
                                best_parameters_index = !best_parameters_index;
2655
 
                                best_partitioned_rice->order = partition_order;
 
3736
                                best_partition_order = partition_order;
2656
3737
                        }
2657
3738
                }
2658
3739
        }
2659
 
        else {
2660
 
                unsigned partition_order;
2661
 
                for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2662
 
#ifdef DONT_ESTIMATE_RICE_BITS
2663
 
                        if(!
2664
 
                                set_partitioned_rice_(
2665
 
                                        abs_residual,
2666
 
                                        residual,
2667
 
                                        residual_samples,
2668
 
                                        predictor_order,
2669
 
                                        rice_parameter,
2670
 
                                        rice_parameter_search_dist,
2671
 
                                        partition_order,
2672
 
                                        &private_->partitioned_rice_contents_extra[!best_parameters_index],
2673
 
                                        &residual_bits
2674
 
                                )
2675
 
                        )
2676
 
#else
2677
 
                        if(!
2678
 
                                set_partitioned_rice_(
2679
 
                                        abs_residual,
2680
 
                                        residual_samples,
2681
 
                                        predictor_order,
2682
 
                                        rice_parameter,
2683
 
                                        rice_parameter_search_dist,
2684
 
                                        partition_order,
2685
 
                                        &private_->partitioned_rice_contents_extra[!best_parameters_index],
2686
 
                                        &residual_bits
2687
 
                                )
2688
 
                        )
2689
 
#endif
2690
 
                        {
2691
 
                                FLAC__ASSERT(best_residual_bits != 0);
 
3740
 
 
3741
        best_ecm->data.partitioned_rice.order = best_partition_order;
 
3742
 
 
3743
        {
 
3744
                /*
 
3745
                 * We are allowed to de-const the pointer based on our special
 
3746
                 * knowledge; it is const to the outside world.
 
3747
                 */
 
3748
                FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents;
 
3749
                unsigned partition;
 
3750
 
 
3751
                /* save best parameters and raw_bits */
 
3752
                FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, max(6, best_partition_order));
 
3753
                memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partition_order)));
 
3754
                if(do_escape_coding)
 
3755
                        memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partition_order)));
 
3756
                /*
 
3757
                 * Now need to check if the type should be changed to
 
3758
                 * FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 based on the
 
3759
                 * size of the rice parameters.
 
3760
                 */
 
3761
                for(partition = 0; partition < (1u<<best_partition_order); partition++) {
 
3762
                        if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
 
3763
                                best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2;
2692
3764
                                break;
2693
3765
                        }
2694
 
                        if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2695
 
                                best_residual_bits = residual_bits;
2696
 
                                best_parameters_index = !best_parameters_index;
2697
 
                                best_partitioned_rice->order = partition_order;
2698
 
                        }
2699
3766
                }
2700
3767
        }
2701
3768
 
2702
 
        /*
2703
 
         * We are allowed to de-const the pointer based on our special knowledge;
2704
 
         * it is const to the outside world.
2705
 
         */
2706
 
        {
2707
 
                FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2708
 
                FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2709
 
                memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2710
 
                memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2711
 
        }
2712
 
 
2713
3769
        return best_residual_bits;
2714
3770
}
2715
3771
 
 
3772
#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
 
3773
extern void precompute_partition_info_sums_32bit_asm_ia32_(
 
3774
        const FLAC__int32 residual[],
 
3775
        FLAC__uint64 abs_residual_partition_sums[],
 
3776
        unsigned blocksize,
 
3777
        unsigned predictor_order,
 
3778
        unsigned min_partition_order,
 
3779
        unsigned max_partition_order
 
3780
);
 
3781
#endif
 
3782
 
2716
3783
void precompute_partition_info_sums_(
2717
 
        const FLAC__uint32 abs_residual[],
 
3784
        const FLAC__int32 residual[],
2718
3785
        FLAC__uint64 abs_residual_partition_sums[],
2719
3786
        unsigned residual_samples,
2720
3787
        unsigned predictor_order,
2721
3788
        unsigned min_partition_order,
2722
 
        unsigned max_partition_order
 
3789
        unsigned max_partition_order,
 
3790
        unsigned bps
2723
3791
)
2724
3792
{
2725
 
        int partition_order;
2726
 
        unsigned from_partition, to_partition = 0;
2727
 
        const unsigned blocksize = residual_samples + predictor_order;
 
3793
        const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
 
3794
        unsigned partitions = 1u << max_partition_order;
 
3795
 
 
3796
        FLAC__ASSERT(default_partition_samples > predictor_order);
 
3797
 
 
3798
#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
 
3799
        /* slightly pessimistic but still catches all common cases */
 
3800
        /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
 
3801
        if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
 
3802
                precompute_partition_info_sums_32bit_asm_ia32_(residual, abs_residual_partition_sums, residual_samples + predictor_order, predictor_order, min_partition_order, max_partition_order);
 
3803
                return;
 
3804
        }
 
3805
#endif
2728
3806
 
2729
3807
        /* first do max_partition_order */
2730
 
        for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2731
 
                FLAC__uint64 abs_residual_partition_sum;
2732
 
                FLAC__uint32 abs_r;
2733
 
                unsigned partition, partition_sample, partition_samples, residual_sample;
2734
 
                const unsigned partitions = 1u << partition_order;
2735
 
                const unsigned default_partition_samples = blocksize >> partition_order;
2736
 
 
2737
 
                FLAC__ASSERT(default_partition_samples > predictor_order);
2738
 
 
2739
 
                for(partition = residual_sample = 0; partition < partitions; partition++) {
2740
 
                        partition_samples = default_partition_samples;
2741
 
                        if(partition == 0)
2742
 
                                partition_samples -= predictor_order;
2743
 
                        abs_residual_partition_sum = 0;
2744
 
                        for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2745
 
                                abs_r = abs_residual[residual_sample];
2746
 
                                abs_residual_partition_sum += abs_r;
2747
 
                                residual_sample++;
2748
 
                        }
2749
 
                        abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2750
 
                }
2751
 
                to_partition = partitions;
2752
 
                break;
 
3808
        {
 
3809
                unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order);
 
3810
                /* slightly pessimistic but still catches all common cases */
 
3811
                /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
 
3812
                if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
 
3813
                        FLAC__uint32 abs_residual_partition_sum;
 
3814
 
 
3815
                        for(partition = residual_sample = 0; partition < partitions; partition++) {
 
3816
                                end += default_partition_samples;
 
3817
                                abs_residual_partition_sum = 0;
 
3818
                                for( ; residual_sample < end; residual_sample++)
 
3819
                                        abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
 
3820
                                abs_residual_partition_sums[partition] = abs_residual_partition_sum;
 
3821
                        }
 
3822
                }
 
3823
                else { /* have to pessimistically use 64 bits for accumulator */
 
3824
                        FLAC__uint64 abs_residual_partition_sum;
 
3825
 
 
3826
                        for(partition = residual_sample = 0; partition < partitions; partition++) {
 
3827
                                end += default_partition_samples;
 
3828
                                abs_residual_partition_sum = 0;
 
3829
                                for( ; residual_sample < end; residual_sample++)
 
3830
                                        abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
 
3831
                                abs_residual_partition_sums[partition] = abs_residual_partition_sum;
 
3832
                        }
 
3833
                }
2753
3834
        }
2754
3835
 
2755
3836
        /* now merge partitions for lower orders */
2756
 
        for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2757
 
                FLAC__uint64 s;
2758
 
                unsigned i;
2759
 
                const unsigned partitions = 1u << partition_order;
2760
 
                for(i = 0; i < partitions; i++) {
2761
 
                        s = abs_residual_partition_sums[from_partition];
2762
 
                        from_partition++;
2763
 
                        abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
2764
 
                        from_partition++;
2765
 
                        to_partition++;
 
3837
        {
 
3838
                unsigned from_partition = 0, to_partition = partitions;
 
3839
                int partition_order;
 
3840
                for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
 
3841
                        unsigned i;
 
3842
                        partitions >>= 1;
 
3843
                        for(i = 0; i < partitions; i++) {
 
3844
                                abs_residual_partition_sums[to_partition++] =
 
3845
                                        abs_residual_partition_sums[from_partition  ] +
 
3846
                                        abs_residual_partition_sums[from_partition+1];
 
3847
                                from_partition += 2;
 
3848
                        }
2766
3849
                }
2767
3850
        }
2768
3851
}
2782
3865
 
2783
3866
        /* first do max_partition_order */
2784
3867
        for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2785
 
                FLAC__int32 r, residual_partition_min, residual_partition_max;
2786
 
                unsigned silog2_min, silog2_max;
 
3868
                FLAC__int32 r;
 
3869
                FLAC__uint32 rmax;
2787
3870
                unsigned partition, partition_sample, partition_samples, residual_sample;
2788
3871
                const unsigned partitions = 1u << partition_order;
2789
3872
                const unsigned default_partition_samples = blocksize >> partition_order;
2794
3877
                        partition_samples = default_partition_samples;
2795
3878
                        if(partition == 0)
2796
3879
                                partition_samples -= predictor_order;
2797
 
                        residual_partition_min = residual_partition_max = 0;
 
3880
                        rmax = 0;
2798
3881
                        for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2799
 
                                r = residual[residual_sample];
2800
 
                                if(r < residual_partition_min)
2801
 
                                        residual_partition_min = r;
2802
 
                                else if(r > residual_partition_max)
2803
 
                                        residual_partition_max = r;
2804
 
                                residual_sample++;
 
3882
                                r = residual[residual_sample++];
 
3883
                                /* OPT: maybe faster: rmax |= r ^ (r>>31) */
 
3884
                                if(r < 0)
 
3885
                                        rmax |= ~r;
 
3886
                                else
 
3887
                                        rmax |= r;
2805
3888
                        }
2806
 
                        silog2_min = FLAC__bitmath_silog2(residual_partition_min);
2807
 
                        silog2_max = FLAC__bitmath_silog2(residual_partition_max);
2808
 
                        raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
 
3889
                        /* now we know all residual values are in the range [-rmax-1,rmax] */
 
3890
                        raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1;
2809
3891
                }
2810
3892
                to_partition = partitions;
2811
 
                break;
 
3893
                break; /*@@@ yuck, should remove the 'for' loop instead */
2812
3894
        }
2813
3895
 
2814
3896
        /* now merge partitions for lower orders */
2826
3908
        }
2827
3909
}
2828
3910
 
2829
 
#ifdef VARIABLE_RICE_BITS
2830
 
#undef VARIABLE_RICE_BITS
2831
 
#endif
2832
 
#ifndef DONT_ESTIMATE_RICE_BITS
2833
 
#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2834
 
#endif
2835
 
 
2836
 
#ifdef DONT_ESTIMATE_RICE_BITS
2837
 
FLAC__bool set_partitioned_rice_(
2838
 
        const FLAC__uint32 abs_residual[],
2839
 
        const FLAC__int32 residual[],
2840
 
        const unsigned residual_samples,
2841
 
        const unsigned predictor_order,
2842
 
        const unsigned suggested_rice_parameter,
2843
 
        const unsigned rice_parameter_search_dist,
2844
 
        const unsigned partition_order,
2845
 
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2846
 
        unsigned *bits
2847
 
)
2848
 
#else
2849
 
FLAC__bool set_partitioned_rice_(
2850
 
        const FLAC__uint32 abs_residual[],
2851
 
        const unsigned residual_samples,
2852
 
        const unsigned predictor_order,
2853
 
        const unsigned suggested_rice_parameter,
2854
 
        const unsigned rice_parameter_search_dist,
2855
 
        const unsigned partition_order,
2856
 
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2857
 
        unsigned *bits
2858
 
)
2859
 
#endif
2860
 
{
2861
 
        unsigned rice_parameter, partition_bits;
2862
 
#ifndef NO_RICE_SEARCH
2863
 
        unsigned best_partition_bits;
2864
 
        unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2865
 
#endif
2866
 
        unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2867
 
        unsigned *parameters;
2868
 
 
2869
 
        FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2870
 
 
2871
 
        FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2872
 
        parameters = partitioned_rice_contents->parameters;
2873
 
 
2874
 
        if(partition_order == 0) {
2875
 
                unsigned i;
2876
 
 
2877
 
#ifndef NO_RICE_SEARCH
2878
 
                if(rice_parameter_search_dist) {
2879
 
                        if(suggested_rice_parameter < rice_parameter_search_dist)
2880
 
                                min_rice_parameter = 0;
2881
 
                        else
2882
 
                                min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2883
 
                        max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2884
 
                        if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2885
 
#ifdef DEBUG_VERBOSE
2886
 
                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2887
 
#endif
2888
 
                                max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2889
 
                        }
2890
 
                }
2891
 
                else
2892
 
                        min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2893
 
 
2894
 
                best_partition_bits = 0xffffffff;
2895
 
                for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2896
 
#endif
2897
 
#ifdef VARIABLE_RICE_BITS
2898
 
#ifdef FLAC__SYMMETRIC_RICE
2899
 
                        partition_bits = (2+rice_parameter) * residual_samples;
2900
 
#else
2901
 
                        const unsigned rice_parameter_estimate = rice_parameter-1;
2902
 
                        partition_bits = (1+rice_parameter) * residual_samples;
2903
 
#endif
2904
 
#else
2905
 
                        partition_bits = 0;
2906
 
#endif
2907
 
                        partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2908
 
                        for(i = 0; i < residual_samples; i++) {
2909
 
#ifdef VARIABLE_RICE_BITS
2910
 
#ifdef FLAC__SYMMETRIC_RICE
2911
 
                                partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2912
 
#else
2913
 
                                partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2914
 
#endif
2915
 
#else
2916
 
                                partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2917
 
#endif
2918
 
                        }
2919
 
#ifndef NO_RICE_SEARCH
2920
 
                        if(partition_bits < best_partition_bits) {
2921
 
                                best_rice_parameter = rice_parameter;
2922
 
                                best_partition_bits = partition_bits;
2923
 
                        }
2924
 
                }
2925
 
#endif
2926
 
                parameters[0] = best_rice_parameter;
2927
 
                bits_ += best_partition_bits;
2928
 
        }
2929
 
        else {
2930
 
                unsigned partition, residual_sample, save_residual_sample, partition_sample;
2931
 
                unsigned partition_samples;
2932
 
                FLAC__uint64 mean, k;
2933
 
                const unsigned partitions = 1u << partition_order;
2934
 
                for(partition = residual_sample = 0; partition < partitions; partition++) {
2935
 
                        partition_samples = (residual_samples+predictor_order) >> partition_order;
2936
 
                        if(partition == 0) {
2937
 
                                if(partition_samples <= predictor_order)
2938
 
                                        return false;
2939
 
                                else
2940
 
                                        partition_samples -= predictor_order;
2941
 
                        }
2942
 
                        mean = 0;
2943
 
                        save_residual_sample = residual_sample;
2944
 
                        for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2945
 
                                mean += abs_residual[residual_sample];
2946
 
                        residual_sample = save_residual_sample;
2947
 
#ifdef FLAC__SYMMETRIC_RICE
2948
 
                        mean += partition_samples >> 1; /* for rounding effect */
2949
 
                        mean /= partition_samples;
2950
 
 
2951
 
                        /* calc rice_parameter = floor(log2(mean)) */
2952
 
                        rice_parameter = 0;
2953
 
                        mean>>=1;
2954
 
                        while(mean) {
2955
 
                                rice_parameter++;
2956
 
                                mean >>= 1;
2957
 
                        }
2958
 
#else
2959
 
                        /* we are basically calculating the size in bits of the
2960
 
                         * average residual magnitude in the partition:
2961
 
                         *   rice_parameter = floor(log2(mean/partition_samples))
2962
 
                         * 'mean' is not a good name for the variable, it is
2963
 
                         * actually the sum of magnitudes of all residual values
2964
 
                         * in the partition, so the actual mean is
2965
 
                         * mean/partition_samples
2966
 
                         */
2967
 
                        for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2968
 
                                ;
2969
 
#endif
2970
 
                        if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2971
 
#ifdef DEBUG_VERBOSE
2972
 
                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2973
 
#endif
2974
 
                                rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2975
 
                        }
2976
 
 
2977
 
#ifndef NO_RICE_SEARCH
2978
 
                        if(rice_parameter_search_dist) {
2979
 
                                if(rice_parameter < rice_parameter_search_dist)
2980
 
                                        min_rice_parameter = 0;
2981
 
                                else
2982
 
                                        min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2983
 
                                max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2984
 
                                if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2985
 
#ifdef DEBUG_VERBOSE
2986
 
                                        fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2987
 
#endif
2988
 
                                        max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2989
 
                                }
2990
 
                        }
2991
 
                        else
2992
 
                                min_rice_parameter = max_rice_parameter = rice_parameter;
2993
 
 
2994
 
                        best_partition_bits = 0xffffffff;
2995
 
                        for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2996
 
#endif
2997
 
#ifdef VARIABLE_RICE_BITS
2998
 
#ifdef FLAC__SYMMETRIC_RICE
2999
 
                                partition_bits = (2+rice_parameter) * partition_samples;
3000
 
#else
3001
 
                                const unsigned rice_parameter_estimate = rice_parameter-1;
3002
 
                                partition_bits = (1+rice_parameter) * partition_samples;
3003
 
#endif
3004
 
#else
3005
 
                                partition_bits = 0;
3006
 
#endif
3007
 
                                partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3008
 
                                save_residual_sample = residual_sample;
3009
 
                                for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3010
 
#ifdef VARIABLE_RICE_BITS
3011
 
#ifdef FLAC__SYMMETRIC_RICE
3012
 
                                        partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
3013
 
#else
3014
 
                                        partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3015
 
#endif
3016
 
#else
3017
 
                                        partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3018
 
#endif
3019
 
                                }
3020
 
#ifndef NO_RICE_SEARCH
3021
 
                                if(rice_parameter != max_rice_parameter)
3022
 
                                        residual_sample = save_residual_sample;
3023
 
                                if(partition_bits < best_partition_bits) {
3024
 
                                        best_rice_parameter = rice_parameter;
3025
 
                                        best_partition_bits = partition_bits;
3026
 
                                }
3027
 
                        }
3028
 
#endif
3029
 
                        parameters[partition] = best_rice_parameter;
3030
 
                        bits_ += best_partition_bits;
3031
 
                }
3032
 
        }
3033
 
 
3034
 
        *bits = bits_;
3035
 
        return true;
3036
 
}
3037
 
 
3038
 
#ifdef DONT_ESTIMATE_RICE_BITS
3039
 
FLAC__bool set_partitioned_rice_with_precompute_(
3040
 
        const FLAC__int32 residual[],
3041
 
        const FLAC__uint64 abs_residual_partition_sums[],
3042
 
        const unsigned raw_bits_per_partition[],
3043
 
        const unsigned residual_samples,
3044
 
        const unsigned predictor_order,
3045
 
        const unsigned suggested_rice_parameter,
3046
 
        const unsigned rice_parameter_search_dist,
3047
 
        const unsigned partition_order,
3048
 
        const FLAC__bool search_for_escapes,
3049
 
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3050
 
        unsigned *bits
3051
 
)
3052
 
#else
3053
 
FLAC__bool set_partitioned_rice_with_precompute_(
3054
 
        const FLAC__uint32 abs_residual[],
3055
 
        const FLAC__uint64 abs_residual_partition_sums[],
3056
 
        const unsigned raw_bits_per_partition[],
3057
 
        const unsigned residual_samples,
3058
 
        const unsigned predictor_order,
3059
 
        const unsigned suggested_rice_parameter,
3060
 
        const unsigned rice_parameter_search_dist,
3061
 
        const unsigned partition_order,
3062
 
        const FLAC__bool search_for_escapes,
3063
 
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3064
 
        unsigned *bits
3065
 
)
3066
 
#endif
3067
 
{
3068
 
        unsigned rice_parameter, partition_bits;
3069
 
#ifndef NO_RICE_SEARCH
3070
 
        unsigned best_partition_bits;
3071
 
        unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3072
 
#endif
3073
 
        unsigned flat_bits;
 
3911
#ifdef EXACT_RICE_BITS_CALCULATION
 
3912
static FLaC__INLINE unsigned count_rice_bits_in_partition_(
 
3913
        const unsigned rice_parameter,
 
3914
        const unsigned partition_samples,
 
3915
        const FLAC__int32 *residual
 
3916
)
 
3917
{
 
3918
        unsigned i, partition_bits =
 
3919
                FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
 
3920
                (1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
 
3921
        ;
 
3922
        for(i = 0; i < partition_samples; i++)
 
3923
                partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
 
3924
        return partition_bits;
 
3925
}
 
3926
#else
 
3927
static FLaC__INLINE unsigned count_rice_bits_in_partition_(
 
3928
        const unsigned rice_parameter,
 
3929
        const unsigned partition_samples,
 
3930
        const FLAC__uint64 abs_residual_partition_sum
 
3931
)
 
3932
{
 
3933
        return
 
3934
                FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
 
3935
                (1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
 
3936
                (
 
3937
                        rice_parameter?
 
3938
                                (unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
 
3939
                                : (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
 
3940
                )
 
3941
                - (partition_samples >> 1)
 
3942
                /* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
 
3943
                 * The actual number of bits used is closer to the sum(for all i in the partition) of  abs(residual[i])>>(rice_parameter-1)
 
3944
                 * By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out.
 
3945
                 * So the subtraction term tries to guess how many extra bits were contributed.
 
3946
                 * If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample.
 
3947
                 */
 
3948
        ;
 
3949
}
 
3950
#endif
 
3951
 
 
3952
FLAC__bool set_partitioned_rice_(
 
3953
#ifdef EXACT_RICE_BITS_CALCULATION
 
3954
        const FLAC__int32 residual[],
 
3955
#endif
 
3956
        const FLAC__uint64 abs_residual_partition_sums[],
 
3957
        const unsigned raw_bits_per_partition[],
 
3958
        const unsigned residual_samples,
 
3959
        const unsigned predictor_order,
 
3960
        const unsigned suggested_rice_parameter,
 
3961
        const unsigned rice_parameter_limit,
 
3962
        const unsigned rice_parameter_search_dist,
 
3963
        const unsigned partition_order,
 
3964
        const FLAC__bool search_for_escapes,
 
3965
        FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
 
3966
        unsigned *bits
 
3967
)
 
3968
{
 
3969
        unsigned rice_parameter, partition_bits;
 
3970
        unsigned best_partition_bits, best_rice_parameter = 0;
3074
3971
        unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3075
3972
        unsigned *parameters, *raw_bits;
 
3973
#ifdef ENABLE_RICE_PARAMETER_SEARCH
 
3974
        unsigned min_rice_parameter, max_rice_parameter;
 
3975
#else
 
3976
        (void)rice_parameter_search_dist;
 
3977
#endif
3076
3978
 
3077
 
        FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
 
3979
        FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
 
3980
        FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
3078
3981
 
3079
3982
        FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3080
3983
        parameters = partitioned_rice_contents->parameters;
3081
3984
        raw_bits = partitioned_rice_contents->raw_bits;
3082
3985
 
3083
3986
        if(partition_order == 0) {
3084
 
                unsigned i;
3085
 
 
3086
 
#ifndef NO_RICE_SEARCH
 
3987
                best_partition_bits = (unsigned)(-1);
 
3988
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3087
3989
                if(rice_parameter_search_dist) {
3088
3990
                        if(suggested_rice_parameter < rice_parameter_search_dist)
3089
3991
                                min_rice_parameter = 0;
3090
3992
                        else
3091
3993
                                min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3092
3994
                        max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3093
 
                        if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
 
3995
                        if(max_rice_parameter >= rice_parameter_limit) {
3094
3996
#ifdef DEBUG_VERBOSE
3095
 
                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
 
3997
                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1);
3096
3998
#endif
3097
 
                                max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
 
3999
                                max_rice_parameter = rice_parameter_limit - 1;
3098
4000
                        }
3099
4001
                }
3100
4002
                else
3101
4003
                        min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3102
4004
 
3103
 
                best_partition_bits = 0xffffffff;
3104
4005
                for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3105
 
#endif
3106
 
#ifdef VARIABLE_RICE_BITS
3107
 
#ifdef FLAC__SYMMETRIC_RICE
3108
 
                        partition_bits = (2+rice_parameter) * residual_samples;
3109
 
#else
3110
 
                        const unsigned rice_parameter_estimate = rice_parameter-1;
3111
 
                        partition_bits = (1+rice_parameter) * residual_samples;
3112
 
#endif
3113
 
#else
3114
 
                        partition_bits = 0;
3115
 
#endif
3116
 
                        partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3117
 
                        for(i = 0; i < residual_samples; i++) {
3118
 
#ifdef VARIABLE_RICE_BITS
3119
 
#ifdef FLAC__SYMMETRIC_RICE
3120
 
                                partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
3121
 
#else
3122
 
                                partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
3123
 
#endif
3124
 
#else
3125
 
                                partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3126
 
#endif
3127
 
                        }
3128
 
#ifndef NO_RICE_SEARCH
 
4006
#else
 
4007
                        rice_parameter = suggested_rice_parameter;
 
4008
#endif
 
4009
#ifdef EXACT_RICE_BITS_CALCULATION
 
4010
                        partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual);
 
4011
#else
 
4012
                        partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]);
 
4013
#endif
3129
4014
                        if(partition_bits < best_partition_bits) {
3130
4015
                                best_rice_parameter = rice_parameter;
3131
4016
                                best_partition_bits = partition_bits;
3132
4017
                        }
 
4018
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3133
4019
                }
3134
4020
#endif
3135
4021
                if(search_for_escapes) {
3136
 
                        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;
3137
 
                        if(flat_bits <= best_partition_bits) {
 
4022
                        partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
 
4023
                        if(partition_bits <= best_partition_bits) {
3138
4024
                                raw_bits[0] = raw_bits_per_partition[0];
3139
 
                                best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3140
 
                                best_partition_bits = flat_bits;
 
4025
                                best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
 
4026
                                best_partition_bits = partition_bits;
3141
4027
                        }
 
4028
                        else
 
4029
                                raw_bits[0] = 0;
3142
4030
                }
3143
4031
                parameters[0] = best_rice_parameter;
3144
4032
                bits_ += best_partition_bits;
3145
4033
        }
3146
4034
        else {
3147
 
                unsigned partition, residual_sample, save_residual_sample, partition_sample;
 
4035
                unsigned partition, residual_sample;
3148
4036
                unsigned partition_samples;
3149
4037
                FLAC__uint64 mean, k;
3150
4038
                const unsigned partitions = 1u << partition_order;
3157
4045
                                        partition_samples -= predictor_order;
3158
4046
                        }
3159
4047
                        mean = abs_residual_partition_sums[partition];
3160
 
#ifdef FLAC__SYMMETRIC_RICE
3161
 
                        mean += partition_samples >> 1; /* for rounding effect */
3162
 
                        mean /= partition_samples;
3163
 
 
3164
 
                        /* calc rice_parameter = floor(log2(mean)) */
3165
 
                        rice_parameter = 0;
3166
 
                        mean>>=1;
3167
 
                        while(mean) {
3168
 
                                rice_parameter++;
3169
 
                                mean >>= 1;
3170
 
                        }
3171
 
#else
3172
4048
                        /* we are basically calculating the size in bits of the
3173
4049
                         * average residual magnitude in the partition:
3174
4050
                         *   rice_parameter = floor(log2(mean/partition_samples))
3179
4055
                         */
3180
4056
                        for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
3181
4057
                                ;
3182
 
#endif
3183
 
                        if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
 
4058
                        if(rice_parameter >= rice_parameter_limit) {
3184
4059
#ifdef DEBUG_VERBOSE
3185
 
                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
 
4060
                                fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
3186
4061
#endif
3187
 
                                rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
 
4062
                                rice_parameter = rice_parameter_limit - 1;
3188
4063
                        }
3189
4064
 
3190
 
#ifndef NO_RICE_SEARCH
 
4065
                        best_partition_bits = (unsigned)(-1);
 
4066
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3191
4067
                        if(rice_parameter_search_dist) {
3192
4068
                                if(rice_parameter < rice_parameter_search_dist)
3193
4069
                                        min_rice_parameter = 0;
3194
4070
                                else
3195
4071
                                        min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3196
4072
                                max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3197
 
                                if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
 
4073
                                if(max_rice_parameter >= rice_parameter_limit) {
3198
4074
#ifdef DEBUG_VERBOSE
3199
 
                                        fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
 
4075
                                        fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
3200
4076
#endif
3201
 
                                        max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
 
4077
                                        max_rice_parameter = rice_parameter_limit - 1;
3202
4078
                                }
3203
4079
                        }
3204
4080
                        else
3205
4081
                                min_rice_parameter = max_rice_parameter = rice_parameter;
3206
4082
 
3207
 
                        best_partition_bits = 0xffffffff;
3208
4083
                        for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3209
4084
#endif
3210
 
#ifdef VARIABLE_RICE_BITS
3211
 
#ifdef FLAC__SYMMETRIC_RICE
3212
 
                                partition_bits = (2+rice_parameter) * partition_samples;
3213
 
#else
3214
 
                                const unsigned rice_parameter_estimate = rice_parameter-1;
3215
 
                                partition_bits = (1+rice_parameter) * partition_samples;
3216
 
#endif
3217
 
#else
3218
 
                                partition_bits = 0;
3219
 
#endif
3220
 
                                partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3221
 
                                save_residual_sample = residual_sample;
3222
 
                                for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3223
 
#ifdef VARIABLE_RICE_BITS
3224
 
#ifdef FLAC__SYMMETRIC_RICE
3225
 
                                        partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
3226
 
#else
3227
 
                                        partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3228
 
#endif
3229
 
#else
3230
 
                                        partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3231
 
#endif
3232
 
                                }
3233
 
#ifndef NO_RICE_SEARCH
3234
 
                                if(rice_parameter != max_rice_parameter)
3235
 
                                        residual_sample = save_residual_sample;
 
4085
#ifdef EXACT_RICE_BITS_CALCULATION
 
4086
                                partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample);
 
4087
#else
 
4088
                                partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]);
 
4089
#endif
3236
4090
                                if(partition_bits < best_partition_bits) {
3237
4091
                                        best_rice_parameter = rice_parameter;
3238
4092
                                        best_partition_bits = partition_bits;
3239
4093
                                }
 
4094
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3240
4095
                        }
3241
4096
#endif
3242
4097
                        if(search_for_escapes) {
3243
 
                                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;
3244
 
                                if(flat_bits <= best_partition_bits) {
 
4098
                                partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
 
4099
                                if(partition_bits <= best_partition_bits) {
3245
4100
                                        raw_bits[partition] = raw_bits_per_partition[partition];
3246
 
                                        best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3247
 
                                        best_partition_bits = flat_bits;
 
4101
                                        best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
 
4102
                                        best_partition_bits = partition_bits;
3248
4103
                                }
 
4104
                                else
 
4105
                                        raw_bits[partition] = 0;
3249
4106
                        }
3250
4107
                        parameters[partition] = best_rice_parameter;
3251
4108
                        bits_ += best_partition_bits;
 
4109
                        residual_sample += partition_samples;
3252
4110
                }
3253
4111
        }
3254
4112
 
3309
4167
        FLAC__ASSERT(fifo->tail <= fifo->size);
3310
4168
}
3311
4169
 
3312
 
FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
 
4170
FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3313
4171
{
3314
4172
        FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
3315
 
        const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
 
4173
        const size_t encoded_bytes = encoder->private_->verify.output.bytes;
3316
4174
        (void)decoder;
3317
4175
 
3318
4176
        if(encoder->private_->verify.needs_magic_hack) {
3344
4202
{
3345
4203
        FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
3346
4204
        unsigned channel;
3347
 
        const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
 
4205
        const unsigned channels = frame->header.channels;
3348
4206
        const unsigned blocksize = frame->header.blocksize;
3349
4207
        const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
3350
4208
 
 
4209
        (void)decoder;
 
4210
 
3351
4211
        for(channel = 0; channel < channels; channel++) {
3352
4212
                if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
3353
4213
                        unsigned i, sample = 0;
3374
4234
                }
3375
4235
        }
3376
4236
        /* dequeue the frame from the fifo */
3377
 
        for(channel = 0; channel < channels; channel++) {
3378
 
                memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
3379
 
        }
3380
4237
        encoder->private_->verify.input_fifo.tail -= blocksize;
 
4238
        FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
 
4239
        for(channel = 0; channel < channels; channel++)
 
4240
                memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail * sizeof(encoder->private_->verify.input_fifo.data[0][0]));
3381
4241
        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3382
4242
}
3383
4243
 
3392
4252
        (void)decoder, (void)status;
3393
4253
        encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
3394
4254
}
 
4255
 
 
4256
FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
 
4257
{
 
4258
        (void)client_data;
 
4259
 
 
4260
        *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
 
4261
        if (*bytes == 0) {
 
4262
                if (feof(encoder->private_->file))
 
4263
                        return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
 
4264
                else if (ferror(encoder->private_->file))
 
4265
                        return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
 
4266
        }
 
4267
        return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
 
4268
}
 
4269
 
 
4270
FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
 
4271
{
 
4272
        (void)client_data;
 
4273
 
 
4274
        if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
 
4275
                return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
 
4276
        else
 
4277
                return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
 
4278
}
 
4279
 
 
4280
FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
 
4281
{
 
4282
        off_t offset;
 
4283
 
 
4284
        (void)client_data;
 
4285
 
 
4286
        offset = ftello(encoder->private_->file);
 
4287
 
 
4288
        if(offset < 0) {
 
4289
                return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
 
4290
        }
 
4291
        else {
 
4292
                *absolute_byte_offset = (FLAC__uint64)offset;
 
4293
                return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
 
4294
        }
 
4295
}
 
4296
 
 
4297
#ifdef FLAC__VALGRIND_TESTING
 
4298
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
 
4299
{
 
4300
        size_t ret = fwrite(ptr, size, nmemb, stream);
 
4301
        if(!ferror(stream))
 
4302
                fflush(stream);
 
4303
        return ret;
 
4304
}
 
4305
#else
 
4306
#define local__fwrite fwrite
 
4307
#endif
 
4308
 
 
4309
FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
 
4310
{
 
4311
        (void)client_data, (void)current_frame;
 
4312
 
 
4313
        if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
 
4314
                FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
 
4315
#if FLAC__HAS_OGG
 
4316
                        /* We would like to be able to use 'samples > 0' in the
 
4317
                         * clause here but currently because of the nature of our
 
4318
                         * Ogg writing implementation, 'samples' is always 0 (see
 
4319
                         * ogg_encoder_aspect.c).  The downside is extra progress
 
4320
                         * callbacks.
 
4321
                         */
 
4322
                        encoder->private_->is_ogg? true :
 
4323
#endif
 
4324
                        samples > 0
 
4325
                );
 
4326
                if(call_it) {
 
4327
                        /* NOTE: We have to add +bytes, +samples, and +1 to the stats
 
4328
                         * because at this point in the callback chain, the stats
 
4329
                         * have not been updated.  Only after we return and control
 
4330
                         * gets back to write_frame_() are the stats updated
 
4331
                         */
 
4332
                        encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data);
 
4333
                }
 
4334
                return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
 
4335
        }
 
4336
        else
 
4337
                return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
 
4338
}
 
4339
 
 
4340
/*
 
4341
 * This will forcibly set stdout to binary mode (for OSes that require it)
 
4342
 */
 
4343
FILE *get_binary_stdout_(void)
 
4344
{
 
4345
        /* if something breaks here it is probably due to the presence or
 
4346
         * absence of an underscore before the identifiers 'setmode',
 
4347
         * 'fileno', and/or 'O_BINARY'; check your system header files.
 
4348
         */
 
4349
#if defined _MSC_VER || defined __MINGW32__
 
4350
        _setmode(_fileno(stdout), _O_BINARY);
 
4351
#elif defined __CYGWIN__
 
4352
        /* almost certainly not needed for any modern Cygwin, but let's be safe... */
 
4353
        setmode(_fileno(stdout), _O_BINARY);
 
4354
#elif defined __EMX__
 
4355
        setmode(fileno(stdout), O_BINARY);
 
4356
#endif
 
4357
 
 
4358
        return stdout;
 
4359
}