Commit 60431ec4 authored by Josh Coalson's avatar Josh Coalson
Browse files

start code cleanup

parent dc321f08
......@@ -24,7 +24,7 @@
#endif
#include <limits.h> /* for LONG_MAX */
#include <math.h> /* for floor() */
#include <stdio.h> /* for FILE et al. */
#include <stdio.h> /* for FILE etc. */
#include <stdlib.h> /* for malloc */
#include <string.h> /* for strcmp() */
#include "FLAC/all.h"
......@@ -43,26 +43,32 @@
#define CHUNK_OF_SAMPLES 2048
typedef struct {
const char *inbasefilename;
FILE *fout;
const char *outfilename;
FLAC__StreamEncoder *encoder;
#ifdef FLAC__HAS_OGG
FLAC__bool use_ogg;
#endif
FLAC__bool verify;
FLAC__bool verbose;
const char *inbasefilename;
const char *outfilename;
FLAC__bool verify_failed;
FLAC__uint64 unencoded_size;
FLAC__uint64 total_samples_to_encode;
FLAC__uint64 bytes_written;
FLAC__uint64 samples_written;
FLAC__uint64 stream_offset; /* i.e. number of bytes before the first byte of the the first frame's header */
unsigned current_frame;
FLAC__StreamMetadata *seek_table;
unsigned first_seek_point_to_check;
FLAC__bool verify_failed;
union {
FLAC__FileEncoder *flac;
#ifdef FLAC__HAS_OGG
FLAC__bool use_ogg;
OggFLAC__StreamEncoder *ogg_encoder;
OggFLAC__StreamEncoder *ogg;
#endif
} encoder_wrapper_struct;
} encoder;
FILE *fin;
FILE *fout;
FLAC__StreamMetadata *seek_table_template;
} EncoderSession;
static FLAC__bool is_big_endian_host;
......@@ -75,13 +81,12 @@ static FLAC__int32 in[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
static FLAC__int32 *input[FLAC__MAX_CHANNELS];
/* local routines */
static FLAC__bool init(encoder_wrapper_struct *encoder_wrapper);
static FLAC__bool init_encoder(encode_options_t options, unsigned channels, unsigned bps, unsigned sample_rate, encoder_wrapper_struct *encoder_wrapper);
static FLAC__bool convert_to_seek_table(char *requested_seek_points, int num_requested_seek_points, FLAC__uint64 stream_samples, FLAC__StreamMetadata *seek_table);
static FLAC__bool init_encoder(encode_options_t options, unsigned channels, unsigned bps, unsigned sample_rate, EncoderSession *encoder_session);
static FLAC__bool convert_to_seek_table_template(char *requested_seek_points, int num_requested_seek_points, FLAC__uint64 stream_samples, FLAC__StreamMetadata *seek_table_template);
static void format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps);
static FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
static void metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
static void print_stats(const EncoderSession *encoder_session);
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);
......@@ -94,7 +99,7 @@ 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;
EncoderSession encoder_session;
FLAC__uint16 x;
FLAC__uint32 xx;
unsigned int channels= 0U, bps= 0U, sample_rate= 0U, sample_frames= 0U;
......@@ -104,45 +109,12 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
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 = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
encoder_wrapper.first_seek_point_to_check = 0;
encoder_wrapper.verify_failed = false;
#ifdef FLAC__HAS_OGG
encoder_wrapper.use_ogg = options.common.use_ogg;
encoder_wrapper.ogg_encoder = 0;
#endif
if(0 == encoder_wrapper.seek_table) {
fprintf(stderr, "%s: ERROR allocating memory for seek table\n", encoder_wrapper.inbasefilename);
return 1;
}
(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;
if(!EncoderSession_construct(&encoder_session, options.common.use_ogg, options.common.verify, options.common.verbose, infile, infilename, outfilename))
return 1;
/* lookahead[] already has "FORMxxxxAIFF", do sub-chunks */
......@@ -154,7 +126,7 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
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);
fprintf(stderr, "%s: ERROR: incomplete chunk identifier\n", encoder_session.inbasefilename);
status= ERROR;
}
......@@ -163,27 +135,27 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
if(status==NORMAL) {
/* COMM chunk size */
if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_big_endian_uint32(infile, &xx, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: non-standard 'COMM' chunk has length = %u\n", encoder_session.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);
fprintf(stderr, "%s: WARNING: non-standard 'COMM' chunk has length = %u\n", encoder_session.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))
if(!read_big_endian_uint16(infile, &x, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_session.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);
fprintf(stderr, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
status= ERROR;
}
channels= x;
......@@ -191,21 +163,21 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
if(status==NORMAL) {
/* number of sample frames */
if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
status= ERROR;
sample_frames= xx;
}
if(status==NORMAL) {
/* bits per sample */
if(!read_big_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
if(!read_big_endian_uint16(infile, &x, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: unsupported bits per sample %u\n", encoder_session.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);
fprintf(stderr, "%s: ERROR: file has %u bits per sample, must be 16 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
status= ERROR;
}
bps= x;
......@@ -213,14 +185,14 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
if(status==NORMAL) {
/* sample rate */
if(!read_sane_extended(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_sane_extended(infile, &xx, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_session.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);
fprintf(stderr, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)xx);
status= ERROR;
}
sample_rate= xx;
......@@ -231,7 +203,7 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
while(status==NORMAL && skip>0U && fseek(infile, skip, SEEK_CUR)<0) {
unsigned int need= min(skip, sizeof ucbuffer);
if(fread(ucbuffer, 1U, need, infile)<need) {
fprintf(stderr, "%s: ERROR during read while skipping extra COMM data\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: ERROR during read while skipping extra COMM data\n", encoder_session.inbasefilename);
status= ERROR;
}
skip-= need;
......@@ -245,16 +217,16 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
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);
fprintf(stderr, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", encoder_session.inbasefilename);
status= ERROR;
}
if(status==NORMAL) {
/* SSND chunk size */
if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_big_endian_uint32(infile, &xx, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: SSND chunk size inconsistent with sample frame count\n", encoder_session.inbasefilename);
status= ERROR;
}
data_bytes= xx;
......@@ -263,10 +235,10 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
if(status==NORMAL) {
/* offset */
if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
status= ERROR;
else if(xx!=0U) {
fprintf(stderr, "%s: ERROR: offset is %u; must be 0\n", encoder_wrapper.inbasefilename, (unsigned int)xx);
fprintf(stderr, "%s: ERROR: offset is %u; must be 0\n", encoder_session.inbasefilename, (unsigned int)xx);
status= ERROR;
}
offset= xx;
......@@ -274,10 +246,10 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
if(status==NORMAL) {
/* block size */
if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_big_endian_uint32(infile, &xx, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: block size is %u; must be 0\n", encoder_session.inbasefilename, (unsigned int)xx);
status= ERROR;
}
block_size= xx;
......@@ -296,7 +268,7 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
while(status==NORMAL && skip>0 && fseek(infile, skip, SEEK_CUR)<0) {
unsigned int need= min(skip, sizeof ucbuffer);
if(fread(ucbuffer, 1U, need, infile)<need) {
fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
status= ERROR;
}
skip-= need;
......@@ -306,28 +278,28 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
if(status==NORMAL) {
data_bytes-= (8U + (unsigned int)options.common.skip*bytes_per_frame); /*@@@ WATCHOUT: 4GB limit */
encoder_wrapper.total_samples_to_encode= data_bytes/bytes_per_frame + *options.common.align_reservoir_samples;
encoder_session.total_samples_to_encode= data_bytes/bytes_per_frame + *options.common.align_reservoir_samples;
if(options.common.sector_align) {
align_remainder= (unsigned int)(encoder_wrapper.total_samples_to_encode % 588U);
align_remainder= (unsigned int)(encoder_session.total_samples_to_encode % 588U);
if(options.common.is_last_file)
encoder_wrapper.total_samples_to_encode+= (588U-align_remainder); /* will pad with zeroes */
encoder_session.total_samples_to_encode+= (588U-align_remainder); /* will pad with zeroes */
else
encoder_wrapper.total_samples_to_encode-= align_remainder; /* will stop short and carry over to next file */
encoder_session.total_samples_to_encode-= align_remainder; /* will stop short and carry over to next file */
}
/* +54 for the size of the AIFF 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_frame+54;
encoder_session.unencoded_size= encoder_session.total_samples_to_encode*bytes_per_frame+54;
if(!init_encoder(options.common, channels, bps, sample_rate, &encoder_wrapper))
if(!init_encoder(options.common, channels, bps, sample_rate, &encoder_session))
status= ERROR;
}
/* first do any samples in the reservoir */
if(status==NORMAL && options.common.sector_align && *options.common.align_reservoir_samples>0U) {
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)]);
encoder_wrapper.verify_failed = (FLAC__stream_encoder_get_state(encoder_wrapper.encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA);
if(!FLAC__stream_encoder_process(encoder_session.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_session.inbasefilename, FLAC__stream_encoder_get_state(encoder_session.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_session.encoder)]);
encoder_session.verify_failed = (FLAC__stream_encoder_get_state(encoder_session.encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA);
status= ERROR;
}
}
......@@ -348,26 +320,26 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
if(bytes_read==0U) {
if(ferror(infile)) {
fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: ERROR during read\n", encoder_session.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);
fprintf(stderr, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned int)encoder_session.total_samples_to_encode, (unsigned int)encoder_session.samples_written);
data_bytes= 0;
}
}
else {
if(bytes_read % bytes_per_frame != 0U) {
fprintf(stderr, "%s: ERROR: got partial sample\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
status= ERROR;
}
else {
unsigned int frames= bytes_read/bytes_per_frame;
format_input(input, frames, true, false, channels, bps);
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)]);
encoder_wrapper.verify_failed = (FLAC__stream_encoder_get_state(encoder_wrapper.encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA);
if(!FLAC__stream_encoder_process(encoder_session.encoder, (const FLAC__int32 *const *)input, frames)) {
fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_session.inbasefilename, FLAC__stream_encoder_get_state(encoder_session.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_session.encoder)]);
encoder_session.verify_failed = (FLAC__stream_encoder_get_state(encoder_session.encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA);
status= ERROR;
}
else
......@@ -388,9 +360,9 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
for(i= 0U; i<channels; ++i)
memset(input[i], 0, pad_frames*(bps>>3));
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)]);
encoder_wrapper.verify_failed = (FLAC__stream_encoder_get_state(encoder_wrapper.encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA);
if(!FLAC__stream_encoder_process(encoder_session.encoder, (const FLAC__int32 *const *)input, pad_frames)) {
fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_session.inbasefilename, FLAC__stream_encoder_get_state(encoder_session.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_session.encoder)]);
encoder_session.verify_failed = (FLAC__stream_encoder_get_state(encoder_session.encoder) == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA);
status= ERROR;
}
}
......@@ -401,11 +373,11 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
FLAC__ASSERT(CHUNK_OF_SAMPLES>=588U);
if(bytes_read==0U && ferror(infile)) {
fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: ERROR during read\n", encoder_session.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);
fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned int)bytes_read, (unsigned int)encoder_session.total_samples_to_encode, (unsigned int)encoder_session.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);
......@@ -418,7 +390,7 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
unsigned char tmp;
if(fread(&tmp, 1U, 1U, infile)<1U) {
fprintf(stderr, "%s: ERROR during read of SSND pad byte\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: ERROR during read of SSND pad byte\n", encoder_session.inbasefilename);
status= ERROR;
}
}
......@@ -427,14 +399,14 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
}
else if(status==NORMAL) { /* other chunk */
if(!strncmp(chunk_id, "COMM", 4))
fprintf(stderr, "%s: WARNING: skipping extra 'COMM' chunk\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: WARNING: skipping extra 'COMM' chunk\n", encoder_session.inbasefilename);
else if(!strncmp(chunk_id, "SSND", 4))
fprintf(stderr, "%s: WARNING: skipping extra 'SSND' chunk\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: WARNING: skipping extra 'SSND' chunk\n", encoder_session.inbasefilename);
else
fprintf(stderr, "%s: WARNING: skipping unknown chunk '%s'\n", encoder_wrapper.inbasefilename, chunk_id);
fprintf(stderr, "%s: WARNING: skipping unknown chunk '%s'\n", encoder_session.inbasefilename, chunk_id);
/* chunk size */
if(!read_big_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
status= ERROR;
else {
unsigned long skip= xx+(xx & 1U);
......@@ -443,7 +415,7 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
while(status==NORMAL && skip>0U && fseek(infile, skip, SEEK_CUR)<0) {
unsigned int need= min(skip, sizeof ucbuffer);
if(fread(ucbuffer, 1U, need, infile)<need) {
fprintf(stderr, "%s: ERROR during read while skipping unknown chunk\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: ERROR during read while skipping unknown chunk\n", encoder_session.inbasefilename);
status= ERROR;
}
skip-= need;
......@@ -453,47 +425,40 @@ flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const ch
}
if(got_ssnd_chunk==false && sample_frames!=0U) {
fprintf(stderr, "%s: ERROR: missing SSND chunk\n", encoder_wrapper.inbasefilename);
fprintf(stderr, "%s: ERROR: missing SSND chunk\n", encoder_session.inbasefilename);
status= ERROR;
}
if(encoder_wrapper.encoder) {
FLAC__stream_encoder_finish(encoder_wrapper.encoder);
FLAC__stream_encoder_delete(encoder_wrapper.encoder);
}
if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
if(encoder_session.encoder)
FLAC__stream_encoder_finish(encoder_session.encoder);
if(encoder_session.verbose && encoder_session.total_samples_to_encode > 0) {
if(status==DONE)
print_stats(&encoder_wrapper);
print_stats(&encoder_session);
fprintf(stderr, "\n");
}
if(0 != encoder_wrapper.seek_table)
FLAC__metadata_object_delete(encoder_wrapper.seek_table);
if(options.common.verify) {
if(encoder_wrapper.verify_failed) {
fprintf(stderr, "Verify FAILED! Do not trust %s\n", outfilename);
status= MISMATCH;
}
if(encoder_session.verify_failed) {
fprintf(stderr, "Verify FAILED! Do not trust %s\n", 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);
fprintf(stderr, "%s: INFO: sector alignment causing %d samples to be carried over\n", encoder_session.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);
fprintf(stderr, "%s: INFO: sector alignment causing %d zero samples to be appended\n", encoder_session.inbasefilename, info_align_zero);
}
else if(status==ERROR)
unlink(outfilename);
EncoderSession_destroy(&encoder_session);
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;
EncoderSession encoder_session;
FLAC__bool is_unsigned_samples = false;
unsigned channels = 0, bps = 0, sample_rate = 0, data_bytes;
size_t bytes_per_wide_sample, bytes_read;
......@@ -505,111 +470,79 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons
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 = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE);
encoder_wrapper.first_seek_point_to_check = 0;
encoder_wrapper.verify_failed = false;
#ifdef FLAC__HAS_OGG
encoder_wrapper.use_ogg = options.common.use_ogg;
encoder_wrapper.ogg_encoder = 0;
#endif
(void)infilesize;
(void)lookahead;
(void)lookahead_length;
if(0 == encoder_wrapper.seek_table) {
fprintf(stderr, "%s: ERROR allocating memory for seek table\n", encoder_wrapper.inbasefilename);
if(!EncoderSession_construct(&encoder_session, options.common.use_ogg, options.common.verify, options.common.verbose, infile, infilename, outfilename))
return 1;
}
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))
goto wav_abort_;
/*
* lookahead[] already has "RIFFxxxxWAVE", do sub-chunks
*/
while(!feof(infile)) {
if(!read_little_endian_uint32(infile, &xx, true, encoder_wrapper.inbasefilename))
if(!read_little_endian_uint32(infile, &xx, true, encoder_session.inbasefilename))
goto wav_abort_;
if(feof(infile))
break;
if(xx == 0x20746d66 && !got_fmt_chunk) { /* "fmt " */
/* fmt sub-chunk size */
if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_little_endian_uint32(infile, &xx, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_session.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);
fprintf(stderr, "%s: WARNING: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_session.inbasefilename, (unsigned)xx);
}
data_bytes = xx;
/* compression code */
if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
goto wav_abort_;
if(x != 1) {
fprintf(stderr, "%s: ERROR: unsupported compression type %u\n", encoder_wrapper.inbasefilename, (unsigned)x);
fprintf(stderr, "%s: ERROR: unsupported compression type %u\n", encoder_session.inbasefilename, (unsigned)x);
goto wav_abort_;
}
/* number of channels */
if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
if(!read_little_endian_uint16(infile, &x, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_session.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);
fprintf(stderr, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, (unsigned)x);
goto wav_abort_;
}
channels = x;
/* sample rate */
if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename))
if(!read_little_endian_uint32(infile, &xx, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_session.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);
fprintf(stderr, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.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))
if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
goto wav_abort_;
/* block align (ignored) */
if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
goto wav_abort_;
/* bits per sample */
if(!read_little_endian_uint16(infile, &x, false, encoder_wrapper.inbasefilename))
if(!read_little_endian_uint16(infile, &x, false, encoder_session.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);
fprintf(stderr, "%s: ERROR: unsupported bits per sample %u\n", encoder_session.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);