src/audio/SDL_audiocvt.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Jul 2013 01:26:43 -0400
changeset 7410 a9878830f05e
parent 7191 75360622e65f
child 8093 b43765095a6f
permissions -rw-r--r--
Fixed off-by-one error in SDL_ConvertStereo().

Fixes Bugzilla #561.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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) { \
slouken@6210
   296
            src -= 1; \
icculus@1982
   297
            dst -= 2; \
icculus@7410
   298
            dst[0] = dst[1] = *src; \
icculus@1982
   299
        } \
icculus@1982
   300
    }
slouken@0
   301
icculus@1982
   302
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   303
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   304
    case 8:
slouken@1985
   305
        dup_chans_1_to_2(Uint8);
slouken@1985
   306
        break;
slouken@1985
   307
    case 16:
slouken@1985
   308
        dup_chans_1_to_2(Uint16);
slouken@1985
   309
        break;
slouken@1985
   310
    case 32:
slouken@1985
   311
        dup_chans_1_to_2(Uint32);
slouken@1985
   312
        break;
slouken@1895
   313
    }
icculus@1982
   314
slouken@1985
   315
#undef dup_chans_1_to_2
icculus@1982
   316
slouken@1895
   317
    cvt->len_cvt *= 2;
slouken@1895
   318
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   319
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   320
    }
slouken@0
   321
}
slouken@0
   322
slouken@942
   323
slouken@942
   324
/* Duplicate a stereo channel to a pseudo-5.1 stream */
icculus@1982
   325
static void SDLCALL
icculus@1982
   326
SDL_ConvertSurround(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   327
{
slouken@1895
   328
    int i;
slouken@942
   329
slouken@942
   330
#ifdef DEBUG_CONVERT
slouken@1895
   331
    fprintf(stderr, "Converting stereo to surround\n");
slouken@942
   332
#endif
slouken@942
   333
slouken@1985
   334
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   335
    case AUDIO_U8:
slouken@1895
   336
        {
slouken@1895
   337
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   338
slouken@1895
   339
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   340
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 3);
slouken@1895
   341
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   342
                dst -= 6;
slouken@1895
   343
                src -= 2;
slouken@1895
   344
                lf = src[0];
slouken@1895
   345
                rf = src[1];
slouken@1895
   346
                ce = (lf / 2) + (rf / 2);
slouken@1895
   347
                dst[0] = lf;
slouken@1895
   348
                dst[1] = rf;
slouken@1895
   349
                dst[2] = lf - ce;
slouken@1895
   350
                dst[3] = rf - ce;
slouken@1895
   351
                dst[4] = ce;
slouken@1895
   352
                dst[5] = ce;
slouken@1895
   353
            }
slouken@1895
   354
        }
slouken@1895
   355
        break;
slouken@942
   356
slouken@1895
   357
    case AUDIO_S8:
slouken@1895
   358
        {
slouken@1895
   359
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   360
slouken@1895
   361
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   362
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 3;
slouken@1895
   363
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   364
                dst -= 6;
slouken@1895
   365
                src -= 2;
slouken@1895
   366
                lf = src[0];
slouken@1895
   367
                rf = src[1];
slouken@1895
   368
                ce = (lf / 2) + (rf / 2);
slouken@1895
   369
                dst[0] = lf;
slouken@1895
   370
                dst[1] = rf;
slouken@1895
   371
                dst[2] = lf - ce;
slouken@1895
   372
                dst[3] = rf - ce;
slouken@1895
   373
                dst[4] = ce;
slouken@1895
   374
                dst[5] = ce;
slouken@1895
   375
            }
slouken@1895
   376
        }
slouken@1895
   377
        break;
slouken@942
   378
slouken@1895
   379
    case AUDIO_U16:
slouken@1895
   380
        {
slouken@1895
   381
            Uint8 *src, *dst;
slouken@1895
   382
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   383
slouken@1895
   384
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   385
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   386
icculus@1982
   387
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   388
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   389
                    dst -= 12;
slouken@1895
   390
                    src -= 4;
slouken@1895
   391
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   392
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   393
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   394
                    rr = lf - ce;
slouken@1895
   395
                    lr = rf - ce;
slouken@1895
   396
                    dst[1] = (lf & 0xFF);
slouken@1895
   397
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   398
                    dst[3] = (rf & 0xFF);
slouken@1895
   399
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   400
slouken@1895
   401
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   402
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   403
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   404
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   405
slouken@1895
   406
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   407
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   408
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   409
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   410
                }
