#define CHUNK_OF_SAMPLES 2048
typedef struct {
+ unsigned sample_rate;
+ unsigned channels;
+ unsigned bits_per_sample; /* width of sample point, including 'shift' bits, valid bps is bits_per_sample-shift */
+ unsigned shift; /* # of LSBs samples have been shifted left by */
+ unsigned bytes_per_wide_sample; /* for convenience, always == channels*((bps+7)/8), or 0 if N/A to input format (like FLAC) */
+ FLAC__bool is_unsigned_samples;
+ FLAC__bool is_big_endian;
+ FLAC__uint32 channel_mask;
+} SampleInfo;
+
+/* this is the client_data attached to the FLAC decoder when encoding from a FLAC file */
+typedef struct {
+ off_t filesize;
+ const FLAC__byte *lookahead;
+ unsigned lookahead_length;
+ size_t num_metadata_blocks;
+ FLAC__StreamMetadata *metadata_blocks[1024]; /*@@@ BAD MAGIC number */
+ FLAC__uint64 samples_left_to_process;
+ FLAC__bool fatal_error;
+} FLACDecoderData;
+
+typedef struct {
#if FLAC__HAS_OGG
FLAC__bool use_ogg;
#endif
const char *infilename;
const char *outfilename;
- FLAC__uint64 skip;
- FLAC__uint64 until; /* a value of 0 mean end-of-stream (i.e. --until=-0) */
FLAC__bool treat_warnings_as_errors;
FLAC__bool continue_through_decode_errors;
FLAC__bool replay_gain;
- unsigned channels;
- unsigned bits_per_sample;
- unsigned sample_rate;
- FLAC__uint64 unencoded_size;
- FLAC__uint64 total_samples_to_encode;
+ FLAC__uint64 total_samples_to_encode; /* (i.e. "wide samples" aka "sample frames") WATCHOUT: may be 0 to mean 'unknown' */
+ FLAC__uint64 unencoded_size; /* an estimate of the input size, only used in the progress indicator */
FLAC__uint64 bytes_written;
FLAC__uint64 samples_written;
unsigned stats_mask;
+ SampleInfo info;
+
+ FileFormat format;
+ union {
+ struct {
+ size_t data_bytes;
+ FLAC__bool pad;
+ } iff;
+ struct {
+ FLAC__StreamDecoder *decoder;
+ FLACDecoderData client_data;
+ } flac;
+ } fmt;
+
FLAC__StreamEncoder *encoder;
FILE *fin;
FLAC__StreamMetadata *seek_table_template;
} EncoderSession;
-/* this is data attached to the FLAC decoder when encoding from a FLAC file */
-typedef struct {
- EncoderSession *encoder_session;
- off_t filesize;
- const FLAC__byte *lookahead;
- unsigned lookahead_length;
- size_t num_metadata_blocks;
- FLAC__StreamMetadata *metadata_blocks[1024]; /*@@@ BAD MAGIC number */
- FLAC__uint64 samples_left_to_process;
- FLAC__bool fatal_error;
-} FLACDecoderData;
-
const int FLAC_ENCODE__DEFAULT_PADDING = 8192;
static FLAC__bool is_big_endian_host_;
/*
* local routines
*/
-static FLAC__bool EncoderSession_construct(EncoderSession *e, FLAC__bool use_ogg, FLAC__bool verify, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FILE *infile, const char *infilename, const char *outfilename);
+static FLAC__bool EncoderSession_construct(EncoderSession *e, encode_options_t options, off_t infilesize, FILE *infile, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length);
static void EncoderSession_destroy(EncoderSession *e);
static int EncoderSession_finish_ok(EncoderSession *e, int info_align_carry, int info_align_zero, foreign_metadata_t *foreign_metadata);
static int EncoderSession_finish_error(EncoderSession *e);
-static FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options, FLAC__uint32 channel_mask, unsigned channels, unsigned bps, unsigned sample_rate, const foreign_metadata_t *foreign_metadata, FLACDecoderData *flac_decoder_data);
+static FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options);
static FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples);
static FLAC__bool convert_to_seek_table_template(const char *requested_seek_points, int num_requested_seek_points, FLAC__StreamMetadata *cuesheet, EncoderSession *e);
static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
static FLAC__uint32 limit_channel_mask(FLAC__uint32 mask, unsigned channels);
#endif
-/*
- * public routines
- */
-int flac__encode_aiff(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options, FLAC__bool is_aifc)
+static FLAC__bool get_sample_info_raw(EncoderSession *e, encode_options_t options)
{
- EncoderSession encoder_session;
- unsigned channels = 0, bps = 0, sample_rate = 0, shift = 0, sample_frames = 0;
- size_t channel_map[FLAC__MAX_CHANNELS];
- FLAC__uint16 x;
- FLAC__uint32 xx;
- FLAC__bool got_comm_chunk = false, got_ssnd_chunk = false;
- int info_align_carry = -1, info_align_zero = -1;
- FLAC__bool is_big_endian_pcm = true;
+ e->info.sample_rate = options.format_options.raw.sample_rate;
+ e->info.channels = options.format_options.raw.channels;
+ e->info.bits_per_sample = options.format_options.raw.bps;
+ e->info.shift = 0;
+ e->info.bytes_per_wide_sample = options.format_options.raw.channels * ((options.format_options.raw.bps+7)/8);
+ e->info.is_unsigned_samples = options.format_options.raw.is_unsigned_samples;
+ e->info.is_big_endian = options.format_options.raw.is_big_endian;
+ e->info.channel_mask = 0;
- (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(!
- EncoderSession_construct(
- &encoder_session,
-#if FLAC__HAS_OGG
- options.common.use_ogg,
-#else
- /*use_ogg=*/false,
-#endif
- options.common.verify,
- options.common.treat_warnings_as_errors,
- options.common.continue_through_decode_errors,
- infile,
- infilename,
- outfilename
- )
- )
- return 1;
+ return true;
+}
- /* initialize default channel map that preserves channel order */
- {
- size_t i;
- for(i = 0; i < sizeof(channel_map)/sizeof(channel_map[0]); i++)
- channel_map[i] = i;
- }
+static FLAC__bool get_sample_info_wave(EncoderSession *e, encode_options_t options)
+{
+ FLAC__bool got_fmt_chunk = false, got_data_chunk = false;
+ unsigned sample_rate = 0, channels = 0, bps = 0, shift = 0;
+ FLAC__uint32 channel_mask = 0;
+ size_t data_bytes = 0;
- if(options.foreign_metadata) {
- const char *error;
- if(!flac__foreign_metadata_read_from_aiff(options.foreign_metadata, infilename, &error)) {
- flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", encoder_session.inbasefilename, error);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
+ e->info.is_unsigned_samples = false;
+ e->info.is_big_endian = false;
/*
- * lookahead[] already has "FORMxxxxAIFF", do chunks
+ * lookahead[] already has "RIFFxxxxWAVE", do "fmt" chunk and beginning of "data" chunk
*/
- while(!feof(infile)) {
+ while(!feof(e->fin) && !got_data_chunk) {
char chunk_id[5] = { '\0', '\0', '\0', '\0', '\0' }; /* one extra byte for terminating NUL so we can also treat it like a C string */
- if(!read_bytes(infile, (FLAC__byte*)chunk_id, 4, /*eof_ok=*/true, encoder_session.inbasefilename)) {
- flac__utils_printf(stderr, 1, "%s: ERROR: incomplete chunk identifier\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ if(!read_bytes(e->fin, (FLAC__byte*)chunk_id, 4, /*eof_ok=*/true, e->inbasefilename)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: incomplete chunk identifier\n", e->inbasefilename);
+ return false;
}
- if(feof(infile))
+ if(feof(e->fin))
break;
- if(!memcmp(chunk_id, "COMM", 4) && !got_comm_chunk) { /* common chunk */
- unsigned long skip;
- const FLAC__uint32 minimum_comm_size = (is_aifc? 22 : 18);
-
- /* COMM chunk size */
- if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- else if(xx < minimum_comm_size) {
- flac__utils_printf(stderr, 1, "%s: ERROR: non-standard %s 'COMM' chunk has length = %u\n", encoder_session.inbasefilename, is_aifc? "AIFF-C" : "AIFF", (unsigned int)xx);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(!is_aifc && xx != minimum_comm_size) {
- flac__utils_printf(stderr, 1, "%s: WARNING: non-standard %s 'COMM' chunk has length = %u, expected %u\n", encoder_session.inbasefilename, is_aifc? "AIFF-C" : "AIFF", (unsigned int)xx, minimum_comm_size);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- skip = (xx-minimum_comm_size)+(xx & 1U);
-
- /* number of channels */
- if(!read_uint16(infile, /*big_endian=*/true, &x, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- else if(x ==0U || x>FLAC__MAX_CHANNELS) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u\n", encoder_session.inbasefilename, (unsigned int)x);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(x>2U && !options.common.channel_map_none) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u for AIFF\n", encoder_session.inbasefilename, (unsigned int)x);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(options.common.sector_align && x!=2U) {
- flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
- return EncoderSession_finish_error(&encoder_session);
- }
- channels = x;
-
- /* number of sample frames */
- if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- sample_frames = xx;
-
- /* bits per sample */
- if(!read_uint16(infile, /*big_endian=*/true, &x, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- else if(x<4U || x>24U) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits-per-sample %u\n", encoder_session.inbasefilename, (unsigned int)x);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(options.common.sector_align && x!=16U) {
- flac__utils_printf(stderr, 1, "%s: ERROR: file has %u bits-per-sample, must be 16 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
- return EncoderSession_finish_error(&encoder_session);
- }
- bps = x;
- shift = (bps%8)? 8-(bps%8) : 0; /* SSND data is always byte-aligned, left-justified but format_input() will double-check */
- bps += shift;
-
- /* sample rate */
- if(!read_sane_extended(infile, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- else if(!FLAC__format_sample_rate_is_valid(xx)) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, (unsigned int)xx);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(options.common.sector_align && xx!=44100U) {
- flac__utils_printf(stderr, 1, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)xx);
- return EncoderSession_finish_error(&encoder_session);
- }
- sample_rate = xx;
-
- /* check compression type for AIFF-C */
- if(is_aifc) {
- if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- if(xx == 0x736F7774) /* "sowt" */
- is_big_endian_pcm = false;
- else if(xx == 0x4E4F4E45) /* "NONE" */
- ; /* nothing to do, we already default to big-endian */
- else {
- flac__utils_printf(stderr, 1, "%s: ERROR: can't handle AIFF-C compression type \"%c%c%c%c\"\n", encoder_session.inbasefilename, (char)(xx>>24), (char)((xx>>16)&8), (char)((xx>>8)&8), (char)(xx&8));
- return EncoderSession_finish_error(&encoder_session);
- }
- }
-
- /* set channel mapping */
- /* FLAC order follows SMPTE and WAVEFORMATEXTENSIBLE but with fewer channels, which are: */
- /* front left, front right, center, LFE, back left, back right, surround left, surround right */
- /* specs say the channel ordering is:
- * 1 2 3 4 5 6
- * ___________________________________________________
- * 2 stereo l r
- * 3 l r c
- * 4 l c r S
- * quad (ambiguous with 4ch) Fl Fr Bl Br
- * 5 Fl Fr Fc Sl Sr
- * 6 l lc c r rc S
- * l:left r:right c:center Fl:front-left Fr:front-right Bl:back-left Br:back-right Lc:left-center Rc:right-center S:surround
- * so we only have unambiguous mappings for 2, 3, and 5 channels
- */
- if(
- options.common.channel_map_none ||
- channels == 1 || /* 1 channel: (mono) */
- channels == 2 || /* 2 channels: left, right */
- channels == 3 || /* 3 channels: left, right, center */
- channels == 5 /* 5 channels: front left, front right, center, surround left, surround right */
- ) {
- /* keep default channel order */
- }
- else {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u for AIFF\n", encoder_session.inbasefilename, channels);
- return EncoderSession_finish_error(&encoder_session);
- }
+ if(!memcmp(chunk_id, "fmt ", 4)) { /* format chunk */
+ FLAC__uint16 x;
+ FLAC__uint32 xx;
+ FLAC__uint16 wFormatTag; /* wFormatTag word from the 'fmt ' chunk */
+ unsigned block_align;
- /* skip any extra data in the COMM chunk */
- if(!fskip_ahead(infile, skip)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over extra COMM data\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ if(got_fmt_chunk) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: file has multiple 'fmt ' chunks\n", e->inbasefilename);
+ return false;
}
- /*
- * now that we know the sample rate, canonicalize the
- * --skip string to a number of samples:
+ /* see
+ * http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
+ * http://windowssdk.msdn.microsoft.com/en-us/library/ms713497.aspx
+ * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/audio_r/hh/Audio_r/aud-prop_d40f094e-44f9-4baa-8a15-03e4fb369501.xml.asp
+ *
+ * WAVEFORMAT is
+ * 4 byte: chunk size
+ * 2 byte: format type: 1 for WAVE_FORMAT_PCM, 65534 for WAVE_FORMAT_EXTENSIBLE
+ * 2 byte: # channels
+ * 4 byte: sample rate (Hz)
+ * 4 byte: avg bytes per sec
+ * 2 byte: block align
+ * 2 byte: bits per sample (not necessarily all significant)
+ * WAVEFORMATEX adds
+ * 2 byte: extension size in bytes (usually 0 for WAVEFORMATEX and 22 for WAVEFORMATEXTENSIBLE with PCM)
+ * WAVEFORMATEXTENSIBLE adds
+ * 2 byte: valid bits per sample
+ * 4 byte: channel mask
+ * 16 byte: subformat GUID, first 2 bytes have format type, 1 being PCM
+ *
+ * Current spec says WAVEFORMATEX with PCM must have bps == 8 or 16, or any multiple of 8 for WAVEFORMATEXTENSIBLE.
+ * Lots of old broken WAVEs/apps have don't follow it, e.g. 20 bps but a block align of 3/6 for mono/stereo.
+ *
+ * Block align for WAVE_FORMAT_PCM or WAVE_FORMAT_EXTENSIBLE is also supposed to be channels*bps/8
+ *
+ * If the channel mask has more set bits than # of channels, the extra MSBs are ignored.
+ * If the channel mask has less set bits than # of channels, the extra channels are unassigned to any speaker.
+ *
+ * Data is supposed to be unsigned for bps <= 8 else signed.
*/
- flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, sample_rate);
- FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
- encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
- FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
-
- got_comm_chunk = true;
- }
- else if(!memcmp(chunk_id, "SSND", 4) && !got_ssnd_chunk) { /* sound data chunk */
- unsigned offset = 0, block_size = 0, align_remainder = 0, data_bytes;
- FLAC__uint64 total_samples_in_input, trim = 0;
- FLAC__bool pad = false;
- const size_t bytes_per_wide_sample = channels * (bps >> 3);
- if(got_comm_chunk == false) {
- flac__utils_printf(stderr, 1, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ /* fmt chunk size */
+ if(!read_uint32(e->fin, /*big_endian=*/false, &xx, e->inbasefilename))
+ return false;
+ data_bytes = xx;
+ if(data_bytes < 16) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: non-standard 'fmt ' chunk has length = %u\n", e->inbasefilename, (unsigned)data_bytes);
+ return false;
}
- /* SSND chunk size */
- if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- if(options.common.ignore_chunk_sizes) {
- FLAC__ASSERT(!options.common.sector_align);
- data_bytes = (unsigned)(-(int)bytes_per_wide_sample); /* max out data_bytes; we'll use EOF as signal to stop reading */
- }
- else {
- data_bytes = xx;
- data_bytes-= 8U; /* discount the offset and block size fields */
+ /* format code */
+ if(!read_uint16(e->fin, /*big_endian=*/false, &wFormatTag, e->inbasefilename))
+ return false;
+ if(wFormatTag != 1 /*WAVE_FORMAT_PCM*/ && wFormatTag != 65534 /*WAVE_FORMAT_EXTENSIBLE*/) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: unsupported format type %u\n", e->inbasefilename, (unsigned)wFormatTag);
+ return false;
}
- pad = (data_bytes & 1U) ? true : false;
-
- /* offset */
- if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- offset = xx;
- data_bytes-= offset;
- /* block size */
- if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- else if(xx!=0U) {
- flac__utils_printf(stderr, 1, "%s: ERROR: block size is %u; must be 0\n", encoder_session.inbasefilename, (unsigned int)xx);
- return EncoderSession_finish_error(&encoder_session);
- }
- block_size = xx;
+ /* number of channels */
+ if(!read_uint16(e->fin, /*big_endian=*/false, &x, e->inbasefilename))
+ return false;
+ channels = (unsigned)x;
- /* skip any SSND offset bytes */
- FLAC__ASSERT(offset<=LONG_MAX);
- if(!fskip_ahead(infile, offset)) {
- flac__utils_printf(stderr, 1, "%s: ERROR: skipping offset in SSND chunk\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- if(data_bytes!=(sample_frames*bytes_per_wide_sample)) {
- flac__utils_printf(stderr, 1, "%s: ERROR: SSND chunk size inconsistent with sample frame count\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
+ /* sample rate */
+ if(!read_uint32(e->fin, /*big_endian=*/false, &xx, e->inbasefilename))
+ return false;
+ sample_rate = xx;
- /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
- FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
- total_samples_in_input = data_bytes / bytes_per_wide_sample + *options.common.align_reservoir_samples;
+ /* avg bytes per second (ignored) */
+ if(!read_uint32(e->fin, /*big_endian=*/false, &xx, e->inbasefilename))
+ return false;
+ /* block align */
+ if(!read_uint16(e->fin, /*big_endian=*/false, &x, e->inbasefilename))
+ return false;
+ block_align = (unsigned)x;
+ /* bits per sample */
+ if(!read_uint16(e->fin, /*big_endian=*/false, &x, e->inbasefilename))
+ return false;
+ bps = (unsigned)x;
- /*
- * now that we know the input size, canonicalize the
- * --until string to an absolute sample number:
- */
- if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, sample_rate, encoder_session.skip, total_samples_in_input))
- return EncoderSession_finish_error(&encoder_session);
- encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
- FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
+ e->info.is_unsigned_samples = (bps <= 8);
- if(encoder_session.skip>0U) {
- if(!fskip_ahead(infile, encoder_session.skip*bytes_per_wide_sample)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ if(wFormatTag == 1) {
+ if(bps != 8 && bps != 16) {
+ if(bps == 24 || bps == 32) {
+ /* let these slide with a warning since they're unambiguous */
+ flac__utils_printf(stderr, 1, "%s: WARNING: legacy WAVE file has format type %u but bits-per-sample=%u\n", e->inbasefilename, (unsigned)wFormatTag, bps);
+ if(e->treat_warnings_as_errors)
+ return false;
+ }
+ else {
+ /* @@@ we could add an option to specify left- or right-justified blocks so we knew how to set 'shift' */
+ flac__utils_printf(stderr, 1, "%s: ERROR: legacy WAVE file has format type %u but bits-per-sample=%u\n", e->inbasefilename, (unsigned)wFormatTag, bps);
+ return false;
+ }
}
- }
-
- data_bytes-= (unsigned int)encoder_session.skip*bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */
- if(options.common.ignore_chunk_sizes) {
- encoder_session.total_samples_to_encode = 0;
- flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
- FLAC__ASSERT(0 == encoder_session.until);
- }
- else {
- encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
- }
- if(encoder_session.until > 0) {
- trim = total_samples_in_input - encoder_session.until;
- FLAC__ASSERT(total_samples_in_input > 0);
- FLAC__ASSERT(!options.common.sector_align);
- data_bytes-= (unsigned int)trim*bytes_per_wide_sample;
- encoder_session.total_samples_to_encode-= trim;
- }
- if(options.common.sector_align) {
- align_remainder = (unsigned int)(encoder_session.total_samples_to_encode % 588U);
- if(options.common.is_last_file)
- encoder_session.total_samples_to_encode+= (588U-align_remainder); /* will pad with zeroes */
- else
- 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_session.unencoded_size = encoder_session.total_samples_to_encode*bytes_per_wide_sample+54;
-
- if(!EncoderSession_init_encoder(&encoder_session, options.common, /*channel_mask=*/0, channels, bps-shift, sample_rate, options.foreign_metadata, /*flac_decoder_data=*/0))
- return EncoderSession_finish_error(&encoder_session);
-
- /* first do any samples in the reservoir */
- if(options.common.sector_align && *options.common.align_reservoir_samples>0U) {
-
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
+#if 0 /* @@@ reinstate once we can get an answer about whether the samples are left- or right-justified */
+ if((bps+7)/8 * channels == block_align) {
+ if(bps % 8) {
+ /* assume legacy file is byte aligned with some LSBs zero; this is double-checked in format_input() */
+ flac__utils_printf(stderr, 1, "%s: WARNING: legacy WAVE file (format type %d) has block alignment=%u, bits-per-sample=%u, channels=%u\n", e->inbasefilename, (unsigned)wFormatTag, block_align, bps, channels);
+ if(e->treat_warnings_as_errors)
+ return false;
+ shift = 8 - (bps % 8);
+ bps += shift;
+ }
+ else
+ shift = 0;
}
- }
-
- /* 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 = 0U;
else {
- *options.common.align_reservoir_samples = align_remainder;
- data_bytes-= (*options.common.align_reservoir_samples)*bytes_per_wide_sample;
- }
- }
-
- /* now do from the file */
- while(data_bytes>0) {
- size_t bytes_read = fread(ucbuffer_, 1U, min(data_bytes, CHUNK_OF_SAMPLES*bytes_per_wide_sample), infile);
-
- if(bytes_read == 0U) {
- if(ferror(infile)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(feof(infile)) {
- if(options.common.ignore_chunk_sizes) {
- flac__utils_printf(stderr, 1, "%s: INFO: hit EOF with --ignore-chunk-sizes, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.samples_written);
- }
- else {
- flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- data_bytes = 0;
- }
- }
- else {
- if(bytes_read % bytes_per_wide_sample != 0U) {
- flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else {
- unsigned int frames = bytes_read/bytes_per_wide_sample;
- if(!format_input(input_, frames, is_big_endian_pcm, /*is_unsigned_samples=*/false, channels, bps, shift, channel_map))
- return EncoderSession_finish_error(&encoder_session);
-
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, frames)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
- else
- data_bytes-= bytes_read;
- }
- }
- }
-
- if(trim>0) {
- FLAC__ASSERT(!options.common.sector_align);
- if(!fskip_ahead(infile, trim*bytes_per_wide_sample)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
-
- /* now read unaligned samples into reservoir or pad with zeroes if necessary */
- if(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<channels; ++i)
- memset(input_[i], 0, sizeof(input_[0][0])*pad_frames);
-
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, pad_frames)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
- }
- }
- else {
- if(*options.common.align_reservoir_samples > 0) {
- size_t bytes_read = fread(ucbuffer_, 1U, (*options.common.align_reservoir_samples)*bytes_per_wide_sample, infile);
-
- FLAC__ASSERT(CHUNK_OF_SAMPLES>=588U);
- if(bytes_read == 0U && ferror(infile)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
- flac__utils_printf(stderr, 1, "%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);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- else {
- info_align_carry = *options.common.align_reservoir_samples;
- if(!format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, is_big_endian_pcm, /*is_unsigned_samples=*/false, channels, bps, shift, channel_map))
- return EncoderSession_finish_error(&encoder_session);
- }
- }
- }
- }
-
- if(pad == true) {
- unsigned char tmp;
-
- if(fread(&tmp, 1U, 1U, infile) < 1U) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read of SSND pad byte\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
-
- got_ssnd_chunk = true;
- }
- else {
- if(!memcmp(chunk_id, "SSND", 4) && !got_comm_chunk) {
- flac__utils_printf(stderr, 1, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
-
- if(!options.foreign_metadata) {
- if(!memcmp(chunk_id, "COMM", 4))
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'COMM' chunk (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename);
- else if(!memcmp(chunk_id, "SSND", 4))
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'SSND' chunk (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename);
- else
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s' (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename, chunk_id);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
-
- /* chunk size */
- if(!read_uint32(infile, /*big_endian=*/true, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- else {
- unsigned long skip = xx+(xx & 1U);
-
- FLAC__ASSERT(skip<=LONG_MAX);
- if(!fskip_ahead(infile, skip)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over unsupported chunk\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
- }
- }
-
- if(got_ssnd_chunk == false && sample_frames != 0U) {
- flac__utils_printf(stderr, 1, "%s: ERROR: missing SSND chunk\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
-
- return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, options.foreign_metadata);
-}
-
-int flac__encode_wave(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options)
-{
- EncoderSession encoder_session;
- FLAC__bool is_unsigned_samples = false;
- unsigned channels = 0, bps = 0, sample_rate = 0, shift = 0;
- size_t channel_map[FLAC__MAX_CHANNELS];
- FLAC__uint16 x, format; /* format is the wFormatTag word from the 'fmt ' chunk */
- FLAC__uint32 xx, channel_mask = 0;
- FLAC__bool got_fmt_chunk = false, got_data_chunk = false;
- int info_align_carry = -1, info_align_zero = -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(!
- EncoderSession_construct(
- &encoder_session,
-#if FLAC__HAS_OGG
- options.common.use_ogg,
-#else
- /*use_ogg=*/false,
-#endif
- options.common.verify,
- options.common.treat_warnings_as_errors,
- options.common.continue_through_decode_errors,
- infile,
- infilename,
- outfilename
- )
- )
- return 1;
-
- /* initialize default channel map that preserves channel order */
- {
- size_t i;
- for(i = 0; i < sizeof(channel_map)/sizeof(channel_map[0]); i++)
- channel_map[i] = i;
- }
-
- if(options.foreign_metadata) {
- const char *error;
- if(!flac__foreign_metadata_read_from_wave(options.foreign_metadata, infilename, &error)) {
- flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", encoder_session.inbasefilename, error);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
-
- /*
- * lookahead[] already has "RIFFxxxxWAVE", do chunks
- */
- while(!feof(infile)) {
- char chunk_id[5] = { '\0', '\0', '\0', '\0', '\0' }; /* one extra byte for terminating NUL so we can also treat it like a C string */
- if(!read_bytes(infile, (FLAC__byte*)chunk_id, 4, /*eof_ok=*/true, encoder_session.inbasefilename)) {
- flac__utils_printf(stderr, 1, "%s: ERROR: incomplete chunk identifier\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- if(feof(infile))
- break;
-
- if(!memcmp(chunk_id, "fmt ", 4) && !got_fmt_chunk) { /* format chunk */
- unsigned block_align, data_bytes;
-
- /* see
- * http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
- * http://windowssdk.msdn.microsoft.com/en-us/library/ms713497.aspx
- * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/audio_r/hh/Audio_r/aud-prop_d40f094e-44f9-4baa-8a15-03e4fb369501.xml.asp
- *
- * WAVEFORMAT is
- * 4 byte: chunk size
- * 2 byte: format type: 1 for WAVE_FORMAT_PCM, 65534 for WAVE_FORMAT_EXTENSIBLE
- * 2 byte: # channels
- * 4 byte: sample rate (Hz)
- * 4 byte: avg bytes per sec
- * 2 byte: block align
- * 2 byte: bits per sample (not necessarily all significant)
- * WAVEFORMATEX adds
- * 2 byte: extension size in bytes (usually 0 for WAVEFORMATEX and 22 for WAVEFORMATEXTENSIBLE with PCM)
- * WAVEFORMATEXTENSIBLE adds
- * 2 byte: valid bits per sample
- * 4 byte: channel mask
- * 16 byte: subformat GUID, first 2 bytes have format type, 1 being PCM
- *
- * Current spec says WAVEFORMATEX with PCM must have bps == 8 or 16, or any multiple of 8 for WAVEFORMATEXTENSIBLE.
- * Lots of old broken WAVEs/apps have don't follow it, e.g. 20 bps but a block align of 3/6 for mono/stereo.
- *
- * Block align for WAVE_FORMAT_PCM or WAVE_FORMAT_EXTENSIBLE is also supposed to be channels*bps/8
- *
- * If the channel mask has more set bits than # of channels, the extra MSBs are ignored.
- * If the channel mask has less set bits than # of channels, the extra channels are unassigned to any speaker.
- *
- * Data is supposed to be unsigned for bps <= 8 else signed.
- */
-
- /* fmt chunk size */
- if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- data_bytes = xx;
- if(data_bytes < 16) {
- flac__utils_printf(stderr, 1, "%s: ERROR: found non-standard 'fmt ' chunk which has length = %u\n", encoder_session.inbasefilename, data_bytes);
- return EncoderSession_finish_error(&encoder_session);
- }
- /* format code */
- if(!read_uint16(infile, /*big_endian=*/false, &format, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- if(format != 1 /*WAVE_FORMAT_PCM*/ && format != 65534 /*WAVE_FORMAT_EXTENSIBLE*/) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported format type %u\n", encoder_session.inbasefilename, (unsigned)format);
- return EncoderSession_finish_error(&encoder_session);
- }
- /* number of channels */
- if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- channels = (unsigned)x;
- if(channels == 0 || channels > FLAC__MAX_CHANNELS) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number of channels %u\n", encoder_session.inbasefilename, channels);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(options.common.sector_align && channels != 2) {
- flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, channels);
- return EncoderSession_finish_error(&encoder_session);
- }
- /* sample rate */
- if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- sample_rate = xx;
- if(!FLAC__format_sample_rate_is_valid(sample_rate)) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, sample_rate);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(options.common.sector_align && sample_rate != 44100) {
- flac__utils_printf(stderr, 1, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.inbasefilename, sample_rate);
- return EncoderSession_finish_error(&encoder_session);
- }
- /* avg bytes per second (ignored) */
- if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- /* block align */
- if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- block_align = (unsigned)x;
- /* bits per sample */
- if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- bps = (unsigned)x;
- is_unsigned_samples = (bps <= 8);
- if(format == 1) {
- if(bps != 8 && bps != 16) {
- if(bps == 24 || bps == 32) {
- /* let these slide with a warning since they're unambiguous */
- flac__utils_printf(stderr, 1, "%s: WARNING: legacy WAVE file has format type %u but bits-per-sample=%u\n", encoder_session.inbasefilename, (unsigned)format, bps);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- else {
- /* @@@ we could add an option to specify left- or right-justified blocks so we knew how to set 'shift' */
- flac__utils_printf(stderr, 1, "%s: ERROR: legacy WAVE file has format type %u but bits-per-sample=%u\n", encoder_session.inbasefilename, (unsigned)format, bps);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
-#if 0 /* @@@ reinstate once we can get an answer about whether the samples are left- or right-justified */
- if((bps+7)/8 * channels == block_align) {
- if(bps % 8) {
- /* assume legacy file is byte aligned with some LSBs zero; this is double-checked in format_input() */
- flac__utils_printf(stderr, 1, "%s: WARNING: legacy WAVE file (format type %d) has block alignment=%u, bits-per-sample=%u, channels=%u\n", encoder_session.inbasefilename, (unsigned)format, block_align, bps, channels);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- shift = 8 - (bps % 8);
- bps += shift;
- }
- else
- shift = 0;
- }
- else {
- flac__utils_printf(stderr, 1, "%s: ERROR: illegal WAVE file (format type %d) has block alignment=%u, bits-per-sample=%u, channels=%u\n", encoder_session.inbasefilename, (unsigned)format, block_align, bps, channels);
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: illegal WAVE file (format type %d) has block alignment=%u, bits-per-sample=%u, channels=%u\n", e->inbasefilename, (unsigned)wFormatTag, block_align, bps, channels);
+ return false;
}
#else
shift = 0;
#endif
- if(channels > 2 && !options.common.channel_map_none) {
- flac__utils_printf(stderr, 1, "%s: ERROR: WAVE has >2 channels but is not WAVE_FORMAT_EXTENSIBLE; cannot assign channels\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ if(channels > 2 && !options.channel_map_none) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: WAVE has >2 channels but is not WAVE_FORMAT_EXTENSIBLE; cannot assign channels\n", e->inbasefilename);
+ return false;
}
FLAC__ASSERT(data_bytes >= 16);
data_bytes -= 16;
}
else {
if(data_bytes < 40) {
- flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with size %u\n", encoder_session.inbasefilename, data_bytes);
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with size %u\n", e->inbasefilename, (unsigned)data_bytes);
+ return false;
}
/* cbSize */
- if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
+ if(!read_uint16(e->fin, /*big_endian=*/false, &x, e->inbasefilename))
+ return false;
if(x < 22) {
- flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with cbSize %u\n", encoder_session.inbasefilename, (unsigned)x);
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with cbSize %u\n", e->inbasefilename, (unsigned)x);
+ return false;
}
/* valid bps */
- if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
+ if(!read_uint16(e->fin, /*big_endian=*/false, &x, e->inbasefilename))
+ return false;
if((unsigned)x > bps) {
- flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with wValidBitsPerSample (%u) > wBitsPerSample (%u)\n", encoder_session.inbasefilename, (unsigned)x, bps);
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: invalid WAVEFORMATEXTENSIBLE chunk with wValidBitsPerSample (%u) > wBitsPerSample (%u)\n", e->inbasefilename, (unsigned)x, bps);
+ return false;
}
shift = bps - (unsigned)x;
/* channel mask */
- if(!read_uint32(infile, /*big_endian=*/false, &channel_mask, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
+ if(!read_uint32(e->fin, /*big_endian=*/false, &channel_mask, e->inbasefilename))
+ return false;
/* for mono/stereo and unassigned channels, we fake the mask */
if(channel_mask == 0) {
if(channels == 1)
#if 0
/* @@@ example for dolby/vorbis order, for reference later in case it becomes important */
if(
- options.common.channel_map_none ||
+ options.channel_map_none ||
channel_mask == 0x0001 || /* 1 channel: (mono) */
channel_mask == 0x0003 || /* 2 channels: front left, front right */
channel_mask == 0x0033 || /* 4 channels: front left, front right, back left, back right */
}
#else
if(
- options.common.channel_map_none ||
+ options.channel_map_none ||
channel_mask == 0x0001 || /* 1 channel: (mono) */
channel_mask == 0x0003 || /* 2 channels: front left, front right */
channel_mask == 0x0007 || /* 3 channels: front left, front right, front center */
}
#endif
else {
- flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk with unsupported channel mask=0x%04X\n\nUse --channel-map=none option to store channels in current order; FLAC files\nmust also be decoded with --channel-map=none to restore correct order.\n", encoder_session.inbasefilename, (unsigned)channel_mask);
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk with unsupported channel mask=0x%04X\n\nUse --channel-map=none option to store channels in current order; FLAC files\nmust also be decoded with --channel-map=none to restore correct order.\n", e->inbasefilename, (unsigned)channel_mask);
+ return false;
}
- if(!options.common.channel_map_none) {
+ if(!options.channel_map_none) {
if(count_channel_mask_bits(channel_mask) < channels) {
- flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk: channel mask 0x%04X has unassigned channels (#channels=%u)\n", encoder_session.inbasefilename, (unsigned)channel_mask, channels);
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk: channel mask 0x%04X has unassigned channels (#channels=%u)\n", e->inbasefilename, (unsigned)channel_mask, channels);
+ return false;
}
#if 0
/* supporting this is too difficult with channel mapping; e.g. what if mask is 0x003f but #channels=4?
channel_mask = limit_channel_mask(channel_mask, channels);
#else
else if(count_channel_mask_bits(channel_mask) > channels) {
- flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk: channel mask 0x%04X has extra bits for non-existant channels (#channels=%u)\n", encoder_session.inbasefilename, (unsigned)channel_mask, channels);
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: WAVEFORMATEXTENSIBLE chunk: channel mask 0x%04X has extra bits for non-existant channels (#channels=%u)\n", e->inbasefilename, (unsigned)channel_mask, channels);
+ return false;
}
#endif
}
/* first part of GUID */
- if(!read_uint16(infile, /*big_endian=*/false, &x, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
+ if(!read_uint16(e->fin, /*big_endian=*/false, &x, e->inbasefilename))
+ return false;
if(x != 1) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported WAVEFORMATEXTENSIBLE chunk with non-PCM format %u\n", encoder_session.inbasefilename, (unsigned)x);
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: unsupported WAVEFORMATEXTENSIBLE chunk with non-PCM format %u\n", e->inbasefilename, (unsigned)x);
+ return false;
}
data_bytes -= 26;
}
- if(bps-shift < 4 || bps-shift > 24) {
- flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits-per-sample %u\n", encoder_session.inbasefilename, bps-shift);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(options.common.sector_align && bps-shift != 16) {
- flac__utils_printf(stderr, 1, "%s: ERROR: file has %u bits-per-sample, must be 16 for --sector-align\n", encoder_session.inbasefilename, bps-shift);
- return EncoderSession_finish_error(&encoder_session);
- }
+ e->info.bytes_per_wide_sample = channels * (bps / 8);
/* skip any extra data in the fmt chunk */
- if(!fskip_ahead(infile, data_bytes)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over extra 'fmt' data\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ if(!fskip_ahead(e->fin, data_bytes)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over extra 'fmt' data\n", e->inbasefilename);
+ return false;
}
- /*
- * now that we know the sample rate, canonicalize the
- * --skip string to a number of samples:
- */
- flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, sample_rate);
- FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
- encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
- FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
-
got_fmt_chunk = true;
}
- else if(!memcmp(chunk_id, "data", 4) && !got_data_chunk && got_fmt_chunk) { /* data chunk */
- unsigned align_remainder = 0, data_bytes;
- FLAC__uint64 total_samples_in_input, trim = 0;
- FLAC__bool pad = false;
- const size_t bytes_per_wide_sample = channels * (bps >> 3);
+ else if(!memcmp(chunk_id, "data", 4)) { /* data chunk */
+ FLAC__uint32 xx;
- /* data size */
- if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- if(options.common.ignore_chunk_sizes) {
- FLAC__ASSERT(!options.common.sector_align);
- data_bytes = (unsigned)(-(int)bytes_per_wide_sample); /* max out data_bytes; we'll use EOF as signal to stop reading */
- }
- else {
- data_bytes = xx;
- if(0 == data_bytes) {
- flac__utils_printf(stderr, 1, "%s: ERROR: 'data' chunk has size of 0\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
+ if(!got_fmt_chunk) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: got 'data' chunk before 'fmt' chunk\n", e->inbasefilename);
+ return false;
}
- pad = (data_bytes & 1U) ? true : false;
-
- /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
- FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
- total_samples_in_input = data_bytes / bytes_per_wide_sample + *options.common.align_reservoir_samples;
- /*
- * now that we know the input size, canonicalize the
- * --until string to an absolute sample number:
- */
- if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, sample_rate, encoder_session.skip, total_samples_in_input))
- return EncoderSession_finish_error(&encoder_session);
- encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
- FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
-
- if(encoder_session.skip > 0) {
- if(!fskip_ahead(infile, encoder_session.skip * bytes_per_wide_sample)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ /* data size */
+ if(!read_uint32(e->fin, /*big_endian=*/false, &xx, e->inbasefilename))
+ return false;
+ if(options.ignore_chunk_sizes) {
+ FLAC__ASSERT(!options.sector_align);
+ if(xx) {
+ flac__utils_printf(stderr, 1, "%s: WARNING: \"data\" chunk has non-zero size, using --ignore-chunk-sizes is probably a bad idea\n", e->inbasefilename, chunk_id);
+ if(e->treat_warnings_as_errors)
+ return false;
}
- }
-
- data_bytes -= (unsigned)encoder_session.skip * bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */
- if(options.common.ignore_chunk_sizes) {
- encoder_session.total_samples_to_encode = 0;
- flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
- FLAC__ASSERT(0 == encoder_session.until);
+ data_bytes = (size_t)0 - (size_t)e->info.bytes_per_wide_sample; /* max out data_bytes; we'll use EOF as signal to stop reading */
}
else {
- encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
- }
- if(encoder_session.until > 0) {
- trim = total_samples_in_input - encoder_session.until;
- FLAC__ASSERT(total_samples_in_input > 0);
- FLAC__ASSERT(!options.common.sector_align);
- data_bytes -= (unsigned int)trim * bytes_per_wide_sample;
- encoder_session.total_samples_to_encode -= trim;
- }
- if(options.common.sector_align) {
- align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
- if(options.common.is_last_file)
- encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
- else
- encoder_session.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_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample + 44;
-
- if(!EncoderSession_init_encoder(&encoder_session, options.common, channel_mask, channels, bps-shift, sample_rate, options.foreign_metadata, /*flac_decoder_data=*/0))
- return EncoderSession_finish_error(&encoder_session);
-
- /*
- * first do any samples in the reservoir
- */
- if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
- }
-
- /*
- * 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;
- }
- }
-
- /*
- * now do from the file
- */
- while(data_bytes > 0) {
- size_t 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)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(feof(infile)) {
- if(options.common.ignore_chunk_sizes) {
- flac__utils_printf(stderr, 1, "%s: INFO: hit EOF with --ignore-chunk-sizes, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.samples_written);
- }
- else {
- flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- data_bytes = 0;
- }
- }
- else {
- if(bytes_read % bytes_per_wide_sample != 0) {
- flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else {
- unsigned wide_samples = bytes_read / bytes_per_wide_sample;
- if(!format_input(input_, wide_samples, /*is_big_endian=*/false, is_unsigned_samples, channels, bps, shift, channel_map))
- return EncoderSession_finish_error(&encoder_session);
-
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
- data_bytes -= bytes_read;
- }
- }
- }
-
- if(trim > 0) {
- FLAC__ASSERT(!options.common.sector_align);
- if(!fskip_ahead(infile, trim * bytes_per_wide_sample)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
-
- /*
- * 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;
- for(channel = 0; channel < channels; channel++)
- memset(input_[channel], 0, sizeof(input_[0][0]) * wide_samples);
-
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
- }
- }
- else {
- if(*options.common.align_reservoir_samples > 0) {
- size_t bytes_read;
- 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)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
- flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- else {
- info_align_carry = *options.common.align_reservoir_samples;
- if(!format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, /*is_big_endian=*/false, is_unsigned_samples, channels, bps, shift, channel_map))
- return EncoderSession_finish_error(&encoder_session);
- }
- }
+ data_bytes = xx;
+ if(0 == data_bytes) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: 'data' chunk has size of 0\n", e->inbasefilename);
+ return false;
}
}
- if(pad == true) {
- unsigned char tmp;
-
- if(fread(&tmp, 1U, 1U, infile) < 1U) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read of data pad byte\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
+ e->fmt.iff.data_bytes = data_bytes;
+ e->fmt.iff.pad = (data_bytes & 1) ? true : false;
got_data_chunk = true;
+ break;
}
else {
- if(!memcmp(chunk_id, "data", 4) && !got_fmt_chunk) {
- flac__utils_printf(stderr, 1, "%s: ERROR: got 'data' chunk before 'fmt' chunk\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
-
- if(!options.foreign_metadata) {
- if(!memcmp(chunk_id, "fmt ", 4) && got_fmt_chunk)
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'fmt ' chunk (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename);
- else if(!memcmp(chunk_id, "data", 4))
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'data' chunk (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename);
- else
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s' (use --keep-foreign-metadata to keep)\n", encoder_session.inbasefilename, chunk_id);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
+ FLAC__uint32 xx;
+ if(!options.format_options.iff.foreign_metadata) {
+ flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s' (use --keep-foreign-metadata to keep)\n", e->inbasefilename, chunk_id);
+ if(e->treat_warnings_as_errors)
+ return false;
}
/* chunk size */
- if(!read_uint32(infile, /*big_endian=*/false, &xx, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
+ if(!read_uint32(e->fin, /*big_endian=*/false, &xx, e->inbasefilename))
+ return false;
else {
- unsigned long skip = xx+(xx & 1U);
+ unsigned long skip = xx + (xx & 1);
- FLAC__ASSERT(skip<=LONG_MAX);
- if(!fskip_ahead(infile, skip)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over unsupported chunk\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ FLAC__ASSERT(skip <= LONG_MAX);
+ if(!fskip_ahead(e->fin, skip)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over chunk\n", e->inbasefilename);
+ return false;
}
}
}
}
- return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, options.foreign_metadata);
+ if(!got_fmt_chunk) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: didn't find fmt chunk\n", e->inbasefilename);
+ return false;
+ }
+ if(!got_data_chunk) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: didn't find data chunk\n", e->inbasefilename);
+ return false;
+ }
+
+ e->info.sample_rate = sample_rate;
+ e->info.channels = channels;
+ e->info.bits_per_sample = bps;
+ e->info.shift = shift;
+ e->info.channel_mask = channel_mask;
+
+ return true;
}
-int flac__encode_raw(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, raw_encode_options_t options)
+static FLAC__bool get_sample_info_aiff(EncoderSession *e, encode_options_t options)
{
- EncoderSession encoder_session;
- 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__uint64 total_samples_in_input = 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);
- FLAC__ASSERT(!options.common.replay_gain || options.channels <= 2);
- FLAC__ASSERT(!options.common.replay_gain || grabbag__replaygain_is_valid_sample_frequency(options.sample_rate));
-
- if(!
- EncoderSession_construct(
- &encoder_session,
-#if FLAC__HAS_OGG
- options.common.use_ogg,
-#else
- /*use_ogg=*/false,
-#endif
- options.common.verify,
- options.common.treat_warnings_as_errors,
- options.common.continue_through_decode_errors,
- infile,
- infilename,
- outfilename
- )
- )
- return 1;
+ FLAC__bool got_comm_chunk = false, got_ssnd_chunk = false;
+ unsigned sample_rate = 0, channels = 0, bps = 0, shift = 0, sample_frames = 0;
+ FLAC__uint32 channel_mask = 0;
+
+ e->info.is_unsigned_samples = false;
+ e->info.is_big_endian = true;
/*
- * now that we know the sample rate, canonicalize the
- * --skip string to a number of samples:
+ * lookahead[] already has "FORMxxxxAIFF", do chunks
*/
- flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, options.sample_rate);
- FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
- encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
- FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
+ while(!feof(e->fin) && !got_ssnd_chunk) {
+ char chunk_id[5] = { '\0', '\0', '\0', '\0', '\0' }; /* one extra byte for terminating NUL so we can also treat it like a C string */
+ if(!read_bytes(e->fin, (FLAC__byte*)chunk_id, 4, /*eof_ok=*/true, e->inbasefilename)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: incomplete chunk identifier\n", e->inbasefilename);
+ return false;
+ }
+ if(feof(e->fin))
+ break;
- if(infilesize < 0)
- total_samples_in_input = 0;
- else {
- /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
- FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
- total_samples_in_input = (FLAC__uint64)infilesize / bytes_per_wide_sample + *options.common.align_reservoir_samples;
- }
+ if(!memcmp(chunk_id, "COMM", 4)) { /* common chunk */
+ FLAC__uint16 x;
+ FLAC__uint32 xx;
+ unsigned long skip;
+ const FLAC__bool is_aifc = options.format == FORMAT_AIFF_C;
+ const FLAC__uint32 minimum_comm_size = (is_aifc? 22 : 18);
- /*
- * now that we know the input size, canonicalize the
- * --until strings to a number of samples:
- */
- if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, options.sample_rate, encoder_session.skip, total_samples_in_input))
- return EncoderSession_finish_error(&encoder_session);
- encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
- FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
-
- infilesize -= (off_t)encoder_session.skip * bytes_per_wide_sample;
- encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
- if(encoder_session.until > 0) {
- const FLAC__uint64 trim = total_samples_in_input - encoder_session.until;
- FLAC__ASSERT(total_samples_in_input > 0);
- FLAC__ASSERT(!options.common.sector_align);
- infilesize -= (off_t)trim * bytes_per_wide_sample;
- encoder_session.total_samples_to_encode -= trim;
- }
- if(infilesize >= 0 && options.common.sector_align) {
- FLAC__ASSERT(encoder_session.skip == 0);
- align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
- if(options.common.is_last_file)
- encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
- else
- encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
- }
- encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample;
+ if(got_comm_chunk) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: file has multiple 'COMM' chunks\n", e->inbasefilename);
+ return false;
+ }
- if(encoder_session.total_samples_to_encode <= 0)
- flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
+ /* COMM chunk size */
+ if(!read_uint32(e->fin, /*big_endian=*/true, &xx, e->inbasefilename))
+ return false;
+ else if(xx < minimum_comm_size) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: non-standard %s 'COMM' chunk has length = %u\n", e->inbasefilename, is_aifc? "AIFF-C" : "AIFF", (unsigned int)xx);
+ return false;
+ }
+ else if(!is_aifc && xx != minimum_comm_size) {
+ flac__utils_printf(stderr, 1, "%s: WARNING: non-standard %s 'COMM' chunk has length = %u, expected %u\n", e->inbasefilename, is_aifc? "AIFF-C" : "AIFF", (unsigned int)xx, minimum_comm_size);
+ if(e->treat_warnings_as_errors)
+ return false;
+ }
+ skip = (xx-minimum_comm_size)+(xx & 1);
- if(encoder_session.skip > 0) {
- unsigned skip_bytes = bytes_per_wide_sample * (unsigned)encoder_session.skip;
- if(skip_bytes > lookahead_length) {
- skip_bytes -= lookahead_length;
- lookahead_length = 0;
- if(!fskip_ahead(infile, skip_bytes)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ /* number of channels */
+ if(!read_uint16(e->fin, /*big_endian=*/true, &x, e->inbasefilename))
+ return false;
+ channels = (unsigned)x;
+ if(channels > 2 && !options.channel_map_none) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u for AIFF\n", e->inbasefilename, channels);
+ return false;
}
- }
- else {
- lookahead += skip_bytes;
- lookahead_length -= skip_bytes;
- }
- }
- if(!EncoderSession_init_encoder(&encoder_session, options.common, /*channel_mask=*/0, options.channels, options.bps, options.sample_rate, /*foreign_metadata=*/0, /*flac_decoder_data=*/0))
- return EncoderSession_finish_error(&encoder_session);
+ /* number of sample frames */
+ if(!read_uint32(e->fin, /*big_endian=*/true, &xx, e->inbasefilename))
+ return false;
+ sample_frames = xx;
- /*
- * first do any samples in the reservoir
- */
- if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
- }
+ /* bits per sample */
+ if(!read_uint16(e->fin, /*big_endian=*/true, &x, e->inbasefilename))
+ return false;
+ bps = (unsigned)x;
+ shift = (bps%8)? 8-(bps%8) : 0; /* SSND data is always byte-aligned, left-justified but format_input() will double-check */
+ bps += shift;
- /*
- * 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 -= (off_t)((*options.common.align_reservoir_samples) * bytes_per_wide_sample);
- FLAC__ASSERT(infilesize >= 0);
- }
- }
+ /* sample rate */
+ if(!read_sane_extended(e->fin, &xx, e->inbasefilename))
+ return false;
+ sample_rate = xx;
- /*
- * now do from the file
- */
- if(infilesize < 0) {
- while(!feof(infile)) {
- if(lookahead_length > 0) {
- FLAC__ASSERT(lookahead_length < CHUNK_OF_SAMPLES * bytes_per_wide_sample);
- 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)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ /* check compression type for AIFF-C */
+ if(is_aifc) {
+ if(!read_uint32(e->fin, /*big_endian=*/true, &xx, e->inbasefilename))
+ return false;
+ if(xx == 0x736F7774) /* "sowt" */
+ e->info.is_big_endian = false;
+ else if(xx == 0x4E4F4E45) /* "NONE" */
+ ; /* nothing to do, we already default to big-endian */
+ else {
+ flac__utils_printf(stderr, 1, "%s: ERROR: can't handle AIFF-C compression type \"%c%c%c%c\"\n", e->inbasefilename, (char)(xx>>24), (char)((xx>>16)&8), (char)((xx>>8)&8), (char)(xx&8));
+ return false;
}
- lookahead_length = 0;
}
- else
- bytes_read = fread(ucbuffer_, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, infile);
- if(bytes_read == 0) {
- if(ferror(infile)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
- else if(bytes_read % bytes_per_wide_sample != 0) {
- flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
+ /* set channel mapping */
+ /* FLAC order follows SMPTE and WAVEFORMATEXTENSIBLE but with fewer channels, which are: */
+ /* front left, front right, center, LFE, back left, back right, surround left, surround right */
+ /* specs say the channel ordering is:
+ * 1 2 3 4 5 6
+ * ___________________________________________________
+ * 2 stereo l r
+ * 3 l r c
+ * 4 l c r S
+ * quad (ambiguous with 4ch) Fl Fr Bl Br
+ * 5 Fl Fr Fc Sl Sr
+ * 6 l lc c r rc S
+ * l:left r:right c:center Fl:front-left Fr:front-right Bl:back-left Br:back-right Lc:left-center Rc:right-center S:surround
+ * so we only have unambiguous mappings for 2, 3, and 5 channels
+ */
+ if(
+ options.channel_map_none ||
+ channels == 1 || /* 1 channel: (mono) */
+ channels == 2 || /* 2 channels: left, right */
+ channels == 3 || /* 3 channels: left, right, center */
+ channels == 5 /* 5 channels: front left, front right, center, surround left, surround right */
+ ) {
+ /* keep default channel order */
}
else {
- unsigned wide_samples = bytes_read / bytes_per_wide_sample;
- if(!format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps, /*shift=*/0, /*channel_map=*/0))
- return EncoderSession_finish_error(&encoder_session);
+ flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %u for AIFF\n", e->inbasefilename, channels);
+ return false;
+ }
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
+ e->info.bytes_per_wide_sample = channels * (bps / 8);
+
+ /* skip any extra data in the COMM chunk */
+ if(!fskip_ahead(e->fin, skip)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over extra COMM data\n", e->inbasefilename);
+ return false;
}
+
+ got_comm_chunk = true;
}
- }
- else {
- const FLAC__uint64 max_input_bytes = infilesize;
- FLAC__uint64 total_input_bytes_read = 0;
- while(total_input_bytes_read < max_input_bytes) {
- {
- size_t wanted = (CHUNK_OF_SAMPLES * bytes_per_wide_sample);
- wanted = (size_t) min((FLAC__uint64)wanted, max_input_bytes - total_input_bytes_read);
-
- if(lookahead_length > 0) {
- FLAC__ASSERT(lookahead_length <= wanted);
- memcpy(ucbuffer_, lookahead, lookahead_length);
- wanted -= lookahead_length;
- bytes_read = lookahead_length;
- if(wanted > 0) {
- bytes_read += fread(ucbuffer_+lookahead_length, sizeof(unsigned char), wanted, infile);
- if(ferror(infile)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- }
- lookahead_length = 0;
- }
- else
- bytes_read = fread(ucbuffer_, sizeof(unsigned char), wanted, infile);
+ else if(!memcmp(chunk_id, "SSND", 4) && !got_ssnd_chunk) { /* sound data chunk */
+ FLAC__uint32 xx;
+ unsigned offset = 0, block_size = 0, data_bytes;
+
+ if(!got_comm_chunk) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", e->inbasefilename);
+ return false;
}
- if(bytes_read == 0) {
- if(ferror(infile)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(feof(infile)) {
- flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- total_input_bytes_read = max_input_bytes;
+ /* SSND chunk size */
+ if(!read_uint32(e->fin, /*big_endian=*/true, &xx, e->inbasefilename))
+ return false;
+ if(options.ignore_chunk_sizes) {
+ FLAC__ASSERT(!options.sector_align);
+ if(xx) {
+ flac__utils_printf(stderr, 1, "%s: WARNING: \"SSND\" chunk has non-zero size, using --ignore-chunk-sizes is probably a bad idea\n", e->inbasefilename, chunk_id);
+ if(e->treat_warnings_as_errors)
+ return false;
}
+ data_bytes = (size_t)0 - (size_t)e->info.bytes_per_wide_sample; /* max out data_bytes; we'll use EOF as signal to stop reading */
}
else {
- if(bytes_read % bytes_per_wide_sample != 0) {
- flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else {
- unsigned wide_samples = bytes_read / bytes_per_wide_sample;
- if(!format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps, /*shift=*/0, /*channel_map=*/0))
- return EncoderSession_finish_error(&encoder_session);
+ data_bytes = xx;
+ data_bytes -= 8; /* discount the offset and block size fields */
+ }
+ e->fmt.iff.pad = (data_bytes & 1) ? true : false;
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
- total_input_bytes_read += bytes_read;
- }
+ /* offset */
+ if(!read_uint32(e->fin, /*big_endian=*/true, &xx, e->inbasefilename))
+ return false;
+ offset = xx;
+ data_bytes -= offset;
+
+ /* block size */
+ if(!read_uint32(e->fin, /*big_endian=*/true, &xx, e->inbasefilename))
+ return false;
+ else if(xx != 0) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: block size is %u; must be 0\n", e->inbasefilename, (unsigned)xx);
+ return false;
+ }
+ block_size = xx;
+
+ /* skip any SSND offset bytes */
+ if(!fskip_ahead(e->fin, offset)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: skipping offset in SSND chunk\n", e->inbasefilename);
+ return false;
+ }
+ if(data_bytes != (sample_frames * e->info.bytes_per_wide_sample)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: SSND chunk size inconsistent with sample frame count\n", e->inbasefilename);
+ return false;
}
- }
- }
- /*
- * 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;
- for(channel = 0; channel < options.channels; channel++)
- memset(input_[channel], 0, sizeof(input_[0][0]) * wide_samples);
-
- if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
- print_error_with_state(&encoder_session, "ERROR during encoding");
- return EncoderSession_finish_error(&encoder_session);
- }
- }
+ e->fmt.iff.data_bytes = data_bytes;
+
+ got_ssnd_chunk = true;
}
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)) {
- flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
- flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- else {
- info_align_carry = *options.common.align_reservoir_samples;
- if(!format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps, /*shift=*/0, /*channel_map=*/0))
- return EncoderSession_finish_error(&encoder_session);
+ FLAC__uint32 xx;
+ if(!options.format_options.iff.foreign_metadata) {
+ flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s' (use --keep-foreign-metadata to keep)\n", e->inbasefilename, chunk_id);
+ if(e->treat_warnings_as_errors)
+ return false;
+ }
+
+ /* chunk size */
+ if(!read_uint32(e->fin, /*big_endian=*/true, &xx, e->inbasefilename))
+ return false;
+ else {
+ unsigned long skip = xx + (xx & 1);
+
+ FLAC__ASSERT(skip <= LONG_MAX);
+ if(!fskip_ahead(e->fin, skip)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping over chunk\n", e->inbasefilename);
+ return false;
}
}
}
}
- return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero, /*foreign_metadata=*/0);
-}
+ if(!got_comm_chunk) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: didn't find COMM chunk\n", e->inbasefilename);
+ return false;
+ }
+ if(!got_ssnd_chunk && sample_frames) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: didn't find SSND chunk\n", e->inbasefilename);
+ return false;
+ }
-int flac__encode_flac(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, flac_encode_options_t options, FLAC__bool input_is_ogg)
-{
- EncoderSession encoder_session;
- FLAC__StreamDecoder *decoder = 0;
- FLACDecoderData decoder_data;
- size_t i;
- int retval;
-
- if(!
- EncoderSession_construct(
- &encoder_session,
-#if FLAC__HAS_OGG
- options.common.use_ogg,
-#else
- /*use_ogg=*/false,
-#endif
- options.common.verify,
- options.common.treat_warnings_as_errors,
- options.common.continue_through_decode_errors,
- infile,
- infilename,
- outfilename
- )
- )
- return 1;
+ e->info.sample_rate = sample_rate;
+ e->info.channels = channels;
+ e->info.bits_per_sample = bps;
+ e->info.shift = shift;
+ e->info.channel_mask = channel_mask;
- decoder_data.encoder_session = &encoder_session;
- decoder_data.filesize = (infilesize == (off_t)(-1)? 0 : infilesize);
- decoder_data.lookahead = lookahead;
- decoder_data.lookahead_length = lookahead_length;
- decoder_data.num_metadata_blocks = 0;
- decoder_data.samples_left_to_process = 0;
- decoder_data.fatal_error = false;
+ return true;
+}
- /*
- * set up FLAC decoder for the input
- */
- if (0 == (decoder = FLAC__stream_decoder_new())) {
- flac__utils_printf(stderr, 1, "%s: ERROR: creating decoder for FLAC input\n", encoder_session.inbasefilename);
- return EncoderSession_finish_error(&encoder_session);
- }
+static FLAC__bool get_sample_info_flac(EncoderSession *e, encode_options_t options)
+{
if (!(
- FLAC__stream_decoder_set_md5_checking(decoder, false) &&
- FLAC__stream_decoder_set_metadata_respond_all(decoder)
+ FLAC__stream_decoder_set_md5_checking(e->fmt.flac.decoder, false) &&
+ FLAC__stream_decoder_set_metadata_respond_all(e->fmt.flac.decoder)
)) {
- flac__utils_printf(stderr, 1, "%s: ERROR: setting up decoder for FLAC input\n", encoder_session.inbasefilename);
- goto fubar1; /*@@@ yuck */
+ flac__utils_printf(stderr, 1, "%s: ERROR: setting up decoder for FLAC input\n", e->inbasefilename);
+ return false;
}
- if (input_is_ogg) {
- if (FLAC__stream_decoder_init_ogg_stream(decoder, flac_decoder_read_callback, flac_decoder_seek_callback, flac_decoder_tell_callback, flac_decoder_length_callback, flac_decoder_eof_callback, flac_decoder_write_callback, flac_decoder_metadata_callback, flac_decoder_error_callback, /*client_data=*/&decoder_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
- flac__utils_printf(stderr, 1, "%s: ERROR: initializing decoder for Ogg FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
- goto fubar1; /*@@@ yuck */
+ if (options.format == FORMAT_OGGFLAC) {
+ if (FLAC__stream_decoder_init_ogg_stream(e->fmt.flac.decoder, flac_decoder_read_callback, flac_decoder_seek_callback, flac_decoder_tell_callback, flac_decoder_length_callback, flac_decoder_eof_callback, flac_decoder_write_callback, flac_decoder_metadata_callback, flac_decoder_error_callback, /*client_data=*/e) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: initializing decoder for Ogg FLAC input, state = %s\n", e->inbasefilename, FLAC__stream_decoder_get_resolved_state_string(e->fmt.flac.decoder));
+ return false;
}
}
- else if (FLAC__stream_decoder_init_stream(decoder, flac_decoder_read_callback, flac_decoder_seek_callback, flac_decoder_tell_callback, flac_decoder_length_callback, flac_decoder_eof_callback, flac_decoder_write_callback, flac_decoder_metadata_callback, flac_decoder_error_callback, /*client_data=*/&decoder_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
- flac__utils_printf(stderr, 1, "%s: ERROR: initializing decoder for FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
- goto fubar1; /*@@@ yuck */
+ else if (FLAC__stream_decoder_init_stream(e->fmt.flac.decoder, flac_decoder_read_callback, flac_decoder_seek_callback, flac_decoder_tell_callback, flac_decoder_length_callback, flac_decoder_eof_callback, flac_decoder_write_callback, flac_decoder_metadata_callback, flac_decoder_error_callback, /*client_data=*/e) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: initializing decoder for FLAC input, state = %s\n", e->inbasefilename, FLAC__stream_decoder_get_resolved_state_string(e->fmt.flac.decoder));
+ return false;
}
- if (!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || decoder_data.fatal_error) {
- if (decoder_data.fatal_error)
- flac__utils_printf(stderr, 1, "%s: ERROR: out of memory or too many metadata blocks while reading metadata in FLAC input\n", encoder_session.inbasefilename);
+ if (!FLAC__stream_decoder_process_until_end_of_metadata(e->fmt.flac.decoder) || e->fmt.flac.client_data.fatal_error) {
+ if (e->fmt.flac.client_data.fatal_error)
+ flac__utils_printf(stderr, 1, "%s: ERROR: out of memory or too many metadata blocks while reading metadata in FLAC input\n", e->inbasefilename);
else
- flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
- goto fubar1; /*@@@ yuck */
+ flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, state = %s\n", e->inbasefilename, FLAC__stream_decoder_get_resolved_state_string(e->fmt.flac.decoder));
+ return false;
}
- if (decoder_data.num_metadata_blocks == 0) {
- flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, got no metadata blocks\n", encoder_session.inbasefilename);
- goto fubar2; /*@@@ yuck */
+ if (e->fmt.flac.client_data.num_metadata_blocks == 0) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, got no metadata blocks\n", e->inbasefilename);
+ return false;
}
- else if (decoder_data.metadata_blocks[0]->type != FLAC__METADATA_TYPE_STREAMINFO) {
- flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, first metadata block is not STREAMINFO\n", encoder_session.inbasefilename);
- goto fubar2; /*@@@ yuck */
+ else if (e->fmt.flac.client_data.metadata_blocks[0]->type != FLAC__METADATA_TYPE_STREAMINFO) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: reading metadata in FLAC input, first metadata block is not STREAMINFO\n", e->inbasefilename);
+ return false;
}
- else if (decoder_data.metadata_blocks[0]->data.stream_info.total_samples == 0) {
- flac__utils_printf(stderr, 1, "%s: ERROR: FLAC input has STREAMINFO with unknown total samples which is not supported\n", encoder_session.inbasefilename);
- goto fubar2; /*@@@ yuck */
+ else if (e->fmt.flac.client_data.metadata_blocks[0]->data.stream_info.total_samples == 0) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: FLAC input has STREAMINFO with unknown total samples which is not supported\n", e->inbasefilename);
+ return false;
}
- /*
- * now that we have the STREAMINFO and know the sample rate,
- * canonicalize the --skip string to a number of samples:
- */
- flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, decoder_data.metadata_blocks[0]->data.stream_info.sample_rate);
- FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
- encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
- FLAC__ASSERT(!options.common.sector_align); /* --sector-align with FLAC input is not supported */
+ e->info.sample_rate = e->fmt.flac.client_data.metadata_blocks[0]->data.stream_info.sample_rate;
+ e->info.channels = e->fmt.flac.client_data.metadata_blocks[0]->data.stream_info.channels;
+ e->info.bits_per_sample = e->fmt.flac.client_data.metadata_blocks[0]->data.stream_info.bits_per_sample;
+ e->info.shift = 0;
+ e->info.bytes_per_wide_sample = 0;
+ e->info.is_unsigned_samples = false; /* not applicable for FLAC input */
+ e->info.is_big_endian = false; /* not applicable for FLAC input */
+ e->info.channel_mask = 0;
+
+ return true;
+}
+
+/*
+ * public routines
+ */
+int flac__encode_file(FILE *infile, off_t infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, encode_options_t options)
+{
+ EncoderSession encoder_session;
+ size_t channel_map[FLAC__MAX_CHANNELS];
+ int info_align_carry = -1, info_align_zero = -1;
+
+ if(!EncoderSession_construct(&encoder_session, options, infilesize, infile, infilename, outfilename, lookahead, lookahead_length))
+ return 1;
+
+ /* initialize default channel map that preserves channel order */
+ {
+ size_t i;
+ for(i = 0; i < sizeof(channel_map)/sizeof(channel_map[0]); i++)
+ channel_map[i] = i;
+ }
+
+ /* read foreign metadata if requested */
+ if(
+ (options.format == FORMAT_WAVE || options.format == FORMAT_AIFF || options.format == FORMAT_AIFF_C) &&
+ options.format_options.iff.foreign_metadata
+ ) {
+ const char *error;
+ if(!(
+ options.format == FORMAT_WAVE?
+ flac__foreign_metadata_read_from_wave(options.format_options.iff.foreign_metadata, infilename, &error) :
+ flac__foreign_metadata_read_from_aiff(options.format_options.iff.foreign_metadata, infilename, &error)
+ )) {
+ flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", encoder_session.inbasefilename, error);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
+
+ /* initialize encoder session with info about the audio (channels/bps/resolution/endianness/etc) */
+ switch(options.format) {
+ case FORMAT_RAW:
+ if(!get_sample_info_raw(&encoder_session, options))
+ return EncoderSession_finish_error(&encoder_session);
+ break;
+ case FORMAT_WAVE:
+ if(!get_sample_info_wave(&encoder_session, options))
+ return EncoderSession_finish_error(&encoder_session);
+ break;
+ case FORMAT_AIFF:
+ case FORMAT_AIFF_C:
+ if(!get_sample_info_aiff(&encoder_session, options))
+ return EncoderSession_finish_error(&encoder_session);
+ break;
+ case FORMAT_FLAC:
+ case FORMAT_OGGFLAC:
+ /*
+ * set up FLAC decoder for the input
+ */
+ if (0 == (encoder_session.fmt.flac.decoder = FLAC__stream_decoder_new())) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: creating decoder for FLAC input\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ if(!get_sample_info_flac(&encoder_session, options))
+ return EncoderSession_finish_error(&encoder_session);
+ break;
+ default:
+ FLAC__ASSERT(0);
+ /* double protection */
+ return EncoderSession_finish_error(&encoder_session);
+ }
+
+ /* some more checks */
+ if(encoder_session.info.channels == 0 || encoder_session.info.channels > FLAC__MAX_CHANNELS) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number of channels %u\n", encoder_session.inbasefilename, encoder_session.info.channels);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ if(!FLAC__format_sample_rate_is_valid(encoder_session.info.sample_rate)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, encoder_session.info.sample_rate);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ if(encoder_session.info.bits_per_sample-encoder_session.info.shift < 4 || encoder_session.info.bits_per_sample-encoder_session.info.shift > 24) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits-per-sample %u\n", encoder_session.inbasefilename, encoder_session.info.bits_per_sample-encoder_session.info.shift);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ if(options.sector_align) {
+ if(encoder_session.info.channels != 2) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, encoder_session.info.channels);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ if(encoder_session.info.sample_rate != 44100) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.inbasefilename, encoder_session.info.sample_rate);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ if(encoder_session.info.bits_per_sample-encoder_session.info.shift != 16) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: file has %u bits-per-sample, must be 16 for --sector-align\n", encoder_session.inbasefilename, encoder_session.info.bits_per_sample-encoder_session.info.shift);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
{
- FLAC__uint64 total_samples_in_input, trim = 0;
+ FLAC__uint64 total_samples_in_input; /* WATCHOUT: may be 0 to mean "unknown" */
+ FLAC__uint64 skip;
+ FLAC__uint64 until; /* a value of 0 mean end-of-stream (i.e. --until=-0) */
+ unsigned align_remainder = 0;
+
+ switch(options.format) {
+ case FORMAT_RAW:
+ if(infilesize < 0)
+ total_samples_in_input = 0;
+ else
+ total_samples_in_input = (FLAC__uint64)infilesize / encoder_session.info.bytes_per_wide_sample + *options.align_reservoir_samples;
+ break;
+ case FORMAT_WAVE:
+ case FORMAT_AIFF:
+ case FORMAT_AIFF_C:
+ total_samples_in_input = encoder_session.fmt.iff.data_bytes / encoder_session.info.bytes_per_wide_sample + *options.align_reservoir_samples;
+ break;
+ case FORMAT_FLAC:
+ case FORMAT_OGGFLAC:
+ total_samples_in_input = encoder_session.fmt.flac.client_data.metadata_blocks[0]->data.stream_info.total_samples + *options.align_reservoir_samples;
+ break;
+ default:
+ FLAC__ASSERT(0);
+ /* double protection */
+ return EncoderSession_finish_error(&encoder_session);
+ }
- total_samples_in_input = decoder_data.metadata_blocks[0]->data.stream_info.total_samples;
+ /*
+ * now that we know the sample rate, canonicalize the
+ * --skip string to an absolute sample number:
+ */
+ flac__utils_canonicalize_skip_until_specification(&options.skip_specification, encoder_session.info.sample_rate);
+ FLAC__ASSERT(options.skip_specification.value.samples >= 0);
+ skip = (FLAC__uint64)options.skip_specification.value.samples;
+ FLAC__ASSERT(!options.sector_align || (options.format != FORMAT_FLAC && options.format != FORMAT_OGGFLAC && skip == 0));
+ /* *options.align_reservoir_samples will be 0 unless --sector-align is used */
+ FLAC__ASSERT(options.sector_align || *options.align_reservoir_samples == 0);
/*
- * now that we know the input size, canonicalize the
+ * now that we possibly know the input size, canonicalize the
* --until string to an absolute sample number:
*/
- if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, decoder_data.metadata_blocks[0]->data.stream_info.sample_rate, encoder_session.skip, total_samples_in_input))
- goto fubar2; /*@@@ yuck */
- encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
-
- encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
- if(encoder_session.until > 0) {
- trim = total_samples_in_input - encoder_session.until;
+ if(!canonicalize_until_specification(&options.until_specification, encoder_session.inbasefilename, encoder_session.info.sample_rate, skip, total_samples_in_input))
+ return EncoderSession_finish_error(&encoder_session);
+ until = (FLAC__uint64)options.until_specification.value.samples;
+ FLAC__ASSERT(!options.sector_align || until == 0);
+
+ /* adjust encoding parameters based on skip and until values */
+ switch(options.format) {
+ case FORMAT_RAW:
+ infilesize -= (off_t)skip * encoder_session.info.bytes_per_wide_sample;
+ encoder_session.total_samples_to_encode = total_samples_in_input - skip;
+ break;
+ case FORMAT_WAVE:
+ case FORMAT_AIFF:
+ case FORMAT_AIFF_C:
+ encoder_session.fmt.iff.data_bytes -= (unsigned)skip * encoder_session.info.bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */
+ if(options.ignore_chunk_sizes) {
+ encoder_session.total_samples_to_encode = 0;
+ flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
+ FLAC__ASSERT(0 == until);
+ }
+ else {
+ encoder_session.total_samples_to_encode = total_samples_in_input - skip;
+ }
+ break;
+ case FORMAT_FLAC:
+ case FORMAT_OGGFLAC:
+ encoder_session.total_samples_to_encode = total_samples_in_input - skip;
+ break;
+ default:
+ FLAC__ASSERT(0);
+ /* double protection */
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ if(until > 0) {
+ const FLAC__uint64 trim = total_samples_in_input - until;
FLAC__ASSERT(total_samples_in_input > 0);
+ FLAC__ASSERT(!options.sector_align);
+ if(options.format == FORMAT_RAW)
+ infilesize -= (off_t)trim * encoder_session.info.bytes_per_wide_sample;
+ else if(options.format == FORMAT_WAVE || options.format == FORMAT_AIFF || options.format == FORMAT_AIFF_C)
+ encoder_session.fmt.iff.data_bytes -= (unsigned)trim * encoder_session.info.bytes_per_wide_sample;
encoder_session.total_samples_to_encode -= trim;
}
+ if(options.sector_align && (options.format != FORMAT_RAW || infilesize >=0)) { /* for RAW, need to know the filesize */
+ FLAC__ASSERT(skip == 0); /* asserted above too, but lest we forget */
+ align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
+ if(options.is_last_file)
+ encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
+ else
+ encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
+ }
+ switch(options.format) {
+ case FORMAT_RAW:
+ encoder_session.unencoded_size = encoder_session.total_samples_to_encode * encoder_session.info.bytes_per_wide_sample;
+ break;
+ case FORMAT_WAVE:
+ /* +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_session.unencoded_size = encoder_session.total_samples_to_encode * encoder_session.info.bytes_per_wide_sample + 44;
+ break;
+ case FORMAT_AIFF:
+ case FORMAT_AIFF_C:
+ /* +54 for the size of the WAV headers; this is just an estimate for the progress indicator and doesn't need to be exact */
+ encoder_session.unencoded_size = encoder_session.total_samples_to_encode * encoder_session.info.bytes_per_wide_sample + 54;
+ break;
+ case FORMAT_FLAC:
+ case FORMAT_OGGFLAC:
+ if(infilesize < 0)
+ /* if we don't know, use 0 as hint to progress indicator (which is the only place this is used): */
+ encoder_session.unencoded_size = 0;
+ else if(skip == 0 && until == 0)
+ encoder_session.unencoded_size = (FLAC__uint64)infilesize;
+ else if(total_samples_in_input)
+ encoder_session.unencoded_size = (FLAC__uint64)infilesize * encoder_session.total_samples_to_encode / total_samples_in_input;
+ else
+ encoder_session.unencoded_size = (FLAC__uint64)infilesize;
+ break;
+ default:
+ FLAC__ASSERT(0);
+ /* double protection */
+ return EncoderSession_finish_error(&encoder_session);
+ }
+
+ if(encoder_session.total_samples_to_encode == 0)
+ flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)\n");
+
+ if(options.format == FORMAT_FLAC || options.format == FORMAT_OGGFLAC)
+ encoder_session.fmt.flac.client_data.samples_left_to_process = encoder_session.total_samples_to_encode;
+
+ /* init the encoder */
+ if(!EncoderSession_init_encoder(&encoder_session, options))
+ return EncoderSession_finish_error(&encoder_session);
- encoder_session.unencoded_size = decoder_data.filesize;
+ /* skip over any samples as requested */
+ if(skip > 0) {
+ switch(options.format) {
+ case FORMAT_RAW:
+ {
+ unsigned skip_bytes = encoder_session.info.bytes_per_wide_sample * (unsigned)skip;
+ if(skip_bytes > lookahead_length) {
+ skip_bytes -= lookahead_length;
+ lookahead_length = 0;
+ if(!fskip_ahead(encoder_session.fin, skip_bytes)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
+ else {
+ lookahead += skip_bytes;
+ lookahead_length -= skip_bytes;
+ }
+ }
+ break;
+ case FORMAT_WAVE:
+ case FORMAT_AIFF:
+ case FORMAT_AIFF_C:
+ if(!fskip_ahead(encoder_session.fin, skip * encoder_session.info.bytes_per_wide_sample)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ break;
+ case FORMAT_FLAC:
+ case FORMAT_OGGFLAC:
+ /*
+ * have to wait until the FLAC encoder is set up for writing
+ * before any seeking in the input FLAC file, because the seek
+ * itself will usually call the decoder's write callback, and
+ * our decoder's write callback passes samples to our FLAC
+ * encoder
+ */
+ if(!FLAC__stream_decoder_seek_absolute(encoder_session.fmt.flac.decoder, skip)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR while skipping samples, FLAC decoder state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(encoder_session.fmt.flac.decoder));
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ break;
+ default:
+ FLAC__ASSERT(0);
+ /* double protection */
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
- /* (channel mask will get copied over from the source VORBIS_COMMENT if it exists) */
- if(!EncoderSession_init_encoder(&encoder_session, options.common, /*channel_mask=*/0, decoder_data.metadata_blocks[0]->data.stream_info.channels, decoder_data.metadata_blocks[0]->data.stream_info.bits_per_sample, decoder_data.metadata_blocks[0]->data.stream_info.sample_rate, /*foreign_metadata=*/0, &decoder_data))
- goto fubar2; /*@@@ yuck */
+ /*
+ * first do any samples in the reservoir
+ */
+ if(options.sector_align && *options.align_reservoir_samples > 0) {
+ FLAC__ASSERT(options.format != FORMAT_FLAC && options.format != FORMAT_OGGFLAC); /* check again */
+ if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.align_reservoir, *options.align_reservoir_samples)) {
+ print_error_with_state(&encoder_session, "ERROR during encoding");
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
/*
- * have to wait until the FLAC encoder is set up for writing
- * before any seeking in the input FLAC file, because the seek
- * itself will usually call the decoder's write callback, and
- * our decoder's write callback passes samples to our FLAC
- * encoder
+ * decrement infilesize or the data_bytes counter if we need to align the file
*/
- decoder_data.samples_left_to_process = encoder_session.total_samples_to_encode;
- if(encoder_session.skip > 0) {
- if(!FLAC__stream_decoder_seek_absolute(decoder, encoder_session.skip)) {
- flac__utils_printf(stderr, 1, "%s: ERROR while skipping samples, FLAC decoder state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
- goto fubar2; /*@@@ yuck */
+ if(options.sector_align) {
+ if(options.is_last_file) {
+ *options.align_reservoir_samples = 0;
+ }
+ else {
+ *options.align_reservoir_samples = align_remainder;
+ if(options.format == FORMAT_RAW) {
+ FLAC__ASSERT(infilesize >= 0);
+ infilesize -= (off_t)((*options.align_reservoir_samples) * encoder_session.info.bytes_per_wide_sample);
+ FLAC__ASSERT(infilesize >= 0);
+ }
+ else if(options.format == FORMAT_WAVE || options.format == FORMAT_AIFF || options.format == FORMAT_AIFF_C)
+ encoder_session.fmt.iff.data_bytes -= (*options.align_reservoir_samples) * encoder_session.info.bytes_per_wide_sample;
}
}
/*
* now do samples from the file
*/
- while(!decoder_data.fatal_error && decoder_data.samples_left_to_process > 0) {
- /* We can also hit the end of stream without samples_left_to_process
- * going to 0 if there are errors and continue_through_decode_errors
- * is on, so we want to break in that case too:
- */
- if(encoder_session.continue_through_decode_errors && FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
+ switch(options.format) {
+ case FORMAT_RAW:
+ if(infilesize < 0) {
+ size_t bytes_read;
+ while(!feof(infile)) {
+ if(lookahead_length > 0) {
+ FLAC__ASSERT(lookahead_length < CHUNK_OF_SAMPLES * encoder_session.info.bytes_per_wide_sample);
+ memcpy(ucbuffer_, lookahead, lookahead_length);
+ bytes_read = fread(ucbuffer_+lookahead_length, sizeof(unsigned char), CHUNK_OF_SAMPLES * encoder_session.info.bytes_per_wide_sample - lookahead_length, infile) + lookahead_length;
+ if(ferror(infile)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ lookahead_length = 0;
+ }
+ else
+ bytes_read = fread(ucbuffer_, sizeof(unsigned char), CHUNK_OF_SAMPLES * encoder_session.info.bytes_per_wide_sample, infile);
+
+ if(bytes_read == 0) {
+ if(ferror(infile)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
+ else if(bytes_read % encoder_session.info.bytes_per_wide_sample != 0) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ else {
+ unsigned wide_samples = bytes_read / encoder_session.info.bytes_per_wide_sample;
+ if(!format_input(input_, wide_samples, encoder_session.info.is_big_endian, encoder_session.info.is_unsigned_samples, encoder_session.info.channels, encoder_session.info.bits_per_sample, encoder_session.info.shift, channel_map))
+ return EncoderSession_finish_error(&encoder_session);
+
+ if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
+ print_error_with_state(&encoder_session, "ERROR during encoding");
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
+ }
+ }
+ else {
+ size_t bytes_read;
+ const FLAC__uint64 max_input_bytes = infilesize;
+ FLAC__uint64 total_input_bytes_read = 0;
+ while(total_input_bytes_read < max_input_bytes) {
+ {
+ size_t wanted = (CHUNK_OF_SAMPLES * encoder_session.info.bytes_per_wide_sample);
+ wanted = (size_t) min((FLAC__uint64)wanted, max_input_bytes - total_input_bytes_read);
+
+ if(lookahead_length > 0) {
+ FLAC__ASSERT(lookahead_length <= wanted);
+ memcpy(ucbuffer_, lookahead, lookahead_length);
+ wanted -= lookahead_length;
+ bytes_read = lookahead_length;
+ if(wanted > 0) {
+ bytes_read += fread(ucbuffer_+lookahead_length, sizeof(unsigned char), wanted, infile);
+ if(ferror(infile)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
+ lookahead_length = 0;
+ }
+ else
+ bytes_read = fread(ucbuffer_, sizeof(unsigned char), wanted, infile);
+ }
+
+ if(bytes_read == 0) {
+ if(ferror(infile)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ else if(feof(infile)) {
+ flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
+ if(encoder_session.treat_warnings_as_errors)
+ return EncoderSession_finish_error(&encoder_session);
+ total_input_bytes_read = max_input_bytes;
+ }
+ }
+ else {
+ if(bytes_read % encoder_session.info.bytes_per_wide_sample != 0) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ else {
+ unsigned wide_samples = bytes_read / encoder_session.info.bytes_per_wide_sample;
+ if(!format_input(input_, wide_samples, encoder_session.info.is_big_endian, encoder_session.info.is_unsigned_samples, encoder_session.info.channels, encoder_session.info.bits_per_sample, encoder_session.info.shift, channel_map))
+ return EncoderSession_finish_error(&encoder_session);
+
+ if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
+ print_error_with_state(&encoder_session, "ERROR during encoding");
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ total_input_bytes_read += bytes_read;
+ }
+ }
+ }
+ }
break;
- if(!FLAC__stream_decoder_process_single(decoder)) {
- flac__utils_printf(stderr, 1, "%s: ERROR: while decoding FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
- goto fubar2; /*@@@ yuck */
- }
+ case FORMAT_WAVE:
+ case FORMAT_AIFF:
+ case FORMAT_AIFF_C:
+ while(encoder_session.fmt.iff.data_bytes > 0) {
+ size_t bytes_read = fread(ucbuffer_, sizeof(unsigned char), min(encoder_session.fmt.iff.data_bytes, CHUNK_OF_SAMPLES * encoder_session.info.bytes_per_wide_sample), infile);
+ if(bytes_read == 0) {
+ if(ferror(infile)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ else if(feof(infile)) {
+ if(options.ignore_chunk_sizes) {
+ flac__utils_printf(stderr, 1, "%s: INFO: hit EOF with --ignore-chunk-sizes, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.samples_written);
+ }
+ else {
+ flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
+ if(encoder_session.treat_warnings_as_errors)
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ encoder_session.fmt.iff.data_bytes = 0;
+ }
+ }
+ else {
+ if(bytes_read % encoder_session.info.bytes_per_wide_sample != 0) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ else {
+ unsigned wide_samples = bytes_read / encoder_session.info.bytes_per_wide_sample;
+ if(!format_input(input_, wide_samples, encoder_session.info.is_big_endian, encoder_session.info.is_unsigned_samples, encoder_session.info.channels, encoder_session.info.bits_per_sample, encoder_session.info.shift, channel_map))
+ return EncoderSession_finish_error(&encoder_session);
+
+ if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
+ print_error_with_state(&encoder_session, "ERROR during encoding");
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ encoder_session.fmt.iff.data_bytes -= bytes_read;
+ }
+ }
+ }
+ break;
+ case FORMAT_FLAC:
+ case FORMAT_OGGFLAC:
+ while(!encoder_session.fmt.flac.client_data.fatal_error && encoder_session.fmt.flac.client_data.samples_left_to_process > 0) {
+ /* We can also hit the end of stream without samples_left_to_process
+ * going to 0 if there are errors and continue_through_decode_errors
+ * is on, so we want to break in that case too:
+ */
+ if(encoder_session.continue_through_decode_errors && FLAC__stream_decoder_get_state(encoder_session.fmt.flac.decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
+ break;
+ if(!FLAC__stream_decoder_process_single(encoder_session.fmt.flac.decoder)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: while decoding FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(encoder_session.fmt.flac.decoder));
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
+ if(encoder_session.fmt.flac.client_data.fatal_error) {
+ flac__utils_printf(stderr, 1, "%s: ERROR: while decoding FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(encoder_session.fmt.flac.decoder));
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ break;
+ default:
+ FLAC__ASSERT(0);
+ /* double protection */
+ return EncoderSession_finish_error(&encoder_session);
}
- if(decoder_data.fatal_error) {
- flac__utils_printf(stderr, 1, "%s: ERROR: while decoding FLAC input, state = %s\n", encoder_session.inbasefilename, FLAC__stream_decoder_get_resolved_state_string(decoder));
- goto fubar2; /*@@@ yuck */
+
+ /*
+ * 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;
+ for(channel = 0; channel < encoder_session.info.channels; channel++)
+ memset(input_[channel], 0, sizeof(input_[0][0]) * wide_samples);
+
+ if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
+ print_error_with_state(&encoder_session, "ERROR during encoding");
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
+ }
+ else {
+ if(*options.align_reservoir_samples > 0) {
+ size_t bytes_read;
+ FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588);
+ bytes_read = fread(ucbuffer_, sizeof(unsigned char), (*options.align_reservoir_samples) * encoder_session.info.bytes_per_wide_sample, infile);
+ if(bytes_read == 0 && ferror(infile)) {
+ flac__utils_printf(stderr, 1, "%s: ERROR during read\n", encoder_session.inbasefilename);
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ else if(bytes_read != (*options.align_reservoir_samples) * encoder_session.info.bytes_per_wide_sample) {
+ flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
+ if(encoder_session.treat_warnings_as_errors)
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ else {
+ info_align_carry = *options.align_reservoir_samples;
+ if(!format_input(options.align_reservoir, *options.align_reservoir_samples, encoder_session.info.is_big_endian, encoder_session.info.is_unsigned_samples, encoder_session.info.channels, encoder_session.info.bits_per_sample, encoder_session.info.shift, channel_map))
+ return EncoderSession_finish_error(&encoder_session);
+ }
+ }
+ }
}
}
- FLAC__stream_decoder_delete(decoder);
- retval = EncoderSession_finish_ok(&encoder_session, -1, -1, /*foreign_metadata=*/0);
- /* have to wail until encoder is completely finished before deleting because of the final step of writing the seekpoint offsets */
- for(i = 0; i < decoder_data.num_metadata_blocks; i++)
- FLAC__metadata_object_delete(decoder_data.metadata_blocks[i]);
- return retval;
-
-fubar2:
- for(i = 0; i < decoder_data.num_metadata_blocks; i++)
- FLAC__metadata_object_delete(decoder_data.metadata_blocks[i]);
-fubar1:
- FLAC__stream_decoder_delete(decoder);
- return EncoderSession_finish_error(&encoder_session);
+ return EncoderSession_finish_ok(
+ &encoder_session,
+ info_align_carry,
+ info_align_zero,
+ (options.format == FORMAT_WAVE || options.format == FORMAT_AIFF || options.format == FORMAT_AIFF_C)? options.format_options.iff.foreign_metadata : 0
+ );
}
-FLAC__bool EncoderSession_construct(EncoderSession *e, FLAC__bool use_ogg, FLAC__bool verify, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FILE *infile, const char *infilename, const char *outfilename)
+FLAC__bool EncoderSession_construct(EncoderSession *e, encode_options_t options, off_t infilesize, FILE *infile, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length)
{
unsigned i;
FLAC__uint32 test = 1;
*/
#if FLAC__HAS_OGG
- e->use_ogg = use_ogg;
-#else
- (void)use_ogg;
+ e->use_ogg = options.use_ogg;
#endif
- e->verify = verify;
- e->treat_warnings_as_errors = treat_warnings_as_errors;
- e->continue_through_decode_errors = continue_through_decode_errors;
+ e->verify = options.verify;
+ e->treat_warnings_as_errors = options.treat_warnings_as_errors;
+ e->continue_through_decode_errors = options.continue_through_decode_errors;
e->is_stdout = (0 == strcmp(outfilename, "-"));
e->outputfile_opened = false;
e->infilename = infilename;
e->outfilename = outfilename;
- e->skip = 0; /* filled in later after the sample_rate is known */
- e->channels = 0;
- e->bits_per_sample = 0;
- e->sample_rate = 0;
- e->unencoded_size = 0;
e->total_samples_to_encode = 0;
+ e->unencoded_size = 0;
e->bytes_written = 0;
e->samples_written = 0;
e->stats_mask = 0;
+ memset(&e->info, 0, sizeof(e->info));
+
+ e->format = options.format;
+
+ switch(options.format) {
+ case FORMAT_RAW:
+ break;
+ case FORMAT_WAVE:
+ case FORMAT_AIFF:
+ case FORMAT_AIFF_C:
+ e->fmt.iff.data_bytes = 0;
+ e->fmt.iff.pad = 0;
+ break;
+ case FORMAT_FLAC:
+ case FORMAT_OGGFLAC:
+ e->fmt.flac.decoder = 0;
+ e->fmt.flac.client_data.filesize = infilesize;
+ e->fmt.flac.client_data.lookahead = lookahead;
+ e->fmt.flac.client_data.lookahead_length = lookahead_length;
+ e->fmt.flac.client_data.num_metadata_blocks = 0;
+ e->fmt.flac.client_data.samples_left_to_process = 0;
+ e->fmt.flac.client_data.fatal_error = false;
+ break;
+ default:
+ FLAC__ASSERT(0);
+ /* double protection */
+ return false;
+ }
+
e->encoder = 0;
e->fin = infile;
int ret = 0;
FLAC__bool verify_error = false;
+ /*@@@ can this go in EncoderSession_destroy()? it's duplicated in EncoderSession_finish_error() */
+ if(e->format == FORMAT_FLAC || e->format == FORMAT_OGGFLAC) {
+ size_t i;
+ for(i = 0; i < e->fmt.flac.client_data.num_metadata_blocks; i++)
+ FLAC__metadata_object_delete(e->fmt.flac.client_data.metadata_blocks[i]);
+ e->fmt.flac.client_data.num_metadata_blocks = 0;
+ if(e->fmt.flac.decoder)
+ FLAC__stream_decoder_delete(e->fmt.flac.decoder);
+ e->fmt.flac.decoder = 0;
+ }
+
if(e->encoder) {
fse_state = FLAC__stream_encoder_get_state(e->encoder);
ret = FLAC__stream_encoder_finish(e->encoder)? 0 : 1;
{
FLAC__ASSERT(e->encoder);
+ /*@@@ can this go in EncoderSession_destroy()? it's duplicated in EncoderSession_finish_ok() */
+ if(e->format == FORMAT_FLAC || e->format == FORMAT_OGGFLAC) {
+ size_t i;
+ for(i = 0; i < e->fmt.flac.client_data.num_metadata_blocks; i++)
+ FLAC__metadata_object_delete(e->fmt.flac.client_data.metadata_blocks[i]);
+ e->fmt.flac.client_data.num_metadata_blocks = 0;
+ if(e->fmt.flac.decoder)
+ FLAC__stream_decoder_delete(e->fmt.flac.decoder);
+ e->fmt.flac.decoder = 0;
+ }
+
if(e->total_samples_to_encode > 0)
flac__utils_printf(stderr, 2, "\n");
return true;
}
-FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options, FLAC__uint32 channel_mask, unsigned channels, unsigned bps, unsigned sample_rate, const foreign_metadata_t *foreign_metadata, FLACDecoderData *flac_decoder_data)
+FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options)
{
+ const unsigned channels = e->info.channels;
+ const unsigned bps = e->info.bits_per_sample - e->info.shift;
+ const unsigned sample_rate = e->info.sample_rate;
+ FLACDecoderData *flac_decoder_data = (options.format == FORMAT_FLAC || options.format == FORMAT_OGGFLAC)? &e->fmt.flac.client_data : 0;
FLAC__StreamMetadata padding;
FLAC__StreamMetadata **metadata = 0;
static_metadata_t static_metadata;
static_metadata_init(&static_metadata);
e->replay_gain = options.replay_gain;
- e->channels = channels;
- e->bits_per_sample = bps;
- e->sample_rate = sample_rate;
apodizations[0] = '\0';
if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
vc_found = true;
if(flac_decoder_data->metadata_blocks[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT && options.vorbis_comment->data.vorbis_comment.num_comments > 0) {
- (void) flac__utils_get_channel_mask_tag(flac_decoder_data->metadata_blocks[i], &channel_mask);
+ (void) flac__utils_get_channel_mask_tag(flac_decoder_data->metadata_blocks[i], &e->info.channel_mask);
flac__utils_printf(stderr, 1, "%s: WARNING, replacing tags from input FLAC file with those given on the command-line\n", e->inbasefilename);
if(e->treat_warnings_as_errors) {
static_metadata_clear(&static_metadata);
if((!vc_found || options.vorbis_comment->data.vorbis_comment.num_comments > 0) && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
/* prepend ours */
FLAC__StreamMetadata *vc = FLAC__metadata_object_clone(options.vorbis_comment);
- if(0 == vc || (channel_mask && !flac__utils_set_channel_mask_tag(vc, channel_mask))) {
+ if(0 == vc || (e->info.channel_mask && !flac__utils_set_channel_mask_tag(vc, e->info.channel_mask))) {
flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for VORBIS_COMMENT block\n", e->inbasefilename);
static_metadata_clear(&static_metadata);
return false;
if(options.padding > 0)
p = options.padding;
if(p < 0)
- p = e->total_samples_to_encode / e->sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8;
+ p = e->total_samples_to_encode / sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8;
if(options.padding != 0) {
if(p > 0 && flac_decoder_data->num_metadata_blocks < sizeof(flac_decoder_data->metadata_blocks)/sizeof(flac_decoder_data->metadata_blocks[0])) {
flac_decoder_data->metadata_blocks[flac_decoder_data->num_metadata_blocks] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
* we're not encoding from FLAC so we will build the metadata
* from scratch
*/
+ const foreign_metadata_t *foreign_metadata = (options.format == FORMAT_WAVE || options.format == FORMAT_AIFF || options.format == FORMAT_AIFF_C)? options.format_options.iff.foreign_metadata : 0;
+
if(e->seek_table_template->data.seek_table.num_points > 0) {
e->seek_table_template->is_last = false; /* the encoder will set this for us */
static_metadata_append(&static_metadata, e->seek_table_template, /*needs_delete=*/false);
}
if(0 != static_metadata.cuesheet)
static_metadata_append(&static_metadata, static_metadata.cuesheet, /*needs_delete=*/false);
- if(channel_mask) {
- if(!flac__utils_set_channel_mask_tag(options.vorbis_comment, channel_mask)) {
+ if(e->info.channel_mask) {
+ if(!flac__utils_set_channel_mask_tag(options.vorbis_comment, e->info.channel_mask)) {
flac__utils_printf(stderr, 1, "%s: ERROR adding channel mask tag\n", e->inbasefilename);
static_metadata_clear(&static_metadata);
return false;
if(options.padding != 0) {
padding.is_last = false; /* the encoder will set this for us */
padding.type = FLAC__METADATA_TYPE_PADDING;
- padding.length = (unsigned)(options.padding>0? options.padding : (e->total_samples_to_encode / e->sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8));
+ padding.length = (unsigned)(options.padding>0? options.padding : (e->total_samples_to_encode / sample_rate < 20*60? FLAC_ENCODE__DEFAULT_PADDING : FLAC_ENCODE__DEFAULT_PADDING*8));
static_metadata_append(&static_metadata, &padding, /*needs_delete=*/false);
}
metadata = static_metadata.metadata;
FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples)
{
if(e->replay_gain) {
- if(!grabbag__replaygain_analyze(buffer, e->channels==2, e->bits_per_sample, samples)) {
+ if(!grabbag__replaygain_analyze(buffer, e->info.channels==2, e->info.bits_per_sample, samples)) {
flac__utils_printf(stderr, 1, "%s: WARNING, error while calculating ReplayGain\n", e->inbasefilename);
if(e->treat_warnings_as_errors)
return false;
if(num_requested_seek_points < 0) {
#if FLAC__HAS_OGG
/*@@@@@@ workaround ogg bug: too many seekpoints makes table not fit in one page */
- if(e->use_ogg && e->total_samples_to_encode > 0 && e->total_samples_to_encode / e->sample_rate / 10 > 230)
+ if(e->use_ogg && e->total_samples_to_encode > 0 && e->total_samples_to_encode / e->info.sample_rate / 10 > 230)
requested_seek_points = "230x;";
else
#endif
}
if(num_requested_seek_points > 0) {
- if(!grabbag__seektable_convert_specification_to_template(requested_seek_points, only_placeholders, e->total_samples_to_encode, e->sample_rate, e->seek_table_template, &has_real_points))
+ if(!grabbag__seektable_convert_specification_to_template(requested_seek_points, only_placeholders, e->total_samples_to_encode, e->info.sample_rate, e->seek_table_template, &has_real_points))
return false;
}
void encoder_progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
{
- EncoderSession *encoder_session = (EncoderSession*)client_data;
+ EncoderSession *e = (EncoderSession*)client_data;
(void)encoder, (void)total_frames_estimate;
- encoder_session->bytes_written = bytes_written;
- encoder_session->samples_written = samples_written;
+ e->bytes_written = bytes_written;
+ e->samples_written = samples_written;
- if(encoder_session->total_samples_to_encode > 0 && !((frames_written-1) & encoder_session->stats_mask))
- print_stats(encoder_session);
+ if(e->total_samples_to_encode > 0 && !((frames_written-1) & e->stats_mask))
+ print_stats(e);
}
FLAC__StreamDecoderReadStatus flac_decoder_read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
size_t n = 0;
- FLACDecoderData *data = (FLACDecoderData*)client_data;
+ EncoderSession *e = (EncoderSession*)client_data;
+ FLACDecoderData *data = &e->fmt.flac.client_data;
+
(void)decoder;
if (data->fatal_error)
/* get the rest from file */
if (*bytes > n) {
- *bytes = n + fread(buffer, 1, *bytes-n, data->encoder_session->fin);
- if(ferror(data->encoder_session->fin))
+ *bytes = n + fread(buffer, 1, *bytes-n, e->fin);
+ if(ferror(e->fin))
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
else if(0 == *bytes)
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
FLAC__StreamDecoderSeekStatus flac_decoder_seek_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
{
- FLACDecoderData *data = (FLACDecoderData*)client_data;
+ EncoderSession *e = (EncoderSession*)client_data;
(void)decoder;
- if(fseeko(data->encoder_session->fin, (off_t)absolute_byte_offset, SEEK_SET) < 0)
+ if(fseeko(e->fin, (off_t)absolute_byte_offset, SEEK_SET) < 0)
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
else
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
FLAC__StreamDecoderTellStatus flac_decoder_tell_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
{
- FLACDecoderData *data = (FLACDecoderData*)client_data;
+ EncoderSession *e = (EncoderSession*)client_data;
off_t pos;
(void)decoder;
- if((pos = ftello(data->encoder_session->fin)) < 0)
+ if((pos = ftello(e->fin)) < 0)
return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
else {
*absolute_byte_offset = (FLAC__uint64)pos;
FLAC__StreamDecoderLengthStatus flac_decoder_length_callback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
{
- FLACDecoderData *data = (FLACDecoderData*)client_data;
+ const EncoderSession *e = (EncoderSession*)client_data;
+ const FLACDecoderData *data = &e->fmt.flac.client_data;
(void)decoder;
- if(0 == data->filesize)
+ if(data->filesize < 0)
return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
else {
*stream_length = (FLAC__uint64)data->filesize;
FLAC__bool flac_decoder_eof_callback(const FLAC__StreamDecoder *decoder, void *client_data)
{
- FLACDecoderData *data = (FLACDecoderData*)client_data;
+ EncoderSession *e = (EncoderSession*)client_data;
(void)decoder;
- return feof(data->encoder_session->fin)? true : false;
+ return feof(e->fin)? true : false;
}
FLAC__StreamDecoderWriteStatus flac_decoder_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
- FLACDecoderData *data = (FLACDecoderData*)client_data;
+ EncoderSession *e = (EncoderSession*)client_data;
+ FLACDecoderData *data = &e->fmt.flac.client_data;
FLAC__uint64 n = min(data->samples_left_to_process, frame->header.blocksize);
(void)decoder;
- if(!EncoderSession_process(data->encoder_session, buffer, (unsigned)n)) {
- print_error_with_state(data->encoder_session, "ERROR during encoding");
+ if(!EncoderSession_process(e, buffer, (unsigned)n)) {
+ print_error_with_state(e, "ERROR during encoding");
data->fatal_error = true;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
void flac_decoder_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
- FLACDecoderData *data = (FLACDecoderData*)client_data;
+ EncoderSession *e = (EncoderSession*)client_data;
+ FLACDecoderData *data = &e->fmt.flac.client_data;
(void)decoder;
if (data->fatal_error)
void flac_decoder_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
- FLACDecoderData *data = (FLACDecoderData*)client_data;
+ EncoderSession *e = (EncoderSession*)client_data;
+ FLACDecoderData *data = &e->fmt.flac.client_data;
(void)decoder;
- flac__utils_printf(stderr, 1, "%s: ERROR got %s while decoding FLAC input\n", data->encoder_session->inbasefilename, FLAC__StreamDecoderErrorStatusString[status]);
- if(!data->encoder_session->continue_through_decode_errors)
+ flac__utils_printf(stderr, 1, "%s: ERROR got %s while decoding FLAC input\n", e->inbasefilename, FLAC__StreamDecoderErrorStatusString[status]);
+ if(!e->continue_through_decode_errors)
data->fatal_error = true;
}
void print_stats(const EncoderSession *encoder_session)
{
const FLAC__uint64 samples_written = min(encoder_session->total_samples_to_encode, encoder_session->samples_written);
+ const FLAC__uint64 uesize = encoder_session->unencoded_size;
#if defined _MSC_VER || defined __MINGW32__
/* with MSVC you have to spoon feed it the casting */
const double progress = (double)(FLAC__int64)samples_written / (double)(FLAC__int64)encoder_session->total_samples_to_encode;
- const double ratio = (double)(FLAC__int64)encoder_session->bytes_written / ((double)(FLAC__int64)encoder_session->unencoded_size * min(1.0, progress));
+ const double ratio = (double)(FLAC__int64)encoder_session->bytes_written / ((double)(FLAC__int64)(uesize? uesize:1) * min(1.0, progress));
#else
const double progress = (double)samples_written / (double)encoder_session->total_samples_to_encode;
- const double ratio = (double)encoder_session->bytes_written / ((double)encoder_session->unencoded_size * min(1.0, progress));
+ const double ratio = (double)encoder_session->bytes_written / ((double)(uesize? uesize:1) * min(1.0, progress));
#endif
FLAC__ASSERT(encoder_session->total_samples_to_encode > 0);
if(samples_written == encoder_session->total_samples_to_encode) {
- flac__utils_printf(stderr, 2, "\r%s:%s wrote %u bytes, ratio=%0.3f",
+ flac__utils_printf(stderr, 2, "\r%s:%s wrote %u bytes, ratio=",
encoder_session->inbasefilename,
encoder_session->verify? " Verify OK," : "",
- (unsigned)encoder_session->bytes_written,
- ratio
+ (unsigned)encoder_session->bytes_written
);
}
else {
- flac__utils_printf(stderr, 2, "\r%s: %u%% complete, ratio=%0.3f", encoder_session->inbasefilename, (unsigned)floor(progress * 100.0 + 0.5), ratio);
+ flac__utils_printf(stderr, 2, "\r%s: %u%% complete, ratio=", encoder_session->inbasefilename, (unsigned)floor(progress * 100.0 + 0.5));
}
+ if(uesize)
+ flac__utils_printf(stderr, 2, "%0.3f", ratio);
+ else
+ flac__utils_printf(stderr, 2, "N/A");
}
void print_error_with_init_status(const EncoderSession *e, const char *message, FLAC__StreamEncoderInitStatus init_status)