change force_mid_side to loose_mid_side
authorJosh Coalson <jcoalson@users.sourceforce.net>
Sun, 28 Jan 2001 09:27:27 +0000 (09:27 +0000)
committerJosh Coalson <jcoalson@users.sourceforce.net>
Sun, 28 Jan 2001 09:27:27 +0000 (09:27 +0000)
include/FLAC/encoder.h
src/flac/encode.c
src/flac/encode.h
src/flac/main.c
src/libFLAC/encoder.c
src/libFLAC/stream_decoder.c

index 366ddde..90c37e6 100644 (file)
@@ -57,7 +57,7 @@ typedef struct {
        FLAC__EncoderState state;             /* must be FLAC__ENCODER_UNINITIALIZED when passed to FLAC__encoder_init() */
        bool     streamable_subset;
        bool     do_mid_side_stereo;          /* 0 or 1; 1 only if channels==2 */
-       bool     force_mid_side_stereo;       /* 0 or 1; 1 only if channels==2 and do_mid_side_stereo==true */
+       bool     loose_mid_side_stereo;       /* 0 or 1; 1 only if channels==2 and do_mid_side_stereo==true */
        unsigned channels;                    /* must be <= FLAC__MAX_CHANNELS */
        unsigned bits_per_sample;             /* do not give the encoder wider data than what you specify here or bad things will happen! */
        unsigned sample_rate;
index 3eb544e..851dda8 100644 (file)
@@ -81,7 +81,7 @@ 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 force_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 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, 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);
@@ -93,7 +93,7 @@ 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 verify, bool lax, bool do_mid_side, bool force_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)
 {
        encoder_wrapper_struct encoder_wrapper;
        FILE *fin;
@@ -245,7 +245,7 @@ int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 ski
        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, force_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))
+       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, &encoder_wrapper))
                goto wav_abort_;
 
        encoder_wrapper.verify_fifo.into_frames = true;
@@ -322,7 +322,7 @@ wav_abort_:
        return 1;
 }
 
-int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, bool lax, bool do_mid_side, bool force_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, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate)
 {
        encoder_wrapper_struct encoder_wrapper;
        FILE *fin;
@@ -395,7 +395,7 @@ int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 ski
                }
        }
 
-       if(!init_encoder(lax, do_mid_side, force_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))
+       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, &encoder_wrapper))
                goto raw_abort_;
 
        encoder_wrapper.verify_fifo.into_frames = true;
@@ -483,10 +483,10 @@ bool init(encoder_wrapper_struct *encoder_wrapper)
        return true;
 }
 
