Commit 5df99fe9 authored by sagnowski's avatar sagnowski
Browse files

Clean up float/int buffer usage in encoder executable

parent 3ea5539f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -141,6 +141,7 @@
    <ClCompile Include="..\lib_util\audio_file_writer.c" />
    <ClCompile Include="..\lib_util\bitstream_reader.c" />
    <ClCompile Include="..\lib_util\bitstream_writer.c" />
    <ClCompile Include="..\lib_util\buffer_conversions.c" />
    <ClCompile Include="..\lib_util\cmdln_parser.c" />
    <ClCompile Include="..\lib_util\cmdl_tools.c" />
    <ClCompile Include="..\lib_util\evs_rtp_payload.c" />
@@ -163,6 +164,7 @@
    <ClInclude Include="..\lib_util\audio_file_writer.h" />
    <ClInclude Include="..\lib_util\bitstream_reader.h" />
    <ClInclude Include="..\lib_util\bitstream_writer.h" />
    <ClInclude Include="..\lib_util\buffer_conversions.h" />
    <ClInclude Include="..\lib_util\cmdln_parser.h" />
    <ClInclude Include="..\lib_util\cmdl_tools.h" />
    <ClInclude Include="..\lib_util\evs_rtp_payload.h" />
+118 −61
Original line number Diff line number Diff line
@@ -41,6 +41,9 @@
#include "jbm_file_reader.h"
#include "masa_file_reader.h"
#include "ism_file_reader.h"
#ifdef FLOAT_INTERFACE_ENC
#include "buffer_conversions.h"
#endif
#ifdef DEBUGGING
#include "debug.h"
#endif
@@ -127,7 +130,9 @@ typedef struct
#endif
#endif
    bool pca;

#ifdef FLOAT_INTERFACE_ENC
    bool useInt16Interface;
#endif
} EncArguments;


@@ -145,47 +150,6 @@ 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()
 *
