/*
** Copyright (C) 1999-2002 Erik de Castro Lopo <erikd@zip.com.au>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
** 
** You should have received a copy of the GNU Lesser 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.
*/

#include	<unistd.h>

#include	"sndfile.h"
#include	"float_cast.h"
#include	"common.h"

static sf_count_t	alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
static sf_count_t	alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
static sf_count_t	alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
static sf_count_t	alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;

static sf_count_t	alaw_write_s2alaw (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
static sf_count_t	alaw_write_i2alaw (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
static sf_count_t	alaw_write_f2alaw (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
static sf_count_t	alaw_write_d2alaw (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;

static	void	alaw2s_array	(unsigned char *buffer, unsigned int count, short *ptr) ;
static	void	alaw2i_array	(unsigned char *buffer, unsigned int count, int *ptr) ;
static	void	alaw2f_array	(unsigned char *buffer, unsigned int count, float *ptr, float normfact) ;
static	void	alaw2d_array	(unsigned char *buffer, unsigned int count, double *ptr, double normfact) ;

static	void	s2alaw_array	(short *buffer, unsigned int count, unsigned char *ptr) ;
static	void	i2alaw_array	(int *buffer, unsigned int count, unsigned char *ptr) ;
static	void	f2alaw_array	(float *buffer, unsigned int count, unsigned char *ptr, float normfact) ;
static	void	d2alaw_array	(double *buffer, unsigned int count, unsigned char *ptr, double normfact) ;


int 
alaw_init (SF_PRIVATE *psf)
{
	if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
	{	psf->read_short  = alaw_read_alaw2s ;
		psf->read_int    = alaw_read_alaw2i ;
		psf->read_float  = alaw_read_alaw2f ;
		psf->read_double = alaw_read_alaw2d ;
		};
		
	if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
	{	psf->write_short  = alaw_write_s2alaw ;
		psf->write_int    = alaw_write_i2alaw ;
		psf->write_float  = alaw_write_f2alaw ;
		psf->write_double = alaw_write_d2alaw ;
		} ;

	psf->bytewidth  = 1 ;
	psf->blockwidth = psf->sf.channels ;

	psf->filelength = psf_get_filelen (psf->filedes) ;
	psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset : 
							psf->filelength - psf->dataoffset ;
	psf->sf.frames = psf->datalength / psf->blockwidth ;

	return 0 ;
} /* alaw_init */

static sf_count_t
alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
{	int			bufferlen, readcount, thisread ;
	sf_count_t	total = 0 ;

	bufferlen = sizeof (psf->buffer) / sizeof (char) ;
	
	while (len > 0)
	{	readcount = (len >= bufferlen) ? bufferlen : (int) len ;
		thisread = psf_fread (psf->buffer, 1, readcount, psf->filedes) ;
		alaw2s_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
		total += thisread ;
		if (thisread < readcount)
			break ;
		len -= thisread ;
		} ;

	
	if (len)
		psf->error = SFE_SHORT_READ ;
	
	return total ;
} /* alaw_read_alaw2s */

static sf_count_t
alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
{	int			bufferlen, readcount, thisread ;
	sf_count_t	total = 0 ;

	bufferlen = sizeof (psf->buffer) / sizeof (char) ;
	
	while (len > 0)
	{	readcount = (len >= bufferlen) ? bufferlen : (int) len ;
		thisread = psf_fread (psf->buffer, 1, readcount, psf->filedes) ;
		alaw2i_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
		total += thisread ;
		if (thisread < readcount)
			break ;
		len -= thisread ;
		} ;

	
	if (len)
		psf->error = SFE_SHORT_READ ;
	
	return total ;
} /* alaw_read_alaw2i */

static sf_count_t
alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
{	int			bufferlen, readcount, thisread ;
	sf_count_t	total = 0 ;
	float	normfact ;

	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;

	bufferlen = sizeof (psf->buffer) / sizeof (char) ;
	
	while (len > 0)
	{	readcount = (len >= bufferlen) ? bufferlen : (int) len ;
		thisread = psf_fread (psf->buffer, 1, readcount, psf->filedes) ;
		alaw2f_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
		total += thisread ;
		if (thisread < readcount)
			break ;
		len -= thisread ;
		} ;

	
	if (len)
		psf->error = SFE_SHORT_READ ;
	
	return total ;
} /* alaw_read_alaw2f */

static sf_count_t
alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
{	int			bufferlen, readcount, thisread ;
	sf_count_t	total = 0 ;
	double	normfact ;
	
	normfact = (psf->norm_double) ? 1.0 / ((double) 0x8000) : 1.0 ;
	bufferlen = sizeof (psf->buffer) / sizeof (char) ;
	
	while (len > 0)
	{	readcount = (len >= bufferlen) ? bufferlen : (int) len ;
		thisread = psf_fread (psf->buffer, 1, readcount, psf->filedes) ;
		alaw2d_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
		total += thisread ;
		if (thisread < readcount)
			break ;
		len -= thisread ;
		} ;

	
	if (len)
		psf->error = SFE_SHORT_READ ;
	
	return total ;
} /* alaw_read_alaw2d */

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

static sf_count_t
alaw_write_s2alaw	(SF_PRIVATE *psf, short *ptr, sf_count_t len)
{	int			bufferlen, writecount, thiswrite ;
	sf_count_t	total = 0 ;

	bufferlen = sizeof (psf->buffer) / sizeof (char) ;
	
	while (len > 0)
	{	writecount = (len >= bufferlen) ? bufferlen : (int) len ;
		s2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
		thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf->filedes) ;
		total += thiswrite ;
		if (thiswrite < writecount)
			break ;
		
		len -= thiswrite ;
		} ;

	
	if (len)
		psf->error = SFE_SHORT_READ ;
	
	return total ;
} /* alaw_write_s2alaw */

static sf_count_t
alaw_write_i2alaw	(SF_PRIVATE *psf, int *ptr, sf_count_t len)
{	int			bufferlen, writecount, thiswrite ;
	sf_count_t	total = 0 ;

	bufferlen = sizeof (psf->buffer) / sizeof (char) ;
	
	while (len > 0)
	{	writecount = (len >= bufferlen) ? bufferlen : (int) len ;
		i2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
		thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf->filedes) ;
		total += thiswrite ;
		if (thiswrite < writecount)
			break ;
		
		len -= thiswrite ;
		} ;

	
	if (len)
		psf->error = SFE_SHORT_READ ;
	
	return total ;
} /* alaw_write_i2alaw */

static sf_count_t
alaw_write_f2alaw	(SF_PRIVATE *psf, float *ptr, sf_count_t len)
{	int			bufferlen, writecount, thiswrite ;
	sf_count_t	total = 0 ;
	float	normfact ;

	normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;

	bufferlen = sizeof (psf->buffer) / sizeof (char) ;
	
	while (len > 0)
	{	writecount = (len >= bufferlen) ? bufferlen : (int) len ;
		f2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
		thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf->filedes) ;
		total += thiswrite ;
		if (thiswrite < writecount)
			break ;
		
		len -= thiswrite ;
		} ;

	
	if (len)
		psf->error = SFE_SHORT_READ ;
	
	return total ;
} /* alaw_write_f2alaw */

static sf_count_t
alaw_write_d2alaw	(SF_PRIVATE *psf, double *ptr, sf_count_t len)
{	int			bufferlen, writecount, thiswrite ;
	sf_count_t	total = 0 ;
	double	normfact ;
	
	normfact = (psf->norm_double) ? (1.0 * 0x7FFF) : 1.0 ;

	bufferlen = sizeof (psf->buffer) / sizeof (char) ;
	
	while (len > 0)
	{	writecount = (len >= bufferlen) ? bufferlen : (int) len ;
		d2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
		thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf->filedes) ;
		total += thiswrite ;
		if (thiswrite < writecount)
			break ;
		
		len -= thiswrite ;
		} ;

	
	if (len)
		psf->error = SFE_SHORT_READ ;
	
	return total ;
} /* alaw_write_d2alaw */