-bool init_encoder(bool lax, bool do_mid_side, bool force_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, encoder_wrapper_struct *encoder_wrapper)
 {
        if(channels != 2 || bps > 16)
-               do_mid_side = force_mid_side = false;
+               do_mid_side = loose_mid_side = false;
 
        if(encoder_wrapper->verify) {
                unsigned i;
@@ -523,7 +523,7 @@ bool init_encoder(bool lax, bool do_mid_side, bool force_mid_side, bool do_exhau
        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->force_mid_side_stereo = force_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;
index e637595..41ffa44 100644 (file)
@@ -21,7 +21,7 @@
 
 #include "FLAC/ordinals.h"
 
-int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, bool lax, bool do_mid_side, bool force_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_raw(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, bool lax, bool do_mid_side, bool force_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_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);
+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, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate);
 
 #endif
index 720b2bf..12d41cc 100644 (file)
@@ -32,7 +32,7 @@ int main(int argc, char *argv[])
 {
        int i;
        bool verify = false, verbose = true, lax = false, mode_decode = false, test_only = false, analyze = false;
-       bool do_mid_side = true, force_mid_side = false, do_exhaustive_model_search = false, do_qlp_coeff_prec_search = false;
+       bool do_mid_side = true, loose_mid_side = false, do_exhaustive_model_search = false, do_qlp_coeff_prec_search = false;
        unsigned max_lpc_order = 8;
        unsigned qlp_coeff_precision = 0;
        uint64 skip = 0;
@@ -80,9 +80,9 @@ int main(int argc, char *argv[])
                else if(0 == strcmp(argv[i], "-m-"))
                        do_mid_side = false;
                else if(0 == strcmp(argv[i], "-M"))
-                       force_mid_side = do_mid_side = true;
+                       loose_mid_side = do_mid_side = true;
                else if(0 == strcmp(argv[i], "-M-"))
-                       force_mid_side = do_mid_side = false;
+                       loose_mid_side = do_mid_side = false;
                else if(0 == strcmp(argv[i], "-p"))
                        do_qlp_coeff_prec_search = true;
                else if(0 == strcmp(argv[i], "-p-"))
@@ -114,7 +114,7 @@ int main(int argc, char *argv[])
                else if(0 == strcmp(argv[i], "-0")) {
                        do_exhaustive_model_search = false;
                        do_mid_side = false;
-                       force_mid_side = false;
+                       loose_mid_side = false;
                        qlp_coeff_precision = 0;
                        rice_optimization_level = 0;
                        max_lpc_order = 0;
@@ -122,7 +122,7 @@ int main(int argc, char *argv[])
                else if(0 == strcmp(argv[i], "-1")) {
                        do_exhaustive_model_search = false;
                        do_mid_side = true;
-                       force_mid_side = true;
+                       loose_mid_side = true;
                        qlp_coeff_precision = 0;
                        rice_optimization_level = 0;
                        max_lpc_order = 0;
@@ -130,14 +130,14 @@ int main(int argc, char *argv[])
                else if(0 == strcmp(argv[i], "-2")) {
                        do_exhaustive_model_search = false;
                        do_mid_side = true;
-                       force_mid_side = false;
+                       loose_mid_side = false;
                        qlp_coeff_precision = 0;
                        max_lpc_order = 0;
                }
                else if(0 == strcmp(argv[i], "-4")) {
                        do_exhaustive_model_search = false;
                        do_mid_side = false;
-                       force_mid_side = false;
+                       loose_mid_side = false;
                        qlp_coeff_precision = 0;
                        rice_optimization_level = 0;
                        max_lpc_order = 8;
@@ -145,7 +145,7 @@ int main(int argc, char *argv[])
                else if(0 == strcmp(argv[i], "-5")) {
                        do_exhaustive_model_search = false;
                        do_mid_side = true;
-                       force_mid_side = true;
+                       loose_mid_side = true;
                        qlp_coeff_precision = 0;
                        rice_optimization_level = 0;
                        max_lpc_order = 8;
@@ -153,21 +153,21 @@ int main(int argc, char *argv[])
                else if(0 == strcmp(argv[i], "-6")) {
                        do_exhaustive_model_search = false;
                        do_mid_side = true;
-                       force_mid_side = false;
+                       loose_mid_side = false;
                        qlp_coeff_precision = 0;
                        max_lpc_order = 8;
                }
                else if(0 == strcmp(argv[i], "-8")) {
                        do_exhaustive_model_search = false;
                        do_mid_side = true;
-                       force_mid_side = false;
+                       loose_mid_side = false;
                        qlp_coeff_precision = 0;
                        max_lpc_order = 32;
                }
                else if(0 == strcmp(argv[i], "-9")) {
                        do_exhaustive_model_search = true;
                        do_mid_side = true;
-                       force_mid_side = false;
+                       loose_mid_side = false;
                        do_qlp_coeff_prec_search = true;
                        rice_optimization_level = 99;
                        max_lpc_order = 32;
@@ -263,7 +263,7 @@ int main(int argc, char *argv[])
 
                if(!mode_decode) {
                        printf("options:%s -b %u%s -l %u%s%s -q %u -r %u%s\n",
-                               lax?" --lax":"", (unsigned)blocksize, force_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order,
+                               lax?" --lax":"", (unsigned)blocksize, loose_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order,
                                do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"",
                                qlp_coeff_precision, (unsigned)rice_optimization_level,
                                verify? " -V":""
@@ -278,9 +278,9 @@ int main(int argc, char *argv[])
                        return decode_raw(argv[i], test_only? 0 : argv[i+1], analyze, verbose, skip, format_is_big_endian, format_is_unsigned_samples);
        else
                if(format_is_wave)
-                       return encode_wav(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, force_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision);
+                       return encode_wav(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision);
                else
-                       return encode_raw(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, force_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate);
+                       return encode_raw(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate);
 
        return 0;
 }
@@ -340,7 +340,7 @@ int usage(const char *message, ...)
        printf("  --lax : allow encoder to generate non-Subset files\n");
        printf("  -b blocksize : default is 1152 for -l 0, else 4608; should be 192/576/1152/2304/4608 (unless --lax is used)\n");
        printf("  -m : try mid-side coding for each frame (stereo input only)\n");
-       printf("  -M : force mid-side coding for all frames (stereo input only)\n");
+       printf("  -M : loose mid-side coding for all frames (stereo input only)\n");
        printf("  -0 .. -9 : fastest compression .. highest compression, default is -6\n");
        printf("             these are synonyms for other options:\n");
        printf("  -0 : synonymous with -l 0\n");
index 9815a9f..5f186ba 100644 (file)
@@ -57,6 +57,10 @@ typedef struct FLAC__EncoderPrivate {
        uint32 *abs_residual;                       /* workspace where the abs(candidate residual) is stored */
        FLAC__BitBuffer frame;                      /* the current frame being worked on */
        bool current_frame_can_do_mid_side;         /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */
+       double loose_mid_side_stereo_frames_exact;  /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
+       unsigned loose_mid_side_stereo_frames;      /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
+       unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
+       FLAC__ChannelAssignment last_channel_assignment;
        FLAC__StreamMetaData metadata;
        unsigned current_sample_number;
        unsigned current_frame_number;
@@ -69,8 +73,8 @@ typedef struct FLAC__EncoderPrivate {
 static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
 static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
 static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
-static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
-static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
+static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
+static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
 static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe);
 static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe);
 static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], const real lp_coeff[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe);
@@ -261,7 +265,7 @@ FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWrite
        if(encoder->do_mid_side_stereo && encoder->bits_per_sample > 16)
                return encoder->state = FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH;
 
-       if(encoder->force_mid_side_stereo && !encoder->do_mid_side_stereo)
+       if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
                return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
 
        if(encoder->bits_per_sample == 0 || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
@@ -346,6 +350,11 @@ FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWrite
        }
        encoder->guts->abs_residual = 0;
        encoder->guts->current_frame_can_do_mid_side = true;
+       encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
+       encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
+       if(encoder->guts->loose_mid_side_stereo_frames == 0)
+               encoder->guts->loose_mid_side_stereo_frames = 1;
+       encoder->guts->loose_mid_side_stereo_frame_count = 0;
        encoder->guts->current_sample_number = 0;
        encoder->guts->current_frame_number = 0;
 
@@ -482,7 +491,7 @@ bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned
                                        encoder->guts->current_frame_can_do_mid_side = false;
                                }
                                else {
-                                       mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as divide-by-two ! */
+                                       mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
                                        encoder->guts->integer_signal_mid_side[0][i] = mid;
                                        encoder->guts->integer_signal_mid_side[1][i] = side;
                                        encoder->guts->real_signal_mid_side[0][i] = (real)mid;
@@ -529,7 +538,7 @@ bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[]
                                                        encoder->guts->current_frame_can_do_mid_side = false;
                                                }
                                                else {
-                                                       mid = (left + x) >> 1; /* NOTE: not the same as divide-by-two ! */
+                                                       mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
                                                        encoder->guts->integer_signal_mid_side[0][i] = mid;
                                                        encoder->guts->integer_signal_mid_side[1][i] = side;
                                                        encoder->guts->real_signal_mid_side[0][i] = (real)mid;
@@ -604,6 +613,7 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
 {
        FLAC__FrameHeader frame_header;
        unsigned channel, max_partition_order;
+       bool do_independent, do_mid_side;
 
        /*
         * Calculate the max Rice partition order
@@ -635,21 +645,53 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
        frame_header.number.frame_number = encoder->guts->current_frame_number;
 
        /*
+        * Figure out what channel assignments to try
+        */
+       if(encoder->do_mid_side_stereo) {
+               if(encoder->loose_mid_side_stereo) {
+                       if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
+                               do_independent = true;
+                               do_mid_side = true;
+                       }
+                       else {
+                               do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
+                               do_mid_side = !do_independent;
+                       }
+               }
+               else {
+                       do_independent = true;
+                       do_mid_side = true;
+               }
+       }
+       else {
+               do_independent = true;
+               do_mid_side = false;
+       }
+       if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
+               do_independent = true;
+               do_mid_side = false;
+       }
+
+       assert(do_independent || do_mid_side);
+
+       /*
         * First do a normal encoding pass of each independent channel
         */
-       for(channel = 0; channel < encoder->channels; channel++) {
-               if(!encoder_process_subframe_(encoder, max_partition_order, encoder->force_mid_side_stereo, &frame_header, encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel))
-                       return false;
+       if(do_independent) {
+               for(channel = 0; channel < encoder->channels; channel++) {
+                       if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->bits_per_sample, encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel))
+                               return false;
+               }
        }
 
        /*
         * Now do mid and side channels if requested
         */
-       if(encoder->do_mid_side_stereo && encoder->guts->current_frame_can_do_mid_side) {
+       if(do_mid_side) {
                assert(encoder->channels == 2);
 
                for(channel = 0; channel < 2; channel++) {
-                       if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel))
+                       if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->bits_per_sample+(channel==0? 0:1), encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel))
                                return false;
                }
        }
@@ -657,56 +699,66 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
        /*
         * Compose the frame bitbuffer
         */
-       if(encoder->do_mid_side_stereo && encoder->guts->current_frame_can_do_mid_side) {
-               unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
-               unsigned min_bits;
-               FLAC__ChannelAssignment ca, min_assignment;
-               assert(encoder->channels == 2);
+       if(do_mid_side) {
+               FLAC__ChannelAssignment channel_assignment;
 
-               /* We have to figure out which channel assignent results in the smallest frame */
-               bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits         [1];
-               bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits_mid_side[1];
-               bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits         [1] + encoder->guts->best_subframe_bits_mid_side[1];
-               bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
+               assert(encoder->channels == 2);
 
-               for(min_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
-                       if(bits[ca] < min_bits) {
-                               min_bits = bits[ca];
-                               min_assignment = ca;
+               if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
+                       channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
+               }
+               else {
+                       unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
+                       unsigned min_bits;
+                       FLAC__ChannelAssignment ca;
+
+                       assert(do_independent && do_mid_side);
+
+                       /* We have to figure out which channel assignent results in the smallest frame */
+                       bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits         [1];
+                       bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits_mid_side[1];
+                       bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits         [1] + encoder->guts->best_subframe_bits_mid_side[1];
+                       bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
+
+                       for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
+                               if(bits[ca] < min_bits) {
+                                       min_bits = bits[ca];
+                                       channel_assignment = ca;
+                               }
                        }
                }
 
-               frame_header.channel_assignment = min_assignment;
+               frame_header.channel_assignment = channel_assignment;
 
                if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
                        encoder->state = FLAC__ENCODER_FRAMING_ERROR;
                        return false;
                }
 
-               switch(min_assignment) {
+               switch(channel_assignment) {
                        /* note that encoder_add_subframe_ sets the state for us in case of an error */
                        case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
-                               if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]], &encoder->guts->frame))
+                               if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample  , &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]], &encoder->guts->frame))
                                        return false;
