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