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.
19 #if defined _WIN32 && !defined __CYGWIN__
20 /* where MSVC puts unlink() */
25 #include <math.h> /* for floor() */
26 #include <stdio.h> /* for FILE et al. */
27 #include <string.h> /* for strcmp() */
32 #include "OggFLAC/stream_decoder.h"
41 FLAC__bool is_wave_out;
42 FLAC__bool continue_through_decode_errors;
44 FLAC__bool analysis_mode;
45 analysis_options aopts;
48 const char *inbasefilename;
49 const char *outfilename;
51 FLAC__uint64 samples_processed;
52 unsigned frame_counter;
53 FLAC__bool abort_flag;
56 FLAC__bool needs_fixup;
59 } wave_chunk_size_fixup;
61 FLAC__bool is_big_endian;
62 FLAC__bool is_unsigned_samples;
63 FLAC__uint64 total_samples;
70 FLAC__FileDecoder *file;
74 OggFLAC__StreamDecoder *stream;
86 static FLAC__bool is_big_endian_host_;
92 static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool verbose, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, FLAC__bool analysis_mode, analysis_options aopts, FLAC__uint64 skip, const char *infilename, const char *outfilename);
93 static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
94 static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, decode_options_t decode_options, const char *infilename);
95 static FLAC__bool DecoderSession_process(DecoderSession *d);
96 static int DecoderSession_finish_ok(DecoderSession *d);
97 static int DecoderSession_finish_error(DecoderSession *d);
98 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
99 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
100 static FLAC__bool fixup_wave_chunk_size(const char *outfilename, unsigned riff_offset, unsigned data_offset, FLAC__uint32 data_size);
102 static FLAC__StreamDecoderReadStatus read_callback(const OggFLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
105 * We use 'void *' so that we can use the same callbacks for the
106 * FLAC__StreamDecoder and FLAC__FileDecoder. The 'decoder' argument is
107 * actually never used in the callbacks.
109 static FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
110 static void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
111 static void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
112 static void print_error_with_state(const DecoderSession *d, const char *message);
113 static void print_stats(const DecoderSession *decoder_session);
119 int flac__decode_wav(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
121 DecoderSession decoder_session;
124 DecoderSession_construct(
127 options.common.is_ogg,
131 options.common.verbose,
132 /*is_wave_out=*/true,
133 options.common.continue_through_decode_errors,
143 if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
144 return DecoderSession_finish_error(&decoder_session);
146 if(!DecoderSession_process(&decoder_session))
147 return DecoderSession_finish_error(&decoder_session);
149 return DecoderSession_finish_ok(&decoder_session);
152 int flac__decode_raw(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, raw_decode_options_t options)
154 DecoderSession decoder_session;
156 decoder_session.is_big_endian = options.is_big_endian;
157 decoder_session.is_unsigned_samples = options.is_unsigned_samples;
160 DecoderSession_construct(
163 options.common.is_ogg,
167 options.common.verbose,
168 /*is_wave_out=*/false,
169 options.common.continue_through_decode_errors,
179 if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
180 return DecoderSession_finish_error(&decoder_session);
182 if(!DecoderSession_process(&decoder_session))
183 return DecoderSession_finish_error(&decoder_session);
185 return DecoderSession_finish_ok(&decoder_session);
188 FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool verbose, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, FLAC__bool analysis_mode, analysis_options aopts, FLAC__uint64 skip, const char *infilename, const char *outfilename)
196 d->verbose = verbose;
197 d->is_wave_out = is_wave_out;
198 d->continue_through_decode_errors = continue_through_decode_errors;
199 d->test_only = (0 == outfilename);
200 d->analysis_mode = analysis_mode;
204 d->inbasefilename = flac__file_get_basename(infilename);
205 d->outfilename = outfilename;
207 d->samples_processed = 0;
208 d->frame_counter = 0;
209 d->abort_flag = false;
211 d->wave_chunk_size_fixup.needs_fixup = false;
213 d->decoder.flac.file = 0;
215 d->decoder.ogg.stream = 0;
221 d->fout = 0; /* initialized with an open file later if necessary */
223 FLAC__ASSERT(!(d->test_only && d->analysis_mode));
226 if(0 == strcmp(outfilename, "-")) {
227 d->fout = file__get_binary_stdout();
230 if(0 == (d->fout = fopen(outfilename, "wb"))) {
231 fprintf(stderr, "%s: ERROR: can't open output file %s\n", d->inbasefilename, outfilename);
232 DecoderSession_destroy(d, /*error_occurred=*/true);
240 if (0 == strcmp(infilename, "-")) {
241 d->fin = file__get_binary_stdin();
243 if (0 == (d->fin = fopen(infilename, "rb"))) {
244 fprintf(stderr, "%s: ERROR: can't open input file %s\n", d->inbasefilename, infilename);
245 DecoderSession_destroy(d, /*error_occurred=*/true);
253 flac__analyze_init(aopts);
258 void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
260 if(0 != d->fout && d->fout != stdout) {
263 unlink(d->outfilename);
266 if(0 != d->fin && d->fin != stdin)
271 FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, decode_options_t decode_options, const char *infilename)
273 FLAC__uint32 test = 1;
275 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
278 if(decoder_session->is_ogg) {
279 decoder_session->decoder.ogg.stream = OggFLAC__stream_decoder_new();
281 if(0 == decoder_session->decoder.ogg.stream) {
282 fprintf(stderr, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
286 if(!decode_options.use_first_serial_number)
287 OggFLAC__stream_decoder_set_serial_number(decoder_session->decoder.ogg.stream, decode_options.serial_number);
289 OggFLAC__stream_decoder_set_read_callback(decoder_session->decoder.ogg.stream, read_callback);
291 * The three ugly casts here are to 'downcast' the 'void *' argument of
292 * the callback down to 'FLAC__StreamDecoder *'. In C++ this would be
293 * unnecessary but here the cast makes the C compiler happy.
295 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);
296 OggFLAC__stream_decoder_set_metadata_callback(decoder_session->decoder.ogg.stream, (void (*)(const OggFLAC__StreamDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
297 OggFLAC__stream_decoder_set_error_callback(decoder_session->decoder.ogg.stream, (void (*)(const OggFLAC__StreamDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
298 OggFLAC__stream_decoder_set_client_data(decoder_session->decoder.ogg.stream, decoder_session);
300 if(OggFLAC__stream_decoder_init(decoder_session->decoder.ogg.stream) != OggFLAC__STREAM_DECODER_OK) {
301 print_error_with_state(decoder_session, "ERROR initializing decoder");
307 (void)decode_options;
310 decoder_session->decoder.flac.file = FLAC__file_decoder_new();
312 if(0 == decoder_session->decoder.flac.file) {
313 fprintf(stderr, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
317 FLAC__file_decoder_set_md5_checking(decoder_session->decoder.flac.file, true);
318 FLAC__file_decoder_set_filename(decoder_session->decoder.flac.file, infilename);
320 * The three ugly casts here are to 'downcast' the 'void *' argument of
321 * the callback down to 'FLAC__FileDecoder *'.
323 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);
324 FLAC__file_decoder_set_metadata_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
325 FLAC__file_decoder_set_error_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
326 FLAC__file_decoder_set_client_data(decoder_session->decoder.flac.file, decoder_session);
328 if(FLAC__file_decoder_init(decoder_session->decoder.flac.file) != FLAC__FILE_DECODER_OK) {
329 print_error_with_state(decoder_session, "ERROR initializing decoder");
337 FLAC__bool DecoderSession_process(DecoderSession *d)
341 if(d->is_ogg) { /*@@@ (move this check into main.c) */
342 fprintf(stderr, "%s: ERROR, can't skip when decoding Ogg-FLAC yet; convert to native-FLAC first\n", d->inbasefilename);
346 if(!FLAC__file_decoder_process_until_end_of_metadata(d->decoder.flac.file)) {
347 print_error_with_state(d, "ERROR while decoding metadata");
352 if(!FLAC__file_decoder_seek_absolute(d->decoder.flac.file, d->skip)) {
353 print_error_with_state(d, "ERROR seeking while skipping bytes");
356 if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file)) {
357 if(d->verbose) fprintf(stderr, "\n");
358 print_error_with_state(d, "ERROR while decoding frames");
361 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) {
362 if(d->verbose) fprintf(stderr, "\n");
363 print_error_with_state(d, "ERROR during decoding");
370 if(!OggFLAC__stream_decoder_process_until_end_of_stream(d->decoder.ogg.stream)) {
371 if(d->verbose) fprintf(stderr, "\n");
372 print_error_with_state(d, "ERROR while decoding data");
375 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) {
376 if(d->verbose) fprintf(stderr, "\n");
377 print_error_with_state(d, "ERROR during decoding");
384 if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file)) {
385 if(d->verbose) fprintf(stderr, "\n");
386 print_error_with_state(d, "ERROR while decoding data");
389 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) {
390 if(d->verbose) fprintf(stderr, "\n");
391 print_error_with_state(d, "ERROR during decoding");
400 int DecoderSession_finish_ok(DecoderSession *d)
402 FLAC__bool md5_failure = false;
406 if(d->decoder.ogg.stream) {
407 OggFLAC__stream_decoder_finish(d->decoder.ogg.stream);
410 OggFLAC__stream_decoder_delete(d->decoder.ogg.stream);
416 if(d->decoder.flac.file) {
417 md5_failure = !FLAC__file_decoder_finish(d->decoder.flac.file);
419 FLAC__file_decoder_delete(d->decoder.flac.file);
423 flac__analyze_finish(d->aopts);
425 fprintf(stderr, "\r%s: WARNING, MD5 signature mismatch\n", d->inbasefilename);
429 fprintf(stderr, "\r%s: %s \n", d->inbasefilename, d->test_only? "ok ":d->analysis_mode?"done ":"done");
431 DecoderSession_destroy(d, /*error_occurred=*/false);
432 if(d->is_wave_out && d->wave_chunk_size_fixup.needs_fixup)
433 if(!fixup_wave_chunk_size(d->outfilename, d->wave_chunk_size_fixup.riff_offset, d->wave_chunk_size_fixup.data_offset, (FLAC__uint32)d->samples_processed))
438 int DecoderSession_finish_error(DecoderSession *d)
442 if(d->decoder.ogg.stream) {
443 OggFLAC__stream_decoder_finish(d->decoder.ogg.stream);
444 OggFLAC__stream_decoder_delete(d->decoder.ogg.stream);
450 if(d->decoder.flac.file) {
451 FLAC__file_decoder_finish(d->decoder.flac.file);
452 FLAC__file_decoder_delete(d->decoder.flac.file);
456 flac__analyze_finish(d->aopts);
457 DecoderSession_destroy(d, /*error_occurred=*/true);
461 FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
463 FLAC__byte *b = (FLAC__byte*)(&val);
464 if(is_big_endian_host_) {
466 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
468 return fwrite(b, 1, 2, f) == 2;
471 FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
473 FLAC__byte *b = (FLAC__byte*)(&val);
474 if(is_big_endian_host_) {
476 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
477 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
479 return fwrite(b, 1, 4, f) == 4;
482 FLAC__bool fixup_wave_chunk_size(const char *outfilename, unsigned riff_offset, unsigned data_offset, FLAC__uint32 data_size)
484 FILE *f = fopen(outfilename, "r+b");
486 fprintf(stderr, "ERROR, couldn't open file %s while fixing up WAVE chunk size\n", outfilename);
489 if(fseek(f, riff_offset, SEEK_SET) < 0) {
490 fprintf(stderr, "ERROR, couldn't seek in file %s while fixing up WAVE chunk size\n", outfilename);
494 if(!write_little_endian_uint32(f, data_size + 36)) {
495 fprintf(stderr, "ERROR, couldn't write size in file %s while fixing up WAVE chunk size\n", outfilename);
499 if(fseek(f, data_offset, SEEK_SET) < 0) {
500 fprintf(stderr, "ERROR, couldn't seek in file %s while fixing up WAVE chunk size\n", outfilename);
504 if(!write_little_endian_uint32(f, data_size)) {
505 fprintf(stderr, "ERROR, couldn't write size in file %s while fixing up WAVE chunk size\n", outfilename);
514 FLAC__StreamDecoderReadStatus read_callback(const OggFLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
516 DecoderSession *decoder_session = (DecoderSession*)client_data;
517 FILE *fin = decoder_session->fin;
520 (void)decoder; /* avoid compiler warning */
522 if (decoder_session->abort_flag)
523 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
526 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
528 bytes_read = fread(buffer, 1, *bytes, fin);
531 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
533 *bytes = (unsigned)bytes_read;
535 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
539 FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
541 DecoderSession *decoder_session = (DecoderSession*)client_data;
542 FILE *fout = decoder_session->fout;
543 const unsigned bps = frame->header.bits_per_sample, channels = frame->header.channels;
544 FLAC__bool is_big_endian = (decoder_session->is_wave_out? false : decoder_session->is_big_endian);
545 FLAC__bool is_unsigned_samples = (decoder_session->is_wave_out? bps<=8 : decoder_session->is_unsigned_samples);
546 unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
547 static FLAC__int8 s8buffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(FLAC__int32)]; /* WATCHOUT: can be up to 2 megs */
548 FLAC__uint8 *u8buffer = (FLAC__uint8 *)s8buffer;
549 FLAC__int16 *s16buffer = (FLAC__int16 *)s8buffer;
550 FLAC__uint16 *u16buffer = (FLAC__uint16 *)s8buffer;
551 FLAC__int32 *s32buffer = (FLAC__int32 *)s8buffer;
552 FLAC__uint32 *u32buffer = (FLAC__uint32 *)s8buffer;
556 if(decoder_session->abort_flag)
557 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
559 if(bps != decoder_session->bps) {
560 fprintf(stderr, "ERROR, bits-per-sample is %u in frame but %u in STREAMINFO\n", bps, decoder_session->bps);
561 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
563 if(channels != decoder_session->channels) {
564 fprintf(stderr, "ERROR, channels is %u in frame but %u in STREAMINFO\n", channels, decoder_session->channels);
565 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
567 if(frame->header.sample_rate != decoder_session->sample_rate) {
568 fprintf(stderr, "ERROR, sample rate is %u in frame but %u in STREAMINFO\n", frame->header.sample_rate, decoder_session->sample_rate);
569 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
572 decoder_session->samples_processed += wide_samples;
573 decoder_session->frame_counter++;
575 if(decoder_session->verbose && !(decoder_session->frame_counter & 0x3f))
576 print_stats(decoder_session);
578 if(decoder_session->analysis_mode) {
579 flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->aopts, fout);
581 else if(!decoder_session->test_only) {
583 if(is_unsigned_samples) {
584 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
585 for(channel = 0; channel < channels; channel++, sample++)
586 u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
589 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
590 for(channel = 0; channel < channels; channel++, sample++)
591 s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
593 if(fwrite(u8buffer, 1, sample, fout) != sample)
594 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
597 if(is_unsigned_samples) {
598 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
599 for(channel = 0; channel < channels; channel++, sample++)
600 u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
603 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
604 for(channel = 0; channel < channels; channel++, sample++)
605 s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
607 if(is_big_endian != is_big_endian_host_) {
609 const unsigned bytes = sample * 2;
610 for(byte = 0; byte < bytes; byte += 2) {
611 tmp = u8buffer[byte];
612 u8buffer[byte] = u8buffer[byte+1];
613 u8buffer[byte+1] = tmp;
616 if(fwrite(u16buffer, 2, sample, fout) != sample)
617 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
620 if(is_unsigned_samples) {
621 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
622 for(channel = 0; channel < channels; channel++, sample++)
623 u32buffer[sample] = buffer[channel][wide_sample] + 0x800000;
626 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
627 for(channel = 0; channel < channels; channel++, sample++)
628 s32buffer[sample] = buffer[channel][wide_sample];
630 if(is_big_endian != is_big_endian_host_) {
632 const unsigned bytes = sample * 4;
633 for(byte = 0; byte < bytes; byte += 4) {
634 tmp = u8buffer[byte];
635 u8buffer[byte] = u8buffer[byte+3];
636 u8buffer[byte+3] = tmp;
637 tmp = u8buffer[byte+1];
638 u8buffer[byte+1] = u8buffer[byte+2];
639 u8buffer[byte+2] = tmp;
644 const unsigned bytes = sample * 4;
645 for(lbyte = byte = 0; byte < bytes; ) {
647 u8buffer[lbyte++] = u8buffer[byte++];
648 u8buffer[lbyte++] = u8buffer[byte++];
649 u8buffer[lbyte++] = u8buffer[byte++];
654 const unsigned bytes = sample * 4;
655 for(lbyte = byte = 0; byte < bytes; ) {
656 u8buffer[lbyte++] = u8buffer[byte++];
657 u8buffer[lbyte++] = u8buffer[byte++];
658 u8buffer[lbyte++] = u8buffer[byte++];
662 if(fwrite(u8buffer, 3, sample, fout) != sample)
663 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
669 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
672 void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
674 DecoderSession *decoder_session = (DecoderSession*)client_data;
676 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
677 /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
678 if(metadata->data.stream_info.total_samples > 0 && decoder_session->skip >= metadata->data.stream_info.total_samples) {
679 fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", decoder_session->inbasefilename);
680 decoder_session->abort_flag = true;
683 else if(metadata->data.stream_info.total_samples == 0 && decoder_session->skip > 0) {
684 fprintf(stderr, "%s: ERROR, can't skip when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
685 decoder_session->abort_flag = true;
689 decoder_session->total_samples = metadata->data.stream_info.total_samples - decoder_session->skip;
690 decoder_session->bps = metadata->data.stream_info.bits_per_sample;
691 decoder_session->channels = metadata->data.stream_info.channels;
692 decoder_session->sample_rate = metadata->data.stream_info.sample_rate;
694 if(decoder_session->bps != 8 && decoder_session->bps != 16 && decoder_session->bps != 24) {
695 fprintf(stderr, "%s: ERROR: bits per sample is not 8/16/24\n", decoder_session->inbasefilename);
696 decoder_session->abort_flag = true;
700 /* write the WAVE headers if necessary */
701 if(!decoder_session->analysis_mode && !decoder_session->test_only && decoder_session->is_wave_out) {
702 FLAC__uint64 data_size = decoder_session->total_samples * decoder_session->channels * ((decoder_session->bps+7)/8);
703 if(decoder_session->total_samples == 0) {
704 if(decoder_session->fout == stdout) {
705 fprintf(stderr, "%s: WARNING, don't have accurate sample count available for WAVE header.\n", decoder_session->inbasefilename);
706 fprintf(stderr, " Generated WAVE file will have a data chunk size of 0. Try\n");
707 fprintf(stderr, " decoding directly to a file instead.\n");
710 decoder_session->wave_chunk_size_fixup.needs_fixup = true;
713 if(data_size >= 0xFFFFFFDC) {
714 fprintf(stderr, "%s: ERROR: stream is too big to fit in a single WAVE file chunk\n", decoder_session->inbasefilename);
715 decoder_session->abort_flag = true;
718 if(fwrite("RIFF", 1, 4, decoder_session->fout) != 4) decoder_session->abort_flag = true;
719 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
720 decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
721 if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)(data_size+36))) decoder_session->abort_flag = true; /* filesize-8 */
722 if(fwrite("WAVEfmt ", 1, 8, decoder_session->fout) != 8) decoder_session->abort_flag = true;
723 if(fwrite("\020\000\000\000", 1, 4, decoder_session->fout) != 4) decoder_session->abort_flag = true; /* chunk size = 16 */
724 if(fwrite("\001\000", 1, 2, decoder_session->fout) != 2) decoder_session->abort_flag = true; /* compression code == 1 */
725 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels))) decoder_session->abort_flag = true;
726 if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate)) decoder_session->abort_flag = true;
727 if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate * decoder_session->channels * ((decoder_session->bps+7) / 8))) decoder_session->abort_flag = true; /* @@@ or is it (sample_rate*channels*bps) / 8 ??? */
728 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels * ((decoder_session->bps+7) / 8)))) decoder_session->abort_flag = true; /* block align */
729 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps))) decoder_session->abort_flag = true; /* bits per sample */
730 if(fwrite("data", 1, 4, decoder_session->fout) != 4) decoder_session->abort_flag = true;
731 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
732 decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
733 if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size)) decoder_session->abort_flag = true; /* data size */
738 void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
740 DecoderSession *decoder_session = (DecoderSession*)client_data;
742 fprintf(stderr, "%s: *** Got error code %d:%s\n", decoder_session->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
743 if(!decoder_session->continue_through_decode_errors)
744 decoder_session->abort_flag = true;
747 void print_error_with_state(const DecoderSession *d, const char *message)
749 const int ilen = strlen(d->inbasefilename) + 1;
751 fprintf(stderr, "\n%s: %s\n", d->inbasefilename, message);
755 const OggFLAC__StreamDecoderState osd_state = OggFLAC__stream_decoder_get_state(d->decoder.ogg.stream);
756 if(osd_state != OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR) {
757 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)osd_state, OggFLAC__StreamDecoderStateString[osd_state]);
760 const FLAC__StreamDecoderState fsd_state = OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(d->decoder.ogg.stream);
761 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
767 const FLAC__FileDecoderState ffd_state = FLAC__file_decoder_get_state(d->decoder.flac.file);
768 if(ffd_state != FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
769 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)ffd_state, FLAC__FileDecoderStateString[ffd_state]);
772 const FLAC__SeekableStreamDecoderState fssd_state = FLAC__file_decoder_get_seekable_stream_decoder_state(d->decoder.flac.file);
773 if(fssd_state != FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
774 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fssd_state, FLAC__SeekableStreamDecoderStateString[fssd_state]);
777 const FLAC__StreamDecoderState fsd_state = FLAC__file_decoder_get_stream_decoder_state(d->decoder.flac.file);
778 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
784 void print_stats(const DecoderSession *decoder_session)
786 if(decoder_session->verbose) {
787 #if defined _MSC_VER || defined __MINGW32__
788 /* with VC++ you have to spoon feed it the casting */
789 const double progress = (double)(FLAC__int64)decoder_session->samples_processed / (double)(FLAC__int64)decoder_session->total_samples * 100.0;
791 const double progress = (double)decoder_session->samples_processed / (double)decoder_session->total_samples * 100.0;
793 if(decoder_session->total_samples > 0) {
794 fprintf(stderr, "\r%s: %s%u%% complete",
795 decoder_session->inbasefilename,
796 decoder_session->test_only? "testing, " : decoder_session->analysis_mode? "analyzing, " : "",
797 (unsigned)floor(progress + 0.5)
801 fprintf(stderr, "\r%s: %s %u samples",
802 decoder_session->inbasefilename,
803 decoder_session->test_only? "tested" : decoder_session->analysis_mode? "analyzed" : "wrote",
804 (unsigned)decoder_session->samples_processed