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