Commit 5b398160 authored by Jan Kiene's avatar Jan Kiene
Browse files

add ring buffer file

parent 48681f8c
Loading
Loading
Loading
Loading
Loading
+346 −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 "basop32.h"
#include "move.h"
#include "typedef.h"
#include <assert.h>
#include <stdlib.h>
#include "ivas_error_utils.h"
#include "ivas_prot_rend_fx.h"
#include "wmc_auto.h"


/*-----------------------------------------------------------------------*
 * Local function prototypes
 *-----------------------------------------------------------------------*/

static UWord32 ivas_td_ringbuf_total_size(
    TD_RINGBUF_HANDLE h )
{
    UWord32 total_size;

    IF( h->is_full )
    {
        total_size = h->capacity;
    }

    IF( h->read_pos <= h->write_pos )
    {
        total_size = L_sub( h->write_pos, h->read_pos );
    }
    /* else wrap around */
    total_size = L_add( h->write_pos, L_sub( h->capacity, h->read_pos ) );

    return total_size;
}


static Word16 ivas_td_ringbuf_has_space_for_num_samples(
    TD_RINGBUF_HANDLE h,
    const UWord32 num_samples )
{
    // float was : return (Word16) ( ivas_td_ringbuf_total_size( h ) + num_samples <= h->capacity );
    Word16 has_space;
    UWord32 total_size_after;

    move16();
    has_space = 0;

    total_size_after = L_add( ivas_td_ringbuf_total_size( h ), num_samples );
    IF( LE_32( total_size_after, h->capacity ) )
    {
        has_space = 1;
    }
    return has_space;
}


/*-----------------------------------------------------------------------*
 * Global function definitions
 *-----------------------------------------------------------------------*/

/*---------------------------------------------------------------------*
 * ivas_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 ivas_TD_RINGBUF_Open(
    TD_RINGBUF_HANDLE *ph,              /* i/o: Ring buffer handle                     */
    const UWord32 capacity_per_channel, /* i  : Number of samples stored per channel   */
    const UWord16 num_channels          /* i  : Number of channels                     */
)
{
    TD_RINGBUF_HANDLE h;
    UWord32 capacity;

    // TODO: how to integer multiplication 32 x 16 ?
    capacity = L_mult0( capacity_per_channel, num_channels );

    h = malloc( sizeof( TD_RINGBUF_DATA ) );
    IF( h == NULL )
    {
        return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Failed to allocate memory for TD ring buffer\n" );
    }
    // TODO: move for pointer assignment?
    h->data = NULL;
    move32();
    h->capacity = 0;
    move16();
    h->num_channels = num_channels;
    move32();
    h->write_pos = 0;
    move32();
    h->read_pos = 0;
    move16();
    h->is_full = 0;
    // TODO: move for pointer assignment?
    *ph = h;

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

    move32();
    h->capacity = capacity;

    return IVAS_ERR_OK;
}


/*---------------------------------------------------------------------*
 * ivas_TD_RINGBUF_Close()
 *
 * Dellocate TD ring buffer. The given handle will be set to NULL.

 *---------------------------------------------------------------------*/

void ivas_TD_RINGBUF_Close(
    TD_RINGBUF_HANDLE *ph /* i/o: Ring buffer handle */
)
{
    TD_RINGBUF_HANDLE h;

    IF( ph == NULL )
    {
        return;
    }
    // TODO: move for pointer assignment?
    h = *ph;

    IF( h == NULL )
    {
        return;
    }

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

    free( h );
    // TODO: move for pointer assignment?
    *ph = NULL;

    return;
}


/*---------------------------------------------------------------------*
 * ivas_TD_RINGBUF_Push()
 *
 * Push samples onto the back of the TD ring buffer.
 * Returns total number of buffered samples (includes number of channels)
 *---------------------------------------------------------------------*/

