src/audio/SDL_wave.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 11 Jun 2019 02:08:31 -0400
changeset 12825 4fb5e61382b7
parent 12814 cee49246e1bc
child 12842 a66900915e4e
permissions -rw-r--r--
windows: Don't let Visual Studio insert an implicit dependency on memset().

Fixes Bugzilla #4662.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 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
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@12806
    23
#ifdef HAVE_LIMITS_H
slouken@12806
    24
#include <limits.h>
slouken@12806
    25
#else
slouken@12806
    26
#ifndef SIZE_MAX
slouken@12806
    27
#define SIZE_MAX ((size_t)-1)
slouken@12806
    28
#endif
slouken@12806
    29
#ifndef INT_MAX
slouken@12806
    30
/* Make a lucky guess. */
slouken@12809
    31
#define INT_MAX SDL_MAX_SINT32
slouken@12806
    32
#endif
slouken@12806
    33
#endif
slouken@12806
    34
slouken@0
    35
/* Microsoft WAVE file loading routines */
slouken@0
    36
slouken@12806
    37
#include "SDL_log.h"
slouken@12806
    38
#include "SDL_hints.h"
slouken@0
    39
#include "SDL_audio.h"
slouken@0
    40
#include "SDL_wave.h"
slouken@0
    41
slouken@12806
    42
/* Reads the value stored at the location of the f1 pointer, multiplies it
slouken@12809
    43
 * with the second argument and then stores the result to f1.
slouken@12809
    44
 * Returns 0 on success, or -1 if the multiplication overflows, in which case f1
slouken@12809
    45
 * does not get modified.
slouken@12806
    46
 */
slouken@12809
    47
static int
slouken@12809
    48
SafeMult(size_t *f1, size_t f2)
slouken@12806
    49
{
slouken@12806
    50
    if (*f1 > 0 && SIZE_MAX / *f1 <= f2) {
slouken@12809
    51
        return -1;
slouken@12806
    52
    }
slouken@12806
    53
    *f1 *= f2;
slouken@12809
    54
    return 0;
slouken@12806
    55
}
slouken@0
    56
slouken@12806
    57
typedef struct ADPCM_DecoderState
slouken@12806
    58
{
slouken@12806
    59
    Uint32 channels;        /* Number of channels. */
slouken@12806
    60
    size_t blocksize;       /* Size of an ADPCM block in bytes. */
slouken@12806
    61
    size_t blockheadersize; /* Size of an ADPCM block header in bytes. */
slouken@12806
    62
    size_t samplesperblock; /* Number of samples per channel in an ADPCM block. */
slouken@12806
    63
    size_t framesize;       /* Size of a sample frame (16-bit PCM) in bytes. */
slouken@12806
    64
    Sint64 framestotal;     /* Total number of sample frames. */
slouken@12806
    65
    Sint64 framesleft;      /* Number of sample frames still to be decoded. */
slouken@12806
    66
    void *ddata;            /* Decoder data from initialization. */
slouken@12806
    67
    void *cstate;           /* Decoding state for each channel. */
slouken@0
    68
slouken@12806
    69
    /* ADPCM data. */
slouken@12806
    70
    struct {
slouken@12806
    71
        Uint8 *data;
slouken@12806
    72
        size_t size;
slouken@12806
    73
        size_t pos;
slouken@12806
    74
    } input;
slouken@12806
    75
slouken@12806
    76
    /* Current ADPCM block in the ADPCM data above. */
slouken@12806
    77
    struct {
slouken@12806
    78
        Uint8 *data;
slouken@12806
    79
        size_t size;
slouken@12806
    80
        size_t pos;
slouken@12806
    81
    } block;
slouken@12806
    82
slouken@12806
    83
    /* Decoded 16-bit PCM data. */
slouken@12806
    84
    struct {
slouken@12806
    85
        Sint16 *data;
slouken@12806
    86
        size_t size;
slouken@12806
    87
        size_t pos;
slouken@12806
    88
    } output;
slouken@12806
    89
} ADPCM_DecoderState;
slouken@12806
    90
slouken@12806
    91
typedef struct MS_ADPCM_CoeffData
slouken@1895
    92
{
slouken@12806
    93
    Uint16 coeffcount;
slouken@12806
    94
    Sint16 *coeff;
slouken@12806
    95
    Sint16 aligndummy; /* Has to be last member. */
slouken@12806
    96
} MS_ADPCM_CoeffData;
slouken@12806
    97
slouken@12806
    98
typedef struct MS_ADPCM_ChannelState
slouken@1895
    99
{
slouken@12806
   100
    Uint16 delta;
slouken@12806
   101
    Sint16 coeff1;
slouken@12806
   102
    Sint16 coeff2;
slouken@12806
   103
} MS_ADPCM_ChannelState;
slouken@12806
   104
slouken@12806
   105
#ifdef SDL_WAVE_DEBUG_LOG_FORMAT
slouken@12806
   106
static void
slouken@12806
   107
WaveDebugLogFormat(WaveFile *file)
slouken@12806
   108
{
slouken@12806
   109
    WaveFormat *format = &file->format;
slouken@12806
   110
    const char *fmtstr = "WAVE file: %s, %u Hz, %s, %u bits, %u %s/s";
slouken@12806
   111
    const char *waveformat, *wavechannel, *wavebpsunit = "B";
slouken@12806
   112
    Uint32 wavebps = format->byterate;
slouken@12814
   113
    char channelstr[64];
slouken@12814
   114
slouken@12814
   115
    SDL_zero(channelstr);
slouken@12806
   116
slouken@12806
   117
    switch (format->encoding) {
slouken@12806
   118
    case PCM_CODE:
slouken@12806
   119
        waveformat = "PCM";
slouken@12806
   120
        break;
slouken@12806
   121
    case IEEE_FLOAT_CODE:
slouken@12806
   122
        waveformat = "IEEE Float";
slouken@12806
   123
        break;
slouken@12806
   124
    case ALAW_CODE:
slouken@12806
   125
        waveformat = "A-law";
slouken@12806
   126
        break;
slouken@12806
   127
    case MULAW_CODE:
slouken@12806
   128
        waveformat = "\xc2\xb5-law";
slouken@12806
   129
        break;
slouken@12806
   130
    case MS_ADPCM_CODE:
slouken@12806
   131
        waveformat = "MS ADPCM";
slouken@12806
   132
        break;
slouken@12806
   133
    case IMA_ADPCM_CODE:
slouken@12806
   134
        waveformat = "IMA ADPCM";
slouken@12806
   135
        break;
slouken@12806
   136
    default:
slouken@12806
   137
        waveformat = "Unknown";
slouken@12806
   138
        break;
slouken@12806
   139
    }
slouken@12806
   140
slouken@12806
   141
#define SDL_WAVE_DEBUG_CHANNELCFG(STR, CODE) case CODE: wavechannel = STR; break;
slouken@12806
   142
#define SDL_WAVE_DEBUG_CHANNELSTR(STR, CODE) if (format->channelmask & CODE) { \
slouken@12806
   143
    SDL_strlcat(channelstr, channelstr[0] ? "-" STR : STR, sizeof(channelstr));}
slouken@12806
   144
slouken@12806
   145
    if (format->formattag == EXTENSIBLE_CODE && format->channelmask > 0) {
slouken@12806
   146
        switch (format->channelmask) {
slouken@12806
   147
            SDL_WAVE_DEBUG_CHANNELCFG("1.0 Mono",         0x4)
slouken@12806
   148
            SDL_WAVE_DEBUG_CHANNELCFG("1.1 Mono",         0xc)
slouken@12806
   149
            SDL_WAVE_DEBUG_CHANNELCFG("2.0 Stereo",       0x3)
slouken@12806
   150
            SDL_WAVE_DEBUG_CHANNELCFG("2.1 Stereo",       0xb)
slouken@12806
   151
            SDL_WAVE_DEBUG_CHANNELCFG("3.0 Stereo",       0x7)
slouken@12806
   152
            SDL_WAVE_DEBUG_CHANNELCFG("3.1 Stereo",       0xf)
slouken@12806
   153
            SDL_WAVE_DEBUG_CHANNELCFG("3.0 Surround",     0x103)
slouken@12806
   154
            SDL_WAVE_DEBUG_CHANNELCFG("3.1 Surround",     0x10b)
slouken@12806
   155
            SDL_WAVE_DEBUG_CHANNELCFG("4.0 Quad",         0x33)
slouken@12806
   156
            SDL_WAVE_DEBUG_CHANNELCFG("4.1 Quad",         0x3b)
slouken@12806
   157
            SDL_WAVE_DEBUG_CHANNELCFG("4.0 Surround",     0x107)
slouken@12806
   158
            SDL_WAVE_DEBUG_CHANNELCFG("4.1 Surround",     0x10f)
slouken@12806
   159
            SDL_WAVE_DEBUG_CHANNELCFG("5.0",              0x37)
slouken@12806
   160
            SDL_WAVE_DEBUG_CHANNELCFG("5.1",              0x3f)
slouken@12806
   161
            SDL_WAVE_DEBUG_CHANNELCFG("5.0 Side",         0x607)
slouken@12806
   162
            SDL_WAVE_DEBUG_CHANNELCFG("5.1 Side",         0x60f)
slouken@12806
   163
            SDL_WAVE_DEBUG_CHANNELCFG("6.0",              0x137)
slouken@12806
   164
            SDL_WAVE_DEBUG_CHANNELCFG("6.1",              0x13f)
slouken@12806
   165
            SDL_WAVE_DEBUG_CHANNELCFG("6.0 Side",         0x707)
slouken@12806
   166
            SDL_WAVE_DEBUG_CHANNELCFG("6.1 Side",         0x70f)
slouken@12806
   167
            SDL_WAVE_DEBUG_CHANNELCFG("7.0",              0xf7)
slouken@12806
   168
            SDL_WAVE_DEBUG_CHANNELCFG("7.1",              0xff)
slouken@12806
   169
            SDL_WAVE_DEBUG_CHANNELCFG("7.0 Side",         0x6c7)
slouken@12806
   170
            SDL_WAVE_DEBUG_CHANNELCFG("7.1 Side",         0x6cf)
slouken@12806
   171
            SDL_WAVE_DEBUG_CHANNELCFG("7.0 Surround",     0x637)
slouken@12806
   172
            SDL_WAVE_DEBUG_CHANNELCFG("7.1 Surround",     0x63f)
slouken@12806
   173
            SDL_WAVE_DEBUG_CHANNELCFG("9.0 Surround",     0x5637)
slouken@12806
   174
            SDL_WAVE_DEBUG_CHANNELCFG("9.1 Surround",     0x563f)
slouken@12806
   175
            SDL_WAVE_DEBUG_CHANNELCFG("11.0 Surround",    0x56f7)
slouken@12806
   176
            SDL_WAVE_DEBUG_CHANNELCFG("11.1 Surround",    0x56ff)
slouken@12806
   177
        default:
slouken@12806
   178
            SDL_WAVE_DEBUG_CHANNELSTR("FL",  0x1)
slouken@12806
   179
            SDL_WAVE_DEBUG_CHANNELSTR("FR",  0x2)
slouken@12806
   180
            SDL_WAVE_DEBUG_CHANNELSTR("FC",  0x4)
slouken@12806
   181
            SDL_WAVE_DEBUG_CHANNELSTR("LF",  0x8)
slouken@12806
   182
            SDL_WAVE_DEBUG_CHANNELSTR("BL",  0x10)
slouken@12806
   183
            SDL_WAVE_DEBUG_CHANNELSTR("BR",  0x20)
slouken@12806
   184
            SDL_WAVE_DEBUG_CHANNELSTR("FLC", 0x40)
slouken@12806
   185
            SDL_WAVE_DEBUG_CHANNELSTR("FRC", 0x80)
slouken@12806
   186
            SDL_WAVE_DEBUG_CHANNELSTR("BC",  0x100)
slouken@12806
   187
            SDL_WAVE_DEBUG_CHANNELSTR("SL",  0x200)
slouken@12806
   188
            SDL_WAVE_DEBUG_CHANNELSTR("SR",  0x400)
slouken@12806
   189
            SDL_WAVE_DEBUG_CHANNELSTR("TC",  0x800)
slouken@12806
   190
            SDL_WAVE_DEBUG_CHANNELSTR("TFL", 0x1000)
slouken@12806
   191
            SDL_WAVE_DEBUG_CHANNELSTR("TFC", 0x2000)
slouken@12806
   192
            SDL_WAVE_DEBUG_CHANNELSTR("TFR", 0x4000)
slouken@12806
   193
            SDL_WAVE_DEBUG_CHANNELSTR("TBL", 0x8000)
slouken@12806
   194
            SDL_WAVE_DEBUG_CHANNELSTR("TBC", 0x10000)
slouken@12806
   195
            SDL_WAVE_DEBUG_CHANNELSTR("TBR", 0x20000)
slouken@12806
   196
            break;
slouken@12806
   197
        }
slouken@12806
   198
    } else {
slouken@12806
   199
        switch (format->channels) {
slouken@12806
   200
        default:
slouken@12806
   201
            if (SDL_snprintf(channelstr, sizeof(channelstr), "%u channels", format->channels) >= 0) {
slouken@12806
   202
                wavechannel = channelstr;
slouken@12806
   203
                break;
slouken@12806
   204
            }
slouken@12806
   205
        case 0:
slouken@12806
   206
            wavechannel = "Unknown";
slouken@12806
   207
            break;
slouken@12806
   208
        case 1:
slouken@12806
   209
            wavechannel = "Mono";
slouken@12806
   210
            break;
slouken@12806
   211
        case 2:
slouken@12806
   212
            wavechannel = "Setero";
slouken@12806
   213
            break;
slouken@12806
   214
        }
slouken@12806
   215
    }
slouken@12806
   216
slouken@12806
   217
#undef SDL_WAVE_DEBUG_CHANNELCFG
slouken@12806
   218
#undef SDL_WAVE_DEBUG_CHANNELSTR
slouken@12806
   219
slouken@12806
   220
    if (wavebps >= 1024) {
slouken@12806
   221
        wavebpsunit = "KiB";
slouken@12806
   222
        wavebps = wavebps / 1024 + (wavebps & 0x3ff ? 1 : 0);
slouken@12806
   223
    }
slouken@12806
   224
slouken@12806
   225
    SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, fmtstr, waveformat, format->frequency, wavechannel, format->bitspersample, wavebps, wavebpsunit);
slouken@12806
   226
}
slouken@12806
   227
#endif
slouken@12806
   228
slouken@12806
   229
#ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
slouken@12806
   230
static void
slouken@12806
   231
