1 /*
2 ** Copyright (C) 1999-2017 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <ctype.h>
25
26 #include "sndfile.h"
27 #include "sfendian.h"
28 #include "common.h"
29
30 /*------------------------------------------------------------------------------
31 ** Macros to handle big/little endian issues.
32 */
33
34 #define DOTSND_MARKER (MAKE_MARKER ('.', 's', 'n', 'd'))
35 #define DNSDOT_MARKER (MAKE_MARKER ('d', 'n', 's', '.'))
36
37 #define AU_DATA_OFFSET 24
38
39 /*------------------------------------------------------------------------------
40 ** Known AU file encoding types.
41 */
42
43 enum
44 { AU_ENCODING_ULAW_8 = 1, /* 8-bit u-law samples */
45 AU_ENCODING_PCM_8 = 2, /* 8-bit linear samples */
46 AU_ENCODING_PCM_16 = 3, /* 16-bit linear samples */
47 AU_ENCODING_PCM_24 = 4, /* 24-bit linear samples */
48 AU_ENCODING_PCM_32 = 5, /* 32-bit linear samples */
49
50 AU_ENCODING_FLOAT = 6, /* floating-point samples */
51 AU_ENCODING_DOUBLE = 7, /* double-precision float samples */
52 AU_ENCODING_INDIRECT = 8, /* fragmented sampled data */
53 AU_ENCODING_NESTED = 9, /* ? */
54 AU_ENCODING_DSP_CORE = 10, /* DSP program */
55 AU_ENCODING_DSP_DATA_8 = 11, /* 8-bit fixed-point samples */
56 AU_ENCODING_DSP_DATA_16 = 12, /* 16-bit fixed-point samples */
57 AU_ENCODING_DSP_DATA_24 = 13, /* 24-bit fixed-point samples */
58 AU_ENCODING_DSP_DATA_32 = 14, /* 32-bit fixed-point samples */
59
60 AU_ENCODING_DISPLAY = 16, /* non-audio display data */
61 AU_ENCODING_MULAW_SQUELCH = 17, /* ? */
62 AU_ENCODING_EMPHASIZED = 18, /* 16-bit linear with emphasis */
63 AU_ENCODING_NEXT = 19, /* 16-bit linear with compression (NEXT) */
64 AU_ENCODING_COMPRESSED_EMPHASIZED = 20, /* A combination of the two above */
65 AU_ENCODING_DSP_COMMANDS = 21, /* Music Kit DSP commands */
66 AU_ENCODING_DSP_COMMANDS_SAMPLES = 22, /* ? */
67
68 AU_ENCODING_ADPCM_G721_32 = 23, /* G721 32 kbs ADPCM - 4 bits per sample. */
69 AU_ENCODING_ADPCM_G722 = 24, /* G722 64 kbs ADPCM */
70 AU_ENCODING_ADPCM_G723_24 = 25, /* G723 24 kbs ADPCM - 3 bits per sample. */
71 AU_ENCODING_ADPCM_G723_40 = 26, /* G723 40 kbs ADPCM - 5 bits per sample. */
72
73 AU_ENCODING_ALAW_8 = 27
74 } ;
75
76 /*------------------------------------------------------------------------------
77 ** Typedefs.
78 */
79
80 typedef struct
81 { int dataoffset ;
82 int datasize ;
83 int encoding ;
84 int samplerate ;
85 int channels ;
86 } AU_FMT ;
87
88
89 /*------------------------------------------------------------------------------
90 ** Private static functions.
91 */
92
93 static int au_close (SF_PRIVATE *psf) ;
94
95 static int au_format_to_encoding (int format) ;
96
97 static int au_write_header (SF_PRIVATE *psf, int calc_length) ;
98 static int au_read_header (SF_PRIVATE *psf) ;
99
100 /*------------------------------------------------------------------------------
101 ** Public function.
102 */
103
104 int
au_open(SF_PRIVATE * psf)105 au_open (SF_PRIVATE *psf)
106 { int subformat ;
107 int error = 0 ;
108
109 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
110 { if ((error = au_read_header (psf)))
111 return error ;
112 } ;
113
114 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_AU)
115 return SFE_BAD_OPEN_FORMAT ;
116
117 subformat = SF_CODEC (psf->sf.format) ;
118
119 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
120 { psf->endian = SF_ENDIAN (psf->sf.format) ;
121 if (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU)
122 psf->endian = SF_ENDIAN_LITTLE ;
123 else if (psf->endian != SF_ENDIAN_LITTLE)
124 psf->endian = SF_ENDIAN_BIG ;
125
126 if (au_write_header (psf, SF_FALSE))
127 return psf->error ;
128
129 psf->write_header = au_write_header ;
130 } ;
131
132 psf->container_close = au_close ;
133
134 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
135
136 switch (subformat)
137 { case SF_FORMAT_ULAW : /* 8-bit Ulaw encoding. */
138 ulaw_init (psf) ;
139 break ;
140
141 case SF_FORMAT_PCM_S8 : /* 8-bit linear PCM. */
142 error = pcm_init (psf) ;
143 break ;
144
145 case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
146 case SF_FORMAT_PCM_24 : /* 24-bit linear PCM */
147 case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
148 error = pcm_init (psf) ;
149 break ;
150
151 case SF_FORMAT_ALAW : /* 8-bit Alaw encoding. */
152 alaw_init (psf) ;
153 break ;
154
155 /* Lite remove start */
156 case SF_FORMAT_FLOAT : /* 32-bit floats. */
157 error = float32_init (psf) ;
158 break ;
159
160 case SF_FORMAT_DOUBLE : /* 64-bit double precision floats. */
161 error = double64_init (psf) ;
162 break ;
163
164 case SF_FORMAT_G721_32 :
165 error = g72x_init (psf) ;
166 psf->sf.seekable = SF_FALSE ;
167 break ;
168
169 case SF_FORMAT_G723_24 :
170 error = g72x_init (psf) ;
171 psf->sf.seekable = SF_FALSE ;
172 break ;
173
174 case SF_FORMAT_G723_40 :
175 error = g72x_init (psf) ;
176 psf->sf.seekable = SF_FALSE ;
177 break ;
178 /* Lite remove end */
179
180 default : break ;
181 } ;
182
183 return error ;
184 } /* au_open */
185
186 /*------------------------------------------------------------------------------
187 */
188
189 static int
au_close(SF_PRIVATE * psf)190 au_close (SF_PRIVATE *psf)
191 {
192 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
193 au_write_header (psf, SF_TRUE) ;
194
195 return 0 ;
196 } /* au_close */
197
198 static int
au_write_header(SF_PRIVATE * psf,int calc_length)199 au_write_header (SF_PRIVATE *psf, int calc_length)
200 { sf_count_t current ;
201 int encoding, datalength ;
202
203 if (psf->pipeoffset > 0)
204 return 0 ;
205
206 current = psf_ftell (psf) ;
207
208 if (calc_length)
209 { psf->filelength = psf_get_filelen (psf) ;
210
211 psf->datalength = psf->filelength - psf->dataoffset ;
212 if (psf->dataend)
213 psf->datalength -= psf->filelength - psf->dataend ;
214 } ;
215
216 encoding = au_format_to_encoding (SF_CODEC (psf->sf.format)) ;
217 if (! encoding)
218 return (psf->error = SFE_BAD_OPEN_FORMAT) ;
219
220 /* Reset the current header length to zero. */
221 psf->header.ptr [0] = 0 ;
222 psf->header.indx = 0 ;
223
224 /*
225 ** Only attempt to seek if we are not writng to a pipe. If we are
226 ** writing to a pipe we shouldn't be here anyway.
227 */
228 if (psf->is_pipe == SF_FALSE)
229 psf_fseek (psf, 0, SEEK_SET) ;
230
231 /*
232 ** AU format files allow a datalength value of -1 if the datalength
233 ** is not know at the time the header is written.
234 ** Also use this value of -1 if the datalength > 2 gigabytes.
235 */
236 if (psf->datalength < 0 || psf->datalength > 0x7FFFFFFF)
237 datalength = -1 ;
238 else
239 datalength = (int) (psf->datalength & 0x7FFFFFFF) ;
240
241 if (psf->endian == SF_ENDIAN_BIG)
242 { psf_binheader_writef (psf, "Em4", BHWm (DOTSND_MARKER), BHW4 (AU_DATA_OFFSET)) ;
243 psf_binheader_writef (psf, "E4444", BHW4 (datalength), BHW4 (encoding), BHW4 (psf->sf.samplerate), BHW4 (psf->sf.channels)) ;
244 }
245 else if (psf->endian == SF_ENDIAN_LITTLE)
246 { psf_binheader_writef (psf, "em4", BHWm (DNSDOT_MARKER), BHW4 (AU_DATA_OFFSET)) ;
247 psf_binheader_writef (psf, "e4444", BHW4 (datalength), BHW4 (encoding), BHW4 (psf->sf.samplerate), BHW4 (psf->sf.channels)) ;
248 }
249 else
250 return (psf->error = SFE_BAD_OPEN_FORMAT) ;
251
252 /* Header construction complete so write it out. */
253 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
254
255 if (psf->error)
256 return psf->error ;
257
258 psf->dataoffset = psf->header.indx ;
259
260 if (current > 0)
261 psf_fseek (psf, current, SEEK_SET) ;
262
263 return psf->error ;
264 } /* au_write_header */
265
266 static int
au_format_to_encoding(int format)267 au_format_to_encoding (int format)
268 {
269 switch (format)
270 { case SF_FORMAT_PCM_S8 : return AU_ENCODING_PCM_8 ;
271 case SF_FORMAT_PCM_16 : return AU_ENCODING_PCM_16 ;
272 case SF_FORMAT_PCM_24 : return AU_ENCODING_PCM_24 ;
273 case SF_FORMAT_PCM_32 : return AU_ENCODING_PCM_32 ;
274
275 case SF_FORMAT_FLOAT : return AU_ENCODING_FLOAT ;
276 case SF_FORMAT_DOUBLE : return AU_ENCODING_DOUBLE ;
277
278 case SF_FORMAT_ULAW : return AU_ENCODING_ULAW_8 ;
279 case SF_FORMAT_ALAW : return AU_ENCODING_ALAW_8 ;
280
281 case SF_FORMAT_G721_32 : return AU_ENCODING_ADPCM_G721_32 ;
282 case SF_FORMAT_G723_24 : return AU_ENCODING_ADPCM_G723_24 ;
283 case SF_FORMAT_G723_40 : return AU_ENCODING_ADPCM_G723_40 ;
284
285 default : break ;
286 } ;
287 return 0 ;
288 } /* au_format_to_encoding */
289
290 static int
au_read_header(SF_PRIVATE * psf)291 au_read_header (SF_PRIVATE *psf)
292 { AU_FMT au_fmt ;
293 int marker, dword ;
294
295 memset (&au_fmt, 0, sizeof (au_fmt)) ;
296 psf_binheader_readf (psf, "pm", 0, &marker) ;
297 psf_log_printf (psf, "%M\n", marker) ;
298
299 if (marker == DOTSND_MARKER)
300 { psf->endian = SF_ENDIAN_BIG ;
301
302 psf_binheader_readf (psf, "E44444", &(au_fmt.dataoffset), &(au_fmt.datasize),
303 &(au_fmt.encoding), &(au_fmt.samplerate), &(au_fmt.channels)) ;
304 }
305 else if (marker == DNSDOT_MARKER)
306 { psf->endian = SF_ENDIAN_LITTLE ;
307 psf_binheader_readf (psf, "e44444", &(au_fmt.dataoffset), &(au_fmt.datasize),
308 &(au_fmt.encoding), &(au_fmt.samplerate), &(au_fmt.channels)) ;
309 }
310 else
311 return SFE_AU_NO_DOTSND ;
312
313 psf_log_printf (psf, " Data Offset : %d\n", au_fmt.dataoffset) ;
314
315 if (psf->fileoffset > 0 && au_fmt.datasize == -1)
316 { psf_log_printf (psf, " Data Size : -1\n") ;
317 return SFE_AU_EMBED_BAD_LEN ;
318 } ;
319
320 if (psf->fileoffset > 0)
321 { psf->filelength = au_fmt.dataoffset + au_fmt.datasize ;
322 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
323 }
324 else if (au_fmt.datasize == -1 || au_fmt.dataoffset + au_fmt.datasize == psf->filelength)
325 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
326 else if (au_fmt.dataoffset + au_fmt.datasize < psf->filelength)
327 { psf->filelength = au_fmt.dataoffset + au_fmt.datasize ;
328 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
329 }
330 else
331 { dword = psf->filelength - au_fmt.dataoffset ;
332 psf_log_printf (psf, " Data Size : %d (should be %d)\n", au_fmt.datasize, dword) ;
333 au_fmt.datasize = dword ;
334 } ;
335
336 psf->dataoffset = au_fmt.dataoffset ;
337 psf->datalength = psf->filelength - psf->dataoffset ;
338
339 if (psf_ftell (psf) < psf->dataoffset)
340 psf_binheader_readf (psf, "j", psf->dataoffset - psf_ftell (psf)) ;
341
342 psf->sf.samplerate = au_fmt.samplerate ;
343 psf->sf.channels = au_fmt.channels ;
344
345 /* Only fill in type major. */
346 if (psf->endian == SF_ENDIAN_BIG)
347 psf->sf.format = SF_FORMAT_AU ;
348 else if (psf->endian == SF_ENDIAN_LITTLE)
349 psf->sf.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU ;
350
351 psf_log_printf (psf, " Encoding : %d => ", au_fmt.encoding) ;
352
353 psf->sf.format = SF_ENDIAN (psf->sf.format) ;
354
355 switch (au_fmt.encoding)
356 { case AU_ENCODING_ULAW_8 :
357 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_ULAW ;
358 psf->bytewidth = 1 ; /* Before decoding */
359 psf_log_printf (psf, "8-bit ISDN u-law\n") ;
360 break ;
361
362 case AU_ENCODING_PCM_8 :
363 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_S8 ;
364 psf->bytewidth = 1 ;
365 psf_log_printf (psf, "8-bit linear PCM\n") ;
366 break ;
367
368 case AU_ENCODING_PCM_16 :
369 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_16 ;
370 psf->bytewidth = 2 ;
371 psf_log_printf (psf, "16-bit linear PCM\n") ;
372 break ;
373
374 case AU_ENCODING_PCM_24 :
375 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_24 ;
376 psf->bytewidth = 3 ;
377 psf_log_printf (psf, "24-bit linear PCM\n") ;
378 break ;
379
380 case AU_ENCODING_PCM_32 :
381 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_32 ;
382 psf->bytewidth = 4 ;
383 psf_log_printf (psf, "32-bit linear PCM\n") ;
384 break ;
385
386 case AU_ENCODING_FLOAT :
387 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_FLOAT ;
388 psf->bytewidth = 4 ;
389 psf_log_printf (psf, "32-bit float\n") ;
390 break ;
391
392 case AU_ENCODING_DOUBLE :
393 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
394 psf->bytewidth = 8 ;
395 psf_log_printf (psf, "64-bit double precision float\n") ;
396 break ;
397
398 case AU_ENCODING_ALAW_8 :
399 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_ALAW ;
400 psf->bytewidth = 1 ; /* Before decoding */
401 psf_log_printf (psf, "8-bit ISDN A-law\n") ;
402 break ;
403
404 case AU_ENCODING_ADPCM_G721_32 :
405 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G721_32 ;
406 psf->bytewidth = 0 ;
407 psf_log_printf (psf, "G721 32kbs ADPCM\n") ;
408 break ;
409
410 case AU_ENCODING_ADPCM_G723_24 :
411 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G723_24 ;
412 psf->bytewidth = 0 ;
413 psf_log_printf (psf, "G723 24kbs ADPCM\n") ;
414 break ;
415
416 case AU_ENCODING_ADPCM_G723_40 :
417 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G723_40 ;
418 psf->bytewidth = 0 ;
419 psf_log_printf (psf, "G723 40kbs ADPCM\n") ;
420 break ;
421
422 case AU_ENCODING_ADPCM_G722 :
423 psf_log_printf (psf, "G722 64 kbs ADPCM (unsupported)\n") ;
424 break ;
425
426 case AU_ENCODING_NEXT :
427 psf_log_printf (psf, "Weird NeXT encoding format (unsupported)\n") ;
428 break ;
429
430 default :
431 psf_log_printf (psf, "Unknown!!\n") ;
432 break ;
433 } ;
434
435 psf_log_printf (psf, " Sample Rate : %d\n", au_fmt.samplerate) ;
436 if (au_fmt.channels < 1)
437 { psf_log_printf (psf, " Channels : %d **** should be >= 1\n", au_fmt.channels) ;
438 return SFE_CHANNEL_COUNT_ZERO ;
439 }
440 else if (au_fmt.channels > SF_MAX_CHANNELS)
441 { psf_log_printf (psf, " Channels : %d **** should be <= %d\n", au_fmt.channels, SF_MAX_CHANNELS) ;
442 return SFE_CHANNEL_COUNT ;
443 } ;
444
445 psf_log_printf (psf, " Channels : %d\n", au_fmt.channels) ;
446
447 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
448
449 if (! psf->sf.frames && psf->blockwidth)
450 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
451
452 return 0 ;
453 } /* au_read_header */
454
455