rework so that rice parameters and raw_bits from the entropy coding method struct...
authorJosh Coalson <jcoalson@users.sourceforce.net>
Sat, 17 Aug 2002 15:23:43 +0000 (15:23 +0000)
committerJosh Coalson <jcoalson@users.sourceforce.net>
Sat, 17 Aug 2002 15:23:43 +0000 (15:23 +0000)
include/FLAC/format.h
src/libFLAC/stream_decoder.c
src/libFLAC/stream_encoder.c

index 5f971dc..ae3df6c 100644 (file)
@@ -182,14 +182,19 @@ extern const char * const FLAC__EntropyCodingMethodTypeString[];
 typedef struct {
 
        unsigned order;
-       /**< The partition order, i.e. # of contexts = 2 ^ order. */
+       /**< The partition order, i.e. # of contexts = 2 ^ \a order. */
 
-       unsigned parameters[1 << FLAC__MAX_RICE_PARTITION_ORDER];
+       unsigned *parameters;
        /**< The Rice parameters for each context. */
 
-       unsigned raw_bits[1 << FLAC__MAX_RICE_PARTITION_ORDER];
+       unsigned *raw_bits;
        /**< Widths for escape-coded partitions. */
 
+       unsigned capacity_by_order;
+       /**< The capacity of the \a parameters and \a raw_bits arrays
+        * specified as an order, i.e. the number of array elements
+        * allocated is 2 ^ \a capacity_by_order.
+        */
 } FLAC__EntropyCodingMethod_PartitionedRice;
 
 extern const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; /**< == 4 (bits) */
index a7a9154..051405a 100644 (file)
 #include "private/cpu.h"
 #include "private/crc.h"
 #include "private/fixed.h"
+#include "private/format.h"
 #include "private/lpc.h"
 
+#ifdef max
+#undef max
+#endif
+#define max(a,b) ((a)>(b)?(a):(b))
+
 /***********************************************************************
  *
  * Private static data
@@ -87,6 +93,7 @@ typedef struct FLAC__StreamDecoderPrivate {
        FLAC__byte *metadata_filter_ids;
        unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
        FLAC__Frame frame;
+       FLAC__EntropyCodingMethod_PartitionedRice allocated_ecm_pr;
        FLAC__bool cached; /* true if there is a byte in lookahead */
        FLAC__CPUInfo cpuinfo;
        FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
@@ -188,6 +195,7 @@ FLAC__StreamDecoder *FLAC__stream_decoder_new()
        decoder->private_->output_capacity = 0;
        decoder->private_->output_channels = 0;
        decoder->private_->has_seek_table = false;
+       FLAC__format_entropy_coding_method_partitioned_rice_init(&decoder->private_->allocated_ecm_pr);
 
        set_defaults_(decoder);
 
@@ -209,6 +217,9 @@ void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
                free(decoder->private_->metadata_filter_ids);
 
        FLAC__bitbuffer_delete(decoder->private_->input);
+
+       FLAC__format_entropy_coding_method_partitioned_rice_clear(&decoder->private_->allocated_ecm_pr);
+
        free(decoder->private_);
        free(decoder->protected_);
        free(decoder);
@@ -284,7 +295,12 @@ void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
        }
        FLAC__bitbuffer_free(decoder->private_->input);
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
-               /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
+               /* WATCHOUT:
+                * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
+                * output arrays have a buffer of up to 3 zeroes in front
+                * (at negative indices) for alignment purposes; we use 4
+                * to keep the data well-aligned.
+                */
                if(0 != decoder->private_->output[i]) {
                        free(decoder->private_->output[i]-4);
                        decoder->private_->output[i] = 0;
@@ -674,7 +690,7 @@ FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigne
        if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
                return true;
 
-       /* @@@ should change to use realloc() */
+       /* @@@@ should change to use realloc() */
 
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
                if(0 != decoder->private_->output[i]) {
@@ -688,7 +704,12 @@ FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigne
        }
 
        for(i = 0; i < channels; i++) {
-               /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
+               /* WATCHOUT:
+                * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
+                * output arrays have a buffer of up to 3 zeroes in front
+                * (at negative indices) for alignment purposes; we use 4
+                * to keep the data well-aligned.
+                */
                tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
                if(tmp == 0) {
                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
@@ -1750,6 +1771,13 @@ FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigne
        const unsigned partitions = 1u << partition_order;
        const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
 
+       if(!FLAC__format_entropy_coding_method_partitioned_rice_ensure_size(&decoder->private_->allocated_ecm_pr, max(6, partition_order))) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return false;
+       }
+       partitioned_rice->parameters = decoder->private_->allocated_ecm_pr.parameters;
+       partitioned_rice->raw_bits = decoder->private_->allocated_ecm_pr.raw_bits;
+
        sample = 0;
        for(partition = 0; partition < partitions; partition++) {
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
index 09dcbc5..48374b9 100644 (file)
 #include "private/bitmath.h"
 #include "private/crc.h"
 #include "private/cpu.h"
-#include "private/stream_encoder_framing.h"
 #include "private/fixed.h"
+#include "private/format.h"
 #include "private/lpc.h"
 #include "private/md5.h"
 #include "private/memory.h"
+#include "private/stream_encoder_framing.h"
 
 #ifdef min
 #undef min
@@ -73,21 +74,21 @@ static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_siz
 static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
 static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
 static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
-static FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
+static FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
 static FLAC__bool add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
 static unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
-static unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
-static unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
+static unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice);
+static unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice);
 static unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
-static unsigned find_best_partition_order_(struct FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
+static unsigned find_best_partition_order_(struct FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice);
 static void precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
 static void precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
 #ifdef DONT_ESTIMATE_RICE_BITS
-static FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits);
-static FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
+static FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits);
+static FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits);
 #else
