PageRenderTime 130ms CodeModel.GetById 19ms app.highlight 101ms RepoModel.GetById 1ms app.codeStats 0ms

/project/jni/sndfile/src/pcm.c

https://github.com/aichunyu/FFPlayer
C | 2901 lines | 2296 code | 518 blank | 87 comment | 395 complexity | 98344d59715f9dffedd871eba32ed2a2 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
   3**
   4** This program is free software; you can redistribute it and/or modify
   5** it under the terms of the GNU Lesser General Public License as published by
   6** the Free Software Foundation; either version 2.1 of the License, or
   7** (at your option) any later version.
   8**
   9** This program is distributed in the hope that it will be useful,
  10** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12** GNU Lesser General Public License for more details.
  13**
  14** You should have received a copy of the GNU Lesser General Public License
  15** along with this program; if not, write to the Free Software
  16** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17*/
  18
  19#include	"sfconfig.h"
  20
  21#include <math.h>
  22
  23#include	"sndfile.h"
  24#include	"sfendian.h"
  25#include	"common.h"
  26
  27/* Need to be able to handle 3 byte (24 bit) integers. So defined a
  28** type and use SIZEOF_TRIBYTE instead of (tribyte).
  29*/
  30
  31typedef	void	tribyte ;
  32
  33#define	SIZEOF_TRIBYTE	3
  34
  35static sf_count_t	pcm_read_sc2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  36static sf_count_t	pcm_read_uc2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  37static sf_count_t	pcm_read_bes2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  38static sf_count_t	pcm_read_les2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  39static sf_count_t	pcm_read_bet2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  40static sf_count_t	pcm_read_let2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  41static sf_count_t	pcm_read_bei2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  42static sf_count_t	pcm_read_lei2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
  43
  44static sf_count_t	pcm_read_sc2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  45static sf_count_t	pcm_read_uc2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  46static sf_count_t	pcm_read_bes2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  47static sf_count_t	pcm_read_les2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  48static sf_count_t	pcm_read_bet2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  49static sf_count_t	pcm_read_let2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  50static sf_count_t	pcm_read_bei2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  51static sf_count_t	pcm_read_lei2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
  52
  53static sf_count_t	pcm_read_sc2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  54static sf_count_t	pcm_read_uc2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  55static sf_count_t	pcm_read_bes2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  56static sf_count_t	pcm_read_les2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  57static sf_count_t	pcm_read_bet2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  58static sf_count_t	pcm_read_let2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  59static sf_count_t	pcm_read_bei2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  60static sf_count_t	pcm_read_lei2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
  61
  62static sf_count_t	pcm_read_sc2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  63static sf_count_t	pcm_read_uc2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  64static sf_count_t	pcm_read_bes2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  65static sf_count_t	pcm_read_les2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  66static sf_count_t	pcm_read_bet2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  67static sf_count_t	pcm_read_let2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  68static sf_count_t	pcm_read_bei2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  69static sf_count_t	pcm_read_lei2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
  70
  71static sf_count_t	pcm_write_s2sc	(SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  72static sf_count_t	pcm_write_s2uc	(SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  73static sf_count_t	pcm_write_s2bes (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  74static sf_count_t	pcm_write_s2les (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  75static sf_count_t	pcm_write_s2bet (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  76static sf_count_t	pcm_write_s2let (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  77static sf_count_t	pcm_write_s2bei (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  78static sf_count_t	pcm_write_s2lei (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
  79
  80static sf_count_t	pcm_write_i2sc	(SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  81static sf_count_t	pcm_write_i2uc	(SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  82static sf_count_t	pcm_write_i2bes (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  83static sf_count_t	pcm_write_i2les (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  84static sf_count_t	pcm_write_i2bet (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  85static sf_count_t	pcm_write_i2let (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  86static sf_count_t	pcm_write_i2bei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  87static sf_count_t	pcm_write_i2lei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
  88
  89static sf_count_t	pcm_write_f2sc	(SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  90static sf_count_t	pcm_write_f2uc	(SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  91static sf_count_t	pcm_write_f2bes (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  92static sf_count_t	pcm_write_f2les (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  93static sf_count_t	pcm_write_f2bet (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  94static sf_count_t	pcm_write_f2let (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  95static sf_count_t	pcm_write_f2bei (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  96static sf_count_t	pcm_write_f2lei (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
  97
  98static sf_count_t	pcm_write_d2sc	(SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
  99static sf_count_t	pcm_write_d2uc	(SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
 100static sf_count_t	pcm_write_d2bes (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
 101static sf_count_t	pcm_write_d2les (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
 102static sf_count_t	pcm_write_d2bet (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
 103static sf_count_t	pcm_write_d2let (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
 104static sf_count_t	pcm_write_d2bei (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
 105static sf_count_t	pcm_write_d2lei (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
 106
 107/*-----------------------------------------------------------------------------------------------
 108*/
 109
 110enum
 111{	/* Char type for 8 bit files. */
 112	SF_CHARS_SIGNED		= 200,
 113	SF_CHARS_UNSIGNED	= 201
 114} ;
 115
 116/*-----------------------------------------------------------------------------------------------
 117*/
 118
 119int
 120pcm_init (SF_PRIVATE *psf)
 121{	int chars = 0 ;
 122
 123	if (psf->bytewidth == 0 || psf->sf.channels == 0)
 124	{	psf_log_printf (psf, "pcm_init : internal error : bytewitdh = %d, channels = %d\n", psf->bytewidth, psf->sf.channels) ;
 125		return SFE_INTERNAL ;
 126		} ;
 127
 128	psf->blockwidth = psf->bytewidth * psf->sf.channels ;
 129
 130	if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_S8)
 131		chars = SF_CHARS_SIGNED ;
 132	else if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_U8)
 133		chars = SF_CHARS_UNSIGNED ;
 134
 135	if (CPU_IS_BIG_ENDIAN)
 136		psf->data_endswap = (psf->endian == SF_ENDIAN_BIG) ? SF_FALSE : SF_TRUE ;
 137	else
 138		psf->data_endswap = (psf->endian == SF_ENDIAN_LITTLE) ? SF_FALSE : SF_TRUE ;
 139
 140	if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
 141	{	switch (psf->bytewidth * 0x10000 + psf->endian + chars)
 142		{	case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
 143			case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
 144					psf->read_short		= pcm_read_sc2s ;
 145					psf->read_int		= pcm_read_sc2i ;
 146					psf->read_float		= pcm_read_sc2f ;
 147					psf->read_double	= pcm_read_sc2d ;
 148					break ;
 149			case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
 150			case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
 151					psf->read_short		= pcm_read_uc2s ;
 152					psf->read_int		= pcm_read_uc2i ;
 153					psf->read_float		= pcm_read_uc2f ;
 154					psf->read_double	= pcm_read_uc2d ;
 155					break ;
 156
 157			case (2 * 0x10000 + SF_ENDIAN_BIG) :
 158					psf->read_short		= pcm_read_bes2s ;
 159					psf->read_int		= pcm_read_bes2i ;
 160					psf->read_float		= pcm_read_bes2f ;
 161					psf->read_double	= pcm_read_bes2d ;
 162					break ;
 163			case (3 * 0x10000 + SF_ENDIAN_BIG) :
 164					psf->read_short		= pcm_read_bet2s ;
 165					psf->read_int		= pcm_read_bet2i ;
 166					psf->read_float		= pcm_read_bet2f ;
 167					psf->read_double	= pcm_read_bet2d ;
 168					break ;
 169			case (4 * 0x10000 + SF_ENDIAN_BIG) :
 170
 171					psf->read_short		= pcm_read_bei2s ;
 172					psf->read_int		= pcm_read_bei2i ;
 173					psf->read_float		= pcm_read_bei2f ;
 174					psf->read_double	= pcm_read_bei2d ;
 175					break ;
 176
 177			case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
 178					psf->read_short		= pcm_read_les2s ;
 179					psf->read_int		= pcm_read_les2i ;
 180					psf->read_float		= pcm_read_les2f ;
 181					psf->read_double	= pcm_read_les2d ;
 182					break ;
 183			case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
 184					psf->read_short		= pcm_read_let2s ;
 185					psf->read_int		= pcm_read_let2i ;
 186					psf->read_float		= pcm_read_let2f ;
 187					psf->read_double	= pcm_read_let2d ;
 188					break ;
 189			case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
 190					psf->read_short		= pcm_read_lei2s ;
 191					psf->read_int		= pcm_read_lei2i ;
 192					psf->read_float		= pcm_read_lei2f ;
 193					psf->read_double	= pcm_read_lei2d ;
 194					break ;
 195			default :
 196				psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\nbytewidth %d    endian %d\n", psf->bytewidth, psf->endian) ;
 197				return SFE_UNIMPLEMENTED ;
 198			} ;
 199		} ;
 200
 201	if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
 202	{	switch (psf->bytewidth * 0x10000 + psf->endian + chars)
 203		{	case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
 204			case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
 205					psf->write_short	= pcm_write_s2sc ;
 206					psf->write_int		= pcm_write_i2sc ;
 207					psf->write_float	= pcm_write_f2sc ;
 208					psf->write_double	= pcm_write_d2sc ;
 209					break ;
 210			case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
 211			case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
 212					psf->write_short	= pcm_write_s2uc ;
 213					psf->write_int		= pcm_write_i2uc ;
 214					psf->write_float	= pcm_write_f2uc ;
 215					psf->write_double	= pcm_write_d2uc ;
 216					break ;
 217
 218			case (2 * 0x10000 + SF_ENDIAN_BIG) :
 219					psf->write_short	= pcm_write_s2bes ;
 220					psf->write_int		= pcm_write_i2bes ;
 221					psf->write_float	= pcm_write_f2bes ;
 222					psf->write_double	= pcm_write_d2bes ;
 223					break ;
 224
 225			case (3 * 0x10000 + SF_ENDIAN_BIG) :
 226					psf->write_short	= pcm_write_s2bet ;
 227					psf->write_int		= pcm_write_i2bet ;
 228					psf->write_float	= pcm_write_f2bet ;
 229					psf->write_double	= pcm_write_d2bet ;
 230					break ;
 231
 232			case (4 * 0x10000 + SF_ENDIAN_BIG) :
 233					psf->write_short	= pcm_write_s2bei ;
 234					psf->write_int		= pcm_write_i2bei ;
 235					psf->write_float	= pcm_write_f2bei ;
 236					psf->write_double	= pcm_write_d2bei ;
 237					break ;
 238
 239			case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
 240					psf->write_short	= pcm_write_s2les ;
 241					psf->write_int		= pcm_write_i2les ;
 242					psf->write_float	= pcm_write_f2les ;
 243					psf->write_double	= pcm_write_d2les ;
 244					break ;
 245
 246			case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
 247					psf->write_short	= pcm_write_s2let ;
 248					psf->write_int		= pcm_write_i2let ;
 249					psf->write_float	= pcm_write_f2let ;
 250					psf->write_double	= pcm_write_d2let ;
 251					break ;
 252
 253			case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
 254					psf->write_short	= pcm_write_s2lei ;
 255					psf->write_int		= pcm_write_i2lei ;
 256					psf->write_float	= pcm_write_f2lei ;
 257					psf->write_double	= pcm_write_d2lei ;
 258					break ;
 259
 260			default :
 261				psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\nbytewidth %s    endian %d\n", psf->bytewidth, psf->endian) ;
 262				return SFE_UNIMPLEMENTED ;
 263			} ;
 264
 265		} ;
 266
 267	if (psf->filelength > psf->dataoffset)
 268	{	psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
 269							psf->filelength - psf->dataoffset ;
 270		}
 271	else
 272		psf->datalength = 0 ;
 273
 274	psf->sf.frames = psf->blockwidth > 0 ? psf->datalength / psf->blockwidth : 0 ;
 275
 276	return 0 ;
 277} /* pcm_init */
 278
 279/*==============================================================================
 280*/
 281
 282static inline void
 283sc2s_array	(signed char *src, int count, short *dest)
 284{	while (--count >= 0)
 285	{	dest [count] = src [count] << 8 ;
 286		} ;
 287} /* sc2s_array */
 288
 289static inline void
 290uc2s_array	(unsigned char *src, int count, short *dest)
 291{	while (--count >= 0)
 292	{	dest [count] = (((short) src [count]) - 0x80) << 8 ;
 293		} ;
 294} /* uc2s_array */
 295
 296static inline void
 297let2s_array (tribyte *src, int count, short *dest)
 298{	unsigned char	*ucptr ;
 299
 300	ucptr = ((unsigned char*) src) + 3 * count ;
 301	while (--count >= 0)
 302	{	ucptr -= 3 ;
 303		dest [count] = LET2H_SHORT_PTR (ucptr) ;
 304		} ;
 305} /* let2s_array */
 306
 307static inline void
 308bet2s_array (tribyte *src, int count, short *dest)
 309{	unsigned char	*ucptr ;
 310
 311	ucptr = ((unsigned char*) src) + 3 * count ;
 312	while (--count >= 0)
 313	{	ucptr -= 3 ;
 314		dest [count] = BET2H_SHORT_PTR (ucptr) ;
 315			} ;
 316} /* bet2s_array */
 317
 318static inline void
 319lei2s_array (int *src, int count, short *dest)
 320{	int value ;
 321
 322	while (--count >= 0)
 323	{	value = LEI2H_INT (src [count]) ;
 324		dest [count] = value >> 16 ;
 325		} ;
 326} /* lei2s_array */
 327
 328static inline void
 329bei2s_array (int *src, int count, short *dest)
 330{	int value ;
 331
 332	while (--count >= 0)
 333	{	value = BEI2H_INT (src [count]) ;
 334		dest [count] = value >> 16 ;
 335		} ;
 336} /* bei2s_array */
 337
 338/*--------------------------------------------------------------------------
 339*/
 340
 341static inline void
 342sc2i_array	(signed char *src, int count, int *dest)
 343{	while (--count >= 0)
 344	{	dest [count] = ((int) src [count]) << 24 ;
 345		} ;
 346} /* sc2i_array */
 347
 348static inline void
 349uc2i_array	(unsigned char *src, int count, int *dest)
 350{	while (--count >= 0)
 351	{	dest [count] = (((int) src [count]) - 128) << 24 ;
 352		} ;
 353} /* uc2i_array */
 354
 355static inline void
 356bes2i_array (short *src, int count, int *dest)
 357{	short value ;
 358
 359	while (--count >= 0)
 360	{	value = BES2H_SHORT (src [count]) ;
 361		dest [count] = value << 16 ;
 362		} ;
 363} /* bes2i_array */
 364
 365static inline void
 366les2i_array (short *src, int count, int *dest)
 367{	short value ;
 368
 369	while (--count >= 0)
 370	{	value = LES2H_SHORT (src [count]) ;
 371		dest [count] = value << 16 ;
 372		} ;
 373} /* les2i_array */
 374
 375static inline void
 376bet2i_array (tribyte *src, int count, int *dest)
 377{	unsigned char	*ucptr ;
 378
 379	ucptr = ((unsigned char*) src) + 3 * count ;
 380	while (--count >= 0)
 381	{	ucptr -= 3 ;
 382		dest [count] = BET2H_INT_PTR (ucptr) ;
 383			} ;
 384} /* bet2i_array */
 385
 386static inline void
 387let2i_array (tribyte *src, int count, int *dest)
 388{	unsigned char	*ucptr ;
 389
 390	ucptr = ((unsigned char*) src) + 3 * count ;
 391	while (--count >= 0)
 392	{	ucptr -= 3 ;
 393		dest [count] = LET2H_INT_PTR (ucptr) ;
 394		} ;
 395} /* let2i_array */
 396
 397/*--------------------------------------------------------------------------
 398*/
 399
 400static inline void
 401sc2f_array	(signed char *src, int count, float *dest, float normfact)
 402{	while (--count >= 0)
 403		dest [count] = ((float) src [count]) * normfact ;
 404} /* sc2f_array */
 405
 406static inline void
 407uc2f_array	(unsigned char *src, int count, float *dest, float normfact)
 408{	while (--count >= 0)
 409		dest [count] = (((int) src [count]) - 128) * normfact ;
 410} /* uc2f_array */
 411
 412static inline void
 413les2f_array (short *src, int count, float *dest, float normfact)
 414{	short	value ;
 415
 416	while (--count >= 0)
 417	{	value = src [count] ;
 418		value = LES2H_SHORT (value) ;
 419		dest [count] = ((float) value) * normfact ;
 420		} ;
 421} /* les2f_array */
 422
 423static inline void
 424bes2f_array (short *src, int count, float *dest, float normfact)
 425{	short			value ;
 426
 427	while (--count >= 0)
 428	{	value = src [count] ;
 429		value = BES2H_SHORT (value) ;
 430		dest [count] = ((float) value) * normfact ;
 431		} ;
 432} /* bes2f_array */
 433
 434static inline void
 435let2f_array (tribyte *src, int count, float *dest, float normfact)
 436{	unsigned char	*ucptr ;
 437	int 			value ;
 438
 439	ucptr = ((unsigned char*) src) + 3 * count ;
 440	while (--count >= 0)
 441	{	ucptr -= 3 ;
 442		value = LET2H_INT_PTR (ucptr) ;
 443		dest [count] = ((float) value) * normfact ;
 444		} ;
 445} /* let2f_array */
 446
 447static inline void
 448bet2f_array (tribyte *src, int count, float *dest, float normfact)
 449{	unsigned char	*ucptr ;
 450	int				value ;
 451
 452	ucptr = ((unsigned char*) src) + 3 * count ;
 453	while (--count >= 0)
 454	{	ucptr -= 3 ;
 455		value = BET2H_INT_PTR (ucptr) ;
 456		dest [count] = ((float) value) * normfact ;
 457			} ;
 458} /* bet2f_array */
 459
 460static inline void
 461lei2f_array (int *src, int count, float *dest, float normfact)
 462{	int 			value ;
 463
 464	while (--count >= 0)
 465	{	value = src [count] ;
 466		value = LEI2H_INT (value) ;
 467		dest [count] = ((float) value) * normfact ;
 468		} ;
 469} /* lei2f_array */
 470
 471static inline void
 472bei2f_array (int *src, int count, float *dest, float normfact)
 473{	int 			value ;
 474
 475	while (--count >= 0)
 476	{	value = src [count] ;
 477		value = BEI2H_INT (value) ;
 478		dest [count] = ((float) value) * normfact ;
 479		} ;
 480} /* bei2f_array */
 481
 482/*--------------------------------------------------------------------------
 483*/
 484
 485static inline void
 486sc2d_array	(signed char *src, int count, double *dest, double normfact)
 487{	while (--count >= 0)
 488		dest [count] = ((double) src [count]) * normfact ;
 489} /* sc2d_array */
 490
 491static inline void
 492uc2d_array	(unsigned char *src, int count, double *dest, double normfact)
 493{	while (--count >= 0)
 494		dest [count] = (((int) src [count]) - 128) * normfact ;
 495} /* uc2d_array */
 496
 497static inline void
 498les2d_array (short *src, int count, double *dest, double normfact)
 499{	short	value ;
 500
 501	while (--count >= 0)
 502	{	value = src [count] ;
 503		value = LES2H_SHORT (value) ;
 504		dest [count] = ((double) value) * normfact ;
 505		} ;
 506} /* les2d_array */
 507
 508static inline void
 509bes2d_array (short *src, int count, double *dest, double normfact)
 510{	short	value ;
 511
 512	while (--count >= 0)
 513	{	value = src [count] ;
 514		value = BES2H_SHORT (value) ;
 515		dest [count] = ((double) value) * normfact ;
 516		} ;
 517} /* bes2d_array */
 518
 519static inline void
 520let2d_array (tribyte *src, int count, double *dest, double normfact)
 521{	unsigned char	*ucptr ;
 522	int				value ;
 523
 524	ucptr = ((unsigned char*) src) + 3 * count ;
 525	while (--count >= 0)
 526	{	ucptr -= 3 ;
 527		value = LET2H_INT_PTR (ucptr) ;
 528		dest [count] = ((double) value) * normfact ;
 529		} ;
 530} /* let2d_array */
 531
 532static inline void
 533bet2d_array (tribyte *src, int count, double *dest, double normfact)
 534{	unsigned char	*ucptr ;
 535	int				value ;
 536
 537	ucptr = ((unsigned char*) src) + 3 * count ;
 538	while (--count >= 0)
 539	{	ucptr -= 3 ;
 540		value = (ucptr [0] << 24) | (ucptr [1] << 16) | (ucptr [2] << 8) ;
 541		dest [count] = ((double) value) * normfact ;
 542		} ;
 543} /* bet2d_array */
 544
 545static inline void
 546lei2d_array (int *src, int count, double *dest, double normfact)
 547{	int 	value ;
 548
 549	while (--count >= 0)
 550	{	value = src [count] ;
 551		value = LEI2H_INT (value) ;
 552		dest [count] = ((double) value) * normfact ;
 553		} ;
 554} /* lei2d_array */
 555
 556static inline void
 557bei2d_array (int *src, int count, double *dest, double normfact)
 558{	int 	value ;
 559
 560	while (--count >= 0)
 561	{	value = src [count] ;
 562		value = BEI2H_INT (value) ;
 563		dest [count] = ((double) value) * normfact ;
 564		} ;
 565} /* bei2d_array */
 566
 567/*--------------------------------------------------------------------------
 568*/
 569
 570static inline void
 571s2sc_array	(const short *src, signed char *dest, int count)
 572{	while (--count >= 0)
 573		dest [count] = src [count] >> 8 ;
 574} /* s2sc_array */
 575
 576static inline void
 577s2uc_array	(const short *src, unsigned char *dest, int count)
 578{	while (--count >= 0)
 579		dest [count] = (src [count] >> 8) + 0x80 ;
 580} /* s2uc_array */
 581
 582static inline void
 583s2let_array (const short *src, tribyte *dest, int count)
 584{	unsigned char	*ucptr ;
 585
 586	ucptr = ((unsigned char*) dest) + 3 * count ;
 587	while (--count >= 0)
 588	{	ucptr -= 3 ;
 589		ucptr [0] = 0 ;
 590		ucptr [1] = src [count] ;
 591		ucptr [2] = src [count] >> 8 ;
 592		} ;
 593} /* s2let_array */
 594
 595static inline void
 596s2bet_array (const short *src, tribyte *dest, int count)
 597{	unsigned char	*ucptr ;
 598
 599	ucptr = ((unsigned char*) dest) + 3 * count ;
 600	while (--count >= 0)
 601	{	ucptr -= 3 ;
 602		ucptr [2] = 0 ;
 603		ucptr [1] = src [count] ;
 604		ucptr [0] = src [count] >> 8 ;
 605		} ;
 606} /* s2bet_array */
 607
 608static inline void
 609s2lei_array (const short *src, int *dest, int count)
 610{	unsigned char	*ucptr ;
 611
 612	ucptr = ((unsigned char*) dest) + 4 * count ;
 613	while (--count >= 0)
 614	{	ucptr -= 4 ;
 615		ucptr [0] = 0 ;
 616		ucptr [1] = 0 ;
 617		ucptr [2] = src [count] ;
 618		ucptr [3] = src [count] >> 8 ;
 619		} ;
 620} /* s2lei_array */
 621
 622static inline void
 623s2bei_array (const short *src, int *dest, int count)
 624{	unsigned char	*ucptr ;
 625
 626	ucptr = ((unsigned char*) dest) + 4 * count ;
 627	while (--count >= 0)
 628	{	ucptr -= 4 ;
 629		ucptr [0] = src [count] >> 8 ;
 630		ucptr [1] = src [count] ;
 631		ucptr [2] = 0 ;
 632		ucptr [3] = 0 ;
 633		} ;
 634} /* s2bei_array */
 635
 636/*--------------------------------------------------------------------------
 637*/
 638
 639static inline void
 640i2sc_array	(const int *src, signed char *dest, int count)
 641{	while (--count >= 0)
 642		dest [count] = (src [count] >> 24) ;
 643} /* i2sc_array */
 644
 645static inline void
 646i2uc_array	(const int *src, unsigned char *dest, int count)
 647{	while (--count >= 0)
 648		dest [count] = ((src [count] >> 24) + 128) ;
 649} /* i2uc_array */
 650
 651static inline void
 652i2bes_array (const int *src, short *dest, int count)
 653{	unsigned char	*ucptr ;
 654
 655	ucptr = ((unsigned char*) dest) + 2 * count ;
 656	while (--count >= 0)
 657	{	ucptr -= 2 ;
 658		ucptr [0] = src [count] >> 24 ;
 659		ucptr [1] = src [count] >> 16 ;
 660		} ;
 661} /* i2bes_array */
 662
 663static inline void
 664i2les_array (const int *src, short *dest, int count)
 665{	unsigned char	*ucptr ;
 666
 667	ucptr = ((unsigned char*) dest) + 2 * count ;
 668	while (--count >= 0)
 669	{	ucptr -= 2 ;
 670		ucptr [0] = src [count] >> 16 ;
 671		ucptr [1] = src [count] >> 24 ;
 672		} ;
 673} /* i2les_array */
 674
 675static inline void
 676i2let_array (const int *src, tribyte *dest, int count)
 677{	unsigned char	*ucptr ;
 678	int				value ;
 679
 680	ucptr = ((unsigned char*) dest) + 3 * count ;
 681	while (--count >= 0)
 682	{	ucptr -= 3 ;
 683		value = src [count] >> 8 ;
 684		ucptr [0] = value ;
 685		ucptr [1] = value >> 8 ;
 686		ucptr [2] = value >> 16 ;
 687		} ;
 688} /* i2let_array */
 689
 690static inline void
 691i2bet_array (const int *src, tribyte *dest, int count)
 692{	unsigned char	*ucptr ;
 693	int				value ;
 694
 695	ucptr = ((unsigned char*) dest) + 3 * count ;
 696	while (--count >= 0)
 697	{	ucptr -= 3 ;
 698		value = src [count] >> 8 ;
 699		ucptr [2] = value ;
 700		ucptr [1] = value >> 8 ;
 701		ucptr [0] = value >> 16 ;
 702		} ;
 703} /* i2bet_array */
 704
 705/*===============================================================================================
 706*/
 707
 708static sf_count_t
 709pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 710{	int			bufferlen, readcount ;
 711	sf_count_t	total = 0 ;
 712
 713	bufferlen = ARRAY_LEN (psf->u.scbuf) ;
 714
 715	while (len > 0)
 716	{	if (len < bufferlen)
 717			bufferlen = (int) len ;
 718		readcount = psf_fread (psf->u.scbuf, sizeof (signed char), bufferlen, psf) ;
 719		sc2s_array (psf->u.scbuf, readcount, ptr + total) ;
 720		total += readcount ;
 721		if (readcount < bufferlen)
 722			break ;
 723		len -= readcount ;
 724		} ;
 725
 726	return total ;
 727} /* pcm_read_sc2s */
 728
 729static sf_count_t
 730pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 731{	int			bufferlen, readcount ;
 732	sf_count_t	total = 0 ;
 733
 734	bufferlen = ARRAY_LEN (psf->u.ucbuf) ;
 735
 736	while (len > 0)
 737	{	if (len < bufferlen)
 738			bufferlen = (int) len ;
 739		readcount = psf_fread (psf->u.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
 740		uc2s_array (psf->u.ucbuf, readcount, ptr + total) ;
 741		total += readcount ;
 742		if (readcount < bufferlen)
 743			break ;
 744		len -= readcount ;
 745		} ;
 746
 747	return total ;
 748} /* pcm_read_uc2s */
 749
 750static sf_count_t
 751pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 752{	int		total ;
 753
 754	total = psf_fread (ptr, sizeof (short), len, psf) ;
 755	if (CPU_IS_LITTLE_ENDIAN)
 756		endswap_short_array (ptr, len) ;
 757
 758	return total ;
 759} /* pcm_read_bes2s */
 760
 761static sf_count_t
 762pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 763{	int		total ;
 764
 765	total = psf_fread (ptr, sizeof (short), len, psf) ;
 766	if (CPU_IS_BIG_ENDIAN)
 767		endswap_short_array (ptr, len) ;
 768
 769	return total ;
 770} /* pcm_read_les2s */
 771
 772static sf_count_t
 773pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 774{	int			bufferlen, readcount ;
 775	sf_count_t	total = 0 ;
 776
 777	bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
 778
 779	while (len > 0)
 780	{	if (len < bufferlen)
 781			bufferlen = (int) len ;
 782		readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
 783		bet2s_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total) ;
 784		total += readcount ;
 785		if (readcount < bufferlen)
 786			break ;
 787		len -= readcount ;
 788		} ;
 789
 790	return total ;
 791} /* pcm_read_bet2s */
 792
 793static sf_count_t
 794pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 795{	int			bufferlen, readcount ;
 796	sf_count_t	total = 0 ;
 797
 798	bufferlen = sizeof (psf->u.ucbuf) / SIZEOF_TRIBYTE ;
 799
 800	while (len > 0)
 801	{	if (len < bufferlen)
 802			bufferlen = (int) len ;
 803		readcount = psf_fread (psf->u.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
 804		let2s_array ((tribyte*) (psf->u.ucbuf), readcount, ptr + total) ;
 805		total += readcount ;
 806		if (readcount < bufferlen)
 807			break ;
 808		len -= readcount ;
 809		} ;
 810
 811	return total ;
 812} /* pcm_read_let2s */
 813
 814static sf_count_t
 815pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 816{	int			bufferlen, readcount ;
 817	sf_count_t	total = 0 ;
 818
 819	bufferlen = ARRAY_LEN (psf->u.ibuf) ;
 820
 821	while (len > 0)
 822	{	if (len < bufferlen)
 823			bufferlen = (int) len ;
 824		readcount = psf_fread (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
 825		bei2s_array (psf->u.ibuf, readcount, ptr + total) ;
 826		total += readcount ;
 827		if (readcount < bufferlen)
 828			break ;
 829		len -= readcount ;
 830		} ;
 831
 832	return total ;
 833} /* pcm_read_bei2s */
 834
 835static sf_count_t
 836pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 837{	int			bufferlen, readcount ;
 838	sf_count_t	total = 0 ;
 839
 840	bufferlen = ARRAY_LEN (psf->u.ibuf) ;
 841
 842	while (len > 0)
 843	{	if (len < bufferlen)
 844			bufferlen = (int) len ;
 845		readcount = psf_fread (psf->u.ibuf, sizeof (int), bufferlen, psf) ;
 846		lei2s_array (psf->u.ibuf, readcount, ptr + total) ;
 847		total += readcount ;
 848		if (readcount < bufferlen)
 849			break ;
 850		len -= readcount ;
 851		} ;
 852
 853	return total ;
 854} /* pcm_read_lei2s */
 855
 856/*-----------------------------------------------------------------------------------------------
 857*/
 858
 859static sf_count_t
 860pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
 861{	int			bufferlen, readcount ;
 862	sf_count_t	total = 0 ;
 863
 864	bufferlen = ARRAY_LEN (psf->u.scbuf) ;
 865
 866	while (len > 0)
 867	{	if (len < bufferlen)
 868			bufferlen = (int) len ;
 869		readcount = psf_fread (psf->u.scbuf, sizeof (signed char), bufferlen, psf) ;
 870		sc2i_array (psf->u.scbuf, readcount, ptr + total) ;
 871		total += readcount ;
 872		if (readcount < bufferlen)
 873			break ;
 874		len -= readcount ;
 875		} ;
 876
 877	return total ;
 878} /* pcm_read_sc2i */
 879
 880static sf_count_t
 881pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
 882{	int			bufferlen, readcount ;
 883	sf_count_t	total = 0 ;
 884
 885	bufferlen = ARRAY_LEN (psf->u.ucbuf) ;
 886
 887	while (len > 0)
 888	{	if (len < bufferlen)
 889			bufferlen = (int) len ;
 890		readcount = psf_fread (psf->u.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
 891		uc2i_array (psf->u.ucbuf, readcount, ptr + total) ;
 892		total += readcount ;
 893		if (readcount < bufferlen)
 894			break ;
 895		len -= readcount ;
 896		} ;
 897
 898	return total ;
 899} /* pcm_read_uc2i */
 900
 901static sf_count_t
 902pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
 903{	int			bufferlen, readcount ;
 904	sf_count_t	total = 0 ;
 905
 906	bufferlen = ARRAY_LEN (psf->u.sbuf) ;
 907
 908	while (len > 0)
 909	{	if (len < bufferlen)
 910