Imported Upstream version 1.0.25
[platform/upstream/libsndfile.git] / src / common.h
1 /*
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #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_STDINT_H
28 #include <stdint.h>
29 #elif HAVE_INTTYPES_H
30 #include <inttypes.h>
31 #endif
32
33 #ifndef SNDFILE_H
34 #include "sndfile.h"
35 #endif
36
37 #ifdef __cplusplus
38 #error "This code is not designed to be compiled with a C++ compiler."
39 #endif
40
41 #if (SIZEOF_LONG == 8)
42 #       define  SF_PLATFORM_S64(x)              x##l
43 #elif (SIZEOF_LONG_LONG == 8)
44 #       define  SF_PLATFORM_S64(x)              x##ll
45 #elif COMPILER_IS_GCC
46 #       define  SF_PLATFORM_S64(x)              x##ll
47 #elif OS_IS_WIN32
48 #       define  SF_PLATFORM_S64(x)              x##I64
49 #else
50 #       error "Don't know how to define a 64 bit integer constant."
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*2)
74 #define SF_FILENAME_LEN                 (512)
75 #define SF_SYSERR_LEN                   (256)
76 #define SF_MAX_STRINGS                  (32)
77 #define SF_STR_BUFFER_LEN               (8192)
78 #define SF_HEADER_LEN                   (4100 + SF_STR_BUFFER_LEN)
79
80 #define PSF_SEEK_ERROR                  ((sf_count_t) -1)
81
82
83 #define BITWIDTH2BYTES(x)       (((x) + 7) / 8)
84
85 /*      For some reason sizeof returns an unsigned  value which causes
86 **      a warning when that value is added or subtracted from a signed
87 **      value. Use SIGNED_SIZEOF instead.
88 */
89 #define         SIGNED_SIZEOF(x)        ((int) sizeof (x))
90
91 #define         ARRAY_LEN(x)    ((int) (sizeof (x) / sizeof ((x) [0])))
92
93 #define         NOT(x)                  (! (x))
94
95 #if (COMPILER_IS_GCC == 1)
96 #define         SF_MAX(x,y)             ({ \
97                                                                 typeof (x) sf_max_x1 = (x) ; \
98                                                                 typeof (y) sf_max_y1 = (y) ; \
99                                                                 (void) (&sf_max_x1 == &sf_max_y1) ; \
100                                                                 sf_max_x1 > sf_max_y1 ? sf_max_x1 : sf_max_y1 ; })
101
102 #define         SF_MIN(x,y)             ({ \
103                                                                 typeof (x) sf_min_x2 = (x) ; \
104                                                                 typeof (y) sf_min_y2 = (y) ; \
105                                                                 (void) (&sf_min_x2 == &sf_min_y2) ; \
106                                                                 sf_min_x2 < sf_min_y2 ? sf_min_x2 : sf_min_y2 ; })
107 #else
108 #define         SF_MAX(a,b)             ((a) > (b) ? (a) : (b))
109 #define         SF_MIN(a,b)             ((a) < (b) ? (a) : (b))
110 #endif
111
112
113 #define         SF_MAX_CHANNELS 256
114
115
116 /*
117 *       Macros for spliting the format file of SF_INFI into contrainer type,
118 **      codec type and endian-ness.
119 */
120 #define SF_CONTAINER(x)         ((x) & SF_FORMAT_TYPEMASK)
121 #define SF_CODEC(x)                     ((x) & SF_FORMAT_SUBMASK)
122 #define SF_ENDIAN(x)            ((x) & SF_FORMAT_ENDMASK)
123
124 enum
125 {       /* PEAK chunk location. */
126         SF_PEAK_START           = 42,
127         SF_PEAK_END                     = 43,
128
129         /* PEAK chunk location. */
130         SF_SCALE_MAX            = 52,
131         SF_SCALE_MIN            = 53,
132
133         /* str_flags values. */
134         SF_STR_ALLOW_START      = 0x0100,
135         SF_STR_ALLOW_END        = 0x0200,
136
137         /* Location of strings. */
138         SF_STR_LOCATE_START     = 0x0400,
139         SF_STR_LOCATE_END       = 0x0800,
140
141         SFD_TYPEMASK            = 0x0FFFFFFF
142 } ;
143
144 #define         SFM_MASK        (SFM_READ | SFM_WRITE | SFM_RDWR)
145 #define         SFM_UNMASK      (~SFM_MASK)
146
147 /*---------------------------------------------------------------------------------------
148 ** Formats that may be supported at some time in the future.
149 ** When support is finalised, these values move to src/sndfile.h.
150 */
151
152 enum
153 {       /* Work in progress. */
154         SF_FORMAT_SPEEX                 = 0x5000000,
155         SF_FORMAT_OGGFLAC               = 0x5000001,
156
157         /* Formats supported read only. */
158         SF_FORMAT_TXW                   = 0x4030000,            /* Yamaha TX16 sampler file */
159         SF_FORMAT_DWD                   = 0x4040000,            /* DiamondWare Digirized */
160
161         /* Following are detected but not supported. */
162         SF_FORMAT_REX                   = 0x40A0000,            /* Propellorheads Rex/Rcy */
163         SF_FORMAT_REX2                  = 0x40D0000,            /* Propellorheads Rex2 */
164         SF_FORMAT_KRZ                   = 0x40E0000,            /* Kurzweil sampler file */
165         SF_FORMAT_WMA                   = 0x4100000,            /* Windows Media Audio. */
166         SF_FORMAT_SHN                   = 0x4110000,            /* Shorten. */
167
168         /* Unsupported encodings. */
169         SF_FORMAT_SVX_FIB               = 0x1020,               /* SVX Fibonacci Delta encoding. */
170         SF_FORMAT_SVX_EXP               = 0x1021,               /* SVX Exponential Delta encoding. */
171
172         SF_FORMAT_PCM_N                 = 0x1030
173 } ;
174
175 /*---------------------------------------------------------------------------------------
176 **      PEAK_CHUNK - This chunk type is common to both AIFF and WAVE files although their
177 **      endian encodings are different.
178 */
179
180 typedef struct
181 {       double          value ;         /* signed value of peak */
182         sf_count_t      position ;      /* the sample frame for the peak */
183 } PEAK_POS ;
184
185 typedef struct
186 {       /* libsndfile internal : write a PEAK chunk at the start or end of the file? */
187         int                             peak_loc ;
188
189         /* WAV/AIFF */
190         unsigned int    version ;       /* version of the PEAK chunk */
191         unsigned int    timestamp ;     /* secs since 1/1/1970  */
192
193         /* CAF */
194         unsigned int    edit_number ;
195
196 #if HAVE_FLEXIBLE_ARRAY
197         /* the per channel peak info */
198         PEAK_POS                peaks [] ;
199 #else
200         /*
201         ** This is not ISO compliant C. It works on some compilers which
202         ** don't support the ISO standard flexible struct array which is
203         ** used above. If your compiler doesn't like this I suggest you find
204         ** youself a 1999 ISO C standards compilant compiler. GCC-3.X is
205         ** highly recommended.
206         */
207         PEAK_POS                peaks [0] ;
208 #endif
209 } PEAK_INFO ;
210
211 static inline PEAK_INFO *
212 peak_info_calloc (int channels)
213 {       return calloc (1, sizeof (PEAK_INFO) + channels * sizeof (PEAK_POS)) ;
214 } /* peak_info_calloc */
215
216 typedef struct
217 {       int             type ;
218         int             flags ;
219         char    *str ;
220 } STR_DATA ;
221
222 static inline size_t
223 make_size_t (int x)
224 {       return (size_t) x ;
225 } /* size_t_of_int */
226
227 typedef SF_BROADCAST_INFO_VAR (16 * 1024) SF_BROADCAST_INFO_16K ;
228
229 #if SIZEOF_WCHAR_T == 2
230 typedef wchar_t sfwchar_t ;
231 #else
232 typedef int16_t sfwchar_t ;
233 #endif
234
235 /*
236 **      This version of isprint specifically ignores any locale info. Its used for
237 **      determining which characters can be printed in things like hexdumps.
238 */
239 static inline int
240 psf_isprint (int ch)
241 {       return (ch >= ' ' && ch <= '~') ;
242 } /* psf_isprint */
243
244 /*=======================================================================================
245 **      SF_PRIVATE stuct - a pointer to this struct is passed back to the caller of the
246 **      sf_open_XXXX functions. The caller however has no knowledge of the struct's
247 **      contents.
248 */
249
250 typedef struct
251 {
252         union
253         {       char            c [SF_FILENAME_LEN] ;
254                 sfwchar_t       wc [SF_FILENAME_LEN] ;
255         } path ;
256
257         union
258         {       char            c [SF_FILENAME_LEN] ;
259                 sfwchar_t       wc [SF_FILENAME_LEN] ;
260         } dir ;
261
262         union
263         {       char            c [SF_FILENAME_LEN / 4] ;
264                 sfwchar_t       wc [SF_FILENAME_LEN / 4] ;
265         } name ;
266
267 #if USE_WINDOWS_API
268         /*
269         **      These fields can only be used in src/file_io.c.
270         **      They are basically the same as a windows file HANDLE.
271         */
272         void                    *handle, *hsaved ;
273
274         int                             use_wchar ;
275 #else
276         /* These fields can only be used in src/file_io.c. */
277         int                     filedes, savedes ;
278 #endif
279
280         int                             do_not_close_descriptor ;
281         int                             mode ;                  /* Open mode : SFM_READ, SFM_WRITE or SFM_RDWR. */
282 } PSF_FILE ;
283
284
285 typedef struct sf_private_tag
286 {
287         /* Canary in a coal mine. */
288         union
289         {       /* Place a double here to encourage double alignment. */
290                 double d [2] ;
291                 char c [16] ;
292                 } canary ;
293
294         /* Force the compiler to double align the start of buffer. */
295         union
296         {       double                  dbuf    [SF_BUFFER_LEN / sizeof (double)] ;
297 #if (defined (SIZEOF_INT64_T) && (SIZEOF_INT64_T == 8))
298                 int64_t                 lbuf    [SF_BUFFER_LEN / sizeof (int64_t)] ;
299 #else
300                 long                    lbuf    [SF_BUFFER_LEN / sizeof (double)] ;
301 #endif
302                 float                   fbuf    [SF_BUFFER_LEN / sizeof (float)] ;
303                 int                             ibuf    [SF_BUFFER_LEN / sizeof (int)] ;
304                 short                   sbuf    [SF_BUFFER_LEN / sizeof (short)] ;
305                 char                    cbuf    [SF_BUFFER_LEN / sizeof (char)] ;
306                 signed char             scbuf   [SF_BUFFER_LEN / sizeof (signed char)] ;
307                 unsigned char   ucbuf   [SF_BUFFER_LEN / sizeof (signed char)] ;
308                 } u ;
309
310
311         PSF_FILE                file, rsrc ;
312
313         char                    syserr          [SF_SYSERR_LEN] ;
314
315         /* logbuffer and logindex should only be changed within the logging functions
316         ** of common.c
317         */
318         char                    logbuffer       [SF_BUFFER_LEN] ;
319         unsigned char   header          [SF_HEADER_LEN] ; /* Must be unsigned */
320         int                             rwf_endian ;    /* Header endian-ness flag. */
321
322         /* Storage and housekeeping data for adding/reading strings from
323         ** sound files.
324         */
325         STR_DATA                strings [SF_MAX_STRINGS] ;
326         char                    str_storage [SF_STR_BUFFER_LEN] ;
327         char                    *str_end ;
328         int                             str_flags ;
329
330         /* Guard value. If this changes the buffers above have overflowed. */
331         int                             Magick ;
332
333         unsigned                unique_id ;
334
335         /* Index variables for maintaining logbuffer and header above. */
336         int                             logindex ;
337         int                             headindex, headend ;
338         int                             has_text ;
339
340         int                             error ;
341
342         int                             endian ;                /* File endianness : SF_ENDIAN_LITTLE or SF_ENDIAN_BIG. */
343         int                             data_endswap ;  /* Need to endswap data? */
344
345         /*
346         ** Maximum float value for calculating the multiplier for
347         ** float/double to short/int conversions.
348         */
349         int                             float_int_mult ;
350         float                   float_max ;
351
352         int                             scale_int_float ;
353
354         /* Vairables for handling pipes. */
355         int                             is_pipe ;               /* True if file is a pipe. */
356         sf_count_t              pipeoffset ;    /* Number of bytes read from a pipe. */
357
358         /* True if clipping must be performed on float->int conversions. */
359         int                             add_clipping ;
360
361         SF_INFO                 sf ;
362
363         int                             have_written ;  /* Has a single write been done to the file? */
364         PEAK_INFO               *peak_info ;
365
366         /* Loop Info */
367         SF_LOOP_INFO    *loop_info ;
368         SF_INSTRUMENT   *instrument ;
369
370         /* Broadcast (EBU) Info */
371         SF_BROADCAST_INFO_16K *broadcast_16k ;
372
373         /* Channel map data (if present) : an array of ints. */
374         int                             *channel_map ;
375
376         sf_count_t              filelength ;    /* Overall length of (embedded) file. */
377         sf_count_t              fileoffset ;    /* Offset in number of bytes from beginning of file. */
378
379         sf_count_t              rsrclength ;    /* Length of the resource fork (if it exists). */
380
381         sf_count_t              dataoffset ;    /* Offset in number of bytes from beginning of file. */
382         sf_count_t              datalength ;    /* Length in bytes of the audio data. */
383         sf_count_t              dataend ;               /* Offset to file tailer. */
384
385         int                             blockwidth ;    /* Size in bytes of one set of interleaved samples. */
386         int                             bytewidth ;             /* Size in bytes of one sample (one channel). */
387
388         void                    *dither ;
389         void                    *interleave ;
390
391         int                             last_op ;               /* Last operation; either SFM_READ or SFM_WRITE */
392         sf_count_t              read_current ;
393         sf_count_t              write_current ;
394
395         void                    *container_data ;       /*      This is a pointer to dynamically allocated file
396                                                                                 **      container format specific data.
397                                                                                 */
398
399         void                    *codec_data ;           /*      This is a pointer to dynamically allocated file
400                                                                                 **      codec format specific data.
401                                                                                 */
402
403         SF_DITHER_INFO  write_dither ;
404         SF_DITHER_INFO  read_dither ;
405
406         int                             norm_double ;
407         int                             norm_float ;
408
409         int                             auto_header ;
410
411         int                             ieee_replace ;
412
413         /* A set of file specific function pointers */
414         sf_count_t              (*read_short)   (struct sf_private_tag*, short *ptr, sf_count_t len) ;
415         sf_count_t              (*read_int)             (struct sf_private_tag*, int *ptr, sf_count_t len) ;
416         sf_count_t              (*read_float)   (struct sf_private_tag*, float *ptr, sf_count_t len) ;
417         sf_count_t              (*read_double)  (struct sf_private_tag*, double *ptr, sf_count_t len) ;
418
419         sf_count_t              (*write_short)  (struct sf_private_tag*, const short *ptr, sf_count_t len) ;
420         sf_count_t              (*write_int)    (struct sf_private_tag*, const int *ptr, sf_count_t len) ;
421         sf_count_t              (*write_float)  (struct sf_private_tag*, const float *ptr, sf_count_t len) ;
422         sf_count_t              (*write_double) (struct sf_private_tag*, const double *ptr, sf_count_t len) ;
423
424         sf_count_t              (*seek)                 (struct sf_private_tag*, int mode, sf_count_t samples_from_start) ;
425         int                             (*write_header) (struct sf_private_tag*, int calc_length) ;
426         int                             (*command)              (struct sf_private_tag*, int command, void *data, int datasize) ;
427
428         /*
429         **      Separate close functions for the codec and the container.
430         **      The codec close function is always called first.
431         */
432         int                             (*codec_close)          (struct sf_private_tag*) ;
433         int                             (*container_close)      (struct sf_private_tag*) ;
434
435         char                    *format_desc ;
436
437         /* Virtual I/O functions. */
438         int                                     virtual_io ;
439         SF_VIRTUAL_IO           vio ;
440         void                            *vio_user_data ;
441 } SF_PRIVATE ;
442
443
444
445 enum
446 {       SFE_NO_ERROR                            = SF_ERR_NO_ERROR,
447         SFE_BAD_OPEN_FORMAT                     = SF_ERR_UNRECOGNISED_FORMAT,
448         SFE_SYSTEM                                      = SF_ERR_SYSTEM,
449         SFE_MALFORMED_FILE                      = SF_ERR_MALFORMED_FILE,
450         SFE_UNSUPPORTED_ENCODING        = SF_ERR_UNSUPPORTED_ENCODING,
451
452         SFE_ZERO_MAJOR_FORMAT,
453         SFE_ZERO_MINOR_FORMAT,
454         SFE_BAD_FILE,
455         SFE_BAD_FILE_READ,
456         SFE_OPEN_FAILED,
457         SFE_BAD_SNDFILE_PTR,
458         SFE_BAD_SF_INFO_PTR,
459         SFE_BAD_SF_INCOMPLETE,
460         SFE_BAD_FILE_PTR,
461         SFE_BAD_INT_PTR,
462         SFE_BAD_STAT_SIZE,
463         SFE_MALLOC_FAILED,
464         SFE_UNIMPLEMENTED,
465         SFE_BAD_READ_ALIGN,
466         SFE_BAD_WRITE_ALIGN,
467         SFE_UNKNOWN_FORMAT,
468         SFE_NOT_READMODE,
469         SFE_NOT_WRITEMODE,
470         SFE_BAD_MODE_RW,
471         SFE_BAD_SF_INFO,
472         SFE_BAD_OFFSET,
473         SFE_NO_EMBED_SUPPORT,
474         SFE_NO_EMBEDDED_RDWR,
475         SFE_NO_PIPE_WRITE,
476
477         SFE_INTERNAL,
478         SFE_BAD_COMMAND_PARAM,
479         SFE_BAD_ENDIAN,
480         SFE_CHANNEL_COUNT_ZERO,
481         SFE_CHANNEL_COUNT,
482
483         SFE_BAD_VIRTUAL_IO,
484
485         SFE_INTERLEAVE_MODE,
486         SFE_INTERLEAVE_SEEK,
487         SFE_INTERLEAVE_READ,
488
489         SFE_BAD_SEEK,
490         SFE_NOT_SEEKABLE,
491         SFE_AMBIGUOUS_SEEK,
492         SFE_WRONG_SEEK,
493         SFE_SEEK_FAILED,
494
495         SFE_BAD_OPEN_MODE,
496         SFE_OPEN_PIPE_RDWR,
497         SFE_RDWR_POSITION,
498         SFE_RDWR_BAD_HEADER,
499         SFE_CMD_HAS_DATA,
500         SFE_BAD_BROADCAST_INFO_SIZE,
501         SFE_BAD_BROADCAST_INFO_TOO_BIG,
502
503         SFE_STR_NO_SUPPORT,
504         SFE_STR_NOT_WRITE,
505         SFE_STR_MAX_DATA,
506         SFE_STR_MAX_COUNT,
507         SFE_STR_BAD_TYPE,
508         SFE_STR_NO_ADD_END,
509         SFE_STR_BAD_STRING,
510         SFE_STR_WEIRD,
511
512         SFE_WAV_NO_RIFF,
513         SFE_WAV_NO_WAVE,
514         SFE_WAV_NO_FMT,
515         SFE_WAV_BAD_FMT,
516         SFE_WAV_FMT_SHORT,
517         SFE_WAV_BAD_FACT,
518         SFE_WAV_BAD_PEAK,
519         SFE_WAV_PEAK_B4_FMT,
520         SFE_WAV_BAD_FORMAT,
521         SFE_WAV_BAD_BLOCKALIGN,
522         SFE_WAV_NO_DATA,
523         SFE_WAV_BAD_LIST,
524         SFE_WAV_ADPCM_NOT4BIT,
525         SFE_WAV_ADPCM_CHANNELS,
526         SFE_WAV_GSM610_FORMAT,
527         SFE_WAV_UNKNOWN_CHUNK,
528         SFE_WAV_WVPK_DATA,
529
530         SFE_AIFF_NO_FORM,
531         SFE_AIFF_AIFF_NO_FORM,
532         SFE_AIFF_COMM_NO_FORM,
533         SFE_AIFF_SSND_NO_COMM,
534         SFE_AIFF_UNKNOWN_CHUNK,
535         SFE_AIFF_COMM_CHUNK_SIZE,
536         SFE_AIFF_BAD_COMM_CHUNK,
537         SFE_AIFF_PEAK_B4_COMM,
538         SFE_AIFF_BAD_PEAK,
539         SFE_AIFF_NO_SSND,
540         SFE_AIFF_NO_DATA,
541         SFE_AIFF_RW_SSND_NOT_LAST,
542
543         SFE_AU_UNKNOWN_FORMAT,
544         SFE_AU_NO_DOTSND,
545         SFE_AU_EMBED_BAD_LEN,
546
547         SFE_RAW_READ_BAD_SPEC,
548         SFE_RAW_BAD_BITWIDTH,
549         SFE_RAW_BAD_FORMAT,
550
551         SFE_PAF_NO_MARKER,
552         SFE_PAF_VERSION,
553         SFE_PAF_UNKNOWN_FORMAT,
554         SFE_PAF_SHORT_HEADER,
555         SFE_PAF_BAD_CHANNELS,
556
557         SFE_SVX_NO_FORM,
558         SFE_SVX_NO_BODY,
559         SFE_SVX_NO_DATA,
560         SFE_SVX_BAD_COMP,
561         SFE_SVX_BAD_NAME_LENGTH,
562
563         SFE_NIST_BAD_HEADER,
564         SFE_NIST_CRLF_CONVERISON,
565         SFE_NIST_BAD_ENCODING,
566
567         SFE_VOC_NO_CREATIVE,
568         SFE_VOC_BAD_FORMAT,
569         SFE_VOC_BAD_VERSION,
570         SFE_VOC_BAD_MARKER,
571         SFE_VOC_BAD_SECTIONS,
572         SFE_VOC_MULTI_SAMPLERATE,
573         SFE_VOC_MULTI_SECTION,
574         SFE_VOC_MULTI_PARAM,
575         SFE_VOC_SECTION_COUNT,
576         SFE_VOC_NO_PIPE,
577
578         SFE_IRCAM_NO_MARKER,
579         SFE_IRCAM_BAD_CHANNELS,
580         SFE_IRCAM_UNKNOWN_FORMAT,
581
582         SFE_W64_64_BIT,
583         SFE_W64_NO_RIFF,
584         SFE_W64_NO_WAVE,
585         SFE_W64_NO_DATA,
586         SFE_W64_ADPCM_NOT4BIT,
587         SFE_W64_ADPCM_CHANNELS,
588         SFE_W64_GSM610_FORMAT,
589
590         SFE_MAT4_BAD_NAME,
591         SFE_MAT4_NO_SAMPLERATE,
592
593         SFE_MAT5_BAD_ENDIAN,
594         SFE_MAT5_NO_BLOCK,
595         SFE_MAT5_SAMPLE_RATE,
596
597         SFE_PVF_NO_PVF1,
598         SFE_PVF_BAD_HEADER,
599         SFE_PVF_BAD_BITWIDTH,
600
601         SFE_DWVW_BAD_BITWIDTH,
602         SFE_G72X_NOT_MONO,
603
604         SFE_XI_BAD_HEADER,
605         SFE_XI_EXCESS_SAMPLES,
606         SFE_XI_NO_PIPE,
607
608         SFE_HTK_NO_PIPE,
609
610         SFE_SDS_NOT_SDS,
611         SFE_SDS_BAD_BIT_WIDTH,
612
613         SFE_SD2_FD_DISALLOWED,
614         SFE_SD2_BAD_DATA_OFFSET,
615         SFE_SD2_BAD_MAP_OFFSET,
616         SFE_SD2_BAD_DATA_LENGTH,
617         SFE_SD2_BAD_MAP_LENGTH,
618         SFE_SD2_BAD_RSRC,
619         SFE_SD2_BAD_SAMPLE_SIZE,
620
621         SFE_FLAC_BAD_HEADER,
622         SFE_FLAC_NEW_DECODER,
623         SFE_FLAC_INIT_DECODER,
624         SFE_FLAC_LOST_SYNC,
625         SFE_FLAC_BAD_SAMPLE_RATE,
626         SFE_FLAC_UNKOWN_ERROR,
627
628         SFE_WVE_NOT_WVE,
629         SFE_WVE_NO_PIPE,
630
631         SFE_VORBIS_ENCODER_BUG,
632
633         SFE_RF64_NOT_RF64,
634
635         SFE_MAX_ERROR                   /* This must be last in list. */
636 } ;
637
638 int subformat_to_bytewidth (int format) ;
639 int s_bitwidth_to_subformat (int bits) ;
640 int u_bitwidth_to_subformat (int bits) ;
641
642 /*  Functions for reading and writing floats and doubles on processors
643 **      with non-IEEE floats/doubles.
644 */
645 float   float32_be_read         (unsigned char *cptr) ;
646 float   float32_le_read         (unsigned char *cptr) ;
647 void    float32_be_write        (float in, unsigned char *out) ;
648 void    float32_le_write        (float in, unsigned char *out) ;
649
650 double  double64_be_read        (unsigned char *cptr) ;
651 double  double64_le_read        (unsigned char *cptr) ;
652 void    double64_be_write       (double in, unsigned char *out) ;
653 void    double64_le_write       (double in, unsigned char *out) ;
654
655 /* Functions for writing to the internal logging buffer. */
656
657 void    psf_log_printf          (SF_PRIVATE *psf, const char *format, ...) ;
658 void    psf_log_SF_INFO         (SF_PRIVATE *psf) ;
659
660 int32_t psf_rand_int32 (void) ;
661
662 void append_snprintf (char * dest, size_t maxlen, const char * fmt, ...) ;
663 void psf_strlcpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
664
665 /* Functions used when writing file headers. */
666
667 int             psf_binheader_writef    (SF_PRIVATE *psf, const char *format, ...) ;
668 void    psf_asciiheader_printf  (SF_PRIVATE *psf, const char *format, ...) ;
669
670 /* Functions used when reading file headers. */
671
672 int             psf_binheader_readf     (SF_PRIVATE *psf, char const *format, ...) ;
673
674 /* Functions used in the write function for updating the peak chunk. */
675
676 void    peak_update_short       (SF_PRIVATE *psf, short *ptr, size_t items) ;
677 void    peak_update_int         (SF_PRIVATE *psf, int *ptr, size_t items) ;
678 void    peak_update_double      (SF_PRIVATE *psf, double *ptr, size_t items) ;
679
680 /* Functions defined in command.c. */
681
682 int             psf_get_format_simple_count     (void) ;
683 int             psf_get_format_simple           (SF_FORMAT_INFO *data) ;
684
685 int             psf_get_format_info                     (SF_FORMAT_INFO *data) ;
686
687 int             psf_get_format_major_count      (void) ;
688 int             psf_get_format_major            (SF_FORMAT_INFO *data) ;
689
690 int             psf_get_format_subtype_count    (void) ;
691 int             psf_get_format_subtype          (SF_FORMAT_INFO *data) ;
692
693 void    psf_generate_format_desc (SF_PRIVATE *psf) ;
694
695 double  psf_calc_signal_max                     (SF_PRIVATE *psf, int normalize) ;
696 int             psf_calc_max_all_channels       (SF_PRIVATE *psf, double *peaks, int normalize) ;
697
698 int             psf_get_signal_max                      (SF_PRIVATE *psf, double *peak) ;
699 int             psf_get_max_all_channels        (SF_PRIVATE *psf, double *peaks) ;
700
701 /* Functions in strings.c. */
702
703 const char* psf_get_string (SF_PRIVATE *psf, int str_type) ;
704 int psf_set_string (SF_PRIVATE *psf, int str_type, const char *str) ;
705 int psf_store_string (SF_PRIVATE *psf, int str_type, const char *str) ;
706 int psf_location_string_count (const SF_PRIVATE * psf, int location) ;
707
708 /* Default seek function. Use for PCM and float encoded data. */
709 sf_count_t      psf_default_seek (SF_PRIVATE *psf, int mode, sf_count_t samples_from_start) ;
710
711 int macos_guess_file_type (SF_PRIVATE *psf, const char *filename) ;
712
713 /*------------------------------------------------------------------------------------
714 **      File I/O functions which will allow access to large files (> 2 Gig) on
715 **      some 32 bit OSes. Implementation in file_io.c.
716 */
717
718 int psf_fopen (SF_PRIVATE *psf) ;
719 int psf_set_stdio (SF_PRIVATE *psf) ;
720 int psf_file_valid (SF_PRIVATE *psf) ;
721 void psf_set_file (SF_PRIVATE *psf, int fd) ;
722 void psf_init_files (SF_PRIVATE *psf) ;
723 void psf_use_rsrc (SF_PRIVATE *psf, int on_off) ;
724
725 SNDFILE * psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo) ;
726
727 sf_count_t psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence) ;
728 sf_count_t psf_fread (void *ptr, sf_count_t bytes, sf_count_t count, SF_PRIVATE *psf) ;
729 sf_count_t psf_fwrite (const void *ptr, sf_count_t bytes, sf_count_t count, SF_PRIVATE *psf) ;
730 sf_count_t psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf) ;
731 sf_count_t psf_ftell (SF_PRIVATE *psf) ;
732 sf_count_t psf_get_filelen (SF_PRIVATE *psf) ;
733
734 void psf_fsync (SF_PRIVATE *psf) ;
735
736 int psf_is_pipe (SF_PRIVATE *psf) ;
737
738 int psf_ftruncate (SF_PRIVATE *psf, sf_count_t len) ;
739 int psf_fclose (SF_PRIVATE *psf) ;
740
741 /* Open and close the resource fork of a file. */
742 int psf_open_rsrc (SF_PRIVATE *psf) ;
743 int psf_close_rsrc (SF_PRIVATE *psf) ;
744
745 /*
746 void psf_fclearerr (SF_PRIVATE *psf) ;
747 int psf_ferror (SF_PRIVATE *psf) ;
748 */
749
750 /*------------------------------------------------------------------------------------
751 ** Functions for reading and writing different file formats.
752 */
753
754 int             aiff_open       (SF_PRIVATE *psf) ;
755 int             au_open         (SF_PRIVATE *psf) ;
756 int             avr_open        (SF_PRIVATE *psf) ;
757 int             htk_open        (SF_PRIVATE *psf) ;
758 int             ircam_open      (SF_PRIVATE *psf) ;
759 int             mat4_open       (SF_PRIVATE *psf) ;
760 int             mat5_open       (SF_PRIVATE *psf) ;
761 int             nist_open       (SF_PRIVATE *psf) ;
762 int             paf_open        (SF_PRIVATE *psf) ;
763 int             pvf_open        (SF_PRIVATE *psf) ;
764 int             raw_open        (SF_PRIVATE *psf) ;
765 int             sd2_open        (SF_PRIVATE *psf) ;
766 int             sds_open        (SF_PRIVATE *psf) ;
767 int             svx_open        (SF_PRIVATE *psf) ;
768 int             voc_open        (SF_PRIVATE *psf) ;
769 int             w64_open        (SF_PRIVATE *psf) ;
770 int             wav_open        (SF_PRIVATE *psf) ;
771 int             xi_open         (SF_PRIVATE *psf) ;
772 int             flac_open       (SF_PRIVATE *psf) ;
773 int             caf_open        (SF_PRIVATE *psf) ;
774 int             mpc2k_open      (SF_PRIVATE *psf) ;
775 int             rf64_open       (SF_PRIVATE *psf) ;
776
777 /* In progress. Do not currently work. */
778
779 int             ogg_vorbis_open (SF_PRIVATE *psf) ;
780 int             ogg_speex_open  (SF_PRIVATE *psf) ;
781 int             ogg_pcm_open    (SF_PRIVATE *psf) ;
782
783
784 int             mpeg_open       (SF_PRIVATE *psf) ;
785 int             ogg_open        (SF_PRIVATE *psf) ;
786 int             rx2_open        (SF_PRIVATE *psf) ;
787 int             txw_open        (SF_PRIVATE *psf) ;
788 int             wve_open        (SF_PRIVATE *psf) ;
789 int             dwd_open        (SF_PRIVATE *psf) ;
790
791 int             macbinary3_open (SF_PRIVATE *psf) ;
792
793 /*------------------------------------------------------------------------------------
794 **      Init functions for a number of common data encodings.
795 */
796
797 int             pcm_init                (SF_PRIVATE *psf) ;
798 int             ulaw_init               (SF_PRIVATE *psf) ;
799 int             alaw_init               (SF_PRIVATE *psf) ;
800 int             float32_init    (SF_PRIVATE *psf) ;
801 int             double64_init   (SF_PRIVATE *psf) ;
802 int             dwvw_init               (SF_PRIVATE *psf, int bitwidth) ;
803 int             gsm610_init             (SF_PRIVATE *psf) ;
804 int             vox_adpcm_init  (SF_PRIVATE *psf) ;
805 int             flac_init               (SF_PRIVATE *psf) ;
806 int             g72x_init               (SF_PRIVATE * psf) ;
807
808 int     dither_init             (SF_PRIVATE *psf, int mode) ;
809
810 int             wav_w64_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
811 int             wav_w64_msadpcm_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
812
813 int             aiff_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
814
815 int             interleave_init (SF_PRIVATE *psf) ;
816
817 /*------------------------------------------------------------------------------------
818 ** Chunk logging functions.
819 */
820
821 typedef struct
822 {       struct
823         {       int chunk ;
824                 sf_count_t offset ;
825                 sf_count_t len ;
826         } l [100] ;
827
828         int count ;
829 } PRIV_CHUNK4 ;
830
831 void pchk4_store (PRIV_CHUNK4 * pchk, int marker, sf_count_t offset, sf_count_t len) ;
832 int pchk4_find (PRIV_CHUNK4 * pchk, int marker) ;
833
834 /*------------------------------------------------------------------------------------
835 ** Functions that work like OpenBSD's strlcpy/strlcat to replace strncpy/strncat.
836 **
837 ** See : http://www.gratisoft.us/todd/papers/strlcpy.html
838 **
839 ** These functions are available on *BSD, but are not avaialble everywhere so we
840 ** implement them here.
841 **
842 ** The argument order has been changed to that of strncpy/strncat to cause
843 ** compiler errors if code is carelessly converted from one to the other.
844 */
845
846 static inline void
847 psf_strlcat (char *dest, size_t n, const char *src)
848 {       strncat (dest, src, n - strlen (dest) - 1) ;
849         dest [n - 1] = 0 ;
850 } /* psf_strlcat */
851
852 static inline void
853 psf_strlcpy (char *dest, size_t n, const char *src)
854 {       strncpy (dest, src, n - 1) ;
855         dest [n - 1] = 0 ;
856 } /* psf_strlcpy */
857
858 /*------------------------------------------------------------------------------------
859 ** Other helper functions.
860 */
861
862 void    *psf_memset (void *s, int c, sf_count_t n) ;
863
864 SF_INSTRUMENT * psf_instrument_alloc (void) ;
865
866 void    psf_sanitize_string (char * cptr, int len) ;
867
868 /* Generate the current date as a string. */
869 void    psf_get_date_str (char *str, int maxlen) ;
870
871 SF_BROADCAST_INFO_16K * broadcast_var_alloc (void) ;
872 int             broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * data, size_t datasize) ;
873 int             broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize) ;
874
875
876 typedef struct
877 {       int channels ;
878         int endianness ;
879 } AUDIO_DETECT ;
880
881 int audio_detect (SF_PRIVATE * psf, AUDIO_DETECT *ad, const unsigned char * data, int datalen) ;
882 int id3_skip (SF_PRIVATE * psf) ;
883
884 /*------------------------------------------------------------------------------------
885 ** Helper/debug functions.
886 */
887
888 void    psf_hexdump (const void *ptr, int len) ;
889
890 const char * str_of_major_format (int format) ;
891 const char * str_of_minor_format (int format) ;
892 const char * str_of_open_mode (int mode) ;
893 const char * str_of_endianness (int end) ;
894
895 /*------------------------------------------------------------------------------------
896 ** Extra commands for sf_command(). Not for public use yet.
897 */
898
899 enum
900 {       SFC_TEST_AIFF_ADD_INST_CHUNK    = 0x2000,
901         SFC_TEST_WAV_ADD_INFO_CHUNK             = 0x2010
902 } ;
903
904 /*
905 ** Maybe, one day, make these functions or something like them, public.
906 **
907 ** Buffer to buffer dithering. Pointer in and out are allowed to point
908 ** to the same buffer for in-place dithering.
909 */
910
911 #if 0
912 int sf_dither_short             (const SF_DITHER_INFO *dither, const short *in, short *out, int count) ;
913 int sf_dither_int               (const SF_DITHER_INFO *dither, const int *in, int *out, int count) ;
914 int sf_dither_float             (const SF_DITHER_INFO *dither, const float *in, float *out, int count) ;
915 int sf_dither_double    (const SF_DITHER_INFO *dither, const double *in, double *out, int count) ;
916 #endif
917
918 #endif /* SNDFILE_COMMON_H */
919