audio: Streams now resample in-place. Removed second allocated buffer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index 28e7156..6a24118 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -869,8 +869,6 @@ struct SDL_AudioStream
SDL_DataQueue *queue;
Uint8 *work_buffer;
int work_buffer_len;
- Uint8 *resample_buffer;
- int resample_buffer_len;
int src_sample_frame_size;
SDL_AudioFormat src_format;
Uint8 src_channels;
@@ -1145,6 +1143,7 @@ int
SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, const Uint32 _buflen)
{
int buflen = (int) _buflen;
+ SDL_bool copied = SDL_FALSE;
if (!stream) {
return SDL_InvalidParamError("stream");
@@ -1162,6 +1161,7 @@ SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, const Uint32 _bufle
if (workbuf == NULL) {
return -1; /* probably out of memory. */
}
+ copied = SDL_TRUE;
SDL_memcpy(workbuf, buf, buflen);
stream->cvt_before_resampling.buf = workbuf;
stream->cvt_before_resampling.len = buflen;
@@ -1174,31 +1174,28 @@ SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, const Uint32 _bufle
if (stream->dst_rate != stream->src_rate) {
const int workbuflen = buflen * ((int) SDL_ceil(stream->rate_incr));
- void *workbuf = EnsureBufferSize(&stream->resample_buffer, &stream->resample_buffer_len, workbuflen);
+ void *workbuf = EnsureBufferSize(&stream->work_buffer, &stream->work_buffer_len, workbuflen);
if (workbuf == NULL) {
return -1; /* probably out of memory. */
}
- buflen = stream->resampler_func(stream, buf, buflen, workbuf, workbuflen);
+ if (!copied) {
+ SDL_memcpy(workbuf, buf, buflen);
+ copied = SDL_TRUE;
+ }
+ buflen = stream->resampler_func(stream, workbuf, buflen, workbuf, workbuflen);
buf = workbuf;
}
if (stream->cvt_after_resampling.needed) {
const int workbuflen = buflen * stream->cvt_after_resampling.len_mult; /* will be "* 1" if not needed */
- Uint8 *workbuf;
-
- if (buf == stream->resample_buffer) {
- workbuf = EnsureBufferSize(&stream->resample_buffer, &stream->resample_buffer_len, workbuflen);
- } else {
- const int inplace = (buf == stream->work_buffer);
- workbuf = EnsureBufferSize(&stream->work_buffer, &stream->work_buffer_len, workbuflen);
- if (workbuf && !inplace) {
- SDL_memcpy(workbuf, buf, buflen);
- }
- }
-
+ Uint8 *workbuf = EnsureBufferSize(&stream->work_buffer, &stream->work_buffer_len, workbuflen);
if (workbuf == NULL) {
return -1; /* probably out of memory. */
}
+ if (!copied) {
+ SDL_memcpy(workbuf, buf, buflen);
+ copied = SDL_TRUE;
+ }
stream->cvt_after_resampling.buf = workbuf;
stream->cvt_after_resampling.len = buflen;
@@ -1260,7 +1257,6 @@ SDL_FreeAudioStream(SDL_AudioStream *stream)
}
SDL_FreeDataQueue(stream->queue);
SDL_free(stream->work_buffer);
- SDL_free(stream->resample_buffer);
SDL_free(stream);
}
}