@@ -214,7 +178,14 @@ int main(
    {
        ismReaders[i] = NULL;
    }
#ifdef FLOAT_INTERFACE_ENC
    int16_t *audioReadBufInt = NULL; /* Buffer for reading audio from int wav files. Interleaved. */
    float *audioReadBufFloat = NULL; /* Buffer for reading audio from float wav files. Interleaved. */
    int16_t *audioFeedBufInt = NULL; /* Buffer for feeding audio to encoder via int interface. Packed. */
    float *audioFeedBufFloat = NULL; /* Buffer for feeding audio to encoder via float interface. Packed. */
#else
    int16_t *pcmBuf = NULL;
#endif
#ifdef DEBUGGING
    FILE *f_forcedModeProfile = NULL;
#ifdef DEBUG_SBA
@@ -563,7 +534,29 @@ int main(
    }
#endif

#ifdef FLOAT_INTERFACE_ENC
    bool inputFileIsFloat = false;   /* TODO(sgi):  */

    if ( inputFileIsFloat )
    {
        audioReadBufFloat = malloc( pcmBufSize * sizeof( float ) );
    }
    else
    {
        audioReadBufInt = malloc( pcmBufSize * sizeof( int16_t ) );
    }

    if ( arg.useInt16Interface )
    {
        audioFeedBufInt = malloc( pcmBufSize * sizeof( int16_t ) );
    }
    else
    {
        audioFeedBufFloat = malloc( pcmBufSize * sizeof( float ) );
    }
#else
    pcmBuf = malloc( pcmBufSize * sizeof( int16_t ) );
#endif

    /*------------------------------------------------------------------------------------------*
     * Compensate for encoder delay (bitstream aligned with input signal)
@@ -580,11 +573,28 @@ int main(
    {
        /* read samples and throw them away */
        int16_t numSamplesRead = 0;
#ifdef FLOAT_INTERFACE_ENC
        if ( inputFileIsFloat )
        {
            fprintf( stderr, "\nReading of float wav files not implemented\n" );
            goto cleanup;
            /* TODO(sgi): Add float reading to AudioFileReader */
        }
        else
        {
            if ( ( error = AudioFileReader_read( audioReader, audioReadBufInt, encDelayInSamples, &numSamplesRead ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nError reading from file %s\n%s\n", arg.inputWavFilename, IVAS_ENC_GetErrorMessage( error ) );
                goto cleanup;
            }
        }
#else
        if ( ( error = AudioFileReader_read( audioReader, pcmBuf, encDelayInSamples, &numSamplesRead ) ) != IVAS_ERR_OK )
        {
            fprintf( stderr, "\nError reading from file %s\n%s\n", arg.inputWavFilename, IVAS_ENC_GetErrorMessage( error ) );
            goto cleanup;
        }
#endif
    }

    int16_t numSamplesRead = 0;
@@ -625,11 +635,28 @@ int main(
    while ( 1 )
    {
        /* Read the input data */
#ifdef FLOAT_INTERFACE_ENC
        if ( inputFileIsFloat )
        {
            fprintf( stderr, "\nReading of float wav files not implemented\n" );
            goto cleanup;
            /* TODO(sgi): Add float reading to AudioFileReader */
        }
        else
        {
            if ( ( error = AudioFileReader_read( audioReader, audioReadBufInt, pcmBufSize, &numSamplesRead ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nError reading from file %s\n%s\n", arg.inputWavFilename, IVAS_ENC_GetErrorMessage( error ) );
                goto cleanup;
            }
        }
#else
        if ( ( error = AudioFileReader_read( audioReader, pcmBuf, pcmBufSize, &numSamplesRead ) ) != IVAS_ERR_OK )
        {
            fprintf( stderr, "\nError reading from file %s\n%s\n", arg.inputWavFilename, IVAS_ENC_GetErrorMessage( error ) );
            goto cleanup;
        }
#endif

        if ( numSamplesRead == 0 )
        {
@@ -757,24 +784,41 @@ int main(
        }

#ifdef FLOAT_INTERFACE_ENC
        float *tmpFloatBuf = NULL;
        bool useFloat = true; /* TODO(sgi): get from input file type or command line flag */
        if (useFloat)
        if ( arg.useInt16Interface )
        {
            /* Do buffer conversions */
            if ( inputFileIsFloat )
            {
                copyBufferInterleavedFloatToPackedInt( audioReadBufFloat, numSamplesRead, audioFeedBufInt, pcmBufNumSamplesPerChannel, pcmBufNumChannels );
            }
            else
            {
            /* TODO(sgi): Don't allocate on every frame */
            tmpFloatBuf = malloc(pcmBufSize * sizeof(float));
            copyBufferInterleavedIntToPackedFloat(pcmBuf, numSamplesRead, pcmBufNumSamplesPerChannel, pcmBufNumChannels, tmpFloatBuf);
                copyBufferInterleavedIntToPackedInt( audioReadBufInt, numSamplesRead, audioFeedBufInt, pcmBufNumSamplesPerChannel, pcmBufNumChannels );
            }

            /* Feed input audio */
            if ( ( error = IVAS_ENC_FeedInputAudioFloat( hIvasEnc, tmpFloatBuf, pcmBufNumSamplesPerChannel, pcmBufNumChannels ) ) != IVAS_ERR_OK )
            if ( ( error = IVAS_ENC_FeedInputAudioInt( hIvasEnc, audioFeedBufInt, pcmBufNumSamplesPerChannel, pcmBufNumChannels ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "\nIVAS_ENC_FeedInputAudioFloat failed: %s\n\n", IVAS_ENC_GetErrorMessage( error ) );
                fprintf( stderr, "\nIVAS_ENC_FeedInputAudioInt failed: %s\n\n", IVAS_ENC_GetErrorMessage( error ) );
                return error;
            }
        } else {
        }
        else
        {
            /* Do buffer conversions */
            if ( inputFileIsFloat )
            {
                copyBufferInterleavedFloatToPackedFloat( audioReadBufFloat, numSamplesRead, audioFeedBufFloat, pcmBufNumSamplesPerChannel, pcmBufNumChannels );
            }
            else
            {
                copyBufferInterleavedIntToPackedFloat( audioReadBufInt, numSamplesRead, audioFeedBufFloat, pcmBufNumSamplesPerChannel, pcmBufNumChannels );
            }

            /* Feed input audio */
            if ( ( error = IVAS_ENC_FeedInputAudioInt( hIvasEnc, pcmBuf, pcmBufNumSamplesPerChannel, pcmBufNumChannels ) ) != IVAS_ERR_OK )
            if ( ( error = IVAS_ENC_FeedInputAudioFloat( hIvasEnc, audioFeedBufFloat, 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;
            }
        }
@@ -791,13 +835,6 @@ int main(
            goto cleanup;
        }

#ifdef FLOAT_INTERFACE_ENC
        if (tmpFloatBuf != NULL)
        {
            free(tmpFloatBuf);
        }
#endif

        /* write bitstream */
        if ( ( error = BS_Writer_WriteFrame_short( hBsWriter, bitStream, numBits, totalBitrate ) ) != IVAS_ERR_OK )
        {
@@ -840,7 +877,14 @@ int main(

cleanup:

#ifdef FLOAT_INTERFACE_ENC
    free( audioReadBufInt );
    free( audioReadBufFloat );
    free( audioFeedBufInt );
    free( audioFeedBufFloat );
#else
    free( pcmBuf );
#endif

    if ( ( error = BS_Writer_Close( &hBsWriter ) ) != IVAS_ERR_OK )
    {
@@ -946,6 +990,9 @@ static void initArgStruct( EncArguments *arg )
#endif
#endif
    arg->pca = false;
#ifdef FLOAT_INTERFACE_ENC
    arg->useInt16Interface = false;
#endif

    return;
}
@@ -1541,6 +1588,13 @@ static bool parseCmdlIVAS_enc(
                return false;
            }
        }
#ifdef FLOAT_INTERFACE_ENC
        else if ( strcmp( argv_to_upper, "-int16_api" ) == 0 )
        {
            arg->useInt16Interface = true;
            i++;
        }
#endif

        /*-----------------------------------------------------------------*
         * Option not recognized
@@ -1741,6 +1795,9 @@ static void usage_enc( void )
    fprintf( stdout, "-info <folder>      : specify subfolder name for debug output\n" );
#endif
#endif
#endif
#ifdef FLOAT_INTERFACE_ENC
    fprintf( stdout, "-int16_api          : Force int16 library interface to be used\n" );
#endif
    fprintf( stdout, "-q                  : Quiet mode, no frame counters\n" );
    fprintf( stdout, "                      default is deactivated\n" );
+177 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2023 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository. All Rights Reserved.

   This software is protected by copyright law and by international treaties.
   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository retain full ownership rights in their respective contributions in
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

   This software is provided "AS IS", without any express or implied warranties. The software is in the
   development stage. It is intended exclusively for experts who have experience with such software and
   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
   and fitness for a particular purpose are hereby disclaimed and excluded.

   Any dispute, controversy or claim arising under or in relation to providing this software shall be
   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/

#include "buffer_conversions.h"
#include "options.h"

#ifdef FLOAT_INTERFACE_ENC

/*--------------------------------------------------------------------------*
 * copyBufferInterleavedFloatToPackedFloat()
 *
 *--------------------------------------------------------------------------*/
void copyBufferInterleavedFloatToPackedFloat(
    const float *srcBuffer,
    const int16_t srcBufferTotalNumSamples,
    float *dstBuffer,
    const int16_t dstBufferNumSamplesPerChannel,
    const int16_t dstBufferNumChannels )
{
    int16_t chnl, smpl, i;

    i = 0;

    for ( smpl = 0; smpl < dstBufferNumSamplesPerChannel; ++smpl )
    {
        for ( chnl = 0; chnl < dstBufferNumChannels; ++chnl )
        {
            if ( i < srcBufferTotalNumSamples )
            {
                dstBuffer[chnl * dstBufferNumSamplesPerChannel + smpl] = srcBuffer[i];
            }
            else
            {
                dstBuffer[chnl * dstBufferNumSamplesPerChannel + smpl] = 0.f;
            }

            ++i;
        }
    }

    return;
}

/*--------------------------------------------------------------------------*
 * copyBufferInterleavedFloatToPackedInt()
 *
 *--------------------------------------------------------------------------*/
void copyBufferInterleavedFloatToPackedInt(
    const float *srcBuffer,
    const int16_t srcBufferTotalNumSamples,
    int16_t *dstBuffer,
    const int16_t dstBufferNumSamplesPerChannel,
    const int16_t dstBufferNumChannels )
{
    int16_t chnl, smpl, i;

    i = 0;

    for ( smpl = 0; smpl < dstBufferNumSamplesPerChannel; ++smpl )
    {
        for ( chnl = 0; chnl < dstBufferNumChannels; ++chnl )
        {
            if ( i < srcBufferTotalNumSamples )
            {
                dstBuffer[chnl * dstBufferNumSamplesPerChannel + smpl] = (int16_t) srcBuffer[i];
            }
            else
            {
                dstBuffer[chnl * dstBufferNumSamplesPerChannel + smpl] = 0;
            }

            ++i;
        }
    }

    return;
}

/*--------------------------------------------------------------------------*
 * copyBufferInterleavedIntToPackedFloat()
 *
 *--------------------------------------------------------------------------*/
void copyBufferInterleavedIntToPackedFloat(
    const int16_t *srcBuffer,
    const int16_t srcBufferTotalNumSamples,
    float *dstBuffer,
    const int16_t dstBufferNumSamplesPerChannel,
    const int16_t dstBufferNumChannels )
{
    int16_t chnl, smpl, i;

    i = 0;

    for ( smpl = 0; smpl < dstBufferNumSamplesPerChannel; ++smpl )
    {
        for ( chnl = 0; chnl < dstBufferNumChannels; ++chnl )
        {
            if ( i < srcBufferTotalNumSamples )
            {
                dstBuffer[chnl * dstBufferNumSamplesPerChannel + smpl] = (float) srcBuffer[i];
            }
            else
            {
                dstBuffer[chnl * dstBufferNumSamplesPerChannel + smpl] = 0.f;
            }

            ++i;
        }
    }

    return;
}

/*--------------------------------------------------------------------------*
 * copyBufferInterleavedIntToPackedInt()
 *
 *--------------------------------------------------------------------------*/
void copyBufferInterleavedIntToPackedInt(
    const int16_t *srcBuffer,
    const int16_t srcBufferTotalNumSamples,
    int16_t *dstBuffer,
    const int16_t dstBufferNumSamplesPerChannel,
    const int16_t dstBufferNumChannels )
{
    int16_t chnl, smpl, i;

    i = 0;

    for ( smpl = 0; smpl < dstBufferNumSamplesPerChannel; ++smpl )
    {
        for ( chnl = 0; chnl < dstBufferNumChannels; ++chnl )
        {
            if ( i < srcBufferTotalNumSamples )
            {
                dstBuffer[chnl * dstBufferNumSamplesPerChannel + smpl] = srcBuffer[i];
            }
            else
            {
                dstBuffer[chnl * dstBufferNumSamplesPerChannel + smpl] = 0;
            }

            ++i;
        }
    }

    return;
}
#endif
+71 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2023 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository. All Rights Reserved.

   This software is protected by copyright law and by international treaties.
   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository retain full ownership rights in their respective contributions in
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

   This software is provided "AS IS", without any express or implied warranties. The software is in the
   development stage. It is intended exclusively for experts who have experience with such software and
   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
   and fitness for a particular purpose are hereby disclaimed and excluded.

   Any dispute, controversy or claim arising under or in relation to providing this software shall be
   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/

#ifndef IVAS_BUFFER_CONVERSIONS_H
#define IVAS_BUFFER_CONVERSIONS_H

#include "options.h"
#include <stdint.h>

#ifdef FLOAT_INTERFACE_ENC

void copyBufferInterleavedFloatToPackedFloat(
    const float *srcBuffer,
    const int16_t srcBufferTotalNumSamples,
    float *dstBuffer,
    const int16_t dstBufferNumSamplesPerChannel,
    const int16_t dstBufferNumChannels );

void copyBufferInterleavedFloatToPackedInt(
    const float *srcBuffer,
    const int16_t srcBufferTotalNumSamples,
    int16_t *dstBuffer,
    const int16_t dstBufferNumSamplesPerChannel,
    const int16_t dstBufferNumChannels );

void copyBufferInterleavedIntToPackedFloat(
    const int16_t *srcBuffer,
    const int16_t srcBufferTotalNumSamples,
    float *dstBuffer,
    const int16_t dstBufferNumSamplesPerChannel,
    const int16_t dstBufferNumChannels );

void copyBufferInterleavedIntToPackedInt(
    const int16_t *srcBuffer,
    const int16_t srcBufferTotalNumSamples,
    int16_t *dstBuffer,
    const int16_t dstBufferNumSamplesPerChannel,
    const int16_t dstBufferNumChannels );
#endif


#endif