File: snd_mp3.c

package info (click to toggle)
quakespasm 0.95.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,712 kB
  • sloc: ansic: 55,977; sh: 306; makefile: 271
file content (502 lines) | stat: -rw-r--r-- 13,950 bytes parent folder | download | duplicates (3)
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
/*
 * MP3 decoding support using libmad.
 *
 * Adapted from the SoX library at http://sourceforge.net/projects/sox/,
 * LGPLv2, Copyright (c) 2007-2009 SoX contributors, written by
 * Fabrizio Gennari <fabrizio.ge@tiscali.it>. As allowed by clause 3 of the
 * LGPL, this copy is licensed under the GNU GPL, version 2; see below.
 *
 * The decoding part was based on the decoder tutorial program madlld
 * written by Bertrand Petit <madlld@phoe.fmug.org> (BSD license, see
 * http://www.bsd-dk.dk/~elrond/audio/madlld/).
 * The copyright notice and BSD-style license are reproduced below.
 *
 * The tag identification functions were adapted from the GPL-licensed
 * libid3tag library, see http://www.underbit.com/products/mad/.
 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
 *
 * Adapted for use in Quake and Hexen II game engines by O.Sezer:
 * Copyright (C) 2010-2019 O.Sezer <sezero@users.sourceforge.net>
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/* Parts of this file are based on the decoder tutorial program madlld:
 *
 * Copyright (C) 2001-2004 Bertrand Petit, all rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of its contributors may
 *    be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "quakedef.h"

#if defined(USE_CODEC_MP3)
#include "snd_codec.h"
#include "snd_codeci.h"
#include "snd_mp3.h"
#include <mad.h>

/* Under Windows, importing data from DLLs is a dicey proposition. This is true
 * when using dlopen, but also true if linking directly against the DLL if the
 * header does not mark the data as __declspec(dllexport), which mad.h does not.
 * Sidestep the issue by defining our own mad_timer_zero. This is needed because
 * mad_timer_zero is used in some of the mad.h macros.
 */
#define mad_timer_zero mad_timer_zero_stub
static mad_timer_t const mad_timer_zero_stub = {0, 0};

/* MAD returns values with MAD_F_FRACBITS (28) bits of precision, though it's
   not certain that all of them are meaningful. Default to 16 bits to
   align with most users expectation of output file should be 16 bits. */
#define MP3_MAD_SAMPLEBITS	16
#define MP3_MAD_SAMPLEWIDTH	2
#define MP3_BUFFER_SIZE		(5 * 8192)

/* Private data */
typedef struct _mp3_priv_t
{
	unsigned char mp3_buffer[MP3_BUFFER_SIZE];
	struct mad_stream	Stream;
	struct mad_frame	Frame;
	struct mad_synth	Synth;
	mad_timer_t		Timer;
	ptrdiff_t		cursamp;
	size_t			FrameCount;
} mp3_priv_t;


/* (Re)fill the stream buffer that is to be decoded.  If any data
 * still exists in the buffer then they are first shifted to be
 * front of the stream buffer.  */
static int mp3_inputdata(snd_stream_t *stream)
{
	mp3_priv_t *p = (mp3_priv_t *) stream->priv;
	size_t bytes_read;
	size_t remaining;

	remaining = p->Stream.bufend - p->Stream.next_frame;

	/* libmad does not consume all the buffer it's given. Some
	 * data, part of a truncated frame, is left unused at the
	 * end of the buffer. That data must be put back at the
	 * beginning of the buffer and taken in account for
	 * refilling the buffer. This means that the input buffer
	 * must be large enough to hold a complete frame at the
	 * highest observable bit-rate (currently 448 kb/s).
	 * TODO: Is 2016 bytes the size of the largest frame?
	 * (448000*(1152/32000))/8
	 */
	memmove(p->mp3_buffer, p->Stream.next_frame, remaining);

	bytes_read = FS_fread(p->mp3_buffer + remaining, 1,
				MP3_BUFFER_SIZE - remaining, &stream->fh);
	if (bytes_read == 0)
		return -1;

	mad_stream_buffer(&p->Stream, p->mp3_buffer, bytes_read+remaining);
	p->Stream.error = MAD_ERROR_NONE;

	return 0;
}

