]>
Commit | Line | Data |
---|---|---|
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" | |
48e2f01c | 18 | |
19 | #define RIFF_FOURCC(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24)) | |
20 | ||
29a8f73f | 21 | #define ENSURE(expr) \ |
48e2f01c | 22 | do { \ |
29a8f73f | 23 | if (!(expr)) goto FAIL;\ |
48e2f01c | 24 | } while (0) |
25 | ||
26 | struct wav_reader_t { | |
2d744bd5 | 27 | pcm_reader_vtbl_t *vtbl; |
48e2f01c | 28 | pcm_sample_description_t sample_format; |
29 | int64_t length; | |
30 | int64_t position; | |
68879398 | 31 | int32_t data_offset; |
48e2f01c | 32 | int ignore_length; |
29a8f73f | 33 | pcm_io_context_t io; |
48e2f01c | 34 | }; |
35 | ||
36 | static const uint8_t WAV_GUID_PCM[] = { | |
37 | 1, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71 | |
38 | }; | |
39 | static const uint8_t WAV_GUID_FLOAT[] = { | |
40 | 3, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71 | |
41 | }; | |
42 | ||
2d744bd5 | 43 | static const pcm_sample_description_t *wav_get_format(pcm_reader_t *reader) |
48e2f01c | 44 | { |
2d744bd5 | 45 | return &((wav_reader_t *)reader)->sample_format; |
48e2f01c | 46 | } |
47 | ||
2d744bd5 | 48 | static int64_t wav_get_length(pcm_reader_t *reader) |
48e2f01c | 49 | { |
2d744bd5 | 50 | return ((wav_reader_t *)reader)->length; |
48e2f01c | 51 | } |
52 | ||
2d744bd5 | 53 | static int64_t wav_get_position(pcm_reader_t *reader) |
48e2f01c | 54 | { |
2d744bd5 | 55 | return ((wav_reader_t *)reader)->position; |
48e2f01c | 56 | } |
57 | ||
2d744bd5 | 58 | static void wav_teardown(pcm_reader_t **reader) |
48e2f01c | 59 | { |
60 | free(*reader); | |
61 | *reader = 0; | |
62 | } | |
63 | ||
48e2f01c | 64 | static |
65 | uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size) | |
66 | { | |
67 | uint32_t fcc; | |
29a8f73f | 68 | return (pcm_scanl(&reader->io, "LL", &fcc, chunk_size) == 2) ? fcc : 0; |
48e2f01c | 69 | } |
70 | ||
2d744bd5 | 71 | static |
72 | int wav_read_frames(pcm_reader_t *preader, void *buffer, unsigned nframes) | |
48e2f01c | 73 | { |
74 | int rc; | |
75 | unsigned nbytes; | |
2d744bd5 | 76 | wav_reader_t *reader = (wav_reader_t *)preader; |
48e2f01c | 77 | |
78 | if (!reader->ignore_length && nframes > reader->length - reader->position) | |
79 | nframes = reader->length - reader->position; | |
80 | nbytes = nframes * reader->sample_format.bytes_per_frame; | |
81 | if (nbytes) { | |
29a8f73f | 82 | if ((rc = pcm_read(&reader->io, buffer, nbytes)) < 0) |
48e2f01c | 83 | return -1; |
84 | nframes = rc / reader->sample_format.bytes_per_frame; | |
85 | reader->position += nframes; | |
86 | } | |
87 | return nframes; | |
88 | } | |
89 | ||
90 | static | |
91 | int riff_ds64(wav_reader_t *reader, int64_t *length) | |
92 | { | |
93 | uint32_t fcc, chunk_size, table_size; | |
94 | uint64_t riff_size, sample_count; | |
95 | ||
96 | fcc = riff_next_chunk(reader, &chunk_size); | |
29a8f73f | 97 | ENSURE(fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28); |
98 | TRY_IO(pcm_scanl(&reader->io, "QQQL", | |
48e2f01c | 99 | &riff_size, length, &sample_count, &table_size) != 4); |
29a8f73f | 100 | TRY_IO(pcm_skip(&reader->io, (chunk_size - 27) & ~1)); |
68879398 | 101 | reader->data_offset += (chunk_size + 9) & ~1; |
48e2f01c | 102 | FAIL: |
103 | return -1; | |
104 | } | |
105 | ||
106 | static | |
107 | int wav_fmt(wav_reader_t *reader, uint32_t size) | |
108 | { | |
109 | uint16_t wFormatTag, nChannels, nBlockAlign, wBitsPerSample, cbSize; | |
110 | uint32_t nSamplesPerSec, nAvgBytesPerSec, dwChannelMask = 0; | |
111 | uint16_t wValidBitsPerSample; | |
112 | uint8_t guid[16]; | |
113 | int is_float = 0; | |
114 | ||
29a8f73f | 115 | ENSURE(size >= 16); |
116 | TRY_IO(pcm_scanl(&reader->io, "SSLLSS", &wFormatTag, &nChannels, | |
48e2f01c | 117 | &nSamplesPerSec, &nAvgBytesPerSec, &nBlockAlign, |
118 | &wBitsPerSample) != 6); | |
119 | wValidBitsPerSample = wBitsPerSample; | |
120 | ||
29a8f73f | 121 | ENSURE(wFormatTag == 1 || wFormatTag == 3 || wFormatTag == 0xfffe); |
122 | ENSURE(nChannels && nSamplesPerSec && nAvgBytesPerSec && | |
123 | nBlockAlign && wBitsPerSample && !(wBitsPerSample & 7) && | |
124 | nBlockAlign == nChannels * wBitsPerSample / 8); | |
125 | ||
48e2f01c | 126 | if (wFormatTag == 3) |
127 | is_float = 1; | |
128 | ||
129 | if (wFormatTag != 0xfffe) | |
29a8f73f | 130 | TRY_IO(pcm_skip(&reader->io, (size - 15) & ~1)); |
48e2f01c | 131 | else { |
29a8f73f | 132 | ENSURE(size >= 40); |
133 | TRY_IO(pcm_scanl(&reader->io, "SSL", | |
48e2f01c | 134 | &cbSize, &wValidBitsPerSample, &dwChannelMask) != 3); |
29a8f73f | 135 | TRY_IO(pcm_read(&reader->io, guid, 16) != 16); |
48e2f01c | 136 | |
137 | if (memcmp(guid, WAV_GUID_FLOAT, 16) == 0) | |
138 | is_float = 1; | |
29a8f73f | 139 | else if (memcmp(guid, WAV_GUID_PCM, 16) != 0) |
48e2f01c | 140 | goto FAIL; |
29a8f73f | 141 | ENSURE(wValidBitsPerSample && wValidBitsPerSample <= wBitsPerSample); |
142 | TRY_IO(pcm_skip(&reader->io, (size - 39) & ~1)); | |
48e2f01c | 143 | } |
144 | reader->sample_format.sample_rate = nSamplesPerSec; | |
145 | reader->sample_format.bits_per_channel = wValidBitsPerSample; | |
146 | reader->sample_format.bytes_per_frame = nBlockAlign; | |
147 | reader->sample_format.channels_per_frame = nChannels; | |
148 | reader->sample_format.channel_mask = dwChannelMask; | |
149 | if (is_float) | |
150 | reader->sample_format.sample_type = PCM_TYPE_FLOAT; | |
151 | else if (wBitsPerSample == 8) | |
152 | reader->sample_format.sample_type = PCM_TYPE_UINT; | |
153 | else | |
154 | reader->sample_format.sample_type = PCM_TYPE_SINT; | |
155 | return 0; | |
156 | FAIL: | |
157 | return -1; | |
158 | } | |
159 | ||
160 | static | |
161 | int wav_parse(wav_reader_t *reader, int64_t *data_length) | |
162 | { | |
163 | uint32_t container, fcc, chunk_size; | |
164 | ||
165 | *data_length = 0; | |
166 | container = riff_next_chunk(reader, &chunk_size); | |
29a8f73f | 167 | ENSURE(container == RIFF_FOURCC('R','I','F','F') || |
168 | container == RIFF_FOURCC('R','F','6','4')); | |
169 | TRY_IO(pcm_read32le(&reader->io, &fcc)); | |
170 | ENSURE(fcc == RIFF_FOURCC('W','A','V','E')); | |
68879398 | 171 | reader->data_offset = 12; |
172 | ||
48e2f01c | 173 | if (container == RIFF_FOURCC('R','F','6','4')) |
174 | riff_ds64(reader, data_length); | |
175 | while ((fcc = riff_next_chunk(reader, &chunk_size)) != 0) { | |
176 | if (fcc == RIFF_FOURCC('f','m','t',' ')) { | |
177 | if (wav_fmt(reader, chunk_size) < 0) | |
178 | goto FAIL; | |
179 | } else if (fcc == RIFF_FOURCC('d','a','t','a')) { | |
180 | if (container == RIFF_FOURCC('R','I','F','F')) | |
181 | *data_length = chunk_size; | |
68879398 | 182 | reader->data_offset += 8; |
48e2f01c | 183 | break; |
68879398 | 184 | } else { |
29a8f73f | 185 | TRY_IO(pcm_skip(&reader->io, (chunk_size + 1) & ~1)); |
68879398 | 186 | } |
187 | reader->data_offset += (chunk_size + 9) & ~1; | |
48e2f01c | 188 | } |
189 | if (fcc == RIFF_FOURCC('d','a','t','a')) | |
190 | return 0; | |
191 | FAIL: | |
192 | return -1; | |
193 | } | |
194 | ||
2d744bd5 | 195 | static pcm_reader_vtbl_t wav_vtable = { |
196 | wav_get_format, | |
197 | wav_get_length, | |
198 | wav_get_position, | |
199 | wav_read_frames, | |
200 | wav_teardown | |
201 | }; | |
202 | ||
29a8f73f | 203 | pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length) |
48e2f01c | 204 | { |
68879398 | 205 | wav_reader_t *reader = 0; |
48e2f01c | 206 | int64_t data_length; |
68879398 | 207 | unsigned bpf; |
48e2f01c | 208 | |
209 | if ((reader = calloc(1, sizeof(wav_reader_t))) == 0) | |
210 | return 0; | |
29a8f73f | 211 | memcpy(&reader->io, io, sizeof(pcm_io_context_t)); |
48e2f01c | 212 | reader->ignore_length = ignore_length; |
213 | if (wav_parse(reader, &data_length) < 0) { | |
214 | free(reader); | |
215 | return 0; | |
216 | } | |
68879398 | 217 | bpf = reader->sample_format.bytes_per_frame; |
218 | if (ignore_length || !data_length || data_length % bpf) | |
48e2f01c | 219 | reader->length = INT64_MAX; |
220 | else | |
68879398 | 221 | reader->length = data_length / bpf; |
222 | ||
29a8f73f | 223 | if (reader->length == INT64_MAX) { |
224 | if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) { | |
225 | int64_t size = pcm_tell(&reader->io); | |
68879398 | 226 | if (size > 0) |
227 | reader->length = (size - reader->data_offset) / bpf; | |
29a8f73f | 228 | pcm_seek(&reader->io, reader->data_offset, SEEK_SET); |
68879398 | 229 | } |
230 | } | |
2d744bd5 | 231 | reader->vtbl = &wav_vtable; |
232 | return (pcm_reader_t *)reader; | |
48e2f01c | 233 | } |
68543176 | 234 | |
29a8f73f | 235 | pcm_reader_t *raw_open(pcm_io_context_t *io, |
68543176 | 236 | const pcm_sample_description_t *desc) |
237 | { | |
238 | wav_reader_t *reader = 0; | |
239 | ||
240 | if ((reader = calloc(1, sizeof(wav_reader_t))) == 0) | |
241 | return 0; | |
29a8f73f | 242 | memcpy(&reader->io, io, sizeof(pcm_io_context_t)); |
68543176 | 243 | memcpy(&reader->sample_format, desc, sizeof(pcm_sample_description_t)); |
29a8f73f | 244 | if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) { |
245 | int64_t size = pcm_tell(&reader->io); | |
246 | if (size > 0) | |
247 | reader->length = size / reader->sample_format.bytes_per_frame; | |
248 | pcm_seek(&reader->io, 0, SEEK_SET); | |
68543176 | 249 | } else |
250 | reader->length = INT64_MAX; | |
2d744bd5 | 251 | reader->vtbl = &wav_vtable; |
252 | return (pcm_reader_t *)reader; | |
68543176 | 253 | } |