add support for 24-bit input
[platform/upstream/flac.git] / src / flac / encode.c
index 151dbde..ad81a52 100644 (file)
@@ -1,5 +1,5 @@
 /* flac - Command-line FLAC encoder/decoder
- * Copyright (C) 2000  Josh Coalson
+ * Copyright (C) 2000,2001  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -16,6 +16,8 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
+/*@@@ need to "_finish()" the verify decoder */
+
 #include <assert.h>
 #if defined _WIN32 && !defined __CYGWIN__
 /* where MSVC puts unlink() */
 #else
 # include <unistd.h>
 #endif
-#include <stdio.h> /* for FILE */
+#include <stdio.h> /* for FILE et al. */
+#include <stdlib.h> /* for malloc */
 #include <string.h> /* for strcmp() */
 #include "FLAC/all.h"
 #include "encode.h"
 
+#ifdef min
+#undef min
+#endif
+#define min(x,y) ((x)<(y)?(x):(y))
+
 #define CHUNK_OF_SAMPLES 2048
 
+typedef enum {
+       FLAC__VERIFY_OK,
+       FLAC__VERIFY_FAILED_IN_FRAME,
+       FLAC__VERIFY_FAILED_IN_METADATA
+} verify_code;
+
+typedef struct {
+       int32 *original[FLAC__MAX_CHANNELS];
+       unsigned size; /* of each original[] in samples */
+       unsigned tail; /* in wide samples */
+       const byte *encoded_signal;
+       unsigned encoded_signal_capacity;
+       unsigned encoded_bytes;
+       bool into_frames;
+       verify_code result;
+       FLAC__StreamDecoder *decoder;
+} verify_fifo_struct;
+
 typedef struct {
        FILE *fout;
        const char *outfile;
        FLAC__Encoder *encoder;
+       bool verify;
        bool verbose;
        uint64 unencoded_size;
        uint64 total_samples_to_encode;
        uint64 bytes_written;
        uint64 samples_written;
        unsigned current_frame;
+       verify_fifo_struct verify_fifo;
 } encoder_wrapper_struct;
 
 static bool is_big_endian_host;
 
-static unsigned char ucbuffer[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*(FLAC__MAX_BITS_PER_SAMPLE>>3)];
+static unsigned char ucbuffer[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*((FLAC__MAX_BITS_PER_SAMPLE+7)/8)];
 static signed char *scbuffer = (signed char *)ucbuffer;
 static uint16 *usbuffer = (uint16 *)ucbuffer;
 static int16 *ssbuffer = (int16 *)ucbuffer;
@@ -54,15 +82,19 @@ static int32 *input[FLAC__MAX_CHANNELS];
 
 /* local routines */
 static bool init(encoder_wrapper_struct *encoder_wrapper);
-static bool init_encoder(bool lax, bool do_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned channels, unsigned bps, unsigned sample_rate, encoder_wrapper_struct *encoder_wrapper);
-static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps);
+static bool init_encoder(bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned channels, unsigned bps, unsigned sample_rate, unsigned padding, encoder_wrapper_struct *encoder_wrapper);
+static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper);
 static FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
 static void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
+static FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
+static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
+static void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
+static void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
 static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
 static bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok);
 static bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok);
 
-int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 skip, bool lax, bool do_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision)
+int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding)
 {
        encoder_wrapper_struct encoder_wrapper;
        FILE *fin;
@@ -73,6 +105,7 @@ int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 ski
        uint32 xx;
 
        encoder_wrapper.encoder = 0;
+       encoder_wrapper.verify = verify;
        encoder_wrapper.verbose = verbose;
        encoder_wrapper.bytes_written = 0;
        encoder_wrapper.samples_written = 0;
@@ -188,19 +221,36 @@ int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 ski
                goto wav_abort_;
        data_bytes = xx;
 
-       if(!init_encoder(lax, do_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, blocksize, qlp_coeff_precision, channels, bps, sample_rate, &encoder_wrapper))
-               goto wav_abort_;
-
        bytes_per_wide_sample = channels * (bps >> 3);
 
-       if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
-               fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
-               goto wav_abort_;
+       if(skip > 0) {
+               if(fin != stdin) {
+                       if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
+                               fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
+                               goto wav_abort_;
+                       }
+               }
+               else {
+                       int64 left;
+                       unsigned need;
+                       for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) {
+                               need = min(left, CHUNK_OF_SAMPLES);
+                               if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) {
+                                       fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
+                                       goto wav_abort_;
+                               }
+                       }
+               }
        }
 
        encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample - skip;
        encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; /* 44 for the size of the WAV headers */
 
