Commit 2d97881f authored by emerit's avatar emerit
Browse files

fix hrtf binary loading with decoder

parent 1bde2346
Loading
Loading
Loading
Loading
+77 −9
Original line number Diff line number Diff line
@@ -179,10 +179,13 @@ typedef struct

static bool parseCmdlIVAS_dec( int16_t argc, char **argv, DecArguments *arg );
static void usage_dec( void );
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 );
static ivas_error decodeVoIP( DecArguments arg, BS_READER_HANDLE hBsReader, RotFileReader *headRotReader, RotFileReader *externalOrientationFileReader, RotFileReader *refRotReader, Vector3PairFileReader *referenceVectorReader, IVAS_DEC_HANDLE hIvasDec );
#ifdef FIX_CREND_SIMPLIFY_CODE
static ivas_error decodeG192( DecArguments arg, BS_READER_HANDLE hBsReader, IVAS_DEC_HRTF_BINARY_WRAPPER *hHrtfBinary, RotFileReader *headRotReader, RotFileReader *externalOrientationFileReader, RotFileReader *refRotReader, Vector3PairFileReader *referenceVectorReader, ISAR_SPLIT_REND_BITS_DATA *splitRendBits, IVAS_DEC_HANDLE hIvasDec, int16_t *pcmBuf );
static ivas_error decodeVoIP( DecArguments arg, BS_READER_HANDLE hBsReader, IVAS_DEC_HRTF_BINARY_WRAPPER *hHrtfBinary, RotFileReader *headRotReader, RotFileReader *externalOrientationFileReader, RotFileReader *refRotReader, Vector3PairFileReader *referenceVectorReader, IVAS_DEC_HANDLE hIvasDec );
static ivas_error load_hrtf_from_file( IVAS_DEC_HRTF_BINARY_WRAPPER *hHrtfBinary, IVAS_DEC_HANDLE hIvasDec, const IVAS_AUDIO_CONFIG OutputConfig, const int32_t output_Fs );
#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 );
static ivas_error decodeVoIP( DecArguments arg, BS_READER_HANDLE hBsReader, RotFileReader *headRotReader, RotFileReader *externalOrientationFileReader, RotFileReader *refRotReader, Vector3PairFileReader *referenceVectorReader, IVAS_DEC_HANDLE hIvasDec );
#endif
#ifdef DEBUGGING
static ivas_error printBitstreamInfoVoip( DecArguments arg, BS_READER_HANDLE hBsReader, IVAS_DEC_HANDLE hIvasDec );
@@ -731,12 +734,6 @@ int main(
        hHrtfBinary.binaural_renderer = IVAS_BIN_RENDERER_TYPE_NONE;
        hHrtfBinary.binaural_renderer_sec = IVAS_BIN_RENDERER_TYPE_NONE;
        hHrtfBinary.hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_INVALID;

        if ( ( error = load_hrtf_from_file( &hHrtfBinary, hIvasDec, arg.outputConfig, 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 )
        {
@@ -863,11 +860,19 @@ int main(

    if ( arg.voipMode )
    {
#ifdef FIX_CREND_SIMPLIFY_CODE
        error = decodeVoIP( arg, hBsReader, &hHrtfBinary, headRotReader, externalOrientationFileReader, refRotReader, referenceVectorReader, hIvasDec );
#else
        error = decodeVoIP( arg, hBsReader, headRotReader, externalOrientationFileReader, refRotReader, referenceVectorReader, hIvasDec );
#endif
    }
    else
    {
#ifdef FIX_CREND_SIMPLIFY_CODE
        error = decodeG192( arg, hBsReader, &hHrtfBinary, 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 )
@@ -2159,6 +2164,9 @@ static ivas_error initOnFirstGoodFrame(
static ivas_error decodeG192(
    DecArguments arg,
    BS_READER_HANDLE hBsReader,
#ifdef FIX_CREND_SIMPLIFY_CODE
    IVAS_DEC_HRTF_BINARY_WRAPPER *hHrtfBinary,
#endif
    RotFileReader *headRotReader,
    RotFileReader *externalOrientationFileReader,
    RotFileReader *refRotReader,
@@ -2518,6 +2526,28 @@ static ivas_error decodeG192(
                    fprintf( stderr, "\nError: could not feed frame to decoder: %s\n\n", IVAS_DEC_GetErrorMessage( error ) );
                    goto cleanup;
                }
#ifdef FIX_CREND_SIMPLIFY_CODE
                /* Read main parameters from the bitstream to set-up the decoder */
                hHrtfBinary->binaural_renderer_old = hHrtfBinary->binaural_renderer;
                hHrtfBinary->binaural_renderer_sec_old = hHrtfBinary->binaural_renderer_sec;
                if ( ( error = IVAS_DEC_ReadFormat( hIvasDec, &hHrtfBinary->binaural_renderer, &hHrtfBinary->binaural_renderer_sec, &hHrtfBinary->hrtf_set_audio_cfg ) ) != IVAS_ERR_OK )
                {
                    return error;
                }

                /* Placeholder for memory reallocation */
                /* ... */

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

            if ( isSplitRend )
@@ -3020,6 +3050,9 @@ static ivas_error writeJbmTraceFileFrameWrapper( const void *data, void *writer
static ivas_error decodeVoIP(
    DecArguments arg,
    BS_READER_HANDLE hBsReader,
#ifdef FIX_CREND_SIMPLIFY_CODE
    IVAS_DEC_HRTF_BINARY_WRAPPER *hHrtf,
#endif
    RotFileReader *headRotReader,
    RotFileReader *externalOrientationFileReader,
    RotFileReader *refRotReader,
@@ -3069,7 +3102,9 @@ static ivas_error decodeVoIP(
    IVAS_VECTOR3 Pos[IVAS_MAX_PARAM_SPATIAL_SUBFRAMES] = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
    int16_t vec_pos_update, vec_pos_len;
    int16_t nOutSamples = 0;

#ifdef FIX_CREND_SIMPLIFY_CODE
    bool bitstreamReadDone = false;
#endif
    vec_pos_update = 0;
    if ( ( error = IVAS_DEC_GetRenderFramesizeMs( hIvasDec, &systemTimeInc_ms ) ) != IVAS_ERR_OK )
    {
@@ -3351,14 +3386,47 @@ static ivas_error decodeVoIP(
        /* decode and get samples */

#ifdef SUPPORT_JBM_TRACEFILE
#ifdef FIX_CREND_SIMPLIFY_CODE
        if ( ( error = IVAS_DEC_VoIP_GetSamples( hIvasDec, nOutSamples, IVAS_DEC_PCM_INT16, (void *) pcmBuf, systemTime_ms, writeJbmTraceFileFrameWrapper, jbmTraceWriter, &bitstreamReadDone ) ) != IVAS_ERR_OK )
#else
        if ( ( error = IVAS_DEC_VoIP_GetSamples( hIvasDec, nOutSamples, IVAS_DEC_PCM_INT16, (void *) pcmBuf, systemTime_ms, writeJbmTraceFileFrameWrapper, jbmTraceWriter ) ) != IVAS_ERR_OK )
#endif
#else
#ifdef FIX_CREND_SIMPLIFY_CODE
        if ( ( error = IVAS_DEC_VoIP_GetSamples( hIvasDec, nOutSamples, IVAS_DEC_PCM_INT16, (void *) pcmBuf, systemTime_ms, &bitstreamReadDone ) ) != IVAS_ERR_OK )
#else
        if ( ( error = IVAS_DEC_VoIP_GetSamples( hIvasDec, nOutSamples, IVAS_DEC_PCM_INT16, (void *) pcmBuf, systemTime_ms ) ) != IVAS_ERR_OK )
#endif
#endif
        {
            fprintf( stderr, "\nError in IVAS_DEC_VoIP_GetSamples: %s\n", IVAS_DEC_GetErrorMessage( error ) );
            goto cleanup;
        }
#ifdef FIX_CREND_SIMPLIFY_CODE
        if ( bitstreamReadDone == true )
        {
            /* Read main parameters from the bitstream to set-up the decoder */
            hHrtf->binaural_renderer_old = hHrtf->binaural_renderer;
            hHrtf->binaural_renderer_sec_old = hHrtf->binaural_renderer_sec;
            if ( ( error = IVAS_DEC_ReadFormat( hIvasDec, &hHrtf->binaural_renderer, &hHrtf->binaural_renderer_sec, &hHrtf->hrtf_set_audio_cfg ) ) != IVAS_ERR_OK )
            {
                return error;
            }

            /* Placeholder for memory reallocation */
            /* ... */

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

        /* write JBM Offset file entry */
        if ( jbmOffsetWriter != NULL )
+17 −0
Original line number Diff line number Diff line
@@ -971,6 +971,11 @@ int main(
            }

#ifdef FIX_CREND_SIMPLIFY_CODE
            if ( ( error = IVAS_REND_GetHrtfCRendHandle( hIvasRend, &hHrtfCrend ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_Rend_GetHrtfCRendHandle failed: %s\n\n", ivas_error_to_string( error ) );
                goto cleanup;
            }
            IVAS_AUDIO_CONFIG hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
            if ( args.inConfig.ambisonicsBuses->audioConfig != IVAS_AUDIO_CONFIG_INVALID && args.outConfig.audioConfig != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
            {
@@ -2618,6 +2623,9 @@ static CmdlnArgs defaultArgs(
    const char *executableName )
{
    CmdlnArgs args;
#ifdef FIX_CREND_SIMPLIFY_CODE
    int16_t i;
#endif

    strncpy( args.executableName, executableName, RENDERER_MAX_CLI_ARG_LENGTH );
    clearString( args.inputFilePath );
@@ -2634,8 +2642,13 @@ static CmdlnArgs defaultArgs(
    args.outConfig.audioConfig = IVAS_AUDIO_CONFIG_INVALID;
    args.outConfig.outSetupCustom.num_spk = 0;
    args.outConfig.outSetupCustom.num_lfe = 0;
    args.inConfig.ambisonicsBuses->audioConfig = IVAS_AUDIO_CONFIG_INVALID;

#ifdef FIX_CREND_SIMPLIFY_CODE
    for ( i = 0; i < RENDERER_MAX_ISM_INPUTS + RENDERER_MAX_MASA_INPUTS; ++i )
#else
    for ( int32_t i = 0; i < RENDERER_MAX_ISM_INPUTS; ++i )
#endif
    {
        clearString( args.inMetadataFilePaths[i] );
    }
@@ -2673,7 +2686,11 @@ static CmdlnArgs defaultArgs(
    args.render_framesize = IVAS_RENDER_FRAMESIZE_20MS;
    args.syncMdDelay = 0;

#ifdef FIX_CREND_SIMPLIFY_CODE
    for ( i = 0; i < RENDERER_MAX_ISM_INPUTS; ++i )
#else
    for ( int32_t i = 0; i < RENDERER_MAX_ISM_INPUTS; ++i )
#endif
    {
        args.directivityPatternId[i] = 65535;
    }
+7 −0
Original line number Diff line number Diff line
@@ -672,6 +672,13 @@ void ivas_renderer_select(
    Decoder_Struct *st_ivas                                     /* i/o: IVAS decoder structure                  */
);

#ifdef FIX_CREND_SIMPLIFY_CODE
/*! r: secondary binaural renderer type */
RENDERER_TYPE ivas_renderer_secondary_select(
    Decoder_Struct *st_ivas                                     /* i/o: IVAS decoder structure                  */
);
#endif

ivas_error ivas_mc_enc_config(
    Encoder_Struct *st_ivas                                     /* i/o: IVAS encoder structure                  */
);
+32 −0
Original line number Diff line number Diff line
@@ -480,3 +480,35 @@ void ivas_renderer_select(

    return;
}

#ifdef FIX_CREND_SIMPLIFY_CODE
/*-------------------------------------------------------------------------*
 * ivas_renderer_secondary_select()
 *
 * Select IVAS secondary binaural renderer (used in combined formats)
 *-------------------------------------------------------------------------*/

/*! r: secondary binaural renderer type */
RENDERER_TYPE ivas_renderer_secondary_select(
    Decoder_Struct *st_ivas /* i/o: IVAS decoder structure  */
)
{
    RENDERER_TYPE renderer_type;
    AUDIO_CONFIG output_config;

    /* disabled by default */
    renderer_type = RENDERER_DISABLE;
    output_config = st_ivas->hDecoderConfig->output_config;

    if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC && output_config == IVAS_AUDIO_CONFIG_BINAURAL )
    {
        renderer_type = RENDERER_BINAURAL_OBJECTS_TD;
    }
    else if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    {
        renderer_type = RENDERER_BINAURAL_OBJECTS_TD;
    }

    return renderer_type;
}
#endif
+172 −0
Original line number Diff line number Diff line
@@ -860,8 +860,173 @@ ivas_error IVAS_DEC_FeedFrame_Serial(

    return IVAS_ERR_OK;
}
#ifdef FIX_CREND_SIMPLIFY_CODE
/*---------------------------------------------------------------------*
 * renderer_type_to_mode()
 *
 * Convert (codec library) renderer type to (API public) binaural renderer mode
 *---------------------------------------------------------------------*/

/*! r: binaural renderer mode (API type) */
static IVAS_BIN_RENDERER_TYPE renderer_type_to_mode(
    const RENDERER_TYPE renderer_type /* i  : renderer type (codec library type) */
)
{
    IVAS_BIN_RENDERER_TYPE binaural_renderer;

    switch ( renderer_type )
    {
        case RENDERER_BINAURAL_OBJECTS_TD:
            binaural_renderer = IVAS_BIN_RENDERER_TYPE_TDREND;
            break;
        case RENDERER_BINAURAL_MIXER_CONV:
        case RENDERER_BINAURAL_MIXER_CONV_ROOM:
            binaural_renderer = IVAS_BIN_RENDERER_TYPE_CREND;
            break;
        case RENDERER_BINAURAL_FASTCONV:
            binaural_renderer = IVAS_BIN_RENDERER_TYPE_FASTCONV;
            break;
        case RENDERER_BINAURAL_FASTCONV_ROOM:
            binaural_renderer = IVAS_BIN_RENDERER_TYPE_FASTCONV;
            break;
        case RENDERER_BINAURAL_PARAMETRIC:
        case RENDERER_BINAURAL_PARAMETRIC_ROOM:
            binaural_renderer = IVAS_BIN_RENDERER_TYPE_PARAMBIN;
            break;
        default:
            binaural_renderer = IVAS_BIN_RENDERER_TYPE_NONE;
            break;
    }

    return binaural_renderer;
}


/*---------------------------------------------------------------------*
 * IVAS_DEC_ReadFormat( )
 *
 * Read main parameters from the bitstream to set-up the decoder:
 * - IVAS format
 * - IVAS format specific signaling
 * - compensate for renderer granularity change in JBM
 *---------------------------------------------------------------------*/

ivas_error IVAS_DEC_ReadFormat(
    IVAS_DEC_HANDLE hIvasDec,                      /* i/o: IVAS decoder handle                      */
    IVAS_BIN_RENDERER_TYPE *binaural_renderer,     /* o  : binaural renderer type                   */
    IVAS_BIN_RENDERER_TYPE *binaural_renderer_sec, /* o  : secondary binaural renderer type         */
    IVAS_AUDIO_CONFIG *hrtf_set_audio_cfg          /* o  : HRTF set audio config.                   */
)
{
    ivas_error error;
    Decoder_Struct *st_ivas;
    IVAS_FORMAT ivas_format_old;
    ISM_MODE ism_mode_old;
    MC_MODE mc_mode_old;
    int16_t nchan_transport_old;
    AUDIO_CONFIG intern_config_old, transport_config_old, output_config;
    RENDERER_TYPE renderer_type_old, renderer_type_sec_new;

    if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    {
        return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    }

    st_ivas = hIvasDec->st_ivas;
    ivas_format_old = st_ivas->ivas_format;
    ism_mode_old = st_ivas->ism_mode;
    mc_mode_old = st_ivas->mc_mode;
    nchan_transport_old = st_ivas->nchan_transport;
    intern_config_old = st_ivas->intern_config;
    transport_config_old = st_ivas->transport_config;
    renderer_type_old = st_ivas->renderer_type;

    output_config = st_ivas->hDecoderConfig->output_config;

    if ( st_ivas->ivas_format == MONO_FORMAT )
    {
        return IVAS_ERR_OK;
    }

    if ( st_ivas->bfi == 0 )
    {
        if ( ( error = ivas_dec_get_format( st_ivas ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        if ( ivas_format_old != st_ivas->ivas_format && st_ivas->ini_frame > 0 && !( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ) )
        {
            return ( IVAS_ERROR( IVAS_ERR_INVALID_INPUT_FORMAT, "IVAS format switching is not allowed." ) );
        }

        /* Select binaural renderer */
        ivas_renderer_select( st_ivas );
        *binaural_renderer = renderer_type_to_mode( st_ivas->renderer_type );

        /* Select secondary binaural renderer (used in combined formats) */
        renderer_type_sec_new = ivas_renderer_secondary_select( st_ivas );
        *binaural_renderer_sec = renderer_type_to_mode( renderer_type_sec_new );

        /* select HRTF audio configuration to load the right HRTF set for the external binary file */
        *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_INVALID;
        if ( *binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV )
        {
            if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
            {
                /* SHD HRIRs */
                *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;

                if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
                {
                    /* BRIRs */
                    *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
                }
            }
            else if ( st_ivas->ivas_format == MC_FORMAT )
            {
                /* HRIRs */
                *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;

                if ( ( st_ivas->hDecoderConfig->Opt_Headrotation ) &&
                     !( st_ivas->mc_mode == MC_MODE_PARAMUPMIX && output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) )
                {
                    /* SHD HRIRs for low complexity rotation */
                    *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
                }
            }
        }
        else if ( *binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND )
        {
            if ( st_ivas->ivas_format == ISM_FORMAT )
            {
                /* BRIRs */
                *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
            }
            else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT )
            {
                /* BRIRs */
                *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;

                if ( st_ivas->hDecoderConfig->Opt_Headrotation && ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
                {
                    /* SHD HRIRs for low complexity rotation */
                    *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
                }
            }
        }
    }

    st_ivas->ism_mode = ism_mode_old;
    st_ivas->mc_mode = mc_mode_old;
    st_ivas->nchan_transport = nchan_transport_old;
    st_ivas->intern_config = intern_config_old;
    st_ivas->transport_config = transport_config_old;
    st_ivas->renderer_type = renderer_type_old;

    return IVAS_ERR_OK;
}
#endif
/*---------------------------------------------------------------------*
 * IVAS_DEC_GetSamples( )
 *
@@ -2733,6 +2898,10 @@ ivas_error IVAS_DEC_VoIP_GetSamples(
    JbmTraceFileWriterFn jbmWriterFn,
    void *jbmWriter
#endif
#ifdef FIX_CREND_SIMPLIFY_CODE
    ,
    bool *bitstreamReadDone /* o  : flag indicating that bitstream was read                                 */
#endif
)
{
    Decoder_Struct *st_ivas;
@@ -2833,6 +3002,9 @@ ivas_error IVAS_DEC_VoIP_GetSamples(
                {
                    return error;
                }
#ifdef FIX_CREND_SIMPLIFY_CODE
                *bitstreamReadDone = true;
#endif
            }
            else if ( hIvasDec->hasDecodedFirstGoodFrame )
            {
Loading