void ivas_TD_RINGBUF_Push(
    TD_RINGBUF_HANDLE h,                  /* i/o: Ring buffer handle                       */
    const Word32 *data,                   /* i  : Input data                               */
    const UWord32 num_samples_per_channel /* i  : Number of samples per channel to store   */
)
{
    UWord32 s;
    UWord16 c;

    // TODO: integer multiplication for 32 * 16?
    assert( ivas_td_ringbuf_has_space_for_num_samples( h, L_mult0( num_samples_per_channel, h->num_channels ) ) );

    FOR( s = 0; s < num_samples_per_channel; ++s )
    {
        FOR( c = 0; c < h->num_channels; ++c )
        {
            UWord32 shift_val;

            /* pointer is advance by c * num_samples_per_channel + s */
            shift_val = L_mult0( c, num_samples_per_channel );
            shift_val = L_add( s, shift_val );

            // TODO: move for pointer assignment?
            h->data[h->write_pos] = *( data + shift_val );
            ++h->write_pos;

            IF( EQ_32( h->write_pos, h->capacity ) )
            {
                move32();
                h->write_pos = 0;
            }
        }
    }

    IF( EQ_32( h->read_pos, h->write_pos ) )
    {
        move32();
        h->is_full = 1;
    }

    return;
}


/*---------------------------------------------------------------------*
 * ivas_TD_RINGBUF_PushZeros()
 *
 * Push zero samples onto the back of the TD ring buffer.
 *---------------------------------------------------------------------*/

void ivas_TD_RINGBUF_PushZeros(
    TD_RINGBUF_HANDLE h,                  /* i/o: Ring buffer handle                   */
    const UWord32 num_samples_per_channel /* i  : Number of zeros per channel to store */
)
{
    UWord32 s;
    UWord16 c;

    // TODO: integer multiplication for 32 * 16?
    assert( ivas_td_ringbuf_has_space_for_num_samples( h, L_mult0( num_samples_per_channel, h->num_channels ) ) );
    IF( EQ_32( num_samples_per_channel, 0 ) )
    {
        return;
    }

    FOR( s = 0; s < num_samples_per_channel; ++s )
    {
        FOR( c = 0; c < h->num_channels; ++c )
        {
            // TODO: move for pointer assignment?
            h->data[h->write_pos] = 0;
            ++h->write_pos;

            IF( EQ_32( h->write_pos, h->capacity ) )
            {
                move32();
                h->write_pos = 0;
            }
        }
    }

    IF( EQ_32( h->read_pos, h->write_pos ) )
    {
        move32();
        h->is_full = 1;
    }

    return;
}


/*---------------------------------------------------------------------*
 * ivas_TD_RINGBUF_Pop()
 *
 * Pop samples from the front of the TD ring buffer.
 *---------------------------------------------------------------------*/

void ivas_TD_RINGBUF_Pop(
    TD_RINGBUF_HANDLE h,                  /* i/o: Ring buffer handle                           */
    Word32 *data,                         /* i  : Output data                                  */
    const UWord32 num_samples_per_channel /* i  : Number of samples per channel to retrieve    */
)
{
    UWord32 s;
    UWord16 c;
    Word32 requested_samples;

    // TODO
    requested_samples = L_mult0( num_samples_per_channel, h->num_channels );
    assert( GE_32( ivas_td_ringbuf_total_size( h ), requested_samples ) );

    FOR( s = 0; s < num_samples_per_channel; ++s )
    {
        FOR( c = 0; c < h->num_channels; ++c )
        {
            UWord32 shift_val;

            /* pointer is advanced by c * num_samples_per_channel + s */
            shift_val = L_mult0( c, num_samples_per_channel );
            shift_val = L_add( s, shift_val );

            *( data + shift_val ) = h->data[h->read_pos];
            ++h->read_pos;

            IF( EQ_32( h->write_pos, h->capacity ) )
            {
                move32();
                h->write_pos = 0;
            }
        }
    }

    IF( NE_16( h->is_full, 0 ) )
    {
        move16();
        h->is_full = 0;
    }

    return;
}


/*---------------------------------------------------------------------*
 * ivas_TD_RINGBUF_Size()
 *
 * Returns number of buffered samples per channel.
 *---------------------------------------------------------------------*/

UWord32 ivas_TD_RINGBUF_Size(
    const TD_RINGBUF_HANDLE h /* i  : Ring buffer handle */
)
{
    UWord32 size;

    size = div_l( ivas_td_ringbuf_total_size( h ), h->num_channels );
    return size;
}