- unsigned int frames= bytes_read/bytes_per_frame;
- 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_frame)) {
- 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_frame, 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_frame) {
- 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 { /* other chunk */
- if(!memcmp(chunk_id, "COMM", 4)) {
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'COMM' chunk\n", encoder_session.inbasefilename);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- else if(!memcmp(chunk_id, "SSND", 4)) {
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'SSND' chunk\n", encoder_session.inbasefilename);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
- else {
- flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s'\n", encoder_session.inbasefilename, chunk_id);
- if(encoder_session.treat_warnings_as_errors)
- return EncoderSession_finish_error(&encoder_session);
- }
-
- /* chunk size */
- if(!read_big_endian_uint32(infile, &xx, false, 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)) {
- fprintf(stderr, "%s: ERROR during read while skipping unknown 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);
-}
-
-int flac__encode_wav(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 bytes_per_wide_sample, bytes_read;
- 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;
- unsigned align_remainder = 0;
- int info_align_carry = -1, info_align_zero = -1;
-
- (void)infilesize;
- (void)lookahead;
- (void)lookahead_length;
-
- 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;
- }
-
- /*
- * lookahead[] already has "RIFFxxxxWAVE", do sub-chunks
- */
- while(!feof(infile)) {
- if(!read_little_endian_uint32(infile, &xx, true, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- if(feof(infile))
- break;
- if(xx == 0x20746d66 && !got_fmt_chunk) { /* "fmt " */
- 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: subchunk 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)
- * WAVEFORMAT 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 sub-chunk size */
- if(!read_little_endian_uint32(infile, &xx, false, 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 ' sub-chunk which has length = %u\n", encoder_session.inbasefilename, data_bytes);
- return EncoderSession_finish_error(&encoder_session);
- }
- /* format code */
- if(!read_little_endian_uint16(infile, &format, false, 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_little_endian_uint16(infile, &x, false, 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_little_endian_uint32(infile, &xx, false, 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_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- /* block align */
- if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
- return EncoderSession_finish_error(&encoder_session);
- block_align = (unsigned)x;
- /* bits per sample */
- if(!read_little_endian_uint16(infile, &x, false, 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);