From db947e46755d8262710e734ccb2fb51ec725c844 Mon Sep 17 00:00:00 2001 From: Fredrik Svedberg Date: Tue, 22 Nov 2022 15:47:24 +0100 Subject: MLBEDSW-7002 Investigate output from code linter Investigated all code linter output and fixed non-intentional reports. Change-Id: If49d6df8fe1a8a6ae4f1e28de4889a8c5763a0b3 Signed-off-by: Fredrik Svedberg --- ethosu/mlw_codec/mlw_encode.c | 265 ++++++++++++++++++++++++------------------ 1 file changed, 150 insertions(+), 115 deletions(-) (limited to 'ethosu/mlw_codec/mlw_encode.c') diff --git a/ethosu/mlw_codec/mlw_encode.c b/ethosu/mlw_codec/mlw_encode.c index 7043746d..e8e1a8ca 100644 --- a/ethosu/mlw_codec/mlw_encode.c +++ b/ethosu/mlw_codec/mlw_encode.c @@ -40,6 +40,8 @@ #define max(a,b) ((a)>(b)?(a):(b)) #endif +#define CHECKED_MALLOC(var, size) { if ( !(var = malloc(size)) ) break; } + typedef struct palette { int16_t lut[32]; int16_t inv_lut[512]; @@ -55,6 +57,16 @@ static int is_power_of_two( int x ) { return ((x-1) & x)==0; } +static int round_up_divide(int num, int den) +{ + return (num + den - 1) / den; +} + +static int round_up(int num, int den) +{ + return round_up_divide(num, den) * den; +} + static int get_palette_index_bits( int size ) { int i; for(i=7; i>=0; i--) @@ -69,10 +81,14 @@ static int search_palette_sections( int16_t *buf, int size, int **palette_restar int i,j,got_palette,restart_i,palette_size=0, last_restart_idx, zero_cnt; int prev_idx[512]; // For each value, keep track of the index of the previous occurence int *restart_pos; - int max_palettes = (size+63)/64; + int max_palettes = round_up_divide(size, 64); + *palette_restart_positions = NULL; // Preliminary allocation of sufficient size restart_pos = (int*)malloc( max_palettes*sizeof(int) ); + if (!restart_pos) { + return 0; + } last_restart_idx=0; got_palette=0; restart_i=1; @@ -96,6 +112,9 @@ static int search_palette_sections( int16_t *buf, int size, int **palette_restar if (restart_i == max_palettes) { max_palettes = max_palettes*2; restart_pos = (int*)realloc( restart_pos, max_palettes*sizeof(int) ); + if (!restart_pos) { + return 0; + } } DPRINTF("restart %d pos %d\n", restart_i, i); restart_pos[restart_i++] = i; @@ -164,6 +183,9 @@ static int search_palette_sections( int16_t *buf, int size, int **palette_restar if (restart_i == max_palettes) { max_palettes = max_palettes*2; restart_pos = (int*)realloc( restart_pos, max_palettes*sizeof(int) ); + if (!restart_pos) { + return 0; + } } restart_pos[restart_i++] = last_restart_idx; } @@ -177,7 +199,7 @@ static int search_palette_sections( int16_t *buf, int size, int **palette_restar } // Reallocate to actual size *palette_restart_positions = (int*)realloc( restart_pos, restart_i*sizeof(int) ); - return restart_i; + return *palette_restart_positions ? restart_i : 0; } // Calculate frequency table @@ -190,8 +212,8 @@ static void calc_freq( const int16_t *buf, int size, int freq[512] ) { } static int cmp_uint64(const void * a, const void * b) { - uint64_t aa = *(uint64_t*)a; - uint64_t bb = *(uint64_t*)b; + uint64_t aa = *(const uint64_t*)a; + uint64_t bb = *(const uint64_t*)b; return aa>bb ? -1 : aa0) { - all_max_val = max(all_max_val, palval); + all_max_val = max(all_max_val, palval); } } @@ -260,7 +282,8 @@ static void create_palette( int freq[512], } // Setup the 32 entry palette - int palette_max_val = 0, val, cnt, pal_cnt=0; + int16_t palette_max_val = 0, val; + int cnt, pal_cnt=0; for(i=0; i>16); val = freq64[i]&0xffff; @@ -341,17 +364,19 @@ static void create_inverse_palette( palette_t *p) { int sign = val&1; int mag = val>>1; int weight = sign ? -mag : mag; - if (weight+256 < 512) - p->inv_lut[ weight+256 ] = i + p->palsize - p->direct_offset; + int index = weight+256; + if (index >= 0 && index < 512) + p->inv_lut[ index ] = i + p->palsize - p->direct_offset; } for(i=0; ipalsize; i++) { int val = p->lut[i]; int sign = val&1; int mag = val>>1; int weight = sign ? -mag : mag; - assert(weight+256 >= 0 && weight+256 < 512); - if (weight+256 < 512) - p->inv_lut[ weight+256 ] = i; + int index = weight+256; + assert(index >= 0 && index < 512); + if (index >= 0 && index < 512) + p->inv_lut[ index ] = i; } } @@ -366,8 +391,8 @@ typedef struct search_state { } search_state_t; // (trunc<<4) | div, 0x20 means uncompressed -static const char w_grc_params[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x20 }; -static const char z_grc_params[] = { 0x00, 0x01, 0x02, 0x03, 0x04 }; +static const uint8_t w_grc_params[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x20 }; +static const uint8_t z_grc_params[] = { 0x00, 0x01, 0x02, 0x03, 0x04 }; @@ -387,7 +412,7 @@ static int search_grc_params( const int *inval_buf, int *bitcnt ) { int n_cfg = zrun_mode ? NZCFG : NWCFG; - const char *grc_params = zrun_mode ? z_grc_params : w_grc_params; + const uint8_t *grc_params = zrun_mode ? z_grc_params : w_grc_params; int i,j; search_state_t *state[MAX_ZWCFG]; @@ -504,12 +529,12 @@ static void bitbuf_init( bitbuf_t *bb, uint8_t *buf, int size, int log_symbols ) bb->log_symbols = log_symbols; } -static void bitbuf_putbit( bitbuf_t *bb, int bit) { +static void bitbuf_putbit( bitbuf_t *bb, uint8_t bit) { int byte_pos = bb->pos>>3; - int bit_pos = bb->pos&7; + uint8_t bit_pos = bb->pos&7; assert( byte_pos >= 0 ); assert( byte_pos < bb->buf_size ); - bb->buf[ byte_pos ] = (bb->buf[ byte_pos ] & ~(1<buf[ byte_pos ] = ((bb->buf[ byte_pos ] & ~(1U<pos += 1; } @@ -519,7 +544,7 @@ static void bitbuf_put( bitbuf_t *bb, const char *name, int len, int data) { if (bb->log_symbols) printf("bitbuf: pos %3d %7s len %d data %x\n", bb->pos, name, len, data); for(i=0; i>i)&1); + bitbuf_putbit(bb, (uint8_t)((data>>i)&1)); } } } @@ -544,6 +569,8 @@ static int encode_slice( const int *w_value, bb->pos = bitpos; assert(nvalues<32768); + if (w_cfg < 0 || z_cfg < 0) + return bitpos; // GRC parameters for this slice int w_grc_div = w_grc_params[w_cfg] & 15; int w_grc_trunc = (w_grc_params[w_cfg] >> 4)==1; @@ -690,7 +717,6 @@ static int encode_slice( const int *w_value, return bb->pos; } - // return new bitpos static int encode_section( const int16_t *inbuf, int size, @@ -717,92 +743,96 @@ static int encode_section( const int16_t *inbuf, uncompressed_bits = 100; } - int *weight_values = malloc( size*sizeof(int) ); - int *zrun_values = malloc( size*sizeof(int) ); - - // Get weights (or weight indicies) AND zero-runs from the input weight stream. - int i=0, n_weights = 0, zcnt; - while(1) { - if (p->use_zero_runs) { - zcnt=0; - // Count zero run - // Special case: if all weights in the section are zero, we must - // still ensure we have one coded weight so the the slice length - // doesn't become 0. Therefore we skip the first zero run and code - // the zero explicitly as a weight value instead - if (!p->only_zeros || i>0) { - while( iuse_zero_runs) { + zcnt=0; + // Count zero run + // Special case: if all weights in the section are zero, we must + // still ensure we have one coded weight so the the slice length + // doesn't become 0. Therefore we skip the first zero run and code + // the zero explicitly as a weight value instead + if (!p->only_zeros || i>0) { + while( iinv_lut[inbuf[i]+256]; + weight_values[n_weights] = value; + n_weights++; + i++; } - if (i==size) - break; - int value = p->inv_lut[inbuf[i]+256]; - weight_values[n_weights] = value; - n_weights++; - i++; - } - - // Search for good GRC parameters for the weight stream - int n_w_slice, w_bitcnt; - uint8_t *w_slice_cfg; - int *w_slice_pos; - w_slice_cfg = malloc( size ); - w_slice_pos = malloc( size*sizeof(int) ); - n_w_slice = search_grc_params( weight_values, n_weights, 0, uncompressed_bits, w_slice_cfg, w_slice_pos, size, 0, 0, &w_bitcnt); - if (n_weights==0) - n_w_slice = 0; - - // Search for good GRC parameters for the zrun stream - int n_z_slice=0, z_bitcnt=0; - uint8_t *z_slice_cfg=0; - int *z_slice_pos=0; - if (p->use_zero_runs) { - z_slice_cfg = malloc( size ); - z_slice_pos = malloc( size*sizeof(int) ); - n_z_slice = search_grc_params( zrun_values, n_weights+1, 1, 0, z_slice_cfg, z_slice_pos, size, w_slice_pos, n_w_slice, &z_bitcnt); - } - // Encode bitstream slice - int pos=0, i_w_slice=0, i_z_slice=0, new_palette=1; - while(posuse_zero_runs) { + CHECKED_MALLOC( z_slice_cfg, size ); + CHECKED_MALLOC( z_slice_pos, size*sizeof(int) ); + n_z_slice = search_grc_params( zrun_values, n_weights+1, 1, 0, z_slice_cfg, z_slice_pos, size, w_slice_pos, n_w_slice, &z_bitcnt); } - if (i_z_sliceuse_zero_runs ? zrun_values+pos+(!new_palette) : 0; - bitpos = encode_slice( weight_values+pos, zrun_buf, len, - p, new_palette, uncompressed_bits, - w_slice_cfg[i_w_slice], p->use_zero_runs ? z_slice_cfg[i_z_slice] : 0, - bitbuf, bitbuf_size, bitpos, verbose ); - new_palette = 0; - - if (i_w_sliceuse_zero_runs ? zrun_values+pos+(!new_palette) : 0; + bitpos = encode_slice( weight_values+pos, zrun_buf, len, + p, new_palette, uncompressed_bits, + w_slice_cfg[i_w_slice], p->use_zero_runs ? z_slice_cfg[i_z_slice] : 0, + bitbuf, bitbuf_size, bitpos, verbose ); + new_palette = 0; + + if (i_w_slice