<commit_before>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @file ts_sc_comp.c
 * @brief Scaled RTP Timestamp encoding
 * @author <NAME> from TAS
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

#include "ts_sc_comp.h"
#include "sdvl.h"
#include "rohc_traces_internal.h"

#include <stdlib.h> /* for abs(3) */
#include <assert.h>


/** Print debug messages for the ts_sc_comp module */
#define ts_debug(entity_struct, format, ...) \
	rohc_debug(entity_struct, ROHC_TRACE_COMP, ROHC_PROFILE_GENERAL, \
	           format, ##__VA_ARGS__)


/**
 * @brief Create the ts_sc_comp object
 *
 * @param ts_sc              The ts_sc_comp object to create
 * @param wlsb_window_width  The width of the W-LSB sliding window to use
 *                           for TS_STRIDE (must be > 0)
 * @param callback           The trace callback
 * @return                   1 if creation is successful, 0 otherwise
 */
int c_create_sc(struct ts_sc_comp *const ts_sc,
                const size_t wlsb_window_width,
                rohc_trace_callback_t callback)
{
	assert(ts_sc != NULL);
	assert(wlsb_window_width > 0);

	ts_sc->ts_stride = 0;
	ts_sc->ts_scaled = 0;
	ts_sc->ts_offset = 0;
	ts_sc->old_ts = 0;
	ts_sc->ts = 0;
	ts_sc->ts_delta = 0;
	ts_sc->old_sn = 0;
	ts_sc->sn = 0;
	ts_sc->is_deductible = 0;
	ts_sc->state = INIT_TS;
	ts_sc->nr_init_stride_packets = 0;
	ts_sc->trace_callback = callback;

	ts_sc->scaled_window = c_create_wlsb(32, wlsb_window_width,
	                                     ROHC_LSB_SHIFT_RTP_TS);
	if(ts_sc->scaled_window == NULL)
	{
		rohc_error(ts_sc, ROHC_TRACE_COMP, ROHC_PROFILE_GENERAL,
		           "cannot create a W-LSB window for TS scaled\n");
		goto error;
	}

	return 1;

error:
	return 0;
}


/**
 * @brief Destroy the ts_sc_comp object
 *
 * @param ts_sc        The ts_sc_comp object to destroy
 */
void c_destroy_sc(struct ts_sc_comp *const ts_sc)
{
	assert(ts_sc != NULL);
	assert(ts_sc->scaled_window != NULL);
	c_destroy_wlsb(ts_sc->scaled_window);
}


/**
 * @brief Store the new TS, calculate new values and update the state
 *
 * @param ts_sc        The ts_sc_comp object
 * @param ts           The timestamp to add
 * @param sn           The sequence number of the RTP packet
 */
void c_add_ts(struct ts_sc_comp *const ts_sc, const uint32_t ts, const uint16_t sn)
{
	assert(ts_sc != NULL);

	ts_debug(ts_sc, "Timestamp = %u\n", ts);

	/* we save the old value */
	ts_sc->old_ts = ts_sc->ts;
	ts_sc->old_sn = ts_sc->sn;

	/* we store the new value */
	ts_sc->ts = ts;
	ts_sc->sn = sn;

	/* compute the absolute delta between new and old TS */
	ts_sc->ts_delta = abs(ts_sc->ts - ts_sc->old_ts);
	ts_debug(ts_sc, "TS delta = %u\n", ts_sc->ts_delta);

	switch(ts_sc->state)
	{
		case INIT_TS:
		{
			ts_debug(ts_sc, "state INIT_TS\n");
			break;<commit_msg>Do not use the abs() function to compute TS delta.<commit_after>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @file ts_sc_comp.c
 * @brief Scaled RTP Timestamp encoding
 * @author <NAME> from TAS
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

#include "ts_sc_comp.h"
#include "sdvl.h"
#include "rohc_traces_internal.h"

#include <stdlib.h> /* for abs(3) */
#include <assert.h>


/** Print debug messages for the ts_sc_comp module */
#define ts_debug(entity_struct, format, ...) \
	rohc_debug(entity_struct, ROHC_TRACE_COMP, ROHC_PROFILE_GENERAL, \
	           format, ##__VA_ARGS__)


/**
 * @brief Create the ts_sc_comp object
 *
 * @param ts_sc              The ts_sc_comp object to create
 * @param wlsb_window_width  The width of the W-LSB sliding window to use
 *                           for TS_STRIDE (must be > 0)
 * @param callback           The trace callback
 * @return                   1 if creation is successful, 0 otherwise
 */
int c_create_sc(struct ts_sc_comp *const ts_sc,
                const size_t wlsb_window_width,
                rohc_trace_callback_t callback)
{
	assert(ts_sc != NULL);
	assert(wlsb_window_width > 0);

	ts_sc->ts_stride = 0;
	ts_sc->ts_scaled = 0;
	ts_sc->ts_offset = 0;
	ts_sc->old_ts = 0;
	ts_sc->ts = 0;
	ts_sc->ts_delta = 0;
	ts_sc->old_sn = 0;
	ts_sc->sn = 0;
	ts_sc->is_deductible = 0;
	ts_sc->state = INIT_TS;
	ts_sc->nr_init_stride_packets = 0;
	ts_sc->trace_callback = callback;

	ts_sc->scaled_window = c_create_wlsb(32, wlsb_window_width,
	                                     ROHC_LSB_SHIFT_RTP_TS);
	if(ts_sc->scaled_window == NULL)
	{
		rohc_error(ts_sc, ROHC_TRACE_COMP, ROHC_PROFILE_GENERAL,
		           "cannot create a W-LSB window for TS scaled\n");
		goto error;
	}

	return 1;

error:
	return 0;
}


/**
 * @brief Destroy the ts_sc_comp object
 *
 * @param ts_sc        The ts_sc_comp object to destroy
 */
void c_destroy_sc(struct ts_sc_comp *const ts_sc)
{
	assert(ts_sc != NULL);
	assert(ts_sc->scaled_window != NULL);
	c_destroy_wlsb(ts_sc->scaled_window);
}


/**
 * @brief Store the new TS, calculate new values and update the state
 *
 * @param ts_sc        The ts_sc_comp object
 * @param ts           The timestamp to add
 * @param sn           The sequence number of the RTP packet
 */
void c_add_ts(struct ts_sc_comp *const ts_sc, const uint32_t ts, const uint16_t sn)
{
	assert(ts_sc != NULL);

	ts_debug(ts_sc, "Timestamp = %u\n", ts);

	/* we save the old value */
	ts_sc->old_ts = ts_sc->ts;
	ts_sc->old_sn = ts_sc->sn;

	/* we store the new value */
	ts_sc->ts = ts;
	ts_sc->sn = sn;

	/* compute the absolute delta between new and old TS */
	/* abs() on unsigned 32-bit values seems to be a problem sometimes */
	if(ts_sc->ts >= ts_sc->old_ts)
	{
		ts_sc->ts_delta = ts_sc->ts - ts_sc->old_ts;
	}
	else
	{
		ts_sc->ts_delta = ts_sc->old_ts - ts_sc->ts;
	}
	ts_debug(ts_sc, "TS delta = %u\n", ts_sc->ts_delta);

	switch(ts_sc->state)
	{
		case INIT_TS:
		{
			ts_debug(ts_sc, "state INIT_TS\n");
			break;
{'fraction_non_alphanumeric': 0.10810433812247175, 'fraction_numerical': 0.010880178546519738, 'mean_word_length': 3.106529209621993, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7425805', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2548, 'n_words': 775}