src/audio/SDL_audiocvt.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 21 Jan 2013 09:16:27 -0800
changeset 6819 afef68703f54
parent 6281 e46d6f4b469e
child 6885 700f1b25f77f
permissions -rw-r--r--
Add additional input validation to SDL_BuildAudioCVT; add additional tests to automation (audio, rwops)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@2728
    22
slouken@0
    23
/* Functions for audio drivers to perform runtime conversion of audio format */
slouken@0
    24
slouken@0
    25
#include "SDL_audio.h"
icculus@1982
    26
#include "SDL_audio_c.h"
slouken@0
    27
icculus@6281
    28
#include "SDL_assert.h"
icculus@6281
    29
icculus@3021
    30
/* #define DEBUG_CONVERT */
slouken@2716
    31
slouken@0
    32
/* Effectively mix right and left channels into a single channel */
icculus@1982
    33
static void SDLCALL
icculus@1982
    34
SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
    35
{
slouken@1895
    36
    int i;
slouken@1895
    37
    Sint32 sample;
slouken@0
    38
slouken@0
    39
#ifdef DEBUG_CONVERT
slouken@1895
    40
    fprintf(stderr, "Converting to mono\n");
slouken@0
    41
#endif
slouken@1985
    42
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
    43
    case AUDIO_U8:
slouken@1895
    44
        {
slouken@1895
    45
            Uint8 *src, *dst;
slouken@0
    46
slouken@1895
    47
            src = cvt->buf;
slouken@1895
    48
            dst = cvt->buf;
slouken@1895
    49
            for (i = cvt->len_cvt / 2; i; --i) {
slouken@1895
    50
                sample = src[0] + src[1];
slouken@2042
    51
                *dst = (Uint8) (sample / 2);
slouken@1895
    52
                src += 2;
slouken@1895
    53
                dst += 1;
slouken@1895
    54
            }
slouken@1895
    55
        }
slouken@1895
    56
        break;
slouken@0
    57
slouken@1895
    58
    case AUDIO_S8:
slouken@1895
    59
        {
slouken@1895
    60
            Sint8 *src, *dst;
slouken@0
    61
slouken@1895
    62
            src = (Sint8 *) cvt->buf;
slouken@1895
    63
            dst = (Sint8 *) cvt->buf;
slouken@1895
    64
            for (i = cvt->len_cvt / 2; i; --i) {
slouken@1895
    65
                sample = src[0] + src[1];
slouken@2042
    66
                *dst = (Sint8) (sample / 2);
slouken@1895
    67
                src += 2;
slouken@1895
    68
                dst += 1;
slouken@1895
    69
            }
slouken@1895
    70
        }
slouken@1895
    71
        break;
slouken@0
    72
slouken@1895
    73
    case AUDIO_U16:
slouken@1895
    74
        {
slouken@1895
    75
            Uint8 *src, *dst;
slouken@0
    76
slouken@1895
    77
            src = cvt->buf;
slouken@1895
    78
            dst = cvt->buf;
icculus@1982
    79
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
    80
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
    81
                    sample = (Uint16) ((src[0] << 8) | src[1]) +
slouken@1895
    82
                        (Uint16) ((src[2] << 8) | src[3]);
slouken@2042
    83
                    sample /= 2;
slouken@2042
    84
                    dst[1] = (sample & 0xFF);
slouken@2042
    85
                    sample >>= 8;
slouken@2042
    86
                    dst[0] = (sample & 0xFF);
slouken@1895
    87
                    src += 4;
slouken@1895
    88
                    dst += 2;
slouken@1895
    89
                }
slouken@1895
    90
            } else {
slouken@1895
    91
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
    92
                    sample = (Uint16) ((src[1] << 8) | src[0]) +
slouken@1895
    93
                        (Uint16) ((src[3] << 8) | src[2]);
slouken@2042
    94
                    sample /= 2;
slouken@2042
    95
                    dst[0] = (sample & 0xFF);
slouken@2042
    96
                    sample >>= 8;
slouken@2042
    97
                    dst[1] = (sample & 0xFF);
slouken@1895
    98
                    src += 4;
slouken@1895
    99
                    dst += 2;
slouken@1895
   100
                }
slouken@1895
   101
            }
slouken@1895
   102
        }
slouken@1895
   103
        break;
slouken@0
   104
slouken@1895
   105
    case AUDIO_S16:
slouken@1895
   106
        {
slouken@1895
   107
            Uint8 *src, *dst;
slouken@0
   108
slouken@1895
   109
            src = cvt->buf;
slouken@1895
   110
            dst = cvt->buf;
icculus@1982
   111
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   112
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   113
                    sample = (Sint16) ((src[0] << 8) | src[1]) +
slouken@1895
   114
                        (Sint16) ((src[2] << 8) | src[3]);
slouken@2042
   115
                    sample /= 2;
slouken@2042
   116
                    dst[1] = (sample & 0xFF);
slouken@2042
   117
                    sample >>= 8;
slouken@2042
   118
                    dst[0] = (sample & 0xFF);
slouken@1895
   119
                    src += 4;
slouken@1895
   120
                    dst += 2;
slouken@1895
   121
                }
slouken@1895
   122
            } else {
slouken@1895
   123
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   124
                    sample = (Sint16) ((src[1] << 8) | src[0]) +
slouken@1895
   125
                        (Sint16) ((src[3] << 8) | src[2]);
slouken@2042
   126
                    sample /= 2;
slouken@2042
   127
                    dst[0] = (sample & 0xFF);
slouken@2042
   128
                    sample >>= 8;
slouken@2042
   129
                    dst[1] = (sample & 0xFF);
slouken@1895
   130
                    src += 4;
slouken@1895
   131
                    dst += 2;
slouken@1895
   132
                }
slouken@1895
   133
            }
slouken@1895
   134
        }
slouken@1895
   135
        break;
icculus@1982
   136
