summaryrefslogtreecommitdiff
path: root/src/libs/sound/decoders/aiffaud.c
blob: 102a78e986b478688dec778ebcc20aee9e7bf3be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* Portions (C) Serge van den Boom (svdb at stack.nl) */
/* Portions (C) Alex Volkov (codepro at usa.net) */

/* AIFF decoder (.aif)
 *
 * Doesn't work on *all* aiff files in general, only 8/16 PCM and
 * 16-bit AIFF-C SDX2-compressed.
 */

#include <stdio.h>
#include <stdlib.h>
		// for abs()
#include <errno.h>
#ifndef _WIN32_WCE
#	include <memory.h>
#endif
#include <string.h>
#include "port.h"
#include "types.h"
#include "libs/uio.h"
#include "endian_uqm.h"
#include "libs/log.h"
#include "aiffaud.h"

typedef uint32 aiff_ID;

#define aiff_MAKE_ID(x1, x2, x3, x4) \
		(((x1) << 24) | ((x2) << 16) | ((x3) << 8) | (x4))

#define aiff_FormID         aiff_MAKE_ID('F', 'O', 'R', 'M')
#define aiff_FormVersionID  aiff_MAKE_ID('F', 'V', 'E', 'R')
#define aiff_CommonID       aiff_MAKE_ID('C', 'O', 'M', 'M')
#define aiff_SoundDataID    aiff_MAKE_ID('S', 'S', 'N', 'D')

#define aiff_FormTypeAIFF   aiff_MAKE_ID('A', 'I', 'F', 'F')
#define aiff_FormTypeAIFC   aiff_MAKE_ID('A', 'I', 'F', 'C')

#define aiff_CompressionTypeSDX2  aiff_MAKE_ID('S', 'D', 'X', '2')


typedef struct
{
	aiff_ID id;
	uint32 size;
} aiff_ChunkHeader;

#define AIFF_CHUNK_HDR_SIZE (4+4)

typedef struct
{
	aiff_ChunkHeader chunk;
	aiff_ID type;
} aiff_FileHeader;

typedef struct
{
	uint32 version;  /* format version, in Mac format */
} aiff_FormatVersionChunk;

typedef struct
{
	uint16 channels;       /* number of channels */
	uint32 sampleFrames;   /* number of sample frames */
	uint16 sampleSize;     /* number of bits per sample */
	sint32 sampleRate;     /* number of frames per second */
			/* this is actually stored as IEEE-754 80bit in files */
} aiff_CommonChunk;

#define AIFF_COMM_SIZE (2+4+2+10)

typedef struct
{
	uint16 channels;      /* number of channels */
	uint32 sampleFrames;  /* number of sample frames */
	uint16 sampleSize;    /* number of bits per sample */
	sint32 sampleRate;    /* number of frames per second */
	aiff_ID extTypeID;    /* compression type ID */
	char extName[32];     /* compression type name */
} aiff_ExtCommonChunk;

#define AIFF_EXT_COMM_SIZE (AIFF_COMM_SIZE+4)

typedef struct
{
	uint32 offset;     /* offset to sound data */
	uint32 blockSize;  /* size of alignment blocks */
} aiff_SoundDataChunk;

#define AIFF_SSND_SIZE (4+4)
 
 
#define THIS_PTR TFB_SoundDecoder* This

static const char* aifa_GetName (void);
static bool aifa_InitModule (int flags, const TFB_DecoderFormats*);
static void aifa_TermModule (void);
static uint32 aifa_GetStructSize (void);
static int aifa_GetError (THIS_PTR);
static bool aifa_Init (THIS_PTR);
static void aifa_Term (THIS_PTR);
static bool aifa_Open (THIS_PTR, uio_DirHandle *dir, const char *filename);
static void aifa_Close (THIS_PTR);
static int aifa_Decode (THIS_PTR, void* buf, sint32 bufsize);
static uint32 aifa_Seek (THIS_PTR, uint32 pcm_pos);
static uint32 aifa_GetFrame (THIS_PTR);

