initial commit
[fdkaac.git] / src / wav_reader.c
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
94 int riff_skip(wav_reader_t *reader, uint32_t count)
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
117 static
118 int riff_read16(wav_reader_t *reader, uint16_t *value)
119 {
120 TRY_IO(riff_read(reader, value, 2) != 2);
121 *value = m4af_ltoh16(*value);
122 return 0;
123 FAIL:
124 return -1;
125 }
126
127 static
128 int riff_read32(wav_reader_t *reader, uint32_t *value)
129 {
130 TRY_IO(riff_read(reader, value, 4) != 4);
131 *value = m4af_ltoh32(*value);
132 return 0;
133 FAIL:
134 return -1;
135 }
136
137 static
138 int riff_read64(wav_reader_t *reader, uint64_t *value)
139 {
140 TRY_IO(riff_read(reader, value, 8) != 8);
141 *value = m4af_ltoh64(*value);
142 return 0;
143 FAIL:
144 return -1;
145 }
146
147 static
148 int riff_scan(wav_reader_t *reader, const char *fmt, ...)
149 {
150 int c, count = 0;
151 va_list ap;
152
153 va_start(ap, fmt);
154 while ((c = *fmt++)) {
155 switch (c) {
156 case 'S':
157 TRY_IO(riff_read16(reader, va_arg(ap, uint16_t*)));
158 ++count;
159 break;
160 case 'L':
161 TRY_IO(riff_read32(reader, va_arg(ap, uint32_t*)));
162 ++count;
163 break;
164 case 'Q':
165 TRY_IO(riff_read64(reader, va_arg(ap, uint64_t*)));
166 ++count;
167 break;
168 }
169 }
170 FAIL:
171 va_end(ap);
172 return count;
173 }
174
175 static
176 uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size)
177 {
178 uint32_t fcc;
179 if (riff_scan(reader, "LL", &fcc, chunk_size) == 2)
180 return fcc;
181 return 0;
182 }
183
184 int wav_read_frames(wav_reader_t *reader, void *buffer, unsigned nframes)
185 {
186 int rc;
187 unsigned nbytes;
188
189 if (!reader->ignore_length && nframes > reader->length - reader->position)
190 nframes = reader->length - reader->position;
191 nbytes = nframes * reader->sample_format.bytes_per_frame;
192 if (nbytes) {
193 if ((rc = riff_read(reader, buffer, nbytes)) < 0)
194 return -1;
195 nframes = rc / reader->sample_format.bytes_per_frame;
196 reader->position += nframes;
197 }
198 return nframes;
199 }
200
201 static
202 int riff_ds64(wav_reader_t *reader, int64_t *length)
203 {
204 uint32_t fcc, chunk_size, table_size;
205 uint64_t riff_size, sample_count;
206
207 fcc = riff_next_chunk(reader, &chunk_size);
208 ASSERT_FORMAT(reader,
209 fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28);
210 TRY_IO(riff_scan(reader, "QQQL",
211 &riff_size, length, &sample_count, &table_size) != 4);
212 if (table_size == 0)
213 return 0;
214 reader->last_error = WAV_UNSUPPORTED_FORMAT;
215 FAIL:
216 return -1;
217 }
218
219 static
220 int wav_fmt(wav_reader_t *reader, uint32_t size)
221 {
222 uint16_t wFormatTag, nChannels, nBlockAlign, wBitsPerSample, cbSize;
223 uint32_t nSamplesPerSec, nAvgBytesPerSec, dwChannelMask = 0;
224 uint16_t wValidBitsPerSample;
225 uint8_t guid[16];
226 int is_float = 0;
227
228 ASSERT_FORMAT(reader, size >= 16);
229 TRY_IO(riff_scan(reader, "SSLLSS", &wFormatTag, &nChannels,
230 &nSamplesPerSec, &nAvgBytesPerSec, &nBlockAlign,
231 &wBitsPerSample) != 6);
232 wValidBitsPerSample = wBitsPerSample;
233
234 if (wFormatTag != 1 && wFormatTag != 3 && wFormatTag != 0xfffe) {
235 reader->last_error = WAV_UNSUPPORTED_FORMAT;
236 goto FAIL;
237 }
238 ASSERT_FORMAT(reader,
239 nChannels && nSamplesPerSec && nAvgBytesPerSec &&
240 nBlockAlign && wBitsPerSample && !(wBitsPerSample & 7) &&
241 nBlockAlign == nChannels * wBitsPerSample / 8);
242 if (wFormatTag == 3)
243 is_float = 1;
244
245 if (wFormatTag != 0xfffe)
246 TRY_IO(riff_skip(reader, (size - 15) & ~1));
247 else {
248 ASSERT_FORMAT(reader, size >= 40);
249 TRY_IO(riff_scan(reader, "SSL",
250 &cbSize, &wValidBitsPerSample, &dwChannelMask) != 3);
251 TRY_IO(riff_read(reader, guid, 16) != 16);
252
253 if (memcmp(guid, WAV_GUID_FLOAT, 16) == 0)
254 is_float = 1;
255 else if (memcmp(guid, WAV_GUID_PCM, 16) != 0) {
256 reader->last_error = WAV_UNSUPPORTED_FORMAT;
257 goto FAIL;
258 }
259 ASSERT_FORMAT(reader,
260 wValidBitsPerSample &&
261 wValidBitsPerSample <= wBitsPerSample);
262 TRY_IO(riff_skip(reader, (size - 39) & ~1));
263 }
264 reader->sample_format.sample_rate = nSamplesPerSec;
265 reader->sample_format.bits_per_channel = wValidBitsPerSample;
266 reader->sample_format.bytes_per_frame = nBlockAlign;
267 reader->sample_format.channels_per_frame = nChannels;
268 reader->sample_format.channel_mask = dwChannelMask;
269 if (is_float)
270 reader->sample_format.sample_type = PCM_TYPE_FLOAT;
271 else if (wBitsPerSample == 8)
272 reader->sample_format.sample_type = PCM_TYPE_UINT;
273 else
274 reader->sample_format.sample_type = PCM_TYPE_SINT;
275 return 0;
276 FAIL:
277 return -1;
278 }
279
280 static
281 int wav_parse(wav_reader_t *reader, int64_t *data_length)
282 {
283 uint32_t container, fcc, chunk_size;
284
285 *data_length = 0;
286 container = riff_next_chunk(reader, &chunk_size);
287 if (container != RIFF_FOURCC('R','I','F','F') &&
288 container != RIFF_FOURCC('R','F','6','4'))
289 goto FAIL;
290 TRY_IO(riff_read32(reader, &fcc));
291 if (fcc != RIFF_FOURCC('W','A','V','E'))
292 goto FAIL;
293 if (container == RIFF_FOURCC('R','F','6','4'))
294 riff_ds64(reader, data_length);
295 while ((fcc = riff_next_chunk(reader, &chunk_size)) != 0) {
296 if (fcc == RIFF_FOURCC('f','m','t',' ')) {
297 if (wav_fmt(reader, chunk_size) < 0)
298 goto FAIL;
299 } else if (fcc == RIFF_FOURCC('d','a','t','a')) {
300 if (container == RIFF_FOURCC('R','I','F','F'))
301 *data_length = chunk_size;
302 break;
303 } else
304 TRY_IO(riff_skip(reader, (chunk_size + 1) & ~1));
305 }
306 if (fcc == RIFF_FOURCC('d','a','t','a'))
307 return 0;
308 FAIL:
309 return -1;
310 }
311
312 wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
313 int ignore_length)
314 {
315 wav_reader_t *reader;
316 int64_t data_length;
317
318 if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
319 return 0;
320 memcpy(&reader->io, io_ctx, sizeof(wav_io_context_t));
321 reader->io_cookie = io_cookie;
322 reader->ignore_length = ignore_length;
323 if (wav_parse(reader, &data_length) < 0) {
324 free(reader);
325 return 0;
326 }
327 if (ignore_length || !data_length ||
328 data_length % reader->sample_format.bytes_per_frame != 0)
329 reader->length = INT64_MAX;
330 else
331 reader->length = data_length / reader->sample_format.bytes_per_frame;
332 return reader;
333 }
This page took 0.032819 seconds and 4 git commands to generate.