Subversion Repositories Kolibri OS

Rev

Rev 1905 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1905 serge 1
/*
2
	format:routines to deal with audio (output) format
3
 
4
	copyright 2008-9 by the mpg123 project - free software under the terms of the LGPL 2.1
5
	see COPYING and AUTHORS files in distribution or http://mpg123.org
6
	initially written by Thomas Orgis, starting with parts of the old audio.c, with only faintly manage to show now
7
*/
8
 
9
#include "mpg123lib_intern.h"
10
#include "debug.h"
11
 
12
/* static int chans[NUM_CHANNELS] = { 1 , 2 }; */
13
static const long my_rates[MPG123_RATES] = /* only the standard rates */
14
{
15
	 8000, 11025, 12000,
16
	16000, 22050, 24000,
17
	32000, 44100, 48000,
18
};
19
 
20
static const int my_encodings[MPG123_ENCODINGS] =
21
{
22
	MPG123_ENC_SIGNED_16,
23
	MPG123_ENC_UNSIGNED_16,
24
	MPG123_ENC_SIGNED_32,
25
	MPG123_ENC_UNSIGNED_32,
3960 Serge 26
	MPG123_ENC_SIGNED_24,
27
	MPG123_ENC_UNSIGNED_24,
28
	/* Floating point range, see below. */
1905 serge 29
	MPG123_ENC_FLOAT_32,
30
	MPG123_ENC_FLOAT_64,
3960 Serge 31
	/* 8 bit range, see below. */
1905 serge 32
	MPG123_ENC_SIGNED_8,
33
	MPG123_ENC_UNSIGNED_8,
34
	MPG123_ENC_ULAW_8,
35
	MPG123_ENC_ALAW_8
36
};
37
 
3960 Serge 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 };
44
 
1905 serge 45
/* Only one type of float is supported. */
46
# ifdef REAL_IS_FLOAT
47
#  define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_32
48
# else
49
#  define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_64
50
# endif
51
 
52
/* The list of actually possible encodings. */
53
static const int good_encodings[] =
54
{
55
#ifndef NO_16BIT
56
	MPG123_ENC_SIGNED_16,
57
	MPG123_ENC_UNSIGNED_16,
58
#endif
59
#ifndef NO_32BIT
60
	MPG123_ENC_SIGNED_32,
61
	MPG123_ENC_UNSIGNED_32,
3960 Serge 62
	MPG123_ENC_SIGNED_24,
63
	MPG123_ENC_UNSIGNED_24,
1905 serge 64
#endif
65
#ifndef NO_REAL
66
	MPG123_FLOAT_ENC,
67
#endif
68
#ifndef NO_8BIT
69
	MPG123_ENC_SIGNED_8,
70
	MPG123_ENC_UNSIGNED_8,
71
	MPG123_ENC_ULAW_8,
72
	MPG123_ENC_ALAW_8
73
#endif
74
};
75
 
76
/* Check if encoding is a valid one in this build.
77
   ...lazy programming: linear search. */
78
static int good_enc(const int enc)
79
{
80
	size_t i;
81
	for(i=0; i
82
	if(enc == good_encodings[i]) return TRUE;
83
 
84
	return FALSE;
85
}
86
 
87
void attribute_align_arg mpg123_rates(const long **list, size_t *number)
88
{
89
	if(list   != NULL) *list   = my_rates;
90
	if(number != NULL) *number = sizeof(my_rates)/sizeof(long);
91
}
92
 
93
/* Now that's a bit tricky... One build of the library knows only a subset of the encodings. */
94
void attribute_align_arg mpg123_encodings(const int **list, size_t *number)
95
{
96
	if(list   != NULL) *list   = good_encodings;
97
	if(number != NULL) *number = sizeof(good_encodings)/sizeof(int);
98
}
99
 
3960 Serge 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
}
115
 
1905 serge 116
/*	char audio_caps[NUM_CHANNELS][MPG123_RATES+1][MPG123_ENCODINGS]; */
117
 
