src/audio/SDL_audiocvt.c
changeset 11096 819384789a7a
parent 10926 97c829825e0e
child 11097 62a0a6e9b48b
     1.1 --- a/src/audio/SDL_audiocvt.c	Mon Jun 12 16:35:34 2017 -0700
     1.2 +++ b/src/audio/SDL_audiocvt.c	Mon Jun 12 16:39:15 2017 -0700
     1.3 @@ -514,6 +514,19 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +static int
     1.8 +SDL_AddAudioCVTFilter(SDL_AudioCVT *cvt, const SDL_AudioFilter filter)
     1.9 +{
    1.10 +    if (cvt->filter_index >= SDL_AUDIOCVT_MAX_FILTERS) {
    1.11 +        return SDL_SetError("Too many filters needed for conversion, exceeded maximum of %d", SDL_AUDIOCVT_MAX_FILTERS);
    1.12 +    }
    1.13 +    if (filter == NULL) {
    1.14 +        return SDL_SetError("Audio filter pointer is NULL");
    1.15 +    }
    1.16 +    cvt->filters[cvt->filter_index++] = filter;
    1.17 +    cvt->filters[cvt->filter_index] = NULL; /* Moving terminator */
    1.18 +    return 0;
    1.19 +}
    1.20  
    1.21  static int
    1.22  SDL_BuildAudioTypeCVTToFloat(SDL_AudioCVT *cvt, const SDL_AudioFormat src_fmt)
    1.23 @@ -521,7 +534,9 @@
    1.24      int retval = 0;  /* 0 == no conversion necessary. */
    1.25  
    1.26      if ((SDL_AUDIO_ISBIGENDIAN(src_fmt) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN)) {
    1.27 -        cvt->filters[cvt->filter_index++] = SDL_Convert_Byteswap;
    1.28 +        if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) {
    1.29 +            return -1;
    1.30 +        }
    1.31          retval = 1;  /* added a converter. */
    1.32      }
    1.33  
    1.34 @@ -543,7 +558,9 @@
    1.35              return SDL_SetError("No conversion available for these formats");
    1.36          }
    1.37  
    1.38 -        cvt->filters[cvt->filter_index++] = filter;
    1.39 +        if (SDL_AddAudioCVTFilter(cvt, filter) < 0) {
    1.40 +            return -1;
    1.41 +        }
    1.42          if (src_bitsize < dst_bitsize) {
    1.43              const int mult = (dst_bitsize / src_bitsize);
    1.44              cvt->len_mult *= mult;
    1.45 @@ -580,7 +597,9 @@
    1.46              return SDL_SetError("No conversion available for these formats");
    1.47          }
    1.48  
    1.49 -        cvt->filters[cvt->filter_index++] = filter;
    1.50 +        if (SDL_AddAudioCVTFilter(cvt, filter) < 0) {
    1.51 +            return -1;
    1.52 +        }
    1.53          if (src_bitsize < dst_bitsize) {
    1.54              const int mult = (dst_bitsize / src_bitsize);
    1.55              cvt->len_mult *= mult;
    1.56 @@ -592,7 +611,9 @@
    1.57      }
    1.58  
    1.59      if ((SDL_AUDIO_ISBIGENDIAN(dst_fmt) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN)) {
    1.60 -        cvt->filters[cvt->filter_index++] = SDL_Convert_Byteswap;
    1.61 +        if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) {
    1.62 +            return -1;
    1.63 +        }
    1.64          retval = 1;  /* added a converter. */
    1.65      }
    1.66  
    1.67 @@ -665,7 +686,9 @@
    1.68      }
    1.69  
    1.70      /* Update (cvt) with filter details... */
    1.71 -    cvt->filters[cvt->filter_index++] = filter;
    1.72 +    if (SDL_AddAudioCVTFilter(cvt, filter) < 0) {
    1.73 +        return -1;
    1.74 +    }
    1.75      if (src_rate < dst_rate) {
    1.76          const double mult = ((double) dst_rate) / ((double) src_rate);
    1.77          cvt->len_mult *= (int) SDL_ceil(mult);
    1.78 @@ -739,7 +762,9 @@
    1.79         format as well. */
    1.80      if ((src_channels == 2) && (dst_channels == 2) && (src_fmt == AUDIO_S16SYS) && (dst_fmt == AUDIO_S16SYS) && (src_rate != dst_rate)) {
    1.81          cvt->needed = 1;
    1.82 -        cvt->filters[cvt->filter_index++] = SDL_ResampleCVT_si16_c2;
    1.83 +        if (SDL_AddAudioCVTFilter(cvt, SDL_ResampleCVT_si16_c2) < 0) {
    1.84 +            return -1;
    1.85 +        }
    1.86          if (src_rate < dst_rate) {
    1.87              const double mult = ((double) dst_rate) / ((double) src_rate);
    1.88              cvt->len_mult *= (int) SDL_ceil(mult);
    1.89 @@ -772,7 +797,9 @@
    1.90  
    1.91          /* just a byteswap needed? */
    1.92          if ((src_fmt & ~SDL_AUDIO_MASK_ENDIAN) == (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN)) {
    1.93 -            cvt->filters[cvt->filter_index++] = SDL_Convert_Byteswap;
    1.94 +            if (SDL_AddAudioCVTFilter(cvt, SDL_Convert_Byteswap) < 0) {
    1.95 +                return -1;
    1.96 +            }
    1.97              cvt->needed = 1;
    1.98              return 1;
    1.99          }
   1.100 @@ -786,36 +813,48 @@
   1.101      /* Channel conversion */
   1.102      if (src_channels != dst_channels) {
   1.103          if ((src_channels == 1) && (dst_channels > 1)) {
   1.104 -            cvt->filters[cvt->filter_index++] = SDL_ConvertMonoToStereo;
   1.105 +            if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertMonoToStereo) < 0) {
   1.106 +                return -1;
   1.107 +            }
   1.108              cvt->len_mult *= 2;
   1.109              src_channels = 2;
   1.110              cvt->len_ratio *= 2;
   1.111          }
   1.112          if ((src_channels == 2) && (dst_channels == 6)) {
   1.113 -            cvt->filters[cvt->filter_index++] = SDL_ConvertStereoTo51;
   1.114 +            if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertStereoTo51) < 0) {
   1.115 +                return -1;
   1.116 +            }
   1.117              src_channels = 6;
   1.118              cvt->len_mult *= 3;
   1.119              cvt->len_ratio *= 3;
   1.120          }
   1.121          if ((src_channels == 2) && (dst_channels == 4)) {
   1.122 -            cvt->filters[cvt->filter_index++] = SDL_ConvertStereoToQuad;
   1.123 +            if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertStereoToQuad) < 0) {
   1.124 +                return -1;
   1.125 +            }
   1.126              src_channels = 4;
   1.127              cvt->len_mult *= 2;
   1.128              cvt->len_ratio *= 2;
   1.129          }
   1.130          while ((src_channels * 2) <= dst_channels) {
   1.131 -            cvt->filters[cvt->filter_index++] = SDL_ConvertMonoToStereo;
   1.132 +            if (SDL_AddAudioCVTFilter(cvt, SDL_ConvertMonoToStereo) < 0) {
   1.133 +                return -1;
   1.134 +            }
   1.135              cvt->len_mult *= 2;
   1.136              src_channels *= 2;
   1.137              cvt->len_ratio *= 2;
   1.138          }
   1.139          if ((src_channels == 6) && (dst_channels <= 2)) {
   1.140 -            cvt->filters[cvt->filter_index++] = SDL_Convert51ToStereo;
   1.141 +            if (SDL_AddAudioCVTFilter(cvt, SDL_Convert51ToStereo) < 0) {
   1.142 +                return -1;
   1.143 +            }
   1.144              src_channels = 2;
   1.145              cvt->len_ratio /= 3;
   1.146          }
   1.147          if ((src_channels == 6) && (dst_channels == 4)) {
   1.148 -            cvt->filters[cvt->filter_index++] = SDL_Convert51ToQuad;
   1.149 +            if (SDL_AddAudioCVTFilter(cvt, SDL_Convert51ToQuad) < 0) {
   1.150 +                return -1;
   1.151 +            }
   1.152              src_channels = 4;
   1.153              cvt->len_ratio /= 2;
   1.154          }
   1.155 @@ -837,7 +876,9 @@
   1.156                  filter = SDL_ConvertStereoToMono;
   1.157              }
   1.158  
   1.159 -            cvt->filters[cvt->filter_index++] = filter;
   1.160 +            if (SDL_AddAudioCVTFilter(cvt, filter) < 0) {
   1.161 +                return -1;
   1.162 +            }
   1.163  
   1.164              src_channels /= 2;
   1.165              cvt->len_ratio /= 2;