]> iEval git - fdkaac.git/blame_incremental - src/wav_reader.c
refactor pcm reader framework
[fdkaac.git] / src / wav_reader.c
... / ...
CommitLineData
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 "pcm_reader.h"
18
19#define RIFF_FOURCC(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24))
20
21typedef 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
31static const uint8_t WAV_GUID_PCM[] = {
32 1, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71
33};
34static 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
38static const pcm_sample_description_t *wav_get_format(pcm_reader_t *reader)
39{
40 return &((wav_reader_t *)reader)->sample_format;
41}
42
43static int64_t wav_get_length(pcm_reader_t *reader)
44{
45 return ((wav_reader_t *)reader)->length;
46}
47
48static int64_t wav_get_position(pcm_reader_t *reader)
49{
50 return ((wav_reader_t *)reader)->position;
51}
52
53static void wav_teardown(pcm_reader_t **reader)
54{
55 free(*reader);
56 *reader = 0;
57}
58
59static
60uint32_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
66static
67int 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
85static
86int 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));
96FAIL:
97 return -1;
98}
99
100static
101int wav_fmt(wav_reader_t *reader, uint32_t size)
102{
103 uint16_t wFormatTag, nChannels, nBlockAlign, wBitsPerSample, cbSize;
104 uint32_t nSamplesPerSec, nAvgBytesPerSec, dwChannelMask = 0;
105 uint16_t wValidBitsPerSample;
106 uint8_t guid[16];
107 int is_float = 0;
108
109 ENSURE(size >= 16);
110 TRY_IO(pcm_scanl(&reader->io, "SSLLSS", &wFormatTag, &nChannels,
111 &nSamplesPerSec, &nAvgBytesPerSec, &nBlockAlign,
112 &wBitsPerSample) != 6);
113 wValidBitsPerSample = wBitsPerSample;
114
115 ENSURE(wFormatTag == 1 || wFormatTag == 3 || wFormatTag == 0xfffe);
116 ENSURE(nChannels && nSamplesPerSec && nAvgBytesPerSec &&
117 nBlockAlign && wBitsPerSample && !(wBitsPerSample & 7) &&
118 nBlockAlign == nChannels * wBitsPerSample / 8);
119
120 if (wFormatTag == 3)
121 is_float = 1;
122
123 if (wFormatTag != 0xfffe)
124 TRY_IO(pcm_skip(&reader->io, (size - 15) & ~1));
125 else {
126 ENSURE(size >= 40);
127 TRY_IO(pcm_scanl(&reader->io, "SSL",
128 &cbSize, &wValidBitsPerSample, &dwChannelMask) != 3);
129 TRY_IO(pcm_read(&reader->io, guid, 16) != 16);
130
131 if (memcmp(guid, WAV_GUID_FLOAT, 16) == 0)
132 is_float = 1;
133 else if (memcmp(guid, WAV_GUID_PCM, 16) != 0)
134 goto FAIL;
135 ENSURE(wValidBitsPerSample && wValidBitsPerSample <= wBitsPerSample);
136 TRY_IO(pcm_skip(&reader->io, (size - 39) & ~1));
137 }
138 reader->sample_format.sample_rate = nSamplesPerSec;
139 reader->sample_format.bits_per_channel = wValidBitsPerSample;
140 reader->sample_format.bytes_per_frame = nBlockAlign;
141 reader->sample_format.channels_per_frame = nChannels;
142 reader->sample_format.channel_mask = dwChannelMask;
143 if (is_float)
144 reader->sample_format.sample_type = PCM_TYPE_FLOAT;
145 else if (wBitsPerSample == 8)
146 reader->sample_format.sample_type = PCM_TYPE_UINT;
147 else
148 reader->sample_format.sample_type = PCM_TYPE_SINT;
149 return 0;
150FAIL:
151 return -1;
152}
153
154static
155int wav_parse(wav_reader_t *reader, int64_t *data_length)
156{
157 uint32_t container, fcc, chunk_size;
158
159 *data_length = 0;
160 container = riff_next_chunk(reader, &chunk_size);
161 ENSURE(container == RIFF_FOURCC('R','I','F','F') ||
162 container == RIFF_FOURCC('R','F','6','4'));
163 TRY_IO(pcm_read32le(&reader->io, &fcc));
164 ENSURE(fcc == RIFF_FOURCC('W','A','V','E'));
165
166 if (container == RIFF_FOURCC('R','F','6','4'))
167 riff_ds64(reader, data_length);
168 while ((fcc = riff_next_chunk(reader, &chunk_size)) != 0) {
169 if (fcc == RIFF_FOURCC('f','m','t',' ')) {
170 if (wav_fmt(reader, chunk_size) < 0)
171 goto FAIL;
172 } else if (fcc == RIFF_FOURCC('d','a','t','a')) {
173 if (container == RIFF_FOURCC('R','I','F','F'))
174 *data_length = chunk_size;
175 reader->data_offset = pcm_tell(&reader->io);
176 break;
177 } else {
178 TRY_IO(pcm_skip(&reader->io, (chunk_size + 1) & ~1));
179 }
180 }
181 if (fcc == RIFF_FOURCC('d','a','t','a'))
182 return 0;
183FAIL:
184 return -1;
185}
186
187static pcm_reader_vtbl_t wav_vtable = {
188 wav_get_format,
189 wav_get_length,
190 wav_get_position,
191 wav_read_frames,
192 wav_teardown
193};
194
195pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length)
196{
197 wav_reader_t *reader = 0;
198 int64_t data_length;
199 unsigned bpf;
200
201 if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
202 return 0;
203 memcpy(&reader->io, io, sizeof(pcm_io_context_t));
204 reader->ignore_length = ignore_length;
205 if (wav_parse(reader, &data_length) < 0) {
206 free(reader);
207 return 0;
208 }
209 bpf = reader->sample_format.bytes_per_frame;
210 if (ignore_length || !data_length || data_length % bpf)
211 reader->length = INT64_MAX;
212 else
213 reader->length = data_length / bpf;
214
215 if (reader->length == INT64_MAX) {
216 if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) {
217 int64_t size = pcm_tell(&reader->io);
218 if (size > 0)
219 reader->length = (size - reader->data_offset) / bpf;
220 pcm_seek(&reader->io, reader->data_offset, SEEK_SET);
221 }
222 }
223 reader->vtbl = &wav_vtable;
224 return (pcm_reader_t *)reader;
225}
226
227pcm_reader_t *raw_open(pcm_io_context_t *io,
228 const pcm_sample_description_t *desc)
229{
230 wav_reader_t *reader = 0;
231
232 if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
233 return 0;
234 memcpy(&reader->io, io, sizeof(pcm_io_context_t));
235 memcpy(&reader->sample_format, desc, sizeof(pcm_sample_description_t));
236 if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) {
237 int64_t size = pcm_tell(&reader->io);
238 if (size > 0)
239 reader->length = size / reader->sample_format.bytes_per_frame;
240 pcm_seek(&reader->io, 0, SEEK_SET);
241 } else
242 reader->length = INT64_MAX;
243 reader->vtbl = &wav_vtable;
244 return (pcm_reader_t *)reader;
245}
This page took 0.018636 seconds and 4 git commands to generate.