Subversion Repositories Kolibri OS

Rev

Rev 1905 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1905 Rev 3960
Line 21... Line 21...
21
{
21
{
22
	MPG123_ENC_SIGNED_16,
22
	MPG123_ENC_SIGNED_16,
23
	MPG123_ENC_UNSIGNED_16,
23
	MPG123_ENC_UNSIGNED_16,
24
	MPG123_ENC_SIGNED_32,
24
	MPG123_ENC_SIGNED_32,
25
	MPG123_ENC_UNSIGNED_32,
25
	MPG123_ENC_UNSIGNED_32,
-
 
26
	MPG123_ENC_SIGNED_24,
-
 
27
	MPG123_ENC_UNSIGNED_24,
-
 
28
	/* Floating point range, see below. */
26
	MPG123_ENC_FLOAT_32,
29
	MPG123_ENC_FLOAT_32,
27
	MPG123_ENC_FLOAT_64,
30
	MPG123_ENC_FLOAT_64,
-
 
31
	/* 8 bit range, see below. */
28
	MPG123_ENC_SIGNED_8,
32
	MPG123_ENC_SIGNED_8,
29
	MPG123_ENC_UNSIGNED_8,
33
	MPG123_ENC_UNSIGNED_8,
30
	MPG123_ENC_ULAW_8,
34
	MPG123_ENC_ULAW_8,
31
	MPG123_ENC_ALAW_8
35
	MPG123_ENC_ALAW_8
32
};
36
};
Line -... Line 37...
-
 
37
 
-
 
38
/* Make that match the above table.
-
 
39
   And yes, I still don't like this kludgy stuff. */
-
 
40
/* range[0] <= i < range[1] for forced floating point */
-
 
41
static const int enc_float_range[2] = { 6, 8 };
-
 
42
/* same for 8 bit encodings */
-
 
43
static const int enc_8bit_range[2] = { 8, 12 };
33
 
44
 
34
/* Only one type of float is supported. */
45
/* Only one type of float is supported. */
35
# ifdef REAL_IS_FLOAT
46
# ifdef REAL_IS_FLOAT
36
#  define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_32
47
#  define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_32
37
# else
48
# else
Line 46... Line 57...
46
	MPG123_ENC_UNSIGNED_16,
57
	MPG123_ENC_UNSIGNED_16,
47
#endif
58
#endif
48
#ifndef NO_32BIT
59
#ifndef NO_32BIT
49
	MPG123_ENC_SIGNED_32,
60
	MPG123_ENC_SIGNED_32,
50
	MPG123_ENC_UNSIGNED_32,
61
	MPG123_ENC_UNSIGNED_32,
-
 
62
	MPG123_ENC_SIGNED_24,
-
 
63
	MPG123_ENC_UNSIGNED_24,
51
#endif
64
#endif
52
#ifndef NO_REAL
65
#ifndef NO_REAL
53
	MPG123_FLOAT_ENC,
66
	MPG123_FLOAT_ENC,
54
#endif
67
#endif
55
#ifndef NO_8BIT
68
#ifndef NO_8BIT
Line 82... Line 95...
82
{
95
{
83
	if(list   != NULL) *list   = good_encodings;
96
	if(list   != NULL) *list   = good_encodings;
84
	if(number != NULL) *number = sizeof(good_encodings)/sizeof(int);
97
	if(number != NULL) *number = sizeof(good_encodings)/sizeof(int);
85
}
98
}
Line -... Line 99...
-
 
99
 
-
 
100
int attribute_align_arg mpg123_encsize(int encoding)
-
 
101
{
-
 
102
	if(encoding & MPG123_ENC_8)
-
 
103
	return 1;
-
 
104
	else if(encoding & MPG123_ENC_16)
-
 
105
	return 2;
-
 
106
	else if(encoding & MPG123_ENC_24)
-
 
107
	return 3;
-
 
108
	else if(encoding & MPG123_ENC_32 || encoding == MPG123_ENC_FLOAT_32)
-
 
109
	return 4;
-
 
110
	else if(encoding == MPG123_ENC_FLOAT_64)
-
 
111
	return 8;
-
 
112
	else
-
 
113
	return 0;
-
 
114
}
86
 
