/src/libsndfile/src/nist.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* |
20 | | ** Some of the information used to read NIST files was gleaned from |
21 | | ** reading the code of Bill Schottstaedt's sndlib library |
22 | | ** ftp://ccrma-ftp.stanford.edu/pub/Lisp/sndlib.tar.gz |
23 | | ** However, no code from that package was used. |
24 | | */ |
25 | | |
26 | | #include "sfconfig.h" |
27 | | |
28 | | #include <stdio.h> |
29 | | #include <fcntl.h> |
30 | | #include <string.h> |
31 | | #include <ctype.h> |
32 | | |
33 | | #include "sndfile.h" |
34 | | #include "sfendian.h" |
35 | | #include "common.h" |
36 | | |
37 | | /*------------------------------------------------------------------------------ |
38 | | */ |
39 | | |
40 | 708 | #define NIST_HEADER_LENGTH 1024 |
41 | | |
42 | | /*------------------------------------------------------------------------------ |
43 | | ** Private static functions. |
44 | | */ |
45 | | |
46 | | static int nist_close (SF_PRIVATE *psf) ; |
47 | | static int nist_write_header (SF_PRIVATE *psf, int calc_length) ; |
48 | | static int nist_read_header (SF_PRIVATE *psf) ; |
49 | | |
50 | | /*------------------------------------------------------------------------------ |
51 | | */ |
52 | | |
53 | | int |
54 | | nist_open (SF_PRIVATE *psf) |
55 | 285 | { int error ; |
56 | | |
57 | 285 | if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0)) |
58 | 285 | { if ((error = nist_read_header (psf))) |
59 | 264 | return error ; |
60 | 285 | } ; |
61 | | |
62 | 21 | if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR) |
63 | 0 | { if (psf->is_pipe) |
64 | 0 | return SFE_NO_PIPE_WRITE ; |
65 | | |
66 | 0 | if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_NIST) |
67 | 0 | return SFE_BAD_OPEN_FORMAT ; |
68 | | |
69 | 0 | psf->endian = SF_ENDIAN (psf->sf.format) ; |
70 | 0 | if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU) |
71 | 0 | psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ; |
72 | |
|
73 | 0 | psf->blockwidth = psf->bytewidth * psf->sf.channels ; |
74 | 0 | psf->sf.frames = 0 ; |
75 | |
|
76 | 0 | if ((error = nist_write_header (psf, SF_FALSE))) |
77 | 0 | return error ; |
78 | | |
79 | 0 | psf->write_header = nist_write_header ; |
80 | 21 | } ; |
81 | | |
82 | 21 | psf->container_close = nist_close ; |
83 | | |
84 | 21 | switch (SF_CODEC (psf->sf.format)) |
85 | 21 | { case SF_FORMAT_PCM_S8 : |
86 | 2 | error = pcm_init (psf) ; |
87 | 2 | break ; |
88 | | |
89 | 2 | case SF_FORMAT_PCM_16 : |
90 | 4 | case SF_FORMAT_PCM_24 : |
91 | 6 | case SF_FORMAT_PCM_32 : |
92 | 6 | error = pcm_init (psf) ; |
93 | 6 | break ; |
94 | | |
95 | 4 | case SF_FORMAT_ULAW : |
96 | 4 | error = ulaw_init (psf) ; |
97 | 4 | break ; |
98 | | |
99 | 2 | case SF_FORMAT_ALAW : |
100 | 2 | error = alaw_init (psf) ; |
101 | 2 | break ; |
102 | | |
103 | 7 | default : error = SFE_UNIMPLEMENTED ; |
104 | 7 | break ; |
105 | 21 | } ; |
106 | | |
107 | 21 | return error ; |
108 | 21 | } /* nist_open */ |
109 | | |
110 | | /*------------------------------------------------------------------------------ |
111 | | */ |
112 | | |
113 | | static char bad_header [] = |
114 | | { 'N', 'I', 'S', 'T', '_', '1', 'A', 0x0d, 0x0a, |
115 | | ' ', ' ', ' ', '1', '0', '2', '4', 0x0d, 0x0a, |
116 | | 0 |
117 | | } ; |
118 | | |
119 | | static int |
120 | | nist_read_header (SF_PRIVATE *psf) |
121 | 285 | { char psf_header [NIST_HEADER_LENGTH + 2] ; |
122 | 285 | int bitwidth = 0, count, encoding ; |
123 | 285 | unsigned bytes = 0 ; |
124 | 285 | char str [64], *cptr ; |
125 | 285 | long samples ; |
126 | | |
127 | | /* Go to start of file and read in the whole header. */ |
128 | 285 | psf_binheader_readf (psf, "pb", 0, psf_header, NIST_HEADER_LENGTH) ; |
129 | | |
130 | | /* Header is a string, so make sure it is null terminated. */ |
131 | 285 | psf_header [NIST_HEADER_LENGTH] = 0 ; |
132 | | |
133 | | /* Now trim the header after the end marker. */ |
134 | 285 | if ((cptr = strstr (psf_header, "end_head"))) |
135 | 9 | { cptr += strlen ("end_head") + 1 ; |
136 | 9 | cptr [0] = 0 ; |
137 | 9 | } ; |
138 | | |
139 | 285 | if (strstr (psf_header, bad_header) == psf_header) |
140 | 2 | return SFE_NIST_CRLF_CONVERISON ; |
141 | | |
142 | | /* Make sure its a NIST file. */ |
143 | 283 | if (strstr (psf_header, "NIST_1A\n") != psf_header) |
144 | 8 | { psf_log_printf (psf, "Not a NIST file.\n") ; |
145 | 8 | return SFE_NIST_BAD_HEADER ; |
146 | 275 | } ; |
147 | | |
148 | 275 | if (sscanf (psf_header, "NIST_1A\n%d\n", &count) == 1) |
149 | 137 | psf->dataoffset = count ; |
150 | 138 | else |
151 | 138 | { psf_log_printf (psf, "*** Suspicious header length.\n") ; |
152 | 138 | psf->dataoffset = NIST_HEADER_LENGTH ; |
153 | 138 | } ; |
154 | | |
155 | | /* Determine sample encoding, start by assuming PCM. */ |
156 | 275 | encoding = SF_FORMAT_PCM_U8 ; |
157 | 275 | if ((cptr = strstr (psf_header, "sample_coding -s"))) |
158 | 144 | { sscanf (cptr, "sample_coding -s%d %63s", &count, str) ; |
159 | | |
160 | 144 | if (strcmp (str, "pcm") == 0) |
161 | 2 | { /* Correct this later when we find out the bitwidth. */ |
162 | 2 | encoding = SF_FORMAT_PCM_U8 ; |
163 | 2 | } |
164 | 142 | else if (strcmp (str, "alaw") == 0) |
165 | 2 | encoding = SF_FORMAT_ALAW ; |
166 | 140 | else if ((strcmp (str, "ulaw") == 0) || (strcmp (str, "mu-law") == 0)) |
167 | 4 | encoding = SF_FORMAT_ULAW ; |
168 | 136 | else |
169 | 136 | { psf_log_printf (psf, "*** Unknown encoding : %s\n", str) ; |
170 | 136 | encoding = 0 ; |
171 | 136 | } ; |
172 | 144 | } ; |
173 | | |
174 | 275 | if ((cptr = strstr (psf_header, "channel_count -i ")) != NULL) |
175 | 34 | sscanf (cptr, "channel_count -i %d", &(psf->sf.channels)) ; |
176 | | |
177 | 275 | if ((cptr = strstr (psf_header, "sample_rate -i ")) != NULL) |
178 | 61 | sscanf (cptr, "sample_rate -i %d", &(psf->sf.samplerate)) ; |
179 | | |
180 | 275 | if ((cptr = strstr (psf_header, "sample_count -i ")) != NULL) |
181 | 37 | { sscanf (cptr, "sample_count -i %ld", &samples) ; |
182 | 37 | psf->sf.frames = samples ; |
183 | 37 | } ; |
184 | | |
185 | 275 | if ((cptr = strstr (psf_header, "sample_n_bytes -i ")) != NULL) |
186 | 119 | sscanf (cptr, "sample_n_bytes -i %d", &(psf->bytewidth)) ; |
187 | | |
188 | | /* Default endian-ness (for 8 bit, u-law, A-law. */ |
189 | 275 | psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ; |
190 | | |
191 | | /* This is where we figure out endian-ness. */ |
192 | 275 | if ((cptr = strstr (psf_header, "sample_byte_format -s")) |
193 | 275 | && sscanf (cptr, "sample_byte_format -s%u %8s", &bytes, str) == 2) |
194 | 154 | { |
195 | 154 | if (bytes != strlen (str)) |
196 | 143 | psf_log_printf (psf, "Weird sample_byte_format : strlen '%s' != %d\n", str, bytes) ; |
197 | | |
198 | 154 | if (bytes > 1) |
199 | 151 | { if (psf->bytewidth == 0) |
200 | 49 | psf->bytewidth = bytes ; |
201 | 102 | else if (psf->bytewidth - bytes != 0) |
202 | 100 | { psf_log_printf (psf, "psf->bytewidth (%d) != bytes (%d)\n", psf->bytewidth, bytes) ; |
203 | 100 | return SFE_NIST_BAD_ENCODING ; |
204 | 100 | } ; |
205 | | |
206 | 51 | if (strcmp (str, "01") == 0) |
207 | 2 | psf->endian = SF_ENDIAN_LITTLE ; |
208 | 49 | else if (strcmp (str, "10") == 0) |
209 | 2 | psf->endian = SF_ENDIAN_BIG ; |
210 | 47 | else |
211 | 47 | { psf_log_printf (psf, "Weird endian-ness : %s\n", str) ; |
212 | 47 | return SFE_NIST_BAD_ENCODING ; |
213 | 47 | } ; |
214 | 7 | } ; |
215 | | |
216 | 7 | psf->sf.format |= psf->endian ; |
217 | 128 | } ; |
218 | | |
219 | 128 | if ((cptr = strstr (psf_header, "sample_sig_bits -i "))) |
220 | 6 | sscanf (cptr, "sample_sig_bits -i %d", &bitwidth) ; |
221 | | |
222 | 128 | if (strstr (psf_header, "channels_interleaved -s5 FALSE")) |
223 | 4 | { psf_log_printf (psf, "Non-interleaved data unsupported.\n", str) ; |
224 | 4 | return SFE_NIST_BAD_ENCODING ; |
225 | 124 | } ; |
226 | | |
227 | 124 | psf->blockwidth = psf->sf.channels * psf->bytewidth ; |
228 | 124 | psf->datalength = psf->filelength - psf->dataoffset ; |
229 | | |
230 | 124 | psf_fseek (psf, psf->dataoffset, SEEK_SET) ; |
231 | | |
232 | 124 | if (encoding == SF_FORMAT_PCM_U8) |
233 | 15 | { switch (psf->bytewidth) |
234 | 15 | { case 1 : |
235 | 2 | psf->sf.format |= SF_FORMAT_PCM_S8 ; |
236 | 2 | break ; |
237 | | |
238 | 2 | case 2 : |
239 | 2 | psf->sf.format |= SF_FORMAT_PCM_16 ; |
240 | 2 | break ; |
241 | | |
242 | 2 | case 3 : |
243 | 2 | psf->sf.format |= SF_FORMAT_PCM_24 ; |
244 | 2 | break ; |
245 | | |
246 | 2 | case 4 : |
247 | 2 | psf->sf.format |= SF_FORMAT_PCM_32 ; |
248 | 2 | break ; |
249 | | |
250 | 7 | default : break ; |
251 | 15 | } ; |
252 | 15 | } |
253 | 109 | else if (encoding != 0) |
254 | 6 | psf->sf.format |= encoding ; |
255 | 103 | else |
256 | 103 | return SFE_UNIMPLEMENTED ; |
257 | | |
258 | | /* Sanitize psf->sf.format. */ |
259 | 21 | switch (SF_CODEC (psf->sf.format)) |
260 | 21 | { case SF_FORMAT_ULAW : |
261 | 6 | case SF_FORMAT_ALAW : |
262 | 6 | case SF_FORMAT_PCM_U8 : |
263 | | /* Blank out endian bits. */ |
264 | 6 | psf->sf.format = SF_FORMAT_NIST | SF_CODEC (psf->sf.format) ; |
265 | 6 | break ; |
266 | | |
267 | 15 | default : |
268 | 15 | break ; |
269 | 21 | } ; |
270 | | |
271 | 21 | return 0 ; |
272 | 21 | } /* nist_read_header */ |
273 | | |
274 | | static int |
275 | | nist_close (SF_PRIVATE *psf) |
276 | 21 | { |
277 | 21 | if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR) |
278 | 0 | nist_write_header (psf, SF_TRUE) ; |
279 | | |
280 | 21 | return 0 ; |
281 | 21 | } /* nist_close */ |
282 | | |
283 | | /*========================================================================= |
284 | | */ |
285 | | |
286 | | static int |
287 | | nist_write_header (SF_PRIVATE *psf, int calc_length) |
288 | 0 | { const char *end_str ; |
289 | 0 | long samples ; |
290 | 0 | sf_count_t current ; |
291 | |
|
292 | 0 | current = psf_ftell (psf) ; |
293 | |
|
294 | 0 | if (calc_length) |
295 | 0 | { psf->filelength = psf_get_filelen (psf) ; |
296 | |
|
297 | 0 | psf->datalength = psf->filelength - psf->dataoffset ; |
298 | |
|
299 | 0 | if (psf->dataend) |
300 | 0 | psf->datalength -= psf->filelength - psf->dataend ; |
301 | |
|
302 | 0 | if (psf->bytewidth > 0) |
303 | 0 | psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ; |
304 | 0 | } ; |
305 | |
|
306 | 0 | if (psf->endian == SF_ENDIAN_BIG) |
307 | 0 | end_str = "10" ; |
308 | 0 | else if (psf->endian == SF_ENDIAN_LITTLE) |
309 | 0 | end_str = "01" ; |
310 | 0 | else |
311 | 0 | end_str = "error" ; |
312 | | |
313 | | /* Clear the whole header. */ |
314 | 0 | memset (psf->header.ptr, 0, psf->header.len) ; |
315 | 0 | psf->header.indx = 0 ; |
316 | |
|
317 | 0 | psf_fseek (psf, 0, SEEK_SET) ; |
318 | |
|
319 | 0 | psf_asciiheader_printf (psf, "NIST_1A\n 1024\n") ; |
320 | 0 | psf_asciiheader_printf (psf, "channel_count -i %d\n", psf->sf.channels) ; |
321 | 0 | psf_asciiheader_printf (psf, "sample_rate -i %d\n", psf->sf.samplerate) ; |
322 | |
|
323 | 0 | switch (SF_CODEC (psf->sf.format)) |
324 | 0 | { case SF_FORMAT_PCM_S8 : |
325 | 0 | psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n") ; |
326 | 0 | psf_asciiheader_printf (psf, "sample_n_bytes -i 1\n" |
327 | 0 | "sample_sig_bits -i 8\n") ; |
328 | 0 | break ; |
329 | | |
330 | 0 | case SF_FORMAT_PCM_16 : |
331 | 0 | case SF_FORMAT_PCM_24 : |
332 | 0 | case SF_FORMAT_PCM_32 : |
333 | 0 | psf_asciiheader_printf (psf, "sample_n_bytes -i %d\n", psf->bytewidth) ; |
334 | 0 | psf_asciiheader_printf (psf, "sample_sig_bits -i %d\n", psf->bytewidth * 8) ; |
335 | 0 | psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n" |
336 | 0 | "sample_byte_format -s%d %s\n", psf->bytewidth, end_str) ; |
337 | 0 | break ; |
338 | | |
339 | 0 | case SF_FORMAT_ALAW : |
340 | 0 | psf_asciiheader_printf (psf, "sample_coding -s4 alaw\n") ; |
341 | 0 | psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ; |
342 | 0 | break ; |
343 | | |
344 | 0 | case SF_FORMAT_ULAW : |
345 | 0 | psf_asciiheader_printf (psf, "sample_coding -s4 ulaw\n") ; |
346 | 0 | psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ; |
347 | 0 | break ; |
348 | | |
349 | 0 | default : return SFE_UNIMPLEMENTED ; |
350 | 0 | } ; |
351 | |
|
352 | 0 | psf->dataoffset = NIST_HEADER_LENGTH ; |
353 | | |
354 | | /* Fix this */ |
355 | 0 | samples = psf->sf.frames ; |
356 | 0 | psf_asciiheader_printf (psf, "sample_count -i %ld\n", samples) ; |
357 | 0 | psf_asciiheader_printf (psf, "end_head\n") ; |
358 | | |
359 | | /* Zero fill to dataoffset. */ |
360 | 0 | psf_binheader_writef (psf, "z", BHWz ((size_t) (NIST_HEADER_LENGTH - psf->header.indx))) ; |
361 | |
|
362 | 0 | psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ; |
363 | |
|
364 | 0 | if (psf->error) |
365 | 0 | return psf->error ; |
366 | | |
367 | 0 | if (current > 0) |
368 | 0 | psf_fseek (psf, current, SEEK_SET) ; |
369 | |
|
370 | 0 | return psf->error ; |
371 | 0 | } /* nist_write_header */ |
372 | | |