#else
# include <unistd.h>
#endif
-#include <stdio.h> /* for FILE */
+#include <math.h> /* for floor() */
+#include <stdio.h> /* for FILE et al. */
#include <string.h> /* for strcmp() */
#include "FLAC/all.h"
#include "decode.h"
+#include "file.h"
typedef struct {
+ const char *inbasefilename;
FILE *fout;
bool abort_flag;
bool analysis_mode;
stream_info.skip_count_too_high = false;
stream_info.samples_processed = 0;
stream_info.frame_counter = 0;
+ stream_info.inbasefilename = flac__file_get_basename(infile);
stream_info.fout = 0; /* initialized with an open file later if necessary */
FLAC__ASSERT(!(stream_info.test_only && stream_info.analysis_mode));
}
else {
if(0 == (stream_info.fout = fopen(outfile, "wb"))) {
- fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
- return false;
+ fprintf(stderr, "%s: ERROR: can't open output file %s\n", stream_info.inbasefilename, outfile);
+ return 1;
}
}
}
if(skip > 0) {
if(!FLAC__file_decoder_process_metadata(decoder)) {
- fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto wav_abort_;
}
if(stream_info.skip_count_too_high) {
- fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", infile);
+ fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", stream_info.inbasefilename);
goto wav_abort_;
}
if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
- fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto wav_abort_;
}
if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
if(verbose) fprintf(stderr, "\n");
- fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto wav_abort_;
}
if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_END_OF_FILE) {
if(verbose) fprintf(stderr, "\n");
- fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto wav_abort_;
}
}
else {
if(!FLAC__file_decoder_process_whole_file(decoder)) {
if(verbose) fprintf(stderr, "\n");
- fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto wav_abort_;
}
if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_END_OF_FILE) {
if(verbose) fprintf(stderr, "\n");
- fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto wav_abort_;
}
}
}
if(0 != stream_info.fout && stream_info.fout != stdout)
fclose(stream_info.fout);
- if(verbose)
- fprintf(stderr, "\n");
if(analysis_mode)
flac__analyze_finish(aopts);
if(md5_failure) {
- fprintf(stderr, "%s: WARNING, MD5 signature mismatch\n", infile);
- return 1;
+ fprintf(stderr, "\r%s: WARNING, MD5 signature mismatch\n", stream_info.inbasefilename);
}
else {
- if(stream_info.test_only)
- fprintf(stderr, "%s: ok\n", infile);
+ if(verbose)
+ fprintf(stderr, "\r%s: %s \n", stream_info.inbasefilename, stream_info.test_only? "ok ":analysis_mode?"done ":"done");
}
return 0;
wav_abort_:
stream_info.skip_count_too_high = false;
stream_info.samples_processed = 0;
stream_info.frame_counter = 0;
+ stream_info.inbasefilename = flac__file_get_basename(infile);
stream_info.fout = 0; /* initialized with an open file later if necessary */
FLAC__ASSERT(!(stream_info.test_only && stream_info.analysis_mode));
}
else {
if(0 == (stream_info.fout = fopen(outfile, "wb"))) {
- fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
- return false;
+ fprintf(stderr, "%s: ERROR: can't open output file %s\n", stream_info.inbasefilename, outfile);
+ return 1;
}
}
}
if(skip > 0) {
if(!FLAC__file_decoder_process_metadata(decoder)) {
- fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto raw_abort_;
}
if(stream_info.skip_count_too_high) {
- fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", infile);
+ fprintf(stderr, "%s: ERROR trying to skip more samples than in stream\n", stream_info.inbasefilename);
goto raw_abort_;
}
if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
- fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto raw_abort_;
}
if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
if(verbose) fprintf(stderr, "\n");
- fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto raw_abort_;
}
if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_END_OF_FILE) {
if(verbose) fprintf(stderr, "\n");
- fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto raw_abort_;
}
}
else {
if(!FLAC__file_decoder_process_whole_file(decoder)) {
if(verbose) fprintf(stderr, "\n");
- fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto raw_abort_;
}
if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_END_OF_FILE) {
if(verbose) fprintf(stderr, "\n");
- fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", stream_info.inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
goto raw_abort_;
}
}
}
if(0 != stream_info.fout && stream_info.fout != stdout)
fclose(stream_info.fout);
- if(verbose)
- fprintf(stderr, "\n");
if(analysis_mode)
flac__analyze_finish(aopts);
if(md5_failure) {
- fprintf(stderr, "%s: WARNING, MD5 signature mismatch\n", infile);
- return 1;
+ fprintf(stderr, "\r%s: WARNING, MD5 signature mismatch\n", stream_info.inbasefilename);
}
else {
- if(stream_info.test_only)
- fprintf(stderr, "%s: ok\n", infile);
+ if(verbose)
+ fprintf(stderr, "\r%s: %s \n", stream_info.inbasefilename, stream_info.test_only? "ok ":analysis_mode?"done ":"done");
}
return 0;
raw_abort_:
decoder = FLAC__file_decoder_new();
if(0 == decoder) {
- fprintf(stderr, "ERROR creating the decoder instance\n");
+ fprintf(stderr, "%s: ERROR creating the decoder instance\n", stream_info->inbasefilename);
return false;
}
if(FLAC__file_decoder_init(decoder, true /*check_md5*/, infile, write_callback, metadata_callback, error_callback, stream_info) != FLAC__FILE_DECODER_OK) {
- fprintf(stderr, "ERROR initializing decoder, state=%d:%s\n", FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
+ fprintf(stderr, "%s: ERROR initializing decoder, state=%d:%s\n", stream_info->inbasefilename, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
return false;
}
stream_info->sample_rate = metadata->data.stream_info.sample_rate;
if(stream_info->bps != 8 && stream_info->bps != 16 && stream_info->bps != 24) {
- fprintf(stderr, "ERROR: bits per sample is not 8/16/24\n");
+ fprintf(stderr, "%s: ERROR: bits per sample is not 8/16/24\n", stream_info->inbasefilename);
stream_info->abort_flag = true;
return;
}
if(!stream_info->analysis_mode && !stream_info->test_only && stream_info->is_wave_out) {
uint64 data_size = stream_info->total_samples * stream_info->channels * ((stream_info->bps+7)/8);
if(data_size >= 0xFFFFFFDC) {
- fprintf(stderr, "ERROR: stream is too big for a wave file\n");
+ fprintf(stderr, "%s: ERROR: stream is too big to fit in a single WAVE file chunk\n", stream_info->inbasefilename);
stream_info->abort_flag = true;
return;
}
{
stream_info_struct *stream_info = (stream_info_struct *)client_data;
(void)decoder;
- fprintf(stderr, "*** Got error code %d:%s\n", status, FLAC__StreamDecoderErrorStatusString[status]);
+ fprintf(stderr, "%s: *** Got error code %d:%s\n", stream_info->infbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
stream_info->abort_flag = true;
}
void print_stats(const stream_info_struct *stream_info)
{
if(stream_info->verbose) {
- if(stream_info->total_samples > 0) {
- fprintf(stderr, "\r%s %u of %u samples, %6.2f%% complete",
- stream_info->test_only? "tested" : stream_info->analysis_mode? "analyzed" : "wrote",
- (unsigned)stream_info->samples_processed,
- (unsigned)stream_info->total_samples,
#ifdef _MSC_VER
- /* with VC++ you have to spoon feed it the casting */
- (double)(int64)stream_info->samples_processed / (double)(int64)stream_info->total_samples * 100.0
+ /* with VC++ you have to spoon feed it the casting */
+ const double progress = (double)(int64)stream_info->samples_processed / (double)(int64)stream_info->total_samples * 100.0;
#else
- (double)stream_info->samples_processed / (double)stream_info->total_samples * 100.0
+ const double progress = (double)stream_info->samples_processed / (double)stream_info->total_samples * 100.0;
#endif
+ if(stream_info->total_samples > 0) {
+ fprintf(stderr, "\r%s: %s%u%% complete",
+ stream_info->inbasefilename,
+ stream_info->test_only? "testing, " : stream_info->analysis_mode? "analyzing, " : "",
+ (unsigned)floor(progress + 0.5)
);
}
else {
- fprintf(stderr, "\r%s %u of ? samples, ?%% complete",
+ fprintf(stderr, "\r%s: %s %u samples",
+ stream_info->inbasefilename,
stream_info->test_only? "tested" : stream_info->analysis_mode? "analyzed" : "wrote",
(unsigned)stream_info->samples_processed
);
#else
# include <unistd.h>
#endif
+#include <math.h> /* for floor() */
#include <stdio.h> /* for FILE et al. */
#include <stdlib.h> /* for malloc */
#include <string.h> /* for strcmp() */
#include "FLAC/all.h"
#include "encode.h"
+#include "file.h"
#ifdef min
#undef min
} verify_fifo_struct;
typedef struct {
+ const char *inbasefilename;
FILE *fout;
const char *outfilename;
FLAC__StreamEncoder *encoder;
static void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
static void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
-static bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok);
-static bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok);
+static bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok, const char *fn);
+static bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok, const char *fn);
static bool write_big_endian_uint16(FILE *f, uint16 val);
static bool write_big_endian_uint64(FILE *f, uint64 val);
encoder_wrapper.bytes_written = 0;
encoder_wrapper.samples_written = 0;
encoder_wrapper.stream_offset = 0;
+ encoder_wrapper.inbasefilename = flac__file_get_basename(infilename);
encoder_wrapper.outfilename = outfilename;
encoder_wrapper.seek_table.points = 0;
encoder_wrapper.first_seek_point_to_check = 0;
}
else {
if(0 == (encoder_wrapper.fout = fopen(outfilename, "wb"))) {
- fprintf(stderr, "ERROR: can't open output file %s\n", outfilename);
+ fprintf(stderr, "%s: ERROR: can't open output file %s\n", encoder_wrapper.inbasefilename, outfilename);
fclose(infile);
- return false;
+ return 1;
}
}
* lookahead[] already has "RIFFxxxxWAVE", do sub-chunks
*/
while(!feof(infile)) {
- if(!read_little_endian_uint32(infile, &xx, true))
+ if(!read_little_endian_uint32(infile, &xx, true, encoder_wrapper.inbasefilename))
goto wav_abort_;
if(feof(infile))
break;
if(xx == 0x20746d66) { /* "fmt " */
if(got_fmt_chunk) {
- fprintf(stderr, "WARNING: skipping extra 'fmt ' sub-chunk\n");
+ fprintf(stderr, "%s: WARNING: skipping extra 'fmt ' sub-chunk\n", encoder_wrapper.inbasefilename);
}
else {
/* fmt sub-chunk size */
- if(!read_little_endian_uint32(infile, &xx, false))
+ if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
if(xx != 16) {
- fprintf(stderr, "ERROR: unsupported non-standard 'fmt ' sub-chunk has length %u != 16\n", (unsigned)xx);
+ fprintf(stderr, "%s: ERROR: unsupported non-standard 'fmt ' sub-chunk has length %u != 16\n", encoder_wrapper.inbasefilename, (unsigned)xx);
goto wav_abort_;
}
/* compression code */
- if(!read_little_endian_uint16(infile, &x, false))
+ if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
if(x != 1) {
- fprintf(stderr, "ERROR: unsupported compression type %u\n", (unsigned)x);
+ fprintf(stderr, "%s: ERROR: unsupported compression type %u\n", encoder_wrapper.inbasefilename, (unsigned)x);
goto wav_abort_;
}
/* number of channels */
- if(!read_little_endian_uint16(infile, &x, false))
+ if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
if(x == 0 || x > FLAC__MAX_CHANNELS) {
- fprintf(stderr, "ERROR: unsupported number channels %u\n", (unsigned)x);
+ fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_wrapper.inbasefilename, (unsigned)x);
goto wav_abort_;
}
channels = x;
/* sample rate */
- if(!read_little_endian_uint32(infile, &xx, false))
+ if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
if(xx == 0 || xx > FLAC__MAX_SAMPLE_RATE) {
- fprintf(stderr, "ERROR: unsupported sample rate %u\n", (unsigned)xx);
+ fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_wrapper.inbasefilename, (unsigned)xx);
goto wav_abort_;
}
sample_rate = xx;
/* avg bytes per second (ignored) */
- if(!read_little_endian_uint32(infile, &xx, false))
+ if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
/* block align (ignored) */
- if(!read_little_endian_uint16(infile, &x, false))
+ if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
/* bits per sample */
- if(!read_little_endian_uint16(infile, &x, false))
+ if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
if(x != 8 && x != 16) {
- fprintf(stderr, "ERROR: unsupported bits per sample %u\n", (unsigned)x);
+ fprintf(stderr, "%s: ERROR: unsupported bits per sample %u\n", encoder_wrapper.inbasefilename, (unsigned)x);
goto wav_abort_;
}
bps = x;
}
else if(xx == 0x61746164) { /* "data" */
if(got_data_chunk) {
- fprintf(stderr, "WARNING: skipping extra 'data' sub-chunk\n");
+ fprintf(stderr, "%s: WARNING: skipping extra 'data' sub-chunk\n", encoder_wrapper.inbasefilename);
}
else if(!got_fmt_chunk) {
- fprintf(stderr, "ERROR: got data sub-chunk before fmt sub-chunk\n");
+ fprintf(stderr, "%s: ERROR: got data sub-chunk before fmt sub-chunk\n", encoder_wrapper.inbasefilename);
goto wav_abort_;
}
else {
/* data size */
- if(!read_little_endian_uint32(infile, &xx, false))
+ if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
data_bytes = xx;
if(skip > 0) {
if(infile != stdin) {
if(-1 == fseek(infile, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
- fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during seek while skipping samples\n", encoder_wrapper.inbasefilename);
goto wav_abort_;
}
}
for(left = skip; left > 0; ) {
need = min(left, CHUNK_OF_SAMPLES);
if(fread(ucbuffer, 1, bytes_per_wide_sample * need, infile) < need) {
- fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_wrapper.inbasefilename);
goto wav_abort_;
}
left -= need;
bytes_read = fread(ucbuffer, sizeof(unsigned char), min(data_bytes, CHUNK_OF_SAMPLES * bytes_per_wide_sample), infile);
if(bytes_read == 0) {
if(ferror(infile)) {
- fprintf(stderr, "ERROR reading from %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename);
goto wav_abort_;
}
else if(feof(infile)) {
- fprintf(stderr, "WARNING: unexpected EOF from %s, expected %u samples, got %u samples\n", infilename, (unsigned)encoder_wrapper.total_samples_to_encode, (unsigned)encoder_wrapper.samples_written);
+ fprintf(stderr, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned)encoder_wrapper.total_samples_to_encode, (unsigned)encoder_wrapper.samples_written);
data_bytes = 0;
}
}
else {
if(bytes_read % bytes_per_wide_sample != 0) {
- fprintf(stderr, "ERROR, got partial sample from input file %s\n", infilename);
+ fprintf(stderr, "%s: ERROR: got partial sample\n", encoder_wrapper.inbasefilename);
goto wav_abort_;
}
else {
/* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */
if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
- fprintf(stderr, "ERROR during encoding, state = %d:%s\n", FLAC__stream_encoder_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper.encoder)]);
+ fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper.encoder)]);
goto wav_abort_;
}
data_bytes -= bytes_read;
}
}
else {
- fprintf(stderr, "WARNING: skipping unknown sub-chunk '%c%c%c%c'\n", (char)(xx&255), (char)((xx>>8)&255), (char)((xx>>16)&255), (char)(xx>>24));
+ fprintf(stderr, "%s: WARNING: skipping unknown sub-chunk '%c%c%c%c'\n", encoder_wrapper.inbasefilename, (char)(xx&255), (char)((xx>>8)&255), (char)((xx>>16)&255), (char)(xx>>24));
/* sub-chunk size */
- if(!read_little_endian_uint32(infile, &xx, false))
+ if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
goto wav_abort_;
if(infile != stdin) {
if(-1 == fseek(infile, xx, SEEK_CUR)) {
- fprintf(stderr, "ERROR seeking ahead while skipping unsupported sub-chunk in input file %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during seek while skipping unsupported sub-chunk\n", encoder_wrapper.inbasefilename);
goto wav_abort_;
}
}
for(left = xx; left > 0; ) {
need = min(left, chunk);
if(fread(ucbuffer, 1, need, infile) < need) {
- fprintf(stderr, "ERROR seeking while skipping unsupported sub-chunk in input file %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during read while skipping unsupported sub-chunk\n", encoder_wrapper.inbasefilename);
goto wav_abort_;
}
left -= need;
FLAC__stream_decoder_finish(encoder_wrapper.verify_fifo.decoder);
FLAC__stream_decoder_delete(encoder_wrapper.verify_fifo.decoder);
if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
- fprintf(stderr, "%s: Verify FAILED! (%s) Do not trust %s\n", infilename, verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
+ fprintf(stderr, "Verify FAILED! (%s) Do not trust %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
return 1;
}
- else if(encoder_wrapper.verbose) {
- fprintf(stderr, "%s: Verify succeeded\n", infilename);
- }
}
if(infile != stdin)
fclose(infile);
FLAC__stream_decoder_finish(encoder_wrapper.verify_fifo.decoder);
FLAC__stream_decoder_delete(encoder_wrapper.verify_fifo.decoder);
if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
- fprintf(stderr, "%s: Verify FAILED! (%s) Do not trust %s\n", infilename, verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
+ fprintf(stderr, "Verify FAILED! (%s) Do not trust %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
return 1;
}
- else if(encoder_wrapper.verbose) {
- fprintf(stderr, "%s: Verify succeeded\n", infilename);
- }
}
if(infile != stdin)
fclose(infile);
encoder_wrapper.bytes_written = 0;
encoder_wrapper.samples_written = 0;
encoder_wrapper.stream_offset = 0;
+ encoder_wrapper.inbasefilename = flac__file_get_basename(infilename);
encoder_wrapper.outfilename = outfilename;
encoder_wrapper.seek_table.points = 0;
encoder_wrapper.first_seek_point_to_check = 0;
if(0 == (encoder_wrapper.fout = fopen(outfilename, "wb"))) {
fprintf(stderr, "ERROR: can't open output file %s\n", outfilename);
fclose(infile);
- return false;
+ return 1;
}
}
lookahead_length = 0;
if(infile != stdin) {
if(-1 == fseek(infile, (long)skip_bytes, SEEK_SET)) {
- fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during seek while skipping samples\n", encoder_wrapper.inbasefilename);
goto raw_abort_;
}
}
for(left = skip_bytes; left > 0; ) {
need = min(left, chunk);
if(fread(ucbuffer, 1, need, infile) < need) {
- fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_wrapper.inbasefilename);
goto raw_abort_;
}
left -= need;
memcpy(ucbuffer, lookahead, lookahead_length);
bytes_read = fread(ucbuffer+lookahead_length, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample - lookahead_length, infile) + lookahead_length;
if(ferror(infile)) {
- fprintf(stderr, "ERROR reading from %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename);
goto raw_abort_;
}
lookahead_length = 0;
if(bytes_read == 0) {
if(ferror(infile)) {
- fprintf(stderr, "ERROR reading from %s\n", infilename);
+ fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename);
goto raw_abort_;
}
}
else if(bytes_read % bytes_per_wide_sample != 0) {
- fprintf(stderr, "ERROR, got partial sample from input file %s\n", infilename);
+ fprintf(stderr, "%s: ERROR: got partial sample\n", encoder_wrapper.inbasefilename);
goto raw_abort_;
}
else {
/* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */
if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
- fprintf(stderr, "ERROR during encoding, state = %d:%s\n", FLAC__stream_encoder_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper.encoder)]);
+ fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper.encoder)]);
goto raw_abort_;
}
}
FLAC__stream_decoder_finish(encoder_wrapper.verify_fifo.decoder);
FLAC__stream_decoder_delete(encoder_wrapper.verify_fifo.decoder);
if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
- fprintf(stderr, "%s: Verify FAILED! (%s) Do not trust %s\n", infilename, verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
+ fprintf(stderr, "Verify FAILED! (%s) Do not trust %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
return 1;
}
- else if(encoder_wrapper.verbose) {
- fprintf(stderr, "%s: Verify succeeded\n", infilename);
- }
}
if(infile != stdin)
fclose(infile);
FLAC__stream_decoder_finish(encoder_wrapper.verify_fifo.decoder);
FLAC__stream_decoder_delete(encoder_wrapper.verify_fifo.decoder);
if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
- fprintf(stderr, "%s: Verify FAILED! (%s) Do not trust %s\n", infilename, verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
+ fprintf(stderr, "Verify FAILED! (%s) Do not trust %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
return 1;
}
- else if(encoder_wrapper.verbose) {
- fprintf(stderr, "%s: Verify succeeded\n", infilename);
- }
}
if(infile != stdin)
fclose(infile);
encoder_wrapper->encoder = FLAC__stream_encoder_new();
if(0 == encoder_wrapper->encoder) {
- fprintf(stderr, "ERROR creating the encoder instance\n");
+ fprintf(stderr, "%s: ERROR creating the encoder instance\n", encoder_wrapper->inbasefilename);
return false;
}
encoder_wrapper->verify_fifo.size = blocksize + CHUNK_OF_SAMPLES;
for(i = 0; i < channels; i++) {
if(0 == (encoder_wrapper->verify_fifo.original[i] = (int32*)malloc(sizeof(int32) * encoder_wrapper->verify_fifo.size))) {
- fprintf(stderr, "ERROR allocating verify buffers\n");
+ fprintf(stderr, "%s: ERROR allocating verify buffers\n", encoder_wrapper->inbasefilename);
return false;
}
}
/* set up a stream decoder for verification */
encoder_wrapper->verify_fifo.decoder = FLAC__stream_decoder_new();
if(0 == encoder_wrapper->verify_fifo.decoder) {
- fprintf(stderr, "ERROR creating the verify decoder instance\n");
+ fprintf(stderr, "%s: ERROR creating the verify decoder instance\n", encoder_wrapper->inbasefilename);
return false;
}
if(FLAC__stream_decoder_init(encoder_wrapper->verify_fifo.decoder, verify_read_callback, verify_write_callback, verify_metadata_callback, verify_error_callback, encoder_wrapper) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) {
- fprintf(stderr, "ERROR initializing decoder, state = %d:%s\n", FLAC__stream_decoder_state(encoder_wrapper->verify_fifo.decoder), FLAC__StreamDecoderStateString[FLAC__stream_decoder_state(encoder_wrapper->verify_fifo.decoder)]);
+ fprintf(stderr, "%s: ERROR initializing decoder, state = %d:%s\n", encoder_wrapper->inbasefilename, FLAC__stream_decoder_state(encoder_wrapper->verify_fifo.decoder), FLAC__StreamDecoderStateString[FLAC__stream_decoder_state(encoder_wrapper->verify_fifo.decoder)]);
return false;
}
}
if(!convert_to_seek_table(requested_seek_points, num_requested_seek_points, encoder_wrapper->total_samples_to_encode, blocksize, &encoder_wrapper->seek_table)) {
- fprintf(stderr, "ERROR allocating seek table\n");
+ fprintf(stderr, "%s: ERROR allocating seek table\n", encoder_wrapper->inbasefilename);
return false;
}
if(FLAC__stream_encoder_init(encoder_wrapper->encoder, !lax, do_mid_side, loose_mid_side, channels, bps, sample_rate, blocksize, max_lpc_order, qlp_coeff_precision, do_qlp_coeff_prec_search, do_exhaustive_model_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, encoder_wrapper->total_samples_to_encode, (encoder_wrapper->seek_table.num_points > 0)? &encoder_wrapper->seek_table : 0, padding, true /*last_metadata_is_last*/, write_callback, metadata_callback, encoder_wrapper) != FLAC__STREAM_ENCODER_OK) {
- fprintf(stderr, "ERROR initializing encoder, state = %d:%s\n", FLAC__stream_encoder_state(encoder_wrapper->encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper->encoder)]);
+ fprintf(stderr, "%s: ERROR initializing encoder, state = %d:%s\n", encoder_wrapper->inbasefilename, FLAC__stream_encoder_state(encoder_wrapper->encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper->encoder)]);
return false;
}
for(channel = 0; channel < channels; channel++) {
if(0 != memcmp(buffer[channel], encoder_wrapper->verify_fifo.original[channel], bytes_per_block)) {
- fprintf(stderr, "\nERROR: mismatch in decoded data, verify FAILED!\n");
+ fprintf(stderr, "\n%s: ERROR: mismatch in decoded data, verify FAILED!\n", encoder_wrapper->inbasefilename);
fprintf(stderr, " Please submit a bug report to\n");
fprintf(stderr, " http://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
fprintf(stderr, " Make sure to include an email contact in the comment and/or use the\n");
void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
+ encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
(void)decoder;
- (void)client_data;
- fprintf(stderr, "\nERROR: verification decoder returned error %d:%s\n", status, FLAC__StreamDecoderErrorStatusString[status]);
+ fprintf(stderr, "\n%s: ERROR: verification decoder returned error %d:%s\n", encoder_wrapper->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
}
void print_stats(const encoder_wrapper_struct *encoder_wrapper)
{
#ifdef _MSC_VER
/* with VC++ you have to spoon feed it the casting */
- double progress = (double)(int64)encoder_wrapper->samples_written / (double)(int64)encoder_wrapper->total_samples_to_encode;
-#else
- double progress = (double)encoder_wrapper->samples_written / (double)encoder_wrapper->total_samples_to_encode;
-#endif
- fprintf(stderr, "\r%0.2f%% complete: frame %u, wrote %u bytes (%u/%u samples), r=%5.3f",
- progress * 100.0, encoder_wrapper->current_frame,
- (unsigned)encoder_wrapper->bytes_written, (unsigned)encoder_wrapper->samples_written, (unsigned)encoder_wrapper->total_samples_to_encode,
-#ifdef _MSC_VER
- /* with VC++ you have to spoon feed it the casting */
- (double)(int64)encoder_wrapper->bytes_written / ((double)(int64)encoder_wrapper->unencoded_size * progress)
+ const double progress = (double)(int64)encoder_wrapper->samples_written / (double)(int64)encoder_wrapper->total_samples_to_encode;
+ const double ratio = (double)(int64)encoder_wrapper->bytes_written / ((double)(int64)encoder_wrapper->unencoded_size * progress);
#else
- (double)encoder_wrapper->bytes_written / ((double)encoder_wrapper->unencoded_size * progress)
+ const double progress = (double)encoder_wrapper->samples_written / (double)encoder_wrapper->total_samples_to_encode;
+ const double ratio = (double)encoder_wrapper->bytes_written / ((double)encoder_wrapper->unencoded_size * progress);
#endif
- );
+
+ if(encoder_wrapper->samples_written == encoder_wrapper->total_samples_to_encode) {
+ fprintf(stderr, "\r%s:%s wrote %u bytes, ratio=%0.3f",
+ encoder_wrapper->inbasefilename,
+ encoder_wrapper->verify? (encoder_wrapper->verify_fifo.result == FLAC__VERIFY_OK? " Verify OK," : " Verify FAILED!") : "",
+ (unsigned)encoder_wrapper->bytes_written,
+ ratio
+ );
+ }
+ else {
+ fprintf(stderr, "\r%s: %u%% complete, ratio=%0.3f", encoder_wrapper->inbasefilename, (unsigned)floor(progress * 100.0 + 0.5), ratio);
+ }
}
-bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok)
+bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok, const char *fn)
{
size_t bytes_read = fread(val, 1, 2, f);
if(bytes_read == 0) {
if(!eof_ok) {
- fprintf(stderr, "ERROR: unexpected EOF\n");
+ fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
return false;
}
else
return true;
}
else if(bytes_read < 2) {
- fprintf(stderr, "ERROR: unexpected EOF\n");
+ fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
return false;
}
else {
}
}
-bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok)
+bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok, const char *fn)
{
size_t bytes_read = fread(val, 1, 4, f);
if(bytes_read == 0) {
if(!eof_ok) {
- fprintf(stderr, "ERROR: unexpected EOF\n");
+ fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
return false;
}
else
return true;
}
else if(bytes_read < 4) {
- fprintf(stderr, "ERROR: unexpected EOF\n");
+ fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
return false;
}
else {