-static FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits);
-static FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
+static FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits);
+static FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits);
 #endif
 static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
 /* verify-related routines: */
@@ -118,6 +119,10 @@ typedef struct FLAC__StreamEncoderPrivate {
        FLAC__Subframe subframe_workspace_mid_side[2][2];
        FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
        FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
+       FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice_workspace[FLAC__MAX_CHANNELS][2];
+       FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice_workspace_mid_side[FLAC__MAX_CHANNELS][2];
+       FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice_workspace_ptr[FLAC__MAX_CHANNELS][2];
+       FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
        unsigned best_subframe[FLAC__MAX_CHANNELS];       /* index into the above workspaces */
        unsigned best_subframe_mid_side[2];
        unsigned best_subframe_bits[FLAC__MAX_CHANNELS];  /* size in bits of the best subframe for each channel */
@@ -161,7 +166,7 @@ typedef struct FLAC__StreamEncoderPrivate {
         * declarations merely to save stack space during encoding.
         */
        FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
-       unsigned parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; /* from find_best_partition_order_() */
+       FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice_extra[2]; /* from find_best_partition_order_() */
        /*
         * The data for the verify section
         */
@@ -226,6 +231,7 @@ const char * const FLAC__StreamEncoderWriteStatusString[] = {
 FLAC__StreamEncoder *FLAC__stream_encoder_new()
 {
        FLAC__StreamEncoder *encoder;
+       unsigned i;
 
        FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
 
@@ -261,6 +267,35 @@ FLAC__StreamEncoder *FLAC__stream_encoder_new()
        set_defaults_(encoder);
 
        encoder->private_->is_being_deleted = false;
+
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
+               encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
+               encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
+       }
+       for(i = 0; i < 2; i++) {
+               encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
+               encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
+       }
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
+               encoder->private_->partitioned_rice_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_workspace[i][0];
+               encoder->private_->partitioned_rice_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_workspace[i][1];
+       }
+       for(i = 0; i < 2; i++) {
+               encoder->private_->partitioned_rice_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_workspace_mid_side[i][0];
+               encoder->private_->partitioned_rice_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_workspace_mid_side[i][1];
+       }
+
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
+               FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_workspace[i][0]);
+               FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_workspace[i][1]);
+       }
+       for(i = 0; i < 2; i++) {
+               FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_workspace_mid_side[i][0]);
+               FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_workspace_mid_side[i][1]);
+       }
+       for(i = 0; i < 2; i++)
+               FLAC__format_entropy_coding_method_partitioned_rice_init(&encoder->private_->partitioned_rice_extra[i]);
+
        encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
 
        return encoder;
@@ -268,6 +303,8 @@ FLAC__StreamEncoder *FLAC__stream_encoder_new()
 
 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
 {
+       unsigned i;
+
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->protected_);
        FLAC__ASSERT(0 != encoder->private_);
@@ -279,6 +316,18 @@ void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
 
        if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
                FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
+
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
+               FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_workspace[i][0]);
+               FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_workspace[i][1]);
+       }
+       for(i = 0; i < 2; i++) {
+               FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_workspace_mid_side[i][0]);
+               FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_workspace_mid_side[i][1]);
+       }
+       for(i = 0; i < 2; i++)
+               FLAC__format_entropy_coding_method_partitioned_rice_clear(&encoder->private_->partitioned_rice_extra[i]);
+
        FLAC__bitbuffer_delete(encoder->private_->frame);
        free(encoder->private_);
        free(encoder->protected_);
@@ -402,14 +451,6 @@ FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
                encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
                encoder->private_->best_subframe_mid_side[i] = 0;
        }
-       for(i = 0; i < encoder->protected_->channels; i++) {
-               encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
-               encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
-       }
-       for(i = 0; i < 2; i++) {
-               encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
-               encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
-       }
        encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
        encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
        encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