-                               if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]], &encoder->guts->frame))
+                               if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample  , &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]], &encoder->guts->frame))
                                        return false;
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
-                               if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]], &encoder->guts->frame))
+                               if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample  , &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]], &encoder->guts->frame))
                                        return false;
-                               if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
+                               if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
                                        return false;
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
-                               if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
+                               if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
                                        return false;
-                               if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]], &encoder->guts->frame))
+                               if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample  , &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]], &encoder->guts->frame))
                                        return false;
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
-                               if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]], &encoder->guts->frame))
+                               if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample  , &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]], &encoder->guts->frame))
                                        return false;
-                               if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
+                               if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1, &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame))
                                        return false;
                                break;
                        default:
@@ -720,17 +772,25 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
                }
 
                for(channel = 0; channel < encoder->channels; channel++) {
-                       if(!encoder_add_subframe_(encoder, &frame_header, &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) {
+                       if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample, &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) {
                                /* the above function sets the state for us in case of an error */
                                return false;
                        }
                }
        }
 
+       if(encoder->loose_mid_side_stereo) {
+               encoder->guts->loose_mid_side_stereo_frame_count++;
+               if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
+                       encoder->guts->loose_mid_side_stereo_frame_count = 0;
+       }
+
+       encoder->guts->last_channel_assignment = frame_header.channel_assignment;
+
        return true;
 }
 
-bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
+bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
 {
        real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
        real lpc_residual_bits_per_sample;
@@ -746,7 +806,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or
 
        /* verbatim subframe is the baseline against which we measure other compressed subframes */
        _best_subframe = 0;
-       _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, frame_header->bits_per_sample, subframe[_best_subframe]);
+       _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, bits_per_sample, subframe[_best_subframe]);
 
        if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
                /* check for constant subframe */
@@ -761,7 +821,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or
                                }
                        }
                        if(signal_is_constant) {
-                               _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], frame_header->bits_per_sample, subframe[!_best_subframe]);
+                               _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], bits_per_sample, subframe[!_best_subframe]);
                                if(_candidate_bits < _best_bits) {
                                        _best_subframe = !_best_subframe;
                                        _best_bits = _candidate_bits;
@@ -778,13 +838,13 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or
                                min_fixed_order = max_fixed_order = guess_fixed_order;
                        }
                        for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
-                               if(fixed_residual_bits_per_sample[fixed_order] >= (real)frame_header->bits_per_sample)
+                               if(fixed_residual_bits_per_sample[fixed_order] >= (real)bits_per_sample)
                                        continue; /* don't even try */
                                /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */
                                rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+1.5) : 0;
                                if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
                                        rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
-                               _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, frame_header->blocksize, frame_header->bits_per_sample, fixed_order, rice_parameter, max_partition_order, subframe[!_best_subframe]);
+                               _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, frame_header->blocksize, bits_per_sample, fixed_order, rice_parameter, max_partition_order, subframe[!_best_subframe]);
                                if(_candidate_bits < _best_bits) {
                                        _best_subframe = !_best_subframe;
                                        _best_bits = _candidate_bits;
@@ -804,26 +864,26 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or
                                                min_lpc_order = 1;
                                        }
                                        else {
-                                               unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, frame_header->bits_per_sample);
+                                               unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, bits_per_sample);
                                                min_lpc_order = max_lpc_order = guess_lpc_order;
                                        }
                                        if(encoder->do_qlp_coeff_prec_search) {
                                                min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
-                                               max_qlp_coeff_precision = 32 - frame_header->bits_per_sample - 1;
+                                               max_qlp_coeff_precision = 32 - bits_per_sample - 1;
                                        }
                                        else {
                                                min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
                                        }
                                        for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
                                                lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize);
