Debian Codegraph @

addons/acodec/voc.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Allegro5 Creative Voice Audio Reader.
12 *
13 * Loosely based on A4 voc loader and tightly based on specs of the soundblaster
14 * hardware programming manual:
15 * <http://pdos.csail.mit.edu/6.828/2007/readings/hardware/SoundBlaster.pdf#1254>
16 * also specs are available at
17 * <http://sox.sourceforge.net/AudioFormats-11.html> section 11.5
18 *
19 * List of VOC audio codecs supported:
20 * supported 0x0000 8-bit unsigned PCM
21 * 0x0001 Creative 8-bit to 4-bit ADPCM *HW implemented on the SB
22 * 0x0002 Creative 8-bit to 3-bit ADPCM *HW implemented on the SB
23 * 0x0003 Creative 8-bit to 2-bit ADPCM *HW implemented on the SB
24 * supported 0x0004 16-bit signed PCM
25 *
26 * these are unsupported and present only in VOC files version 1.20 and above
27 * 0x0006 CCITT a-Law * not really used
28 * 0x0007 CCITT u-Law * not really used
29 * 0x0200 Creative 16-bit to 4-bit ADPCM *HW implemented on the SB
30 *
31 *
32 * author: pkrcel (aka Andrea Provasi) <[email protected]>
33 *
34 * Revisions:
35 * 2015-01-03 Source derived from previous sndfile implementation
36 * cleaned up and put decoder into voc.c
37 * 2015-01-08 Clean up ISO C90 related warnings and tried to get
38 * a consistent code style and removed some comments.
39 * 2015-01-15 Corrected style and enriched header.
40 */
41
42
43 #include "allegro5/allegro_audio.h"
44 #include "allegro5/internal/aintern_audio.h"
45 #include "acodec.h"
46 #include "helper.h"
47
48 ALLEGRO_DEBUG_CHANNEL("voc")
49
50 typedef struct AL_VOC_DATA AL_VOC_DATA;
51
52 struct AL_VOC_DATA {
53 ALLEGRO_FILE *file;
54 size_t datapos;
55 int samplerate;
56 short bits; /* 8 (unsigned char) or 16 (signed short) */
57 short channels; /* 1 (mono) or 2 (stereo) */
58 int sample_size; /* channels * bits/8 */
59 int samples; /* # of samples. size = samples * sample_size */
60 };
61
62 /*
63 * Fills in a VOC data header with information obtained by opening an
64 * caller-provided ALLEGRO_FILE.
65 * The datapos index will be the first data byte of the first data block which
66 * contains ACTUAL data.
67 */
68
69 #define READNBYTES(f, data, n, retv) \
70 do { \
71 if (al_fread(f, &data, n) != n) { \
72 ALLEGRO_WARN("voc_open: Bad Number of bytes read in last operation"); \
73 return retv; \
74 } \
75 } while(0)
76
77
78 static AL_VOC_DATA *voc_open(ALLEGRO_FILE *fp)
79 {
80 AL_VOC_DATA *vocdata;
81 char hdrbuf[0x16];
82 size_t readcount = 0;
83
84 uint8_t blocktype = 0;
85 uint8_t x = 0;
86
87 uint16_t timeconstant = 0;
88 uint16_t format = 0; // can be 16-bit in Blocktype 9 (voc version > 1.20
89 uint16_t vocversion = 0;
90 uint16_t checkver = 0; // must be 1's complement of vocversion + 0x1234
91
92 uint32_t blocklength = 0; //length is stored in 3 bites LE, gd byteorder.
93
94 ASSERT(fp);
95
96 /* init VOC data */
97 vocdata = al_malloc(sizeof(AL_VOC_DATA));
98 memset(vocdata, 0, sizeof(*vocdata));
99 memset(hdrbuf, 0, sizeof(hdrbuf));
100 vocdata->file = fp;
101
102 /* Begin checking the Header info */
103 readcount = al_fread(fp, hdrbuf, 0x16);
104 if (readcount != 0x16 /*short header*/
105 || memcmp(hdrbuf, "Creative Voice File\x1A", 0x14) != 0 /*wrong id */
106 || memcmp(hdrbuf+0x14, "\x1A\x00", 0x2) != 0) { /*wrong offset */
107 ALLEGRO_ERROR("voc_open: File does not appear to be a valid VOC file");
108 return NULL;
109 }
110
111 READNBYTES(fp, vocversion, 2, NULL);
112 if (vocversion != 0x10A && vocversion != 0x114) { // known ver 1.10 -1.20
113 ALLEGRO_ERROR("voc_open: File is of unknown version");
114 return NULL;
115 }
116 /* checksum version check */
117 READNBYTES(fp, checkver, 2, NULL);
118 if (checkver != ~vocversion + 0x1234) {
119 ALLEGRO_ERROR("voc_open: Bad VOC Version Identification Number");
120 return NULL;
121 }
122 /*
123 * We're at the first datablock, we shall check type and set all the relevant
124 * info in the vocdata structure, including finally the datapos index to the
125 * first valid data byte.
126 */
127 READNBYTES(fp, blocktype, 1, NULL);
128 READNBYTES(fp, blocklength, 2, NULL);
129 READNBYTES(fp, x, 1, NULL);
130 blocklength += x<<16;
131 switch (blocktype) {
132 case 1:
133 /* blocktype 1 is the most basic header with 1byte format, time
134 * constant and length equal to (datalength + 2).
135 */
136 blocklength -= 2;
137 READNBYTES(fp, timeconstant, 1, NULL);
138 READNBYTES(fp, format, 1, NULL);
139 vocdata->bits = 8; /* only possible codec for Blocktype 1 */
140 vocdata->channels = 1; /* block 1 alone means MONO */
141 vocdata->samplerate = 1000000 / (256 - timeconstant);
142 vocdata->sample_size = 1; /* or better: 1 * 8 / 8 */
143 /*
144 * Expected number of samples is at LEAST what is in this block.
145 * IIF lentgh 0xFFF there will be a following blocktype 2.
146 * We will deal with this later in load_voc.
147 */
148 vocdata->samples = blocklength / vocdata->sample_size;
149 vocdata->datapos = al_ftell(fp);
150 break;
151 case 8:
152 /* Blocktype 8 is enhanced data block (mainly for Stereo samples I
153 * guess) that precedes a Blocktype 1, of which the sound infor have
154 * to be ignored.
155 * We skip to the end of the following Blocktype 1 once we get all the
156 * required header info.
157 */
158 if (blocklength != 4) {
159 ALLEGRO_ERROR("voc_open: Got opening Blocktype 8 of wrong length");
160 return NULL;
161 }
162 READNBYTES(fp, timeconstant, 2, NULL);
163 READNBYTES(fp, format, 1, NULL);
164 READNBYTES(fp, vocdata->channels, 1, NULL);
165 vocdata->channels += 1; /* was 0 for mono, 1 for stereo */
166 vocdata->bits = 8; /* only possible codec for Blocktype 8 */
167 vocdata->samplerate = 1000000 / (256 - timeconstant);
168 vocdata->samplerate /= vocdata->channels;
169 vocdata->sample_size = vocdata->channels * vocdata->bits / 8;
170 /*
171 * Now following there is a blocktype 1 which tells us the length of
172 * the data block and all other info are discarded.
173 */
174 READNBYTES(fp, blocktype, 1, NULL);
175 if (blocktype != 1) {
176 ALLEGRO_ERROR("voc_open: Blocktype following type 8 is not 1");
177 return NULL;
178 }
179 READNBYTES(fp, blocklength, 2, NULL);
180 READNBYTES(fp, x, 1, NULL);
181 blocklength += x<<16;
182 blocklength -= 2;
183 READNBYTES(fp, x, 2, NULL);
184 vocdata->samples = blocklength / vocdata->sample_size;
185 vocdata->datapos = al_ftell(fp);
186 break;
187 case 9:
188 /*
189 * Blocktype 9 is available only for VOC version 1.20 and above.
190 * Deals with 16-bit codecs and stereo and is richier than blocktype 1
191 * or the BLocktype 8+1 combo
192 * Length is 12 bytes more than actual data.
193 */
194 blocklength -= 12;
195 READNBYTES(fp, vocdata->samplerate, 4, NULL); // actual samplerate
196 READNBYTES(fp, vocdata->bits, 1, NULL); // actual bits
197 // after compression
198 READNBYTES(fp, vocdata->channels, 1, NULL); // actual channels
199 READNBYTES(fp, format, 2, NULL);
200 if ((vocdata->bits != 8 && vocdata->bits != 16) ||
201 (format != 0 && format != 4)) {
202 ALLEGRO_ERROR("voc_open: unsupported CODEC in voc data");
203 return NULL;
204 }
205 READNBYTES(fp, x, 4, NULL); // just skip 4 reserved bytes
206 vocdata->datapos = al_ftell(fp);
207 break;
208 case 2: //
209 case 3: // these cases are just
210 case 4: // ignored in this span
211 case 5: // and wll not return a
212 case 6: // valid VOC data.
213 case 7: //
214 default:
215 ALLEGRO_ERROR("voc_open: opening Block is of unsupported type");
216 return NULL;
217 break;
218 }
219 return vocdata;
220 }
221
222 static void voc_close(AL_VOC_DATA *vocdata)
223 {
224 ASSERT(vocdata);
225
226 al_free(vocdata);
227 }
228
229
230 ALLEGRO_SAMPLE *_al_load_voc(const char *filename)
231 {
232 ALLEGRO_FILE *f;
233 ALLEGRO_SAMPLE *spl;
234 ASSERT(filename);
235
236 ALLEGRO_INFO("Loading VOC sample %s.\n", filename);
237 f = al_fopen(filename, "rb");
238 if (!f) {
239 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
240 return NULL;
241 }
242
243 spl = _al_load_voc_f(f);
244
245 al_fclose(f);
246
247 return spl;
248 }
249
250
251 ALLEGRO_SAMPLE *_al_load_voc_f(ALLEGRO_FILE *file)
252 {
253 AL_VOC_DATA *vocdata;
254 ALLEGRO_SAMPLE *sample = NULL;
255 size_t pos = 0; /* where to write in the buffer */
256 size_t read = 0; /*bytes read during last operation */
257 char* buffer;
258 size_t buffer_size = 0;
259
260 size_t bytestoread = 0;
261 bool endofvoc = false;
262
263 /*
264 * Open file and populate VOC DATA, then create a buffer for the number of
265 * samples of the frst block.
266 * Iterate on the following blocks till EOF or terminator block
267 */
268 vocdata = voc_open(file);
269 if (!vocdata) return NULL;
270
271 ALLEGRO_DEBUG("channels %d\n", vocdata->channels);
272 ALLEGRO_DEBUG("word_size %d\n", vocdata->sample_size);
273 ALLEGRO_DEBUG("rate %d\n", vocdata->samplerate);
274 ALLEGRO_DEBUG("first_block_samples %d\n", vocdata->samples);
275 ALLEGRO_DEBUG("first_block_size %d\n", vocdata->samples * vocdata->sample_size);
276
277 /*
278 * Let's allocate at least the first block's bytes;
279 */
280 buffer_size = (size_t)vocdata->samples * vocdata->sample_size;
281 buffer = al_malloc(buffer_size);
282 if (!buffer) {
283 return NULL;
284 }
285 /*
286 * We now need to iterate over data blocks till either we hit end of file
287 * or we find a terminator block.
288 */
289 bytestoread = buffer_size;
290 while(!endofvoc && !al_feof(vocdata->file)) {
291 uint32_t blocktype = 0;
292 uint32_t x = 0, len = 0;
293 read = al_fread(vocdata->file, buffer, bytestoread);
294 pos += read;
295 READNBYTES(vocdata->file, blocktype, 1, NULL); // read next block type
296 if (al_feof(vocdata->file)) break;
297 switch (blocktype) {
298 case 0:{ /* we found a terminator block */
299 endofvoc = true;
300 break;
301 }
302 case 2:{ /*we found a continuation block: unlikely but handled */
303 x = 0;
304 bytestoread = 0;
305 READNBYTES(vocdata->file, bytestoread, 2, NULL);
306 READNBYTES(vocdata->file, x, 1, NULL);
307 bytestoread += (size_t)x << 16;
308 /* increase subsequently storage */
309 buffer_size += bytestoread;
310 buffer = al_realloc(buffer, buffer_size);
311 break;
312 }
313 case 1: // we found a NEW data block starter, I assume this is wrong
314 case 8: // and let the so far read sample data correctly in the
315 case 9:{ // already allocated buffer.
316 endofvoc = true;
317 break;
318 }
319 case 3: /* we found a pause block */
320 case 4: /* we found a marker block */
321 case 5: /* we found an ASCII c-string block */
322 case 6: /* we found a repeat block */
323 case 7:{ /* we found an end repeat block */
324 /* all these blocks will be skipped */
325 unsigned int ii;
326 len = 0;
327 x = 0;
328 READNBYTES(vocdata->file, len, 2, NULL);
329 READNBYTES(vocdata->file, x, 1, NULL);
330 len += x<<16; // this is the length what's left to skip */
331 for (ii = 0; ii < len ; ++ii) {
332 al_fgetc(vocdata->file);
333 }
334 bytestoread = 0; //should let safely check for the next block */
335 break;
336 }
337 default:
338 break;
339 }
340 }
341
342 sample = al_create_sample(buffer, pos, vocdata->samplerate,
343 _al_word_size_to_depth_conf(vocdata->sample_size),
344 _al_count_to_channel_conf(vocdata->channels),
345 true);
346 if (!sample)
347 al_free(buffer);
348
349 voc_close(vocdata);
350
351 return sample;
352 }
353
354 /*
355 * So far a stream implementation is not provided, since it is deemed unlikely
356 * that this format will ever be used as such.
357 */
358
359
360 bool _al_identify_voc(ALLEGRO_FILE *f)
361 {
362 uint8_t x[22];
363 if (al_fread(f, x, 22) < 22)
364 return false;
365 if (memcmp(x, "Creative Voice File\x1A\x1A\x00", 22) == 0)
366 return true;
367 return false;
368 }