Don't do any audio conversion if none is necessary
authorSam Lantinga <slouken@libsdl.org>
Thu, 05 Jan 2017 23:53:46 -0800
changeset 10767b6046389b839
parent 10766 6cfccf993c5d
child 10768 73dfaab7e779
Don't do any audio conversion if none is necessary
src/audio/SDL_audiocvt.c
     1.1 --- a/src/audio/SDL_audiocvt.c	Thu Jan 05 23:26:13 2017 -0800
     1.2 +++ b/src/audio/SDL_audiocvt.c	Thu Jan 05 23:53:46 2017 -0800
     1.3 @@ -467,6 +467,9 @@
     1.4          return SDL_InvalidParamError("cvt");
     1.5      }
     1.6  
     1.7 +    /* Make sure we zero out the audio conversion before error checking */
     1.8 +    SDL_zerop(cvt);
     1.9 +
    1.10      /* there are no unsigned types over 16 bits, so catch this up front. */
    1.11      if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
    1.12          return SDL_SetError("Invalid source format");
    1.13 @@ -488,7 +491,6 @@
    1.14  #endif
    1.15  
    1.16      /* Start off with no conversion necessary */
    1.17 -    SDL_zerop(cvt);
    1.18      cvt->src_format = src_fmt;
    1.19      cvt->dst_format = dst_fmt;
    1.20      cvt->needed = 0;
    1.21 @@ -512,17 +514,22 @@
    1.22         (script-generated) custom converters for every data type and
    1.23         it was a bloat on SDL compile times and final library size. */
    1.24  
    1.25 -    /* see if we can skip float conversion entirely (just a byteswap needed). */
    1.26 -    if ((src_rate == dst_rate) && (src_channels == dst_channels) &&
    1.27 -        ((src_fmt != dst_fmt) &&
    1.28 -         ((src_fmt & ~SDL_AUDIO_MASK_ENDIAN) == (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN)))) {
    1.29 -        cvt->filters[cvt->filter_index++] = SDL_Convert_Byteswap;
    1.30 -        cvt->needed = 1;
    1.31 -        return 1;
    1.32 +    /* see if we can skip float conversion entirely. */
    1.33 +    if (src_rate == dst_rate && src_channels == dst_channels) {
    1.34 +        if (src_fmt == dst_fmt) {
    1.35 +            return 0;
    1.36 +        }
    1.37 +
    1.38 +        /* just a byteswap needed? */
    1.39 +        if ((src_fmt & ~SDL_AUDIO_MASK_ENDIAN) == (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN)) {
    1.40 +            cvt->filters[cvt->filter_index++] = SDL_Convert_Byteswap;
    1.41 +            cvt->needed = 1;
    1.42 +            return 1;
    1.43 +        }
    1.44      }
    1.45  
    1.46      /* Convert data types, if necessary. Updates (cvt). */
    1.47 -    if (SDL_BuildAudioTypeCVTToFloat(cvt, src_fmt) == -1) {
    1.48 +    if (SDL_BuildAudioTypeCVTToFloat(cvt, src_fmt) < 0) {
    1.49          return -1;              /* shouldn't happen, but just in case... */
    1.50      }
    1.51  
    1.52 @@ -578,12 +585,12 @@
    1.53      }
    1.54  
    1.55      /* Do rate conversion, if necessary. Updates (cvt). */
    1.56 -    if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) == -1) {
    1.57 +    if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) < 0) {
    1.58          return -1;              /* shouldn't happen, but just in case... */
    1.59      }
    1.60  
    1.61      /* Move to final data type. */
    1.62 -    if (SDL_BuildAudioTypeCVTFromFloat(cvt, dst_fmt) == -1) {
    1.63 +    if (SDL_BuildAudioTypeCVTFromFloat(cvt, dst_fmt) < 0) {
    1.64          return -1;              /* shouldn't happen, but just in case... */
    1.65      }
    1.66