slouken@1895
   411
            } else {
slouken@1895
   412
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   413
                    dst -= 12;
slouken@1895
   414
                    src -= 4;
slouken@1895
   415
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   416
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   417
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   418
                    rr = lf - ce;
slouken@1895
   419
                    lr = rf - ce;
slouken@1895
   420
                    dst[0] = (lf & 0xFF);
slouken@1895
   421
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   422
                    dst[2] = (rf & 0xFF);
slouken@1895
   423
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   424
slouken@1895
   425
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   426
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   427
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   428
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   429
slouken@1895
   430
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   431
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   432
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   433
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   434
                }
slouken@1895
   435
            }
slouken@1895
   436
        }
slouken@1895
   437
        break;
slouken@942
   438
slouken@1895
   439
    case AUDIO_S16:
slouken@1895
   440
        {
slouken@1895
   441
            Uint8 *src, *dst;
slouken@1895
   442
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   443
slouken@1895
   444
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   445
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   446
icculus@1982
   447
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   448
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   449
                    dst -= 12;
slouken@1895
   450
                    src -= 4;
slouken@1895
   451
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   452
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   453
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   454
                    rr = lf - ce;
slouken@1895
   455
                    lr = rf - ce;
slouken@1895
   456
                    dst[1] = (lf & 0xFF);
slouken@1895
   457
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   458
                    dst[3] = (rf & 0xFF);
slouken@1895
   459
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   460
slouken@1895
   461
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   462
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   463
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   464
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   465
slouken@1895
   466
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   467
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   468
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   469
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   470
                }
slouken@1895
   471
            } else {
slouken@1895
   472
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   473
                    dst -= 12;
slouken@1895
   474
                    src -= 4;
slouken@1895
   475
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   476
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   477
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   478
                    rr = lf - ce;
slouken@1895
   479
                    lr = rf - ce;
slouken@1895
   480
                    dst[0] = (lf & 0xFF);
slouken@1895
   481
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   482
                    dst[2] = (rf & 0xFF);
slouken@1895
   483
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   484
slouken@1895
   485
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   486
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   487
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   488
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   489
slouken@1895
   490
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   491
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   492
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   493
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   494
                }
slouken@1895
   495
            }
slouken@1895
   496
        }
slouken@1895
   497
        break;
icculus@1982
   498
icculus@1982
   499
    case AUDIO_S32:
icculus@1982
   500
        {
icculus@1982
   501
            Sint32 lf, rf, ce;
icculus@1982
   502
            const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
icculus@1982
   503
            Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   504
icculus@1982
   505
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   506
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   507
                    dst -= 6;
icculus@1982
   508
                    src -= 2;
icculus@1982
   509
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   510
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   511
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   512
                    dst[0] = SDL_SwapBE32((Uint32) lf);
icculus@1982
   513
                    dst[1] = SDL_SwapBE32((Uint32) rf);
icculus@1982
   514
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   515
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   516
                    dst[4] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   517
                    dst[5] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   518
                }
icculus@1982
   519
            } else {
icculus@1982
   520
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   521
                    dst -= 6;
icculus@1982
   522
                    src -= 2;
icculus@1982
   523
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   524
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   525
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   526
                    dst[0] = src[0];
icculus@1982
   527
                    dst[1] = src[1];
icculus@1982
   528
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   529
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   530
                    dst[4] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   531
                    dst[5] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   532
                }
icculus@1982
   533
            }
icculus@1982
   534
        }
icculus@1982
   535
        break;
icculus@1982
   536
icculus@1982
   537
    case AUDIO_F32:
