/*****************************************************************
 * gavl - a general purpose audio/video processing library
 *
 * Copyright (c) 2001 - 2024 Members of the Gmerlin project
 * http://github.com/bplaum
 *
 * 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, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/



/* Taken from libsamplerate, original copyright below */

/*
** Copyright (C) 2002-2008 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** 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.
*/

/*
** This code is part of Secret Rabibt Code aka libsamplerate. A commercial
** use license for this code is available, please see:
**		http://www.mega-nerd.com/SRC/procedure.html
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "common.h"

static int zoh_vari_process_f (SRC_PRIVATE *psrc, SRC_DATA *data) ;
static int zoh_vari_process_d (SRC_PRIVATE *psrc, SRC_DATA *data) ;
static void zoh_reset (SRC_PRIVATE *psrc) ;

/*========================================================================================
*/

#define	ZOH_MAGIC_MARKER	MAKE_MAGIC ('s', 'r', 'c', 'z', 'o', 'h')

typedef struct
{	int		zoh_magic_marker ;
	int		channels ;
	int		reset ;
	long	in_count, in_used ;
	long	out_count, out_gen ;
	float	last_value_f [1] ;
	double	last_value_d [1] ;
        int d;
  } ZOH_DATA ;

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

static int
zoh_vari_process_f (SRC_PRIVATE *psrc, SRC_DATA *data)
{	ZOH_DATA 	*zoh ;
	double		src_ratio, input_index, rem ;
	int			ch ;

	if (psrc->private_data == NULL)
		return SRC_ERR_NO_PRIVATE ;

	zoh = (ZOH_DATA*) psrc->private_data ;

	if (zoh->reset)
	{	/* If we have just been reset, set the last_value data. */
		for (ch = 0 ; ch < zoh->channels ; ch++)
			zoh->last_value_f [ch] = data->data_in_f [ch] ;
		zoh->reset = 0 ;
		} ;

	zoh->in_count = data->input_frames * zoh->channels ;
	zoh->out_count = data->output_frames * zoh->channels ;
	zoh->in_used = zoh->out_gen = 0 ;

	src_ratio = psrc->last_ratio ;
	input_index = psrc->last_position ;

	/* Calculate samples before first sample in input array. */
	while (input_index < 1.0 && zoh->out_gen < zoh->out_count)
	{
		if (zoh->in_used + zoh->channels * input_index >= zoh->in_count)
			break ;

		if (zoh->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
			src_ratio = psrc->last_ratio + zoh->out_gen * (data->src_ratio - psrc->last_ratio) / zoh->out_count ;

		for (ch = 0 ; ch < zoh->channels ; ch++)
		{	data->data_out_f [zoh->out_gen] = zoh->last_value_f [ch] ;
			zoh->out_gen ++ ;
			} ;

		/* Figure out the next index. */
		input_index += 1.0 / src_ratio ;
		} ;

	rem = fmod_one (input_index) ;
	zoh->in_used += zoh->channels * lrint (input_index - rem) ;
	input_index = rem ;

	/* Main processing loop. */
	while (zoh->out_gen < zoh->out_count && zoh->in_used + zoh->channels * input_index <= zoh->in_count)
	{
		if (zoh->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
			src_ratio = psrc->last_ratio + zoh->out_gen * (data->src_ratio - psrc->last_ratio) / zoh->out_count ;

		for (ch = 0 ; ch < zoh->channels ; ch++)
		{	data->data_out_f [zoh->out_gen] = data->data_in_f [zoh->in_used - zoh->channels + ch] ;
			zoh->out_gen ++ ;
			} ;

		/* Figure out the next index. */
		input_index += 1.0 / src_ratio ;
		rem = fmod_one (input_index) ;

		zoh->in_used += zoh->channels * lrint (input_index - rem) ;
		input_index = rem ;
		} ;

	if (zoh->in_used > zoh->in_count)
	{	input_index += (zoh->in_used - zoh->in_count) / zoh->channels ;
		zoh->in_used = zoh->in_count ;
		} ;

	psrc->last_position = input_index ;

	if (zoh->in_used > 0)
		for (ch = 0 ; ch < zoh->channels ; ch++)
			zoh->last_value_f [ch] = data->data_in_f [zoh->in_used - zoh->channels + ch] ;

	/* Save current ratio rather then target ratio. */
	psrc->last_ratio = src_ratio ;

	data->input_frames_used = zoh->in_used / zoh->channels ;
	data->output_frames_gen = zoh->out_gen / zoh->channels ;

	return SRC_ERR_NO_ERROR ;
} /* zoh_vari_process_f */

static int
zoh_vari_process_d (SRC_PRIVATE *psrc, SRC_DATA *data)
{	ZOH_DATA 	*zoh ;
	double		src_ratio, input_index, rem ;
	int			ch ;

	if (psrc->private_data == NULL)
		return SRC_ERR_NO_PRIVATE ;

	zoh = (ZOH_DATA*) psrc->private_data ;

	if (zoh->reset)
	{	/* If we have just been reset, set the last_value data. */
		for (ch = 0 ; ch < zoh->channels ; ch++)
			zoh->last_value_d [ch] = data->data_in_d [ch] ;
		zoh->reset = 0 ;
		} ;

	zoh->in_count = data->input_frames * zoh->channels ;
	zoh->out_count = data->output_frames * zoh->channels ;
	zoh->in_used = zoh->out_gen = 0 ;

	src_ratio = psrc->last_ratio ;
	input_index = psrc->last_position ;

	/* Calculate samples before first sample in input array. */
	while (input_index < 1.0 && zoh->out_gen < zoh->out_count)
	{
		if (zoh->in_used + zoh->channels * input_index >= zoh->in_count)
			break ;

		if (zoh->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
			src_ratio = psrc->last_ratio + zoh->out_gen * (data->src_ratio - psrc->last_ratio) / zoh->out_count ;

		for (ch = 0 ; ch < zoh->channels ; ch++)
		{	data->data_out_d [zoh->out_gen] = zoh->last_value_d [ch] ;
			zoh->out_gen ++ ;
			} ;

		/* Figure out the next index. */
		input_index += 1.0 / src_ratio ;
		} ;

	rem = fmod_one (input_index) ;
	zoh->in_used += zoh->channels * lrint (input_index - rem) ;
	input_index = rem ;

	/* Main processing loop. */
	while (zoh->out_gen < zoh->out_count && zoh->in_used + zoh->channels * input_index <= zoh->in_count)
	{
		if (zoh->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
			src_ratio = psrc->last_ratio + zoh->out_gen * (data->src_ratio - psrc->last_ratio) / zoh->out_count ;

		for (ch = 0 ; ch < zoh->channels ; ch++)
		{	data->data_out_d [zoh->out_gen] = data->data_in_d [zoh->in_used - zoh->channels + ch] ;
			zoh->out_gen ++ ;
			} ;

		/* Figure out the next index. */
		input_index += 1.0 / src_ratio ;
		rem = fmod_one (input_index) ;

		zoh->in_used += zoh->channels * lrint (input_index - rem) ;
		input_index = rem ;
		} ;

	if (zoh->in_used > zoh->in_count)
	{	input_index += (zoh->in_used - zoh->in_count) / zoh->channels ;
		zoh->in_used = zoh->in_count ;
		} ;

	psrc->last_position = input_index ;

	if (zoh->in_used > 0)
		for (ch = 0 ; ch < zoh->channels ; ch++)
			zoh->last_value_d [ch] = data->data_in_d [zoh->in_used - zoh->channels + ch] ;

	/* Save current ratio rather then target ratio. */
	psrc->last_ratio = src_ratio ;

	data->input_frames_used = zoh->in_used / zoh->channels ;
	data->output_frames_gen = zoh->out_gen / zoh->channels ;

	return SRC_ERR_NO_ERROR ;
} /* zoh_vari_process_d */


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

const char*
zoh_get_name (int src_enum)
{
	if (src_enum == SRC_ZERO_ORDER_HOLD)
		return "ZOH Interpolator" ;

	return NULL ;
} /* zoh_get_name */

const char*
zoh_get_description (int src_enum)
{
	if (src_enum == SRC_ZERO_ORDER_HOLD)
		return "Zero order hold interpolator, very fast, poor quality." ;

	return NULL ;
} /* zoh_get_descrition */

int
gavl_zoh_set_converter (SRC_PRIVATE *psrc, int src_enum, int d)
{	ZOH_DATA *zoh = NULL ;

	if (src_enum != SRC_ZERO_ORDER_HOLD)
		return SRC_ERR_BAD_CONVERTER ;

	if (psrc->private_data != NULL)
	{	zoh = (ZOH_DATA*) psrc->private_data ;
		if (zoh->zoh_magic_marker != ZOH_MAGIC_MARKER)
		{	free (psrc->private_data) ;
			psrc->private_data = NULL ;
			} ;
		} ;

	if (psrc->private_data == NULL)
	{	zoh = calloc (1, sizeof (*zoh) + psrc->channels * sizeof (float)) ;
		if (zoh == NULL)
			return SRC_ERR_MALLOC_FAILED ;
		psrc->private_data = zoh ;
		} ;

	zoh->zoh_magic_marker = ZOH_MAGIC_MARKER ;
	zoh->channels = psrc->channels ;
        zoh->d = d;
        if(d)
          {
          psrc->const_process = zoh_vari_process_d ;
          psrc->vari_process = zoh_vari_process_d ;
          }
        else
          {
          psrc->const_process = zoh_vari_process_f ;
          psrc->vari_process = zoh_vari_process_f ;
          }
        
	psrc->reset = zoh_reset ;

	zoh_reset (psrc) ;

	return SRC_ERR_NO_ERROR ;
} /* zoh_set_converter */

/*===================================================================================
*/

static void
zoh_reset (SRC_PRIVATE *psrc)
{	ZOH_DATA *zoh ;

	zoh = (ZOH_DATA*) psrc->private_data ;
	if (zoh == NULL)
		return ;

	zoh->channels = psrc->channels ;
	zoh->reset = 1 ;
        if(zoh->d)
          memset (zoh->last_value_d, 0, sizeof (zoh->last_value_d [0]) * zoh->channels) ;
        else
          memset (zoh->last_value_f, 0, sizeof (zoh->last_value_f [0]) * zoh->channels) ;
          
	return ;
} /* zoh_reset */