/*=============================================================================================
 *	Private static functions and data.
 */

static	
short	alaw_decode [128] =
{	 -5504,  -5248,  -6016,  -5760,  -4480,  -4224,  -4992,  -4736, 
	 -7552,  -7296,  -8064,  -7808,  -6528,  -6272,  -7040,  -6784, 
	 -2752,  -2624,  -3008,  -2880,  -2240,  -2112,  -2496,  -2368, 
	 -3776,  -3648,  -4032,  -3904,  -3264,  -3136,  -3520,  -3392, 
	-22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944, 
	-30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, 
	-11008, -10496, -12032, -11520,  -8960,  -8448,  -9984,  -9472, 
	-15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, 
	  -344,   -328,   -376,   -360,   -280,   -264,   -312,   -296, 
	  -472,   -456,   -504,   -488,   -408,   -392,   -440,   -424, 
	   -88,    -72,   -120,   -104,    -24,     -8,    -56,    -40, 
	  -216,   -200,   -248,   -232,   -152,   -136,   -184,   -168, 
	 -1376,  -1312,  -1504,  -1440,  -1120,  -1056,  -1248,  -1184, 
	 -1888,  -1824,  -2016,  -1952,  -1632,  -1568,  -1760,  -1696, 
	  -688,   -656,   -752,   -720,   -560,   -528,   -624,   -592, 
	  -944,   -912,  -1008,   -976,   -816,   -784,   -880,   -848
} ; /* alaw_decode */