icculus@1982
   137
    case AUDIO_S32:
icculus@1982
   138
        {
icculus@1982
   139
            const Uint32 *src = (const Uint32 *) cvt->buf;
icculus@1982
   140
            Uint32 *dst = (Uint32 *) cvt->buf;
icculus@1982
   141
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   142
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   143
                    const Sint64 added =
slouken@1985
   144
                        (((Sint64) (Sint32) SDL_SwapBE32(src[0])) +
slouken@1985
   145
                         ((Sint64) (Sint32) SDL_SwapBE32(src[1])));
icculus@2078
   146
                    *(dst++) = SDL_SwapBE32((Uint32) ((Sint32) (added / 2)));
icculus@1982
   147
                }
icculus@1982
   148
            } else {
icculus@1982
   149
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   150
                    const Sint64 added =
slouken@1985
   151
                        (((Sint64) (Sint32) SDL_SwapLE32(src[0])) +
slouken@1985
   152
                         ((Sint64) (Sint32) SDL_SwapLE32(src[1])));
icculus@2078
   153
                    *(dst++) = SDL_SwapLE32((Uint32) ((Sint32) (added / 2)));
icculus@1982
   154
                }
icculus@1982
   155
            }
icculus@1982
   156
        }
icculus@1982
   157
        break;
icculus@1982
   158
icculus@1982
   159
    case AUDIO_F32:
icculus@1982
   160
        {
icculus@2014
   161
            const float *src = (const float *) cvt->buf;
icculus@2014
   162
            float *dst = (float *) cvt->buf;
icculus@1982
   163
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   164
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@2049
   165
                    const float src1 = SDL_SwapFloatBE(src[0]);
icculus@2049
   166
                    const float src2 = SDL_SwapFloatBE(src[1]);
icculus@1982
   167
                    const double added = ((double) src1) + ((double) src2);
icculus@2049
   168
                    const float halved = (float) (added * 0.5);
icculus@2049
   169
                    *(dst++) = SDL_SwapFloatBE(halved);
icculus@1982
   170
                }
icculus@1982
   171
            } else {
icculus@1982
   172
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@2049
   173
                    const float src1 = SDL_SwapFloatLE(src[0]);
icculus@2049
   174
                    const float src2 = SDL_SwapFloatLE(src[1]);
icculus@1982
   175
                    const double added = ((double) src1) + ((double) src2);
icculus@2049
   176
                    const float halved = (float) (added * 0.5);
icculus@2049
   177
                    *(dst++) = SDL_SwapFloatLE(halved);
icculus@1982
   178
                }
icculus@1982
   179
            }
icculus@1982
   180
        }
icculus@1982
   181
        break;
slouken@1895
   182
    }
icculus@1982
   183
slouken@1895
   184
    cvt->len_cvt /= 2;
slouken@1895
   185
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   186
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   187
    }
slouken@0
   188
}
slouken@0
   189
icculus@1982
   190
slouken@942
   191
/* Discard top 4 channels */
icculus@1982
   192
static void SDLCALL
icculus@1982
   193
SDL_ConvertStrip(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   194
{
slouken@1895
   195
    int i;
slouken@942
   196
slouken@942
   197
#ifdef DEBUG_CONVERT
icculus@1982
   198
    fprintf(stderr, "Converting down from 6 channels to stereo\n");
slouken@942
   199
#endif
slouken@942
   200
slouken@1985
   201
#define strip_chans_6_to_2(type) \
icculus@1982
   202
    { \
icculus@1982
   203
        const type *src = (const type *) cvt->buf; \
icculus@1982
   204
        type *dst = (type *) cvt->buf; \
icculus@1982
   205
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   206
            dst[0] = src[0]; \
icculus@1982
   207
            dst[1] = src[1]; \
icculus@1982
   208
            src += 6; \
icculus@1982
   209
            dst += 2; \
icculus@1982
   210
        } \
icculus@1982
   211
    }
slouken@942
   212
icculus@1982
   213
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   214
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   215
    case 8:
slouken@1985
   216
        strip_chans_6_to_2(Uint8);
slouken@1985
   217
        break;
slouken@1985
   218
    case 16:
slouken@1985
   219
        strip_chans_6_to_2(Uint16);
slouken@1985
   220
        break;
slouken@1985
   221
    case 32:
slouken@1985
   222
        strip_chans_6_to_2(Uint32);
slouken@1985
   223
        break;
icculus@1982
   224
    }
slouken@942
   225
slouken@1985
   226
#undef strip_chans_6_to_2
slouken@942
   227
slouken@1895
   228
    cvt->len_cvt /= 3;
slouken@1895
   229
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   230
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   231
    }
slouken@942
   232
}
slouken@942
   233
slouken@942
   234
slouken@942
   235
/* Discard top 2 channels of 6 */
icculus@1982
   236
static void SDLCALL
icculus@1982
   237
SDL_ConvertStrip_2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   238
{
slouken@1895
   239
    int i;
slouken@942
   240
slouken@942
   241
#ifdef DEBUG_CONVERT
slouken@1895
   242
    fprintf(stderr, "Converting 6 down to quad\n");
slouken@942
   243
#endif
slouken@942
   244
slouken@1985
   245
#define strip_chans_6_to_4(type) \
icculus@1982
   246
    { \
icculus@1982
   247
        const type *src = (const type *) cvt->buf; \
icculus@1982
   248
        type *dst = (type *) cvt->buf; \
icculus@1982
   249
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   250
            dst[0] = src[0]; \
icculus@1982
   251
            dst[1] = src[1]; \
icculus@1982
   252
            dst[2] = src[2]; \
icculus@1982
   253
            dst[3] = src[3]; \
icculus@1982
   254
            src += 6; \
icculus@1982
   255
            dst += 4; \
icculus@1982
   256
        } \
icculus@1982
   257
    }
slouken@942
   258
icculus@1982
   259
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   260
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   261
    case 8:
slouken@1985
   262
        strip_chans_6_to_4(Uint8);
slouken@1985
   263
        break;
slouken@1985
   264
    case 16:
slouken@1985
   265
        strip_chans_6_to_4(Uint16);
slouken@1985
   266
        break;
slouken@1985
   267
    case 32:
slouken@1985
   268
        strip_chans_6_to_4(Uint32);
slouken@1985
   269
        break;
icculus@1982
   270
    }
slouken@942
   271
slouken@1985
   272
#undef strip_chans_6_to_4
slouken@942
   273
icculus@1982
   274
    cvt->len_cvt /= 6;
icculus@1982
   275
    cvt->len_cvt *= 4;
slouken@1895
   276
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   277
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   278
    }
slouken@942
   279
}
slouken@0
   280
slouken@0
   281
/* Duplicate a mono channel to both stereo channels */
icculus@1982
   282
static void SDLCALL
icculus@1982
   283
SDL_ConvertStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
   284
{
slouken@1895
   285
    int i;
slouken@0
   286
slouken@0
   287
#ifdef DEBUG_CONVERT
slouken@1895
   288
    fprintf(stderr, "Converting to stereo\n");
slouken@0
   289
#endif
slouken@0
   290
slouken@1985
   291
#define dup_chans_1_to_2(type) \
icculus@1982
   292
    { \
icculus@1982
   293
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   294
        type *dst = (type *) (cvt->buf + cvt->len_cvt * 2); \
slouken@6210
   295
        for (i = cvt->len_cvt / sizeof(type); i; --i) { \
icculus@1982
   296
            const type val = *src; \
slouken@6210
   297
            src -= 1; \
icculus@1982
   298
            dst -= 2; \
icculus@1982
   299
            dst[0] = dst[1] = val; \
icculus@1982
   300
        } \
icculus@1982
   301
    }
slouken@0
   302
icculus@1982
   303
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   304
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   305
    case 8:
slouken@1985
   306
        dup_chans_1_to_2(Uint8);
slouken@1985
   307
        break;
slouken@1985
   308
    case 16:
slouken@1985
   309
        dup_chans_1_to_2(Uint16);
slouken@1985
   310
        break;
slouken@1985
   311
    case 32:
slouken@1985
   312
        dup_chans_1_to_2(Uint32);
slouken@1985
   313
        break;
slouken@1895
   314
    }
icculus@1982
   315
slouken@1985
   316
#undef dup_chans_1_to_2
icculus@1982
   317
slouken@1895
   318
    cvt->len_cvt *= 2;
slouken@1895
   319
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   320
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   321
    }
slouken@0
   322
}
slouken@0
   323
slouken@942
   324
slouken@942
   325
/* Duplicate a stereo channel to a pseudo-5.1 stream */
icculus@1982
   326
static void SDLCALL
icculus@1982
   327
SDL_ConvertSurround(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   328
{
slouken@1895
   329
    int i;
slouken@942
   330
slouken@942
   331
#ifdef DEBUG_CONVERT
slouken@1895
   332
    fprintf(stderr, "Converting stereo to surround\n");
slouken@942
   333
#endif
slouken@942
   334
slouken@1985
   335
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   336
    case AUDIO_U8:
slouken@1895
   337
        {
slouken@1895
   338
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   339
slouken@1895
   340
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   341
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 3);
slouken@1895
   342
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   343
                dst -= 6;
slouken@1895
   344
                src -= 2;
slouken@1895
   345
                lf = src[0];
slouken@1895
   346
                rf = src[1];
slouken@1895
   347
                ce = (lf / 2) + (rf / 2);
slouken@1895
   348
                dst[0] = lf;
slouken@1895
   349
                dst[1] = rf;
slouken@1895
   350
                dst[2] = lf - ce;
slouken@1895
   351
                dst[3] = rf - ce;
slouken@1895
   352
                dst[4] = ce;
slouken@1895
   353
                dst[5] = ce;
slouken@1895
   354
            }
slouken@1895
   355
        }
slouken@1895
   356
        break;
slouken@942
   357
slouken@1895
   358
    case AUDIO_S8:
slouken@1895
   359
        {
slouken@1895
   360
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   361
slouken@1895
   362
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   363
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 3;
slouken@1895
   364
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   365
                dst -= 6;
slouken@1895
   366
                src -= 2;
slouken@1895
   367
                lf = src[0];
slouken@1895
   368
                rf = src[1];
slouken@1895
   369
                ce = (lf / 2) + (rf / 2);
slouken@1895
   370
                dst[0] = lf;
slouken@1895
   371
                dst[1] = rf;
slouken@1895
   372
                dst[2] = lf - ce;
slouken@1895
   373
                dst[3] = rf - ce;
slouken@1895
   374
                dst[4] = ce;
slouken@1895
   375
                dst[5] = ce;
slouken@1895
   376
            }
slouken@1895
   377
        }
slouken@1895
   378
        break;
slouken@942
   379
slouken@1895
   380
    case AUDIO_U16:
slouken@1895
   381
        {
slouken@1895
   382
            Uint8 *src, *dst;
slouken@1895
   383
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   384
slouken@1895
   385
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   386
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   387
icculus@1982
   388
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   389
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   390
                    dst -= 12;
slouken@1895
   391
                    src -= 4;
slouken@1895
   392
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   393
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   394
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   395
                    rr = lf - ce;
slouken@1895
   396
                    lr = rf - ce;
slouken@1895
   397
                    dst[1] = (lf & 0xFF);
slouken@1895
   398
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   399
                    dst[3] = (rf & 0xFF);
slouken@1895
   400
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   401
slouken@1895
   402
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   403
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   404
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   405
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   406
slouken@1895
   407
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   408
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   409
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   410
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   411
                }
