1 /* 2 * QEMU Mixing engine 3 * 4 * Copyright (c) 2004-2005 Vassili Karpov (malc) 5 * Copyright (c) 1998 Fabrice Bellard 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 #include "qemu/osdep.h" 26 #include "qemu/bswap.h" 27 #include "qemu/error-report.h" 28 #include "audio.h" 29 30 #define AUDIO_CAP "mixeng" 31 #include "audio_int.h" 32 33 /* 8 bit */ 34 #define ENDIAN_CONVERSION natural 35 #define ENDIAN_CONVERT(v) (v) 36 37 /* Signed 8 bit */ 38 #define BSIZE 8 39 #define ITYPE int 40 #define IN_MIN SCHAR_MIN 41 #define IN_MAX SCHAR_MAX 42 #define SIGNED 43 #define SHIFT 8 44 #include "mixeng_template.h" 45 #undef SIGNED 46 #undef IN_MAX 47 #undef IN_MIN 48 #undef BSIZE 49 #undef ITYPE 50 #undef SHIFT 51 52 /* Unsigned 8 bit */ 53 #define BSIZE 8 54 #define ITYPE uint 55 #define IN_MIN 0 56 #define IN_MAX UCHAR_MAX 57 #define SHIFT 8 58 #include "mixeng_template.h" 59 #undef IN_MAX 60 #undef IN_MIN 61 #undef BSIZE 62 #undef ITYPE 63 #undef SHIFT 64 65 #undef ENDIAN_CONVERT 66 #undef ENDIAN_CONVERSION 67 68 /* Signed 16 bit */ 69 #define BSIZE 16 70 #define ITYPE int 71 #define IN_MIN SHRT_MIN 72 #define IN_MAX SHRT_MAX 73 #define SIGNED 74 #define SHIFT 16 75 #define ENDIAN_CONVERSION natural 76 #define ENDIAN_CONVERT(v) (v) 77 #include "mixeng_template.h" 78 #undef ENDIAN_CONVERT 79 #undef ENDIAN_CONVERSION 80 #define ENDIAN_CONVERSION swap 81 #define ENDIAN_CONVERT(v) bswap16 (v) 82 #include "mixeng_template.h" 83 #undef ENDIAN_CONVERT 84 #undef ENDIAN_CONVERSION 85 #undef SIGNED 86 #undef IN_MAX 87 #undef IN_MIN 88 #undef BSIZE 89 #undef ITYPE 90 #undef SHIFT 91 92 /* Unsigned 16 bit */ 93 #define BSIZE 16 94 #define ITYPE uint 95 #define IN_MIN 0 96 #define IN_MAX USHRT_MAX 97 #define SHIFT 16 98 #define ENDIAN_CONVERSION natural 99 #define ENDIAN_CONVERT(v) (v) 100 #include "mixeng_template.h" 101 #undef ENDIAN_CONVERT 102 #undef ENDIAN_CONVERSION 103 #define ENDIAN_CONVERSION swap 104 #define ENDIAN_CONVERT(v) bswap16 (v) 105 #include "mixeng_template.h" 106 #undef ENDIAN_CONVERT 107 #undef ENDIAN_CONVERSION 108 #undef IN_MAX 109 #undef IN_MIN 110 #undef BSIZE 111 #undef ITYPE 112 #undef SHIFT 113 114 /* Signed 32 bit */ 115 #define BSIZE 32 116 #define ITYPE int 117 #define IN_MIN INT32_MIN 118 #define IN_MAX INT32_MAX 119 #define SIGNED 120 #define SHIFT 32 121 #define ENDIAN_CONVERSION natural 122 #define ENDIAN_CONVERT(v) (v) 123 #include "mixeng_template.h" 124 #undef ENDIAN_CONVERT 125 #undef ENDIAN_CONVERSION 126 #define ENDIAN_CONVERSION swap 127 #define ENDIAN_CONVERT(v) bswap32 (v) 128 #include "mixeng_template.h" 129 #undef ENDIAN_CONVERT 130 #undef ENDIAN_CONVERSION 131 #undef SIGNED 132 #undef IN_MAX 133 #undef IN_MIN 134 #undef BSIZE 135 #undef ITYPE 136 #undef SHIFT 137 138 /* Unsigned 32 bit */ 139 #define BSIZE 32 140 #define ITYPE uint 141 #define IN_MIN 0 142 #define IN_MAX UINT32_MAX 143 #define SHIFT 32 144 #define ENDIAN_CONVERSION natural 145 #define ENDIAN_CONVERT(v) (v) 146 #include "mixeng_template.h" 147 #undef ENDIAN_CONVERT 148 #undef ENDIAN_CONVERSION 149 #define ENDIAN_CONVERSION swap 150 #define ENDIAN_CONVERT(v) bswap32 (v) 151 #include "mixeng_template.h" 152 #undef ENDIAN_CONVERT 153 #undef ENDIAN_CONVERSION 154 #undef IN_MAX 155 #undef IN_MIN 156 #undef BSIZE 157 #undef ITYPE 158 #undef SHIFT 159 160 t_sample *mixeng_conv[2][2][2][3] = { 161 { 162 { 163 { 164 conv_natural_uint8_t_to_mono, 165 conv_natural_uint16_t_to_mono, 166 conv_natural_uint32_t_to_mono 167 }, 168 { 169 conv_natural_uint8_t_to_mono, 170 conv_swap_uint16_t_to_mono, 171 conv_swap_uint32_t_to_mono, 172 } 173 }, 174 { 175 { 176 conv_natural_int8_t_to_mono, 177 conv_natural_int16_t_to_mono, 178 conv_natural_int32_t_to_mono 179 }, 180 { 181 conv_natural_int8_t_to_mono, 182 conv_swap_int16_t_to_mono, 183 conv_swap_int32_t_to_mono 184 } 185 } 186 }, 187 { 188 { 189 { 190 conv_natural_uint8_t_to_stereo, 191 conv_natural_uint16_t_to_stereo, 192 conv_natural_uint32_t_to_stereo 193 }, 194 { 195 conv_natural_uint8_t_to_stereo, 196 conv_swap_uint16_t_to_stereo, 197 conv_swap_uint32_t_to_stereo 198 } 199 }, 200 { 201 { 202 conv_natural_int8_t_to_stereo, 203 conv_natural_int16_t_to_stereo, 204 conv_natural_int32_t_to_stereo 205 }, 206 { 207 conv_natural_int8_t_to_stereo, 208 conv_swap_int16_t_to_stereo, 209 conv_swap_int32_t_to_stereo, 210 } 211 } 212 } 213 }; 214 215 f_sample *mixeng_clip[2][2][2][3] = { 216 { 217 { 218 { 219 clip_natural_uint8_t_from_mono, 220 clip_natural_uint16_t_from_mono, 221 clip_natural_uint32_t_from_mono 222 }, 223 { 224 clip_natural_uint8_t_from_mono, 225 clip_swap_uint16_t_from_mono, 226 clip_swap_uint32_t_from_mono 227 } 228 }, 229 { 230 { 231 clip_natural_int8_t_from_mono, 232 clip_natural_int16_t_from_mono, 233 clip_natural_int32_t_from_mono 234 }, 235 { 236 clip_natural_int8_t_from_mono, 237 clip_swap_int16_t_from_mono, 238 clip_swap_int32_t_from_mono 239 } 240 } 241 }, 242 { 243 { 244 { 245 clip_natural_uint8_t_from_stereo, 246 clip_natural_uint16_t_from_stereo, 247 clip_natural_uint32_t_from_stereo 248 }, 249 { 250 clip_natural_uint8_t_from_stereo, 251 clip_swap_uint16_t_from_stereo, 252 clip_swap_uint32_t_from_stereo 253 } 254 }, 255 { 256 { 257 clip_natural_int8_t_from_stereo, 258 clip_natural_int16_t_from_stereo, 259 clip_natural_int32_t_from_stereo 260 }, 261 { 262 clip_natural_int8_t_from_stereo, 263 clip_swap_int16_t_from_stereo, 264 clip_swap_int32_t_from_stereo 265 } 266 } 267 } 268 }; 269 270 #ifdef FLOAT_MIXENG 271 #define CONV_NATURAL_FLOAT(x) (x) 272 #define CLIP_NATURAL_FLOAT(x) (x) 273 #else 274 /* macros to map [-1.f, 1.f] <-> [INT32_MIN, INT32_MAX + 1] */ 275 static const float float_scale = (int64_t)INT32_MAX + 1; 276 #define CONV_NATURAL_FLOAT(x) ((x) * float_scale) 277 278 #ifdef RECIPROCAL 279 static const float float_scale_reciprocal = 1.f / ((int64_t)INT32_MAX + 1); 280 #define CLIP_NATURAL_FLOAT(x) ((x) * float_scale_reciprocal) 281 #else 282 #define CLIP_NATURAL_FLOAT(x) ((x) / float_scale) 283 #endif 284 #endif 285 286 #define F32_TO_F32S(v) \ 287 bswap32((union { uint32_t i; float f; }){ .f = (v) }.i) 288 #define F32S_TO_F32(v) \ 289 ((union { uint32_t i; float f; }){ .i = bswap32(v) }.f) 290 291 static void conv_natural_float_to_mono(struct st_sample *dst, const void *src, 292 int samples) 293 { 294 const float *in = src; 295 296 while (samples--) { 297 dst->r = dst->l = CONV_NATURAL_FLOAT(*in++); 298 dst++; 299 } 300 } 301 302 static void conv_swap_float_to_mono(struct st_sample *dst, const void *src, 303 int samples) 304 { 305 const uint32_t *in_f32s = src; 306 307 while (samples--) { 308 dst->r = dst->l = CONV_NATURAL_FLOAT(F32S_TO_F32(*in_f32s++)); 309 dst++; 310 } 311 } 312 313 static void conv_natural_float_to_stereo(struct st_sample *dst, const void *src, 314 int samples) 315 { 316 const float *in = src; 317 318 while (samples--) { 319 dst->l = CONV_NATURAL_FLOAT(*in++); 320 dst->r = CONV_NATURAL_FLOAT(*in++); 321 dst++; 322 } 323 } 324 325 static void conv_swap_float_to_stereo(struct st_sample *dst, const void *src, 326 int samples) 327 { 328 const uint32_t *in_f32s = src; 329 330 while (samples--) { 331 dst->l = CONV_NATURAL_FLOAT(F32S_TO_F32(*in_f32s++)); 332 dst->r = CONV_NATURAL_FLOAT(F32S_TO_F32(*in_f32s++)); 333 dst++; 334 } 335 } 336 337 t_sample *mixeng_conv_float[2][2] = { 338 { 339 conv_natural_float_to_mono, 340 conv_swap_float_to_mono, 341 }, 342 { 343 conv_natural_float_to_stereo, 344 conv_swap_float_to_stereo, 345 } 346 }; 347 348 static void clip_natural_float_from_mono(void *dst, const struct st_sample *src, 349 int samples) 350 { 351 float *out = dst; 352 353 while (samples--) { 354 *out++ = CLIP_NATURAL_FLOAT(src->l + src->r); 355 src++; 356 } 357 } 358 359 static void clip_swap_float_from_mono(void *dst, const struct st_sample *src, 360 int samples) 361 { 362 uint32_t *out_f32s = dst; 363 364 while (samples--) { 365 *out_f32s++ = F32_TO_F32S(CLIP_NATURAL_FLOAT(src->l + src->r)); 366 src++; 367 } 368 } 369 370 static void clip_natural_float_from_stereo( 371 void *dst, const struct st_sample *src, int samples) 372 { 373 float *out = dst; 374 375 while (samples--) { 376 *out++ = CLIP_NATURAL_FLOAT(src->l); 377 *out++ = CLIP_NATURAL_FLOAT(src->r); 378 src++; 379 } 380 } 381 382 static void clip_swap_float_from_stereo( 383 void *dst, const struct st_sample *src, int samples) 384 { 385 uint32_t *out_f32s = dst; 386 387 while (samples--) { 388 *out_f32s++ = F32_TO_F32S(CLIP_NATURAL_FLOAT(src->l)); 389 *out_f32s++ = F32_TO_F32S(CLIP_NATURAL_FLOAT(src->r)); 390 src++; 391 } 392 } 393 394 f_sample *mixeng_clip_float[2][2] = { 395 { 396 clip_natural_float_from_mono, 397 clip_swap_float_from_mono, 398 }, 399 { 400 clip_natural_float_from_stereo, 401 clip_swap_float_from_stereo, 402 } 403 }; 404 405 void audio_sample_to_uint64(const void *samples, int pos, 406 uint64_t *left, uint64_t *right) 407 { 408 #ifdef FLOAT_MIXENG 409 error_report( 410 "Coreaudio and floating point samples are not supported by replay yet"); 411 abort(); 412 #else 413 const struct st_sample *sample = samples; 414 sample += pos; 415 *left = sample->l; 416 *right = sample->r; 417 #endif 418 } 419 420 void audio_sample_from_uint64(void *samples, int pos, 421 uint64_t left, uint64_t right) 422 { 423 #ifdef FLOAT_MIXENG 424 error_report( 425 "Coreaudio and floating point samples are not supported by replay yet"); 426 abort(); 427 #else 428 struct st_sample *sample = samples; 429 sample += pos; 430 sample->l = left; 431 sample->r = right; 432 #endif 433 } 434 435 /* 436 * August 21, 1998 437 * Copyright 1998 Fabrice Bellard. 438 * 439 * [Rewrote completely the code of Lance Norskog And Sundry 440 * Contributors with a more efficient algorithm.] 441 * 442 * This source code is freely redistributable and may be used for 443 * any purpose. This copyright notice must be maintained. 444 * Lance Norskog And Sundry Contributors are not responsible for 445 * the consequences of using this software. 446 */ 447 448 /* 449 * Sound Tools rate change effect file. 450 */ 451 /* 452 * Linear Interpolation. 453 * 454 * The use of fractional increment allows us to use no buffer. It 455 * avoid the problems at the end of the buffer we had with the old 456 * method which stored a possibly big buffer of size 457 * lcm(in_rate,out_rate). 458 * 459 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If 460 * the input & output frequencies are equal, a delay of one sample is 461 * introduced. Limited to processing 32-bit count worth of samples. 462 * 463 * 1 << FRAC_BITS evaluating to zero in several places. Changed with 464 * an (unsigned long) cast to make it safe. MarkMLl 2/1/99 465 */ 466 467 /* Private data */ 468 struct rate { 469 uint64_t opos; 470 uint64_t opos_inc; 471 uint32_t ipos; /* position in the input stream (integer) */ 472 struct st_sample ilast; /* last sample in the input stream */ 473 }; 474 475 /* 476 * Prepare processing. 477 */ 478 void *st_rate_start (int inrate, int outrate) 479 { 480 struct rate *rate = g_new0(struct rate, 1); 481 482 rate->opos = 0; 483 484 /* increment */ 485 rate->opos_inc = ((uint64_t) inrate << 32) / outrate; 486 487 rate->ipos = 0; 488 rate->ilast.l = 0; 489 rate->ilast.r = 0; 490 return rate; 491 } 492 493 #define NAME st_rate_flow_mix 494 #define OP(a, b) a += b 495 #include "rate_template.h" 496 497 #define NAME st_rate_flow 498 #define OP(a, b) a = b 499 #include "rate_template.h" 500 501 void st_rate_stop (void *opaque) 502 { 503 g_free (opaque); 504 } 505 506 /** 507 * st_rate_frames_out() - returns the number of frames the resampling code 508 * generates from frames_in frames 509 * 510 * @opaque: pointer to struct rate 511 * @frames_in: number of frames 512 * 513 * When upsampling, there may be more than one correct result. In this case, 514 * the function returns the maximum number of output frames the resampling 515 * code can generate. 516 */ 517 uint32_t st_rate_frames_out(void *opaque, uint32_t frames_in) 518 { 519 struct rate *rate = opaque; 520 uint64_t opos_end, opos_delta; 521 uint32_t ipos_end; 522 uint32_t frames_out; 523 524 if (rate->opos_inc == 1ULL << 32) { 525 return frames_in; 526 } 527 528 /* no output frame without at least one input frame */ 529 if (!frames_in) { 530 return 0; 531 } 532 533 /* last frame read was at rate->ipos - 1 */ 534 ipos_end = rate->ipos - 1 + frames_in; 535 opos_end = (uint64_t)ipos_end << 32; 536 537 /* last frame written was at rate->opos - rate->opos_inc */ 538 if (opos_end + rate->opos_inc <= rate->opos) { 539 return 0; 540 } 541 opos_delta = opos_end - rate->opos + rate->opos_inc; 542 frames_out = opos_delta / rate->opos_inc; 543 544 return opos_delta % rate->opos_inc ? frames_out : frames_out - 1; 545 } 546 547 /** 548 * st_rate_frames_in() - returns the number of frames needed to 549 * get frames_out frames after resampling 550 * 551 * @opaque: pointer to struct rate 552 * @frames_out: number of frames 553 * 554 * When downsampling, there may be more than one correct result. In this 555 * case, the function returns the maximum number of input frames needed. 556 */ 557 uint32_t st_rate_frames_in(void *opaque, uint32_t frames_out) 558 { 559 struct rate *rate = opaque; 560 uint64_t opos_start, opos_end; 561 uint32_t ipos_start, ipos_end; 562 563 if (rate->opos_inc == 1ULL << 32) { 564 return frames_out; 565 } 566 567 if (frames_out) { 568 opos_start = rate->opos; 569 ipos_start = rate->ipos; 570 } else { 571 uint64_t offset; 572 573 /* add offset = ceil(opos_inc) to opos and ipos to avoid an underflow */ 574 offset = (rate->opos_inc + (1ULL << 32) - 1) & ~((1ULL << 32) - 1); 575 opos_start = rate->opos + offset; 576 ipos_start = rate->ipos + (offset >> 32); 577 } 578 /* last frame written was at opos_start - rate->opos_inc */ 579 opos_end = opos_start - rate->opos_inc + rate->opos_inc * frames_out; 580 ipos_end = (opos_end >> 32) + 1; 581 582 /* last frame read was at ipos_start - 1 */ 583 return ipos_end + 1 > ipos_start ? ipos_end + 1 - ipos_start : 0; 584 } 585 586 void mixeng_clear (struct st_sample *buf, int len) 587 { 588 memset (buf, 0, len * sizeof (struct st_sample)); 589 } 590 591 void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol) 592 { 593 if (vol->mute) { 594 mixeng_clear (buf, len); 595 return; 596 } 597 598 while (len--) { 599 #ifdef FLOAT_MIXENG 600 buf->l = buf->l * vol->l; 601 buf->r = buf->r * vol->r; 602 #else 603 buf->l = (buf->l * vol->l) >> 32; 604 buf->r = (buf->r * vol->r) >> 32; 605 #endif 606 buf += 1; 607 } 608 } 609