Commit 084a523e authored by vaclav's avatar vaclav
Browse files

correction and simplification of lib_dec.c; under LIB_DEC_REVISION

parent 37f235fe
Loading
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -259,7 +259,11 @@ int main(
     * Open decoder handle
     *------------------------------------------------------------------------------------------*/

#ifdef LIB_DEC_REVISION
    if ( ( error = IVAS_DEC_Open( &hIvasDec, arg.output_Fs, arg.decMode ) ) != IVAS_ERR_OK )
#else
    if ( ( error = IVAS_DEC_Open( &hIvasDec, arg.decMode ) ) != IVAS_ERR_OK )
#endif
    {
        fprintf( stderr, "Open failed: %s\n", IVAS_DEC_GetErrorMessage( error ) );
        goto cleanup;
+1 −0
Original line number Diff line number Diff line
@@ -163,6 +163,7 @@
#define FIX_1285_RENDER_CONFIG_PTR_COMPARE              /* Philips: Warning about pointer comparison in the render config reader */
#define FIX_1298_MEMORY_OPT_IVAS_CORE_ENC               /* VA: issue 1298: Memory saving in ivas_core_enc() */
#define SIMPLIFY_IVAS_CORE                              /* VA: simplify ivas core coder functions */
#define LIB_DEC_REVISION                                /* VA: correction and simplification of lib_dec.c */

/* #################### End BE switches ################################## */

+105 −13
Original line number Diff line number Diff line
@@ -55,7 +55,9 @@

struct IVAS_DEC_VOIP
{
#ifndef LIB_DEC_REVISION
    uint16_t nSamplesFrame; /* Total number of samples in a frame (includes number of channels) */
#endif
    JB4_HANDLE hJBM;
    uint16_t lastDecodedWasActive;
    JB4_DATAUNIT_HANDLE hCurrentDataUnit; /* Points to the currently processed data unit */
@@ -90,7 +92,9 @@ struct IVAS_DEC
    bool hasBeenFedFrame;
    bool updateOrientation;
    uint16_t nSamplesAvailableNext;
#ifndef LIB_DEC_REVISION
    int16_t nSamplesRendered;
#endif
    int16_t nTransportChannelsOld;
    int16_t amrwb_rfc4867_flag; /* MIME from rfc4867 is used */
    int16_t sdp_hf_only;        /* RTP payload format parameter: only Header-Full format without zero padding for size collision avoidance */
@@ -119,8 +123,10 @@ static ivas_error IVAS_DEC_VoIP_reconfigure( IVAS_DEC_HANDLE hIvasDec, const uin
static ivas_error IVAS_DEC_Setup( IVAS_DEC_HANDLE hIvasDec, uint16_t *nTcBufferGranularity, uint8_t *nTransportChannels );
static ivas_error IVAS_DEC_GetTcSamples( IVAS_DEC_HANDLE hIvasDec, float *pcmBuf, int16_t *nOutSamples );
static ivas_error IVAS_DEC_RendererFeedTcSamples( IVAS_DEC_HANDLE hIvasDec, const int16_t nSamplesForRendering, int16_t *nSamplesResidual, float *pcmBuf );
#ifndef LIB_DEC_REVISION
static ivas_error IVAS_DEC_GetRenderedSamples( IVAS_DEC_HANDLE hIvasDec, const uint16_t nSamplesForRendering, uint16_t *nSamplesRendered, uint16_t *nSamplesAvailableNext, const IVAS_DEC_PCM_TYPE pcmType, void *pcmBuf );
static ivas_error IVAS_DEC_GetBufferedNumberOfSamples( IVAS_DEC_HANDLE hIvasDec, int16_t *nSamplesBuffered );
#endif
static PCM_RESOLUTION pcm_type_API_to_internal( const IVAS_DEC_PCM_TYPE pcmType );
static void *pcm_buffer_offset( void *buffer, const IVAS_DEC_PCM_TYPE pcmType, const int32_t offset );
static ivas_error set_pcm_buffer_to_zero( void *buffer, const IVAS_DEC_PCM_TYPE pcmType, const int16_t nZeroSamples );
@@ -141,6 +147,9 @@ static void update_voip_rendered20ms( IVAS_DEC_HANDLE hIvasDec, const int16_t nS
/* may return an error but may still have allocated memory - thus run Close also in case of error to release memory */
ivas_error IVAS_DEC_Open(
    IVAS_DEC_HANDLE *phIvasDec, /* i/o: pointer to an IVAS decoder handle to be opened  */
#ifdef LIB_DEC_REVISION
    const int32_t output_Fs, /* i  : output sampling frequency                       */
#endif
    const IVAS_DEC_MODE mode /* i  : compatibility mode (EVS or IVAS)                */
)
{
@@ -171,8 +180,12 @@ ivas_error IVAS_DEC_Open(
    hIvasDec->needNewFrame = false;
    hIvasDec->nTransportChannelsOld = 0;
    hIvasDec->nSamplesAvailableNext = 0;
#ifdef LIB_DEC_REVISION
    hIvasDec->nSamplesFrame = (uint16_t) ( output_Fs / FRAMES_PER_SEC );
#else
    hIvasDec->nSamplesRendered = 0;
    hIvasDec->nSamplesFrame = 0;
#endif
    hIvasDec->hasBeenFedFrame = false;
    hIvasDec->hasBeenFedFirstGoodFrame = false;
    hIvasDec->hasDecodedFirstGoodFrame = false;
@@ -522,6 +535,7 @@ ivas_error IVAS_DEC_Configure(
        hIvasDec->st_ivas->ivas_format = MONO_FORMAT;
    }

#ifndef LIB_DEC_REVISION
    hIvasDec->nSamplesFrame = (uint16_t) ( hDecoderConfig->output_Fs / FRAMES_PER_SEC );
    hIvasDec->nSamplesAvailableNext = 0;
    hIvasDec->nSamplesRendered = 0;
@@ -540,7 +554,7 @@ ivas_error IVAS_DEC_Configure(
        hIvasDec->pcmType = IVAS_DEC_PCM_INT16;
        set_s( (int16_t *) hIvasDec->flushbuffer, 0, CPE_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES );
    }

#endif
    return IVAS_ERR_OK;
}

@@ -770,7 +784,9 @@ ivas_error IVAS_DEC_EnableVoIP(

    hIvasDec->hVoIP->lastDecodedWasActive = 0;
    hIvasDec->hVoIP->hCurrentDataUnit = NULL;
#ifndef LIB_DEC_REVISION
    hIvasDec->hVoIP->nSamplesFrame = (uint16_t) ( hDecoderConfig->output_Fs / FRAMES_PER_SEC );
#endif
    hIvasDec->hVoIP->nSamplesRendered20ms = 0;

#define WMC_TOOL_SKIP
@@ -896,7 +912,9 @@ ivas_error IVAS_DEC_FeedFrame_Serial(

    hIvasDec->needNewFrame = false;
    hIvasDec->hasBeenFedFrame = true;
#ifndef LIB_DEC_REVISION
    hIvasDec->nSamplesRendered = 0;
#endif
    hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;

    return IVAS_ERR_OK;
@@ -1658,9 +1676,12 @@ ivas_error IVAS_DEC_GetSamplesRenderer(
)
{
    ivas_error error;
#ifndef LIB_DEC_REVISION
    int16_t nSamplesToRender;
#endif
    uint16_t nSamplesRendered, nSamplesRendered_loop;
    uint8_t nOutChannels;
    Decoder_Struct *st_ivas;

    nSamplesRendered = 0;
    nOutChannels = 0;
@@ -1677,13 +1698,15 @@ ivas_error IVAS_DEC_GetSamplesRenderer(
        return IVAS_ERR_UNKNOWN;
    }

    st_ivas = hIvasDec->st_ivas;

    if ( hIvasDec->updateOrientation )
    {
        /*----------------------------------------------------------------*
         * Combine orientations
         *----------------------------------------------------------------*/

        if ( ( error = combine_external_and_head_orientations_dec( hIvasDec->st_ivas->hHeadTrackData, hIvasDec->st_ivas->hExtOrientationData, hIvasDec->st_ivas->hCombinedOrientationData ) ) != IVAS_ERR_OK )
        if ( ( error = combine_external_and_head_orientations_dec( st_ivas->hHeadTrackData, st_ivas->hExtOrientationData, st_ivas->hCombinedOrientationData ) ) != IVAS_ERR_OK )
        {
            return error;
        }
@@ -1692,9 +1715,9 @@ ivas_error IVAS_DEC_GetSamplesRenderer(
         * Binaural split rendering setup
         *----------------------------------------------------------------*/

        if ( hIvasDec->st_ivas->hCombinedOrientationData != NULL && ( hIvasDec->st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || hIvasDec->st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
        if ( st_ivas->hCombinedOrientationData != NULL && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
        {
            isar_set_split_rend_ht_setup( &hIvasDec->st_ivas->hSplitBinRend->splitrend, hIvasDec->st_ivas->hCombinedOrientationData->Quaternions, hIvasDec->st_ivas->hCombinedOrientationData->Rmat );
            isar_set_split_rend_ht_setup( &st_ivas->hSplitBinRend->splitrend, st_ivas->hCombinedOrientationData->Quaternions, st_ivas->hCombinedOrientationData->Rmat );
        }

        hIvasDec->updateOrientation = false;
@@ -1710,38 +1733,59 @@ ivas_error IVAS_DEC_GetSamplesRenderer(
    }

    /* check if we are still at the beginning with bad frames, put out zeroes, keep track of subframes */
    if ( !hIvasDec->isInitialized && hIvasDec->st_ivas->bfi )
    if ( !hIvasDec->isInitialized && st_ivas->bfi )
    {
        hIvasDec->hasBeenFedFrame = false;
        set_s( pcmBuf, 0, hIvasDec->st_ivas->hDecoderConfig->nchan_out * nSamplesAsked );
        set_s( pcmBuf, 0, st_ivas->hDecoderConfig->nchan_out * nSamplesAsked );
#ifdef LIB_DEC_REVISION
        nSamplesRendered = nSamplesAsked;
#else
        hIvasDec->nSamplesRendered += nSamplesAsked;
        *nOutSamples = nSamplesAsked;
#endif
        hIvasDec->nSamplesAvailableNext -= nSamplesAsked;
#ifndef LIB_DEC_REVISION
        if ( hIvasDec->nSamplesAvailableNext == 0 )
        {
            hIvasDec->needNewFrame = true;
            *needNewFrame = true;
        }
#endif
    }
    else
    {
        nOutChannels = (uint8_t) hIvasDec->st_ivas->hDecoderConfig->nchan_out;
        nOutChannels = (uint8_t) st_ivas->hDecoderConfig->nchan_out;
        hIvasDec->hasBeenFedFrame = false;

        /* check for possible flushed samples from a rate switch */
        if ( hIvasDec->nSamplesFlushed > 0 )
        {
#ifndef LIB_DEC_REVISION
            void *pPcmBuffer;
#endif
#ifdef DEBUGGING
            assert( hIvasDec->pcmType == pcmType );
#endif
#ifdef LIB_DEC_REVISION
            /* note: offset (rendered samples) is always 0 */
#else
            pPcmBuffer = pcm_buffer_offset( pcmBuf, pcmType, nSamplesRendered * nOutChannels );
#endif
            if ( pcmType == IVAS_DEC_PCM_INT16 )
            {
#ifdef LIB_DEC_REVISION
                mvs2s( (int16_t *) hIvasDec->flushbuffer, pcmBuf, hIvasDec->nSamplesFlushed * nOutChannels );
#else
                mvs2s( (int16_t *) hIvasDec->flushbuffer, pPcmBuffer, hIvasDec->nSamplesFlushed * nOutChannels );
#endif
            }
            else if ( pcmType == IVAS_DEC_PCM_FLOAT )
            {
#ifdef LIB_DEC_REVISION
                mvr2r( (float *) hIvasDec->flushbuffer, pcmBuf, hIvasDec->nSamplesFlushed * nOutChannels );
#else
                mvr2r( (float *) hIvasDec->flushbuffer, pPcmBuffer, hIvasDec->nSamplesFlushed * nOutChannels );
#endif
            }
#ifdef DEBUGGING
            else
@@ -1749,18 +1793,27 @@ ivas_error IVAS_DEC_GetSamplesRenderer(
                assert( 0 && "wrong PCM type for the flush buffer!" );
            }
#endif
#ifdef LIB_DEC_REVISION
            nSamplesRendered = hIvasDec->nSamplesFlushed;
#else
            nSamplesRendered += hIvasDec->nSamplesFlushed;
#endif
            hIvasDec->nSamplesFlushed = 0;
        }

        /* render IVAS frames directly to the output buffer */
#ifdef LIB_DEC_REVISION
        if ( ( error = ivas_jbm_dec_render( st_ivas, nSamplesAsked - nSamplesRendered, &nSamplesRendered_loop, &hIvasDec->nSamplesAvailableNext, pcm_type_API_to_internal( pcmType ), pcm_buffer_offset( pcmBuf, pcmType, nSamplesRendered * nOutChannels ) ) ) != IVAS_ERR_OK )
#else
        nSamplesToRender = nSamplesAsked - nSamplesRendered;
        if ( ( error = IVAS_DEC_GetRenderedSamples( hIvasDec, nSamplesToRender, &nSamplesRendered_loop, &hIvasDec->nSamplesAvailableNext, pcmType, pcm_buffer_offset( pcmBuf, pcmType, nSamplesRendered * nOutChannels ) ) ) != IVAS_ERR_OK )
#endif
        {
            return error;
        }

        nSamplesRendered += nSamplesRendered_loop;
#ifndef LIB_DEC_REVISION
        nSamplesToRender -= nSamplesRendered_loop;
        if ( hIvasDec->nSamplesAvailableNext == 0 )
        {
@@ -1774,10 +1827,25 @@ ivas_error IVAS_DEC_GetSamplesRenderer(
#ifdef NONBE_1293_CRASH_FIRST_FRAME_LOST
        *nOutSamples = nSamplesRendered;
#endif
#endif
    }

#ifdef LIB_DEC_REVISION
    if ( hIvasDec->nSamplesAvailableNext == 0 )
    {
        *needNewFrame = true;
        hIvasDec->needNewFrame = true;
    }
    else
    {
        *needNewFrame = false;
    }

    *nOutSamples = nSamplesRendered;
#else
#ifndef NONBE_1293_CRASH_FIRST_FRAME_LOST
    *nOutSamples = nSamplesRendered;
#endif
#endif

    return IVAS_ERR_OK;
@@ -2146,7 +2214,7 @@ static ivas_error IVAS_DEC_RendererFeedTcSamples(
    return IVAS_ERR_OK;
}


#ifndef LIB_DEC_REVISION
/*---------------------------------------------------------------------*
 * IVAS_DEC_GetRenderedSamples( )
 *
@@ -2205,7 +2273,7 @@ static ivas_error IVAS_DEC_GetBufferedNumberOfSamples(

    return IVAS_ERR_OK;
}

#endif

/*---------------------------------------------------------------------*
 * IVAS_DEC_GetNumObjects( )
@@ -3561,17 +3629,25 @@ ivas_error IVAS_DEC_VoIP_GetSamples(
            nSamplesBuffered = 0;
            if ( hIvasDec->hasBeenFedFirstGoodFrame )
            {
#ifdef LIB_DEC_REVISION
                /* check if the TC buffer already exists, otherweise nothing is buffered anyway */
                if ( st_ivas->hTcBuffer != NULL )
                {
                    nSamplesBuffered = st_ivas->hTcBuffer->n_samples_buffered - st_ivas->hTcBuffer->n_samples_rendered;
                    nSamplesBuffered += hVoIP->nSamplesRendered20ms;
                }
#else
                if ( ( error = IVAS_DEC_GetBufferedNumberOfSamples( hIvasDec, &nSamplesBuffered ) ) != IVAS_ERR_OK )
                {
                    return error;
                }
#endif
            }

            extBufferedSamples = nSamplesBuffered;
            extBufferedTime_ms = extBufferedSamples * 1000 / hDecoderConfig->output_Fs;
            dataUnit = NULL;


            /* pop one access unit from the jitter buffer */
            result = JB4_PopDataUnit( hVoIP->hJBM, systemTimestamp_ms, extBufferedTime_ms, &dataUnit, &scale, &maxScaling );
            if ( result != 0 )
@@ -3671,7 +3747,9 @@ ivas_error IVAS_DEC_VoIP_GetSamples(
            if ( !hIvasDec->hasBeenFedFirstGoodFrame )
            {
                hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
#ifndef LIB_DEC_REVISION
                hIvasDec->nSamplesRendered = 0;
#endif
            }
        }

@@ -3683,8 +3761,9 @@ ivas_error IVAS_DEC_VoIP_GetSamples(
            int16_t nSamplesToZero = min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
            set_pcm_buffer_to_zero( pcm_buffer_offset( pcmBuf, pcmType, ( *nSamplesRendered ) * nOutChannels ), pcmType, nSamplesToZero * nOutChannels );
            *nSamplesRendered += nSamplesToZero;

#ifndef LIB_DEC_REVISION
            hIvasDec->nSamplesRendered += nSamplesToZero;
#endif
            hIvasDec->nSamplesAvailableNext -= nSamplesToZero;
            update_voip_rendered20ms( hIvasDec, nSamplesToZero );
        }
@@ -3741,7 +3820,7 @@ ivas_error IVAS_DEC_VoIP_GetSamples(
/*---------------------------------------------------------------------*
 * update_voip_rendered20ms( )
 *
 * Function to flush remaining audio in VoIP
 * Return number of samples that have been rendered since the last 20ms render border
 *---------------------------------------------------------------------*/

static void update_voip_rendered20ms(
@@ -3753,12 +3832,20 @@ static void update_voip_rendered20ms(
    nSamplesRenderedTotal = hIvasDec->hVoIP->nSamplesRendered20ms + nSamplesRendered;

    /* we have crossed a 20ms border, reset the time scaling done flag */
#ifdef LIB_DEC_REVISION
    if ( nSamplesRenderedTotal >= hIvasDec->nSamplesFrame )
#else
    if ( nSamplesRenderedTotal >= hIvasDec->hVoIP->nSamplesFrame )
#endif
    {
        hIvasDec->timeScalingDone = 0;
    }

#ifdef LIB_DEC_REVISION
    hIvasDec->hVoIP->nSamplesRendered20ms = nSamplesRenderedTotal % hIvasDec->nSamplesFrame;
#else
    hIvasDec->hVoIP->nSamplesRendered20ms = nSamplesRenderedTotal % hIvasDec->hVoIP->nSamplesFrame;
#endif

    return;
}
@@ -3795,12 +3882,17 @@ ivas_error IVAS_DEC_Flush(
    error = IVAS_ERR_OK;
    if ( nSamplesToRender > 0 && hIvasDec->st_ivas->ivas_format != MONO_FORMAT )
    {
#ifdef LIB_DEC_REVISION
        error = ivas_jbm_dec_render( hIvasDec->st_ivas, nSamplesToRender, &nSamplesFlushedLocal, &hIvasDec->nSamplesAvailableNext, pcmType, pcmBuf );
#else
        error = IVAS_DEC_GetRenderedSamples( hIvasDec, nSamplesToRender, &nSamplesFlushedLocal, &hIvasDec->nSamplesAvailableNext, pcmType, pcmBuf );
#endif
    }
    else
    {
        *nSamplesFlushed = 0;
    }

    return error;
}

+3 −0
Original line number Diff line number Diff line
@@ -114,6 +114,9 @@ typedef ivas_error ( *JbmTraceFileWriterFn )( const void *data, void *writer );
/*! r: error code */
ivas_error IVAS_DEC_Open(
    IVAS_DEC_HANDLE *phIvasDec,                 /* i/o: pointer to an IVAS decoder handle to be opened                          */
#ifdef LIB_DEC_REVISION
    const int32_t output_Fs,                    /* i  : output sampling frequency                                               */
#endif
    IVAS_DEC_MODE mode                          /* i  : compatibility mode (EVS or IVAS)                                        */
);