Commit f7760733 authored by vaclav's avatar vaclav
Browse files

fix memory issue when HRTFs are loaded from a binary file; under FIX_HRTF_LOAD

parent e51fb5bf
Loading
Loading
Loading
Loading
Loading
+262 −0
Original line number Diff line number Diff line
@@ -161,6 +161,23 @@ typedef struct

} DecArguments;

#ifdef FIX_HRTF_LOAD

typedef struct
{
    hrtfFileReader *hrtfReader;
    char *hrtfFileName;

    IVAS_DEC_HRTF_HANDLE *hHrtfTD;
    IVAS_DEC_HRTF_CREND_HANDLE *hSetOfHRTF;
    IVAS_DEC_HRTF_STATISTICS_HANDLE *hHrtfStatistics;

    ISAR_SPLIT_REND_RENDERER_SELECTION binaural_renderer;
    ISAR_SPLIT_REND_RENDERER_SELECTION binaural_renderer_old;

} IVAS_DEC_HRTF_WRAPPER;

#endif

/*------------------------------------------------------------------------------------------*
 * Local functions prototypes
@@ -168,8 +185,15 @@ typedef struct

static bool parseCmdlIVAS_dec( int16_t argc, char **argv, DecArguments *arg );
static void usage_dec( void );
#ifdef FIX_HRTF_LOAD
static ivas_error decodeG192( DecArguments arg, BS_READER_HANDLE hBsReader, IVAS_DEC_HRTF_WRAPPER *hHrtf, RotFileReader *headRotReader, RotFileReader *externalOrientationFileReader, RotFileReader *refRotReader, Vector3PairFileReader *referenceVectorReader, ISAR_SPLIT_REND_BITS_DATA *splitRendBits, IVAS_DEC_HANDLE hIvasDec, int16_t *pcmBuf );
#else
static ivas_error decodeG192( DecArguments arg, BS_READER_HANDLE hBsReader, RotFileReader *headRotReader, RotFileReader *externalOrientationFileReader, RotFileReader *refRotReader, Vector3PairFileReader *referenceVectorReader, ISAR_SPLIT_REND_BITS_DATA *splitRendBits, IVAS_DEC_HANDLE hIvasDec, int16_t *pcmBuf );
#endif
static ivas_error decodeVoIP( DecArguments arg, BS_READER_HANDLE hBsReader, RotFileReader *headRotReader, RotFileReader *externalOrientationFileReader, RotFileReader *refRotReader, Vector3PairFileReader *referenceVectorReader, IVAS_DEC_HANDLE hIvasDec );
#ifdef FIX_HRTF_LOAD
static ivas_error IVAS_DEC_LoadHrtfFromFile( IVAS_DEC_HRTF_WRAPPER *hHrtf, IVAS_DEC_HANDLE hIvasDec, const int32_t output_Fs );
#endif

#ifdef DEBUGGING
static ivas_error printBitstreamInfoVoip( DecArguments arg, BS_READER_HANDLE hBsReader, IVAS_DEC_HANDLE hIvasDec );
@@ -205,9 +229,13 @@ int main(
    RenderConfigReader *renderConfigReader = NULL;
    int16_t *pcmBuf = NULL;
    IVAS_RENDER_FRAMESIZE asked_frame_size;
#ifdef FIX_HRTF_LOAD
    IVAS_DEC_HRTF_WRAPPER hHrtf;
#else
    IVAS_DEC_HRTF_HANDLE *hHrtfTD = NULL;
    IVAS_DEC_HRTF_CREND_HANDLE *hSetOfHRTF = NULL;
    IVAS_DEC_HRTF_STATISTICS_HANDLE *hHrtfStatistics = NULL;
#endif
#ifdef DEBUGGING
    int32_t noClipping;
    int32_t cnt_frames_limited;
@@ -224,6 +252,13 @@ int main(
    reset_mem( USE_BYTES );
#endif

#ifdef FIX_HRTF_LOAD
    hHrtf.binaural_renderer = ISAR_SPLIT_REND_RENDERER_SELECTION_NONE;
    hHrtf.hHrtfTD = NULL;         /* just to avoid compilation warning */
    hHrtf.hSetOfHRTF = NULL;      /* just to avoid compilation warning */
    hHrtf.hHrtfStatistics = NULL; /* just to avoid compilation warning */