WaveDebugDumpFormat(WaveFile *file, Uint32 rifflen, Uint32 fmtlen, Uint32 datalen)
slouken@12806
   232
{
slouken@12806
   233
    WaveFormat *format = &file->format;
slouken@12806
   234
    const char *fmtstr1 = "WAVE chunk dump:\n"
slouken@12806
   235
        "-------------------------------------------\n"
slouken@12806
   236
        "RIFF                            %11u\n"
slouken@12806
   237
        "-------------------------------------------\n"
slouken@12806
   238
        "    fmt                         %11u\n"
slouken@12806
   239
        "        wFormatTag                   0x%04x\n"
slouken@12806
   240
        "        nChannels               %11u\n"
slouken@12806
   241
        "        nSamplesPerSec          %11u\n"
slouken@12806
   242
        "        nAvgBytesPerSec         %11u\n"
slouken@12806
   243
        "        nBlockAlign             %11u\n";
slouken@12806
   244
    const char *fmtstr2 = "        wBitsPerSample          %11u\n";
slouken@12806
   245
    const char *fmtstr3 = "        cbSize                  %11u\n";
slouken@12806
   246
    const char *fmtstr4a = "        wValidBitsPerSample     %11u\n";
slouken@12806
   247
    const char *fmtstr4b = "        wSamplesPerBlock        %11u\n";
slouken@12806
   248
    const char *fmtstr5 = "        dwChannelMask            0x%08x\n"
slouken@12806
   249
        "        SubFormat\n"
slouken@12806
   250
        "        %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n";
slouken@12806
   251
    const char *fmtstr6 = "-------------------------------------------\n"
slouken@12806
   252
        " fact\n"
slouken@12806
   253
        "  dwSampleLength                %11u\n";
slouken@12806
   254
    const char *fmtstr7 = "-------------------------------------------\n"
slouken@12806
   255
        " data                           %11u\n"
slouken@12806
   256
        "-------------------------------------------\n";
slouken@12806
   257
    char *dumpstr;
slouken@12806
   258
    size_t dumppos = 0;
slouken@12806
   259
    const size_t bufsize = 1024;
slouken@12806
   260
    int res;
slouken@12806
   261
slouken@12806
   262
    dumpstr = SDL_malloc(bufsize);
slouken@12806
   263
    if (dumpstr == NULL) {
slouken@12806
   264
        return;
slouken@12806
   265
    }
slouken@12806
   266
    dumpstr[0] = 0;
slouken@12806
   267
slouken@12806
   268
    res = SDL_snprintf(dumpstr, bufsize, fmtstr1, rifflen, fmtlen, format->formattag, format->channels, format->frequency, format->byterate, format->blockalign);
slouken@12806
   269
    dumppos += res > 0 ? res : 0;
slouken@12806
   270
    if (fmtlen >= 16) {
slouken@12806
   271
        res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr2, format->bitspersample);
slouken@12806
   272
        dumppos += res > 0 ? res : 0;
slouken@12806
   273
    }
slouken@12806
   274
    if (fmtlen >= 18) {
slouken@12806
   275
        res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr3, format->extsize);
slouken@12806
   276
        dumppos += res > 0 ? res : 0;
slouken@12806
   277
    }
slouken@12806
   278
    if (format->formattag == EXTENSIBLE_CODE && fmtlen >= 40 && format->extsize >= 22) {
slouken@12806
   279
        const Uint8 *g = format->subformat;
slouken@12806
   280
        const Uint32 g1 = g[0] | ((Uint32)g[1] << 8) | ((Uint32)g[2] << 16) | ((Uint32)g[3] << 24);
slouken@12806
   281
        const Uint32 g2 = g[4] | ((Uint32)g[5] << 8);
slouken@12806
   282
        const Uint32 g3 = g[6] | ((Uint32)g[7] << 8);
slouken@12806
   283
slouken@12806
   284
        switch (format->encoding) {
slouken@12806
   285
        default:
slouken@12806
   286
            res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4a, format->validsamplebits);
slouken@12806
   287
            dumppos += res > 0 ? res : 0;
slouken@12806
   288
            break;
slouken@12806
   289
        case MS_ADPCM_CODE:
slouken@12806
   290
        case IMA_ADPCM_CODE:
slouken@12806
   291
            res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4b, format->samplesperblock);
slouken@12806
   292
            dumppos += res > 0 ? res : 0;
slouken@12806
   293
            break;
slouken@12806
   294
        }
slouken@12806
   295
        res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr5, format->channelmask, g1, g2, g3, g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15]);
slouken@12806
   296
        dumppos += res > 0 ? res : 0;
slouken@12806
   297
    } else {
slouken@12806
   298
        switch (format->encoding) {
slouken@12806
   299
        case MS_ADPCM_CODE:
slouken@12806
   300
        case IMA_ADPCM_CODE:
slouken@12806
   301
            if (fmtlen >= 20 && format->extsize >= 2) {
slouken@12806
   302
                res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4b, format->samplesperblock);
slouken@12806
   303
                dumppos += res > 0 ? res : 0;
slouken@12806
   304
            }
slouken@12806
   305
            break;
slouken@12806
   306
        }
slouken@12806
   307
    }
slouken@12806
   308
    if (file->fact.status >= 1) {
slouken@12806
   309
        res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr6, file->fact.samplelength);
slouken@12806
   310
        dumppos += res > 0 ? res : 0;
slouken@12806
   311
    }
slouken@12806
   312
    res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr7, datalen);
slouken@12806
   313
    dumppos += res > 0 ? res : 0;
slouken@12806
   314
slouken@12806
   315
    SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", dumpstr);
slouken@12806
   316
slouken@12806
   317
    free(dumpstr);
slouken@12806
   318
}
slouken@12806
   319
#endif
slouken@12806
   320
slouken@12806
   321
static Sint64
slouken@12806
   322
WaveAdjustToFactValue(WaveFile *file, Sint64 sampleframes)
slouken@12806
   323
{
slouken@12806
   324
    if (file->fact.status == 2) {
slouken@12806
   325
        if (file->facthint == FactStrict && sampleframes < file->fact.samplelength) {
slouken@12806
   326
            return SDL_SetError("Invalid number of sample frames in WAVE fact chunk (too many)");
slouken@12806
   327
        } else if (sampleframes > file->fact.samplelength) {
slouken@12806
   328
            return file->fact.samplelength;
slouken@12806
   329
        }
slouken@12806
   330
    }
slouken@12806
   331
slouken@12806
   332
    return sampleframes;
slouken@12806
   333
}
slouken@0
   334
slouken@1895
   335
static int
slouken@12806
   336
MS_ADPCM_CalculateSampleFrames(WaveFile *file, size_t datalength)
slouken@0
   337
{
slouken@12806
   338
    WaveFormat *format = &file->format;
slouken@12809
   339
    const size_t blockheadersize = (size_t)file->format.channels * 7;
slouken@12806
   340
    const size_t availableblocks = datalength / file->format.blockalign;
slouken@12809
   341
    const size_t blockframebitsize = (size_t)file->format.bitspersample * file->format.channels;
slouken@12806
   342
    const size_t trailingdata = datalength % file->format.blockalign;
slouken@0
   343
slouken@12806
   344
    if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
slouken@12806
   345
        /* The size of the data chunk must be a multiple of the block size. */
slouken@12806
   346
        if (datalength < blockheadersize || trailingdata > 0) {
slouken@12806
   347
            return SDL_SetError("Truncated MS ADPCM block");
slouken@12806
   348
        }
slouken@1895
   349
    }
slouken@12806
   350
slouken@12806
   351
    /* Calculate number of sample frames that will be decoded. */
slouken@12806
   352
    file->sampleframes = (Sint64)availableblocks * format->samplesperblock;
slouken@12806
   353
    if (trailingdata > 0) {
slouken@12806
   354
        /* The last block is truncated. Check if we can get any samples out of it. */
slouken@12806
   355
        if (file->trunchint == TruncDropFrame) {
slouken@12806
   356
            /* Drop incomplete sample frame. */
slouken@12806
   357
            if (trailingdata >= blockheadersize) {
slouken@12806
   358
                size_t trailingsamples = 2 + (trailingdata - blockheadersize) * 8 / blockframebitsize;
slouken@12806
   359
                if (trailingsamples > format->samplesperblock) {
slouken@12806
   360
                    trailingsamples = format->samplesperblock;
slouken@12806
   361
                }
slouken@12806
   362
                file->sampleframes += trailingsamples;
slouken@12806
   363
            }
slouken@12806
   364
        }
slouken@1895
   365
    }
slouken@12806
   366
slouken@12806
   367
    file->sampleframes = WaveAdjustToFactValue(file, file->sampleframes);
slouken@12806
   368
    if (file->sampleframes < 0) {
slouken@12806
   369
        return -1;
slouken@1895
   370
    }
slouken@12806
   371
slouken@12806
   372
    return 0;
slouken@0
   373
}
slouken@0
   374
slouken@12806
   375
static int
slouken@12806
   376
MS_ADPCM_Init(WaveFile *file, size_t datalength)
slouken@0
   377
{
slouken@12806
   378
    WaveFormat *format = &file->format;
slouken@12806
   379
    WaveChunk *chunk = &file->chunk;
slouken@12809
   380
    const size_t blockheadersize = (size_t)format->channels * 7;
slouken@12806
   381
    const size_t blockdatasize = (size_t)format->blockalign - blockheadersize;
slouken@12809
   382
    const size_t blockframebitsize = (size_t)format->bitspersample * format->channels;
slouken@12806
   383
    const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
slouken@12806
   384
    const Sint16 presetcoeffs[14] = {256, 0, 512, -256, 0, 0, 192, 64, 240, 0, 460, -208, 392, -232};
slouken@12806
   385
    size_t i, coeffcount;
slouken@12806
   386
    MS_ADPCM_CoeffData *coeffdata;
slouken@12806
   387
slouken@12806
   388
    /* Sanity checks. */
slouken@12806
   389
slouken@12806
   390
    /* While it's clear how IMA ADPCM handles more than two channels, the nibble
slouken@12806
   391
     * order of MS ADPCM makes it awkward. The Standards Update does not talk
slouken@12806
   392
     * about supporting more than stereo anyway.
slouken@12806
   393
     */
slouken@12806
   394
    if (format->channels > 2) {
slouken@12806
   395
        return SDL_SetError("Invalid number of channels");
slouken@12806
   396
    }
slouken@12806
   397
slouken@12806
   398
    if (format->bitspersample != 4) {
slouken@12809
   399
        return SDL_SetError("Invalid MS ADPCM bits per sample of %u", (unsigned int)format->bitspersample);
slouken@12806
   400
    }
slouken@12806
   401
slouken@12806
   402
    /* The block size must be big enough to contain the block header. */
slouken@12806
   403
    if (format->blockalign < blockheadersize) {
slouken@12806
   404
        return SDL_SetError("Invalid MS ADPCM block size (nBlockAlign)");
slouken@12806
   405
    }
slouken@12806
   406
slouken@12806
   407
    if (format->formattag == EXTENSIBLE_CODE) {
slouken@12806
   408
        /* Does have a GUID (like all format tags), but there's no specification
slouken@12806
   409
         * for how the data is packed into the extensible header. Making
slouken@12806
   410
         * assumptions here could lead to new formats nobody wants to support.
slouken@12806
   411
         */
slouken@12806
   412
        return SDL_SetError("MS ADPCM with the extensible header is not supported");
slouken@12806
   413
    }
slouken@12806
   414
slouken@12806
   415
    /* There are wSamplesPerBlock, wNumCoef, and at least 7 coefficient pairs in
slouken@12806
   416
     * the extended part of the header.
slouken@12806
   417
     */
slouken@12806
   418
    if (chunk->size < 22) {
slouken@12806
   419
        return SDL_SetError("Could not read MS ADPCM format header");
slouken@12806
   420
    }
slouken@12806
   421
slouken@12806
   422
    format->samplesperblock = chunk->data[18] | ((Uint16)chunk->data[19] << 8);
slouken@12806
   423
    /* Number of coefficient pairs. A pair has two 16-bit integers. */
slouken@12806
   424
    coeffcount = chunk->data[20] | ((size_t)chunk->data[21] << 8);
slouken@12806
   425
    /* bPredictor, the integer offset into the coefficients array, is only
slouken@12806
   426
     * 8 bits. It can only address the first 256 coefficients. Let's limit
slouken@12806
   427
     * the count number here.
slouken@12806
   428
     */
slouken@12806
   429
    if (coeffcount > 256) {
slouken@12806
   430
        coeffcount = 256;
slouken@12806
   431
    }
slouken@12806
   432
slouken@12806
   433
    if (chunk->size < 22 + coeffcount * 4) {
slouken@12806
   434
        return SDL_SetError("Could not read custom coefficients in MS ADPCM format header");
slouken@12806
   435
    } else if (format->extsize < 4 + coeffcount * 4) {
slouken@12806
   436
        return SDL_SetError("Invalid MS ADPCM format header (too small)");
slouken@12806
   437
    } else if (coeffcount < 7) {
slouken@12806
   438
        return SDL_SetError("Missing required coefficients in MS ADPCM format header");
slouken@12806
   439
    }
slouken@12806
   440
slouken@12806
   441
    coeffdata = (MS_ADPCM_CoeffData *)SDL_malloc(sizeof(MS_ADPCM_CoeffData) + coeffcount * 4);
slouken@12806
   442
    file->decoderdata = coeffdata; /* Freed in cleanup. */
slouken@12806
   443
    if (coeffdata == NULL) {
slouken@12806
   444
        return SDL_OutOfMemory();
slouken@12806
   445
    }
slouken@12806
   446
    coeffdata->coeff = &coeffdata->aligndummy;
slouken@12806
   447
    coeffdata->coeffcount = (Uint16)coeffcount;
slouken@12806
   448
slouken@12806
   449
    /* Copy the 16-bit pairs. */
slouken@12806
   450
    for (i = 0; i < coeffcount * 2; i++) {
slouken@12806
   451
        Sint32 c = chunk->data[22 + i * 2] | ((Sint32)chunk->data[23 + i * 2] << 8);
slouken@12806
   452
        if (c >= 0x8000) {
slouken@12806
   453
            c -= 0x10000;
slouken@12806
   454
        }
slouken@12806
   455
        if (i < 14 && c != presetcoeffs[i]) {
slouken@12806
   456
            return SDL_SetError("Wrong preset coefficients in MS ADPCM format header");
slouken@12806
   457
        }
slouken@12806
   458
        coeffdata->coeff[i] = (Sint16)c;
slouken@12806
   459
    }
slouken@12806
   460
slouken@12806
   461
    /* Technically, wSamplesPerBlock is required, but we have all the
slouken@12806
   462
     * information in the other fields to calculate it, if it's zero.
slouken@12806
   463
     */
slouken@12806
   464
    if (format->samplesperblock == 0) {
slouken@12806
   465
        /* Let's be nice to the encoders that didn't know how to fill this.
slouken@12806
   466
         * The Standards Update calculates it this way:
slouken@12806
   467
         *
slouken@12806
   468
         *   x = Block size (in bits) minus header size (in bits)
slouken@12806
   469
         *   y = Bit depth multiplied by channel count
slouken@12806
   470
         *   z = Number of samples per channel in block header
slouken@12806
   471
         *   wSamplesPerBlock = x / y + z
slouken@12806
   472
         */
slouken@12806
   473
        format->samplesperblock = (Uint32)blockdatasamples + 2;
slouken@12806
   474
    }
slouken@12806
   475
slouken@12806
   476
    /* nBlockAlign can be in conflict with wSamplesPerBlock. For example, if
slouken@12806
   477
     * the number of samples doesn't fit into the block. The Standards Update
slouken@12806
   478
     * also describes wSamplesPerBlock with a formula that makes it necessary to
slouken@12806
   479
     * always fill the block with the maximum amount of samples, but this is not
slouken@12806
   480
     * enforced here as there are no compatibility issues.
slouken@12806
   481
     * A truncated block header with just one sample is not supported.
slouken@12806
   482
     */
slouken@12806
   483
    if (format->samplesperblock == 1 || blockdatasamples < format->samplesperblock - 2) {
slouken@12806
   484
        return SDL_SetError("Invalid number of samples per MS ADPCM block (wSamplesPerBlock)");
slouken@12806
   485
    }
slouken@12806
   486
slouken@12806
   487
    if (MS_ADPCM_CalculateSampleFrames(file, datalength) < 0) {
slouken@12806
   488
        return -1;
slouken@12806
   489
    }
slouken@12806
   490
slouken@12806
   491
    return 0;
slouken@12806
   492
}
slouken@12806
   493
slouken@12806
   494
static Sint16
slouken@12806
   495