TFB_SoundDecoderFuncs aifa_DecoderVtbl = 
{
	aifa_GetName,
	aifa_InitModule,
	aifa_TermModule,
	aifa_GetStructSize,
	aifa_GetError,
	aifa_Init,
	aifa_Term,
	aifa_Open,
	aifa_Close,
	aifa_Decode,
	aifa_Seek,
	aifa_GetFrame,
};


typedef enum
{
	aifc_None,
	aifc_Sdx2,
} aiff_CompressionType;

#define MAX_CHANNELS 4

typedef struct tfb_wavesounddecoder
{
	// always the first member
	TFB_SoundDecoder decoder;

	// private
	sint32 last_error;
	uio_Stream *fp;
	aiff_ExtCommonChunk fmtHdr;
	aiff_CompressionType comp_type;
	unsigned bits_per_sample;
	unsigned block_align;
	unsigned file_block;
	uint32 data_ofs;
	uint32 data_size;
	uint32 max_pcm;
	uint32 cur_pcm;
	sint32 prev_val[MAX_CHANNELS];

} TFB_AiffSoundDecoder;

static const TFB_DecoderFormats* aifa_formats = NULL;

static int aifa_DecodePCM (TFB_AiffSoundDecoder*, void* buf, sint32 bufsize);
static int aifa_DecodeSDX2 (TFB_AiffSoundDecoder*, void* buf, sint32 bufsize);


static const char*
aifa_GetName (void)
{
	return "AIFF";
}

static bool
aifa_InitModule (int flags, const TFB_DecoderFormats* fmts)
{
	aifa_formats = fmts;
	return true;
	
	(void)flags;	// laugh at compiler warning
}

static void
aifa_TermModule (void)
{
	// no specific module term
}

static uint32
aifa_GetStructSize (void)
{
	return sizeof (TFB_AiffSoundDecoder);
}

static int
aifa_GetError (THIS_PTR)
{
	TFB_AiffSoundDecoder* aifa = (TFB_AiffSoundDecoder*) This;
	int ret = aifa->last_error;
	aifa->last_error = 0;
	return ret;
}

static bool
aifa_Init (THIS_PTR)
{
	//TFB_AiffSoundDecoder* aifa = (TFB_AiffSoundDecoder*) This;
	This->need_swap = !aifa_formats->want_big_endian;
	return true;
}

static void
aifa_Term (THIS_PTR)
{
	//TFB_AiffSoundDecoder* aifa = (TFB_AiffSoundDecoder*) This;
	aifa_Close (This); // ensure cleanup
}

static bool
read_be_16 (uio_Stream *fp, uint16 *v)
{
	if (!uio_fread (v, sizeof(*v), 1, fp))
		return false;
	*v = UQM_SwapBE16 (*v);
	return true;
}

static bool
read_be_32 (uio_Stream *fp, uint32 *v)
{
	if (!uio_fread (v, sizeof(*v), 1, fp))
		return false;
	*v = UQM_SwapBE32 (*v);
	return true;
}

// Read 80-bit IEEE 754 floating point number.
// We are only interested in values that we can work with,
//   so using an sint32 here is fine.
static bool
read_be_f80 (uio_Stream *fp, sint32 *v)
{
	int sign, exp;
	int shift;
	uint16 se;
	uint32 mant, mant_low;
	if (!read_be_16 (fp, &se) ||
		!read_be_32 (fp, &mant) || !read_be_32 (fp, &mant_low))
		return false;

	sign = (se >> 15) & 1;        // sign is the highest bit
	exp = (se & ((1 << 15) - 1)); // exponent is next highest 15 bits
#if 0 // XXX: 80bit IEEE 754 used in AIFF uses explicit mantissa MS bit
	// mantissa has an implied leading bit which is typically 1
	mant >>= 1;
	if (exp != 0)
		mant |= 0x80000000;
#endif
	mant >>= 1;            // we also need space for sign
	exp -= (1 << 14) - 1;  // exponent is biased by (2^(e-1) - 1)
	shift = exp - 31 + 1;  // mantissa is already 31 bits before decimal pt.
	if (shift > 0)
		mant = 0x7fffffff; // already too big
	else if (shift < 0)
		mant >>= -shift;

	*v = sign ? -(sint32)mant : (sint32)mant;

	return true;
}

