From b10d83cd9de33860d6974953959483faff832446 Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Tue, 23 Jan 2001 01:19:17 +0000 Subject: [PATCH] fix bug: --skip and pipes --- src/flac/encode.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/flac/encode.c b/src/flac/encode.c index 27d1ab7..7844643 100644 --- a/src/flac/encode.c +++ b/src/flac/encode.c @@ -31,6 +31,11 @@ #include "FLAC/all.h" #include "encode.h" +#ifdef min +#undef min +#endif +#define min(x,y) ((x)<(y)?(x):(y)) + #define CHUNK_OF_SAMPLES 2048 typedef enum { @@ -218,9 +223,22 @@ int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 ski bytes_per_wide_sample = channels * (bps >> 3); if(skip > 0) { - if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) { - fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile); - goto wav_abort_; + if(fin != stdin) { + if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) { + fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile); + goto wav_abort_; + } + } + else { + int64 left; + unsigned need; + for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) { + need = min(left, CHUNK_OF_SAMPLES); + if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) { + fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile); + goto wav_abort_; + } + } } } @@ -357,9 +375,24 @@ int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 ski } } - if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) { - fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile); - goto raw_abort_; + if(skip > 0) { + if(fin != stdin) { + if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) { + fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile); + goto raw_abort_; + } + } + else { + int64 left; + unsigned need; + for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) { + need = min(left, CHUNK_OF_SAMPLES); + if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) { + fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile); + goto raw_abort_; + } + } + } } if(!init_encoder(lax, do_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, blocksize, qlp_coeff_precision, channels, bps, sample_rate, &encoder_wrapper)) -- 2.7.4