MS_ADPCM_ProcessNibble(MS_ADPCM_ChannelState *cstate, Sint32 sample1, Sint32 sample2, Uint8 nybble)
slouken@12806
   496
{
slouken@12806
   497
    const Sint32 max_audioval = 32767;
slouken@12806
   498
    const Sint32 min_audioval = -32768;
slouken@12806
   499
    const Uint16 max_deltaval = 65535;
slouken@12806
   500
    const Uint16 adaptive[] = {
slouken@1895
   501
        230, 230, 230, 230, 307, 409, 512, 614,
slouken@1895
   502
        768, 614, 512, 409, 307, 230, 230, 230
slouken@1895
   503
    };
slouken@12806
   504
    Sint32 new_sample;
slouken@12806
   505
    Sint32 errordelta;
slouken@12806
   506
    Uint32 delta = cstate->delta;
slouken@0
   507
slouken@12806
   508
    new_sample = (sample1 * cstate->coeff1 + sample2 * cstate->coeff2) / 256;
slouken@12806
   509
    /* The nibble is a signed 4-bit error delta. */
slouken@12806
   510
    errordelta = (Sint32)nybble - (nybble >= 0x08 ? 0x10 : 0);
slouken@12806
   511
    new_sample += (Sint32)delta * errordelta;
slouken@1895
   512
    if (new_sample < min_audioval) {
slouken@1895
   513
        new_sample = min_audioval;
slouken@1895
   514
    } else if (new_sample > max_audioval) {
slouken@1895
   515
        new_sample = max_audioval;
slouken@1895
   516
    }
slouken@12806
   517
    delta = (delta * adaptive[nybble]) / 256;
slouken@1895
   518
    if (delta < 16) {
slouken@1895
   519
        delta = 16;
slouken@12806
   520
    } else if (delta > max_deltaval) {
slouken@12806
   521
        /* This issue is not described in the Standards Update and therefore
slouken@12806
   522
         * undefined. It seems sensible to prevent overflows with a limit.
slouken@12806
   523
         */
slouken@12806
   524
        delta = max_deltaval;
slouken@1895
   525
    }
slouken@12806
   526
slouken@12806
   527
    cstate->delta = (Uint16)delta;
slouken@12806
   528
    return (Sint16)new_sample;
slouken@0
   529
}
slouken@0
   530
slouken@1895
   531
static int
slouken@12806
   532
MS_ADPCM_DecodeBlockHeader(ADPCM_DecoderState *state)
slouken@0
   533
{
slouken@12806
   534
    Uint8 coeffindex;
slouken@12806
   535
    const Uint32 channels = state->channels;
slouken@12806
   536
    Sint32 sample;
slouken@12806
   537
    Uint32 c;
slouken@12806
   538
    MS_ADPCM_ChannelState *cstate = (MS_ADPCM_ChannelState *)state->cstate;
slouken@12806
   539
    MS_ADPCM_CoeffData *ddata = (MS_ADPCM_CoeffData *)state->ddata;
slouken@0
   540
slouken@12806
   541
    for (c = 0; c < channels; c++) {
slouken@12806
   542
        size_t o = c;
slouken@12806
   543
slouken@12806
   544
        /* Load the coefficient pair into the channel state. */
slouken@12806
   545
        coeffindex = state->block.data[o];
slouken@12806
   546
        if (coeffindex > ddata->coeffcount) {
slouken@12806
   547
            return SDL_SetError("Invalid MS ADPCM coefficient index in block header");
slouken@12806
   548
        }
slouken@12806
   549
        cstate[c].coeff1 = ddata->coeff[coeffindex * 2];
slouken@12806
   550
        cstate[c].coeff2 = ddata->coeff[coeffindex * 2 + 1];
slouken@12806
   551
slouken@12806
   552
        /* Initial delta value. */
slouken@12806
   553
        o = channels + c * 2;
slouken@12806
   554
        cstate[c].delta = state->block.data[o] | ((Uint16)state->block.data[o + 1] << 8);
slouken@12806
   555
slouken@12806
   556
        /* Load the samples from the header. Interestingly, the sample later in
slouken@12806
   557
         * the output stream comes first.
slouken@12806
   558
         */
slouken@12806
   559
        o = channels * 3 + c * 2;
slouken@12806
   560
        sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
slouken@12806
   561
        if (sample >= 0x8000) {
slouken@12806
   562
            sample -= 0x10000;
slouken@12806
   563
        }
slouken@12806
   564
        state->output.data[state->output.pos + channels] = (Sint16)sample;
slouken@12806
   565
slouken@12806
   566
        o = channels * 5 + c * 2;
slouken@12806
   567
        sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
slouken@12806
   568
        if (sample >= 0x8000) {
slouken@12806
   569
            sample -= 0x10000;
slouken@12806
   570
        }
slouken@12806
   571
        state->output.data[state->output.pos] = (Sint16)sample;
slouken@12806
   572
slouken@12806
   573
        state->output.pos++;
slouken@12806
   574
    }
slouken@12806
   575
slouken@12806
   576
    state->block.pos += state->blockheadersize;
slouken@12806
   577
slouken@12806
   578
    /* Skip second sample frame that came from the header. */
slouken@12806
   579
    state->output.pos += state->channels;
slouken@12806
   580
slouken@12806
   581
    /* Header provided two sample frames. */
slouken@12806
   582
    state->framesleft -= 2;
slouken@12806
   583
slouken@12806
   584
    return 0;
slouken@12806
   585
}
slouken@12806
   586
slouken@12806
   587
/* Decodes the data of the MS ADPCM block. Decoding will stop if a block is too
slouken@12806
   588
 * short, returning with none or partially decoded data. The partial data
slouken@12806
   589
 * will always contain full sample frames (same sample count for each channel).
slouken@12806
   590
 * Incomplete sample frames are discarded.
slouken@12806
   591
 */
slouken@12806
   592
static int
slouken@12806
   593
MS_ADPCM_DecodeBlockData(ADPCM_DecoderState *state)
slouken@12806
   594
{
slouken@12806
   595
    Uint16 nybble = 0;
slouken@12806
   596
    Sint16 sample1, sample2;
slouken@12806
   597
    const Uint32 channels = state->channels;
slouken@12806
   598
    Uint32 c;
slouken@12806
   599
    MS_ADPCM_ChannelState *cstate = (MS_ADPCM_ChannelState *)state->cstate;
slouken@12806
   600
slouken@12806
   601
    size_t blockpos = state->block.pos;
slouken@12806
   602
    size_t blocksize = state->block.size;
slouken@12806
   603
slouken@12806
   604
    size_t outpos = state->output.pos;
slouken@12806
   605
slouken@12806
   606
    Sint64 blockframesleft = state->samplesperblock - 2;
slouken@12806
   607
    if (blockframesleft > state->framesleft) {
slouken@12806
   608
        blockframesleft = state->framesleft;
slouken@12806
   609
    }
slouken@12806
   610
slouken@12806
   611
    while (blockframesleft > 0) {
slouken@12806
   612
        for (c = 0; c < channels; c++) {
slouken@12809
   613
            if (nybble & 0x4000) {
slouken@12806
   614
                nybble <<= 4;
slouken@12806
   615
            } else if (blockpos < blocksize) {
slouken@12809
   616
                nybble = state->block.data[blockpos++] | 0x4000;
slouken@12806
   617
            } else {
slouken@12806
   618
                /* Out of input data. Drop the incomplete frame and return. */
slouken@12806
   619
                state->output.pos = outpos - c;
slouken@12806
   620
                return -1;
slouken@12806
   621
            }
slouken@12806
   622
slouken@12806
   623
            /* Load previous samples which may come from the block header. */
slouken@12806
   624
            sample1 = state->output.data[outpos - channels];
slouken@12806
   625
            sample2 = state->output.data[outpos - channels * 2];
slouken@12806
   626
slouken@12806
   627
            sample1 = MS_ADPCM_ProcessNibble(cstate + c, sample1, sample2, (nybble >> 4) & 0x0f);
slouken@12806
   628
            state->output.data[outpos++] = sample1;
slouken@12806
   629
        }
slouken@12806
   630
slouken@12806
   631
        state->framesleft--;
slouken@12806
   632
        blockframesleft--;
slouken@12806
   633
    }
slouken@12806
   634
slouken@12806
   635
    state->output.pos = outpos;
slouken@12806
   636
slouken@12806
   637
    return 0;
slouken@12806
   638
}
slouken@12806
   639
slouken@12806
   640
static int
slouken@12806
   641
MS_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
slouken@12806
   642
{
slouken@12806
   643
    int result;
slouken@12806
   644
    size_t bytesleft, outputsize;
slouken@12806
   645
    WaveChunk *chunk = &file->chunk;
slouken@12814
   646
    ADPCM_DecoderState state;
slouken@12807
   647
    MS_ADPCM_ChannelState cstate[2];
slouken@12807
   648
slouken@12814
   649
    SDL_zero(state);
slouken@12814
   650
    SDL_zero(cstate);
slouken@12806
   651
slouken@12806
   652
    if (chunk->size != chunk->length) {
slouken@12806
   653
        /* Could not read everything. Recalculate number of sample frames. */
slouken@12806
   654
        if (MS_ADPCM_CalculateSampleFrames(file, chunk->size) < 0) {
slouken@12806
   655
            return -1;
slouken@12806
   656
        }
slouken@12806
   657
    }
slouken@12806
   658
slouken@12806
   659
    /* Nothing to decode, nothing to return. */
slouken@12806
   660
    if (file->sampleframes == 0) {
slouken@12806
   661
        *audio_buf = NULL;
slouken@12806
   662
        *audio_len = 0;
slouken@12806
   663
        return 0;
slouken@12806
   664
    }
slouken@12806
   665
slouken@12806
   666
    state.blocksize = file->format.blockalign;
slouken@12806
   667
    state.channels = file->format.channels;
slouken@12809
   668
    state.blockheadersize = (size_t)state.channels * 7;
slouken@12806
   669
    state.samplesperblock = file->format.samplesperblock;
slouken@12806
   670
    state.framesize = state.channels * sizeof(Sint16);
slouken@12806
   671
    state.ddata = file->decoderdata;
slouken@12806
   672
    state.framestotal = file->sampleframes;
slouken@12806
   673
    state.framesleft = state.framestotal;
slouken@12806
   674
slouken@12806
   675
    state.input.data = chunk->data;
slouken@12806
   676
    state.input.size = chunk->size;
slouken@12806
   677
    state.input.pos = 0;
slouken@12806
   678
slouken@12806
   679
    /* The output size in bytes. May get modified if data is truncated. */
slouken@12806
   680
    outputsize = (size_t)state.framestotal;
slouken@12809
   681
    if (SafeMult(&outputsize, state.framesize)) {
slouken@12806
   682
        return SDL_OutOfMemory();
slouken@12806
   683
    } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
slouken@12806
   684
        return SDL_SetError("WAVE file too big");
slouken@12806
   685
    }
slouken@12806
   686
slouken@12806
   687
    state.output.pos = 0;
slouken@12806
   688
    state.output.size = outputsize / sizeof(Sint16);
slouken@12806
   689
    state.output.data = (Sint16 *)SDL_malloc(outputsize);
slouken@12806
   690
    if (state.output.data == NULL) {
icculus@7037
   691
        return SDL_OutOfMemory();
slouken@1895
   692
    }
slouken@0
   693
slouken@12806
   694
    state.cstate = &cstate;
slouken@0
   695
slouken@12806
   696
    /* Decode block by block. A truncated block will stop the decoding. */
slouken@12806
   697
    bytesleft = state.input.size - state.input.pos;
slouken@12806
   698
    while (state.framesleft > 0 && bytesleft >= state.blockheadersize) {
slouken@12806
   699
        state.block.data = state.input.data + state.input.pos;
slouken@12806
   700
        state.block.size = bytesleft < state.blocksize ? bytesleft : state.blocksize;
slouken@12806
   701
        state.block.pos = 0;
slouken@12806
   702
slouken@12806
   703
        if (state.output.size - state.output.pos < (Uint64)state.framesleft * state.channels) {
slouken@12806
   704
            /* Somehow didn't allocate enough space for the output. */
slouken@12806
   705
            SDL_free(state.output.data);
slouken@12806
   706
            return SDL_SetError("Unexpected overflow in MS ADPCM decoder");
slouken@1895
   707
        }
slouken@0
   708
slouken@12806
   709
        /* Initialize decoder with the values from the block header. */
slouken@12806
   710
        result = MS_ADPCM_DecodeBlockHeader(&state);
slouken@12806
   711
        if (result == -1) {
slouken@12806
   712
            SDL_free(state.output.data);
slouken@12806
   713
            return -1;
slouken@12806
   714
        }
slouken@0
   715
slouken@12806
   716
        /* Decode the block data. It stores the samples directly in the output. */
slouken@12806
   717
        result = MS_ADPCM_DecodeBlockData(&state);
slouken@12806
   718
        if (result == -1) {
slouken@12806
   719
            /* Unexpected end. Stop decoding and return partial data if necessary. */
slouken@12806
   720
            if (file->trunchint == TruncVeryStrict || file->trunchint == TruncVeryStrict) {
slouken@12806
   721
                SDL_free(state.output.data);
slouken@12806
   722
                return SDL_SetError("Truncated data chunk");
slouken@12806
   723
            } else if (file->trunchint != TruncDropFrame) {
slouken@12806
   724
                state.output.pos -= state.output.pos % (state.samplesperblock * state.channels);
slouken@12806
   725
            }
slouken@12806
   726
            outputsize = state.output.pos * sizeof(Sint16); /* Can't overflow, is always smaller. */
slouken@12806
   727
            break;
slouken@12806
   728
        }
slouken@0
   729
slouken@12806
   730
        state.input.pos += state.block.size;
slouken@12806
   731
        bytesleft = state.input.size - state.input.pos;
slouken@1895
   732
    }
slouken@12806
   733
slouken@12806
   734
    *audio_buf = (Uint8 *)state.output.data;
slouken@12806
   735
    *audio_len = (Uint32)outputsize;
slouken@12806
   736
slouken@12806
   737
    return 0;
slouken@0
   738
}
slouken@0
   739
slouken@12806
   740
static int
slouken@12806
   741
IMA_ADPCM_CalculateSampleFrames(WaveFile *file, size_t datalength)
slouken@1895
   742
{
slouken@12806
   743
    WaveFormat *format = &file->format;
slouken@12809
   744
    const size_t blockheadersize = (size_t)format->channels * 4;
slouken@12809
   745
    const size_t subblockframesize = (size_t)format->channels * 4;
slouken@12806
   746
    const size_t availableblocks = datalength / format->blockalign;
slouken@12806
   747
    const size_t trailingdata = datalength % format->blockalign;
slouken@12806
   748
slouken@12806
   749
    if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
slouken@12806
   750
        /* The size of the data chunk must be a multiple of the block size. */
slouken@12806
   751
        if (datalength < blockheadersize || trailingdata > 0) {
slouken@12806
   752
            return SDL_SetError("Truncated IMA ADPCM block");
slouken@12806
   753
        }
slouken@12806
   754
    }
slouken@12806
   755
slouken@12806
   756
    /* Calculate number of sample frames that will be decoded. */
slouken@12806
   757
    file->sampleframes = (Uint64)availableblocks * format->samplesperblock;
slouken@12806
   758
    if (trailingdata > 0) {
slouken@12806
   759
        /* The last block is truncated. Check if we can get any samples out of it. */
slouken@12806
   760
        if (file->trunchint == TruncDropFrame && trailingdata > blockheadersize - 2) {
slouken@12806
   761
            /* The sample frame in the header of the truncated block is present.
slouken@12806
   762
             * Drop incomplete sample frames.
slouken@12806
   763
             */
slouken@12806
   764
            size_t trailingsamples = 1;
slouken@12806
   765
slouken@12806
   766
            if (trailingdata > blockheadersize) {
slouken@12806
   767
                /* More data following after the header. */
slouken@12806
   768
                const size_t trailingblockdata = trailingdata - blockheadersize;
slouken@12806
   769
                const size_t trailingsubblockdata = trailingblockdata % subblockframesize;
slouken@12806
   770
                trailingsamples += (trailingblockdata / subblockframesize) * 8;
slouken@12806
   771
                /* Due to the interleaved sub-blocks, the last 4 bytes determine
slouken@12806
   772
                 * how many samples of the truncated sub-block are lost.
slouken@12806
   773
                 */
slouken@12806
   774
                if (trailingsubblockdata > subblockframesize - 4) {
slouken@12806
   775
                    trailingsamples += (trailingsubblockdata % 4) * 2;
slouken@12806
   776
                }
slouken@12806
   777
            }
slouken@12806
   778
slouken@12806
   779
            if (trailingsamples > format->samplesperblock) {
slouken@12806
   780
                trailingsamples = format->samplesperblock;
slouken@12806
   781
            }
slouken@12806
   782
            file->sampleframes += trailingsamples;
slouken@12806
   783
        }
slouken@12806
   784
    }