+       if(!init_encoder(lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, blocksize, qlp_coeff_precision, channels, bps, sample_rate, padding, &encoder_wrapper))
+               goto wav_abort_;
+
+       encoder_wrapper.verify_fifo.into_frames = true;
+
        while(data_bytes > 0) {
                bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
                if(bytes_read == 0) {
@@ -211,24 +261,28 @@ int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 ski
                        else if(feof(fin))
                                break;
                }
-               else if(bytes_read % bytes_per_wide_sample != 0) {
-                       fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
-                       goto wav_abort_;
-               }
                else {
-                       unsigned wide_samples = bytes_read / bytes_per_wide_sample;
-                       format_input(wide_samples, false, is_unsigned_samples, channels, bps);
-                       if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
-                               fprintf(stderr, "ERROR during encoding, state = %d\n", encoder_wrapper.encoder->state);
+                       if(bytes_read > data_bytes)
+                               bytes_read = data_bytes; /* chop off anything after the end of the data chunk */
+                       if(bytes_read % bytes_per_wide_sample != 0) {
+                               fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
                                goto wav_abort_;
                        }
-                       data_bytes -= bytes_read;
+                       else {
+                               unsigned wide_samples = bytes_read / bytes_per_wide_sample;
+                               format_input(wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper);
+                               if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
+                                       fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
+                                       goto wav_abort_;
+                               }
+                               data_bytes -= bytes_read;
+                       }
                }
        }
 
 wav_end_:
        if(encoder_wrapper.encoder) {
-               if(encoder_wrapper.encoder->state != FLAC__ENCODER_UNINITIALIZED)
+               if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
                        FLAC__encoder_finish(encoder_wrapper.encoder);
                FLAC__encoder_free_instance(encoder_wrapper.encoder);
        }
@@ -236,22 +290,40 @@ wav_end_:
                print_stats(&encoder_wrapper);
                printf("\n");
        }
+       if(verify) {
+               if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
+                       printf("Verify FAILED!  Do not use %s\n", outfile);
+                       return 1;
+               }
+               else {
+                       printf("Verify succeeded\n");
+               }
+       }
        fclose(fin);
        return 0;
 wav_abort_:
        if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
                printf("\n");
        if(encoder_wrapper.encoder) {
-               if(encoder_wrapper.encoder->state != FLAC__ENCODER_UNINITIALIZED)
+               if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
                        FLAC__encoder_finish(encoder_wrapper.encoder);
                FLAC__encoder_free_instance(encoder_wrapper.encoder);
        }
+       if(verify) {
+               if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
+                       printf("Verify FAILED!  Do not use %s\n", outfile);
+                       return 1;
+               }
+               else {
+                       printf("Verify succeeded\n");
+               }
+       }
        fclose(fin);
        unlink(outfile);
        return 1;
 }
 
-int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 skip, bool lax, bool do_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate)
+int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate)
 {
        encoder_wrapper_struct encoder_wrapper;
        FILE *fin;
@@ -259,6 +331,7 @@ int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 ski
        const size_t bytes_per_wide_sample = channels * (bps >> 3);
 
        encoder_wrapper.encoder = 0;
+       encoder_wrapper.verify = verify;
        encoder_wrapper.verbose = verbose;
        encoder_wrapper.bytes_written = 0;
        encoder_wrapper.samples_written = 0;
@@ -287,9 +360,6 @@ int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 ski
        if(!init(&encoder_wrapper))
                goto raw_abort_;
 
-       if(!init_encoder(lax, do_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, blocksize, qlp_coeff_precision, channels, bps, sample_rate, &encoder_wrapper))
-               goto raw_abort_;
-
        /* get the file length */
        if(0 != fseek(fin, 0, SEEK_END)) {
                encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
@@ -306,10 +376,33 @@ int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 ski
                }
        }
 
-       if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) {
-               fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
-               goto raw_abort_;
+       if(skip > 0) {
+               if(fin != stdin) {
+                       if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) {
+                               fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
+                               goto raw_abort_;
+                       }
+               }
+               else {
+                       int64 left;
+                       unsigned need;
+                       for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) {
+                               need = min(left, CHUNK_OF_SAMPLES);
+                               if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) {
+                                       fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
+                                       goto raw_abort_;
+                               }
+                       }
+               }
        }
