1 /*
2 format: routines to deal with audio (output) format
3
4 copyright 2008-20 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 "sample.h"
30 #include "debug.h"
31
32 /* static int chans[NUM_CHANNELS] = { 1 , 2 }; */
33 static const long my_rates[MPG123_RATES] = /* only the standard rates */
34 {
35 8000, 11025, 12000,
36 16000, 22050, 24000,
37 32000, 44100, 48000,
38 };
39
40 static const int my_encodings[MPG123_ENCODINGS] =
41 {
42 MPG123_ENC_SIGNED_16,
43 MPG123_ENC_UNSIGNED_16,
44 MPG123_ENC_SIGNED_32,
45 MPG123_ENC_UNSIGNED_32,
46 MPG123_ENC_SIGNED_24,
47 MPG123_ENC_UNSIGNED_24,
48 /* Floating point range, see below. */
49 MPG123_ENC_FLOAT_32,
50 MPG123_ENC_FLOAT_64,
51 /* 8 bit range, see below. */
52 MPG123_ENC_SIGNED_8,
53 MPG123_ENC_UNSIGNED_8,
54 MPG123_ENC_ULAW_8,
55 MPG123_ENC_ALAW_8
56 };
57
58 /* Make that match the above table.
59 And yes, I still don't like this kludgy stuff. */
60 /* range[0] <= i < range[1] for forced floating point */
61 static const int enc_float_range[2] = { 6, 8 };
62 /* same for 8 bit encodings */
63 static const int enc_8bit_range[2] = { 8, 12 };
64 // for 24 bit quality (24 and 32 bit integers)
65 static const int enc_24bit_range[2] = { 2, 6 };
66 // for completeness, the 16 bits
67 static const int enc_16bit_range[2] = { 0, 2};
68
69 /*
70 Only one type of float is supported.
71 Actually, double is a very special experimental case not occuring in normal
72 builds. Might actually get rid of it.
73
74 Remember here: Also with REAL_IS_FIXED, I want to be able to produce float
75 output (f32) via post-processing.
76 */
77 # ifdef REAL_IS_DOUBLE
78 # define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_64
79 # else
80 # define MPG123_FLOAT_ENC MPG123_ENC_FLOAT_32
81 # endif
82
83 /* The list of actually possible encodings. */
84 static const int good_encodings[] =
85 {
86 #ifndef NO_16BIT
87 MPG123_ENC_SIGNED_16,
88 MPG123_ENC_UNSIGNED_16,
89 #endif
90 #ifndef NO_32BIT
91 MPG123_ENC_SIGNED_32,
92 MPG123_ENC_UNSIGNED_32,
93 MPG123_ENC_SIGNED_24,
94 MPG123_ENC_UNSIGNED_24,
95 #endif
96 #ifndef NO_REAL
97 MPG123_FLOAT_ENC,
98 #endif
99 #ifndef NO_8BIT
100 MPG123_ENC_SIGNED_8,
101 MPG123_ENC_UNSIGNED_8,
102 MPG123_ENC_ULAW_8,
103 MPG123_ENC_ALAW_8
104 #endif
105 };
106
107 /* Check if encoding is a valid one in this build.
108 ...lazy programming: linear search. */
good_enc(const int enc)109 static int good_enc(const int enc)
110 {
111 size_t i;
112 for(i=0; i<sizeof(good_encodings)/sizeof(int); ++i)
113 if(enc == good_encodings[i]) return TRUE;
114
115 return FALSE;
116 }
117
mpg123_rates(const long ** list,size_t * number)118 void attribute_align_arg mpg123_rates(const long **list, size_t *number)
119 {
120 if(list != NULL) *list = my_rates;
121 if(number != NULL) *number = sizeof(my_rates)/sizeof(long);
122 }
123
124 /* Now that's a bit tricky... One build of the library knows only a subset of the encodings. */
mpg123_encodings(const int ** list,size_t * number)125 void attribute_align_arg mpg123_encodings(const int **list, size_t *number)
126 {
127 if(list != NULL) *list = good_encodings;
128 if(number != NULL) *number = sizeof(good_encodings)/sizeof(int);
129 }
130
mpg123_encsize(int encoding)131 int attribute_align_arg mpg123_encsize(int encoding)
132 {
133 return MPG123_SAMPLESIZE(encoding);
134 }
135
136 /* char audio_caps[NUM_CHANNELS][MPG123_RATES+1][MPG123_ENCODINGS]; */
137
rate2num(mpg123_pars * mp,long r)138 static int rate2num(mpg123_pars *mp, long r)
139 {
140 int i;
141 for(i=0;i<MPG123_RATES;i++) if(my_rates[i] == r) return i;
142 #ifndef NO_NTOM
143 if(mp && mp->force_rate != 0 && mp->force_rate == r) return MPG123_RATES;
144 #endif
145
146 return -1;
147 }
148
enc2num(int encoding)149 static int enc2num(int encoding)
150 {
151 int i;
152 for(i=0;i<MPG123_ENCODINGS;++i)
153 if(my_encodings[i] == encoding) return i;
154
155 return -1;
156 }
157
cap_fit(mpg123_pars * p,struct audioformat * nf,int f0,int f2)158 static int cap_fit(mpg123_pars *p, struct audioformat *nf, int f0, int f2)
159 {
160 int i;
161 int c = nf->channels-1;
162 int rn = rate2num(p, nf->rate);
163 if(rn >= 0) for(i=f0;i<f2;i++)
164 {
165 if(p->audio_caps[c][rn][i])
166 {
167 nf->encoding = my_encodings[i];
168 return 1;
169 }
170 }
171 return 0;
172 }
173
imin(int a,int b)174 static int imin(int a, int b)
175 {
176 return a < b ? a : b;
177 }
178
imax(int a,int b)179 static int imax(int a, int b)
180 {
181 return a > b ? a : b;
182 }
183
184 // Find a possible encoding with given rate and channel count,
185 // try differing channel count, too.
186 // This updates the given format and returns TRUE if an encoding
187 // was found.
enc_chan_fit(mpg123_pars * p,long rate,struct audioformat * nnf,int f0,int f2,int try_float)188 static int enc_chan_fit( mpg123_pars *p, long rate, struct audioformat *nnf
189 , int f0, int f2, int try_float )
190 {
191 #define ENCRANGE(range) imax(f0, range[0]), imin(f2, range[1])
192 struct audioformat nf = *nnf;
193 nf.rate = rate;
194 if(cap_fit(p, &nf, ENCRANGE(enc_16bit_range)))
195 goto eend;
196 if(cap_fit(p, &nf, ENCRANGE(enc_24bit_range)))
197 goto eend;
198 if(try_float &&
199 cap_fit(p, &nf, ENCRANGE(enc_float_range)))
200 goto eend;
201 if(cap_fit(p, &nf, ENCRANGE(enc_8bit_range)))
202 goto eend;
203
204 /* try again with different stereoness */
205 if(nf.channels == 2 && !(p->flags & MPG123_FORCE_STEREO)) nf.channels = 1;
206 else if(nf.channels == 1 && !(p->flags & MPG123_FORCE_MONO)) nf.channels = 2;
207
208 if(cap_fit(p, &nf, ENCRANGE(enc_16bit_range)))
209 goto eend;
210 if(cap_fit(p, &nf, ENCRANGE(enc_24bit_range)))
211 goto eend;
212 if(try_float &&
213 cap_fit(p, &nf, ENCRANGE(enc_float_range)))
214 goto eend;
215 if(cap_fit(p, &nf, ENCRANGE(enc_8bit_range)))
216 goto eend;
217 return FALSE;
218 eend:
219 *nnf = nf;
220 return TRUE;
221 #undef ENCRANGE
222 }
223
224 /* match constraints against supported audio formats, store possible setup in frame
225 return: -1: error; 0: no format change; 1: format change */
frame_output_format(mpg123_handle * fr)226 int frame_output_format(mpg123_handle *fr)
227 {
228 struct audioformat nf;
229 int f0=0;
230 int f2=MPG123_ENCODINGS+1; // Include all encodings by default.
231 mpg123_pars *p = &fr->p;
232 int try_float = (p->flags & MPG123_FLOAT_FALLBACK) ? 0 : 1;
233 /* initialize new format, encoding comes later */
234 nf.channels = fr->stereo;
235
236 // I intended the forcing stuff to be weaved into the format support table,
237 // but this probably will never happen, as this would change library behaviour.
238 // One could introduce an additional effective format table that takes for
239 // forcings into account, but that would have to be updated on any flag
240 // change. Tedious.
241 if(p->flags & MPG123_FORCE_8BIT)
242 {
243 f0 = enc_8bit_range[0];
244 f2 = enc_8bit_range[1];
245 }
246 if(p->flags & MPG123_FORCE_FLOAT)
247 {
248 try_float = 1;
249 f0 = enc_float_range[0];
250 f2 = enc_float_range[1];
251 }
252
253 /* force stereo is stronger */
254 if(p->flags & MPG123_FORCE_MONO) nf.channels = 1;
255 if(p->flags & MPG123_FORCE_STEREO) nf.channels = 2;
256
257 // Strategy update: Avoid too early triggering of the NtoM decoder.
258 // Main target is the native rate, with any encoding.
259 // Then, native rate with any channel count and any encoding.
260 // Then, it's down_sample from native rate.
261 // As last resort: NtoM rate.
262 // So the priority is 1. rate 2. channels 3. encoding.
263 // As encodings go, 16 bit is tranditionally preferred as efficient choice.
264 // Next in line are wider float and integer encodings, then 8 bit as
265 // last resort.
266
267 #ifndef NO_NTOM
268 if(p->force_rate)
269 {
270 if(enc_chan_fit(p, p->force_rate, &nf, f0, f2, try_float))
271 goto end;
272 // Keep the order consistent if float is considered fallback only.
273 if(!try_float &&
274 enc_chan_fit(p, p->force_rate, &nf, f0, f2, TRUE))
275 goto end;
276
277 merror( "Unable to set up output format! Constraints: %s%s%liHz."
278 , ( p->flags & MPG123_FORCE_STEREO ? "stereo, " :
279 (p->flags & MPG123_FORCE_MONO ? "mono, " : "") )
280 , ( p->flags & MPG123_FORCE_FLOAT ? "float, " :
281 (p->flags & MPG123_FORCE_8BIT ? "8bit, " : "") )
282 , p->force_rate );
283 /* if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
284
285 fr->err = MPG123_BAD_OUTFORMAT;
286 return -1;
287 }
288 #endif
289 // Native decoder rate first.
290 if(enc_chan_fit(p, frame_freq(fr)>>p->down_sample, &nf, f0, f2, try_float))
291 goto end;
292 // Then downsamplings.
293 if(p->flags & MPG123_AUTO_RESAMPLE && p->down_sample < 2)
294 {
295 if(enc_chan_fit( p, frame_freq(fr)>>(p->down_sample+1), &nf
296 , f0, f2, try_float ))
297 goto end;
298 if(p->down_sample < 1 && enc_chan_fit( p, frame_freq(fr)>>2, &nf
299 , f0, f2, try_float ))
300 goto end;
301 }
302 // And again the whole deal with float fallback.
303 if(!try_float)
304 {
305 if(enc_chan_fit(p, frame_freq(fr)>>p->down_sample, &nf, f0, f2, TRUE))
306 goto end;
307 // Then downsamplings.
308 if(p->flags & MPG123_AUTO_RESAMPLE && p->down_sample < 2)
309 {
310 if(enc_chan_fit( p, frame_freq(fr)>>(p->down_sample+1), &nf
311 , f0, f2, TRUE ))
312 goto end;
313 if(p->down_sample < 1 && enc_chan_fit( p, frame_freq(fr)>>2, &nf
314 , f0, f2, TRUE ))
315 goto end;
316 }
317 }
318 #ifndef NO_NTOM
319 // Try to find any rate that works and resample using NtoM hackery.
320 if( p->flags & MPG123_AUTO_RESAMPLE && fr->p.down_sample == 0)
321 {
322 int i;
323 int rn = rate2num(p, frame_freq(fr));
324 int rrn;
325 if(rn < 0) return 0;
326 /* Try higher rates first. */
327 for(rrn=rn+1; rrn<MPG123_RATES; ++rrn)
328 if(enc_chan_fit(p, my_rates[rrn], &nf, f0, f2, try_float))
329 goto end;
330 /* Then lower rates. */
331 for(i=f0;i<f2;i++) for(rrn=rn-1; rrn>=0; --rrn)
332 if(enc_chan_fit(p, my_rates[rrn], &nf, f0, f2, try_float))
333 goto end;
334 // And again for float fallback.
335 if(!try_float)
336 {
337 /* Try higher rates first. */
338 for(rrn=rn+1; rrn<MPG123_RATES; ++rrn)
339 if(enc_chan_fit(p, my_rates[rrn], &nf, f0, f2, TRUE))
340 goto end;
341 /* Then lower rates. */
342 for(i=f0;i<f2;i++) for(rrn=rn-1; rrn>=0; --rrn)
343 if(enc_chan_fit(p, my_rates[rrn], &nf, f0, f2, TRUE))
344 goto end;
345 }
346 }
347 #endif
348
349 /* Here is the _bad_ end. */
350 merror( "Unable to set up output format! Constraints: %s%s%li, %li or %liHz."
351 , ( p->flags & MPG123_FORCE_STEREO ? "stereo, " :
352 (p->flags & MPG123_FORCE_MONO ? "mono, " : "") )
353 , ( p->flags & MPG123_FORCE_FLOAT ? "float, " :
354 (p->flags & MPG123_FORCE_8BIT ? "8bit, " : "") )
355 , frame_freq(fr), frame_freq(fr)>>1, frame_freq(fr)>>2 );
356 /* if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
357
358 fr->err = MPG123_BAD_OUTFORMAT;
359 return -1;
360
361 end: /* Here is the _good_ end. */
362 /* we had a successful match, now see if there's a change */
363 if(nf.rate == fr->af.rate && nf.channels == fr->af.channels && nf.encoding == fr->af.encoding)
364 {
365 debug2("Old format with %i channels, and FORCE_MONO=%li", nf.channels, p->flags & MPG123_FORCE_MONO);
366 return 0; /* the same format as before */
367 }
368 else /* a new format */
369 {
370 debug1("New format with %i channels!", nf.channels);
371 fr->af.rate = nf.rate;
372 fr->af.channels = nf.channels;
373 fr->af.encoding = nf.encoding;
374 /* Cache the size of one sample in bytes, for ease of use. */
375 fr->af.encsize = mpg123_encsize(fr->af.encoding);
376 if(fr->af.encsize < 1)
377 {
378 error1("Some unknown encoding??? (%i)", fr->af.encoding);
379
380 fr->err = MPG123_BAD_OUTFORMAT;
381 return -1;
382 }
383 /* Set up the decoder synth format. Might differ. */
384 #ifdef NO_SYNTH32
385 /* Without high-precision synths, 16 bit signed is the basis for
386 everything higher than 8 bit. */
387 if(fr->af.encsize > 2)
388 fr->af.dec_enc = MPG123_ENC_SIGNED_16;
389 else
390 {
391 #endif
392 switch(fr->af.encoding)
393 {
394 #ifndef NO_32BIT
395 case MPG123_ENC_SIGNED_24:
396 case MPG123_ENC_UNSIGNED_24:
397 case MPG123_ENC_UNSIGNED_32:
398 fr->af.dec_enc = MPG123_ENC_SIGNED_32;
399 break;
400 #endif
401 #ifndef NO_16BIT
402 case MPG123_ENC_UNSIGNED_16:
403 fr->af.dec_enc = MPG123_ENC_SIGNED_16;
404 break;
405 #endif
406 default:
407 fr->af.dec_enc = fr->af.encoding;
408 }
409 #ifdef NO_SYNTH32
410 }
411 #endif
412 fr->af.dec_encsize = mpg123_encsize(fr->af.dec_enc);
413 return 1;
414 }
415 }
416
mpg123_format_none(mpg123_handle * mh)417 int attribute_align_arg mpg123_format_none(mpg123_handle *mh)
418 {
419 int r;
420 if(mh == NULL) return MPG123_BAD_HANDLE;
421
422 r = mpg123_fmt_none(&mh->p);
423 if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
424
425 return r;
426 }
427
mpg123_fmt_none(mpg123_pars * mp)428 int attribute_align_arg mpg123_fmt_none(mpg123_pars *mp)
429 {
430 if(mp == NULL) return MPG123_BAD_PARS;
431
432 if(PVERB(mp,3)) fprintf(stderr, "Note: Disabling all formats.\n");
433
434 memset(mp->audio_caps,0,sizeof(mp->audio_caps));
435 return MPG123_OK;
436 }
437
mpg123_format_all(mpg123_handle * mh)438 int attribute_align_arg mpg123_format_all(mpg123_handle *mh)
439 {
440 int r;
441 if(mh == NULL) return MPG123_BAD_HANDLE;
442
443 r = mpg123_fmt_all(&mh->p);
444 if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
445
446 return r;
447 }
448
mpg123_fmt_all(mpg123_pars * mp)449 int attribute_align_arg mpg123_fmt_all(mpg123_pars *mp)
450 {
451 size_t rate, ch, enc;
452 if(mp == NULL) return MPG123_BAD_PARS;
453
454 if(PVERB(mp,3)) fprintf(stderr, "Note: Enabling all formats.\n");
455
456 for(ch=0; ch < NUM_CHANNELS; ++ch)
457 for(rate=0; rate < MPG123_RATES+1; ++rate)
458 for(enc=0; enc < MPG123_ENCODINGS; ++enc)
459 mp->audio_caps[ch][rate][enc] = good_enc(my_encodings[enc]) ? 1 : 0;
460
461 return MPG123_OK;
462 }
463
mpg123_format2(mpg123_handle * mh,long rate,int channels,int encodings)464 int attribute_align_arg mpg123_format2(mpg123_handle *mh, long rate, int channels, int encodings)
465 {
466 int r;
467 if(mh == NULL) return MPG123_BAD_HANDLE;
468 r = mpg123_fmt2(&mh->p, rate, channels, encodings);
469 if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
470
471 return r;
472 }
473
474 // Keep old behaviour.
mpg123_format(mpg123_handle * mh,long rate,int channels,int encodings)475 int attribute_align_arg mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings)
476 {
477 int r;
478 if(mh == NULL) return MPG123_BAD_HANDLE;
479 r = mpg123_fmt(&mh->p, rate, channels, encodings);
480 if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
481
482 return r;
483 }
484
mpg123_fmt2(mpg123_pars * mp,long rate,int channels,int encodings)485 int attribute_align_arg mpg123_fmt2(mpg123_pars *mp, long rate, int channels, int encodings)
486 {
487 int ie, ic, ratei, r1, r2;
488 int ch[2] = {0, 1};
489 if(mp == NULL) return MPG123_BAD_PARS;
490 if(!(channels & (MPG123_MONO|MPG123_STEREO))) return MPG123_BAD_CHANNEL;
491
492 if(PVERB(mp,3)) fprintf(stderr, "Note: Want to enable format %li/%i for encodings 0x%x.\n", rate, channels, encodings);
493
494 if(!(channels & MPG123_STEREO)) ch[1] = 0; /* {0,0} */
495 else if(!(channels & MPG123_MONO)) ch[0] = 1; /* {1,1} */
496 if(rate)
497 {
498 r1 = rate2num(mp, rate);
499 r2 = r1+1;
500 }
501 else
502 {
503 r1 = 0;
504 r2 = MPG123_RATES+1; /* including forced rate */
505 }
506
507 if(r1 < 0) return MPG123_BAD_RATE;
508
509 /* now match the encodings */
510 for(ratei = r1; ratei < r2; ++ratei)
511 for(ic = 0; ic < 2; ++ic)
512 {
513 for(ie = 0; ie < MPG123_ENCODINGS; ++ie)
514 if(good_enc(my_encodings[ie]) && ((my_encodings[ie] & encodings) == my_encodings[ie]))
515 mp->audio_caps[ch[ic]][ratei][ie] = 1;
516
517 if(ch[0] == ch[1]) break; /* no need to do it again */
518 }
519
520 return MPG123_OK;
521 }
522
523 // Keep old behaviour, error on rate=0.
mpg123_fmt(mpg123_pars * mp,long rate,int channels,int encodings)524 int attribute_align_arg mpg123_fmt(mpg123_pars *mp, long rate, int channels, int encodings)
525 {
526 return (rate == 0)
527 ? MPG123_BAD_RATE
528 : mpg123_fmt2(mp, rate, channels, encodings);
529 }
530
mpg123_format_support(mpg123_handle * mh,long rate,int encoding)531 int attribute_align_arg mpg123_format_support(mpg123_handle *mh, long rate, int encoding)
532 {
533 if(mh == NULL) return 0;
534 else return mpg123_fmt_support(&mh->p, rate, encoding);
535 }
536
mpg123_fmt_support(mpg123_pars * mp,long rate,int encoding)537 int attribute_align_arg mpg123_fmt_support(mpg123_pars *mp, long rate, int encoding)
538 {
539 int ch = 0;
540 int ratei, enci;
541 ratei = rate2num(mp, rate);
542 enci = enc2num(encoding);
543 if(mp == NULL || ratei < 0 || enci < 0) return 0;
544 if(mp->audio_caps[0][ratei][enci]) ch |= MPG123_MONO;
545 if(mp->audio_caps[1][ratei][enci]) ch |= MPG123_STEREO;
546 return ch;
547 }
548
549 /* Call this one to ensure that any valid format will be something different than this. */
invalidate_format(struct audioformat * af)550 void invalidate_format(struct audioformat *af)
551 {
552 af->encoding = 0;
553 af->rate = 0;
554 af->channels = 0;
555 }
556
557 /* Number of bytes the decoder produces. */
decoder_synth_bytes(mpg123_handle * fr,off_t s)558 off_t decoder_synth_bytes(mpg123_handle *fr, off_t s)
559 {
560 return s * fr->af.dec_encsize * fr->af.channels;
561 }
562
563 /* Samples/bytes for output buffer after post-processing. */
564 /* take into account: channels, bytes per sample -- NOT resampling!*/
samples_to_bytes(mpg123_handle * fr,off_t s)565 off_t samples_to_bytes(mpg123_handle *fr , off_t s)
566 {
567 return s * fr->af.encsize * fr->af.channels;
568 }
569
bytes_to_samples(mpg123_handle * fr,off_t b)570 off_t bytes_to_samples(mpg123_handle *fr , off_t b)
571 {
572 return b / fr->af.encsize / fr->af.channels;
573 }
574
575 /* Number of bytes needed for decoding _and_ post-processing. */
outblock_bytes(mpg123_handle * fr,off_t s)576 off_t outblock_bytes(mpg123_handle *fr, off_t s)
577 {
578 int encsize = (fr->af.encoding & MPG123_ENC_24)
579 ? 4 /* Intermediate 32 bit. */
580 : (fr->af.encsize > fr->af.dec_encsize
581 ? fr->af.encsize
582 : fr->af.dec_encsize);
583 return s * encsize * fr->af.channels;
584 }
585
586 #ifndef NO_32BIT
587
588 /* Remove every fourth byte, facilitating conversion from 32 bit to 24 bit integers.
589 This has to be aware of endianness, of course. */
chop_fourth_byte(struct outbuffer * buf)590 static void chop_fourth_byte(struct outbuffer *buf)
591 {
592 unsigned char *wpos = buf->data;
593 unsigned char *rpos = buf->data;
594 size_t blocks = buf->fill/4;
595 size_t i;
596 for(i=0; i<blocks; ++i,wpos+=3,rpos+=4)
597 DROP4BYTE(wpos, rpos)
598 buf->fill = wpos-buf->data;
599 }
600
conv_s32_to_u32(struct outbuffer * buf)601 static void conv_s32_to_u32(struct outbuffer *buf)
602 {
603 size_t i;
604 int32_t *ssamples = (int32_t*) buf->data;
605 uint32_t *usamples = (uint32_t*) buf->data;
606 size_t count = buf->fill/sizeof(int32_t);
607
608 for(i=0; i<count; ++i)
609 usamples[i] = CONV_SU32(ssamples[i]);
610 }
611
612 #endif
613
614
615 /* We always assume that whole numbers are written!
616 partials will be cut out. */
617
618 static const char *bufsizeerr = "Fatal: Buffer too small for postprocessing!";
619
620
621 #ifndef NO_16BIT
622
conv_s16_to_u16(struct outbuffer * buf)623 static void conv_s16_to_u16(struct outbuffer *buf)
624 {
625 size_t i;
626 int16_t *ssamples = (int16_t*) buf->data;
627 uint16_t *usamples = (uint16_t*)buf->data;
628 size_t count = buf->fill/sizeof(int16_t);
629
630 for(i=0; i<count; ++i)
631 usamples[i] = CONV_SU16(ssamples[i]);
632 }
633
634 #ifndef NO_REAL
conv_s16_to_f32(struct outbuffer * buf)635 static void conv_s16_to_f32(struct outbuffer *buf)
636 {
637 ssize_t i;
638 int16_t *in = (int16_t*) buf->data;
639 float *out = (float*) buf->data;
640 size_t count = buf->fill/sizeof(int16_t);
641 /* Does that make any sense? In x86, there is an actual instruction to divide
642 float by integer ... but then, if we have that FPU, we don't really need
643 fixed point decoder hacks ...? */
644 float scale = 1./SHORT_SCALE;
645
646 if(buf->size < count*sizeof(float))
647 {
648 error1("%s", bufsizeerr);
649 return;
650 }
651
652 /* Work from the back since output is bigger. */
653 for(i=count-1; i>=0; --i)
654 out[i] = (float)in[i] * scale;
655
656 buf->fill = count*sizeof(float);
657 }
658 #endif
659
660 #ifndef NO_32BIT
conv_s16_to_s32(struct outbuffer * buf)661 static void conv_s16_to_s32(struct outbuffer *buf)
662 {
663 ssize_t i;
664 int16_t *in = (int16_t*) buf->data;
665 int32_t *out = (int32_t*) buf->data;
666 size_t count = buf->fill/sizeof(int16_t);
667
668 if(buf->size < count*sizeof(int32_t))
669 {
670 error1("%s", bufsizeerr);
671 return;
672 }
673
674 /* Work from the back since output is bigger. */
675 for(i=count-1; i>=0; --i)
676 {
677 out[i] = in[i];
678 /* Could just shift bytes, but would have to mess with sign bit. */
679 out[i] *= S32_RESCALE;
680 }
681
682 buf->fill = count*sizeof(int32_t);
683 }
684 #endif
685 #endif
686
687 #include "swap_bytes_impl.h"
688
swap_endian(struct outbuffer * buf,int block)689 void swap_endian(struct outbuffer *buf, int block)
690 {
691 size_t count;
692
693 if(block >= 2)
694 {
695 count = buf->fill/(unsigned int)block;
696 swap_bytes(buf->data, (size_t)block, count);
697 }
698 }
699
postprocess_buffer(mpg123_handle * fr)700 void postprocess_buffer(mpg123_handle *fr)
701 {
702 /*
703 This caters for the final output formats that are never produced by
704 decoder synth directly (wide unsigned and 24 bit formats) or that are
705 missing because of limited decoder precision (16 bit synth but 32 or
706 24 bit output).
707 */
708 switch(fr->af.dec_enc)
709 {
710 #ifndef NO_32BIT
711 case MPG123_ENC_SIGNED_32:
712 switch(fr->af.encoding)
713 {
714 case MPG123_ENC_UNSIGNED_32:
715 conv_s32_to_u32(&fr->buffer);
716 break;
717 case MPG123_ENC_UNSIGNED_24:
718 conv_s32_to_u32(&fr->buffer);
719 chop_fourth_byte(&fr->buffer);
720 break;
721 case MPG123_ENC_SIGNED_24:
722 chop_fourth_byte(&fr->buffer);
723 break;
724 }
725 break;
726 #endif
727 #ifndef NO_16BIT
728 case MPG123_ENC_SIGNED_16:
729 switch(fr->af.encoding)
730 {
731 case MPG123_ENC_UNSIGNED_16:
732 conv_s16_to_u16(&fr->buffer);
733 break;
734 #ifndef NO_REAL
735 case MPG123_ENC_FLOAT_32:
736 conv_s16_to_f32(&fr->buffer);
737 break;
738 #endif
739 #ifndef NO_32BIT
740 case MPG123_ENC_SIGNED_32:
741 conv_s16_to_s32(&fr->buffer);
742 break;
743 case MPG123_ENC_UNSIGNED_32:
744 conv_s16_to_s32(&fr->buffer);
745 conv_s32_to_u32(&fr->buffer);
746 break;
747 case MPG123_ENC_UNSIGNED_24:
748 conv_s16_to_s32(&fr->buffer);
749 conv_s32_to_u32(&fr->buffer);
750 chop_fourth_byte(&fr->buffer);
751 break;
752 case MPG123_ENC_SIGNED_24:
753 conv_s16_to_s32(&fr->buffer);
754 chop_fourth_byte(&fr->buffer);
755 break;
756 #endif
757 }
758 break;
759 #endif
760 }
761 if(fr->p.flags & MPG123_FORCE_ENDIAN)
762 {
763 if(
764 #ifdef WORDS_BIGENDIAN
765 !(
766 #endif
767 fr->p.flags & MPG123_BIG_ENDIAN
768 #ifdef WORDS_BIGENDIAN
769 )
770 #endif
771 )
772 swap_endian(&fr->buffer, mpg123_encsize(fr->af.encoding));
773 }
774 }
775