static int mp3_startread(snd_stream_t *stream)
{
	mp3_priv_t *p = (mp3_priv_t *) stream->priv;
	size_t ReadSize;

	mad_stream_init(&p->Stream);
	mad_frame_init(&p->Frame);
	mad_synth_init(&p->Synth);
	mad_timer_reset(&p->Timer);

	/* Decode at least one valid frame to find out the input
	 * format.  The decoded frame will be saved off so that it
	 * can be processed later.
	 */
	ReadSize = FS_fread(p->mp3_buffer, 1, MP3_BUFFER_SIZE, &stream->fh);
	if (!ReadSize || FS_ferror(&stream->fh))
		return -1;

	mad_stream_buffer(&p->Stream, p->mp3_buffer, ReadSize);

	/* Find a valid frame before starting up.  This makes sure
	 * that we have a valid MP3.
	 */
	p->Stream.error = MAD_ERROR_NONE;
	while (mad_frame_decode(&p->Frame,&p->Stream))
	{
		/* check whether input buffer needs a refill */
		if (p->Stream.error == MAD_ERROR_BUFLEN)
		{
			if (mp3_inputdata(stream) == -1)
				return -1;/* EOF with no valid data */

			continue;
		}

		/* We know that a valid frame hasn't been found yet
		 * so help libmad out and go back into frame seek mode.
		 */
		mad_stream_sync(&p->Stream);
		p->Stream.error = MAD_ERROR_NONE;
	}

	if (p->Stream.error)
	{
		Con_Printf("MP3: No valid MP3 frame found\n");
		return -1;
	}

	switch(p->Frame.header.mode)
	{
	case MAD_MODE_SINGLE_CHANNEL:
	case MAD_MODE_DUAL_CHANNEL:
	case MAD_MODE_JOINT_STEREO:
	case MAD_MODE_STEREO:
		stream->info.channels = MAD_NCHANNELS(&p->Frame.header);
		break;
	default:
		Con_Printf("MP3: Cannot determine number of channels\n");
		return -1;
	}

	p->FrameCount = 1;

	mad_timer_add(&p->Timer,p->Frame.header.duration);
	mad_synth_frame(&p->Synth,&p->Frame);
	stream->info.rate = p->Synth.pcm.samplerate;
	stream->info.bits = MP3_MAD_SAMPLEBITS;
	stream->info.width = MP3_MAD_SAMPLEWIDTH;

	p->cursamp = 0;

	return 0;
}

/* Read up to len samples from p->Synth
 * If needed, read some more MP3 data, decode them and synth them
 * Place in buf[].
 * Return number of samples read.  */
static int mp3_decode(snd_stream_t *stream, byte *buf, int len)
{
	mp3_priv_t *p = (mp3_priv_t *) stream->priv;
	int donow, i, done = 0;
	mad_fixed_t sample;
	int chan, x;

	do
	{
		x = (p->Synth.pcm.length - p->cursamp) * stream->info.channels;
		donow = q_min(len, x);
		i = 0;
		while (i < donow)
		{
			for (chan = 0; chan < stream->info.channels; chan++)
			{
				sample = p->Synth.pcm.samples[chan][p->cursamp];
				/* convert from fixed to short,
				 * write in host-endian format. */
				if (sample <= -MAD_F_ONE)
					sample = -0x7FFF;
				else if (sample >= MAD_F_ONE)
					sample = 0x7FFF;
				else
					sample >>= (MAD_F_FRACBITS + 1 - 16);
				if (host_bigendian)
				{
					*buf++ = (sample >> 8) & 0xFF;
					*buf++ = sample & 0xFF;
				}
				else /* assumed LITTLE_ENDIAN. */
				{
					*buf++ = sample & 0xFF;
					*buf++ = (sample >> 8) & 0xFF;
				}
				i++;
			}
			p->cursamp++;
		}

		len -= donow;
		done += donow;

		if (len == 0)
			break;

		/* check whether input buffer needs a refill */
		if (p->Stream.error == MAD_ERROR_BUFLEN)
		{
			if (mp3_inputdata(stream) == -1)
			{
				/* check feof() ?? */
				Con_DPrintf("mp3 EOF\n");
				break;
			}
		}

		if (mad_frame_decode(&p->Frame, &p->Stream))
		{
			if (MAD_RECOVERABLE(p->Stream.error))
			{
				mad_stream_sync(&p->Stream); /* to frame seek mode */
				continue;
			}
			else
			{
				if (p->Stream.error == MAD_ERROR_BUFLEN)
					continue;
				else
				{
					Con_Printf("MP3: unrecoverable frame level error (%s)\n",
							mad_stream_errorstr(&p->Stream));
					break;
				}
			}
		}
		p->FrameCount++;
		mad_timer_add(&p->Timer, p->Frame.header.duration);
		mad_synth_frame(&p->Synth, &p->Frame);
		p->cursamp = 0;
	} while (1);

	return done;
}

static int mp3_stopread(snd_stream_t *stream)
{
	mp3_priv_t *p = (mp3_priv_t*) stream->priv;

	mad_synth_finish(&p->Synth);
	mad_frame_finish(&p->Frame);
	mad_stream_finish(&p->Stream);

	return 0;
}

