Commit 141240ae authored by vaclav's avatar vaclav
Browse files

harmonize DirAC parameters allocation/deallocation; under DIRAC_ALLOC_HARM

parent 50680cf6
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -3457,6 +3457,13 @@ ivas_error ivas_dirac_dec_open(
    Decoder_Struct *st_ivas                                     /* i/o: IVAS decoder structure                  */
);

#ifdef DIRAC_ALLOC_HARM
ivas_error ivas_dirac_allocate_parameters(
    DIRAC_DEC_HANDLE hDirAC,                                    /* i/o: decoder DirAC handle                    */
    const int16_t params_flag                                   /* i  : set of parameters flag                  */
);
#endif

ivas_error ivas_dirac_dec_config(
    Decoder_Struct *st_ivas,                                    /* i/o: IVAS decoder structure                                       */
    const DIRAC_CONFIG_FLAG flag_configopen                     /* i/ : Flag determining if we open or reconfigure the DirAC decoder */
@@ -3466,6 +3473,13 @@ void ivas_dirac_dec_close(
    DIRAC_DEC_HANDLE *hDirAC                                    /* i/o: decoder DirAC handle                    */
);

#ifdef DIRAC_ALLOC_HARM
void ivas_dirac_deallocate_parameters(
    DIRAC_DEC_HANDLE hDirAC,                                    /* i/o: decoder DirAC handle                    */
    const int16_t params_flag                                   /* i  : set of parameters flag                  */
);
#endif

void ivas_dirac_dec_read_BS(
    const int32_t ivas_total_brate,                             /* i  : IVAS total bitrate                      */
    Decoder_State *st,                                          /* i/o: decoder Core state structure            */
+2 −0
Original line number Diff line number Diff line
@@ -209,6 +209,8 @@
//#define HODIRAC_WRITE_PARAMS
//#define HODIRAC_READ_PARAMS
#endif
#define DIRAC_ALLOC_HARM                                /* VA: harmonize DirAC parameters allocation/deallocation */


/* ################## End DEVELOPMENT switches ######################### */
/* clang-format on */
+349 −2
Original line number Diff line number Diff line
@@ -113,6 +113,146 @@ ivas_error ivas_dirac_dec_open(
}


#ifdef DIRAC_ALLOC_HARM
/*-------------------------------------------------------------------------
 * ivas_dirac_allocate_parameters()
 *
 * Allocate and initialize DirAC parameters
 *-------------------------------------------------------------------------*/

ivas_error ivas_dirac_allocate_parameters(
    DIRAC_DEC_HANDLE hDirAC,  /* i/o: decoder DirAC handle      */
    const int16_t params_flag /* i  : set of parameters flag    */
)
{
    int16_t i;

    if ( params_flag == 1 )
    {
        if ( ( hDirAC->azimuth = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        if ( ( hDirAC->elevation = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        if ( ( hDirAC->diffuseness_vector = (float **) malloc( hDirAC->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        if ( ( hDirAC->energy_ratio1 = (float **) malloc( hDirAC->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        if ( ( hDirAC->spreadCoherence = (float **) malloc( hDirAC->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        if ( ( hDirAC->surroundingCoherence = (float **) malloc( hDirAC->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
        {
            if ( ( hDirAC->azimuth[i] = (int16_t *) malloc( hDirAC->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_s( hDirAC->azimuth[i], 0, hDirAC->num_freq_bands );

            if ( ( hDirAC->elevation[i] = (int16_t *) malloc( hDirAC->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_s( hDirAC->elevation[i], 0, hDirAC->num_freq_bands );

            if ( ( hDirAC->diffuseness_vector[i] = (float *) malloc( hDirAC->num_freq_bands * sizeof( float ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_f( hDirAC->diffuseness_vector[i], 1.0f, hDirAC->num_freq_bands );

            if ( ( hDirAC->energy_ratio1[i] = (float *) malloc( hDirAC->num_freq_bands * sizeof( float ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_f( hDirAC->energy_ratio1[i], 0.0f, hDirAC->num_freq_bands );

            if ( ( hDirAC->spreadCoherence[i] = (float *) malloc( hDirAC->num_freq_bands * sizeof( float ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_f( hDirAC->spreadCoherence[i], 0.0f, hDirAC->num_freq_bands );

            if ( ( hDirAC->surroundingCoherence[i] = (float *) malloc( hDirAC->num_freq_bands * sizeof( float ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_f( hDirAC->surroundingCoherence[i], 0.0f, hDirAC->num_freq_bands );
        }
    }
    else if ( params_flag == 2 )
    {
        if ( ( hDirAC->azimuth2 = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        if ( ( hDirAC->elevation2 = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        if ( ( hDirAC->energy_ratio2 = (float **) malloc( hDirAC->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        if ( ( hDirAC->spreadCoherence2 = (float **) malloc( hDirAC->dirac_md_buffer_length * sizeof( float * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
        }

        for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
        {
            if ( ( hDirAC->azimuth2[i] = (int16_t *) malloc( hDirAC->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_s( hDirAC->azimuth2[i], 0, hDirAC->num_freq_bands );

            if ( ( hDirAC->elevation2[i] = (int16_t *) malloc( hDirAC->num_freq_bands * sizeof( int16_t ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_s( hDirAC->elevation2[i], 0, hDirAC->num_freq_bands );

            if ( ( hDirAC->energy_ratio2[i] = (float *) malloc( hDirAC->num_freq_bands * sizeof( float ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_f( hDirAC->energy_ratio2[i], 0.0f, hDirAC->num_freq_bands );

            if ( ( hDirAC->spreadCoherence2[i] = (float *) malloc( hDirAC->num_freq_bands * sizeof( float ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
            }
            set_f( hDirAC->spreadCoherence2[i], 0.0f, hDirAC->num_freq_bands );
        }
    }

    return IVAS_ERR_OK;
}
#endif


/*-------------------------------------------------------------------------
 * ivas_dirac_dec_config()
 *
@@ -281,7 +421,12 @@ ivas_error ivas_dirac_dec_config(
    {
        if ( st_ivas->hDecoderConfig->ivas_total_brate > IVAS_256k && hDirAC->azimuth2 == NULL )
        {
            // TODO: the following block is repeated in several places -> introduce new function?
#ifdef DIRAC_ALLOC_HARM
            if ( ( error = ivas_dirac_allocate_parameters( hDirAC, 2 ) ) != IVAS_ERR_OK )
            {
                return error;
            }
#else
            if ( ( hDirAC->azimuth2 = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
@@ -328,9 +473,13 @@ ivas_error ivas_dirac_dec_config(
                }
                set_f( hDirAC->spreadCoherence2[i], 0.0f, hDirAC->num_freq_bands );
            }
#endif
        }
        else if ( st_ivas->hDecoderConfig->ivas_total_brate <= IVAS_256k && hDirAC->azimuth2 != NULL )
        {
#ifdef DIRAC_ALLOC_HARM
            ivas_dirac_deallocate_parameters( hDirAC, 2 );
#else
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                free( hDirAC->azimuth2[i] );
@@ -348,6 +497,7 @@ ivas_error ivas_dirac_dec_config(
            hDirAC->elevation2 = NULL;
            hDirAC->energy_ratio2 = NULL;
            hDirAC->spreadCoherence2 = NULL;
#endif
        }
    }
#endif
@@ -995,6 +1145,12 @@ ivas_error ivas_dirac_dec_config(
            }
        }

#ifdef DIRAC_ALLOC_HARM
        if ( ( error = ivas_dirac_allocate_parameters( hDirAC, 1 ) ) != IVAS_ERR_OK )
        {
            return error;
        }
#else
        if ( ( hDirAC->azimuth = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
@@ -1063,6 +1219,7 @@ ivas_error ivas_dirac_dec_config(
            }
            set_f( hDirAC->surroundingCoherence[i], 0.0f, hDirAC->num_freq_bands );
        }
#endif

        if ( st_ivas->ivas_format == MASA_FORMAT
#ifdef HODIRAC
@@ -1070,6 +1227,12 @@ ivas_error ivas_dirac_dec_config(
#endif
        )
        {
#ifdef DIRAC_ALLOC_HARM
            if ( ( error = ivas_dirac_allocate_parameters( hDirAC, 2 ) ) != IVAS_ERR_OK )
            {
                return error;
            }
#else
            if ( ( hDirAC->azimuth2 = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
@@ -1116,6 +1279,7 @@ ivas_error ivas_dirac_dec_config(
                }
                set_f( hDirAC->spreadCoherence2[i], 0.0f, hDirAC->num_freq_bands );
            }
#endif
        }
        else
        {
@@ -1139,7 +1303,9 @@ ivas_error ivas_dirac_dec_config(
        {
            if ( ( hDirAC->hConfig->dec_param_estim_old != hDirAC->hConfig->dec_param_estim ) )
            {

#ifdef DIRAC_ALLOC_HARM
                ivas_dirac_deallocate_parameters( hDirAC, 1 );
#else
                for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
                {
                    if ( hDirAC->azimuth[i] != NULL )
@@ -1203,12 +1369,20 @@ ivas_error ivas_dirac_dec_config(
                    free( hDirAC->surroundingCoherence );
                    hDirAC->surroundingCoherence = NULL;
                }
#endif
            }
            hDirAC->dirac_md_buffer_length = ( MAX_PARAM_SPATIAL_SUBFRAMES + DELAY_DIRAC_PARAM_DEC_SFR ) * num_slots_in_subfr;
            hDirAC->dirac_bs_md_write_idx = DELAY_DIRAC_PARAM_DEC_SFR * num_slots_in_subfr;
            hDirAC->spar_to_dirac_write_idx = DELAY_DIRAC_PARAM_DEC_SFR * num_slots_in_subfr;
            hDirAC->dirac_read_idx = 0;
            hDirAC->dirac_estimator_idx = 0;

#ifdef DIRAC_ALLOC_HARM
            if ( ( error = ivas_dirac_allocate_parameters( hDirAC, 1 ) ) != IVAS_ERR_OK )
            {
                return error;
            }
#else
            if ( ( hDirAC->azimuth = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
            {
                return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
@@ -1276,6 +1450,7 @@ ivas_error ivas_dirac_dec_config(
                }
                set_f( hDirAC->surroundingCoherence[i], 0.0f, hDirAC->num_freq_bands );
            }
#endif
        }
    }
#endif
@@ -1380,6 +1555,10 @@ void ivas_dirac_dec_close(
        hDirAC->buffer_energy = NULL;
    }

#ifdef DIRAC_ALLOC_HARM
    ivas_dirac_deallocate_parameters( hDirAC, 1 );
    ivas_dirac_deallocate_parameters( hDirAC, 2 );
#else
    for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
    {
        if ( hDirAC->azimuth[i] != NULL )
@@ -1512,6 +1691,7 @@ void ivas_dirac_dec_close(
        free( hDirAC->surroundingCoherence );
        hDirAC->surroundingCoherence = NULL;
    }
#endif

    if ( hDirAC->masa_stereo_type_detect != NULL )
    {
@@ -1528,6 +1708,173 @@ void ivas_dirac_dec_close(
}


#ifdef DIRAC_ALLOC_HARM
/*-------------------------------------------------------------------------
 * ivas_dirac_deallocate_parameters()
 *
 * Deallocate DirAC parameters
 *-------------------------------------------------------------------------*/

void ivas_dirac_deallocate_parameters(
    DIRAC_DEC_HANDLE hDirAC,  /* i/o: decoder DirAC handle      */
    const int16_t params_flag /* i  : set of parameters flag    */
)
{
    int16_t i;

    if ( params_flag == 1 )
    {
        if ( hDirAC->azimuth != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->azimuth[i] != NULL )
                {
                    free( hDirAC->azimuth[i] );
                    hDirAC->azimuth[i] = NULL;
                }
            }

            free( hDirAC->azimuth );
            hDirAC->azimuth = NULL;
        }

        if ( hDirAC->elevation != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->elevation[i] != NULL )
                {
                    free( hDirAC->elevation[i] );
                    hDirAC->elevation[i] = NULL;
                }
            }

            free( hDirAC->elevation );
            hDirAC->elevation = NULL;
        }

        if ( hDirAC->energy_ratio1 != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->energy_ratio1[i] != NULL )
                {
                    free( hDirAC->energy_ratio1[i] );
                    hDirAC->energy_ratio1[i] = NULL;
                }
            }
            free( hDirAC->energy_ratio1 );
            hDirAC->energy_ratio1 = NULL;
        }

        if ( hDirAC->diffuseness_vector != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->diffuseness_vector[i] != NULL )
                {
                    free( hDirAC->diffuseness_vector[i] );
                    hDirAC->diffuseness_vector[i] = NULL;
                }
            }

            free( hDirAC->diffuseness_vector );
            hDirAC->diffuseness_vector = NULL;
        }

        if ( hDirAC->spreadCoherence != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->spreadCoherence[i] != NULL )
                {
                    free( hDirAC->spreadCoherence[i] );
                    hDirAC->spreadCoherence[i] = NULL;
                }
            }
            free( hDirAC->spreadCoherence );
            hDirAC->spreadCoherence = NULL;
        }

        if ( hDirAC->surroundingCoherence != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->surroundingCoherence[i] != NULL )
                {
                    free( hDirAC->surroundingCoherence[i] );
                    hDirAC->surroundingCoherence[i] = NULL;
                }
            }
            free( hDirAC->surroundingCoherence );
            hDirAC->surroundingCoherence = NULL;
        }
    }
    else if ( params_flag == 2 )
    {
        if ( hDirAC->azimuth2 != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->azimuth2[i] != NULL )
                {
                    free( hDirAC->azimuth2[i] );
                    hDirAC->azimuth2[i] = NULL;
                }
            }
            free( hDirAC->azimuth2 );
            hDirAC->azimuth2 = NULL;
        }

        if ( hDirAC->elevation2 != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->elevation2[i] != NULL )
                {
                    free( hDirAC->elevation2[i] );
                    hDirAC->elevation2[i] = NULL;
                }
            }
            free( hDirAC->elevation2 );
            hDirAC->elevation2 = NULL;
        }

        if ( hDirAC->energy_ratio2 != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->energy_ratio2[i] != NULL )
                {
                    free( hDirAC->energy_ratio2[i] );
                    hDirAC->energy_ratio2[i] = NULL;
                }
            }
            free( hDirAC->energy_ratio2 );
            hDirAC->energy_ratio2 = NULL;
        }

        if ( hDirAC->spreadCoherence2 != NULL )
        {
            for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
            {
                if ( hDirAC->spreadCoherence2[i] != NULL )
                {
                    free( hDirAC->spreadCoherence2[i] );
                    hDirAC->spreadCoherence2[i] = NULL;
                }
            }
            free( hDirAC->spreadCoherence2 );
            hDirAC->spreadCoherence2 = NULL;
        }
    }

    return;
}
#endif


/*-------------------------------------------------------------------------
 * ivas_dirac_alloc_mem()
 *
+18 −0
Original line number Diff line number Diff line
@@ -495,6 +495,18 @@ ivas_error ivas_param_ism_dec_open(
    if ( ( output_config == AUDIO_CONFIG_BINAURAL || output_config == AUDIO_CONFIG_BINAURAL_ROOM ) )
    {
        hDirAC->dirac_md_buffer_length = MAX_PARAM_SPATIAL_SUBFRAMES;

#ifdef DIRAC_ALLOC_HARM
        if ( ( error = ivas_dirac_allocate_parameters( hDirAC, 1 ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        if ( ( error = ivas_dirac_allocate_parameters( hDirAC, 2 ) ) != IVAS_ERR_OK )
        {
            return error;
        }
#else
        if ( ( hDirAC->azimuth = (int16_t **) malloc( hDirAC->dirac_md_buffer_length * sizeof( int16_t * ) ) ) == NULL )
        {
            return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Param ISM Rendering handle\n" ) );
@@ -596,6 +608,7 @@ ivas_error ivas_param_ism_dec_open(
            }
            set_f( hDirAC->surroundingCoherence[i], 0.0f, hDirAC->num_freq_bands );
        }
#endif
    }

    st_ivas->hISMDTX.dtx_flag = 0;
@@ -637,6 +650,10 @@ void ivas_param_ism_dec_close(

    if ( ( output_config == AUDIO_CONFIG_BINAURAL || output_config == AUDIO_CONFIG_BINAURAL_ROOM ) )
    {
#ifdef DIRAC_ALLOC_HARM
        ivas_dirac_deallocate_parameters( hDirAC, 1 );
        ivas_dirac_deallocate_parameters( hDirAC, 2 );
#else
        for ( i = 0; i < hDirAC->dirac_md_buffer_length; i++ )
        {
            if ( hDirAC->azimuth[i] != NULL )
@@ -760,6 +777,7 @@ void ivas_param_ism_dec_close(
            free( hDirAC->surroundingCoherence );
            hDirAC->surroundingCoherence = NULL;
        }
#endif
    }

    if ( !( output_config == AUDIO_CONFIG_MONO || output_config == AUDIO_CONFIG_STEREO ) )