Commit f0f272f1 authored by sagnowski's avatar sagnowski
Browse files

Merge branch...

Merge branch '94-amend-audiofilereader_open-by-expnumchannels-return-error-in-case-of-mismatch' into 'main'

Resolve "Amend AudioFileReader_open() by expNumChannels, return error in case of mismatch"

See merge request !470
parents 40517030 6fd7b13b
Loading
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
@@ -213,6 +213,7 @@ int main(
        goto cleanup;
    }

#ifndef FIX_94_VERIFY_WAV_NUM_CHANNELS
    /*------------------------------------------------------------------------------------------*
     * Open input audio file
     *------------------------------------------------------------------------------------------*/
@@ -228,6 +229,7 @@ int main(
        fprintf( stderr, "Sampling rate mismatch: %d Hz requested, but %d Hz found in file %s\n", arg.inputFs, inFileSampleRate, arg.inputWavFilename );
        goto cleanup;
    }
#endif

    /*------------------------------------------------------------------------------------------*
     * Open output bitstream file
@@ -450,6 +452,67 @@ int main(
        goto cleanup;
    }

#ifdef FIX_94_VERIFY_WAV_NUM_CHANNELS
    /*------------------------------------------------------------------------------------------*
     * Open input audio file
     *------------------------------------------------------------------------------------------*/

    if ( AudioFileReader_open( &audioReader, arg.inputWavFilename ) != IVAS_ERR_OK )
    {
        fprintf( stderr, "\nCan't open %s\n\n", arg.inputWavFilename );
        goto cleanup;
    }

    /* Validate input sampling rate */
    int32_t inFileSampleRate = 0;
    error = AudioFileReader_getSamplingRate( audioReader, &inFileSampleRate );
    switch ( error )
    {
        case IVAS_ERR_OK:
            if ( inFileSampleRate != arg.inputFs )
            {
                fprintf( stderr, "\nSampling rate mismatch: %d Hz requested, but %d Hz found in file %s\n\n", arg.inputFs, inFileSampleRate, arg.inputWavFilename );
                goto cleanup;
            }
            break;
        case IVAS_ERR_SAMPLING_RATE_UNKNOWN:
            /* IVAS_ERR_SAMPLING_RATE_UNKNOWN will be returned for raw PCM files.
             * Nothing to check here */
            break;
        default:
            fprintf( stderr, "\nError: %s\n", ivas_error_to_string( error ) );
            goto cleanup;
    }


    /* Validate number of channels */
    int16_t encInNumChannels = 0;
    if ( ( error = IVAS_ENC_GetNumInChannels( hIvasEnc, &encInNumChannels ) ) != IVAS_ERR_OK )
    {
        fprintf( stderr, "\nError: %s\n", ivas_error_to_string( error ) );
        goto cleanup;
    }
    int16_t inFileNumChannels = 0;
    error = AudioFileReader_getNumChannels( audioReader, &inFileNumChannels );
    switch ( error )
    {
        case IVAS_ERR_OK:
            if ( inFileNumChannels != encInNumChannels )
            {
                fprintf( stderr, "\nNumber of input audio channels mismatch: %d accepted by encoder, but %d found in file %s\n\n", encInNumChannels, inFileNumChannels, arg.inputWavFilename );
                goto cleanup;
            }
            break;
        case IVAS_ERR_NUM_CHANNELS_UNKNOWN:
            /* IVAS_ERR_NUM_CHANNELS_UNKNOWN will be returned for raw PCM files.
             * Nothing to check here */
            break;
        default:
            fprintf( stderr, "\nError: %s\n", ivas_error_to_string( error ) );
            goto cleanup;
    }
#endif

    /*------------------------------------------------------------------------------------------*
     * Open input metadata files
     *------------------------------------------------------------------------------------------*/
+44 −0
Original line number Diff line number Diff line
@@ -565,6 +565,45 @@ int main(
        setupWithSingleFormatInput( args, audioFilePath, positionProvider, masaReaders );
    }

