From 449989493c623ab7cc7312e27c817a687506c5df Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Fri, 19 Jul 2002 05:40:26 +0000 Subject: [PATCH] add AIFF support patches from Brady Patterson, add support for --sector-align and raw files --- src/flac/encode.c | 986 ++++++++++++++++++++++++++++++++++++---------- src/flac/encode.h | 10 +- src/flac/main.c | 92 +++-- 3 files changed, 844 insertions(+), 244 deletions(-) diff --git a/src/flac/encode.c b/src/flac/encode.c index 6d76d3c6..27e42047 100644 --- a/src/flac/encode.c +++ b/src/flac/encode.c @@ -22,6 +22,7 @@ #else # include #endif +#include /* for LONG_MAX */ #include /* for floor() */ #include /* for FILE et al. */ #include /* for malloc */ @@ -121,9 +122,413 @@ static void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__Stre static void print_stats(const encoder_wrapper_struct *encoder_wrapper); static FLAC__bool read_little_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn); static FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn); +static FLAC__bool read_big_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn); +static FLAC__bool read_big_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn); +static FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn); static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val); static FLAC__bool write_big_endian_uint64(FILE *f, FLAC__uint64 val); +int +flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const char *outfilename, + const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options) +{ + encoder_wrapper_struct encoder_wrapper; + FLAC__uint16 x; + FLAC__uint32 xx; + unsigned int channels= 0U, bps= 0U, sample_rate= 0U, sample_frames= 0U; + FLAC__bool got_comm_chunk= false, got_ssnd_chunk= false; + int info_align_carry= -1, info_align_zero= -1; + enum { NORMAL, DONE, ERROR, MISMATCH } status= NORMAL; + + FLAC__ASSERT(!options.common.sector_align || options.common.skip == 0); + + encoder_wrapper.encoder = 0; + encoder_wrapper.verify = options.common.verify; + encoder_wrapper.verbose = options.common.verbose; + 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; +#ifdef FLAC__HAS_OGG + encoder_wrapper.use_ogg = options.common.use_ogg; +#endif + + (void)infilesize; /* silence compiler warning about unused parameter */ + (void)lookahead; /* silence compiler warning about unused parameter */ + (void)lookahead_length; /* silence compiler warning about unused parameter */ + + if(0 == strcmp(outfilename, "-")) { + encoder_wrapper.fout = file__get_binary_stdout(); + } + else { + if(0 == (encoder_wrapper.fout = fopen(outfilename, "wb"))) { + fprintf(stderr, "%s: ERROR: can't open output file %s\n", encoder_wrapper.inbasefilename, outfilename); + if(infile != stdin) + fclose(infile); + return 1; + } + } + + if(!init(&encoder_wrapper)) + status= ERROR; + + /* lookahead[] already has "FORMxxxxAIFF", do sub-chunks */ + + while(status==NORMAL) { + size_t c= 0U; + char chunk_id[4]; + + /* chunk identifier; really conservative about behavior of fread() and feof() */ + if(feof(infile) || ((c= fread(chunk_id, 1U, 4U, infile)), c==0U && feof(infile))) + status= DONE; + else if(c<4U || feof(infile)) { + fprintf(stderr, "%s: ERROR: incomplete chunk identifier\n", encoder_wrapper.inbasefilename); + status= ERROR; + } + + if(status==NORMAL && got_comm_chunk==false && !strncmp(chunk_id, "COMM", 4)) { /* common chunk */ + unsigned long skip; + + if(status==NORMAL) { + /* COMM chunk size */ + if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + status= ERROR; + else if(xx<18U) { + fprintf(stderr, "%s: ERROR: non-standard 'COMM' chunk has length = %u\n", encoder_wrapper.inbasefilename, (unsigned int)xx); + status= ERROR; + } + else if(xx!=18U) + fprintf(stderr, "%s: WARNING: non-standard 'COMM' chunk has length = %u\n", encoder_wrapper.inbasefilename, (unsigned int)xx); + skip= (xx-18U)+(xx & 1U); + } + + if(status==NORMAL) { + /* number of channels */ + if(!read_big_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename)) + status= ERROR; + else if(x==0U || x>FLAC__MAX_CHANNELS) { + fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_wrapper.inbasefilename, (unsigned int)x); + status= ERROR; + } + else if(options.common.sector_align && x!=2U) { + fprintf(stderr, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_wrapper.inbasefilename, (unsigned int)x); + status= ERROR; + } + channels= x; + } + + if(status==NORMAL) { + /* number of sample frames */ + if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + status= ERROR; + sample_frames= xx; + } + + if(status==NORMAL) { + /* bits per sample */ + if(!read_big_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename)) + status= ERROR; + else if(x!=8U && x!=16U && x!=24U) { + fprintf(stderr, "%s: ERROR: unsupported bits per sample %u\n", encoder_wrapper.inbasefilename, (unsigned int)x); + status= ERROR; + } + else if(options.common.sector_align && x!=16U) { + fprintf(stderr, "%s: ERROR: file has %u bits per sample, must be 16 for --sector-align\n", encoder_wrapper.inbasefilename, (unsigned int)x); + status= ERROR; + } + bps= x; + } + + if(status==NORMAL) { + /* sample rate */ + if(!read_sane_extended(infile, &xx, false, encoder_wrapper.inbasefilename)) + status= ERROR; + else if(!FLAC__format_sample_rate_is_valid(xx)) { + fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_wrapper.inbasefilename, (unsigned int)xx); + status= ERROR; + } + else if(options.common.sector_align && xx!=44100U) { + fprintf(stderr, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_wrapper.inbasefilename, (unsigned int)xx); + status= ERROR; + } + sample_rate= xx; + } + + /* skip any extra data in the COMM chunk */ + FLAC__ASSERT(skip<=LONG_MAX); + while(status==NORMAL && skip>0U && fseek(infile, skip, SEEK_CUR)<0) { + unsigned int need= min(skip, sizeof ucbuffer); + if(fread(ucbuffer, 1U, need, infile)>3); + FLAC__bool pad= false; + + if(status==NORMAL && got_comm_chunk==false) { + fprintf(stderr, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", encoder_wrapper.inbasefilename); + status= ERROR; + } + + if(status==NORMAL) { + /* SSND chunk size */ + if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + status= ERROR; + else if(xx!=(sample_frames*bytes_per_frame + 8U)) { + fprintf(stderr, "%s: ERROR: SSND chunk size inconsistent with sample frame count\n", encoder_wrapper.inbasefilename); + status= ERROR; + } + data_bytes= xx; + pad= (data_bytes & 1U) ? true : false; + } + + if(status==NORMAL) { + /* offset */ + if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + status= ERROR; + else if(xx!=0U) { + fprintf(stderr, "%s: ERROR: offset is %u; must be 0\n", encoder_wrapper.inbasefilename, (unsigned int)xx); + status= ERROR; + } + offset= xx; + } + + if(status==NORMAL) { + /* block size */ + if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + status= ERROR; + else if(xx!=0U) { + fprintf(stderr, "%s: ERROR: block size is %u; must be 0\n", encoder_wrapper.inbasefilename, (unsigned int)xx); + status= ERROR; + } + block_size= xx; + } + + if(status==NORMAL && options.common.skip>0U) { + FLAC__uint64 remaining= options.common.skip*bytes_per_frame; + + /* do 1<<30 bytes at a time, since 1<<30 is a nice round number, and */ + /* is guaranteed to be less than LONG_MAX */ + for(; remaining>0U; remaining-= remaining>(1U<<30) ? remaining : (1U<<30)) + { + unsigned long skip= remaining % (1U<<30); + + FLAC__ASSERT(skip<=LONG_MAX); + while(status==NORMAL && skip>0 && fseek(infile, skip, SEEK_CUR)<0) { + unsigned int need= min(skip, sizeof ucbuffer); + if(fread(ucbuffer, 1U, need, infile)0U) { + append_to_verify_fifo(&encoder_wrapper, (const FLAC__int32 *const *)options.common.align_reservoir, channels, *options.common.align_reservoir_samples); + + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 *const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + status= ERROR; + } + } + + /* decrement the data_bytes counter if we need to align the file */ + if(status==NORMAL && options.common.sector_align) { + if(options.common.is_last_file) + *options.common.align_reservoir_samples= 0U; + else { + *options.common.align_reservoir_samples= align_remainder; + data_bytes-= (*options.common.align_reservoir_samples)*bytes_per_frame; + } + } + + /* now do from the file */ + while(status==NORMAL && data_bytes>0) { + size_t bytes_read= fread(ucbuffer, 1U, min(data_bytes, CHUNK_OF_SAMPLES*bytes_per_frame), infile); + + if(bytes_read==0U) { + if(ferror(infile)) { + fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename); + status= ERROR; + } + else if(feof(infile)) { + fprintf(stderr, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned int)encoder_wrapper.total_samples_to_encode, (unsigned int)encoder_wrapper.samples_written); + data_bytes= 0; + } + } + else { + if(bytes_read % bytes_per_frame != 0U) { + fprintf(stderr, "%s: ERROR: got partial sample\n", encoder_wrapper.inbasefilename); + status= ERROR; + } + else { + unsigned int frames= bytes_read/bytes_per_frame; + format_input(input, frames, true, false, channels, bps, &encoder_wrapper); + + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 *const *)input, frames)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + status= ERROR; + } + else + data_bytes-= bytes_read; + } + } + } + + /* now read unaligned samples into reservoir or pad with zeroes if necessary */ + if(status==NORMAL && options.common.sector_align) { + if(options.common.is_last_file) { + unsigned int pad_frames= 588U-align_remainder; + + if(pad_frames<588U) { + unsigned int i; + + info_align_zero= pad_frames; + for(i= 0U; i>3)); + append_to_verify_fifo(&encoder_wrapper, (const FLAC__int32 *const *)input, channels, pad_frames); + + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 *const *)input, pad_frames)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + status= ERROR; + } + } + } + else { + if(*options.common.align_reservoir_samples > 0) { + size_t bytes_read= fread(ucbuffer, 1U, (*options.common.align_reservoir_samples)*bytes_per_frame, infile); + + FLAC__ASSERT(CHUNK_OF_SAMPLES>=588U); + if(bytes_read==0U && ferror(infile)) { + fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename); + status= ERROR; + } + else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_frame) + fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned int)bytes_read, (unsigned int)encoder_wrapper.total_samples_to_encode, (unsigned int)encoder_wrapper.samples_written); + else { + info_align_carry= *options.common.align_reservoir_samples; + format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, true, false, channels, bps, &encoder_wrapper); + } + } + } + } + + if(status==NORMAL && pad==true) { + unsigned char tmp; + + if(fread(&tmp, 1U, 1U, infile)<1U) { + fprintf(stderr, "%s: ERROR during read of SSND pad byte\n", encoder_wrapper.inbasefilename); + status= ERROR; + } + } + + got_ssnd_chunk= true; + } + else if(status==NORMAL) { /* other chunk */ + if(!strncmp(chunk_id, "COMM", 4)) + fprintf(stderr, "%s: WARNING: skipping extra 'COMM' chunk\n", encoder_wrapper.inbasefilename); + else if(!strncmp(chunk_id, "SSND", 4)) + fprintf(stderr, "%s: WARNING: skipping extra 'SSND' chunk\n", encoder_wrapper.inbasefilename); + else + fprintf(stderr, "%s: WARNING: skipping unknown chunk '%s'\n", encoder_wrapper.inbasefilename, chunk_id); + + /* chunk size */ + if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + status= ERROR; + else { + unsigned long skip= xx+(xx & 1U); + + FLAC__ASSERT(skip<=LONG_MAX); + while(status==NORMAL && skip>0U && fseek(infile, skip, SEEK_CUR)<0) { + unsigned int need= min(skip, sizeof ucbuffer); + if(fread(ucbuffer, 1U, need, infile) 0) { + if(status==DONE) + print_stats(&encoder_wrapper); + fprintf(stderr, "\n"); + } + + if(0 != encoder_wrapper.seek_table.points) + free(encoder_wrapper.seek_table.points); + if(options.common.verify) { + 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, "Verify FAILED! (%s) Do not trust %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfilename); + status= MISMATCH; + } + } + + if(infile != stdin) + fclose(infile); + + if(status==DONE) { + if(info_align_carry >= 0) + fprintf(stderr, "%s: INFO: sector alignment causing %d samples to be carried over\n", encoder_wrapper.inbasefilename, info_align_carry); + if(info_align_zero >= 0) + fprintf(stderr, "%s: INFO: sector alignment causing %d zero samples to be appended\n", encoder_wrapper.inbasefilename, info_align_zero); + } + else if(status==ERROR) + unlink(outfilename); + + return status==ERROR || status==MISMATCH; +} + int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options) { encoder_wrapper_struct encoder_wrapper; @@ -136,7 +541,7 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons unsigned align_remainder = 0; int info_align_carry = -1, info_align_zero = -1; - FLAC__ASSERT(!options.sector_align || options.common.skip == 0); + FLAC__ASSERT(!options.common.sector_align || options.common.skip == 0); encoder_wrapper.encoder = 0; encoder_wrapper.verify = options.common.verify; @@ -178,239 +583,246 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons goto wav_abort_; if(feof(infile)) break; - if(xx == 0x20746d66) { /* "fmt " */ - if(got_fmt_chunk) { - fprintf(stderr, "%s: WARNING: skipping extra 'fmt ' sub-chunk\n", encoder_wrapper.inbasefilename); + if(xx == 0x20746d66 && !got_fmt_chunk) { /* "fmt " */ + /* fmt sub-chunk size */ + if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + goto wav_abort_; + if(xx < 16) { + fprintf(stderr, "%s: ERROR: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_wrapper.inbasefilename, (unsigned)xx); + goto wav_abort_; } - else { - /* fmt sub-chunk size */ - if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) - goto wav_abort_; - if(xx < 16) { - fprintf(stderr, "%s: ERROR: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_wrapper.inbasefilename, (unsigned)xx); - goto wav_abort_; - } - else if(xx != 16 && xx != 18) { - fprintf(stderr, "%s: WARNING: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_wrapper.inbasefilename, (unsigned)xx); - } - data_bytes = xx; - /* compression code */ - if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename)) - goto wav_abort_; - if(x != 1) { - 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, encoder_wrapper.inbasefilename)) - goto wav_abort_; - if(x == 0 || x > FLAC__MAX_CHANNELS) { - fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_wrapper.inbasefilename, (unsigned)x); - goto wav_abort_; - } - else if(options.sector_align && x != 2) { - fprintf(stderr, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_wrapper.inbasefilename, (unsigned)x); - goto wav_abort_; - } - channels = x; - /* sample rate */ - if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) - goto wav_abort_; - if(!FLAC__format_sample_rate_is_valid(xx)) { - fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_wrapper.inbasefilename, (unsigned)xx); - goto wav_abort_; - } - else if(options.sector_align && xx != 44100) { - fprintf(stderr, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\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, encoder_wrapper.inbasefilename)) - goto wav_abort_; - /* block align (ignored) */ - 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, encoder_wrapper.inbasefilename)) - goto wav_abort_; - if(x != 8 && x != 16 && x != 24) { - fprintf(stderr, "%s: ERROR: unsupported bits per sample %u\n", encoder_wrapper.inbasefilename, (unsigned)x); - goto wav_abort_; - } - bps = x; - is_unsigned_samples = (x == 8); + else if(xx != 16 && xx != 18) { + fprintf(stderr, "%s: WARNING: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_wrapper.inbasefilename, (unsigned)xx); + } + data_bytes = xx; + /* compression code */ + if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename)) + goto wav_abort_; + if(x != 1) { + 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, encoder_wrapper.inbasefilename)) + goto wav_abort_; + if(x == 0 || x > FLAC__MAX_CHANNELS) { + fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_wrapper.inbasefilename, (unsigned)x); + goto wav_abort_; + } + else if(options.common.sector_align && x != 2) { + fprintf(stderr, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_wrapper.inbasefilename, (unsigned)x); + goto wav_abort_; + } + channels = x; + /* sample rate */ + if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + goto wav_abort_; + if(!FLAC__format_sample_rate_is_valid(xx)) { + fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_wrapper.inbasefilename, (unsigned)xx); + goto wav_abort_; + } + else if(options.common.sector_align && xx != 44100) { + fprintf(stderr, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\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, encoder_wrapper.inbasefilename)) + goto wav_abort_; + /* block align (ignored) */ + 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, encoder_wrapper.inbasefilename)) + goto wav_abort_; + if(x != 8 && x != 16 && x != 24) { + fprintf(stderr, "%s: ERROR: unsupported bits per sample %u\n", encoder_wrapper.inbasefilename, (unsigned)x); + goto wav_abort_; + } + else if(options.common.sector_align && x != 16) { + fprintf(stderr, "%s: ERROR: file has %u bits per sample, must be 16 for --sector-align\n", encoder_wrapper.inbasefilename, (unsigned)x); + goto wav_abort_; + } + bps = x; + is_unsigned_samples = (x == 8); - /* skip any extra data in the fmt sub-chunk */ - data_bytes -= 16; - if(data_bytes > 0) { + /* skip any extra data in the fmt sub-chunk */ + data_bytes -= 16; + if(data_bytes > 0) { + unsigned left, need; + for(left = data_bytes; left > 0; ) { + need = min(left, CHUNK_OF_SAMPLES); + if(fread(ucbuffer, 1U, need, infile) < need) { + fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_wrapper.inbasefilename); + goto wav_abort_; + } + left -= need; + } + } + + got_fmt_chunk = true; + } + else if(xx == 0x61746164 && !got_data_chunk && got_fmt_chunk) { /* "data" */ + /* data size */ + if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + goto wav_abort_; + data_bytes = xx; + + bytes_per_wide_sample = channels * (bps >> 3); + + if(options.common.skip > 0) { + if(fseek(infile, bytes_per_wide_sample * (unsigned)options.common.skip, SEEK_CUR) < 0) { + /* can't seek input, read ahead manually... */ unsigned left, need; - for(left = data_bytes; left > 0; ) { + for(left = (unsigned)options.common.skip; left > 0; ) { /*@@@ WATCHOUT: 4GB limit */ need = min(left, CHUNK_OF_SAMPLES); - if(fread(ucbuffer, 1, need, infile) < need) { + if(fread(ucbuffer, bytes_per_wide_sample, need, infile) < need) { fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_wrapper.inbasefilename); goto wav_abort_; } left -= need; } } + } - got_fmt_chunk = true; + data_bytes -= (unsigned)options.common.skip * bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */ + encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample + *options.common.align_reservoir_samples; + if(options.common.sector_align) { + align_remainder = (unsigned)(encoder_wrapper.total_samples_to_encode % 588); + if(options.common.is_last_file) + encoder_wrapper.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */ + else + encoder_wrapper.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */ } - } - else if(xx == 0x61746164) { /* "data" */ - if(got_data_chunk) { - fprintf(stderr, "%s: WARNING: skipping extra 'data' sub-chunk\n", encoder_wrapper.inbasefilename); - } - else if(!got_fmt_chunk) { - fprintf(stderr, "%s: ERROR: got 'data' sub-chunk before 'fmt' sub-chunk\n", encoder_wrapper.inbasefilename); + + /* +44 for the size of the WAV headers; this is just an estimate for the progress indicator and doesn't need to be exact */ + encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; + + if(!init_encoder(options.common, channels, bps, sample_rate, &encoder_wrapper)) goto wav_abort_; - } - else { - /* data size */ - if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) + + encoder_wrapper.verify_fifo.into_frames = true; + + /* + * first do any samples in the reservoir + */ + if(options.common.sector_align && *options.common.align_reservoir_samples > 0) { + append_to_verify_fifo(&encoder_wrapper, (const FLAC__int32 * const *)options.common.align_reservoir, channels, *options.common.align_reservoir_samples); + + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); goto wav_abort_; - data_bytes = xx; + } + } - bytes_per_wide_sample = channels * (bps >> 3); + /* + * decrement the data_bytes counter if we need to align the file + */ + if(options.common.sector_align) { + if(options.common.is_last_file) { + *options.common.align_reservoir_samples = 0; + } + else { + *options.common.align_reservoir_samples = align_remainder; + data_bytes -= (*options.common.align_reservoir_samples) * bytes_per_wide_sample; + } + } - if(options.common.skip > 0) { - if(fseek(infile, bytes_per_wide_sample * (unsigned)options.common.skip, SEEK_CUR) < 0) { - /* can't seek input, read ahead manually... */ - unsigned left, need; - for(left = (unsigned)options.common.skip; left > 0; ) { /*@@@ WATCHOUT: 4GB limit */ - need = min(left, CHUNK_OF_SAMPLES); - if(fread(ucbuffer, bytes_per_wide_sample, need, infile) < need) { - fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_wrapper.inbasefilename); - goto wav_abort_; - } - left -= need; + /* + * now do from the file + */ + while(data_bytes > 0) { + 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, "%s: ERROR during read\n", encoder_wrapper.inbasefilename); + goto wav_abort_; + } + else if(feof(infile)) { + 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, "%s: ERROR: got partial sample\n", encoder_wrapper.inbasefilename); + goto wav_abort_; + } + else { + unsigned wide_samples = bytes_read / bytes_per_wide_sample; + format_input(input, wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper); + + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 * const *)input, wide_samples)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + goto wav_abort_; + } + data_bytes -= bytes_read; + } + } + } + + /* + * now read unaligned samples into reservoir or pad with zeroes if necessary + */ + if(options.common.sector_align) { + if(options.common.is_last_file) { + unsigned wide_samples = 588 - align_remainder; + if(wide_samples < 588) { + unsigned channel; + + info_align_zero = wide_samples; + data_bytes = wide_samples * (bps >> 3); + for(channel = 0; channel < channels; channel++) + memset(input[channel], 0, data_bytes); + append_to_verify_fifo(&encoder_wrapper, (const FLAC__int32 * const *)input, channels, wide_samples); + + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 * const *)input, wide_samples)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + goto wav_abort_; } } } - - data_bytes -= (unsigned)options.common.skip * bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */ - encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample + *options.align_reservoir_samples; - if(options.sector_align) { - align_remainder = (unsigned)(encoder_wrapper.total_samples_to_encode % 588); - if(options.is_last_file) - encoder_wrapper.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */ - else - encoder_wrapper.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */ - } - - /* +44 for the size of the WAV headers; this is just an estimate for the progress indicator and doesn't need to be exact */ - encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; - - if(!init_encoder(options.common, channels, bps, sample_rate, &encoder_wrapper)) - goto wav_abort_; - - encoder_wrapper.verify_fifo.into_frames = true; - - /* - * first do any samples in the reservoir - */ - if(options.sector_align && *options.align_reservoir_samples > 0) { - append_to_verify_fifo(&encoder_wrapper, (const FLAC__int32 * const *)options.align_reservoir, channels, *options.align_reservoir_samples); - - if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 * const *)options.align_reservoir, *options.align_reservoir_samples)) { - fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); - goto wav_abort_; - } - } - - /* - * decrement the data_bytes counter if we need to align the file - */ - if(options.sector_align) { - if(options.is_last_file) { - *options.align_reservoir_samples = 0; - } - else { - *options.align_reservoir_samples = align_remainder; - data_bytes -= (*options.align_reservoir_samples) * bytes_per_wide_sample; - } - } - - /* - * now do from the file - */ - while(data_bytes > 0) { - 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)) { + else { + if(*options.common.align_reservoir_samples > 0) { + FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588); + bytes_read = fread(ucbuffer, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile); + if(bytes_read == 0 && ferror(infile)) { fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename); goto wav_abort_; } - else if(feof(infile)) { - 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); + else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) { + fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned)bytes_read, (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, "%s: ERROR: got partial sample\n", encoder_wrapper.inbasefilename); - goto wav_abort_; - } else { - unsigned wide_samples = bytes_read / bytes_per_wide_sample; - format_input(input, wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper); - - if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 * const *)input, wide_samples)) { - fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); - goto wav_abort_; - } - data_bytes -= bytes_read; + info_align_carry = *options.common.align_reservoir_samples; + format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper); } } } - - /* - * now read unaligned samples into reservoir or pad with zeroes if necessary - */ - if(options.sector_align) { - if(options.is_last_file) { - unsigned wide_samples = 588 - align_remainder; - if(wide_samples < 588) { - unsigned channel; - - info_align_zero = wide_samples; - data_bytes = wide_samples * bytes_per_wide_sample; - for(channel = 0; channel < channels; channel++) - memset(input[channel], 0, data_bytes); - append_to_verify_fifo(&encoder_wrapper, (const FLAC__int32 * const *)input, channels, wide_samples); - - if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 * const *)input, wide_samples)) { - fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); - goto wav_abort_; - } - } - } - else { - if(*options.align_reservoir_samples > 0) { - FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588); - bytes_read = fread(ucbuffer, sizeof(unsigned char), (*options.align_reservoir_samples) * bytes_per_wide_sample, infile); - if(bytes_read == 0 && ferror(infile)) { - fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename); - goto wav_abort_; - } - else if(bytes_read != (*options.align_reservoir_samples) * bytes_per_wide_sample) { - fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_wrapper.total_samples_to_encode, (unsigned)encoder_wrapper.samples_written); - data_bytes = 0; - } - else { - info_align_carry = *options.align_reservoir_samples; - format_input(options.align_reservoir, *options.align_reservoir_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper); - } - } - } - } - - got_data_chunk = true; } + + got_data_chunk = true; } else { - 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)); + if(xx == 0x20746d66 && got_fmt_chunk) { /* "fmt " */ + fprintf(stderr, "%s: WARNING: skipping extra 'fmt ' sub-chunk\n", encoder_wrapper.inbasefilename); + } + else if(xx == 0x61746164) { /* "data" */ + if(got_data_chunk) { + fprintf(stderr, "%s: WARNING: skipping extra 'data' sub-chunk\n", encoder_wrapper.inbasefilename); + } + else if(!got_fmt_chunk) { + fprintf(stderr, "%s: ERROR: got 'data' sub-chunk before 'fmt' sub-chunk\n", encoder_wrapper.inbasefilename); + goto wav_abort_; + } + else { + FLAC__ASSERT(0); + } + } + else { + 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, encoder_wrapper.inbasefilename)) goto wav_abort_; @@ -491,6 +903,14 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons encoder_wrapper_struct encoder_wrapper; size_t bytes_read; const size_t bytes_per_wide_sample = options.channels * (options.bps >> 3); + unsigned align_remainder = 0; + int info_align_carry = -1, info_align_zero = -1; + + FLAC__ASSERT(!options.common.sector_align || options.common.skip == 0); + FLAC__ASSERT(!options.common.sector_align || options.channels == 2); + FLAC__ASSERT(!options.common.sector_align || options.bps == 16); + FLAC__ASSERT(!options.common.sector_align || options.sample_rate == 44100); + FLAC__ASSERT(!options.common.sector_align || infilesize >= 0); encoder_wrapper.encoder = 0; encoder_wrapper.verify = options.common.verify; @@ -526,7 +946,19 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0; } else { - encoder_wrapper.total_samples_to_encode = (unsigned)infilesize / bytes_per_wide_sample - options.common.skip; + if(options.common.sector_align) { + FLAC__ASSERT(options.common.skip == 0); + encoder_wrapper.total_samples_to_encode = (unsigned)infilesize / bytes_per_wide_sample + *options.common.align_reservoir_samples; + align_remainder = (unsigned)(encoder_wrapper.total_samples_to_encode % 588); + if(options.common.is_last_file) + encoder_wrapper.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */ + else + encoder_wrapper.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */ + } + else { + encoder_wrapper.total_samples_to_encode = (unsigned)infilesize / bytes_per_wide_sample - options.common.skip; + } + encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample; } @@ -563,6 +995,35 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons encoder_wrapper.verify_fifo.into_frames = true; + /* + * first do any samples in the reservoir + */ + if(options.common.sector_align && *options.common.align_reservoir_samples > 0) { + append_to_verify_fifo(&encoder_wrapper, (const FLAC__int32 * const *)options.common.align_reservoir, options.channels, *options.common.align_reservoir_samples); + + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + goto raw_abort_; + } + } + + /* + * decrement infilesize if we need to align the file + */ + if(options.common.sector_align) { + FLAC__ASSERT(infilesize >= 0); + if(options.common.is_last_file) { + *options.common.align_reservoir_samples = 0; + } + else { + *options.common.align_reservoir_samples = align_remainder; + infilesize -= (long)((*options.common.align_reservoir_samples) * bytes_per_wide_sample); + } + } + + /* + * now do from the file + */ while(!feof(infile)) { if(lookahead_length > 0) { FLAC__ASSERT(lookahead_length < CHUNK_OF_SAMPLES * bytes_per_wide_sample); @@ -598,6 +1059,46 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons } } + /* + * now read unaligned samples into reservoir or pad with zeroes if necessary + */ + if(options.common.sector_align) { + if(options.common.is_last_file) { + unsigned wide_samples = 588 - align_remainder; + if(wide_samples < 588) { + unsigned channel, data_bytes; + + info_align_zero = wide_samples; + data_bytes = wide_samples * (options.bps >> 3); + for(channel = 0; channel < options.channels; channel++) + memset(input[channel], 0, data_bytes); + append_to_verify_fifo(&encoder_wrapper, (const FLAC__int32 * const *)input, options.channels, wide_samples); + + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, (const FLAC__int32 * const *)input, wide_samples)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + goto raw_abort_; + } + } + } + else { + if(*options.common.align_reservoir_samples > 0) { + FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588); + bytes_read = fread(ucbuffer, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile); + if(bytes_read == 0 && ferror(infile)) { + fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename); + goto raw_abort_; + } + else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) { + fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_wrapper.total_samples_to_encode, (unsigned)encoder_wrapper.samples_written); + } + else { + info_align_carry = *options.common.align_reservoir_samples; + format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, false, options.is_unsigned_samples, options.channels, options.bps, &encoder_wrapper); + } + } + } + } + if(encoder_wrapper.encoder) { FLAC__stream_encoder_finish(encoder_wrapper.encoder); FLAC__stream_encoder_delete(encoder_wrapper.encoder); @@ -620,6 +1121,10 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons return 1; } } + if(info_align_carry >= 0) + fprintf(stderr, "%s: INFO: sector alignment causing %d samples to be carried over\n", encoder_wrapper.inbasefilename, info_align_carry); + if(info_align_zero >= 0) + fprintf(stderr, "%s: INFO: sector alignment causing %d zero samples to be appended\n", encoder_wrapper.inbasefilename, info_align_zero); if(infile != stdin) fclose(infile); return 0; @@ -1318,6 +1823,79 @@ FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ } } +FLAC__bool +read_big_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn) +{ + unsigned char buf[4]; + size_t bytes_read= fread(buf, 1, 2, f); + + if(bytes_read==0U && eof_ok) + return true; + else if(bytes_read<2U) { + fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn); + return false; + } + + /* this is independent of host endianness */ + *val= (FLAC__uint16)(buf[0])<<8 | buf[1]; + + return true; +} + +FLAC__bool +read_big_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn) +{ + unsigned char buf[4]; + size_t bytes_read= fread(buf, 1, 4, f); + + if(bytes_read==0U && eof_ok) + return true; + else if(bytes_read<4U) { + fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn); + return false; + } + + /* this is independent of host endianness */ + *val= (FLAC__uint32)(buf[0])<<24 | (FLAC__uint32)(buf[1])<<16 | + (FLAC__uint32)(buf[2])<<8 | buf[3]; + + return true; +} + +FLAC__bool +read_sane_extended(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn) + /* Read an IEEE 754 80-bit (aka SANE) extended floating point value from 'f', + * convert it into an integral value and store in 'val'. Return false if only + * between 1 and 9 bytes remain in 'f', if 0 bytes remain in 'f' and 'eof_ok' is + * false, or if the value is negative, between zero and one, or too large to be + * represented by 'val'; return true otherwise. + */ +{ + unsigned int i; + unsigned char buf[10]; + size_t bytes_read= fread(buf, 1U, 10U, f); + FLAC__int16 e= ((FLAC__uint16)(buf[0])<<8 | (FLAC__uint16)(buf[1]))-0x3FFF; + FLAC__int16 shift= 63-e; + FLAC__uint64 p= 0U; + + if(bytes_read==0U && eof_ok) + return true; + else if(bytes_read<10U) { + fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn); + return false; + } + else if((buf[0]>>7)==1U || e<0 || e>63) { + fprintf(stderr, "%s: ERROR: invalid floating-point value\n", fn); + return false; + } + + for(i= 0U; i<8U; ++i) + p|= (FLAC__uint64)(buf[i+2])<<(56U-i*8); + *val= (FLAC__uint32)(p>>shift)+(p>>(shift-1) & 0x1); + + return true; +} + FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val) { if(!is_big_endian_host) { diff --git a/src/flac/encode.h b/src/flac/encode.h index 0676ecc3..a2529ed1 100644 --- a/src/flac/encode.h +++ b/src/flac/encode.h @@ -43,15 +43,16 @@ typedef struct { int padding; char *requested_seek_points; int num_requested_seek_points; -} encode_options_t; - -typedef struct { - encode_options_t common; + /* options related to --sector-align */ FLAC__bool is_last_file; FLAC__int32 **align_reservoir; unsigned *align_reservoir_samples; FLAC__bool sector_align; +} encode_options_t; + +typedef struct { + encode_options_t common; } wav_encode_options_t; typedef struct { @@ -64,6 +65,7 @@ typedef struct { unsigned sample_rate; } raw_encode_options_t; +int flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options); int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options); int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, raw_encode_options_t options); diff --git a/src/flac/main.c b/src/flac/main.c index 4e5bb486..e6e18b56 100644 --- a/src/flac/main.c +++ b/src/flac/main.c @@ -33,8 +33,12 @@ #include "encode.h" #include "file.h" +typedef enum { RAW, WAV, AIF } FileFormat; + static int short_usage(const char *message, ...); static int long_usage(const char *message, ...); +static void format_mistake(const char *infilename, const char *wrong, const char *right); + static int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file); static int decode_file(const char *infilename, const char *forced_outfilename); @@ -409,8 +413,10 @@ int main(int argc, char *argv[]) return long_usage("ERROR: --sector-align not allowed with --skip\n"); else if(format_channels >= 0 && format_channels != 2) return long_usage("ERROR: --sector-align can only be done with stereo input\n"); - else if(format_sample_rate >= 0 && format_sample_rate != 2) - return long_usage("ERROR: --sector-align can only be done with sample rate of 44100\n"); + else if(format_bps >= 0 && format_bps != 16) + return long_usage("ERROR: --sector-align can only be done with 16-bit samples\n"); + else if(format_sample_rate >= 0 && format_sample_rate != 44100) + return long_usage("ERROR: --sector-align can only be done with a sample rate of 44100\n"); } if(argc - i > 1 && cmdline_forced_outfilename) { return long_usage("ERROR: -o cannot be used with multiple files\n"); @@ -559,7 +565,7 @@ int long_usage(const char *message, ...) fprintf(out, " flac [options] [infile [...]]\n"); fprintf(out, "\n"); fprintf(out, "For encoding:\n"); - fprintf(out, " the input file(s) may be a PCM RIFF WAVE file or raw samples\n"); + fprintf(out, " the input file(s) may be a PCM RIFF WAVE file, AIFF file, or raw samples\n"); fprintf(out, " the output file(s) will be in FLAC format\n"); fprintf(out, "For decoding, the reverse is true\n"); fprintf(out, "\n"); @@ -571,11 +577,11 @@ int long_usage(const char *message, ...) fprintf(out, "since the former allows flac to seek backwards to write the STREAMINFO or\n"); fprintf(out, "RIFF WAVE header contents when necessary.\n"); fprintf(out, "\n"); - fprintf(out, "flac checks for the presence of a RIFF WAVE header to decide whether or not\n"); - fprintf(out, "to treat an input file as WAVE format or raw samples. If any infile is raw\n"); - fprintf(out, "you must specify the format options {-fb|fl} -fc -fp and -fs, which will\n"); - fprintf(out, "apply to all raw files. You can force WAVE files to be treated as a raw files\n"); - fprintf(out, "using -fr.\n"); + fprintf(out, "flac checks for the presence of a AIFF/RIFF WAVE header to decide whether or\n"); + fprintf(out, "not to treat an input file as AIFF/WAVE format or raw samples. If any infile\n"); + fprintf(out, "is raw you must specify the format options {-fb|fl} -fc -fp and -fs, which will\n"); + fprintf(out, "apply to all raw files. You can force AIFF/WAVE files to be treated as a raw\n"); + fprintf(out, "files using -fr.\n"); fprintf(out, "\n"); fprintf(out, "generic options:\n"); fprintf(out, " -d : decode (default behavior is encode)\n"); @@ -668,6 +674,13 @@ int long_usage(const char *message, ...) return message? 1 : 0; } +void +format_mistake(const char *infilename, const char *wrong, const char *right) +{ + fprintf(stderr, "WARNING: %s is not a %s file; treating as a %s file\n", + infilename, wrong, right); +} + int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file) { FILE *encode_infile; @@ -675,7 +688,7 @@ int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bo char *p; FLAC__byte lookahead[12]; unsigned lookahead_length = 0; - FLAC__bool treat_as_wave = false; + FileFormat fmt= RAW; int retval; long infilesize; encode_options_t common_options; @@ -694,34 +707,38 @@ int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bo if(!force_raw_format) { /* first set format based on name */ - if(0 == strcasecmp(infilename+(strlen(infilename)-4), ".wav")) - treat_as_wave = true; - else - treat_as_wave = false; + if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".wav")) + fmt= WAV; + else if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".aif")) + fmt= AIF; + else if(strlen(infilename) > 4 && 0 == strcasecmp(infilename+(strlen(infilename)-5), ".aiff")) + fmt= AIF; /* attempt to guess the file type based on the first 12 bytes */ if((lookahead_length = fread(lookahead, 1, 12, encode_infile)) < 12) { - if(treat_as_wave) - fprintf(stderr, "WARNING: %s is not a WAVE file, treating as a raw file\n", infilename); - treat_as_wave = false; + if(fmt != RAW) + format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw"); + fmt= RAW; } else { - if(strncmp(lookahead, "RIFF", 4) || strncmp(lookahead+8, "WAVE", 4)) { - if(treat_as_wave) - fprintf(stderr, "WARNING: %s is not a WAVE file, treating as a raw file\n", infilename); - treat_as_wave = false; + if(!strncmp(lookahead, "RIFF", 4) && !strncmp(lookahead+8, "WAVE", 4)) + fmt= WAV; + else if(!strncmp(lookahead, "FORM", 4) && !strncmp(lookahead+8, "AIFF", 4)) + fmt= AIF; + else { + if(fmt != RAW) + format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw"); + fmt= RAW; } - else - treat_as_wave = true; } } - if(sector_align && !treat_as_wave && infilesize < 0) { + if(sector_align && fmt == RAW && infilesize < 0) { fprintf(stderr, "ERROR: can't --sector-align when the input size is unknown\n"); return 1; } - if(!treat_as_wave) { + if(fmt == RAW) { if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0) return long_usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n"); } @@ -769,19 +786,12 @@ int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bo common_options.padding = padding; common_options.requested_seek_points = requested_seek_points; common_options.num_requested_seek_points = num_requested_seek_points; + common_options.is_last_file = is_last_file; + common_options.align_reservoir = align_reservoir; + common_options.align_reservoir_samples = &align_reservoir_samples; + common_options.sector_align = sector_align; - if(treat_as_wave) { - wav_encode_options_t options; - - options.common = common_options; - options.is_last_file = is_last_file; - options.align_reservoir = align_reservoir; - options.align_reservoir_samples = &align_reservoir_samples; - options.sector_align = sector_align; - - retval = flac__encode_wav(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options); - } - else { + if(fmt == RAW) { raw_encode_options_t options; options.common = common_options; @@ -793,6 +803,16 @@ int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bo retval = flac__encode_raw(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options); } + else { + wav_encode_options_t options; + + options.common = common_options; + + if(fmt == AIF) + retval = flac__encode_aif(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options); + else + retval = flac__encode_wav(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options); + } if(retval == 0 && strcmp(infilename, "-")) { if(strcmp(forced_outfilename, "-"))