Commit a9162a8d authored by sagnowski's avatar sagnowski
Browse files

Fix missing interleaved-to-packed conversion in encoder

parent 9653e5de
Loading
Loading
Loading
Loading
+65 −7
Original line number Diff line number Diff line
@@ -145,6 +145,46 @@ static ivas_error readForcedMode( FILE *file, IVAS_ENC_FORCED_MODE *forcedMode,
static IVAS_ENC_FORCED_MODE parseForcedMode( char *forcedModeChar );
#endif

#ifdef FLOAT_INTERFACE_ENC
/* TODO(sgi): move to lib_util, re-use between renderer, encoder and decoder */
/*--------------------------------------------------------------------------*
 * copyBufferInterleavedIntToPackedFloat()
 *
 * Convert input buffer from WAV/PCM file (int16_t, interleaved) to a format
 * accepted by the renderer (float, packed)
 *--------------------------------------------------------------------------*/

static void copyBufferInterleavedIntToPackedFloat(
    const int16_t *intBuffer,
    const int16_t totalNumSamplesInIntBuffer,
    const int16_t numFloatSamplesPerChannel,
    const int16_t numChannels,
    float *floatBuffer )
{
    int16_t chnl, smpl, i;

    i = 0;

    for ( smpl = 0; smpl < numFloatSamplesPerChannel; ++smpl )
    {
        for ( chnl = 0; chnl < numChannels; ++chnl )
        {
            if ( i < totalNumSamplesInIntBuffer )
            {
                floatBuffer[chnl * numFloatSamplesPerChannel + smpl] = (float) intBuffer[i];
            }
            else
            {
                floatBuffer[chnl * numFloatSamplesPerChannel + smpl] = 0.f;
            }

            ++i;
        }
    }

    return;
}
#endif

/*------------------------------------------------------------------------------------------*
 * main()
@@ -499,12 +539,29 @@ int main(
     * Allocate input data buffer
     *------------------------------------------------------------------------------------------*/

#ifdef FLOAT_INTERFACE_ENC
    int16_t pcmBufNumChannels;
    int16_t pcmBufNumSamplesPerChannel;
    if ( ( error = IVAS_ENC_GetNumInputChannels( hIvasEnc, &pcmBufNumChannels ) ) != IVAS_ERR_OK )
    {
        fprintf( stderr, "\nGetNumInputChannels failed: %s\n\n", IVAS_ENC_GetErrorMessage( error ) );
        goto cleanup;
    }
    if ( ( error = IVAS_ENC_GetNumInputSamplesPerChannel( hIvasEnc, &pcmBufNumSamplesPerChannel ) ) != IVAS_ERR_OK )
    {
        fprintf( stderr, "\nGetNumInputSamplesPerChannel failed: %s\n\n", IVAS_ENC_GetErrorMessage( error ) );
        goto cleanup;
    }

    const int16_t pcmBufSize = pcmBufNumChannels * pcmBufNumSamplesPerChannel ;
#else
    int16_t pcmBufSize;
    if ( ( error = IVAS_ENC_GetInputBufferSize( hIvasEnc, &pcmBufSize ) ) != IVAS_ERR_OK )
    {
        fprintf( stderr, "\nGetInputBufferSize failed: %s\n\n", IVAS_ENC_GetErrorMessage( error ) );
        goto cleanup;
    }
#endif

    pcmBuf = malloc( pcmBufSize * sizeof( int16_t ) );

@@ -580,6 +637,9 @@ int main(
            break;
        }

#ifndef FLOAT_INTERFACE_ENC
        /* FLOAT_INTERFACE_ENC: Now this is handled during interleaved-to-packed conversion */

        /* Zero-pad if not enough samples were read (expected in last frame) */
        if ( numSamplesRead < pcmBufSize )
        {
@@ -588,6 +648,7 @@ int main(
                pcmBuf[i] = 0;
            }
        }
#endif

        /* Process switching files */
        if ( f_bitrateProfile )
@@ -702,19 +763,16 @@ int main(
        {
            /* TODO(sgi): Don't allocate on every frame */
            tmpFloatBuf = malloc(pcmBufSize * sizeof(float));
            for (int32_t i = 0; i < pcmBufSize; ++i)
            {
                tmpFloatBuf[i] = pcmBuf[i];
            }
            copyBufferInterleavedIntToPackedFloat(pcmBuf, numSamplesRead, pcmBufNumSamplesPerChannel, pcmBufNumChannels, tmpFloatBuf);
            /* Feed input audio */
            if ( ( error = IVAS_ENC_FeedInputAudioFloat( hIvasEnc, tmpFloatBuf, pcmBufSize ) ) != IVAS_ERR_OK )
            if ( ( error = IVAS_ENC_FeedInputAudioFloat( hIvasEnc, tmpFloatBuf, pcmBufNumSamplesPerChannel, pcmBufNumChannels ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_ENC_FeedInputAudioInt failed: %s\n\n", IVAS_ENC_GetErrorMessage( error ) );
                fprintf( stderr, "\nIVAS_ENC_FeedInputAudioFloat failed: %s\n\n", IVAS_ENC_GetErrorMessage( error ) );
                return error;
            }
        } else {
            /* Feed input audio */
            if ( ( error = IVAS_ENC_FeedInputAudioInt( hIvasEnc, pcmBuf, pcmBufSize ) ) != IVAS_ERR_OK )
            if ( ( error = IVAS_ENC_FeedInputAudioInt( hIvasEnc, pcmBuf, pcmBufNumSamplesPerChannel, pcmBufNumChannels ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_ENC_FeedInputAudioInt failed: %s\n\n", IVAS_ENC_GetErrorMessage( error ) );
                return error;
+75 −16
Original line number Diff line number Diff line
@@ -982,6 +982,60 @@ static int16_t getInputBufferSize(
    return (int16_t) ( st_ivas->hEncoderConfig->input_Fs * st_ivas->hEncoderConfig->nchan_inp / FRAMES_PER_SEC );
}

#ifdef FLOAT_INTERFACE_ENC
/*---------------------------------------------------------------------*
 * IVAS_ENC_GetNumInputChannels()
 *
 *
 *---------------------------------------------------------------------*/

ivas_error IVAS_ENC_GetNumInputChannels(
    const IVAS_ENC_HANDLE hIvasEnc,                 /* i/o: IVAS encoder handle                                                                                 */
    int16_t *numInputChannels                       /* o  : number of channels expected in the input buffer for current encoder configuration                   */
)
{
    if ( !hIvasEnc->isConfigured )
    {
        return IVAS_ERR_NOT_CONFIGURED;
    }

    if ( numInputChannels == NULL )
    {
        return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    }

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

    return IVAS_ERR_OK;
}

/*---------------------------------------------------------------------*
 * IVAS_ENC_GetNumInputSamplesPerChannel()
 *
 *
 *---------------------------------------------------------------------*/

ivas_error IVAS_ENC_GetNumInputSamplesPerChannel(
    const IVAS_ENC_HANDLE hIvasEnc,                 /* i/o: IVAS encoder handle                                                                                 */
    int16_t *numInputSamplesPerChannel              /* o  : number of samples per channel expected in the input buffer for current encoder configuration        */
)
{
    if ( !hIvasEnc->isConfigured )
    {
        return IVAS_ERR_NOT_CONFIGURED;
    }

    if ( numInputSamplesPerChannel == NULL )
    {
        return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    }

    *numInputSamplesPerChannel = hIvasEnc->st_ivas->hEncoderConfig->input_Fs / FRAMES_PER_SECOND;

    return IVAS_ERR_OK;
}
#else


/*---------------------------------------------------------------------*
 * IVAS_ENC_GetInputBufferSize()
@@ -1008,6 +1062,7 @@ ivas_error IVAS_ENC_GetInputBufferSize(

    return IVAS_ERR_OK;
}
#endif

#ifdef FLOAT_INTERFACE_ENC

@@ -1020,14 +1075,15 @@ ivas_error IVAS_ENC_GetInputBufferSize(
ivas_error IVAS_ENC_FeedInputAudioInt(
    IVAS_ENC_HANDLE hIvasEnc,                       /* i/o: IVAS encoder handle                                                                                 */
    int16_t *inputBuffer,                           /* i  : PCM input                                                                                           */
    int16_t inputBufferSize   /* i  : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize()        */
    int16_t numInputSamplesPerChannel,              /* i  : number of samples per channel in the input buffer                                                   */
    int16_t numInputChannels                        /* i  : number of channels in the input buffer                                                              */
)
{
    Encoder_Struct *st_ivas;

    st_ivas = hIvasEnc->st_ivas;

    if ( inputBufferSize != getInputBufferSize( st_ivas ) )
    if ( numInputChannels != hIvasEnc->st_ivas->hEncoderConfig->nchan_inp )
    {
        return IVAS_ERR_INVALID_INPUT_BUFFER_SIZE;
    }
    if ( numInputSamplesPerChannel != hIvasEnc->st_ivas->hEncoderConfig->input_Fs / FRAMES_PER_SECOND )
    {
        return IVAS_ERR_INVALID_INPUT_BUFFER_SIZE;
    }
@@ -1049,14 +1105,15 @@ ivas_error IVAS_ENC_FeedInputAudioInt(
ivas_error IVAS_ENC_FeedInputAudioFloat(
    IVAS_ENC_HANDLE hIvasEnc,                       /* i/o: IVAS encoder handle                                                                                 */
    float *inputBuffer,                             /* i  : PCM input                                                                                           */
    int16_t inputBufferSize   /* i  : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize()        */
    int16_t numInputSamplesPerChannel,              /* i  : number of samples per channel in the input buffer                                                   */
    int16_t numInputChannels                        /* i  : number of channels in the input buffer                                                              */
)
{
    Encoder_Struct *st_ivas;

    st_ivas = hIvasEnc->st_ivas;

    if ( inputBufferSize != getInputBufferSize( st_ivas ) )
    if ( numInputChannels != hIvasEnc->st_ivas->hEncoderConfig->nchan_inp )
    {
        return IVAS_ERR_INVALID_INPUT_BUFFER_SIZE;
    }
    if ( numInputSamplesPerChannel != hIvasEnc->st_ivas->hEncoderConfig->input_Fs / FRAMES_PER_SECOND )
    {
        return IVAS_ERR_INVALID_INPUT_BUFFER_SIZE;
    }
@@ -1269,8 +1326,10 @@ fail:
/* IVAS_fmToDo: Currently unused and untested */
ivas_error IVAS_ENC_EncodeFrameToCompact(
    IVAS_ENC_HANDLE hIvasEnc,      /* i/o: IVAS encoder handle                                                                                 */
#ifndef FLOAT_INTERFACE_ENC
    int16_t *inputBuffer,          /* i  : PCM input                                                                                           */
    const int16_t inputBufferSize, /* i  : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize()        */
#endif
    uint8_t *outputBitStream,      /* o  : pointer to compact output bitstream. The array must already be allocated.                           */
    uint16_t *numOutBits           /* o  : number of bits written to output bitstream                                                          */
)
+22 −4
Original line number Diff line number Diff line
@@ -250,15 +250,17 @@ ivas_error IVAS_ENC_FeedMasaMetadata(
/*! r: error code */
ivas_error IVAS_ENC_FeedInputAudioInt(
    IVAS_ENC_HANDLE hIvasEnc,                       /* i/o: IVAS encoder handle                                                                                 */
    int16_t *inputBuffer,                           /* i  : PCM input buffer with a packed layout                                                               */
    int16_t inputBufferSize                         /* i  : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize()        */
    int16_t *inputBuffer,                           /* i  : PCM input buffer in packed layout                                                                   */
    int16_t numInputSamplesPerChannel,              /* i  : number of samples per channel in the input buffer                                                   */
    int16_t numInputChannels                        /* i  : number of channels in the input buffer                                                              */
);

/*! r: error code */
ivas_error IVAS_ENC_FeedInputAudioFloat(
    IVAS_ENC_HANDLE hIvasEnc,                       /* i/o: IVAS encoder handle                                                                                 */
    float *inputBuffer,                             /* i  : PCM input buffer with a packed layout                                                               */
    int16_t inputBufferSize                         /* i  : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize()        */
    float *inputBuffer,                             /* i  : PCM input buffer in packed layout                                                                   */
    int16_t numInputSamplesPerChannel,              /* i  : number of samples per channel in the input buffer                                                   */
    int16_t numInputChannels                        /* i  : number of channels in the input buffer                                                              */
);
#endif

@@ -276,8 +278,10 @@ ivas_error IVAS_ENC_EncodeFrameToSerial(
/*! r: error code */
ivas_error IVAS_ENC_EncodeFrameToCompact(
    IVAS_ENC_HANDLE hIvasEnc,                       /* i/o: IVAS encoder handle                                                                                 */
#ifndef FLOAT_INTERFACE_ENC
    int16_t *inputBuffer,                           /* i  : PCM input                                                                                           */
    const int16_t inputBufferSize,                  /* i  : total number of samples in the input buffer. Related function: IVAS_ENC_GetInputBufferSize()        */
#endif
    uint8_t *outputBitStream,                       /* o  : pointer to compact output bitstream. The array must already be allocated.                           */
    uint16_t *numOutBits                            /* o  : number of bits written to output bitstream                                                          */
);
@@ -318,11 +322,25 @@ ivas_error IVAS_ENC_GetDelay(
    int16_t *delay                                  /* o  : encoder delay                                                                                       */
);

#ifdef FLOAT_INTERFACE_ENC
/*! r: encoder error code */
ivas_error IVAS_ENC_GetNumInputChannels(
    const IVAS_ENC_HANDLE hIvasEnc,                 /* i/o: IVAS encoder handle                                                                                 */
    int16_t *numInputChannels                       /* o  : number of channels expected in the input buffer for current encoder configuration                   */
);

/*! r: encoder error code */
ivas_error IVAS_ENC_GetNumInputSamplesPerChannel(
    const IVAS_ENC_HANDLE hIvasEnc,                 /* i/o: IVAS encoder handle                                                                                 */
    int16_t *numInputSamplesPerChannel              /* o  : number of samples per channel expected in the input buffer for current encoder configuration        */
);
#else
/*! r: encoder error code */
ivas_error IVAS_ENC_GetInputBufferSize(
    const IVAS_ENC_HANDLE hIvasEnc,                 /* i/o: IVAS encoder handle                                                                                 */
    int16_t *inputBufferSize                        /* o  : total number of samples expected in the input buffer for current encoder configuration              */
);
#endif

/* Utility functions */
/*! r: default bandwidth for the encoder */
+4 −4
Original line number Diff line number Diff line
@@ -154,8 +154,8 @@ void AudioFileReader_close(
ivas_error AudioFileReader_read(
    AudioFileReader *self,    /* i/o: AudioFileReader handle                */
    int16_t *samples,         /* o  : samples read from the opened file     */
    const int16_t numSamples, /* i  : number of samples to read           */
    int16_t *numSamplesRead   /* i  : number of samples actualy read      */
    const int16_t numSamples, /* i  : total number of samples to read       */
    int16_t *numSamplesRead   /* i  : total number of samples actualy read  */
)
{
    uint32_t numSamplesRead32 = 0;
+4 −4
Original line number Diff line number Diff line
@@ -51,8 +51,8 @@ ivas_error AudioFileReader_open(
ivas_error AudioFileReader_read(
    AudioFileReader *self,                    /* i/o: AudioFileReader handle                */
    int16_t *samples,                         /* o  : samples read from the opened file     */
    const int16_t numSamples,                 /* i  : number of samples to read           */
    int16_t *numSamplesRead                   /* i  : number of samples actualy read      */
    const int16_t numSamples,                 /* i  : total number of samples to read       */
    int16_t *numSamplesRead                   /* i  : total number of samples actualy read  */
);

/*! r: number of channels of the opened file */