+       else {
+               fseek(fin, 0, SEEK_SET);
+       }
+
+       if(!init_encoder(lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, blocksize, qlp_coeff_precision, channels, bps, sample_rate, padding, &encoder_wrapper))
+               goto raw_abort_;
+
+       encoder_wrapper.verify_fifo.into_frames = true;
 
        while(!feof(fin)) {
                bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
@@ -325,16 +418,16 @@ int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 ski
                }
                else {
                        unsigned wide_samples = bytes_read / bytes_per_wide_sample;
-                       format_input(wide_samples, is_big_endian, is_unsigned_samples, channels, bps);
+                       format_input(wide_samples, is_big_endian, is_unsigned_samples, channels, bps, &encoder_wrapper);
                        if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
-                               fprintf(stderr, "ERROR during encoding, state = %d\n", encoder_wrapper.encoder->state);
+                               fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
                                goto raw_abort_;
                        }
                }
        }
 
        if(encoder_wrapper.encoder) {
-               if(encoder_wrapper.encoder->state != FLAC__ENCODER_UNINITIALIZED)
+               if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
                        FLAC__encoder_finish(encoder_wrapper.encoder);
                FLAC__encoder_free_instance(encoder_wrapper.encoder);
        }
@@ -342,16 +435,34 @@ int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 ski
                print_stats(&encoder_wrapper);
                printf("\n");
        }
+       if(verify) {
+               if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
+                       printf("Verify FAILED!  Do not use %s\n", outfile);
+                       return 1;
+               }
+               else {
+                       printf("Verify succeeded\n");
+               }
+       }
        fclose(fin);
        return 0;
 raw_abort_:
        if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
                printf("\n");
        if(encoder_wrapper.encoder) {
-               if(encoder_wrapper.encoder->state != FLAC__ENCODER_UNINITIALIZED)
+               if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
                        FLAC__encoder_finish(encoder_wrapper.encoder);
                FLAC__encoder_free_instance(encoder_wrapper.encoder);
        }
+       if(verify) {
+               if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
+                       printf("Verify FAILED!  Do not use %s\n", outfile);
+                       return 1;
+               }
+               else {
+                       printf("Verify succeeded\n");
+               }
+       }
        fclose(fin);
        unlink(outfile);
        return 1;
@@ -376,10 +487,37 @@ bool init(encoder_wrapper_struct *encoder_wrapper)
        return true;
 }
 
