• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
135 enum
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 
163 enum
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 
189 typedef 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 
207 typedef struct
208 {	double		value ;		/* signed value of peak */
209 	sf_count_t	position ;	/* the sample frame for the peak */
210 } PEAK_POS ;
211 
212 typedef 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 
227 static inline PEAK_INFO *
peak_info_calloc(int channels)228 peak_info_calloc (int channels)
229 {	return calloc (1, sizeof (PEAK_INFO) + channels * sizeof (PEAK_POS)) ;
230 } /* peak_info_calloc */
231 
232 typedef struct
233 {	int		type ;
234 	int		flags ;
235 	size_t 	offset ;
236 } STR_DATA ;
237 
238 typedef 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 
247 typedef struct
248 {	uint64_t	hash ;
249 	uint32_t	mark32 ;
250 	uint32_t	len ;
251 	void		*data ;
252 } WRITE_CHUNK ;
253 
254 typedef struct
255 {	uint32_t	count ;
256 	uint32_t	used ;
257 	READ_CHUNK	*chunks ;
258 } READ_CHUNKS ;
259 typedef struct
260 {	uint32_t	count ;
261 	uint32_t	used ;
262 	WRITE_CHUNK	*chunks ;
263 } WRITE_CHUNKS ;
264 
265 struct SF_CHUNK_ITERATOR
266 {	uint32_t	current ;
267 	int64_t		hash ;
268 	char		id [64] ;
269 	unsigned	id_size ;
270 	SNDFILE		*sndfile ;
271 } ;
272 
273 static inline size_t
make_size_t(int x)274 make_size_t (int x)
275 {	return (size_t) x ;
276 } /* make_size_t */
277 
278 typedef SF_BROADCAST_INFO_VAR (16 * 1024) SF_BROADCAST_INFO_16K ;
279 
280 typedef SF_CART_INFO_VAR (16 * 1024) SF_CART_INFO_16K ;
281 
282 typedef 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
289 typedef wchar_t	sfwchar_t ;
290 #else
291 typedef int16_t sfwchar_t ;
292 #endif
293 
294 
295 void *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 */
301 int 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 
309 typedef 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 
332 typedef 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 
349 typedef 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 
514 enum
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. */
739 SF_PRIVATE * psf_allocate (void) ;
740 
741 int subformat_to_bytewidth (int format) ;
742 int s_bitwidth_to_subformat (int bits) ;
743 int 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 */
748 float	float32_be_read		(const unsigned char *cptr) ;
749 float	float32_le_read		(const unsigned char *cptr) ;
750 void	float32_be_write	(float in, unsigned char *out) ;
751 void	float32_le_write	(float in, unsigned char *out) ;
752 
753 double	double64_be_read	(const unsigned char *cptr) ;
754 double	double64_le_read	(const unsigned char *cptr) ;
755 void	double64_be_write	(double in, unsigned char *out) ;
756 void	double64_le_write	(double in, unsigned char *out) ;
757 
758 /* Functions for writing to the internal logging buffer. */
759 
760 void	psf_log_printf		(SF_PRIVATE *psf, const char *format, ...) ;
761 void	psf_log_SF_INFO 	(SF_PRIVATE *psf) ;
762 
763 int32_t	psf_rand_int32 (void) ;
764 
765 void append_snprintf (char * dest, size_t maxlen, const char * fmt, ...) ;
766 void psf_strlcpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
767 
768 sf_count_t psf_decode_frame_count (SF_PRIVATE *psf) ;
769 
770 /* Functions used when writing file headers. */
771 
772 int		psf_binheader_writef	(SF_PRIVATE *psf, const char *format, ...) ;
773 void	psf_asciiheader_printf	(SF_PRIVATE *psf, const char *format, ...) ;
774 
775 /* Functions used when reading file headers. */
776 
777 int		psf_binheader_readf	(SF_PRIVATE *psf, char const *format, ...) ;
778 
779 /* Functions used in the write function for updating the peak chunk. */
780 
781 void	peak_update_short	(SF_PRIVATE *psf, short *ptr, size_t items) ;
782 void	peak_update_int		(SF_PRIVATE *psf, int *ptr, size_t items) ;
783 void	peak_update_double	(SF_PRIVATE *psf, double *ptr, size_t items) ;
784 
785 /* Functions defined in command.c. */
786 
787 int		psf_get_format_simple_count	(void) ;
788 int		psf_get_format_simple		(SF_FORMAT_INFO *data) ;
789 
790 int		psf_get_format_info			(SF_FORMAT_INFO *data) ;
791 
792 int		psf_get_format_major_count	(void) ;
793 int		psf_get_format_major		(SF_FORMAT_INFO *data) ;
794 
795 int		psf_get_format_subtype_count	(void) ;
796 int		psf_get_format_subtype		(SF_FORMAT_INFO *data) ;
797 
798 void	psf_generate_format_desc (SF_PRIVATE *psf) ;
799 
800 double	psf_calc_signal_max			(SF_PRIVATE *psf, int normalize) ;
801 int		psf_calc_max_all_channels	(SF_PRIVATE *psf, double *peaks, int normalize) ;
802 
803 int		psf_get_signal_max			(SF_PRIVATE *psf, double *peak) ;
804 int		psf_get_max_all_channels	(SF_PRIVATE *psf, double *peaks) ;
805 
806 /* Functions in strings.c. */
807 
808 const char* psf_get_string (SF_PRIVATE *psf, int str_type) ;
809 int psf_set_string (SF_PRIVATE *psf, int str_type, const char *str) ;
810 int psf_store_string (SF_PRIVATE *psf, int str_type, const char *str) ;
811 int psf_location_string_count (const SF_PRIVATE * psf, int location) ;
812 
813 /* Default seek function. Use for PCM and float encoded data. */
814 sf_count_t	psf_default_seek (SF_PRIVATE *psf, int mode, sf_count_t samples_from_start) ;
815 
816 int 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 
823 int psf_fopen (SF_PRIVATE *psf) ;
824 int psf_set_stdio (SF_PRIVATE *psf) ;
825 int psf_file_valid (SF_PRIVATE *psf) ;
826 void psf_set_file (SF_PRIVATE *psf, int fd) ;
827 void psf_init_files (SF_PRIVATE *psf) ;
828 void psf_use_rsrc (SF_PRIVATE *psf, int on_off) ;
829 
830 SNDFILE * psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo) ;
831 
832 sf_count_t psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence) ;
833 sf_count_t psf_fread (void *ptr, sf_count_t bytes, sf_count_t count, SF_PRIVATE *psf) ;
834 sf_count_t psf_fwrite (const void *ptr, sf_count_t bytes, sf_count_t count, SF_PRIVATE *psf) ;
835 sf_count_t psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf) ;
836 sf_count_t psf_ftell (SF_PRIVATE *psf) ;
837 sf_count_t psf_get_filelen (SF_PRIVATE *psf) ;
838 
839 void psf_fsync (SF_PRIVATE *psf) ;
840 
841 int psf_is_pipe (SF_PRIVATE *psf) ;
842 
843 int psf_ftruncate (SF_PRIVATE *psf, sf_count_t len) ;
844 int psf_fclose (SF_PRIVATE *psf) ;
845 
846 /* Open and close the resource fork of a file. */
847 int psf_open_rsrc (SF_PRIVATE *psf) ;
848 int psf_close_rsrc (SF_PRIVATE *psf) ;
849 
850 int	psf_copy_filename (SF_PRIVATE *psf, const char *path) ;
851 
852 /*
853 void psf_fclearerr (SF_PRIVATE *psf) ;
854 int psf_ferror (SF_PRIVATE *psf) ;
855 */
856 
857 /*------------------------------------------------------------------------------------
858 ** Functions for reading and writing different file formats.
859 */
860 
861 int		aiff_open	(SF_PRIVATE *psf) ;
862 int		au_open		(SF_PRIVATE *psf) ;
863 int		avr_open	(SF_PRIVATE *psf) ;
864 int		htk_open	(SF_PRIVATE *psf) ;
865 int		ircam_open	(SF_PRIVATE *psf) ;
866 int		mat4_open	(SF_PRIVATE *psf) ;
867 int		mat5_open	(SF_PRIVATE *psf) ;
868 int		nist_open	(SF_PRIVATE *psf) ;
869 int		paf_open	(SF_PRIVATE *psf) ;
870 int		pvf_open	(SF_PRIVATE *psf) ;
871 int		raw_open	(SF_PRIVATE *psf) ;
872 int		sd2_open	(SF_PRIVATE *psf) ;
873 int		sds_open	(SF_PRIVATE *psf) ;
874 int		svx_open	(SF_PRIVATE *psf) ;
875 int		voc_open	(SF_PRIVATE *psf) ;
876 int		w64_open	(SF_PRIVATE *psf) ;
877 int		wav_open	(SF_PRIVATE *psf) ;
878 int		xi_open		(SF_PRIVATE *psf) ;
879 int		flac_open	(SF_PRIVATE *psf) ;
880 int		caf_open	(SF_PRIVATE *psf) ;
881 int		mpc2k_open	(SF_PRIVATE *psf) ;
882 int		rf64_open	(SF_PRIVATE *psf) ;
883 
884 int		ogg_vorbis_open	(SF_PRIVATE *psf) ;
885 int		ogg_speex_open	(SF_PRIVATE *psf) ;
886 int		ogg_pcm_open	(SF_PRIVATE *psf) ;
887 int		ogg_opus_open	(SF_PRIVATE *psf) ;
888 int		ogg_open	(SF_PRIVATE *psf) ;
889 
890 int		mpeg_open	(SF_PRIVATE *psf) ;
891 
892 /* In progress. Do not currently work. */
893 
894 int		rx2_open	(SF_PRIVATE *psf) ;
895 int		txw_open	(SF_PRIVATE *psf) ;
896 int		wve_open	(SF_PRIVATE *psf) ;
897 int		dwd_open	(SF_PRIVATE *psf) ;
898 
899 /*------------------------------------------------------------------------------------
900 **	Init functions for a number of common data encodings.
901 */
902 
903 int		pcm_init		(SF_PRIVATE *psf) ;
904 int		ulaw_init		(SF_PRIVATE *psf) ;
905 int		alaw_init		(SF_PRIVATE *psf) ;
906 int		float32_init	(SF_PRIVATE *psf) ;
907 int		double64_init	(SF_PRIVATE *psf) ;
908 int		dwvw_init		(SF_PRIVATE *psf, int bitwidth) ;
909 int		gsm610_init		(SF_PRIVATE *psf) ;
910 int		nms_adpcm_init	(SF_PRIVATE *psf) ;
911 int		vox_adpcm_init	(SF_PRIVATE *psf) ;
912 int		flac_init		(SF_PRIVATE *psf) ;
913 int		g72x_init 		(SF_PRIVATE * psf) ;
914 int		alac_init		(SF_PRIVATE *psf, const ALAC_DECODER_INFO * info) ;
915 int		mpeg_init		(SF_PRIVATE *psf, int bitrate_mode, int write_metadata) ;
916 
917 int 	dither_init		(SF_PRIVATE *psf, int mode) ;
918 
919 int		wavlike_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
920 int		wavlike_msadpcm_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
921 
922 int		aiff_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
923 
924 int		interleave_init (SF_PRIVATE *psf) ;
925 
926 /*------------------------------------------------------------------------------------
927 ** Chunk logging functions.
928 */
929 
930 SF_CHUNK_ITERATOR * psf_get_chunk_iterator (SF_PRIVATE * psf, const char * marker_str) ;
931 SF_CHUNK_ITERATOR * psf_next_chunk_iterator (const READ_CHUNKS * pchk , SF_CHUNK_ITERATOR *iterator) ;
932 int		psf_store_read_chunk_u32 (READ_CHUNKS * pchk, uint32_t marker, sf_count_t offset, uint32_t len) ;
933 int		psf_store_read_chunk_str (READ_CHUNKS * pchk, const char * marker, sf_count_t offset, uint32_t len) ;
934 int		psf_save_write_chunk (WRITE_CHUNKS * pchk, const SF_CHUNK_INFO * chunk_info) ;
935 int		psf_find_read_chunk_str (const READ_CHUNKS * pchk, const char * marker) ;
936 int		psf_find_read_chunk_m32 (const READ_CHUNKS * pchk, uint32_t marker) ;
937 int		psf_find_read_chunk_iterator (const READ_CHUNKS * pchk, const SF_CHUNK_ITERATOR * marker) ;
938 
939 int		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 
953 void psf_strlcat (char *dest, size_t n, const char *src) ;
954 void psf_strlcpy (char *dest, size_t n, const char *src) ;
955 
956 /*------------------------------------------------------------------------------------
957 ** SIMD optimized math functions.
958 */
959 
psf_lrintf(float x)960 static 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 
psf_lrint(double x)969 static 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 
982 void	*psf_memset (void *s, int c, sf_count_t n) ;
983 
984 SF_CUES * psf_cues_dup (const void * ptr, size_t datasize) ;
985 SF_CUES * psf_cues_alloc (uint32_t cue_count) ;
986 void psf_get_cues (SF_PRIVATE * psf, void * data, size_t datasize) ;
987 
988 SF_INSTRUMENT * psf_instrument_alloc (void) ;
989 
990 void	psf_sanitize_string (char * cptr, int len) ;
991 
992 /* Generate the current date as a string. */
993 void	psf_get_date_str (char *str, int maxlen) ;
994 
995 SF_BROADCAST_INFO_16K * broadcast_var_alloc (void) ;
996 int		broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * data, size_t datasize) ;
997 int		broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize) ;
998 
999 
1000 SF_CART_INFO_16K * cart_var_alloc (void) ;
1001 int 		cart_var_set (SF_PRIVATE *psf, const SF_CART_INFO * date, size_t datasize) ;
1002 int		cart_var_get (SF_PRIVATE *psf, SF_CART_INFO * data, size_t datasize) ;
1003 
1004 typedef struct
1005 {	int channels ;
1006 	int endianness ;
1007 } AUDIO_DETECT ;
1008 
1009 int audio_detect (SF_PRIVATE * psf, AUDIO_DETECT *ad, const unsigned char * data, int datalen) ;
1010 int id3_skip (SF_PRIVATE * psf) ;
1011 const char *id3_lookup_v1_genre (int number) ;
1012 
1013 void	alac_get_desc_chunk_items (int subformat, uint32_t *fmt_flags, uint32_t *frames_per_packet) ;
1014 
1015 FILE *	psf_open_tmpfile (char * fname, size_t fnamelen) ;
1016 
1017 /*------------------------------------------------------------------------------------
1018 ** Helper/debug functions.
1019 */
1020 
1021 void	psf_hexdump (const void *ptr, int len) ;
1022 
1023 const char * str_of_major_format (int format) ;
1024 const char * str_of_minor_format (int format) ;
1025 const char * str_of_open_mode (int mode) ;
1026 const char * str_of_endianness (int end) ;
1027 
1028 /*------------------------------------------------------------------------------------
1029 ** Extra commands for sf_command(). Not for public use yet.
1030 */
1031 
1032 enum
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
1045 int sf_dither_short		(const SF_DITHER_INFO *dither, const short *in, short *out, int count) ;
1046 int sf_dither_int		(const SF_DITHER_INFO *dither, const int *in, int *out, int count) ;
1047 int sf_dither_float		(const SF_DITHER_INFO *dither, const float *in, float *out, int count) ;
1048 int 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 
1055 void psf_f2s_array (const float *src, short *dest, int count, int normalize) ;
1056 void psf_f2s_clip_array (const float *src, short *dest, int count, int normalize) ;
1057 
1058 void psf_d2s_array (const double *src, short *dest, int count, int normalize) ;
1059 void psf_d2s_clip_array (const double *src, short *dest, int count, int normalize) ;
1060 
1061 void psf_f2i_array (const float *src, int *dest, int count, int normalize) ;
1062 void psf_f2i_clip_array (const float *src, int *dest, int count, int normalize) ;
1063 
1064 void psf_d2i_array (const double *src, int *dest, int count, int normalize) ;
1065 void 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 
1080 static inline int32_t ALWAYS_INLINE
arith_shift_left(int32_t x,int shift)1081 arith_shift_left (int32_t x, int shift)
1082 {	return (int32_t) (((uint32_t) x) << shift) ;
1083 } /* arith_shift_left */
1084 
1085 static inline int32_t ALWAYS_INLINE
arith_shift_right(int32_t x,int shift)1086 arith_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