icculus@1982
   538
        {
icculus@1982
   539
            float lf, rf, ce;
icculus@2014
   540
            const float *src = (const float *) cvt->buf + cvt->len_cvt;
icculus@2014
   541
            float *dst = (float *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   542
icculus@1982
   543
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   544
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   545
                    dst -= 6;
icculus@1982
   546
                    src -= 2;
icculus@2014
   547
                    lf = SDL_SwapFloatBE(src[0]);
icculus@2014
   548
                    rf = SDL_SwapFloatBE(src[1]);
icculus@1982
   549
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   550
                    dst[0] = src[0];
icculus@1982
   551
                    dst[1] = src[1];
icculus@2014
   552
                    dst[2] = SDL_SwapFloatBE(lf - ce);
icculus@2014
   553
                    dst[3] = SDL_SwapFloatBE(rf - ce);
icculus@2014
   554
                    dst[4] = dst[5] = SDL_SwapFloatBE(ce);
icculus@1982
   555
                }
icculus@1982
   556
            } else {
icculus@1982
   557
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   558
                    dst -= 6;
icculus@1982
   559
                    src -= 2;
icculus@2014
   560
                    lf = SDL_SwapFloatLE(src[0]);
icculus@2014
   561
                    rf = SDL_SwapFloatLE(src[1]);
icculus@1982
   562
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   563
                    dst[0] = src[0];
icculus@1982
   564
                    dst[1] = src[1];
icculus@2014
   565
                    dst[2] = SDL_SwapFloatLE(lf - ce);
icculus@2014
   566
                    dst[3] = SDL_SwapFloatLE(rf - ce);
icculus@2014
   567
                    dst[4] = dst[5] = SDL_SwapFloatLE(ce);
icculus@1982
   568
                }
icculus@1982
   569
            }
icculus@1982
   570
        }
icculus@1982
   571
        break;
icculus@1982
   572
slouken@1895
   573
    }
slouken@1895
   574
    cvt->len_cvt *= 3;
slouken@1895
   575
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   576
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   577
    }
slouken@942
   578
}
slouken@942
   579
slouken@942
   580
slouken@942
   581
/* Duplicate a stereo channel to a pseudo-4.0 stream */
icculus@1982
   582
static void SDLCALL
icculus@1982
   583
SDL_ConvertSurround_4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   584
{
slouken@1895
   585
    int i;
slouken@942
   586
slouken@942
   587
#ifdef DEBUG_CONVERT
slouken@1895
   588
    fprintf(stderr, "Converting stereo to quad\n");
slouken@942
   589
#endif
slouken@942
   590
slouken@1985
   591
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   592
    case AUDIO_U8:
slouken@1895
   593
        {
slouken@1895
   594
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   595
slouken@1895
   596
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   597
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 2);
slouken@1895
   598
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   599
                dst -= 4;
slouken@1895
   600
                src -= 2;
slouken@1895
   601
                lf = src[0];
slouken@1895
   602
                rf = src[1];
slouken@1895
   603
                ce = (lf / 2) + (rf / 2);
slouken@1895
   604
                dst[0] = lf;
slouken@1895
   605
                dst[1] = rf;
slouken@1895
   606
                dst[2] = lf - ce;
slouken@1895
   607
                dst[3] = rf - ce;
slouken@1895
   608
            }
slouken@1895
   609
        }
slouken@1895
   610
        break;
slouken@942
   611
slouken@1895
   612
    case AUDIO_S8:
slouken@1895
   613
        {
slouken@1895
   614
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   615
slouken@1895
   616
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   617
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 2;
slouken@1895
   618
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   619
                dst -= 4;
slouken@1895
   620
                src -= 2;
slouken@1895
   621
                lf = src[0];
slouken@1895
   622
                rf = src[1];
slouken@1895
   623
                ce = (lf / 2) + (rf / 2);
slouken@1895
   624
                dst[0] = lf;
slouken@1895
   625
                dst[1] = rf;
slouken@1895
   626
                dst[2] = lf - ce;
slouken@1895
   627
                dst[3] = rf - ce;
slouken@1895
   628
            }
slouken@1895
   629
        }
slouken@1895
   630
        break;
slouken@942
   631
slouken@1895
   632
    case AUDIO_U16:
slouken@1895
   633
        {
slouken@1895
   634
            Uint8 *src, *dst;
slouken@1895
   635
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   636
slouken@1895
   637
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   638
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   639
icculus@1982
   640
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   641
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   642
                    dst -= 8;
slouken@1895
   643
                    src -= 4;
slouken@1895
   644
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   645
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   646
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   647
                    rr = lf - ce;
slouken@1895
   648
                    lr = rf - ce;
slouken@1895
   649
                    dst[1] = (lf & 0xFF);
slouken@1895
   650
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   651
                    dst[3] = (rf & 0xFF);
slouken@1895
   652
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   653
slouken@1895
   654
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   655
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   656
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   657
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   658
                }