static bool
aifa_readFileHeader (TFB_AiffSoundDecoder* aifa, aiff_FileHeader* hdr)
{
	if (!read_be_32 (aifa->fp, &hdr->chunk.id) ||
			!read_be_32 (aifa->fp, &hdr->chunk.size) ||
			!read_be_32 (aifa->fp, &hdr->type))
	{
		aifa->last_error = errno;
		return false;
	}
	return true;
}

static bool
aifa_readChunkHeader (TFB_AiffSoundDecoder* aifa, aiff_ChunkHeader* hdr)
{
	if (!read_be_32 (aifa->fp, &hdr->id) ||
			!read_be_32 (aifa->fp, &hdr->size))
	{
		aifa->last_error = errno;
		return false;
	}
	return true;
}

static int
aifa_readCommonChunk (TFB_AiffSoundDecoder* aifa, uint32 size,
		aiff_ExtCommonChunk* fmt)
{
	int bytes;

	memset(fmt, 0, sizeof(*fmt));
	if (size < AIFF_COMM_SIZE)
	{
		aifa->last_error = aifae_BadFile;
		return 0;
	}

	if (!read_be_16 (aifa->fp, &fmt->channels) ||
			!read_be_32 (aifa->fp, &fmt->sampleFrames) ||
			!read_be_16 (aifa->fp, &fmt->sampleSize) ||
			!read_be_f80 (aifa->fp, &fmt->sampleRate))
	{
		aifa->last_error = errno;
		return 0;
	}
	bytes = AIFF_COMM_SIZE;

	if (size >= AIFF_EXT_COMM_SIZE)
	{
		if (!read_be_32 (aifa->fp, &fmt->extTypeID))
		{
			aifa->last_error = errno;
			return 0;
		}
		bytes += sizeof(fmt->extTypeID);
	}
	
	return bytes;
}

static bool
aifa_readSoundDataChunk (TFB_AiffSoundDecoder* aifa,
		aiff_SoundDataChunk* data)
{
	if (!read_be_32 (aifa->fp, &data->offset) ||
			!read_be_32 (aifa->fp, &data->blockSize))
	{
		aifa->last_error = errno;
		return false;
	}
	return true;
}

