src/audio/SDL_audiocvt.c
changeset 10843 31c9dede7b9c
parent 10842 30b37eaf3b3c
child 10844 936e8095db14
     1.1 --- a/src/audio/SDL_audiocvt.c	Tue Jan 24 00:08:24 2017 -0500
     1.2 +++ b/src/audio/SDL_audiocvt.c	Tue Jan 24 00:17:40 2017 -0500
     1.3 @@ -869,8 +869,6 @@
     1.4      SDL_DataQueue *queue;
     1.5      Uint8 *work_buffer;
     1.6      int work_buffer_len;
     1.7 -    Uint8 *resample_buffer;
     1.8 -    int resample_buffer_len;
     1.9      int src_sample_frame_size;
    1.10      SDL_AudioFormat src_format;
    1.11      Uint8 src_channels;
    1.12 @@ -1145,6 +1143,7 @@
    1.13  SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, const Uint32 _buflen)
    1.14  {
    1.15      int buflen = (int) _buflen;
    1.16 +    SDL_bool copied = SDL_FALSE;
    1.17  
    1.18      if (!stream) {
    1.19          return SDL_InvalidParamError("stream");
    1.20 @@ -1162,6 +1161,7 @@
    1.21          if (workbuf == NULL) {
    1.22              return -1;  /* probably out of memory. */
    1.23          }
    1.24 +        copied = SDL_TRUE;
    1.25          SDL_memcpy(workbuf, buf, buflen);
    1.26          stream->cvt_before_resampling.buf = workbuf;
    1.27          stream->cvt_before_resampling.len = buflen;
    1.28 @@ -1174,31 +1174,28 @@
    1.29  
    1.30      if (stream->dst_rate != stream->src_rate) {
    1.31          const int workbuflen = buflen * ((int) SDL_ceil(stream->rate_incr));
    1.32 -        void *workbuf = EnsureBufferSize(&stream->resample_buffer, &stream->resample_buffer_len, workbuflen);
    1.33 +        void *workbuf = EnsureBufferSize(&stream->work_buffer, &stream->work_buffer_len, workbuflen);
    1.34          if (workbuf == NULL) {
    1.35              return -1;  /* probably out of memory. */
    1.36          }
    1.37 -        buflen = stream->resampler_func(stream, buf, buflen, workbuf, workbuflen);
    1.38 +        if (!copied) {
    1.39 +            SDL_memcpy(workbuf, buf, buflen);
    1.40 +            copied = SDL_TRUE;
    1.41 +        }
    1.42 +        buflen = stream->resampler_func(stream, workbuf, buflen, workbuf, workbuflen);
    1.43          buf = workbuf;
    1.44      }
    1.45  
    1.46      if (stream->cvt_after_resampling.needed) {
    1.47          const int workbuflen = buflen * stream->cvt_after_resampling.len_mult;  /* will be "* 1" if not needed */
    1.48 -        Uint8 *workbuf;
    1.49 -
    1.50 -        if (buf == stream->resample_buffer) {
    1.51 -            workbuf = EnsureBufferSize(&stream->resample_buffer, &stream->resample_buffer_len, workbuflen);
    1.52 -        } else {
    1.53 -            const int inplace = (buf == stream->work_buffer);
    1.54 -            workbuf = EnsureBufferSize(&stream->work_buffer, &stream->work_buffer_len, workbuflen);
    1.55 -            if (workbuf && !inplace) {
    1.56 -                SDL_memcpy(workbuf, buf, buflen);
    1.57 -            }
    1.58 -        }
    1.59 -
    1.60 +        Uint8 *workbuf = EnsureBufferSize(&stream->work_buffer, &stream->work_buffer_len, workbuflen);
    1.61          if (workbuf == NULL) {
    1.62              return -1;  /* probably out of memory. */
    1.63          }
    1.64 +        if (!copied) {
    1.65 +            SDL_memcpy(workbuf, buf, buflen);
    1.66 +            copied = SDL_TRUE;
    1.67 +        }
    1.68  
    1.69          stream->cvt_after_resampling.buf = workbuf;
    1.70          stream->cvt_after_resampling.len = buflen;
    1.71 @@ -1260,7 +1257,6 @@
    1.72          }
    1.73          SDL_FreeDataQueue(stream->queue);
    1.74          SDL_free(stream->work_buffer);
    1.75 -        SDL_free(stream->resample_buffer);
    1.76          SDL_free(stream);
    1.77      }
    1.78  }