From 309b369d6a4ee068a3d91b8aedc109de0379f82c Mon Sep 17 00:00:00 2001 From: Geolm Date: Mon, 26 Feb 2024 11:34:38 -0500 Subject: [PATCH] switch to uint32_t --- arithmetic_codec.h | 154 +++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 76 deletions(-) diff --git a/arithmetic_codec.h b/arithmetic_codec.h index b170e72..5c9d01f 100644 --- a/arithmetic_codec.h +++ b/arithmetic_codec.h @@ -8,6 +8,8 @@ extern "C" { #endif +#include + struct adaptive_data_model; struct static_data_model; struct arithmetic_codec; @@ -17,7 +19,7 @@ struct arithmetic_codec; //---------------------------------------------------------------------------------------------------------------------- // Initialize the adaptive data model, returns a pointer to the model -struct adaptive_data_model* adaptive_data_model_init(unsigned int number_of_symbols); +struct adaptive_data_model* adaptive_data_model_init(uint32_t number_of_symbols); // Release memory void adaptive_data_model_terminate(struct adaptive_data_model* model); @@ -26,10 +28,10 @@ void adaptive_data_model_terminate(struct adaptive_data_model* model); void adaptive_data_model_reset(struct adaptive_data_model* model); // Change the number of symbols of the model. It will reset the model -void adaptive_data_model_set_alphabet(struct adaptive_data_model* model, unsigned int number_of_symbols); +void adaptive_data_model_set_alphabet(struct adaptive_data_model* model, uint32_t number_of_symbols); // Return how many time the symbol has been encoded -unsigned int adaptive_data_model_get_symbol_count(const struct adaptive_data_model* model, unsigned int symbol); +uint32_t adaptive_data_model_get_symbol_count(const struct adaptive_data_model* model, uint32_t symbol); //---------------------------------------------------------------------------------------------------------------------- // Static data model @@ -39,14 +41,14 @@ unsigned int adaptive_data_model_get_symbol_count(const struct adaptive_data_mod // number_of_symbols Number of symbols maximum // probability Pointer to an array of float containing probability. Array must have the size of number_of_symbols // Each float must be [0;1] -struct static_data_model* static_data_model_init(unsigned int number_of_symbols, const float *probability); +struct static_data_model* static_data_model_init(uint32_t number_of_symbols, const float *probability); // Set up the distribution // number_of_symbols Number of symbols maximum // probability Pointer to an array of float containing probability. Array must have the size of number_of_symbols // Each float must be [0;1] -void static_data_model_set_distribution(struct static_data_model* model, unsigned int number_of_symbols, const float *probability); +void static_data_model_set_distribution(struct static_data_model* model, uint32_t number_of_symbols, const float *probability); // Release memory @@ -64,7 +66,7 @@ struct arithmetic_codec* ac_init(void); // Set the buffer for compressed data // max_code_bytes Maximum size of the buffer in bytes // user_buffer If the pointer to the buffer is NULL, memory will be allocated internally using malloc() -void ac_set_buffer(struct arithmetic_codec* codec, unsigned int max_code_bytes, unsigned char *user_buffer); +void ac_set_buffer(struct arithmetic_codec* codec, uint32_t max_code_bytes, unsigned char *user_buffer); // Set the codec to encoding mode void ac_start_encoder(struct arithmetic_codec* codec); @@ -73,28 +75,28 @@ void ac_start_encoder(struct arithmetic_codec* codec); void ac_start_decoder(struct arithmetic_codec* codec); // Stop encoding, return the number of bytes used in the compressed buffer -unsigned int ac_stop_encoder(struct arithmetic_codec* codec); +uint32_t ac_stop_encoder(struct arithmetic_codec* codec); // Stop the decoder void ac_stop_decoder(struct arithmetic_codec* codec); // Store multiple bits of data in the buffer -void ac_put_bits(struct arithmetic_codec* codec, unsigned int data, unsigned int number_of_bits); +void ac_put_bits(struct arithmetic_codec* codec, uint32_t data, uint32_t number_of_bits); // Get multiple bits of data from the buffer, returns the bits -unsigned int ac_get_bits(struct arithmetic_codec* codec, unsigned int number_of_bits); +uint32_t ac_get_bits(struct arithmetic_codec* codec, uint32_t number_of_bits); // Encode data using an adaptive model, the model should be initialized -void ac_encode_adaptive(struct arithmetic_codec* codec, unsigned int data, struct adaptive_data_model* model); +void ac_encode_adaptive(struct arithmetic_codec* codec, uint32_t data, struct adaptive_data_model* model); // Decode the next data from the buffer using an adaptative model -unsigned int ac_decode_adaptive(struct arithmetic_codec* codec, struct adaptive_data_model* model); +uint32_t ac_decode_adaptive(struct arithmetic_codec* codec, struct adaptive_data_model* model); // Encode data using an static model, the model should be initialized -void ac_encode_static(struct arithmetic_codec* codec, unsigned int data, struct static_data_model* model); +void ac_encode_static(struct arithmetic_codec* codec, uint32_t data, struct static_data_model* model); // Decode the next data from the buffer using an static model -unsigned int ac_decode_static(struct arithmetic_codec* codec, struct static_data_model* model); +uint32_t ac_decode_static(struct arithmetic_codec* codec, struct static_data_model* model); // Return a pointer to the compressed buffer unsigned char* ac_get_buffer(struct arithmetic_codec* codec); @@ -138,15 +140,15 @@ void ac_terminate(struct arithmetic_codec* codec); struct adaptive_data_model { - unsigned int *distribution, *symbol_count, *decoder_table; - unsigned int total_count, update_cycle, symbols_until_update; - unsigned int data_symbols, last_symbol, table_size, table_shift; + uint32_t *distribution, *symbol_count, *decoder_table; + uint32_t total_count, update_cycle, symbols_until_update; + uint32_t data_symbols, last_symbol, table_size, table_shift; }; void adaptive_data_model_update(struct adaptive_data_model* model, int from_encoder); //---------------------------------------------------------------------------------------------------------------------- -struct adaptive_data_model* adaptive_data_model_init(unsigned int number_of_symbols) +struct adaptive_data_model* adaptive_data_model_init(uint32_t number_of_symbols) { struct adaptive_data_model* model = (struct adaptive_data_model*) AC_ALLOC(sizeof(struct adaptive_data_model)); @@ -175,7 +177,7 @@ void adaptive_data_model_reset(struct adaptive_data_model* model) model->total_count = 0; model->update_cycle = model->data_symbols; - for (unsigned int k = 0; k < model->data_symbols; k++) + for (uint32_t k = 0; k < model->data_symbols; k++) model->symbol_count[k] = 1; adaptive_data_model_update(model, 0); @@ -183,7 +185,7 @@ void adaptive_data_model_reset(struct adaptive_data_model* model) } //---------------------------------------------------------------------------------------------------------------------- -void adaptive_data_model_set_alphabet(struct adaptive_data_model* model, unsigned int number_of_symbols) +void adaptive_data_model_set_alphabet(struct adaptive_data_model* model, uint32_t number_of_symbols) { assert(number_of_symbols>1 && (number_of_symbols <= (1 << 11))); // invalid number of data symbols @@ -197,11 +199,11 @@ void adaptive_data_model_set_alphabet(struct adaptive_data_model* model, unsigne // define size of table for fast decoding if (model->data_symbols > 16) { - unsigned int table_bits = 3; + uint32_t table_bits = 3; while (model->data_symbols > (1U << (table_bits + 2))) ++table_bits; model->table_size = 1 << table_bits; model->table_shift = DM__LengthShift - table_bits; - model->distribution = (unsigned int*) AC_ALLOC(sizeof(unsigned int) * (2 * model->data_symbols+model->table_size+2)); + model->distribution = (uint32_t*) AC_ALLOC(sizeof(uint32_t) * (2 * model->data_symbols+model->table_size+2)); model->decoder_table = model->distribution + 2 * model->data_symbols; assert(model->distribution != NULL); } @@ -210,7 +212,7 @@ void adaptive_data_model_set_alphabet(struct adaptive_data_model* model, unsigne // small alphabet: no table needed model->decoder_table = 0; model->table_size = model->table_shift = 0; - model->distribution = (unsigned int*) AC_ALLOC( sizeof(unsigned int) * 2 * model->data_symbols); + model->distribution = (uint32_t*) AC_ALLOC( sizeof(uint32_t) * 2 * model->data_symbols); } model->symbol_count = model->distribution + model->data_symbols; assert(model->distribution != NULL); // cannot assign model memory @@ -226,13 +228,13 @@ void adaptive_data_model_update(struct adaptive_data_model* model, int from_enco if ((model->total_count += model->update_cycle) > DM__MaxCount) { model->total_count = 0; - for (unsigned int n = 0; n < model->data_symbols; n++) + for (uint32_t n = 0; n < model->data_symbols; n++) model->total_count += (model->symbol_count[n] = (model->symbol_count[n] + 1) >> 1); } // compute cumulative distribution, decoder table - unsigned int k, sum = 0, s = 0; - unsigned int scale = 0x80000000U / model->total_count; + uint32_t k, sum = 0, s = 0; + uint32_t scale = 0x80000000U / model->total_count; if (from_encoder || (model->table_size == 0)) { @@ -248,7 +250,7 @@ void adaptive_data_model_update(struct adaptive_data_model* model, int from_enco { model->distribution[k] = (scale * sum) >> (31 - DM__LengthShift); sum += model->symbol_count[k]; - unsigned int w = model->distribution[k] >> model->table_shift; + uint32_t w = model->distribution[k] >> model->table_shift; while (s < w) model->decoder_table[++s] = k - 1; } model->decoder_table[0] = 0; @@ -257,14 +259,14 @@ void adaptive_data_model_update(struct adaptive_data_model* model, int from_enco // set frequency of model updates model->update_cycle = (5 * model->update_cycle) >> 2; - unsigned int max_cycle = (model->data_symbols + 6) << 3; + uint32_t max_cycle = (model->data_symbols + 6) << 3; if (model->update_cycle > max_cycle) model->update_cycle = max_cycle; model->symbols_until_update = model->update_cycle; } //---------------------------------------------------------------------------------------------------------------------- -unsigned int adaptive_data_model_get_symbol_count(const struct adaptive_data_model* model, unsigned int symbol) +uint32_t adaptive_data_model_get_symbol_count(const struct adaptive_data_model* model, uint32_t symbol) { assert(symbol < model->data_symbols); // invalid data symbols assert(model->distribution != NULL); // adaptive model should be initialized @@ -278,12 +280,12 @@ unsigned int adaptive_data_model_get_symbol_count(const struct adaptive_data_mod struct static_data_model { - unsigned int *distribution, *decoder_table; - unsigned int data_symbols, last_symbol, table_size, table_shift; + uint32_t *distribution, *decoder_table; + uint32_t data_symbols, last_symbol, table_size, table_shift; }; //---------------------------------------------------------------------------------------------------------------------- -struct static_data_model* static_data_model_init(unsigned int number_of_symbols, const float *probability) +struct static_data_model* static_data_model_init(uint32_t number_of_symbols, const float *probability) { struct static_data_model* model = (struct static_data_model*) AC_ALLOC(sizeof(struct static_data_model)); @@ -296,7 +298,7 @@ struct static_data_model* static_data_model_init(unsigned int number_of_symbols, } //---------------------------------------------------------------------------------------------------------------------- -void static_data_model_set_distribution(struct static_data_model* model, unsigned int number_of_symbols, const float *probability) +void static_data_model_set_distribution(struct static_data_model* model, uint32_t number_of_symbols, const float *probability) { assert(number_of_symbols>1 && (number_of_symbols <= (1 << 11))); // invalid number of data symbols @@ -310,24 +312,24 @@ void static_data_model_set_distribution(struct static_data_model* model, unsigne // define size of table for fast decoding if (model->data_symbols > 16) { - unsigned int table_bits = 3; + uint32_t table_bits = 3; while (model->data_symbols > (1U << (table_bits + 2))) ++table_bits; model->table_size = 1 << table_bits; model->table_shift = DM__LengthShift - table_bits; - model->distribution = (unsigned int*) AC_ALLOC(sizeof(unsigned int) * (model->data_symbols+model->table_size+2)); + model->distribution = (uint32_t*) AC_ALLOC(sizeof(uint32_t) * (model->data_symbols+model->table_size+2)); model->decoder_table = model->distribution + model->data_symbols; } else { // small alphabet: no table needed model->decoder_table = 0; model->table_size = model->table_shift = 0; - model->distribution = (unsigned int*) AC_ALLOC(sizeof(unsigned int) * model->data_symbols); + model->distribution = (uint32_t*) AC_ALLOC(sizeof(uint32_t) * model->data_symbols); } assert(model->distribution != NULL); } // compute cumulative distribution, decoder table - unsigned int s = 0; + uint32_t s = 0; float sum = 0.0f, p = 1.0f / (float)(model->data_symbols); for (unsigned k = 0; k < model->data_symbols; k++) @@ -337,13 +339,13 @@ void static_data_model_set_distribution(struct static_data_model* model, unsigne assert(p>=0.f && p<= 1.f); - model->distribution[k] = (unsigned int)(sum * (1 << DM__LengthShift)); + model->distribution[k] = (uint32_t)(sum * (1 << DM__LengthShift)); sum += p; if (model->table_size == 0) continue; - unsigned int w = model->distribution[k] >> model->table_shift; + uint32_t w = model->distribution[k] >> model->table_shift; while (s < w) model->decoder_table[++s] = k - 1; } @@ -371,9 +373,9 @@ void static_data_model_terminate(struct static_data_model* model) struct arithmetic_codec { unsigned char *code_buffer, *new_buffer, *ac_pointer; - unsigned int base, value, length; // arithmetic coding state - unsigned int buffer_size; - unsigned int mode; // mode: 0 = undef, 1 = encoder, 2 = decoder + uint32_t base, value, length; // arithmetic coding state + uint32_t buffer_size; + uint32_t mode; // mode: 0 = undef, 1 = encoder, 2 = decoder }; //---------------------------------------------------------------------------------------------------------------------- @@ -401,7 +403,7 @@ inline static void ac_renorm_dec_interval(struct arithmetic_codec* codec) { do // read least-significant byte { - codec->value = (codec->value << 8) | (unsigned int)(*++codec->ac_pointer); + codec->value = (codec->value << 8) | (uint32_t)(*++codec->ac_pointer); } while ((codec->length <<= 8) < AC__MinLength); // length multiplied by 256 } @@ -417,7 +419,7 @@ struct arithmetic_codec* ac_init(void) } //---------------------------------------------------------------------------------------------------------------------- -void ac_set_buffer(struct arithmetic_codec* codec, unsigned int max_code_bytes, unsigned char *user_buffer) +void ac_set_buffer(struct arithmetic_codec* codec, uint32_t max_code_bytes, unsigned char *user_buffer) { assert(codec->mode == 0); // cannot set buffer while encoding or decoding @@ -461,19 +463,19 @@ void ac_start_decoder(struct arithmetic_codec* codec) codec->mode = 2; codec->length = AC__MaxLength; codec->ac_pointer = codec->code_buffer + 3; - codec->value = ((unsigned int)(codec->code_buffer[0]) << 24) | - ((unsigned int)(codec->code_buffer[1]) << 16) | - ((unsigned int)(codec->code_buffer[2]) << 8) | - (unsigned int)(codec->code_buffer[3]); + codec->value = ((uint32_t)(codec->code_buffer[0]) << 24) | + ((uint32_t)(codec->code_buffer[1]) << 16) | + ((uint32_t)(codec->code_buffer[2]) << 8) | + (uint32_t)(codec->code_buffer[3]); } //---------------------------------------------------------------------------------------------------------------------- -unsigned int ac_stop_encoder(struct arithmetic_codec* codec) +uint32_t ac_stop_encoder(struct arithmetic_codec* codec) { assert(codec->mode == 1); // invalid to stop encoder codec->mode = 0; - unsigned int init_base = codec->base; // done encoding: set final data bytes + uint32_t init_base = codec->base; // done encoding: set final data bytes if (codec->length > 2 * AC__MinLength) { @@ -491,7 +493,7 @@ unsigned int ac_stop_encoder(struct arithmetic_codec* codec) ac_renorm_enc_interval(codec); // renormalization = output last bytes - unsigned int code_bytes = (unsigned int)(codec->ac_pointer - codec->code_buffer); + uint32_t code_bytes = (uint32_t)(codec->ac_pointer - codec->code_buffer); assert(code_bytes <= codec->buffer_size); // code buffer overflow return code_bytes; // number of bytes used @@ -505,14 +507,14 @@ void ac_stop_decoder(struct arithmetic_codec* codec) } //---------------------------------------------------------------------------------------------------------------------- -void ac_put_bit(struct arithmetic_codec* codec, unsigned int bit) +void ac_put_bit(struct arithmetic_codec* codec, uint32_t bit) { assert(codec->mode == 1); // encoder not initialized codec->length >>= 1; // halve interval if (bit) { - unsigned int init_base = codec->base; + uint32_t init_base = codec->base; codec->base += codec->length; // move base if (init_base > codec->base) ac_propagate_carry(codec); // overflow = carry @@ -523,12 +525,12 @@ void ac_put_bit(struct arithmetic_codec* codec, unsigned int bit) } //---------------------------------------------------------------------------------------------------------------------- -unsigned int ac_get_bit(struct arithmetic_codec* codec) +uint32_t ac_get_bit(struct arithmetic_codec* codec) { assert(codec->mode == 2); // decoder not initialized codec->length >>= 1; // halve interval - unsigned int bit = (codec->value >= codec->length); // decode bit + uint32_t bit = (codec->value >= codec->length); // decode bit if (bit) codec->value -= codec->length; // move base @@ -539,13 +541,13 @@ unsigned int ac_get_bit(struct arithmetic_codec* codec) } //---------------------------------------------------------------------------------------------------------------------- -void ac_put_bits(struct arithmetic_codec* codec, unsigned int data, unsigned int number_of_bits) +void ac_put_bits(struct arithmetic_codec* codec, uint32_t data, uint32_t number_of_bits) { assert(codec->mode == 1); // encoder not initialized assert((number_of_bits > 0) && (number_of_bits < 21)); // invalid number of bits assert(data < (1U << number_of_bits)); // invalid data - unsigned int init_base = codec->base; + uint32_t init_base = codec->base; codec->base += data * (codec->length >>= number_of_bits); // new interval base and length if (init_base > codec->base) @@ -556,7 +558,7 @@ void ac_put_bits(struct arithmetic_codec* codec, unsigned int data, unsigned int } //---------------------------------------------------------------------------------------------------------------------- -unsigned int ac_get_bits(struct arithmetic_codec* codec, unsigned int number_of_bits) +uint32_t ac_get_bits(struct arithmetic_codec* codec, uint32_t number_of_bits) { assert(codec->mode == 2); // decoder not initialized assert((number_of_bits > 0) && (number_of_bits < 21)); // invalid number of bits @@ -572,14 +574,14 @@ unsigned int ac_get_bits(struct arithmetic_codec* codec, unsigned int number_of_ } //---------------------------------------------------------------------------------------------------------------------- -void ac_encode_adaptive(struct arithmetic_codec* codec, unsigned int data, struct adaptive_data_model* model) +void ac_encode_adaptive(struct arithmetic_codec* codec, uint32_t data, struct adaptive_data_model* model) { assert(codec->mode == 1); // encoder not initialized assert(data < model->data_symbols); // invalid data symbols assert(model->distribution != NULL); // adaptive model should be initialized - unsigned int x; - unsigned int init_base = codec->base; + uint32_t x; + uint32_t init_base = codec->base; // compute products if (data == model->last_symbol) @@ -609,25 +611,25 @@ void ac_encode_adaptive(struct arithmetic_codec* codec, unsigned int data, struc } //---------------------------------------------------------------------------------------------------------------------- -unsigned int ac_decode_adaptive(struct arithmetic_codec* codec, struct adaptive_data_model* model) +uint32_t ac_decode_adaptive(struct arithmetic_codec* codec, struct adaptive_data_model* model) { assert(codec->mode == 2); // decoder not initialized assert(model->distribution != NULL); // adaptive model should be initialized - unsigned int n, s, x, y = codec->length; + uint32_t n, s, x, y = codec->length; if (model->decoder_table) { // use table look-up for faster decoding - unsigned int dv = codec->value / (codec->length >>= DM__LengthShift); - unsigned int t = dv >> model->table_shift; + uint32_t dv = codec->value / (codec->length >>= DM__LengthShift); + uint32_t t = dv >> model->table_shift; s = model->decoder_table[t]; // initial decision based on table look-up n = model->decoder_table[t+1] + 1; while (n > s + 1) { // finish with bisection search - unsigned int m = (s + n) >> 1; + uint32_t m = (s + n) >> 1; if (model->distribution[m] > dv) n = m; else s = m; @@ -643,12 +645,12 @@ unsigned int ac_decode_adaptive(struct arithmetic_codec* codec, struct adaptive_ // decode using only multiplications x = s = 0; codec->length >>= DM__LengthShift; - unsigned int m = (n = model->data_symbols) >> 1; + uint32_t m = (n = model->data_symbols) >> 1; // decode via bisection search do { - unsigned int z = codec->length * model->distribution[m]; + uint32_t z = codec->length * model->distribution[m]; if (z > codec->value) { n = m; @@ -676,12 +678,12 @@ unsigned int ac_decode_adaptive(struct arithmetic_codec* codec, struct adaptive_ } //---------------------------------------------------------------------------------------------------------------------- -void ac_encode_static(struct arithmetic_codec* codec, unsigned int data, struct static_data_model* model) +void ac_encode_static(struct arithmetic_codec* codec, uint32_t data, struct static_data_model* model) { assert(codec->mode == 1); // encoder not initialized assert(data < model->data_symbols); // invalid data symbol - unsigned int x, init_base = codec->base; + uint32_t x, init_base = codec->base; // compute products if (data == model->last_symbol) @@ -705,17 +707,17 @@ void ac_encode_static(struct arithmetic_codec* codec, unsigned int data, struct } //---------------------------------------------------------------------------------------------------------------------- -unsigned int ac_decode_static(struct arithmetic_codec* codec, struct static_data_model* model) +uint32_t ac_decode_static(struct arithmetic_codec* codec, struct static_data_model* model) { assert(codec->mode == 2); // decoder not initialized - unsigned int n, s, x, y = codec->length; + uint32_t n, s, x, y = codec->length; if (model->decoder_table) { // use table look-up for faster decoding - unsigned int dv = codec->value / (codec->length >>= DM__LengthShift); - unsigned int t = dv >> model->table_shift; + uint32_t dv = codec->value / (codec->length >>= DM__LengthShift); + uint32_t t = dv >> model->table_shift; // initial decision based on table look-up s = model->decoder_table[t]; @@ -724,7 +726,7 @@ unsigned int ac_decode_static(struct arithmetic_codec* codec, struct static_data while (n > s + 1) { // finish with bisection search - unsigned int m = (s + n) >> 1; + uint32_t m = (s + n) >> 1; if (model->distribution[m] > dv) n = m; else @@ -740,12 +742,12 @@ unsigned int ac_decode_static(struct arithmetic_codec* codec, struct static_data // decode using only multiplications x = s = 0; codec->length >>= DM__LengthShift; - unsigned int m = (n = model->data_symbols) >> 1; + uint32_t m = (n = model->data_symbols) >> 1; // decode via bisection search do { - unsigned int z = codec->length * model->distribution[m]; + uint32_t z = codec->length * model->distribution[m]; if (z > codec->value) { n = m;