slouken@1895
   659
            } else {
slouken@1895
   660
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   661
                    dst -= 8;
slouken@1895
   662
                    src -= 4;
slouken@1895
   663
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   664
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   665
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   666
                    rr = lf - ce;
slouken@1895
   667
                    lr = rf - ce;
slouken@1895
   668
                    dst[0] = (lf & 0xFF);
slouken@1895
   669
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   670
                    dst[2] = (rf & 0xFF);
slouken@1895
   671
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   672
slouken@1895
   673
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   674
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   675
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   676
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   677
                }
slouken@1895
   678
            }
slouken@1895
   679
        }
slouken@1895
   680
        break;
slouken@942
   681
slouken@1895
   682
    case AUDIO_S16:
slouken@1895
   683
        {
slouken@1895
   684
            Uint8 *src, *dst;
slouken@1895
   685
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   686
slouken@1895
   687
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   688
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   689
icculus@1982
   690
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   691
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   692
                    dst -= 8;
slouken@1895
   693
                    src -= 4;
slouken@1895
   694
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   695
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   696
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   697
                    rr = lf - ce;
slouken@1895
   698
                    lr = rf - ce;
slouken@1895
   699
                    dst[1] = (lf & 0xFF);
slouken@1895
   700
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   701
                    dst[3] = (rf & 0xFF);
slouken@1895
   702
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   703
slouken@1895
   704
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   705
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   706
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   707
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   708
                }
slouken@1895
   709
            } else {
slouken@1895
   710
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   711
                    dst -= 8;
slouken@1895
   712
                    src -= 4;
slouken@1895
   713
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   714
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   715
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   716
                    rr = lf - ce;
slouken@1895
   717
                    lr = rf - ce;
slouken@1895
   718
                    dst[0] = (lf & 0xFF);
slouken@1895
   719
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   720
                    dst[2] = (rf & 0xFF);
slouken@1895
   721
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   722
slouken@1895
   723
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   724
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   725
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   726
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   727
                }
slouken@1895
   728
            }
slouken@1895
   729
        }
slouken@1895
   730
        break;
slouken@942
   731
icculus@1982
   732
    case AUDIO_S32:
icculus@1982
   733
        {
icculus@1982
   734
            const Uint32 *src = (const Uint32 *) (cvt->buf + cvt->len_cvt);
icculus@1982
   735
            Uint32 *dst = (Uint32 *) (cvt->buf + cvt->len_cvt * 2);
icculus@1982
   736
            Sint32 lf, rf, ce;
slouken@942
   737
icculus@1982
   738
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   739
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   740
                    dst -= 4;
icculus@1982
   741
                    src -= 2;
icculus@1982
   742
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   743
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   744
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   745
                    dst[0] = src[0];
icculus@1982
   746
                    dst[1] = src[1];
icculus@1982
   747
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   748
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   749
                }
icculus@1982
   750
            } else {
icculus@1982
   751
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   752
                    dst -= 4;
icculus@1982
   753
                    src -= 2;
icculus@1982
   754
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   755
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   756
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   757
                    dst[0] = src[0];
icculus@1982
   758
                    dst[1] = src[1];
icculus@1982
   759
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   760
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   761
                }
icculus@1982
   762
            }
slouken@1895
   763
        }
slouken@1895
   764
        break;
slouken@1895
   765
    }
slouken@1895
   766
    cvt->len_cvt *= 2;
slouken@1895
   767
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   768
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   769
    }
slouken@0
   770
}
slouken@0
   771
slouken@0
   772
slouken@1895
   773
int
slouken@1895
   774