-                                               if(lpc_residual_bits_per_sample >= (real)frame_header->bits_per_sample)
+                                               if(lpc_residual_bits_per_sample >= (real)bits_per_sample)
                                                        continue; /* don't even try */
                                                /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */
                                                rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+1.5) : 0;
                                                if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
                                                        rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
                                                for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
-                                                       _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, lp_coeff[lpc_order-1], frame_header->blocksize, frame_header->bits_per_sample, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, subframe[!_best_subframe]);
+                                                       _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, lp_coeff[lpc_order-1], frame_header->blocksize, bits_per_sample, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, subframe[!_best_subframe]);
                                                        if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
                                                                if(_candidate_bits < _best_bits) {
                                                                        _best_subframe = !_best_subframe;
@@ -843,29 +903,29 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or
        return true;
 }
 
-bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
+bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
 {
        switch(subframe->type) {
                case FLAC__SUBFRAME_TYPE_CONSTANT:
-                       if(!FLAC__subframe_add_constant(&(subframe->data.constant), frame_header->bits_per_sample, frame)) {
+                       if(!FLAC__subframe_add_constant(&(subframe->data.constant), bits_per_sample, frame)) {
                                encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
                                return false;
                        }
                        break;
                case FLAC__SUBFRAME_TYPE_FIXED:
-                       if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, frame_header->bits_per_sample, frame)) {
+                       if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, bits_per_sample, frame)) {
                                encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
                                return false;
                        }
                        break;
                case FLAC__SUBFRAME_TYPE_LPC:
