1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #if defined _WIN32 && !defined __CYGWIN__
24 /* where MSVC puts unlink() */
29 #if defined _MSC_VER || defined __MINGW32__
30 #include <sys/types.h> /* for off_t */
31 #if _MSC_VER <= 1600 /* @@@ [2G limit] */
37 #include <limits.h> /* for LONG_MAX */
38 #include <math.h> /* for floor() */
39 #include <stdio.h> /* for FILE etc. */
40 #include <stdlib.h> /* for malloc */
41 #include <string.h> /* for strcmp(), strerror() */
43 #include "share/alloc.h"
44 #include "share/grabbag.h"
50 #define min(x,y) ((x)<(y)?(x):(y))
54 #define max(x,y) ((x)>(y)?(x):(y))
56 /* this MUST be >= 588 so that sector aligning can take place with one read */
57 #define CHUNK_OF_SAMPLES 2048
65 FLAC__bool outputfile_opened; /* true if we successfully opened the output file and we want it to be deleted if there is an error */
66 const char *inbasefilename;
67 const char *infilename;
68 const char *outfilename;
71 FLAC__uint64 until; /* a value of 0 mean end-of-stream (i.e. --until=-0) */
72 FLAC__bool treat_warnings_as_errors;
73 FLAC__bool continue_through_decode_errors;
74 FLAC__bool replay_gain;
76 unsigned bits_per_sample;
78 FLAC__uint64 unencoded_size;
79 FLAC__uint64 total_samples_to_encode;
80 FLAC__uint64 bytes_written;
81 FLAC__uint64 samples_written;
84 FLAC__StreamEncoder *encoder;
87 FLAC__StreamMetadata *seek_table_template;
90 /* this is data attached to the FLAC decoder when encoding from a FLAC file */
92 EncoderSession *encoder_session;
94 const FLAC__byte *lookahead;
95 unsigned lookahead_length;
96 size_t num_metadata_blocks;
97 FLAC__StreamMetadata *metadata_blocks[1024]; /*@@@ BAD MAGIC number */
98 FLAC__uint64 samples_left_to_process;
99 FLAC__bool fatal_error;
102 const int FLAC_ENCODE__DEFAULT_PADDING = 8192;
104 static FLAC__bool is_big_endian_host_;
106 static unsigned char ucbuffer_[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*((FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE+7)/8)];
107 static signed char *scbuffer_ = (signed char *)ucbuffer_;
108 static FLAC__uint16 *usbuffer_ = (FLAC__uint16 *)ucbuffer_;
109 static FLAC__int16 *ssbuffer_ = (FLAC__int16 *)ucbuffer_;
111 static FLAC__int32 in_[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
112 static FLAC__int32 *input_[FLAC__MAX_CHANNELS];
116 * unpublished debug routines from the FLAC libs
118 extern FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
119 extern FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
120 extern FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
121 extern FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value);
126 static FLAC__bool EncoderSession_construct(EncoderSession *e, FLAC__bool use_ogg, FLAC__bool verify, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FILE *infile, const char *infilename, const char *outfilename);
127 static void EncoderSession_destroy(EncoderSession *e);
128 static int EncoderSession_finish_ok(EncoderSession *e, int info_align_carry, int info_align_zero, foreign_metadata_t *foreign_metadata);
129 static int EncoderSession_finish_error(EncoderSession *e);
130 static FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options, FLAC__uint32 channel_mask, unsigned channels, unsigned bps, unsigned sample_rate, const foreign_metadata_t *foreign_metadata, FLACDecoderData *flac_decoder_data);
131 static FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples);
132 static FLAC__bool convert_to_seek_table_template(const char *requested_seek_points, int num_requested_seek_points, FLAC__StreamMetadata *cuesheet, EncoderSession *e);
133 static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
134 static FLAC__bool verify_metadata(const EncoderSession *e, FLAC__StreamMetadata **metadata, unsigned num_metadata);
135 static FLAC__bool format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned shift, size_t *channel_map);
136 static void encoder_progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data);
137 static FLAC__StreamDecoderReadStatus flac_decoder_read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
138 static FLAC__StreamDecoderSeekStatus flac_decoder_seek_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
139 static FLAC__StreamDecoderTellStatus flac_decoder_tell_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
140 static FLAC__StreamDecoderLengthStatus flac_decoder_length_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
141 static FLAC__bool flac_decoder_eof_callback(const FLAC__StreamDecoder *decoder, void *client_data);
142 static FLAC__StreamDecoderWriteStatus flac_decoder_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
143 static void flac_decoder_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
144 static void flac_decoder_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
145 static FLAC__bool parse_cuesheet(FLAC__StreamMetadata **cuesheet, const char *cuesheet_filename, const char *inbasefilename, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset, FLAC__bool treat_warnings_as_errors);
146 static void print_stats(const EncoderSession *encoder_session);
147 static void print_error_with_init_status(const EncoderSession *e, const char *message, FLAC__StreamEncoderInitStatus init_status);
148 static void print_error_with_state(const EncoderSession *e, const char *message);
149 static void print_verify_error(EncoderSession *e);
150 static FLAC__bool read_little_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn);
151 static FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
152 static FLAC__bool read_big_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn);
153 static FLAC__bool read_big_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
154 static FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
155 static FLAC__bool fskip_ahead(FILE *f, FLAC__uint64 offset);
156 static unsigned count_channel_mask_bits(FLAC__uint32 mask);
158 static FLAC__uint32 limit_channel_mask(FLAC__uint32 mask, unsigned channels);
164 int flac__encode_aif(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options, FLAC__bool is_aifc)
166 EncoderSession encoder_session;
169 unsigned int channels= 0U, bps= 0U, shift= 0U, sample_rate= 0U, sample_frames= 0U;
170 size_t channel_map[FLAC__MAX_CHANNELS];
171 FLAC__bool got_comm_chunk= false, got_ssnd_chunk= false;
172 int info_align_carry= -1, info_align_zero= -1;
173 FLAC__bool is_big_endian_pcm = true;
175 (void)infilesize; /* silence compiler warning about unused parameter */
176 (void)lookahead; /* silence compiler warning about unused parameter */
177 (void)lookahead_length; /* silence compiler warning about unused parameter */
180 EncoderSession_construct(
183 options.common.use_ogg,
187 options.common.verify,
188 options.common.treat_warnings_as_errors,
189 options.common.continue_through_decode_errors,
197 /* initialize default channel map that preserves channel order */
200 for(i = 0; i < sizeof(channel_map)/sizeof(channel_map[0]); i++)
204 if(options.foreign_metadata) {
206 if(!flac__foreign_metadata_read_from_aiff(options.foreign_metadata, infilename, &error)) {
207 flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", encoder_session.inbasefilename, error);
208 return EncoderSession_finish_error(&encoder_session);
212 /* lookahead[] already has "FORMxxxxAIFF", do sub-chunks */
216 char chunk_id[5] = { '\0', '\0', '\0', '\0', '\0' }; /* one extra byte for terminating NUL so we can also treat it like a C string */
218 /* chunk identifier; really conservative about behavior of fread() and feof() */
219 if(feof(infile) || ((c= fread(chunk_id, 1U, 4U, infile)), c==0U && feof(infile)))
221 else if(c<4U || feof(infile)) {
222 flac__utils_printf(stderr, 1, "%s: ERROR: incomplete chunk identifier\n", encoder_session.inbasefilename);
223 return EncoderSession_finish_error(&encoder_session);
226 if(got_comm_chunk==false && !memcmp(chunk_id, "COMM", 4)) { /* common chunk */
228 const FLAC__uint32 minimum_comm_size = (is_aifc? 22 : 18);
230 /* COMM chunk size */
231 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
232 return EncoderSession_finish_error(&encoder_session);
233 else if(xx<minimum_comm_size) {
234 flac__utils_printf(stderr, 1, "%s: ERROR: non-standard %s 'COMM' chunk has length = %u\n", encoder_session.inbasefilename, is_aifc? "AIFF-C" : "AIFF", (unsigned int)xx);
235 return EncoderSession_finish_error(&encoder_session);
237 else if(!is_aifc && xx!=minimum_comm_size) {
238 flac__utils_printf(stderr, 1, "%s: WARNING: non-standard %s 'COMM' chunk has length = %u, expected %u\n", encoder_session.inbasefilename, is_aifc? "AIFF-C" : "AIFF", (unsigned int)xx, minimum_comm_size);
239 if(encoder_session.treat_warnings_as_errors)
240 return EncoderSession_finish_error(&encoder_session);
242 skip= (xx-minimum_comm_size)+(xx & 1U);
244 /* number of channels */
245 if(!read_big_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
246 return EncoderSession_finish_error(&encoder_session);
247 else if(x==0U || x>FLAC__MAX_CHANNELS) {
248 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u\n", encoder_session.inbasefilename, (unsigned int)x);
249 return EncoderSession_finish_error(&encoder_session);
251 else if(x>2U && !options.common.channel_map_none) {
252 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u for AIFF\n", encoder_session.inbasefilename, (unsigned int)x);
253 return EncoderSession_finish_error(&encoder_session);
255 else if(options.common.sector_align && x!=2U) {
256 flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
257 return EncoderSession_finish_error(&encoder_session);
261 /* number of sample frames */
262 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
263 return EncoderSession_finish_error(&encoder_session);
266 /* bits per sample */
267 if(!read_big_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
268 return EncoderSession_finish_error(&encoder_session);
269 else if(x<4U || x>24U) {
270 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits-per-sample %u\n", encoder_session.inbasefilename, (unsigned int)x);
271 return EncoderSession_finish_error(&encoder_session);
273 else if(options.common.sector_align && x!=16U) {
274 flac__utils_printf(stderr, 1, "%s: ERROR: file has %u bits-per-sample, must be 16 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
275 return EncoderSession_finish_error(&encoder_session);
278 shift= (bps%8)? 8-(bps%8) : 0; /* SSND data is always byte-aligned, left-justified but format_input() will double-check */
282 if(!read_sane_extended(infile, &xx, false, encoder_session.inbasefilename))
283 return EncoderSession_finish_error(&encoder_session);
284 else if(!FLAC__format_sample_rate_is_valid(xx)) {
285 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, (unsigned int)xx);
286 return EncoderSession_finish_error(&encoder_session);
288 else if(options.common.sector_align && xx!=44100U) {
289 flac__utils_printf(stderr, 1, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)xx);
290 return EncoderSession_finish_error(&encoder_session);
294 /* check compression type for AIFF-C */
296 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
297 return EncoderSession_finish_error(&encoder_session);
298 if(xx == 0x736F7774) /* "sowt" */
299 is_big_endian_pcm = false;
300 else if(xx == 0x4E4F4E45) /* "NONE" */
301 ; /* nothing to do, we already default to big-endian */
303 flac__utils_printf(stderr, 1, "%s: ERROR: can't handle AIFF-C compression type \"%c%c%c%c\"\n", encoder_session.inbasefilename, (char)(xx>>24), (char)((xx>>16)&8), (char)((xx>>8)&8), (char)(xx&8));
304 return EncoderSession_finish_error(&encoder_session);
308 /* set channel mapping */
309 /* FLAC order follows SMPTE and WAVEFORMATEXTENSIBLE but with fewer channels, which are: */
310 /* front left, front right, center, LFE, back left, back right, surround left, surround right */
311 /* specs say the channel ordering is:
313 * ___________________________________________________
317 * quad (ambiguous with 4ch) Fl Fr Bl Br
320 * l:left r:right c:center Fl:front-left Fr:front-right Bl:back-left Br:back-right Lc:left-center Rc:right-center S:surround
321 * so we only have unambiguous mappings for 2, 3, and 5 channels
324 options.common.channel_map_none ||
325 channels == 1 || /* 1 channel: (mono) */
326 channels == 2 || /* 2 channels: left, right */
327 channels == 3 || /* 3 channels: left, right, center */
328 channels == 5 /* 5 channels: front left, front right, center, surround left, surround right */
330 /* keep default channel order */
333 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u for AIFF\n", encoder_session.inbasefilename, channels);
334 return EncoderSession_finish_error(&encoder_session);
337 /* skip any extra data in the COMM chunk */
338 if(!fskip_ahead(infile, skip)) {
339 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping extra COMM data\n", encoder_session.inbasefilename);
340 return EncoderSession_finish_error(&encoder_session);
344 * now that we know the sample rate, canonicalize the
345 * --skip string to a number of samples:
347 flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, sample_rate);
348 FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
349 encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
350 FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
352 got_comm_chunk= true;
354 else if(got_ssnd_chunk==false && !memcmp(chunk_id, "SSND", 4)) { /* sound data chunk */
355 unsigned int offset= 0U, block_size= 0U, align_remainder= 0U, data_bytes;
356 const size_t bytes_per_frame= channels*(bps>>3);
357 FLAC__uint64 total_samples_in_input, trim = 0;
358 FLAC__bool pad= false;
360 if(got_comm_chunk==false) {
361 flac__utils_printf(stderr, 1, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", encoder_session.inbasefilename);
362 return EncoderSession_finish_error(&encoder_session);
365 /* SSND chunk size */
366 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
367 return EncoderSession_finish_error(&encoder_session);
368 if(options.common.ignore_chunk_sizes) {
369 FLAC__ASSERT(!options.common.sector_align);
370 data_bytes = (unsigned)(-(int)bytes_per_frame); /* max out data_bytes; we'll use EOF as signal to stop reading */
374 data_bytes-= 8U; /* discount the offset and block size fields */
376 pad= (data_bytes & 1U) ? true : false;
379 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
380 return EncoderSession_finish_error(&encoder_session);
385 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
386 return EncoderSession_finish_error(&encoder_session);
388 flac__utils_printf(stderr, 1, "%s: ERROR: block size is %u; must be 0\n", encoder_session.inbasefilename, (unsigned int)xx);
389 return EncoderSession_finish_error(&encoder_session);
393 /* skip any SSND offset bytes */
394 FLAC__ASSERT(offset<=LONG_MAX);
395 if(!fskip_ahead(infile, offset)) {
396 flac__utils_printf(stderr, 1, "%s: ERROR: skipping offset in SSND chunk\n", encoder_session.inbasefilename);
397 return EncoderSession_finish_error(&encoder_session);
399 if(data_bytes!=(sample_frames*bytes_per_frame)) {
400 flac__utils_printf(stderr, 1, "%s: ERROR: SSND chunk size inconsistent with sample frame count\n", encoder_session.inbasefilename);
401 return EncoderSession_finish_error(&encoder_session);
404 /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
405 FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
406 total_samples_in_input = data_bytes / bytes_per_frame + *options.common.align_reservoir_samples;
409 * now that we know the input size, canonicalize the
410 * --until string to an absolute sample number:
412 if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, sample_rate, encoder_session.skip, total_samples_in_input))
413 return EncoderSession_finish_error(&encoder_session);
414 encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
415 FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
417 if(encoder_session.skip>0U) {
418 if(!fskip_ahead(infile, encoder_session.skip*bytes_per_frame)) {
419 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
420 return EncoderSession_finish_error(&encoder_session);
424 data_bytes-= (unsigned int)encoder_session.skip*bytes_per_frame; /*@@@ WATCHOUT: 4GB limit */
425 if(options.common.ignore_chunk_sizes) {
426 encoder_session.total_samples_to_encode= 0;
427 flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
428 FLAC__ASSERT(0 == encoder_session.until);
431 encoder_session.total_samples_to_encode= total_samples_in_input - encoder_session.skip;
433 if(encoder_session.until > 0) {
434 trim = total_samples_in_input - encoder_session.until;
435 FLAC__ASSERT(total_samples_in_input > 0);
436 FLAC__ASSERT(!options.common.sector_align);
437 data_bytes-= (unsigned int)trim*bytes_per_frame;
438 encoder_session.total_samples_to_encode-= trim;
440 if(options.common.sector_align) {
441 align_remainder= (unsigned int)(encoder_session.total_samples_to_encode % 588U);
442 if(options.common.is_last_file)
443 encoder_session.total_samples_to_encode+= (588U-align_remainder); /* will pad with zeroes */
445 encoder_session.total_samples_to_encode-= align_remainder; /* will stop short and carry over to next file */
448 /* +54 for the size of the AIFF headers; this is just an estimate for the progress indicator and doesn't need to be exact */
449 encoder_session.unencoded_size= encoder_session.total_samples_to_encode*bytes_per_frame+54;
451 if(!EncoderSession_init_encoder(&encoder_session, options.common, /*channel_mask=*/0, channels, bps-shift, sample_rate, options.foreign_metadata, /*flac_decoder_data=*/0))
452 return EncoderSession_finish_error(&encoder_session);
454 /* first do any samples in the reservoir */
455 if(options.common.sector_align && *options.common.align_reservoir_samples>0U) {
457 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
458 print_error_with_state(&encoder_session, "ERROR during encoding");
459 return EncoderSession_finish_error(&encoder_session);
463 /* decrement the data_bytes counter if we need to align the file */
464 if(options.common.sector_align) {
465 if(options.common.is_last_file)
466 *options.common.align_reservoir_samples= 0U;
468 *options.common.align_reservoir_samples= align_remainder;
469 data_bytes-= (*options.common.align_reservoir_samples)*bytes_per_frame;
473 /* now do from the file */
474 while(data_bytes>0) {
475 size_t bytes_read= fread(ucbuffer_, 1U, min(data_bytes, CHUNK_OF_SAMPLES*bytes_per_frame), infile);
479 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
480 return EncoderSession_finish_error(&encoder_session);
482 else if(feof(infile)) {
483 if(options.common.ignore_chunk_sizes) {
484 flac__utils_printf(stderr, 1, "%s: INFO: hit EOF with --ignore-chunk-sizes, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.samples_written);
487 flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
488 if(encoder_session.treat_warnings_as_errors)
489 return EncoderSession_finish_error(&encoder_session);
495 if(bytes_read % bytes_per_frame != 0U) {
496 flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
497 return EncoderSession_finish_error(&encoder_session);
500 unsigned int frames= bytes_read/bytes_per_frame;
501 if(!format_input(input_, frames, is_big_endian_pcm, /*is_unsigned_samples=*/false, channels, bps, shift, channel_map))
502 return EncoderSession_finish_error(&encoder_session);
504 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, frames)) {
505 print_error_with_state(&encoder_session, "ERROR during encoding");
506 return EncoderSession_finish_error(&encoder_session);
509 data_bytes-= bytes_read;
515 FLAC__ASSERT(!options.common.sector_align);
516 if(!fskip_ahead(infile, trim*bytes_per_frame)) {
517 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
518 return EncoderSession_finish_error(&encoder_session);
522 /* now read unaligned samples into reservoir or pad with zeroes if necessary */
523 if(options.common.sector_align) {
524 if(options.common.is_last_file) {
525 unsigned int pad_frames= 588U-align_remainder;
527 if(pad_frames<588U) {
530 info_align_zero= pad_frames;
531 for(i= 0U; i<channels; ++i)
532 memset(input_[i], 0, sizeof(input_[0][0])*pad_frames);
534 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, pad_frames)) {
535 print_error_with_state(&encoder_session, "ERROR during encoding");
536 return EncoderSession_finish_error(&encoder_session);
541 if(*options.common.align_reservoir_samples > 0) {
542 size_t bytes_read= fread(ucbuffer_, 1U, (*options.common.align_reservoir_samples)*bytes_per_frame, infile);
544 FLAC__ASSERT(CHUNK_OF_SAMPLES>=588U);
545 if(bytes_read==0U && ferror(infile)) {
546 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
547 return EncoderSession_finish_error(&encoder_session);
549 else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_frame) {
550 flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned int)bytes_read, (unsigned int)encoder_session.total_samples_to_encode, (unsigned int)encoder_session.samples_written);
551 if(encoder_session.treat_warnings_as_errors)
552 return EncoderSession_finish_error(&encoder_session);
555 info_align_carry= *options.common.align_reservoir_samples;
556 if(!format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, is_big_endian_pcm, /*is_unsigned_samples=*/false, channels, bps, shift, channel_map))
557 return EncoderSession_finish_error(&encoder_session);
566 if(fread(&tmp, 1U, 1U, infile)<1U) {
567 flac__utils_printf(stderr, 1, "%s: ERROR during read of SSND pad byte\n", encoder_session.inbasefilename);
568 return EncoderSession_finish_error(&encoder_session);
572 got_ssnd_chunk= true;
574 else { /* other chunk */
575 if(!memcmp(chunk_id, "COMM", 4)) {
576 flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'COMM' chunk\n", encoder_session.inbasefilename);
577 if(encoder_session.treat_warnings_as_errors)
578 return EncoderSession_finish_error(&encoder_session);
580 else if(!memcmp(chunk_id, "SSND", 4)) {
581 flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'SSND' chunk\n", encoder_session.inbasefilename);
582 if(encoder_session.treat_warnings_as_errors)
583 return EncoderSession_finish_error(&encoder_session);
585 else if(!options.foreign_metadata) {
586 flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s'\n", encoder_session.inbasefilename, chunk_id);
587 if(encoder_session.treat_warnings_as_errors)
588 return EncoderSession_finish_error(&encoder_session);
592 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
593 return EncoderSession_finish_error(&encoder_session);
595 unsigned long skip= xx+(xx & 1U);
597 FLAC__ASSERT(skip<=LONG_MAX);
598 if(!fskip_ahead(infile, skip)) {
599 fprintf(stderr, "%s: ERROR during read while skipping unknown chunk\n", encoder_session.inbasefilename);
600 return EncoderSession_finish_error(&encoder_session);
606 if(got_ssnd_chunk==false && sample_frames!=0U) {
607 flac__utils_printf(stderr, 1, "%s: ERROR: missing SSND chunk\n", encoder_session.inbasefilename);
608 return EncoderSession_finish_error(&encoder_session);
611 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, options.foreign_metadata);
614 int flac__encode_wav(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options)
616 EncoderSession encoder_session;
617 FLAC__bool is_unsigned_samples = false;
618 unsigned channels = 0, bps = 0, sample_rate = 0, shift = 0;
620 size_t channel_map[FLAC__MAX_CHANNELS];
621 FLAC__uint16 x, format; /* format is the wFormatTag word from the 'fmt ' chunk */
622 FLAC__uint32 xx, channel_mask = 0;
623 FLAC__bool got_fmt_chunk = false, got_data_chunk = false;
624 unsigned align_remainder = 0;
625 int info_align_carry = -1, info_align_zero = -1;
629 (void)lookahead_length;
632 EncoderSession_construct(
635 options.common.use_ogg,
639 options.common.verify,
640 options.common.treat_warnings_as_errors,
641 options.common.continue_through_decode_errors,
649 /* initialize default channel map that preserves channel order */
652 for(i = 0; i < sizeof(channel_map)/sizeof(channel_map[0]); i++)
656 if(options.foreign_metadata) {
658 if(!flac__foreign_metadata_read_from_wave(options.foreign_metadata, infilename, &error)) {
659 flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", encoder_session.inbasefilename, error);
660 return EncoderSession_finish_error(&encoder_session);
665 * lookahead[] already has "RIFFxxxxWAVE", do sub-chunks
667 while(!feof(infile)) {
668 if(!read_little_endian_uint32(infile, &xx, true, encoder_session.inbasefilename))
669 return EncoderSession_finish_error(&encoder_session);
672 if(xx == 0x20746d66 && !got_fmt_chunk) { /* "fmt " */
673 unsigned block_align, data_bytes;
676 * http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
677 * http://windowssdk.msdn.microsoft.com/en-us/library/ms713497.aspx
678 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/audio_r/hh/Audio_r/aud-prop_d40f094e-44f9-4baa-8a15-03e4fb369501.xml.asp
681 * 4 byte: subchunk size
682 * 2 byte: format type: 1 for WAVE_FORMAT_PCM, 65534 for WAVE_FORMAT_EXTENSIBLE
684 * 4 byte: sample rate (Hz)
685 * 4 byte: avg bytes per sec
686 * 2 byte: block align
687 * 2 byte: bits per sample (not necessarily all significant)
689 * 2 byte: extension size in bytes (usually 0 for WAVEFORMATEX and 22 for WAVEFORMATEXTENSIBLE with PCM)
690 * WAVEFORMATEXTENSIBLE adds
691 * 2 byte: valid bits per sample
692 * 4 byte: channel mask
693 * 16 byte: subformat GUID, first 2 bytes have format type, 1 being PCM
695 * Current spec says WAVEFORMATEX with PCM must have bps == 8 or 16, or any multiple of 8 for WAVEFORMATEXTENSIBLE.
696 * Lots of old broken WAVEs/apps have don't follow it, e.g. 20 bps but a block align of 3/6 for mono/stereo.
698 * Block align for WAVE_FORMAT_PCM or WAVE_FORMAT_EXTENSIBLE is also supposed to be channels*bps/8
700 * If the channel mask has more set bits than # of channels, the extra MSBs are ignored.
701 * If the channel mask has less set bits than # of channels, the extra channels are unassigned to any speaker.
703 * Data is supposed to be unsigned for bps <= 8 else signed.
706 /* fmt sub-chunk size */
707 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
708 return EncoderSession_finish_error(&encoder_session);
710 if(data_bytes < 16) {
711 flac__utils_printf(stderr, 1, "%s: ERROR: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_session.inbasefilename, data_bytes);
712 return EncoderSession_finish_error(&encoder_session);
715 if(!read_little_endian_uint16(infile, &format, false, encoder_session.inbasefilename))
716 return EncoderSession_finish_error(&encoder_session);
717 if(format != 1 /*WAVE_FORMAT_PCM*/ && format != 65534 /*WAVE_FORMAT_EXTENSIBLE*/) {
718 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported format type %u\n", encoder_session.inbasefilename, (unsigned)format);
719 return EncoderSession_finish_error(&encoder_session);
721 /* number of channels */
722 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
723 return EncoderSession_finish_error(&encoder_session);
724 channels = (unsigned)x;
725 if(channels == 0 || channels > FLAC__MAX_CHANNELS) {
726 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number of channels %u\n", encoder_session.inbasefilename, channels);
727 return EncoderSession_finish_error(&encoder_session);
729 else if(options.common.sector_align && channels != 2) {
730 flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, channels);
731 return EncoderSession_finish_error(&encoder_session);
734 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
735 return EncoderSession_finish_error(&encoder_session);
737 if(!FLAC__format_sample_rate_is_valid(sample_rate)) {
738 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, sample_rate);
739 return EncoderSession_finish_error(&encoder_session);
741 else if(options.common.sector_align && sample_rate != 44100) {
742 flac__utils_printf(stderr, 1, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.inbasefilename, sample_rate);
743 return EncoderSession_finish_error(&encoder_session);
745 /* avg bytes per second (ignored) */
746 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
747 return EncoderSession_finish_error(&encoder_session);
749 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
750 return EncoderSession_finish_error(&encoder_session);
751 block_align = (unsigned)x;
752 /* bits per sample */
753 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
754 return EncoderSession_finish_error(&encoder_session);
756 is_unsigned_samples = (bps <= 8);
758 if(bps != 8 && bps != 16) {
759 if(bps == 24 || bps == 32) {
760 /* let these slide with a warning since they're unambiguous */
761 flac__utils_printf(stderr, 1, "%s: WARNING: legacy WAVE file has format type %u but bits-per-sample=%u\n", encoder_session.inbasefilename, (unsigned)format, bps);
762 if(encoder_session.treat_warnings_as_errors)
763 return EncoderSession_finish_error(&encoder_session);
766 /* @@@ we could add an option to specify left- or right-justified blocks so we knew how to set 'shift' */
767 flac__utils_printf(stderr, 1, "%s: ERROR: legacy WAVE file has format type %u but bits-per-sample=%u\n", encoder_session.inbasefilename, (unsigned)format, bps);
768 return EncoderSession_finish_error(&encoder_session);
771 #if 0 /* @@@ reinstate once we can get an answer about whether the samples are left- or right-justified */
772 if((bps+7)/8 * channels == block_align) {
774 /* assume legacy file is byte aligned with some LSBs zero; this is double-checked in format_input() */
775 flac__utils_printf(stderr, 1, "%s: WARNING: legacy WAVE file (format type %d) has block alignment=%u, bits-per-sample=%u, channels=%u\n", encoder_session.inbasefilename, (unsigned)format, block_align, bps, channels);
776 if(encoder_session.treat_warnings_as_errors)
777 return EncoderSession_finish_error(&encoder_session);
778 shift = 8 - (bps % 8);
785 flac__utils_printf(stderr, 1, "%s: ERROR: illegal WAVE file (format type %d) has block alignment=%u, bits-per-sample=%u, channels=%u\n", encoder_session.inbasefilename, (unsigned)format, block_align, bps, channels);
786 return EncoderSession_finish_error(&encoder_session);
791 if(channels > 2 && !options.common.channel_map_none) {
792 flac__utils_printf(stderr, 1, "%s: ERROR: WAVE has >2 channels but is not WAVE_FORMAT_EXTENSIBLE; cannot assign channels\n", encoder_session.inbasefilename);
793 return EncoderSession_finish_error(&encoder_session);
795 FLAC__ASSERT(data_bytes >= 16);
799 if(data_bytes < 40) {
800 flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with size %u\n", encoder_session.inbasefilename, data_bytes);
801 return EncoderSession_finish_error(&encoder_session);
804 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
805 return EncoderSession_finish_error(&encoder_session);
807 flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with cbSize %u\n", encoder_session.inbasefilename, (unsigned)x);
808 return EncoderSession_finish_error(&encoder_session);
811 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
812 return EncoderSession_finish_error(&encoder_session);
813 if((unsigned)x > bps) {
814 flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with wValidBitsPerSample (%u) > wBitsPerSample (%u)\n", encoder_session.inbasefilename, (unsigned)x, bps);
815 return EncoderSession_finish_error(&encoder_session);
817 shift = bps - (unsigned)x;
819 if(!read_little_endian_uint32(infile, &channel_mask, false, encoder_session.inbasefilename))
820 return EncoderSession_finish_error(&encoder_session);
821 /* for mono/stereo and unassigned channels, we fake the mask */
822 if(channel_mask == 0) {
824 channel_mask = 0x0001;
825 else if(channels == 2)
826 channel_mask = 0x0003;
828 /* set channel mapping */
829 /* FLAC order follows SMPTE and WAVEFORMATEXTENSIBLE but with fewer channels, which are: */
830 /* front left, front right, center, LFE, back left, back right, surround left, surround right */
831 /* the default mapping is sufficient for 1-6 channels and 7-8 are currently unspecified anyway */
833 /* @@@ example for dolby/vorbis order, for reference later in case it becomes important */
835 options.common.channel_map_none ||
836 channel_mask == 0x0001 || /* 1 channel: (mono) */
837 channel_mask == 0x0003 || /* 2 channels: front left, front right */
838 channel_mask == 0x0033 || /* 4 channels: front left, front right, back left, back right */
839 channel_mask == 0x0603 /* 4 channels: front left, front right, side left, side right */
841 /* keep default channel order */
844 channel_mask == 0x0007 || /* 3 channels: front left, front right, front center */
845 channel_mask == 0x0037 || /* 5 channels: front left, front right, front center, back left, back right */
846 channel_mask == 0x0607 /* 5 channels: front left, front right, front center, side left, side right */
848 /* to dolby order: front left, center, front right [, surround left, surround right ] */
853 channel_mask == 0x003f || /* 6 channels: front left, front right, front center, LFE, back left, back right */
854 channel_mask == 0x060f /* 6 channels: front left, front right, front center, LFE, side left, side right */
856 /* to dolby order: front left, center, front right, surround left, surround right, LFE */
865 options.common.channel_map_none ||
866 channel_mask == 0x0001 || /* 1 channel: (mono) */
867 channel_mask == 0x0003 || /* 2 channels: front left, front right */
868 channel_mask == 0x0007 || /* 3 channels: front left, front right, front center */
869 channel_mask == 0x0033 || /* 4 channels: front left, front right, back left, back right */
870 channel_mask == 0x0603 || /* 4 channels: front left, front right, side left, side right */
871 channel_mask == 0x0037 || /* 5 channels: front left, front right, front center, back left, back right */
872 channel_mask == 0x0607 || /* 5 channels: front left, front right, front center, side left, side right */
873 channel_mask == 0x003f || /* 6 channels: front left, front right, front center, LFE, back left, back right */
874 channel_mask == 0x060f /* 6 channels: front left, front right, front center, LFE, side left, side right */
876 /* keep default channel order */
880 flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk with unsupported channel mask=0x%04X\n", encoder_session.inbasefilename, (unsigned)channel_mask);
881 return EncoderSession_finish_error(&encoder_session);
883 if(!options.common.channel_map_none) {
884 if(count_channel_mask_bits(channel_mask) < channels) {
885 flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk: channel mask 0x%04X has unassigned channels (#channels=%u)\n", encoder_session.inbasefilename, (unsigned)channel_mask, channels);
886 return EncoderSession_finish_error(&encoder_session);
889 /* supporting this is too difficult with channel mapping; e.g. what if mask is 0x003f but #channels=4?
890 * there would be holes in the order that would have to be filled in, or the mask would have to be
891 * limited and the logic above rerun to see if it still fits into the FLAC mapping.
893 else if(count_channel_mask_bits(channel_mask) > channels)
894 channel_mask = limit_channel_mask(channel_mask, channels);
896 else if(count_channel_mask_bits(channel_mask) > channels) {
897 flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk: channel mask 0x%04X has extra bits for non-existant channels (#channels=%u)\n", encoder_session.inbasefilename, (unsigned)channel_mask, channels);
898 return EncoderSession_finish_error(&encoder_session);
902 /* first part of GUID */
903 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
904 return EncoderSession_finish_error(&encoder_session);
906 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported WAVEFORMATEXTENSIBLE chunk with non-PCM format %u\n", encoder_session.inbasefilename, (unsigned)x);
907 return EncoderSession_finish_error(&encoder_session);
912 if(bps-shift < 4 || bps-shift > 24) {
913 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits-per-sample %u\n", encoder_session.inbasefilename, bps-shift);
914 return EncoderSession_finish_error(&encoder_session);
916 else if(options.common.sector_align && bps-shift != 16) {
917 flac__utils_printf(stderr, 1, "%s: ERROR: file has %u bits-per-sample, must be 16 for --sector-align\n", encoder_session.inbasefilename, bps-shift);
918 return EncoderSession_finish_error(&encoder_session);
921 /* skip any extra data in the fmt sub-chunk */
922 if(!fskip_ahead(infile, data_bytes)) {
923 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping extra 'fmt' data\n", encoder_session.inbasefilename);
924 return EncoderSession_finish_error(&encoder_session);
928 * now that we know the sample rate, canonicalize the
929 * --skip string to a number of samples:
931 flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, sample_rate);
932 FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
933 encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
934 FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
936 got_fmt_chunk = true;
938 else if(xx == 0x61746164 && !got_data_chunk && got_fmt_chunk) { /* "data" */
939 FLAC__uint64 total_samples_in_input, trim = 0;
940 FLAC__bool pad = false;
941 const size_t bytes_per_wide_sample = channels * (bps >> 3);
945 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
946 return EncoderSession_finish_error(&encoder_session);
947 if(options.common.ignore_chunk_sizes) {
948 FLAC__ASSERT(!options.common.sector_align);
949 data_bytes = (unsigned)(-(int)bytes_per_wide_sample); /* max out data_bytes; we'll use EOF as signal to stop reading */
953 if(0 == data_bytes) {
954 flac__utils_printf(stderr, 1, "%s: ERROR: 'data' subchunk has size of 0\n", encoder_session.inbasefilename);
955 return EncoderSession_finish_error(&encoder_session);
958 pad = (data_bytes & 1U) ? true : false;
960 /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
961 FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
962 total_samples_in_input = data_bytes / bytes_per_wide_sample + *options.common.align_reservoir_samples;
965 * now that we know the input size, canonicalize the
966 * --until string to an absolute sample number:
968 if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, sample_rate, encoder_session.skip, total_samples_in_input))
969 return EncoderSession_finish_error(&encoder_session);
970 encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
971 FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
973 if(encoder_session.skip > 0) {
974 if(!fskip_ahead(infile, encoder_session.skip * bytes_per_wide_sample)) {
975 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
976 return EncoderSession_finish_error(&encoder_session);
980 data_bytes -= (unsigned)encoder_session.skip * bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */
981 if(options.common.ignore_chunk_sizes) {
982 encoder_session.total_samples_to_encode = 0;
983 flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
984 FLAC__ASSERT(0 == encoder_session.until);
987 encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
989 if(encoder_session.until > 0) {
990 trim = total_samples_in_input - encoder_session.until;
991 FLAC__ASSERT(total_samples_in_input > 0);
992 FLAC__ASSERT(!options.common.sector_align);
993 data_bytes -= (unsigned int)trim * bytes_per_wide_sample;
994 encoder_session.total_samples_to_encode -= trim;
996 if(options.common.sector_align) {
997 align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
998 if(options.common.is_last_file)
999 encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
1001 encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
1004 /* +44 for the size of the WAV headers; this is just an estimate for the progress indicator and doesn't need to be exact */
1005 encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample + 44;
1007 if(!EncoderSession_init_encoder(&encoder_session, options.common, channel_mask, channels, bps-shift, sample_rate, options.foreign_metadata, /*flac_decoder_data=*/0))
1008 return EncoderSession_finish_error(&encoder_session);
1011 * first do any samples in the reservoir
1013 if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
1014 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
1015 print_error_with_state(&encoder_session, "ERROR during encoding");
1016 return EncoderSession_finish_error(&encoder_session);
1021 * decrement the data_bytes counter if we need to align the file
1023 if(options.common.sector_align) {
1024 if(options.common.is_last_file) {
1025 *options.common.align_reservoir_samples = 0;
1028 *options.common.align_reservoir_samples = align_remainder;
1029 data_bytes -= (*options.common.align_reservoir_samples) * bytes_per_wide_sample;
1034 * now do from the file
1036 while(data_bytes > 0) {
1037 bytes_read = fread(ucbuffer_, sizeof(unsigned char), min(data_bytes, CHUNK_OF_SAMPLES * bytes_per_wide_sample), infile);
1038 if(bytes_read == 0) {
1039 if(ferror(infile)) {
1040 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1041 return EncoderSession_finish_error(&encoder_session);
1043 else if(feof(infile)) {
1044 if(options.common.ignore_chunk_sizes) {
1045 flac__utils_printf(stderr, 1, "%s: INFO: hit EOF with --ignore-chunk-sizes, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.samples_written);
1048 flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
1049 if(encoder_session.treat_warnings_as_errors)
1050 return EncoderSession_finish_error(&encoder_session);
1056 if(bytes_read % bytes_per_wide_sample != 0) {
1057 flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
1058 return EncoderSession_finish_error(&encoder_session);
1061 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
1062 if(!format_input(input_, wide_samples, /*is_big_endian=*/false, is_unsigned_samples, channels, bps, shift, channel_map))
1063 return EncoderSession_finish_error(&encoder_session);
1065 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1066 print_error_with_state(&encoder_session, "ERROR during encoding");
1067 return EncoderSession_finish_error(&encoder_session);
1069 data_bytes -= bytes_read;
1075 FLAC__ASSERT(!options.common.sector_align);
1076 if(!fskip_ahead(infile, trim * bytes_per_wide_sample)) {
1077 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
1078 return EncoderSession_finish_error(&encoder_session);
1083 * now read unaligned samples into reservoir or pad with zeroes if necessary
1085 if(options.common.sector_align) {
1086 if(options.common.is_last_file) {
1087 unsigned wide_samples = 588 - align_remainder;
1088 if(wide_samples < 588) {
1091 info_align_zero = wide_samples;
1092 for(channel = 0; channel < channels; channel++)
1093 memset(input_[channel], 0, sizeof(input_[0][0]) * wide_samples);
1095 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1096 print_error_with_state(&encoder_session, "ERROR during encoding");
1097 return EncoderSession_finish_error(&encoder_session);
1102 if(*options.common.align_reservoir_samples > 0) {
1103 FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588);
1104 bytes_read = fread(ucbuffer_, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile);
1105 if(bytes_read == 0 && ferror(infile)) {
1106 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1107 return EncoderSession_finish_error(&encoder_session);
1109 else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
1110 flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
1111 if(encoder_session.treat_warnings_as_errors)
1112 return EncoderSession_finish_error(&encoder_session);
1115 info_align_carry = *options.common.align_reservoir_samples;
1116 if(!format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, /*is_big_endian=*/false, is_unsigned_samples, channels, bps, shift, channel_map))
1117 return EncoderSession_finish_error(&encoder_session);
1126 if(fread(&tmp, 1U, 1U, infile) < 1U) {
1127 flac__utils_printf(stderr, 1, "%s: ERROR during read of data pad byte\n", encoder_session.inbasefilename);
1128 return EncoderSession_finish_error(&encoder_session);
1132 got_data_chunk = true;
1135 if(xx == 0x20746d66 && got_fmt_chunk) { /* "fmt " */
1136 flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'fmt ' sub-chunk\n", encoder_session.inbasefilename);
1137 if(encoder_session.treat_warnings_as_errors)
1138 return EncoderSession_finish_error(&encoder_session);
1140 else if(xx == 0x61746164) { /* "data" */
1141 if(got_data_chunk) {
1142 flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'data' sub-chunk\n", encoder_session.inbasefilename);
1143 if(encoder_session.treat_warnings_as_errors)
1144 return EncoderSession_finish_error(&encoder_session);
1146 else if(!got_fmt_chunk) {
1147 flac__utils_printf(stderr, 1, "%s: ERROR: got 'data' sub-chunk before 'fmt' sub-chunk\n", encoder_session.inbasefilename);
1148 return EncoderSession_finish_error(&encoder_session);
1154 else if(!options.foreign_metadata) {
1155 flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown sub-chunk '%c%c%c%c'\n", encoder_session.inbasefilename, (char)(xx&255), (char)((xx>>8)&255), (char)((xx>>16)&255), (char)(xx>>24));
1156 if(encoder_session.treat_warnings_as_errors)
1157 return EncoderSession_finish_error(&encoder_session);
1159 /* sub-chunk size */
1160 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
1161 return EncoderSession_finish_error(&encoder_session);
1163 unsigned long skip = xx+(xx & 1U);
1165 FLAC__ASSERT(skip<=LONG_MAX);
1166 if(!fskip_ahead(infile, skip)) {
1167 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping unsupported sub-chunk\n", encoder_session.inbasefilename);
1168 return EncoderSession_finish_error(&encoder_session);
1174 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, options.foreign_metadata);
1177 int flac__encode_raw(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, raw_encode_options_t options)
1179 EncoderSession encoder_session;
1181 const size_t bytes_per_wide_sample = options.channels * (options.bps >> 3);
1182 unsigned align_remainder = 0;
1183 int info_align_carry = -1, info_align_zero = -1;
1184 FLAC__uint64 total_samples_in_input = 0;
1186 FLAC__ASSERT(!options.common.sector_align || options.channels == 2);
1187 FLAC__ASSERT(!options.common.sector_align || options.bps == 16);
1188 FLAC__ASSERT(!options.common.sector_align || options.sample_rate == 44100);
1189 FLAC__ASSERT(!options.common.sector_align || infilesize >= 0);
1190 FLAC__ASSERT(!options.common.replay_gain || options.channels <= 2);
1191 FLAC__ASSERT(!options.common.replay_gain || grabbag__replaygain_is_valid_sample_frequency(options.sample_rate));
1194 EncoderSession_construct(
1197 options.common.use_ogg,
1201 options.common.verify,
1202 options.common.treat_warnings_as_errors,
1203 options.common.continue_through_decode_errors,
1212 * now that we know the sample rate, canonicalize the
1213 * --skip string to a number of samples:
1215 flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, options.sample_rate);
1216 FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
1217 encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
1218 FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
1221 total_samples_in_input = 0;
1223 /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
1224 FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
1225 total_samples_in_input = (FLAC__uint64)infilesize / bytes_per_wide_sample + *options.common.align_reservoir_samples;
1229 * now that we know the input size, canonicalize the
1230 * --until strings to a number of samples:
1232 if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, options.sample_rate, encoder_session.skip, total_samples_in_input))
1233 return EncoderSession_finish_error(&encoder_session);
1234 encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
1235 FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
1237 infilesize -= (off_t)encoder_session.skip * bytes_per_wide_sample;
1238 encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
1239 if(encoder_session.until > 0) {
1240 const FLAC__uint64 trim = total_samples_in_input - encoder_session.until;
1241 FLAC__ASSERT(total_samples_in_input > 0);
1242 FLAC__ASSERT(!options.common.sector_align);
1243 infilesize -= (off_t)trim * bytes_per_wide_sample;
1244 encoder_session.total_samples_to_encode -= trim;
1246 if(infilesize >= 0 && options.common.sector_align) {
1247 FLAC__ASSERT(encoder_session.skip == 0);
1248 align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
1249 if(options.common.is_last_file)
1250 encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
1252 encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
1254 encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample;
1256 if(encoder_session.total_samples_to_encode <= 0)
1257 flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
1259 if(encoder_session.skip > 0) {
1260 unsigned skip_bytes = bytes_per_wide_sample * (unsigned)encoder_session.skip;
1261 if(skip_bytes > lookahead_length) {
1262 skip_bytes -= lookahead_length;
1263 lookahead_length = 0;
1264 if(!fskip_ahead(infile, skip_bytes)) {
1265 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
1266 return EncoderSession_finish_error(&encoder_session);
1270 lookahead += skip_bytes;
1271 lookahead_length -= skip_bytes;
1275 if(!EncoderSession_init_encoder(&encoder_session, options.common, /*channel_mask=*/0, options.channels, options.bps, options.sample_rate, /*foreign_metadata=*/0, /*flac_decoder_data=*/0))
1276 return EncoderSession_finish_error(&encoder_session);
1279 * first do any samples in the reservoir
1281 if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
1282 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
1283 print_error_with_state(&encoder_session, "ERROR during encoding");
1284 return EncoderSession_finish_error(&encoder_session);
1289 * decrement infilesize if we need to align the file
1291 if(options.common.sector_align) {
1292 FLAC__ASSERT(infilesize >= 0);
1293 if(options.common.is_last_file) {
1294 *options.common.align_reservoir_samples = 0;
1297 *options.common.align_reservoir_samples = align_remainder;
1298 infilesize -= (off_t)((*options.common.align_reservoir_samples) * bytes_per_wide_sample);
1299 FLAC__ASSERT(infilesize >= 0);
1304 * now do from the file
1306 if(infilesize < 0) {
1307 while(!feof(infile)) {
1308 if(lookahead_length > 0) {
1309 FLAC__ASSERT(lookahead_length < CHUNK_OF_SAMPLES * bytes_per_wide_sample);
1310 memcpy(ucbuffer_, lookahead, lookahead_length);
1311 bytes_read = fread(ucbuffer_+lookahead_length, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample - lookahead_length, infile) + lookahead_length;
1312 if(ferror(infile)) {
1313 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1314 return EncoderSession_finish_error(&encoder_session);
1316 lookahead_length = 0;
1319 bytes_read = fread(ucbuffer_, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, infile);
1321 if(bytes_read == 0) {
1322 if(ferror(infile)) {
1323 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1324 return EncoderSession_finish_error(&encoder_session);
1327 else if(bytes_read % bytes_per_wide_sample != 0) {
1328 flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
1329 return EncoderSession_finish_error(&encoder_session);
1332 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
1333 if(!format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps, /*shift=*/0, /*channel_map=*/0))
1334 return EncoderSession_finish_error(&encoder_session);
1336 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1337 print_error_with_state(&encoder_session, "ERROR during encoding");
1338 return EncoderSession_finish_error(&encoder_session);
1344 const FLAC__uint64 max_input_bytes = infilesize;
1345 FLAC__uint64 total_input_bytes_read = 0;
1346 while(total_input_bytes_read < max_input_bytes) {
1348 size_t wanted = (CHUNK_OF_SAMPLES * bytes_per_wide_sample);
1349 wanted = (size_t) min((FLAC__uint64)wanted, max_input_bytes - total_input_bytes_read);
1351 if(lookahead_length > 0) {
1352 FLAC__ASSERT(lookahead_length <= wanted);
1353 memcpy(ucbuffer_, lookahead, lookahead_length);
1354 wanted -= lookahead_length;
1355 bytes_read = lookahead_length;
1357 bytes_read += fread(ucbuffer_+lookahead_length, sizeof(unsigned char), wanted, infile);
1358 if(ferror(infile)) {
1359 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1360 return EncoderSession_finish_error(&encoder_session);
1363 lookahead_length = 0;
1366 bytes_read = fread(ucbuffer_, sizeof(unsigned char), wanted, infile);
1369 if(bytes_read == 0) {
1370 if(ferror(infile)) {
1371 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1372 return EncoderSession_finish_error(&encoder_session);
1374 else if(feof(infile)) {
1375 flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
1376 if(encoder_session.treat_warnings_as_errors)
1377 return EncoderSession_finish_error(&encoder_session);
1378 total_input_bytes_read = max_input_bytes;
1382 if(bytes_read % bytes_per_wide_sample != 0) {
1383 flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
1384 return EncoderSession_finish_error(&encoder_session);
1387 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
1388 if(!format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps, /*shift=*/0, /*channel_map=*/0))
1389 return EncoderSession_finish_error(&encoder_session);
1391 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1392 print_error_with_state(&encoder_session, "ERROR during encoding");
1393 return EncoderSession_finish_error(&encoder_session);
1395 total_input_bytes_read += bytes_read;
1402 * now read unaligned samples into reservoir or pad with zeroes if necessary
1404 if(options.common.sector_align) {
1405 if(options.common.is_last_file) {
1406 unsigned wide_samples = 588 - align_remainder;
1407 if(wide_samples < 588) {
1410 info_align_zero = wide_samples;
1411 for(channel = 0; channel < options.channels; channel++)
1412 memset(input_[channel], 0, sizeof(input_[0][0]) * wide_samples);
1414 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1415 print_error_with_state(&encoder_session, "ERROR during encoding");
1416 return EncoderSession_finish_error(&encoder_session);
1421 if(*options.common.align_reservoir_samples > 0) {
1422 FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588);
1423 bytes_read = fread(ucbuffer_, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile);
1424 if(bytes_read == 0 && ferror(infile)) {
1425 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1426 return EncoderSession_finish_error(&encoder_session);
1428 else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
1429 flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
1430 if(encoder_session.treat_warnings_as_errors)
1431 return EncoderSession_finish_error(&encoder_session);
1434 info_align_carry = *options.common.align_reservoir_samples;
1435 if(!format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps, /*shift=*/0, /*channel_map=*/0))
1436 return EncoderSession_finish_error(&encoder_session);
1442 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, /*foreign_metadata=*/0);
1445 int flac__encode_flac(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, flac_encode_options_t options, FLAC__bool input_is_ogg)
1447 EncoderSession encoder_session;
1448 FLAC__StreamDecoder *decoder = 0;
1449 FLACDecoderData decoder_data;
1454 EncoderSession_construct(
1457 options.common.use_ogg,
1461 options.common.verify,
1462 options.common.treat_warnings_as_errors,
1463 options.common.continue_through_decode_errors,
1471 decoder_data.encoder_session = &encoder_session;
1472 decoder_data.filesize = (infilesize == (off_t)(-1)? 0 : infilesize);
1473 decoder_data.lookahead = lookahead;
1474 decoder_data.lookahead_length = lookahead_length;
1475 decoder_data.num_metadata_blocks = 0;
1476 decoder_data.samples_left_to_process = 0;
1477 decoder_data.fatal_error = false;
1480 * set up FLAC decoder for the input
1482 if (0 == (decoder = FLAC__stream_decoder_new())) {
1483 flac__utils_printf(stderr, 1, "%s: ERROR: creating decoder for FLAC input\n", encoder_session.inbasefilename);
1484 return EncoderSession_finish_error(&encoder_session);
1487 FLAC__stream_decoder_set_md5_checking(decoder, false) &&
1488 FLAC__stream_decoder_set_metadata_respond_all(decoder)
1490 flac__utils_printf(stderr, 1, "%s: ERROR: setting up decoder for FLAC input\n", encoder_session.inbasefilename);
1491 goto fubar1; /*@@@ yuck */
1495 if (FLAC__stream_decoder_init_ogg_stream(decoder, flac_decoder_read_callback, flac_decoder_seek_callback, flac_decoder_tell_callback, flac_decoder_length_callback, flac_decoder_eof_callback, flac_decoder_write_callback, flac_decoder_metadata_callback, flac_decoder_error_callback, /*client_data=*/&decoder_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
1496 flac__utils_printf(stderr, 1, "%s: ERROR: initializing decoder for Ogg FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
1497 goto fubar1; /*@@@ yuck */
1500 else if (FLAC__stream_decoder_init_stream(decoder, flac_decoder_read_callback, flac_decoder_seek_callback, flac_decoder_tell_callback, flac_decoder_length_callback, flac_decoder_eof_callback, flac_decoder_write_callback, flac_decoder_metadata_callback, flac_decoder_error_callback, /*client_data=*/&decoder_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
1501 flac__utils_printf(stderr, 1, "%s: ERROR: initializing decoder for FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
1502 goto fubar1; /*@@@ yuck */
1505 if (!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || decoder_data.fatal_error) {
1506 if (decoder_data.fatal_error)
1507 flac__utils_printf(stderr, 1, "%s: ERROR: out of memory or too many metadata blocks while reading metadata in FLAC input\n", encoder_session.inbasefilename);
1509 flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
1510 goto fubar1; /*@@@ yuck */
1513 if (decoder_data.num_metadata_blocks == 0) {
1514 flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, got no metadata blocks\n", encoder_session.inbasefilename);
1515 goto fubar2; /*@@@ yuck */
1517 else if (decoder_data.metadata_blocks[0]->type != FLAC__METADATA_TYPE_STREAMINFO) {
1518 flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, first metadata block is not STREAMINFO\n", encoder_session.inbasefilename);
1519 goto fubar2; /*@@@ yuck */
1521 else if (decoder_data.metadata_blocks[0]->data.stream_info.total_samples == 0) {
1522 flac__utils_printf(stderr, 1, "%s: ERROR: FLAC input has STREAMINFO with unknown total samples which is not supported\n", encoder_session.inbasefilename);
1523 goto fubar2; /*@@@ yuck */
1527 * now that we have the STREAMINFO and know the sample rate,
1528 * canonicalize the --skip string to a number of samples:
1530 flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, decoder_data.metadata_blocks[0]->data.stream_info.sample_rate);
1531 FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
1532 encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
1533 FLAC__ASSERT(!options.common.sector_align); /* --sector-align with FLAC input is not supported */
1536 FLAC__uint64 total_samples_in_input, trim = 0;
1538 total_samples_in_input = decoder_data.metadata_blocks[0]->data.stream_info.total_samples;
1541 * now that we know the input size, canonicalize the
1542 * --until string to an absolute sample number:
1544 if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, decoder_data.metadata_blocks[0]->data.stream_info.sample_rate, encoder_session.skip, total_samples_in_input))
1545 goto fubar2; /*@@@ yuck */
1546 encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
1548 encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
1549 if(encoder_session.until > 0) {
1550 trim = total_samples_in_input - encoder_session.until;
1551 FLAC__ASSERT(total_samples_in_input > 0);
1552 encoder_session.total_samples_to_encode -= trim;
1555 encoder_session.unencoded_size = decoder_data.filesize;
1557 /* (channel mask will get copied over from the source VORBIS_COMMENT if it exists) */
1558 if(!EncoderSession_init_encoder(&encoder_session, options.common, /*channel_mask=*/0, decoder_data.metadata_blocks[0]->data.stream_info.channels, decoder_data.metadata_blocks[0]->data.stream_info.bits_per_sample, decoder_data.metadata_blocks[0]->data.stream_info.sample_rate, /*foreign_metadata=*/0, &decoder_data))
1559 goto fubar2; /*@@@ yuck */
1562 * have to wait until the FLAC encoder is set up for writing
1563 * before any seeking in the input FLAC file, because the seek
1564 * itself will usually call the decoder's write callback, and
1565 * our decoder's write callback passes samples to our FLAC
1568 decoder_data.samples_left_to_process = encoder_session.total_samples_to_encode;
1569 if(encoder_session.skip > 0) {
1570 if(!FLAC__stream_decoder_seek_absolute(decoder, encoder_session.skip)) {
1571 flac__utils_printf(stderr, 1, "%s: ERROR while skipping samples, FLAC decoder state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
1572 goto fubar2; /*@@@ yuck */
1577 * now do samples from the file
1579 while(!decoder_data.fatal_error && decoder_data.samples_left_to_process > 0) {
1580 /* We can also hit the end of stream without samples_left_to_process
1581 * going to 0 if there are errors and continue_through_decode_errors
1582 * is on, so we want to break in that case too:
1584 if(encoder_session.continue_through_decode_errors && FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
1586 if(!FLAC__stream_decoder_process_single(decoder)) {
1587 flac__utils_printf(stderr, 1, "%s: ERROR: while decoding FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
1588 goto fubar2; /*@@@ yuck */
1591 if(decoder_data.fatal_error) {
1592 flac__utils_printf(stderr, 1, "%s: ERROR: while decoding FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
1593 goto fubar2; /*@@@ yuck */
1597 FLAC__stream_decoder_delete(decoder);
1598 retval = EncoderSession_finish_ok(&encoder_session, -1, -1, /*foreign_metadata=*/0);
1599 /* have to wail until encoder is completely finished before deleting because of the final step of writing the seekpoint offsets */
1600 for(i = 0; i < decoder_data.num_metadata_blocks; i++)
1601 FLAC__metadata_object_delete(decoder_data.metadata_blocks[i]);
1605 for(i = 0; i < decoder_data.num_metadata_blocks; i++)
1606 FLAC__metadata_object_delete(decoder_data.metadata_blocks[i]);
1608 FLAC__stream_decoder_delete(decoder);
1609 return EncoderSession_finish_error(&encoder_session);
1612 FLAC__bool EncoderSession_construct(EncoderSession *e, FLAC__bool use_ogg, FLAC__bool verify, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FILE *infile, const char *infilename, const char *outfilename)
1615 FLAC__uint32 test = 1;
1618 * initialize globals
1621 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
1623 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
1624 input_[i] = &(in_[i][0]);
1628 * initialize instance
1632 e->use_ogg = use_ogg;
1637 e->treat_warnings_as_errors = treat_warnings_as_errors;
1638 e->continue_through_decode_errors = continue_through_decode_errors;
1640 e->is_stdout = (0 == strcmp(outfilename, "-"));
1641 e->outputfile_opened = false;
1643 e->inbasefilename = grabbag__file_get_basename(infilename);
1644 e->infilename = infilename;
1645 e->outfilename = outfilename;
1647 e->skip = 0; /* filled in later after the sample_rate is known */
1648 e->unencoded_size = 0;
1649 e->total_samples_to_encode = 0;
1650 e->bytes_written = 0;
1651 e->samples_written = 0;
1657 e->seek_table_template = 0;
1659 if(0 == (e->seek_table_template = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE))) {
1660 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for seek table\n", e->inbasefilename);
1664 e->encoder = FLAC__stream_encoder_new();
1665 if(0 == e->encoder) {
1666 flac__utils_printf(stderr, 1, "%s: ERROR creating the encoder instance\n", e->inbasefilename);
1667 EncoderSession_destroy(e);
1674 void EncoderSession_destroy(EncoderSession *e)
1679 if(0 != e->encoder) {
1680 FLAC__stream_encoder_delete(e->encoder);
1684 if(0 != e->seek_table_template) {
1685 FLAC__metadata_object_delete(e->seek_table_template);
1686 e->seek_table_template = 0;
1690 int EncoderSession_finish_ok(EncoderSession *e, int info_align_carry, int info_align_zero, foreign_metadata_t *foreign_metadata)
1692 FLAC__StreamEncoderState fse_state = FLAC__STREAM_ENCODER_OK;
1694 FLAC__bool verify_error = false;
1697 fse_state = FLAC__stream_encoder_get_state(e->encoder);
1698 ret = FLAC__stream_encoder_finish(e->encoder)? 0 : 1;
1700 fse_state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA ||
1701 FLAC__stream_encoder_get_state(e->encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA
1704 /* all errors except verify errors should interrupt the stats */
1705 if(ret && !verify_error)
1706 print_error_with_state(e, "ERROR during encoding");
1707 else if(e->total_samples_to_encode > 0) {
1709 flac__utils_printf(stderr, 2, "\n");
1713 print_verify_error(e);
1717 if(info_align_carry >= 0) {
1718 flac__utils_printf(stderr, 1, "%s: INFO: sector alignment causing %d samples to be carried over\n", e->inbasefilename, info_align_carry);
1720 if(info_align_zero >= 0) {
1721 flac__utils_printf(stderr, 1, "%s: INFO: sector alignment causing %d zero samples to be appended\n", e->inbasefilename, info_align_zero);
1725 /*@@@@@@ should this go here or somewhere else? */
1726 if(ret == 0 && foreign_metadata) {
1728 if(!flac__foreign_metadata_write_to_flac(foreign_metadata, e->infilename, e->outfilename, &error)) {
1729 flac__utils_printf(stderr, 1, "%s: ERROR: updating foreign metadata in FLAC file: %s\n", e->inbasefilename, error);
1734 EncoderSession_destroy(e);
1739 int EncoderSession_finish_error(EncoderSession *e)
1741 FLAC__ASSERT(e->encoder);
1743 if(e->total_samples_to_encode > 0)
1744 flac__utils_printf(stderr, 2, "\n");
1746 if(FLAC__stream_encoder_get_state(e->encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1747 print_verify_error(e);
1748 else if(e->outputfile_opened)
1749 /* only want to delete the file if we opened it; otherwise it could be an existing file and our overwrite failed */
1750 unlink(e->outfilename);
1752 EncoderSession_destroy(e);
1758 unsigned num_metadata;
1759 FLAC__bool *needs_delete;
1760 FLAC__StreamMetadata **metadata;
1761 FLAC__StreamMetadata *cuesheet; /* always needs to be deleted */
1762 } static_metadata_t;
1764 static void static_metadata_init(static_metadata_t *m)
1766 m->num_metadata = 0;
1767 m->needs_delete = 0;
1772 static void static_metadata_clear(static_metadata_t *m)
1775 for(i = 0; i < m->num_metadata; i++)
1776 if(m->needs_delete[i])
1777 FLAC__metadata_object_delete(m->metadata[i]);
1781 free(m->needs_delete);
1783 FLAC__metadata_object_delete(m->cuesheet);
1784 static_metadata_init(m);
1787 static FLAC__bool static_metadata_append(static_metadata_t *m, FLAC__StreamMetadata *d, FLAC__bool needs_delete)
1790 if(0 == (x = safe_realloc_muladd2_(m->metadata, sizeof(*m->metadata), /*times (*/m->num_metadata, /*+*/1/*)*/)))
1792 m->metadata = (FLAC__StreamMetadata**)x;
1793 if(0 == (x = safe_realloc_muladd2_(m->needs_delete, sizeof(*m->needs_delete), /*times (*/m->num_metadata, /*+*/1/*)*/)))
1795 m->needs_delete = (FLAC__bool*)x;
1796 m->metadata[m->num_metadata] = d;
1797 m->needs_delete[m->num_metadata] = needs_delete;
1802 FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options, FLAC__uint32 channel_mask, unsigned channels, unsigned bps, unsigned sample_rate, const foreign_metadata_t *foreign_metadata, FLACDecoderData *flac_decoder_data)
1804 FLAC__StreamMetadata padding;
1805 FLAC__StreamMetadata **metadata = 0;
1806 static_metadata_t static_metadata;
1807 unsigned num_metadata = 0, i;
1808 FLAC__StreamEncoderInitStatus init_status;
1809 const FLAC__bool is_cdda = (channels == 1 || channels == 2) && (bps == 16) && (sample_rate == 44100);
1810 char apodizations[2000];
1812 FLAC__ASSERT(sizeof(options.pictures)/sizeof(options.pictures[0]) <= 64);
1814 static_metadata_init(&static_metadata);
1816 e->replay_gain = options.replay_gain;
1817 e->channels = channels;
1818 e->bits_per_sample = bps;
1819 e->sample_rate = sample_rate;
1821 apodizations[0] = '\0';
1823 if(e->replay_gain) {
1824 if(channels != 1 && channels != 2) {
1825 flac__utils_printf(stderr, 1, "%s: ERROR, number of channels (%u) must be 1 or 2 for --replay-gain\n", e->inbasefilename, channels);
1828 if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
1829 flac__utils_printf(stderr, 1, "%s: ERROR, invalid sample rate (%u) for --replay-gain\n", e->inbasefilename, sample_rate);
1832 if(options.is_first_file) {
1833 if(!grabbag__replaygain_init(sample_rate)) {
1834 flac__utils_printf(stderr, 1, "%s: ERROR initializing ReplayGain stage\n", e->inbasefilename);
1840 if(!parse_cuesheet(&static_metadata.cuesheet, options.cuesheet_filename, e->inbasefilename, is_cdda, e->total_samples_to_encode, e->treat_warnings_as_errors))
1843 if(!convert_to_seek_table_template(options.requested_seek_points, options.num_requested_seek_points, options.cued_seekpoints? static_metadata.cuesheet : 0, e)) {
1844 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for seek table\n", e->inbasefilename);
1845 static_metadata_clear(&static_metadata);
1849 /* build metadata */
1850 if(flac_decoder_data) {
1852 * we're encoding from FLAC so we will use the FLAC file's
1853 * metadata as the basis for the encoded file
1857 * first handle pictures: simple append any --pictures
1860 for(i = 0; i < options.num_pictures; i++) {
1861 FLAC__StreamMetadata *pic = FLAC__metadata_object_clone(options.pictures[i]);
1863 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for PICTURE block\n", e->inbasefilename);
1864 static_metadata_clear(&static_metadata);
1867 flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks++] = pic;
1872 * next handle vorbis comment: if any tags were specified
1873 * or there is no existing vorbis comment, we create a
1874 * new vorbis comment (discarding any existing one); else
1875 * we keep the existing one. also need to make sure to
1876 * propagate any channel mask tag.
1878 /* @@@ change to append -T values from options.vorbis_comment if input has VC already? */
1880 FLAC__bool vc_found = false;
1881 for(i = 0, j = 0; i < flac_decoder_data->num_metadata_blocks; i++) {
1882 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
1884 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT && options.vorbis_comment->data.vorbis_comment.num_comments > 0) {
1885 (void) flac__utils_get_channel_mask_tag(flac_decoder_data->metadata_blocks[i], &channel_mask);
1886 flac__utils_printf(stderr, 1, "%s: WARNING, replacing tags from input FLAC file with those given on the command-line\n", e->inbasefilename);
1887 if(e->treat_warnings_as_errors) {
1888 static_metadata_clear(&static_metadata);
1891 FLAC__metadata_object_delete(flac_decoder_data->metadata_blocks[i]);
1892 flac_decoder_data->metadata_blocks[i] = 0;
1895 flac_decoder_data->metadata_blocks[j++] = flac_decoder_data->metadata_blocks[i];
1897 flac_decoder_data->num_metadata_blocks = j;
1898 if((!vc_found || options.vorbis_comment->data.vorbis_comment.num_comments > 0) && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
1900 FLAC__StreamMetadata *vc = FLAC__metadata_object_clone(options.vorbis_comment);
1901 if(0 == vc || (channel_mask && !flac__utils_set_channel_mask_tag(vc, channel_mask))) {
1902 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for VORBIS_COMMENT block\n", e->inbasefilename);
1903 static_metadata_clear(&static_metadata);
1906 for(i = flac_decoder_data->num_metadata_blocks; i > 1; i--)
1907 flac_decoder_data->metadata_blocks[i] = flac_decoder_data->metadata_blocks[i-1];
1908 flac_decoder_data->metadata_blocks[1] = vc;
1909 flac_decoder_data->num_metadata_blocks++;
1914 * next handle cuesheet: if --cuesheet was specified, use
1915 * it; else if file has existing CUESHEET and cuesheet's
1916 * lead-out offset is correct, keep it; else no CUESHEET
1919 for(i = 0, j = 0; i < flac_decoder_data->num_metadata_blocks; i++) {
1920 FLAC__bool existing_cuesheet_is_bad = false;
1921 /* check if existing cuesheet matches the input audio */
1922 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_CUESHEET && 0 == static_metadata.cuesheet) {
1923 const FLAC__StreamMetadata_CueSheet *cs = &flac_decoder_data->metadata_blocks[i]->data.cue_sheet;
1924 if(e->total_samples_to_encode == 0) {
1925 flac__utils_printf(stderr, 1, "%s: WARNING, cuesheet in input FLAC file cannot be kept if input size is not known, dropping it...\n", e->inbasefilename);
1926 if(e->treat_warnings_as_errors) {
1927 static_metadata_clear(&static_metadata);
1930 existing_cuesheet_is_bad = true;
1932 else if(e->total_samples_to_encode != cs->tracks[cs->num_tracks-1].offset) {
1933 flac__utils_printf(stderr, 1, "%s: WARNING, lead-out offset of cuesheet in input FLAC file does not match input length, dropping existing cuesheet...\n", e->inbasefilename);
1934 if(e->treat_warnings_as_errors) {
1935 static_metadata_clear(&static_metadata);
1938 existing_cuesheet_is_bad = true;
1941 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_CUESHEET && (existing_cuesheet_is_bad || 0 != static_metadata.cuesheet)) {
1942 if(0 != static_metadata.cuesheet) {
1943 flac__utils_printf(stderr, 1, "%s: WARNING, replacing cuesheet in input FLAC file with the one given on the command-line\n", e->inbasefilename);
1944 if(e->treat_warnings_as_errors) {
1945 static_metadata_clear(&static_metadata);
1949 FLAC__metadata_object_delete(flac_decoder_data->metadata_blocks[i]);
1950 flac_decoder_data->metadata_blocks[i] = 0;
1953 flac_decoder_data->metadata_blocks[j++] = flac_decoder_data->metadata_blocks[i];
1955 flac_decoder_data->num_metadata_blocks = j;
1956 if(0 != static_metadata.cuesheet && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
1958 FLAC__StreamMetadata *cs = FLAC__metadata_object_clone(static_metadata.cuesheet);
1960 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for CUESHEET block\n", e->inbasefilename);
1961 static_metadata_clear(&static_metadata);
1964 for(i = flac_decoder_data->num_metadata_blocks; i > 1; i--)
1965 flac_decoder_data->metadata_blocks[i] = flac_decoder_data->metadata_blocks[i-1];
1966 flac_decoder_data->metadata_blocks[1] = cs;
1967 flac_decoder_data->num_metadata_blocks++;
1972 * next handle seektable: if -S- was specified, no
1973 * SEEKTABLE; else if -S was specified, use it/them;
1974 * else if file has existing SEEKTABLE and input size is
1975 * preserved (no --skip/--until/etc specified), keep it;
1976 * else use default seektable options
1978 * note: meanings of num_requested_seek_points:
1979 * -1 : no -S option given, default to some value
1980 * 0 : -S- given (no seektable)
1981 * >0 : one or more -S options given
1984 FLAC__bool existing_seektable = false;
1985 for(i = 0, j = 0; i < flac_decoder_data->num_metadata_blocks; i++) {
1986 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_SEEKTABLE)
1987 existing_seektable = true;
1988 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_SEEKTABLE && (e->total_samples_to_encode != flac_decoder_data->metadata_blocks[0]->data.stream_info.total_samples || options.num_requested_seek_points >= 0)) {
1989 if(options.num_requested_seek_points > 0) {
1990 flac__utils_printf(stderr, 1, "%s: WARNING, replacing seektable in input FLAC file with the one given on the command-line\n", e->inbasefilename);
1991 if(e->treat_warnings_as_errors) {
1992 static_metadata_clear(&static_metadata);
1996 else if(options.num_requested_seek_points == 0)
1997 ; /* no warning, silently delete existing SEEKTABLE since user specified --no-seektable (-S-) */
1999 flac__utils_printf(stderr, 1, "%s: WARNING, can't use existing seektable in input FLAC since the input size is changing or unknown, dropping existing SEEKTABLE block...\n", e->inbasefilename);
2000 if(e->treat_warnings_as_errors) {
2001 static_metadata_clear(&static_metadata);
2005 FLAC__metadata_object_delete(flac_decoder_data->metadata_blocks[i]);
2006 flac_decoder_data->metadata_blocks[i] = 0;
2007 existing_seektable = false;
2010 flac_decoder_data->metadata_blocks[j++] = flac_decoder_data->metadata_blocks[i];
2012 flac_decoder_data->num_metadata_blocks = j;
2013 if((options.num_requested_seek_points > 0 || (options.num_requested_seek_points < 0 && !existing_seektable)) && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
2015 FLAC__StreamMetadata *st = FLAC__metadata_object_clone(e->seek_table_template);
2017 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for SEEKTABLE block\n", e->inbasefilename);
2018 static_metadata_clear(&static_metadata);
2021 for(i = flac_decoder_data->num_metadata_blocks; i > 1; i--)
2022 flac_decoder_data->metadata_blocks[i] = flac_decoder_data->metadata_blocks[i-1];
2023 flac_decoder_data->metadata_blocks[1] = st;
2024 flac_decoder_data->num_metadata_blocks++;
2029 * finally handle padding: if --no-padding was specified,
2030 * then delete all padding; else if -P was specified,
2031 * use that instead of existing padding (if any); else
2032 * if existing file has padding, move all existing
2033 * padding blocks to one padding block at the end; else
2034 * use default padding.
2038 for(i = 0, j = 0; i < flac_decoder_data->num_metadata_blocks; i++) {
2039 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_PADDING) {
2042 p += flac_decoder_data->metadata_blocks[i]->length;
2043 FLAC__metadata_object_delete(flac_decoder_data->metadata_blocks[i]);
2044 flac_decoder_data->metadata_blocks[i] = 0;
2047 flac_decoder_data->metadata_blocks[j++] = flac_decoder_data->metadata_blocks[i];
2049 flac_decoder_data->num_metadata_blocks = j;
2050 if(options.padding > 0)
2051 p = options.padding;
2053 p = e->total_samples_to_encode / e->sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8;
2054 if(options.padding != 0) {
2055 if(p > 0 && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
2056 flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
2057 if(0 == flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks]) {
2058 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for PADDING block\n", e->inbasefilename);
2059 static_metadata_clear(&static_metadata);
2062 flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks]->is_last = false; /* the encoder will set this for us */
2063 flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks]->length = p;
2064 flac_decoder_data->num_metadata_blocks++;
2068 metadata = &flac_decoder_data->metadata_blocks[1]; /* don't include STREAMINFO */
2069 num_metadata = flac_decoder_data->num_metadata_blocks - 1;
2073 * we're not encoding from FLAC so we will build the metadata
2076 if(e->seek_table_template->data.seek_table.num_points > 0) {
2077 e->seek_table_template->is_last = false; /* the encoder will set this for us */
2078 static_metadata_append(&static_metadata, e->seek_table_template, /*needs_delete=*/false);
2080 if(0 != static_metadata.cuesheet)
2081 static_metadata_append(&static_metadata, static_metadata.cuesheet, /*needs_delete=*/false);
2083 if(!flac__utils_set_channel_mask_tag(options.vorbis_comment, channel_mask)) {
2084 flac__utils_printf(stderr, 1, "%s: ERROR adding channel mask tag\n", e->inbasefilename);
2085 static_metadata_clear(&static_metadata);
2089 static_metadata_append(&static_metadata, options.vorbis_comment, /*needs_delete=*/false);
2090 for(i = 0; i < options.num_pictures; i++)
2091 static_metadata_append(&static_metadata, options.pictures[i], /*needs_delete=*/false);
2092 if(foreign_metadata) {
2093 for(i = 0; i < foreign_metadata->num_blocks; i++) {
2094 FLAC__StreamMetadata *p = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
2096 flac__utils_printf(stderr, 1, "%s: ERROR: out of memory\n", e->inbasefilename);
2097 static_metadata_clear(&static_metadata);
2100 static_metadata_append(&static_metadata, p, /*needs_delete=*/true);
2101 static_metadata.metadata[static_metadata.num_metadata-1]->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8 + foreign_metadata->blocks[i].size;
2102 /*fprintf(stderr,"@@@@@@ add PADDING=%u\n",static_metadata.metadata[static_metadata.num_metadata-1]->length);*/
2105 if(options.padding != 0) {
2106 padding.is_last = false; /* the encoder will set this for us */
2107 padding.type = FLAC__METADATA_TYPE_PADDING;
2108 padding.length = (unsigned)(options.padding>0? options.padding : (e->total_samples_to_encode / e->sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8));
2109 static_metadata_append(&static_metadata, &padding, /*needs_delete=*/false);
2111 metadata = static_metadata.metadata;
2112 num_metadata = static_metadata.num_metadata;
2115 /* check for a few things that have not already been checked. the
2116 * FLAC__stream_encoder_init*() will check it but only return
2117 * FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA so we check some
2118 * up front to give a better error message.
2120 if(!verify_metadata(e, metadata, num_metadata)) {
2121 static_metadata_clear(&static_metadata);
2125 FLAC__stream_encoder_set_verify(e->encoder, options.verify);
2126 FLAC__stream_encoder_set_streamable_subset(e->encoder, !options.lax);
2127 FLAC__stream_encoder_set_channels(e->encoder, channels);
2128 FLAC__stream_encoder_set_bits_per_sample(e->encoder, bps);
2129 FLAC__stream_encoder_set_sample_rate(e->encoder, sample_rate);
2130 for(i = 0; i < options.num_compression_settings; i++) {
2131 switch(options.compression_settings[i].type) {
2133 FLAC__stream_encoder_set_blocksize(e->encoder, options.compression_settings[i].value.t_unsigned);
2135 case CST_COMPRESSION_LEVEL:
2136 FLAC__stream_encoder_set_compression_level(e->encoder, options.compression_settings[i].value.t_unsigned);
2137 apodizations[0] = '\0';
2139 case CST_DO_MID_SIDE:
2140 FLAC__stream_encoder_set_do_mid_side_stereo(e->encoder, options.compression_settings[i].value.t_bool);
2142 case CST_LOOSE_MID_SIDE:
2143 FLAC__stream_encoder_set_loose_mid_side_stereo(e->encoder, options.compression_settings[i].value.t_bool);
2145 case CST_APODIZATION:
2146 if(strlen(apodizations)+strlen(options.compression_settings[i].value.t_string)+2 >= sizeof(apodizations)) {
2147 flac__utils_printf(stderr, 1, "%s: ERROR: too many apodization functions requested\n", e->inbasefilename);
2148 static_metadata_clear(&static_metadata);
2152 strcat(apodizations, options.compression_settings[i].value.t_string);
2153 strcat(apodizations, ";");
2156 case CST_MAX_LPC_ORDER:
2157 FLAC__stream_encoder_set_max_lpc_order(e->encoder, options.compression_settings[i].value.t_unsigned);
2159 case CST_QLP_COEFF_PRECISION:
2160 FLAC__stream_encoder_set_qlp_coeff_precision(e->encoder, options.compression_settings[i].value.t_unsigned);
2162 case CST_DO_QLP_COEFF_PREC_SEARCH:
2163 FLAC__stream_encoder_set_do_qlp_coeff_prec_search(e->encoder, options.compression_settings[i].value.t_bool);
2165 case CST_DO_ESCAPE_CODING:
2166 FLAC__stream_encoder_set_do_escape_coding(e->encoder, options.compression_settings[i].value.t_bool);
2168 case CST_DO_EXHAUSTIVE_MODEL_SEARCH:
2169 FLAC__stream_encoder_set_do_exhaustive_model_search(e->encoder, options.compression_settings[i].value.t_bool);
2171 case CST_MIN_RESIDUAL_PARTITION_ORDER:
2172 FLAC__stream_encoder_set_min_residual_partition_order(e->encoder, options.compression_settings[i].value.t_unsigned);
2174 case CST_MAX_RESIDUAL_PARTITION_ORDER:
2175 FLAC__stream_encoder_set_max_residual_partition_order(e->encoder, options.compression_settings[i].value.t_unsigned);
2177 case CST_RICE_PARAMETER_SEARCH_DIST:
2178 FLAC__stream_encoder_set_rice_parameter_search_dist(e->encoder, options.compression_settings[i].value.t_unsigned);
2183 FLAC__stream_encoder_set_apodization(e->encoder, apodizations);
2184 FLAC__stream_encoder_set_total_samples_estimate(e->encoder, e->total_samples_to_encode);
2185 FLAC__stream_encoder_set_metadata(e->encoder, (num_metadata > 0)? metadata : 0, num_metadata);
2187 FLAC__stream_encoder_disable_constant_subframes(e->encoder, options.debug.disable_constant_subframes);
2188 FLAC__stream_encoder_disable_fixed_subframes(e->encoder, options.debug.disable_fixed_subframes);
2189 FLAC__stream_encoder_disable_verbatim_subframes(e->encoder, options.debug.disable_verbatim_subframes);
2190 if(!options.debug.do_md5) {
2191 flac__utils_printf(stderr, 1, "%s: WARNING, MD5 computation disabled, resulting file will not have MD5 sum\n", e->inbasefilename);
2192 if(e->treat_warnings_as_errors) {
2193 static_metadata_clear(&static_metadata);
2196 FLAC__stream_encoder_set_do_md5(e->encoder, false);
2201 FLAC__stream_encoder_set_ogg_serial_number(e->encoder, options.serial_number);
2203 init_status = FLAC__stream_encoder_init_ogg_file(e->encoder, e->is_stdout? 0 : e->outfilename, encoder_progress_callback, /*client_data=*/e);
2208 init_status = FLAC__stream_encoder_init_file(e->encoder, e->is_stdout? 0 : e->outfilename, encoder_progress_callback, /*client_data=*/e);
2211 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
2212 print_error_with_init_status(e, "ERROR initializing encoder", init_status);
2213 if(FLAC__stream_encoder_get_state(e->encoder) != FLAC__STREAM_ENCODER_IO_ERROR)
2214 e->outputfile_opened = true;
2215 static_metadata_clear(&static_metadata);
2219 e->outputfile_opened = true;
2222 (FLAC__stream_encoder_get_do_exhaustive_model_search(e->encoder) && FLAC__stream_encoder_get_do_qlp_coeff_prec_search(e->encoder))? 0x07 :
2223 (FLAC__stream_encoder_get_do_exhaustive_model_search(e->encoder) || FLAC__stream_encoder_get_do_qlp_coeff_prec_search(e->encoder))? 0x0f :
2226 static_metadata_clear(&static_metadata);
2231 FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples)
2233 if(e->replay_gain) {
2234 if(!grabbag__replaygain_analyze(buffer, e->channels==2, e->bits_per_sample, samples)) {
2235 flac__utils_printf(stderr, 1, "%s: WARNING, error while calculating ReplayGain\n", e->inbasefilename);
2236 if(e->treat_warnings_as_errors)
2241 return FLAC__stream_encoder_process(e->encoder, buffer, samples);
2244 FLAC__bool convert_to_seek_table_template(const char *requested_seek_points, int num_requested_seek_points, FLAC__StreamMetadata *cuesheet, EncoderSession *e)
2246 const FLAC__bool only_placeholders = e->is_stdout;
2247 FLAC__bool has_real_points;
2249 if(num_requested_seek_points == 0 && 0 == cuesheet)
2252 if(num_requested_seek_points < 0) {
2254 /*@@@@@@ workaround ogg bug: too many seekpoints makes table not fit in one page */
2255 if(e->use_ogg && e->total_samples_to_encode > 0 && e->total_samples_to_encode / e->sample_rate / 10 > 230)
2256 requested_seek_points = "230x;";
2259 requested_seek_points = "10s;";
2260 num_requested_seek_points = 1;
2263 if(num_requested_seek_points > 0) {
2264 if(!grabbag__seektable_convert_specification_to_template(requested_seek_points, only_placeholders, e->total_samples_to_encode, e->sample_rate, e->seek_table_template, &has_real_points))
2270 const FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
2271 for(i = 0; i < cs->num_tracks; i++) {
2272 const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+i;
2273 for(j = 0; j < tr->num_indices; j++) {
2274 if(!FLAC__metadata_object_seektable_template_append_point(e->seek_table_template, tr->offset + tr->indices[j].offset))
2276 has_real_points = true;
2280 if(!FLAC__metadata_object_seektable_template_sort(e->seek_table_template, /*compact=*/true))
2284 if(has_real_points) {
2286 flac__utils_printf(stderr, 1, "%s: WARNING, cannot write back seekpoints when encoding to stdout\n", e->inbasefilename);
2287 if(e->treat_warnings_as_errors)
2295 FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input)
2297 /* convert from mm:ss.sss to sample number if necessary */
2298 flac__utils_canonicalize_skip_until_specification(spec, sample_rate);
2300 /* special case: if "--until=-0", use the special value '0' to mean "end-of-stream" */
2301 if(spec->is_relative && spec->value.samples == 0) {
2302 spec->is_relative = false;
2306 /* in any other case the total samples in the input must be known */
2307 if(total_samples_in_input == 0) {
2308 flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when input length is unknown\n", inbasefilename);
2312 FLAC__ASSERT(spec->value_is_samples);
2314 /* convert relative specifications to absolute */
2315 if(spec->is_relative) {
2316 if(spec->value.samples <= 0)
2317 spec->value.samples += (FLAC__int64)total_samples_in_input;
2319 spec->value.samples += skip;
2320 spec->is_relative = false;
2324 if(spec->value.samples < 0) {
2325 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before beginning of input\n", inbasefilename);
2328 if((FLAC__uint64)spec->value.samples <= skip) {
2329 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before --skip point\n", inbasefilename);
2332 if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
2333 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is after end of input\n", inbasefilename);
2340 FLAC__bool verify_metadata(const EncoderSession *e, FLAC__StreamMetadata **metadata, unsigned num_metadata)
2342 FLAC__bool metadata_picture_has_type1 = false;
2343 FLAC__bool metadata_picture_has_type2 = false;
2346 FLAC__ASSERT(0 != metadata);
2347 for(i = 0; i < num_metadata; i++) {
2348 const FLAC__StreamMetadata *m = metadata[i];
2349 if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
2350 if(!FLAC__format_seektable_is_legal(&m->data.seek_table)) {
2351 flac__utils_printf(stderr, 1, "%s: ERROR: SEEKTABLE metadata block is invalid\n", e->inbasefilename);
2355 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
2356 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0)) {
2357 flac__utils_printf(stderr, 1, "%s: ERROR: CUESHEET metadata block is invalid\n", e->inbasefilename);
2361 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
2362 const char *error = 0;
2363 if(!FLAC__format_picture_is_legal(&m->data.picture, &error)) {
2364 flac__utils_printf(stderr, 1, "%s: ERROR: PICTURE metadata block is invalid: %s\n", e->inbasefilename, error);
2367 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
2368 if(metadata_picture_has_type1) {
2369 flac__utils_printf(stderr, 1, "%s: ERROR: there may only be one picture of type 1 (32x32 icon) in the file\n", e->inbasefilename);
2372 metadata_picture_has_type1 = true;
2374 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
2375 if(metadata_picture_has_type2) {
2376 flac__utils_printf(stderr, 1, "%s: ERROR: there may only be one picture of type 2 (icon) in the file\n", e->inbasefilename);
2379 metadata_picture_has_type2 = true;
2387 FLAC__bool format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned shift, size_t *channel_map)
2389 unsigned wide_sample, sample, channel, byte;
2390 FLAC__int32 *out[FLAC__MAX_CHANNELS];
2392 if(0 == channel_map) {
2393 for(channel = 0; channel < channels; channel++)
2394 out[channel] = dest[channel];
2397 for(channel = 0; channel < channels; channel++)
2398 out[channel] = dest[channel_map[channel]];
2402 if(is_unsigned_samples) {
2403 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2404 for(channel = 0; channel < channels; channel++, sample++)
2405 out[channel][wide_sample] = (FLAC__int32)ucbuffer_[sample] - 0x80;
2408 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2409 for(channel = 0; channel < channels; channel++, sample++)
2410 out[channel][wide_sample] = (FLAC__int32)scbuffer_[sample];
2413 else if(bps == 16) {
2414 if(is_big_endian != is_big_endian_host_) {
2416 const unsigned bytes = wide_samples * channels * (bps >> 3);
2417 for(byte = 0; byte < bytes; byte += 2) {
2418 tmp = ucbuffer_[byte];
2419 ucbuffer_[byte] = ucbuffer_[byte+1];
2420 ucbuffer_[byte+1] = tmp;
2423 if(is_unsigned_samples) {
2424 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2425 for(channel = 0; channel < channels; channel++, sample++)
2426 out[channel][wide_sample] = (FLAC__int32)usbuffer_[sample] - 0x8000;
2429 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2430 for(channel = 0; channel < channels; channel++, sample++)
2431 out[channel][wide_sample] = (FLAC__int32)ssbuffer_[sample];
2434 else if(bps == 24) {
2435 if(!is_big_endian) {
2437 const unsigned bytes = wide_samples * channels * (bps >> 3);
2438 for(byte = 0; byte < bytes; byte += 3) {
2439 tmp = ucbuffer_[byte];
2440 ucbuffer_[byte] = ucbuffer_[byte+2];
2441 ucbuffer_[byte+2] = tmp;
2444 if(is_unsigned_samples) {
2445 for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2446 for(channel = 0; channel < channels; channel++, sample++) {
2447 out[channel][wide_sample] = ucbuffer_[byte++]; out[channel][wide_sample] <<= 8;
2448 out[channel][wide_sample] |= ucbuffer_[byte++]; out[channel][wide_sample] <<= 8;
2449 out[channel][wide_sample] |= ucbuffer_[byte++];
2450 out[channel][wide_sample] -= 0x800000;
2454 for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2455 for(channel = 0; channel < channels; channel++, sample++) {
2456 out[channel][wide_sample] = scbuffer_[byte++]; out[channel][wide_sample] <<= 8;
2457 out[channel][wide_sample] |= ucbuffer_[byte++]; out[channel][wide_sample] <<= 8;
2458 out[channel][wide_sample] |= ucbuffer_[byte++];
2466 FLAC__int32 mask = (1<<shift)-1;
2467 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2468 for(channel = 0; channel < channels; channel++) {
2469 if(out[channel][wide_sample] & mask) {
2470 flac__utils_printf(stderr, 1, "ERROR during read, sample data (channel#%u sample#%u = %d) has non-zero least-significant bits\n WAVE/AIFF header said the last %u bits are not significant and should be zero.\n", channel, wide_sample, out[channel][wide_sample], shift);
2473 out[channel][wide_sample] >>= shift;
2479 void encoder_progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
2481 EncoderSession *encoder_session = (EncoderSession*)client_data;
2483 (void)encoder, (void)total_frames_estimate;
2485 encoder_session->bytes_written = bytes_written;
2486 encoder_session->samples_written = samples_written;
2488 if(encoder_session->total_samples_to_encode > 0 && !((frames_written-1) & encoder_session->stats_mask))
2489 print_stats(encoder_session);
2492 FLAC__StreamDecoderReadStatus flac_decoder_read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
2495 FLACDecoderData *data = (FLACDecoderData*)client_data;
2498 if (data->fatal_error)
2499 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2501 /* use up lookahead first */
2502 if (data->lookahead_length) {
2503 n = min(data->lookahead_length, *bytes);
2504 memcpy(buffer, data->lookahead, n);
2506 data->lookahead += n;
2507 data->lookahead_length -= n;
2510 /* get the rest from file */
2512 *bytes = n + fread(buffer, 1, *bytes-n, data->encoder_session->fin);
2513 if(ferror(data->encoder_session->fin))
2514 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2515 else if(0 == *bytes)
2516 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
2518 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2521 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2524 FLAC__StreamDecoderSeekStatus flac_decoder_seek_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
2526 FLACDecoderData *data = (FLACDecoderData*)client_data;
2529 if(fseeko(data->encoder_session->fin, (off_t)absolute_byte_offset, SEEK_SET) < 0)
2530 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
2532 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
2535 FLAC__StreamDecoderTellStatus flac_decoder_tell_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
2537 FLACDecoderData *data = (FLACDecoderData*)client_data;
2541 if((pos = ftello(data->encoder_session->fin)) < 0)
2542 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
2544 *absolute_byte_offset = (FLAC__uint64)pos;
2545 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
2549 FLAC__StreamDecoderLengthStatus flac_decoder_length_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
2551 FLACDecoderData *data = (FLACDecoderData*)client_data;
2554 if(0 == data->filesize)
2555 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
2557 *stream_length = (FLAC__uint64)data->filesize;
2558 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
2562 FLAC__bool flac_decoder_eof_callback(const FLAC__StreamDecoder *decoder, void *client_data)
2564 FLACDecoderData *data = (FLACDecoderData*)client_data;
2567 return feof(data->encoder_session->fin)? true : false;
2570 FLAC__StreamDecoderWriteStatus flac_decoder_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
2572 FLACDecoderData *data = (FLACDecoderData*)client_data;
2573 FLAC__uint64 n = min(data->samples_left_to_process, frame->header.blocksize);
2576 if(!EncoderSession_process(data->encoder_session, buffer, (unsigned)n)) {
2577 print_error_with_state(data->encoder_session, "ERROR during encoding");
2578 data->fatal_error = true;
2579 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2582 data->samples_left_to_process -= n;
2583 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2586 void flac_decoder_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
2588 FLACDecoderData *data = (FLACDecoderData*)client_data;
2591 if (data->fatal_error)
2595 data->num_metadata_blocks == sizeof(data->metadata_blocks)/sizeof(data->metadata_blocks[0]) ||
2596 0 == (data->metadata_blocks[data->num_metadata_blocks] = FLAC__metadata_object_clone(metadata))
2598 data->fatal_error = true;
2600 data->num_metadata_blocks++;
2603 void flac_decoder_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
2605 FLACDecoderData *data = (FLACDecoderData*)client_data;
2608 flac__utils_printf(stderr, 1, "%s: ERROR got %s while decoding FLAC input\n", data->encoder_session->inbasefilename, FLAC__StreamDecoderErrorStatusString[status]);
2609 if(!data->encoder_session->continue_through_decode_errors)
2610 data->fatal_error = true;
2613 FLAC__bool parse_cuesheet(FLAC__StreamMetadata **cuesheet, const char *cuesheet_filename, const char *inbasefilename, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset, FLAC__bool treat_warnings_as_errors)
2616 unsigned last_line_read;
2617 const char *error_message;
2619 if(0 == cuesheet_filename)
2622 if(lead_out_offset == 0) {
2623 flac__utils_printf(stderr, 1, "%s: ERROR cannot import cuesheet when the number of input samples to encode is unknown\n", inbasefilename);
2627 if(0 == (f = fopen(cuesheet_filename, "r"))) {
2628 flac__utils_printf(stderr, 1, "%s: ERROR opening cuesheet \"%s\" for reading: %s\n", inbasefilename, cuesheet_filename, strerror(errno));
2632 *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, is_cdda, lead_out_offset);
2636 if(0 == *cuesheet) {
2637 flac__utils_printf(stderr, 1, "%s: ERROR parsing cuesheet \"%s\" on line %u: %s\n", inbasefilename, cuesheet_filename, last_line_read, error_message);
2641 if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) {
2642 flac__utils_printf(stderr, 1, "%s: ERROR parsing cuesheet \"%s\": %s\n", inbasefilename, cuesheet_filename, error_message);
2646 /* if we're expecting CDDA, warn about non-compliance */
2647 if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) {
2648 flac__utils_printf(stderr, 1, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", inbasefilename, cuesheet_filename, error_message);
2649 if(treat_warnings_as_errors)
2651 (*cuesheet)->data.cue_sheet.is_cd = false;
2657 void print_stats(const EncoderSession *encoder_session)
2659 const FLAC__uint64 samples_written = min(encoder_session->total_samples_to_encode, encoder_session->samples_written);
2660 #if defined _MSC_VER || defined __MINGW32__
2661 /* with MSVC you have to spoon feed it the casting */
2662 const double progress = (double)(FLAC__int64)samples_written / (double)(FLAC__int64)encoder_session->total_samples_to_encode;
2663 const double ratio = (double)(FLAC__int64)encoder_session->bytes_written / ((double)(FLAC__int64)encoder_session->unencoded_size * min(1.0, progress));
2665 const double progress = (double)samples_written / (double)encoder_session->total_samples_to_encode;
2666 const double ratio = (double)encoder_session->bytes_written / ((double)encoder_session->unencoded_size * min(1.0, progress));
2669 FLAC__ASSERT(encoder_session->total_samples_to_encode > 0);
2671 if(samples_written == encoder_session->total_samples_to_encode) {
2672 flac__utils_printf(stderr, 2, "\r%s:%s wrote %u bytes, ratio=%0.3f",
2673 encoder_session->inbasefilename,
2674 encoder_session->verify? " Verify OK," : "",
2675 (unsigned)encoder_session->bytes_written,
2680 flac__utils_printf(stderr, 2, "\r%s: %u%% complete, ratio=%0.3f", encoder_session->inbasefilename, (unsigned)floor(progress * 100.0 + 0.5), ratio);
2684 void print_error_with_init_status(const EncoderSession *e, const char *message, FLAC__StreamEncoderInitStatus init_status)
2686 const int ilen = strlen(e->inbasefilename) + 1;
2687 const char *state_string = "";
2689 flac__utils_printf(stderr, 1, "\n%s: %s\n", e->inbasefilename, message);
2691 flac__utils_printf(stderr, 1, "%*s init_status = %s\n", ilen, "", FLAC__StreamEncoderInitStatusString[init_status]);
2693 if(init_status == FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR) {
2694 state_string = FLAC__stream_encoder_get_resolved_state_string(e->encoder);
2696 flac__utils_printf(stderr, 1, "%*s state = %s\n", ilen, "", state_string);
2698 /* print out some more info for some errors: */
2699 if(0 == strcmp(state_string, FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_CLIENT_ERROR])) {
2700 flac__utils_printf(stderr, 1,
2702 "An error occurred while writing; the most common cause is that the disk is full.\n"
2705 else if(0 == strcmp(state_string, FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_IO_ERROR])) {
2706 flac__utils_printf(stderr, 1,
2708 "An error occurred opening the output file; it is likely that the output\n"
2709 "directory does not exist or is not writable, the output file already exists and\n"
2710 "is not writable, or the disk is full.\n"
2714 else if(init_status == FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE) {
2715 flac__utils_printf(stderr, 1,
2717 "The encoding parameters specified do not conform to the FLAC Subset and may not\n"
2718 "be streamable or playable in hardware devices. If you really understand the\n"
2719 "consequences, you can add --lax to the command-line options to encode with\n"
2720 "these parameters anyway. See http://flac.sourceforge.net/format.html#subset\n"
2725 void print_error_with_state(const EncoderSession *e, const char *message)
2727 const int ilen = strlen(e->inbasefilename) + 1;
2728 const char *state_string;
2730 flac__utils_printf(stderr, 1, "\n%s: %s\n", e->inbasefilename, message);
2732 state_string = FLAC__stream_encoder_get_resolved_state_string(e->encoder);
2734 flac__utils_printf(stderr, 1, "%*s state = %s\n", ilen, "", state_string);
2736 /* print out some more info for some errors: */
2737 if(0 == strcmp(state_string, FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_CLIENT_ERROR])) {
2738 flac__utils_printf(stderr, 1,
2740 "An error occurred while writing; the most common cause is that the disk is full.\n"
2745 void print_verify_error(EncoderSession *e)
2747 FLAC__uint64 absolute_sample;
2748 unsigned frame_number;
2751 FLAC__int32 expected;
2754 FLAC__stream_encoder_get_verify_decoder_error_stats(e->encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
2756 flac__utils_printf(stderr, 1, "%s: ERROR: mismatch in decoded data, verify FAILED!\n", e->inbasefilename);
2757 flac__utils_printf(stderr, 1, " Absolute sample=%u, frame=%u, channel=%u, sample=%u, expected %d, got %d\n", (unsigned)absolute_sample, frame_number, channel, sample, expected, got);
2758 flac__utils_printf(stderr, 1, " In all known cases, verify errors are caused by hardware problems,\n");
2759 flac__utils_printf(stderr, 1, " usually overclocking or bad RAM. Delete %s\n", e->outfilename);
2760 flac__utils_printf(stderr, 1, " and repeat the flac command exactly as before. If it does not give a\n");
2761 flac__utils_printf(stderr, 1, " verify error in the exact same place each time you try it, then there is\n");
2762 flac__utils_printf(stderr, 1, " a problem with your hardware; please see the FAQ:\n");
2763 flac__utils_printf(stderr, 1, " http://flac.sourceforge.net/faq.html#tools__hardware_prob\n");
2764 flac__utils_printf(stderr, 1, " If it does fail in the exact same place every time, keep\n");
2765 flac__utils_printf(stderr, 1, " %s and submit a bug report to:\n", e->outfilename);
2766 flac__utils_printf(stderr, 1, " https://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
2767 flac__utils_printf(stderr, 1, " Make sure to upload the FLAC file and use the \"Monitor\" feature to\n");
2768 flac__utils_printf(stderr, 1, " monitor the bug status.\n");
2769 flac__utils_printf(stderr, 1, "Verify FAILED! Do not trust %s\n", e->outfilename);
2772 FLAC__bool read_little_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn)
2774 size_t bytes_read = fread(val, 1, 2, f);
2776 if(bytes_read == 0) {
2778 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2784 else if(bytes_read < 2) {
2785 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2789 if(is_big_endian_host_) {
2790 FLAC__byte tmp, *b = (FLAC__byte*)val;
2791 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
2797 FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
2799 size_t bytes_read = fread(val, 1, 4, f);
2801 if(bytes_read == 0) {
2803 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2809 else if(bytes_read < 4) {
2810 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2814 if(is_big_endian_host_) {
2815 FLAC__byte tmp, *b = (FLAC__byte*)val;
2816 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
2817 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
2823 FLAC__bool read_big_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn)
2825 unsigned char buf[4];
2826 size_t bytes_read= fread(buf, 1, 2, f);
2828 if(bytes_read==0U && eof_ok)
2830 else if(bytes_read<2U) {
2831 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2835 /* this is independent of host endianness */
2836 *val= (FLAC__uint16)(buf[0])<<8 | buf[1];
2841 FLAC__bool read_big_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
2843 unsigned char buf[4];
2844 size_t bytes_read= fread(buf, 1, 4, f);
2846 if(bytes_read==0U && eof_ok)
2848 else if(bytes_read<4U) {
2849 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2853 /* this is independent of host endianness */
2854 *val= (FLAC__uint32)(buf[0])<<24 | (FLAC__uint32)(buf[1])<<16 |
2855 (FLAC__uint32)(buf[2])<<8 | buf[3];
2860 FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
2861 /* Read an IEEE 754 80-bit (aka SANE) extended floating point value from 'f',
2862 * convert it into an integral value and store in 'val'. Return false if only
2863 * between 1 and 9 bytes remain in 'f', if 0 bytes remain in 'f' and 'eof_ok' is
2864 * false, or if the value is negative, between zero and one, or too large to be
2865 * represented by 'val'; return true otherwise.
2869 unsigned char buf[10];
2870 size_t bytes_read= fread(buf, 1U, 10U, f);
2871 FLAC__int16 e= ((FLAC__uint16)(buf[0])<<8 | (FLAC__uint16)(buf[1]))-0x3FFF;
2872 FLAC__int16 shift= 63-e;
2875 if(bytes_read==0U && eof_ok)
2877 else if(bytes_read<10U) {
2878 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2881 else if((buf[0]>>7)==1U || e<0 || e>63) {
2882 flac__utils_printf(stderr, 1, "%s: ERROR: invalid floating-point value\n", fn);
2886 for(i= 0U; i<8U; ++i)
2887 p|= (FLAC__uint64)(buf[i+2])<<(56U-i*8);
2888 *val= (FLAC__uint32)((p>>shift)+(p>>(shift-1) & 0x1));
2893 FLAC__bool fskip_ahead(FILE *f, FLAC__uint64 offset)
2895 static unsigned char dump[8192];
2898 long need = (long)min(offset, LONG_MAX);
2899 if(fseeko(f, need, SEEK_CUR) < 0) {
2900 need = (long)min(offset, sizeof(dump));
2901 if((long)fread(dump, 1, need, f) < need)
2906 #if 0 /* pure non-fseek() version */
2908 const long need = (long)min(offset, sizeof(dump));
2909 if(fread(dump, 1, need, f) < need)
2917 unsigned count_channel_mask_bits(FLAC__uint32 mask)
2929 FLAC__uint32 limit_channel_mask(FLAC__uint32 mask, unsigned channels)
2931 FLAC__uint32 x = 0x80000000;
2932 unsigned count = count_channel_mask_bits(mask);
2933 while(x && count > channels) {
2940 FLAC__ASSERT(count_channel_mask_bits(mask) == channels);