SDL_ConvertAudio(SDL_AudioCVT * cvt)
slouken@0
   775
{
icculus@3021
   776
    /* !!! FIXME: (cvt) should be const; stack-copy it here. */
icculus@3021
   777
    /* !!! FIXME: (actually, we can't...len_cvt needs to be updated. Grr.) */
icculus@3021
   778
slouken@1895
   779
    /* Make sure there's data to convert */
slouken@1895
   780
    if (cvt->buf == NULL) {
slouken@1895
   781
        SDL_SetError("No buffer allocated for conversion");
slouken@1895
   782
        return (-1);
slouken@1895
   783
    }
slouken@1895
   784
    /* Return okay if no conversion is necessary */
slouken@1895
   785
    cvt->len_cvt = cvt->len;
slouken@1895
   786
    if (cvt->filters[0] == NULL) {
slouken@1895
   787
        return (0);
slouken@1895
   788
    }
slouken@0
   789
slouken@1895
   790
    /* Set up the conversion and go! */
slouken@1895
   791
    cvt->filter_index = 0;
slouken@1895
   792
    cvt->filters[0] (cvt, cvt->src_format);
slouken@1895
   793
    return (0);
slouken@0
   794
}
slouken@0
   795
icculus@1982
   796
icculus@1982
   797
static SDL_AudioFilter
icculus@1982
   798
SDL_HandTunedTypeCVT(SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
   799
{
icculus@1982
   800
    /*
icculus@1982
   801
     * Fill in any future conversions that are specialized to a
icculus@1982
   802
     *  processor, platform, compiler, or library here.
icculus@1982
   803
     */
icculus@1982
   804
slouken@1985
   805
    return NULL;                /* no specialized converter code available. */
icculus@1982
   806
}
icculus@1982
   807
icculus@1982
   808
icculus@1982
   809
/*
icculus@1982
   810
 * Find a converter between two data types. We try to select a hand-tuned
icculus@1982
   811
 *  asm/vectorized/optimized function first, and then fallback to an
icculus@1982
   812
 *  autogenerated function that is customized to convert between two
icculus@1982
   813
 *  specific data types.
icculus@1982
   814
 */
icculus@1982
   815
static int
icculus@1982
   816
SDL_BuildAudioTypeCVT(SDL_AudioCVT * cvt,
icculus@1982
   817
                      SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
   818
{
icculus@1982
   819
    if (src_fmt != dst_fmt) {
icculus@1982
   820
        const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt);
icculus@1982
   821
        const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt);
icculus@1982
   822
        SDL_AudioFilter filter = SDL_HandTunedTypeCVT(src_fmt, dst_fmt);
icculus@1982
   823
icculus@1982
   824
        /* No hand-tuned converter? Try the autogenerated ones. */
icculus@1982
   825
        if (filter == NULL) {
icculus@1982
   826
            int i;
icculus@1982
   827
            for (i = 0; sdl_audio_type_filters[i].filter != NULL; i++) {
icculus@1982
   828
                const SDL_AudioTypeFilters *filt = &sdl_audio_type_filters[i];
icculus@1982
   829
                if ((filt->src_fmt == src_fmt) && (filt->dst_fmt == dst_fmt)) {
icculus@1982
   830
                    filter = filt->filter;
icculus@1982
   831
                    break;
icculus@1982
   832
                }
icculus@1982
   833
            }
icculus@1982
   834
icculus@1982
   835
            if (filter == NULL) {
slouken@3491
   836
                SDL_SetError("No conversion available for these formats");
slouken@3491
   837
                return -1;
icculus@1982
   838
            }
icculus@1982
   839
        }
icculus@1982
   840
icculus@1982
   841
        /* Update (cvt) with filter details... */
icculus@1982
   842
        cvt->filters[cvt->filter_index++] = filter;
icculus@1982
   843
        if (src_bitsize < dst_bitsize) {
icculus@1982
   844
            const int mult = (dst_bitsize / src_bitsize);
icculus@1982
   845
            cvt->len_mult *= mult;
icculus@1982
   846
            cvt->len_ratio *= mult;
icculus@1982
   847
        } else if (src_bitsize > dst_bitsize) {
icculus@1982
   848
            cvt->len_ratio /= (src_bitsize / dst_bitsize);
icculus@1982
   849
        }
icculus@1982
   850
slouken@1985
   851
        return 1;               /* added a converter. */
icculus@1982
   852
    }
icculus@1982
   853
slouken@1985
   854
    return 0;                   /* no conversion necessary. */
icculus@1982
   855
}
icculus@1982
   856
icculus@3021
   857
icculus@3021
   858
static SDL_AudioFilter
icculus@3021
   859
