1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000,2001,2002,2003,2004,2005 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() */
30 #include <math.h> /* for floor() */
31 #include <stdio.h> /* for FILE et al. */
32 #include <string.h> /* for strcmp() */
34 #include "share/grabbag.h"
35 #include "share/replaygain_synthesis.h"
39 #include "OggFLAC/file_decoder.h"
47 FLAC__bool is_aiff_out;
48 FLAC__bool is_wave_out;
49 FLAC__bool continue_through_decode_errors;
52 replaygain_synthesis_spec_t spec;
53 FLAC__bool apply; /* 'spec.apply' is just a request; this 'apply' means we actually parsed the RG tags and are ready to go */
55 DitherContext dither_context;
59 FLAC__bool analysis_mode;
60 analysis_options aopts;
61 utils__SkipUntilSpecification *skip_specification;
62 utils__SkipUntilSpecification *until_specification; /* a canonicalized value of 0 mean end-of-stream (i.e. --until=-0) */
63 utils__CueSpecification *cue_specification;
65 const char *inbasefilename;
66 const char *outfilename;
68 FLAC__uint64 samples_processed;
69 unsigned frame_counter;
70 FLAC__bool abort_flag;
71 FLAC__bool aborting_due_to_until; /* true if we intentionally abort decoding prematurely because we hit the --until point */
74 FLAC__bool needs_fixup;
75 unsigned riff_offset; /* or FORM offset for AIFF */
76 unsigned data_offset; /* or SSND offset for AIFF */
77 unsigned frames_offset; /* AIFF only */
78 } wave_chunk_size_fixup;
80 FLAC__bool is_big_endian;
81 FLAC__bool is_unsigned_samples;
82 FLAC__uint64 total_samples;
89 FLAC__FileDecoder *file;
93 OggFLAC__FileDecoder *file;
102 static FLAC__bool is_big_endian_host_;
108 static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, const char *infilename, const char *outfilename);
109 static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
110 static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, decode_options_t decode_options, const char *infilename);
111 static FLAC__bool DecoderSession_process(DecoderSession *d);
112 static int DecoderSession_finish_ok(DecoderSession *d);
113 static int DecoderSession_finish_error(DecoderSession *d);
114 static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
115 static FLAC__bool write_necessary_headers(DecoderSession *decoder_session);
116 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
117 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
118 static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val);
119 static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val);
120 static FLAC__bool write_sane_extended(FILE *f, unsigned val);
121 static FLAC__bool fixup_wave_chunk_size(const char *outfilename, FLAC__bool is_wave_out, unsigned riff_offset, unsigned data_offset, unsigned frames_offset, FLAC__uint32 total_samples, unsigned channels, unsigned bps);
123 * We use 'void *' so that we can use the same callbacks for the
124 * FLAC__StreamDecoder and FLAC__FileDecoder. The 'decoder' argument is
125 * actually never used in the callbacks.
127 static FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
128 static void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
129 static void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
130 static void print_error_with_state(const DecoderSession *d, const char *message);
131 static void print_stats(const DecoderSession *decoder_session);
137 int flac__decode_aiff(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
139 DecoderSession decoder_session;
142 DecoderSession_construct(
145 options.common.is_ogg,
149 /*is_aiff_out=*/true,
150 /*is_wave_out=*/false,
151 options.common.continue_through_decode_errors,
152 options.common.replaygain_synthesis_spec,
155 &options.common.skip_specification,
156 &options.common.until_specification,
157 options.common.has_cue_specification? &options.common.cue_specification : 0,
164 if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
165 return DecoderSession_finish_error(&decoder_session);
167 if(!DecoderSession_process(&decoder_session))
168 return DecoderSession_finish_error(&decoder_session);
170 return DecoderSession_finish_ok(&decoder_session);
173 int flac__decode_wav(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
175 DecoderSession decoder_session;
178 DecoderSession_construct(
181 options.common.is_ogg,
185 /*is_aiff_out=*/false,
186 /*is_wave_out=*/true,
187 options.common.continue_through_decode_errors,
188 options.common.replaygain_synthesis_spec,
191 &options.common.skip_specification,
192 &options.common.until_specification,
193 options.common.has_cue_specification? &options.common.cue_specification : 0,
200 if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
201 return DecoderSession_finish_error(&decoder_session);
203 if(!DecoderSession_process(&decoder_session))
204 return DecoderSession_finish_error(&decoder_session);
206 return DecoderSession_finish_ok(&decoder_session);
209 int flac__decode_raw(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, raw_decode_options_t options)
211 DecoderSession decoder_session;
213 decoder_session.is_big_endian = options.is_big_endian;
214 decoder_session.is_unsigned_samples = options.is_unsigned_samples;
217 DecoderSession_construct(
220 options.common.is_ogg,
224 /*is_aiff_out=*/false,
225 /*is_wave_out=*/false,
226 options.common.continue_through_decode_errors,
227 options.common.replaygain_synthesis_spec,
230 &options.common.skip_specification,
231 &options.common.until_specification,
232 options.common.has_cue_specification? &options.common.cue_specification : 0,
239 if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
240 return DecoderSession_finish_error(&decoder_session);
242 if(!DecoderSession_process(&decoder_session))
243 return DecoderSession_finish_error(&decoder_session);
245 return DecoderSession_finish_ok(&decoder_session);
248 FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, const char *infilename, const char *outfilename)
256 d->is_aiff_out = is_aiff_out;
257 d->is_wave_out = is_wave_out;
258 d->continue_through_decode_errors = continue_through_decode_errors;
259 d->replaygain.spec = replaygain_synthesis_spec;
260 d->replaygain.apply = false;
261 d->replaygain.scale = 0.0;
262 /* d->replaygain.dither_context gets initialized later once we know the sample resolution */
263 d->test_only = (0 == outfilename);
264 d->analysis_mode = analysis_mode;
266 d->skip_specification = skip_specification;
267 d->until_specification = until_specification;
268 d->cue_specification = cue_specification;
270 d->inbasefilename = grabbag__file_get_basename(infilename);
271 d->outfilename = outfilename;
273 d->samples_processed = 0;
274 d->frame_counter = 0;
275 d->abort_flag = false;
276 d->aborting_due_to_until = false;
278 d->wave_chunk_size_fixup.needs_fixup = false;
280 d->decoder.flac.file = 0;
282 d->decoder.ogg.file = 0;
285 d->fout = 0; /* initialized with an open file later if necessary */
287 FLAC__ASSERT(!(d->test_only && d->analysis_mode));
290 if(0 == strcmp(outfilename, "-")) {
291 d->fout = grabbag__file_get_binary_stdout();
294 if(0 == (d->fout = fopen(outfilename, "wb"))) {
295 flac__utils_printf(stderr, 1, "%s: ERROR: can't open output file %s\n", d->inbasefilename, outfilename);
296 DecoderSession_destroy(d, /*error_occurred=*/true);
303 flac__analyze_init(aopts);
308 void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
310 if(0 != d->fout && d->fout != stdout) {
313 unlink(d->outfilename);
317 FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, decode_options_t decode_options, const char *infilename)
319 FLAC__uint32 test = 1;
321 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
324 if(decoder_session->is_ogg) {
325 decoder_session->decoder.ogg.file = OggFLAC__file_decoder_new();
327 if(0 == decoder_session->decoder.ogg.file) {
328 flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
332 OggFLAC__file_decoder_set_md5_checking(decoder_session->decoder.ogg.file, true);
333 OggFLAC__file_decoder_set_filename(decoder_session->decoder.ogg.file, infilename);
334 if(!decode_options.use_first_serial_number)
335 OggFLAC__file_decoder_set_serial_number(decoder_session->decoder.ogg.file, decode_options.serial_number);
336 if (0 != decoder_session->cue_specification)
337 OggFLAC__file_decoder_set_metadata_respond(decoder_session->decoder.ogg.file, FLAC__METADATA_TYPE_CUESHEET);
338 if (decoder_session->replaygain.spec.apply)
339 OggFLAC__file_decoder_set_metadata_respond(decoder_session->decoder.ogg.file, FLAC__METADATA_TYPE_VORBIS_COMMENT);
342 * The three ugly casts here are to 'downcast' the 'void *' argument of
343 * the callback down to 'OggFLAC__FileDecoder *'. In C++ this would be
344 * unnecessary but here the cast makes the C compiler happy.
346 OggFLAC__file_decoder_set_write_callback(decoder_session->decoder.ogg.file, (FLAC__StreamDecoderWriteStatus (*)(const OggFLAC__FileDecoder *, const FLAC__Frame *, const FLAC__int32 * const [], void *))write_callback);
347 OggFLAC__file_decoder_set_metadata_callback(decoder_session->decoder.ogg.file, (void (*)(const OggFLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
348 OggFLAC__file_decoder_set_error_callback(decoder_session->decoder.ogg.file, (void (*)(const OggFLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
349 OggFLAC__file_decoder_set_client_data(decoder_session->decoder.ogg.file, decoder_session);
351 if(OggFLAC__file_decoder_init(decoder_session->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK) {
352 print_error_with_state(decoder_session, "ERROR initializing decoder");
358 (void)decode_options;
361 decoder_session->decoder.flac.file = FLAC__file_decoder_new();
363 if(0 == decoder_session->decoder.flac.file) {
364 flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
368 FLAC__file_decoder_set_md5_checking(decoder_session->decoder.flac.file, true);
369 FLAC__file_decoder_set_filename(decoder_session->decoder.flac.file, infilename);
370 if (0 != decoder_session->cue_specification)
371 FLAC__file_decoder_set_metadata_respond(decoder_session->decoder.flac.file, FLAC__METADATA_TYPE_CUESHEET);
372 if (decoder_session->replaygain.spec.apply)
373 FLAC__file_decoder_set_metadata_respond(decoder_session->decoder.flac.file, FLAC__METADATA_TYPE_VORBIS_COMMENT);
375 * The three ugly casts here are to 'downcast' the 'void *' argument of
376 * the callback down to 'FLAC__FileDecoder *'.
378 FLAC__file_decoder_set_write_callback(decoder_session->decoder.flac.file, (FLAC__StreamDecoderWriteStatus (*)(const FLAC__FileDecoder *, const FLAC__Frame *, const FLAC__int32 * const [], void *))write_callback);
379 FLAC__file_decoder_set_metadata_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
380 FLAC__file_decoder_set_error_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
381 FLAC__file_decoder_set_client_data(decoder_session->decoder.flac.file, decoder_session);
383 if(FLAC__file_decoder_init(decoder_session->decoder.flac.file) != FLAC__FILE_DECODER_OK) {
384 print_error_with_state(decoder_session, "ERROR initializing decoder");
392 FLAC__bool DecoderSession_process(DecoderSession *d)
396 if(!OggFLAC__file_decoder_process_until_end_of_metadata(d->decoder.ogg.file)) {
397 flac__utils_printf(stderr, 2, "\n");
398 print_error_with_state(d, "ERROR while decoding metadata");
401 if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE) {
402 flac__utils_printf(stderr, 2, "\n");
403 print_error_with_state(d, "ERROR during metadata decoding");
410 if(!FLAC__file_decoder_process_until_end_of_metadata(d->decoder.flac.file)) {
411 flac__utils_printf(stderr, 2, "\n");
412 print_error_with_state(d, "ERROR while decoding metadata");
415 if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE) {
416 flac__utils_printf(stderr, 2, "\n");
417 print_error_with_state(d, "ERROR during metadata decoding");
424 /* write the WAVE/AIFF headers if necessary */
425 if(!write_necessary_headers(d)) {
426 d->abort_flag = true;
430 if(d->skip_specification->value.samples > 0) {
431 const FLAC__uint64 skip = (FLAC__uint64)d->skip_specification->value.samples;
435 if(!OggFLAC__file_decoder_seek_absolute(d->decoder.ogg.file, skip)) {
436 print_error_with_state(d, "ERROR seeking while skipping bytes");
439 if(!OggFLAC__file_decoder_process_until_end_of_file(d->decoder.ogg.file) && !d->aborting_due_to_until) {
440 flac__utils_printf(stderr, 2, "\n");
441 print_error_with_state(d, "ERROR while decoding frames");
444 if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
445 flac__utils_printf(stderr, 2, "\n");
446 print_error_with_state(d, "ERROR during decoding");
453 if(!FLAC__file_decoder_seek_absolute(d->decoder.flac.file, skip)) {
454 print_error_with_state(d, "ERROR seeking while skipping bytes");
457 if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file) && !d->aborting_due_to_until) {
458 flac__utils_printf(stderr, 2, "\n");
459 print_error_with_state(d, "ERROR while decoding frames");
462 if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
463 flac__utils_printf(stderr, 2, "\n");
464 print_error_with_state(d, "ERROR during decoding");
472 if(!OggFLAC__file_decoder_process_until_end_of_file(d->decoder.ogg.file) && !d->aborting_due_to_until) {
473 flac__utils_printf(stderr, 2, "\n");
474 print_error_with_state(d, "ERROR while decoding data");
477 if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
478 flac__utils_printf(stderr, 2, "\n");
479 print_error_with_state(d, "ERROR during decoding");
486 if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file) && !d->aborting_due_to_until) {
487 flac__utils_printf(stderr, 2, "\n");
488 print_error_with_state(d, "ERROR while decoding data");
491 if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
492 flac__utils_printf(stderr, 2, "\n");
493 print_error_with_state(d, "ERROR during decoding");
499 if((d->is_wave_out || d->is_aiff_out) && ((d->total_samples * d->channels * ((d->bps+7)/8)) & 1)) {
500 if(flac__utils_fwrite("\000", 1, 1, d->fout) != 1) {
501 print_error_with_state(d, d->is_wave_out?
502 "ERROR writing pad byte to WAVE data chunk" :
503 "ERROR writing pad byte to AIFF SSND chunk"
512 int DecoderSession_finish_ok(DecoderSession *d)
514 FLAC__bool md5_failure = false;
518 if(d->decoder.ogg.file) {
519 md5_failure = !OggFLAC__file_decoder_finish(d->decoder.ogg.file) && !d->aborting_due_to_until;
521 OggFLAC__file_decoder_delete(d->decoder.ogg.file);
527 if(d->decoder.flac.file) {
528 md5_failure = !FLAC__file_decoder_finish(d->decoder.flac.file) && !d->aborting_due_to_until;
530 FLAC__file_decoder_delete(d->decoder.flac.file);
534 flac__analyze_finish(d->aopts);
536 flac__utils_printf(stderr, 1, "\r%s: WARNING, MD5 signature mismatch\n", d->inbasefilename);
539 flac__utils_printf(stderr, 2, "\r%s: %s \n", d->inbasefilename, d->test_only? "ok ":d->analysis_mode?"done ":"done");
541 DecoderSession_destroy(d, /*error_occurred=*/false);
542 if((d->is_wave_out || d->is_aiff_out) && d->wave_chunk_size_fixup.needs_fixup)
543 if(!fixup_wave_chunk_size(d->outfilename, d->is_wave_out, d->wave_chunk_size_fixup.riff_offset, d->wave_chunk_size_fixup.data_offset, d->wave_chunk_size_fixup.frames_offset, (FLAC__uint32)d->samples_processed, d->channels, d->bps))
548 int DecoderSession_finish_error(DecoderSession *d)
552 if(d->decoder.ogg.file) {
553 OggFLAC__file_decoder_finish(d->decoder.ogg.file);
554 OggFLAC__file_decoder_delete(d->decoder.ogg.file);
560 if(d->decoder.flac.file) {
561 FLAC__file_decoder_finish(d->decoder.flac.file);
562 FLAC__file_decoder_delete(d->decoder.flac.file);
566 flac__analyze_finish(d->aopts);
567 DecoderSession_destroy(d, /*error_occurred=*/true);
571 FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input)
573 /* convert from mm:ss.sss to sample number if necessary */
574 flac__utils_canonicalize_skip_until_specification(spec, sample_rate);
576 /* special case: if "--until=-0", use the special value '0' to mean "end-of-stream" */
577 if(spec->is_relative && spec->value.samples == 0) {
578 spec->is_relative = false;
582 /* in any other case the total samples in the input must be known */
583 if(total_samples_in_input == 0) {
584 flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when FLAC metadata has total sample count of 0\n", inbasefilename);
588 FLAC__ASSERT(spec->value_is_samples);
590 /* convert relative specifications to absolute */
591 if(spec->is_relative) {
592 if(spec->value.samples <= 0)
593 spec->value.samples += (FLAC__int64)total_samples_in_input;
595 spec->value.samples += skip;
596 spec->is_relative = false;
600 if(spec->value.samples < 0) {
601 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before beginning of input\n", inbasefilename);
604 if((FLAC__uint64)spec->value.samples <= skip) {
605 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before --skip point\n", inbasefilename);
608 if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
609 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is after end of input\n", inbasefilename);
616 FLAC__bool write_necessary_headers(DecoderSession *decoder_session)
618 /* write the WAVE/AIFF headers if necessary */
619 if(!decoder_session->analysis_mode && !decoder_session->test_only && (decoder_session->is_wave_out || decoder_session->is_aiff_out)) {
620 const char *fmt_desc = decoder_session->is_wave_out? "WAVE" : "AIFF";
621 FLAC__uint64 data_size = decoder_session->total_samples * decoder_session->channels * ((decoder_session->bps+7)/8);
622 const FLAC__uint32 aligned_data_size = (FLAC__uint32)((data_size+1) & (~1U)); /* we'll check for overflow later */
623 if(decoder_session->total_samples == 0) {
624 if(decoder_session->fout == stdout) {
625 flac__utils_printf(stderr, 1, "%s: WARNING, don't have accurate sample count available for %s header.\n", decoder_session->inbasefilename, fmt_desc);
626 flac__utils_printf(stderr, 1, " Generated %s file will have a data chunk size of 0. Try\n", fmt_desc);
627 flac__utils_printf(stderr, 1, " decoding directly to a file instead.\n");
630 decoder_session->wave_chunk_size_fixup.needs_fixup = true;
633 if(data_size >= 0xFFFFFFDC) {
634 flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file chunk\n", decoder_session->inbasefilename, fmt_desc);
637 if(decoder_session->is_wave_out) {
638 if(flac__utils_fwrite("RIFF", 1, 4, decoder_session->fout) != 4)
641 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
642 decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
644 if(!write_little_endian_uint32(decoder_session->fout, aligned_data_size+36)) /* filesize-8 */
647 if(flac__utils_fwrite("WAVEfmt ", 1, 8, decoder_session->fout) != 8)
650 if(flac__utils_fwrite("\020\000\000\000", 1, 4, decoder_session->fout) != 4) /* chunk size = 16 */
653 if(flac__utils_fwrite("\001\000", 1, 2, decoder_session->fout) != 2) /* compression code == 1 */
656 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
659 if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate))
662 if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate * decoder_session->channels * ((decoder_session->bps+7) / 8))) /* @@@ or is it (sample_rate*channels*bps) / 8 ??? */
665 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels * ((decoder_session->bps+7) / 8)))) /* block align */
668 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps))) /* bits per sample */
671 if(flac__utils_fwrite("data", 1, 4, decoder_session->fout) != 4)
674 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
675 decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
677 if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size)) /* data size */
681 if(flac__utils_fwrite("FORM", 1, 4, decoder_session->fout) != 4)
684 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
685 decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
687 if(!write_big_endian_uint32(decoder_session->fout, aligned_data_size+46)) /* filesize-8 */
690 if(flac__utils_fwrite("AIFFCOMM", 1, 8, decoder_session->fout) != 8)
693 if(flac__utils_fwrite("\000\000\000\022", 1, 4, decoder_session->fout) != 4) /* chunk size = 18 */
696 if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
699 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
700 decoder_session->wave_chunk_size_fixup.frames_offset = ftell(decoder_session->fout);
702 if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)decoder_session->total_samples))
705 if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps)))
708 if(!write_sane_extended(decoder_session->fout, decoder_session->sample_rate))
711 if(flac__utils_fwrite("SSND", 1, 4, decoder_session->fout) != 4)
714 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
715 decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
717 if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size+8)) /* data size */
720 if(!write_big_endian_uint32(decoder_session->fout, 0/*offset*/))
723 if(!write_big_endian_uint32(decoder_session->fout, 0/*block_size*/))
731 FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
733 FLAC__byte *b = (FLAC__byte*)(&val);
734 if(is_big_endian_host_) {
736 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
738 return flac__utils_fwrite(b, 1, 2, f) == 2;
741 FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
743 FLAC__byte *b = (FLAC__byte*)(&val);
744 if(is_big_endian_host_) {
746 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
747 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
749 return flac__utils_fwrite(b, 1, 4, f) == 4;
752 FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val)
754 FLAC__byte *b = (FLAC__byte*)(&val);
755 if(!is_big_endian_host_) {
757 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
759 return flac__utils_fwrite(b, 1, 2, f) == 2;
762 FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val)
764 FLAC__byte *b = (FLAC__byte*)(&val);
765 if(!is_big_endian_host_) {
767 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
768 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
770 return flac__utils_fwrite(b, 1, 4, f) == 4;
773 FLAC__bool write_sane_extended(FILE *f, unsigned val)
774 /* Write to 'f' a SANE extended representation of 'val'. Return false if
775 * the write succeeds; return true otherwise.
777 * SANE extended is an 80-bit IEEE-754 representation with sign bit, 15 bits
778 * of exponent, and 64 bits of significand (mantissa). Unlike most IEEE-754
779 * representations, it does not imply a 1 above the MSB of the significand.
785 unsigned int shift, exponent;
787 FLAC__ASSERT(val!=0U); /* handling 0 would require a special case */
789 for(shift= 0U; (val>>(31-shift))==0U; ++shift)
792 exponent= 63U-(shift+32U); /* add 32 for unused second word */
794 if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent+0x3FFF)))
796 if(!write_big_endian_uint32(f, val))
798 if(!write_big_endian_uint32(f, 0)) /* unused second word */
804 FLAC__bool fixup_wave_chunk_size(const char *outfilename, FLAC__bool is_wave_out, unsigned riff_offset, unsigned data_offset, unsigned frames_offset, FLAC__uint32 total_samples, unsigned channels, unsigned bps)
806 const char *fmt_desc = (is_wave_out? "WAVE" : "AIFF");
807 FLAC__bool (*write_it)(FILE *, FLAC__uint32) = (is_wave_out? write_little_endian_uint32 : write_big_endian_uint32);
808 FILE *f = fopen(outfilename, "r+b");
809 FLAC__uint32 data_size, aligned_data_size;
812 flac__utils_printf(stderr, 1, "ERROR, couldn't open file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
816 data_size = aligned_data_size = total_samples * channels * ((bps+7)/8);
817 if(aligned_data_size & 1)
820 if(fseek(f, riff_offset, SEEK_SET) < 0) {
821 flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
825 if(!write_it(f, aligned_data_size + (is_wave_out? 36 : 46))) {
826 flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
831 if(fseek(f, frames_offset, SEEK_SET) < 0) {
832 flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
836 if(!write_it(f, total_samples)) {
837 flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
842 if(fseek(f, data_offset, SEEK_SET) < 0) {
843 flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
847 if(!write_it(f, data_size + (is_wave_out? 0 : 8))) {
848 flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
856 FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
858 DecoderSession *decoder_session = (DecoderSession*)client_data;
859 FILE *fout = decoder_session->fout;
860 const unsigned bps = frame->header.bits_per_sample, channels = frame->header.channels;
861 FLAC__bool is_big_endian = (decoder_session->is_aiff_out? true : (decoder_session->is_wave_out? false : decoder_session->is_big_endian));
862 FLAC__bool is_unsigned_samples = (decoder_session->is_aiff_out? false : (decoder_session->is_wave_out? bps<=8 : decoder_session->is_unsigned_samples));
863 unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
864 static FLAC__int8 s8buffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(FLAC__int32)]; /* WATCHOUT: can be up to 2 megs */
865 FLAC__uint8 *u8buffer = (FLAC__uint8 *)s8buffer;
866 FLAC__int16 *s16buffer = (FLAC__int16 *)s8buffer;
867 FLAC__uint16 *u16buffer = (FLAC__uint16 *)s8buffer;
868 FLAC__int32 *s32buffer = (FLAC__int32 *)s8buffer;
869 FLAC__uint32 *u32buffer = (FLAC__uint32 *)s8buffer;
870 size_t bytes_to_write = 0;
874 if(decoder_session->abort_flag)
875 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
877 if(bps != decoder_session->bps) {
878 flac__utils_printf(stderr, 1, "%s: ERROR, bits-per-sample is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, bps, decoder_session->bps);
879 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
881 if(channels != decoder_session->channels) {
882 flac__utils_printf(stderr, 1, "%s: ERROR, channels is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, channels, decoder_session->channels);
883 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
885 if(frame->header.sample_rate != decoder_session->sample_rate) {
886 flac__utils_printf(stderr, 1, "%s: ERROR, sample rate is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, frame->header.sample_rate, decoder_session->sample_rate);
887 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
891 * limit the number of samples to accept based on --until
893 FLAC__ASSERT(!decoder_session->skip_specification->is_relative);
894 FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
895 FLAC__ASSERT(!decoder_session->until_specification->is_relative);
896 FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
897 if(decoder_session->until_specification->value.samples > 0) {
898 const FLAC__uint64 skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
899 const FLAC__uint64 until = (FLAC__uint64)decoder_session->until_specification->value.samples;
900 const FLAC__uint64 input_samples_passed = skip + decoder_session->samples_processed;
901 FLAC__ASSERT(until >= input_samples_passed);
902 if(input_samples_passed + wide_samples > until)
903 wide_samples = (unsigned)(until - input_samples_passed);
904 if (wide_samples == 0) {
905 decoder_session->abort_flag = true;
906 decoder_session->aborting_due_to_until = true;
907 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
911 if(wide_samples > 0) {
912 decoder_session->samples_processed += wide_samples;
913 decoder_session->frame_counter++;
915 if(!(decoder_session->frame_counter & 0x3f))
916 print_stats(decoder_session);
918 if(decoder_session->analysis_mode) {
919 flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->aopts, fout);
921 else if(!decoder_session->test_only) {
922 if (decoder_session->replaygain.apply) {
923 bytes_to_write = FLAC__replaygain_synthesis__apply_gain(
930 bps, /* source_bps */
931 bps, /* target_bps */
932 decoder_session->replaygain.scale,
933 decoder_session->replaygain.spec.limiter == RGSS_LIMIT__HARD, /* hard_limit */
934 decoder_session->replaygain.spec.noise_shaping != NOISE_SHAPING_NONE, /* do_dithering */
935 &decoder_session->replaygain.dither_context
939 if(is_unsigned_samples) {
940 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
941 for(channel = 0; channel < channels; channel++, sample++)
942 u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
945 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
946 for(channel = 0; channel < channels; channel++, sample++)
947 s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
949 bytes_to_write = sample;
952 if(is_unsigned_samples) {
953 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
954 for(channel = 0; channel < channels; channel++, sample++)
955 u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
958 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
959 for(channel = 0; channel < channels; channel++, sample++)
960 s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
962 if(is_big_endian != is_big_endian_host_) {
964 const unsigned bytes = sample * 2;
965 for(byte = 0; byte < bytes; byte += 2) {
966 tmp = u8buffer[byte];
967 u8buffer[byte] = u8buffer[byte+1];
968 u8buffer[byte+1] = tmp;
971 bytes_to_write = 2 * sample;
974 if(is_unsigned_samples) {
975 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
976 for(channel = 0; channel < channels; channel++, sample++)
977 u32buffer[sample] = buffer[channel][wide_sample] + 0x800000;
980 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
981 for(channel = 0; channel < channels; channel++, sample++)
982 s32buffer[sample] = buffer[channel][wide_sample];
984 if(is_big_endian != is_big_endian_host_) {
986 const unsigned bytes = sample * 4;
987 for(byte = 0; byte < bytes; byte += 4) {
988 tmp = u8buffer[byte];
989 u8buffer[byte] = u8buffer[byte+3];
990 u8buffer[byte+3] = tmp;
991 tmp = u8buffer[byte+1];
992 u8buffer[byte+1] = u8buffer[byte+2];
993 u8buffer[byte+2] = tmp;
998 const unsigned bytes = sample * 4;
999 for(lbyte = byte = 0; byte < bytes; ) {
1001 u8buffer[lbyte++] = u8buffer[byte++];
1002 u8buffer[lbyte++] = u8buffer[byte++];
1003 u8buffer[lbyte++] = u8buffer[byte++];
1008 const unsigned bytes = sample * 4;
1009 for(lbyte = byte = 0; byte < bytes; ) {
1010 u8buffer[lbyte++] = u8buffer[byte++];
1011 u8buffer[lbyte++] = u8buffer[byte++];
1012 u8buffer[lbyte++] = u8buffer[byte++];
1016 bytes_to_write = 3 * sample;
1023 if(bytes_to_write > 0) {
1024 if(flac__utils_fwrite(u8buffer, 1, bytes_to_write, fout) != bytes_to_write) {
1025 /* if a pipe closed when writing to stdout, we let it go without an error message */
1026 if(errno == EPIPE && decoder_session->fout == stdout)
1027 decoder_session->aborting_due_to_until = true;
1028 decoder_session->abort_flag = true;
1029 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
1032 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
1035 void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
1037 DecoderSession *decoder_session = (DecoderSession*)client_data;
1039 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
1040 FLAC__uint64 skip, until;
1041 decoder_session->bps = metadata->data.stream_info.bits_per_sample;
1042 decoder_session->channels = metadata->data.stream_info.channels;
1043 decoder_session->sample_rate = metadata->data.stream_info.sample_rate;
1045 flac__utils_canonicalize_skip_until_specification(decoder_session->skip_specification, decoder_session->sample_rate);
1046 FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
1047 skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
1049 /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
1050 if(metadata->data.stream_info.total_samples > 0 && skip >= metadata->data.stream_info.total_samples) {
1051 flac__utils_printf(stderr, 1, "%s: ERROR trying to --skip more samples than in stream\n", decoder_session->inbasefilename);
1052 decoder_session->abort_flag = true;
1055 else if(metadata->data.stream_info.total_samples == 0 && skip > 0) {
1056 flac__utils_printf(stderr, 1, "%s: ERROR, can't --skip when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
1057 decoder_session->abort_flag = true;
1060 FLAC__ASSERT(skip == 0 || 0 == decoder_session->cue_specification);
1061 decoder_session->total_samples = metadata->data.stream_info.total_samples - skip;
1063 /* note that we use metadata->data.stream_info.total_samples instead of decoder_session->total_samples */
1064 if(!canonicalize_until_specification(decoder_session->until_specification, decoder_session->inbasefilename, decoder_session->sample_rate, skip, metadata->data.stream_info.total_samples)) {
1065 decoder_session->abort_flag = true;
1068 FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
1069 until = (FLAC__uint64)decoder_session->until_specification->value.samples;
1072 FLAC__ASSERT(decoder_session->total_samples != 0);
1073 FLAC__ASSERT(0 == decoder_session->cue_specification);
1074 decoder_session->total_samples -= (metadata->data.stream_info.total_samples - until);
1077 if(decoder_session->bps != 8 && decoder_session->bps != 16 && decoder_session->bps != 24) {
1078 flac__utils_printf(stderr, 1, "%s: ERROR: bits per sample is not 8/16/24\n", decoder_session->inbasefilename);
1079 decoder_session->abort_flag = true;
1083 else if(metadata->type == FLAC__METADATA_TYPE_CUESHEET) {
1084 /* remember, at this point, decoder_session->total_samples can be 0, meaning 'unknown' */
1085 if(decoder_session->total_samples == 0) {
1086 flac__utils_printf(stderr, 1, "%s: ERROR can't use --cue when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
1087 decoder_session->abort_flag = true;
1091 flac__utils_canonicalize_cue_specification(decoder_session->cue_specification, &metadata->data.cue_sheet, decoder_session->total_samples, decoder_session->skip_specification, decoder_session->until_specification);
1093 FLAC__ASSERT(!decoder_session->skip_specification->is_relative);
1094 FLAC__ASSERT(decoder_session->skip_specification->value_is_samples);
1096 FLAC__ASSERT(!decoder_session->until_specification->is_relative);
1097 FLAC__ASSERT(decoder_session->until_specification->value_is_samples);
1099 FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
1100 FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
1101 FLAC__ASSERT((FLAC__uint64)decoder_session->until_specification->value.samples <= decoder_session->total_samples);
1102 FLAC__ASSERT(decoder_session->skip_specification->value.samples <= decoder_session->until_specification->value.samples);
1104 decoder_session->total_samples = decoder_session->until_specification->value.samples - decoder_session->skip_specification->value.samples;
1106 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
1107 if (decoder_session->replaygain.spec.apply) {
1109 if (!(decoder_session->replaygain.apply = grabbag__replaygain_load_from_vorbiscomment(metadata, decoder_session->replaygain.spec.use_album_gain, &gain, &peak))) {
1110 flac__utils_printf(stderr, 1, "%s: WARNING: can't get %s ReplayGain tag\n", decoder_session->inbasefilename, decoder_session->replaygain.spec.use_album_gain? "album":"track");
1113 const char *ls[] = { "no", "peak", "hard" };
1114 const char *ns[] = { "no", "low", "medium", "high" };
1115 decoder_session->replaygain.scale = grabbag__replaygain_compute_scale_factor(peak, gain, decoder_session->replaygain.spec.preamp, decoder_session->replaygain.spec.limiter == RGSS_LIMIT__PEAK);
1116 FLAC__ASSERT(decoder_session->bps > 0 && decoder_session->bps <= 32);
1117 FLAC__replaygain_synthesis__init_dither_context(&decoder_session->replaygain.dither_context, decoder_session->bps, decoder_session->replaygain.spec.noise_shaping);
1118 flac__utils_printf(stderr, 1, "%s: INFO: applying %s ReplayGain (gain=%0.2fdB+preamp=%0.1fdB, %s noise shaping, %s limiting) to output\n", decoder_session->inbasefilename, decoder_session->replaygain.spec.use_album_gain? "album":"track", gain, decoder_session->replaygain.spec.preamp, ns[decoder_session->replaygain.spec.noise_shaping], ls[decoder_session->replaygain.spec.limiter]);
1119 flac__utils_printf(stderr, 1, "%s: WARNING: applying ReplayGain is not lossless\n", decoder_session->inbasefilename);
1125 void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
1127 DecoderSession *decoder_session = (DecoderSession*)client_data;
1129 flac__utils_printf(stderr, 1, "%s: *** Got error code %d:%s\n", decoder_session->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
1130 if(!decoder_session->continue_through_decode_errors)
1131 decoder_session->abort_flag = true;
1134 void print_error_with_state(const DecoderSession *d, const char *message)
1136 const int ilen = strlen(d->inbasefilename) + 1;
1137 const char *state_string;
1139 flac__utils_printf(stderr, 1, "\n%s: %s\n", d->inbasefilename, message);
1141 #ifdef FLAC__HAS_OGG
1143 state_string = OggFLAC__file_decoder_get_resolved_state_string(d->decoder.ogg.file);
1148 state_string = FLAC__file_decoder_get_resolved_state_string(d->decoder.flac.file);
1151 flac__utils_printf(stderr, 1, "%*s state = %s\n", ilen, "", state_string);
1153 /* print out some more info for some errors: */
1154 if (0 == strcmp(state_string, FLAC__StreamDecoderStateString[FLAC__STREAM_DECODER_UNPARSEABLE_STREAM])) {
1155 flac__utils_printf(stderr, 1,
1157 "The FLAC stream may have been created by a more advanced encoder. Try\n"
1158 " metaflac --show-vendor-tag %s\n"
1159 "If the version number is greater than %s, this decoder is probably\n"
1160 "not able to decode the file. If the version number is not, the file\n"
1161 "may be corrupted, or you may have found a bug. In this case please\n"
1162 "submit a bug report to\n"
1163 " http://sourceforge.net/bugs/?func=addbug&group_id=13478\n"
1164 "Make sure to use the \"Monitor\" feature to monitor the bug status.\n",
1165 d->inbasefilename, FLAC__VERSION_STRING
1169 0 == strcmp(state_string, FLAC__FileDecoderStateString[FLAC__FILE_DECODER_ERROR_OPENING_FILE])
1170 #ifdef FLAC__HAS_OGG
1171 || 0 == strcmp(state_string, OggFLAC__FileDecoderStateString[OggFLAC__FILE_DECODER_ERROR_OPENING_FILE])
1174 flac__utils_printf(stderr, 1,
1176 "An error occurred opening the input file; it is likely that it does not exist\n"
1177 "or is not readable.\n"
1182 void print_stats(const DecoderSession *decoder_session)
1184 if(flac__utils_verbosity_ >= 2) {
1185 #if defined _MSC_VER || defined __MINGW32__
1186 /* with MSVC you have to spoon feed it the casting */
1187 const double progress = (double)(FLAC__int64)decoder_session->samples_processed / (double)(FLAC__int64)decoder_session->total_samples * 100.0;
1189 const double progress = (double)decoder_session->samples_processed / (double)decoder_session->total_samples * 100.0;
1191 if(decoder_session->total_samples > 0) {
1192 fprintf(stderr, "\r%s: %s%u%% complete",
1193 decoder_session->inbasefilename,
1194 decoder_session->test_only? "testing, " : decoder_session->analysis_mode? "analyzing, " : "",
1195 (unsigned)floor(progress + 0.5)
1199 fprintf(stderr, "\r%s: %s %u samples",
1200 decoder_session->inbasefilename,
1201 decoder_session->test_only? "tested" : decoder_session->analysis_mode? "analyzed" : "wrote",
1202 (unsigned)decoder_session->samples_processed