slouken@12806
   785
slouken@12806
   786
    file->sampleframes = WaveAdjustToFactValue(file, file->sampleframes);
slouken@12806
   787
    if (file->sampleframes < 0) {
slouken@12806
   788
        return -1;
slouken@12806
   789
    }
slouken@12806
   790
slouken@12806
   791
    return 0;
slouken@12806
   792
}
slouken@0
   793
slouken@1895
   794
static int
slouken@12806
   795
IMA_ADPCM_Init(WaveFile *file, size_t datalength)
slouken@0
   796
{
slouken@12806
   797
    WaveFormat *format = &file->format;
slouken@12806
   798
    WaveChunk *chunk = &file->chunk;
slouken@12809
   799
    const size_t blockheadersize = (size_t)format->channels * 4;
slouken@12806
   800
    const size_t blockdatasize = (size_t)format->blockalign - blockheadersize;
slouken@12809
   801
    const size_t blockframebitsize = (size_t)format->bitspersample * format->channels;
slouken@12806
   802
    const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
slouken@0
   803
slouken@12806
   804
    /* Sanity checks. */
slouken@12806
   805
slouken@12809
   806
    /* IMA ADPCM can also have 3-bit samples, but it's not supported by SDL at this time. */
slouken@12806
   807
    if (format->bitspersample == 3) {
slouken@12806
   808
        return SDL_SetError("3-bit IMA ADPCM currently not supported");
slouken@12806
   809
    } else if (format->bitspersample != 4) {
slouken@12809
   810
        return SDL_SetError("Invalid IMA ADPCM bits per sample of %u", (unsigned int)format->bitspersample);
slouken@1895
   811
    }
slouken@12806
   812
slouken@12806
   813
    /* The block size is required to be a multiple of 4 and it must be able to
slouken@12806
   814
     * hold a block header.
slouken@12806
   815
     */
slouken@12806
   816
    if (format->blockalign < blockheadersize || format->blockalign % 4) {
slouken@12806
   817
        return SDL_SetError("Invalid IMA ADPCM block size (nBlockAlign)");
slouken@12806
   818
    }
slouken@12806
   819
slouken@12806
   820
    if (format->formattag == EXTENSIBLE_CODE) {
slouken@12806
   821
        /* There's no specification for this, but it's basically the same
slouken@12806
   822
         * format because the extensible header has wSampePerBlocks too.
slouken@12806
   823
         */
slouken@12806
   824
    } else  {
slouken@12806
   825
        /* The Standards Update says there 'should' be 2 bytes for wSamplesPerBlock. */
slouken@12806
   826
        if (chunk->size >= 20 && format->extsize >= 2) {
slouken@12806
   827
            format->samplesperblock = chunk->data[18] | ((Uint16)chunk->data[19] << 8);
slouken@12806
   828
        }
slouken@12806
   829
    }
slouken@12806
   830
slouken@12806
   831
    if (format->samplesperblock == 0) {
slouken@12806
   832
        /* Field zero? No problem. We just assume the encoder packed the block.
slouken@12806
   833
         * The specification calculates it this way:
slouken@12806
   834
         *
slouken@12806
   835
         *   x = Block size (in bits) minus header size (in bits)
slouken@12806
   836
         *   y = Bit depth multiplied by channel count
slouken@12806
   837
         *   z = Number of samples per channel in header
slouken@12806
   838
         *   wSamplesPerBlock = x / y + z
slouken@12806
   839
         */
slouken@12806
   840
        format->samplesperblock = (Uint32)blockdatasamples + 1;
slouken@12806
   841
    }
slouken@12806
   842
slouken@12806
   843
    /* nBlockAlign can be in conflict with wSamplesPerBlock. For example, if
slouken@12806
   844
     * the number of samples doesn't fit into the block. The Standards Update
slouken@12806
   845
     * also describes wSamplesPerBlock with a formula that makes it necessary
slouken@12806
   846
     * to always fill the block with the maximum amount of samples, but this is
slouken@12806
   847
     * not enforced here as there are no compatibility issues.
slouken@12806
   848
     */
slouken@12806
   849
    if (blockdatasamples < format->samplesperblock - 1) {
slouken@12806
   850
        return SDL_SetError("Invalid number of samples per IMA ADPCM block (wSamplesPerBlock)");
slouken@12806
   851
    }
slouken@12806
   852
slouken@12806
   853
    if (IMA_ADPCM_CalculateSampleFrames(file, datalength) < 0) {
slouken@12806
   854
        return -1;
slouken@12806
   855
    }
slouken@12806
   856
slouken@12806
   857
    return 0;
slouken@0
   858
}
slouken@0
   859
slouken@12806
   860
static Sint16
slouken@12806
   861
IMA_ADPCM_ProcessNibble(Sint8 *cindex, Sint16 lastsample, Uint8 nybble)
slouken@0
   862
{
slouken@12806
   863
    const Sint32 max_audioval = 32767;
slouken@12806
   864
    const Sint32 min_audioval = -32768;
slouken@12806
   865
    const Sint8 index_table_4b[16] = {
slouken@1895
   866
        -1, -1, -1, -1,
slouken@1895
   867
        2, 4, 6, 8,
slouken@1895
   868
        -1, -1, -1, -1,
slouken@1895
   869
        2, 4, 6, 8
slouken@1895
   870
    };
slouken@12806
   871
    const Uint16 step_table[89] = {
slouken@1895
   872
        7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
slouken@1895
   873
        34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
slouken@1895
   874
        143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
slouken@1895
   875
        449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
slouken@1895
   876
        1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
slouken@1895
   877
        3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
slouken@1895
   878
        9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
slouken@1895
   879
        22385, 24623, 27086, 29794, 32767
slouken@1895
   880
    };
slouken@12806
   881
    Uint32 step;
slouken@12806
   882
    Sint32 sample, delta;
slouken@12806
   883
    Sint8 index = *cindex;
slouken@0
   884
slouken@12806
   885
    /* Clamp index into valid range. */
slouken@12806
   886
    if (index > 88) {
slouken@12806
   887
        index = 88;
slouken@12806
   888
    } else if (index < 0) {
slouken@12806
   889
        index = 0;
slouken@7313
   890
    }
slouken@12806
   891
sbc@8879
   892
    /* explicit cast to avoid gcc warning about using 'char' as array index */
slouken@12806
   893
    step = step_table[(size_t)index];
slouken@12806
   894
slouken@12806
   895
    /* Update index value */
slouken@12806
   896
    *cindex = index + index_table_4b[nybble];
slouken@12806
   897
slouken@12806
   898
    /* This calculation uses shifts and additions because multiplications were
slouken@12806
   899
     * much slower back then. Sadly, this can't just be replaced with an actual
slouken@12806
   900
     * multiplication now as the old algorithm drops some bits. The closest
slouken@12806
   901
     * approximation I could find is something like this:
slouken@12806
   902
     * (nybble & 0x8 ? -1 : 1) * ((nybble & 0x7) * step / 4 + step / 8)
slouken@12806
   903
     */
slouken@1895
   904
    delta = step >> 3;
slouken@1895
   905
    if (nybble & 0x04)
slouken@1895
   906
        delta += step;
slouken@1895
   907
    if (nybble & 0x02)
slouken@12806
   908
        delta += step >> 1;
slouken@1895
   909
    if (nybble & 0x01)
slouken@12806
   910
        delta += step >> 2;
slouken@1895
   911
    if (nybble & 0x08)
slouken@1895
   912
        delta = -delta;
slouken@0
   913
slouken@12806
   914
    sample = lastsample + delta;
slouken@0
   915
slouken@1895
   916
    /* Clamp output sample */
slouken@12806
   917
    if (sample > max_audioval) {
slouken@12806
   918
        sample = max_audioval;
slouken@12806
   919
    } else if (sample < min_audioval) {
slouken@12806
   920
        sample = min_audioval;
slouken@1895
   921
    }
slouken@0
   922
slouken@12806
   923
    return (Sint16)sample;
slouken@0
   924
}
slouken@0
   925
slouken@1895
   926
static int
slouken@12806
   927
IMA_ADPCM_DecodeBlockHeader(ADPCM_DecoderState *state)
slouken@0
   928
{
slouken@12806
   929
    Sint16 step;
slouken@12806
   930
    Uint32 c;
slouken@12806
   931
    Uint8 *cstate = state->cstate;
slouken@0
   932
slouken@12806
   933
    for (c = 0; c < state->channels; c++) {
slouken@12806
   934
        size_t o = state->block.pos + c * 4;
slouken@12806
   935
slouken@12806
   936
        /* Extract the sample from the header. */
slouken@12806
   937
        Sint32 sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
slouken@12806
   938
        if (sample >= 0x8000) {
slouken@12806
   939
            sample -= 0x10000;
slouken@12806
   940
        }
slouken@12806
   941
        state->output.data[state->output.pos++] = (Sint16)sample;
slouken@12806
   942
slouken@12806
   943
        /* Channel step index. */
slouken@12806
   944
        step = (Sint16)state->block.data[o + 2];
slouken@12806
   945
        cstate[c] = (Sint8)(step > 0x80 ? step - 0x100 : step);
slouken@12806
   946
slouken@12806
   947
        /* Reserved byte in block header, should be 0. */
slouken@12806
   948
        if (state->block.data[o + 3] != 0) {
slouken@12806
   949
            /* Uh oh, corrupt data?  Buggy code? */ ;
slouken@12806
   950
        }
slouken@1895
   951
    }
slouken@0
   952
slouken@12806
   953
    state->block.pos += state->blockheadersize;
slouken@12806
   954
slouken@12806
   955
    /* Header provided one sample frame. */
slouken@12806
   956
    state->framesleft--;
slouken@12806
   957
slouken@12806
   958
    return 0;
slouken@12806
   959
}
slouken@12806
   960
slouken@12806
   961
/* Decodes the data of the IMA ADPCM block. Decoding will stop if a block is too
slouken@12806
   962
 * short, returning with none or partially decoded data. The partial data always
slouken@12806
   963
 * contains full sample frames (same sample count for each channel).
slouken@12806
   964
 * Incomplete sample frames are discarded.
slouken@12806
   965
 */
slouken@12806
   966
static int
slouken@12806
   967
IMA_ADPCM_DecodeBlockData(ADPCM_DecoderState *state)
slouken@12806
   968
{
slouken@12806
   969
    size_t i;
slouken@12806
   970
    int retval = 0;
slouken@12806
   971
    const Uint32 channels = state->channels;
slouken@12806
   972
    const size_t subblockframesize = channels * 4;
slouken@12806
   973
    Uint64 bytesrequired;
slouken@12806
   974
    Uint32 c;
slouken@12806
   975
slouken@12806
   976
    size_t blockpos = state->block.pos;
slouken@12806
   977
    size_t blocksize = state->block.size;
slouken@12806
   978
    size_t blockleft = blocksize - blockpos;
slouken@12806
   979
slouken@12806
   980
    size_t outpos = state->output.pos;
slouken@12806
   981
slouken@12806
   982
    Sint64 blockframesleft = state->samplesperblock - 1;
slouken@12806
   983
    if (blockframesleft > state->framesleft) {
slouken@12806
   984
        blockframesleft = state->framesleft;
slouken@1895
   985
    }
slouken@0
   986
slouken@12806
   987
    bytesrequired = (blockframesleft + 7) / 8 * subblockframesize;
slouken@12806
   988
    if (blockleft < bytesrequired) {
slouken@12806
   989
        /* Data truncated. Calculate how many samples we can get out if it. */
slouken@12806
   990
        const size_t guaranteedframes = blockleft / subblockframesize;
slouken@12806
   991
        const size_t remainingbytes = blockleft % subblockframesize;
slouken@12806
   992
        blockframesleft = guaranteedframes;
slouken@12806
   993
        if (remainingbytes > subblockframesize - 4) {
slouken@12806
   994
            blockframesleft += (remainingbytes % 4) * 2;
slouken@12806
   995
        }
slouken@12806
   996
        /* Signal the truncation. */
slouken@12806
   997
        retval = -1;
slouken@12806
   998
    }
slouken@12806
   999
slouken@12806
  1000
    /* Each channel has their nibbles packed into 32-bit blocks. These blocks
slouken@12806
  1001
     * are interleaved and make up the data part of the ADPCM block. This loop
slouken@12806
  1002
     * decodes the samples as they come from the input data and puts them at
slouken@12806
  1003
     * the appropriate places in the output data.
slouken@12806
  1004
     */
slouken@12806
  1005
    while (blockframesleft > 0) {
slouken@12806
  1006
        const size_t subblocksamples = blockframesleft < 8 ? (size_t)blockframesleft : 8;
slouken@12806
  1007
slouken@12806
  1008
        for (c = 0; c < channels; c++) {
slouken@12806
  1009
            Uint8 nybble = 0;
slouken@12806
  1010
            /* Load previous sample which may come from the block header. */
slouken@12806
  1011
            Sint16 sample = state->output.data[outpos + c - channels];
slouken@12806
  1012
slouken@12806
  1013
            for (i = 0; i < subblocksamples; i++) {
slouken@12806
  1014
                if (i & 1) {
slouken@12806
  1015
                    nybble >>= 4;
slouken@12806
  1016
                } else {
slouken@12806
  1017
                    nybble = state->block.data[blockpos++];
slouken@12806
  1018
                }
slouken@12806
  1019
slouken@12806
  1020
                sample = IMA_ADPCM_ProcessNibble((Sint8 *)state->cstate + c, sample, nybble & 0x0f);
slouken@12806
  1021
                state->output.data[outpos + c + i * channels] = sample;
slouken@1895
  1022
            }
slouken@1895
  1023
        }
slouken@0
  1024
slouken@12806
  1025
        outpos += channels * subblocksamples;
slouken@12806
  1026
        state->framesleft -= subblocksamples;
slouken@12806
  1027
        blockframesleft -= subblocksamples;
slouken@1895
  1028
    }
slouken@12806
  1029
slouken@12806
  1030
    state->block.pos = blockpos;
slouken@12806
  1031
    state->output.pos = outpos;
slouken@12806
  1032
slouken@12806
  1033
    return retval;
slouken@0
  1034
}
slouken@0
  1035
slouken@12806
  1036
static int
slouken@12806
  1037
IMA_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
slouken@12806
  1038
{
slouken@12806
  1039
    int result;
slouken@12806
  1040
    size_t bytesleft, outputsize;
slouken@12806
  1041
    WaveChunk *chunk = &file->chunk;
icculus@12825
  1042
    ADPCM_DecoderState state;
slouken@12806
  1043
    Sint8 *cstate;
icculus@11464
  1044
slouken@12806
  1045
    if (chunk->size != chunk->length) {
slouken@12806
  1046
        /* Could not read everything. Recalculate number of sample frames. */
slouken@12806
  1047
        if (IMA_ADPCM_CalculateSampleFrames(file, chunk->size) < 0) {
slouken@12806
  1048
            return -1;
slouken@12806
  1049
        }
slouken@12806
  1050
    }
icculus@11464
  1051
slouken@12806
  1052
    /* Nothing to decode, nothing to return. */
slouken@12806
  1053
    if (file->sampleframes == 0) {
slouken@12806
  1054
        *audio_buf = NULL;
slouken@12806
  1055
        *audio_len = 0;
slouken@12806
  1056
        return 0;
slouken@12806
  1057
    }
slouken@12806
  1058
icculus@12825
  1059
    SDL_zero(state);
slouken@12806
  1060
    state.channels = file->format.channels;
slouken@12806
  1061
    state.blocksize = file->format.blockalign;
slouken@12809
  1062
    state.blockheadersize = (size_t)state.channels * 4;
slouken@12806
  1063
    state.samplesperblock = file->format.samplesperblock;
slouken@12806
  1064
    state.framesize = state.channels * sizeof(Sint16);
slouken@12806
  1065
    state.framestotal = file->sampleframes;
slouken@12806
  1066
    state.framesleft = state.framestotal;
slouken@12806
  1067
slouken@12806
  1068
    state.input.data = chunk->data;
slouken@12806
  1069
    state.input.size = chunk->size;
slouken@12806
  1070
    state.input.pos = 0;
slouken@12806
  1071
slouken@12806
  1072
    /* The output size in bytes. May get modified if data is truncated. */
slouken@12806
  1073
    outputsize = (size_t)state.framestotal;
slouken@12809
  1074
    if (SafeMult(&outputsize, state.framesize)) {
slouken@12806
  1075
        return SDL_OutOfMemory();
slouken@12806
  1076
    } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
slouken@12806
  1077
        return SDL_SetError("WAVE file too big");
slouken@12806
  1078
    }
