From 160c614b1ae26ebc17457f94470103deae1f4608 Mon Sep 17 00:00:00 2001 From: malenov Date: Wed, 12 Jul 2023 15:45:43 +0200 Subject: [PATCH 1/2] fix failure of the automatic memory re-allocation mechanism when ind_list[] buffer is depleted --- lib_com/bitstream.c | 136 ++++++++++++++++++++++++++ lib_com/options.h | 2 +- lib_com/prot.h | 8 ++ lib_enc/init_enc.c | 3 + lib_enc/ivas_corecoder_enc_reconfig.c | 3 + lib_enc/ivas_cpe_enc.c | 3 + lib_enc/ivas_sce_enc.c | 3 + lib_enc/ivas_spar_md_enc.c | 3 + lib_enc/ivas_stereo_td_enc.c | 3 + lib_enc/stat_enc.h | 4 +- 10 files changed, 166 insertions(+), 2 deletions(-) diff --git a/lib_com/bitstream.c b/lib_com/bitstream.c index 25d388aed1..cbb9358d8a 100644 --- a/lib_com/bitstream.c +++ b/lib_com/bitstream.c @@ -169,6 +169,131 @@ Word16 rate2EVSmode( * Re-allocate the list of indices *-------------------------------------------------------------------*/ +#ifdef FIX_MEM_REALLOC_IND_LIST +ivas_error ind_list_realloc( + INDICE_HANDLE old_ind_list, /* i : pointer to the beginning of the old buffer of indices */ + const int16_t max_num_indices, /* i : new maximum number of allowed indices in the list */ + Encoder_Struct *st_ivas /* i : IVAS encoder structure */ +) +{ + int16_t i, n, ch, n_channels, ind_list_pos, is_metadata, ivas_max_num_indices; + INDICE_HANDLE new_ind_list; + BSTR_ENC_HANDLE hBstr; + + if (st_ivas == NULL) + { + return IVAS_ERR_OK; + } + + /* get the pointer to the beginning of the old buffer of indices (either metadata or core coders) */ + if ( old_ind_list == st_ivas->ind_list_metadata ) + { + is_metadata = 1; + ivas_max_num_indices = st_ivas->ivas_max_num_indices_metadata; + } + else + { + is_metadata = 0; + ivas_max_num_indices = st_ivas->ivas_max_num_indices; + } + + /* allocate new buffer of indices */ + if ( ( new_ind_list = (INDICE_HANDLE) malloc( max_num_indices * sizeof( Indice ) ) ) == NULL ) + { + return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) ); + } + + /* move indices from the old list to the new list */ + for ( i = 0; i < min( max_num_indices, ivas_max_num_indices ); i++ ) + { + if ( old_ind_list[i].nb_bits > -1 ) + { + new_ind_list[i].id = old_ind_list[i].id; + new_ind_list[i].value = old_ind_list[i].value; + } + new_ind_list[i].nb_bits = old_ind_list[i].nb_bits; + } + + /* reset nb_bits of all other indices to -1 */ + for ( ; i < max_num_indices; i++ ) + { + new_ind_list[i].nb_bits = -1; + } + + /* update parameters in all SCE elements */ + for ( n = 0; n < st_ivas->nSCE; n++ ) + { + /* get the pointer to hBstr */ + if ( is_metadata ) + { + hBstr = st_ivas->hSCE[n]->hMetaData; + } + else + { + hBstr = st_ivas->hSCE[n]->hCoreCoder[0]->hBstr; + } + + if ( hBstr != NULL ) + { + /* get the current position inside the old list */ + ind_list_pos = ( int16_t )( hBstr->ind_list - old_ind_list ); + + /* set pointers in the new list */ + *( hBstr->ivas_ind_list_zero ) = new_ind_list; + hBstr->ind_list = &new_ind_list[ind_list_pos]; + + /* set the new maximum number of indices */ + *( hBstr->ivas_max_num_indices ) = max_num_indices; + } + } + + /* update parameters in all CPE elements */ + for ( n = 0; n < st_ivas->nCPE; n++ ) + { + /* get the pointer to hBstr */ + if ( is_metadata ) + { + n_channels = 1; + } + else + { + n_channels = CPE_CHANNELS; + } + + for ( ch = 0; ch < n_channels; ch++ ) + { + if ( is_metadata ) + { + hBstr = st_ivas->hCPE[n]->hMetaData; + } + else + { + hBstr = st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr; + } + + if ( hBstr != NULL ) + { + /* get the current position inside the old list */ + ind_list_pos = ( int16_t )( hBstr->ind_list - old_ind_list ); + + /* set pointers in the new list */ + *( hBstr->ivas_ind_list_zero ) = new_ind_list; + hBstr->ind_list = &new_ind_list[ind_list_pos]; + + /* set the new maximum number of indices */ + *( hBstr->ivas_max_num_indices ) = max_num_indices; + } + } + } + + /* free the old list */ + free( old_ind_list ); + + return IVAS_ERR_OK; +} + +#else + ivas_error ind_list_realloc( BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */ const int16_t max_num_indices /* i : new maximum number of allowed indices in the list */ @@ -216,6 +341,8 @@ ivas_error ind_list_realloc( return IVAS_ERR_OK; } +#endif + /*-----------------------------------------------------------------------* * get_ivas_max_num_indices() @@ -468,6 +595,7 @@ int16_t get_core_max_num_indices( const int32_t total_brate /* i : total bitrate */ ) { + /* set the maximum number of indices in the core coder */ if ( core == ACELP_CORE || core == AMR_WB_CORE ) { @@ -866,7 +994,11 @@ ivas_error check_ind_list_limits( #endif /* reallocate the buffer of indices with increased limit */ +#ifdef FIX_MEM_REALLOC_IND_LIST + if ( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES , hBstr->st_ivas) ) != IVAS_ERR_OK ) +#else if ( ( error = ind_list_realloc( hBstr, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES ) ) != IVAS_ERR_OK ) +#endif { return error; } @@ -894,7 +1026,11 @@ ivas_error check_ind_list_limits( #endif /* no available empty slot -> need to re-allocate the buffer */ +#ifdef FIX_MEM_REALLOC_IND_LIST + if ( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES, hBstr->st_ivas ) ) != IVAS_ERR_OK ) +#else if ( ( error = ind_list_realloc( hBstr, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES ) ) != IVAS_ERR_OK ) +#endif { return error; } diff --git a/lib_com/options.h b/lib_com/options.h index a66408dd60..ebe810b16c 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -127,7 +127,6 @@ /*#define DEBUG_BINAURAL_FILTER_DESIGN*/ /* debugging of Crend binaural filter design */ /*#define DEBUG_AGC_ENCODER_CMD_OPTION*/ /* Ability to force enable or disable AGC behaviour in DIRAC/SPAR via command line option */ #define DEBUG_JBM_CMD_OPTION /* ability for telling the decoder the frontend fetch size and to not delay compensate for bad frames at the beginning */ - #define VARIABLE_SPEED_DECODING /* variable speed decoding employing the JBM functioniality; move to DEBUGGING after build for disabled is fixed */ #endif @@ -167,6 +166,7 @@ #define FIX_580_PARAMMC_ENER_BURSTS /* FhG: issue 580: energy bursts due to ILD holding when energy relations change too much */ #define FIX_593_STL_INCLUDE /* FhG: Issue 593: correct include of stl.h in lib_enc/ivas_stereo_eclvq_enc.c */ #define FIX_583_CLANG_TRANS_DET /* FhG: Issue 583: clang left shift on ramp_up_flag in transient detector */ +#define FIX_MEM_REALLOC_IND_LIST /* VA: issue 601: failure of the automatic memory re-allocation mechanism when ind_list[] buffer is depleted in MASA mode with 2 TC*/ /* ################## End BE DEVELOPMENT switches ######################### */ diff --git a/lib_com/prot.h b/lib_com/prot.h index 592d0253d3..1667745077 100644 --- a/lib_com/prot.h +++ b/lib_com/prot.h @@ -538,10 +538,18 @@ int16_t get_ivas_max_num_indices_metadata( const int32_t ivas_total_brate /* i : IVAS total bitrate */ ); +#ifdef FIX_MEM_REALLOC_IND_LIST ivas_error ind_list_realloc( + INDICE_HANDLE old_ind_list, /* i : pointer to the beginning of the old buffer of indices */ + const int16_t max_num_indices, /* i : new maximum number of allowed indices in the list */ + Encoder_Struct *st_ivas /* i : IVAS encoder structure */ +); +#else + ivas_error ind_list_realloc( BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */ const int16_t max_num_indices /* i : new maximum number of allowed indices in the list */ ); +#endif ivas_error check_ind_list_limits( BSTR_ENC_HANDLE hBstr /* i/o: encoder bitstream handle */ diff --git a/lib_enc/init_enc.c b/lib_enc/init_enc.c index 8cde5b838b..96d32aa77e 100644 --- a/lib_enc/init_enc.c +++ b/lib_enc/init_enc.c @@ -126,6 +126,9 @@ ivas_error init_encoder( st->hBstr->ivas_max_num_indices = &st_ivas->ivas_max_num_indices; st->hBstr->nb_ind_tot = 0; st->hBstr->nb_bits_tot = 0; +#ifdef FIX_MEM_REALLOC_IND_LIST + st->hBstr->st_ivas = st_ivas; +#endif } else { diff --git a/lib_enc/ivas_corecoder_enc_reconfig.c b/lib_enc/ivas_corecoder_enc_reconfig.c index 10c627c91b..ff79dd11b0 100644 --- a/lib_enc/ivas_corecoder_enc_reconfig.c +++ b/lib_enc/ivas_corecoder_enc_reconfig.c @@ -468,6 +468,9 @@ ivas_error ivas_corecoder_enc_reconfig( st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData->ivas_max_num_indices = &st_ivas->ivas_max_num_indices_metadata; st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData->nb_ind_tot = 0; st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData->nb_bits_tot = 0; +#ifdef FIX_MEM_REALLOC_IND_LIST + st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData->st_ivas = st_ivas; +#endif } reset_indices_enc( st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData, st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData->nb_ind_tot ); diff --git a/lib_enc/ivas_cpe_enc.c b/lib_enc/ivas_cpe_enc.c index 3c1d7a61c6..09cd09eacc 100644 --- a/lib_enc/ivas_cpe_enc.c +++ b/lib_enc/ivas_cpe_enc.c @@ -812,6 +812,9 @@ ivas_error create_cpe_enc( hCPE->hMetaData->ind_list = st_ivas->ind_list_metadata; hCPE->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata; hCPE->hMetaData->ivas_max_num_indices = &st_ivas->ivas_max_num_indices_metadata; +#ifdef FIX_MEM_REALLOC_IND_LIST + hCPE->hMetaData->st_ivas = st_ivas; +#endif reset_indices_enc( hCPE->hMetaData, st_ivas->ivas_max_num_indices_metadata ); } diff --git a/lib_enc/ivas_sce_enc.c b/lib_enc/ivas_sce_enc.c index 97992ecb9f..b9e6a85527 100644 --- a/lib_enc/ivas_sce_enc.c +++ b/lib_enc/ivas_sce_enc.c @@ -316,6 +316,9 @@ ivas_error create_sce_enc( hSCE->hMetaData->ind_list = st_ivas->ind_list_metadata; hSCE->hMetaData->ivas_ind_list_zero = &st_ivas->ind_list_metadata; hSCE->hMetaData->ivas_max_num_indices = &st_ivas->ivas_max_num_indices_metadata; +#ifdef FIX_MEM_REALLOC_IND_LIST + hSCE->hMetaData->st_ivas = st_ivas; +#endif reset_indices_enc( hSCE->hMetaData, st_ivas->ivas_max_num_indices_metadata ); } else diff --git a/lib_enc/ivas_spar_md_enc.c b/lib_enc/ivas_spar_md_enc.c index 3a93f3d0a1..66dc292fdf 100644 --- a/lib_enc/ivas_spar_md_enc.c +++ b/lib_enc/ivas_spar_md_enc.c @@ -627,6 +627,9 @@ ivas_error ivas_spar_md_enc_process( max_num_indices_tmp = MAX_BITS_METADATA; hMetaData_tmp.ivas_max_num_indices = &max_num_indices_tmp; hMetaData_tmp.ivas_ind_list_zero = (Indice **) ( &hMetaData_tmp.ind_list ); +#ifdef FIX_MEM_REALLOC_IND_LIST + hMetaData_tmp.st_ivas = NULL; +#endif /* Save state of metadata bitstream buffer */ bit_pos_start = hMetaData->nb_bits_tot; diff --git a/lib_enc/ivas_stereo_td_enc.c b/lib_enc/ivas_stereo_td_enc.c index 3f8fe55809..a59ac93e49 100644 --- a/lib_enc/ivas_stereo_td_enc.c +++ b/lib_enc/ivas_stereo_td_enc.c @@ -128,6 +128,9 @@ void stereo_td_init_enc( hStereoTD->tdm_hBstr_tmp.ivas_ind_list_zero = (Indice **) ( &hStereoTD->tdm_hBstr_tmp.ind_list ); hStereoTD->max_ind_tdm_tmp = MAX_IND_TDM_TMP; hStereoTD->tdm_hBstr_tmp.ivas_max_num_indices = &hStereoTD->max_ind_tdm_tmp; +#ifdef FIX_MEM_REALLOC_IND_LIST + hStereoTD->tdm_hBstr_tmp.st_ivas = NULL; +#endif reset_indices_enc( &hStereoTD->tdm_hBstr_tmp, MAX_IND_TDM_TMP ); return; diff --git a/lib_enc/stat_enc.h b/lib_enc/stat_enc.h index 2e7924f371..e6d9e863d0 100644 --- a/lib_enc/stat_enc.h +++ b/lib_enc/stat_enc.h @@ -46,7 +46,6 @@ #include "cnst.h" #include "ivas_cnst.h" - /*------------------------------------------------------------------------------------------* * Indice *------------------------------------------------------------------------------------------*/ @@ -69,6 +68,9 @@ typedef struct bitstream_enc_data_structure Indice *ind_list; /* list of indices */ int16_t *ivas_max_num_indices; /* maximum total number of indices in the list */ Indice **ivas_ind_list_zero; /* beginning of the buffer of indices */ +#ifdef FIX_MEM_REALLOC_IND_LIST + void *st_ivas; /* IVAS encoder structure */ +#endif } BSTR_ENC_DATA, *BSTR_ENC_HANDLE; /*----------------------------------------------------------------------------------* -- GitLab From a0ae7d6d53d362e802955c405987f04286e5a65e Mon Sep 17 00:00:00 2001 From: Vladimir Malenovsky Date: Wed, 12 Jul 2023 15:57:58 +0200 Subject: [PATCH 2/2] formatting --- lib_com/bitstream.c | 16 ++++++++-------- lib_com/prot.h | 8 ++++---- lib_enc/stat_enc.h | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib_com/bitstream.c b/lib_com/bitstream.c index cbb9358d8a..dee02c69b6 100644 --- a/lib_com/bitstream.c +++ b/lib_com/bitstream.c @@ -171,20 +171,20 @@ Word16 rate2EVSmode( #ifdef FIX_MEM_REALLOC_IND_LIST ivas_error ind_list_realloc( - INDICE_HANDLE old_ind_list, /* i : pointer to the beginning of the old buffer of indices */ - const int16_t max_num_indices, /* i : new maximum number of allowed indices in the list */ - Encoder_Struct *st_ivas /* i : IVAS encoder structure */ + INDICE_HANDLE old_ind_list, /* i : pointer to the beginning of the old buffer of indices */ + const int16_t max_num_indices, /* i : new maximum number of allowed indices in the list */ + Encoder_Struct *st_ivas /* i : IVAS encoder structure */ ) { int16_t i, n, ch, n_channels, ind_list_pos, is_metadata, ivas_max_num_indices; INDICE_HANDLE new_ind_list; BSTR_ENC_HANDLE hBstr; - if (st_ivas == NULL) + if ( st_ivas == NULL ) { return IVAS_ERR_OK; } - + /* get the pointer to the beginning of the old buffer of indices (either metadata or core coders) */ if ( old_ind_list == st_ivas->ind_list_metadata ) { @@ -236,7 +236,7 @@ ivas_error ind_list_realloc( if ( hBstr != NULL ) { /* get the current position inside the old list */ - ind_list_pos = ( int16_t )( hBstr->ind_list - old_ind_list ); + ind_list_pos = (int16_t) ( hBstr->ind_list - old_ind_list ); /* set pointers in the new list */ *( hBstr->ivas_ind_list_zero ) = new_ind_list; @@ -274,7 +274,7 @@ ivas_error ind_list_realloc( if ( hBstr != NULL ) { /* get the current position inside the old list */ - ind_list_pos = ( int16_t )( hBstr->ind_list - old_ind_list ); + ind_list_pos = (int16_t) ( hBstr->ind_list - old_ind_list ); /* set pointers in the new list */ *( hBstr->ivas_ind_list_zero ) = new_ind_list; @@ -995,7 +995,7 @@ ivas_error check_ind_list_limits( /* reallocate the buffer of indices with increased limit */ #ifdef FIX_MEM_REALLOC_IND_LIST - if ( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES , hBstr->st_ivas) ) != IVAS_ERR_OK ) + if ( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES, hBstr->st_ivas ) ) != IVAS_ERR_OK ) #else if ( ( error = ind_list_realloc( hBstr, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES ) ) != IVAS_ERR_OK ) #endif diff --git a/lib_com/prot.h b/lib_com/prot.h index 1667745077..4abc410b70 100644 --- a/lib_com/prot.h +++ b/lib_com/prot.h @@ -540,12 +540,12 @@ int16_t get_ivas_max_num_indices_metadata( #ifdef FIX_MEM_REALLOC_IND_LIST ivas_error ind_list_realloc( - INDICE_HANDLE old_ind_list, /* i : pointer to the beginning of the old buffer of indices */ - const int16_t max_num_indices, /* i : new maximum number of allowed indices in the list */ - Encoder_Struct *st_ivas /* i : IVAS encoder structure */ + INDICE_HANDLE old_ind_list, /* i : pointer to the beginning of the old buffer of indices */ + const int16_t max_num_indices, /* i : new maximum number of allowed indices in the list */ + Encoder_Struct *st_ivas /* i : IVAS encoder structure */ ); #else - ivas_error ind_list_realloc( +ivas_error ind_list_realloc( BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */ const int16_t max_num_indices /* i : new maximum number of allowed indices in the list */ ); diff --git a/lib_enc/stat_enc.h b/lib_enc/stat_enc.h index e6d9e863d0..45d01ac6ca 100644 --- a/lib_enc/stat_enc.h +++ b/lib_enc/stat_enc.h @@ -69,7 +69,7 @@ typedef struct bitstream_enc_data_structure int16_t *ivas_max_num_indices; /* maximum total number of indices in the list */ Indice **ivas_ind_list_zero; /* beginning of the buffer of indices */ #ifdef FIX_MEM_REALLOC_IND_LIST - void *st_ivas; /* IVAS encoder structure */ + void *st_ivas; /* IVAS encoder structure */ #endif } BSTR_ENC_DATA, *BSTR_ENC_HANDLE; -- GitLab