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