-bool init_encoder(bool lax, bool do_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned channels, unsigned bps, unsigned sample_rate, encoder_wrapper_struct *encoder_wrapper)
+bool init_encoder(bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned channels, unsigned bps, unsigned sample_rate, unsigned padding, encoder_wrapper_struct *encoder_wrapper)
 {
-       if(channels != 2 || bps > 16)
-               do_mid_side = false;
+       if(channels != 2 /*@@@ not necessary? || bps > 16*/)
+               do_mid_side = loose_mid_side = false;
+
+       if(encoder_wrapper->verify) {
+               unsigned i;
+
+               /* set up the fifo which will hold the original signal to compare against */
+               encoder_wrapper->verify_fifo.size = blocksize + CHUNK_OF_SAMPLES;
+               for(i = 0; i < channels; i++) {
+                       if(0 == (encoder_wrapper->verify_fifo.original[i] = (int32*)malloc(sizeof(int32) * encoder_wrapper->verify_fifo.size))) {
+                               fprintf(stderr, "ERROR allocating verify buffers\n");
+                               return false;
+                       }
+               }
+               encoder_wrapper->verify_fifo.tail = 0;
+               encoder_wrapper->verify_fifo.into_frames = false;
+               encoder_wrapper->verify_fifo.result = FLAC__VERIFY_OK;
+
+               /* set up a stream decoder for verification */
+               encoder_wrapper->verify_fifo.decoder = FLAC__stream_decoder_get_new_instance();
+               if(0 == encoder_wrapper->verify_fifo.decoder) {
+                       fprintf(stderr, "ERROR creating the verify decoder instance\n");
+                       return false;
+               }
+               if(FLAC__stream_decoder_init(encoder_wrapper->verify_fifo.decoder, verify_read_callback, verify_write_callback, verify_metadata_callback, verify_error_callback, encoder_wrapper) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) {
+                       fprintf(stderr, "ERROR initializing decoder, state = %d:%s\n", encoder_wrapper->verify_fifo.decoder->state, FLAC__StreamDecoderStateString[encoder_wrapper->verify_fifo.decoder->state]);
+                       return false;
+               }
+       }
 
        encoder_wrapper->encoder->streamable_subset = !lax;
        encoder_wrapper->encoder->channels = channels;
@@ -389,9 +527,12 @@ bool init_encoder(bool lax, bool do_mid_side, bool do_exhaustive_model_search, b
        encoder_wrapper->encoder->qlp_coeff_precision = qlp_coeff_precision;
        encoder_wrapper->encoder->max_lpc_order = max_lpc_order;
        encoder_wrapper->encoder->do_mid_side_stereo = do_mid_side;
+       encoder_wrapper->encoder->loose_mid_side_stereo = loose_mid_side;
        encoder_wrapper->encoder->do_exhaustive_model_search = do_exhaustive_model_search;
        encoder_wrapper->encoder->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
        encoder_wrapper->encoder->rice_optimization_level = rice_optimization_level;
+       encoder_wrapper->encoder->total_samples_estimate = encoder_wrapper->total_samples_to_encode;
+       encoder_wrapper->encoder->padding = padding;
 
        if(FLAC__encoder_init(encoder_wrapper->encoder, write_callback, metadata_callback, encoder_wrapper) != FLAC__ENCODER_OK) {
                fprintf(stderr, "ERROR initializing encoder, state = %d\n", encoder_wrapper->encoder->state);
@@ -401,7 +542,7 @@ bool init_encoder(bool lax, bool do_mid_side, bool do_exhaustive_model_search, b
        return true;
 }
 
-void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps)
+void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper)
 {
        unsigned wide_sample, sample, channel, byte;
 
@@ -409,7 +550,7 @@ void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_sa
                if(is_unsigned_samples) {
                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
                                for(channel = 0; channel < channels; channel++, sample++)
-                                       input[channel][wide_sample] = (int32)ucbuffer[sample] - 128;
+                                       input[channel][wide_sample] = (int32)ucbuffer[sample] - 0x80;
                }
                else {
                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
@@ -417,7 +558,7 @@ void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_sa
                                        input[channel][wide_sample] = (int32)scbuffer[sample];
                }
        }
-       else {
+       else if(bps == 16) {
                if(is_big_endian != is_big_endian_host) {
                        unsigned char tmp;
                        const unsigned bytes = wide_samples * channels * (bps >> 3);
@@ -430,7 +571,7 @@ void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_sa
                if(is_unsigned_samples) {
                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
                                for(channel = 0; channel < channels; channel++, sample++)
-                                       input[channel][wide_sample] = (int32)usbuffer[sample] - 32768;
+                                       input[channel][wide_sample] = (int32)usbuffer[sample] - 0x8000;
                }
                else {
                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
@@ -438,6 +579,44 @@ void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_sa
                                        input[channel][wide_sample] = (int32)ssbuffer[sample];
                }
        }
+       else if(bps == 24) {
+               if(!is_big_endian) {
+                       unsigned char tmp;
+                       const unsigned bytes = wide_samples * channels * (bps >> 3);
+                       for(byte = 0; byte < bytes; byte += 3) {
+                               tmp = ucbuffer[byte];
+                               ucbuffer[byte] = ucbuffer[byte+2];
+                               ucbuffer[byte+2] = tmp;
+                       }
+               }
+               if(is_unsigned_samples) {
+                       for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                               for(channel = 0; channel < channels; channel++, sample++) {
+                                       input[channel][wide_sample]  = ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
+                                       input[channel][wide_sample] |= ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
+                                       input[channel][wide_sample] |= ucbuffer[byte++];
+                                       input[channel][wide_sample] -= 0x800000;
+                               }
+               }
+               else {
+                       for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                               for(channel = 0; channel < channels; channel++, sample++) {
+                                       input[channel][wide_sample]  = scbuffer[byte++]; input[channel][wide_sample] <<= 8;
+                                       input[channel][wide_sample] |= ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
+                                       input[channel][wide_sample] |= ucbuffer[byte++];
+                               }
+               }
+       }
+       else {
+               assert(0);
+       }
+
+       if(encoder_wrapper->verify) {
+               for(channel = 0; channel < channels; channel++)
+                       memcpy(&encoder_wrapper->verify_fifo.original[channel][encoder_wrapper->verify_fifo.tail], &input[channel][0], sizeof(int32) * wide_samples);
+               encoder_wrapper->verify_fifo.tail += wide_samples;
+               assert(encoder_wrapper->verify_fifo.tail <= encoder_wrapper->verify_fifo.size);
+       }
 }
 
 FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
@@ -452,6 +631,23 @@ FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte
        if(samples && encoder_wrapper->verbose && encoder_wrapper->total_samples_to_encode > 0 && !(current_frame & mask))
                print_stats(encoder_wrapper);
 
+       if(encoder_wrapper->verify) {
+               encoder_wrapper->verify_fifo.encoded_signal = buffer;
+               encoder_wrapper->verify_fifo.encoded_bytes = bytes;
+               if(encoder_wrapper->verify_fifo.into_frames) {
+                       if(!FLAC__stream_decoder_process_one_frame(encoder_wrapper->verify_fifo.decoder)) {
+                               encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_FRAME;
+                               return FLAC__ENCODER_WRITE_FATAL_ERROR;
+                       }
+               }
+               else {
+                       if(!FLAC__stream_decoder_process_metadata(encoder_wrapper->verify_fifo.decoder)) {
+                               encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_METADATA;
+                               return FLAC__ENCODER_WRITE_FATAL_ERROR;
+                       }
+               }
+       }
+
        if(fwrite(buffer, sizeof(byte), bytes, encoder_wrapper->fout) == bytes)
                return FLAC__ENCODER_WRITE_OK;
        else
@@ -463,9 +659,9 @@ void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData
        encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
        byte b;
        FILE *f;
-       const uint64 samples = metadata->data.encoding.total_samples;
-       const unsigned min_framesize = metadata->data.encoding.min_framesize;
-       const unsigned max_framesize = metadata->data.encoding.max_framesize;
+       const uint64 samples = metadata->data.stream_info.total_samples;
+       const unsigned min_framesize = metadata->data.stream_info.min_framesize;
+       const unsigned max_framesize = metadata->data.stream_info.max_framesize;
 
        (void)encoder; /* silence compiler warning about unused parameter */
 
@@ -481,6 +677,10 @@ void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData
         * would also break all streams encoded in the previous format.
         */
 
+       if(-1 == fseek(f, 26, SEEK_SET)) goto samples_;
+       fwrite(metadata->data.stream_info.md5sum, 1, 16, f);
+
+samples_:
        if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
        if(fread(&b, 1, 1, f) != 1) goto framesize_;
        if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
@@ -510,10 +710,68 @@ framesize_:
        b = (byte)(max_framesize & 0xFF);
        if(fwrite(&b, 1, 1, f) != 1) goto end_;
 end_:
-       fclose(encoder_wrapper->fout);
+       fclose(f);
        return;
 }
 
+FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data)
+{
+       encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
+       const unsigned encoded_bytes = encoder_wrapper->verify_fifo.encoded_bytes;
+       (void)decoder;
+
+       if(encoded_bytes <= *bytes) {
+               *bytes = encoded_bytes;
+               memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
+       }
+       else {
+               memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
+               encoder_wrapper->verify_fifo.encoded_signal += *bytes;
+               encoder_wrapper->verify_fifo.encoded_bytes -= *bytes;
+       }
+
+       return FLAC__STREAM_DECODER_READ_CONTINUE;
+}
+
+FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
+{
+       encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
+       unsigned channel, l, r;
+
+       for(channel = 0; channel < decoder->channels; channel++) {
+               if(0 != memcmp(buffer[channel], encoder_wrapper->verify_fifo.original[channel], sizeof(int32) * decoder->blocksize)) {
+                       fprintf(stderr, "\nERROR: mismatch in decoded data, verify FAILED!\n");
+                       fprintf(stderr, "       Please submit a bug report to http://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
+for(l=0;l<decoder->blocksize;l++)
+if(buffer[channel][l]!=encoder_wrapper->verify_fifo.original[channel][l])break;
+fprintf(stderr,"@@@channel=%u, sample=%u, expected %08x, got %08x\n",channel,l,buffer[channel][l],encoder_wrapper->verify_fifo.original[channel][l]);
+                       return FLAC__STREAM_DECODER_WRITE_ABORT;
+               }
+       }
+       /* dequeue the frame from the fifo */
+       for(channel = 0; channel < decoder->channels; channel++) {
+               for(l = 0, r = frame->header.blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
+                       encoder_wrapper->verify_fifo.original[channel][l] = encoder_wrapper->verify_fifo.original[channel][r];
+               }
+       }
+       encoder_wrapper->verify_fifo.tail -= frame->header.blocksize;
+       return FLAC__STREAM_DECODER_WRITE_CONTINUE;
+}
+
+void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
+{
+       (void)decoder;
+       (void)metadata;
+       (void)client_data;
+}
+
+void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
+{
+       (void)decoder;
+       (void)client_data;
+       fprintf(stderr, "\nERROR: verification decoder returned error %d:%s\n", status, FLAC__StreamDecoderErrorStatusString[status]);
+}
+
 void print_stats(const encoder_wrapper_struct *encoder_wrapper)
 {
 #ifdef _MSC_VER