#endif

    splitRendBits.bits_buf = splitRendBitsBuf;

    /*------------------------------------------------------------------------------------------*
@@ -721,6 +756,21 @@ int main(

    if ( arg.hrtfReaderEnabled )
    {
#ifdef FIX_HRTF_LOAD
        hHrtf.hrtfReader = hrtfReader;
        hHrtf.hrtfFileName = arg.hrtfFileName;

        if ( arg.voipMode )
        {
            hHrtf.binaural_renderer = ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT;
            hHrtf.binaural_renderer_old = ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT;
            if ( ( error = IVAS_DEC_LoadHrtfFromFile( &hHrtf, hIvasDec, arg.output_Fs ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_DEC_LoadHrtfFromFile failed: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
                goto cleanup;
            }
        }
#else
        if ( ( error = IVAS_DEC_GetHrtfHandle( hIvasDec, &hHrtfTD ) ) != IVAS_ERR_OK )
        {
            fprintf( stderr, "\nIVAS_DEC_GetHrtfHandle failed: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
@@ -817,6 +867,7 @@ int main(
                destroy_hrtf_statistics( hHrtfStatistics );
            }
        }
#endif
    }

    /*------------------------------------------------------------------------------------------*
@@ -842,7 +893,11 @@ int main(
    }
    else
    {
#ifdef FIX_HRTF_LOAD
        error = decodeG192( arg, hBsReader, &hHrtf, headRotReader, externalOrientationFileReader, refRotReader, referenceVectorReader, &splitRendBits, hIvasDec, pcmBuf );
#else
        error = decodeG192( arg, hBsReader, headRotReader, externalOrientationFileReader, refRotReader, referenceVectorReader, &splitRendBits, hIvasDec, pcmBuf );
#endif
    }

    if ( error == IVAS_ERR_OK || error == IVAS_ERR_END_OF_FILE )
@@ -894,6 +949,11 @@ cleanup:
#endif
    if ( arg.hrtfReaderEnabled )
    {
#ifdef FIX_HRTF_LOAD
        destroy_td_hrtf( hHrtf.hHrtfTD );
        destroy_SetOfHRTF( hHrtf.hSetOfHRTF );
        destroy_hrtf_statistics( hHrtf.hHrtfStatistics );
#else
        IVAS_DEC_GetHrtfHandle( hIvasDec, &hHrtfTD );
        destroy_td_hrtf( hHrtfTD );

@@ -902,6 +962,7 @@ cleanup:

        IVAS_DEC_GetHrtfStatisticsHandle( hIvasDec, &hHrtfStatistics );
        destroy_hrtf_statistics( hHrtfStatistics );
#endif
    }

    IVAS_DEC_Close( &hIvasDec );
@@ -2094,6 +2155,9 @@ static ivas_error initOnFirstGoodFrame(
static ivas_error decodeG192(
    DecArguments arg,
    BS_READER_HANDLE hBsReader,
#ifdef FIX_HRTF_LOAD
    IVAS_DEC_HRTF_WRAPPER *hHrtf,
#endif
    RotFileReader *headRotReader,
    RotFileReader *externalOrientationFileReader,
    RotFileReader *refRotReader,
@@ -2465,6 +2529,34 @@ static ivas_error decodeG192(
                    fprintf( stderr, "\nError: could not feed frame to decoder: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
                    goto cleanup;
                }

#ifdef FIX_HRTF_LOAD
                /* Read main parameters from the bitstream to set-up the decoder */
                hHrtf->binaural_renderer_old = hHrtf->binaural_renderer;
                if ( ( error = IVAS_DEC_ReadFormat( hIvasDec, &hHrtf->binaural_renderer ) ) != IVAS_ERR_OK )
                {
                    return error;
                }

                /* Placeholder for memory reallocation */

                /* Load HRTF binary file data */
                if ( arg.hrtfReaderEnabled )
                {
                    if ( ( error = IVAS_DEC_LoadHrtfFromFile( hHrtf, hIvasDec, arg.output_Fs ) ) != IVAS_ERR_OK )
                    {
                        fprintf( stderr, "\nIVAS_DEC_LoadHrtfFromFile failed: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
                        goto cleanup;
                    }
                }

                /* decode transport channels, do TSM and feed to renderer */
                if ( ( error = IVAS_DEC_GetSamplesDecoder( hIvasDec, isSplitRend, splitRendBits ) ) != IVAS_ERR_OK )
                {
                    return error;
                }

#endif
#ifdef OBJ_EDITING_API
#ifdef OBJ_EDITING_EXAMPLE
                if ( arg.objEditEnabled )
@@ -3705,4 +3797,174 @@ static IVAS_DEC_FORCED_REND_MODE parseForcedRendModeDec(
}
#endif

#ifdef FIX_HRTF_LOAD

/*---------------------------------------------------------------------*
 * IVAS_DEC_LoadHrtfFromFile()
 *
 *
 *---------------------------------------------------------------------*/

static ivas_error IVAS_DEC_LoadHrtfFromFile(
    IVAS_DEC_HRTF_WRAPPER *hHrtf,
    IVAS_DEC_HANDLE hIvasDec,
    const int32_t output_Fs )
{
    ivas_error error;
    ISAR_SPLIT_REND_RENDERER_SELECTION binaural_renderer;

    if ( hHrtf == NULL || hIvasDec == NULL )
    {
        return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    }

    binaural_renderer = hHrtf->binaural_renderer;

    if ( ( binaural_renderer != hHrtf->binaural_renderer_old ) || ( binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT ) )
    {
        /*------------------------------------------------------------------------------------------*
         * Release HRTF binary data
         *------------------------------------------------------------------------------------------*/

        if ( !( binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_TDREND || binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT ) )
        {
            destroy_td_hrtf( hHrtf->hHrtfTD );
        }

        if ( !( binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_CREND || binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT ) )
        {
            destroy_SetOfHRTF( hHrtf->hSetOfHRTF );
        }

        if ( ( error = IVAS_DEC_HRTF_binary_close( hIvasDec, hHrtf->binaural_renderer_old ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        /*------------------------------------------------------------------------------------------*
         * Load HRTF binary data
         *------------------------------------------------------------------------------------------*/

        if ( ( error = IVAS_DEC_HRTF_binary_open( hIvasDec, binaural_renderer ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        if ( binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_TDREND || binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT )
        {
            if ( ( error = IVAS_DEC_GetHrtfHandle( hIvasDec, &hHrtf->hHrtfTD ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_DEC_GetHrtfHandle failed: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
                return error;
            }

            if ( ( error = load_HRTF_binary( *hHrtf->hHrtfTD, hHrtf->hrtfReader ) ) != IVAS_ERR_OK )
            {
                if ( error != IVAS_ERR_BINARY_FILE_WITHOUT_BINAURAL_RENDERER_DATA )
                {
                    fprintf( stderr, "\nError in loading HRTF binary file %s \n\n", hHrtf->hrtfFileName );
                    return error;
                }
                else
                {
                    destroy_td_hrtf( hHrtf->hHrtfTD );
                }
            }
        }

        if ( binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_CREND || binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT )
        {
            if ( ( error = IVAS_DEC_GetHrtfCRendHandle( hIvasDec, &hHrtf->hSetOfHRTF ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_DEC_GetHrtfCRendHandle failed: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
                return error;
            }

            if ( ( error = create_SetOfHRTF_from_binary( *hHrtf->hSetOfHRTF, hHrtf->hrtfReader, output_Fs ) ) != IVAS_ERR_OK )
            {
                if ( error != IVAS_ERR_BINARY_FILE_WITHOUT_BINAURAL_RENDERER_DATA )
                {
                    fprintf( stderr, "\nError in loading HRTF binary file %s \n\n", hHrtf->hrtfFileName );
                    return error;
                }
                else
                {
                    destroy_SetOfHRTF( hHrtf->hSetOfHRTF );
                }
            }
        }

        if ( binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_FASTCONV || binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT )
        {
            IVAS_DEC_HRTF_FASTCONV_HANDLE *hHrtfFastConv = NULL;
            if ( ( error = IVAS_DEC_GetHrtfFastConvHandle( hIvasDec, &hHrtfFastConv ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_DEC_GetHrtfFastConvHandle failed: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
                return error;
            }

            if ( ( error = load_fastconv_HRTF_from_binary( *hHrtfFastConv, hHrtf->hrtfReader ) ) != IVAS_ERR_OK )
            {
                if ( error != IVAS_ERR_BINARY_FILE_WITHOUT_BINAURAL_RENDERER_DATA )
                {
                    fprintf( stderr, "\nError in loading HRTF binary file %s \n\n", hHrtf->hrtfFileName );
                    return error;
                }
                else
                {
                    destroy_fastconv_hrtf( hHrtfFastConv );
                }
            }
        }

        if ( binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_PARAMBIN || binaural_renderer == ISAR_SPLIT_REND_RENDERER_SELECTION_DEFAULT )
        {
            IVAS_DEC_HRTF_PARAMBIN_HANDLE *hHrtfParambin = NULL;
            if ( ( error = IVAS_DEC_GetHrtfParamBinHandle( hIvasDec, &hHrtfParambin ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_DEC_GetHrtfParamBinHandle failed: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
                return error;
            }

            if ( ( error = load_parambin_HRTF_from_binary( *hHrtfParambin, hHrtf->hrtfReader ) ) != IVAS_ERR_OK )
            {
                if ( error != IVAS_ERR_BINARY_FILE_WITHOUT_BINAURAL_RENDERER_DATA )
                {
                    fprintf( stderr, "\nError in loading HRTF binary file %s \n\n", hHrtf->hrtfFileName );
                    return error;
                }
                else
                {
                    destroy_parambin_hrtf( hHrtfParambin );
                }
            }
        }
    }

    if ( hHrtf->hHrtfStatistics == NULL )
    {
        if ( ( error = IVAS_DEC_GetHrtfStatisticsHandle( hIvasDec, &hHrtf->hHrtfStatistics ) ) != IVAS_ERR_OK )
        {
            fprintf( stderr, "\nIVAS_DEC_GetHrtfHandle failed: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
            return error;
        }

        if ( ( error = load_reverb_binary( *hHrtf->hHrtfStatistics, output_Fs, hHrtf->hrtfReader ) ) != IVAS_ERR_OK )
        {
            if ( error != IVAS_ERR_BINARY_FILE_WITHOUT_BINAURAL_RENDERER_DATA )
            {
                fprintf( stderr, "\nError in loading HRTF binary file %s \n\n", hHrtf->hrtfFileName );
                return error;
            }
            else
            {
                destroy_hrtf_statistics( hHrtf->hHrtfStatistics );
            }
        }
    }

    return IVAS_ERR_OK;
}
#endif

#undef WMC_TOOL_SKIP
+3 −0
Original line number Diff line number Diff line
@@ -253,6 +253,9 @@ typedef enum

typedef enum
{
#ifdef FIX_HRTF_LOAD // VE: rename the enum -> IVAS_RENDERER_xx
    ISAR_SPLIT_REND_RENDERER_SELECTION_NONE,
#endif
    ISAR_SPLIT_REND_RENDERER_SELECTION_CREND,
    ISAR_SPLIT_REND_RENDERER_SELECTION_FASTCONV,
    ISAR_SPLIT_REND_RENDERER_SELECTION_PARAMBIN,
+6 −0
Original line number Diff line number Diff line
@@ -318,6 +318,12 @@ ivas_error ivas_dec(
    int16_t *data                                               /* o  : output synthesis signal                 */
);

#ifdef FIX_HRTF_LOAD
ivas_error ivas_dec_get_format(
    Decoder_Struct *st_ivas                                     /* i/o: IVAS decoder structure                  */
);

#endif
ivas_error ivas_dec_setup(
    Decoder_Struct *st_ivas,                                    /* i/o: IVAS decoder structure                  */
    uint16_t *nSamplesRendered,                                 /* o  : number of samples flushed from the previous frame (JBM) */
+2 −0
Original line number Diff line number Diff line
@@ -171,6 +171,8 @@

#define FIX_1158_FASTCONV_REVERB_HRTF                   /* Philips: issue 1158: Rendering with FastConv to BINAURAL_ROOM_REVERB uses BRIR convolution instead of HRTF */

#define FIX_HRTF_LOAD   /* VA: fix memory issue when HRTFs are loaded from a binary file */

/* #################### End BE switches ################################## */

/* #################### Start NON-BE switches ############################ */
+275 −0

File changed.

Preview size limit exceeded, changes collapsed.

Loading