Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | 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,
26
	MPG123_ENC_FLOAT_32,
27
	MPG123_ENC_FLOAT_64,
28
	MPG123_ENC_SIGNED_8,
29
	MPG123_ENC_UNSIGNED_8,
30
	MPG123_ENC_ULAW_8,
31
	MPG123_ENC_ALAW_8
32
};
33
 
34
/* Only one type of float is supported. */
35
# ifdef REAL_IS_FLOAT
36
#  define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_32
37
# else
38
#  define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_64
39
# endif
40
 
41
/* The list of actually possible encodings. */
42
static const int good_encodings[] =
43
{
44
#ifndef NO_16BIT
45
	MPG123_ENC_SIGNED_16,
46
	MPG123_ENC_UNSIGNED_16,
47
#endif
48
#ifndef NO_32BIT
49
	MPG123_ENC_SIGNED_32,
50
	MPG123_ENC_UNSIGNED_32,
51
#endif
52
#ifndef NO_REAL
53
	MPG123_FLOAT_ENC,
54
#endif
55
#ifndef NO_8BIT
56
	MPG123_ENC_SIGNED_8,
57
	MPG123_ENC_UNSIGNED_8,
58
	MPG123_ENC_ULAW_8,
59
	MPG123_ENC_ALAW_8
60
#endif
61
};
62
 
63
/* Check if encoding is a valid one in this build.
64
   ...lazy programming: linear search. */