static bool
aifa_Open (THIS_PTR, uio_DirHandle *dir, const char *filename)
{
	TFB_AiffSoundDecoder* aifa = (TFB_AiffSoundDecoder*) This;
	aiff_FileHeader fileHdr;
	aiff_ChunkHeader chunkHdr;
	sint32 remSize;

	aifa->fp = uio_fopen (dir, filename, "rb");
	if (!aifa->fp)
	{
		aifa->last_error = errno;
		return false;
	}

	aifa->data_size = 0;
	aifa->max_pcm = 0;
	aifa->data_ofs = 0;
	memset(&aifa->fmtHdr, 0, sizeof(aifa->fmtHdr));
	memset(aifa->prev_val, 0, sizeof(aifa->prev_val));

	// read wave header
	if (!aifa_readFileHeader (aifa, &fileHdr))
	{
		aifa->last_error = errno;
		aifa_Close (This);
		return false;
	}
	if (fileHdr.chunk.id != aiff_FormID)
	{
		log_add (log_Warning, "aifa_Open(): not an aiff file, ID 0x%08x",
				fileHdr.chunk.id);
		aifa_Close (This);
		return false;
	}
	if (fileHdr.type != aiff_FormTypeAIFF && fileHdr.type != aiff_FormTypeAIFC)
	{
		log_add (log_Warning, "aifa_Open(): unsupported aiff file"
				", Type 0x%08x", fileHdr.type);
		aifa_Close (This);
		return false;
	}

	for (remSize = fileHdr.chunk.size - sizeof(aiff_ID); remSize > 0;
			remSize -= ((chunkHdr.size + 1) & ~1) + AIFF_CHUNK_HDR_SIZE)
	{
		if (!aifa_readChunkHeader (aifa, &chunkHdr))
		{
			aifa_Close (This);
			return false;
		}

		if (chunkHdr.id == aiff_CommonID)
		{
			int read = aifa_readCommonChunk (aifa, chunkHdr.size, &aifa->fmtHdr);
			if (!read)
			{
				aifa_Close (This);
				return false;
			}
			uio_fseek (aifa->fp, chunkHdr.size - read, SEEK_CUR);
		}
		else if (chunkHdr.id == aiff_SoundDataID)
		{
			aiff_SoundDataChunk data;
			if (!aifa_readSoundDataChunk (aifa, &data))
			{
				aifa_Close (This);
				return false;
			}
			aifa->data_ofs = uio_ftell (aifa->fp) + data.offset;
			uio_fseek (aifa->fp, chunkHdr.size - AIFF_SSND_SIZE, SEEK_CUR);
		}
		else
		{	// skip uninteresting chunk
			uio_fseek (aifa->fp, chunkHdr.size, SEEK_CUR);
		}

		// 2-align the file ptr
		uio_fseek (aifa->fp, chunkHdr.size & 1, SEEK_CUR);
	}

	if (aifa->fmtHdr.sampleFrames == 0)
	{
		log_add (log_Warning, "aifa_Open(): aiff file has no sound data");
		aifa_Close (This);
		return false;
	}
	
	// make bits-per-sample a multiple of 8
	aifa->bits_per_sample = (aifa->fmtHdr.sampleSize + 7) & ~7;
	if (aifa->bits_per_sample == 0 || aifa->bits_per_sample > 16)
	{	// XXX: for now we do not support 24 and 32 bps
		log_add (log_Warning, "aifa_Open(): unsupported sample size %u",
				aifa->bits_per_sample);
		aifa_Close (This);
		return false;
	}
	if (aifa->fmtHdr.channels != 1 && aifa->fmtHdr.channels != 2)
	{
		log_add (log_Warning, "aifa_Open(): unsupported number of channels %u",
				(unsigned)aifa->fmtHdr.channels);
		aifa_Close (This);
		return false;
	}
	if (aifa->fmtHdr.sampleRate < 300 || aifa->fmtHdr.sampleRate > 128000)
	{
		log_add (log_Warning, "aifa_Open(): unsupported sampling rate %ld",
				(long)aifa->fmtHdr.sampleRate);
		aifa_Close (This);
		return false;
	}

	aifa->block_align = aifa->bits_per_sample / 8 * aifa->fmtHdr.channels;
	aifa->file_block = aifa->block_align;
	if (!aifa->data_ofs)
	{
		log_add (log_Warning, "aifa_Open(): bad aiff file,"
				" no SSND chunk found");
		aifa_Close (This);
		return false;
	}

	if (fileHdr.type == aiff_FormTypeAIFF)
	{
		if (aifa->fmtHdr.extTypeID != 0)
		{
			log_add (log_Warning, "aifa_Open(): unsupported extension 0x%08x",
					aifa->fmtHdr.extTypeID);
			aifa_Close (This);
			return false;
		}
		aifa->comp_type = aifc_None;
	}
	else if (fileHdr.type == aiff_FormTypeAIFC)
	{
		if (aifa->fmtHdr.extTypeID != aiff_CompressionTypeSDX2)
		{
			log_add (log_Warning, "aifa_Open(): unsupported compression 0x%08x",
					aifa->fmtHdr.extTypeID);
			aifa_Close (This);
			return false;
		}
		aifa->comp_type = aifc_Sdx2;
		aifa->file_block /= 2;
		assert(aifa->fmtHdr.channels <= MAX_CHANNELS);
		// after decompression, we will get samples in machine byte order
		This->need_swap = (aifa_formats->big_endian
				!= aifa_formats->want_big_endian);
	}

	aifa->data_size = aifa->fmtHdr.sampleFrames * aifa->file_block;

	if (aifa->comp_type == aifc_Sdx2 && aifa->bits_per_sample != 16)
	{
		log_add (log_Warning, "aifa_Open(): unsupported sample size %u for SDX2",
				(unsigned)aifa->fmtHdr.sampleSize);
		aifa_Close (This);
		return false;
	}

	This->format = (aifa->fmtHdr.channels == 1 ?
			(aifa->bits_per_sample == 8 ?
				aifa_formats->mono8 : aifa_formats->mono16)
			:
			(aifa->bits_per_sample == 8 ?
				aifa_formats->stereo8 : aifa_formats->stereo16)
			);
	This->frequency = aifa->fmtHdr.sampleRate;

	uio_fseek (aifa->fp, aifa->data_ofs, SEEK_SET);
	aifa->max_pcm = aifa->fmtHdr.sampleFrames;
	aifa->cur_pcm = 0;
	This->length = (float) aifa->max_pcm / aifa->fmtHdr.sampleRate;
	aifa->last_error = 0;

	return true;
}

