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, 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;
123 if(!DecoderSession_construct(&decoder_session, options.common.is_ogg, options.common.verbose, /*is_wave_out=*/true, options.common.continue_through_decode_errors, analysis_mode, aopts, options.common.skip, infilename, outfilename))
126 if(!DecoderSession_init_decoder(&decoder_session, infilename))
127 return DecoderSession_finish_error(&decoder_session);
129 if(!DecoderSession_process(&decoder_session))
130 return DecoderSession_finish_error(&decoder_session);
132 return DecoderSession_finish_ok(&decoder_session);
135 int flac__decode_raw(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, raw_decode_options_t options)
137 DecoderSession decoder_session;
139 decoder_session.is_big_endian = options.is_big_endian;
140 decoder_session.is_unsigned_samples = options.is_unsigned_samples;
142 if(!DecoderSession_construct(&decoder_session, options.common.is_ogg, options.common.verbose, /*is_wave_out=*/false, options.common.continue_through_decode_errors, analysis_mode, aopts, options.common.skip, infilename, outfilename))
145 if(!DecoderSession_init_decoder(&decoder_session, infilename))
146 return DecoderSession_finish_error(&decoder_session);
148 if(!DecoderSession_process(&decoder_session))
149 return DecoderSession_finish_error(&decoder_session);
151 return DecoderSession_finish_ok(&decoder_session);
154 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)
160 d->verbose = verbose;
161 d->is_wave_out = is_wave_out;
162 d->continue_through_decode_errors = continue_through_decode_errors;
163 d->test_only = (0 == outfilename);
164 d->analysis_mode = analysis_mode;
168 d->inbasefilename = flac__file_get_basename(infilename);
169 d->outfilename = outfilename;
171 d->samples_processed = 0;
172 d->frame_counter = 0;
173 d->abort_flag = false;
175 d->wave_chunk_size_fixup.needs_fixup = false;
177 d->decoder.flac.file = 0;
179 d->decoder.ogg.stream = 0;
185 d->fout = 0; /* initialized with an open file later if necessary */
187 FLAC__ASSERT(!(d->test_only && d->analysis_mode));
190 if(0 == strcmp(outfilename, "-")) {
191 d->fout = file__get_binary_stdout();
194 if(0 == (d->fout = fopen(outfilename, "wb"))) {
195 fprintf(stderr, "%s: ERROR: can't open output file %s\n", d->inbasefilename, outfilename);
196 DecoderSession_destroy(d, /*error_occurred=*/true);
204 if (0 == strcmp(infilename, "-")) {
205 d->fin = file__get_binary_stdin();
207 if (0 == (d->fin = fopen(infilename, "rb"))) {
208 fprintf(stderr, "%s: ERROR: can't open input file %s\n", d->inbasefilename, infilename);
209 DecoderSession_destroy(d, /*error_occurred=*/true);
217 flac__analyze_init(aopts);
222 void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
224 if(0 != d->fout && d->fout != stdout) {
227 unlink(d->outfilename);
230 if(0 != d->fin && d->fin != stdin)
235 FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, const char *infilename)
237 FLAC__uint32 test = 1;
239 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
242 if(decoder_session->is_ogg) {
243 decoder_session->decoder.ogg.stream = OggFLAC__stream_decoder_new();
245 if(0 == decoder_session->decoder.ogg.stream) {
246 fprintf(stderr, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
250 OggFLAC__stream_decoder_set_read_callback(decoder_session->decoder.ogg.stream, read_callback);
252 * The three ugly casts here are to 'downcast' the 'void *' argument of
253 * the callback down to 'FLAC__StreamDecoder *'. In C++ this would be
254 * unnecessary but here the cast makes the C compiler happy.
256 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);
257 OggFLAC__stream_decoder_set_metadata_callback(decoder_session->decoder.ogg.stream, (void (*)(const OggFLAC__StreamDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
258 OggFLAC__stream_decoder_set_error_callback(decoder_session->decoder.ogg.stream, (void (*)(const OggFLAC__StreamDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
259 OggFLAC__stream_decoder_set_client_data(decoder_session->decoder.ogg.stream, decoder_session);
261 if(OggFLAC__stream_decoder_init(decoder_session->decoder.ogg.stream) != OggFLAC__STREAM_DECODER_OK) {
262 print_error_with_state(decoder_session, "ERROR initializing decoder");
269 decoder_session->decoder.flac.file = FLAC__file_decoder_new();
271 if(0 == decoder_session->decoder.flac.file) {
272 fprintf(stderr, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
276 FLAC__file_decoder_set_md5_checking(decoder_session->decoder.flac.file, true);
277 FLAC__file_decoder_set_filename(decoder_session->decoder.flac.file, infilename);
279 * The three ugly casts here are to 'downcast' the 'void *' argument of
280 * the callback down to 'FLAC__FileDecoder *'.
282 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);
283 FLAC__file_decoder_set_metadata_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
284 FLAC__file_decoder_set_error_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
285 FLAC__file_decoder_set_client_data(decoder_session->decoder.flac.file, decoder_session);
287 if(FLAC__file_decoder_init(decoder_session->decoder.flac.file) != FLAC__FILE_DECODER_OK) {
288 print_error_with_state(decoder_session, "ERROR initializing decoder");
296 FLAC__bool DecoderSession_process(DecoderSession *d)
300 if(d->is_ogg) { /*@@@ (move this check into main.c) */
301 fprintf(stderr, "%s: ERROR, can't skip when decoding Ogg-FLAC yet; convert to native-FLAC first\n", d->inbasefilename);
305 if(!FLAC__file_decoder_process_until_end_of_metadata(d->decoder.flac.file)) {
306 print_error_with_state(d, "ERROR while decoding metadata");
311 if(!FLAC__file_decoder_seek_absolute(d->decoder.flac.file, d->skip)) {
312 print_error_with_state(d, "ERROR seeking while skipping bytes");
315 if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file)) {
316 if(d->verbose) fprintf(stderr, "\n");
317 print_error_with_state(d, "ERROR while decoding frames");
320 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) {
321 if(d->verbose) fprintf(stderr, "\n");
322 print_error_with_state(d, "ERROR during decoding");
329 if(!OggFLAC__stream_decoder_process_until_end_of_stream(d->decoder.ogg.stream)) {
330 if(d->verbose) fprintf(stderr, "\n");
331 print_error_with_state(d, "ERROR while decoding data");
334 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) {
335 if(d->verbose) fprintf(stderr, "\n");
336 print_error_with_state(d, "ERROR during decoding");
343 if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file)) {
344 if(d->verbose) fprintf(stderr, "\n");
345 print_error_with_state(d, "ERROR while decoding data");
348 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) {
349 if(d->verbose) fprintf(stderr, "\n");
350 print_error_with_state(d, "ERROR during decoding");
359 int DecoderSession_finish_ok(DecoderSession *d)
361 FLAC__bool md5_failure = false;
365 if(d->decoder.ogg.stream) {
366 OggFLAC__stream_decoder_finish(d->decoder.ogg.stream);
369 OggFLAC__stream_decoder_delete(d->decoder.ogg.stream);
375 if(d->decoder.flac.file) {
376 md5_failure = !FLAC__file_decoder_finish(d->decoder.flac.file);
378 FLAC__file_decoder_delete(d->decoder.flac.file);
382 flac__analyze_finish(d->aopts);
384 fprintf(stderr, "\r%s: WARNING, MD5 signature mismatch\n", d->inbasefilename);
388 fprintf(stderr, "\r%s: %s \n", d->inbasefilename, d->test_only? "ok ":d->analysis_mode?"done ":"done");
390 DecoderSession_destroy(d, /*error_occurred=*/false);
391 if(d->is_wave_out && d->wave_chunk_size_fixup.needs_fixup)
392 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))
397 int DecoderSession_finish_error(DecoderSession *d)
401 if(d->decoder.ogg.stream) {
402 OggFLAC__stream_decoder_finish(d->decoder.ogg.stream);
403 OggFLAC__stream_decoder_delete(d->decoder.ogg.stream);
409 if(d->decoder.flac.file) {
410 FLAC__file_decoder_finish(d->decoder.flac.file);
411 FLAC__file_decoder_delete(d->decoder.flac.file);
415 flac__analyze_finish(d->aopts);
416 DecoderSession_destroy(d, /*error_occurred=*/true);
420 FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
422 FLAC__byte *b = (FLAC__byte*)(&val);
423 if(is_big_endian_host_) {
425 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
427 return fwrite(b, 1, 2, f) == 2;
430 FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
432 FLAC__byte *b = (FLAC__byte*)(&val);
433 if(is_big_endian_host_) {
435 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
436 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
438 return fwrite(b, 1, 4, f) == 4;
441 FLAC__bool fixup_wave_chunk_size(const char *outfilename, unsigned riff_offset, unsigned data_offset, FLAC__uint32 data_size)
443 FILE *f = fopen(outfilename, "r+b");
445 fprintf(stderr, "ERROR, couldn't open file %s while fixing up WAVE chunk size\n", outfilename);
448 if(fseek(f, riff_offset, SEEK_SET) < 0) {
449 fprintf(stderr, "ERROR, couldn't seek in file %s while fixing up WAVE chunk size\n", outfilename);
453 if(!write_little_endian_uint32(f, data_size + 36)) {
454 fprintf(stderr, "ERROR, couldn't write size in file %s while fixing up WAVE chunk size\n", outfilename);
458 if(fseek(f, data_offset, SEEK_SET) < 0) {
459 fprintf(stderr, "ERROR, couldn't seek in file %s while fixing up WAVE chunk size\n", outfilename);
463 if(!write_little_endian_uint32(f, data_size)) {
464 fprintf(stderr, "ERROR, couldn't write size in file %s while fixing up WAVE chunk size\n", outfilename);
473 FLAC__StreamDecoderReadStatus read_callback(const OggFLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
475 DecoderSession *decoder_session = (DecoderSession*)client_data;
476 FILE *fin = decoder_session->fin;
479 (void)decoder; /* avoid compiler warning */
481 if (decoder_session->abort_flag)
482 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
485 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
487 bytes_read = fread(buffer, 1, *bytes, fin);
490 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
492 *bytes = (unsigned)bytes_read;
494 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
498 FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
500 DecoderSession *decoder_session = (DecoderSession*)client_data;
501 FILE *fout = decoder_session->fout;
502 unsigned bps = decoder_session->bps, channels = decoder_session->channels;
503 FLAC__bool is_big_endian = (decoder_session->is_wave_out? false : decoder_session->is_big_endian);
504 FLAC__bool is_unsigned_samples = (decoder_session->is_wave_out? bps<=8 : decoder_session->is_unsigned_samples);
505 unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
506 static FLAC__int8 s8buffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(FLAC__int32)]; /* WATCHOUT: can be up to 2 megs */
507 FLAC__uint8 *u8buffer = (FLAC__uint8 *)s8buffer;
508 FLAC__int16 *s16buffer = (FLAC__int16 *)s8buffer;
509 FLAC__uint16 *u16buffer = (FLAC__uint16 *)s8buffer;
510 FLAC__int32 *s32buffer = (FLAC__int32 *)s8buffer;
511 FLAC__uint32 *u32buffer = (FLAC__uint32 *)s8buffer;
515 if(decoder_session->abort_flag)
516 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
518 decoder_session->samples_processed += wide_samples;
519 decoder_session->frame_counter++;
521 if(decoder_session->verbose && !(decoder_session->frame_counter & 0x3f))
522 print_stats(decoder_session);
524 if(decoder_session->analysis_mode) {
525 flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->aopts, fout);
527 else if(!decoder_session->test_only) {
529 if(is_unsigned_samples) {
530 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
531 for(channel = 0; channel < channels; channel++, sample++)
532 u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
535 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
536 for(channel = 0; channel < channels; channel++, sample++)
537 s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
539 if(fwrite(u8buffer, 1, sample, fout) != sample)
540 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
543 if(is_unsigned_samples) {
544 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
545 for(channel = 0; channel < channels; channel++, sample++)
546 u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
549 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
550 for(channel = 0; channel < channels; channel++, sample++)
551 s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
553 if(is_big_endian != is_big_endian_host_) {
555 const unsigned bytes = sample * 2;
556 for(byte = 0; byte < bytes; byte += 2) {
557 tmp = u8buffer[byte];
558 u8buffer[byte] = u8buffer[byte+1];
559 u8buffer[byte+1] = tmp;
562 if(fwrite(u16buffer, 2, sample, fout) != sample)
563 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
566 if(is_unsigned_samples) {
567 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
568 for(channel = 0; channel < channels; channel++, sample++)
569 u32buffer[sample] = buffer[channel][wide_sample] + 0x800000;
572 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
573 for(channel = 0; channel < channels; channel++, sample++)
574 s32buffer[sample] = buffer[channel][wide_sample];
576 if(is_big_endian != is_big_endian_host_) {
578 const unsigned bytes = sample * 4;
579 for(byte = 0; byte < bytes; byte += 4) {
580 tmp = u8buffer[byte];
581 u8buffer[byte] = u8buffer[byte+3];
582 u8buffer[byte+3] = tmp;
583 tmp = u8buffer[byte+1];
584 u8buffer[byte+1] = u8buffer[byte+2];
585 u8buffer[byte+2] = tmp;
590 const unsigned bytes = sample * 4;
591 for(lbyte = byte = 0; byte < bytes; ) {
593 u8buffer[lbyte++] = u8buffer[byte++];
594 u8buffer[lbyte++] = u8buffer[byte++];
595 u8buffer[lbyte++] = u8buffer[byte++];
600 const unsigned bytes = sample * 4;
601 for(lbyte = byte = 0; byte < bytes; ) {
602 u8buffer[lbyte++] = u8buffer[byte++];
603 u8buffer[lbyte++] = u8buffer[byte++];
604 u8buffer[lbyte++] = u8buffer[byte++];
608 if(fwrite(u8buffer, 3, sample, fout) != sample)
609 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
615 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
618 void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
620 DecoderSession *decoder_session = (DecoderSession*)client_data;
622 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
623 /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
624 if(metadata->data.stream_info.total_samples > 0 && decoder_session->skip >= metadata->data.stream_info.total_samples) {
625 fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", decoder_session->inbasefilename);
626 decoder_session->abort_flag = true;
629 else if(metadata->data.stream_info.total_samples == 0 && decoder_session->skip > 0) {
630 fprintf(stderr, "%s: ERROR, can't skip when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
631 decoder_session->abort_flag = true;
635 decoder_session->total_samples = metadata->data.stream_info.total_samples - decoder_session->skip;
636 decoder_session->bps = metadata->data.stream_info.bits_per_sample;
637 decoder_session->channels = metadata->data.stream_info.channels;
638 decoder_session->sample_rate = metadata->data.stream_info.sample_rate;
640 if(decoder_session->bps != 8 && decoder_session->bps != 16 && decoder_session->bps != 24) {
641 fprintf(stderr, "%s: ERROR: bits per sample is not 8/16/24\n", decoder_session->inbasefilename);
642 decoder_session->abort_flag = true;
646 /* write the WAVE headers if necessary */
647 if(!decoder_session->analysis_mode && !decoder_session->test_only && decoder_session->is_wave_out) {
648 FLAC__uint64 data_size = decoder_session->total_samples * decoder_session->channels * ((decoder_session->bps+7)/8);
649 if(decoder_session->total_samples == 0) {
650 if(decoder_session->fout == stdout) {
651 fprintf(stderr, "%s: WARNING, don't have accurate sample count available for WAVE header.\n", decoder_session->inbasefilename);
652 fprintf(stderr, " Generated WAVE file will have a data chunk size of 0. Try\n");
653 fprintf(stderr, " decoding directly to a file instead.\n");
656 decoder_session->wave_chunk_size_fixup.needs_fixup = true;
659 if(data_size >= 0xFFFFFFDC) {
660 fprintf(stderr, "%s: ERROR: stream is too big to fit in a single WAVE file chunk\n", decoder_session->inbasefilename);
661 decoder_session->abort_flag = true;
664 if(fwrite("RIFF", 1, 4, decoder_session->fout) != 4) decoder_session->abort_flag = true;
665 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
666 decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
667 if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)(data_size+36))) decoder_session->abort_flag = true; /* filesize-8 */
668 if(fwrite("WAVEfmt ", 1, 8, decoder_session->fout) != 8) decoder_session->abort_flag = true;
669 if(fwrite("\020\000\000\000", 1, 4, decoder_session->fout) != 4) decoder_session->abort_flag = true; /* chunk size = 16 */
670 if(fwrite("\001\000", 1, 2, decoder_session->fout) != 2) decoder_session->abort_flag = true; /* compression code == 1 */
671 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels))) decoder_session->abort_flag = true;
672 if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate)) decoder_session->abort_flag = true;
673 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 ??? */
674 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 */
675 if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps))) decoder_session->abort_flag = true; /* bits per sample */
676 if(fwrite("data", 1, 4, decoder_session->fout) != 4) decoder_session->abort_flag = true;
677 if(decoder_session->wave_chunk_size_fixup.needs_fixup)
678 decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
679 if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size)) decoder_session->abort_flag = true; /* data size */
684 void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
686 DecoderSession *decoder_session = (DecoderSession*)client_data;
688 fprintf(stderr, "%s: *** Got error code %d:%s\n", decoder_session->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
689 if(!decoder_session->continue_through_decode_errors)
690 decoder_session->abort_flag = true;
693 void print_error_with_state(const DecoderSession *d, const char *message)
695 const int ilen = strlen(d->inbasefilename) + 1;
697 fprintf(stderr, "\n%s: %s\n", d->inbasefilename, message);
701 const OggFLAC__StreamDecoderState osd_state = OggFLAC__stream_decoder_get_state(d->decoder.ogg.stream);
702 if(osd_state != OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR) {
703 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)osd_state, OggFLAC__StreamDecoderStateString[osd_state]);
706 const FLAC__StreamDecoderState fsd_state = OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(d->decoder.ogg.stream);
707 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
713 const FLAC__FileDecoderState ffd_state = FLAC__file_decoder_get_state(d->decoder.flac.file);
714 if(ffd_state != FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
715 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)ffd_state, FLAC__FileDecoderStateString[ffd_state]);
718 const FLAC__SeekableStreamDecoderState fssd_state = FLAC__file_decoder_get_seekable_stream_decoder_state(d->decoder.flac.file);
719 if(fssd_state != FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
720 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fssd_state, FLAC__SeekableStreamDecoderStateString[fssd_state]);
723 const FLAC__StreamDecoderState fsd_state = FLAC__file_decoder_get_stream_decoder_state(d->decoder.flac.file);
724 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
730 void print_stats(const DecoderSession *decoder_session)
732 if(decoder_session->verbose) {
733 #if defined _MSC_VER || defined __MINGW32__
734 /* with VC++ you have to spoon feed it the casting */
735 const double progress = (double)(FLAC__int64)decoder_session->samples_processed / (double)(FLAC__int64)decoder_session->total_samples * 100.0;
737 const double progress = (double)decoder_session->samples_processed / (double)decoder_session->total_samples * 100.0;
739 if(decoder_session->total_samples > 0) {
740 fprintf(stderr, "\r%s: %s%u%% complete",
741 decoder_session->inbasefilename,
742 decoder_session->test_only? "testing, " : decoder_session->analysis_mode? "analyzing, " : "",
743 (unsigned)floor(progress + 0.5)
747 fprintf(stderr, "\r%s: %s %u samples",
748 decoder_session->inbasefilename,
749 decoder_session->test_only? "tested" : decoder_session->analysis_mode? "analyzed" : "wrote",
750 (unsigned)decoder_session->samples_processed