SDL_HandTunedResampleCVT(SDL_AudioCVT * cvt, int dst_channels,
icculus@3021
   860
                         int src_rate, int dst_rate)
slouken@2716
   861
{
icculus@3021
   862
    /*
icculus@3021
   863
     * Fill in any future conversions that are specialized to a
icculus@3021
   864
     *  processor, platform, compiler, or library here.
icculus@3021
   865
     */
slouken@2716
   866
icculus@3021
   867
    return NULL;                /* no specialized converter code available. */
icculus@3021
   868
}
slouken@2716
   869
icculus@3021
   870
static int
icculus@3021
   871
SDL_FindFrequencyMultiple(const int src_rate, const int dst_rate)
icculus@3021
   872
{
icculus@3021
   873
    int retval = 0;
slouken@2716
   874
icculus@3021
   875
    /* If we only built with the arbitrary resamplers, ignore multiples. */
icculus@3021
   876
#if !LESS_RESAMPLERS
icculus@3021
   877
    int lo, hi;
icculus@3021
   878
    int div;
slouken@2716
   879
icculus@6281
   880
    SDL_assert(src_rate != 0);
icculus@6281
   881
    SDL_assert(dst_rate != 0);
icculus@6281
   882
    SDL_assert(src_rate != dst_rate);
slouken@2716
   883
icculus@3021
   884
    if (src_rate < dst_rate) {
icculus@3021
   885
        lo = src_rate;
icculus@3021
   886
        hi = dst_rate;
icculus@3021
   887
    } else {
icculus@3021
   888
        lo = dst_rate;
icculus@3021
   889
        hi = src_rate;
icculus@3021
   890
    }
slouken@2716
   891
icculus@3021
   892
    /* zero means "not a supported multiple" ... we only do 2x and 4x. */
icculus@3021
   893
    if ((hi % lo) != 0)
slouken@3040
   894
        return 0;               /* not a multiple. */
slouken@2716
   895
icculus@3021
   896
    div = hi / lo;
icculus@3021
   897
    retval = ((div == 2) || (div == 4)) ? div : 0;
icculus@3021
   898
#endif
icculus@3021
   899
icculus@3021
   900
    return retval;
icculus@3021
   901
}
icculus@3021
   902
icculus@3021
   903
static int
icculus@3021
   904
SDL_BuildAudioResampleCVT(SDL_AudioCVT * cvt, int dst_channels,
icculus@3021
   905
                          int src_rate, int dst_rate)
icculus@3021
   906
{
icculus@3021
   907
    if (src_rate != dst_rate) {
icculus@3021
   908
        SDL_AudioFilter filter = SDL_HandTunedResampleCVT(cvt, dst_channels,
icculus@3021
   909
                                                          src_rate, dst_rate);
icculus@3021
   910
icculus@3021
   911
        /* No hand-tuned converter? Try the autogenerated ones. */
icculus@3021
   912
        if (filter == NULL) {
icculus@3021
   913
            int i;
icculus@3021
   914
            const int upsample = (src_rate < dst_rate) ? 1 : 0;
slouken@3040
   915
            const int multiple =
slouken@3040
   916
                SDL_FindFrequencyMultiple(src_rate, dst_rate);
icculus@3021
   917
icculus@3021
   918
            for (i = 0; sdl_audio_rate_filters[i].filter != NULL; i++) {
icculus@3021
   919
                const SDL_AudioRateFilters *filt = &sdl_audio_rate_filters[i];
icculus@3021
   920
                if ((filt->fmt == cvt->dst_format) &&
icculus@3021
   921
                    (filt->channels == dst_channels) &&
icculus@3021
   922
                    (filt->upsample == upsample) &&
icculus@3021
   923
                    (filt->multiple == multiple)) {
icculus@3021
   924
                    filter = filt->filter;
icculus@3021
   925
                    break;
icculus@3021
   926
                }
icculus@3021
   927
            }
icculus@3021
   928
icculus@3021
   929
            if (filter == NULL) {
slouken@3491
   930
                SDL_SetError("No conversion available for these rates");
slouken@3491
   931
                return -1;
icculus@3021
   932
            }
slouken@2716
   933
        }
slouken@2716
   934
icculus@3021
   935
        /* Update (cvt) with filter details... */
icculus@3021
   936
        cvt->filters[cvt->filter_index++] = filter;
icculus@3021
   937
        if (src_rate < dst_rate) {
icculus@3021
   938
            const double mult = ((double) dst_rate) / ((double) src_rate);
slouken@3032
   939
            cvt->len_mult *= (int) SDL_ceil(mult);
icculus@3021
   940
            cvt->len_ratio *= mult;
icculus@3021
   941
        } else {
icculus@3021
   942
            cvt->len_ratio /= ((double) src_rate) / ((double) dst_rate);
slouken@2716
   943
        }
icculus@3021
   944
icculus@3021
   945
        return 1;               /* added a converter. */
slouken@2716
   946
    }
slouken@2716
   947
icculus@3021
   948
    return 0;                   /* no conversion necessary. */
slouken@2716
   949
}
icculus@1982
   950