slouken@12806
  1079
slouken@12806
  1080
    state.output.pos = 0;
slouken@12806
  1081
    state.output.size = outputsize / sizeof(Sint16);
slouken@12806
  1082
    state.output.data = (Sint16 *)SDL_malloc(outputsize);
slouken@12806
  1083
    if (state.output.data == NULL) {
icculus@11464
  1084
        return SDL_OutOfMemory();
icculus@11464
  1085
    }
icculus@11464
  1086
slouken@12806
  1087
    cstate = (Sint8 *)SDL_calloc(state.channels, sizeof(Sint8));
slouken@12806
  1088
    if (cstate == NULL) {
slouken@12806
  1089
        SDL_free(state.output.data);
slouken@12806
  1090
        return SDL_OutOfMemory();
slouken@12806
  1091
    }
slouken@12806
  1092
    state.cstate = cstate;
icculus@11464
  1093
slouken@12806
  1094
    /* Decode block by block. A truncated block will stop the decoding. */
slouken@12806
  1095
    bytesleft = state.input.size - state.input.pos;
slouken@12806
  1096
    while (state.framesleft > 0 && bytesleft >= state.blockheadersize) {
slouken@12806
  1097
        state.block.data = state.input.data + state.input.pos;
slouken@12806
  1098
        state.block.size = bytesleft < state.blocksize ? bytesleft : state.blocksize;
slouken@12806
  1099
        state.block.pos = 0;
slouken@12806
  1100
slouken@12806
  1101
        if (state.output.size - state.output.pos < (Uint64)state.framesleft * state.channels) {
slouken@12806
  1102
            /* Somehow didn't allocate enough space for the output. */
slouken@12806
  1103
            SDL_free(state.output.data);
slouken@12806
  1104
            SDL_free(cstate);
slouken@12806
  1105
            return SDL_SetError("Unexpected overflow in IMA ADPCM decoder");
slouken@12806
  1106
        }
slouken@12806
  1107
slouken@12806
  1108
        /* Initialize decoder with the values from the block header. */
slouken@12806
  1109
        result = IMA_ADPCM_DecodeBlockHeader(&state);
slouken@12806
  1110
slouken@12806
  1111
        /* Decode the block data. It stores the samples directly in the output. */
slouken@12806
  1112
        result = IMA_ADPCM_DecodeBlockData(&state);
slouken@12806
  1113
        if (result == -1) {
slouken@12806
  1114
            /* Unexpected end. Stop decoding and return partial data if necessary. */
slouken@12806
  1115
            if (file->trunchint == TruncVeryStrict || file->trunchint == TruncVeryStrict) {
slouken@12806
  1116
                SDL_free(state.output.data);
slouken@12806
  1117
                SDL_free(cstate);
slouken@12806
  1118
                return SDL_SetError("Truncated data chunk");
slouken@12806
  1119
            } else if (file->trunchint != TruncDropFrame) {
slouken@12806
  1120
                state.output.pos -= state.output.pos % (state.samplesperblock * state.channels);
slouken@12806
  1121
            }
slouken@12806
  1122
            outputsize = state.output.pos * sizeof(Sint16); /* Can't overflow, is always smaller. */
slouken@12806
  1123
            break;
slouken@12806
  1124
        }
slouken@12806
  1125
slouken@12806
  1126
        state.input.pos += state.block.size;
slouken@12806
  1127
        bytesleft = state.input.size - state.input.pos;
slouken@12806
  1128
    }
slouken@12806
  1129
slouken@12806
  1130
    *audio_buf = (Uint8 *)state.output.data;
slouken@12806
  1131
    *audio_len = (Uint32)outputsize;
slouken@12806
  1132
slouken@12806
  1133
    SDL_free(cstate);
slouken@12806
  1134
slouken@12806
  1135
    return 0;
slouken@12806
  1136
}
slouken@12806
  1137
slouken@12806
  1138
static int
slouken@12806
  1139
LAW_Init(WaveFile *file, size_t datalength)
slouken@12806
  1140
{
slouken@12806
  1141
    WaveFormat *format = &file->format;
slouken@12806
  1142
slouken@12806
  1143
    /* Standards Update requires this to be 8. */
slouken@12806
  1144
    if (format->bitspersample != 8) {
slouken@12809
  1145
        return SDL_SetError("Invalid companded bits per sample of %u", (unsigned int)format->bitspersample);
slouken@12806
  1146
    }
slouken@12806
  1147
slouken@12806
  1148
    /* Not going to bother with weird padding. */
slouken@12806
  1149
    if (format->blockalign != format->channels) {
slouken@12806
  1150
        return SDL_SetError("Unsupported block alignment");
slouken@12806
  1151
    }
slouken@12806
  1152
slouken@12806
  1153
    if ((file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict)) {
slouken@12806
  1154
        if (format->blockalign > 1 && datalength % format->blockalign) {
slouken@12806
  1155
            return SDL_SetError("Truncated data chunk in WAVE file");
slouken@12806
  1156
        }
slouken@12806
  1157
    }
slouken@12806
  1158
slouken@12806
  1159
    file->sampleframes = WaveAdjustToFactValue(file, datalength / format->blockalign);
slouken@12806
  1160
    if (file->sampleframes < 0) {
slouken@12806
  1161
        return -1;
icculus@11464
  1162
    }
icculus@11464
  1163
icculus@11464
  1164
    return 0;
icculus@11464
  1165
}
icculus@11464
  1166
slouken@12806
  1167
static int
slouken@12806
  1168
LAW_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
slouken@12806
  1169
{
slouken@12806
  1170
#ifdef SDL_WAVE_LAW_LUT
slouken@12806
  1171
    const Sint16 alaw_lut[256] = {
slouken@12806
  1172
        -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752,
slouken@12806
  1173
        -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
slouken@12806
  1174
        -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
slouken@12806
  1175
        -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, -344,
slouken@12806
  1176
        -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88,
slouken@12806
  1177
        -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376,
slouken@12806
  1178
        -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688,
slouken@12806
  1179
        -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504,
slouken@12806
  1180
        5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752,
slouken@12806
  1181
        2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016,
slouken@12806
  1182
        20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008,
slouken@12806
  1183
        10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344,
slouken@12806
  1184
        328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88,
slouken@12806
  1185
        72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376,
slouken@12806
  1186
        1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688,
slouken@12806
  1187
        656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848
slouken@12806
  1188
    };
slouken@12806
  1189
    const Sint16 mulaw_lut[256] = {
slouken@12806
  1190
        -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996,
slouken@12806
  1191
        -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932,
slouken@12806
  1192
        -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900,
slouken@12806
  1193
        -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,
slouken@12806
  1194
        -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876,
slouken@12806
  1195
        -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372,
slouken@12806
  1196
        -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120,
slouken@12806
  1197
        -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124,
slouken@12806
  1198
        31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996,
slouken@12806
  1199
        15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932,
slouken@12806
  1200
        7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900,
slouken@12806
  1201
        3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884,
slouken@12806
  1202
        1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876,
slouken@12806
  1203
        844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372,
slouken@12806
  1204
        356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120,
slouken@12806
  1205
        112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0
slouken@12806
  1206
    };
slouken@12806
  1207
#endif
icculus@11464
  1208
slouken@12806
  1209
    WaveFormat *format = &file->format;
slouken@12806
  1210
    WaveChunk *chunk = &file->chunk;
slouken@12806
  1211
    size_t i, sample_count, expanded_len;
slouken@12806
  1212
    Uint8 *src;
slouken@12806
  1213
    Sint16 *dst;
icculus@10894
  1214
slouken@12806
  1215
    if (chunk->length != chunk->size) {
slouken@12806
  1216
        file->sampleframes = WaveAdjustToFactValue(file, chunk->size / format->blockalign);
slouken@12806
  1217
        if (file->sampleframes < 0) {
slouken@12806
  1218
            return -1;
slouken@12806
  1219
        }
slouken@1895
  1220
    }
slouken@0
  1221
slouken@12806
  1222
    /* Nothing to decode, nothing to return. */
slouken@12806
  1223
    if (file->sampleframes == 0) {
slouken@12806
  1224
        *audio_buf = NULL;
slouken@12806
  1225
        *audio_len = 0;
slouken@12806
  1226
        return 0;
slouken@12806
  1227
    }
slouken@12806
  1228
slouken@12806
  1229
    sample_count = (size_t)file->sampleframes;
slouken@12809
  1230
    if (SafeMult(&sample_count, format->channels)) {
slouken@12806
  1231
        return SDL_OutOfMemory();
slouken@12806
  1232
    }
slouken@12806
  1233
slouken@12806
  1234
    expanded_len = sample_count;
slouken@12809
  1235
    if (SafeMult(&expanded_len, sizeof(Sint16))) {
slouken@12806
  1236
        return SDL_OutOfMemory();
slouken@12806
  1237
    } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
slouken@12806
  1238
        return SDL_SetError("WAVE file too big");
slouken@12806
  1239
    }
slouken@12806
  1240
slouken@12806
  1241
    src = (Uint8 *)SDL_realloc(chunk->data, expanded_len);
slouken@12806
  1242
    if (src == NULL) {
slouken@12806
  1243
        return SDL_OutOfMemory();
slouken@12806
  1244
    }
slouken@12806
  1245
    chunk->data = NULL;
slouken@12806
  1246
    chunk->size = 0;
slouken@12806
  1247
slouken@12806
  1248
    dst = (Sint16 *)src;
slouken@12806
  1249
slouken@12806
  1250
    /* Work backwards, since we're expanding in-place. SDL_AudioSpec.format will
slouken@12806
  1251
     * inform the caller about the byte order.
slouken@12806
  1252
     */
slouken@12806
  1253
    i = sample_count;
slouken@12806
  1254
    switch (file->format.encoding) {
slouken@12806
  1255
#ifdef SDL_WAVE_LAW_LUT
slouken@12806
  1256
    case ALAW_CODE:
slouken@12806
  1257
        while (i--) {
slouken@12806
  1258
            dst[i] = alaw_lut[src[i]];
slouken@12806
  1259
        }
slouken@12806
  1260
        break;
slouken@12806
  1261
    case MULAW_CODE:
slouken@12806
  1262
        while (i--) {
slouken@12806
  1263
            dst[i] = mulaw_lut[src[i]];
slouken@12806
  1264
        }
slouken@12806
  1265
        break;
slouken@12806
  1266
#else
slouken@12806
  1267
    case ALAW_CODE:
slouken@12806
  1268
        while (i--) {
slouken@12806
  1269
            Uint8 nibble = src[i];
slouken@12806
  1270
            Uint8 exponent = (nibble & 0x7f) ^ 0x55;
slouken@12806
  1271
            Sint16 mantissa = exponent & 0xf;
slouken@12806
  1272
slouken@12806
  1273
            exponent >>= 4;
slouken@12806
  1274
            if (exponent > 0) {
slouken@12806
  1275
                mantissa |= 0x10;
slouken@12806
  1276
            }
slouken@12809
  1277
            mantissa = (mantissa << 4) | 0x8;
slouken@12806
  1278
            if (exponent > 1) {
slouken@12806
  1279
                mantissa <<= exponent - 1;
slouken@12806
  1280
            }
slouken@12806
  1281
slouken@12806
  1282
            dst[i] = nibble & 0x80 ? mantissa : -mantissa;
slouken@12806
  1283
        }
slouken@12806
  1284
        break;
slouken@12806
  1285
    case MULAW_CODE:
slouken@12806
  1286
        while (i--) {
slouken@12806
  1287
            Uint8 nibble = ~src[i];
slouken@12806
  1288
            Sint16 mantissa = nibble & 0xf;
slouken@12809
  1289
            Uint8 exponent = (nibble >> 4) & 0x7;
slouken@12806
  1290
            Sint16 step = 4 << (exponent + 1);
slouken@12806
  1291
slouken@12806
  1292
            mantissa = (0x80 << exponent) + step * mantissa + step / 2 - 132;
slouken@12806
  1293
slouken@12806
  1294
            dst[i] = nibble & 0x80 ? -mantissa : mantissa;
slouken@12806
  1295
        }
slouken@12806
  1296
        break;
slouken@12806
  1297
#endif
slouken@12806
  1298
    default:
slouken@12806
  1299
        SDL_free(src);
slouken@12806
  1300
        return SDL_SetError("Unknown companded encoding");
slouken@12806
  1301
    }
slouken@12806
  1302
slouken@12806
  1303
    *audio_buf = src;
slouken@12806
  1304
    *audio_len = (Uint32)expanded_len;
slouken@12806
  1305
slouken@12806
  1306
    return 0;
slouken@12806
  1307
}
slouken@12806
  1308
slouken@12806
  1309
static int
slouken@12806
  1310
PCM_Init(WaveFile *file, size_t datalength)
slouken@12806
  1311
{
slouken@12806
  1312
    WaveFormat *format = &file->format;
slouken@12806
  1313
slouken@12806
  1314
    if (format->encoding == PCM_CODE) {
slouken@12806
  1315
        switch (format->bitspersample) {
slouken@12806
  1316
        case 8:
slouken@12806
  1317
        case 16:
slouken@12806
  1318
        case 24:
slouken@12806
  1319
        case 32:
slouken@12806
  1320
            /* These are supported. */
slouken@12806
  1321
            break;
slouken@12806
  1322
        default:
slouken@12809
  1323
            return SDL_SetError("%u-bit PCM format not supported", (unsigned int)format->bitspersample);
slouken@12806
  1324
        }
slouken@12806
  1325
    } else if (format->encoding == IEEE_FLOAT_CODE) {
slouken@12806
  1326
        if (format->bitspersample != 32) {
slouken@12809
  1327
            return SDL_SetError("%u-bit IEEE floating-point format not supported", (unsigned int)format->bitspersample);
slouken@12806
  1328
        }
slouken@12806
  1329
    }
slouken@12806
  1330
slouken@12806
  1331
    /* It wouldn't be that hard to support more exotic block sizes, but
slouken@12806
  1332
     * the most common formats should do for now.
slouken@12806
  1333
     */
slouken@12806
  1334
    if (format->blockalign * 8 != format->channels * format->bitspersample) {
slouken@12806
  1335
        return SDL_SetError("Unsupported block alignment");
slouken@12806
  1336
    }
slouken@12806
  1337
slouken@12806
  1338
    if ((file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict)) {
slouken@12806
  1339
        if (format->blockalign > 1 && datalength % format->blockalign) {
slouken@12806
  1340
            return SDL_SetError("Truncated data chunk in WAVE file");
slouken@12806
  1341
        }
slouken@12806
  1342
    }
slouken@12806
  1343
slouken@12806
  1344
    file->sampleframes = WaveAdjustToFactValue(file, datalength / format->blockalign);
slouken@12806
  1345
    if (file->sampleframes < 0) {
slouken@12806
  1346
        return -1;
slouken@12806
  1347
    }
slouken@12806
  1348
slouken@12806
  1349
    return 0;
slouken@12806
  1350
}
slouken@12806
  1351
