Commit b978b6c3 authored by TYAGIRIS's avatar TYAGIRIS
Browse files

post rend cleanup, stack optimizations

parent d6118e12
Loading
Loading
Loading
Loading
Loading
+3 −22
Original line number Diff line number Diff line
@@ -1249,30 +1249,11 @@ int main(
            }
        }

        if ( args.inConfig.numBinBuses != 0 )
        {
        if ( ( error = ISAR_POST_REND_GetSplitBinauralSamples( hIsarPostRend, outBuffer, &splitBinNeedsNewFrame ) ) != IVAS_ERR_OK )
        {
            fprintf( stderr, "Error: %s\n", ivas_error_to_string( error ) );
            exit( -1 );
        }
        }
        else if ( args.outConfig.audioConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || args.outConfig.audioConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
        {
            if ( ( error = ISAR_POST_REND_GetSplitBinauralBitstream( hIsarPostRend, outBuffer, &bitsBuffer ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "Error: %s\n", ivas_error_to_string( error ) );
                exit( -1 );
            }
        }
        else
        {
            if ( ( error = ISAR_POST_REND_getSamples( hIsarPostRend, outBuffer, NULL ) ) != IVAS_ERR_OK )
            {
                fprintf( stderr, "Error %s\n", ivas_error_to_string( error ) );
                exit( -1 );
            }
        }

        int16_t num_out_channels;
        num_out_channels = outBuffer.config.numChannels;
+23 −0
Original line number Diff line number Diff line
@@ -1710,6 +1710,29 @@ void isar_init_split_rend_handles(
    return;
}

/*-------------------------------------------------------------------------
 * Function closeSplitRend()
 *
 *
 *------------------------------------------------------------------------*/

void closeSplitRend(
    SPLIT_REND_WRAPPER *pSplitRendWrapper,
    IVAS_REND_AudioBuffer *pSplitRendEncBuffer )
{
    isar_split_renderer_close( pSplitRendWrapper );

    if ( pSplitRendEncBuffer->data != NULL )
    {
        free( pSplitRendEncBuffer->data );
        pSplitRendEncBuffer->data = NULL;
    }

    pSplitRendEncBuffer->config.numChannels = 0;
    pSplitRendEncBuffer->config.numSamplesPerChannel = 0;

    return;
}

/*-------------------------------------------------------------------------
 * Function split_renderer_open_lc3plus()
+7 −257
Original line number Diff line number Diff line
@@ -106,7 +106,6 @@ typedef struct
    const IVAS_REND_HeadRotData *pHeadRotData;
    const RENDER_CONFIG_HANDLE *hhRendererConfig;
    const int16_t *pSplitRendBFI;
    const SPLIT_REND_WRAPPER *pSplitRendWrapper;
} rendering_context;

/* Common base for input structs */
@@ -142,8 +141,6 @@ struct ISAR_POST_REND

    AUDIO_CONFIG inputConfig;
    AUDIO_CONFIG outputConfig;
    SPLIT_REND_WRAPPER splitRendWrapper;
    IVAS_REND_AudioBuffer splitRendEncBuffer;

    IVAS_REND_HeadRotData headRotData;
    int16_t splitRendBFI;
@@ -254,38 +251,6 @@ static void convertInternalBitsBuffToBitsBuffer(
    return;
}

static void copyBufferToCLDFBarray(
    const IVAS_REND_AudioBuffer buffer,
    float re[MAX_OUTPUT_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX],
    float im[MAX_OUTPUT_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX] )
{
    uint32_t smplIdx, slotIdx;
    uint32_t numCldfbSamples, num_bands;
    uint32_t chnlIdx;
    const float *readPtr;

    assert( ( buffer.config.is_cldfb == 1 ) && "for time domain input call copyBufferTo2dArray()" );
    readPtr = buffer.data;
    numCldfbSamples = ( (uint32_t) buffer.config.numSamplesPerChannel ) >> 1;
    num_bands = numCldfbSamples / CLDFB_NO_COL_MAX;
    for ( chnlIdx = 0; chnlIdx < (uint32_t) buffer.config.numChannels; ++chnlIdx )
    {
        for ( slotIdx = 0; slotIdx < CLDFB_NO_COL_MAX; ++slotIdx )
        {
            for ( smplIdx = 0; smplIdx < num_bands; ++smplIdx )
            {
                re[chnlIdx][slotIdx][smplIdx] = *readPtr++;
            }
            for ( smplIdx = 0; smplIdx < num_bands; ++smplIdx )
            {
                im[chnlIdx][slotIdx][smplIdx] = *readPtr++;
            }
        }
    }

    return;
}

static void copyBufferTo2dArray(
    const IVAS_REND_AudioBuffer buffer,
    float array[][L_FRAME48k] )
@@ -511,7 +476,6 @@ static rendering_context getRendCtx(
    ctx.pOutSampleRate = &hIvasRend->sampleRateOut;
    ctx.pHeadRotData = &hIvasRend->headRotData;
    ctx.pSplitRendBFI = &hIvasRend->splitRendBFI;
    ctx.pSplitRendWrapper = &hIvasRend->splitRendWrapper;

    return ctx;
}
@@ -792,8 +756,6 @@ ivas_error ISAR_POST_REND_open(
    }

    /* Initialize inputs */
    isar_init_split_rend_handles( &hIvasRend->splitRendWrapper );
    hIvasRend->splitRendEncBuffer.data = NULL;

    for ( i = 0; i < RENDERER_MAX_BIN_INPUTS; ++i )
    {
@@ -1003,49 +965,6 @@ static ivas_error findFreeInputSlot(
}


static int16_t getCldfbRendFlag(
    ISAR_POST_REND_HANDLE hIvasRend, /* i  : Renderer handle               */
    const ISAR_POST_REND_AudioConfigType new_configType )
{
    int16_t numMasaInputs = 0, numSbaInputs = 0, numIsmInputs = 0, numMcInputs = 0;
    int16_t isCldfbRend;

    (void) new_configType;

    isCldfbRend = 0;

    if ( numIsmInputs > 0 || numMcInputs > 0 )
    {
        isCldfbRend = 0;
    }
    else if ( ( numMasaInputs > 0 ) || ( numSbaInputs > 0 && hIvasRend->splitRenderConfig.rendererSelection == ISAR_SPLIT_REND_RENDERER_SELECTION_FASTCONV ) )
    {
        isCldfbRend = 1;
    }

    return isCldfbRend;
}


void closeSplitRend(
    SPLIT_REND_WRAPPER *pSplitRendWrapper,
    IVAS_REND_AudioBuffer *pSplitRendEncBuffer )
{
    isar_split_renderer_close( pSplitRendWrapper );

    if ( pSplitRendEncBuffer->data != NULL )
    {
        free( pSplitRendEncBuffer->data );
        pSplitRendEncBuffer->data = NULL;
    }

    pSplitRendEncBuffer->config.numChannels = 0;
    pSplitRendEncBuffer->config.numSamplesPerChannel = 0;

    return;
}


/*-------------------------------------------------------------------*
 * ISAR_POST_REND_AddInput()
 *
@@ -1349,68 +1268,6 @@ int16_t ISAR_POST_REND_GetRenderConfig(
    return IVAS_ERR_OK;
}


/*-------------------------------------------------------------------*
 * ISAR_POST_REND_FeedRenderConfig()
 *
 *
 *-------------------------------------------------------------------*/

int16_t ISAR_POST_REND_FeedRenderConfig(
    ISAR_POST_REND_HANDLE hIvasRend,                    /* i/o: IVAS decoder handle         */
    const ISAR_SPLIT_REND_CONFIG_DATA splitRenderConfig /* i  : Render configuration struct */
)
{
    ISAR_SPLIT_REND_CONFIG_HANDLE hRenderConfig;
    ivas_error error;

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

    hRenderConfig = &hIvasRend->splitRenderConfig;

#ifdef DEBUGGING
    hRenderConfig->renderer_type_override = renderConfig.renderer_type_override;
#endif

    /* Overwrite any pose correction settings if 0 DOF (no pose correction) was selected */
    if ( hRenderConfig->dof == 0 )
    {
        hRenderConfig->poseCorrectionMode = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE;
    }

    hRenderConfig->codec = splitRenderConfig.codec;

    if ( ( error = isar_split_rend_validate_config( hRenderConfig, ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0 ) ) != IVAS_ERR_OK )
    {
        return error;
    }

    /* Must re-initialize split rendering config in case renderer config is updated after adding renderer inputs */
    /* if its not initialized yet then no need to re-initialize, initialization will happen while adding inputs*/
    if ( hIvasRend->splitRendEncBuffer.data != NULL )
    {
        int16_t cldfb_in_flag;
        cldfb_in_flag = getCldfbRendFlag( hIvasRend, ISAR_POST_REND_AUDIO_CONFIG_TYPE_UNKNOWN );
        closeSplitRend( &hIvasRend->splitRendWrapper, &hIvasRend->splitRendEncBuffer );

        if ( ( error = isar_split_rend_choose_default_codec( &hIvasRend->splitRenderConfig.codec, &hIvasRend->splitRenderConfig.codec_frame_size_ms, cldfb_in_flag, hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM, hIvasRend->num_subframes ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        if ( ( error = initSplitRend( &hIvasRend->splitRendWrapper, &hIvasRend->splitRendEncBuffer, &hIvasRend->splitRenderConfig, hIvasRend->headRotData, hIvasRend->sampleRateOut, hIvasRend->outputConfig, cldfb_in_flag, hIvasRend->num_subframes ) ) != IVAS_ERR_OK )
        {
            return error;
        }
    }

    return IVAS_ERR_OK;
}


/*-------------------------------------------------------------------*
 * ISAR_POST_REND_FeedSplitBinauralBitstream()
 *
@@ -1540,7 +1397,7 @@ ivas_error ISAR_POST_REND_SetSplitRendBFI(
static ivas_error splitBinLc3plusDecode(
    ISAR_SPLIT_POST_REND_WRAPPER *hSplitBin,
    ISAR_SPLIT_REND_BITS_HANDLE bits,
    float outputBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k],
    float outputBuffer[BINAURAL_CHANNELS][L_FRAME48k],
    ISAR_SPLIT_REND_POSE_CORRECTION_MODE pose_correction )
{
    ivas_error error;
@@ -1559,7 +1416,7 @@ static ivas_error splitBinLc3plusDecode(
    lc3plusBitrateId = ISAR_SPLIT_REND_BITStream_read_int32( bits, 8 );
    lc3plusBitstreamSize = isar_get_lc3plus_size_from_id( (int8_t) lc3plusBitrateId, pose_correction, (int16_t) ( hSplitBin->hLc3plusDec->config.ivas_frame_duration_us / 1000 ) );

    for ( int16_t i = 0; i < BINAURAL_CHANNELS * hSplitBin->multiBinPoseData.num_poses; ++i )
    for ( int16_t i = 0; i < BINAURAL_CHANNELS; ++i )
    {
        channel_ptrs[i] = outputBuffer[i];
    }
@@ -1588,7 +1445,7 @@ static ivas_error renderSplitBinauralWithPostRot(
    IVAS_QUATERNION QuaternionsPost[MAX_PARAM_SPATIAL_SUBFRAMES];
    int16_t sf_idx, ch_idx;
    ISAR_SPLIT_REND_BITS_DATA bits;
    float tmpCrendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    float tmpCrendBuffer[BINAURAL_CHANNELS][L_FRAME48k];
    float tmpCrendBuffer_sf[BINAURAL_CHANNELS][L_FRAME48k];
    ISAR_SPLIT_POST_REND_WRAPPER *hSplitBin;
    int8_t isPostRendInputCldfb;
@@ -1892,14 +1749,12 @@ static ivas_error renderActiveInputsSplitBin(

ivas_error ISAR_POST_REND_getSamples(
    ISAR_POST_REND_HANDLE hIvasRend, /* i/o: Renderer handle          */
    IVAS_REND_AudioBuffer outAudio /* i/o: buffer for output audio  */,
    ISAR_POST_REND_BitstreamBuffer *hBits /*i/o: buffer for input/output bitstream. Needed in split rendering mode*/
    IVAS_REND_AudioBuffer outAudio   /* i/o: buffer for output audio  */
)
{
    ivas_error error;
    int16_t numOutChannels;
    int16_t cldfb2tdSampleFact;
    IVAS_REND_AudioBuffer outAudioOrig;

    /* Validate function arguments */
    if ( hIvasRend == NULL || outAudio.data == NULL )
@@ -1921,8 +1776,6 @@ ivas_error ISAR_POST_REND_getSamples(
    }

    if ( isar_getAudioConfigType( hIvasRend->outputConfig ) == ISAR_POST_REND_AUDIO_CONFIG_TYPE_BINAURAL &&
         hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED &&
         hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM &&
         ( outAudio.config.numSamplesPerChannel * 1000 / cldfb2tdSampleFact ) != ( hIvasRend->num_subframes * BINAURAL_RENDERING_FRAME_SIZE_MS ) * hIvasRend->sampleRateOut )
    {
        return IVAS_ERROR( IVAS_ERR_INVALID_BUFFER_SIZE, "Binaural rendering requires specific frame size" );
@@ -1941,121 +1794,21 @@ ivas_error ISAR_POST_REND_getSamples(
    /* Clear original output buffer */
    set_zero( outAudio.data, outAudio.config.numChannels * outAudio.config.numSamplesPerChannel );

    outAudioOrig = outAudio;
    /* Use internal buffer if outputting split rendering bitstream */
    if ( ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) ||
         ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    {
        int16_t num_poses_orig;
        num_poses_orig = hIvasRend->splitRendWrapper.multiBinPoseData.num_poses;
        outAudio = hIvasRend->splitRendEncBuffer;
        isar_renderSplitGetMultiBinPoseData( &hIvasRend->splitRenderConfig, &hIvasRend->splitRendWrapper.multiBinPoseData, hIvasRend->headRotData.sr_pose_pred_axis );
        assert( num_poses_orig == hIvasRend->splitRendWrapper.multiBinPoseData.num_poses && "number of poses should not change dynamically" );

        /* Clear output buffer for split rendering bitstream */
        set_zero( outAudio.data, outAudio.config.numChannels * outAudio.config.numSamplesPerChannel );
    }

    if ( ( error = renderActiveInputsSplitBin( hIvasRend, outAudio ) ) != IVAS_ERR_OK )
    {
        return error;
    }

    if ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    {
        ISAR_SPLIT_REND_BITS_DATA bits;
        int16_t cldfb_in_flag;
        float Cldfb_RealBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
        float Cldfb_ImagBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
        int16_t ch;
        int16_t ro_md_flag;
        float *tmpBinaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS], tmpBinaural_buff[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][L_FRAME48k];

        for ( ch = 0; ch < MAX_OUTPUT_CHANNELS; ch++ )
        {
            tmpBinaural[ch] = tmpBinaural_buff[ch];
        }

        if ( outAudio.config.is_cldfb == 1 )
        {
            cldfb_in_flag = 1;
            copyBufferToCLDFBarray( outAudio, Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural );
        }
        else
        {
            cldfb_in_flag = 0;
            copyBufferTo2dArray( outAudio, tmpBinaural_buff );
        }

        /* Encode split rendering bitstream */
        convertBitsBufferToInternalBitsBuff( *hBits, &bits );

        ro_md_flag = 0;

        if ( ( error = isar_renderMultiBinToSplitBinaural( &hIvasRend->splitRendWrapper, hIvasRend->headRotData.headPositions[0], hIvasRend->splitRenderConfig.splitRendBitRate, hIvasRend->splitRenderConfig.codec, hIvasRend->splitRenderConfig.codec_frame_size_ms,
                                                           &bits, Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, ( const int16_t )( ( BINAURAL_MAXBANDS * hIvasRend->sampleRateOut ) / 48000 ), tmpBinaural, 1, cldfb_in_flag, ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0, ro_md_flag ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        convertInternalBitsBuffToBitsBuffer( hBits, bits );

        /* reset to outAudioOrig in case of PCM output */
        outAudio = outAudioOrig;

        if ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
        {
            accumulate2dArrayToBuffer( tmpBinaural_buff, &outAudio );
        }
    }

    if ( outAudio.config.is_cldfb == 0 )
    {
#ifndef DISABLE_LIMITER
#ifdef DEBUGGING
    hIvasRend->numClipping +=
#endif
        limitRendererOutput( hIvasRend->hLimiter, outAudio.data, outAudio.config.numSamplesPerChannel, IVAS_LIMITER_THRESHOLD );
#endif
    }

    return IVAS_ERR_OK;
}


/*-------------------------------------------------------------------*
 * ISAR_POST_REND_GetSplitBinauralBitstream()
 *
 *
 *-------------------------------------------------------------------*/

ivas_error ISAR_POST_REND_GetSplitBinauralBitstream(
    ISAR_POST_REND_HANDLE hIvasRend,      /* i/o: Renderer handle             */
    IVAS_REND_AudioBuffer outAudio,       /* i/o: buffer for output audio     */
    ISAR_POST_REND_BitstreamBuffer *hBits /* o  : buffer for output bitstream */
)
{
    int16_t cldfb_in_flag;

    cldfb_in_flag = getCldfbRendFlag( hIvasRend, ISAR_POST_REND_AUDIO_CONFIG_TYPE_UNKNOWN );
    hIvasRend->splitRendEncBuffer.config.is_cldfb = cldfb_in_flag;

    if ( hIvasRend->splitRenderConfig.dof == 0 || hIvasRend->splitRenderConfig.poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE )
    {
        hIvasRend->splitRendEncBuffer.config.numSamplesPerChannel = outAudio.config.numSamplesPerChannel;
    }
    else
    {
        hIvasRend->splitRendEncBuffer.config.numSamplesPerChannel = (int16_t) ( hIvasRend->sampleRateOut / FRAMES_PER_SEC );
    }
    hIvasRend->splitRendEncBuffer.config.numSamplesPerChannel *= cldfb_in_flag ? 2 : 1;

    /* hIvasRend->splitRendEncBuffer used for BINAURAL_SPLIT_CODED output
       outAudio used for BINAURAL_SPLIT_PCM output */
    return ISAR_POST_REND_getSamples( hIvasRend, outAudio, hBits );
}


/*-------------------------------------------------------------------*
 * ISAR_POST_REND_GetSplitBinauralSamples()
 *
@@ -2069,7 +1822,7 @@ ivas_error ISAR_POST_REND_GetSplitBinauralSamples(
{
    ivas_error error;

    if ( ( error = ISAR_POST_REND_getSamples( hIvasRend, outAudio, NULL ) ) != IVAS_ERR_OK )
    if ( ( error = ISAR_POST_REND_getSamples( hIvasRend, outAudio ) ) != IVAS_ERR_OK )
    {
        return error;
    }
@@ -2106,9 +1859,6 @@ void ISAR_POST_REND_Close(

    ivas_limiter_close( &hIvasRend->hLimiter );

    /* Split binaural rendering */
    closeSplitRend( &hIvasRend->splitRendWrapper, &hIvasRend->splitRendEncBuffer );

    free( hIvasRend );
    *phIvasRend = NULL;

+1 −13
Original line number Diff line number Diff line
@@ -165,11 +165,6 @@ int16_t ISAR_POST_REND_GetRenderConfig(
    const ISAR_SPLIT_REND_CONFIG_HANDLE splitRenderConfig       /* o  : Render configuration handle                         */
);

int16_t ISAR_POST_REND_FeedRenderConfig(
    ISAR_POST_REND_HANDLE hIvasRend,                            /* i/o: IVAS decoder handle                                 */
    const ISAR_SPLIT_REND_CONFIG_DATA splitRenderConfig         /* o  : Render configuration handle                         */
);

ivas_error ISAR_POST_REND_FeedSplitBinauralBitstream(
    ISAR_POST_REND_HANDLE hIvasRend,                            /* i/o: Renderer handle                                     */
    const ISAR_POST_REND_InputId inputId,                       /* i  : ID of the input                                     */
@@ -182,12 +177,6 @@ ivas_error ISAR_POST_REND_GetSplitBinauralSamples(
    bool* needNewFrame
);

ivas_error ISAR_POST_REND_GetSplitBinauralBitstream(
    ISAR_POST_REND_HANDLE hIvasRend,                            /* i/o: Renderer handle                                     */
    IVAS_REND_AudioBuffer outAudio,                             /* i/o: buffer for output audio                             */
    ISAR_POST_REND_BitstreamBuffer *hBits                       /* o  : buffer for output bitstream                         */
);

ivas_error ISAR_POST_REND_SetHeadRotation(
    ISAR_POST_REND_HANDLE hIvasRend,                            /* i/o: Renderer handle                                     */
    const IVAS_QUATERNION headRot,                              /* i  : head orientations for next rendering call           */
@@ -247,8 +236,7 @@ ivas_error ISAR_POST_REND_GetCombinedOrientation(

ivas_error ISAR_POST_REND_getSamples(
    ISAR_POST_REND_HANDLE hIvasRend,                            /* i/o: Renderer handle                                     */
    IVAS_REND_AudioBuffer outAudio,                             /* i/o: buffer for output audio                             */
    ISAR_POST_REND_BitstreamBuffer *hBits                       /*i/o: buffer for input/output bitstream. Needed in split rendering mode*/
    IVAS_REND_AudioBuffer outAudio                             /* i/o: buffer for output audio                             */
);

/* Functions to be called after rendering */