Commit c804584d authored by Archit Tamarapu's avatar Archit Tamarapu
Browse files

first working version of HRTF loading from file for FastConv renderer

parent 3d9c9caf
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -663,7 +663,11 @@ ivas_error ivas_binRenderer_open(
            return error;
        }
        hBinRenderer->hoa_dec_mtx = st_ivas->hoa_dec_mtx;
#ifdef HRTF_BINARY_FASTCONV
        st_ivas->binaural_latency_ns = (int32_t) ( hBinRenderer->hHrtfFastConv->FASTCONV_BRIR_latency_s * 1000000000.f );
#else
        st_ivas->binaural_latency_ns = (int32_t) ( FASTCONV_BRIR_latency_s * 1000000000.f );
#endif
    }
    else
    {
+146 −146
Original line number Diff line number Diff line
@@ -882,25 +882,27 @@ static ivas_error init_fastconv_HRTF_handle(

static ivas_error create_fastconv_HRTF_from_rawdata(
    HRTFS_FASTCONV_HANDLE *hHRTF,         /* i/o: HRTF FastConv handle */
    char *hrtf_data               /* i: pointer to binary file */
    char *hrtf_data,                      /* i  : pointer to binary file */
    RENDERER_TYPE rend_type,              /* i  : Renderer type */
    BINAURAL_INPUT_AUDIO_CONFIG input_cfg /* i  : Input binaural config */
)
{
    int i, j;
    char *hrtf_data_rptr;

    if ( hHRTF == NULL )
    {
        if ( ( ( *hHRTF ) = (HRTFS_FASTCONV_HANDLE) count_malloc( sizeof( HRTFS_FASTCONV ) ) ) == NULL )
        {
            return IVAS_ERROR( IVAS_ERR_UNEXPECTED_NULL_POINTER, "Can not allocate memory for Fastconv HRTF binary\n" );
        }
    // if ( hHRTF == NULL )
    // {
    //     if ( ( ( *hHRTF ) = (HRTFS_FASTCONV_HANDLE) count_malloc( sizeof( HRTFS_FASTCONV ) ) ) == NULL )
    //     {
    //         return IVAS_ERROR( IVAS_ERR_UNEXPECTED_NULL_POINTER, "Can not allocate memory for Fastconv HRTF binary\n" );
    //     }

        init_fastconv_HRTF_handle( *hHRTF );
    }
    else
    {
        return IVAS_ERR_INTERNAL;
    }
    //     init_fastconv_HRTF_handle( *hHRTF );
    // }
    // else
    // {
    //     return IVAS_ERR_INTERNAL;
    // }

    hrtf_data_rptr = hrtf_data;

@@ -912,6 +914,8 @@ static ivas_error create_fastconv_HRTF_from_rawdata(
    hrtf_data_rptr += sizeof( uint16_t );

    // HRIR
    if ( rend_type == RENDERER_BINAURAL_FASTCONV && input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_COMBINED )
    {
        ( *hHRTF )->FASTCONV_HRIR_latency_s = *( (float *) ( hrtf_data_rptr ) );
        hrtf_data_rptr += sizeof( float );

@@ -959,7 +963,9 @@ static ivas_error create_fastconv_HRTF_from_rawdata(
                hrtf_data_rptr += 7 * sizeof( float );
            }
        }

    }
    if ( rend_type == RENDERER_BINAURAL_FASTCONV && input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_HOA )
    {
        // HRIR_HOA3
        ( *hHRTF )->FASTCONV_HOA3_latency_s = *( (float *) ( hrtf_data_rptr ) );
        hrtf_data_rptr += sizeof( float );
@@ -1008,8 +1014,10 @@ static ivas_error create_fastconv_HRTF_from_rawdata(
                hrtf_data_rptr += 7 * sizeof( float );
            }
        }

    }
    // BRIR
    if ( rend_type == RENDERER_BINAURAL_FASTCONV_ROOM && input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_COMBINED )
    {
        ( *hHRTF )->FASTCONV_BRIR_latency_s = *( (float *) ( hrtf_data_rptr ) );
        hrtf_data_rptr += sizeof( float );

@@ -1059,18 +1067,18 @@ static ivas_error create_fastconv_HRTF_from_rawdata(
        }

        // Reverb Parameters
    if ( CLDFB_NO_COL_MAX != *( (uint16_t *) ( hrtf_data_rptr ) ) )
        if ( CLDFB_NO_CHANNELS_MAX != *( (uint16_t *) ( hrtf_data_rptr ) ) )
        {
            return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "HRTF binary file not compliant (CLDFB_NO_COL_MAX)" );
        }
        hrtf_data_rptr += sizeof( uint16_t );

    memcpy( ( *hHRTF )->fastconvReverberationTimes, hrtf_data_rptr, CLDFB_NO_COL_MAX * sizeof( float ) );
    hrtf_data_rptr += CLDFB_NO_COL_MAX * sizeof( float );

    memcpy( ( *hHRTF )->fastconvReverberationEneCorrections, hrtf_data_rptr, CLDFB_NO_COL_MAX * sizeof( float ) );
    hrtf_data_rptr += CLDFB_NO_COL_MAX * sizeof( float );
        memcpy( ( *hHRTF )->fastconvReverberationTimes, hrtf_data_rptr, CLDFB_NO_CHANNELS_MAX * sizeof( float ) );
        hrtf_data_rptr += CLDFB_NO_CHANNELS_MAX * sizeof( float );

        memcpy( ( *hHRTF )->fastconvReverberationEneCorrections, hrtf_data_rptr, CLDFB_NO_CHANNELS_MAX * sizeof( float ) );
        hrtf_data_rptr += CLDFB_NO_CHANNELS_MAX * sizeof( float );
    }
    return IVAS_ERR_OK;
}

