summaryrefslogtreecommitdiff
path: root/src/libs/sound/decoders/dukaud.c
blob: aeff3733b2c740adbc84c64226ba3845f6938d1c (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
/*
 *  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.
 */

/* .duk sound track decoder
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "libs/memlib.h"
#include "port.h"
#include "types.h"
#include "libs/uio.h"
#include "dukaud.h"
#include "decoder.h"
#include "endian_uqm.h"

#define DATA_BUF_SIZE	  0x8000
#define DUCK_GENERAL_FPS  14.622f


#define THIS_PTR TFB_SoundDecoder* This

static const char* duka_GetName (void);
static bool duka_InitModule (int flags, const TFB_DecoderFormats*);
static void duka_TermModule (void);
static uint32 duka_GetStructSize (void);
static int duka_GetError (THIS_PTR);
static bool duka_Init (THIS_PTR);
static void duka_Term (THIS_PTR);
static bool duka_Open (THIS_PTR, uio_DirHandle *dir, const char *filename);
static void duka_Close (THIS_PTR);
static int duka_Decode (THIS_PTR, void* buf, sint32 bufsize);
static uint32 duka_Seek (THIS_PTR, uint32 pcm_pos);
static uint32 duka_GetFrame (THIS_PTR);

TFB_SoundDecoderFuncs duka_DecoderVtbl = 
{
	duka_GetName,
	duka_InitModule,
	duka_TermModule,
	duka_GetStructSize,
	duka_GetError,
	duka_Init,
	duka_Term,
	duka_Open,
	duka_Close,
	duka_Decode,
	duka_Seek,
	duka_GetFrame,
};

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

	// public read-only
	uint32 iframe;    // current frame index
	uint32 cframes;   // total count of frames
	uint32 channels;  // number of channels
	uint32 pcm_frame; // samples per frame

	// private
	sint32 last_error;
	uio_Stream* duk;
	uint32* frames;
	// buffer
	void* data;
	uint32 maxdata;
	uint32 cbdata;
	uint32 dataofs;
	// decoder stuff
	sint32 predictors[2];

} TFB_DuckSoundDecoder;


typedef struct
{
	uint32 audsize;
	uint32 vidsize;
} DukAud_FrameHeader;

typedef struct
{
	uint16 magic;  // always 0xf77f
	uint16 numsamples;
	uint16 tag;
	uint16 indices[2];  // initial indices for channels
} DukAud_AudSubframe;

static const TFB_DecoderFormats* duka_formats = NULL;

static sint32
duka_readAudFrameHeader (TFB_DuckSoundDecoder* duka, uint32 iframe,
		DukAud_AudSubframe* aud)
{
	DukAud_FrameHeader hdr;

	uio_fseek (duka->duk, duka->frames[iframe], SEEK_SET);
	if (uio_fread (&hdr, sizeof(hdr), 1, duka->duk) != 1)
	{
		duka->last_error = errno;
		return dukae_BadFile;
	}
	hdr.audsize = UQM_SwapBE32 (hdr.audsize);

	if (uio_fread (aud, sizeof(*aud), 1, duka->duk) != 1)
	{
		duka->last_error = errno;
		return dukae_BadFile;
	}

	aud->magic = UQM_SwapBE16 (aud->magic);
	if (aud->magic != 0xf77f)
		return duka->last_error = dukae_BadFile;
	
	aud->numsamples = UQM_SwapBE16 (aud->numsamples);
	aud->tag = UQM_SwapBE16 (aud->tag);
	aud->indices[0] = UQM_SwapBE16 (aud->indices[0]);
	aud->indices[1] = UQM_SwapBE16 (aud->indices[1]);

	return 0;
}

// This table is from one of the files that came with the original 3do source
// It's slightly different from the data used by MPlayer.
static int adpcm_step[89] = {
		0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xF,
		0x10, 0x12, 0x13, 0x15, 0x17, 0x1A, 0x1C, 0x1F,
		0x22, 0x26, 0x29, 0x2E, 0x32, 0x37, 0x3D, 0x43,
		0x4A, 0x51, 0x59, 0x62, 0x6C, 0x76, 0x82, 0x8F,
		0x9E, 0xAD, 0xBF, 0xD2, 0xE7, 0xFE, 0x117, 0x133,
		0x152, 0x174, 0x199, 0x1C2, 0x1EF, 0x220, 0x256, 0x292,
		0x2D4, 0x31D, 0x36C, 0x3C4, 0x424, 0x48E, 0x503, 0x583,
		0x610, 0x6AC, 0x756, 0x812, 0x8E1, 0x9C4, 0xABE, 0xBD1,
		0xCFF, 0xE4C, 0xFBA, 0x114D, 0x1308, 0x14EF, 0x1707, 0x1954,
		0x1BDD, 0x1EA6, 0x21B7, 0x2516,
		0x28CB, 0x2CDF, 0x315C, 0x364C,
		0x3BBA, 0x41B2, 0x4844, 0x4F7E,
		0x5771, 0x6030, 0x69CE, 0x7463,
		0x7FFF
		};


// *** BEGIN part copied from MPlayer ***
// (some little changes)

#if 0
// pertinent tables for IMA ADPCM
static int adpcm_step[89] = {
		7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
		19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
		50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
		130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
		337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
		876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
		2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
		5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
		15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
		};
#endif

static int adpcm_index[16] = {
		-1, -1, -1, -1, 2, 4, 6, 8,
		-1, -1, -1, -1, 2, 4, 6, 8
		};

// clamp a number between 0 and 88
#define CLAMP_0_TO_88(x) \
		if ((x) < 0) (x) = 0; else if ((x) > 88) (x) = 88;

// clamp a number within a signed 16-bit range
#define CLAMP_S16(x) \
		if ((x) < -32768) \
			(x) = -32768; \
		else if ((x) > 32767) \
			(x) = 32767;

static void
decode_nibbles (sint16 *output, sint32 output_size, sint32 channels,
		sint32* predictors, uint16* indices)
{
	sint32 step[2];
	sint32 index[2];
	sint32 diff;
	sint32 i;
	int sign;
	sint32 delta;
	int channel_number = 0;

	channels -= 1;
	index[0] = indices[0];
	index[1] = indices[1];
	step[0] = adpcm_step[index[0]];
	step[1] = adpcm_step[index[1]];

	for (i = 0; i < output_size; i++)
	{
		delta = output[i];

		index[channel_number] += adpcm_index[delta];
		CLAMP_0_TO_88(index[channel_number]);

		sign = delta & 8;
		delta = delta & 7;

#if 0
		// fast approximation, used in most decoders
		diff = step[channel_number] >> 3;
		if (delta & 4) diff += step[channel_number];
		if (delta & 2) diff += step[channel_number] >> 1;
		if (delta & 1) diff += step[channel_number] >> 2;
#else
		// real thing
//		diff = ((signed)delta + 0.5) * step[channel_number] / 4;
		diff = (((delta << 1) + 1) * step[channel_number]) >> 3;
#endif

		if (sign)
			predictors[channel_number] -= diff;
		else
			predictors[channel_number] += diff;

		CLAMP_S16(predictors[channel_number]);
		output[i] = predictors[channel_number];
		step[channel_number] = adpcm_step[index[channel_number]];

		// toggle channel
		channel_number ^= channels;
	}
}
// *** END part copied from MPlayer ***

static sint32
duka_decodeFrame (TFB_DuckSoundDecoder* duka, DukAud_AudSubframe* header,
		uint8* input)
{
	uint8* inend;
	sint16* output;
	sint16* outptr;
	sint32 outputsize;

	outputsize = header->numsamples * 2 * sizeof (sint16);
	outptr = output = (sint16*) ((uint8*)duka->data + duka->cbdata);
	
	for (inend = input + header->numsamples; input < inend; ++input)
	{
		*(outptr++) = *input >> 4;
		*(outptr++) = *input & 0x0f;
	}
	
	decode_nibbles (output, header->numsamples * 2, duka->channels,
			duka->predictors, header->indices);

	duka->cbdata += outputsize;

	return outputsize;
}


static sint32
duka_readNextFrame (TFB_DuckSoundDecoder* duka)
{
	DukAud_FrameHeader hdr;
	DukAud_AudSubframe* aud;
	uint8* p;

	uio_fseek (duka->duk, duka->frames[duka->iframe], SEEK_SET);
	if (uio_fread (&hdr, sizeof(hdr), 1, duka->duk) != 1)
	{
		duka->last_error = errno;
		return dukae_BadFile;
	}
	hdr.audsize = UQM_SwapBE32 (hdr.audsize);

	// dump encoded data at the end of the buffer aligned on 8-byte
	p = ((uint8*)duka->data + duka->maxdata - ((hdr.audsize + 7) & (-8)));
	if (uio_fread (p, 1, hdr.audsize, duka->duk) != hdr.audsize)
	{
		duka->last_error = errno;
		return dukae_BadFile;
	}
	aud = (DukAud_AudSubframe*) p;
	p += sizeof(DukAud_AudSubframe);

	aud->magic = UQM_SwapBE16 (aud->magic);
	if (aud->magic != 0xf77f)
		return duka->last_error = dukae_BadFile;
	
	aud->numsamples = UQM_SwapBE16 (aud->numsamples);
	aud->tag = UQM_SwapBE16 (aud->tag);
	aud->indices[0] = UQM_SwapBE16 (aud->indices[0]);
	aud->indices[1] = UQM_SwapBE16 (aud->indices[1]);

	duka->iframe++;

	return duka_decodeFrame (duka, aud, p);
}

static sint32
duka_stuffBuffer (TFB_DuckSoundDecoder* duka, void* buf, sint32 bufsize)
{
	sint32 dataleft;

	dataleft = duka->cbdata - duka->dataofs;
	if (dataleft > 0)
	{
		if (dataleft > bufsize)
			dataleft = bufsize & (-4);
		memcpy (buf, (uint8*)duka->data + duka->dataofs, dataleft);
		duka->dataofs += dataleft;
	}

	if (duka->cbdata > 0 && duka->dataofs >= duka->cbdata)
		duka->cbdata = duka->dataofs = 0; // reset for new data

	return dataleft;
}


static const char*
duka_GetName (void)
{
	return "DukAud";
}

static bool
duka_InitModule (int flags, const TFB_DecoderFormats* fmts)
{
	duka_formats = fmts;
	return true;

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

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

static uint32
duka_GetStructSize (void)
{
	return sizeof (TFB_DuckSoundDecoder);
}

static int
duka_GetError (THIS_PTR)
{
	TFB_DuckSoundDecoder* duka = (TFB_DuckSoundDecoder*) This;
	int ret = duka->last_error;
	duka->last_error = dukae_None;
	return ret;
}

static bool
duka_Init (THIS_PTR)
{
	//TFB_DuckSoundDecoder* duka = (TFB_DuckSoundDecoder*) This;
	This->need_swap =
			duka_formats->big_endian != duka_formats->want_big_endian;
	return true;
}

static void
duka_Term (THIS_PTR)
{
	//TFB_DuckSoundDecoder* duka = (TFB_DuckSoundDecoder*) This;
	duka_Close (This); // ensure cleanup
}

static bool
duka_Open (THIS_PTR, uio_DirHandle *dir, const char *file)
{
	TFB_DuckSoundDecoder* duka = (TFB_DuckSoundDecoder*) This;
	uio_Stream* duk;
	uio_Stream* frm;
	DukAud_AudSubframe aud;
	char filename[256];
	uint32 filelen;
	size_t cread;
	uint32 i;

	filelen = strlen (file);
	if (filelen > sizeof (filename) - 1)
		return false;
	strcpy (filename, file);

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

	strcpy (filename + filelen - 3, "frm");
	frm = uio_fopen (dir, filename, "rb");
	if (!frm)
	{
		duka->last_error = errno;
		uio_fclose (duk);
		return false;
	}

	duka->duk = duk;

	uio_fseek (frm, 0, SEEK_END);
	duka->cframes = uio_ftell (frm) / sizeof (uint32);
	uio_fseek (frm, 0, SEEK_SET);
	if (!duka->cframes)
	{
		duka->last_error = dukae_BadFile;
		uio_fclose (frm);
		duka_Close (This);
		return false;
	}
	
	duka->frames = (uint32*) HMalloc (duka->cframes * sizeof (uint32));
	cread = uio_fread (duka->frames, sizeof (uint32), duka->cframes, frm);
	uio_fclose (frm);
	if (cread != duka->cframes)
	{
		duka->last_error = dukae_BadFile;
		duka_Close (This);
		return false;
	}

	for (i = 0; i < duka->cframes; ++i)
		duka->frames[i] = UQM_SwapBE32 (duka->frames[i]);

	if (duka_readAudFrameHeader (duka, 0, &aud) < 0)
	{
		duka_Close (This);
		return false;
	}

	This->frequency = 22050;
	This->format = duka_formats->stereo16;
	duka->channels = 2;
	duka->pcm_frame = aud.numsamples;
	duka->data = HMalloc (DATA_BUF_SIZE);
	duka->maxdata = DATA_BUF_SIZE;

	// estimate
	This->length = (float) duka->cframes / DUCK_GENERAL_FPS;

	duka->last_error = 0;

	return true;
}

static void
duka_Close (THIS_PTR)
{
	TFB_DuckSoundDecoder* duka = (TFB_DuckSoundDecoder*) This;

	if (duka->data)
	{
		HFree (duka->data);
		duka->data = NULL;
	}
	if (duka->frames)
	{
		HFree (duka->frames);
		duka->frames = NULL;
	}
	if (duka->duk)
	{
		uio_fclose (duka->duk);
		duka->duk = NULL;
	}
	duka->last_error = 0;
}

static int
duka_Decode (THIS_PTR, void* buf, sint32 bufsize)
{
	TFB_DuckSoundDecoder* duka = (TFB_DuckSoundDecoder*) This;
	sint32 stuffed;
	sint32 total = 0;

	if (bufsize <= 0)
		return duka->last_error = dukae_BadArg;

	do
	{
		stuffed = duka_stuffBuffer (duka, buf, bufsize);
		buf = (uint8*)buf + stuffed;
		bufsize -= stuffed;
		total += stuffed;
	
		if (bufsize > 0 && duka->iframe < duka->cframes)
		{
			stuffed = duka_readNextFrame (duka);
			if (stuffed <= 0)
				return stuffed;
		}
	} while (bufsize > 0 && duka->iframe < duka->cframes);

	return total;
}

static uint32
duka_Seek (THIS_PTR, uint32 pcm_pos)
{
	TFB_DuckSoundDecoder* duka = (TFB_DuckSoundDecoder*) This;
	uint32 iframe;

	iframe = pcm_pos / duka->pcm_frame;
	if (iframe < duka->cframes)
	{
		duka->iframe = iframe;
		duka->cbdata = 0;
		duka->dataofs = 0;
		duka->predictors[0] = 0;
		duka->predictors[1] = 0;
	}
	return duka->iframe * duka->pcm_frame;
}

static uint32
duka_GetFrame (THIS_PTR)
{
	TFB_DuckSoundDecoder* duka = (TFB_DuckSoundDecoder*) This;

	// if there is nothing buffered return the actual current frame
	//  otherwise return previous
	return duka->dataofs == duka->cbdata ?
			duka->iframe : duka->iframe - 1;
}