slouken@12806
  1352
static int
slouken@12806
  1353
PCM_ConvertSint24ToSint32(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
slouken@12806
  1354
{
slouken@12806
  1355
    WaveFormat *format = &file->format;
slouken@12806
  1356
    WaveChunk *chunk = &file->chunk;
slouken@12806
  1357
    size_t i, expanded_len, sample_count;
slouken@12806
  1358
    Uint8 *ptr;
slouken@12806
  1359
slouken@12806
  1360
    sample_count = (size_t)file->sampleframes;
slouken@12809
  1361
    if (SafeMult(&sample_count, format->channels)) {
slouken@12806
  1362
        return SDL_OutOfMemory();
slouken@12806
  1363
    }
slouken@12806
  1364
slouken@12806
  1365
    expanded_len = sample_count;
slouken@12809
  1366
    if (SafeMult(&expanded_len, sizeof(Sint32))) {
slouken@12806
  1367
        return SDL_OutOfMemory();
slouken@12806
  1368
    } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
slouken@12806
  1369
        return SDL_SetError("WAVE file too big");
slouken@12806
  1370
    }
slouken@12806
  1371
slouken@12806
  1372
    ptr = (Uint8 *)SDL_realloc(chunk->data, expanded_len);
slouken@12806
  1373
    if (ptr == NULL) {
slouken@12806
  1374
        return SDL_OutOfMemory();
slouken@12806
  1375
    }
slouken@12806
  1376
slouken@12806
  1377
    /* This pointer is now invalid. */
slouken@12806
  1378
    chunk->data = NULL;
slouken@12806
  1379
    chunk->size = 0;
slouken@12806
  1380
slouken@12806
  1381
    *audio_buf = ptr;
slouken@12806
  1382
    *audio_len = (Uint32)expanded_len;
slouken@12806
  1383
slouken@12806
  1384
    /* work from end to start, since we're expanding in-place. */
slouken@12806
  1385
    for (i = sample_count; i > 0; i--) {
slouken@12806
  1386
        const size_t o = i - 1;
slouken@12806
  1387
        uint8_t b[4];
slouken@12806
  1388
slouken@12806
  1389
        b[0] = 0;
slouken@12806
  1390
        b[1] = ptr[o * 3];
slouken@12806
  1391
        b[2] = ptr[o * 3 + 1];
slouken@12806
  1392
        b[3] = ptr[o * 3 + 2];
slouken@12806
  1393
slouken@12806
  1394
        ptr[o * 4 + 0] = b[0];
slouken@12806
  1395
        ptr[o * 4 + 1] = b[1];
slouken@12806
  1396
        ptr[o * 4 + 2] = b[2];
slouken@12806
  1397
        ptr[o * 4 + 3] = b[3];
slouken@12806
  1398
    }
slouken@12806
  1399
slouken@12806
  1400
    return 0;
slouken@12806
  1401
}
slouken@12806
  1402
slouken@12806
  1403
static int
slouken@12806
  1404
PCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
slouken@12806
  1405
{
slouken@12806
  1406
    WaveFormat *format = &file->format;
slouken@12806
  1407
    WaveChunk *chunk = &file->chunk;
slouken@12806
  1408
    size_t outputsize;
slouken@12806
  1409
slouken@12806
  1410
    if (chunk->length != chunk->size) {
slouken@12806
  1411
        file->sampleframes = WaveAdjustToFactValue(file, chunk->size / format->blockalign);
slouken@12806
  1412
        if (file->sampleframes < 0) {
slouken@12806
  1413
            return -1;
slouken@12806
  1414
        }
slouken@12806
  1415
    }
slouken@12806
  1416
slouken@12806
  1417
    /* Nothing to decode, nothing to return. */
slouken@12806
  1418
    if (file->sampleframes == 0) {
slouken@12806
  1419
        *audio_buf = NULL;
slouken@12806
  1420
        *audio_len = 0;
slouken@12806
  1421
        return 0;
slouken@12806
  1422
    }
slouken@12806
  1423
slouken@12806
  1424
    /* 24-bit samples get shifted to 32 bits. */
slouken@12806
  1425
    if (format->encoding == PCM_CODE && format->bitspersample == 24) {
slouken@12806
  1426
        return PCM_ConvertSint24ToSint32(file, audio_buf, audio_len);
slouken@12806
  1427
    }
slouken@12806
  1428
slouken@12806
  1429
    outputsize = (size_t)file->sampleframes;
slouken@12809
  1430
    if (SafeMult(&outputsize, format->blockalign)) {
slouken@12806
  1431
        return SDL_OutOfMemory();
slouken@12806
  1432
    } else if (outputsize > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
slouken@12806
  1433
        return SDL_SetError("WAVE file too big");
slouken@12806
  1434
    }
slouken@12806
  1435
slouken@12806
  1436
    *audio_buf = chunk->data;
slouken@12806
  1437
    *audio_len = (Uint32)outputsize;
slouken@12806
  1438
slouken@12806
  1439
    /* This pointer is going to be returned to the caller. Prevent free in cleanup. */
slouken@12806
  1440
    chunk->data = NULL;
slouken@12806
  1441
    chunk->size = 0;
slouken@12806
  1442
slouken@12806
  1443
    return 0;
slouken@12806
  1444
}
slouken@12806
  1445
slouken@12806
  1446
static WaveRiffSizeHint
slouken@12806
  1447
WaveGetRiffSizeHint()
slouken@12806
  1448
{
slouken@12806
  1449
    const char *hint = SDL_GetHint(SDL_HINT_WAVE_RIFF_CHUNK_SIZE);
slouken@12806
  1450
slouken@12806
  1451
    if (hint != NULL) {
slouken@12809
  1452
        if (SDL_strcmp(hint, "force") == 0) {
slouken@12809
  1453
            return RiffSizeForce;
slouken@12806
  1454
        } else if (SDL_strcmp(hint, "ignore") == 0) {
slouken@12806
  1455
            return RiffSizeIgnore;
slouken@12806
  1456
        } else if (SDL_strcmp(hint, "ignorezero") == 0) {
slouken@12806
  1457
            return RiffSizeIgnoreZero;
slouken@12806
  1458
        } else if (SDL_strcmp(hint, "maximum") == 0) {
slouken@12806
  1459
            return RiffSizeMaximum;
slouken@12806
  1460
        }
slouken@12806
  1461
    }
slouken@12806
  1462
slouken@12806
  1463
    return RiffSizeNoHint;
slouken@12806
  1464
}
slouken@12806
  1465
slouken@12806
  1466
static WaveTruncationHint
slouken@12806
  1467
WaveGetTruncationHint()
slouken@12806
  1468
{
slouken@12806
  1469
    const char *hint = SDL_GetHint(SDL_HINT_WAVE_TRUNCATION);
slouken@12806
  1470
slouken@12806
  1471
    if (hint != NULL) {
slouken@12806
  1472
        if (SDL_strcmp(hint, "verystrict") == 0) {
slouken@12806
  1473
            return TruncVeryStrict;
slouken@12806
  1474
        } else if (SDL_strcmp(hint, "strict") == 0) {
slouken@12806
  1475
            return TruncStrict;
slouken@12806
  1476
        } else if (SDL_strcmp(hint, "dropframe") == 0) {
slouken@12806
  1477
            return TruncDropFrame;
slouken@12806
  1478
        } else if (SDL_strcmp(hint, "dropblock") == 0) {
slouken@12806
  1479
            return TruncDropBlock;
slouken@12806
  1480
        }
slouken@12806
  1481
    }
slouken@12806
  1482
slouken@12806
  1483
    return TruncNoHint;
slouken@12806
  1484
}
slouken@12806
  1485
slouken@12806
  1486
static WaveFactChunkHint
slouken@12806
  1487
WaveGetFactChunkHint()
slouken@12806
  1488
{
slouken@12806
  1489
    const char *hint = SDL_GetHint(SDL_HINT_WAVE_FACT_CHUNK);
slouken@12806
  1490
slouken@12806
  1491
    if (hint != NULL) {
slouken@12806
  1492
        if (SDL_strcmp(hint, "truncate") == 0) {
slouken@12806
  1493
            return FactTruncate;
slouken@12806
  1494
        } else if (SDL_strcmp(hint, "strict") == 0) {
slouken@12806
  1495
            return FactStrict;
slouken@12806
  1496
        } else if (SDL_strcmp(hint, "ignorezero") == 0) {
slouken@12806
  1497
            return FactIgnoreZero;
slouken@12806
  1498
        } else if (SDL_strcmp(hint, "ignore") == 0) {
slouken@12806
  1499
            return FactIgnore;
slouken@12806
  1500
        }
slouken@12806
  1501
    }
slouken@12806
  1502
slouken@12806
  1503
    return FactNoHint;
slouken@12806
  1504
}
slouken@12806
  1505
slouken@12806
  1506
static void
slouken@12806
  1507
WaveFreeChunkData(WaveChunk *chunk)
slouken@12806
  1508
{
slouken@12806
  1509
    if (chunk->data != NULL) {
slouken@12806
  1510
        SDL_free(chunk->data);
slouken@12806
  1511
        chunk->data = NULL;
slouken@12806
  1512
    }
slouken@12806
  1513
    chunk->size = 0;
slouken@12806
  1514
}
slouken@12806
  1515
slouken@12806
  1516
static int
slouken@12806
  1517
WaveNextChunk(SDL_RWops *src, WaveChunk *chunk)
slouken@12806
  1518
{
slouken@12806
  1519
    Uint32 chunkheader[2];
slouken@12806
  1520
    Sint64 nextposition = chunk->position + chunk->length;
slouken@12806
  1521
slouken@12806
  1522
    /* Data is no longer valid after this function returns. */
slouken@12806
  1523
    WaveFreeChunkData(chunk);
slouken@12806
  1524
slouken@12809
  1525
    /* Error on overflows. */
slouken@12809
  1526
    if (SDL_MAX_SINT64 - chunk->length < chunk->position || SDL_MAX_SINT64 - 8 < nextposition) {
slouken@12809
  1527
        return -1;
slouken@12809
  1528
    }
slouken@12809
  1529
slouken@12806
  1530
    /* RIFF chunks have a 2-byte alignment. Skip padding byte. */
slouken@12806
  1531
    if (chunk->length & 1) {
slouken@12806
  1532
        nextposition++;
slouken@12806
  1533
    }
slouken@12806
  1534
slouken@12806
  1535
    if (SDL_RWseek(src, nextposition, RW_SEEK_SET) != nextposition) {
slouken@12806
  1536
        /* Not sure how we ended up here. Just abort. */
slouken@12806
  1537
        return -2;
slouken@12806
  1538
    } else if (SDL_RWread(src, chunkheader, 4, 2) != 2) {
slouken@12806
  1539
        return -1;
slouken@12806
  1540
    }
slouken@12806
  1541
slouken@12806
  1542
    chunk->fourcc = SDL_SwapLE32(chunkheader[0]);
slouken@12806
  1543
    chunk->length = SDL_SwapLE32(chunkheader[1]);
slouken@12806
  1544
    chunk->position = nextposition + 8;
slouken@12806
  1545
slouken@12806
  1546
    return 0;
slouken@12806
  1547
}
slouken@12806
  1548
slouken@12806
  1549
static int
slouken@12806
  1550
WaveReadPartialChunkData(SDL_RWops *src, WaveChunk *chunk, size_t length)
slouken@12806
  1551
{
slouken@12806
  1552
    WaveFreeChunkData(chunk);
slouken@12806
  1553
slouken@12806
  1554
    if (length > chunk->length) {
slouken@12806
  1555
        length = chunk->length;
slouken@12806
  1556
    }
slouken@12806
  1557
slouken@12806
  1558
    if (length > 0) {
slouken@12806
  1559
        chunk->data = SDL_malloc(length);
slouken@12806
  1560
        if (chunk->data == NULL) {
slouken@12806
  1561
            return SDL_OutOfMemory();
slouken@12806
  1562
        }
slouken@12806
  1563
slouken@12806
  1564
        if (SDL_RWseek(src, chunk->position, RW_SEEK_SET) != chunk->position) {
slouken@12806
  1565
            /* Not sure how we ended up here. Just abort. */
slouken@12806
  1566
            return -2;
slouken@12806
  1567
        }
slouken@12806
  1568
slouken@12806
  1569
        chunk->size = SDL_RWread(src, chunk->data, 1, length);
slouken@12806
  1570
        if (chunk->size != length) {
slouken@12806
  1571
            /* Expected to be handled by the caller. */
slouken@12806
  1572
        }
slouken@12806
  1573
    }
slouken@12806
  1574
slouken@12806
  1575
    return 0;
slouken@12806
  1576
}
slouken@12806
  1577
slouken@12806
  1578
static int
slouken@12806
  1579
WaveReadChunkData(SDL_RWops *src, WaveChunk *chunk)
slouken@12806
  1580
{
slouken@12806
  1581
    return WaveReadPartialChunkData(src, chunk, chunk->length);
slouken@12806
  1582
}
slouken@12806
  1583
slouken@12806
  1584
typedef struct WaveExtensibleGUID {
slouken@12806
  1585
    Uint16 encoding;
slouken@12806
  1586
    Uint8 guid[16];
slouken@12806
  1587
} WaveExtensibleGUID;
slouken@12806
  1588
slouken@12806
  1589
/* Some of the GUIDs that are used by WAVEFORMATEXTENSIBLE. */
slouken@12806
  1590
#define WAVE_FORMATTAG_GUID(tag) {(tag) & 0xff, (tag) >> 8, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113}
slouken@12806
  1591
static WaveExtensibleGUID extensible_guids[] = {
slouken@12806
  1592
    {PCM_CODE,        WAVE_FORMATTAG_GUID(PCM_CODE)},
slouken@12806
  1593
    {MS_ADPCM_CODE,   WAVE_FORMATTAG_GUID(MS_ADPCM_CODE)},
slouken@12806
  1594
    {IEEE_FLOAT_CODE, WAVE_FORMATTAG_GUID(IEEE_FLOAT_CODE)},
slouken@12806
  1595
    {ALAW_CODE,       WAVE_FORMATTAG_GUID(ALAW_CODE)},
slouken@12806
  1596
    {MULAW_CODE,      WAVE_FORMATTAG_GUID(MULAW_CODE)},
slouken@12806
  1597
    {IMA_ADPCM_CODE,  WAVE_FORMATTAG_GUID(IMA_ADPCM_CODE)}
slouken@12806
  1598
};
slouken@12806
  1599
slouken@12806
  1600
static Uint16
slouken@12806
  1601
WaveGetFormatGUIDEncoding(WaveFormat *format)
slouken@12806
  1602
{
slouken@12806
  1603
    size_t i;
slouken@12806
  1604
    for (i = 0; i < SDL_arraysize(extensible_guids); i++) {
slouken@12806
  1605
        if (SDL_memcmp(format->subformat, extensible_guids[i].guid, 16) == 0) {
slouken@12806
  1606
            return extensible_guids[i].encoding;
slouken@12806
  1607
        }
slouken@12806
  1608
    }
slouken@12806
  1609
    return UNKNOWN_CODE;
slouken@12806
  1610
}
slouken@12806
  1611
slouken@12806
  1612
static int
slouken@12806
  1613