static void
aifa_Close (THIS_PTR)
{
	TFB_AiffSoundDecoder* aifa = (TFB_AiffSoundDecoder*) This;

	if (aifa->fp)
	{
		uio_fclose (aifa->fp);
		aifa->fp = NULL;
	}
}

static int
aifa_Decode (THIS_PTR, void* buf, sint32 bufsize)
{
	TFB_AiffSoundDecoder* aifa = (TFB_AiffSoundDecoder*) This;
	switch (aifa->comp_type)
	{
	case aifc_None:
		return aifa_DecodePCM (aifa, buf, bufsize);
	case aifc_Sdx2:
		return aifa_DecodeSDX2 (aifa, buf, bufsize);
	default:
		assert(false && "Unknown comp_type");
		return 0;
	}
}

static int
aifa_DecodePCM (TFB_AiffSoundDecoder* aifa, void* buf, sint32 bufsize)
{
	uint32 dec_pcm;
	uint32 size;

	dec_pcm = bufsize / aifa->block_align;
	if (dec_pcm > aifa->max_pcm - aifa->cur_pcm)
		dec_pcm = aifa->max_pcm - aifa->cur_pcm;

	dec_pcm = uio_fread (buf, aifa->file_block, dec_pcm, aifa->fp);
	aifa->cur_pcm += dec_pcm;
	size = dec_pcm * aifa->block_align;

	if (aifa->bits_per_sample == 8)
	{	// AIFF files store 8-bit data as signed
		// and we need it unsigned
		uint8* ptr = (uint8*)buf;
		uint32 left;
		for (left = size; left > 0; --left, ++ptr)
			*ptr += 128;
	}
	
	return size;
}

static int
aifa_DecodeSDX2 (TFB_AiffSoundDecoder* aifa, void* buf, sint32 bufsize)
{
	uint32 dec_pcm;
	sint8 *src;
	sint16 *dst = buf;
	uint32 left;

	dec_pcm = bufsize / aifa->block_align;
	if (dec_pcm > aifa->max_pcm - aifa->cur_pcm)
		dec_pcm = aifa->max_pcm - aifa->cur_pcm;

	src = (sint8*)buf + bufsize - (dec_pcm * aifa->file_block);
	dec_pcm = uio_fread (src, aifa->file_block, dec_pcm, aifa->fp);
	aifa->cur_pcm += dec_pcm;

	for (left = dec_pcm; left > 0; --left)
	{
		int i;
		sint32 *prev = aifa->prev_val;
		for (i = aifa->fmtHdr.channels; i > 0; --i, ++prev, ++src, ++dst)
		{
			sint32 v = (*src * abs(*src)) << 1;
			if (*src & 1)
				v += *prev;
			// saturate the value
			if (v > 32767)
				v = 32767;
			else if (v < -32768)
				v = -32768;
			*prev = v;
			*dst = v;
		}
	}

	return dec_pcm * aifa->block_align;
}

static uint32
aifa_Seek (THIS_PTR, uint32 pcm_pos)
{
	TFB_AiffSoundDecoder* aifa = (TFB_AiffSoundDecoder*) This;

	if (pcm_pos > aifa->max_pcm)
		pcm_pos = aifa->max_pcm;
	aifa->cur_pcm = pcm_pos;
	uio_fseek (aifa->fp,
			aifa->data_ofs + pcm_pos * aifa->file_block,
			SEEK_SET);

	// reset previous values for SDX2 on seek ops
	// the delta will recover faster with reset
	memset(aifa->prev_val, 0, sizeof(aifa->prev_val));

	return pcm_pos;
}

static uint32
aifa_GetFrame (THIS_PTR)
{
	//TFB_AiffSoundDecoder* aifa = (TFB_AiffSoundDecoder*) This;
	return 0; // only 1 frame for now

	(void)This;	// laugh at compiler warning
}