Debian Codegraph @

src/libsyn123/sampleconv.c

raw
1 /*
2 sampleconv: libsyn123 sample conversion functions
3
4 copyright 2018 by the mpg123 project
5 licensed under the terms of the LGPL 2.1
6 see COPYING and AUTHORS files in distribution or http://mpg123.org
7
8 initially written by Thomas Orgis
9
10 This code directly contains wave generators and the generic entry
11 code for libsyn123. The waves started the whole thing and stay here
12 for now. Other signal generators go into separate files.
13
14 This is the part of the mpg123 codebase where C99 first got into use
15 for real, but still with some caution.
16 */
17
18 #define NO_GROW_BUF
19 #define NO_SMAX
20 #define RAND_XORSHIFT32
21 #include "syn123_int.h"
22 #include "../common/sample.h"
23 #include "../common/debug.h"
24 // Avoid conflict with pragmas in isnan() and friends.
25 #undef warning
26
27 /* Conversions between native byte order encodings. */
28
29 #include "g711_impl.h"
30
31 /* Some trivial conversion functions. No need for another library. */
32
33 /* 1. From double/float to various. */
34
35 // We have symmetric signals with +/- 2^(n-1)-1 in mind, nevertheless
36 // clipping the negative side at -2^(n-1) for lossless conversion of
37 // any input in integer encoding.
38 // There might be possible optimizations. We hope for some auto-vectorization.
39 // And also, nowadays, CPUs can do so much while they wait for memory ...
40 // Dither noise is always a parameter, hopefully optimized away if zero
41 // in the ditherless code.
42 #define CONV(name, ftype, type, maxval) \
43 static type name(ftype d, float dither, size_t *clips) \
44 { \
45 type imax = maxval; \
46 if(isnan(d)) \
47 { \
48 d = 0.; \
49 ++*clips; \
50 } \
51 d *= imax; \
52 d += dither; \
53 if(d>=0) \
54 { \
55 d += 0.5; \
56 if(d > imax){ ++*clips; return imax; } \
57 else { return (type)d; } \
58 } \
59 else \
60 { \
61 d -= 0.5; \
62 if(d < (-imax-1)){++*clips; return (-imax-1); } \
63 else { return (type)d; } \
64 } \
65 }
66
67 /* If there is an actual 24 bit integer with cleared last byte, */
68 /* single precision should be accurate. Otherwise, double is needed. */
69 CONV(d_s32, double, int32_t, 2147483647L)
70 CONV(f_s16, float, int16_t, 32767)
71 CONV(f_s8, float, int8_t, 127)
72
73 // If the compiler can inline two levels ... the dithering code
74 // should be optimized away for the non-dithered calls.
75 static uint8_t f_u8(float f, float dither, size_t *clips)
76 {
77 return CONV_SU8(f_s8(f,dither,clips));
78 }
79 static uint16_t f_u16(float f, float dither, size_t *clips)
80 {
81 return CONV_SU16(f_s16(f,dither,clips));
82 }
83 static uint32_t d_u32(double d, float dither, size_t *clips)
84 {
85 return CONV_SU32(d_s32(d,dither,clips));
86 }
87 static unsigned char f_alaw(float f, float dither, size_t *clips)
88 {
89 return linear2alaw(f_s16(f,dither,clips));
90 }
91 static unsigned char f_ulaw(float f, float dither, size_t *clips)
92 {
93 return linear2ulaw(f_s16(f,dither,clips));
94 }
95
96 // 2. From various to double/float.
97 // I used to limit the most negative number to -1 here, but that
98 // counters the careful non-clipping of that value when converting
99 // from float. When I want to reproduce the whole range of given
100 // integer input, I shall just divide here.
101
102 static double s32_d(int32_t n)
103 {
104 return (double)n/2147483647.;
105 }
106 static float s16_f(int16_t n)
107 {
108 return (float)n/32767.f;
109 }
110 static float s8_f (int8_t n)
111 {
112 return (float)n/127.f;
113 }
114 static float u8_f (uint8_t u) { return s8_f(CONV_US8(u)); }
115 static float u16_f(uint16_t u) { return s16_f(CONV_US16(u)); }
116 static double u32_d(uint32_t u) { return s32_d(CONV_US32(u)); }
117 static float alaw_f(unsigned char n){ return s16_f(alaw2linear(n)); }
118 static float ulaw_f(unsigned char n){ return s16_f(ulaw2linear(n)); }
119
120 size_t attribute_align_arg
121 syn123_clip(void *buf, int encoding, size_t samples)
122 {
123 if(!buf)
124 return 0;
125
126 size_t clipped = 0;
127 #define CLIPCODE(type) \
128 { \
129 type *p = buf; \
130 for(size_t i=0; i<samples; ++i) \
131 if(isnan(p[i])) { p[i] = 0.0; ++clipped; } \
132 else if(p[i] < -1.0){ p[i] = -1.0; ++clipped; } \
133 else if(p[i] > +1.0){ p[i] = +1.0; ++clipped; } \
134 }
135 switch(encoding)
136 {
137 case MPG123_ENC_FLOAT_32:
138 CLIPCODE(float)
139 break;
140 case MPG123_ENC_FLOAT_64:
141 CLIPCODE(double)
142 break;
143 }
144 #undef CLIPCODE
145 return clipped;
146 }
147
148 size_t attribute_align_arg
149 syn123_soft_clip( void *buf, int encoding, size_t samples
150 , double limit, double width, syn123_handle *sh )
151 {
152 if(!buf)
153 return 0;
154 if(width > limit)
155 limit = width;
156 size_t clipped = 0;
157 // I wonder how well branch predictors work with this tangle.
158 #define CLIPCODE(type) \
159 { \
160 type w = (type)width; \
161 type ww = w*w; \
162 type w21 = 2*w-(type)limit; \
163 type h = (type)limit-w; \
164 type l = -(type)limit+w; \
165 type *p = buf; \
166 for(size_t i=0; i<samples; ++i) \
167 { \
168 if(isnan(p[i])) { p[i] = (type)0.; ++clipped; } \
169 else if(p[i] > h){ p[i] = (type)limit - ww/(w21+p[i]); ++clipped; } \
170 else if(p[i] < l){ p[i] = -(type)limit + ww/(w21-p[i]); ++clipped; } \
171 } \
172 }
173 switch(encoding)
174 {
175 case MPG123_ENC_FLOAT_32:
176 CLIPCODE(float)
177 break;
178 case MPG123_ENC_FLOAT_64:
179 CLIPCODE(double)
180 break;
181 }
182 #undef CLIPCODE
183 if(sh)
184 {
185 char *cbuf = buf;
186 int mixenc = syn123_mixenc(encoding, encoding);
187 int mixframe = MPG123_SAMPLESIZE(mixenc);
188 int inframe = MPG123_SAMPLESIZE(encoding);
189 if(!mixenc || !mixframe || !inframe)
190 return 0;
191 // Use the whole workbuf, both halves.
192 int mbufblock = sizeof(sh->workbuf)/mixframe;
193 mdebug("mbufblock=%i (enc %i)", mbufblock, mixenc);
194 while(samples)
195 {
196 int block = (int)smin(samples, mbufblock);
197 int err = mixenc == MPG123_ENC_FLOAT_64
198 ? syn123_conv(
199 sh->workbuf.f64, mixenc, sizeof(sh->workbuf.f64)
200 , cbuf, encoding, inframe*block
201 , NULL, NULL, NULL )
202 : syn123_conv(
203 sh->workbuf.f32, mixenc, sizeof(sh->workbuf.f32)
204 , cbuf, encoding, inframe*block
205 , NULL, NULL, NULL );
206 if(!err)
207 {
208 clipped += mixenc == MPG123_ENC_FLOAT_64
209 ? syn123_soft_clip(sh->workbuf.f64, mixenc, block, limit, width, NULL)
210 : syn123_soft_clip(sh->workbuf.f32, mixenc, block, limit, width, NULL);
211 // No additional clipping can happen here.
212 err = mixenc == MPG123_ENC_FLOAT_64
213 ? syn123_conv(
214 cbuf, encoding, inframe*block
215 , sh->workbuf.f64, mixenc, mixframe*block
216 , NULL, NULL, NULL )
217 : syn123_conv(
218 cbuf, encoding, inframe*block
219 , sh->workbuf.f32, mixenc, mixframe*block
220 , NULL, NULL, NULL );
221 }
222 if(err)
223 {
224 mdebug("conv error: %i", err);
225 return clipped; // If there really is an error, this should be zero, still.
226 }
227 cbuf += block*inframe;
228 samples -= block;
229 }
230 }
231 return clipped;
232 }
233
234 // All together in a happy matrix game, but only directly to/from
235 // double or float.
236
237 #define FROM_FLT(type) \
238 { type *tsrc = src; type *tend = tsrc+samples; char *tdest = dst; \
239 switch(dst_enc) \
240 { \
241 case MPG123_ENC_SIGNED_16: \
242 for(; tsrc!=tend; ++tsrc, tdest+=2) \
243 *(int16_t*)tdest = f_s16(*tsrc, 0, &clips); \
244 break; \
245 case MPG123_ENC_SIGNED_32: \
246 for(; tsrc!=tend; ++tsrc, tdest+=4) \
247 *(int32_t*)tdest = d_s32(*tsrc, 0, &clips); \
248 break; \
249 case MPG123_ENC_SIGNED_24: \
250 for(; tsrc!=tend; ++tsrc, tdest+=3) \
251 { \
252 union { int32_t i; char c[4]; } tmp; \
253 tmp.i = d_s32(*tsrc, 0, &clips); \
254 DROP4BYTE(tdest, tmp.c); \
255 } \
256 break; \
257 case MPG123_ENC_SIGNED_8: \
258 for(; tsrc!=tend; ++tsrc, tdest+=1) \
259 *(int8_t*)tdest = f_s8(*tsrc, 0, &clips); \
260 break; \
261 case MPG123_ENC_ULAW_8: \
262 for(; tsrc!=tend; ++tsrc, tdest+=1) \
263 *(unsigned char*)tdest = f_ulaw(*tsrc, 0, &clips); \
264 break; \
265 case MPG123_ENC_ALAW_8: \
266 for(; tsrc!=tend; ++tsrc, tdest+=1) \
267 *(unsigned char*)tdest = f_alaw(*tsrc, 0, &clips); \
268 break; \
269 case MPG123_ENC_UNSIGNED_8: \
270 for(; tsrc!=tend; ++tsrc, tdest+=1) \
271 *(uint8_t*)tdest = f_u8(*tsrc, 0, &clips); \
272 break; \
273 case MPG123_ENC_UNSIGNED_16: \
274 for(; tsrc!=tend; ++tsrc, tdest+=2) \
275 *(uint16_t*)tdest = f_u16(*tsrc, 0, &clips); \
276 break; \
277 case MPG123_ENC_UNSIGNED_24: \
278 for(; tsrc!=tend; ++tsrc, tdest+=3) \
279 { \
280 union { uint32_t i; char c[4]; } tmp; \
281 tmp.i = d_u32(*tsrc, 0, &clips); \
282 DROP4BYTE(tdest, tmp.c); \
283 } \
284 break; \
285 case MPG123_ENC_UNSIGNED_32: \
286 for(; tsrc!=tend; ++tsrc, tdest+=4) \
287 *(uint32_t*)tdest = d_u32(*tsrc, 0, &clips); \
288 break; \
289 case MPG123_ENC_FLOAT_32: \
290 for(; tsrc!=tend; ++tsrc, tdest+=4) \
291 *((float*)tdest) = (float)*tsrc; \
292 break; \
293 case MPG123_ENC_FLOAT_64: \
294 for(; tsrc!=tend; ++tsrc, tdest+=8) \
295 *(double*)tdest = *tsrc; \
296 break; \
297 default: \
298 return SYN123_BAD_CONV; \
299 }}
300
301
302 static float dither_noise(uint32_t *seed)
303 {
304 return rand_xorshift32(seed) + rand_xorshift32(seed);
305 }
306
307 // Dithering to 24 bit needs attention: I think just
308 // adding 255*[-1:1] before cutting off the last byte should do the trick.
309 #define NOISE dither_noise(&sh->dither_seed)
310 #define FROM_FLT_DITHER(type) \
311 { type *tsrc = src; type *tend = tsrc+samples; char *tdest = dst; \
312 switch(dst_enc) \
313 { \
314 case MPG123_ENC_SIGNED_16: \
315 for(; tsrc!=tend; ++tsrc, tdest+=2) \
316 *(int16_t*)tdest = f_s16(*tsrc, NOISE, &clips); \
317 break; \
318 case MPG123_ENC_SIGNED_32: \
319 for(; tsrc!=tend; ++tsrc, tdest+=4) \
320 *(int32_t*)tdest = d_s32(*tsrc, NOISE, &clips); \
321 break; \
322 case MPG123_ENC_SIGNED_24: \
323 for(; tsrc!=tend; ++tsrc, tdest+=3) \
324 { \
325 union { int32_t i; char c[4]; } tmp; \
326 tmp.i = d_s32(*tsrc, NOISE*255, &clips); \
327 DROP4BYTE(tdest, tmp.c); \
328 } \
329 break; \
330 case MPG123_ENC_SIGNED_8: \
331 for(; tsrc!=tend; ++tsrc, tdest+=1) \
332 *(int8_t*)tdest = f_s8(*tsrc, NOISE, &clips); \
333 break; \
334 case MPG123_ENC_ULAW_8: \
335 for(; tsrc!=tend; ++tsrc, tdest+=1) \
336 *(unsigned char*)tdest = f_ulaw(*tsrc, NOISE, &clips); \
337 break; \
338 case MPG123_ENC_ALAW_8: \
339 for(; tsrc!=tend; ++tsrc, tdest+=1) \
340 *(unsigned char*)tdest = f_alaw(*tsrc, NOISE, &clips); \
341 break; \
342 case MPG123_ENC_UNSIGNED_8: \
343 for(; tsrc!=tend; ++tsrc, tdest+=1) \
344 *(uint8_t*)tdest = f_u8(*tsrc, NOISE, &clips); \
345 break; \
346 case MPG123_ENC_UNSIGNED_16: \
347 for(; tsrc!=tend; ++tsrc, tdest+=2) \
348 *(uint16_t*)tdest = f_u16(*tsrc, NOISE, &clips); \
349 break; \
350 case MPG123_ENC_UNSIGNED_24: \
351 for(; tsrc!=tend; ++tsrc, tdest+=3) \
352 { \
353 union { uint32_t i; char c[4]; } tmp; \
354 tmp.i = d_u32(*tsrc, NOISE*255, &clips); \
355 DROP4BYTE(tdest, tmp.c); \
356 } \
357 break; \
358 case MPG123_ENC_UNSIGNED_32: \
359 for(; tsrc!=tend; ++tsrc, tdest+=4) \
360 *(uint32_t*)tdest = d_u32(*tsrc, NOISE, &clips); \
361 break; \
362 case MPG123_ENC_FLOAT_32: \
363 for(; tsrc!=tend; ++tsrc, tdest+=4) \
364 *((float*)tdest) = (float)*tsrc; \
365 break; \
366 case MPG123_ENC_FLOAT_64: \
367 for(; tsrc!=tend; ++tsrc, tdest+=8) \
368 *(double*)tdest = *tsrc; \
369 break; \
370 default: \
371 return SYN123_BAD_CONV; \
372 }}
373
374 #define TO_FLT(type) \
375 { type* tdest = dst; type* tend = tdest + samples; char * tsrc = src; \
376 switch(src_enc) \
377 { \
378 case MPG123_ENC_SIGNED_16: \
379 for(; tdest!=tend; ++tdest, tsrc+=2) \
380 *tdest = s16_f(*(int16_t*)tsrc); \
381 break; \
382 case MPG123_ENC_SIGNED_32: \
383 for(; tdest!=tend; ++tdest, tsrc+=4) \
384 *tdest = (type)s32_d(*(int32_t*)tsrc); \
385 break; \
386 case MPG123_ENC_SIGNED_24: \
387 for(; tdest!=tend; ++tdest, tsrc+=3) \
388 { \
389 union { int32_t i; char c[4]; } tmp; \
390 ADD4BYTE(tmp.c, tsrc); \
391 *tdest = (type)s32_d(tmp.i); \
392 } \
393 break; \
394 case MPG123_ENC_SIGNED_8: \
395 for(; tdest!=tend; ++tdest, tsrc+=1) \
396 *tdest = s8_f(*(int8_t*)tsrc); \
397 break; \
398 case MPG123_ENC_ULAW_8: \
399 for(; tdest!=tend; ++tdest, tsrc+=1) \
400 *tdest = ulaw_f(*(unsigned char*)tsrc); \
401 break; \
402 case MPG123_ENC_ALAW_8: \
403 for(; tdest!=tend; ++tdest, tsrc+=1) \
404 *tdest = alaw_f(*(unsigned char*)tsrc); \
405 break; \
406 case MPG123_ENC_UNSIGNED_8: \
407 for(; tdest!=tend; ++tdest, tsrc+=1) \
408 *tdest = u8_f(*(uint8_t*)tsrc); \
409 break; \
410 case MPG123_ENC_UNSIGNED_16: \
411 for(; tdest!=tend; ++tdest, tsrc+=2) \
412 *tdest = u16_f(*(uint16_t*)tsrc); \
413 break; \
414 case MPG123_ENC_UNSIGNED_24: \
415 for(; tdest!=tend; ++tdest, tsrc+=3) \
416 { \
417 union { uint32_t i; char c[4]; } tmp; \
418 ADD4BYTE(tmp.c, tsrc); \
419 *tdest = (type)u32_d(tmp.i); \
420 } \
421 break; \
422 case MPG123_ENC_UNSIGNED_32: \
423 for(; tdest!=tend; ++tdest, tsrc+=4) \
424 *tdest = (type)(u32_d(*(uint32_t*)tsrc)); \
425 break; \
426 case MPG123_ENC_FLOAT_32: \
427 for(; tdest!=tend; ++tdest, tsrc+=4) \
428 *tdest = *(float*)tsrc; \
429 break; \
430 case MPG123_ENC_FLOAT_64: \
431 for(; tdest!=tend; ++tdest, tsrc+=8) \
432 *tdest = (type)(*(double*)tsrc); \
433 break; \
434 default: \
435 return SYN123_BAD_CONV; \
436 }}
437
438 int attribute_align_arg
439 syn123_mixenc(int src_enc, int dst_enc)
440 {
441 int isize = MPG123_SAMPLESIZE(src_enc);
442 int osize = MPG123_SAMPLESIZE(dst_enc);
443 if(!isize || !osize)
444 return 0;
445 else // Only if both sides warrant high precision, double is used.
446 return ( src_enc != MPG123_ENC_FLOAT_32 && isize > 3 &&
447 dst_enc != MPG123_ENC_FLOAT_32 && osize > 3 )
448 ? MPG123_ENC_FLOAT_64
449 : MPG123_ENC_FLOAT_32;
450 }
451
452 // Return non-zero if the indicated conversion will involve roundoff errors
453 // that can be improved using dithering.
454 static int need_dither(int from, int to)
455 {
456 // No dithering when converting to float ...
457 if(to & MPG123_ENC_FLOAT)
458 return 0;
459 // ... and always dither when going from float to integer.
460 if(from & MPG123_ENC_FLOAT)
461 return 1;
462 // When converting from integer to integer, rounding occurs if the
463 // precision goes down.
464 unsigned int fromsize = MPG123_SAMPLESIZE(from);
465 unsigned int tosize = MPG123_SAMPLESIZE(to);
466 if(to & MPG123_ENC_8)
467 {
468 // The funky 8 bit encodings encode 16 bit values.
469 if(to == MPG123_ENC_ALAW_8 || to == MPG123_ENC_ULAW_8)
470 tosize = 2;
471 }
472 // If the output encoding width is same or bigger, no rounding occurs.
473 return (tosize < fromsize) ? 1 : 0;
474 }
475
476 int attribute_align_arg
477 syn123_conv( void * MPG123_RESTRICT dst, int dst_enc, size_t dst_size
478 , void * MPG123_RESTRICT src, int src_enc, size_t src_bytes
479 , size_t *dst_bytes, size_t *clipped, syn123_handle *sh )
480 {
481 size_t srcframe = MPG123_SAMPLESIZE(src_enc);
482 size_t dstframe = MPG123_SAMPLESIZE(dst_enc);
483 // Take the provided do_dither setting and make sure to have it cleared.
484 int do_dither = 0;
485 if(sh)
486 {
487 do_dither = sh->do_dither;
488 sh->do_dither = 0;
489 }
490 if(!srcframe || !dstframe)
491 return SYN123_BAD_ENC;
492 size_t samples = src_bytes/srcframe;
493 size_t clips = 0;
494 mdebug( "conv from %i (%i) to %i (%i), %zu into %zu bytes, dither %i/%i"
495 , src_enc, MPG123_SAMPLESIZE(src_enc)
496 , dst_enc, MPG123_SAMPLESIZE(dst_enc)
497 , src_bytes, dst_size
498 , sh ? sh->dither : 0, do_dither );
499 if(!dst || !src)
500 return SYN123_BAD_BUF;
501 if(samples*srcframe != src_bytes)
502 return SYN123_BAD_CHOP;
503 if(samples*dstframe > dst_size)
504 return SYN123_BAD_SIZE;
505 if(src_enc == dst_enc)
506 memcpy(dst, src, samples*dstframe);
507 // Always shortcut for converting to float, not considering dither.
508 else if(dst_enc & MPG123_ENC_FLOAT)
509 {
510 debug("to_flt");
511 if(dst_enc == MPG123_ENC_FLOAT_64)
512 TO_FLT(double)
513 else if(dst_enc == MPG123_ENC_FLOAT_32)
514 TO_FLT(float)
515 else
516 return SYN123_BAD_CONV;
517 }
518 // If converting from float, possibly add dither noise.
519 else if(src_enc & MPG123_ENC_FLOAT)
520 {
521 // Either this is a direct call and dithering is decided by given encodings
522 // or this is an intermediate conversion called from syn123_conv() itself,
523 // where the outer instance decided to enforce (1) or disable (-1) dithering.
524 if( sh && sh->dither
525 && ( do_dither == 1
526 || (do_dither == 0 && need_dither(src_enc, dst_enc))
527 ) )
528 {
529 debug("from_flt dither");
530 if(src_enc == MPG123_ENC_FLOAT_64)
531 FROM_FLT_DITHER(double)
532 else if(src_enc == MPG123_ENC_FLOAT_32)
533 FROM_FLT_DITHER(float)
534 else
535 return SYN123_BAD_CONV;
536 }
537 else
538 {
539 debug("from_flt");
540 if(src_enc == MPG123_ENC_FLOAT_64)
541 FROM_FLT(double)
542 else if(src_enc == MPG123_ENC_FLOAT_32)
543 FROM_FLT(float)
544 else
545 return SYN123_BAD_CONV;
546 }
547 }
548 else if(sh)
549 {
550 char *cdst = dst;
551 char *csrc = src;
552 int mixenc = syn123_mixenc(src_enc, dst_enc);
553 int mixframe = MPG123_SAMPLESIZE(mixenc);
554 if(!mixenc || !mixframe)
555 return SYN123_BAD_CONV;
556 // Use the whole workbuf, both halves.
557 int mbufblock = sizeof(sh->workbuf)/mixframe;
558 mdebug("mbufblock=%i (enc %i)", mbufblock, mixenc);
559 // Abuse the handle workbuf for intermediate storage.
560 size_t samples_left = samples;
561 // The final conversion needs to apply dithering depending on
562 // actual input and output encodings.
563 // The intermediate calls get dithering on/off enforced via sh->do_dither.
564 if(!do_dither)
565 do_dither = sh->dither && need_dither(src_enc, dst_enc) ? 1 : -1;
566 while(samples_left)
567 {
568 int block = (int)smin(samples_left, mbufblock);
569 size_t clipped_now = 0;
570 int err = mixenc == MPG123_ENC_FLOAT_64
571 ? syn123_conv(
572 sh->workbuf.f64, mixenc, sizeof(sh->workbuf.f64)
573 , csrc, src_enc, srcframe*block
574 , NULL, NULL, NULL )
575 : syn123_conv(
576 sh->workbuf.f32, mixenc, sizeof(sh->workbuf.f32)
577 , csrc, src_enc, srcframe*block
578 , NULL, NULL, NULL );
579 sh->do_dither = do_dither; // possibly dither now
580 if(!err)
581 err = mixenc == MPG123_ENC_FLOAT_64
582 ? syn123_conv(
583 cdst, dst_enc, dstframe*block
584 , sh->workbuf.f64, mixenc, mixframe*block
585 , NULL, &clipped_now, sh )
586 : syn123_conv(
587 cdst, dst_enc, dstframe*block
588 , sh->workbuf.f32, mixenc, mixframe*block
589 , NULL, &clipped_now, sh );
590 if(err)
591 {
592 mdebug("conv error: %i", err);
593 return SYN123_BAD_CONV;
594 }
595 clips += clipped_now;
596 cdst += dstframe*block;
597 csrc += srcframe*block;
598 samples_left -= block;
599 }
600 } else
601 return SYN123_BAD_CONV;
602 if(dst_bytes)
603 *dst_bytes = dstframe*samples;
604 if(clipped)
605 *clipped = clips;
606 return SYN123_OK;
607 }
608
609 // Any compiler worth its salt should be able to unroll the
610 // fixed loops resultung from this macro. And yes, I should
611 // measure that. Goal is the hardware memory bandwidth.
612 // Any impact from putting the channel loop to the outside?
613
614 #define BYTEMULTIPLY(dest, src, bytes, count, channels) \
615 { \
616 for(size_t i=0; i<count; ++i) \
617 { \
618 for(int j=0; j<channels; ++j) \
619 for(size_t b=0; b<bytes; ++b) \
620 ((char*)dest)[(i*channels+j)*bytes+b] = ((char*)src)[i*bytes+b]; \
621 } \
622 }
623
624 #define BYTEINTERLEAVE(dest, src, bytes, count, channels) \
625 { \
626 for(size_t i=0; i<count; ++i) \
627 { \
628 for(int j=0; j<channels; ++j) \
629 for(size_t b=0; b<bytes; ++b) \
630 ((char*)dest)[(i*channels+j)*bytes+b] = ((char**)src)[j][i*bytes+b]; \
631 } \
632 }
633
634 #define BYTEDEINTERLEAVE(dest, src, bytes, count, channels) \
635 { \
636 for(size_t i=0; i<count; ++i) \
637 { \
638 for(int j=0; j<channels; ++j) \
639 for(size_t b=0; b<bytes; ++b) \
640 ((char**)dest)[j][i*bytes+b] = ((char*)src)[(i*channels+j)*bytes+b]; \
641 } \
642 }
643
644 /* Special case of multiplying a mono stream. */
645 void attribute_align_arg
646 syn123_mono2many( void * MPG123_RESTRICT dst, void * MPG123_RESTRICT src
647 , int channels, size_t samplesize, size_t samplecount )
648 {
649 #ifndef SYN123_NO_CASES
650 switch(channels)
651 {
652 case 1:
653 memcpy(dst, src, samplesize*samplecount);
654 break;
655 case 2:
656 switch(samplesize)
657 {
658 case 1:
659 BYTEMULTIPLY(dst, src, 1, samplecount, 2)
660 break;
661 case 2:
662 BYTEMULTIPLY(dst, src, 2, samplecount, 2)
663 break;
664 case 3:
665 BYTEMULTIPLY(dst, src, 3, samplecount, 2)
666 break;
667 case 4:
668 BYTEMULTIPLY(dst, src, 4, samplecount, 2)
669 break;
670 default:
671 BYTEMULTIPLY(dst, src, samplesize, samplecount, 2)
672 break;
673 }
674 break;
675 default:
676 switch(samplesize)
677 {
678 case 1:
679 BYTEMULTIPLY(dst, src, 1, samplecount, channels)
680 break;
681 case 2:
682 BYTEMULTIPLY(dst, src, 2, samplecount, channels)
683 break;
684 case 3:
685 BYTEMULTIPLY(dst, src, 3, samplecount, channels)
686 break;
687 case 4:
688 BYTEMULTIPLY(dst, src, 4, samplecount, channels)
689 break;
690 default:
691 BYTEMULTIPLY(dst, src, samplesize, samplecount, channels)
692 break;
693 }
694 break;
695 }
696 #else
697 BYTEMULTIPLY(dst, src, samplesize, samplecount, channels)
698 #endif
699 }
700
701 void attribute_align_arg
702 syn123_interleave(void * MPG123_RESTRICT dst, void ** MPG123_RESTRICT src
703 , int channels, size_t samplesize, size_t samplecount)
704 {
705 #ifndef SYN123_NO_CASES
706 switch(channels)
707 {
708 case 1:
709 memcpy(dst, src, samplesize*samplecount);
710 break;
711 case 2:
712 switch(samplesize)
713 {
714 case 1:
715 BYTEINTERLEAVE(dst, src, 1, samplecount, 2)
716 break;
717 case 2:
718 BYTEINTERLEAVE(dst, src, 2, samplecount, 2)
719 break;
720 case 3:
721 BYTEINTERLEAVE(dst, src, 3, samplecount, 2)
722 break;
723 case 4:
724 BYTEINTERLEAVE(dst, src, 4, samplecount, 2)
725 break;
726 default:
727 BYTEINTERLEAVE(dst, src, samplesize, samplecount, 2)
728 break;
729 }
730 break;
731 default:
732 switch(samplesize)
733 {
734 case 1:
735 BYTEINTERLEAVE(dst, src, 1, samplecount, channels)
736 break;
737 case 2:
738 BYTEINTERLEAVE(dst, src, 2, samplecount, channels)
739 break;
740 case 3:
741 BYTEINTERLEAVE(dst, src, 3, samplecount, channels)
742 break;
743 case 4:
744 BYTEINTERLEAVE(dst, src, 4, samplecount, channels)
745 break;
746 default:
747 BYTEINTERLEAVE(dst, src, samplesize, samplecount, channels)
748 break;
749 }
750 break;
751 }
752 #else
753 BYTEINTERLEAVE(dst, src, samplesize, samplecount, channels)
754 #endif
755 }
756
757 void attribute_align_arg
758 syn123_deinterleave(void ** MPG123_RESTRICT dst, void * MPG123_RESTRICT src
759 , int channels, size_t samplesize, size_t samplecount)
760 {
761 #ifndef SYN123_NO_CASES
762 switch(channels)
763 {
764 case 1:
765 memcpy(dst[0], src, samplesize*samplecount);
766 break;
767 case 2:
768 switch(samplesize)
769 {
770 case 1:
771 BYTEDEINTERLEAVE(dst, src, 1, samplecount, 2)
772 break;
773 case 2:
774 BYTEDEINTERLEAVE(dst, src, 2, samplecount, 2)
775 break;
776 case 3:
777 BYTEDEINTERLEAVE(dst, src, 3, samplecount, 2)
778 break;
779 case 4:
780 BYTEDEINTERLEAVE(dst, src, 4, samplecount, 2)
781 break;
782 default:
783 BYTEDEINTERLEAVE(dst, src, samplesize, samplecount, 2)
784 break;
785 }
786 break;
787 default:
788 switch(samplesize)
789 {
790 case 1:
791 BYTEDEINTERLEAVE(dst, src, 1, samplecount, channels)
792 break;
793 case 2:
794 BYTEDEINTERLEAVE(dst, src, 2, samplecount, channels)
795 break;
796 case 3:
797 BYTEDEINTERLEAVE(dst, src, 3, samplecount, channels)
798 break;
799 case 4:
800 BYTEDEINTERLEAVE(dst, src, 4, samplecount, channels)
801 break;
802 default:
803 BYTEDEINTERLEAVE(dst, src, samplesize, samplecount, channels)
804 break;
805 }
806 break;
807 }
808 #else
809 BYTEDEINTERLEAVE(dst, src, samplesize, samplecount, channels)
810 #endif
811 }
812
813 #define MIX_CODE(type,scc,dcc) \
814 for(size_t i=0; i<samples; ++i) \
815 { \
816 for(int dc=0; dc<dcc; ++dc) \
817 { \
818 for(int sc=0; sc<scc; ++sc) \
819 dst[SYN123_IOFF(i,dc,dcc)] += \
820 (type)mixmatrix[SYN123_IOFF(dc,sc,scc)] * src[SYN123_IOFF(i,sc,scc)]; \
821 } \
822 }
823
824 // I decided against optimizing for mixing factors of 1, unity matrix ...
825 // A floating point multiplication is not that expensive and there might
826 // be value in the runtime of the function not depending on the matrix
827 // values, only on channel setup and sample count.
828 // I might trust the compiler here to hardcode the case value for
829 // src/dst_channels. But why take the risk?
830 #ifndef SYN123_NO_CASES
831 #define SYN123_MIX_FUNC(type) \
832 switch(src_channels) \
833 { \
834 case 1: \
835 switch(dst_channels) \
836 { \
837 case 1: \
838 MIX_CODE(type,1,1) \
839 break; \
840 case 2: \
841 MIX_CODE(type,1,2) \
842 break; \
843 default: \
844 MIX_CODE(type,1,dst_channels) \
845 } \
846 break; \
847 case 2: \
848 switch(dst_channels) \
849 { \
850 case 1: \
851 MIX_CODE(type,2,1) \
852 break; \
853 case 2: \
854 MIX_CODE(type,2,2) \
855 break; \
856 default: \
857 MIX_CODE(type,2,dst_channels) \
858 } \
859 break; \
860 default: \
861 MIX_CODE(type,src_channels, dst_channels) \
862 }
863 #else
864 #define SYN123_MIX_FUNC(type) \
865 MIX_CODE(type, src_channels, dst_channels)
866 #endif
867
868 static void syn123_mix_f32( float * MPG123_RESTRICT dst, int dst_channels
869 , float * MPG123_RESTRICT src, int src_channels
870 , const double * MPG123_RESTRICT mixmatrix
871 , size_t samples )
872 {
873 debug("syn123_mix_f32");
874 SYN123_MIX_FUNC(float)
875 }
876
877 static void syn123_mix_f64( double * MPG123_RESTRICT dst, int dst_channels
878 , double * MPG123_RESTRICT src, int src_channels
879 , const double * MPG123_RESTRICT mixmatrix
880 , size_t samples )
881 {
882 debug("syn123_mix_f64");
883 SYN123_MIX_FUNC(double)
884 }
885
886 // Call with f64 or f32 for conversion mixer code piece.
887 #define CONVMIX(fXX) \
888 err = syn123_conv( \
889 sh->workbuf.fXX[0], mixenc, sizeof(sh->workbuf.fXX[0]) \
890 , csrc, src_enc, srcframe*block \
891 , NULL, NULL, NULL ); \
892 if(err) \
893 goto mix_end; \
894 /* Initialize to zero or convert from old output signal. */ \
895 if(silence) \
896 for(int i=0; i<block*dst_channels; ++i) \
897 sh->workbuf.fXX[1][i] = 0.; \
898 else \
899 err = syn123_conv( \
900 sh->workbuf.fXX[1], mixenc, sizeof(sh->workbuf.fXX[1]) \
901 , cdst, dst_enc, dstframe*block \
902 , NULL, NULL, NULL ); \
903 /* Now mix one work buffer into the other. */ \
904 if(err) \
905 goto mix_end; \
906 err = syn123_mix( sh->workbuf.fXX[1], mixenc, dst_channels \
907 , sh->workbuf.fXX[0], mixenc, src_channels, mixmatrix, block \
908 , 0, NULL, NULL ); \
909 if(err) \
910 goto mix_end; \
911 /* And convert to the final output format. */ \
912 err = syn123_conv( \
913 cdst, dst_enc, dstframe*block \
914 , sh->workbuf.fXX[1], mixenc, mixoutframe*block \
915 , NULL, &clips_block, NULL );
916
917
918 int attribute_align_arg
919 syn123_mix( void * MPG123_RESTRICT dst, int dst_enc, int dst_channels
920 , void * MPG123_RESTRICT src, int src_enc, int src_channels
921 , const double * mixmatrix
922 , size_t samples, int silence, size_t *clipped, syn123_handle *sh )
923 {
924 int err = 0;
925 size_t clips = 0;
926 if(src_channels < 1 || dst_channels < 1)
927 {
928 err = SYN123_BAD_FMT;
929 goto mix_end;
930 }
931 if(!dst || !src || !mixmatrix)
932 {
933 err = SYN123_BAD_BUF;
934 goto mix_end;
935 }
936 if(dst_enc == src_enc) switch(dst_enc)
937 {
938 case MPG123_ENC_FLOAT_32:
939 if(silence)
940 for(size_t i=0; i<samples*dst_channels; ++i)
941 ((float*)dst)[i] = 0;
942 syn123_mix_f32( dst, dst_channels, src, src_channels
943 , mixmatrix, samples );
944 goto mix_end;
945 break;
946 case MPG123_ENC_FLOAT_64:
947 if(silence)
948 for(size_t i=0; i<samples*dst_channels; ++i)
949 ((double*)dst)[i] = 0;
950 syn123_mix_f64( dst, dst_channels, src, src_channels
951 , mixmatrix, samples );
952 goto mix_end;
953 break;
954 }
955 // If still here: Some conversion needed.
956 if(!sh)
957 {
958 err = SYN123_BAD_ENC;
959 goto mix_end;
960 }
961 else
962 {
963 debug("mix with conversion");
964 char *cdst = dst;
965 char *csrc = src;
966 int srcframe = MPG123_SAMPLESIZE(src_enc)*src_channels;
967 int dstframe = MPG123_SAMPLESIZE(dst_enc)*dst_channels;
968 if(!srcframe || !dstframe)
969 {
970 err = SYN123_BAD_ENC;
971 goto mix_end;
972 }
973 int mixenc = syn123_mixenc(src_enc, dst_enc);
974 int mixinframe = MPG123_SAMPLESIZE(mixenc)*src_channels;
975 int mixoutframe = MPG123_SAMPLESIZE(mixenc)*dst_channels;
976 int mixframe = mixinframe > mixoutframe ? mixinframe : mixoutframe;
977 if(!mixenc || !mixinframe || !mixoutframe)
978 {
979 err = SYN123_BAD_CONV;
980 goto mix_end;
981 }
982 // Mix from buffblock[0] to buffblock[1].
983 int mbufblock = sizeof(sh->workbuf)/2/mixframe;
984 mdebug("mbufblock=%i (enc %i)", mbufblock, mixenc);
985 // Need at least one sample per round to avoid endless loop.
986 // Of course, we would prefer more, but it's your fault for
987 // giving an excessive amount of channels without handling conversion
988 // beforehand.
989 if(mbufblock < 1)
990 {
991 err = SYN123_BAD_CONV;
992 goto mix_end;
993 }
994 while(samples)
995 {
996 int block = (int)smin(samples, mbufblock);
997 size_t clips_block = 0;
998 // The same logic for f64 and f32, just written explictly for buffer type clarity.
999 if(mixenc == MPG123_ENC_FLOAT_64)
1000 {
1001 CONVMIX(f64)
1002 } else
1003 {
1004 CONVMIX(f32)
1005 }
1006 clips += clips_block;
1007 if(err)
1008 goto mix_end;
1009 cdst += dstframe*block;
1010 csrc += srcframe*block;
1011 samples -= block;
1012 }
1013 }
1014 mix_end:
1015 if(clipped)
1016 *clipped = clips;
1017 return err;
1018 }
1019
1020 /* All the byte-swappery for those little big endian boxes. */
1021
1022 #include "../common/swap_bytes_impl.h"
1023
1024 void attribute_align_arg
1025 syn123_swap_bytes(void* buf, size_t samplesize, size_t samplecount)
1026 {
1027 swap_bytes(buf, samplesize, samplecount);
1028 }
1029
1030 void attribute_align_arg
1031 syn123_host2le(void *buf, size_t samplesize, size_t samplecount)
1032 {
1033 #ifdef WORDS_BIGENDIAN
1034 swap_bytes(buf, samplesize, samplecount);
1035 #endif
1036 }
1037
1038 void attribute_align_arg
1039 syn123_host2be(void *buf, size_t samplesize, size_t samplecount)
1040 {
1041 #ifndef WORDS_BIGENDIAN
1042 swap_bytes(buf, samplesize, samplecount);
1043 #endif
1044 }
1045
1046 void attribute_align_arg
1047 syn123_le2host(void *buf, size_t samplesize, size_t samplecount)
1048 {
1049 #ifdef WORDS_BIGENDIAN
1050 swap_bytes(buf, samplesize, samplecount);
1051 #endif
1052 }
1053
1054 void attribute_align_arg
1055 syn123_be2host(void *buf, size_t samplesize, size_t samplecount)
1056 {
1057 #ifndef WORDS_BIGENDIAN
1058 swap_bytes(buf, samplesize, samplecount);
1059 #endif
1060 }