xref: /reactos/sdk/lib/3rdparty/libmpg123/format.c (revision 845faec4)
1 /*
2 	format:routines to deal with audio (output) format
3 
4 	copyright 2008-14 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 	A Major change from mpg123 <= 1.18 is that all encodings are only really
9 	disabled when done so via specific build configuration. Otherwise, the
10 	missing support of decoders to produce a certain format is augmented by
11 	postprocessing that converts the samples. This means happily creating
12 	data with higher resolution from less accurate decoder output.
13 
14 	The main point is to still offer float encoding when the decoding core uses
15 	a fixed point representation that has only 16 bit output. Actually, that's
16 	the only point: A fixed-point build needs to create float from 16 bit, also
17 	32 or 24 bit from the same source. That's all there is to it: Everything else
18 	is covered by fallback synth functions. It may be a further step to check if
19 	there are cases where conversion in postprocessing works well enough to omit
20 	a certain specialized decoder ... but usually, they are justified by some
21 	special way to get from float to integer to begin with.
22 
23 	I won't cover the case of faking double output with float/s16 decoders here.
24 	Double precision output is a thing for experimental builds anyway. Mostly
25 	theoretical and without a point.
26 */
27 
28 #include "mpg123lib_intern.h"
29 #include "debug.h"
30 
31 /* static int chans[NUM_CHANNELS] = { 1 , 2 }; */
32 static const long my_rates[MPG123_RATES] = /* only the standard rates */
33 {
34 	 8000, 11025, 12000,
35 	16000, 22050, 24000,
36 	32000, 44100, 48000,
37 };
38 
39 static const int my_encodings[MPG123_ENCODINGS] =
40 {
41 	MPG123_ENC_SIGNED_16,
42 	MPG123_ENC_UNSIGNED_16,
43 	MPG123_ENC_SIGNED_32,
44 	MPG123_ENC_UNSIGNED_32,
45 	MPG123_ENC_SIGNED_24,
46 	MPG123_ENC_UNSIGNED_24,
47 	/* Floating point range, see below. */
48 	MPG123_ENC_FLOAT_32,
49 	MPG123_ENC_FLOAT_64,
50 	/* 8 bit range, see below. */
51 	MPG123_ENC_SIGNED_8,
52 	MPG123_ENC_UNSIGNED_8,
53 	MPG123_ENC_ULAW_8,
54 	MPG123_ENC_ALAW_8
55 };
56 
57 /* Make that match the above table.
58    And yes, I still don't like this kludgy stuff. */
59 /* range[0] <= i < range[1] for forced floating point */
60 static const int enc_float_range[2] = { 6, 8 };
61 /* same for 8 bit encodings */
62 static const int enc_8bit_range[2] = { 8, 12 };
63 
64 /*
65 	Only one type of float is supported.
66 	Actually, double is a very special experimental case not occuring in normal
67 	builds. Might actually get rid of it.
68 
69 	Remember here: Also with REAL_IS_FIXED, I want to be able to produce float
70 	output (f32) via post-processing.
71 */
72 # ifdef REAL_IS_DOUBLE
73 #  define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_64
74 # else
75 #  define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_32
76 # endif
77 
78 /* The list of actually possible encodings. */
79 static const int good_encodings[] =
80 {
81 #ifndef NO_16BIT
82 	MPG123_ENC_SIGNED_16,
83 	MPG123_ENC_UNSIGNED_16,
84 #endif
85 #ifndef NO_32BIT
86 	MPG123_ENC_SIGNED_32,
87 	MPG123_ENC_UNSIGNED_32,
88 	MPG123_ENC_SIGNED_24,
89 	MPG123_ENC_UNSIGNED_24,
90 #endif
91 #ifndef NO_REAL
92 	MPG123_FLOAT_ENC,
93 #endif
94 #ifndef NO_8BIT
95 	MPG123_ENC_SIGNED_8,
96 	MPG123_ENC_UNSIGNED_8,
97 	MPG123_ENC_ULAW_8,
98 	MPG123_ENC_ALAW_8
99 #endif
100 };
101 
102 /* Check if encoding is a valid one in this build.
103    ...lazy programming: linear search. */
104 static int good_enc(const int enc)
105 {
106 	size_t i;
107 	for(i=0; i<sizeof(good_encodings)/sizeof(int); ++i)
108 	if(enc == good_encodings[i]) return TRUE;
109 
110 	return FALSE;
111 }
112 
113 void attribute_align_arg mpg123_rates(const long **list, size_t *number)
114 {
115 	if(list   != NULL) *list   = my_rates;
116 	if(number != NULL) *number = sizeof(my_rates)/sizeof(long);
117 }
118 
119 /* Now that's a bit tricky... One build of the library knows only a subset of the encodings. */
120 void attribute_align_arg mpg123_encodings(const int **list, size_t *number)
121 {
122 	if(list   != NULL) *list   = good_encodings;
123 	if(number != NULL) *number = sizeof(good_encodings)/sizeof(int);
124 }
125 
126 int attribute_align_arg mpg123_encsize(int encoding)
127 {
128 	return MPG123_SAMPLESIZE(encoding);
129 }
130 
131 /*	char audio_caps[NUM_CHANNELS][MPG123_RATES+1][MPG123_ENCODINGS]; */
132 
133 static int rate2num(mpg123_pars *mp, long r)
134 {
135 	int i;
136 	for(i=0;i<MPG123_RATES;i++) if(my_rates[i] == r) return i;
137 #ifndef NO_NTOM
138 	if(mp && mp->force_rate != 0 && mp->force_rate == r) return MPG123_RATES;
139 #endif
140 
141 	return -1;
142 }
143 
144 static int enc2num(int encoding)
145 {
146 	int i;
147 	for(i=0;i<MPG123_ENCODINGS;++i)
148 	if(my_encodings[i] == encoding) return i;
149 
150 	return -1;
151 }
152 
153 static int cap_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2)
154 {
155 	int i;
156 	int c  = nf->channels-1;
157 	int rn = rate2num(&fr->p, nf->rate);
158 	if(rn >= 0)	for(i=f0;i<f2;i++)
159 	{
160 		if(fr->p.audio_caps[c][rn][i])
161 		{
162 			nf->encoding = my_encodings[i];
163 			return 1;
164 		}
165 	}
166 	return 0;
167 }
168 
169 static int freq_fit(mpg123_handle *fr, struct audioformat *nf, int f0, int f2)
170 {
171 	nf->rate = frame_freq(fr)>>fr->p.down_sample;
172 	if(cap_fit(fr,nf,f0,f2)) return 1;
173 	if(fr->p.flags & MPG123_AUTO_RESAMPLE)
174 	{
175 		nf->rate>>=1;
176 		if(cap_fit(fr,nf,f0,f2)) return 1;
177 		nf->rate>>=1;
178 		if(cap_fit(fr,nf,f0,f2)) return 1;
179 	}
180 #ifndef NO_NTOM
181 	/* If nothing worked, try the other rates, only without constrains from user.
182 	   In case you didn't guess: We enable flexible resampling if we find a working rate. */
183 	if(  fr->p.flags & MPG123_AUTO_RESAMPLE &&
184 	    !fr->p.force_rate && fr->p.down_sample == 0)
185 	{
186 		int i;
187 		int c  = nf->channels-1;
188 		int rn = rate2num(&fr->p, frame_freq(fr));
189 		int rrn;
190 		if(rn < 0) return 0;
191 		/* Try higher rates first. */
192 		for(i=f0;i<f2;i++) for(rrn=rn+1; rrn<MPG123_RATES; ++rrn)
193 		if(fr->p.audio_caps[c][rrn][i])
194 		{
195 			nf->rate = my_rates[rrn];
196 			nf->encoding = my_encodings[i];
197 			return 1;
198 		}
199 		/* Then lower rates. */
200 		for(i=f0;i<f2;i++) for(rrn=rn-1; rrn>=0; --rrn)
201 		if(fr->p.audio_caps[c][rrn][i])
202 		{
203 			nf->rate = my_rates[rrn];
204 			nf->encoding = my_encodings[i];
205 			return 1;
206 		}
207 	}
208 #endif
209 
210 	return 0;
211 }
212 
213 /* match constraints against supported audio formats, store possible setup in frame
214   return: -1: error; 0: no format change; 1: format change */
215 int frame_output_format(mpg123_handle *fr)
216 {
217 	struct audioformat nf;
218 	int f0=0;
219 	int f2=MPG123_ENCODINGS; /* Omit the 32bit and float encodings. */
220 	mpg123_pars *p = &fr->p;
221 	/* initialize new format, encoding comes later */
222 	nf.channels = fr->stereo;
223 
224 	/* All this forcing should be removed in favour of the capabilities table... */
225 	if(p->flags & MPG123_FORCE_8BIT)
226 	{
227 		f0 = enc_8bit_range[0];
228 		f2 = enc_8bit_range[1];
229 	}
230 	if(p->flags & MPG123_FORCE_FLOAT)
231 	{
232 		f0 = enc_float_range[0];
233 		f2 = enc_float_range[1];
234 	}
235 
236 	/* force stereo is stronger */
237 	if(p->flags & MPG123_FORCE_MONO)   nf.channels = 1;
238 	if(p->flags & MPG123_FORCE_STEREO) nf.channels = 2;
239 
240 #ifndef NO_NTOM
241 	if(p->force_rate)
242 	{
243 		nf.rate = p->force_rate;
244 		if(cap_fit(fr,&nf,f0,2)) goto end;            /* 16bit encodings */
245 		if(cap_fit(fr,&nf,f0<=2 ? 2 : f0,f2)) goto end; /*  8bit encodings */
246 
247 		/* try again with different stereoness */
248 		if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1;
249 		else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2;
250 
251 		if(cap_fit(fr,&nf,f0,2)) goto end;            /* 16bit encodings */
252 		if(cap_fit(fr,&nf,f0<=2 ? 2 : f0,f2)) goto end; /*  8bit encodings */
253 
254 		if(NOQUIET)
255 		error3( "Unable to set up output format! Constraints: %s%s%liHz.",
256 		        ( p->flags & MPG123_FORCE_STEREO ? "stereo, " :
257 		          (p->flags & MPG123_FORCE_MONO ? "mono, " : "") ),
258 		        (p->flags & MPG123_FORCE_8BIT ? "8bit, " : ""),
259 		        p->force_rate );
260 /*		if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
261 
262 		fr->err = MPG123_BAD_OUTFORMAT;
263 		return -1;
264 	}
265 #endif
266 
267 	if(freq_fit(fr, &nf, f0, 2)) goto end; /* try rates with 16bit */
268 	if(freq_fit(fr, &nf, f0<=2 ? 2 : f0, f2)) goto end; /* ... 8bit */
269 
270 	/* try again with different stereoness */
271 	if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1;
272 	else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2;
273 
274 	if(freq_fit(fr, &nf, f0, 2)) goto end; /* try rates with 16bit */
275 	if(freq_fit(fr, &nf,  f0<=2 ? 2 : f0, f2)) goto end; /* ... 8bit */
276 
277 	/* Here is the _bad_ end. */
278 	if(NOQUIET)
279 	{
280 		error5( "Unable to set up output format! Constraints: %s%s%li, %li or %liHz.",
281 		        ( p->flags & MPG123_FORCE_STEREO ? "stereo, " :
282 		          (p->flags & MPG123_FORCE_MONO ? "mono, "  : "") ),
283 		        (p->flags & MPG123_FORCE_8BIT  ? "8bit, " : ""),
284 		        frame_freq(fr),  frame_freq(fr)>>1, frame_freq(fr)>>2 );
285 	}
286 /*	if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
287 
288 	fr->err = MPG123_BAD_OUTFORMAT;
289 	return -1;
290 
291 end: /* Here is the _good_ end. */
292 	/* we had a successful match, now see if there's a change */
293 	if(nf.rate == fr->af.rate && nf.channels == fr->af.channels && nf.encoding == fr->af.encoding)
294 	{
295 		debug2("Old format with %i channels, and FORCE_MONO=%li", nf.channels, p->flags & MPG123_FORCE_MONO);
296 		return 0; /* the same format as before */
297 	}
298 	else /* a new format */
299 	{
300 		debug1("New format with %i channels!", nf.channels);
301 		fr->af.rate = nf.rate;
302 		fr->af.channels = nf.channels;
303 		fr->af.encoding = nf.encoding;
304 		/* Cache the size of one sample in bytes, for ease of use. */
305 		fr->af.encsize = mpg123_encsize(fr->af.encoding);
306 		if(fr->af.encsize < 1)
307 		{
308 			if(NOQUIET) error1("Some unknown encoding??? (%i)", fr->af.encoding);
309 
310 			fr->err = MPG123_BAD_OUTFORMAT;
311 			return -1;
312 		}
313 		/* Set up the decoder synth format. Might differ. */
314 #ifdef NO_SYNTH32
315 		/* Without high-precision synths, 16 bit signed is the basis for
316 		   everything higher than 8 bit. */
317 		if(fr->af.encsize > 2)
318 		fr->af.dec_enc = MPG123_ENC_SIGNED_16;
319 		else
320 		{
321 #endif
322 			switch(fr->af.encoding)
323 			{
324 #ifndef NO_32BIT
325 			case MPG123_ENC_SIGNED_24:
326 			case MPG123_ENC_UNSIGNED_24:
327 			case MPG123_ENC_UNSIGNED_32:
328 				fr->af.dec_enc = MPG123_ENC_SIGNED_32;
329 			break;
330 #endif
331 #ifndef NO_16BIT
332 			case MPG123_ENC_UNSIGNED_16:
333 				fr->af.dec_enc = MPG123_ENC_SIGNED_16;
334 			break;
335 #endif
336 			default:
337 				fr->af.dec_enc = fr->af.encoding;
338 			}
339 #ifdef NO_SYNTH32
340 		}
341 #endif
342 		fr->af.dec_encsize = mpg123_encsize(fr->af.dec_enc);
343 		return 1;
344 	}
345 }
346 
347 int attribute_align_arg mpg123_format_none(mpg123_handle *mh)
348 {
349 	int r;
350 	if(mh == NULL) return MPG123_BAD_HANDLE;
351 
352 	r = mpg123_fmt_none(&mh->p);
353 	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
354 
355 	return r;
356 }
357 
358 int attribute_align_arg mpg123_fmt_none(mpg123_pars *mp)
359 {
360 	if(mp == NULL) return MPG123_BAD_PARS;
361 
362 	if(PVERB(mp,3)) fprintf(stderr, "Note: Disabling all formats.\n");
363 
364 	memset(mp->audio_caps,0,sizeof(mp->audio_caps));
365 	return MPG123_OK;
366 }
367 
368 int attribute_align_arg mpg123_format_all(mpg123_handle *mh)
369 {
370 	int r;
371 	if(mh == NULL) return MPG123_BAD_HANDLE;
372 
373 	r = mpg123_fmt_all(&mh->p);
374 	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
375 
376 	return r;
377 }
378 
379 int attribute_align_arg mpg123_fmt_all(mpg123_pars *mp)
380 {
381 	size_t rate, ch, enc;
382 	if(mp == NULL) return MPG123_BAD_PARS;
383 
384 	if(PVERB(mp,3)) fprintf(stderr, "Note: Enabling all formats.\n");
385 
386 	for(ch=0;   ch   < NUM_CHANNELS;     ++ch)
387 	for(rate=0; rate < MPG123_RATES+1;   ++rate)
388 	for(enc=0;  enc  < MPG123_ENCODINGS; ++enc)
389 	mp->audio_caps[ch][rate][enc] = good_enc(my_encodings[enc]) ? 1 : 0;
390 
391 	return MPG123_OK;
392 }
393 
394 int attribute_align_arg mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings)
395 {
396 	int r;
397 	if(mh == NULL) return MPG123_BAD_HANDLE;
398 	r = mpg123_fmt(&mh->p, rate, channels, encodings);
399 	if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
400 
401 	return r;
402 }
403 
404 int attribute_align_arg mpg123_fmt(mpg123_pars *mp, long rate, int channels, int encodings)
405 {
406 	int ie, ic, ratei;
407 	int ch[2] = {0, 1};
408 	if(mp == NULL) return MPG123_BAD_PARS;
409 	if(!(channels & (MPG123_MONO|MPG123_STEREO))) return MPG123_BAD_CHANNEL;
410 
411 	if(PVERB(mp,3)) fprintf(stderr, "Note: Want to enable format %li/%i for encodings 0x%x.\n", rate, channels, encodings);
412 
413 	if(!(channels & MPG123_STEREO)) ch[1] = 0;     /* {0,0} */
414 	else if(!(channels & MPG123_MONO)) ch[0] = 1; /* {1,1} */
415 	ratei = rate2num(mp, rate);
416 	if(ratei < 0) return MPG123_BAD_RATE;
417 
418 	/* now match the encodings */
419 	for(ic = 0; ic < 2; ++ic)
420 	{
421 		for(ie = 0; ie < MPG123_ENCODINGS; ++ie)
422 		if(good_enc(my_encodings[ie]) && ((my_encodings[ie] & encodings) == my_encodings[ie]))
423 		mp->audio_caps[ch[ic]][ratei][ie] = 1;
424 
425 		if(ch[0] == ch[1]) break; /* no need to do it again */
426 	}
427 
428 	return MPG123_OK;
429 }
430 
431 int attribute_align_arg mpg123_format_support(mpg123_handle *mh, long rate, int encoding)
432 {
433 	if(mh == NULL) return 0;
434 	else return mpg123_fmt_support(&mh->p, rate, encoding);
435 }
436 
437 int attribute_align_arg mpg123_fmt_support(mpg123_pars *mp, long rate, int encoding)
438 {
439 	int ch = 0;
440 	int ratei, enci;
441 	ratei = rate2num(mp, rate);
442 	enci  = enc2num(encoding);
443 	if(mp == NULL || ratei < 0 || enci < 0) return 0;
444 	if(mp->audio_caps[0][ratei][enci]) ch |= MPG123_MONO;
445 	if(mp->audio_caps[1][ratei][enci]) ch |= MPG123_STEREO;
446 	return ch;
447 }
448 
449 /* Call this one to ensure that any valid format will be something different than this. */
450 void invalidate_format(struct audioformat *af)
451 {
452 	af->encoding = 0;
453 	af->rate     = 0;
454 	af->channels = 0;
455 }
456 
457 /* Number of bytes the decoder produces. */
458 off_t decoder_synth_bytes(mpg123_handle *fr, off_t s)
459 {
460 	return s * fr->af.dec_encsize * fr->af.channels;
461 }
462 
463 /* Samples/bytes for output buffer after post-processing. */
464 /* take into account: channels, bytes per sample -- NOT resampling!*/
465 off_t samples_to_bytes(mpg123_handle *fr , off_t s)
466 {
467 	return s * fr->af.encsize * fr->af.channels;
468 }
469 
470 off_t bytes_to_samples(mpg123_handle *fr , off_t b)
471 {
472 	return b / fr->af.encsize / fr->af.channels;
473 }
474 
475 /* Number of bytes needed for decoding _and_ post-processing. */
476 off_t outblock_bytes(mpg123_handle *fr, off_t s)
477 {
478 	int encsize = (fr->af.encoding & MPG123_ENC_24)
479 	? 4 /* Intermediate 32 bit. */
480 	: (fr->af.encsize > fr->af.dec_encsize
481 		? fr->af.encsize
482 		: fr->af.dec_encsize);
483 	return s * encsize * fr->af.channels;
484 }
485 
486 #ifndef NO_32BIT
487 /* Remove every fourth byte, facilitating conversion from 32 bit to 24 bit integers.
488    This has to be aware of endianness, of course. */
489 static void chop_fourth_byte(struct outbuffer *buf)
490 {
491 	unsigned char *wpos = buf->data;
492 	unsigned char *rpos = buf->data;
493 #ifdef WORDS_BIGENDIAN
494 	while((size_t) (rpos - buf->data + 4) <= buf->fill)
495 	{
496 		/* Really stupid: Copy, increment. Byte per byte. */
497 		*wpos = *rpos;
498 		wpos++; rpos++;
499 		*wpos = *rpos;
500 		wpos++; rpos++;
501 		*wpos = *rpos;
502 		wpos++; rpos++;
503 		rpos++; /* Skip the lowest byte (last). */
504 	}
505 #else
506 	while((size_t) (rpos - buf->data + 4) <= buf->fill)
507 	{
508 		/* Really stupid: Copy, increment. Byte per byte. */
509 		rpos++; /* Skip the lowest byte (first). */
510 		*wpos = *rpos;
511 		wpos++; rpos++;
512 		*wpos = *rpos;
513 		wpos++; rpos++;
514 		*wpos = *rpos;
515 		wpos++; rpos++;
516 	}
517 #endif
518 	buf->fill = wpos-buf->data;
519 }
520 
521 static void conv_s32_to_u32(struct outbuffer *buf)
522 {
523 	size_t i;
524 	int32_t  *ssamples = (int32_t*)  buf->data;
525 	uint32_t *usamples = (uint32_t*) buf->data;
526 	size_t count = buf->fill/sizeof(int32_t);
527 
528 	for(i=0; i<count; ++i)
529 	{
530 		/* Different strategy since we don't have a larger type at hand.
531 			 Also watch out for silly +-1 fun because integer constants are signed in C90! */
532 		if(ssamples[i] >= 0)
533 		usamples[i] = (uint32_t)ssamples[i] + 2147483647+1;
534 		/* The smallest value goes zero. */
535 		else if(ssamples[i] == ((int32_t)-2147483647-1))
536 		usamples[i] = 0;
537 		/* Now -value is in the positive range of signed int ... so it's a possible value at all. */
538 		else
539 		usamples[i] = (uint32_t)2147483647+1 - (uint32_t)(-ssamples[i]);
540 	}
541 }
542 
543 #endif
544 
545 
546 /* We always assume that whole numbers are written!
547    partials will be cut out. */
548 
549 static const char *bufsizeerr = "Fatal: Buffer too small for postprocessing!";
550 
551 
552 #ifndef NO_16BIT
553 
554 static void conv_s16_to_u16(struct outbuffer *buf)
555 {
556 	size_t i;
557 	int16_t  *ssamples = (int16_t*) buf->data;
558 	uint16_t *usamples = (uint16_t*)buf->data;
559 	size_t count = buf->fill/sizeof(int16_t);
560 
561 	for(i=0; i<count; ++i)
562 	{
563 		long tmp = (long)ssamples[i]+32768;
564 		usamples[i] = (uint16_t)tmp;
565 	}
566 }
567 
568 #ifndef NO_REAL
569 static void conv_s16_to_f32(struct outbuffer *buf)
570 {
571 	ssize_t i;
572 	int16_t *in = (int16_t*) buf->data;
573 	float  *out = (float*)   buf->data;
574 	size_t count = buf->fill/sizeof(int16_t);
575 	/* Does that make any sense? In x86, there is an actual instruction to divide
576 	   float by integer ... but then, if we have that FPU, we don't really need
577 	   fixed point decoder hacks ...? */
578 	float scale = 1./SHORT_SCALE;
579 
580 	if(buf->size < count*sizeof(float))
581 	{
582 		error1("%s", bufsizeerr);
583 		return;
584 	}
585 
586 	/* Work from the back since output is bigger. */
587 	for(i=count-1; i>=0; --i)
588 	out[i] = (float)in[i] * scale;
589 
590 	buf->fill = count*sizeof(float);
591 }
592 #endif
593 
594 #ifndef NO_32BIT
595 static void conv_s16_to_s32(struct outbuffer *buf)
596 {
597 	ssize_t i;
598 	int16_t  *in = (int16_t*) buf->data;
599 	int32_t *out = (int32_t*) buf->data;
600 	size_t count = buf->fill/sizeof(int16_t);
601 
602 	if(buf->size < count*sizeof(int32_t))
603 	{
604 		error1("%s", bufsizeerr);
605 		return;
606 	}
607 
608 	/* Work from the back since output is bigger. */
609 	for(i=count-1; i>=0; --i)
610 	{
611 		out[i] = in[i];
612 		/* Could just shift bytes, but would have to mess with sign bit. */
613 		out[i] *= S32_RESCALE;
614 	}
615 
616 	buf->fill = count*sizeof(int32_t);
617 }
618 #endif
619 #endif
620 
621 
622 void postprocess_buffer(mpg123_handle *fr)
623 {
624 	/*
625 		This caters for the final output formats that are never produced by
626 		decoder synth directly (wide unsigned and 24 bit formats) or that are
627 		missing because of limited decoder precision (16 bit synth but 32 or
628 		24 bit output).
629 	*/
630 	switch(fr->af.dec_enc)
631 	{
632 #ifndef NO_32BIT
633 	case MPG123_ENC_SIGNED_32:
634 		switch(fr->af.encoding)
635 		{
636 		case MPG123_ENC_UNSIGNED_32:
637 			conv_s32_to_u32(&fr->buffer);
638 		break;
639 		case MPG123_ENC_UNSIGNED_24:
640 			conv_s32_to_u32(&fr->buffer);
641 			chop_fourth_byte(&fr->buffer);
642 		break;
643 		case MPG123_ENC_SIGNED_24:
644 			chop_fourth_byte(&fr->buffer);
645 		break;
646 		}
647 	break;
648 #endif
649 #ifndef NO_16BIT
650 	case MPG123_ENC_SIGNED_16:
651 		switch(fr->af.encoding)
652 		{
653 		case MPG123_ENC_UNSIGNED_16:
654 			conv_s16_to_u16(&fr->buffer);
655 		break;
656 #ifndef NO_REAL
657 		case MPG123_ENC_FLOAT_32:
658 			conv_s16_to_f32(&fr->buffer);
659 		break;
660 #endif
661 #ifndef NO_32BIT
662 		case MPG123_ENC_SIGNED_32:
663 			conv_s16_to_s32(&fr->buffer);
664 		break;
665 		case MPG123_ENC_UNSIGNED_32:
666 			conv_s16_to_s32(&fr->buffer);
667 			conv_s32_to_u32(&fr->buffer);
668 		break;
669 		case MPG123_ENC_UNSIGNED_24:
670 			conv_s16_to_s32(&fr->buffer);
671 			conv_s32_to_u32(&fr->buffer);
672 			chop_fourth_byte(&fr->buffer);
673 		break;
674 		case MPG123_ENC_SIGNED_24:
675 			conv_s16_to_s32(&fr->buffer);
676 			chop_fourth_byte(&fr->buffer);
677 		break;
678 #endif
679 		}
680 	break;
681 #endif
682 	}
683 }
684