#ifdef FIX_94_VERIFY_WAV_NUM_CHANNELS
    if ( AudioFileReader_open( &audioReader, audioFilePath ) != IVAS_ERR_OK )
    {
        fprintf( stderr, "Error opening file: %s\n", audioFilePath );
        exit( -1 );
    }

    int32_t inFileSampleRate = 0;
    error = AudioFileReader_getSamplingRate( audioReader, &inFileSampleRate );
    switch ( error )
    {
        case IVAS_ERR_OK:
            if ( inFileSampleRate != args.sampleRate )
            {
                fprintf( stderr, "Sampling rate mismatch: %d Hz requested, but %d Hz found in file %s\n", args.sampleRate, inFileSampleRate, args.inputFilePath );
                exit( -1 );
            }
            break;
        case IVAS_ERR_SAMPLING_RATE_UNKNOWN: /* Returned when input is raw PCM */
            if ( args.sampleRate == 0 )
            {
                fprintf( stderr, "Sampling rate must be specified on command line when using raw PCM input\n" );
                exit( -1 );
            }
            args.sampleRate = inFileSampleRate;
            break;
        default:
            fprintf( stderr, "Error: %s\n", ivas_error_to_string( error ) );
            exit( -1 );
    }

    int16_t inFileNumChannels = 0;
    error = AudioFileReader_getNumChannels( audioReader, &inFileNumChannels );
    if ( error != IVAS_ERR_OK && error != IVAS_ERR_NUM_CHANNELS_UNKNOWN )
    {
        fprintf( stderr, "Error: %s\n", ivas_error_to_string( error ) );
        exit( -1 );
    }
#else
    int32_t inFileSampleRate = 0;
    if ( AudioFileReader_open( &audioReader, audioFilePath, &inFileSampleRate ) != IVAS_ERR_OK )
    {
@@ -585,6 +624,7 @@ int main(
    {
        args.sampleRate = inFileSampleRate;
    }
#endif
    const int16_t frameSize_smpls = (int16_t) ( 20 * args.sampleRate / 1000 );

    IVAS_REND_InputId mcIds[RENDERER_MAX_MC_INPUTS] = { 0 };
@@ -752,8 +792,12 @@ int main(

    const int16_t totalNumInChannels = getTotalNumInChannels( hIvasRend, mcIds, ismIds, sbaIds, masaIds );

#ifdef FIX_94_VERIFY_WAV_NUM_CHANNELS
    if ( inFileNumChannels != 0 /* inFileNumChannels is 0 with raw PCM input */ && totalNumInChannels != inFileNumChannels )
#else
    if ( AudioFileReader_getNumChannels( audioReader ) != 0 /* If input file is raw PCM, audio reader has no info about number of channels */
         && totalNumInChannels != AudioFileReader_getNumChannels( audioReader ) )
#endif
    {
        fprintf( stderr, "Number of channels in input file does not match selected configuration\n" );
        exit( -1 );
+4 −0
Original line number Diff line number Diff line
@@ -120,6 +120,10 @@ typedef enum
    IVAS_ERR_BITSTREAM_WRITER_INVALID_FORMAT,
    IVAS_ERR_BITSTREAM_READER_INVALID_DATA,
    IVAS_ERR_BITSTREAM_READER_INVALID_FORMAT,
#ifdef FIX_94_VERIFY_WAV_NUM_CHANNELS
    IVAS_ERR_NO_FILE_OPEN,
    IVAS_ERR_SAMPLING_RATE_UNKNOWN,
#endif

    /*----------------------------------------*
     *    renderer (lib_rend only)            *
+2 −0
Original line number Diff line number Diff line
@@ -162,6 +162,8 @@

#define BINAURALIZATION_DELAY_REPORT                    /* VA: Issue 255 - Changes the way the decoder delay is reported */

#define FIX_94_VERIFY_WAV_NUM_CHANNELS                  /* FhG: Issue 94 - Check if number of channels in input wav file matches encoder/renderer configuration */


/* ################## End DEVELOPMENT switches ######################### */
/* clang-format on */
+27 −0
Original line number Diff line number Diff line
@@ -994,6 +994,33 @@ static int16_t getInputBufferSize(
    return (int16_t) ( st_ivas->hEncoderConfig->input_Fs * st_ivas->hEncoderConfig->nchan_inp / FRAMES_PER_SEC );
}

#ifdef FIX_94_VERIFY_WAV_NUM_CHANNELS
/*---------------------------------------------------------------------*
 * IVAS_ENC_GetNumInChannels()
 *
 *
 *---------------------------------------------------------------------*/
ivas_error IVAS_ENC_GetNumInChannels(
    const IVAS_ENC_HANDLE hIvasEnc, /* i/o: IVAS encoder handle                                                                                 */
    int16_t *numInChannels          /* o  : total number of samples expected in the input buffer for current encoder configuration              */
)
{
    if ( hIvasEnc == NULL || numInChannels == NULL )
    {
        return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    }

    if ( !hIvasEnc->isConfigured )
    {
        return IVAS_ERR_NOT_CONFIGURED;
    }

    *numInChannels = hIvasEnc->st_ivas->hEncoderConfig->nchan_inp;

    return IVAS_ERR_OK;
}
#endif


/*---------------------------------------------------------------------*
 * IVAS_ENC_GetInputBufferSize()
Loading