21 #include "../SDL_internal.h"
34 #include "../SDL_dataqueue.h"
37 #define DEBUG_AUDIOSTREAM 0
40 #define HAVE_SSE3_INTRINSICS 1
43 #if HAVE_SSE3_INTRINSICS
48 float *
dst = (
float *) cvt->
buf;
57 if ((((
size_t)
dst) & 15) == 0) {
59 const __m128 divby2 = _mm_set1_ps(0.5
f);
61 _mm_store_ps(
dst, _mm_mul_ps(_mm_hadd_ps(_mm_load_ps(
src), _mm_load_ps(
src+4)), divby2));
83 float *
dst = (
float *) cvt->
buf;
105 float *
dst = (
float *) cvt->
buf;
114 const float front_center_distributed =
src[2] * 0.5f;
115 dst[0] = (
src[0] + front_center_distributed +
src[4]) / 2.5
f;
116 dst[1] = (
src[1] + front_center_distributed +
src[5]) / 2.5
f;
130 float *
dst = (
float *) cvt->
buf;
153 float *
dst = (
float *) cvt->
buf;
161 const float surround_left_distributed =
src[6] * 0.5f;
162 const float surround_right_distributed =
src[7] * 0.5f;
163 dst[0] = (
src[0] + surround_left_distributed) / 1.5
f;
164 dst[1] = (
src[1] + surround_right_distributed) / 1.5
f;
167 dst[4] = (
src[4] + surround_left_distributed) / 1.5
f;
168 dst[5] = (
src[5] + surround_right_distributed) / 1.5
f;
183 float *
dst = (
float *) cvt->
buf;
193 const float front_center_distributed =
src[2] * 0.5f;
194 dst[0] = (
src[0] + front_center_distributed) / 1.5
f;
195 dst[1] = (
src[1] + front_center_distributed) / 1.5
f;
212 const float *
src = (
const float *) (cvt->
buf + cvt->
len_cvt);
219 for (
i = cvt->
len_cvt / sizeof (
float);
i; --
i) {
238 const float *
src = (
const float *) (cvt->
buf + cvt->
len_cvt);
244 for (
i = cvt->
len_cvt / (
sizeof(
float) * 2);
i; --
i) {
249 ce = (lf + rf) * 0.5
f;
251 dst[0] = lf + (lf - ce);
252 dst[1] = rf + (rf - ce);
271 float lf, rf, lb, rb, ce;
272 const float *
src = (
const float *) (cvt->
buf + cvt->
len_cvt);
279 for (
i = cvt->
len_cvt / (
sizeof(
float) * 4);
i; --
i) {
286 ce = (lf + rf) * 0.5
f;
288 dst[0] = lf + (lf - ce);
289 dst[1] = rf + (rf - ce);
307 const float *
src = (
const float *) (cvt->
buf + cvt->
len_cvt);
315 for (
i = cvt->
len_cvt / (
sizeof(
float) * 2);
i; --
i) {
337 float lf, rf, lb, rb, ls, rs;
339 const float *
src = (
const float *) (cvt->
buf + cvt->
len_cvt);
346 for (
i = cvt->
len_cvt / (
sizeof(
float) * 6);
i; --
i) {
353 ls = (lf + lb) * 0.5
f;
354 rs = (rf + rb) * 0.5
f;
380 #define RESAMPLER_ZERO_CROSSINGS 5
381 #define RESAMPLER_BITS_PER_SAMPLE 16
382 #define RESAMPLER_SAMPLES_PER_ZERO_CROSSING (1 << ((RESAMPLER_BITS_PER_SAMPLE / 2) + 1))
383 #define RESAMPLER_FILTER_SIZE ((RESAMPLER_SAMPLES_PER_ZERO_CROSSING * RESAMPLER_ZERO_CROSSINGS) + 1)
389 const double xdiv2 =
x / 2.0;
396 if (diff < 1.0
e-21
f) {
411 const int lenm1 = tablelen - 1;
412 const int lenm1div2 = lenm1 / 2;
416 for (
i = 1;
i < tablelen;
i++) {
418 table[tablelen -
i] = (float) kaiser;
421 for (
i = 1;
i < tablelen;
i++) {
440 const double dB = 80.0;
441 const double beta = 0.1102 * (dB - 8.7);
475 if (inrate == outrate) {
477 }
else if (inrate > outrate) {
486 const float *lpadding,
const float *rpadding,
487 const float *inbuf,
const int inbuflen,
488 float *outbuf,
const int outbuflen)
490 const double finrate = (double) inrate;
491 const double outtimeincr = 1.0 / ((float) outrate);
492 const double ratio = ((float) outrate) / ((float) inrate);
494 const int framelen = chans * (int)
sizeof (
float);
495 const int inframes = inbuflen / framelen;
496 const int wantedoutframes = (int) ((inbuflen / framelen) * ratio);
497 const int maxoutframes = outbuflen / framelen;
498 const int outframes =
SDL_min(wantedoutframes, maxoutframes);
500 double outtime = 0.0;
503 for (
i = 0;
i < outframes;
i++) {
504 const int srcindex = (int) (outtime * inrate);
505 const double intime = ((double) srcindex) / finrate;
506 const double innexttime = ((double) (srcindex + 1)) / finrate;
507 const double interpolation1 = 1.0 - ((innexttime - outtime) / (innexttime - intime));
509 const double interpolation2 = 1.0 - interpolation1;
512 for (chan = 0; chan < chans; chan++) {
513 float outsample = 0.0f;
518 const int srcframe = srcindex -
j;
520 const float insample = (srcframe < 0) ? lpadding[((paddinglen + srcframe) * chans) + chan] : inbuf[(srcframe * chans) + chan];
525 const int srcframe = srcindex + 1 +
j;
527 const float insample = (srcframe >= inframes) ? rpadding[((srcframe - inframes) * chans) + chan] : inbuf[(srcframe * chans) + chan];
530 *(
dst++) = outsample;
533 outtime += outtimeincr;
536 return outframes * chans *
sizeof (float);
547 return SDL_SetError(
"No buffer allocated for conversion");
566 printf(
"Converting byte order\n");
570 #define CASESWAP(b) \
572 Uint##b *ptr = (Uint##b *) cvt->buf; \
574 for (i = cvt->len_cvt / sizeof (*ptr); i; --i, ++ptr) { \
575 *ptr = SDL_Swap##b(*ptr); \
586 default:
SDL_assert(!
"unhandled byteswap datatype!");
break;
628 const Uint16 dst_bitsize = 32;
637 default:
SDL_assert(!
"Unexpected audio format!");
break;
641 return SDL_SetError(
"No conversion from source format to float available");
647 if (src_bitsize < dst_bitsize) {
648 const int mult = (dst_bitsize / src_bitsize);
651 }
else if (src_bitsize > dst_bitsize) {
652 cvt->
len_ratio /= (src_bitsize / dst_bitsize);
668 const Uint16 src_bitsize = 32;
676 default:
SDL_assert(!
"Unexpected audio format!");
break;
680 return SDL_SetError(
"No conversion from float to destination format available");
686 if (src_bitsize < dst_bitsize) {
687 const int mult = (dst_bitsize / src_bitsize);
690 }
else if (src_bitsize > dst_bitsize) {
691 cvt->
len_ratio /= (src_bitsize / dst_bitsize);
714 const float *
src = (
const float *) cvt->
buf;
715 const int srclen = cvt->
len_cvt;
719 float *
dst = (
float *) (cvt->
buf + srclen);
720 const int dstlen = (cvt->
len * cvt->
len_mult) - srclen;
727 padding = (
float *)
SDL_calloc(paddingsamples ? paddingsamples : 1,
sizeof (
float));
748 #define RESAMPLER_FUNCS(chans) \
749 static void SDLCALL \
750 SDL_ResampleCVT_c##chans(SDL_AudioCVT *cvt, SDL_AudioFormat format) { \
751 SDL_ResampleCVT(cvt, chans, format); \
758 #undef RESAMPLER_FUNCS
763 switch (dst_channels) {
764 case 1:
return SDL_ResampleCVT_c1;
765 case 2:
return SDL_ResampleCVT_c2;
766 case 4:
return SDL_ResampleCVT_c4;
767 case 6:
return SDL_ResampleCVT_c6;
768 case 8:
return SDL_ResampleCVT_c8;
777 const int src_rate,
const int dst_rate)
781 if (src_rate == dst_rate) {
787 return SDL_SetError(
"No conversion available for these rates");
808 if (src_rate < dst_rate) {
809 const double mult = ((double) dst_rate) / ((double) src_rate);
813 cvt->
len_ratio /= ((double) src_rate) / ((double) dst_rate);
892 }
else if (src_rate == 0) {
894 }
else if (dst_rate == 0) {
899 printf(
"Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
900 src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
911 cvt->
rate_incr = ((double) dst_rate) / ((double) src_rate);
931 if (src_rate == dst_rate && src_channels == dst_channels) {
932 if (src_fmt == dst_fmt) {
952 if (src_channels < dst_channels) {
955 if ((src_channels == 1) && (dst_channels > 1)) {
964 if ((src_channels == 2) && (dst_channels >= 6)) {
973 if ((src_channels == 4) && (dst_channels >= 6)) {
982 if ((src_channels == 6) && (dst_channels == 8)) {
993 if ((src_channels == 2) && (dst_channels == 4)) {
1001 }
else if (src_channels > dst_channels) {
1005 if ((src_channels == 8) && (dst_channels <= 6)) {
1013 if ((src_channels == 6) && (dst_channels <= 2)) {
1021 if ((src_channels == 6) && (dst_channels == 4)) {
1029 if ((src_channels == 4) && (dst_channels <= 2)) {
1037 if ((src_channels == 2) && (dst_channels == 1)) {
1040 #if HAVE_SSE3_INTRINSICS
1042 filter = SDL_ConvertStereoToMono_SSE3;
1059 if (src_channels != dst_channels) {
1119 if (
stream->work_buffer_len >= newlen) {
1120 ptr =
stream->work_buffer_base;
1128 stream->work_buffer_base = ptr;
1129 stream->work_buffer_len = newlen;
1136 #ifdef HAVE_LIBSAMPLERATE_H
1138 SDL_ResampleAudioStream_SRC(SDL_AudioStream *
stream,
const void *_inbuf,
const int inbuflen,
void *_outbuf,
const int outbuflen)
1140 const float *inbuf = (
const float *) _inbuf;
1141 float *outbuf = (
float *) _outbuf;
1142 const int framelen =
sizeof(float) *
stream->pre_resample_channels;
1143 SRC_STATE *
state = (SRC_STATE *)
stream->resampler_state;
1147 SDL_assert(inbuf != ((
const float *) outbuf));
1149 data.data_in = (
float *)inbuf;
1150 data.input_frames = inbuflen / framelen;
1151 data.input_frames_used = 0;
1153 data.data_out = outbuf;
1154 data.output_frames = outbuflen / framelen;
1156 data.end_of_input = 0;
1168 return data.output_frames_gen * (
sizeof(float) *
stream->pre_resample_channels);
1172 SDL_ResetAudioStreamResampler_SRC(SDL_AudioStream *
stream)
1174 SRC_src_reset((SRC_STATE *)
stream->resampler_state);
1178 SDL_CleanupAudioStreamResampler_SRC(SDL_AudioStream *
stream)
1180 SRC_STATE *
state = (SRC_STATE *)
stream->resampler_state;
1182 SRC_src_delete(
state);
1192 SetupLibSampleRateResampling(SDL_AudioStream *
stream)
1197 if (SRC_available) {
1205 SDL_CleanupAudioStreamResampler_SRC(
stream);
1210 stream->resampler_func = SDL_ResampleAudioStream_SRC;
1211 stream->reset_resampler_func = SDL_ResetAudioStreamResampler_SRC;
1212 stream->cleanup_resampler_func = SDL_CleanupAudioStreamResampler_SRC;
1222 const Uint8 *inbufend = ((
const Uint8 *) _inbuf) + inbuflen;
1223 const float *inbuf = (
const float *) _inbuf;
1224 float *outbuf = (
float *) _outbuf;
1225 const int chans = (int)
stream->pre_resample_channels;
1226 const int inrate =
stream->src_rate;
1227 const int outrate =
stream->dst_rate;
1228 const int paddingsamples =
stream->resampler_padding_samples;
1229 const int paddingbytes = paddingsamples * sizeof (
float);
1230 float *lpadding = (
float *)
stream->resampler_state;
1231 const float *rpadding = (
const float *) inbufend;
1232 const int cpy =
SDL_min(inbuflen, paddingbytes);
1235 SDL_assert(inbuf != ((
const float *) outbuf));
1240 SDL_memcpy((lpadding + paddingsamples) - (cpy /
sizeof (
float)), inbufend - cpy, cpy);
1248 const int len =
stream->resampler_padding_samples;
1260 const Uint8 src_channels,
1263 const Uint8 dst_channels,
1266 const int packetlen = 4096;
1267 Uint8 pre_resample_channels;
1279 pre_resample_channels =
SDL_min(src_channels, dst_channels);
1283 retval->src_format = src_format;
1284 retval->src_channels = src_channels;
1285 retval->src_rate = src_rate;
1287 retval->dst_format = dst_format;
1288 retval->dst_channels = dst_channels;
1289 retval->dst_rate = dst_rate;
1290 retval->pre_resample_channels = pre_resample_channels;
1291 retval->packetlen = packetlen;
1292 retval->rate_incr = ((double) dst_rate) / ((double) src_rate);
1294 retval->resampler_padding = (
float *)
SDL_calloc(
retval->resampler_padding_samples ?
retval->resampler_padding_samples : 1, sizeof (
float));
1302 retval->staging_buffer_size = ((
retval->resampler_padding_samples /
retval->pre_resample_channels) *
retval->src_sample_frame_size);
1303 if (
retval->staging_buffer_size > 0) {
1313 if (src_rate == dst_rate) {
1315 if (
SDL_BuildAudioCVT(&
retval->cvt_after_resampling, src_format, src_channels, dst_rate, dst_format, dst_channels, dst_rate) < 0) {
1327 #ifdef HAVE_LIBSAMPLERATE_H
1328 SetupLibSampleRateResampling(
retval);
1331 if (!
retval->resampler_func) {
1333 if (!
retval->resampler_state) {
1374 int resamplebuflen = 0;
1375 int neededpaddingbytes;
1387 neededpaddingbytes =
stream->resampler_padding_samples *
sizeof (float);
1388 paddingbytes =
stream->first_run ? 0 : neededpaddingbytes;
1392 workbuflen = buflen;
1393 if (
stream->cvt_before_resampling.needed) {
1394 workbuflen *=
stream->cvt_before_resampling.len_mult;
1399 const int framesize =
stream->pre_resample_channels *
sizeof (float);
1400 const int frames = workbuflen / framesize;
1401 resamplebuflen = ((int)
SDL_ceil(frames *
stream->rate_incr)) * framesize;
1402 #if DEBUG_AUDIOSTREAM
1403 printf(
"AUDIOSTREAM: will resample %d bytes to %d (ratio=%.6f)\n", workbuflen, resamplebuflen,
stream->rate_incr);
1405 workbuflen += resamplebuflen;
1408 if (
stream->cvt_after_resampling.needed) {
1410 workbuflen *=
stream->cvt_after_resampling.len_mult;
1413 workbuflen += neededpaddingbytes;
1415 #if DEBUG_AUDIOSTREAM
1416 printf(
"AUDIOSTREAM: Putting %d bytes of preconverted audio, need %d byte work buffer\n", buflen, workbuflen);
1424 resamplebuf = workbuf;
1428 if (
stream->cvt_before_resampling.needed) {
1429 stream->cvt_before_resampling.buf = workbuf + paddingbytes;
1430 stream->cvt_before_resampling.len = buflen;
1434 buflen =
stream->cvt_before_resampling.len_cvt;
1436 #if DEBUG_AUDIOSTREAM
1437 printf(
"AUDIOSTREAM: After initial conversion we have %d bytes\n", buflen);
1449 buflen += paddingbytes;
1453 SDL_memcpy(
stream->resampler_padding, workbuf + (buflen - neededpaddingbytes), neededpaddingbytes);
1455 resamplebuf = workbuf + buflen;
1457 if (buflen > neededpaddingbytes) {
1458 buflen =
stream->resampler_func(
stream, workbuf, buflen - neededpaddingbytes, resamplebuf, resamplebuflen);
1463 #if DEBUG_AUDIOSTREAM
1464 printf(
"AUDIOSTREAM: After resampling we have %d bytes\n", buflen);
1468 if (
stream->cvt_after_resampling.needed && (buflen > 0)) {
1469 stream->cvt_after_resampling.buf = resamplebuf;
1470 stream->cvt_after_resampling.len = buflen;
1474 buflen =
stream->cvt_after_resampling.len_cvt;
1476 #if DEBUG_AUDIOSTREAM
1477 printf(
"AUDIOSTREAM: After final conversion we have %d bytes\n", buflen);
1481 #if DEBUG_AUDIOSTREAM
1482 printf(
"AUDIOSTREAM: Final output is %d bytes\n", buflen);
1486 const int maxbytes = *maxputbytes;
1487 if (buflen > maxbytes)
1489 *maxputbytes -= buflen;
1507 #if DEBUG_AUDIOSTREAM
1508 printf(
"AUDIOSTREAM: wants to put %d preconverted bytes\n", buflen);
1515 }
else if (
len == 0) {
1517 }
else if ((
len %
stream->src_sample_frame_size) != 0) {
1518 return SDL_SetError(
"Can't add partial sample frames");
1521 if (!
stream->cvt_before_resampling.needed &&
1523 !
stream->cvt_after_resampling.needed) {
1524 #if DEBUG_AUDIOSTREAM
1525 printf(
"AUDIOSTREAM: no conversion needed at all, queueing %d bytes.\n",
len);
1536 if (!
stream->staging_buffer_filled &&
len >=
stream->staging_buffer_size) {
1541 if ((
stream->staging_buffer_filled +
len) <
stream->staging_buffer_size) {
1548 amount = (
stream->staging_buffer_size -
stream->staging_buffer_filled);
1551 stream->staging_buffer_filled = 0;
1567 #if DEBUG_AUDIOSTREAM
1568 printf(
"AUDIOSTREAM: flushing! staging_buffer_filled=%d bytes\n",
stream->staging_buffer_filled);
1574 if (
stream->staging_buffer_filled > 0) {
1579 const int filled =
stream->staging_buffer_filled;
1580 int actual_input_frames = filled /
stream->src_sample_frame_size;
1582 actual_input_frames +=
stream->resampler_padding_samples /
stream->pre_resample_channels;
1584 if (actual_input_frames > 0) {
1586 int flush_remaining = ((int)
SDL_ceil(actual_input_frames *
stream->rate_incr)) *
stream->dst_sample_frame_size;
1589 printf(
"AUDIOSTREAM: flushing with padding to get max %d bytes!\n", flush_remaining);
1607 stream->staging_buffer_filled = 0;
1617 #if DEBUG_AUDIOSTREAM
1618 printf(
"AUDIOSTREAM: want to get %d converted bytes\n",
len);
1625 }
else if (
len <= 0) {
1627 }
else if ((
len %
stream->dst_sample_frame_size) != 0) {
1628 return SDL_SetError(
"Can't request partial sample frames");
1648 if (
stream->reset_resampler_func) {
1652 stream->staging_buffer_filled = 0;
1661 if (
stream->cleanup_resampler_func) {