Git init
[external/libsndfile.git] / src / mat4.c
1 /*
2 ** Copyright (C) 2002-2009 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 #include        <math.h>
26
27 #include        "sndfile.h"
28 #include        "sfendian.h"
29 #include        "common.h"
30
31 /*------------------------------------------------------------------------------
32 ** Information on how to decode and encode this file was obtained in a PDF
33 ** file which I found on http://www.wotsit.org/.
34 ** Also did a lot of testing with GNU Octave but do not have access to
35 ** Matlab (tm) and so could not test it there.
36 */
37
38 /*------------------------------------------------------------------------------
39 ** Macros to handle big/little endian issues.
40 */
41
42 #define MAT4_BE_DOUBLE  (MAKE_MARKER (0, 0, 0x03, 0xE8))
43 #define MAT4_LE_DOUBLE  (MAKE_MARKER (0, 0, 0, 0))
44
45 #define MAT4_BE_FLOAT   (MAKE_MARKER (0, 0, 0x03, 0xF2))
46 #define MAT4_LE_FLOAT   (MAKE_MARKER (0x0A, 0, 0, 0))
47
48 #define MAT4_BE_PCM_32  (MAKE_MARKER (0, 0, 0x03, 0xFC))
49 #define MAT4_LE_PCM_32  (MAKE_MARKER (0x14, 0, 0, 0))
50
51 #define MAT4_BE_PCM_16  (MAKE_MARKER (0, 0, 0x04, 0x06))
52 #define MAT4_LE_PCM_16  (MAKE_MARKER (0x1E, 0, 0, 0))
53
54 /* Can't see any reason to ever implement this. */
55 #define MAT4_BE_PCM_U8  (MAKE_MARKER (0, 0, 0x04, 0x1A))
56 #define MAT4_LE_PCM_U8  (MAKE_MARKER (0x32, 0, 0, 0))
57
58 /*------------------------------------------------------------------------------
59 ** Private static functions.
60 */
61
62 static  int             mat4_close              (SF_PRIVATE *psf) ;
63
64 static  int     mat4_format_to_encoding (int format, int endian) ;
65
66 static int              mat4_write_header (SF_PRIVATE *psf, int calc_length) ;
67 static int              mat4_read_header (SF_PRIVATE *psf) ;
68
69 static const char * mat4_marker_to_str (int marker) ;
70
71 /*------------------------------------------------------------------------------
72 ** Public function.
73 */
74
75 int
76 mat4_open       (SF_PRIVATE *psf)
77 {       int             subformat, error = 0 ;
78
79         if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
80         {       if ((error = mat4_read_header (psf)))
81                         return error ;
82                 } ;
83
84         if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_MAT4)
85                 return  SFE_BAD_OPEN_FORMAT ;
86
87         subformat = SF_CODEC (psf->sf.format) ;
88
89         if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
90         {       if (psf->is_pipe)
91                         return SFE_NO_PIPE_WRITE ;
92
93                 psf->endian = SF_ENDIAN (psf->sf.format) ;
94                 if (CPU_IS_LITTLE_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
95                         psf->endian = SF_ENDIAN_LITTLE ;
96                 else if (CPU_IS_BIG_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
97                         psf->endian = SF_ENDIAN_BIG ;
98
99                 if ((error = mat4_write_header (psf, SF_FALSE)))
100                         return error ;
101
102                 psf->write_header = mat4_write_header ;
103                 } ;
104
105         psf->container_close = mat4_close ;
106
107         psf->blockwidth = psf->bytewidth * psf->sf.channels ;
108
109         switch (subformat)
110         {       case SF_FORMAT_PCM_16 :
111                 case SF_FORMAT_PCM_32 :
112                                 error = pcm_init (psf) ;
113                                 break ;
114
115                 case SF_FORMAT_FLOAT :
116                                 error = float32_init (psf) ;
117                                 break ;
118
119                 case SF_FORMAT_DOUBLE :
120                                 error = double64_init (psf) ;
121                                 break ;
122
123                 default : break ;
124                 } ;
125
126         if (error)
127                 return error ;
128
129         return error ;
130 } /* mat4_open */
131
132 /*------------------------------------------------------------------------------
133 */
134
135 static int
136 mat4_close      (SF_PRIVATE *psf)
137 {
138         if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
139                 mat4_write_header (psf, SF_TRUE) ;
140
141         return 0 ;
142 } /* mat4_close */
143
144 /*------------------------------------------------------------------------------
145 */
146
147 static int
148 mat4_write_header (SF_PRIVATE *psf, int calc_length)
149 {       sf_count_t      current ;
150         int                     encoding ;
151         double          samplerate ;
152
153         current = psf_ftell (psf) ;
154
155         if (calc_length)
156         {       psf->filelength = psf_get_filelen (psf) ;
157
158                 psf->datalength = psf->filelength - psf->dataoffset ;
159                 if (psf->dataend)
160                         psf->datalength -= psf->filelength - psf->dataend ;
161
162                 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
163                 } ;
164
165         encoding = mat4_format_to_encoding (SF_CODEC (psf->sf.format), psf->endian) ;
166
167         if (encoding == -1)
168                 return SFE_BAD_OPEN_FORMAT ;
169
170         /* Reset the current header length to zero. */
171         psf->header [0] = 0 ;
172         psf->headindex = 0 ;
173         psf_fseek (psf, 0, SEEK_SET) ;
174
175         /* Need sample rate as a double for writing to the header. */
176         samplerate = psf->sf.samplerate ;
177
178         if (psf->endian == SF_ENDIAN_BIG)
179         {       psf_binheader_writef (psf, "Em444", MAT4_BE_DOUBLE, 1, 1, 0) ;
180                 psf_binheader_writef (psf, "E4bd", 11, "samplerate", make_size_t (11), samplerate) ;
181                 psf_binheader_writef (psf, "tEm484", encoding, psf->sf.channels, psf->sf.frames, 0) ;
182                 psf_binheader_writef (psf, "E4b", 9, "wavedata", make_size_t (9)) ;
183                 }
184         else if (psf->endian == SF_ENDIAN_LITTLE)
185         {       psf_binheader_writef (psf, "em444", MAT4_LE_DOUBLE, 1, 1, 0) ;
186                 psf_binheader_writef (psf, "e4bd", 11, "samplerate", make_size_t (11), samplerate) ;
187                 psf_binheader_writef (psf, "tem484", encoding, psf->sf.channels, psf->sf.frames, 0) ;
188                 psf_binheader_writef (psf, "e4b", 9, "wavedata", make_size_t (9)) ;
189                 }
190         else
191                 return SFE_BAD_OPEN_FORMAT ;
192
193         /* Header construction complete so write it out. */
194         psf_fwrite (psf->header, psf->headindex, 1, psf) ;
195
196         if (psf->error)
197                 return psf->error ;
198
199         psf->dataoffset = psf->headindex ;
200
201         if (current > 0)
202                 psf_fseek (psf, current, SEEK_SET) ;
203
204         return psf->error ;
205 } /* mat4_write_header */
206
207 static int
208 mat4_read_header (SF_PRIVATE *psf)
209 {       int             marker, rows, cols, imag ;
210         unsigned namesize ;
211         double  value ;
212         const char *marker_str ;
213         char    name [64] ;
214
215         psf_binheader_readf (psf, "pm", 0, &marker) ;
216
217         /* MAT4 file must start with a double for the samplerate. */
218         if (marker == MAT4_BE_DOUBLE)
219         {       psf->endian = psf->rwf_endian = SF_ENDIAN_BIG ;
220                 marker_str = "big endian double" ;
221                 }
222         else if (marker == MAT4_LE_DOUBLE)
223         {       psf->endian = psf->rwf_endian = SF_ENDIAN_LITTLE ;
224                 marker_str = "little endian double" ;
225                 }
226         else
227                 return SFE_UNIMPLEMENTED ;
228
229         psf_log_printf (psf, "GNU Octave 2.0 / MATLAB v4.2 format\nMarker : %s\n", marker_str) ;
230
231         psf_binheader_readf (psf, "444", &rows, &cols, &imag) ;
232
233         psf_log_printf (psf, " Rows  : %d\n Cols  : %d\n Imag  : %s\n", rows, cols, imag ? "True" : "False") ;
234
235         psf_binheader_readf (psf, "4", &namesize) ;
236
237         if (namesize >= SIGNED_SIZEOF (name))
238                 return SFE_MAT4_BAD_NAME ;
239
240         psf_binheader_readf (psf, "b", name, namesize) ;
241         name [namesize] = 0 ;
242
243         psf_log_printf (psf, " Name  : %s\n", name) ;
244
245         psf_binheader_readf (psf, "d", &value) ;
246
247         snprintf (psf->u.cbuf, sizeof (psf->u.cbuf), " Value : %f\n", value) ;
248         psf_log_printf (psf, psf->u.cbuf) ;
249
250         if ((rows != 1) || (cols != 1))
251                 return SFE_MAT4_NO_SAMPLERATE ;
252
253         psf->sf.samplerate = lrint (value) ;
254
255         /* Now write out the audio data. */
256
257         psf_binheader_readf (psf, "m", &marker) ;
258
259         psf_log_printf (psf, "Marker : %s\n", mat4_marker_to_str (marker)) ;
260
261         psf_binheader_readf (psf, "444", &rows, &cols, &imag) ;
262
263         psf_log_printf (psf, " Rows  : %d\n Cols  : %d\n Imag  : %s\n", rows, cols, imag ? "True" : "False") ;
264
265         psf_binheader_readf (psf, "4", &namesize) ;
266
267         if (namesize >= SIGNED_SIZEOF (name))
268                 return SFE_MAT4_BAD_NAME ;
269
270         psf_binheader_readf (psf, "b", name, namesize) ;
271         name [namesize] = 0 ;
272
273         psf_log_printf (psf, " Name  : %s\n", name) ;
274
275         psf->dataoffset = psf_ftell (psf) ;
276
277         if (rows == 0 && cols == 0)
278         {       psf_log_printf (psf, "*** Error : zero channel count.\n") ;
279                 return SFE_CHANNEL_COUNT_ZERO ;
280                 } ;
281
282         psf->sf.channels        = rows ;
283         psf->sf.frames          = cols ;
284
285         psf->sf.format = psf->endian | SF_FORMAT_MAT4 ;
286         switch (marker)
287         {       case MAT4_BE_DOUBLE :
288                 case MAT4_LE_DOUBLE :
289                                 psf->sf.format |= SF_FORMAT_DOUBLE ;
290                                 psf->bytewidth = 8 ;
291                                 break ;
292
293                 case MAT4_BE_FLOAT :
294                 case MAT4_LE_FLOAT :
295                                 psf->sf.format |= SF_FORMAT_FLOAT ;
296                                 psf->bytewidth = 4 ;
297                                 break ;
298
299                 case MAT4_BE_PCM_32     :
300                 case MAT4_LE_PCM_32     :
301                                 psf->sf.format |= SF_FORMAT_PCM_32 ;
302                                 psf->bytewidth = 4 ;
303                                 break ;
304
305                 case MAT4_BE_PCM_16     :
306                 case MAT4_LE_PCM_16     :
307                                 psf->sf.format |= SF_FORMAT_PCM_16 ;
308                                 psf->bytewidth = 2 ;
309                                 break ;
310
311                 default :
312                                 psf_log_printf (psf, "*** Error : Bad marker %08X\n", marker) ;
313                                 return SFE_UNIMPLEMENTED ;
314                 } ;
315
316         if ((psf->filelength - psf->dataoffset) < psf->sf.channels * psf->sf.frames * psf->bytewidth)
317         {       psf_log_printf (psf, "*** File seems to be truncated. %D <--> %D\n",
318                                 psf->filelength - psf->dataoffset, psf->sf.channels * psf->sf.frames * psf->bytewidth) ;
319                 }
320         else if ((psf->filelength - psf->dataoffset) > psf->sf.channels * psf->sf.frames * psf->bytewidth)
321                 psf->dataend = psf->dataoffset + rows * cols * psf->bytewidth ;
322
323         psf->datalength = psf->filelength - psf->dataoffset - psf->dataend ;
324
325         psf->sf.sections = 1 ;
326
327         return 0 ;
328 } /* mat4_read_header */
329
330 static int
331 mat4_format_to_encoding (int format, int endian)
332 {
333         switch (format | endian)
334         {       case (SF_FORMAT_PCM_16 | SF_ENDIAN_BIG) :
335                                 return MAT4_BE_PCM_16 ;
336
337                 case (SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE) :
338                                 return MAT4_LE_PCM_16 ;
339
340                 case (SF_FORMAT_PCM_32 | SF_ENDIAN_BIG) :
341                                 return MAT4_BE_PCM_32 ;
342
343                 case (SF_FORMAT_PCM_32 | SF_ENDIAN_LITTLE) :
344                                 return MAT4_LE_PCM_32 ;
345
346                 case (SF_FORMAT_FLOAT | SF_ENDIAN_BIG) :
347                                 return MAT4_BE_FLOAT ;
348
349                 case (SF_FORMAT_FLOAT | SF_ENDIAN_LITTLE) :
350                                 return MAT4_LE_FLOAT ;
351
352                 case (SF_FORMAT_DOUBLE | SF_ENDIAN_BIG) :
353                                 return MAT4_BE_DOUBLE ;
354
355                 case (SF_FORMAT_DOUBLE | SF_ENDIAN_LITTLE) :
356                                 return MAT4_LE_DOUBLE ;
357
358                 default : break ;
359                 } ;
360
361         return -1 ;
362 } /* mat4_format_to_encoding */
363
364 static const char *
365 mat4_marker_to_str (int marker)
366 {       static char str [32] ;
367
368         switch (marker)
369         {
370                 case MAT4_BE_PCM_16     :       return "big endian 16 bit PCM" ;
371                 case MAT4_LE_PCM_16     :       return "little endian 16 bit PCM" ;
372
373                 case MAT4_BE_PCM_32     :       return "big endian 32 bit PCM" ;
374                 case MAT4_LE_PCM_32     :       return "little endian 32 bit PCM" ;
375
376
377                 case MAT4_BE_FLOAT :    return "big endian float" ;
378                 case MAT4_LE_FLOAT :    return "big endian float" ;
379
380                 case MAT4_BE_DOUBLE     :       return "big endian double" ;
381                 case MAT4_LE_DOUBLE     :       return "little endian double" ;
382                 } ;
383
384         /* This is a little unsafe but is really only for debugging. */
385         str [sizeof (str) - 1] = 0 ;
386         snprintf (str, sizeof (str) - 1, "%08X", marker) ;
387         return str ;
388 } /* mat4_marker_to_str */
389