static int mp3_madseek(snd_stream_t *stream, unsigned long offset)
{
	mp3_priv_t *p = (mp3_priv_t *) stream->priv;
	size_t   initial_bitrate = p->Frame.header.bitrate;
	size_t   consumed = 0;
	int vbr = 0;		/* Variable Bit Rate, bool */
	qboolean depadded = false;
	unsigned long to_skip_samples = 0;

	/* Reset all */
	FS_rewind(&stream->fh);
	mad_timer_reset(&p->Timer);
	p->FrameCount = 0;

	/* They where opened in startread */
	mad_synth_finish(&p->Synth);
	mad_frame_finish(&p->Frame);
	mad_stream_finish(&p->Stream);

	mad_stream_init(&p->Stream);
	mad_frame_init(&p->Frame);
	mad_synth_init(&p->Synth);

	offset /= stream->info.channels;
	to_skip_samples = offset;

	while (1)	/* Read data from the MP3 file */
	{
		int bytes_read, padding = 0;
		size_t leftover = p->Stream.bufend - p->Stream.next_frame;

		memcpy(p->mp3_buffer, p->Stream.this_frame, leftover);
		bytes_read = FS_fread(p->mp3_buffer + leftover, (size_t) 1,
					MP3_BUFFER_SIZE - leftover, &stream->fh);
		if (bytes_read <= 0)
		{
			Con_DPrintf("seek failure. unexpected EOF (frames=%lu leftover=%lu)\n",
					(unsigned long)p->FrameCount, (unsigned long)leftover);
			break;
		}
		for ( ; !depadded && padding < bytes_read && !p->mp3_buffer[padding]; ++padding)
			;
		depadded = true;
		mad_stream_buffer(&p->Stream, p->mp3_buffer + padding, leftover + bytes_read - padding);

		while (1)	/* Decode frame headers */
		{
			static unsigned short samples;
			p->Stream.error = MAD_ERROR_NONE;

			/* Not an audio frame */
			if (mad_header_decode(&p->Frame.header, &p->Stream) == -1)
			{
				if (p->Stream.error == MAD_ERROR_BUFLEN)
					break;	/* Normal behaviour; get some more data from the file */
				if (!MAD_RECOVERABLE(p->Stream.error))
				{
					Con_DPrintf("unrecoverable MAD error\n");
					break;
				}
				if (p->Stream.error == MAD_ERROR_LOSTSYNC)
				{
					Con_DPrintf("MAD lost sync\n");
				}
				else
				{
					Con_DPrintf("recoverable MAD error\n");
				}
				continue;
			}

			consumed +=  p->Stream.next_frame - p->Stream.this_frame;
			vbr      |= (p->Frame.header.bitrate != initial_bitrate);

			samples = 32 * MAD_NSBSAMPLES(&p->Frame.header);

			p->FrameCount++;
			mad_timer_add(&p->Timer, p->Frame.header.duration);

			if (to_skip_samples <= samples)
			{
				mad_frame_decode(&p->Frame,&p->Stream);
				mad_synth_frame(&p->Synth, &p->Frame);
				p->cursamp = to_skip_samples;
				return 0;
			}
			else	to_skip_samples -= samples;

			/* If not VBR, we can extrapolate frame size */
			if (p->FrameCount == 64 && !vbr)
			{
				p->FrameCount = offset / samples;
				to_skip_samples = offset % samples;
				if (0 != FS_fseek(&stream->fh, (p->FrameCount * consumed / 64), SEEK_SET))
					return -1;

				/* Reset Stream for refilling buffer */
				mad_stream_finish(&p->Stream);
				mad_stream_init(&p->Stream);
				break;
			}
		}
	}

	return -1;
}

static qboolean S_MP3_CodecInitialize (void)
{
	return true;
}

static void S_MP3_CodecShutdown (void)
{
}

static qboolean S_MP3_CodecOpenStream (snd_stream_t *stream)
{
	int err;

	if (mp3_skiptags(stream) < 0)
	{
		Con_Printf("Corrupt mp3 file (bad tags.)\n");
		return false;
	}

	stream->priv = calloc(1, sizeof(mp3_priv_t));
	if (!stream->priv)
	{
		Con_Printf("Insufficient memory for MP3 audio\n");
		return false;
	}
	err = mp3_startread(stream);
	if (err != 0)
	{
		Con_Printf("%s is not a valid mp3 file\n", stream->name);
	}
	else if (stream->info.channels != 1 && stream->info.channels != 2)
	{
		Con_Printf("Unsupported number of channels %d in %s\n",
					stream->info.channels, stream->name);
	}
	else
	{
		return true;
	}
	free(stream->priv);
	return false;
}

static int S_MP3_CodecReadStream (snd_stream_t *stream, int bytes, void *buffer)
{
	int res = mp3_decode(stream, (byte *)buffer, bytes / stream->info.width);
	return res * stream->info.width;
}

static void S_MP3_CodecCloseStream (snd_stream_t *stream)
{
	mp3_stopread(stream);
	free(stream->priv);
	S_CodecUtilClose(&stream);
}

static int S_MP3_CodecRewindStream (snd_stream_t *stream)
{
	/*
	mp3_stopread(stream);
	FS_rewind(&stream->fh);
	return mp3_startread(stream);
	*/
	return mp3_madseek(stream, 0);
}

snd_codec_t mp3_codec =
{
	CODECTYPE_MP3,
	true,	/* always available. */
	"mp3",
	S_MP3_CodecInitialize,
	S_MP3_CodecShutdown,
	S_MP3_CodecOpenStream,
	S_MP3_CodecReadStream,
	S_MP3_CodecRewindStream,
	NULL, /* jump */
	S_MP3_CodecCloseStream,
	NULL
};

#endif	/* USE_CODEC_MP3 */