slouken@1895
   412
            } else {
slouken@1895
   413
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   414
                    dst -= 12;
slouken@1895
   415
                    src -= 4;
slouken@1895
   416
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   417
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   418
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   419
                    rr = lf - ce;
slouken@1895
   420
                    lr = rf - ce;
slouken@1895
   421
                    dst[0] = (lf & 0xFF);
slouken@1895
   422
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   423
                    dst[2] = (rf & 0xFF);
slouken@1895
   424
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   425
slouken@1895
   426
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   427
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   428
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   429
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   430
slouken@1895
   431
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   432
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   433
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   434
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   435
                }
slouken@1895
   436
            }
slouken@1895
   437
        }
slouken@1895
   438
        break;
slouken@942
   439
slouken@1895
   440
    case AUDIO_S16:
slouken@1895
   441
        {
slouken@1895
   442
            Uint8 *src, *dst;
slouken@1895
   443
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   444
slouken@1895
   445
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   446
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   447
icculus@1982
   448
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   449
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   450
                    dst -= 12;
slouken@1895
   451
                    src -= 4;
slouken@1895
   452
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   453
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   454
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   455
                    rr = lf - ce;
slouken@1895
   456
                    lr = rf - ce;
slouken@1895
   457
                    dst[1] = (lf & 0xFF);
slouken@1895
   458
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   459
                    dst[3] = (rf & 0xFF);
slouken@1895
   460
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   461
slouken@1895
   462
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   463
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   464
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   465
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   466
slouken@1895
   467
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   468
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   469
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   470
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   471
                }
slouken@1895
   472
            } else {
slouken@1895
   473
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   474
                    dst -= 12;
slouken@1895
   475
                    src -= 4;
slouken@1895
   476
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   477
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   478
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   479
                    rr = lf - ce;
slouken@1895
   480
                    lr = rf - ce;
slouken@1895
   481
                    dst[0] = (lf & 0xFF);
slouken@1895
   482
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   483
                    dst[2] = (rf & 0xFF);
slouken@1895
   484
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   485
slouken@1895
   486
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   487
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   488
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   489
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   490
slouken@1895
   491
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   492
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   493
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   494
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   495
                }
slouken@1895
   496
            }
slouken@1895
   497
        }
slouken@1895
   498
        break;
icculus@1982
   499
icculus@1982
   500
    case AUDIO_S32:
icculus@1982
   501
        {
icculus@1982
   502
            Sint32 lf, rf, ce;
icculus@1982
   503
            const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
icculus@1982
   504
            Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   505
icculus@1982
   506
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   507
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   508
                    dst -= 6;
icculus@1982
   509
                    src -= 2;
icculus@1982
   510
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   511
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   512
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   513
                    dst[0] = SDL_SwapBE32((Uint32) lf);
icculus@1982
   514
                    dst[1] = SDL_SwapBE32((Uint32) rf);
icculus@1982
   515
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   516
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   517
                    dst[4] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   518
                    dst[5] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   519
                }
icculus@1982
   520
            } else {
icculus@1982
   521
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   522
                    dst -= 6;
icculus@1982
   523
                    src -= 2;
icculus@1982
   524
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   525
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   526
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   527
                    dst[0] = src[0];
icculus@1982
   528
                    dst[1] = src[1];
icculus@1982
   529
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   530
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   531
                    dst[4] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   532
                    dst[5] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   533
                }
icculus@1982
   534
            }
icculus@1982
   535
        }
icculus@1982
   536
        break;
icculus@1982
   537
icculus@1982
   538
    case AUDIO_F32:
icculus@1982
   539
        {
icculus@1982
   540
            float lf, rf, ce;
icculus@2014
   541
            const float *src = (const float *) cvt->buf + cvt->len_cvt;
icculus@2014
   542
            float *dst = (float *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   543
icculus@1982
   544
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   545
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   546
                    dst -= 6;
icculus@1982
   547
                    src -= 2;
icculus@2014
   548
                    lf = SDL_SwapFloatBE(src[0]);
icculus@2014
   549
                    rf = SDL_SwapFloatBE(src[1]);
icculus@1982
   550
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   551
                    dst[0] = src[0];
icculus@1982
   552
                    dst[1] = src[1];
icculus@2014
   553
                    dst[2] = SDL_SwapFloatBE(lf - ce);
icculus@2014
   554
                    dst[3] = SDL_SwapFloatBE(rf - ce);
icculus@2014
   555
                    dst[4] = dst[5] = SDL_SwapFloatBE(ce);
icculus@1982
   556
                }
icculus@1982
   557
            } else {
icculus@1982
   558
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   559
                    dst -= 6;
icculus@1982
   560
                    src -= 2;
icculus@2014
   561
                    lf = SDL_SwapFloatLE(src[0]);
icculus@2014
   562
                    rf = SDL_SwapFloatLE(src[1]);
icculus@1982
   563
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   564
                    dst[0] = src[0];
icculus@1982
   565
                    dst[1] = src[1];
icculus@2014
   566
                    dst[2] = SDL_SwapFloatLE(lf - ce);
icculus@2014
   567
                    dst[3] = SDL_SwapFloatLE(rf - ce);
icculus@2014
   568
                    dst[4] = dst[5] = SDL_SwapFloatLE(ce);
icculus@1982
   569
                }
icculus@1982
   570
            }
icculus@1982
   571
        }
icculus@1982
   572
        break;
icculus@1982
   573
slouken@1895
   574
    }
slouken@1895
   575
    cvt->len_cvt *= 3;
slouken@1895
   576
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   577
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   578
    }
slouken@942
   579
}
slouken@942
   580
slouken@942
   581
slouken@942
   582
/* Duplicate a stereo channel to a pseudo-4.0 stream */
icculus@1982
   583
static void SDLCALL
icculus@1982
   584