-                       if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, frame_header->bits_per_sample, frame)) {
+                       if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, bits_per_sample, frame)) {
                                encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
                                return false;
                        }
                        break;
                case FLAC__SUBFRAME_TYPE_VERBATIM:
-                       if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, frame_header->bits_per_sample, frame)) {
+                       if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, bits_per_sample, frame)) {
                                encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
                                return false;
                        }
index 611a025..442867e 100644 (file)
@@ -52,11 +52,11 @@ static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
 static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
 static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
 static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel);
-static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel);
-static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order);
-static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order);
-static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel);
+static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
+static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
+static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
+static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
+static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
 static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
 static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
 static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
@@ -602,7 +602,33 @@ bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
        if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize))
                return false;
        for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
-               if(!stream_decoder_read_subframe_(decoder, channel))
+               /*
+                * first figure the correct bits-per-sample of the subframe
+                */
+               unsigned bps = decoder->guts->frame.header.bits_per_sample;
+               switch(decoder->guts->frame.header.channel_assignment) {
+                       case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
+                               /* no adjustment needed */
+                               break;
+                       case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
+                               if(channel == 1)
+                                       bps++;
+                               break;
+                       case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
+                               if(channel == 0)
+                                       bps++;
+                               break;
+                       case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
+                               if(channel == 1)
+                                       bps++;
+                               break;
+                       default:
+                               assert(0);
+               }
+               /*
+                * now read it
+                */
+               if(!stream_decoder_read_subframe_(decoder, channel, bps))
                        return false;
                if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
                        decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
@@ -953,7 +979,7 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
        return true;
 }
 
-bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel)
+bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
 {
        uint32 x;
 
@@ -965,28 +991,28 @@ bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channe
                return true;
        }
        else if(x == 0) {
-               return stream_decoder_read_subframe_constant_(decoder, channel);
+               return stream_decoder_read_subframe_constant_(decoder, channel, bps);
        }
        else if(x == 2) {
-               return stream_decoder_read_subframe_verbatim_(decoder, channel);
+               return stream_decoder_read_subframe_verbatim_(decoder, channel, bps);
        }
        else if(x < 16) {
                decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
                return false;
        }
        else if(x <= 24) {
-               return stream_decoder_read_subframe_fixed_(decoder, channel, (x>>1)&7);
+               return stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7);
        }
        else if(x < 64) {
                decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
                return false;
        }
        else {
-               return stream_decoder_read_subframe_lpc_(decoder, channel, ((x>>1)&31)+1);
+               return stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1);
        }
 }
 
-bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel)
+bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
 {
        FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
        int32 x;
@@ -995,7 +1021,7 @@ bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsign
 
        decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
 
-       if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame.header.bits_per_sample, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
 
        subframe->value = x;
@@ -1007,7 +1033,7 @@ bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsign
        return true;
 }
 
-bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
+bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
 {
        FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
        int32 i32;
@@ -1021,7 +1047,7 @@ bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned
 
        /* read warm-up samples */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame.header.bits_per_sample, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                subframe->warmup[u] = i32;
        }
@@ -1044,7 +1070,7 @@ bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned
        /* read residual */
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, subframe->residual))
+                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
                                return false;
                        break;
                default:
@@ -1058,7 +1084,7 @@ bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned
        return true;
 }
 
-bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
+bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
 {
        FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
        int32 i32;
@@ -1072,7 +1098,7 @@ bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned ch
 
        /* read warm-up samples */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame.header.bits_per_sample, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                subframe->warmup[u] = i32;
        }
@@ -1117,7 +1143,7 @@ bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned ch
        /* read residual */
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, subframe->residual))
+                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
                                return false;
                        break;
                default:
@@ -1131,20 +1157,20 @@ bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned ch
        return true;
 }
 
-bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel)
+bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
 {
        FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
-       int32 x;
+       int32 x, *residual = decoder->guts->residual[channel];
        unsigned i;
 
        decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
 
-       subframe->data = decoder->guts->residual[channel];
+       subframe->data = residual;
 
        for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame.header.bits_per_sample, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               subframe->data[i] = x;
+               residual[i] = x;
        }
 
        /* decode the subframe */