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_bytes(FILE *f, FLAC__byte *buf, size_t n, FLAC__bool eof_ok, const char *fn);
151 static FLAC__bool read_uint16(FILE *f, FLAC__bool big_endian, FLAC__uint16 *val, const char *fn);
152 static FLAC__bool read_uint32(FILE *f, FLAC__bool big_endian, FLAC__uint32 *val, const char *fn);
153 static FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, const char *fn);
154 static FLAC__bool fskip_ahead(FILE *f, FLAC__uint64 offset);
155 static unsigned count_channel_mask_bits(FLAC__uint32 mask);
157 static FLAC__uint32 limit_channel_mask(FLAC__uint32 mask, unsigned channels);
163 int flac__encode_aiff(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)
165 EncoderSession encoder_session;
166 unsigned channels = 0, bps = 0, sample_rate = 0, shift = 0, sample_frames = 0;
167 size_t channel_map[FLAC__MAX_CHANNELS];
170 FLAC__bool got_comm_chunk = false, got_ssnd_chunk = false;
171 int info_align_carry = -1, info_align_zero = -1;
172 FLAC__bool is_big_endian_pcm = true;
174 (void)infilesize; /* silence compiler warning about unused parameter */
175 (void)lookahead; /* silence compiler warning about unused parameter */
176 (void)lookahead_length; /* silence compiler warning about unused parameter */
179 EncoderSession_construct(
182 options.common.use_ogg,
186 options.common.verify,
187 options.common.treat_warnings_as_errors,
188 options.common.continue_through_decode_errors,
196 /* initialize default channel map that preserves channel order */
199 for(i = 0; i < sizeof(channel_map)/sizeof(channel_map[0]); i++)
203 if(options.foreign_metadata) {
205 if(!flac__foreign_metadata_read_from_aiff(options.foreign_metadata, infilename, &error)) {
206 flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", encoder_session.inbasefilename, error);
207 return EncoderSession_finish_error(&encoder_session);
212 * lookahead[] already has "FORMxxxxAIFF", do chunks
214 while(!feof(infile)) {
215 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 */
216 if(!read_bytes(infile, (FLAC__byte*)chunk_id, 4, /*eof_ok=*/true, encoder_session.inbasefilename)) {
217 flac__utils_printf(stderr, 1, "%s: ERROR: incomplete chunk identifier\n", encoder_session.inbasefilename);
218 return EncoderSession_finish_error(&encoder_session);
223 if(!memcmp(chunk_id, "COMM", 4) && !got_comm_chunk) { /* common chunk */
225 const FLAC__uint32 minimum_comm_size = (is_aifc? 22 : 18);
227 /* COMM chunk size */
228 if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
229 return EncoderSession_finish_error(&encoder_session);
230 else if(xx < minimum_comm_size) {
231 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);
232 return EncoderSession_finish_error(&encoder_session);
234 else if(!is_aifc && xx != minimum_comm_size) {
235 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);
236 if(encoder_session.treat_warnings_as_errors)
237 return EncoderSession_finish_error(&encoder_session);
239 skip = (xx-minimum_comm_size)+(xx & 1U);
241 /* number of channels */
242 if(!read_uint16(infile, /*big_endian=*/true, &x, encoder_session.inbasefilename))
243 return EncoderSession_finish_error(&encoder_session);
244 else if(x ==0U || x>FLAC__MAX_CHANNELS) {
245 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u\n", encoder_session.inbasefilename, (unsigned int)x);
246 return EncoderSession_finish_error(&encoder_session);
248 else if(x>2U && !options.common.channel_map_none) {
249 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u for AIFF\n", encoder_session.inbasefilename, (unsigned int)x);
250 return EncoderSession_finish_error(&encoder_session);
252 else if(options.common.sector_align && x!=2U) {
253 flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
254 return EncoderSession_finish_error(&encoder_session);
258 /* number of sample frames */
259 if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
260 return EncoderSession_finish_error(&encoder_session);
263 /* bits per sample */
264 if(!read_uint16(infile, /*big_endian=*/true, &x, encoder_session.inbasefilename))
265 return EncoderSession_finish_error(&encoder_session);
266 else if(x<4U || x>24U) {
267 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits-per-sample %u\n", encoder_session.inbasefilename, (unsigned int)x);
268 return EncoderSession_finish_error(&encoder_session);
270 else if(options.common.sector_align && x!=16U) {
271 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);
272 return EncoderSession_finish_error(&encoder_session);
275 shift = (bps%8)? 8-(bps%8) : 0; /* SSND data is always byte-aligned, left-justified but format_input() will double-check */
279 if(!read_sane_extended(infile, &xx, encoder_session.inbasefilename))
280 return EncoderSession_finish_error(&encoder_session);
281 else if(!FLAC__format_sample_rate_is_valid(xx)) {
282 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, (unsigned int)xx);
283 return EncoderSession_finish_error(&encoder_session);
285 else if(options.common.sector_align && xx!=44100U) {
286 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);
287 return EncoderSession_finish_error(&encoder_session);
291 /* check compression type for AIFF-C */
293 if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
294 return EncoderSession_finish_error(&encoder_session);
295 if(xx == 0x736F7774) /* "sowt" */
296 is_big_endian_pcm = false;
297 else if(xx == 0x4E4F4E45) /* "NONE" */
298 ; /* nothing to do, we already default to big-endian */
300 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));
301 return EncoderSession_finish_error(&encoder_session);
305 /* set channel mapping */
306 /* FLAC order follows SMPTE and WAVEFORMATEXTENSIBLE but with fewer channels, which are: */
307 /* front left, front right, center, LFE, back left, back right, surround left, surround right */
308 /* specs say the channel ordering is:
310 * ___________________________________________________
314 * quad (ambiguous with 4ch) Fl Fr Bl Br
317 * 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
318 * so we only have unambiguous mappings for 2, 3, and 5 channels
321 options.common.channel_map_none ||
322 channels == 1 || /* 1 channel: (mono) */
323 channels == 2 || /* 2 channels: left, right */
324 channels == 3 || /* 3 channels: left, right, center */
325 channels == 5 /* 5 channels: front left, front right, center, surround left, surround right */
327 /* keep default channel order */
330 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u for AIFF\n", encoder_session.inbasefilename, channels);
331 return EncoderSession_finish_error(&encoder_session);
334 /* skip any extra data in the COMM chunk */
335 if(!fskip_ahead(infile, skip)) {
336 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over extra COMM data\n", encoder_session.inbasefilename);
337 return EncoderSession_finish_error(&encoder_session);
341 * now that we know the sample rate, canonicalize the
342 * --skip string to a number of samples:
344 flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, sample_rate);
345 FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
346 encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
347 FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
349 got_comm_chunk = true;
351 else if(!memcmp(chunk_id, "SSND", 4) && !got_ssnd_chunk) { /* sound data chunk */
352 unsigned offset = 0, block_size = 0, align_remainder = 0, data_bytes;
353 FLAC__uint64 total_samples_in_input, trim = 0;
354 FLAC__bool pad = false;
355 const size_t bytes_per_wide_sample = channels * (bps >> 3);
357 if(got_comm_chunk == false) {
358 flac__utils_printf(stderr, 1, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", encoder_session.inbasefilename);
359 return EncoderSession_finish_error(&encoder_session);
362 /* SSND chunk size */
363 if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
364 return EncoderSession_finish_error(&encoder_session);
365 if(options.common.ignore_chunk_sizes) {
366 FLAC__ASSERT(!options.common.sector_align);
367 data_bytes = (unsigned)(-(int)bytes_per_wide_sample); /* max out data_bytes; we'll use EOF as signal to stop reading */
371 data_bytes-= 8U; /* discount the offset and block size fields */
373 pad = (data_bytes & 1U) ? true : false;
376 if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
377 return EncoderSession_finish_error(&encoder_session);
382 if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
383 return EncoderSession_finish_error(&encoder_session);
385 flac__utils_printf(stderr, 1, "%s: ERROR: block size is %u; must be 0\n", encoder_session.inbasefilename, (unsigned int)xx);
386 return EncoderSession_finish_error(&encoder_session);
390 /* skip any SSND offset bytes */
391 FLAC__ASSERT(offset<=LONG_MAX);
392 if(!fskip_ahead(infile, offset)) {
393 flac__utils_printf(stderr, 1, "%s: ERROR: skipping offset in SSND chunk\n", encoder_session.inbasefilename);
394 return EncoderSession_finish_error(&encoder_session);
396 if(data_bytes!=(sample_frames*bytes_per_wide_sample)) {
397 flac__utils_printf(stderr, 1, "%s: ERROR: SSND chunk size inconsistent with sample frame count\n", encoder_session.inbasefilename);
398 return EncoderSession_finish_error(&encoder_session);
401 /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
402 FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
403 total_samples_in_input = data_bytes / bytes_per_wide_sample + *options.common.align_reservoir_samples;
406 * now that we know the input size, canonicalize the
407 * --until string to an absolute sample number:
409 if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, sample_rate, encoder_session.skip, total_samples_in_input))
410 return EncoderSession_finish_error(&encoder_session);
411 encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
412 FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
414 if(encoder_session.skip>0U) {
415 if(!fskip_ahead(infile, encoder_session.skip*bytes_per_wide_sample)) {
416 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
417 return EncoderSession_finish_error(&encoder_session);
421 data_bytes-= (unsigned int)encoder_session.skip*bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */
422 if(options.common.ignore_chunk_sizes) {
423 encoder_session.total_samples_to_encode = 0;
424 flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
425 FLAC__ASSERT(0 == encoder_session.until);
428 encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
430 if(encoder_session.until > 0) {
431 trim = total_samples_in_input - encoder_session.until;
432 FLAC__ASSERT(total_samples_in_input > 0);
433 FLAC__ASSERT(!options.common.sector_align);
434 data_bytes-= (unsigned int)trim*bytes_per_wide_sample;
435 encoder_session.total_samples_to_encode-= trim;
437 if(options.common.sector_align) {
438 align_remainder = (unsigned int)(encoder_session.total_samples_to_encode % 588U);
439 if(options.common.is_last_file)
440 encoder_session.total_samples_to_encode+= (588U-align_remainder); /* will pad with zeroes */
442 encoder_session.total_samples_to_encode-= align_remainder; /* will stop short and carry over to next file */
445 /* +54 for the size of the AIFF headers; this is just an estimate for the progress indicator and doesn't need to be exact */
446 encoder_session.unencoded_size = encoder_session.total_samples_to_encode*bytes_per_wide_sample+54;
448 if(!EncoderSession_init_encoder(&encoder_session, options.common, /*channel_mask=*/0, channels, bps-shift, sample_rate, options.foreign_metadata, /*flac_decoder_data=*/0))
449 return EncoderSession_finish_error(&encoder_session);
451 /* first do any samples in the reservoir */
452 if(options.common.sector_align && *options.common.align_reservoir_samples>0U) {
454 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
455 print_error_with_state(&encoder_session, "ERROR during encoding");
456 return EncoderSession_finish_error(&encoder_session);
460 /* decrement the data_bytes counter if we need to align the file */
461 if(options.common.sector_align) {
462 if(options.common.is_last_file)
463 *options.common.align_reservoir_samples = 0U;
465 *options.common.align_reservoir_samples = align_remainder;
466 data_bytes-= (*options.common.align_reservoir_samples)*bytes_per_wide_sample;
470 /* now do from the file */
471 while(data_bytes>0) {
472 size_t bytes_read = fread(ucbuffer_, 1U, min(data_bytes, CHUNK_OF_SAMPLES*bytes_per_wide_sample), infile);
474 if(bytes_read == 0U) {
476 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
477 return EncoderSession_finish_error(&encoder_session);
479 else if(feof(infile)) {
480 if(options.common.ignore_chunk_sizes) {
481 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);
484 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);
485 if(encoder_session.treat_warnings_as_errors)
486 return EncoderSession_finish_error(&encoder_session);
492 if(bytes_read % bytes_per_wide_sample != 0U) {
493 flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
494 return EncoderSession_finish_error(&encoder_session);
497 unsigned int frames = bytes_read/bytes_per_wide_sample;
498 if(!format_input(input_, frames, is_big_endian_pcm, /*is_unsigned_samples=*/false, channels, bps, shift, channel_map))
499 return EncoderSession_finish_error(&encoder_session);
501 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, frames)) {
502 print_error_with_state(&encoder_session, "ERROR during encoding");
503 return EncoderSession_finish_error(&encoder_session);
506 data_bytes-= bytes_read;
512 FLAC__ASSERT(!options.common.sector_align);
513 if(!fskip_ahead(infile, trim*bytes_per_wide_sample)) {
514 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
515 return EncoderSession_finish_error(&encoder_session);
519 /* now read unaligned samples into reservoir or pad with zeroes if necessary */
520 if(options.common.sector_align) {
521 if(options.common.is_last_file) {
522 unsigned int pad_frames = 588U-align_remainder;
524 if(pad_frames<588U) {
527 info_align_zero = pad_frames;
528 for(i = 0U; i<channels; ++i)
529 memset(input_[i], 0, sizeof(input_[0][0])*pad_frames);
531 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, pad_frames)) {
532 print_error_with_state(&encoder_session, "ERROR during encoding");
533 return EncoderSession_finish_error(&encoder_session);
538 if(*options.common.align_reservoir_samples > 0) {
539 size_t bytes_read = fread(ucbuffer_, 1U, (*options.common.align_reservoir_samples)*bytes_per_wide_sample, infile);
541 FLAC__ASSERT(CHUNK_OF_SAMPLES>=588U);
542 if(bytes_read == 0U && ferror(infile)) {
543 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
544 return EncoderSession_finish_error(&encoder_session);
546 else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
547 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);
548 if(encoder_session.treat_warnings_as_errors)
549 return EncoderSession_finish_error(&encoder_session);
552 info_align_carry = *options.common.align_reservoir_samples;
553 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))
554 return EncoderSession_finish_error(&encoder_session);
563 if(fread(&tmp, 1U, 1U, infile) < 1U) {
564 flac__utils_printf(stderr, 1, "%s: ERROR during read of SSND pad byte\n", encoder_session.inbasefilename);
565 return EncoderSession_finish_error(&encoder_session);
569 got_ssnd_chunk = true;
572 if(!memcmp(chunk_id, "SSND", 4) && !got_comm_chunk) {
573 flac__utils_printf(stderr, 1, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", encoder_session.inbasefilename);
574 return EncoderSession_finish_error(&encoder_session);
577 if(!options.foreign_metadata) {
578 if(!memcmp(chunk_id, "COMM", 4))
579 flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'COMM' chunk (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename);
580 else if(!memcmp(chunk_id, "SSND", 4))
581 flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'SSND' chunk (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename);
583 flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s' (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename, chunk_id);
584 if(encoder_session.treat_warnings_as_errors)
585 return EncoderSession_finish_error(&encoder_session);
589 if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
590 return EncoderSession_finish_error(&encoder_session);
592 unsigned long skip = xx+(xx & 1U);
594 FLAC__ASSERT(skip<=LONG_MAX);
595 if(!fskip_ahead(infile, skip)) {
596 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over unsupported chunk\n", encoder_session.inbasefilename);
597 return EncoderSession_finish_error(&encoder_session);
603 if(got_ssnd_chunk == false && sample_frames != 0U) {
604 flac__utils_printf(stderr, 1, "%s: ERROR: missing SSND chunk\n", encoder_session.inbasefilename);
605 return EncoderSession_finish_error(&encoder_session);
608 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, options.foreign_metadata);
611 int flac__encode_wave(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options)
613 EncoderSession encoder_session;
614 FLAC__bool is_unsigned_samples = false;
615 unsigned channels = 0, bps = 0, sample_rate = 0, shift = 0;
616 size_t channel_map[FLAC__MAX_CHANNELS];
617 FLAC__uint16 x, format; /* format is the wFormatTag word from the 'fmt ' chunk */
618 FLAC__uint32 xx, channel_mask = 0;
619 FLAC__bool got_fmt_chunk = false, got_data_chunk = false;
620 int info_align_carry = -1, info_align_zero = -1;
622 (void)infilesize; /* silence compiler warning about unused parameter */
623 (void)lookahead; /* silence compiler warning about unused parameter */
624 (void)lookahead_length; /* silence compiler warning about unused parameter */
627 EncoderSession_construct(
630 options.common.use_ogg,
634 options.common.verify,
635 options.common.treat_warnings_as_errors,
636 options.common.continue_through_decode_errors,
644 /* initialize default channel map that preserves channel order */
647 for(i = 0; i < sizeof(channel_map)/sizeof(channel_map[0]); i++)
651 if(options.foreign_metadata) {
653 if(!flac__foreign_metadata_read_from_wave(options.foreign_metadata, infilename, &error)) {
654 flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", encoder_session.inbasefilename, error);
655 return EncoderSession_finish_error(&encoder_session);
660 * lookahead[] already has "RIFFxxxxWAVE", do chunks
662 while(!feof(infile)) {
663 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 */
664 if(!read_bytes(infile, (FLAC__byte*)chunk_id, 4, /*eof_ok=*/true, encoder_session.inbasefilename)) {
665 flac__utils_printf(stderr, 1, "%s: ERROR: incomplete chunk identifier\n", encoder_session.inbasefilename);
666 return EncoderSession_finish_error(&encoder_session);
671 if(!memcmp(chunk_id, "fmt ", 4) && !got_fmt_chunk) { /* format chunk */
672 unsigned block_align, data_bytes;
675 * http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
676 * http://windowssdk.msdn.microsoft.com/en-us/library/ms713497.aspx
677 * 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 * 2 byte: format type: 1 for WAVE_FORMAT_PCM, 65534 for WAVE_FORMAT_EXTENSIBLE
683 * 4 byte: sample rate (Hz)
684 * 4 byte: avg bytes per sec
685 * 2 byte: block align
686 * 2 byte: bits per sample (not necessarily all significant)
688 * 2 byte: extension size in bytes (usually 0 for WAVEFORMATEX and 22 for WAVEFORMATEXTENSIBLE with PCM)
689 * WAVEFORMATEXTENSIBLE adds
690 * 2 byte: valid bits per sample
691 * 4 byte: channel mask
692 * 16 byte: subformat GUID, first 2 bytes have format type, 1 being PCM
694 * Current spec says WAVEFORMATEX with PCM must have bps == 8 or 16, or any multiple of 8 for WAVEFORMATEXTENSIBLE.
695 * 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.
697 * Block align for WAVE_FORMAT_PCM or WAVE_FORMAT_EXTENSIBLE is also supposed to be channels*bps/8
699 * If the channel mask has more set bits than # of channels, the extra MSBs are ignored.
700 * If the channel mask has less set bits than # of channels, the extra channels are unassigned to any speaker.
702 * Data is supposed to be unsigned for bps <= 8 else signed.
706 if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
707 return EncoderSession_finish_error(&encoder_session);
709 if(data_bytes < 16) {
710 flac__utils_printf(stderr, 1, "%s: ERROR: found non-standard 'fmt ' chunk which has length = %u\n", encoder_session.inbasefilename, data_bytes);
711 return EncoderSession_finish_error(&encoder_session);
714 if(!read_uint16(infile, /*big_endian=*/false, &format, encoder_session.inbasefilename))
715 return EncoderSession_finish_error(&encoder_session);
716 if(format != 1 /*WAVE_FORMAT_PCM*/ && format != 65534 /*WAVE_FORMAT_EXTENSIBLE*/) {
717 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported format type %u\n", encoder_session.inbasefilename, (unsigned)format);
718 return EncoderSession_finish_error(&encoder_session);
720 /* number of channels */
721 if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
722 return EncoderSession_finish_error(&encoder_session);
723 channels = (unsigned)x;
724 if(channels == 0 || channels > FLAC__MAX_CHANNELS) {
725 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number of channels %u\n", encoder_session.inbasefilename, channels);
726 return EncoderSession_finish_error(&encoder_session);
728 else if(options.common.sector_align && channels != 2) {
729 flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, channels);
730 return EncoderSession_finish_error(&encoder_session);
733 if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
734 return EncoderSession_finish_error(&encoder_session);
736 if(!FLAC__format_sample_rate_is_valid(sample_rate)) {
737 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, sample_rate);
738 return EncoderSession_finish_error(&encoder_session);
740 else if(options.common.sector_align && sample_rate != 44100) {
741 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);
742 return EncoderSession_finish_error(&encoder_session);
744 /* avg bytes per second (ignored) */
745 if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
746 return EncoderSession_finish_error(&encoder_session);
748 if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
749 return EncoderSession_finish_error(&encoder_session);
750 block_align = (unsigned)x;
751 /* bits per sample */
752 if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
753 return EncoderSession_finish_error(&encoder_session);
755 is_unsigned_samples = (bps <= 8);
757 if(bps != 8 && bps != 16) {
758 if(bps == 24 || bps == 32) {
759 /* let these slide with a warning since they're unambiguous */
760 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);
761 if(encoder_session.treat_warnings_as_errors)
762 return EncoderSession_finish_error(&encoder_session);
765 /* @@@ we could add an option to specify left- or right-justified blocks so we knew how to set 'shift' */
766 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);
767 return EncoderSession_finish_error(&encoder_session);
770 #if 0 /* @@@ reinstate once we can get an answer about whether the samples are left- or right-justified */
771 if((bps+7)/8 * channels == block_align) {
773 /* assume legacy file is byte aligned with some LSBs zero; this is double-checked in format_input() */
774 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);
775 if(encoder_session.treat_warnings_as_errors)
776 return EncoderSession_finish_error(&encoder_session);
777 shift = 8 - (bps % 8);
784 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);
785 return EncoderSession_finish_error(&encoder_session);
790 if(channels > 2 && !options.common.channel_map_none) {
791 flac__utils_printf(stderr, 1, "%s: ERROR: WAVE has >2 channels but is not WAVE_FORMAT_EXTENSIBLE; cannot assign channels\n", encoder_session.inbasefilename);
792 return EncoderSession_finish_error(&encoder_session);
794 FLAC__ASSERT(data_bytes >= 16);
798 if(data_bytes < 40) {
799 flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with size %u\n", encoder_session.inbasefilename, data_bytes);
800 return EncoderSession_finish_error(&encoder_session);
803 if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
804 return EncoderSession_finish_error(&encoder_session);
806 flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with cbSize %u\n", encoder_session.inbasefilename, (unsigned)x);
807 return EncoderSession_finish_error(&encoder_session);
810 if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
811 return EncoderSession_finish_error(&encoder_session);
812 if((unsigned)x > bps) {
813 flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with wValidBitsPerSample (%u) > wBitsPerSample (%u)\n", encoder_session.inbasefilename, (unsigned)x, bps);
814 return EncoderSession_finish_error(&encoder_session);
816 shift = bps - (unsigned)x;
818 if(!read_uint32(infile, /*big_endian=*/false, &channel_mask, encoder_session.inbasefilename))
819 return EncoderSession_finish_error(&encoder_session);
820 /* for mono/stereo and unassigned channels, we fake the mask */
821 if(channel_mask == 0) {
823 channel_mask = 0x0001;
824 else if(channels == 2)
825 channel_mask = 0x0003;
827 /* set channel mapping */
828 /* FLAC order follows SMPTE and WAVEFORMATEXTENSIBLE but with fewer channels, which are: */
829 /* front left, front right, center, LFE, back left, back right, surround left, surround right */
830 /* the default mapping is sufficient for 1-6 channels and 7-8 are currently unspecified anyway */
832 /* @@@ example for dolby/vorbis order, for reference later in case it becomes important */
834 options.common.channel_map_none ||
835 channel_mask == 0x0001 || /* 1 channel: (mono) */
836 channel_mask == 0x0003 || /* 2 channels: front left, front right */
837 channel_mask == 0x0033 || /* 4 channels: front left, front right, back left, back right */
838 channel_mask == 0x0603 /* 4 channels: front left, front right, side left, side right */
840 /* keep default channel order */
843 channel_mask == 0x0007 || /* 3 channels: front left, front right, front center */
844 channel_mask == 0x0037 || /* 5 channels: front left, front right, front center, back left, back right */
845 channel_mask == 0x0607 /* 5 channels: front left, front right, front center, side left, side right */
847 /* to dolby order: front left, center, front right [, surround left, surround right ] */
852 channel_mask == 0x003f || /* 6 channels: front left, front right, front center, LFE, back left, back right */
853 channel_mask == 0x060f /* 6 channels: front left, front right, front center, LFE, side left, side right */
855 /* to dolby order: front left, center, front right, surround left, surround right, LFE */
864 options.common.channel_map_none ||
865 channel_mask == 0x0001 || /* 1 channel: (mono) */
866 channel_mask == 0x0003 || /* 2 channels: front left, front right */
867 channel_mask == 0x0007 || /* 3 channels: front left, front right, front center */
868 channel_mask == 0x0033 || /* 4 channels: front left, front right, back left, back right */
869 channel_mask == 0x0603 || /* 4 channels: front left, front right, side left, side right */
870 channel_mask == 0x0037 || /* 5 channels: front left, front right, front center, back left, back right */
871 channel_mask == 0x0607 || /* 5 channels: front left, front right, front center, side left, side right */
872 channel_mask == 0x003f || /* 6 channels: front left, front right, front center, LFE, back left, back right */
873 channel_mask == 0x060f /* 6 channels: front left, front right, front center, LFE, side left, side right */
875 /* keep default channel order */
879 flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk with unsupported channel mask=0x%04X\n\nUse --channel-map=none option to store channels in current order; FLAC files\nmust also be decoded with --channel-map=none to restore correct order.\n", encoder_session.inbasefilename, (unsigned)channel_mask);
880 return EncoderSession_finish_error(&encoder_session);
882 if(!options.common.channel_map_none) {
883 if(count_channel_mask_bits(channel_mask) < channels) {
884 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);
885 return EncoderSession_finish_error(&encoder_session);
888 /* supporting this is too difficult with channel mapping; e.g. what if mask is 0x003f but #channels=4?
889 * there would be holes in the order that would have to be filled in, or the mask would have to be
890 * limited and the logic above rerun to see if it still fits into the FLAC mapping.
892 else if(count_channel_mask_bits(channel_mask) > channels)
893 channel_mask = limit_channel_mask(channel_mask, channels);
895 else if(count_channel_mask_bits(channel_mask) > channels) {
896 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);
897 return EncoderSession_finish_error(&encoder_session);
901 /* first part of GUID */
902 if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
903 return EncoderSession_finish_error(&encoder_session);
905 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported WAVEFORMATEXTENSIBLE chunk with non-PCM format %u\n", encoder_session.inbasefilename, (unsigned)x);
906 return EncoderSession_finish_error(&encoder_session);
911 if(bps-shift < 4 || bps-shift > 24) {
912 flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits-per-sample %u\n", encoder_session.inbasefilename, bps-shift);
913 return EncoderSession_finish_error(&encoder_session);
915 else if(options.common.sector_align && bps-shift != 16) {
916 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);
917 return EncoderSession_finish_error(&encoder_session);
920 /* skip any extra data in the fmt chunk */
921 if(!fskip_ahead(infile, data_bytes)) {
922 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over extra 'fmt' data\n", encoder_session.inbasefilename);
923 return EncoderSession_finish_error(&encoder_session);
927 * now that we know the sample rate, canonicalize the
928 * --skip string to a number of samples:
930 flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, sample_rate);
931 FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
932 encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
933 FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
935 got_fmt_chunk = true;
937 else if(!memcmp(chunk_id, "data", 4) && !got_data_chunk && got_fmt_chunk) { /* data chunk */
938 unsigned align_remainder = 0, data_bytes;
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);
944 if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
945 return EncoderSession_finish_error(&encoder_session);
946 if(options.common.ignore_chunk_sizes) {
947 FLAC__ASSERT(!options.common.sector_align);
948 data_bytes = (unsigned)(-(int)bytes_per_wide_sample); /* max out data_bytes; we'll use EOF as signal to stop reading */
952 if(0 == data_bytes) {
953 flac__utils_printf(stderr, 1, "%s: ERROR: 'data' chunk has size of 0\n", encoder_session.inbasefilename);
954 return EncoderSession_finish_error(&encoder_session);
957 pad = (data_bytes & 1U) ? true : false;
959 /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
960 FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
961 total_samples_in_input = data_bytes / bytes_per_wide_sample + *options.common.align_reservoir_samples;
964 * now that we know the input size, canonicalize the
965 * --until string to an absolute sample number:
967 if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, sample_rate, encoder_session.skip, total_samples_in_input))
968 return EncoderSession_finish_error(&encoder_session);
969 encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
970 FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
972 if(encoder_session.skip > 0) {
973 if(!fskip_ahead(infile, encoder_session.skip * bytes_per_wide_sample)) {
974 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
975 return EncoderSession_finish_error(&encoder_session);
979 data_bytes -= (unsigned)encoder_session.skip * bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */
980 if(options.common.ignore_chunk_sizes) {
981 encoder_session.total_samples_to_encode = 0;
982 flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
983 FLAC__ASSERT(0 == encoder_session.until);
986 encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
988 if(encoder_session.until > 0) {
989 trim = total_samples_in_input - encoder_session.until;
990 FLAC__ASSERT(total_samples_in_input > 0);
991 FLAC__ASSERT(!options.common.sector_align);
992 data_bytes -= (unsigned int)trim * bytes_per_wide_sample;
993 encoder_session.total_samples_to_encode -= trim;
995 if(options.common.sector_align) {
996 align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
997 if(options.common.is_last_file)
998 encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
1000 encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
1003 /* +44 for the size of the WAV headers; this is just an estimate for the progress indicator and doesn't need to be exact */
1004 encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample + 44;
1006 if(!EncoderSession_init_encoder(&encoder_session, options.common, channel_mask, channels, bps-shift, sample_rate, options.foreign_metadata, /*flac_decoder_data=*/0))
1007 return EncoderSession_finish_error(&encoder_session);
1010 * first do any samples in the reservoir
1012 if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
1013 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
1014 print_error_with_state(&encoder_session, "ERROR during encoding");
1015 return EncoderSession_finish_error(&encoder_session);
1020 * decrement the data_bytes counter if we need to align the file
1022 if(options.common.sector_align) {
1023 if(options.common.is_last_file) {
1024 *options.common.align_reservoir_samples = 0;
1027 *options.common.align_reservoir_samples = align_remainder;
1028 data_bytes -= (*options.common.align_reservoir_samples) * bytes_per_wide_sample;
1033 * now do from the file
1035 while(data_bytes > 0) {
1036 size_t bytes_read = fread(ucbuffer_, sizeof(unsigned char), min(data_bytes, CHUNK_OF_SAMPLES * bytes_per_wide_sample), infile);
1037 if(bytes_read == 0) {
1038 if(ferror(infile)) {
1039 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1040 return EncoderSession_finish_error(&encoder_session);
1042 else if(feof(infile)) {
1043 if(options.common.ignore_chunk_sizes) {
1044 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);
1047 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);
1048 if(encoder_session.treat_warnings_as_errors)
1049 return EncoderSession_finish_error(&encoder_session);
1055 if(bytes_read % bytes_per_wide_sample != 0) {
1056 flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
1057 return EncoderSession_finish_error(&encoder_session);
1060 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
1061 if(!format_input(input_, wide_samples, /*is_big_endian=*/false, is_unsigned_samples, channels, bps, shift, channel_map))
1062 return EncoderSession_finish_error(&encoder_session);
1064 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1065 print_error_with_state(&encoder_session, "ERROR during encoding");
1066 return EncoderSession_finish_error(&encoder_session);
1068 data_bytes -= bytes_read;
1074 FLAC__ASSERT(!options.common.sector_align);
1075 if(!fskip_ahead(infile, trim * bytes_per_wide_sample)) {
1076 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
1077 return EncoderSession_finish_error(&encoder_session);
1082 * now read unaligned samples into reservoir or pad with zeroes if necessary
1084 if(options.common.sector_align) {
1085 if(options.common.is_last_file) {
1086 unsigned wide_samples = 588 - align_remainder;
1087 if(wide_samples < 588) {
1090 info_align_zero = wide_samples;
1091 for(channel = 0; channel < channels; channel++)
1092 memset(input_[channel], 0, sizeof(input_[0][0]) * wide_samples);
1094 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1095 print_error_with_state(&encoder_session, "ERROR during encoding");
1096 return EncoderSession_finish_error(&encoder_session);
1101 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(!memcmp(chunk_id, "data", 4) && !got_fmt_chunk) {
1136 flac__utils_printf(stderr, 1, "%s: ERROR: got 'data' chunk before 'fmt' chunk\n", encoder_session.inbasefilename);
1137 return EncoderSession_finish_error(&encoder_session);
1140 if(!options.foreign_metadata) {
1141 if(!memcmp(chunk_id, "fmt ", 4) && got_fmt_chunk)
1142 flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'fmt ' chunk (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename);
1143 else if(!memcmp(chunk_id, "data", 4))
1144 flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'data' chunk (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename);
1146 flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s' (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename, chunk_id);
1147 if(encoder_session.treat_warnings_as_errors)
1148 return EncoderSession_finish_error(&encoder_session);
1152 if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
1153 return EncoderSession_finish_error(&encoder_session);
1155 unsigned long skip = xx+(xx & 1U);
1157 FLAC__ASSERT(skip<=LONG_MAX);
1158 if(!fskip_ahead(infile, skip)) {
1159 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over unsupported chunk\n", encoder_session.inbasefilename);
1160 return EncoderSession_finish_error(&encoder_session);
1166 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, options.foreign_metadata);
1169 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)
1171 EncoderSession encoder_session;
1173 const size_t bytes_per_wide_sample = options.channels * (options.bps >> 3);
1174 unsigned align_remainder = 0;
1175 int info_align_carry = -1, info_align_zero = -1;
1176 FLAC__uint64 total_samples_in_input = 0;
1178 FLAC__ASSERT(!options.common.sector_align || options.channels == 2);
1179 FLAC__ASSERT(!options.common.sector_align || options.bps == 16);
1180 FLAC__ASSERT(!options.common.sector_align || options.sample_rate == 44100);
1181 FLAC__ASSERT(!options.common.sector_align || infilesize >= 0);
1182 FLAC__ASSERT(!options.common.replay_gain || options.channels <= 2);
1183 FLAC__ASSERT(!options.common.replay_gain || grabbag__replaygain_is_valid_sample_frequency(options.sample_rate));
1186 EncoderSession_construct(
1189 options.common.use_ogg,
1193 options.common.verify,
1194 options.common.treat_warnings_as_errors,
1195 options.common.continue_through_decode_errors,
1204 * now that we know the sample rate, canonicalize the
1205 * --skip string to a number of samples:
1207 flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, options.sample_rate);
1208 FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
1209 encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
1210 FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
1213 total_samples_in_input = 0;
1215 /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
1216 FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
1217 total_samples_in_input = (FLAC__uint64)infilesize / bytes_per_wide_sample + *options.common.align_reservoir_samples;
1221 * now that we know the input size, canonicalize the
1222 * --until strings to a number of samples:
1224 if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, options.sample_rate, encoder_session.skip, total_samples_in_input))
1225 return EncoderSession_finish_error(&encoder_session);
1226 encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
1227 FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
1229 infilesize -= (off_t)encoder_session.skip * bytes_per_wide_sample;
1230 encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
1231 if(encoder_session.until > 0) {
1232 const FLAC__uint64 trim = total_samples_in_input - encoder_session.until;
1233 FLAC__ASSERT(total_samples_in_input > 0);
1234 FLAC__ASSERT(!options.common.sector_align);
1235 infilesize -= (off_t)trim * bytes_per_wide_sample;
1236 encoder_session.total_samples_to_encode -= trim;
1238 if(infilesize >= 0 && options.common.sector_align) {
1239 FLAC__ASSERT(encoder_session.skip == 0);
1240 align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
1241 if(options.common.is_last_file)
1242 encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
1244 encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
1246 encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample;
1248 if(encoder_session.total_samples_to_encode <= 0)
1249 flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
1251 if(encoder_session.skip > 0) {
1252 unsigned skip_bytes = bytes_per_wide_sample * (unsigned)encoder_session.skip;
1253 if(skip_bytes > lookahead_length) {
1254 skip_bytes -= lookahead_length;
1255 lookahead_length = 0;
1256 if(!fskip_ahead(infile, skip_bytes)) {
1257 flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
1258 return EncoderSession_finish_error(&encoder_session);
1262 lookahead += skip_bytes;
1263 lookahead_length -= skip_bytes;
1267 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))
1268 return EncoderSession_finish_error(&encoder_session);
1271 * first do any samples in the reservoir
1273 if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
1274 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
1275 print_error_with_state(&encoder_session, "ERROR during encoding");
1276 return EncoderSession_finish_error(&encoder_session);
1281 * decrement infilesize if we need to align the file
1283 if(options.common.sector_align) {
1284 FLAC__ASSERT(infilesize >= 0);
1285 if(options.common.is_last_file) {
1286 *options.common.align_reservoir_samples = 0;
1289 *options.common.align_reservoir_samples = align_remainder;
1290 infilesize -= (off_t)((*options.common.align_reservoir_samples) * bytes_per_wide_sample);
1291 FLAC__ASSERT(infilesize >= 0);
1296 * now do from the file
1298 if(infilesize < 0) {
1299 while(!feof(infile)) {
1300 if(lookahead_length > 0) {
1301 FLAC__ASSERT(lookahead_length < CHUNK_OF_SAMPLES * bytes_per_wide_sample);
1302 memcpy(ucbuffer_, lookahead, lookahead_length);
1303 bytes_read = fread(ucbuffer_+lookahead_length, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample - lookahead_length, infile) + lookahead_length;
1304 if(ferror(infile)) {
1305 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1306 return EncoderSession_finish_error(&encoder_session);
1308 lookahead_length = 0;
1311 bytes_read = fread(ucbuffer_, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, infile);
1313 if(bytes_read == 0) {
1314 if(ferror(infile)) {
1315 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1316 return EncoderSession_finish_error(&encoder_session);
1319 else if(bytes_read % bytes_per_wide_sample != 0) {
1320 flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
1321 return EncoderSession_finish_error(&encoder_session);
1324 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
1325 if(!format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps, /*shift=*/0, /*channel_map=*/0))
1326 return EncoderSession_finish_error(&encoder_session);
1328 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1329 print_error_with_state(&encoder_session, "ERROR during encoding");
1330 return EncoderSession_finish_error(&encoder_session);
1336 const FLAC__uint64 max_input_bytes = infilesize;
1337 FLAC__uint64 total_input_bytes_read = 0;
1338 while(total_input_bytes_read < max_input_bytes) {
1340 size_t wanted = (CHUNK_OF_SAMPLES * bytes_per_wide_sample);
1341 wanted = (size_t) min((FLAC__uint64)wanted, max_input_bytes - total_input_bytes_read);
1343 if(lookahead_length > 0) {
1344 FLAC__ASSERT(lookahead_length <= wanted);
1345 memcpy(ucbuffer_, lookahead, lookahead_length);
1346 wanted -= lookahead_length;
1347 bytes_read = lookahead_length;
1349 bytes_read += fread(ucbuffer_+lookahead_length, sizeof(unsigned char), wanted, infile);
1350 if(ferror(infile)) {
1351 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1352 return EncoderSession_finish_error(&encoder_session);
1355 lookahead_length = 0;
1358 bytes_read = fread(ucbuffer_, sizeof(unsigned char), wanted, infile);
1361 if(bytes_read == 0) {
1362 if(ferror(infile)) {
1363 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1364 return EncoderSession_finish_error(&encoder_session);
1366 else if(feof(infile)) {
1367 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);
1368 if(encoder_session.treat_warnings_as_errors)
1369 return EncoderSession_finish_error(&encoder_session);
1370 total_input_bytes_read = max_input_bytes;
1374 if(bytes_read % bytes_per_wide_sample != 0) {
1375 flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
1376 return EncoderSession_finish_error(&encoder_session);
1379 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
1380 if(!format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps, /*shift=*/0, /*channel_map=*/0))
1381 return EncoderSession_finish_error(&encoder_session);
1383 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1384 print_error_with_state(&encoder_session, "ERROR during encoding");
1385 return EncoderSession_finish_error(&encoder_session);
1387 total_input_bytes_read += bytes_read;
1394 * now read unaligned samples into reservoir or pad with zeroes if necessary
1396 if(options.common.sector_align) {
1397 if(options.common.is_last_file) {
1398 unsigned wide_samples = 588 - align_remainder;
1399 if(wide_samples < 588) {
1402 info_align_zero = wide_samples;
1403 for(channel = 0; channel < options.channels; channel++)
1404 memset(input_[channel], 0, sizeof(input_[0][0]) * wide_samples);
1406 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
1407 print_error_with_state(&encoder_session, "ERROR during encoding");
1408 return EncoderSession_finish_error(&encoder_session);
1413 if(*options.common.align_reservoir_samples > 0) {
1414 FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588);
1415 bytes_read = fread(ucbuffer_, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile);
1416 if(bytes_read == 0 && ferror(infile)) {
1417 flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
1418 return EncoderSession_finish_error(&encoder_session);
1420 else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
1421 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);
1422 if(encoder_session.treat_warnings_as_errors)
1423 return EncoderSession_finish_error(&encoder_session);
1426 info_align_carry = *options.common.align_reservoir_samples;
1427 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))
1428 return EncoderSession_finish_error(&encoder_session);
1434 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, /*foreign_metadata=*/0);
1437 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)
1439 EncoderSession encoder_session;
1440 FLAC__StreamDecoder *decoder = 0;
1441 FLACDecoderData decoder_data;
1446 EncoderSession_construct(
1449 options.common.use_ogg,
1453 options.common.verify,
1454 options.common.treat_warnings_as_errors,
1455 options.common.continue_through_decode_errors,
1463 decoder_data.encoder_session = &encoder_session;
1464 decoder_data.filesize = (infilesize == (off_t)(-1)? 0 : infilesize);
1465 decoder_data.lookahead = lookahead;
1466 decoder_data.lookahead_length = lookahead_length;
1467 decoder_data.num_metadata_blocks = 0;
1468 decoder_data.samples_left_to_process = 0;
1469 decoder_data.fatal_error = false;
1472 * set up FLAC decoder for the input
1474 if (0 == (decoder = FLAC__stream_decoder_new())) {
1475 flac__utils_printf(stderr, 1, "%s: ERROR: creating decoder for FLAC input\n", encoder_session.inbasefilename);
1476 return EncoderSession_finish_error(&encoder_session);
1479 FLAC__stream_decoder_set_md5_checking(decoder, false) &&
1480 FLAC__stream_decoder_set_metadata_respond_all(decoder)
1482 flac__utils_printf(stderr, 1, "%s: ERROR: setting up decoder for FLAC input\n", encoder_session.inbasefilename);
1483 goto fubar1; /*@@@ yuck */
1487 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) {
1488 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));
1489 goto fubar1; /*@@@ yuck */
1492 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) {
1493 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));
1494 goto fubar1; /*@@@ yuck */
1497 if (!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || decoder_data.fatal_error) {
1498 if (decoder_data.fatal_error)
1499 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);
1501 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));
1502 goto fubar1; /*@@@ yuck */
1505 if (decoder_data.num_metadata_blocks == 0) {
1506 flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, got no metadata blocks\n", encoder_session.inbasefilename);
1507 goto fubar2; /*@@@ yuck */
1509 else if (decoder_data.metadata_blocks[0]->type != FLAC__METADATA_TYPE_STREAMINFO) {
1510 flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, first metadata block is not STREAMINFO\n", encoder_session.inbasefilename);
1511 goto fubar2; /*@@@ yuck */
1513 else if (decoder_data.metadata_blocks[0]->data.stream_info.total_samples == 0) {
1514 flac__utils_printf(stderr, 1, "%s: ERROR: FLAC input has STREAMINFO with unknown total samples which is not supported\n", encoder_session.inbasefilename);
1515 goto fubar2; /*@@@ yuck */
1519 * now that we have the STREAMINFO and know the sample rate,
1520 * canonicalize the --skip string to a number of samples:
1522 flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, decoder_data.metadata_blocks[0]->data.stream_info.sample_rate);
1523 FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
1524 encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
1525 FLAC__ASSERT(!options.common.sector_align); /* --sector-align with FLAC input is not supported */
1528 FLAC__uint64 total_samples_in_input, trim = 0;
1530 total_samples_in_input = decoder_data.metadata_blocks[0]->data.stream_info.total_samples;
1533 * now that we know the input size, canonicalize the
1534 * --until string to an absolute sample number:
1536 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))
1537 goto fubar2; /*@@@ yuck */
1538 encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
1540 encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
1541 if(encoder_session.until > 0) {
1542 trim = total_samples_in_input - encoder_session.until;
1543 FLAC__ASSERT(total_samples_in_input > 0);
1544 encoder_session.total_samples_to_encode -= trim;
1547 encoder_session.unencoded_size = decoder_data.filesize;
1549 /* (channel mask will get copied over from the source VORBIS_COMMENT if it exists) */
1550 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))
1551 goto fubar2; /*@@@ yuck */
1554 * have to wait until the FLAC encoder is set up for writing
1555 * before any seeking in the input FLAC file, because the seek
1556 * itself will usually call the decoder's write callback, and
1557 * our decoder's write callback passes samples to our FLAC
1560 decoder_data.samples_left_to_process = encoder_session.total_samples_to_encode;
1561 if(encoder_session.skip > 0) {
1562 if(!FLAC__stream_decoder_seek_absolute(decoder, encoder_session.skip)) {
1563 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));
1564 goto fubar2; /*@@@ yuck */
1569 * now do samples from the file
1571 while(!decoder_data.fatal_error && decoder_data.samples_left_to_process > 0) {
1572 /* We can also hit the end of stream without samples_left_to_process
1573 * going to 0 if there are errors and continue_through_decode_errors
1574 * is on, so we want to break in that case too:
1576 if(encoder_session.continue_through_decode_errors && FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
1578 if(!FLAC__stream_decoder_process_single(decoder)) {
1579 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));
1580 goto fubar2; /*@@@ yuck */
1583 if(decoder_data.fatal_error) {
1584 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));
1585 goto fubar2; /*@@@ yuck */
1589 FLAC__stream_decoder_delete(decoder);
1590 retval = EncoderSession_finish_ok(&encoder_session, -1, -1, /*foreign_metadata=*/0);
1591 /* have to wail until encoder is completely finished before deleting because of the final step of writing the seekpoint offsets */
1592 for(i = 0; i < decoder_data.num_metadata_blocks; i++)
1593 FLAC__metadata_object_delete(decoder_data.metadata_blocks[i]);
1597 for(i = 0; i < decoder_data.num_metadata_blocks; i++)
1598 FLAC__metadata_object_delete(decoder_data.metadata_blocks[i]);
1600 FLAC__stream_decoder_delete(decoder);
1601 return EncoderSession_finish_error(&encoder_session);
1604 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)
1607 FLAC__uint32 test = 1;
1610 * initialize globals
1613 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
1615 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
1616 input_[i] = &(in_[i][0]);
1620 * initialize instance
1624 e->use_ogg = use_ogg;
1629 e->treat_warnings_as_errors = treat_warnings_as_errors;
1630 e->continue_through_decode_errors = continue_through_decode_errors;
1632 e->is_stdout = (0 == strcmp(outfilename, "-"));
1633 e->outputfile_opened = false;
1635 e->inbasefilename = grabbag__file_get_basename(infilename);
1636 e->infilename = infilename;
1637 e->outfilename = outfilename;
1639 e->skip = 0; /* filled in later after the sample_rate is known */
1641 e->bits_per_sample = 0;
1643 e->unencoded_size = 0;
1644 e->total_samples_to_encode = 0;
1645 e->bytes_written = 0;
1646 e->samples_written = 0;
1652 e->seek_table_template = 0;
1654 if(0 == (e->seek_table_template = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE))) {
1655 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for seek table\n", e->inbasefilename);
1659 e->encoder = FLAC__stream_encoder_new();
1660 if(0 == e->encoder) {
1661 flac__utils_printf(stderr, 1, "%s: ERROR creating the encoder instance\n", e->inbasefilename);
1662 EncoderSession_destroy(e);
1669 void EncoderSession_destroy(EncoderSession *e)
1674 if(0 != e->encoder) {
1675 FLAC__stream_encoder_delete(e->encoder);
1679 if(0 != e->seek_table_template) {
1680 FLAC__metadata_object_delete(e->seek_table_template);
1681 e->seek_table_template = 0;
1685 int EncoderSession_finish_ok(EncoderSession *e, int info_align_carry, int info_align_zero, foreign_metadata_t *foreign_metadata)
1687 FLAC__StreamEncoderState fse_state = FLAC__STREAM_ENCODER_OK;
1689 FLAC__bool verify_error = false;
1692 fse_state = FLAC__stream_encoder_get_state(e->encoder);
1693 ret = FLAC__stream_encoder_finish(e->encoder)? 0 : 1;
1695 fse_state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA ||
1696 FLAC__stream_encoder_get_state(e->encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA
1699 /* all errors except verify errors should interrupt the stats */
1700 if(ret && !verify_error)
1701 print_error_with_state(e, "ERROR during encoding");
1702 else if(e->total_samples_to_encode > 0) {
1704 flac__utils_printf(stderr, 2, "\n");
1708 print_verify_error(e);
1712 if(info_align_carry >= 0) {
1713 flac__utils_printf(stderr, 1, "%s: INFO: sector alignment causing %d samples to be carried over\n", e->inbasefilename, info_align_carry);
1715 if(info_align_zero >= 0) {
1716 flac__utils_printf(stderr, 1, "%s: INFO: sector alignment causing %d zero samples to be appended\n", e->inbasefilename, info_align_zero);
1720 /*@@@@@@ should this go here or somewhere else? */
1721 if(ret == 0 && foreign_metadata) {
1723 if(!flac__foreign_metadata_write_to_flac(foreign_metadata, e->infilename, e->outfilename, &error)) {
1724 flac__utils_printf(stderr, 1, "%s: ERROR: updating foreign metadata in FLAC file: %s\n", e->inbasefilename, error);
1729 EncoderSession_destroy(e);
1734 int EncoderSession_finish_error(EncoderSession *e)
1736 FLAC__ASSERT(e->encoder);
1738 if(e->total_samples_to_encode > 0)
1739 flac__utils_printf(stderr, 2, "\n");
1741 if(FLAC__stream_encoder_get_state(e->encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1742 print_verify_error(e);
1743 else if(e->outputfile_opened)
1744 /* only want to delete the file if we opened it; otherwise it could be an existing file and our overwrite failed */
1745 unlink(e->outfilename);
1747 EncoderSession_destroy(e);
1753 unsigned num_metadata;
1754 FLAC__bool *needs_delete;
1755 FLAC__StreamMetadata **metadata;
1756 FLAC__StreamMetadata *cuesheet; /* always needs to be deleted */
1757 } static_metadata_t;
1759 static void static_metadata_init(static_metadata_t *m)
1761 m->num_metadata = 0;
1762 m->needs_delete = 0;
1767 static void static_metadata_clear(static_metadata_t *m)
1770 for(i = 0; i < m->num_metadata; i++)
1771 if(m->needs_delete[i])
1772 FLAC__metadata_object_delete(m->metadata[i]);
1776 free(m->needs_delete);
1778 FLAC__metadata_object_delete(m->cuesheet);
1779 static_metadata_init(m);
1782 static FLAC__bool static_metadata_append(static_metadata_t *m, FLAC__StreamMetadata *d, FLAC__bool needs_delete)
1785 if(0 == (x = safe_realloc_muladd2_(m->metadata, sizeof(*m->metadata), /*times (*/m->num_metadata, /*+*/1/*)*/)))
1787 m->metadata = (FLAC__StreamMetadata**)x;
1788 if(0 == (x = safe_realloc_muladd2_(m->needs_delete, sizeof(*m->needs_delete), /*times (*/m->num_metadata, /*+*/1/*)*/)))
1790 m->needs_delete = (FLAC__bool*)x;
1791 m->metadata[m->num_metadata] = d;
1792 m->needs_delete[m->num_metadata] = needs_delete;
1797 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)
1799 FLAC__StreamMetadata padding;
1800 FLAC__StreamMetadata **metadata = 0;
1801 static_metadata_t static_metadata;
1802 unsigned num_metadata = 0, i;
1803 FLAC__StreamEncoderInitStatus init_status;
1804 const FLAC__bool is_cdda = (channels == 1 || channels == 2) && (bps == 16) && (sample_rate == 44100);
1805 char apodizations[2000];
1807 FLAC__ASSERT(sizeof(options.pictures)/sizeof(options.pictures[0]) <= 64);
1809 static_metadata_init(&static_metadata);
1811 e->replay_gain = options.replay_gain;
1812 e->channels = channels;
1813 e->bits_per_sample = bps;
1814 e->sample_rate = sample_rate;
1816 apodizations[0] = '\0';
1818 if(e->replay_gain) {
1819 if(channels != 1 && channels != 2) {
1820 flac__utils_printf(stderr, 1, "%s: ERROR, number of channels (%u) must be 1 or 2 for --replay-gain\n", e->inbasefilename, channels);
1823 if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
1824 flac__utils_printf(stderr, 1, "%s: ERROR, invalid sample rate (%u) for --replay-gain\n", e->inbasefilename, sample_rate);
1827 if(options.is_first_file) {
1828 if(!grabbag__replaygain_init(sample_rate)) {
1829 flac__utils_printf(stderr, 1, "%s: ERROR initializing ReplayGain stage\n", e->inbasefilename);
1835 if(!parse_cuesheet(&static_metadata.cuesheet, options.cuesheet_filename, e->inbasefilename, is_cdda, e->total_samples_to_encode, e->treat_warnings_as_errors))
1838 if(!convert_to_seek_table_template(options.requested_seek_points, options.num_requested_seek_points, options.cued_seekpoints? static_metadata.cuesheet : 0, e)) {
1839 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for seek table\n", e->inbasefilename);
1840 static_metadata_clear(&static_metadata);
1844 /* build metadata */
1845 if(flac_decoder_data) {
1847 * we're encoding from FLAC so we will use the FLAC file's
1848 * metadata as the basis for the encoded file
1852 * first handle pictures: simple append any --pictures
1855 for(i = 0; i < options.num_pictures; i++) {
1856 FLAC__StreamMetadata *pic = FLAC__metadata_object_clone(options.pictures[i]);
1858 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for PICTURE block\n", e->inbasefilename);
1859 static_metadata_clear(&static_metadata);
1862 flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks++] = pic;
1867 * next handle vorbis comment: if any tags were specified
1868 * or there is no existing vorbis comment, we create a
1869 * new vorbis comment (discarding any existing one); else
1870 * we keep the existing one. also need to make sure to
1871 * propagate any channel mask tag.
1873 /* @@@ change to append -T values from options.vorbis_comment if input has VC already? */
1875 FLAC__bool vc_found = false;
1876 for(i = 0, j = 0; i < flac_decoder_data->num_metadata_blocks; i++) {
1877 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
1879 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT && options.vorbis_comment->data.vorbis_comment.num_comments > 0) {
1880 (void) flac__utils_get_channel_mask_tag(flac_decoder_data->metadata_blocks[i], &channel_mask);
1881 flac__utils_printf(stderr, 1, "%s: WARNING, replacing tags from input FLAC file with those given on the command-line\n", e->inbasefilename);
1882 if(e->treat_warnings_as_errors) {
1883 static_metadata_clear(&static_metadata);
1886 FLAC__metadata_object_delete(flac_decoder_data->metadata_blocks[i]);
1887 flac_decoder_data->metadata_blocks[i] = 0;
1890 flac_decoder_data->metadata_blocks[j++] = flac_decoder_data->metadata_blocks[i];
1892 flac_decoder_data->num_metadata_blocks = j;
1893 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])) {
1895 FLAC__StreamMetadata *vc = FLAC__metadata_object_clone(options.vorbis_comment);
1896 if(0 == vc || (channel_mask && !flac__utils_set_channel_mask_tag(vc, channel_mask))) {
1897 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for VORBIS_COMMENT block\n", e->inbasefilename);
1898 static_metadata_clear(&static_metadata);
1901 for(i = flac_decoder_data->num_metadata_blocks; i > 1; i--)
1902 flac_decoder_data->metadata_blocks[i] = flac_decoder_data->metadata_blocks[i-1];
1903 flac_decoder_data->metadata_blocks[1] = vc;
1904 flac_decoder_data->num_metadata_blocks++;
1909 * next handle cuesheet: if --cuesheet was specified, use
1910 * it; else if file has existing CUESHEET and cuesheet's
1911 * lead-out offset is correct, keep it; else no CUESHEET
1914 for(i = 0, j = 0; i < flac_decoder_data->num_metadata_blocks; i++) {
1915 FLAC__bool existing_cuesheet_is_bad = false;
1916 /* check if existing cuesheet matches the input audio */
1917 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_CUESHEET && 0 == static_metadata.cuesheet) {
1918 const FLAC__StreamMetadata_CueSheet *cs = &flac_decoder_data->metadata_blocks[i]->data.cue_sheet;
1919 if(e->total_samples_to_encode == 0) {
1920 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);
1921 if(e->treat_warnings_as_errors) {
1922 static_metadata_clear(&static_metadata);
1925 existing_cuesheet_is_bad = true;
1927 else if(e->total_samples_to_encode != cs->tracks[cs->num_tracks-1].offset) {
1928 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);
1929 if(e->treat_warnings_as_errors) {
1930 static_metadata_clear(&static_metadata);
1933 existing_cuesheet_is_bad = true;
1936 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_CUESHEET && (existing_cuesheet_is_bad || 0 != static_metadata.cuesheet)) {
1937 if(0 != static_metadata.cuesheet) {
1938 flac__utils_printf(stderr, 1, "%s: WARNING, replacing cuesheet in input FLAC file with the one given on the command-line\n", e->inbasefilename);
1939 if(e->treat_warnings_as_errors) {
1940 static_metadata_clear(&static_metadata);
1944 FLAC__metadata_object_delete(flac_decoder_data->metadata_blocks[i]);
1945 flac_decoder_data->metadata_blocks[i] = 0;
1948 flac_decoder_data->metadata_blocks[j++] = flac_decoder_data->metadata_blocks[i];
1950 flac_decoder_data->num_metadata_blocks = j;
1951 if(0 != static_metadata.cuesheet && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
1953 FLAC__StreamMetadata *cs = FLAC__metadata_object_clone(static_metadata.cuesheet);
1955 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for CUESHEET block\n", e->inbasefilename);
1956 static_metadata_clear(&static_metadata);
1959 for(i = flac_decoder_data->num_metadata_blocks; i > 1; i--)
1960 flac_decoder_data->metadata_blocks[i] = flac_decoder_data->metadata_blocks[i-1];
1961 flac_decoder_data->metadata_blocks[1] = cs;
1962 flac_decoder_data->num_metadata_blocks++;
1967 * next handle seektable: if -S- was specified, no
1968 * SEEKTABLE; else if -S was specified, use it/them;
1969 * else if file has existing SEEKTABLE and input size is
1970 * preserved (no --skip/--until/etc specified), keep it;
1971 * else use default seektable options
1973 * note: meanings of num_requested_seek_points:
1974 * -1 : no -S option given, default to some value
1975 * 0 : -S- given (no seektable)
1976 * >0 : one or more -S options given
1979 FLAC__bool existing_seektable = false;
1980 for(i = 0, j = 0; i < flac_decoder_data->num_metadata_blocks; i++) {
1981 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_SEEKTABLE)
1982 existing_seektable = true;
1983 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)) {
1984 if(options.num_requested_seek_points > 0) {
1985 flac__utils_printf(stderr, 1, "%s: WARNING, replacing seektable in input FLAC file with the one given on the command-line\n", e->inbasefilename);
1986 if(e->treat_warnings_as_errors) {
1987 static_metadata_clear(&static_metadata);
1991 else if(options.num_requested_seek_points == 0)
1992 ; /* no warning, silently delete existing SEEKTABLE since user specified --no-seektable (-S-) */
1994 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);
1995 if(e->treat_warnings_as_errors) {
1996 static_metadata_clear(&static_metadata);
2000 FLAC__metadata_object_delete(flac_decoder_data->metadata_blocks[i]);
2001 flac_decoder_data->metadata_blocks[i] = 0;
2002 existing_seektable = false;
2005 flac_decoder_data->metadata_blocks[j++] = flac_decoder_data->metadata_blocks[i];
2007 flac_decoder_data->num_metadata_blocks = j;
2008 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])) {
2010 FLAC__StreamMetadata *st = FLAC__metadata_object_clone(e->seek_table_template);
2012 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for SEEKTABLE block\n", e->inbasefilename);
2013 static_metadata_clear(&static_metadata);
2016 for(i = flac_decoder_data->num_metadata_blocks; i > 1; i--)
2017 flac_decoder_data->metadata_blocks[i] = flac_decoder_data->metadata_blocks[i-1];
2018 flac_decoder_data->metadata_blocks[1] = st;
2019 flac_decoder_data->num_metadata_blocks++;
2024 * finally handle padding: if --no-padding was specified,
2025 * then delete all padding; else if -P was specified,
2026 * use that instead of existing padding (if any); else
2027 * if existing file has padding, move all existing
2028 * padding blocks to one padding block at the end; else
2029 * use default padding.
2033 for(i = 0, j = 0; i < flac_decoder_data->num_metadata_blocks; i++) {
2034 if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_PADDING) {
2037 p += flac_decoder_data->metadata_blocks[i]->length;
2038 FLAC__metadata_object_delete(flac_decoder_data->metadata_blocks[i]);
2039 flac_decoder_data->metadata_blocks[i] = 0;
2042 flac_decoder_data->metadata_blocks[j++] = flac_decoder_data->metadata_blocks[i];
2044 flac_decoder_data->num_metadata_blocks = j;
2045 if(options.padding > 0)
2046 p = options.padding;
2048 p = e->total_samples_to_encode / e->sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8;
2049 if(options.padding != 0) {
2050 if(p > 0 && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
2051 flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
2052 if(0 == flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks]) {
2053 flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for PADDING block\n", e->inbasefilename);
2054 static_metadata_clear(&static_metadata);
2057 flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks]->is_last = false; /* the encoder will set this for us */
2058 flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks]->length = p;
2059 flac_decoder_data->num_metadata_blocks++;
2063 metadata = &flac_decoder_data->metadata_blocks[1]; /* don't include STREAMINFO */
2064 num_metadata = flac_decoder_data->num_metadata_blocks - 1;
2068 * we're not encoding from FLAC so we will build the metadata
2071 if(e->seek_table_template->data.seek_table.num_points > 0) {
2072 e->seek_table_template->is_last = false; /* the encoder will set this for us */
2073 static_metadata_append(&static_metadata, e->seek_table_template, /*needs_delete=*/false);
2075 if(0 != static_metadata.cuesheet)
2076 static_metadata_append(&static_metadata, static_metadata.cuesheet, /*needs_delete=*/false);
2078 if(!flac__utils_set_channel_mask_tag(options.vorbis_comment, channel_mask)) {
2079 flac__utils_printf(stderr, 1, "%s: ERROR adding channel mask tag\n", e->inbasefilename);
2080 static_metadata_clear(&static_metadata);
2084 static_metadata_append(&static_metadata, options.vorbis_comment, /*needs_delete=*/false);
2085 for(i = 0; i < options.num_pictures; i++)
2086 static_metadata_append(&static_metadata, options.pictures[i], /*needs_delete=*/false);
2087 if(foreign_metadata) {
2088 for(i = 0; i < foreign_metadata->num_blocks; i++) {
2089 FLAC__StreamMetadata *p = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
2091 flac__utils_printf(stderr, 1, "%s: ERROR: out of memory\n", e->inbasefilename);
2092 static_metadata_clear(&static_metadata);
2095 static_metadata_append(&static_metadata, p, /*needs_delete=*/true);
2096 static_metadata.metadata[static_metadata.num_metadata-1]->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8 + foreign_metadata->blocks[i].size;
2097 /*fprintf(stderr,"@@@@@@ add PADDING=%u\n",static_metadata.metadata[static_metadata.num_metadata-1]->length);*/
2100 if(options.padding != 0) {
2101 padding.is_last = false; /* the encoder will set this for us */
2102 padding.type = FLAC__METADATA_TYPE_PADDING;
2103 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));
2104 static_metadata_append(&static_metadata, &padding, /*needs_delete=*/false);
2106 metadata = static_metadata.metadata;
2107 num_metadata = static_metadata.num_metadata;
2110 /* check for a few things that have not already been checked. the
2111 * FLAC__stream_encoder_init*() will check it but only return
2112 * FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA so we check some
2113 * up front to give a better error message.
2115 if(!verify_metadata(e, metadata, num_metadata)) {
2116 static_metadata_clear(&static_metadata);
2120 FLAC__stream_encoder_set_verify(e->encoder, options.verify);
2121 FLAC__stream_encoder_set_streamable_subset(e->encoder, !options.lax);
2122 FLAC__stream_encoder_set_channels(e->encoder, channels);
2123 FLAC__stream_encoder_set_bits_per_sample(e->encoder, bps);
2124 FLAC__stream_encoder_set_sample_rate(e->encoder, sample_rate);
2125 for(i = 0; i < options.num_compression_settings; i++) {
2126 switch(options.compression_settings[i].type) {
2128 FLAC__stream_encoder_set_blocksize(e->encoder, options.compression_settings[i].value.t_unsigned);
2130 case CST_COMPRESSION_LEVEL:
2131 FLAC__stream_encoder_set_compression_level(e->encoder, options.compression_settings[i].value.t_unsigned);
2132 apodizations[0] = '\0';
2134 case CST_DO_MID_SIDE:
2135 FLAC__stream_encoder_set_do_mid_side_stereo(e->encoder, options.compression_settings[i].value.t_bool);
2137 case CST_LOOSE_MID_SIDE:
2138 FLAC__stream_encoder_set_loose_mid_side_stereo(e->encoder, options.compression_settings[i].value.t_bool);
2140 case CST_APODIZATION:
2141 if(strlen(apodizations)+strlen(options.compression_settings[i].value.t_string)+2 >= sizeof(apodizations)) {
2142 flac__utils_printf(stderr, 1, "%s: ERROR: too many apodization functions requested\n", e->inbasefilename);
2143 static_metadata_clear(&static_metadata);
2147 strcat(apodizations, options.compression_settings[i].value.t_string);
2148 strcat(apodizations, ";");
2151 case CST_MAX_LPC_ORDER:
2152 FLAC__stream_encoder_set_max_lpc_order(e->encoder, options.compression_settings[i].value.t_unsigned);
2154 case CST_QLP_COEFF_PRECISION:
2155 FLAC__stream_encoder_set_qlp_coeff_precision(e->encoder, options.compression_settings[i].value.t_unsigned);
2157 case CST_DO_QLP_COEFF_PREC_SEARCH:
2158 FLAC__stream_encoder_set_do_qlp_coeff_prec_search(e->encoder, options.compression_settings[i].value.t_bool);
2160 case CST_DO_ESCAPE_CODING:
2161 FLAC__stream_encoder_set_do_escape_coding(e->encoder, options.compression_settings[i].value.t_bool);
2163 case CST_DO_EXHAUSTIVE_MODEL_SEARCH:
2164 FLAC__stream_encoder_set_do_exhaustive_model_search(e->encoder, options.compression_settings[i].value.t_bool);
2166 case CST_MIN_RESIDUAL_PARTITION_ORDER:
2167 FLAC__stream_encoder_set_min_residual_partition_order(e->encoder, options.compression_settings[i].value.t_unsigned);
2169 case CST_MAX_RESIDUAL_PARTITION_ORDER:
2170 FLAC__stream_encoder_set_max_residual_partition_order(e->encoder, options.compression_settings[i].value.t_unsigned);
2172 case CST_RICE_PARAMETER_SEARCH_DIST:
2173 FLAC__stream_encoder_set_rice_parameter_search_dist(e->encoder, options.compression_settings[i].value.t_unsigned);
2178 FLAC__stream_encoder_set_apodization(e->encoder, apodizations);
2179 FLAC__stream_encoder_set_total_samples_estimate(e->encoder, e->total_samples_to_encode);
2180 FLAC__stream_encoder_set_metadata(e->encoder, (num_metadata > 0)? metadata : 0, num_metadata);
2182 FLAC__stream_encoder_disable_constant_subframes(e->encoder, options.debug.disable_constant_subframes);
2183 FLAC__stream_encoder_disable_fixed_subframes(e->encoder, options.debug.disable_fixed_subframes);
2184 FLAC__stream_encoder_disable_verbatim_subframes(e->encoder, options.debug.disable_verbatim_subframes);
2185 if(!options.debug.do_md5) {
2186 flac__utils_printf(stderr, 1, "%s: WARNING, MD5 computation disabled, resulting file will not have MD5 sum\n", e->inbasefilename);
2187 if(e->treat_warnings_as_errors) {
2188 static_metadata_clear(&static_metadata);
2191 FLAC__stream_encoder_set_do_md5(e->encoder, false);
2196 FLAC__stream_encoder_set_ogg_serial_number(e->encoder, options.serial_number);
2198 init_status = FLAC__stream_encoder_init_ogg_file(e->encoder, e->is_stdout? 0 : e->outfilename, encoder_progress_callback, /*client_data=*/e);
2203 init_status = FLAC__stream_encoder_init_file(e->encoder, e->is_stdout? 0 : e->outfilename, encoder_progress_callback, /*client_data=*/e);
2206 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
2207 print_error_with_init_status(e, "ERROR initializing encoder", init_status);
2208 if(FLAC__stream_encoder_get_state(e->encoder) != FLAC__STREAM_ENCODER_IO_ERROR)
2209 e->outputfile_opened = true;
2210 static_metadata_clear(&static_metadata);
2214 e->outputfile_opened = true;
2217 (FLAC__stream_encoder_get_do_exhaustive_model_search(e->encoder) && FLAC__stream_encoder_get_do_qlp_coeff_prec_search(e->encoder))? 0x07 :
2218 (FLAC__stream_encoder_get_do_exhaustive_model_search(e->encoder) || FLAC__stream_encoder_get_do_qlp_coeff_prec_search(e->encoder))? 0x0f :
2221 static_metadata_clear(&static_metadata);
2226 FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples)
2228 if(e->replay_gain) {
2229 if(!grabbag__replaygain_analyze(buffer, e->channels==2, e->bits_per_sample, samples)) {
2230 flac__utils_printf(stderr, 1, "%s: WARNING, error while calculating ReplayGain\n", e->inbasefilename);
2231 if(e->treat_warnings_as_errors)
2236 return FLAC__stream_encoder_process(e->encoder, buffer, samples);
2239 FLAC__bool convert_to_seek_table_template(const char *requested_seek_points, int num_requested_seek_points, FLAC__StreamMetadata *cuesheet, EncoderSession *e)
2241 const FLAC__bool only_placeholders = e->is_stdout;
2242 FLAC__bool has_real_points;
2244 if(num_requested_seek_points == 0 && 0 == cuesheet)
2247 if(num_requested_seek_points < 0) {
2249 /*@@@@@@ workaround ogg bug: too many seekpoints makes table not fit in one page */
2250 if(e->use_ogg && e->total_samples_to_encode > 0 && e->total_samples_to_encode / e->sample_rate / 10 > 230)
2251 requested_seek_points = "230x;";
2254 requested_seek_points = "10s;";
2255 num_requested_seek_points = 1;
2258 if(num_requested_seek_points > 0) {
2259 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))
2265 const FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
2266 for(i = 0; i < cs->num_tracks; i++) {
2267 const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+i;
2268 for(j = 0; j < tr->num_indices; j++) {
2269 if(!FLAC__metadata_object_seektable_template_append_point(e->seek_table_template, tr->offset + tr->indices[j].offset))
2271 has_real_points = true;
2275 if(!FLAC__metadata_object_seektable_template_sort(e->seek_table_template, /*compact=*/true))
2279 if(has_real_points) {
2281 flac__utils_printf(stderr, 1, "%s: WARNING, cannot write back seekpoints when encoding to stdout\n", e->inbasefilename);
2282 if(e->treat_warnings_as_errors)
2290 FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input)
2292 /* convert from mm:ss.sss to sample number if necessary */
2293 flac__utils_canonicalize_skip_until_specification(spec, sample_rate);
2295 /* special case: if "--until=-0", use the special value '0' to mean "end-of-stream" */
2296 if(spec->is_relative && spec->value.samples == 0) {
2297 spec->is_relative = false;
2301 /* in any other case the total samples in the input must be known */
2302 if(total_samples_in_input == 0) {
2303 flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when input length is unknown\n", inbasefilename);
2307 FLAC__ASSERT(spec->value_is_samples);
2309 /* convert relative specifications to absolute */
2310 if(spec->is_relative) {
2311 if(spec->value.samples <= 0)
2312 spec->value.samples += (FLAC__int64)total_samples_in_input;
2314 spec->value.samples += skip;
2315 spec->is_relative = false;
2319 if(spec->value.samples < 0) {
2320 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before beginning of input\n", inbasefilename);
2323 if((FLAC__uint64)spec->value.samples <= skip) {
2324 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before --skip point\n", inbasefilename);
2327 if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
2328 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is after end of input\n", inbasefilename);
2335 FLAC__bool verify_metadata(const EncoderSession *e, FLAC__StreamMetadata **metadata, unsigned num_metadata)
2337 FLAC__bool metadata_picture_has_type1 = false;
2338 FLAC__bool metadata_picture_has_type2 = false;
2341 FLAC__ASSERT(0 != metadata);
2342 for(i = 0; i < num_metadata; i++) {
2343 const FLAC__StreamMetadata *m = metadata[i];
2344 if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
2345 if(!FLAC__format_seektable_is_legal(&m->data.seek_table)) {
2346 flac__utils_printf(stderr, 1, "%s: ERROR: SEEKTABLE metadata block is invalid\n", e->inbasefilename);
2350 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
2351 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0)) {
2352 flac__utils_printf(stderr, 1, "%s: ERROR: CUESHEET metadata block is invalid\n", e->inbasefilename);
2356 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
2357 const char *error = 0;
2358 if(!FLAC__format_picture_is_legal(&m->data.picture, &error)) {
2359 flac__utils_printf(stderr, 1, "%s: ERROR: PICTURE metadata block is invalid: %s\n", e->inbasefilename, error);
2362 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
2363 if(metadata_picture_has_type1) {
2364 flac__utils_printf(stderr, 1, "%s: ERROR: there may only be one picture of type 1 (32x32 icon) in the file\n", e->inbasefilename);
2367 metadata_picture_has_type1 = true;
2369 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
2370 if(metadata_picture_has_type2) {
2371 flac__utils_printf(stderr, 1, "%s: ERROR: there may only be one picture of type 2 (icon) in the file\n", e->inbasefilename);
2374 metadata_picture_has_type2 = true;
2382 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)
2384 unsigned wide_sample, sample, channel, byte;
2385 FLAC__int32 *out[FLAC__MAX_CHANNELS];
2387 if(0 == channel_map) {
2388 for(channel = 0; channel < channels; channel++)
2389 out[channel] = dest[channel];
2392 for(channel = 0; channel < channels; channel++)
2393 out[channel] = dest[channel_map[channel]];
2397 if(is_unsigned_samples) {
2398 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2399 for(channel = 0; channel < channels; channel++, sample++)
2400 out[channel][wide_sample] = (FLAC__int32)ucbuffer_[sample] - 0x80;
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)scbuffer_[sample];
2408 else if(bps == 16) {
2409 if(is_big_endian != is_big_endian_host_) {
2411 const unsigned bytes = wide_samples * channels * (bps >> 3);
2412 for(byte = 0; byte < bytes; byte += 2) {
2413 tmp = ucbuffer_[byte];
2414 ucbuffer_[byte] = ucbuffer_[byte+1];
2415 ucbuffer_[byte+1] = tmp;
2418 if(is_unsigned_samples) {
2419 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2420 for(channel = 0; channel < channels; channel++, sample++)
2421 out[channel][wide_sample] = (FLAC__int32)usbuffer_[sample] - 0x8000;
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)ssbuffer_[sample];
2429 else if(bps == 24) {
2430 if(!is_big_endian) {
2432 const unsigned bytes = wide_samples * channels * (bps >> 3);
2433 for(byte = 0; byte < bytes; byte += 3) {
2434 tmp = ucbuffer_[byte];
2435 ucbuffer_[byte] = ucbuffer_[byte+2];
2436 ucbuffer_[byte+2] = tmp;
2439 if(is_unsigned_samples) {
2440 for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2441 for(channel = 0; channel < channels; channel++, sample++) {
2442 out[channel][wide_sample] = ucbuffer_[byte++]; out[channel][wide_sample] <<= 8;
2443 out[channel][wide_sample] |= ucbuffer_[byte++]; out[channel][wide_sample] <<= 8;
2444 out[channel][wide_sample] |= ucbuffer_[byte++];
2445 out[channel][wide_sample] -= 0x800000;
2449 for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2450 for(channel = 0; channel < channels; channel++, sample++) {
2451 out[channel][wide_sample] = scbuffer_[byte++]; out[channel][wide_sample] <<= 8;
2452 out[channel][wide_sample] |= ucbuffer_[byte++]; out[channel][wide_sample] <<= 8;
2453 out[channel][wide_sample] |= ucbuffer_[byte++];
2461 FLAC__int32 mask = (1<<shift)-1;
2462 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++)
2463 for(channel = 0; channel < channels; channel++) {
2464 if(out[channel][wide_sample] & mask) {
2465 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);
2468 out[channel][wide_sample] >>= shift;
2474 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)
2476 EncoderSession *encoder_session = (EncoderSession*)client_data;
2478 (void)encoder, (void)total_frames_estimate;
2480 encoder_session->bytes_written = bytes_written;
2481 encoder_session->samples_written = samples_written;
2483 if(encoder_session->total_samples_to_encode > 0 && !((frames_written-1) & encoder_session->stats_mask))
2484 print_stats(encoder_session);
2487 FLAC__StreamDecoderReadStatus flac_decoder_read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
2490 FLACDecoderData *data = (FLACDecoderData*)client_data;
2493 if (data->fatal_error)
2494 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2496 /* use up lookahead first */
2497 if (data->lookahead_length) {
2498 n = min(data->lookahead_length, *bytes);
2499 memcpy(buffer, data->lookahead, n);
2501 data->lookahead += n;
2502 data->lookahead_length -= n;
2505 /* get the rest from file */
2507 *bytes = n + fread(buffer, 1, *bytes-n, data->encoder_session->fin);
2508 if(ferror(data->encoder_session->fin))
2509 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2510 else if(0 == *bytes)
2511 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
2513 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2516 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2519 FLAC__StreamDecoderSeekStatus flac_decoder_seek_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
2521 FLACDecoderData *data = (FLACDecoderData*)client_data;
2524 if(fseeko(data->encoder_session->fin, (off_t)absolute_byte_offset, SEEK_SET) < 0)
2525 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
2527 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
2530 FLAC__StreamDecoderTellStatus flac_decoder_tell_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
2532 FLACDecoderData *data = (FLACDecoderData*)client_data;
2536 if((pos = ftello(data->encoder_session->fin)) < 0)
2537 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
2539 *absolute_byte_offset = (FLAC__uint64)pos;
2540 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
2544 FLAC__StreamDecoderLengthStatus flac_decoder_length_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
2546 FLACDecoderData *data = (FLACDecoderData*)client_data;
2549 if(0 == data->filesize)
2550 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
2552 *stream_length = (FLAC__uint64)data->filesize;
2553 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
2557 FLAC__bool flac_decoder_eof_callback(const FLAC__StreamDecoder *decoder, void *client_data)
2559 FLACDecoderData *data = (FLACDecoderData*)client_data;
2562 return feof(data->encoder_session->fin)? true : false;
2565 FLAC__StreamDecoderWriteStatus flac_decoder_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
2567 FLACDecoderData *data = (FLACDecoderData*)client_data;
2568 FLAC__uint64 n = min(data->samples_left_to_process, frame->header.blocksize);
2571 if(!EncoderSession_process(data->encoder_session, buffer, (unsigned)n)) {
2572 print_error_with_state(data->encoder_session, "ERROR during encoding");
2573 data->fatal_error = true;
2574 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2577 data->samples_left_to_process -= n;
2578 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2581 void flac_decoder_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
2583 FLACDecoderData *data = (FLACDecoderData*)client_data;
2586 if (data->fatal_error)
2590 data->num_metadata_blocks == sizeof(data->metadata_blocks)/sizeof(data->metadata_blocks[0]) ||
2591 0 == (data->metadata_blocks[data->num_metadata_blocks] = FLAC__metadata_object_clone(metadata))
2593 data->fatal_error = true;
2595 data->num_metadata_blocks++;
2598 void flac_decoder_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
2600 FLACDecoderData *data = (FLACDecoderData*)client_data;
2603 flac__utils_printf(stderr, 1, "%s: ERROR got %s while decoding FLAC input\n", data->encoder_session->inbasefilename, FLAC__StreamDecoderErrorStatusString[status]);
2604 if(!data->encoder_session->continue_through_decode_errors)
2605 data->fatal_error = true;
2608 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)
2611 unsigned last_line_read;
2612 const char *error_message;
2614 if(0 == cuesheet_filename)
2617 if(lead_out_offset == 0) {
2618 flac__utils_printf(stderr, 1, "%s: ERROR cannot import cuesheet when the number of input samples to encode is unknown\n", inbasefilename);
2622 if(0 == (f = fopen(cuesheet_filename, "r"))) {
2623 flac__utils_printf(stderr, 1, "%s: ERROR opening cuesheet \"%s\" for reading: %s\n", inbasefilename, cuesheet_filename, strerror(errno));
2627 *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, is_cdda, lead_out_offset);
2631 if(0 == *cuesheet) {
2632 flac__utils_printf(stderr, 1, "%s: ERROR parsing cuesheet \"%s\" on line %u: %s\n", inbasefilename, cuesheet_filename, last_line_read, error_message);
2636 if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) {
2637 flac__utils_printf(stderr, 1, "%s: ERROR parsing cuesheet \"%s\": %s\n", inbasefilename, cuesheet_filename, error_message);
2641 /* if we're expecting CDDA, warn about non-compliance */
2642 if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) {
2643 flac__utils_printf(stderr, 1, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", inbasefilename, cuesheet_filename, error_message);
2644 if(treat_warnings_as_errors)
2646 (*cuesheet)->data.cue_sheet.is_cd = false;
2652 void print_stats(const EncoderSession *encoder_session)
2654 const FLAC__uint64 samples_written = min(encoder_session->total_samples_to_encode, encoder_session->samples_written);
2655 #if defined _MSC_VER || defined __MINGW32__
2656 /* with MSVC you have to spoon feed it the casting */
2657 const double progress = (double)(FLAC__int64)samples_written / (double)(FLAC__int64)encoder_session->total_samples_to_encode;
2658 const double ratio = (double)(FLAC__int64)encoder_session->bytes_written / ((double)(FLAC__int64)encoder_session->unencoded_size * min(1.0, progress));
2660 const double progress = (double)samples_written / (double)encoder_session->total_samples_to_encode;
2661 const double ratio = (double)encoder_session->bytes_written / ((double)encoder_session->unencoded_size * min(1.0, progress));
2664 FLAC__ASSERT(encoder_session->total_samples_to_encode > 0);
2666 if(samples_written == encoder_session->total_samples_to_encode) {
2667 flac__utils_printf(stderr, 2, "\r%s:%s wrote %u bytes, ratio=%0.3f",
2668 encoder_session->inbasefilename,
2669 encoder_session->verify? " Verify OK," : "",
2670 (unsigned)encoder_session->bytes_written,
2675 flac__utils_printf(stderr, 2, "\r%s: %u%% complete, ratio=%0.3f", encoder_session->inbasefilename, (unsigned)floor(progress * 100.0 + 0.5), ratio);
2679 void print_error_with_init_status(const EncoderSession *e, const char *message, FLAC__StreamEncoderInitStatus init_status)
2681 const int ilen = strlen(e->inbasefilename) + 1;
2682 const char *state_string = "";
2684 flac__utils_printf(stderr, 1, "\n%s: %s\n", e->inbasefilename, message);
2686 flac__utils_printf(stderr, 1, "%*s init_status = %s\n", ilen, "", FLAC__StreamEncoderInitStatusString[init_status]);
2688 if(init_status == FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR) {
2689 state_string = FLAC__stream_encoder_get_resolved_state_string(e->encoder);
2691 flac__utils_printf(stderr, 1, "%*s state = %s\n", ilen, "", state_string);
2693 /* print out some more info for some errors: */
2694 if(0 == strcmp(state_string, FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_CLIENT_ERROR])) {
2695 flac__utils_printf(stderr, 1,
2697 "An error occurred while writing; the most common cause is that the disk is full.\n"
2700 else if(0 == strcmp(state_string, FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_IO_ERROR])) {
2701 flac__utils_printf(stderr, 1,
2703 "An error occurred opening the output file; it is likely that the output\n"
2704 "directory does not exist or is not writable, the output file already exists and\n"
2705 "is not writable, or the disk is full.\n"
2709 else if(init_status == FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE) {
2710 flac__utils_printf(stderr, 1,
2712 "The encoding parameters specified do not conform to the FLAC Subset and may not\n"
2713 "be streamable or playable in hardware devices. If you really understand the\n"
2714 "consequences, you can add --lax to the command-line options to encode with\n"
2715 "these parameters anyway. See http://flac.sourceforge.net/format.html#subset\n"
2720 void print_error_with_state(const EncoderSession *e, const char *message)
2722 const int ilen = strlen(e->inbasefilename) + 1;
2723 const char *state_string;
2725 flac__utils_printf(stderr, 1, "\n%s: %s\n", e->inbasefilename, message);
2727 state_string = FLAC__stream_encoder_get_resolved_state_string(e->encoder);
2729 flac__utils_printf(stderr, 1, "%*s state = %s\n", ilen, "", state_string);
2731 /* print out some more info for some errors: */
2732 if(0 == strcmp(state_string, FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_CLIENT_ERROR])) {
2733 flac__utils_printf(stderr, 1,
2735 "An error occurred while writing; the most common cause is that the disk is full.\n"
2740 void print_verify_error(EncoderSession *e)
2742 FLAC__uint64 absolute_sample;
2743 unsigned frame_number;
2746 FLAC__int32 expected;
2749 FLAC__stream_encoder_get_verify_decoder_error_stats(e->encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
2751 flac__utils_printf(stderr, 1, "%s: ERROR: mismatch in decoded data, verify FAILED!\n", e->inbasefilename);
2752 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);
2753 flac__utils_printf(stderr, 1, " In all known cases, verify errors are caused by hardware problems,\n");
2754 flac__utils_printf(stderr, 1, " usually overclocking or bad RAM. Delete %s\n", e->outfilename);
2755 flac__utils_printf(stderr, 1, " and repeat the flac command exactly as before. If it does not give a\n");
2756 flac__utils_printf(stderr, 1, " verify error in the exact same place each time you try it, then there is\n");
2757 flac__utils_printf(stderr, 1, " a problem with your hardware; please see the FAQ:\n");
2758 flac__utils_printf(stderr, 1, " http://flac.sourceforge.net/faq.html#tools__hardware_prob\n");
2759 flac__utils_printf(stderr, 1, " If it does fail in the exact same place every time, keep\n");
2760 flac__utils_printf(stderr, 1, " %s and submit a bug report to:\n", e->outfilename);
2761 flac__utils_printf(stderr, 1, " https://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
2762 flac__utils_printf(stderr, 1, " Make sure to upload the FLAC file and use the \"Monitor\" feature to\n");
2763 flac__utils_printf(stderr, 1, " monitor the bug status.\n");
2764 flac__utils_printf(stderr, 1, "Verify FAILED! Do not trust %s\n", e->outfilename);
2767 FLAC__bool read_bytes(FILE *f, FLAC__byte *buf, size_t n, FLAC__bool eof_ok, const char *fn)
2769 size_t bytes_read = fread(buf, 1, n, f);
2771 if(bytes_read == 0) {
2773 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2779 if(bytes_read < n) {
2780 flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOF\n", fn);
2786 FLAC__bool read_uint16(FILE *f, FLAC__bool big_endian, FLAC__uint16 *val, const char *fn)
2788 if(!read_bytes(f, (FLAC__byte*)val, 2, /*eof_ok=*/false, fn))
2790 if(is_big_endian_host_ != big_endian) {
2791 FLAC__byte tmp, *b = (FLAC__byte*)val;
2792 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
2797 FLAC__bool read_uint32(FILE *f, FLAC__bool big_endian, FLAC__uint32 *val, const char *fn)
2799 if(!read_bytes(f, (FLAC__byte*)val, 4, /*eof_ok=*/false, fn))
2801 if(is_big_endian_host_ != big_endian) {
2802 FLAC__byte tmp, *b = (FLAC__byte*)val;
2803 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
2804 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
2809 FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, const char *fn)
2810 /* Read an IEEE 754 80-bit (aka SANE) extended floating point value from 'f',
2811 * convert it into an integral value and store in 'val'. Return false if only
2812 * between 1 and 9 bytes remain in 'f', if 0 bytes remain in 'f', or if the
2813 * value is negative, between zero and one, or too large to be represented by
2814 * 'val'; return true otherwise.
2823 if(!read_bytes(f, buf, sizeof(buf), /*eof_ok=*/false, fn))
2825 e = ((FLAC__uint16)(buf[0])<<8 | (FLAC__uint16)(buf[1]))-0x3FFF;
2827 if((buf[0]>>7)==1U || e<0 || e>63) {
2828 flac__utils_printf(stderr, 1, "%s: ERROR: invalid floating-point value\n", fn);
2832 for(i = 0; i < 8; ++i)
2833 p |= (FLAC__uint64)(buf[i+2])<<(56U-i*8);
2834 *val = (FLAC__uint32)((p>>shift)+(p>>(shift-1) & 0x1));
2839 FLAC__bool fskip_ahead(FILE *f, FLAC__uint64 offset)
2841 static unsigned char dump[8192];
2845 /* MS' stdio impl can't even seek forward on stdin, have to use pure non-fseek() version: */
2847 const long need = (long)min(offset, sizeof(dump));
2848 if((long)fread(dump, 1, need, f) < need)
2857 long need = (long)min(offset, LONG_MAX);
2858 if(fseeko(f, need, SEEK_CUR) < 0) {
2859 need = (long)min(offset, sizeof(dump));
2860 if((long)fread(dump, 1, need, f) < need)
2869 unsigned count_channel_mask_bits(FLAC__uint32 mask)
2881 FLAC__uint32 limit_channel_mask(FLAC__uint32 mask, unsigned channels)
2883 FLAC__uint32 x = 0x80000000;
2884 unsigned count = count_channel_mask_bits(mask);
2885 while(x && count > channels) {
2892 FLAC__ASSERT(count_channel_mask_bits(mask) == channels);