@@ -1083,12 +1091,10 @@ ivas_error load_fastconv_HRTF_from_binary(
    int32_t hrtf_data_size_max;
    char *hrtf_data;
    ivas_hrtfs_header_t hrtf_header;
    HRTFS_FASTCONV **hHRTF;
    ivas_error header_check_result;
    ivas_hrtfs_file_header_t hrtfs_file_header;
    int16_t hrtf_id;

    hHRTF = NULL;
    f_hrtf = hrtfReader->file;

    if ( ( header_check_result = read_and_check_hrtf_binary_file_header( &hrtfs_file_header, f_hrtf ) ) != IVAS_ERR_OK )
@@ -1123,7 +1129,6 @@ ivas_error load_fastconv_HRTF_from_binary(
            return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "HRTF binary file not compliant (number of HRTF)" );
        }

        // TODO tmu needs review, probably all covered under one renderer type
        if ( ( hrtf_header.frequency == 48000 ) &&
             ( ( hrtf_header.rend_type == RENDERER_BINAURAL_FASTCONV ) || ( hrtf_header.rend_type == RENDERER_BINAURAL_FASTCONV_ROOM ) ) )
        {
@@ -1133,21 +1138,16 @@ ivas_error load_fastconv_HRTF_from_binary(
                return IVAS_ERROR( IVAS_ERR_FAILED_FILE_READ, "Error in HRTF file reading" );
            }

            if ( hHRTF != NULL )
            {
            // Create the HRTF reading the raw data from the binary file
                if ( ( create_fastconv_HRTF_from_rawdata( hHRTF, hrtf_data ) ) != IVAS_ERR_OK )
            if ( ( create_fastconv_HRTF_from_rawdata( &hHrtfFastConv, hrtf_data, hrtf_header.rend_type, hrtf_header.input_cfg ) ) != IVAS_ERR_OK )
            {
                count_free( hrtf_data );
                return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Could not create HRTF from binary file" );
            }
        }
    }
    }
    count_free( hrtf_data );

    hHrtfFastConv = *hHRTF;

    return IVAS_ERR_OK;
}
#endif
+185 −150
Original line number Diff line number Diff line
@@ -217,7 +217,7 @@ int main( int argc, char *argv[] )
    setOfHRTF = (char **) malloc( nbHRTFMax * sizeof( char * ) );
    memset( setOfHRTF, 0x00, nbHRTFMax * sizeof( char * ) );

    // Create the HRTF for RENDERER_BINAURAL_MIXER_CONV and RENDERER_BINAURAL_MIXER_CONV_ROOM
    // Create the HRTF

    nbHRTF = 0;
    hrtf_size_max = 0;