WaveReadFormat(WaveFile *file)
slouken@12806
  1614
{
slouken@12806
  1615
    WaveChunk *chunk = &file->chunk;
slouken@12806
  1616
    WaveFormat *format = &file->format;
slouken@12806
  1617
    SDL_RWops *fmtsrc;
slouken@12806
  1618
    size_t fmtlen = chunk->size;
slouken@12806
  1619
slouken@12806
  1620
    if (fmtlen > SDL_MAX_SINT32) {
slouken@12806
  1621
        /* Limit given by SDL_RWFromConstMem. */
slouken@12806
  1622
        return SDL_SetError("Data of WAVE fmt chunk too big");
slouken@12806
  1623
    }
slouken@12806
  1624
    fmtsrc = SDL_RWFromConstMem(chunk->data, (int)chunk->size);
slouken@12806
  1625
    if (fmtsrc == NULL) {
slouken@12806
  1626
        return SDL_OutOfMemory();
slouken@12806
  1627
    }
slouken@12806
  1628
slouken@12806
  1629
    format->formattag = SDL_ReadLE16(fmtsrc);
slouken@12806
  1630
    format->encoding = format->formattag;
slouken@12806
  1631
    format->channels = SDL_ReadLE16(fmtsrc);
slouken@12806
  1632
    format->frequency = SDL_ReadLE32(fmtsrc);
slouken@12806
  1633
    format->byterate = SDL_ReadLE32(fmtsrc);
slouken@12806
  1634
    format->blockalign = SDL_ReadLE16(fmtsrc);
slouken@12806
  1635
slouken@12806
  1636
    /* This is PCM specific in the first version of the specification. */
slouken@12806
  1637
    if (fmtlen >= 16) {
slouken@12806
  1638
        format->bitspersample = SDL_ReadLE16(fmtsrc);
slouken@12806
  1639
    } else if (format->encoding == PCM_CODE) {
slouken@12806
  1640
        SDL_RWclose(fmtsrc);
slouken@12806
  1641
        return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
slouken@12806
  1642
    }
slouken@12806
  1643
slouken@12806
  1644
    /* The earlier versions also don't have this field. */
slouken@12806
  1645
    if (fmtlen >= 18) {
slouken@12806
  1646
        format->extsize = SDL_ReadLE16(fmtsrc);
slouken@12806
  1647
    }
slouken@12806
  1648
slouken@12806
  1649
    if (format->formattag == EXTENSIBLE_CODE) {
slouken@12806
  1650
        /* note that this ignores channel masks, smaller valid bit counts
slouken@12806
  1651
         * inside a larger container, and most subtypes. This is just enough
slouken@12806
  1652
         * to get things that didn't really _need_ WAVE_FORMAT_EXTENSIBLE
slouken@12806
  1653
         * to be useful working when they use this format flag.
slouken@12806
  1654
         */
slouken@12806
  1655
slouken@12806
  1656
        /* Extensible header must be at least 22 bytes. */
slouken@12806
  1657
        if (fmtlen < 40 || format->extsize < 22) {
slouken@12806
  1658
            SDL_RWclose(fmtsrc);
slouken@12806
  1659
            return SDL_SetError("Extensible WAVE header too small");
slouken@12806
  1660
        }
slouken@12806
  1661
slouken@12806
  1662
        format->validsamplebits = SDL_ReadLE16(fmtsrc);
slouken@12806
  1663
        format->samplesperblock = format->validsamplebits;
slouken@12806
  1664
        format->channelmask = SDL_ReadLE32(fmtsrc);
slouken@12806
  1665
        SDL_RWread(fmtsrc, format->subformat, 1, 16);
slouken@12806
  1666
        format->encoding = WaveGetFormatGUIDEncoding(format);
slouken@12806
  1667
    }
slouken@12806
  1668
slouken@12806
  1669
    SDL_RWclose(fmtsrc);
slouken@12806
  1670
slouken@12806
  1671
    return 0;
slouken@12806
  1672
}
slouken@12806
  1673
slouken@12806
  1674
static int
slouken@12806
  1675
WaveCheckFormat(WaveFile *file, size_t datalength)
slouken@12806
  1676
{
slouken@12806
  1677
    WaveFormat *format = &file->format;
slouken@12806
  1678
slouken@12806
  1679
    /* Check for some obvious issues. */
slouken@12806
  1680
slouken@12806
  1681
    if (format->channels == 0) {
slouken@12806
  1682
        return SDL_SetError("Invalid number of channels");
slouken@12806
  1683
    } else if (format->channels > 255) {
slouken@12806
  1684
        /* Limit given by SDL_AudioSpec.channels. */
slouken@12806
  1685
        return SDL_SetError("Number of channels exceeds limit of 255");
slouken@12806
  1686
    }
slouken@12806
  1687
slouken@12806
  1688
    if (format->frequency == 0) {
slouken@12806
  1689
        return SDL_SetError("Invalid sample rate");
slouken@12806
  1690
    } else if (format->frequency > INT_MAX) {
slouken@12806
  1691
        /* Limit given by SDL_AudioSpec.freq. */
slouken@12806
  1692
        return SDL_SetError("Sample rate exceeds limit of %d", INT_MAX);
slouken@12806
  1693
    }
slouken@12806
  1694
slouken@12806
  1695
    /* Reject invalid fact chunks in strict mode. */
slouken@12806
  1696
    if (file->facthint == FactStrict && file->fact.status == -1) {
slouken@12806
  1697
        return SDL_SetError("Invalid fact chunk in WAVE file");
slouken@12806
  1698
    }
slouken@12806
  1699
slouken@12809
  1700
    /* Check for issues common to all encodings. Some unsupported formats set
slouken@12806
  1701
     * the bits per sample to zero. These fall through to the 'unsupported
slouken@12806
  1702
     * format' error.
slouken@12806
  1703
     */
slouken@12806
  1704
    switch (format->encoding) {
slouken@12806
  1705
    case IEEE_FLOAT_CODE:
slouken@12806
  1706
    case ALAW_CODE:
slouken@12806
  1707
    case MULAW_CODE:
slouken@12806
  1708
    case MS_ADPCM_CODE:
slouken@12806
  1709
    case IMA_ADPCM_CODE:
slouken@12806
  1710
        /* These formats require a fact chunk. */
slouken@12806
  1711
        if (file->facthint == FactStrict && file->fact.status <= 0) {
slouken@12806
  1712
            return SDL_SetError("Missing fact chunk in WAVE file");
slouken@12806
  1713
        }
slouken@12806
  1714
        /* fallthrough */
slouken@12806
  1715
    case PCM_CODE:
slouken@12806
  1716
        /* All supported formats require a non-zero bit depth. */
slouken@12806
  1717
        if (file->chunk.size < 16) {
slouken@12806
  1718
            return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
slouken@12806
  1719
        } else if (format->bitspersample == 0) {
slouken@12806
  1720
            return SDL_SetError("Invalid bits per sample");
slouken@12806
  1721
        }
slouken@12806
  1722
slouken@12806
  1723
        /* All supported formats must have a proper block size. */
slouken@12806
  1724
        if (format->blockalign == 0) {
slouken@12806
  1725
            return SDL_SetError("Invalid block alignment");
slouken@12806
  1726
        }
slouken@12806
  1727
slouken@12806
  1728
        /* If the fact chunk is valid and the appropriate hint is set, the
slouken@12806
  1729
         * decoders will use the number of sample frames from the fact chunk.
slouken@12806
  1730
         */
slouken@12806
  1731
        if (file->fact.status == 1) {
slouken@12806
  1732
            WaveFactChunkHint hint = file->facthint;
slouken@12806
  1733
            Uint32 samples = file->fact.samplelength;
slouken@12806
  1734
            if (hint == FactTruncate || hint == FactStrict || (hint == FactIgnoreZero && samples > 0)) {
slouken@12806
  1735
                file->fact.status = 2;
slouken@12806
  1736
            }
slouken@12806
  1737
        }
slouken@12806
  1738
    }
slouken@12806
  1739
slouken@12806
  1740
    /* Check the format for encoding specific issues and initialize decoders. */
slouken@12806
  1741
    switch (format->encoding) {
slouken@12806
  1742
    case PCM_CODE:
slouken@12806
  1743
    case IEEE_FLOAT_CODE:
slouken@12806
  1744
        if (PCM_Init(file, datalength) < 0) {
slouken@12806
  1745
            return -1;
slouken@12806
  1746
        }
slouken@12806
  1747
        break;
slouken@12806
  1748
    case ALAW_CODE:
slouken@12806
  1749
    case MULAW_CODE:
slouken@12806
  1750
        if (LAW_Init(file, datalength) < 0) {
slouken@12806
  1751
            return -1;
slouken@12806
  1752
        }
slouken@12806
  1753
        break;
slouken@12806
  1754
    case MS_ADPCM_CODE:
slouken@12806
  1755
        if (MS_ADPCM_Init(file, datalength) < 0) {
slouken@12806
  1756
            return -1;
slouken@12806
  1757
        }
slouken@12806
  1758
        break;
slouken@12806
  1759
    case IMA_ADPCM_CODE:
slouken@12806
  1760
        if (IMA_ADPCM_Init(file, datalength) < 0) {
slouken@12806
  1761
            return -1;
slouken@12806
  1762
        }
slouken@12806
  1763
        break;
slouken@12806
  1764
    case MPEG_CODE:
slouken@12806
  1765
    case MPEGLAYER3_CODE:
slouken@12806
  1766
        return SDL_SetError("MPEG formats not supported");
slouken@12806
  1767
    default:
slouken@12806
  1768
        if (format->formattag == EXTENSIBLE_CODE) {
slouken@12806
  1769
            const char *errstr = "Unknown WAVE format GUID: %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x";
slouken@12806
  1770
            const Uint8 *g = format->subformat;
slouken@12806
  1771
            const Uint32 g1 = g[0] | ((Uint32)g[1] << 8) | ((Uint32)g[2] << 16) | ((Uint32)g[3] << 24);
slouken@12806
  1772
            const Uint32 g2 = g[4] | ((Uint32)g[5] << 8);
slouken@12806
  1773
            const Uint32 g3 = g[6] | ((Uint32)g[7] << 8);
slouken@12806
  1774
            return SDL_SetError(errstr, g1, g2, g3, g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15]);
slouken@12806
  1775
        }
slouken@12809
  1776
        return SDL_SetError("Unknown WAVE format tag: 0x%04x", (unsigned int)format->encoding);
slouken@12806
  1777
    }
slouken@12806
  1778
slouken@12806
  1779
    return 0;
slouken@12806
  1780
}
slouken@12806
  1781
slouken@12806
  1782
static int
slouken@12806
  1783
WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
slouken@12806
  1784
{
slouken@12806
  1785
    int result;
slouken@12806
  1786
    Uint32 chunkcount = 0;
slouken@12806
  1787
    Uint32 chunkcountlimit = 10000;
slouken@12806
  1788
    char *envchunkcountlimit;
slouken@12806
  1789
    Sint64 RIFFstart, RIFFend, lastchunkpos;
slouken@12806
  1790
    SDL_bool RIFFlengthknown = SDL_FALSE;
slouken@12806
  1791
    WaveFormat *format = &file->format;
slouken@12806
  1792
    WaveChunk *chunk = &file->chunk;
slouken@12806
  1793
    WaveChunk RIFFchunk = {0};
slouken@12806
  1794
    WaveChunk fmtchunk = {0};
slouken@12806
  1795
    WaveChunk datachunk = {0};
slouken@12806
  1796
slouken@12806
  1797
    envchunkcountlimit = SDL_getenv("SDL_WAVE_CHUNK_LIMIT");
slouken@12806
  1798
    if (envchunkcountlimit != NULL) {
slouken@12806
  1799
        unsigned int count;
slouken@12806
  1800
        if (SDL_sscanf(envchunkcountlimit, "%u", &count) == 1) {
slouken@12806
  1801
            chunkcountlimit = count <= SDL_MAX_UINT32 ? count : SDL_MAX_UINT32;
slouken@12806
  1802
        }
slouken@12806
  1803
    }
slouken@12806
  1804
slouken@12806
  1805
    RIFFstart = SDL_RWtell(src);
slouken@12806
  1806
    if (RIFFstart < 0) {
slouken@12806
  1807
        return SDL_SetError("Could not seek in file");
slouken@12806
  1808
    }
slouken@12806
  1809
slouken@12806
  1810
    RIFFchunk.position = RIFFstart;
slouken@12806
  1811
    if (WaveNextChunk(src, &RIFFchunk) < 0) {
slouken@12806
  1812
        return SDL_SetError("Could not read RIFF header");
slouken@12806
  1813
    }
slouken@12806
  1814
slouken@12806
  1815
    /* Check main WAVE file identifiers. */
slouken@12806
  1816
    if (RIFFchunk.fourcc == RIFF) {
slouken@12806
  1817
        Uint32 formtype;
slouken@12806
  1818
        /* Read the form type. "WAVE" expected. */
slouken@12806
  1819
        if (SDL_RWread(src, &formtype, sizeof(Uint32), 1) != 1) {
slouken@12806
  1820
            return SDL_SetError("Could not read RIFF form type");
slouken@12806
  1821
        } else if (SDL_SwapLE32(formtype) != WAVE) {
slouken@12806
  1822
            return SDL_SetError("RIFF form type is not WAVE (not a Waveform file)");
slouken@12806
  1823
        }
slouken@12806
  1824
    } else if (RIFFchunk.fourcc == WAVE) {
slouken@12806
  1825
        /* RIFF chunk missing or skipped. Length unknown. */
slouken@12806
  1826
        RIFFchunk.position = 0;
slouken@12806
  1827
        RIFFchunk.length = 0;
slouken@1895
  1828
    } else {
slouken@12806
  1829
        return SDL_SetError("Could not find RIFF or WAVE identifiers (not a Waveform file)");
slouken@1895
  1830
    }
slouken@12806
  1831
slouken@12806
  1832
    /* The 4-byte form type is immediately followed by the first chunk.*/
slouken@12806
  1833
    chunk->position = RIFFchunk.position + 4;
slouken@12806
  1834
slouken@12806
  1835
    /* Use the RIFF chunk size to limit the search for the chunks. This is not
slouken@12806
  1836
     * always reliable and the hint can be used to tune the behavior. By
slouken@12806
  1837
     * default, it will never search past 4 GiB.
slouken@12806
  1838
     */
slouken@12806
  1839
    switch (file->riffhint) {
slouken@12806
  1840
    case RiffSizeIgnore:
slouken@12806
  1841
        RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
slouken@12806
  1842
        break;
slouken@12806
  1843
    default:
slouken@12806
  1844
    case RiffSizeIgnoreZero:
slouken@12806
  1845
        if (RIFFchunk.length == 0) {
slouken@12806
  1846
            RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
slouken@12806
  1847
            break;
slouken@12806
  1848
        }
slouken@12806
  1849
        /* fallthrough */
slouken@12809
  1850
    case RiffSizeForce:
slouken@12806
  1851
        RIFFend = RIFFchunk.position + RIFFchunk.length;
slouken@12806
  1852
        RIFFlengthknown = SDL_TRUE;
slouken@12806
  1853
        break;
slouken@12806
  1854
    case RiffSizeMaximum:
slouken@12806
  1855
        RIFFend = SDL_MAX_SINT64;
slouken@12806
  1856
        break;
slouken@1895
  1857
    }
slouken@0
  1858
slouken@12806
  1859
    /* Step through all chunks and save information on the fmt, data, and fact
slouken@12806
  1860
     * chunks. Ignore the chunks we don't know as per specification. This
slouken@12806
  1861
     * currently also ignores cue, list, and slnt chunks.
slouken@12806
  1862
     */
slouken@12809
  1863
    while ((Uint64)RIFFend > (Uint64)chunk->position + chunk->length + (chunk->length & 1)) {
slouken@12806
  1864
        /* Abort after too many chunks or else corrupt files may waste time. */
slouken@12806
  1865
        if (chunkcount++ >= chunkcountlimit) {
slouken@12806
  1866
            return SDL_SetError("Chunk count in WAVE file exceeds limit of %u", chunkcountlimit);
slouken@1895
  1867
        }
slouken@0
  1868
slouken@12806
  1869
        result = WaveNextChunk(src, chunk);
slouken@12806
  1870
        if (result == -1) {
slouken@12806
  1871
            /* Unexpected EOF. Corrupt file or I/O issues. */
slouken@12806
  1872
            if (file->trunchint == TruncVeryStrict) {
slouken@12806
  1873
                return SDL_SetError("Unexpected end of WAVE file");
slouken@12806
  1874
            }
slouken@12806
  1875
            /* Let the checks after this loop sort this issue out. */
slouken@12806
  1876
            break;
slouken@12806
  1877
        } else if (result == -2) {
slouken@12806
  1878
            return SDL_SetError("Could not seek to WAVE chunk header");
slouken@12806
  1879
        }
slouken@12806
  1880
slouken@12806
  1881
        if (chunk->fourcc == FMT) {
slouken@12806
  1882
            if (fmtchunk.fourcc == FMT) {
slouken@12806
  1883
                /* Multiple fmt chunks. Ignore or error? */
slouken@12806
  1884
            } else {
slouken@12806
  1885
                /* The fmt chunk must occur before the data chunk. */
slouken@12806
  1886
                if (datachunk.fourcc == DATA) {
slouken@12806
  1887
                    return SDL_SetError("fmt chunk after data chunk in WAVE file");
slouken@12806
  1888
                }
slouken@12806
  1889
                fmtchunk = *chunk;
slouken@12806
  1890
            }
slouken@12806
  1891
        } else if (chunk->fourcc == DATA) {
slouken@12806
  1892
            /* Only use the first data chunk. Handling the wavl list madness
slouken@12806
  1893
             * may require a different approach.
slouken@12806
  1894
             */
slouken@12806
  1895
            if (datachunk.fourcc != DATA) {
slouken@12806
  1896
                datachunk = *chunk;
slouken@12806
  1897
            }
slouken@12806
  1898
        } else if (chunk->fourcc == FACT) {
slouken@12806
  1899
            /* The fact chunk data must be at least 4 bytes for the
slouken@12806
  1900
             * dwSampleLength field. Ignore all fact chunks after the first one.
slouken@12806
  1901
             */
slouken@12806
  1902
            if (file->fact.status == 0) {
slouken@12806
  1903
                if (chunk->length < 4) {
slouken@12806
  1904
                    file->fact.status = -1;
slouken@12806
  1905
                } else {
slouken@12806
  1906
                    /* Let's use src directly, it's just too convenient. */
slouken@12806
  1907
                    Sint64 position = SDL_RWseek(src, chunk->position, RW_SEEK_SET);
slouken@12806
  1908
                    Uint32 samplelength;
slouken@12806
  1909
                    if (position == chunk->position && SDL_RWread(src, &samplelength, sizeof(Uint32), 1) == 1) {
slouken@12806
  1910
                        file->fact.status = 1;
slouken@12806
  1911
                        file->fact.samplelength = SDL_SwapLE32(samplelength);
slouken@12806
  1912
                    } else {
slouken@12806
  1913
                        file->fact.status = -1;
slouken@12806
  1914
                    }
slouken@12806
  1915
                }
slouken@12806
  1916
            }
slouken@12806
  1917
        }
slouken@12806
  1918
slouken@12806
  1919
        /* Go through all chunks in verystrict mode or stop the search early if
slouken@12806
  1920
         * all required chunks were found.
slouken@12806
  1921
         */
slouken@12806
  1922
        if (file->trunchint == TruncVeryStrict) {
slouken@12809
  1923
            if ((Uint64)RIFFend < (Uint64)chunk->position + chunk->length) {
slouken@12806
  1924
                return SDL_SetError("RIFF size truncates chunk");
slouken@12806
  1925
            }
slouken@12806
  1926
        } else if (fmtchunk.fourcc == FMT && datachunk.fourcc == DATA) {
slouken@12806
  1927
            if (file->fact.status == 1 || file->facthint == FactIgnore || file->facthint == FactNoHint) {
slouken@12806
  1928
                break;
slouken@12806
  1929
            }
slouken@12806
  1930
        }
slouken@1895
  1931
    }
slouken@12806
  1932
slouken@12806
  1933
    /* Save the position after the last chunk. This position will be used if the
slouken@12806
  1934
     * RIFF length is unknown.
slouken@12806
  1935
     */
slouken@12806
  1936
    lastchunkpos = chunk->position + chunk->length;
slouken@12806
  1937
slouken@12806
  1938
    /* The fmt chunk is mandatory. */
slouken@12806
  1939
    if (fmtchunk.fourcc != FMT) {
slouken@12806
  1940
        return SDL_SetError("Missing fmt chunk in WAVE file");
slouken@12806
  1941
    }
slouken@12806
  1942
    /* A data chunk must be present. */
slouken@12806
  1943
    if (datachunk.fourcc != DATA) {
slouken@12806
  1944
        return SDL_SetError("Missing data chunk in WAVE file");
slouken@12806
  1945
    }
slouken@12806
  1946
    /* Check if the last chunk has all of its data in verystrict mode. */
slouken@12806
  1947
    if (file->trunchint == TruncVeryStrict) {
slouken@12806
  1948
        /* data chunk is handled later. */
slouken@12806
  1949
        if (chunk->fourcc != DATA && chunk->length > 0) {
slouken@12806
  1950
            Uint8 tmp;
slouken@12809
  1951
            Uint64 position = (Uint64)chunk->position + chunk->length - 1;
slouken@12809
  1952
            if (position > SDL_MAX_SINT64 || SDL_RWseek(src, (Sint64)position, RW_SEEK_SET) != (Sint64)position) {
slouken@12806
  1953
                return SDL_SetError("Could not seek to WAVE chunk data");
slouken@12806
  1954
            } else if (SDL_RWread(src, &tmp, 1, 1) != 1) {
slouken@12806
  1955
                return SDL_SetError("RIFF size truncates chunk");
slouken@12806
  1956
            }
slouken@12806
  1957
        }
slouken@12806
  1958
    }
slouken@12806
  1959
slouken@12806
  1960
    /* Process fmt chunk. */
slouken@12806
  1961
    *chunk = fmtchunk;
slouken@12806
  1962
slouken@12806
  1963
    /* No need to read more than 1046 bytes of the fmt chunk data with the
slouken@12806
  1964
     * formats that are currently supported. (1046 because of MS ADPCM coefficients)
slouken@12806
  1965
     */
slouken@12806
  1966
    if (WaveReadPartialChunkData(src, chunk, 1046) < 0) {
slouken@12806
  1967
        return SDL_SetError("Could not read data of WAVE fmt chunk");
slouken@12806
  1968
    }
slouken@12806
  1969
slouken@12806
  1970
    /* The fmt chunk data must be at least 14 bytes to include all common fields.
slouken@12806
  1971
     * It usually is 16 and larger depending on the header and encoding.
slouken@12806
  1972
     */
slouken@12806
  1973
    if (chunk->length < 14) {
slouken@12806
  1974
        return SDL_SetError("Invalid WAVE fmt chunk length (too small)");
slouken@12806
  1975
    } else if (chunk->size < 14) {
slouken@12806
  1976
        return SDL_SetError("Could not read data of WAVE fmt chunk");
slouken@12806
  1977
    } else if (WaveReadFormat(file) < 0) {
slouken@12806
  1978
        return -1;
slouken@12806
  1979
    } else if (WaveCheckFormat(file, (size_t)datachunk.length) < 0) {
slouken@12806
  1980
        return -1;
slouken@12806
  1981
    }
slouken@12806
  1982
slouken@12806
  1983
#ifdef SDL_WAVE_DEBUG_LOG_FORMAT
slouken@12806
  1984
    WaveDebugLogFormat(file);
slouken@12806
  1985
#endif
slouken@12806
  1986
#ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
slouken@12806
  1987
    WaveDebugDumpFormat(file, RIFFchunk.length, fmtchunk.length, datachunk.length);
slouken@12806
  1988
#endif
slouken@12806
  1989
slouken@12806
  1990
    WaveFreeChunkData(chunk);
slouken@12806
  1991
slouken@12806
  1992
    /* Process data chunk. */
slouken@12806
  1993
    *chunk = datachunk;
slouken@12806
  1994
slouken@12806
  1995
    if (chunk->length > 0) {
slouken@12806
  1996
        result = WaveReadChunkData(src, chunk);
slouken@12806
  1997
        if (result == -1) {
slouken@12806
  1998
            return -1;
slouken@12806
  1999
        } else if (result == -2) {
slouken@12806
  2000
            return SDL_SetError("Could not seek data of WAVE data chunk");
slouken@12806
  2001
        }
slouken@12806
  2002
    }
slouken@12806
  2003
slouken@12806
  2004
    if (chunk->length != chunk->size) {
slouken@12806
  2005
        /* I/O issues or corrupt file. */
slouken@12806
  2006
        if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
slouken@12806
  2007
            return SDL_SetError("Could not read data of WAVE data chunk");
slouken@12806
  2008
        }
slouken@12806
  2009
        /* The decoders handle this truncation. */
slouken@12806
  2010
    }
slouken@12806
  2011
slouken@12806
  2012
    /* Decode or convert the data if necessary. */
slouken@12806
  2013
    switch (format->encoding) {
slouken@1895
  2014
    case PCM_CODE:
slouken@12806
  2015
    case IEEE_FLOAT_CODE:
slouken@12806
  2016
        if (PCM_Decode(file, audio_buf, audio_len) < 0) {
slouken@12806
  2017
            return -1;
slouken@12806
  2018
        }
slouken@12806
  2019
        break;
slouken@12806
  2020
    case ALAW_CODE:
slouken@12806
  2021
    case MULAW_CODE:
slouken@12806
  2022
        if (LAW_Decode(file, audio_buf, audio_len) < 0) {
slouken@12806
  2023
            return -1;
slouken@12806
  2024
        }
slouken@12806
  2025
        break;
slouken@12806
  2026
    case MS_ADPCM_CODE:
slouken@12806
  2027
        if (MS_ADPCM_Decode(file, audio_buf, audio_len) < 0) {
slouken@12806
  2028
            return -1;
slouken@12806
  2029
        }
slouken@12806
  2030
        break;
slouken@12806
  2031
    case IMA_ADPCM_CODE:
slouken@12806
  2032
        if (IMA_ADPCM_Decode(file, audio_buf, audio_len) < 0) {
slouken@12806
  2033
            return -1;
slouken@12806
  2034
        }
slouken@12806
  2035
        break;
slouken@12806
  2036
    }
slouken@12806
  2037
slouken@12806
  2038
    /* Setting up the SDL_AudioSpec. All unsupported formats were filtered out
slouken@12806
  2039
     * by checks earlier in this function.
slouken@12806
  2040
     */
slouken@12806
  2041
    SDL_zerop(spec);
slouken@12806
  2042
    spec->freq = format->frequency;
slouken@12806
  2043
    spec->channels = (Uint8)format->channels;
slouken@12806
  2044
    spec->samples = 4096;       /* Good default buffer size */
slouken@12806
  2045
slouken@12806
  2046
    switch (format->encoding) {
slouken@12806
  2047
    case MS_ADPCM_CODE:
slouken@12806
  2048
    case IMA_ADPCM_CODE:
slouken@12806
  2049
    case ALAW_CODE:
slouken@12806
  2050
    case MULAW_CODE:
slouken@12806
  2051
        /* These can be easily stored in the byte order of the system. */
slouken@12806
  2052
        spec->format = AUDIO_S16SYS;
slouken@1895
  2053
        break;
icculus@1994
  2054
    case IEEE_FLOAT_CODE:
slouken@12806
  2055
        spec->format = AUDIO_F32LSB;
icculus@1994
  2056
        break;
slouken@12806
  2057
    case PCM_CODE:
slouken@12806
  2058
        switch (format->bitspersample) {
icculus@1994
  2059
        case 8:
icculus@1994
  2060
            spec->format = AUDIO_U8;
icculus@1994
  2061
            break;
icculus@1994
  2062
        case 16:
slouken@12806
  2063
            spec->format = AUDIO_S16LSB;
icculus@1994
  2064
            break;
slouken@12806
  2065
        case 24: /* Has been shifted to 32 bits. */
icculus@1994
  2066
        case 32:
slouken@12806
  2067
            spec->format = AUDIO_S32LSB;
icculus@1994
  2068
            break;
icculus@1994
  2069
        default:
slouken@12806
  2070
            /* Just in case something unexpected happened in the checks. */
slouken@12809
  2071
            return SDL_SetError("Unexpected %u-bit PCM data format", (unsigned int)format->bitspersample);
slouken@1895
  2072
        }
slouken@12806
  2073
        break;
slouken@1895
  2074
    }
icculus@1994
  2075
slouken@12806
  2076
    /* Report the end position back to the cleanup code. */
slouken@12806
  2077
    if (RIFFlengthknown) {
slouken@12806
  2078
        chunk->position = RIFFend;
slouken@12806
  2079
    } else {
slouken@12806
  2080
        chunk->position = lastchunkpos;
slouken@1895
  2081
    }
slouken@0
  2082
slouken@12806
  2083
    return 0;
slouken@12806
  2084
}
slouken@12806
  2085
slouken@12806
  2086
SDL_AudioSpec *
slouken@12806
  2087
SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
slouken@12806
  2088
{
slouken@12806
  2089
    int result;
slouken@12807
  2090
    WaveFile file;
slouken@12807
  2091
slouken@12807
  2092
    SDL_zero(file);
slouken@12806
  2093
slouken@12806
  2094
    /* Make sure we are passed a valid data source */
slouken@12806
  2095
    if (src == NULL) {
slouken@12806
  2096
        /* Error may come from RWops. */
slouken@12806
  2097
        return NULL;
slouken@12806
  2098
    } else if (spec == NULL) {
slouken@12806
  2099
        SDL_InvalidParamError("spec");
slouken@12806
  2100
        return NULL;
slouken@12806
  2101
    } else if (audio_buf == NULL) {
slouken@12806
  2102
        SDL_InvalidParamError("audio_buf");
slouken@12806
  2103
        return NULL;
slouken@12806
  2104
    } else if (audio_len == NULL) {
slouken@12806
  2105
        SDL_InvalidParamError("audio_len");
slouken@12806
  2106
        return NULL;
icculus@11464
  2107
    }
icculus@11464
  2108
slouken@12806
  2109
    *audio_buf = NULL;
slouken@12806
  2110
    *audio_len = 0;
slouken@1895
  2111
slouken@12806
  2112
    file.riffhint = WaveGetRiffSizeHint();
slouken@12806
  2113
    file.trunchint = WaveGetTruncationHint();
slouken@12806
  2114
    file.facthint = WaveGetFactChunkHint();
slouken@12806
  2115
slouken@12806
  2116
    result = WaveLoad(src, &file, spec, audio_buf, audio_len);
slouken@12806
  2117
    if (result < 0) {
slouken@12806
  2118
        SDL_free(*audio_buf);
slouken@12806
  2119
        spec = NULL;
slouken@12806
  2120
        audio_buf = NULL;
slouken@12806
  2121
        audio_len = 0;
slouken@1895
  2122
    }
slouken@12806
  2123
slouken@12806
  2124
    /* Cleanup */
slouken@12806
  2125
    if (freesrc) {
slouken@12806
  2126
        SDL_RWclose(src);
slouken@12806
  2127
    } else {
slouken@12806
  2128
        SDL_RWseek(src, file.chunk.position, RW_SEEK_SET);
slouken@1895
  2129
    }
slouken@12806
  2130
    WaveFreeChunkData(&file.chunk);
slouken@12806
  2131
    SDL_free(file.decoderdata);
slouken@12806
  2132
slouken@12806
  2133
    return spec;
slouken@0
  2134
}
slouken@0
  2135
slouken@0
  2136
/* Since the WAV memory is allocated in the shared library, it must also
slouken@0
  2137
   be freed here.  (Necessary under Win32, VC++)
slouken@0
  2138
 */
slouken@1895
  2139
void
slouken@12806
  2140
SDL_FreeWAV(Uint8 *audio_buf)
slouken@0
  2141
{
slouken@7719
  2142
    SDL_free(audio_buf);
slouken@0
  2143
}
slouken@0
  2144
slouken@1895
  2145
/* vi: set ts=4 sw=4 expandtab: */