SDL_ConvertSurround_4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   585
{
slouken@1895
   586
    int i;
slouken@942
   587
slouken@942
   588
#ifdef DEBUG_CONVERT
slouken@1895
   589
    fprintf(stderr, "Converting stereo to quad\n");
slouken@942
   590
#endif
slouken@942
   591
slouken@1985
   592
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   593
    case AUDIO_U8:
slouken@1895
   594
        {
slouken@1895
   595
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   596
slouken@1895
   597
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   598
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 2);
slouken@1895
   599
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   600
                dst -= 4;
slouken@1895
   601
                src -= 2;
slouken@1895
   602
                lf = src[0];
slouken@1895
   603
                rf = src[1];
slouken@1895
   604
                ce = (lf / 2) + (rf / 2);
slouken@1895
   605
                dst[0] = lf;
slouken@1895
   606
                dst[1] = rf;
slouken@1895
   607
                dst[2] = lf - ce;
slouken@1895
   608
                dst[3] = rf - ce;
slouken@1895
   609
            }
slouken@1895
   610
        }
slouken@1895
   611
        break;
slouken@942
   612
slouken@1895
   613
    case AUDIO_S8:
slouken@1895
   614
        {
slouken@1895
   615
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   616
slouken@1895
   617
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   618
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 2;
slouken@1895
   619
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   620
                dst -= 4;
slouken@1895
   621
                src -= 2;
slouken@1895
   622
                lf = src[0];
slouken@1895
   623
                rf = src[1];
slouken@1895
   624
                ce = (lf / 2) + (rf / 2);
slouken@1895
   625
                dst[0] = lf;
slouken@1895
   626
                dst[1] = rf;
slouken@1895
   627
                dst[2] = lf - ce;
slouken@1895
   628
                dst[3] = rf - ce;
slouken@1895
   629
            }
slouken@1895
   630
        }
slouken@1895
   631
        break;
slouken@942
   632
slouken@1895
   633
    case AUDIO_U16:
slouken@1895
   634
        {
slouken@1895
   635
            Uint8 *src, *dst;
slouken@1895
   636
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   637
slouken@1895
   638
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   639
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   640
icculus@1982
   641
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   642
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   643
                    dst -= 8;
slouken@1895
   644
                    src -= 4;
slouken@1895
   645
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   646
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   647
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   648
                    rr = lf - ce;
slouken@1895
   649
                    lr = rf - ce;
slouken@1895
   650
                    dst[1] = (lf & 0xFF);
slouken@1895
   651
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   652
                    dst[3] = (rf & 0xFF);
slouken@1895
   653
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   654
slouken@1895
   655
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   656
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   657
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   658
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   659
                }
slouken@1895
   660
            } else {
slouken@1895
   661
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   662
                    dst -= 8;
slouken@1895
   663
                    src -= 4;
slouken@1895
   664
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   665
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   666
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   667
                    rr = lf - ce;
slouken@1895
   668
                    lr = rf - ce;
slouken@1895
   669
                    dst[0] = (lf & 0xFF);
slouken@1895
   670
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   671
                    dst[2] = (rf & 0xFF);
slouken@1895
   672
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   673
slouken@1895
   674
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   675
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   676
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   677
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   678
                }
slouken@1895
   679
            }
slouken@1895
   680
        }
slouken@1895
   681
        break;
slouken@942
   682
slouken@1895
   683
    case AUDIO_S16:
slouken@1895
   684
        {
slouken@1895
   685
            Uint8 *src, *dst;
slouken@1895
   686
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   687
slouken@1895
   688
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   689
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   690
icculus@1982
   691
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   692
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   693
                    dst -= 8;
slouken@1895
   694
                    src -= 4;
slouken@1895
   695
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   696
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   697
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   698
                    rr = lf - ce;
slouken@1895
   699
                    lr = rf - ce;
slouken@1895
   700
                    dst[1] = (lf & 0xFF);
slouken@1895
   701
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   702
                    dst[3] = (rf & 0xFF);
slouken@1895
   703
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   704
slouken@1895
   705
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   706
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   707
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   708
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   709
                }
slouken@1895
   710
            } else {
slouken@1895
   711
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   712
                    dst -= 8;
slouken@1895
   713
                    src -= 4;
slouken@1895
   714
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   715
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   716
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   717
                    rr = lf - ce;
slouken@1895
   718
                    lr = rf - ce;
slouken@1895
   719
                    dst[0] = (lf & 0xFF);
slouken@1895
   720
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   721
                    dst[2] = (rf & 0xFF);
slouken@1895
   722
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   723
slouken@1895
   724
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   725
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   726
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   727
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   728
                }
slouken@1895
   729
            }
slouken@1895
   730
        }
slouken@1895
   731
        break;
slouken@942
   732
icculus@1982
   733
    case AUDIO_S32:
icculus@1982
   734
        {
icculus@1982
   735
            const Uint32 *src = (const Uint32 *) (cvt->buf + cvt->len_cvt);
icculus@1982
   736
            Uint32 *dst = (Uint32 *) (cvt->buf + cvt->len_cvt * 2);
icculus@1982
   737
            Sint32 lf, rf, ce;
slouken@942
   738
icculus@1982
   739
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   740
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   741
                    dst -= 4;
icculus@1982
   742
                    src -= 2;
icculus@1982
   743
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   744
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   745
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   746
                    dst[0] = src[0];
icculus@1982
   747
                    dst[1] = src[1];
icculus@1982
   748
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   749
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   750
                }
icculus@1982
   751
            } else {
icculus@1982
   752
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   753
                    dst -= 4;
icculus@1982
   754
                    src -= 2;
icculus@1982
   755
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   756
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   757
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   758
                    dst[0] = src[0];
icculus@1982
   759
                    dst[1] = src[1];
icculus@1982
   760
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   761
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   762
                }
icculus@1982
   763
            }
slouken@1895
   764
        }
slouken@1895
   765
        break;
slouken@1895
   766
    }
slouken@1895
   767
    cvt->len_cvt *= 2;
slouken@1895
   768
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   769
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   770
    }