@@ -750,6 +750,7 @@ char *create_hrtf_fastconv(

            BINAURAL_CONVBANDS => uint16_t

            HRIRs
                HRIR_latency_s => float
                HRTF_LS_CHANNELS => uint16_t
                ntaps => int16_t
@@ -758,6 +759,7 @@ char *create_hrtf_fastconv(
                rightHRIRReal => float[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][num_taps]
                rightHRIRImag => float[BINAURAL_CONVBANDS][HRTF_LS_CHANNELS][num_taps]

            HOA3 HRIRs
                HRIR_HOA3_latency_s => float
                HRTF_SH_CHANNELS => uint16_t
                ntaps => int16_t
@@ -766,6 +768,7 @@ char *create_hrtf_fastconv(
                rightHRIRReal_HOA3 => float[BINAURAL_CONVBANDS][HRTF_SH_CHANNELS][num_taps]
                rightHRIRImag_HOA3 => float[BINAURAL_CONVBANDS][HRTF_SH_CHANNELS][num_taps]

            BRIRs
                BRIR_latency_s => float
                HRTF_LS_CHANNELS => uint16_t
                ntaps => int16_t
@@ -780,21 +783,57 @@ char *create_hrtf_fastconv(
                fastConvReverberationEneCorrection => float[CLDFB_NO_CHANNELS_MAX]
*/

    // Set table sizes
    binaural_convbands = BINAURAL_CONVBANDS;

    // Compute total size of data to write
    if ( input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_COMBINED )
    {
        if ( rend_type == RENDERER_BINAURAL_FASTCONV )
        {
            latency_s = FASTCONV_HRIR_latency_s;
            hrtf_channels = HRTF_LS_CHANNELS;
            num_taps = 7;
        }
        else if ( rend_type == RENDERER_BINAURAL_FASTCONV_ROOM )
        {
            latency_s = FASTCONV_BRIR_latency_s;
            hrtf_channels = HRTF_LS_CHANNELS;
            num_taps = BINAURAL_NTAPS_MAX;
            cldfb_nchan_max = CLDFB_NO_CHANNELS_MAX;
        }
    }
    else if ( input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_HOA )
    {
        if ( rend_type == RENDERER_BINAURAL_FASTCONV_ROOM )
        {
            /* No HOA3 BRIRs */
            return NULL;
        }

        latency_s = FASTCONV_HOA3_latency_s;
        hrtf_channels = HRTF_SH_CHANNELS;
        num_taps = 7;
    }
    else
    {
        fprintf( stderr, "Unsupported renderer type in create_hrtf_fastconv()\n\n" );
        return NULL;
    }

    // Compute total size of data to write
    hrtf_data_size += sizeof( uint16_t ); // BINAURAL_CONVBANDS

    hrtf_data_size += 3 * sizeof( float );                                                                  // latency_s
    hrtf_data_size += 3 * sizeof( uint16_t );                                                               // HRTF_{SH,LS}_CHANNELS
    hrtf_data_size += 3 * sizeof( uint16_t );                                                               // num_taps
    hrtf_data_size += 4 * ( BINAURAL_CONVBANDS * HRTF_LS_CHANNELS * 7 * sizeof( float ) );                  // HRIR
    hrtf_data_size += 4 * ( BINAURAL_CONVBANDS * HRTF_SH_CHANNELS * 7 * sizeof( float ) );                  // HRIR_HOA3
    hrtf_data_size += 4 * ( BINAURAL_CONVBANDS * HRTF_LS_CHANNELS * BINAURAL_NTAPS_MAX * sizeof( float ) ); // BRIR
    hrtf_data_size += sizeof( float );                                                         // latency_s
    hrtf_data_size += sizeof( uint16_t );                                                      // HRTF_{SH,LS}_CHANNELS
    hrtf_data_size += sizeof( uint16_t );                                                      // num_taps
    hrtf_data_size += 4 * ( BINAURAL_CONVBANDS * hrtf_channels * num_taps * sizeof( float ) ); // HRTF

    if ( rend_type == RENDERER_BINAURAL_FASTCONV_ROOM && input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_COMBINED )
    {
        hrtf_data_size += sizeof( uint16_t );                      // CLDFB_NO_CHANNELS_MAX
        hrtf_data_size += CLDFB_NO_CHANNELS_MAX * sizeof( float ); // fastconvReverberationTimes
        hrtf_data_size += CLDFB_NO_CHANNELS_MAX * sizeof( float ); // fastconvEneCorrections
    }

    // Allocate memory
    *hrtf_size = sizeof( ivas_hrtfs_header_t ) + hrtf_data_size;
@@ -836,28 +875,26 @@ char *create_hrtf_fastconv(


    // Write the HRTF raw data
    binaural_convbands = BINAURAL_CONVBANDS;
    memcpy( hrtf_wptr, &( binaural_convbands ), sizeof( uint16_t ) ); // nbands_bin => uint16_t
    hrtf_wptr += sizeof( uint16_t );


    // HRIR
    latency_s = FASTCONV_HRIR_latency_s;
    if ( rend_type == RENDERER_BINAURAL_FASTCONV && input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_COMBINED )
    {
        memcpy( hrtf_wptr, &( latency_s ), sizeof( float ) ); // latency_s => float
        hrtf_wptr += sizeof( float );

    hrtf_channels = HRTF_LS_CHANNELS;
        memcpy( hrtf_wptr, &( hrtf_channels ), sizeof( uint16_t ) ); // hrtf_channels => uint16_t
        hrtf_wptr += sizeof( uint16_t );

    num_taps = 7;
        memcpy( hrtf_wptr, &( num_taps ), sizeof( uint16_t ) ); // num_taps => uint16_t
        hrtf_wptr += sizeof( uint16_t );

        data_size_tmp = num_taps * sizeof( float );
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_LS_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &leftHRIRReal[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -865,7 +902,7 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_LS_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &leftHRIRImag[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -873,7 +910,7 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_LS_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &rightHRIRReal[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -881,30 +918,29 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_LS_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &rightHRIRImag[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
            }
        }

    }
    // HRIR_HOA3
    latency_s = FASTCONV_HOA3_latency_s;
    else if ( rend_type == RENDERER_BINAURAL_FASTCONV && input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_HOA )
    {
        memcpy( hrtf_wptr, &( latency_s ), sizeof( float ) ); // latency_s => float
        hrtf_wptr += sizeof( float );

    hrtf_channels = HRTF_SH_CHANNELS;
        memcpy( hrtf_wptr, &( hrtf_channels ), sizeof( uint16_t ) ); // hrtf_channels => uint16_t
        hrtf_wptr += sizeof( uint16_t );

    num_taps = 7;
        memcpy( hrtf_wptr, &( num_taps ), sizeof( uint16_t ) ); // num_taps => uint16_t
        hrtf_wptr += sizeof( uint16_t );

        data_size_tmp = num_taps * sizeof( float );
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_SH_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &leftHRIRReal_HOA3[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -912,7 +948,7 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_SH_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &leftHRIRImag_HOA3[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -920,7 +956,7 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_SH_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &rightHRIRReal_HOA3[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -928,30 +964,29 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_SH_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &rightHRIRImag_HOA3[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
            }
        }

    }
    // BRIR
    latency_s = FASTCONV_BRIR_latency_s;
    else if ( rend_type == RENDERER_BINAURAL_FASTCONV_ROOM && input_cfg == BINAURAL_INPUT_AUDIO_CONFIG_COMBINED )
    {
        memcpy( hrtf_wptr, &( latency_s ), sizeof( float ) ); // latency_s => float
        hrtf_wptr += sizeof( float );

    hrtf_channels = HRTF_LS_CHANNELS;
        memcpy( hrtf_wptr, &( hrtf_channels ), sizeof( uint16_t ) ); // hrtf_channels => uint16_t
        hrtf_wptr += sizeof( uint16_t );

    num_taps = BINAURAL_NTAPS_MAX;
        memcpy( hrtf_wptr, &( num_taps ), sizeof( uint16_t ) ); // num_taps => uint16_t
        hrtf_wptr += sizeof( uint16_t );

        data_size_tmp = num_taps * sizeof( float );
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_LS_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, leftBRIRReal[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -959,7 +994,7 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_LS_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &leftBRIRImag[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -967,7 +1002,7 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_LS_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &rightBRIRReal[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -975,7 +1010,7 @@ char *create_hrtf_fastconv(
        }
        for ( i = 0; i < BINAURAL_CONVBANDS; i++ )
        {
        for ( j = 0; j < HRTF_LS_CHANNELS; j++ )
            for ( j = 0; j < hrtf_channels; j++ )
            {
                memcpy( hrtf_wptr, &rightBRIRImag[i][j], data_size_tmp );
                hrtf_wptr += data_size_tmp;
@@ -983,13 +1018,13 @@ char *create_hrtf_fastconv(
        }

        // Reverb Parameters
    cldfb_nchan_max = CLDFB_NO_CHANNELS_MAX;
        memcpy( hrtf_wptr, &( cldfb_nchan_max ), sizeof( uint16_t ) ); // cldfb_ncol => int16_t
        hrtf_wptr += sizeof( uint16_t );

        data_size_tmp = cldfb_nchan_max * sizeof( float );
        memcpy( hrtf_wptr, &( fastconvReverberationTimes ), data_size_tmp );          // fastconvReverberationTimes
        memcpy( hrtf_wptr, &( fastconvReverberationEneCorrections ), data_size_tmp ); // fastconvEneCorrections
    }

    return hrtf;
}