Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/mpg123/src/libmpg123/format.c
4394 views
1
/*
2
format: routines to deal with audio (output) format
3
4
copyright 2008-23 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 "../common/sample.h"
30
#include "../common/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. */
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
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. */
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
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
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
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
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
174
static int imin(int a, int b)
175
{
176
return a < b ? a : b;
177
}
178
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.
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 */
226
int INT123_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
mdebug("native frame format: %d ch @ %ld Hz", fr->stereo, INT123_frame_freq(fr));
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, INT123_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, INT123_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, INT123_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, INT123_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, INT123_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, INT123_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, INT123_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
, INT123_frame_freq(fr)>>p->down_sample
356
, INT123_frame_freq(fr)>>(p->down_sample ? p->down_sample : 1)
357
, INT123_frame_freq(fr)>>2 );
358
/* if(NOQUIET && p->verbose <= 1) print_capabilities(fr); */
359
360
fr->err = MPG123_BAD_OUTFORMAT;
361
return -1;
362
363
end: /* Here is the _good_ end. */
364
/* we had a successful match, now see if there's a change */
365
if(nf.rate == fr->af.rate && nf.channels == fr->af.channels && nf.encoding == fr->af.encoding)
366
{
367
debug2("Old format with %i channels, and FORCE_MONO=%li", nf.channels, p->flags & MPG123_FORCE_MONO);
368
return 0; /* the same format as before */
369
}
370
else /* a new format */
371
{
372
debug1("New format with %i channels!", nf.channels);
373
fr->af.rate = nf.rate;
374
fr->af.channels = nf.channels;
375
fr->af.encoding = nf.encoding;
376
/* Cache the size of one sample in bytes, for ease of use. */
377
fr->af.encsize = mpg123_encsize(fr->af.encoding);
378
if(fr->af.encsize < 1)
379
{
380
error1("Some unknown encoding??? (%i)", fr->af.encoding);
381
382
fr->err = MPG123_BAD_OUTFORMAT;
383
return -1;
384
}
385
/* Set up the decoder synth format. Might differ. */
386
#ifdef NO_SYNTH32
387
/* Without high-precision synths, 16 bit signed is the basis for
388
everything higher than 8 bit. */
389
if(fr->af.encsize > 2)
390
fr->af.dec_enc = MPG123_ENC_SIGNED_16;
391
else
392
{
393
#endif
394
switch(fr->af.encoding)
395
{
396
#ifndef NO_32BIT
397
case MPG123_ENC_SIGNED_24:
398
case MPG123_ENC_UNSIGNED_24:
399
case MPG123_ENC_UNSIGNED_32:
400
fr->af.dec_enc = MPG123_ENC_SIGNED_32;
401
break;
402
#endif
403
#ifndef NO_16BIT
404
case MPG123_ENC_UNSIGNED_16:
405
fr->af.dec_enc = MPG123_ENC_SIGNED_16;
406
break;
407
#endif
408
default:
409
fr->af.dec_enc = fr->af.encoding;
410
}
411
#ifdef NO_SYNTH32
412
}
413
#endif
414
fr->af.dec_encsize = mpg123_encsize(fr->af.dec_enc);
415
return 1;
416
}
417
}
418
419
int attribute_align_arg mpg123_format_none(mpg123_handle *mh)
420
{
421
int r;
422
if(mh == NULL) return MPG123_BAD_HANDLE;
423
424
r = mpg123_fmt_none(&mh->p);
425
if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
426
427
return r;
428
}
429
430
int attribute_align_arg mpg123_fmt_none(mpg123_pars *mp)
431
{
432
if(mp == NULL) return MPG123_BAD_PARS;
433
434
if(PVERB(mp,3)) fprintf(stderr, "Note: Disabling all formats.\n");
435
436
memset(mp->audio_caps,0,sizeof(mp->audio_caps));
437
return MPG123_OK;
438
}
439
440
int attribute_align_arg mpg123_format_all(mpg123_handle *mh)
441
{
442
int r;
443
if(mh == NULL) return MPG123_BAD_HANDLE;
444
445
r = mpg123_fmt_all(&mh->p);
446
if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
447
448
return r;
449
}
450
451
int attribute_align_arg mpg123_fmt_all(mpg123_pars *mp)
452
{
453
size_t rate, ch, enc;
454
if(mp == NULL) return MPG123_BAD_PARS;
455
456
if(PVERB(mp,3)) fprintf(stderr, "Note: Enabling all formats.\n");
457
458
for(ch=0; ch < NUM_CHANNELS; ++ch)
459
for(rate=0; rate < MPG123_RATES+1; ++rate)
460
for(enc=0; enc < MPG123_ENCODINGS; ++enc)
461
mp->audio_caps[ch][rate][enc] = good_enc(my_encodings[enc]) ? 1 : 0;
462
463
return MPG123_OK;
464
}
465
466
int attribute_align_arg mpg123_format2(mpg123_handle *mh, long rate, int channels, int encodings)
467
{
468
int r;
469
if(mh == NULL) return MPG123_BAD_HANDLE;
470
r = mpg123_fmt2(&mh->p, rate, channels, encodings);
471
if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
472
473
return r;
474
}
475
476
// Keep old behaviour.
477
int attribute_align_arg mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings)
478
{
479
int r;
480
if(mh == NULL) return MPG123_BAD_HANDLE;
481
r = mpg123_fmt(&mh->p, rate, channels, encodings);
482
if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
483
484
return r;
485
}
486
487
int attribute_align_arg mpg123_fmt2(mpg123_pars *mp, long rate, int channels, int encodings)
488
{
489
int ie, ic, ratei, r1, r2;
490
int ch[2] = {0, 1};
491
if(mp == NULL) return MPG123_BAD_PARS;
492
if(!(channels & (MPG123_MONO|MPG123_STEREO))) return MPG123_BAD_CHANNEL;
493
494
if(PVERB(mp,3)) fprintf(stderr, "Note: Want to enable format %li/%i for encodings 0x%x.\n", rate, channels, encodings);
495
496
if(!(channels & MPG123_STEREO)) ch[1] = 0; /* {0,0} */
497
else if(!(channels & MPG123_MONO)) ch[0] = 1; /* {1,1} */
498
if(rate)
499
{
500
r1 = rate2num(mp, rate);
501
r2 = r1+1;
502
}
503
else
504
{
505
r1 = 0;
506
r2 = MPG123_RATES+1; /* including forced rate */
507
}
508
509
if(r1 < 0) return MPG123_BAD_RATE;
510
511
/* now match the encodings */
512
for(ratei = r1; ratei < r2; ++ratei)
513
for(ic = 0; ic < 2; ++ic)
514
{
515
for(ie = 0; ie < MPG123_ENCODINGS; ++ie)
516
if(good_enc(my_encodings[ie]) && ((my_encodings[ie] & encodings) == my_encodings[ie]))
517
mp->audio_caps[ch[ic]][ratei][ie] = 1;
518
519
if(ch[0] == ch[1]) break; /* no need to do it again */
520
}
521
522
return MPG123_OK;
523
}
524
525
// Keep old behaviour, error on rate=0.
526
int attribute_align_arg mpg123_fmt(mpg123_pars *mp, long rate, int channels, int encodings)
527
{
528
return (rate == 0)
529
? MPG123_BAD_RATE
530
: mpg123_fmt2(mp, rate, channels, encodings);
531
}
532
533
int attribute_align_arg mpg123_format_support(mpg123_handle *mh, long rate, int encoding)
534
{
535
if(mh == NULL) return 0;
536
else return mpg123_fmt_support(&mh->p, rate, encoding);
537
}
538
539
int attribute_align_arg mpg123_fmt_support(mpg123_pars *mp, long rate, int encoding)
540
{
541
int ch = 0;
542
int ratei, enci;
543
ratei = rate2num(mp, rate);
544
enci = enc2num(encoding);
545
if(mp == NULL || ratei < 0 || enci < 0) return 0;
546
if(mp->audio_caps[0][ratei][enci]) ch |= MPG123_MONO;
547
if(mp->audio_caps[1][ratei][enci]) ch |= MPG123_STEREO;
548
return ch;
549
}
550
551
/* Call this one to ensure that any valid format will be something different than this. */
552
void INT123_invalidate_format(struct audioformat *af)
553
{
554
af->encoding = 0;
555
af->rate = 0;
556
af->channels = 0;
557
}
558
559
/* Number of bytes the decoder produces. */
560
int64_t INT123_decoder_synth_bytes(mpg123_handle *fr, int64_t s)
561
{
562
return s * fr->af.dec_encsize * fr->af.channels;
563
}
564
565
/* Samples/bytes for output buffer after post-processing. */
566
/* take into account: channels, bytes per sample -- NOT resampling!*/
567
int64_t INT123_samples_to_bytes(mpg123_handle *fr , int64_t s)
568
{
569
return s * fr->af.encsize * fr->af.channels;
570
}
571
572
int64_t INT123_bytes_to_samples(mpg123_handle *fr , int64_t b)
573
{
574
return b / fr->af.encsize / fr->af.channels;
575
}
576
577
/* Number of bytes needed for decoding _and_ post-processing. */
578
int64_t INT123_outblock_bytes(mpg123_handle *fr, int64_t s)
579
{
580
int encsize = (fr->af.encoding & MPG123_ENC_24)
581
? 4 /* Intermediate 32 bit. */
582
: (fr->af.encsize > fr->af.dec_encsize
583
? fr->af.encsize
584
: fr->af.dec_encsize);
585
return s * encsize * fr->af.channels;
586
}
587
588
#ifndef NO_32BIT
589
590
/* Remove every fourth byte, facilitating conversion from 32 bit to 24 bit integers.
591
This has to be aware of endianness, of course. */
592
static void chop_fourth_byte(struct outbuffer *buf)
593
{
594
unsigned char *wpos = buf->data;
595
unsigned char *rpos = buf->data;
596
size_t blocks = buf->fill/4;
597
size_t i;
598
for(i=0; i<blocks; ++i,wpos+=3,rpos+=4)
599
DROP4BYTE(wpos, rpos)
600
buf->fill = wpos-buf->data;
601
}
602
603
static void conv_s32_to_u32(struct outbuffer *buf)
604
{
605
size_t i;
606
int32_t *ssamples = (int32_t*) buf->data;
607
uint32_t *usamples = (uint32_t*) buf->data;
608
size_t count = buf->fill/sizeof(int32_t);
609
610
for(i=0; i<count; ++i)
611
usamples[i] = CONV_SU32(ssamples[i]);
612
}
613
614
#endif
615
616
617
/* We always assume that whole numbers are written!
618
partials will be cut out. */
619
620
static const char *bufsizeerr = "Fatal: Buffer too small for postprocessing!";
621
622
623
#ifndef NO_16BIT
624
625
static void conv_s16_to_u16(struct outbuffer *buf)
626
{
627
size_t i;
628
int16_t *ssamples = (int16_t*) buf->data;
629
uint16_t *usamples = (uint16_t*)buf->data;
630
size_t count = buf->fill/sizeof(int16_t);
631
632
for(i=0; i<count; ++i)
633
usamples[i] = CONV_SU16(ssamples[i]);
634
}
635
636
#ifndef NO_REAL
637
static void conv_s16_to_f32(struct outbuffer *buf)
638
{
639
ptrdiff_t i;
640
int16_t *in = (int16_t*) buf->data;
641
float *out = (float*) buf->data;
642
size_t count = buf->fill/sizeof(int16_t);
643
/* Does that make any sense? In x86, there is an actual instruction to divide
644
float by integer ... but then, if we have that FPU, we don't really need
645
fixed point decoder hacks ...? */
646
float scale = 1./SHORT_SCALE;
647
648
if(buf->size < count*sizeof(float))
649
{
650
error1("%s", bufsizeerr);
651
return;
652
}
653
654
/* Work from the back since output is bigger. */
655
for(i=count-1; i>=0; --i)
656
out[i] = (float)in[i] * scale;
657
658
buf->fill = count*sizeof(float);
659
}
660
#endif
661
662
#ifndef NO_32BIT
663
static void conv_s16_to_s32(struct outbuffer *buf)
664
{
665
ptrdiff_t i;
666
int16_t *in = (int16_t*) buf->data;
667
int32_t *out = (int32_t*) buf->data;
668
size_t count = buf->fill/sizeof(int16_t);
669
670
if(buf->size < count*sizeof(int32_t))
671
{
672
error1("%s", bufsizeerr);
673
return;
674
}
675
676
/* Work from the back since output is bigger. */
677
for(i=count-1; i>=0; --i)
678
{
679
out[i] = in[i];
680
/* Could just shift bytes, but would have to mess with sign bit. */
681
out[i] *= S32_RESCALE;
682
}
683
684
buf->fill = count*sizeof(int32_t);
685
}
686
#endif
687
#endif
688
689
#include "../common/swap_bytes_impl.h"
690
691
static void swap_endian(struct outbuffer *buf, int block)
692
{
693
size_t count;
694
695
if(block >= 2)
696
{
697
count = buf->fill/(unsigned int)block;
698
swap_bytes(buf->data, (size_t)block, count);
699
}
700
}
701
702
void INT123_postprocess_buffer(mpg123_handle *fr)
703
{
704
/*
705
This caters for the final output formats that are never produced by
706
decoder synth directly (wide unsigned and 24 bit formats) or that are
707
missing because of limited decoder precision (16 bit synth but 32 or
708
24 bit output).
709
*/
710
switch(fr->af.dec_enc)
711
{
712
#ifndef NO_32BIT
713
case MPG123_ENC_SIGNED_32:
714
switch(fr->af.encoding)
715
{
716
case MPG123_ENC_UNSIGNED_32:
717
conv_s32_to_u32(&fr->buffer);
718
break;
719
case MPG123_ENC_UNSIGNED_24:
720
conv_s32_to_u32(&fr->buffer);
721
chop_fourth_byte(&fr->buffer);
722
break;
723
case MPG123_ENC_SIGNED_24:
724
chop_fourth_byte(&fr->buffer);
725
break;
726
}
727
break;
728
#endif
729
#ifndef NO_16BIT
730
case MPG123_ENC_SIGNED_16:
731
switch(fr->af.encoding)
732
{
733
case MPG123_ENC_UNSIGNED_16:
734
conv_s16_to_u16(&fr->buffer);
735
break;
736
#ifndef NO_REAL
737
case MPG123_ENC_FLOAT_32:
738
conv_s16_to_f32(&fr->buffer);
739
break;
740
#endif
741
#ifndef NO_32BIT
742
case MPG123_ENC_SIGNED_32:
743
conv_s16_to_s32(&fr->buffer);
744
break;
745
case MPG123_ENC_UNSIGNED_32:
746
conv_s16_to_s32(&fr->buffer);
747
conv_s32_to_u32(&fr->buffer);
748
break;
749
case MPG123_ENC_UNSIGNED_24:
750
conv_s16_to_s32(&fr->buffer);
751
conv_s32_to_u32(&fr->buffer);
752
chop_fourth_byte(&fr->buffer);
753
break;
754
case MPG123_ENC_SIGNED_24:
755
conv_s16_to_s32(&fr->buffer);
756
chop_fourth_byte(&fr->buffer);
757
break;
758
#endif
759
}
760
break;
761
#endif
762
}
763
if(fr->p.flags & MPG123_FORCE_ENDIAN)
764
{
765
if(
766
#ifdef WORDS_BIGENDIAN
767
!(
768
#endif
769
fr->p.flags & MPG123_BIG_ENDIAN
770
#ifdef WORDS_BIGENDIAN
771
)
772
#endif
773
)
774
swap_endian(&fr->buffer, mpg123_encsize(fr->af.encoding));
775
}
776
}
777
778