Commit e75628ae authored by Archit Tamarapu's avatar Archit Tamarapu
Browse files

add delay alignment for multiple inputs in external renderer

parent 74caff91
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -2004,6 +2004,8 @@ int main(
#endif
    }

    /* TODO a flush method is needed to empty the delay buffers */

    /* add zeros at the end to have equal length of synthesized signals */
    if ( audioWriter != NULL )
    {
+235 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2025 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 "ivas_td_ring_buffer.h"
#include "ivas_error_utils.h"
#include <stdlib.h>
#include <assert.h>

struct TdRingBuf
{
    float *data; /* samples in interleaved layout */
    uint32_t capacity;
    uint16_t num_channels;
    uint32_t write_pos;
    uint32_t read_pos;
    int16_t is_full;
};

ivas_error TD_RINGBUF_Open( TD_RINGBUF_HANDLE *ph, uint32_t capacity_per_channel, uint16_t num_channels )
{
    TD_RINGBUF_HANDLE h;
    uint32_t capacity;
    capacity = capacity_per_channel * num_channels;

    h = malloc( sizeof( struct TdRingBuf ) );
    if ( h == NULL )
    {
        return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Failed to allocate memory for TD ring buffer\n" );
    }
    h->data = NULL;
    h->capacity = 0;
    h->num_channels = num_channels;
    h->write_pos = 0;
    h->read_pos = 0;
    h->is_full = 0;
    *ph = h;

    h->data = malloc( capacity * sizeof( float ) );
    if ( h->data == NULL )
    {
        return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Failed to allocate memory for TD ring buffer\n" );
    }
    h->capacity = capacity;

    return IVAS_ERR_OK;
}

void TD_RINGBUF_Close( TD_RINGBUF_HANDLE *ph )
{
    TD_RINGBUF_HANDLE h;

    if ( ph == NULL )
    {
        return;
    }
    h = *ph;

    if ( h == NULL )
    {
        return;
    }

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

    free( h );
    *ph = NULL;

    return;
}

/* Returns total number of buffered samples (includes number of channels) */
static uint32_t total_size( TD_RINGBUF_HANDLE h )
{
    if ( TD_RINGBUF_IsFull( h ) )
    {
        return h->capacity;
    }

    if ( h->read_pos <= h->write_pos )
    {
        return h->write_pos - h->read_pos;
    }
    /* else wrap around */
    return h->write_pos + h->capacity - h->read_pos;
}

static int16_t has_space_for_num_samples( TD_RINGBUF_HANDLE h, uint32_t num_samples )
{
    return (int16_t) ( total_size( h ) + num_samples <= h->capacity );
}

void TD_RINGBUF_Push( TD_RINGBUF_HANDLE h, const float *data, uint32_t num_samples_per_channel )
{
    uint32_t s;
    uint16_t c;

    assert( has_space_for_num_samples( h, num_samples_per_channel * h->num_channels ) );

    for ( s = 0; s < num_samples_per_channel; ++s )
    {
        for ( c = 0; c < h->num_channels; ++c )
        {
            h->data[h->write_pos] = *( data + c * num_samples_per_channel + s );
            ++h->write_pos;

            if ( h->write_pos == h->capacity )
            {
                h->write_pos = 0;
            }
        }
    }

    if ( h->read_pos == h->write_pos )
    {
        h->is_full = 1;
    }

    return;
}

void TD_RINGBUF_PushZeros( TD_RINGBUF_HANDLE h, uint32_t num_samples_per_channel )
{
    uint32_t s;
    uint16_t c;

    assert( has_space_for_num_samples( h, num_samples_per_channel * h->num_channels ) );
    if ( !num_samples_per_channel )
    {
        return;
    }

    for ( s = 0; s < num_samples_per_channel; ++s )
    {
        for ( c = 0; c < h->num_channels; ++c )
        {
            h->data[h->write_pos] = 0.f;
            ++h->write_pos;

            if ( h->write_pos == h->capacity )
            {
                h->write_pos = 0;
            }
        }
    }

    if ( h->read_pos == h->write_pos )
    {
        h->is_full = 1;
    }

    return;
}

void TD_RINGBUF_Pop( TD_RINGBUF_HANDLE h, float *data, uint32_t num_samples_per_channel )
{
    uint32_t s;
    uint16_t c;

    assert( total_size( h ) >= num_samples_per_channel * h->num_channels );

    for ( s = 0; s < num_samples_per_channel; ++s )
    {
        for ( c = 0; c < h->num_channels; ++c )
        {
            *( data + c * num_samples_per_channel + s ) = h->data[h->read_pos];
            ++h->read_pos;

            if ( h->read_pos == h->capacity )
            {
                h->read_pos = 0;
            }
        }
    }

    if ( h->is_full )
    {
        h->is_full = 0;
    }

    return;
}

int16_t TD_RINGBUF_IsEmpty( TD_RINGBUF_HANDLE h )
{
    return (int16_t) ( h->read_pos == h->write_pos && !h->is_full );
}

int16_t TD_RINGBUF_IsFull( TD_RINGBUF_HANDLE h )
{
    return h->is_full;
}

uint32_t TD_RINGBUF_Size( TD_RINGBUF_HANDLE h )
{
    return total_size( h ) / (uint32_t) h->num_channels;
}

void TD_RINGBUF_Clear( TD_RINGBUF_HANDLE h )
{
    h->read_pos = h->write_pos;
    h->is_full = 0;
    return;
}
+106 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2025 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_TD_RING_BUFFER_H
#define IVAS_TD_RING_BUFFER_H

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

typedef struct TdRingBuf *TD_RINGBUF_HANDLE;

/*---------------------------------------------------------------------*
 * TD_RINGBUF_Open()
 *
 * Allocate a ring buffer for TD data with the given capacity of TD samples per channel.
 *
 * May return IVAS_ERR_FAILED_ALLOC on failed allocation, or IVAS_ERR_OK otherwise.
 *---------------------------------------------------------------------*/
ivas_error TD_RINGBUF_Open( TD_RINGBUF_HANDLE *ph, uint32_t capacity_per_channel, uint16_t num_channels );

/*---------------------------------------------------------------------*
 * TD_RINGBUF_Close()
 *
 * Dellocate TD ring buffer. The given handle will be set to NULL.
 *---------------------------------------------------------------------*/
void TD_RINGBUF_Close( TD_RINGBUF_HANDLE *ph );

/*---------------------------------------------------------------------*
 * TD_RINGBUF_Push()
 *
 * Push samples onto the back of the TD ring buffer.
 *---------------------------------------------------------------------*/
void TD_RINGBUF_Push( TD_RINGBUF_HANDLE h, const float *data, uint32_t num_samples_per_channel );

/*---------------------------------------------------------------------*
 * TD_RINGBUF_PushZeros()
 *
 * Push zero samples onto the back of the TD ring buffer.
 *---------------------------------------------------------------------*/
void TD_RINGBUF_PushZeros( TD_RINGBUF_HANDLE h, uint32_t num_samples_per_channel );

/*---------------------------------------------------------------------*
 * TD_RINGBUF_Pop()
 *
 * Pop samples from the front of the TD ring buffer.
 *---------------------------------------------------------------------*/
void TD_RINGBUF_Pop( TD_RINGBUF_HANDLE h, float *data, uint32_t num_samples_per_channel );

/*---------------------------------------------------------------------*
 * TD_RINGBUF_IsEmpty()
 *
 * Returns 1 if the ring buffer is empty, or 0 otherwise.
 *---------------------------------------------------------------------*/
int16_t TD_RINGBUF_IsEmpty( TD_RINGBUF_HANDLE h );

/*---------------------------------------------------------------------*
 * TD_RINGBUF_IsFull()
 *
 * Returns 1 if the ring buffer is full, or 0 otherwise.
 *---------------------------------------------------------------------*/
int16_t TD_RINGBUF_IsFull( TD_RINGBUF_HANDLE h );

/*---------------------------------------------------------------------*
 * TD_RINGBUF_Size()
 *
 * Returns number of buffered samples per channel.
 *---------------------------------------------------------------------*/
uint32_t TD_RINGBUF_Size( TD_RINGBUF_HANDLE h );

/*---------------------------------------------------------------------*
 * TD_RINGBUF_Clear()
 *
 * Remove all samples from the buffer.
 *---------------------------------------------------------------------*/
void TD_RINGBUF_Clear( TD_RINGBUF_HANDLE h );

#endif /* IVAS_TD_RING_BUFFER_H */
+277 −71

File changed.

Preview size limit exceeded, changes collapsed.