slouken@0
   771
}
slouken@0
   772
slouken@0
   773
slouken@1895
   774
int
slouken@1895
   775
SDL_ConvertAudio(SDL_AudioCVT * cvt)
slouken@0
   776
{
icculus@3021
   777
    /* !!! FIXME: (cvt) should be const; stack-copy it here. */
icculus@3021
   778
    /* !!! FIXME: (actually, we can't...len_cvt needs to be updated. Grr.) */
icculus@3021
   779
slouken@1895
   780
    /* Make sure there's data to convert */
slouken@1895
   781
    if (cvt->buf == NULL) {
slouken@1895
   782
        SDL_SetError("No buffer allocated for conversion");
slouken@1895
   783
        return (-1);
slouken@1895
   784
    }
slouken@1895
   785
    /* Return okay if no conversion is necessary */
slouken@1895
   786
    cvt->len_cvt = cvt->len;
slouken@1895
   787
    if (cvt->filters[0] == NULL) {
slouken@1895
   788
        return (0);
slouken@1895
   789
    }
slouken@0
   790
slouken@1895
   791
    /* Set up the conversion and go! */
slouken@1895
   792
    cvt->filter_index = 0;
slouken@1895
   793
    cvt->filters[0] (cvt, cvt->src_format);
slouken@1895
   794
    return (0);
slouken@0
   795
}
slouken@0
   796
icculus@1982
   797
icculus@1982
   798
static SDL_AudioFilter
icculus@1982
   799
SDL_HandTunedTypeCVT(SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
   800
{
icculus@1982
   801
    /*
icculus@1982
   802
     * Fill in any future conversions that are specialized to a
icculus@1982
   803
     *  processor, platform, compiler, or library here.
icculus@1982
   804
     */
icculus@1982
   805
slouken@1985
   806
    return NULL;                /* no specialized converter code available. */
icculus@1982
   807
}
icculus@1982
   808
icculus@1982
   809
icculus@1982
   810
/*
icculus@1982
   811
 * Find a converter between two data types. We try to select a hand-tuned
icculus@1982
   812
 *  asm/vectorized/optimized function first, and then fallback to an
icculus@1982
   813
 *  autogenerated function that is customized to convert between two
icculus@1982
   814
 *  specific data types.
icculus@1982
   815
 */
icculus@1982
   816
static int
icculus@1982
   817
SDL_BuildAudioTypeCVT(SDL_AudioCVT * cvt,
icculus@1982
   818
                      SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
   819
{
icculus@1982
   820
    if (src_fmt != dst_fmt) {
icculus@1982
   821
        const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt);
icculus@1982
   822
        const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt);
icculus@1982
   823
        SDL_AudioFilter filter = SDL_HandTunedTypeCVT(src_fmt, dst_fmt);
icculus@1982
   824
icculus@1982
   825
        /* No hand-tuned converter? Try the autogenerated ones. */
icculus@1982
   826
        if (filter == NULL) {
icculus@1982
   827
            int i;
icculus@1982
   828
            for (i = 0; sdl_audio_type_filters[i].filter != NULL; i++) {
icculus@1982
   829
                const SDL_AudioTypeFilters *filt = &sdl_audio_type_filters[i];
icculus@1982
   830
                if ((filt->src_fmt == src_fmt) && (filt->dst_fmt == dst_fmt)) {
icculus@1982
   831
                    filter = filt->filter;
icculus@1982
   832
                    break;
icculus@1982
   833
                }
icculus@1982
   834
            }
icculus@1982
   835
icculus@1982
   836
            if (filter == NULL) {
slouken@3491
   837
                SDL_SetError("No conversion available for these formats");
slouken@3491
   838
                return -1;
icculus@1982
   839
            }
icculus@1982
   840
        }
icculus@1982
   841
icculus@1982
   842
        /* Update (cvt) with filter details... */
icculus@1982
   843
        cvt->filters[cvt->filter_index++] = filter;
icculus@1982
   844
        if (src_bitsize < dst_bitsize) {
icculus@1982
   845
            const int mult = (dst_bitsize / src_bitsize);
icculus@1982
   846
            cvt->len_mult *= mult;
icculus@1982
   847
            cvt->len_ratio *= mult;
icculus@1982
   848
        } else if (src_bitsize > dst_bitsize) {
icculus@1982
   849
            cvt->len_ratio /= (src_bitsize / dst_bitsize);
icculus@1982
   850
        }
icculus@1982
   851
slouken@1985
   852
        return 1;               /* added a converter. */
icculus@1982
   853
    }
icculus@1982
   854
slouken@1985
   855
    return 0;                   /* no conversion necessary. */
icculus@1982
   856
}
icculus@1982
   857
icculus@3021
   858
icculus@3021
   859
static SDL_AudioFilter
icculus@3021
   860
SDL_HandTunedResampleCVT(SDL_AudioCVT * cvt, int dst_channels,
icculus@3021
   861
                         int src_rate, int dst_rate)
slouken@2716
   862
{
icculus@3021
   863
    /*
icculus@3021
   864
     * Fill in any future conversions that are specialized to a
icculus@3021
   865
     *  processor, platform, compiler, or library here.
icculus@3021
   866
     */
slouken@2716
   867
icculus@3021
   868
    return NULL;                /* no specialized converter code available. */
icculus@3021
   869
}
slouken@2716
   870
icculus@3021
   871
static int
icculus@3021
   872
