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