118
static int rate2num(mpg123_pars *mp, long r)
119
{
120
	int i;
121
	for(i=0;i
122
#ifndef NO_NTOM
123
	if(mp && mp->force_rate != 0 && mp->force_rate == r) return MPG123_RATES;
124
#endif
125
 
126
	return -1;
127
}
128
 
129
static int enc2num(int encoding)
130
{
131
	int i;
132
	for(i=0;i
133
	if(my_encodings[i] == encoding) return i;
134
 
135
	return -1;
136
}
137
 
138
static int cap_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2)
139
{
140
	int i;
141
	int c  = nf->channels-1;
142
	int rn = rate2num(&fr->p, nf->rate);
143
	if(rn >= 0)	for(i=f0;i
144
	{
145
		if(fr->p.audio_caps[c][rn][i])
146
		{
147
			nf->encoding = my_encodings[i];
148
			return 1;
149
		}
150
	}
151
	return 0;
152
}
153
 
154
static int freq_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2)
155
{
156
	nf->rate = frame_freq(fr)>>fr->p.down_sample;
157
	if(cap_fit(fr,nf,f0,f2)) return 1;
3960 Serge 158
	if(fr->p.flags & MPG123_AUTO_RESAMPLE)
159
	{
160
		nf->rate>>=1;
161
		if(cap_fit(fr,nf,f0,f2)) return 1;
162
		nf->rate>>=1;
163
		if(cap_fit(fr,nf,f0,f2)) return 1;
164
	}
1905 serge 165
#ifndef NO_NTOM
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. */
3960 Serge 168
	if(  fr->p.flags & MPG123_AUTO_RESAMPLE &&
169
	    !fr->p.force_rate && fr->p.down_sample == 0)
1905 serge 170
	{
171
		int i;
172
		int c  = nf->channels-1;
173
		int rn = rate2num(&fr->p, frame_freq(fr));
174
		int rrn;
175
		if(rn < 0) return 0;
176
		/* Try higher rates first. */
177
		for(i=f0;i
178
		if(fr->p.audio_caps[c][rrn][i])
179
		{
180
			nf->rate = my_rates[rrn];
181
			nf->encoding = my_encodings[i];
182
			return 1;
183
		}
184
		/* Then lower rates. */
185
		for(i=f0;i=0; --rrn)
186
		if(fr->p.audio_caps[c][rrn][i])
187
		{
188
			nf->rate = my_rates[rrn];
189
			nf->encoding = my_encodings[i];
190
			return 1;
191
		}
192
	}
193
#endif
194
 
195
	return 0;
196
}
197
 
198
/* match constraints against supported audio formats, store possible setup in frame
199
  return: -1: error; 0: no format change; 1: format change */
200
int frame_output_format(mpg123_handle *fr)
201
{
202
	struct audioformat nf;
203
	int f0=0;
204
	int f2=MPG123_ENCODINGS; /* Omit the 32bit and float encodings. */
205
	mpg123_pars *p = &fr->p;
206
	/* initialize new format, encoding comes later */
207
	nf.channels = fr->stereo;
208
 
209
	/* All this forcing should be removed in favour of the capabilities table... */
210
	if(p->flags & MPG123_FORCE_8BIT)
211
	{
3960 Serge 212
		f0 = enc_8bit_range[0];
213
		f2 = enc_8bit_range[1];
1905 serge 214
	}
215
	if(p->flags & MPG123_FORCE_FLOAT)
216
	{
3960 Serge 217
		f0 = enc_float_range[0];
218
		f2 = enc_float_range[1];
1905 serge 219
	}
220
 
221
	/* force stereo is stronger */
222
	if(p->flags & MPG123_FORCE_MONO)   nf.channels = 1;
223
	if(p->flags & MPG123_FORCE_STEREO) nf.channels = 2;
224
 
225
#ifndef NO_NTOM
226
	if(p->force_rate)
227
	{
228
		nf.rate = p->force_rate;
229
		if(cap_fit(fr,&nf,f0,2)) goto end;            /* 16bit encodings */
230
		if(cap_fit(fr,&nf,f0<=2 ? 2 : f0,f2)) goto end; /*  8bit encodings */
231
 
232
		/* try again with different stereoness */
233
		if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1;
234
		else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2;
235
 
236
		if(cap_fit(fr,&nf,f0,2)) goto end;            /* 16bit encodings */
237
		if(cap_fit(fr,&nf,f0<=2 ? 2 : f0,f2)) goto end; /*  8bit encodings */
238
 
239
		if(NOQUIET)
240
		error3( "Unable to set up output format! Constraints: %s%s%liHz.",
241
		        ( p->flags & MPG123_FORCE_STEREO ? "stereo, " :
242
		          (p->flags & MPG123_FORCE_MONO ? "mono, " : "") ),
243
		        (p->flags & MPG123_FORCE_8BIT ? "8bit, " : ""),
244
		        p->force_rate );
245
/*		if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
246
 
247
		fr->err = MPG123_BAD_OUTFORMAT;
248
		return -1;
249
	}
250
#endif
251
 
252
	if(freq_fit(fr, &nf, f0, 2)) goto end; /* try rates with 16bit */
253
	if(freq_fit(fr, &nf, f0<=2 ? 2 : f0, f2)) goto end; /* ... 8bit */
254
 
255
	/* try again with different stereoness */
256
	if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1;
257
	else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2;
258
 
259
	if(freq_fit(fr, &nf, f0, 2)) goto end; /* try rates with 16bit */
260
	if(freq_fit(fr, &nf,  f0<=2 ? 2 : f0, f2)) goto end; /* ... 8bit */
261
 
262
	/* Here is the _bad_ end. */
263
	if(NOQUIET)
264
	{
265
		error5( "Unable to set up output format! Constraints: %s%s%li, %li or %liHz.",
266
		        ( p->flags & MPG123_FORCE_STEREO ? "stereo, " :
267
		          (p->flags & MPG123_FORCE_MONO ? "mono, "  : "") ),
268
		        (p->flags & MPG123_FORCE_8BIT  ? "8bit, " : ""),
269
		        frame_freq(fr),  frame_freq(fr)>>1, frame_freq(fr)>>2 );
270
	}
271
/*	if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
272
 
273
	fr->err = MPG123_BAD_OUTFORMAT;
274
	return -1;
275
 
276
end: /* Here is the _good_ end. */
277
	/* we had a successful match, now see if there's a change */
278
	if(nf.rate == fr->af.rate && nf.channels == fr->af.channels && nf.encoding == fr->af.encoding)
279
	{
280
		debug2("Old format with %i channels, and FORCE_MONO=%li", nf.channels, p->flags & MPG123_FORCE_MONO);
281
		return 0; /* the same format as before */
282
	}
283
	else /* a new format */
284
	{
285
		debug1("New format with %i channels!", nf.channels);
286
		fr->af.rate = nf.rate;
287
		fr->af.channels = nf.channels;
288
		fr->af.encoding = nf.encoding;
289
		/* Cache the size of one sample in bytes, for ease of use. */
3960 Serge 290
		fr->af.encsize = mpg123_encsize(fr->af.encoding);
291
		if(fr->af.encsize < 1)
1905 serge 292
		{
293
			if(NOQUIET) error1("Some unknown encoding??? (%i)", fr->af.encoding);
294
 
295
			fr->err = MPG123_BAD_OUTFORMAT;
296
			return -1;
297
		}
298
		return 1;
299
	}
300
}
301
 
302
int attribute_align_arg mpg123_format_none(mpg123_handle *mh)
303
{
304
	int r;
305
	if(mh == NULL) return MPG123_ERR;
306
 
307
	r = mpg123_fmt_none(&mh->p);
308
	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
309
 
310
	return r;
311
}
312
 
313
int attribute_align_arg mpg123_fmt_none(mpg123_pars *mp)
314
{
315
	if(mp == NULL) return MPG123_BAD_PARS;
316
 
317
	if(PVERB(mp,3)) fprintf(stderr, "Note: Disabling all formats.\n");
318
 
319
	memset(mp->audio_caps,0,sizeof(mp->audio_caps));
320
	return MPG123_OK;
321
}
322
 
323
int attribute_align_arg mpg123_format_all(mpg123_handle *mh)
324
{
325
	int r;
326
	if(mh == NULL) return MPG123_ERR;
327
 
328
	r = mpg123_fmt_all(&mh->p);
329
	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
330
 
331
	return r;
332
}
333
 
334
int attribute_align_arg mpg123_fmt_all(mpg123_pars *mp)
335
{
336
	size_t rate, ch, enc;
337
	if(mp == NULL) return MPG123_BAD_PARS;
338
 
339
	if(PVERB(mp,3)) fprintf(stderr, "Note: Enabling all formats.\n");
340
 
341
	for(ch=0;   ch   < NUM_CHANNELS;     ++ch)
342
	for(rate=0; rate < MPG123_RATES+1;   ++rate)
343
	for(enc=0;  enc  < MPG123_ENCODINGS; ++enc)
344
	mp->audio_caps[ch][rate][enc] = good_enc(my_encodings[enc]) ? 1 : 0;
345
 
346
	return MPG123_OK;
347
}
348
 
349
int attribute_align_arg mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings)
350
{
351
	int r;
352
	if(mh == NULL) return MPG123_ERR;
353
	r = mpg123_fmt(&mh->p, rate, channels, encodings);
354
	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
355
 
356
	return r;
357
}
358
 
359
int attribute_align_arg mpg123_fmt(mpg123_pars *mp, long rate, int channels, int encodings)
360
{
361
	int ie, ic, ratei;
362
	int ch[2] = {0, 1};
363
	if(mp == NULL) return MPG123_BAD_PARS;
364
	if(!(channels & (MPG123_MONO|MPG123_STEREO))) return MPG123_BAD_CHANNEL;
365
 
366
	if(PVERB(mp,3)) fprintf(stderr, "Note: Want to enable format %li/%i for encodings 0x%x.\n", rate, channels, encodings);
367
 
368
	if(!(channels & MPG123_STEREO)) ch[1] = 0;     /* {0,0} */
369
	else if(!(channels & MPG123_MONO)) ch[0] = 1; /* {1,1} */
370
	ratei = rate2num(mp, rate);
371
	if(ratei < 0) return MPG123_BAD_RATE;
372
 
373
	/* now match the encodings */
374
	for(ic = 0; ic < 2; ++ic)
375
	{
376
		for(ie = 0; ie < MPG123_ENCODINGS; ++ie)
377
		if(good_enc(my_encodings[ie]) && ((my_encodings[ie] & encodings) == my_encodings[ie]))
378
		mp->audio_caps[ch[ic]][ratei][ie] = 1;
379
 
380
		if(ch[0] == ch[1]) break; /* no need to do it again */
381
	}
382
 
383
	return MPG123_OK;
384
}
385
 
386
int attribute_align_arg mpg123_format_support(mpg123_handle *mh, long rate, int encoding)
387
{
388
	if(mh == NULL) return 0;
389
	else return mpg123_fmt_support(&mh->p, rate, encoding);
390
}
391
 
392
int attribute_align_arg mpg123_fmt_support(mpg123_pars *mp, long rate, int encoding)
393
{
394
	int ch = 0;
395
	int ratei, enci;
396
	ratei = rate2num(mp, rate);
397
	enci  = enc2num(encoding);
398
	if(mp == NULL || ratei < 0 || enci < 0) return 0;
399
	if(mp->audio_caps[0][ratei][enci]) ch |= MPG123_MONO;
400
	if(mp->audio_caps[1][ratei][enci]) ch |= MPG123_STEREO;
401
	return ch;
402
}
403
 
404
/* Call this one to ensure that any valid format will be something different than this. */
405
void invalidate_format(struct audioformat *af)
406
{
407
	af->encoding = 0;
408
	af->rate     = 0;
409
	af->channels = 0;
410
}
411
 
3960 Serge 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
}
420
 
1905 serge 421
/* take into account: channels, bytes per sample -- NOT resampling!*/
422
off_t samples_to_bytes(mpg123_handle *fr , off_t s)
423
{
424
	return s * fr->af.encsize * fr->af.channels;
425
}
426
 
427
off_t bytes_to_samples(mpg123_handle *fr , off_t b)
428
{
429
	return b / fr->af.encsize / fr->af.channels;
430
}
3960 Serge 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
	}
520
#endif
521
}