audio: Converter now checks a strict list of channels and formats we support.
authorRyan C. Gordon <icculus@icculus.org>
Mon, 12 Jun 2017 21:35:24 -0400
changeset 1109762a0a6e9b48b
parent 11096 819384789a7a
child 11098 b5a4675cc883
audio: Converter now checks a strict list of channels and formats we support.
src/audio/SDL_audiocvt.c
     1.1 --- a/src/audio/SDL_audiocvt.c	Mon Jun 12 16:39:15 2017 -0700
     1.2 +++ b/src/audio/SDL_audiocvt.c	Mon Jun 12 21:35:24 2017 -0400
     1.3 @@ -700,6 +700,47 @@
     1.4      return 1;               /* added a converter. */
     1.5  }
     1.6  
     1.7 +static SDL_bool
     1.8 +SDL_SupportedAudioFormat(const SDL_AudioFormat fmt)
     1.9 +{
    1.10 +    switch (fmt) {
    1.11 +        case AUDIO_U8:
    1.12 +        case AUDIO_S8:
    1.13 +        case AUDIO_U16LSB:
    1.14 +        case AUDIO_S16LSB:
    1.15 +        case AUDIO_U16MSB:
    1.16 +        case AUDIO_S16MSB:
    1.17 +        case AUDIO_S32LSB:
    1.18 +        case AUDIO_S32MSB:
    1.19 +        case AUDIO_F32LSB:
    1.20 +        case AUDIO_F32MSB:
    1.21 +            return SDL_TRUE;  /* supported. */
    1.22 +
    1.23 +        default:
    1.24 +            break;
    1.25 +    }
    1.26 +
    1.27 +    return SDL_FALSE;  /* unsupported. */
    1.28 +}
    1.29 +
    1.30 +static SDL_bool
    1.31 +SDL_SupportedChannelCount(const int channels)
    1.32 +{
    1.33 +    switch (channels) {
    1.34 +        case 1:  /* mono */
    1.35 +        case 2:  /* stereo */
    1.36 +        case 4:  /* quad */
    1.37 +        case 6:  /* 5.1 */
    1.38 +            return SDL_TRUE;  /* supported. */
    1.39 +
    1.40 +        case 8:  /* !!! FIXME: 7.1 */
    1.41 +        default:
    1.42 +            break;
    1.43 +    }
    1.44 +
    1.45 +    return SDL_FALSE;  /* unsupported. */
    1.46 +}
    1.47 +
    1.48  
    1.49  /* Creates a set of audio filters to convert from one format to another.
    1.50     Returns -1 if the format conversion is not supported, 0 if there's
    1.51 @@ -719,21 +760,20 @@
    1.52      /* Make sure we zero out the audio conversion before error checking */
    1.53      SDL_zerop(cvt);
    1.54  
    1.55 -    /* there are no unsigned types over 16 bits, so catch this up front. */
    1.56 -    if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
    1.57 +    if (!SDL_SupportedAudioFormat(src_fmt)) {
    1.58          return SDL_SetError("Invalid source format");
    1.59 -    }
    1.60 -    if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
    1.61 +    } else if (!SDL_SupportedAudioFormat(dst_fmt)) {
    1.62          return SDL_SetError("Invalid destination format");
    1.63 +    } else if (!SDL_SupportedChannelCount(src_channels)) {
    1.64 +        return SDL_SetError("Invalid source channels");
    1.65 +    } else if (!SDL_SupportedChannelCount(dst_channels)) {
    1.66 +        return SDL_SetError("Invalid destination channels");
    1.67 +    } else if (src_rate == 0) {
    1.68 +        return SDL_SetError("Source rate is zero");
    1.69 +    } else if (dst_rate == 0) {
    1.70 +        return SDL_SetError("Destination rate is zero");
    1.71      }
    1.72  
    1.73 -    /* prevent possible divisions by zero, etc. */
    1.74 -    if ((src_channels == 0) || (dst_channels == 0)) {
    1.75 -        return SDL_SetError("Source or destination channels is zero");
    1.76 -    }
    1.77 -    if ((src_rate == 0) || (dst_rate == 0)) {
    1.78 -        return SDL_SetError("Source or destination rate is zero");
    1.79 -    }
    1.80  #if DEBUG_CONVERT
    1.81      printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
    1.82             src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);