1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000,2001,2002 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 #include <math.h> /* for floor() */
30 #include <stdio.h> /* for FILE et al. */
31 #include <string.h> /* for strcmp() */
33 #include "share/grabbag.h"
37 #include "OggFLAC/stream_decoder.h"
46 FLAC__bool is_aiff_out;
47 FLAC__bool is_wave_out;
48 FLAC__bool continue_through_decode_errors;
50 FLAC__bool analysis_mode;
51 analysis_options aopts;
52 utils__SkipUntilSpecification *skip_specification;
53 utils__SkipUntilSpecification *until_specification; /* a canonicalized value of 0 mean end-of-stream (i.e. --until=-0) */
55 const char *inbasefilename;
56 const char *outfilename;
58 FLAC__uint64 samples_processed;
59 unsigned frame_counter;
60 FLAC__bool abort_flag;
63 FLAC__bool needs_fixup;
64 unsigned riff_offset; /* or FORM offset for AIFF */
65 unsigned data_offset; /* or SSND offset for AIFF */
66 unsigned frames_offset; /* AIFF only */
67 } wave_chunk_size_fixup;
69 FLAC__bool is_big_endian;
70 FLAC__bool is_unsigned_samples;
71 FLAC__uint64 total_samples;
78 FLAC__FileDecoder *file;
82 OggFLAC__StreamDecoder *stream;
94 static FLAC__bool is_big_endian_host_;
100 static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool verbose, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, const char *infilename, const char *outfilename);
101 static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
102 static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, decode_options_t decode_options, const char *infilename);
103 static FLAC__bool DecoderSession_process(DecoderSession *d);
104 static int DecoderSession_finish_ok(DecoderSession *d);
105 static int DecoderSession_finish_error(DecoderSession *d);
106 static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
107 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
108 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
109 static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val);
110 static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val);
111 static FLAC__bool write_sane_extended(FILE *f, unsigned val);
112 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);
114 static FLAC__StreamDecoderReadStatus read_callback(const OggFLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
117 * We use 'void *' so that we can use the same callbacks for the
118 * FLAC__StreamDecoder and FLAC__FileDecoder. The 'decoder' argument is
119 * actually never used in the callbacks.
121 static FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
122 static void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
123 static void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
124 static void print_error_with_state(const DecoderSession *d, const char *message);
125 static void print_stats(const DecoderSession *decoder_session);
131 int flac__decode_aiff(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
133 DecoderSession decoder_session;
136 DecoderSession_construct(
139 options.common.is_ogg,
143 options.common.verbose,
144 /*is_aiff_out=*/true,
145 /*is_wave_out=*/false,
146 options.common.continue_through_decode_errors,
149 &options.common.skip_specification,
150 &options.common.until_specification,
157 if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
158 return DecoderSession_finish_error(&decoder_session);
160 if(!DecoderSession_process(&decoder_session))
161 return DecoderSession_finish_error(&decoder_session);
163 return DecoderSession_finish_ok(&decoder_session);
166 int flac__decode_wav(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
168 DecoderSession decoder_session;
171 DecoderSession_construct(
174 options.common.is_ogg,
178 options.common.verbose,
179 /*is_aiff_out=*/false,
180 /*is_wave_out=*/true,
181 options.common.continue_through_decode_errors,
184 &options.common.skip_specification,
185 &options.common.until_specification,
192 if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
193 return DecoderSession_finish_error(&decoder_session);
195 if(!DecoderSession_process(&decoder_session))
196 return DecoderSession_finish_error(&decoder_session);
198 return DecoderSession_finish_ok(&decoder_session);
201 int flac__decode_raw(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, raw_decode_options_t options)
203 DecoderSession decoder_session;
205 decoder_session.is_big_endian = options.is_big_endian;
206 decoder_session.is_unsigned_samples = options.is_unsigned_samples;
209 DecoderSession_construct(
212 options.common.is_ogg,
216 options.common.verbose,
217 /*is_aiff_out=*/false,
218 /*is_wave_out=*/false,
219 options.common.continue_through_decode_errors,
222 &options.common.skip_specification,
223 &options.common.until_specification,
230 if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
231 return DecoderSession_finish_error(&decoder_session);
233 if(!DecoderSession_process(&decoder_session))
234 return DecoderSession_finish_error(&decoder_session);
236 return DecoderSession_finish_ok(&decoder_session);
239 FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool verbose, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, const char *infilename, const char *outfilename)
247 d->verbose = verbose;
248 d->is_aiff_out = is_aiff_out;
249 d->is_wave_out = is_wave_out;
250 d->continue_through_decode_errors = continue_through_decode_errors;
251 d->test_only = (0 == outfilename);
252 d->analysis_mode = analysis_mode;
254 d->skip_specification = skip_specification;
255 d->until_specification = until_specification;
257 d->inbasefilename = grabbag__file_get_basename(infilename);
258 d->outfilename = outfilename;
260 d->samples_processed = 0;
261 d->frame_counter = 0;
262 d->abort_flag = false;
264 d->wave_chunk_size_fixup.needs_fixup = false;
266 d->decoder.flac.file = 0;
268 d->decoder.ogg.stream = 0;
274 d->fout = 0; /* initialized with an open file later if necessary */
276 FLAC__ASSERT(!(d->test_only && d->analysis_mode));
279 if(0 == strcmp(outfilename, "-")) {
280 d->fout = grabbag__file_get_binary_stdout();
283 if(0 == (d->fout = fopen(outfilename, "wb"))) {
284 fprintf(stderr, "%s: ERROR: can't open output file %s\n", d->inbasefilename, outfilename);
285 DecoderSession_destroy(d, /*error_occurred=*/true);
293 if (0 == strcmp(infilename, "-")) {
294 d->fin = grabbag__file_get_binary_stdin();
296 if (0 == (d->fin = fopen(infilename, "rb"))) {
297 fprintf(stderr, "%s: ERROR: can't open input file %s\n", d->inbasefilename, infilename);
298 DecoderSession_destroy(d, /*error_occurred=*/true);
306 flac__analyze_init(aopts);
311 void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
313 if(0 != d->fout && d->fout != stdout) {
316 unlink(d->outfilename);
319 if(0 != d->fin && d->fin != stdin)
324 FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, decode_options_t decode_options, const char *infilename)
326 FLAC__uint32 test = 1;
328 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
331 if(decoder_session->is_ogg) {
332 decoder_session->decoder.ogg.stream = OggFLAC__stream_decoder_new();
334 if(0 == decoder_session->decoder.ogg.stream) {
335 fprintf(stderr, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
339 if(!decode_options.use_first_serial_number)
340 OggFLAC__stream_decoder_set_serial_number(decoder_session->decoder.ogg.stream, decode_options.serial_number);
342 OggFLAC__stream_decoder_set_read_callback(decoder_session->decoder.ogg.stream, read_callback);
344 * The three ugly casts here are to 'downcast' the 'void *' argument of
345 * the callback down to 'FLAC__StreamDecoder *'. In C++ this would be
346 * unnecessary but here the cast makes the C compiler happy.
348 OggFLAC__stream_decoder_set_write_callback(decoder_session->decoder.ogg.stream, (FLAC__StreamDecoderWriteStatus (*)(const OggFLAC__StreamDecoder *, const FLAC__Frame *, const FLAC__int32 * const [], void *))write_callback);
349 OggFLAC__stream_decoder_set_metadata_callback(decoder_session->decoder.ogg.stream, (void (*)(const OggFLAC__StreamDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
350 OggFLAC__stream_decoder_set_error_callback(decoder_session->decoder.ogg.stream, (void (*)(const OggFLAC__StreamDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
351 OggFLAC__stream_decoder_set_client_data(decoder_session->decoder.ogg.stream, decoder_session);
353 if(OggFLAC__stream_decoder_init(decoder_session->decoder.ogg.stream) != OggFLAC__STREAM_DECODER_OK) {
354 print_error_with_state(decoder_session, "ERROR initializing decoder");
360 (void)decode_options;
363 decoder_session->decoder.flac.file = FLAC__file_decoder_new();
365 if(0 == decoder_session->decoder.flac.file) {
366 fprintf(stderr, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
370 FLAC__file_decoder_set_md5_checking(decoder_session->decoder.flac.file, true);
371 FLAC__file_decoder_set_filename(decoder_session->decoder.flac.file, infilename);
373 * The three ugly casts here are to 'downcast' the 'void *' argument of
374 * the callback down to 'FLAC__FileDecoder *'.
376 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);
377 FLAC__file_decoder_set_metadata_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
378 FLAC__file_decoder_set_error_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
379 FLAC__file_decoder_set_client_data(decoder_session->decoder.flac.file, decoder_session);
381 if(FLAC__file_decoder_init(decoder_session->decoder.flac.file) != FLAC__FILE_DECODER_OK) {
382 print_error_with_state(decoder_session, "ERROR initializing decoder");
390 FLAC__bool DecoderSession_process(DecoderSession *d)
394 if(!OggFLAC__stream_decoder_process_until_end_of_metadata(d->decoder.ogg.stream)) {
395 if(d->verbose) fprintf(stderr, "\n");
396 print_error_with_state(d, "ERROR while decoding metadata");
399 if(OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(d->decoder.ogg.stream) != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC && OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(d->decoder.ogg.stream) != FLAC__STREAM_DECODER_END_OF_STREAM) {
400 if(d->verbose) fprintf(stderr, "\n");
401 print_error_with_state(d, "ERROR during metadata decoding");
408 if(!FLAC__file_decoder_process_until_end_of_metadata(d->decoder.flac.file)) {
409 if(d->verbose) fprintf(stderr, "\n");
410 print_error_with_state(d, "ERROR while decoding metadata");
413 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) {
414 if(d->verbose) fprintf(stderr, "\n");
415 print_error_with_state(d, "ERROR during metadata decoding");
422 if(d->skip_specification->value.samples > 0) {
423 const FLAC__uint64 skip = (FLAC__uint64)d->skip_specification->value.samples;
426 if(d->is_ogg) { /*@@@ (move this check into main.c) */
427 fprintf(stderr, "%s: ERROR, can't skip when decoding Ogg-FLAC yet; convert to native-FLAC first\n", d->inbasefilename);
431 if(!FLAC__file_decoder_seek_absolute(d->decoder.flac.file, skip)) {
432 print_error_with_state(d, "ERROR seeking while skipping bytes");
435 if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file)) {
436 if(d->verbose) fprintf(stderr, "\n");
437 print_error_with_state(d, "ERROR while decoding frames");
440 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) {
441 if(d->verbose) fprintf(stderr, "\n");
442 print_error_with_state(d, "ERROR during decoding");
449 if(!OggFLAC__stream_decoder_process_until_end_of_stream(d->decoder.ogg.stream)) {
450 if(d->verbose) fprintf(stderr, "\n");
451 print_error_with_state(d, "ERROR while decoding data");
454 if(OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(d->decoder.ogg.stream) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA && OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(d->decoder.ogg.stream) != FLAC__STREAM_DECODER_END_OF_STREAM) {
455 if(d->verbose) fprintf(stderr, "\n");
456 print_error_with_state(d, "ERROR during decoding");
463 if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file)) {
464 if(d->verbose) fprintf(stderr, "\n");
465 print_error_with_state(d, "ERROR while decoding data");
468 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) {
469 if(d->verbose) fprintf(stderr, "\n");
470 print_error_with_state(d, "ERROR during decoding");
476 if(d->is_aiff_out && ((d->total_samples * d->channels * ((d->bps+7)/8)) & 1)) {
477 if(fwrite("\000", 1, 1, d->fout) != 1) {
478 print_error_with_state(d, "ERROR writing pad byte to AIFF SSND chunk");
486 int DecoderSession_finish_ok(DecoderSession *d)
488 FLAC__bool md5_failure = false;
492 if(d->decoder.ogg.stream) {
493 OggFLAC__stream_decoder_finish(d->decoder.ogg.stream);
496 OggFLAC__stream_decoder_delete(d->decoder.ogg.stream);
502 if(d->decoder.flac.file) {
503 md5_failure = !FLAC__file_decoder_finish(d->decoder.flac.file);
505 FLAC__file_decoder_delete(d->decoder.flac.file);
509 flac__analyze_finish(d->aopts);
511 fprintf(stderr, "\r%s: WARNING, MD5 signature mismatch\n", d->inbasefilename);
515 fprintf(stderr, "\r%s: %s \n", d->inbasefilename, d->test_only? "ok ":d->analysis_mode?"done ":"done");
517 DecoderSession_destroy(d, /*error_occurred=*/false);
518 if((d->is_wave_out || d->is_aiff_out) && d->wave_chunk_size_fixup.needs_fixup)
519 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))
524 int DecoderSession_finish_error(DecoderSession *d)
528 if(d->decoder.ogg.stream) {
529 OggFLAC__stream_decoder_finish(d->decoder.ogg.stream);
530 OggFLAC__stream_decoder_delete(d->decoder.ogg.stream);
536 if(d->decoder.flac.file) {
537 FLAC__file_decoder_finish(d->decoder.flac.file);
538 FLAC__file_decoder_delete(d->decoder.flac.file);
542 flac__analyze_finish(d->aopts);
543 DecoderSession_destroy(d, /*error_occurred=*/true);
547 FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input)
549 /* convert from mm:ss.sss to sample number if necessary */
550 flac__utils_canonicalize_skip_until_specification(spec, sample_rate);
552 /* special case: if "--until=-0", use the special value '0' to mean "end-of-stream" */
553 if(spec->is_relative && spec->value.samples == 0) {
554 spec->is_relative = false;
558 /* in any other case the total samples in the input must be known */
559 if(total_samples_in_input == 0) {
560 fprintf(stderr, "%s: ERROR, cannot use --until when FLAC metadata has total sample count of 0\n", inbasefilename);
564 FLAC__ASSERT(spec->value_is_samples);
566 /* convert relative specifications to absolute */
567 if(spec->is_relative) {
568 if(spec->value.samples <= 0)
569 spec->value.samples += (FLAC__int64)total_samples_in_input;
571 spec->value.samples += skip;
572 spec->is_relative = false;
576 if(spec->value.samples < 0) {
577 fprintf(stderr, "%s: ERROR, --until value is before beginning of input\n", inbasefilename);
580 if((FLAC__uint64)spec->value.samples <= skip) {
581 fprintf(stderr, "%s: ERROR, --until value is before --skip point\n", inbasefilename);
584 if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
585 fprintf(stderr, "%s: ERROR, --until value is after end of input\n", inbasefilename);
592 FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
594 FLAC__byte *b = (FLAC__byte*)(&val);
595 if(is_big_endian_host_) {
597 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
599 return fwrite(b, 1, 2, f) == 2;
602 FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
604 FLAC__byte *b = (FLAC__byte*)(&val);
605 if(is_big_endian_host_) {
607 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
608 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
610 return fwrite(b, 1, 4, f) == 4;
613 FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val)
615 FLAC__byte *b = (FLAC__byte*)(&val);
616 if(!is_big_endian_host_) {
618 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
620 return fwrite(b, 1, 2, f) == 2;
623 FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val)
625 FLAC__byte *b = (FLAC__byte*)(&val);
626 if(!is_big_endian_host_) {
628 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
629 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
631 return fwrite(b, 1, 4, f) == 4;
634 FLAC__bool write_sane_extended(FILE *f, unsigned val)
636 unsigned i, exponent;
638 /* this reasonable limitation make the implementation simpler */
639 FLAC__ASSERT(val < 0x80000000);
641 /* we'll use the denormalized form, with no implicit '1' (i bit == 0) */
643 for(i = val, exponent = 0; i; i >>= 1, exponent++)
645 if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent + 16383)))
648 for(i = 32; i; i--) {
653 if(!write_big_endian_uint32(f, val))
655 if(!write_big_endian_uint32(f, 0))
661 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)
663 const char *fmt_desc = (is_wave_out? "WAVE" : "AIFF");
664 FLAC__bool (*write_it)(FILE *, FLAC__uint32) = (is_wave_out? write_little_endian_uint32 : write_big_endian_uint32);
665 FILE *f = fopen(outfilename, "r+b");
666 FLAC__uint32 data_size, aligned_data_size;
669 fprintf(stderr, "ERROR, couldn't open file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
673 data_size = aligned_data_size = total_samples * channels * ((bps+7)/8);
674 if(!is_wave_out && (aligned_data_size & 1))
677 if(fseek(f, riff_offset, SEEK_SET) < 0) {
678 fprintf(stderr, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
682 if(!write_it(f, aligned_data_size + (is_wave_out? 36 : 46))) {
683 fprintf(stderr, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
688 if(fseek(f, frames_offset, SEEK_SET) < 0) {
689 fprintf(stderr, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
693 if(!write_it(f, total_samples)) {
694 fprintf(stderr, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
699 if(fseek(f, data_offset, SEEK_SET) < 0) {
700 fprintf(stderr, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
704 if(!write_it(f, data_size + (is_wave_out? 0 : 8))) {
705 fprintf(stderr, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
714 FLAC__StreamDecoderReadStatus read_callback(const OggFLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
716 DecoderSession *decoder_session = (DecoderSession*)client_data;
717 FILE *fin = decoder_session->fin;
720 (void)decoder; /* avoid compiler warning */
722 if (decoder_session->abort_flag)
723 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
726 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
728 bytes_read = fread(buffer, 1, *bytes, fin);
731 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
733 *bytes = (unsigned)bytes_read;
735 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
739 FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
741 DecoderSession *decoder_session = (DecoderSession*)client_data;
742 FILE *fout = decoder_session->fout;
743 const unsigned bps = frame->header.bits_per_sample, channels = frame->header.channels;
744 FLAC__bool is_big_endian = (decoder_session->is_aiff_out? true : (decoder_session->is_wave_out? false : decoder_session->is_big_endian));
745 FLAC__bool is_unsigned_samples = (decoder_session->is_aiff_out? false : (decoder_session->is_wave_out? bps<=8 : decoder_session->is_unsigned_samples));
746 unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
747 static FLAC__int8 s8buffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(FLAC__int32)]; /* WATCHOUT: can be up to 2 megs */
748 FLAC__uint8 *u8buffer = (FLAC__uint8 *)s8buffer;
749 FLAC__int16 *s16buffer = (FLAC__int16 *)s8buffer;
750 FLAC__uint16 *u16buffer = (FLAC__uint16 *)s8buffer;
751 FLAC__int32 *s32buffer = (FLAC__int32 *)s8buffer;
752 FLAC__uint32 *u32buffer = (FLAC__uint32 *)s8buffer;
756 if(decoder_session->abort_flag)
757 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
759 if(bps != decoder_session->bps) {
760 fprintf(stderr, "%s: ERROR, bits-per-sample is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, bps, decoder_session->bps);
761 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
763 if(channels != decoder_session->channels) {
764 fprintf(stderr, "%s: ERROR, channels is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, channels, decoder_session->channels);
765 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
767 if(frame->header.sample_rate != decoder_session->sample_rate) {
768 fprintf(stderr, "%s: ERROR, sample rate is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, frame->header.sample_rate, decoder_session->sample_rate);
769 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
773 * limit the number of samples to accept based on --until
775 FLAC__ASSERT(!decoder_session->skip_specification->is_relative);
776 FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
777 FLAC__ASSERT(!decoder_session->until_specification->is_relative);
778 FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
779 if(decoder_session->until_specification->value.samples > 0) {
780 const FLAC__uint64 skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
781 const FLAC__uint64 until = (FLAC__uint64)decoder_session->until_specification->value.samples;
782 const FLAC__uint64 input_samples_passed = skip + decoder_session->samples_processed;
783 FLAC__ASSERT(until >= input_samples_passed);
784 if(input_samples_passed + wide_samples > until)
785 wide_samples = until - input_samples_passed;
788 if(wide_samples > 0) {
789 decoder_session->samples_processed += wide_samples;
790 decoder_session->frame_counter++;
792 if(decoder_session->verbose && !(decoder_session->frame_counter & 0x3f))
793 print_stats(decoder_session);
795 if(decoder_session->analysis_mode) {
796 flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->aopts, fout);
798 else if(!decoder_session->test_only) {
800 if(is_unsigned_samples) {
801 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
802 for(channel = 0; channel < channels; channel++, sample++)
803 u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
806 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
807 for(channel = 0; channel < channels; channel++, sample++)
808 s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
810 if(fwrite(u8buffer, 1, sample, fout) != sample)
811 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
814 if(is_unsigned_samples) {
815 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
816 for(channel = 0; channel < channels; channel++, sample++)
817 u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
820 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
821 for(channel = 0; channel < channels; channel++, sample++)
822 s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
824 if(is_big_endian != is_big_endian_host_) {
826 const unsigned bytes = sample * 2;
827 for(byte = 0; byte < bytes; byte += 2) {
828 tmp = u8buffer[byte];
829 u8buffer[byte] = u8buffer[byte+1];
830 u8buffer[byte+1] = tmp;
833 if(fwrite(u16buffer, 2, sample, fout) != sample)
834 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
837 if(is_unsigned_samples) {
838 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
839 for(channel = 0; channel < channels; channel++, sample++)
840 u32buffer[sample] = buffer[channel][wide_sample] + 0x800000;
843 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
844 for(channel = 0; channel < channels; channel++, sample++)
845 s32buffer[sample] = buffer[channel][wide_sample];
847 if(is_big_endian != is_big_endian_host_) {
849 const unsigned bytes = sample * 4;
850 for(byte = 0; byte < bytes; byte += 4) {
851 tmp = u8buffer[byte];
852 u8buffer[byte] = u8buffer[byte+3];
853 u8buffer[byte+3] = tmp;
854 tmp = u8buffer[byte+1];
855 u8buffer[byte+1] = u8buffer[byte+2];
856 u8buffer[byte+2] = tmp;
861 const unsigned bytes = sample * 4;
862 for(lbyte = byte = 0; byte < bytes; ) {
864 u8buffer[lbyte++] = u8buffer[byte++];
865 u8buffer[lbyte++] = u8buffer[byte++];
866 u8buffer[lbyte++] = u8buffer[byte++];
871 const unsigned bytes = sample * 4;
872 for(lbyte = byte = 0; byte < bytes; ) {
873 u8buffer[lbyte++] = u8buffer[byte++];
874 u8buffer[lbyte++] = u8buffer[byte++];
875 u8buffer[lbyte++] = u8buffer[byte++];
879 if(fwrite(u8buffer, 3, sample, fout) != sample)
880 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
887 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
890 void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
892 DecoderSession *decoder_session = (DecoderSession*)client_data;
894 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
895 FLAC__uint64 skip, until;
896 decoder_session->bps = metadata->data.stream_info.bits_per_sample;
897 decoder_session->channels = metadata->data.stream_info.channels;
898 decoder_session->sample_rate = metadata->data.stream_info.sample_rate;
900 flac__utils_canonicalize_skip_until_specification(decoder_session->skip_specification, decoder_session->sample_rate);
901 FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
902 skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
904 /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
905 if(metadata->data.stream_info.total_samples > 0 && skip >= metadata->data.stream_info.total_samples) {
906 fprintf(stderr, "%s: ERROR trying to --skip more samples than in stream\n", decoder_session->inbasefilename);
907 decoder_session->abort_flag = true;
910 else if(metadata->data.stream_info.total_samples == 0 && skip > 0) {
911 fprintf(stderr, "%s: ERROR, can't --skip when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
912 decoder_session->abort_flag = true;
915 decoder_session->total_samples = metadata->data.stream_info.total_samples - skip;
917 /* note that we use metadata->data.stream_info.total_samples instead of decoder_session->total_samples */
918 if(!canonicalize_until_specification(decoder_session->until_specification, decoder_session->inbasefilename, decoder_session->sample_rate, skip, metadata->data.stream_info.total_samples)) {
919 decoder_session->abort_flag = true;
922 FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
923 until = (FLAC__uint64)decoder_session->until_specification->value.samples;
926 decoder_session->total_samples -= (metadata->data.stream_info.total_samples - until);
928 if(decoder_session->bps != 8 && decoder_session->bps != 16 && decoder_session->bps != 24) {
929 fprintf(stderr, "%s: ERROR: bits per sample is not 8/16/24\n", decoder_session->inbasefilename);
930 decoder_session->abort_flag = true;
934 /* write the WAVE/AIFF headers if necessary */
935 if(!decoder_session->analysis_mode && !decoder_session->test_only && (decoder_session->is_wave_out || decoder_session->is_aiff_out)) {
936 const char *fmt_desc = decoder_session->is_wave_out? "WAVE" : "AIFF";
937 FLAC__uint64 data_size = decoder_session->total_samples * decoder_session->channels * ((decoder_session->bps+7)/8);
938 if(decoder_session->total_samples == 0) {
939 if(decoder_session->fout == stdout) {
940 fprintf(stderr, "%s: WARNING, don't have accurate sample count available for %s header.\n", decoder_session->inbasefilename, fmt_desc);
941 fprintf(stderr, " Generated %s file will have a data chunk size of 0. Try\n", fmt_desc);
942 fprintf(stderr, " decoding directly to a file instead.\n");
945 decoder_session->wave_chunk_size_fixup.needs_fixup = true;
948 if(data_size >= 0xFFFFFFDC) {
949 fprintf(stderr, "%s: ERROR: stream is too big to fit in a single %s file chunk\n", decoder_session->inbasefilename, fmt_desc);
950 decoder_session->abort_flag = true;
953 if(decoder_session->is_wave_out) {
954 if(fwrite("RIFF", 1, 4, decoder_session->fout) != 4)
955 decoder_session->abort_flag = true;
957 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
958 decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
960 if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)(data_size+36))) /* filesize-8 */
961 decoder_session->abort_flag = true;
963 if(fwrite("WAVEfmt ", 1, 8, decoder_session->fout) != 8)
964 decoder_session->abort_flag = true;
966 if(fwrite("\020\000\000\000", 1, 4, decoder_session->fout) != 4) /* chunk size = 16 */
967 decoder_session->abort_flag = true;
969 if(fwrite("\001\000", 1, 2, decoder_session->fout) != 2) /* compression code == 1 */
970 decoder_session->abort_flag = true;
972 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
973 decoder_session->abort_flag = true;
975 if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate))
976 decoder_session->abort_flag = true;
978 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 ??? */
979 decoder_session->abort_flag = true;
981 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels * ((decoder_session->bps+7) / 8)))) /* block align */
982 decoder_session->abort_flag = true;
984 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps))) /* bits per sample */
985 decoder_session->abort_flag = true;
987 if(fwrite("data", 1, 4, decoder_session->fout) != 4)
988 decoder_session->abort_flag = true;
990 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
991 decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
993 if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size)) /* data size */
994 decoder_session->abort_flag = true;
997 const FLAC__uint32 aligned_data_size = (data_size+1) & (~1U);
999 if(fwrite("FORM", 1, 4, decoder_session->fout) != 4)
1000 decoder_session->abort_flag = true;
1002 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
1003 decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
1005 if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)(aligned_data_size+46))) /* filesize-8 */
1006 decoder_session->abort_flag = true;
1008 if(fwrite("AIFFCOMM", 1, 8, decoder_session->fout) != 8)
1009 decoder_session->abort_flag = true;
1011 if(fwrite("\000\000\000\022", 1, 4, decoder_session->fout) != 4) /* chunk size = 18 */
1012 decoder_session->abort_flag = true;
1014 if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
1015 decoder_session->abort_flag = true;
1017 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
1018 decoder_session->wave_chunk_size_fixup.frames_offset = ftell(decoder_session->fout);
1020 if(!write_big_endian_uint32(decoder_session->fout, decoder_session->total_samples))
1021 decoder_session->abort_flag = true;
1023 if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps)))
1024 decoder_session->abort_flag = true;
1026 if(!write_sane_extended(decoder_session->fout, decoder_session->sample_rate))
1027 decoder_session->abort_flag = true;
1029 if(fwrite("SSND", 1, 4, decoder_session->fout) != 4)
1030 decoder_session->abort_flag = true;
1032 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
1033 decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
1035 if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size+8)) /* data size */
1036 decoder_session->abort_flag = true;
1038 if(!write_big_endian_uint32(decoder_session->fout, 0/*offset*/))
1039 decoder_session->abort_flag = true;
1041 if(!write_big_endian_uint32(decoder_session->fout, 0/*block_size*/))
1042 decoder_session->abort_flag = true;
1048 void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
1050 DecoderSession *decoder_session = (DecoderSession*)client_data;
1052 fprintf(stderr, "%s: *** Got error code %d:%s\n", decoder_session->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
1053 if(!decoder_session->continue_through_decode_errors)
1054 decoder_session->abort_flag = true;
1057 void print_error_with_state(const DecoderSession *d, const char *message)
1059 const int ilen = strlen(d->inbasefilename) + 1;
1061 fprintf(stderr, "\n%s: %s\n", d->inbasefilename, message);
1063 #ifdef FLAC__HAS_OGG
1065 const OggFLAC__StreamDecoderState osd_state = OggFLAC__stream_decoder_get_state(d->decoder.ogg.stream);
1066 if(osd_state != OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR) {
1067 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)osd_state, OggFLAC__StreamDecoderStateString[osd_state]);
1070 const FLAC__StreamDecoderState fsd_state = OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(d->decoder.ogg.stream);
1071 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
1077 const FLAC__FileDecoderState ffd_state = FLAC__file_decoder_get_state(d->decoder.flac.file);
1078 if(ffd_state != FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
1079 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)ffd_state, FLAC__FileDecoderStateString[ffd_state]);
1082 const FLAC__SeekableStreamDecoderState fssd_state = FLAC__file_decoder_get_seekable_stream_decoder_state(d->decoder.flac.file);
1083 if(fssd_state != FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
1084 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fssd_state, FLAC__SeekableStreamDecoderStateString[fssd_state]);
1087 const FLAC__StreamDecoderState fsd_state = FLAC__file_decoder_get_stream_decoder_state(d->decoder.flac.file);
1088 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
1094 void print_stats(const DecoderSession *decoder_session)
1096 if(decoder_session->verbose) {
1097 #if defined _MSC_VER || defined __MINGW32__
1098 /* with VC++ you have to spoon feed it the casting */
1099 const double progress = (double)(FLAC__int64)decoder_session->samples_processed / (double)(FLAC__int64)decoder_session->total_samples * 100.0;
1101 const double progress = (double)decoder_session->samples_processed / (double)decoder_session->total_samples * 100.0;
1103 if(decoder_session->total_samples > 0) {
1104 fprintf(stderr, "\r%s: %s%u%% complete",
1105 decoder_session->inbasefilename,
1106 decoder_session->test_only? "testing, " : decoder_session->analysis_mode? "analyzing, " : "",
1107 (unsigned)floor(progress + 0.5)
1111 fprintf(stderr, "\r%s: %s %u samples",
1112 decoder_session->inbasefilename,
1113 decoder_session->test_only? "tested" : decoder_session->analysis_mode? "analyzed" : "wrote",
1114 (unsigned)decoder_session->samples_processed