src/audio/SDL_audiotypecvt.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Nov 2016 00:09:02 -0800
changeset 10608 8661c956c368
parent 10575 bb99dede0675
child 10708 f6cd81aab88e
permissions -rw-r--r--
Fixed bug 3488 - Random crashes (because Memory overlap in audio converters detected by Valgrind)

Vitaly Novichkov

Okay, when I researched code and algorithm, I tried to replace condition "while(dst >= target)" with "while(dst > target)" and crashes are gone.
Seems on some moments it tries to write into the place before memory block begin, therefore phantom crashes appearing after some moments.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include "../SDL_internal.h"
    23 #include "SDL_audio.h"
    24 #include "SDL_audio_c.h"
    25 #include "SDL_assert.h"
    26 
    27 #define DIVBY127 0.0078740157480315f
    28 #define DIVBY32767 3.05185094759972e-05f
    29 #define DIVBY2147483647 4.6566128752458e-10f
    30 
    31 void SDLCALL
    32 SDL_Convert_S8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    33 {
    34     const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
    35     float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
    36     int i;
    37 
    38     LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32");
    39 
    40     for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
    41         *dst = (((float) ((Sint8) *src)) * DIVBY127);
    42     }
    43 
    44     cvt->len_cvt *= 4;
    45     if (cvt->filters[++cvt->filter_index]) {
    46         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
    47     }
    48 }
    49 
    50 void SDLCALL
    51 SDL_Convert_U8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    52 {
    53     const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
    54     float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
    55     int i;
    56 
    57     LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32");
    58 
    59     for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
    60         *dst = ((((float) *src) * DIVBY127) - 1.0f);
    61     }
    62 
    63     cvt->len_cvt *= 4;
    64     if (cvt->filters[++cvt->filter_index]) {
    65         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
    66     }
    67 }
    68 
    69 void SDLCALL
    70 SDL_Convert_S16_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    71 {
    72     const Sint16 *src = ((const Sint16 *) (cvt->buf + cvt->len_cvt)) - 1;
    73     float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
    74     int i;
    75 
    76     LOG_DEBUG_CONVERT("AUDIO_S16", "AUDIO_F32");
    77 
    78     for (i = cvt->len_cvt / sizeof (Sint16); i; --i, --src, --dst) {
    79         *dst = (((float) *src) * DIVBY32767);
    80     }
    81 
    82     cvt->len_cvt *= 2;
    83     if (cvt->filters[++cvt->filter_index]) {
    84         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
    85     }
    86 }
    87 
    88 void SDLCALL
    89 SDL_Convert_U16_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
    90 {
    91     const Uint16 *src = ((const Uint16 *) (cvt->buf + cvt->len_cvt)) - 1;
    92     float *dst = ((float *) (cvt->buf + cvt->len_cvt * 2)) - 1;
    93     int i;
    94 
    95     LOG_DEBUG_CONVERT("AUDIO_U16", "AUDIO_F32");
    96 
    97     for (i = cvt->len_cvt / sizeof (Uint16); i; --i, --src, --dst) {
    98         *dst = ((((float) *src) * DIVBY32767) - 1.0f);
    99     }
   100 
   101     cvt->len_cvt *= 2;
   102     if (cvt->filters[++cvt->filter_index]) {
   103         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   104     }
   105 }
   106 
   107 void SDLCALL
   108 SDL_Convert_S32_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   109 {
   110     const Uint32 *src = (const Uint32 *) cvt->buf;
   111     float *dst = (float *) cvt->buf;
   112     int i;
   113 
   114     LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32");
   115 
   116     for (i = cvt->len_cvt / sizeof (Sint32); i; --i, ++src, ++dst) {
   117         *dst = (((float) *src) * DIVBY2147483647);
   118     }
   119 
   120     if (cvt->filters[++cvt->filter_index]) {
   121         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   122     }
   123 }
   124 
   125 void SDLCALL
   126 SDL_Convert_F32_to_S8(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   127 {
   128     const float *src = (const float *) cvt->buf;
   129     Sint8 *dst = (Sint8 *) cvt->buf;
   130     int i;
   131 
   132     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S8");
   133 
   134     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   135         *dst = (Sint8) (*src * 127.0f);
   136     }
   137 
   138     cvt->len_cvt /= 4;
   139     if (cvt->filters[++cvt->filter_index]) {
   140         cvt->filters[cvt->filter_index](cvt, AUDIO_S8);
   141     }
   142 }
   143 
   144 void SDLCALL
   145 SDL_Convert_F32_to_U8(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   146 {
   147     const float *src = (const float *) cvt->buf;
   148     Uint8 *dst = (Uint8 *) cvt->buf;
   149     int i;
   150 
   151     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U8");
   152 
   153     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   154         *dst = (Uint8) ((*src + 1.0f) * 127.0f);
   155     }
   156 
   157     cvt->len_cvt /= 4;
   158     if (cvt->filters[++cvt->filter_index]) {
   159         cvt->filters[cvt->filter_index](cvt, AUDIO_U8);
   160     }
   161 }
   162 
   163 void SDLCALL
   164 SDL_Convert_F32_to_S16(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   165 {
   166     const float *src = (const float *) cvt->buf;
   167     Sint16 *dst = (Sint16 *) cvt->buf;
   168     int i;
   169 
   170     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S16");
   171 
   172     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   173         *dst = (Sint16) (*src * 32767.0f);
   174     }
   175 
   176     cvt->len_cvt /= 2;
   177     if (cvt->filters[++cvt->filter_index]) {
   178         cvt->filters[cvt->filter_index](cvt, AUDIO_S16SYS);
   179     }
   180 }
   181 
   182 void SDLCALL
   183 SDL_Convert_F32_to_U16(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   184 {
   185     const float *src = (const float *) cvt->buf;
   186     Uint16 *dst = (Uint16 *) cvt->buf;
   187     int i;
   188 
   189     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_U16");
   190 
   191     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   192         *dst = (Uint16) ((*src + 1.0f) * 32767.0f);
   193     }
   194 
   195     cvt->len_cvt /= 2;
   196     if (cvt->filters[++cvt->filter_index]) {
   197         cvt->filters[cvt->filter_index](cvt, AUDIO_U16SYS);
   198     }
   199 }
   200 
   201 void SDLCALL
   202 SDL_Convert_F32_to_S32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
   203 {
   204     const float *src = (const float *) cvt->buf;
   205     Sint32 *dst = (Sint32 *) cvt->buf;
   206     int i;
   207 
   208     LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32");
   209 
   210     for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
   211         *dst = (Sint32) (*src * 2147483647.0);
   212     }
   213 
   214     if (cvt->filters[++cvt->filter_index]) {
   215         cvt->filters[cvt->filter_index](cvt, AUDIO_S32SYS);
   216     }
   217 }
   218 
   219 void
   220 SDL_Upsample_Arbitrary(SDL_AudioCVT *cvt, const int channels)
   221 {
   222     const int srcsize = cvt->len_cvt - (64 * channels);
   223     const int dstsize = (int) (((double)(cvt->len_cvt/(channels*4))) * cvt->rate_incr) * (channels*4);
   224     register int eps = 0;
   225     float *dst = ((float *) (cvt->buf + dstsize)) - 8;
   226     const float *src = ((float *) (cvt->buf + cvt->len_cvt)) - 8;
   227     const float *target = ((const float *) cvt->buf);
   228     const size_t cpy = sizeof (float) * channels;
   229     float last_sample[8];
   230     float sample[8];
   231     int i;
   232 
   233 #if DEBUG_CONVERT
   234     fprintf(stderr, "Upsample arbitrary (x%f), %d channels.\n", cvt->rate_incr, channels);
   235 #endif
   236 
   237     SDL_assert(channels <= 8);
   238 
   239     SDL_memcpy(sample, src, cpy);
   240     SDL_memcpy(last_sample, src, cpy);
   241 
   242     while (dst > target) {
   243         SDL_memcpy(dst, sample, cpy);
   244         dst -= 8;
   245         eps += srcsize;
   246         if ((eps << 1) >= dstsize) {
   247             src -= 8;
   248             for (i = 0; i < channels; i++) {
   249                 sample[i] = (float) ((((double) src[i]) + ((double) last_sample[i])) * 0.5);
   250             }
   251             SDL_memcpy(last_sample, sample, cpy);
   252             eps -= dstsize;
   253         }
   254     }
   255 
   256     cvt->len_cvt = dstsize;
   257     if (cvt->filters[++cvt->filter_index]) {
   258         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   259     }
   260 }
   261 
   262 void
   263 SDL_Downsample_Arbitrary(SDL_AudioCVT *cvt, const int channels)
   264 {
   265     const int srcsize = cvt->len_cvt - (64 * channels);
   266     const int dstsize = (int) (((double)(cvt->len_cvt/(channels*4))) * cvt->rate_incr) * (channels*4);
   267     register int eps = 0;
   268     float *dst = (float *) cvt->buf;
   269     const float *src = (float *) cvt->buf;
   270     const float *target = (const float *) (cvt->buf + dstsize);
   271     const size_t cpy = sizeof (float) * channels;
   272     float last_sample[8];
   273     float sample[8];
   274     int i;
   275 
   276 #if DEBUG_CONVERT
   277     fprintf(stderr, "Downsample arbitrary (x%f), %d channels.\n", cvt->rate_incr, channels);
   278 #endif
   279 
   280     SDL_assert(channels <= 8);
   281 
   282     SDL_memcpy(sample, src, cpy);
   283     SDL_memcpy(last_sample, src, cpy);
   284 
   285     while (dst < target) {
   286         src += 8;
   287         eps += dstsize;
   288         if ((eps << 1) >= srcsize) {
   289             SDL_memcpy(dst, sample, cpy);
   290             dst += 8;
   291             for (i = 0; i < channels; i++) {
   292                 sample[i] = (float) ((((double) src[i]) + ((double) last_sample[i])) * 0.5);
   293             }
   294             SDL_memcpy(last_sample, sample, cpy);
   295             eps -= srcsize;
   296         }
   297     }
   298 
   299     cvt->len_cvt = dstsize;
   300     if (cvt->filters[++cvt->filter_index]) {
   301         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   302     }
   303 }
   304 
   305 void
   306 SDL_Upsample_x2(SDL_AudioCVT *cvt, const int channels)
   307 {
   308     const int dstsize = cvt->len_cvt * 2;
   309     float *dst = ((float *) (cvt->buf + dstsize)) - (channels * 2);
   310     const float *src = ((float *) (cvt->buf + cvt->len_cvt)) - channels;
   311     const float *target = ((const float *) cvt->buf);
   312     const size_t cpy = sizeof (float) * channels;
   313     float last_sample[8];
   314     int i;
   315 
   316 #if DEBUG_CONVERT
   317     fprintf(stderr, "Upsample (x2), %d channels.\n", channels);
   318 #endif
   319 
   320     SDL_assert(channels <= 8);
   321     SDL_memcpy(last_sample, src, cpy);
   322 
   323     while (dst > target) {
   324         for (i = 0; i < channels; i++) {
   325             dst[i] = (float) ((((double)src[i]) + ((double)last_sample[i])) * 0.5);
   326         }
   327         dst -= channels;
   328         SDL_memcpy(dst, src, cpy);
   329         SDL_memcpy(last_sample, src, cpy);
   330         src -= channels;
   331         dst -= channels;
   332     }
   333 
   334     cvt->len_cvt = dstsize;
   335     if (cvt->filters[++cvt->filter_index]) {
   336         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   337     }
   338 }
   339 
   340 void
   341 SDL_Upsample_x4(SDL_AudioCVT *cvt, const int channels)
   342 {
   343     const int dstsize = cvt->len_cvt * 4;
   344     float *dst = ((float *) (cvt->buf + dstsize)) - (channels * 4);
   345     const float *src = ((float *) (cvt->buf + cvt->len_cvt)) - channels;
   346     const float *target = ((const float *) cvt->buf);
   347     const size_t cpy = sizeof (float) * channels;
   348     float last_sample[8];
   349     int i;
   350 
   351 #if DEBUG_CONVERT
   352     fprintf(stderr, "Upsample (x4), %d channels.\n", channels);
   353 #endif
   354 
   355     SDL_assert(channels <= 8);
   356     SDL_memcpy(last_sample, src, cpy);
   357 
   358     while (dst > target) {
   359         for (i = 0; i < channels; i++) {
   360             dst[i] = (float) ((((double) src[i]) + (3.0 * ((double) last_sample[i]))) * 0.25);
   361         }
   362         dst -= channels;
   363         for (i = 0; i < channels; i++) {
   364             dst[i] = (float) ((((double) src[i]) + ((double) last_sample[i])) * 0.25);
   365         }
   366         dst -= channels;
   367         for (i = 0; i < channels; i++) {
   368             dst[i] = (float) (((3.0 * ((double) src[i])) + ((double) last_sample[i])) * 0.25);
   369         }
   370         dst -= channels;
   371         SDL_memcpy(dst, src, cpy);
   372         dst -= channels;
   373         SDL_memcpy(last_sample, src, cpy);
   374         src -= channels;
   375     }
   376 
   377     cvt->len_cvt = dstsize;
   378     if (cvt->filters[++cvt->filter_index]) {
   379         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   380     }
   381 }
   382 
   383 void
   384 SDL_Downsample_Multiple(SDL_AudioCVT *cvt, const int multiple, const int channels)
   385 {
   386     const int dstsize = cvt->len_cvt / multiple;
   387     float *dst = (float *) cvt->buf;
   388     const float *src = (float *) cvt->buf;
   389     const float *target = (const float *) (cvt->buf + dstsize);
   390     const size_t cpy = sizeof (float) * channels;
   391     float last_sample[8];
   392     int i;
   393 
   394 #if DEBUG_CONVERT
   395     fprintf(stderr, "Downsample (x%d), %d channels.\n", multiple, channels);
   396 #endif
   397 
   398     SDL_assert(channels <= 8);
   399     SDL_memcpy(last_sample, src, cpy);
   400 
   401     while (dst < target) {
   402         for (i = 0; i < channels; i++) {
   403             dst[i] = (float) ((((double)src[i]) + ((double)last_sample[i])) * 0.5);
   404         }
   405         dst += channels;
   406 
   407         SDL_memcpy(last_sample, src, cpy);
   408         src += (channels * multiple);
   409     }
   410 
   411     cvt->len_cvt = dstsize;
   412     if (cvt->filters[++cvt->filter_index]) {
   413         cvt->filters[cvt->filter_index](cvt, AUDIO_F32SYS);
   414     }
   415 }
   416 
   417 /* vi: set ts=4 sw=4 expandtab: */