48e2f01c |
1 | /* |
2 | * Copyright (C) 2013 nu774 |
3 | * For conditions of distribution and use, see copyright notice in COPYING |
4 | */ |
5 | #if HAVE_CONFIG_H |
6 | # include "config.h" |
7 | #endif |
8 | |
9 | #if HAVE_STDINT_H |
10 | # include <stdint.h> |
11 | #endif |
12 | |
13 | #include <stdio.h> |
14 | #include <stdlib.h> |
15 | #include <string.h> |
16 | #include <stdarg.h> |
17 | #include "wav_reader.h" |
18 | #include "m4af_endian.h" |
19 | |
20 | #define RIFF_FOURCC(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24)) |
21 | |
22 | #define TRY_IO(expr) \ |
23 | do { \ |
24 | if (expr) \ |
25 | goto FAIL; \ |
26 | } while (0) |
27 | |
28 | #define ASSERT_FORMAT(ctx, expr) \ |
29 | do { \ |
30 | if (!expr) { \ |
31 | if (!ctx->last_error) \ |
32 | ctx->last_error = WAV_INVALID_FORMAT; \ |
33 | goto FAIL;\ |
34 | } \ |
35 | } while (0) |
36 | |
37 | struct wav_reader_t { |
38 | pcm_sample_description_t sample_format; |
39 | int64_t length; |
40 | int64_t position; |
41 | int ignore_length; |
42 | int last_error; |
43 | wav_io_context_t io; |
44 | void *io_cookie; |
45 | }; |
46 | |
47 | static const uint8_t WAV_GUID_PCM[] = { |
48 | 1, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71 |
49 | }; |
50 | static const uint8_t WAV_GUID_FLOAT[] = { |
51 | 3, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71 |
52 | }; |
53 | |
54 | const pcm_sample_description_t *wav_get_format(wav_reader_t *reader) |
55 | { |
56 | return &reader->sample_format; |
57 | } |
58 | |
59 | int64_t wav_get_length(wav_reader_t *reader) |
60 | { |
61 | return reader->length; |
62 | } |
63 | |
64 | int64_t wav_get_position(wav_reader_t *reader) |
65 | { |
66 | return reader->position; |
67 | } |
68 | |
69 | void wav_teardown(wav_reader_t **reader) |
70 | { |
71 | free(*reader); |
72 | *reader = 0; |
73 | } |
74 | |
75 | static |
76 | int riff_read(wav_reader_t *reader, void *buffer, uint32_t size) |
77 | { |
78 | int rc; |
79 | uint32_t count = 0; |
80 | |
81 | if (reader->last_error) |
82 | return -1; |
83 | do { |
84 | rc = reader->io.read(reader->io_cookie, buffer, size - count); |
85 | if (rc > 0) |
86 | count += rc; |
87 | else if (rc < 0) |
88 | reader->last_error = WAV_IO_ERROR; |
89 | } while (rc > 0 && count < size); |
90 | return count > 0 ? count : rc; |
91 | } |
92 | |
93 | static |
68543176 |
94 | int riff_skip(wav_reader_t *reader, int64_t count) |
48e2f01c |
95 | { |
96 | char buff[8192]; |
97 | int rc; |
98 | |
99 | if (reader->last_error) |
100 | return -1; |
101 | if (count == 0) |
102 | return 0; |
103 | if (reader->io.seek && |
104 | reader->io.seek(reader->io_cookie, count, SEEK_CUR) >= 0) |
105 | return 0; |
106 | |
107 | do { |
108 | if ((rc = riff_read(reader, buff, count > 8192 ? 8192 : count)) > 0) |
109 | count -= rc; |
110 | } while (rc > 0 && count > 0); |
111 | |
112 | if (count > 0) |
113 | reader->last_error = WAV_IO_ERROR; |
114 | return reader->last_error ? -1 : 0; |
115 | } |
116 | |
68543176 |
117 | static |
118 | int riff_seek(wav_reader_t *reader, int64_t off, int whence) |
119 | { |
120 | int rc; |
121 | if (reader->last_error) |
122 | return -1; |
123 | if (!reader->io.seek) |
124 | goto FAIL; |
125 | if ((rc = reader->io.seek(reader->io_cookie, off, whence)) < 0) |
126 | goto FAIL; |
127 | return 0; |
128 | FAIL: |
129 | reader->last_error = WAV_IO_ERROR; |
130 | return -1; |
131 | } |
132 | |
133 | static |
134 | int64_t riff_tell(wav_reader_t *reader) |
135 | { |
136 | int64_t off; |
137 | |
138 | if (reader->last_error || !reader->io.tell) |
139 | return -1; |
140 | off = reader->io.tell(reader->io_cookie); |
141 | if (off < 0) { |
142 | reader->last_error = WAV_IO_ERROR; |
143 | return -1; |
144 | } |
145 | return off; |
146 | } |
147 | |
48e2f01c |
148 | static |
149 | int riff_read16(wav_reader_t *reader, uint16_t *value) |
150 | { |
151 | TRY_IO(riff_read(reader, value, 2) != 2); |
152 | *value = m4af_ltoh16(*value); |
153 | return 0; |
154 | FAIL: |
155 | return -1; |
156 | } |
157 | |
158 | static |
159 | int riff_read32(wav_reader_t *reader, uint32_t *value) |
160 | { |
161 | TRY_IO(riff_read(reader, value, 4) != 4); |
162 | *value = m4af_ltoh32(*value); |
163 | return 0; |
164 | FAIL: |
165 | return -1; |
166 | } |
167 | |
168 | static |
169 | int riff_read64(wav_reader_t *reader, uint64_t *value) |
170 | { |
171 | TRY_IO(riff_read(reader, value, 8) != 8); |
172 | *value = m4af_ltoh64(*value); |
173 | return 0; |
174 | FAIL: |
175 | return -1; |
176 | } |
177 | |
178 | static |
179 | int riff_scan(wav_reader_t *reader, const char *fmt, ...) |
180 | { |
181 | int c, count = 0; |
182 | va_list ap; |
183 | |
184 | va_start(ap, fmt); |
185 | while ((c = *fmt++)) { |
186 | switch (c) { |
187 | case 'S': |
188 | TRY_IO(riff_read16(reader, va_arg(ap, uint16_t*))); |
189 | ++count; |
190 | break; |
191 | case 'L': |
192 | TRY_IO(riff_read32(reader, va_arg(ap, uint32_t*))); |
193 | ++count; |
194 | break; |
195 | case 'Q': |
196 | TRY_IO(riff_read64(reader, va_arg(ap, uint64_t*))); |
197 | ++count; |
198 | break; |
199 | } |
200 | } |
201 | FAIL: |
202 | va_end(ap); |
203 | return count; |
204 | } |
205 | |
206 | static |
207 | uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size) |
208 | { |
209 | uint32_t fcc; |
210 | if (riff_scan(reader, "LL", &fcc, chunk_size) == 2) |
211 | return fcc; |
212 | return 0; |
213 | } |
214 | |
215 | int wav_read_frames(wav_reader_t *reader, void *buffer, unsigned nframes) |
216 | { |
217 | int rc; |
218 | unsigned nbytes; |
219 | |
220 | if (!reader->ignore_length && nframes > reader->length - reader->position) |
221 | nframes = reader->length - reader->position; |
222 | nbytes = nframes * reader->sample_format.bytes_per_frame; |
223 | if (nbytes) { |
224 | if ((rc = riff_read(reader, buffer, nbytes)) < 0) |
225 | return -1; |
226 | nframes = rc / reader->sample_format.bytes_per_frame; |
227 | reader->position += nframes; |
228 | } |
229 | return nframes; |
230 | } |
231 | |
232 | static |
233 | int riff_ds64(wav_reader_t *reader, int64_t *length) |
234 | { |
235 | uint32_t fcc, chunk_size, table_size; |
236 | uint64_t riff_size, sample_count; |
237 | |
238 | fcc = riff_next_chunk(reader, &chunk_size); |
239 | ASSERT_FORMAT(reader, |
240 | fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28); |
241 | TRY_IO(riff_scan(reader, "QQQL", |
242 | &riff_size, length, &sample_count, &table_size) != 4); |
243 | if (table_size == 0) |
244 | return 0; |
245 | reader->last_error = WAV_UNSUPPORTED_FORMAT; |
246 | FAIL: |
247 | return -1; |
248 | } |
249 | |
250 | static |
251 | int wav_fmt(wav_reader_t *reader, uint32_t size) |
252 | { |
253 | uint16_t wFormatTag, nChannels, nBlockAlign, wBitsPerSample, cbSize; |
254 | uint32_t nSamplesPerSec, nAvgBytesPerSec, dwChannelMask = 0; |
255 | uint16_t wValidBitsPerSample; |
256 | uint8_t guid[16]; |
257 | int is_float = 0; |
258 | |
259 | ASSERT_FORMAT(reader, size >= 16); |
260 | TRY_IO(riff_scan(reader, "SSLLSS", &wFormatTag, &nChannels, |
261 | &nSamplesPerSec, &nAvgBytesPerSec, &nBlockAlign, |
262 | &wBitsPerSample) != 6); |
263 | wValidBitsPerSample = wBitsPerSample; |
264 | |
265 | if (wFormatTag != 1 && wFormatTag != 3 && wFormatTag != 0xfffe) { |
266 | reader->last_error = WAV_UNSUPPORTED_FORMAT; |
267 | goto FAIL; |
268 | } |
269 | ASSERT_FORMAT(reader, |
270 | nChannels && nSamplesPerSec && nAvgBytesPerSec && |
271 | nBlockAlign && wBitsPerSample && !(wBitsPerSample & 7) && |
272 | nBlockAlign == nChannels * wBitsPerSample / 8); |
273 | if (wFormatTag == 3) |
274 | is_float = 1; |
275 | |
276 | if (wFormatTag != 0xfffe) |
277 | TRY_IO(riff_skip(reader, (size - 15) & ~1)); |
278 | else { |
279 | ASSERT_FORMAT(reader, size >= 40); |
280 | TRY_IO(riff_scan(reader, "SSL", |
281 | &cbSize, &wValidBitsPerSample, &dwChannelMask) != 3); |
282 | TRY_IO(riff_read(reader, guid, 16) != 16); |
283 | |
284 | if (memcmp(guid, WAV_GUID_FLOAT, 16) == 0) |
285 | is_float = 1; |
286 | else if (memcmp(guid, WAV_GUID_PCM, 16) != 0) { |
287 | reader->last_error = WAV_UNSUPPORTED_FORMAT; |
288 | goto FAIL; |
289 | } |
290 | ASSERT_FORMAT(reader, |
291 | wValidBitsPerSample && |
292 | wValidBitsPerSample <= wBitsPerSample); |
293 | TRY_IO(riff_skip(reader, (size - 39) & ~1)); |
294 | } |
295 | reader->sample_format.sample_rate = nSamplesPerSec; |
296 | reader->sample_format.bits_per_channel = wValidBitsPerSample; |
297 | reader->sample_format.bytes_per_frame = nBlockAlign; |
298 | reader->sample_format.channels_per_frame = nChannels; |
299 | reader->sample_format.channel_mask = dwChannelMask; |
300 | if (is_float) |
301 | reader->sample_format.sample_type = PCM_TYPE_FLOAT; |
302 | else if (wBitsPerSample == 8) |
303 | reader->sample_format.sample_type = PCM_TYPE_UINT; |
304 | else |
305 | reader->sample_format.sample_type = PCM_TYPE_SINT; |
306 | return 0; |
307 | FAIL: |
308 | return -1; |
309 | } |
310 | |
311 | static |
312 | int wav_parse(wav_reader_t *reader, int64_t *data_length) |
313 | { |
314 | uint32_t container, fcc, chunk_size; |
315 | |
316 | *data_length = 0; |
317 | container = riff_next_chunk(reader, &chunk_size); |
318 | if (container != RIFF_FOURCC('R','I','F','F') && |
319 | container != RIFF_FOURCC('R','F','6','4')) |
320 | goto FAIL; |
321 | TRY_IO(riff_read32(reader, &fcc)); |
322 | if (fcc != RIFF_FOURCC('W','A','V','E')) |
323 | goto FAIL; |
324 | if (container == RIFF_FOURCC('R','F','6','4')) |
325 | riff_ds64(reader, data_length); |
326 | while ((fcc = riff_next_chunk(reader, &chunk_size)) != 0) { |
327 | if (fcc == RIFF_FOURCC('f','m','t',' ')) { |
328 | if (wav_fmt(reader, chunk_size) < 0) |
329 | goto FAIL; |
330 | } else if (fcc == RIFF_FOURCC('d','a','t','a')) { |
331 | if (container == RIFF_FOURCC('R','I','F','F')) |
332 | *data_length = chunk_size; |
333 | break; |
334 | } else |
335 | TRY_IO(riff_skip(reader, (chunk_size + 1) & ~1)); |
336 | } |
337 | if (fcc == RIFF_FOURCC('d','a','t','a')) |
338 | return 0; |
339 | FAIL: |
340 | return -1; |
341 | } |
342 | |
343 | wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, |
344 | int ignore_length) |
345 | { |
346 | wav_reader_t *reader; |
347 | int64_t data_length; |
348 | |
349 | if ((reader = calloc(1, sizeof(wav_reader_t))) == 0) |
350 | return 0; |
351 | memcpy(&reader->io, io_ctx, sizeof(wav_io_context_t)); |
352 | reader->io_cookie = io_cookie; |
353 | reader->ignore_length = ignore_length; |
354 | if (wav_parse(reader, &data_length) < 0) { |
355 | free(reader); |
356 | return 0; |
357 | } |
358 | if (ignore_length || !data_length || |
359 | data_length % reader->sample_format.bytes_per_frame != 0) |
360 | reader->length = INT64_MAX; |
361 | else |
362 | reader->length = data_length / reader->sample_format.bytes_per_frame; |
363 | return reader; |
364 | } |
68543176 |
365 | |
366 | wav_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie, |
367 | const pcm_sample_description_t *desc) |
368 | { |
369 | wav_reader_t *reader = 0; |
370 | |
371 | if ((reader = calloc(1, sizeof(wav_reader_t))) == 0) |
372 | return 0; |
373 | memcpy(&reader->io, io_ctx, sizeof(wav_io_context_t)); |
374 | memcpy(&reader->sample_format, desc, sizeof(pcm_sample_description_t)); |
375 | reader->io_cookie = io_cookie; |
376 | if (io_ctx->seek && io_ctx->tell) { |
377 | TRY_IO(riff_seek(reader, 0, SEEK_END)); |
378 | reader->length = riff_tell(reader) / desc->bytes_per_frame; |
379 | TRY_IO(riff_seek(reader, 0, SEEK_SET)); |
380 | } else |
381 | reader->length = INT64_MAX; |
382 | return reader; |
383 | FAIL: |
384 | if (reader) |
385 | free(reader); |
386 | return 0; |
387 | } |
388 | |
389 | |