icculus@1982
   951
icculus@1982
   952
/* Creates a set of audio filters to convert from one format to another.
icculus@1982
   953
   Returns -1 if the format conversion is not supported, 0 if there's
icculus@1982
   954
   no conversion needed, or 1 if the audio filter is set up.
slouken@0
   955
*/
slouken@1895
   956
slouken@1895
   957
int
slouken@1895
   958
SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
icculus@1982
   959
                  SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate,
icculus@1982
   960
                  SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate)
slouken@0
   961
{
icculus@3021
   962
    /*
icculus@3021
   963
     * !!! FIXME: reorder filters based on which grow/shrink the buffer.
icculus@3021
   964
     * !!! FIXME: ideally, we should do everything that shrinks the buffer
icculus@3021
   965
     * !!! FIXME: first, so we don't have to process as many bytes in a given
icculus@3021
   966
     * !!! FIXME: filter and abuse the CPU cache less. This might not be as
icculus@3021
   967
     * !!! FIXME: good in practice as it sounds in theory, though.
icculus@3021
   968
     */
icculus@3021
   969
aschiffler@6819
   970
    /* Sanity check target pointer */
aschiffler@6819
   971
    if (cvt == NULL) {
icculus@7037
   972
        return SDL_InvalidParamError("cvt");
aschiffler@6819
   973
    }
slouken@7191
   974
slouken@3491
   975
    /* there are no unsigned types over 16 bits, so catch this up front. */
icculus@1982
   976
    if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
icculus@7037
   977
        return SDL_SetError("Invalid source format");
icculus@1982
   978
    }
icculus@1982
   979
    if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
icculus@7037
   980
        return SDL_SetError("Invalid destination format");
icculus@1982
   981
    }
icculus@3021
   982
icculus@3021
   983
    /* prevent possible divisions by zero, etc. */
aschiffler@6819
   984
    if ((src_channels == 0) || (dst_channels == 0)) {
icculus@7037
   985
        return SDL_SetError("Source or destination channels is zero");
aschiffler@6819
   986
    }
icculus@3021
   987
    if ((src_rate == 0) || (dst_rate == 0)) {
icculus@7037
   988
        return SDL_SetError("Source or destination rate is zero");
icculus@3021
   989
    }
slouken@1985
   990
#ifdef DEBUG_CONVERT
icculus@1982
   991
    printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
slouken@1985
   992
           src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
slouken@1985
   993
#endif
icculus@1982
   994
slouken@1895
   995
    /* Start off with no conversion necessary */
icculus@2879
   996
    SDL_zerop(cvt);
icculus@1982
   997
    cvt->src_format = src_fmt;
icculus@1982
   998
    cvt->dst_format = dst_fmt;
slouken@1895
   999
    cvt->needed = 0;
slouken@1895
  1000
    cvt->filter_index = 0;
slouken@1895
  1001
    cvt->filters[0] = NULL;
slouken@1895
  1002
    cvt->len_mult = 1;
slouken@1895
  1003
    cvt->len_ratio = 1.0;
icculus@3021
  1004
    cvt->rate_incr = ((double) dst_rate) / ((double) src_rate);
slouken@0
  1005
icculus@1982
  1006
    /* Convert data types, if necessary. Updates (cvt). */
