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_common.h | 4 +- ethosu/mlw_codec/mlw_decode.c | 72 +++++++----- ethosu/mlw_codec/mlw_decode.h | 10 +- ethosu/mlw_codec/mlw_encode.c | 265 ++++++++++++++++++++++++------------------ ethosu/mlw_codec/mlw_encode.h | 14 +-- ethosu/mlw_codec/mlw_main.c | 31 ++--- 6 files changed, 225 insertions(+), 171 deletions(-) (limited to 'ethosu') diff --git a/ethosu/mlw_codec/mlw_common.h b/ethosu/mlw_codec/mlw_common.h index 70f72de1..947a655c 100644 --- a/ethosu/mlw_codec/mlw_common.h +++ b/ethosu/mlw_codec/mlw_common.h @@ -18,8 +18,8 @@ #include -#ifndef __MLW_COMMON_H__ -#define __MLW_COMMON_H__ +#ifndef MLW_COMMON_H +#define MLW_COMMON_H #define ZDIV_DISABLE 6 // not alternating mode #define ZDIV_EOS 7 // indicates end of stream diff --git a/ethosu/mlw_codec/mlw_decode.c b/ethosu/mlw_codec/mlw_decode.c index 97de7869..aaba368a 100644 --- a/ethosu/mlw_codec/mlw_decode.c +++ b/ethosu/mlw_codec/mlw_decode.c @@ -28,6 +28,7 @@ #include "mlw_common.h" #include "mlw_decode.h" +#define CHECKED_MALLOC(var, size) { if ( !(var = malloc(size)) ) break; } /////////////////////////////// Read from bitstream @@ -97,8 +98,10 @@ int mlw_decode( uint8_t *inbuf, int inbuf_size, int16_t **outbuf, int verbose) { bitbuf_t bitbuf_s, *bb=&bitbuf_s; bitbuf_init( bb, inbuf, inbuf_size, (verbose&2)?1:0 ); + int *w_value = NULL; + int *z_value = NULL; // Loop over all slices - while(1) { + do { // Decode slice header z_grc_div = bitbuf_get( bb, "ZDIV", 3 ); while(z_grc_div==ZDIV_EOS) { // TODO: change to ZDIV_PAD @@ -169,8 +172,9 @@ int mlw_decode( uint8_t *inbuf, int inbuf_size, int16_t **outbuf, int verbose) { // Decode the slice int z_nvalues = nvalues + (new_palette?1:0); - int *w_value = malloc( nvalues*sizeof(int) ); - int *z_value = malloc( z_nvalues*sizeof(int) ); + CHECKED_MALLOC( w_value, nvalues*sizeof(int) ); + CHECKED_MALLOC( z_value, z_nvalues*sizeof(int) ); + z_value[0] = 0; int w_pos=0, z_pos=0; int w_prev_pos=0, z_prev_pos=0; int w_unary0=0, w_unary1=0, w_unary1_len=0, w_q[12]={0}, w_carry=0; @@ -262,39 +266,49 @@ int mlw_decode( uint8_t *inbuf, int inbuf_size, int16_t **outbuf, int verbose) { // Interleave non-zero and zeros into the outbut buffer // Increase the outbuffer to fit the new slice *outbuf = realloc( *outbuf, (outbuf_size + nvalues + total_zcnt)*sizeof(int16_t)); + if (*outbuf) + { + int k=outbuf_size; - int k=outbuf_size; - - // Insert initial zeros - // (slices redefining the palette may start with zeros) - if (new_palette && use_zero_run) { - for(j=0; j>1; - (*outbuf)[k++] = sign ? -mag : mag; - if (use_zero_run) { - for(j=0; j>1; + (*outbuf)[k++] = sign ? -mag : mag; + if (use_zero_run) { + for(j=0; j -#ifndef __MLW_DECODE_H__ -#define __MLW_DECODE_H__ +#ifndef MLW_DECODE_H +#define MLW_DECODE_H #ifdef _MSC_VER - #define EXPORTED __declspec(dllexport) + #define MLW_DECODE_EXPORTED __declspec(dllexport) #else - #define EXPORTED __attribute__((visibility("default"))) + #define MLW_DECODE_EXPORTED __attribute__((visibility("default"))) #endif #if __cplusplus @@ -32,7 +32,7 @@ extern "C" { #endif -EXPORTED +MLW_DECODE_EXPORTED int mlw_decode(uint8_t *inbuf, int inbuf_size, int16_t **outbuf, int verbose); #if __cplusplus 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 -#ifndef __MLW_ENCODE_H__ -#define __MLW_ENCODE_H__ +#ifndef MLW_ENCODE_H +#define MLW_ENCODE_H #ifdef _MSC_VER - #define EXPORTED __declspec(dllexport) + #define MLW_ENCODE_EXPORTED __declspec(dllexport) #else - #define EXPORTED __attribute__((visibility("default"))) + #define MLW_ENCODE_EXPORTED __attribute__((visibility("default"))) #endif #if __cplusplus @@ -32,13 +32,13 @@ extern "C" { #endif -EXPORTED +MLW_ENCODE_EXPORTED int mlw_encode(int16_t *inbuf, int inbuf_size, uint8_t **outbuf, int verbose); -EXPORTED +MLW_ENCODE_EXPORTED void mlw_free_outbuf(uint8_t *outbuf); -EXPORTED +MLW_ENCODE_EXPORTED int mlw_reorder_encode( int ifm_ublock_depth, int ofm_ublock_depth, diff --git a/ethosu/mlw_codec/mlw_main.c b/ethosu/mlw_codec/mlw_main.c index 11cb8f99..a3a38be6 100644 --- a/ethosu/mlw_codec/mlw_main.c +++ b/ethosu/mlw_codec/mlw_main.c @@ -28,6 +28,8 @@ #include "mlw_encode.h" #include "mlw_decode.h" +#define UNCHECKED(call) (void)call + static void fatal_error(const char *format, ...) { va_list ap; va_start (ap, format); @@ -49,17 +51,20 @@ static void print_usage(void) { } // Read file into allocated buffer. Return length in bytes. -static int read_file( FILE *f, uint8_t **buf) { - - fseek(f, 0, SEEK_END); - int size = ftell(f); - fseek(f, 0, SEEK_SET); +static size_t read_file( FILE *f, uint8_t **buf) { + size_t rsize = 0; + UNCHECKED(fseek(f, 0, SEEK_END)); + long size = ftell(f); + size = size < 0 ? 0 : size; + UNCHECKED(fseek(f, 0, SEEK_SET)); *buf = malloc(size); - assert(*buf); - int rsize = fread(*buf, 1, size, f); - assert(rsize==size); - fclose(f); - return size; + if (*buf) + { + rsize = fread(*buf, 1, size, f); + assert(rsize==size); + } + UNCHECKED(fclose(f)); + return rsize; } @@ -155,12 +160,12 @@ int main(int argc, char *argv[]) outbuf[i] = weights[i]; } free(weights); - printf("Input size %d output size %d bpw %4.2f\n", inbuf_size, n, inbuf_size*8.0/n); + printf("Input size %d output size %d bpw %4.2f\n", inbuf_size, n, n ? inbuf_size*8.0/n : 0); } if (outfile) { - fwrite(outbuf, 1, outbuf_size, outfile); + UNCHECKED(fwrite(outbuf, 1, outbuf_size, outfile)); } if (inbuf) @@ -170,7 +175,7 @@ int main(int argc, char *argv[]) } if (outfile) { - fclose(outfile); + UNCHECKED(fclose(outfile)); } return 0; -- cgit v1.2.1