1 /*
2 ** Copyright (C) 1999-2018 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 #ifndef SNDFILE_COMMON_H
20 #define SNDFILE_COMMON_H
21 
22 #include "sfconfig.h"
23 
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #if HAVE_INTTYPES_H
28 #include <inttypes.h>
29 #elif HAVE_STDINT_H
30 #include <stdint.h>
31 #endif
32 #if HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35 
36 #ifndef SNDFILE_H
37 #include "sndfile.h"
38 #endif
39 
40 #include <math.h>
41 
42 #ifdef USE_SSE2
43 #include <immintrin.h>
44 #endif
45 
46 #ifdef __cplusplus
47 #error "This code is not designed to be compiled with a C++ compiler."
48 #endif
49 
50 #ifdef INT64_C
51 #	define	SF_PLATFORM_S64(x)		INT64_C (x)
52 #elif (SIZEOF_LONG == 8)
53 #	define	SF_PLATFORM_S64(x)		x##l
54 #elif (SIZEOF_LONG_LONG == 8)
55 #	define	SF_PLATFORM_S64(x)		x##ll
56 #elif COMPILER_IS_GCC
57 #	define	SF_PLATFORM_S64(x)		x##ll
58 #elif OS_IS_WIN32
59 #	define	SF_PLATFORM_S64(x)		x##I64
60 #else
61 #	error "Don't know how to define a 64 bit integer constant."
62 #endif
63 
64 
65 
66 /*
67 ** Inspiration : http://sourcefrog.net/weblog/software/languages/C/unused.html
68 */
69 #ifdef UNUSED
70 #elif defined (__GNUC__)
71 #	define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
72 #elif defined (__LCLINT__)
73 #	define UNUSED(x) /*@unused@*/ x
74 #else
75 #	define UNUSED(x) x
76 #endif
77 
78 #ifdef __GNUC__
79 #	define WARN_UNUSED	__attribute__ ((warn_unused_result))
80 #else
81 #	define WARN_UNUSED
82 #endif
83 
84 #define	SF_BUFFER_LEN			(8192)
85 #define	SF_FILENAME_LEN			(1024)
86 #define SF_SYSERR_LEN			(256)
87 #define SF_MAX_STRINGS			(32)
88 #define	SF_PARSELOG_LEN			(2048)
89 
90 #define	PSF_SEEK_ERROR			((sf_count_t) -1)
91 
92 #define	BITWIDTH2BYTES(x)	(((x) + 7) / 8)
93 
94 /*	For some reason sizeof returns an unsigned  value which causes
95 **	a warning when that value is added or subtracted from a signed
96 **	value. Use SIGNED_SIZEOF instead.
97 */
98 #define		SIGNED_SIZEOF(x)	((int) sizeof (x))
99 
100 #define		ARRAY_LEN(x)	((int) (sizeof (x) / sizeof ((x) [0])))
101 
102 #define		NOT(x)			(! (x))
103 
104 #if COMPILER_IS_GCC
105 #define		SF_MAX(x, y)	({ \
106 								typeof (x) sf_max_x1 = (x) ; \
107 								typeof (y) sf_max_y1 = (y) ; \
108 								(void) (&sf_max_x1 == &sf_max_y1) ; \
109 								sf_max_x1 > sf_max_y1 ? sf_max_x1 : sf_max_y1 ; })
110 
111 #define		SF_MIN(x, y)	({ \
112 								typeof (x) sf_min_x2 = (x) ; \
113 								typeof (y) sf_min_y2 = (y) ; \
114 								(void) (&sf_min_x2 == &sf_min_y2) ; \
115 								sf_min_x2 < sf_min_y2 ? sf_min_x2 : sf_min_y2 ; })
116 #else
117 #define		SF_MAX(a, b)	((a) > (b) ? (a) : (b))
118 #define		SF_MIN(a, b)	((a) < (b) ? (a) : (b))
119 #endif
120 
121 
122 #define		COMPILE_TIME_ASSERT(e)	(sizeof (struct { int : - !! (e) ; }))
123 
124 
125 #define		SF_MAX_CHANNELS		1024
126 
127 
128 /*
129 *	Macros for spliting the format file of SF_INFO into container type,
130 **	codec type and endian-ness.
131 */
132 #define SF_CONTAINER(x)		((x) & SF_FORMAT_TYPEMASK)
133 #define SF_CODEC(x)			((x) & SF_FORMAT_SUBMASK)
134 #define SF_ENDIAN(x)		((x) & SF_FORMAT_ENDMASK)
135 
136 /*
137 **	Binheader cast macros.
138 */
139 
140 #define BHW1(x) ((uint8_t) (x))
141 #define BHW2(x) ((uint16_t) (x))
142 #define BHW3(x) ((uint32_t) (x))
143 #define BHW4(x) ((uint32_t) (x))
144 #define BHW8(x) ((uint64_t) (x))
145 
146 #define BHWm(x) ((uint32_t) (x))
147 #define BHWS(x) ((char *) (x))
148 
149 #define BHWf(x) ((double) (x))
150 #define BHWd(x) ((double) (x))
151 
152 #define BHWh(x) ((void *) (x))
153 #define BHWj(x) ((size_t) (x))
154 #define BHWp(x) ((char *) (x))
155 #define BHWo(x) ((size_t) (x))
156 #define BHWs(x) ((char *) (x))
157 #define BHWv(x) ((const void *) (x))
158 #define BHWz(x) ((size_t) (x))
159 
160 /*------------------------------------------------------------------------------
161 */
162 
163 enum
164 {	/* PEAK chunk location. */
165 	SF_PEAK_START		= 42,
166 	SF_PEAK_END			= 43,
167 
168 	/* PEAK chunk location. */
169 	SF_SCALE_MAX		= 52,
170 	SF_SCALE_MIN		= 53,
171 
172 	/* str_flags values. */
173 	SF_STR_ALLOW_START	= 0x0100,
174 	SF_STR_ALLOW_END	= 0x0200,
175 
176 	/* Location of strings. */
177 	SF_STR_LOCATE_START	= 0x0400,
178 	SF_STR_LOCATE_END	= 0x0800,
179 
180 	SFD_TYPEMASK		= 0x0FFFFFFF
181 } ;
182 
183 #define		SFM_MASK 	(SFM_READ | SFM_WRITE | SFM_RDWR)
184 #define		SFM_UNMASK 	(~SFM_MASK)
185 
186 /*---------------------------------------------------------------------------------------
187 ** Formats that may be supported at some time in the future.
188 ** When support is finalised, these values move to src/sndfile.h.
189 */
190 
191 enum
192 {	/* Work in progress. */
193 	SF_FORMAT_SPEEX			= 0x5000000,
194 	SF_FORMAT_OGGFLAC		= 0x5000001,
195 
196 	/* Formats supported read only. */
197 	SF_FORMAT_TXW			= 0x4030000,		/* Yamaha TX16 sampler file */
198 	SF_FORMAT_DWD			= 0x4040000,		/* DiamondWare Digirized */
199 
200 	/* Following are detected but not supported. */
201 	SF_FORMAT_REX			= 0x40A0000,		/* Propellorheads Rex/Rcy */
202 	SF_FORMAT_REX2			= 0x40D0000,		/* Propellorheads Rex2 */
203 	SF_FORMAT_KRZ			= 0x40E0000,		/* Kurzweil sampler file */
204 	SF_FORMAT_WMA			= 0x4100000,		/* Windows Media Audio. */
205 	SF_FORMAT_SHN			= 0x4110000,		/* Shorten. */
206 
207 	/* Unsupported encodings. */
208 	SF_FORMAT_SVX_FIB		= 0x1020, 		/* SVX Fibonacci Delta encoding. */
209 	SF_FORMAT_SVX_EXP		= 0x1021, 		/* SVX Exponential Delta encoding. */
210 
211 	SF_FORMAT_PCM_N			= 0x1030
212 } ;
213 
214 /*---------------------------------------------------------------------------------------
215 */
216 
217 typedef struct
218 {	unsigned	kuki_offset ;
219 	unsigned	pakt_offset ;
220 
221 	unsigned	bits_per_sample ;
222 	unsigned	frames_per_packet ;
223 
224 	int64_t 	packets ;
225 	int64_t 	valid_frames ;
226 	int32_t 	priming_frames ;
227 	int32_t 	remainder_frames ;
228 } ALAC_DECODER_INFO ;
229 
230 /*---------------------------------------------------------------------------------------
231 **	PEAK_CHUNK - This chunk type is common to both AIFF and WAVE files although their
232 **	endian encodings are different.
233 */
234 
235 typedef struct
236 {	double		value ;		/* signed value of peak */
237 	sf_count_t	position ;	/* the sample frame for the peak */
238 } PEAK_POS ;
239 
240 typedef struct
241 {	/* libsndfile internal : write a PEAK chunk at the start or end of the file? */
242 	int				peak_loc ;
243 
244 	/* WAV/AIFF */
245 	unsigned int	version ;	/* version of the PEAK chunk */
246 	unsigned int	timestamp ;	/* secs since 1/1/1970  */
247 
248 	/* CAF */
249 	unsigned int	edit_number ;
250 
251 	/* the per channel peak info */
252 	PEAK_POS		peaks [] ;
253 } PEAK_INFO ;
254 
255 static inline PEAK_INFO *
peak_info_calloc(int channels)256 peak_info_calloc (int channels)
257 {	return calloc (1, sizeof (PEAK_INFO) + channels * sizeof (PEAK_POS)) ;
258 } /* peak_info_calloc */
259 
260 typedef struct
261 {	int		type ;
262 	int		flags ;
263 	size_t 	offset ;
264 } STR_DATA ;
265 
266 typedef struct
267 {	uint64_t	hash ;
268 	char		id [64] ;
269 	unsigned	id_size ;
270 	uint32_t	mark32 ;
271 	sf_count_t	offset ;
272 	uint32_t	len ;
273 } READ_CHUNK ;
274 
275 typedef struct
276 {	uint64_t	hash ;
277 	uint32_t	mark32 ;
278 	uint32_t	len ;
279 	void		*data ;
280 } WRITE_CHUNK ;
281 
282 typedef struct
283 {	uint32_t	count ;
284 	uint32_t	used ;
285 	READ_CHUNK	*chunks ;
286 } READ_CHUNKS ;
287 typedef struct
288 {	uint32_t	count ;
289 	uint32_t	used ;
290 	WRITE_CHUNK	*chunks ;
291 } WRITE_CHUNKS ;
292 
293 struct SF_CHUNK_ITERATOR
294 {	uint32_t	current ;
295 	int64_t		hash ;
296 	char		id [64] ;
297 	unsigned	id_size ;
298 	SNDFILE		*sndfile ;
299 } ;
300 
301 static inline size_t
make_size_t(int x)302 make_size_t (int x)
303 {	return (size_t) x ;
304 } /* make_size_t */
305 
306 typedef SF_BROADCAST_INFO_VAR (16 * 1024) SF_BROADCAST_INFO_16K ;
307 
308 typedef SF_CART_INFO_VAR (16 * 1024) SF_CART_INFO_16K ;
309 
310 #if SIZEOF_WCHAR_T == 2
311 typedef wchar_t	sfwchar_t ;
312 #else
313 typedef int16_t sfwchar_t ;
314 #endif
315 
316 
317 static inline void *
psf_memdup(const void * src,size_t n)318 psf_memdup (const void *src, size_t n)
319 {	void * mem = calloc (1, n & 3 ? n + 4 - (n & 3) : n) ;
320 	return memcpy (mem, src, n) ;
321 } /* psf_memdup */
322 
323 /*
324 **	This version of isprint specifically ignores any locale info. Its used for
325 **	determining which characters can be printed in things like hexdumps.
326 */
327 static inline int
psf_isprint(int ch)328 psf_isprint (int ch)
329 {	return (ch >= ' ' && ch <= '~') ;
330 } /* psf_isprint */
331 
332 /*=======================================================================================
333 **	SF_PRIVATE stuct - a pointer to this struct is passed back to the caller of the
334 **	sf_open_XXXX functions. The caller however has no knowledge of the struct's
335 **	contents.
336 */
337 
338 typedef struct
339 {
340 	union
341 	{	char		c [SF_FILENAME_LEN] ;
342 		sfwchar_t	wc [SF_FILENAME_LEN] ;
343 	} path ;
344 
345 	union
346 	{	char		c [SF_FILENAME_LEN] ;
347 		sfwchar_t	wc [SF_FILENAME_LEN] ;
348 	} dir ;
349 
350 	union
351 	{	char		c [SF_FILENAME_LEN / 4] ;
352 		sfwchar_t	wc [SF_FILENAME_LEN / 4] ;
353 	} name ;
354 
355 #if USE_WINDOWS_API
356 	/*
357 	**	These fields can only be used in src/file_io.c.
358 	**	They are basically the same as a windows file HANDLE.
359 	*/
360 	void 			*handle, *hsaved ;
361 
362 	int				use_wchar ;
363 #else
364 	/* These fields can only be used in src/file_io.c. */
365 	int 			filedes, savedes ;
366 #endif
367 
368 	int				do_not_close_descriptor ;
369 	int				mode ;			/* Open mode : SFM_READ, SFM_WRITE or SFM_RDWR. */
370 } PSF_FILE ;
371 
372 
373 
374 typedef union
375 {	double			dbuf	[SF_BUFFER_LEN / sizeof (double)] ;
376 #if (defined (SIZEOF_INT64_T) && (SIZEOF_INT64_T == 8))
377 	int64_t			lbuf	[SF_BUFFER_LEN / sizeof (int64_t)] ;
378 #else
379 	long			lbuf	[SF_BUFFER_LEN / sizeof (double)] ;
380 #endif
381 	float			fbuf	[SF_BUFFER_LEN / sizeof (float)] ;
382 	int				ibuf	[SF_BUFFER_LEN / sizeof (int)] ;
383 	short			sbuf	[SF_BUFFER_LEN / sizeof (short)] ;
384 	char			cbuf	[SF_BUFFER_LEN / sizeof (char)] ;
385 	signed char		scbuf	[SF_BUFFER_LEN / sizeof (signed char)] ;
386 	unsigned char	ucbuf	[SF_BUFFER_LEN / sizeof (signed char)] ;
387 } BUF_UNION ;
388 
389 
390 
391 typedef struct sf_private_tag
392 {
393 	/* Canary in a coal mine. */
394 	union
395 	{	/* Place a double here to encourage double alignment. */
396 		double d [2] ;
397 		char c [16] ;
398 		} canary ;
399 
400 	PSF_FILE		file, rsrc ;
401 
402 	char			syserr		[SF_SYSERR_LEN] ;
403 
404 	/* parselog and indx should only be changed within the logging functions
405 	** of common.c
406 	*/
407 	struct
408 	{	char			buf	[SF_PARSELOG_LEN] ;
409 		int				indx ;
410 	} parselog ;
411 
412 
413 	struct
414 	{	unsigned char	* ptr ;
415 		sf_count_t		indx, end, len ;
416 	} header ;
417 
418 	int				rwf_endian ;	/* Header endian-ness flag. */
419 
420 	/* Storage and housekeeping data for adding/reading strings from
421 	** sound files.
422 	*/
423 	struct
424 	{	STR_DATA	data [SF_MAX_STRINGS] ;
425 		char		*storage ;
426 		size_t		storage_len ;
427 		size_t		storage_used ;
428 		uint32_t	flags ;
429 	} strings ;
430 
431 	/* Guard value. If this changes the buffers above have overflowed. */
432 	int				Magick ;
433 
434 	unsigned		unique_id ;
435 
436 	int				error ;
437 
438 	int				endian ;		/* File endianness : SF_ENDIAN_LITTLE or SF_ENDIAN_BIG. */
439 	int				data_endswap ;	/* Need to endswap data? */
440 
441 	/*
442 	** Maximum float value for calculating the multiplier for
443 	** float/double to short/int conversions.
444 	*/
445 	int				float_int_mult ;
446 	float			float_max ;
447 
448 	int				scale_int_float ;
449 
450 	/* Vairables for handling pipes. */
451 	int				is_pipe ;		/* True if file is a pipe. */
452 	sf_count_t		pipeoffset ;	/* Number of bytes read from a pipe. */
453 
454 	/* True if clipping must be performed on float->int conversions. */
455 	int				add_clipping ;
456 
457 	SF_INFO			sf ;
458 
459 	int				have_written ;	/* Has a single write been done to the file? */
460 	PEAK_INFO		*peak_info ;
461 
462 	/* Cue Marker Info */
463 	SF_CUES		*cues ;
464 
465 	/* Loop Info */
466 	SF_LOOP_INFO	*loop_info ;
467 	SF_INSTRUMENT	*instrument ;
468 
469 	/* Broadcast (EBU) Info */
470 	SF_BROADCAST_INFO_16K *broadcast_16k ;
471 
472 	/* Cart (AES46) Info */
473 	SF_CART_INFO_16K *cart_16k ;
474 
475 	/* Channel map data (if present) : an array of ints. */
476 	int				*channel_map ;
477 
478 	sf_count_t		filelength ;	/* Overall length of (embedded) file. */
479 	sf_count_t		fileoffset ;	/* Offset in number of bytes from beginning of file. */
480 
481 	sf_count_t		rsrclength ;	/* Length of the resource fork (if it exists). */
482 
483 	sf_count_t		dataoffset ;	/* Offset in number of bytes from beginning of file. */
484 	sf_count_t		datalength ;	/* Length in bytes of the audio data. */
485 	sf_count_t		dataend ;		/* Offset to file tailer. */
486 
487 	int				blockwidth ;	/* Size in bytes of one set of interleaved samples. */
488 	int				bytewidth ;		/* Size in bytes of one sample (one channel). */
489 
490 	void			*dither ;
491 	void			*interleave ;
492 
493 	int				last_op ;		/* Last operation; either SFM_READ or SFM_WRITE */
494 	sf_count_t		read_current ;
495 	sf_count_t		write_current ;
496 
497 	void			*container_data ;	/*	This is a pointer to dynamically allocated file
498 										**	container format specific data.
499 										*/
500 
501 	void			*codec_data ;		/*	This is a pointer to dynamically allocated file
502 										**	codec format specific data.
503 										*/
504 
505 	SF_DITHER_INFO	write_dither ;
506 	SF_DITHER_INFO	read_dither ;
507 
508 	int				norm_double ;
509 	int				norm_float ;
510 
511 	int				auto_header ;
512 
513 	int				ieee_replace ;
514 
515 	/* A set of file specific function pointers */
516 	sf_count_t		(*read_short)	(struct sf_private_tag*, short *ptr, sf_count_t len) ;
517 	sf_count_t		(*read_int)		(struct sf_private_tag*, int *ptr, sf_count_t len) ;
518 	sf_count_t		(*read_float)	(struct sf_private_tag*, float *ptr, sf_count_t len) ;
519 	sf_count_t		(*read_double)	(struct sf_private_tag*, double *ptr, sf_count_t len) ;
520 
521 	sf_count_t		(*write_short)	(struct sf_private_tag*, const short *ptr, sf_count_t len) ;
522 	sf_count_t		(*write_int)	(struct sf_private_tag*, const int *ptr, sf_count_t len) ;
523 	sf_count_t		(*write_float)	(struct sf_private_tag*, const float *ptr, sf_count_t len) ;
524 	sf_count_t		(*write_double)	(struct sf_private_tag*, const double *ptr, sf_count_t len) ;
525 
526 	sf_count_t		(*seek) 		(struct sf_private_tag*, int mode, sf_count_t samples_from_start) ;
527 	int				(*write_header)	(struct sf_private_tag*, int calc_length) ;
528 	int				(*command)		(struct sf_private_tag*, int command, void *data, int datasize) ;
529 	int				(*byterate)		(struct sf_private_tag*) ;
530 
531 	/*
532 	**	Separate close functions for the codec and the container.
533 	**	The codec close function is always called first.
534 	*/
535 	int				(*codec_close)		(struct sf_private_tag*) ;
536 	int				(*container_close)	(struct sf_private_tag*) ;
537 
538 	char			*format_desc ;
539 
540 	/* Virtual I/O functions. */
541 	int					virtual_io ;
542 	SF_VIRTUAL_IO		vio ;
543 	void				*vio_user_data ;
544 
545 	/* Chunk get/set. */
546 	SF_CHUNK_ITERATOR	*iterator ;
547 
548 	READ_CHUNKS			rchunks ;
549 	WRITE_CHUNKS		wchunks ;
550 
551 	int					(*set_chunk)		(struct sf_private_tag*, const SF_CHUNK_INFO * chunk_info) ;
552 	SF_CHUNK_ITERATOR *	(*next_chunk_iterator)	(struct sf_private_tag*, SF_CHUNK_ITERATOR * iterator) ;
553 	int					(*get_chunk_size)	(struct sf_private_tag*, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
554 	int					(*get_chunk_data)	(struct sf_private_tag*, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
555 
556 	int cpu_flags ;
557 } SF_PRIVATE ;
558 
559 
560 
561 enum
562 {	SFE_NO_ERROR				= SF_ERR_NO_ERROR,
563 	SFE_BAD_OPEN_FORMAT			= SF_ERR_UNRECOGNISED_FORMAT,
564 	SFE_SYSTEM					= SF_ERR_SYSTEM,
565 	SFE_MALFORMED_FILE			= SF_ERR_MALFORMED_FILE,
566 	SFE_UNSUPPORTED_ENCODING	= SF_ERR_UNSUPPORTED_ENCODING,
567 
568 	SFE_ZERO_MAJOR_FORMAT,
569 	SFE_ZERO_MINOR_FORMAT,
570 	SFE_BAD_FILE,
571 	SFE_BAD_FILE_READ,
572 	SFE_OPEN_FAILED,
573 	SFE_BAD_SNDFILE_PTR,
574 	SFE_BAD_SF_INFO_PTR,
575 	SFE_BAD_SF_INCOMPLETE,
576 	SFE_BAD_FILE_PTR,
577 	SFE_BAD_INT_PTR,
578 	SFE_BAD_STAT_SIZE,
579 	SFE_NO_TEMP_DIR,
580 	SFE_MALLOC_FAILED,
581 	SFE_UNIMPLEMENTED,
582 	SFE_BAD_READ_ALIGN,
583 	SFE_BAD_WRITE_ALIGN,
584 	SFE_NOT_READMODE,
585 	SFE_NOT_WRITEMODE,
586 	SFE_BAD_MODE_RW,
587 	SFE_BAD_SF_INFO,
588 	SFE_BAD_OFFSET,
589 	SFE_NO_EMBED_SUPPORT,
590 	SFE_NO_EMBEDDED_RDWR,
591 	SFE_NO_PIPE_WRITE,
592 
593 	SFE_INTERNAL,
594 	SFE_BAD_COMMAND_PARAM,
595 	SFE_BAD_ENDIAN,
596 	SFE_CHANNEL_COUNT_ZERO,
597 	SFE_CHANNEL_COUNT,
598 	SFE_CHANNEL_COUNT_BAD,
599 
600 	SFE_BAD_VIRTUAL_IO,
601 
602 	SFE_INTERLEAVE_MODE,
603 	SFE_INTERLEAVE_SEEK,
604 	SFE_INTERLEAVE_READ,
605 
606 	SFE_BAD_SEEK,
607 	SFE_NOT_SEEKABLE,
608 	SFE_AMBIGUOUS_SEEK,
609 	SFE_WRONG_SEEK,
610 	SFE_SEEK_FAILED,
611 
612 	SFE_BAD_OPEN_MODE,
613 	SFE_OPEN_PIPE_RDWR,
614 	SFE_RDWR_POSITION,
615 	SFE_RDWR_BAD_HEADER,
616 	SFE_CMD_HAS_DATA,
617 	SFE_BAD_BROADCAST_INFO_SIZE,
618 	SFE_BAD_BROADCAST_INFO_TOO_BIG,
619 	SFE_BAD_CART_INFO_SIZE,
620 	SFE_BAD_CART_INFO_TOO_BIG,
621 
622 	SFE_STR_NO_SUPPORT,
623 	SFE_STR_NOT_WRITE,
624 	SFE_STR_MAX_DATA,
625 	SFE_STR_MAX_COUNT,
626 	SFE_STR_BAD_TYPE,
627 	SFE_STR_NO_ADD_END,
628 	SFE_STR_BAD_STRING,
629 	SFE_STR_WEIRD,
630 
631 	SFE_WAV_NO_RIFF,
632 	SFE_WAV_NO_WAVE,
633 	SFE_WAV_NO_FMT,
634 	SFE_WAV_BAD_FMT,
635 	SFE_WAV_FMT_SHORT,
636 	SFE_WAV_BAD_FACT,
637 	SFE_WAV_BAD_PEAK,
638 	SFE_WAV_PEAK_B4_FMT,
639 	SFE_WAV_BAD_FORMAT,
640 	SFE_WAV_BAD_BLOCKALIGN,
641 	SFE_WAV_NO_DATA,
642 	SFE_WAV_BAD_LIST,
643 	SFE_WAV_ADPCM_NOT4BIT,
644 	SFE_WAV_ADPCM_CHANNELS,
645 	SFE_WAV_ADPCM_SAMPLES,
646 	SFE_WAV_GSM610_FORMAT,
647 	SFE_WAV_UNKNOWN_CHUNK,
648 	SFE_WAV_WVPK_DATA,
649 	SFE_WAV_NMS_FORMAT,
650 
651 	SFE_AIFF_NO_FORM,
652 	SFE_AIFF_AIFF_NO_FORM,
653 	SFE_AIFF_COMM_NO_FORM,
654 	SFE_AIFF_SSND_NO_COMM,
655 	SFE_AIFF_UNKNOWN_CHUNK,
656 	SFE_AIFF_COMM_CHUNK_SIZE,
657 	SFE_AIFF_BAD_COMM_CHUNK,
658 	SFE_AIFF_PEAK_B4_COMM,
659 	SFE_AIFF_BAD_PEAK,
660 	SFE_AIFF_NO_SSND,
661 	SFE_AIFF_NO_DATA,
662 	SFE_AIFF_RW_SSND_NOT_LAST,
663 
664 	SFE_AU_UNKNOWN_FORMAT,
665 	SFE_AU_NO_DOTSND,
666 	SFE_AU_EMBED_BAD_LEN,
667 
668 	SFE_RAW_READ_BAD_SPEC,
669 	SFE_RAW_BAD_BITWIDTH,
670 	SFE_RAW_BAD_FORMAT,
671 
672 	SFE_PAF_NO_MARKER,
673 	SFE_PAF_VERSION,
674 	SFE_PAF_UNKNOWN_FORMAT,
675 	SFE_PAF_SHORT_HEADER,
676 	SFE_PAF_BAD_CHANNELS,
677 
678 	SFE_SVX_NO_FORM,
679 	SFE_SVX_NO_BODY,
680 	SFE_SVX_NO_DATA,
681 	SFE_SVX_BAD_COMP,
682 	SFE_SVX_BAD_NAME_LENGTH,
683 
684 	SFE_NIST_BAD_HEADER,
685 	SFE_NIST_CRLF_CONVERISON,
686 	SFE_NIST_BAD_ENCODING,
687 
688 	SFE_VOC_NO_CREATIVE,
689 	SFE_VOC_BAD_FORMAT,
690 	SFE_VOC_BAD_VERSION,
691 	SFE_VOC_BAD_MARKER,
692 	SFE_VOC_BAD_SECTIONS,
693 	SFE_VOC_MULTI_SAMPLERATE,
694 	SFE_VOC_MULTI_SECTION,
695 	SFE_VOC_MULTI_PARAM,
696 	SFE_VOC_SECTION_COUNT,
697 	SFE_VOC_NO_PIPE,
698 
699 	SFE_IRCAM_NO_MARKER,
700 	SFE_IRCAM_BAD_CHANNELS,
701 	SFE_IRCAM_UNKNOWN_FORMAT,
702 
703 	SFE_W64_64_BIT,
704 	SFE_W64_NO_RIFF,
705 	SFE_W64_NO_WAVE,
706 	SFE_W64_NO_DATA,
707 	SFE_W64_ADPCM_NOT4BIT,
708 	SFE_W64_ADPCM_CHANNELS,
709 	SFE_W64_GSM610_FORMAT,
710 
711 	SFE_MAT4_BAD_NAME,
712 	SFE_MAT4_NO_SAMPLERATE,
713 
714 	SFE_MAT5_BAD_ENDIAN,
715 	SFE_MAT5_NO_BLOCK,
716 	SFE_MAT5_SAMPLE_RATE,
717 
718 	SFE_PVF_NO_PVF1,
719 	SFE_PVF_BAD_HEADER,
720 	SFE_PVF_BAD_BITWIDTH,
721 
722 	SFE_DWVW_BAD_BITWIDTH,
723 	SFE_G72X_NOT_MONO,
724 	SFE_NMS_ADPCM_NOT_MONO,
725 
726 	SFE_XI_BAD_HEADER,
727 	SFE_XI_EXCESS_SAMPLES,
728 	SFE_XI_NO_PIPE,
729 
730 	SFE_HTK_NO_PIPE,
731 
732 	SFE_SDS_NOT_SDS,
733 	SFE_SDS_BAD_BIT_WIDTH,
734 
735 	SFE_SD2_FD_DISALLOWED,
736 	SFE_SD2_BAD_DATA_OFFSET,
737 	SFE_SD2_BAD_MAP_OFFSET,
738 	SFE_SD2_BAD_DATA_LENGTH,
739 	SFE_SD2_BAD_MAP_LENGTH,
740 	SFE_SD2_BAD_RSRC,
741 	SFE_SD2_BAD_SAMPLE_SIZE,
742 
743 	SFE_FLAC_BAD_HEADER,
744 	SFE_FLAC_NEW_DECODER,
745 	SFE_FLAC_INIT_DECODER,
746 	SFE_FLAC_LOST_SYNC,
747 	SFE_FLAC_BAD_SAMPLE_RATE,
748 	SFE_FLAC_CHANNEL_COUNT_CHANGED,
749 	SFE_FLAC_UNKOWN_ERROR,
750 
751 	SFE_WVE_NOT_WVE,
752 	SFE_WVE_NO_PIPE,
753 
754 	SFE_VORBIS_ENCODER_BUG,
755 
756 	SFE_RF64_NOT_RF64,
757 	SFE_RF64_PEAK_B4_FMT,
758 	SFE_RF64_NO_DATA,
759 
760 	SFE_BAD_CHUNK_PTR,
761 	SFE_UNKNOWN_CHUNK,
762 	SFE_BAD_CHUNK_FORMAT,
763 	SFE_BAD_CHUNK_MARKER,
764 	SFE_BAD_CHUNK_DATA_PTR,
765 	SFE_ALAC_FAIL_TMPFILE,
766 	SFE_FILENAME_TOO_LONG,
767 	SFE_NEGATIVE_RW_LEN,
768 
769 	SFE_OPUS_BAD_SAMPLERATE,
770 
771 	SFE_MAX_ERROR			/* This must be last in list. */
772 } ;
773 
774 /* Allocate and initialize the SF_PRIVATE struct. */
775 SF_PRIVATE * psf_allocate (void) ;
776 
777 int subformat_to_bytewidth (int format) ;
778 int s_bitwidth_to_subformat (int bits) ;
779 int u_bitwidth_to_subformat (int bits) ;
780 
781 /*  Functions for reading and writing floats and doubles on processors
782 **	with non-IEEE floats/doubles.
783 */
784 float	float32_be_read		(const unsigned char *cptr) ;
785 float	float32_le_read		(const unsigned char *cptr) ;
786 void	float32_be_write	(float in, unsigned char *out) ;
787 void	float32_le_write	(float in, unsigned char *out) ;
788 
789 double	double64_be_read	(const unsigned char *cptr) ;
790 double	double64_le_read	(const unsigned char *cptr) ;
791 void	double64_be_write	(double in, unsigned char *out) ;
792 void	double64_le_write	(double in, unsigned char *out) ;
793 
794 /* Functions for writing to the internal logging buffer. */
795 
796 void	psf_log_printf		(SF_PRIVATE *psf, const char *format, ...) ;
797 void	psf_log_SF_INFO 	(SF_PRIVATE *psf) ;
798 
799 int32_t	psf_rand_int32 (void) ;
800 
801 void append_snprintf (char * dest, size_t maxlen, const char * fmt, ...) ;
802 void psf_strlcpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
803 
804 sf_count_t psf_decode_frame_count (SF_PRIVATE *psf) ;
805 
806 /* Functions used when writing file headers. */
807 
808 int		psf_binheader_writef	(SF_PRIVATE *psf, const char *format, ...) ;
809 void	psf_asciiheader_printf	(SF_PRIVATE *psf, const char *format, ...) ;
810 
811 /* Functions used when reading file headers. */
812 
813 int		psf_binheader_readf	(SF_PRIVATE *psf, char const *format, ...) ;
814 
815 /* Functions used in the write function for updating the peak chunk. */
816 
817 void	peak_update_short	(SF_PRIVATE *psf, short *ptr, size_t items) ;
818 void	peak_update_int		(SF_PRIVATE *psf, int *ptr, size_t items) ;
819 void	peak_update_double	(SF_PRIVATE *psf, double *ptr, size_t items) ;
820 
821 /* Functions defined in command.c. */
822 
823 int		psf_get_format_simple_count	(void) ;
824 int		psf_get_format_simple		(SF_FORMAT_INFO *data) ;
825 
826 int		psf_get_format_info			(SF_FORMAT_INFO *data) ;
827 
828 int		psf_get_format_major_count	(void) ;
829 int		psf_get_format_major		(SF_FORMAT_INFO *data) ;
830 
831 int		psf_get_format_subtype_count	(void) ;
832 int		psf_get_format_subtype		(SF_FORMAT_INFO *data) ;
833 
834 void	psf_generate_format_desc (SF_PRIVATE *psf) ;
835 
836 double	psf_calc_signal_max			(SF_PRIVATE *psf, int normalize) ;
837 int		psf_calc_max_all_channels	(SF_PRIVATE *psf, double *peaks, int normalize) ;
838 
839 int		psf_get_signal_max			(SF_PRIVATE *psf, double *peak) ;
840 int		psf_get_max_all_channels	(SF_PRIVATE *psf, double *peaks) ;
841 
842 /* Functions in strings.c. */
843 
844 const char* psf_get_string (SF_PRIVATE *psf, int str_type) ;
845 int psf_set_string (SF_PRIVATE *psf, int str_type, const char *str) ;
846 int psf_store_string (SF_PRIVATE *psf, int str_type, const char *str) ;
847 int psf_location_string_count (const SF_PRIVATE * psf, int location) ;
848 
849 /* Default seek function. Use for PCM and float encoded data. */
850 sf_count_t	psf_default_seek (SF_PRIVATE *psf, int mode, sf_count_t samples_from_start) ;
851 
852 int macos_guess_file_type (SF_PRIVATE *psf, const char *filename) ;
853 
854 /*------------------------------------------------------------------------------------
855 **	File I/O functions which will allow access to large files (> 2 Gig) on
856 **	some 32 bit OSes. Implementation in file_io.c.
857 */
858 
859 int psf_fopen (SF_PRIVATE *psf) ;
860 int psf_set_stdio (SF_PRIVATE *psf) ;
861 int psf_file_valid (SF_PRIVATE *psf) ;
862 void psf_set_file (SF_PRIVATE *psf, int fd) ;
863 void psf_init_files (SF_PRIVATE *psf) ;
864 void psf_use_rsrc (SF_PRIVATE *psf, int on_off) ;
865 
866 SNDFILE * psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo) ;
867 
868 sf_count_t psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence) ;
869 sf_count_t psf_fread (void *ptr, sf_count_t bytes, sf_count_t count, SF_PRIVATE *psf) ;
870 sf_count_t psf_fwrite (const void *ptr, sf_count_t bytes, sf_count_t count, SF_PRIVATE *psf) ;
871 sf_count_t psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf) ;
872 sf_count_t psf_ftell (SF_PRIVATE *psf) ;
873 sf_count_t psf_get_filelen (SF_PRIVATE *psf) ;
874 
875 void psf_fsync (SF_PRIVATE *psf) ;
876 
877 int psf_is_pipe (SF_PRIVATE *psf) ;
878 
879 int psf_ftruncate (SF_PRIVATE *psf, sf_count_t len) ;
880 int psf_fclose (SF_PRIVATE *psf) ;
881 
882 /* Open and close the resource fork of a file. */
883 int psf_open_rsrc (SF_PRIVATE *psf) ;
884 int psf_close_rsrc (SF_PRIVATE *psf) ;
885 
886 /*
887 void psf_fclearerr (SF_PRIVATE *psf) ;
888 int psf_ferror (SF_PRIVATE *psf) ;
889 */
890 
891 /*------------------------------------------------------------------------------------
892 ** Functions for reading and writing different file formats.
893 */
894 
895 int		aiff_open	(SF_PRIVATE *psf) ;
896 int		au_open		(SF_PRIVATE *psf) ;
897 int		avr_open	(SF_PRIVATE *psf) ;
898 int		htk_open	(SF_PRIVATE *psf) ;
899 int		ircam_open	(SF_PRIVATE *psf) ;
900 int		mat4_open	(SF_PRIVATE *psf) ;
901 int		mat5_open	(SF_PRIVATE *psf) ;
902 int		nist_open	(SF_PRIVATE *psf) ;
903 int		paf_open	(SF_PRIVATE *psf) ;
904 int		pvf_open	(SF_PRIVATE *psf) ;
905 int		raw_open	(SF_PRIVATE *psf) ;
906 int		sd2_open	(SF_PRIVATE *psf) ;
907 int		sds_open	(SF_PRIVATE *psf) ;
908 int		svx_open	(SF_PRIVATE *psf) ;
909 int		voc_open	(SF_PRIVATE *psf) ;
910 int		w64_open	(SF_PRIVATE *psf) ;
911 int		wav_open	(SF_PRIVATE *psf) ;
912 int		xi_open		(SF_PRIVATE *psf) ;
913 int		flac_open	(SF_PRIVATE *psf) ;
914 int		caf_open	(SF_PRIVATE *psf) ;
915 int		mpc2k_open	(SF_PRIVATE *psf) ;
916 int		rf64_open	(SF_PRIVATE *psf) ;
917 
918 int		ogg_vorbis_open	(SF_PRIVATE *psf) ;
919 int		ogg_speex_open	(SF_PRIVATE *psf) ;
920 int		ogg_pcm_open	(SF_PRIVATE *psf) ;
921 int		ogg_opus_open	(SF_PRIVATE *psf) ;
922 int		ogg_open	(SF_PRIVATE *psf) ;
923 
924 
925 /* In progress. Do not currently work. */
926 
927 int		mpeg_open	(SF_PRIVATE *psf) ;
928 int		rx2_open	(SF_PRIVATE *psf) ;
929 int		txw_open	(SF_PRIVATE *psf) ;
930 int		wve_open	(SF_PRIVATE *psf) ;
931 int		dwd_open	(SF_PRIVATE *psf) ;
932 
933 /*------------------------------------------------------------------------------------
934 **	Init functions for a number of common data encodings.
935 */
936 
937 int		pcm_init		(SF_PRIVATE *psf) ;
938 int		ulaw_init		(SF_PRIVATE *psf) ;
939 int		alaw_init		(SF_PRIVATE *psf) ;
940 int		float32_init	(SF_PRIVATE *psf) ;
941 int		double64_init	(SF_PRIVATE *psf) ;
942 int		dwvw_init		(SF_PRIVATE *psf, int bitwidth) ;
943 int		gsm610_init		(SF_PRIVATE *psf) ;
944 int		nms_adpcm_init	(SF_PRIVATE *psf) ;
945 int		vox_adpcm_init	(SF_PRIVATE *psf) ;
946 int		flac_init		(SF_PRIVATE *psf) ;
947 int		g72x_init 		(SF_PRIVATE * psf) ;
948 int		alac_init		(SF_PRIVATE *psf, const ALAC_DECODER_INFO * info) ;
949 
950 int 	dither_init		(SF_PRIVATE *psf, int mode) ;
951 
952 int		wavlike_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
953 int		wavlike_msadpcm_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
954 
955 int		aiff_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
956 
957 int		interleave_init (SF_PRIVATE *psf) ;
958 
959 /*------------------------------------------------------------------------------------
960 ** Chunk logging functions.
961 */
962 
963 SF_CHUNK_ITERATOR * psf_get_chunk_iterator (SF_PRIVATE * psf, const char * marker_str) ;
964 SF_CHUNK_ITERATOR * psf_next_chunk_iterator (const READ_CHUNKS * pchk , SF_CHUNK_ITERATOR *iterator) ;
965 int		psf_store_read_chunk_u32 (READ_CHUNKS * pchk, uint32_t marker, sf_count_t offset, uint32_t len) ;
966 int		psf_store_read_chunk_str (READ_CHUNKS * pchk, const char * marker, sf_count_t offset, uint32_t len) ;
967 int		psf_save_write_chunk (WRITE_CHUNKS * pchk, const SF_CHUNK_INFO * chunk_info) ;
968 int		psf_find_read_chunk_str (const READ_CHUNKS * pchk, const char * marker) ;
969 int		psf_find_read_chunk_m32 (const READ_CHUNKS * pchk, uint32_t marker) ;
970 int		psf_find_read_chunk_iterator (const READ_CHUNKS * pchk, const SF_CHUNK_ITERATOR * marker) ;
971 
972 int		psf_find_write_chunk (WRITE_CHUNKS * pchk, const char * marker) ;
973 
974 static inline int
fourcc_to_marker(const SF_CHUNK_INFO * chunk_info)975 fourcc_to_marker (const SF_CHUNK_INFO * chunk_info)
976 {	const unsigned char * cptr ;
977 
978 	if (chunk_info->id_size != 4)
979 		return 0 ;
980 
981 	cptr = (const unsigned char *) chunk_info->id ;
982 	return (cptr [3] << 24) + (cptr [2] << 16) + (cptr [1] << 8) + cptr [0] ;
983 } /* fourcc_to_marker */
984 
985 /*------------------------------------------------------------------------------------
986 ** Functions that work like OpenBSD's strlcpy/strlcat to replace strncpy/strncat.
987 **
988 ** See : http://www.gratisoft.us/todd/papers/strlcpy.html
989 **
990 ** These functions are available on *BSD, but are not avaialble everywhere so we
991 ** implement them here.
992 **
993 ** The argument order has been changed to that of strncpy/strncat to cause
994 ** compiler errors if code is carelessly converted from one to the other.
995 */
996 
997 static inline void
psf_strlcat(char * dest,size_t n,const char * src)998 psf_strlcat (char *dest, size_t n, const char *src)
999 {	strncat (dest, src, n - strlen (dest) - 1) ;
1000 	dest [n - 1] = 0 ;
1001 } /* psf_strlcat */
1002 
1003 static inline void
psf_strlcpy(char * dest,size_t n,const char * src)1004 psf_strlcpy (char *dest, size_t n, const char *src)
1005 {	strncpy (dest, src, n - 1) ;
1006 	dest [n - 1] = 0 ;
1007 } /* psf_strlcpy */
1008 
1009 /*------------------------------------------------------------------------------------
1010 ** SIMD optimized math functions.
1011 */
1012 
psf_lrintf(float x)1013 static inline int psf_lrintf (float x)
1014 {
1015 	#ifdef USE_SSE2
1016  		return _mm_cvtss_si32 (_mm_load_ss (&x)) ;
1017 	#else
1018 		return lrintf (x) ;
1019 	#endif
1020 } /* psf_lrintf */
1021 
psf_lrint(double x)1022 static inline int psf_lrint (double x)
1023 {
1024 	#ifdef USE_SSE2
1025  		return _mm_cvtsd_si32 (_mm_load_sd (&x)) ;
1026 	#else
1027 		return lrint (x) ;
1028 	#endif
1029 } /* psf_lrintf */
1030 
1031 /*------------------------------------------------------------------------------------
1032 ** Other helper functions.
1033 */
1034 
1035 void	*psf_memset (void *s, int c, sf_count_t n) ;
1036 
1037 SF_CUES * psf_cues_dup (const void * ptr, size_t datasize) ;
1038 SF_CUES * psf_cues_alloc (uint32_t cue_count) ;
1039 void psf_get_cues (SF_PRIVATE * psf, void * data, size_t datasize) ;
1040 
1041 SF_INSTRUMENT * psf_instrument_alloc (void) ;
1042 
1043 void	psf_sanitize_string (char * cptr, int len) ;
1044 
1045 /* Generate the current date as a string. */
1046 void	psf_get_date_str (char *str, int maxlen) ;
1047 
1048 SF_BROADCAST_INFO_16K * broadcast_var_alloc (void) ;
1049 int		broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * data, size_t datasize) ;
1050 int		broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize) ;
1051 
1052 
1053 SF_CART_INFO_16K * cart_var_alloc (void) ;
1054 int 		cart_var_set (SF_PRIVATE *psf, const SF_CART_INFO * date, size_t datasize) ;
1055 int		cart_var_get (SF_PRIVATE *psf, SF_CART_INFO * data, size_t datasize) ;
1056 
1057 typedef struct
1058 {	int channels ;
1059 	int endianness ;
1060 } AUDIO_DETECT ;
1061 
1062 int audio_detect (SF_PRIVATE * psf, AUDIO_DETECT *ad, const unsigned char * data, int datalen) ;
1063 int id3_skip (SF_PRIVATE * psf) ;
1064 
1065 void	alac_get_desc_chunk_items (int subformat, uint32_t *fmt_flags, uint32_t *frames_per_packet) ;
1066 
1067 FILE *	psf_open_tmpfile (char * fname, size_t fnamelen) ;
1068 
1069 /*------------------------------------------------------------------------------------
1070 ** Helper/debug functions.
1071 */
1072 
1073 void	psf_hexdump (const void *ptr, int len) ;
1074 
1075 const char * str_of_major_format (int format) ;
1076 const char * str_of_minor_format (int format) ;
1077 const char * str_of_open_mode (int mode) ;
1078 const char * str_of_endianness (int end) ;
1079 
1080 /*------------------------------------------------------------------------------------
1081 ** Extra commands for sf_command(). Not for public use yet.
1082 */
1083 
1084 enum
1085 {	SFC_TEST_AIFF_ADD_INST_CHUNK	= 0x2000,
1086 	SFC_TEST_WAV_ADD_INFO_CHUNK		= 0x2010
1087 } ;
1088 
1089 /*
1090 ** Maybe, one day, make these functions or something like them, public.
1091 **
1092 ** Buffer to buffer dithering. Pointer in and out are allowed to point
1093 ** to the same buffer for in-place dithering.
1094 */
1095 
1096 #if 0
1097 int sf_dither_short		(const SF_DITHER_INFO *dither, const short *in, short *out, int count) ;
1098 int sf_dither_int		(const SF_DITHER_INFO *dither, const int *in, int *out, int count) ;
1099 int sf_dither_float		(const SF_DITHER_INFO *dither, const float *in, float *out, int count) ;
1100 int sf_dither_double	(const SF_DITHER_INFO *dither, const double *in, double *out, int count) ;
1101 #endif
1102 
1103 /*------------------------------------------------------------------------------------
1104 ** Data conversion functions.
1105 */
1106 
1107 void psf_f2s_array (const float *src, short *dest, int count, int normalize) ;
1108 void psf_f2s_clip_array (const float *src, short *dest, int count, int normalize) ;
1109 
1110 void psf_d2s_array (const double *src, short *dest, int count, int normalize) ;
1111 void psf_d2s_clip_array (const double *src, short *dest, int count, int normalize) ;
1112 
1113 void psf_f2i_array (const float *src, int *dest, int count, int normalize) ;
1114 void psf_f2i_clip_array (const float *src, int *dest, int count, int normalize) ;
1115 
1116 void psf_d2i_array (const double *src, int *dest, int count, int normalize) ;
1117 void psf_d2i_clip_array (const double *src, int *dest, int count, int normalize) ;
1118 
1119 
1120 /*------------------------------------------------------------------------------------
1121 ** Left and right shift on int. According to the C standard, the left and right
1122 ** shift operations applied to a negative integer results in undefined behavior.
1123 ** These twp functions work around that.
1124 */
1125 
1126 #if __GNUC__
1127 #define ALWAYS_INLINE		__attribute__ ((always_inline))
1128 #else
1129 #define ALWAYS_INLINE
1130 #endif
1131 
1132 static inline int32_t ALWAYS_INLINE
arith_shift_left(int32_t x,int shift)1133 arith_shift_left (int32_t x, int shift)
1134 {	return (int32_t) (((uint32_t) x) << shift) ;
1135 } /* arith_shift_left */
1136 
1137 static inline int32_t ALWAYS_INLINE
arith_shift_right(int32_t x,int shift)1138 arith_shift_right (int32_t x, int shift)
1139 {	if (x >= 0)
1140 		return x >> shift ;
1141 	return ~ ((~x) >> shift) ;
1142 } /* arith_shift_right */
1143 
1144 #endif /* SNDFILE_COMMON_H */
1145