static	
unsigned char	alaw_encode [2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x90, 0x90, 0x90, 0x93, 0x93, 0x93, 0x93, 
	0x93, 0x93, 0x93, 0x93, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 
	0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9C, 0x9C, 0x9C, 0x9C, 
	0x9C, 0x9C, 0x9C, 0x9C, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 
	0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x99, 0x99, 0x99, 0x99, 
	0x99, 0x99, 0x99, 0x99, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 
	0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9A, 0x9A, 0x9A, 0x9A, 
	0x9A, 0x9A, 0x9A, 0x9A, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 
	0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x84, 0x84, 0x84, 0x84, 
	0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 
	0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 
	0x87, 0x87, 0x87, 0x87, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 
	0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x81, 0x81, 0x81, 0x81, 
	0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 
	0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 
	0x80, 0x80, 0x80, 0x80, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 
	0x83, 0x83, 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AA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 
	0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x2A
} ; /* alaw_encode */

static void	
alaw2s_array	(unsigned char *buffer, unsigned int count, short *ptr)
{	while (count)
	{	count -- ;
		if (buffer [count] & 0x80)
			ptr [count] = -1 * alaw_decode [((int) buffer [count]) & 0x7F] ;
		else
			ptr [count] = alaw_decode [((int) buffer [count]) & 0x7F] ;
		} ;
} /* alaw2s_array */

static void	
alaw2i_array	(unsigned char *buffer, unsigned int count, int *ptr)
{	while (count)
	{	count -- ;
		if (buffer [count] & 0x80)
			ptr [count] = (-1 * alaw_decode [((int) buffer [count]) & 0x7F]) << 16 ;
		else
			ptr [count] = alaw_decode [((int) buffer [count]) & 0x7F] << 16 ;
		} ;
} /* alaw2i_array */

static void	
alaw2f_array	(unsigned char *buffer, unsigned int count, float *ptr, float normfact)
{	while (count)
	{	count -- ;
		if (buffer [count] & 0x80)
			ptr [count] = -normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
		else
			ptr [count] = normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
		} ;
} /* alaw2d_array */

static void	
alaw2d_array	(unsigned char *buffer, unsigned int count, double *ptr, double normfact)
{	while (count)
	{	count -- ;
		if (buffer [count] & 0x80)
			ptr [count] = -normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
		else
			ptr [count] = normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
		} ;
} /* alaw2d_array */

static void    
s2alaw_array      (short *ptr, unsigned int count, unsigned char *buffer)
{	while (count)
	{	count -- ;
		if (ptr [count] >= 0) 
			buffer [count] = alaw_encode [ptr [count] / 16] ;
		else
			buffer [count] = 0x7F & alaw_encode [ptr [count] / -16] ;
		} ;
} /* s2alaw_array */

static void    
i2alaw_array      (int *ptr, unsigned int count, unsigned char *buffer)
{	while (count)
	{	count -- ;
		if (ptr [count] >= 0) 
			buffer [count] = alaw_encode [ptr [count] >> (16 + 4)] ;
		else
			buffer [count] = 0x7F & alaw_encode [- ptr [count] >> (16 + 4)] ;
		} ;
} /* i2alaw_array */

static void    
f2alaw_array      (float *ptr, unsigned int count, unsigned char *buffer, float normfact)
{	while (count)
	{	count -- ;
		if (ptr [count] >= 0) 
			buffer [count] = alaw_encode [(lrintf (normfact * ptr [count])) / 16] ;
		else
			buffer [count] = 0x7F & alaw_encode [(lrintf (normfact * ptr [count])) / -16] ;
		} ;
} /* f2alaw_array */

static void    
d2alaw_array      (double *ptr, unsigned int count, unsigned char *buffer, double normfact)
{	while (count)
	{	count -- ;
		if (ptr [count] >= 0) 
			buffer [count] = alaw_encode [(lrint (normfact * ptr [count])) / 16] ;
		else
			buffer [count] = 0x7F & alaw_encode [(lrint (normfact * ptr [count])) / -16] ;
		} ;
} /* d2alaw_array */