65
static int good_enc(const int enc)
66
{
67
	size_t i;
68
	for(i=0; i
69
	if(enc == good_encodings[i]) return TRUE;
70
 
71
	return FALSE;
72
}
73
 
74
void attribute_align_arg mpg123_rates(const long **list, size_t *number)
75
{
76
	if(list   != NULL) *list   = my_rates;
77
	if(number != NULL) *number = sizeof(my_rates)/sizeof(long);
78
}
79
 
80
/* Now that's a bit tricky... One build of the library knows only a subset of the encodings. */
81
void attribute_align_arg mpg123_encodings(const int **list, size_t *number)
82
{
83
	if(list   != NULL) *list   = good_encodings;
84
	if(number != NULL) *number = sizeof(good_encodings)/sizeof(int);
85
}
86
 
87
/*	char audio_caps[NUM_CHANNELS][MPG123_RATES+1][MPG123_ENCODINGS]; */
88
 
89
static int rate2num(mpg123_pars *mp, long r)
90
{
91
	int i;
92
	for(i=0;i
93
#ifndef NO_NTOM
94
	if(mp && mp->force_rate != 0 && mp->force_rate == r) return MPG123_RATES;
95
#endif
96
 
97
	return -1;
98
}
99
 
100
static int enc2num(int encoding)
101
{
102
	int i;
103
	for(i=0;i
104
	if(my_encodings[i] == encoding) return i;
105
 
106
	return -1;
107
}
108
 
109
static int cap_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2)
110
{
111
	int i;
112
	int c  = nf->channels-1;
113
	int rn = rate2num(&fr->p, nf->rate);
114
	if(rn >= 0)	for(i=f0;i
115
	{
116
		if(fr->p.audio_caps[c][rn][i])
117
		{
118
			nf->encoding = my_encodings[i];
119
			return 1;
120
		}
121
	}
122
	return 0;
123
}
124
 
125
static int freq_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2)
126
{
127
	nf->rate = frame_freq(fr)>>fr->p.down_sample;
128
	if(cap_fit(fr,nf,f0,f2)) return 1;
129
	nf->rate>>=1;
130
	if(cap_fit(fr,nf,f0,f2)) return 1;
131
	nf->rate>>=1;
132
	if(cap_fit(fr,nf,f0,f2)) return 1;
133
#ifndef NO_NTOM
134
	/* If nothing worked, try the other rates, only without constrains from user.
135
	   In case you didn't guess: We enable flexible resampling if we find a working rate. */
136
	if(!fr->p.force_rate && fr->p.down_sample == 0)
137
	{
138
		int i;
139
		int c  = nf->channels-1;
140
		int rn = rate2num(&fr->p, frame_freq(fr));
141
		int rrn;
142
		if(rn < 0) return 0;
143
		/* Try higher rates first. */
144
		for(i=f0;i
145
		if(fr->p.audio_caps[c][rrn][i])
146
		{
147
			nf->rate = my_rates[rrn];
148
			nf->encoding = my_encodings[i];
149
			return 1;
150
		}
151
		/* Then lower rates. */
152
		for(i=f0;i=0; --rrn)
153
		if(fr->p.audio_caps[c][rrn][i])
154
		{
155
			nf->rate = my_rates[rrn];
156
			nf->encoding = my_encodings[i];
157
			return 1;
158
		}
159
	}
160
#endif
161
 
162
	return 0;
163
}
164
 
165
/* match constraints against supported audio formats, store possible setup in frame
166
  return: -1: error; 0: no format change; 1: format change */
167
int frame_output_format(mpg123_handle *fr)
168
{
169
	struct audioformat nf;
170
	int f0=0;
171
	int f2=MPG123_ENCODINGS; /* Omit the 32bit and float encodings. */
172
	mpg123_pars *p = &fr->p;
173
	/* initialize new format, encoding comes later */
174
	nf.channels = fr->stereo;
175
 
176
	/* All this forcing should be removed in favour of the capabilities table... */
177
	if(p->flags & MPG123_FORCE_8BIT)
178
	{
179
		f0 = 6;
180
		f2 = 10;
181
	}
182
	if(p->flags & MPG123_FORCE_FLOAT)
183
	{
184
		f0 = 4;
185
		f2 = 6;
186
	}
187
 
188
	/* force stereo is stronger */
189
	if(p->flags & MPG123_FORCE_MONO)   nf.channels = 1;
190
	if(p->flags & MPG123_FORCE_STEREO) nf.channels = 2;
191
 
192
#ifndef NO_NTOM
193
	if(p->force_rate)
194
	{
195
		nf.rate = p->force_rate;
196
		if(cap_fit(fr,&nf,f0,2)) goto end;            /* 16bit encodings */
197
		if(cap_fit(fr,&nf,f0<=2 ? 2 : f0,f2)) goto end; /*  8bit encodings */
198
 
199
		/* try again with different stereoness */
200
		if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1;
201
		else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2;
202
 
203
		if(cap_fit(fr,&nf,f0,2)) goto end;            /* 16bit encodings */
204
		if(cap_fit(fr,&nf,f0<=2 ? 2 : f0,f2)) goto end; /*  8bit encodings */
205
 
206
		if(NOQUIET)
207
		error3( "Unable to set up output format! Constraints: %s%s%liHz.",
208
		        ( p->flags & MPG123_FORCE_STEREO ? "stereo, " :
209
		          (p->flags & MPG123_FORCE_MONO ? "mono, " : "") ),
210
		        (p->flags & MPG123_FORCE_8BIT ? "8bit, " : ""),
211
		        p->force_rate );
212
/*		if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
213
 
214
		fr->err = MPG123_BAD_OUTFORMAT;
215
		return -1;
216
	}
217
#endif
218
 
219
	if(freq_fit(fr, &nf, f0, 2)) goto end; /* try rates with 16bit */
220
	if(freq_fit(fr, &nf, f0<=2 ? 2 : f0, f2)) goto end; /* ... 8bit */
221
 
222
	/* try again with different stereoness */
223
	if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1;
224
	else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2;
225
 
226
	if(freq_fit(fr, &nf, f0, 2)) goto end; /* try rates with 16bit */
227
	if(freq_fit(fr, &nf,  f0<=2 ? 2 : f0, f2)) goto end; /* ... 8bit */
228
 
229
	/* Here is the _bad_ end. */
230
	if(NOQUIET)
231
	{
232
		error5( "Unable to set up output format! Constraints: %s%s%li, %li or %liHz.",
233
		        ( p->flags & MPG123_FORCE_STEREO ? "stereo, " :
234
		          (p->flags & MPG123_FORCE_MONO ? "mono, "  : "") ),
235
		        (p->flags & MPG123_FORCE_8BIT  ? "8bit, " : ""),
236
		        frame_freq(fr),  frame_freq(fr)>>1, frame_freq(fr)>>2 );
237
	}
238
/*	if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
239
 
240
	fr->err = MPG123_BAD_OUTFORMAT;
241
	return -1;
242
 
243
end: /* Here is the _good_ end. */
244
	/* we had a successful match, now see if there's a change */
245
	if(nf.rate == fr->af.rate && nf.channels == fr->af.channels && nf.encoding == fr->af.encoding)
246
	{
247
		debug2("Old format with %i channels, and FORCE_MONO=%li", nf.channels, p->flags & MPG123_FORCE_MONO);
248
		return 0; /* the same format as before */
249
	}
250
	else /* a new format */
251
	{
252
		debug1("New format with %i channels!", nf.channels);
253
		fr->af.rate = nf.rate;
254
		fr->af.channels = nf.channels;
255
		fr->af.encoding = nf.encoding;
256
		/* 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)
262
		fr->af.encsize = 4;
263
		else if(fr->af.encoding == MPG123_ENC_FLOAT_64)
264
		fr->af.encsize = 8;
265
		else
266
		{
267
			if(NOQUIET) error1("Some unknown encoding??? (%i)", fr->af.encoding);
268
 
269
			fr->err = MPG123_BAD_OUTFORMAT;
270
			return -1;
271
		}
272
		return 1;
273
	}
274
}
275
 
276
int attribute_align_arg mpg123_format_none(mpg123_handle *mh)
277
{
278
	int r;
279
	if(mh == NULL) return MPG123_ERR;
280
 
281
	r = mpg123_fmt_none(&mh->p);
282
	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
283
 
284
	return r;
285
}
286
 
287
int attribute_align_arg mpg123_fmt_none(mpg123_pars *mp)
288
{
289
	if(mp == NULL) return MPG123_BAD_PARS;
290
 
291
	if(PVERB(mp,3)) fprintf(stderr, "Note: Disabling all formats.\n");
292
 
293
	memset(mp->audio_caps,0,sizeof(mp->audio_caps));
294
	return MPG123_OK;
295
}
296
 
297
int attribute_align_arg mpg123_format_all(mpg123_handle *mh)
298
{
299
	int r;
300
	if(mh == NULL) return MPG123_ERR;
301
 
302
	r = mpg123_fmt_all(&mh->p);
303
	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
304
 
305
	return r;
306
}
307
 
308
int attribute_align_arg mpg123_fmt_all(mpg123_pars *mp)
309
{
310
	size_t rate, ch, enc;
311
	if(mp == NULL) return MPG123_BAD_PARS;
312
 
313
	if(PVERB(mp,3)) fprintf(stderr, "Note: Enabling all formats.\n");
314
 
315
	for(ch=0;   ch   < NUM_CHANNELS;     ++ch)
316
	for(rate=0; rate < MPG123_RATES+1;   ++rate)
317
	for(enc=0;  enc  < MPG123_ENCODINGS; ++enc)
318
	mp->audio_caps[ch][rate][enc] = good_enc(my_encodings[enc]) ? 1 : 0;
319
 
320
	return MPG123_OK;
321
}
322
 
323
int attribute_align_arg mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings)
324
{
325
	int r;
326
	if(mh == NULL) return MPG123_ERR;
327
	r = mpg123_fmt(&mh->p, rate, channels, encodings);
328
	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
329
 
330
	return r;
331
}
332
 
333
int attribute_align_arg mpg123_fmt(mpg123_pars *mp, long rate, int channels, int encodings)
334
{
335
	int ie, ic, ratei;
336
	int ch[2] = {0, 1};
337
	if(mp == NULL) return MPG123_BAD_PARS;
338
	if(!(channels & (MPG123_MONO|MPG123_STEREO))) return MPG123_BAD_CHANNEL;
339
 
340
	if(PVERB(mp,3)) fprintf(stderr, "Note: Want to enable format %li/%i for encodings 0x%x.\n", rate, channels, encodings);
341
 
342
	if(!(channels & MPG123_STEREO)) ch[1] = 0;     /* {0,0} */
343
	else if(!(channels & MPG123_MONO)) ch[0] = 1; /* {1,1} */
344
	ratei = rate2num(mp, rate);
345
	if(ratei < 0) return MPG123_BAD_RATE;
346
 
347
	/* now match the encodings */
348
	for(ic = 0; ic < 2; ++ic)
349
	{
350
		for(ie = 0; ie < MPG123_ENCODINGS; ++ie)
351
		if(good_enc(my_encodings[ie]) && ((my_encodings[ie] & encodings) == my_encodings[ie]))
352
		mp->audio_caps[ch[ic]][ratei][ie] = 1;
353
 
354
		if(ch[0] == ch[1]) break; /* no need to do it again */
355
	}
356
 
357
	return MPG123_OK;
358
}
359
 
360
int attribute_align_arg mpg123_format_support(mpg123_handle *mh, long rate, int encoding)
361
{
362
	if(mh == NULL) return 0;
363
	else return mpg123_fmt_support(&mh->p, rate, encoding);
364
}
365
 
366
int attribute_align_arg mpg123_fmt_support(mpg123_pars *mp, long rate, int encoding)
367
{
368
	int ch = 0;
369
	int ratei, enci;
370
	ratei = rate2num(mp, rate);
371
	enci  = enc2num(encoding);
372
	if(mp == NULL || ratei < 0 || enci < 0) return 0;
373
	if(mp->audio_caps[0][ratei][enci]) ch |= MPG123_MONO;
374
	if(mp->audio_caps[1][ratei][enci]) ch |= MPG123_STEREO;
375
	return ch;
376
}
377
 
378
/* Call this one to ensure that any valid format will be something different than this. */
379
void invalidate_format(struct audioformat *af)
380
{
381
	af->encoding = 0;
382
	af->rate     = 0;
383
	af->channels = 0;
384
}
385
 
386
/* take into account: channels, bytes per sample -- NOT resampling!*/
387
off_t samples_to_bytes(mpg123_handle *fr , off_t s)
388
{
389
	return s * fr->af.encsize * fr->af.channels;
390
}
391
 
392
off_t bytes_to_samples(mpg123_handle *fr , off_t b)
393
{
394
	return b / fr->af.encsize / fr->af.channels;
395
}