@@ -1335,12 +1376,8 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_f
                max_partition_order = 0;
        }
        else {
-               unsigned limit = 0, b = encoder->protected_->blocksize;
-               while(!(b & 1)) {
-                       limit++;
-                       b >>= 1;
-               }
-               max_partition_order = min(encoder->protected_->max_residual_partition_order, limit);
+               max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
+               max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
        }
        min_partition_order = min(min_partition_order, max_partition_order);
 
@@ -1391,18 +1428,16 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_f
         * Check for wasted bits; set effective bps for each subframe
         */
        if(do_independent) {
-               unsigned w;
                for(channel = 0; channel < encoder->protected_->channels; channel++) {
-                       w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
+                       const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
                        encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
                        encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
                }
        }
        if(do_mid_side) {
-               unsigned w;
                FLAC__ASSERT(encoder->protected_->channels == 2);
                for(channel = 0; channel < 2; channel++) {
-                       w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
+                       const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
                        encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
                        encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
                }
@@ -1413,7 +1448,7 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_f
         */
        if(do_independent) {
                for(channel = 0; channel < encoder->protected_->channels; channel++) {
-                       if(!process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps[channel], encoder->private_->integer_signal[channel], encoder->private_->real_signal[channel], encoder->private_->subframe_workspace_ptr[channel], encoder->private_->residual_workspace[channel], encoder->private_->best_subframe+channel, encoder->private_->best_subframe_bits+channel))
+                       if(!process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps[channel], encoder->private_->integer_signal[channel], encoder->private_->real_signal[channel], encoder->private_->subframe_workspace_ptr[channel], encoder->private_->partitioned_rice_workspace_ptr[channel], encoder->private_->residual_workspace[channel], encoder->private_->best_subframe+channel, encoder->private_->best_subframe_bits+channel))
                                return false;
                }
        }
@@ -1425,7 +1460,7 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_f
                FLAC__ASSERT(encoder->protected_->channels == 2);
 
                for(channel = 0; channel < 2; channel++) {
-                       if(!process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps_mid_side[channel], encoder->private_->integer_signal_mid_side[channel], encoder->private_->real_signal_mid_side[channel], encoder->private_->subframe_workspace_ptr_mid_side[channel], encoder->private_->residual_workspace_mid_side[channel], encoder->private_->best_subframe_mid_side+channel, encoder->private_->best_subframe_bits_mid_side+channel))
+                       if(!process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps_mid_side[channel], encoder->private_->integer_signal_mid_side[channel], encoder->private_->real_signal_mid_side[channel], encoder->private_->subframe_workspace_ptr_mid_side[channel], encoder->private_->partitioned_rice_workspace_ptr_mid_side[channel], encoder->private_->residual_workspace_mid_side[channel], encoder->private_->best_subframe_mid_side+channel, encoder->private_->best_subframe_bits_mid_side+channel))
                                return false;
                }
        }
@@ -1544,7 +1579,7 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_f
        return true;
 }
 
-FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
+FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
 {
        FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
        FLAC__real lpc_residual_bits_per_sample;
@@ -1603,7 +1638,7 @@ FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partitio
 #endif
                                        rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
                                }
-                               _candidate_bits = evaluate_fixed_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe]);
+                               _candidate_bits = evaluate_fixed_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe], partitioned_rice[!_best_subframe]);
                                if(_candidate_bits < _best_bits) {
                                        _best_subframe = !_best_subframe;
                                        _best_bits = _candidate_bits;
@@ -1650,7 +1685,7 @@ FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partitio
                                                                rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
                                                        }
                                                        for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
-                                                               _candidate_bits = evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, encoder->private_->lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe]);
+                                                               _candidate_bits = evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, encoder->private_->lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe], partitioned_rice[!_best_subframe]);
                                                                if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
                                                                        if(_candidate_bits < _best_bits) {
                                                                                _best_subframe = !_best_subframe;
@@ -1713,7 +1748,7 @@ unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
 }
 
-unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
+unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice)
 {
        unsigned i, residual_bits;
        const unsigned residual_samples = blocksize - order;
@@ -1725,7 +1760,9 @@ unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int3
        subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
        subframe->data.fixed.residual = residual;
 
-       residual_bits = find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits);
+       residual_bits = find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, partitioned_rice);
+
+       subframe->data.fixed.entropy_coding_method.data.partitioned_rice = *partitioned_rice;
 
        subframe->data.fixed.order = order;
        for(i = 0; i < order; i++)
@@ -1734,7 +1771,7 @@ unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int3
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
 }
 
-unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
+unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice)
 {
        FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
        unsigned i, residual_bits;
@@ -1755,7 +1792,9 @@ unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32
        subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
        subframe->data.lpc.residual = residual;
 
-       residual_bits = find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits);
+       residual_bits = find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, partitioned_rice);
+
+       subframe->data.fixed.entropy_coding_method.data.partitioned_rice = *partitioned_rice;
 
        subframe->data.lpc.order = order;
        subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
@@ -1776,7 +1815,7 @@ unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocks
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
 }
 
-unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
+unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice)
 {
        FLAC__int32 r;
        unsigned residual_bits, best_residual_bits = 0;
@@ -1790,9 +1829,7 @@ unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const
                abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
        }
 
-       while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order)
-               max_partition_order--;
-       FLAC__ASSERT(blocksize >> max_partition_order > predictor_order);
+       max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
        min_partition_order = min(min_partition_order, max_partition_order);
 
        if(precompute_partition_sums) {
@@ -1806,9 +1843,9 @@ unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const
 
                for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
 #ifdef DONT_ESTIMATE_RICE_BITS
-                       if(!set_partitioned_rice_with_precompute_(residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, private_->parameters[!best_parameters_index], private_->raw_bits[!best_parameters_index], &residual_bits))
+                       if(!set_partitioned_rice_with_precompute_(residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, &private_->partitioned_rice_extra[!best_parameters_index], &residual_bits))
 #else
-                       if(!set_partitioned_rice_with_precompute_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, private_->parameters[!best_parameters_index], private_->raw_bits[!best_parameters_index], &residual_bits))
+                       if(!set_partitioned_rice_with_precompute_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, &private_->partitioned_rice_extra[!best_parameters_index], &residual_bits))
 #endif
                        {
                                FLAC__ASSERT(best_residual_bits != 0);
@@ -1817,8 +1854,8 @@ unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const
                        sum += 1u << partition_order;
                        if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
                                best_residual_bits = residual_bits;
-                               *best_partition_order = partition_order;
                                best_parameters_index = !best_parameters_index;
+                               best_partitioned_rice->order = partition_order;
                        }
                }
        }
@@ -1826,9 +1863,9 @@ unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const
                unsigned partition_order;
                for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
 #ifdef DONT_ESTIMATE_RICE_BITS
-                       if(!set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits))
+                       if(!set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, &private_->partitioned_rice_extra[!best_parameters_index], &residual_bits))
 #else
-                       if(!set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits))
+                       if(!set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, &private_->partitioned_rice_extra[!best_parameters_index], &residual_bits))
 #endif
                        {
                                FLAC__ASSERT(best_residual_bits != 0);
@@ -1836,14 +1873,15 @@ unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const
                        }
                        if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
                                best_residual_bits = residual_bits;
-                               *best_partition_order = partition_order;
                                best_parameters_index = !best_parameters_index;
+                               best_partitioned_rice->order = partition_order;
                        }
                }
        }
 
-       memcpy(best_parameters, private_->parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
-       memcpy(best_raw_bits, private_->raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
+       FLAC__format_entropy_coding_method_partitioned_rice_ensure_size(best_partitioned_rice, max(6, best_partitioned_rice->order));
+       memcpy(best_partitioned_rice->parameters, private_->partitioned_rice_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
+       memcpy(best_partitioned_rice->raw_bits, private_->partitioned_rice_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
 
        return best_residual_bits;
 }
@@ -1955,9 +1993,9 @@ void precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned r
 #endif
 
 #ifdef DONT_ESTIMATE_RICE_BITS
-FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits)
+FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits)
 #else
-FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits)
+FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits)
 #endif
 {
        unsigned rice_parameter, partition_bits;
@@ -1966,9 +2004,14 @@ FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsign
        unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
 #endif
        unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
+       unsigned *parameters;
 
        FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
 
+       partitioned_rice->order = partition_order;
+       FLAC__format_entropy_coding_method_partitioned_rice_ensure_size(partitioned_rice, max(6, partition_order));
+       parameters = partitioned_rice->parameters;
+
        if(partition_order == 0) {
                unsigned i;
 
@@ -2127,9 +2170,9 @@ FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsign
 }
 
 #ifdef DONT_ESTIMATE_RICE_BITS
-FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
+FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits)
 #else
-FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
+FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, unsigned *bits)
 #endif
 {
        unsigned rice_parameter, partition_bits;
@@ -2139,9 +2182,15 @@ FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual
 #endif
        unsigned flat_bits;
        unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
+       unsigned *parameters, *raw_bits;
 
        FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
 
+       partitioned_rice->order = partition_order;
+       FLAC__format_entropy_coding_method_partitioned_rice_ensure_size(partitioned_rice, max(6, partition_order));
+       parameters = partitioned_rice->parameters;
+       raw_bits = partitioned_rice->raw_bits;
+
        if(partition_order == 0) {
                unsigned i;