115
 
Line 87... Line 116...
87
/*	char audio_caps[NUM_CHANNELS][MPG123_RATES+1][MPG123_ENCODINGS]; */
116
/*	char audio_caps[NUM_CHANNELS][MPG123_RATES+1][MPG123_ENCODINGS]; */
88
 
117
 
89
static int rate2num(mpg123_pars *mp, long r)
118
static int rate2num(mpg123_pars *mp, long r)
Line 124... Line 153...
124
 
153
 
125
static int freq_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2)
154
static int freq_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2)
126
{
155
{
127
	nf->rate = frame_freq(fr)>>fr->p.down_sample;
156
	nf->rate = frame_freq(fr)>>fr->p.down_sample;
-
 
157
	if(cap_fit(fr,nf,f0,f2)) return 1;
-
 
158
	if(fr->p.flags & MPG123_AUTO_RESAMPLE)
128
	if(cap_fit(fr,nf,f0,f2)) return 1;
159
	{
129
	nf->rate>>=1;
160
		nf->rate>>=1;
130
	if(cap_fit(fr,nf,f0,f2)) return 1;
161
		if(cap_fit(fr,nf,f0,f2)) return 1;
131
	nf->rate>>=1;
162
		nf->rate>>=1;
-
 
163
		if(cap_fit(fr,nf,f0,f2)) return 1;
132
	if(cap_fit(fr,nf,f0,f2)) return 1;
164
	}
133
#ifndef NO_NTOM
165
#ifndef NO_NTOM
134
	/* If nothing worked, try the other rates, only without constrains from user.
166
	/* If nothing worked, try the other rates, only without constrains from user.
-
 
167
	   In case you didn't guess: We enable flexible resampling if we find a working rate. */
135
	   In case you didn't guess: We enable flexible resampling if we find a working rate. */
168
	if(  fr->p.flags & MPG123_AUTO_RESAMPLE &&
136
	if(!fr->p.force_rate && fr->p.down_sample == 0)
169
	    !fr->p.force_rate && fr->p.down_sample == 0)
137
	{
170
	{
138
		int i;
171
		int i;
139
		int c  = nf->channels-1;
172
		int c  = nf->channels-1;
140
		int rn = rate2num(&fr->p, frame_freq(fr));
173
		int rn = rate2num(&fr->p, frame_freq(fr));
Line 174... Line 207...
174
	nf.channels = fr->stereo;
207
	nf.channels = fr->stereo;
Line 175... Line 208...
175
 
208
 
176
	/* All this forcing should be removed in favour of the capabilities table... */
209
	/* All this forcing should be removed in favour of the capabilities table... */
177
	if(p->flags & MPG123_FORCE_8BIT)
210
	if(p->flags & MPG123_FORCE_8BIT)
178
	{
211
	{
179
		f0 = 6;
212
		f0 = enc_8bit_range[0];
180
		f2 = 10;
213
		f2 = enc_8bit_range[1];
181
	}
214
	}
182
	if(p->flags & MPG123_FORCE_FLOAT)
215
	if(p->flags & MPG123_FORCE_FLOAT)
183
	{
216
	{
184
		f0 = 4;
217
		f0 = enc_float_range[0];
185
		f2 = 6;
218
		f2 = enc_float_range[1];
Line 186... Line 219...
186
	}
219
	}
187
 
220
 
188
	/* force stereo is stronger */
221
	/* force stereo is stronger */
Line 252... Line 285...
252
		debug1("New format with %i channels!", nf.channels);
285
		debug1("New format with %i channels!", nf.channels);
253
		fr->af.rate = nf.rate;
286
		fr->af.rate = nf.rate;
254
		fr->af.channels = nf.channels;
287
		fr->af.channels = nf.channels;
255
		fr->af.encoding = nf.encoding;
288
		fr->af.encoding = nf.encoding;
256
		/* Cache the size of one sample in bytes, for ease of use. */
289
		/* Cache the size of one sample in bytes, for ease of use. */
257
		if(fr->af.encoding & MPG123_ENC_8)
-
 
258
		fr->af.encsize = 1;
-
 
259
		else if(fr->af.encoding & MPG123_ENC_16)
-
 
260
		fr->af.encsize = 2;
-
 
261
		else if(fr->af.encoding & MPG123_ENC_32 || fr->af.encoding == MPG123_ENC_FLOAT_32)
290
		fr->af.encsize = mpg123_encsize(fr->af.encoding);
262
		fr->af.encsize = 4;
-
 
263
		else if(fr->af.encoding == MPG123_ENC_FLOAT_64)
-
 
264
		fr->af.encsize = 8;
291
		if(fr->af.encsize < 1)
265
		else
-
 
266
		{
292
		{
267
			if(NOQUIET) error1("Some unknown encoding??? (%i)", fr->af.encoding);
293
			if(NOQUIET) error1("Some unknown encoding??? (%i)", fr->af.encoding);
Line 268... Line 294...
268
 
294
 
269
			fr->err = MPG123_BAD_OUTFORMAT;
295
			fr->err = MPG123_BAD_OUTFORMAT;
Line 381... Line 407...
381
	af->encoding = 0;
407
	af->encoding = 0;
382
	af->rate     = 0;
408
	af->rate     = 0;
383
	af->channels = 0;
409
	af->channels = 0;
384
}
410
}
Line -... Line 411...
-
 
411
 
-
 
412
/* Consider 24bit output needing 32bit output as temporary storage. */
-
 
413
off_t samples_to_storage(mpg123_handle *fr, off_t s)
-
 
414
{
-
 
415
	if(fr->af.encoding & MPG123_ENC_24)
-
 
416
	return s*4*fr->af.channels; /* 4 bytes per sample */
-
 
417
	else
-
 
418
	return samples_to_bytes(fr, s);
-
 
419
}
385
 
420
 
386
/* take into account: channels, bytes per sample -- NOT resampling!*/
421
/* take into account: channels, bytes per sample -- NOT resampling!*/
387
off_t samples_to_bytes(mpg123_handle *fr , off_t s)
422
off_t samples_to_bytes(mpg123_handle *fr , off_t s)
388
{
423
{
389
	return s * fr->af.encsize * fr->af.channels;
424
	return s * fr->af.encsize * fr->af.channels;
Line 390... Line 425...
390
}
425
}
391
 
426
 
392
off_t bytes_to_samples(mpg123_handle *fr , off_t b)
427
off_t bytes_to_samples(mpg123_handle *fr , off_t b)
393
{
428
{
-
 
429
	return b / fr->af.encsize / fr->af.channels;
-
 
430
}
-
 
431
 
-
 
432
 
-
 
433
#ifndef NO_32BIT
-
 
434
/* Remove every fourth byte, facilitating conversion from 32 bit to 24 bit integers.
-
 
435
   This has to be aware of endianness, of course. */
-
 
436
static void chop_fourth_byte(struct outbuffer *buf)
-
 
437
{
-
 
438
	unsigned char *wpos = buf->data;
-
 
439
	unsigned char *rpos = buf->data;
-
 
440
#ifdef WORDS_BIGENDIAN
-
 
441
	while((size_t) (rpos - buf->data + 4) <= buf->fill)
-
 
442
	{
-
 
443
		/* Really stupid: Copy, increment. Byte per byte. */
-
 
444
		*wpos = *rpos;
-
 
445
		wpos++; rpos++;
-
 
446
		*wpos = *rpos;
-
 
447
		wpos++; rpos++;
-
 
448
		*wpos = *rpos;
-
 
449
		wpos++; rpos++;
-
 
450
		rpos++; /* Skip the lowest byte (last). */
-
 
451
	}
-
 
452
#else
-
 
453
	while((size_t) (rpos - buf->data + 4) <= buf->fill)
-
 
454
	{
-
 
455
		/* Really stupid: Copy, increment. Byte per byte. */
-
 
456
		rpos++; /* Skip the lowest byte (first). */
-
 
457
		*wpos = *rpos;
-
 
458
		wpos++; rpos++;
-
 
459
		*wpos = *rpos;
-
 
460
		wpos++; rpos++;
-
 
461
		*wpos = *rpos;
-
 
462
		wpos++; rpos++;
-
 
463
	}
-
 
464
#endif
-
 
465
	buf->fill = wpos-buf->data;
-
 
466
}
-
 
467
#endif
-
 
468
 
-
 
469
void postprocess_buffer(mpg123_handle *fr)
-
 
470
{
-
 
471
	/* Handle unsigned output formats via reshifting after decode here.
-
 
472
	   Also handle conversion to 24 bit. */
-
 
473
#ifndef NO_32BIT
-
 
474
	if(fr->af.encoding == MPG123_ENC_UNSIGNED_32 || fr->af.encoding == MPG123_ENC_UNSIGNED_24)
-
 
475
	{ /* 32bit signed -> unsigned */
-
 
476
		size_t i;
-
 
477
		int32_t *ssamples;
-
 
478
		uint32_t *usamples;
-
 
479
		ssamples = (int32_t*)fr->buffer.data;
-
 
480
		usamples = (uint32_t*)fr->buffer.data;
-
 
481
		debug("converting output to unsigned 32 bit integer");
-
 
482
		for(i=0; ibuffer.fill/sizeof(int32_t); ++i)
-
 
483
		{
-
 
484
			/* Different strategy since we don't have a larger type at hand.
-
 
485
				 Also watch out for silly +-1 fun because integer constants are signed in C90! */
-
 
486
			if(ssamples[i] >= 0)
-
 
487
			usamples[i] = (uint32_t)ssamples[i] + 2147483647+1;
-
 
488
			/* The smalles value goes zero. */
-
 
489
			else if(ssamples[i] == ((int32_t)-2147483647-1))
-
 
490
			usamples[i] = 0;
-
 
491
			/* Now -value is in the positive range of signed int ... so it's a possible value at all. */
-
 
492
			else
-
 
493
			usamples[i] = (uint32_t)2147483647+1 - (uint32_t)(-ssamples[i]);
-
 
494
		}
-
 
495
		/* Dumb brute force: A second pass for hacking off the last byte. */
-
 
496
		if(fr->af.encoding == MPG123_ENC_UNSIGNED_24)
-
 
497
		chop_fourth_byte(&fr->buffer);
-
 
498
	}
-
 
499
	else if(fr->af.encoding == MPG123_ENC_SIGNED_24)
-
 
500
	{
-
 
501
		/* We got 32 bit signed ... chop off for 24 bit signed. */
-
 
502
		chop_fourth_byte(&fr->buffer);
-
 
503
	}
-
 
504
#endif
-
 
505
#ifndef NO_16BIT
-
 
506
	if(fr->af.encoding == MPG123_ENC_UNSIGNED_16)
-
 
507
	{
-
 
508
		size_t i;
-
 
509
		short *ssamples;
-
 
510
		unsigned short *usamples;
-
 
511
		ssamples = (short*)fr->buffer.data;
-
 
512
		usamples = (unsigned short*)fr->buffer.data;
-
 
513
		debug("converting output to unsigned 16 bit integer");
-
 
514
		for(i=0; ibuffer.fill/sizeof(short); ++i)
-
 
515
		{
-
 
516
			long tmp = (long)ssamples[i]+32768;
-
 
517
			usamples[i] = (unsigned short)tmp;
-
 
518
		}
-
 
519
	}