From 52ac081159252e6a84f4baacf3c00baf055139e1 Mon Sep 17 00:00:00 2001 From: malenov Date: Thu, 1 Jun 2023 16:58:44 +0200 Subject: [PATCH 1/2] - replace assert() with warning message when hitting memory limit in the buffer of indices - improve error handling --- lib_com/bitstream.c | 81 ++++++++++++++++++++++++++++++++++++++++- lib_com/options.h | 3 ++ lib_com/prot.h | 12 ++++++ lib_enc/ivas_init_enc.c | 2 - lib_enc/lib_enc.c | 4 -- 5 files changed, 95 insertions(+), 7 deletions(-) diff --git a/lib_com/bitstream.c b/lib_com/bitstream.c index c9571d41c1..a7f6c896bb 100755 --- a/lib_com/bitstream.c +++ b/lib_com/bitstream.c @@ -907,16 +907,22 @@ int16_t get_ivas_max_num_indices_metadata( /* o * Move indices inside the buffer or among two buffers *-------------------------------------------------------------------*/ +#ifdef FIX_545_ASSERT +void move_indices( +#else ivas_error move_indices( +#endif INDICE_HANDLE old_ind_list, /* i/o: old location of indices */ INDICE_HANDLE new_ind_list, /* i/o: new location of indices */ const int16_t nb_indices /* i : number of moved indices */ ) { int16_t i; +#ifndef FIX_545_ASSERT ivas_error error; error = IVAS_ERR_OK; +#endif if ( new_ind_list < old_ind_list ) { @@ -941,7 +947,11 @@ ivas_error move_indices( } } +#ifdef FIX_545_ASSERT + return; +#else return error; +#endif } #endif @@ -969,13 +979,24 @@ ivas_error check_ind_list_limits( if ( ( &hBstr->ind_list[hBstr->nb_ind_tot] - ivas_ind_list_zero ) >= *( hBstr->ivas_max_num_indices ) ) { #ifdef DEBUGGING +#ifdef FIX_545_ASSERT + fprintf( stderr, "Warning: The maximum number of indices %d has been exceeded in frame %d! Increase the limits in get_ivas_max_num_indices() or get_max_num_indices_metadata().\n", *( hBstr->ivas_max_num_indices ), frame ); +#else /* TODO: replace with the warning message below before the finalization of the IVAS codec */ /* fprintf( stderr, "Warning: The maximum number of indices %d has been exceeded in frame %d! Increase the limits in get_ivas_max_num_indices() or get_max_num_indices_metadata().\n", *( hBstr->ivas_max_num_indices ), frame ); */ assert( 0 && "The maximum number of indices has been exceeded! Increase the limits in get_ivas_max_num_indices() or get_max_num_indices_metadata()." ); +#endif #endif /* reallocate the buffer of indices with increased limit */ +#ifdef FIX_545_ASSERT + if ( ( error = ind_list_realloc( hBstr, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES ) ) != IVAS_ERR_OK ) + { + return error; + } +#else ind_list_realloc( hBstr, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES ); +#endif } /* check, if we will not overwrite an existing indice */ @@ -996,13 +1017,24 @@ ivas_error check_ind_list_limits( if ( hBstr->ind_list >= ivas_ind_list_last ) { #ifdef DEBUGGING +#ifdef FIX_545_ASSERT + fprintf( stderr, "Warning: The maximum number of indices %d has been exceeded in frame %d! Increase the limits in get_ivas_max_num_indices() or get_max_num_indices_metadata().\n", *( hBstr->ivas_max_num_indices ), frame ); +#else /* TODO: replace with the warning message below before the finalization of the IVAS codec */ /* fprintf( stderr, "Warning: The maximum number of indices %d has been exceeded in frame %d! Increase the limits in get_ivas_max_num_indices() or get_max_num_indices_metadata().\n", *( hBstr->ivas_max_num_indices ), frame ); */ assert( 0 && "The maximum number of indices has been exceeded! Increase the limits in get_ivas_max_num_indices() or get_max_num_indices_metadata()." ); +#endif #endif /* no available empty slot -> need to re-allocate the buffer */ +#ifdef FIX_545_ASSERT + if ( ( error = ind_list_realloc( hBstr, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES ) ) != IVAS_ERR_OK ) + { + return error; + } +#else ind_list_realloc( hBstr, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES ); +#endif } } else @@ -1080,8 +1112,15 @@ ivas_error push_indice( #ifdef IND_LIST_DYN /* check the limits of the list of indices */ +#ifdef FIX_545_ASSERT + if ( ( error = check_ind_list_limits( hBstr )) != IVAS_ERR_OK ) + { + return IVAS_ERROR( error, "Error occured in push_indice() while re-allocating the list of indices (frame %d) !\n", frame ); + } +#else error = check_ind_list_limits( hBstr ); #endif +#endif #ifdef IND_LIST_DYN /* find the location in the list of indices based on ID */ @@ -1203,8 +1242,15 @@ ivas_error push_next_indice( #ifdef IND_LIST_DYN /* check the limits of the list of indices */ +#ifdef FIX_545_ASSERT + if ( ( error = check_ind_list_limits( hBstr )) != IVAS_ERR_OK ) + { + return error; + } +#else error = check_ind_list_limits( hBstr ); #endif +#endif #ifdef IND_LIST_DYN /* get the id of the previous indice -> it will be re-used */ @@ -1246,9 +1292,17 @@ ivas_error push_next_indice( *-------------------------------------------------------------------*/ #ifdef DEBUG_BS_READ_WRITE +#ifdef FIX_545_ASSERT +ivas_error push_next_bits_( +#else void push_next_bits_( +#endif +#else +#ifdef FIX_545_ASSERT +ivas_error push_next_bits( #else void push_next_bits( +#endif #endif BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */ const uint16_t bits[], /* i : bit buffer to pack, sequence of single bits */ @@ -1265,6 +1319,11 @@ void push_next_bits( Indice *ptr; #ifdef IND_LIST_DYN int16_t prev_id; +#ifdef FIX_545_ASSERT + ivas_error error; + + error = IVAS_ERR_OK; +#endif #endif #ifdef DEBUG_BS_READ_WRITE printf( "%s: %d: %d\n", func, line, nb_bits ); @@ -1293,7 +1352,14 @@ void push_next_bits( #ifdef IND_LIST_DYN /* check the limits of the list of indices */ +#ifdef FIX_545_ASSERT + if ( ( error = check_ind_list_limits( hBstr )) != IVAS_ERR_OK ) + { + return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n", frame ); + } +#else check_ind_list_limits( hBstr ); +#endif ptr = &hBstr->ind_list[hBstr->nb_ind_tot]; #endif @@ -1313,7 +1379,14 @@ void push_next_bits( { #ifdef IND_LIST_DYN /* check the limits of the list of indices */ +#ifdef FIX_545_ASSERT + if ( ( error = check_ind_list_limits( hBstr )) != IVAS_ERR_OK ) + { + return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n", frame ); + } +#else check_ind_list_limits( hBstr ); +#endif ptr = &hBstr->ind_list[hBstr->nb_ind_tot]; #endif @@ -1334,7 +1407,11 @@ void push_next_bits( #endif hBstr->nb_bits_tot = hBstr->nb_bits_tot + nb_bits; +#ifdef FIX_545_ASSERT + return error; +#else return; +#endif } @@ -1535,10 +1612,12 @@ uint16_t get_indice( { uint16_t value; int16_t i; + int32_t nbits_total; assert( nb_bits <= 16 ); - int32_t nbits_total; + nbits_total = st->total_brate / FRAMES_PER_SEC; + /* detect corrupted bitstream */ if ( pos + nb_bits > nbits_total ) { diff --git a/lib_com/options.h b/lib_com/options.h index 8a491e2499..fabf8f14c3 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -228,6 +228,9 @@ #define FIX_510 /* FhG: fix issue 510, misleading error message for invalid input format */ #define FIX_509 /* FhG: fix issue 509, too low number of bitsream indices in SBA */ #define FIX_519_JBM_ACCESS_NULL_TC_BUFFER /* FhG: fix issue 519, accessing a yet uninitialized TC Buffer in frame 0*/ +#ifdef IND_LIST_DYN +#define FIX_545_ASSERT /* VA: fix issue 545, replace assert() with warning message when hitting memory limit in the buffer of indices */ +#endif #define FIX_TODO_NON_DIEGETIC_PAN_NOT_IMPLELENTED_IN_RENDERER /* ..\apps\renderer.c(240): .... (todo: implementation)",*/ #define REMOVE_OBS_CODE /* FhG: Remove unnecessary assignement after LFE cleanup (Issue #451)*/ diff --git a/lib_com/prot.h b/lib_com/prot.h index e6f371c468..23c3b98756 100644 --- a/lib_com/prot.h +++ b/lib_com/prot.h @@ -505,9 +505,17 @@ ivas_error push_next_indice( #ifdef DEBUG_BS_READ_WRITE #define push_next_bits( ... ) push_next_bits_( __VA_ARGS__, __LINE__, __func__ ) +#ifdef FIX_545_ASSERT +ivas_error push_next_bits_( +#else void push_next_bits_( +#endif +#else +#ifdef FIX_545_ASSERT +ivas_error push_next_bits( #else void push_next_bits( +#endif #endif BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */ const uint16_t bits[], /* i : bit buffer to pack, sequence of single bits */ @@ -548,7 +556,11 @@ ivas_error check_ind_list_limits( BSTR_ENC_HANDLE hBstr /* i/o: encoder bitstream handle */ ); +#ifdef FIX_545_ASSERT +void move_indices( +#else ivas_error move_indices( +#endif INDICE_HANDLE old_ind_list, /* i/o: old location of indices */ INDICE_HANDLE new_ind_list, /* i/o: new location of indices */ const int16_t nb_indices /* i : number of moved indices */ diff --git a/lib_enc/ivas_init_enc.c b/lib_enc/ivas_init_enc.c index 006fb176fb..5591b12f40 100644 --- a/lib_enc/ivas_init_enc.c +++ b/lib_enc/ivas_init_enc.c @@ -329,8 +329,6 @@ ivas_error ivas_init_encoder( #ifndef IND_LIST_DYN , Indice ind_list[][MAX_NUM_INDICES] /* o : bitstream indices */ -#endif -#ifndef IND_LIST_DYN , Indice ind_list_metadata[][MAX_BITS_METADATA] /* o : bitstream indices metadata */ #endif diff --git a/lib_enc/lib_enc.c b/lib_enc/lib_enc.c index 37c3e57ab5..453d74ea03 100755 --- a/lib_enc/lib_enc.c +++ b/lib_enc/lib_enc.c @@ -52,8 +52,6 @@ struct IVAS_ENC Encoder_Struct *st_ivas; #ifndef IND_LIST_DYN Indice ind_list[MAX_NUM_DATA][MAX_NUM_INDICES]; /* list of indices */ -#endif -#ifndef IND_LIST_DYN Indice ind_list_metadata[MAX_NUM_METADATA][MAX_BITS_METADATA]; /* list of indices for metadata */ #endif ENC_CORE_HANDLE hCoreCoder; @@ -941,8 +939,6 @@ static ivas_error configureEncoder( #ifndef IND_LIST_DYN , hIvasEnc->ind_list -#endif -#ifndef IND_LIST_DYN , hIvasEnc->ind_list_metadata #endif -- GitLab From c0d2d3082a90a61b0d82559f7134b73f4e25ba23 Mon Sep 17 00:00:00 2001 From: Vladimir Malenovsky Date: Fri, 2 Jun 2023 09:25:28 +0200 Subject: [PATCH 2/2] clang format --- lib_com/bitstream.c | 8 ++++---- lib_enc/lib_enc.c | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib_com/bitstream.c b/lib_com/bitstream.c index a7f6c896bb..2321d37334 100755 --- a/lib_com/bitstream.c +++ b/lib_com/bitstream.c @@ -1113,7 +1113,7 @@ ivas_error push_indice( #ifdef IND_LIST_DYN /* check the limits of the list of indices */ #ifdef FIX_545_ASSERT - if ( ( error = check_ind_list_limits( hBstr )) != IVAS_ERR_OK ) + if ( ( error = check_ind_list_limits( hBstr ) ) != IVAS_ERR_OK ) { return IVAS_ERROR( error, "Error occured in push_indice() while re-allocating the list of indices (frame %d) !\n", frame ); } @@ -1243,7 +1243,7 @@ ivas_error push_next_indice( #ifdef IND_LIST_DYN /* check the limits of the list of indices */ #ifdef FIX_545_ASSERT - if ( ( error = check_ind_list_limits( hBstr )) != IVAS_ERR_OK ) + if ( ( error = check_ind_list_limits( hBstr ) ) != IVAS_ERR_OK ) { return error; } @@ -1353,7 +1353,7 @@ void push_next_bits( #ifdef IND_LIST_DYN /* check the limits of the list of indices */ #ifdef FIX_545_ASSERT - if ( ( error = check_ind_list_limits( hBstr )) != IVAS_ERR_OK ) + if ( ( error = check_ind_list_limits( hBstr ) ) != IVAS_ERR_OK ) { return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n", frame ); } @@ -1380,7 +1380,7 @@ void push_next_bits( #ifdef IND_LIST_DYN /* check the limits of the list of indices */ #ifdef FIX_545_ASSERT - if ( ( error = check_ind_list_limits( hBstr )) != IVAS_ERR_OK ) + if ( ( error = check_ind_list_limits( hBstr ) ) != IVAS_ERR_OK ) { return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n", frame ); } diff --git a/lib_enc/lib_enc.c b/lib_enc/lib_enc.c index 453d74ea03..bc71f62ece 100755 --- a/lib_enc/lib_enc.c +++ b/lib_enc/lib_enc.c @@ -51,7 +51,7 @@ struct IVAS_ENC { Encoder_Struct *st_ivas; #ifndef IND_LIST_DYN - Indice ind_list[MAX_NUM_DATA][MAX_NUM_INDICES]; /* list of indices */ + Indice ind_list[MAX_NUM_DATA][MAX_NUM_INDICES]; /* list of indices */ Indice ind_list_metadata[MAX_NUM_METADATA][MAX_BITS_METADATA]; /* list of indices for metadata */ #endif ENC_CORE_HANDLE hCoreCoder; @@ -938,8 +938,7 @@ static ivas_error configureEncoder( if ( ( error = ivas_init_encoder( st_ivas #ifndef IND_LIST_DYN , - hIvasEnc->ind_list - , + hIvasEnc->ind_list, hIvasEnc->ind_list_metadata #endif ) ) != IVAS_ERR_OK ) -- GitLab