SDL_FindFrequencyMultiple(const int src_rate, const int dst_rate)
icculus@3021
   873
{
icculus@3021
   874
    int retval = 0;
slouken@2716
   875
icculus@3021
   876
    /* If we only built with the arbitrary resamplers, ignore multiples. */
icculus@3021
   877
#if !LESS_RESAMPLERS
icculus@3021
   878
    int lo, hi;
icculus@3021
   879
    int div;
slouken@2716
   880
icculus@6281
   881
    SDL_assert(src_rate != 0);
icculus@6281
   882
    SDL_assert(dst_rate != 0);
icculus@6281
   883
    SDL_assert(src_rate != dst_rate);
slouken@2716
   884
icculus@3021
   885
    if (src_rate < dst_rate) {
icculus@3021
   886
        lo = src_rate;
icculus@3021
   887
        hi = dst_rate;
icculus@3021
   888
    } else {
icculus@3021
   889
        lo = dst_rate;
icculus@3021
   890
        hi = src_rate;
icculus@3021
   891
    }
slouken@2716
   892
icculus@3021
   893
    /* zero means "not a supported multiple" ... we only do 2x and 4x. */
icculus@3021
   894
    if ((hi % lo) != 0)
slouken@3040
   895
        return 0;               /* not a multiple. */
slouken@2716
   896
icculus@3021
   897
    div = hi / lo;
icculus@3021
   898
    retval = ((div == 2) || (div == 4)) ? div : 0;
icculus@3021
   899
#endif
icculus@3021
   900
icculus@3021
   901
    return retval;
icculus@3021
   902
}
icculus@3021
   903
icculus@3021
   904
static int
icculus@3021
   905
SDL_BuildAudioResampleCVT(SDL_AudioCVT * cvt, int dst_channels,
icculus@3021
   906
                          int src_rate, int dst_rate)
icculus@3021
   907
{
icculus@3021
   908
    if (src_rate != dst_rate) {
icculus@3021
   909
        SDL_AudioFilter filter = SDL_HandTunedResampleCVT(cvt, dst_channels,
icculus@3021
   910
                                                          src_rate, dst_rate);
icculus@3021
   911
icculus@3021
   912
        /* No hand-tuned converter? Try the autogenerated ones. */
icculus@3021
   913
        if (filter == NULL) {
icculus@3021
   914
            int i;
icculus@3021
   915
            const int upsample = (src_rate < dst_rate) ? 1 : 0;
slouken@3040
   916
            const int multiple =
slouken@3040
   917
                SDL_FindFrequencyMultiple(src_rate, dst_rate);
icculus@3021
   918
icculus@3021
   919
            for (i = 0; sdl_audio_rate_filters[i].filter != NULL; i++) {
icculus@3021
   920
                const SDL_AudioRateFilters *filt = &sdl_audio_rate_filters[i];
icculus@3021
   921
                if ((filt->fmt == cvt->dst_format) &&
icculus@3021
   922
                    (filt->channels == dst_channels) &&
icculus@3021
   923
                    (filt->upsample == upsample) &&
icculus@3021
   924
                    (filt->multiple == multiple)) {
icculus@3021
   925
                    filter = filt->filter;
icculus@3021
   926
                    break;
icculus@3021
   927
                }
icculus@3021
   928
            }
icculus@3021
   929
icculus@3021
   930
            if (filter == NULL) {
slouken@3491
   931
                SDL_SetError("No conversion available for these rates");
slouken@3491
   932
                return -1;
icculus@3021
   933
            }
slouken@2716
   934
        }
slouken@2716
   935
icculus@3021
   936
        /* Update (cvt) with filter details... */
icculus@3021
   937
        cvt->filters[cvt->filter_index++] = filter;
icculus@3021
   938
        if (src_rate < dst_rate) {
icculus@3021
   939
            const double mult = ((double) dst_rate) / ((double) src_rate);
slouken@3032
   940
            cvt->len_mult *= (int) SDL_ceil(mult);
icculus@3021
   941
            cvt->len_ratio *= mult;
icculus@3021
   942
        } else {
icculus@3021
   943
            cvt->len_ratio /= ((double) src_rate) / ((double) dst_rate);
slouken@2716
   944
        }
icculus@3021
   945
icculus@3021
   946
        return 1;               /* added a converter. */
slouken@2716
   947
    }
slouken@2716
   948
icculus@3021
   949
    return 0;                   /* no conversion necessary. */
slouken@2716
   950
}
icculus@1982
   951
icculus@1982
   952
icculus@1982
   953
/* Creates a set of audio filters to convert from one format to another.
icculus@1982
   954
   Returns -1 if the format conversion is not supported, 0 if there's
icculus@1982
   955
   no conversion needed, or 1 if the audio filter is set up.
slouken@0
   956
*/
slouken@1895
   957
slouken@1895
   958
int
slouken@1895
   959
SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
icculus@1982
   960
                  SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate,
icculus@1982
   961
                  SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate)
slouken@0
   962
{
icculus@3021
   963
    /*
icculus@3021
   964
     * !!! FIXME: reorder filters based on which grow/shrink the buffer.
icculus@3021
   965
     * !!! FIXME: ideally, we should do everything that shrinks the buffer
icculus@3021
   966
     * !!! FIXME: first, so we don't have to process as many bytes in a given
icculus@3021
   967
     * !!! FIXME: filter and abuse the CPU cache less. This might not be as
icculus@3021
   968
     * !!! FIXME: good in practice as it sounds in theory, though.
icculus@3021
   969
     */
icculus@3021
   970
aschiffler@6819
   971
    /* Sanity check target pointer */
aschiffler@6819
   972
    if (cvt == NULL) {
aschiffler@6819
   973
        SDL_InvalidParamError("cvt");
aschiffler@6819
   974
        return -1;
aschiffler@6819
   975
    }
aschiffler@6819
   976
    
slouken@3491
   977
    /* there are no unsigned types over 16 bits, so catch this up front. */
icculus@1982
   978
    if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
slouken@3491
   979
        SDL_SetError("Invalid source format");
icculus@1982
   980
        return -1;
icculus@1982
   981
    }
icculus@1982
   982
    if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
slouken@3491
   983
        SDL_SetError("Invalid destination format");
icculus@1982
   984
        return -1;
icculus@1982
   985
    }
icculus@3021
   986
icculus@3021
   987
    /* prevent possible divisions by zero, etc. */