icculus@3021
  1007
    if (SDL_BuildAudioTypeCVT(cvt, src_fmt, dst_fmt) == -1) {
slouken@1985
  1008
        return -1;              /* shouldn't happen, but just in case... */
icculus@3021
  1009
    }
slouken@0
  1010
icculus@1982
  1011
    /* Channel conversion */
slouken@1895
  1012
    if (src_channels != dst_channels) {
slouken@1895
  1013
        if ((src_channels == 1) && (dst_channels > 1)) {
slouken@1895
  1014
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1015
            cvt->len_mult *= 2;
slouken@1895
  1016
            src_channels = 2;
slouken@1895
  1017
            cvt->len_ratio *= 2;
slouken@1895
  1018
        }
slouken@1895
  1019
        if ((src_channels == 2) && (dst_channels == 6)) {
slouken@1895
  1020
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround;
slouken@1895
  1021
            src_channels = 6;
slouken@1895
  1022
            cvt->len_mult *= 3;
slouken@1895
  1023
            cvt->len_ratio *= 3;
slouken@1895
  1024
        }
slouken@1895
  1025
        if ((src_channels == 2) && (dst_channels == 4)) {
slouken@1895
  1026
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround_4;
slouken@1895
  1027
            src_channels = 4;
slouken@1895
  1028
            cvt->len_mult *= 2;
slouken@1895
  1029
            cvt->len_ratio *= 2;
slouken@1895
  1030
        }
slouken@1895
  1031
        while ((src_channels * 2) <= dst_channels) {
slouken@1895
  1032
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1033
            cvt->len_mult *= 2;
slouken@1895
  1034
            src_channels *= 2;
slouken@1895
  1035
            cvt->len_ratio *= 2;
slouken@1895
  1036
        }
slouken@1895
  1037
        if ((src_channels == 6) && (dst_channels <= 2)) {
slouken@1895
  1038
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip;
slouken@1895
  1039
            src_channels = 2;
slouken@1895
  1040
            cvt->len_ratio /= 3;
slouken@1895
  1041
        }
slouken@1895
  1042
        if ((src_channels == 6) && (dst_channels == 4)) {
slouken@1895
  1043
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip_2;
slouken@1895
  1044
            src_channels = 4;
slouken@1895
  1045
            cvt->len_ratio /= 2;
slouken@1895
  1046
        }
slouken@1895
  1047
        /* This assumes that 4 channel audio is in the format:
slouken@1895
  1048
           Left {front/back} + Right {front/back}
slouken@1895
  1049
           so converting to L/R stereo works properly.
slouken@1895
  1050
         */
slouken@1895
  1051
        while (((src_channels % 2) == 0) &&
slouken@1895
  1052
               ((src_channels / 2) >= dst_channels)) {
slouken@1895
  1053
            cvt->filters[cvt->filter_index++] = SDL_ConvertMono;
slouken@1895
  1054
            src_channels /= 2;
slouken@1895
  1055
            cvt->len_ratio /= 2;
slouken@1895
  1056
        }
slouken@1895
  1057
        if (src_channels != dst_channels) {
slouken@1895
  1058
            /* Uh oh.. */ ;
slouken@1895
  1059
        }
slouken@1895
  1060
    }
slouken@0
  1061
icculus@3021
  1062
    /* Do rate conversion, if necessary. Updates (cvt). */
slouken@3040
  1063
    if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) ==
slouken@3040
  1064
        -1) {
icculus@3021
  1065
        return -1;              /* shouldn't happen, but just in case... */
slouken@2716
  1066
    }
slouken@2716
  1067
slouken@1895
  1068
    /* Set up the filter information */
slouken@1895
  1069
    if (cvt->filter_index != 0) {
slouken@1895
  1070
        cvt->needed = 1;
icculus@1982
  1071
        cvt->src_format = src_fmt;
icculus@1982
  1072
        cvt->dst_format = dst_fmt;
slouken@1895
  1073
        cvt->len = 0;
slouken@1895
  1074
        cvt->buf = NULL;
slouken@1895
  1075
        cvt->filters[cvt->filter_index] = NULL;
slouken@1895
  1076
    }
slouken@1895
  1077
    return (cvt->needed);
slouken@0
  1078
}
slouken@1895
  1079
slouken@2716
  1080
slouken@1895
  1081
/* vi: set ts=4 sw=4 expandtab: */