aschiffler@6819
   988
    if ((src_channels == 0) || (dst_channels == 0)) {
aschiffler@6819
   989
        SDL_SetError("Source or destination channels is zero");
aschiffler@6819
   990
        return -1;
aschiffler@6819
   991
    }
icculus@3021
   992
    if ((src_rate == 0) || (dst_rate == 0)) {
slouken@3491
   993
        SDL_SetError("Source or destination rate is zero");
icculus@3021
   994
        return -1;
icculus@3021
   995
    }
slouken@1985
   996
#ifdef DEBUG_CONVERT
icculus@1982
   997
    printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
slouken@1985
   998
           src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
slouken@1985
   999
#endif
icculus@1982
  1000
slouken@1895
  1001
    /* Start off with no conversion necessary */
icculus@2879
  1002
    SDL_zerop(cvt);
icculus@1982
  1003
    cvt->src_format = src_fmt;
icculus@1982
  1004
    cvt->dst_format = dst_fmt;
slouken@1895
  1005
    cvt->needed = 0;
slouken@1895
  1006
    cvt->filter_index = 0;
slouken@1895
  1007
    cvt->filters[0] = NULL;
slouken@1895
  1008
    cvt->len_mult = 1;
slouken@1895
  1009
    cvt->len_ratio = 1.0;
icculus@3021
  1010
    cvt->rate_incr = ((double) dst_rate) / ((double) src_rate);
slouken@0
  1011
icculus@1982
  1012
    /* Convert data types, if necessary. Updates (cvt). */
icculus@3021
  1013
    if (SDL_BuildAudioTypeCVT(cvt, src_fmt, dst_fmt) == -1) {
slouken@1985
  1014
        return -1;              /* shouldn't happen, but just in case... */
icculus@3021
  1015
    }
slouken@0
  1016
icculus@1982
  1017
    /* Channel conversion */
slouken@1895
  1018
    if (src_channels != dst_channels) {
slouken@1895
  1019
        if ((src_channels == 1) && (dst_channels > 1)) {
slouken@1895
  1020
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1021
            cvt->len_mult *= 2;
slouken@1895
  1022
            src_channels = 2;
slouken@1895
  1023
            cvt->len_ratio *= 2;
slouken@1895
  1024
        }
slouken@1895
  1025
        if ((src_channels == 2) && (dst_channels == 6)) {
slouken@1895
  1026
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround;
slouken@1895
  1027
            src_channels = 6;
slouken@1895
  1028
            cvt->len_mult *= 3;
slouken@1895
  1029
            cvt->len_ratio *= 3;
slouken@1895
  1030
        }
slouken@1895
  1031
        if ((src_channels == 2) && (dst_channels == 4)) {
slouken@1895
  1032
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround_4;
slouken@1895
  1033
            src_channels = 4;
slouken@1895
  1034
            cvt->len_mult *= 2;
slouken@1895
  1035
            cvt->len_ratio *= 2;
slouken@1895
  1036
        }
slouken@1895
  1037
        while ((src_channels * 2) <= dst_channels) {
slouken@1895
  1038
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1039
            cvt->len_mult *= 2;
slouken@1895
  1040
            src_channels *= 2;
slouken@1895
  1041
            cvt->len_ratio *= 2;
slouken@1895
  1042
        }
slouken@1895
  1043
        if ((src_channels == 6) && (dst_channels <= 2)) {
slouken@1895
  1044
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip;
slouken@1895
  1045
            src_channels = 2;
slouken@1895
  1046
            cvt->len_ratio /= 3;
slouken@1895
  1047
        }
slouken@1895
  1048
        if ((src_channels == 6) && (dst_channels == 4)) {
slouken@1895
  1049
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip_2;
slouken@1895
  1050
            src_channels = 4;
slouken@1895
  1051
            cvt->len_ratio /= 2;
slouken@1895
  1052
        }
slouken@1895
  1053
        /* This assumes that 4 channel audio is in the format:
slouken@1895
  1054
           Left {front/back} + Right {front/back}
slouken@1895
  1055
           so converting to L/R stereo works properly.
slouken@1895
  1056
         */
slouken@1895
  1057
        while (((src_channels % 2) == 0) &&
slouken@1895
  1058
               ((src_channels / 2) >= dst_channels)) {
slouken@1895
  1059
            cvt->filters[cvt->filter_index++] = SDL_ConvertMono;
slouken@1895
  1060
            src_channels /= 2;
slouken@1895
  1061
            cvt->len_ratio /= 2;
slouken@1895
  1062
        }
slouken@1895
  1063
        if (src_channels != dst_channels) {
slouken@1895
  1064
            /* Uh oh.. */ ;
slouken@1895
  1065
        }
slouken@1895
  1066
    }
slouken@0
  1067
icculus@3021
  1068
    /* Do rate conversion, if necessary. Updates (cvt). */
slouken@3040
  1069
    if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) ==
slouken@3040
  1070
        -1) {
icculus@3021
  1071
        return -1;              /* shouldn't happen, but just in case... */
slouken@2716
  1072
    }
slouken@2716
  1073
slouken@1895
  1074
    /* Set up the filter information */
slouken@1895
  1075
    if (cvt->filter_index != 0) {
slouken@1895
  1076
        cvt->needed = 1;
icculus@1982
  1077
        cvt->src_format = src_fmt;
icculus@1982
  1078
        cvt->dst_format = dst_fmt;
slouken@1895
  1079
        cvt->len = 0;
slouken@1895
  1080
        cvt->buf = NULL;
slouken@1895
  1081
        cvt->filters[cvt->filter_index] = NULL;
slouken@1895
  1082
    }
slouken@1895
  1083
    return (cvt->needed);
slouken@0
  1084
}
slouken@1895
  1085
slouken@2716
  1086
slouken@1895
  1087
/* vi: set ts=4 sw=4 expandtab: */