src/audio/sdlgenaudiocvt.pl
author Ryan C. Gordon
Mon, 28 Dec 2009 08:28:24 +0000
changeset 3602 bfa8d34ce03a
parent 3032 77c3e67f0740
child 4889 8c9cbb623d55
permissions -rwxr-xr-x
Fixed buffer overflows in resamplers.

I'm not confident this is a complete fix, but I'm not confident the current
resamplers are really worth keeping at all, either.
icculus@1982
     1
#!/usr/bin/perl -w
icculus@1982
     2
icculus@1982
     3
use warnings;
icculus@1982
     4
use strict;
icculus@1982
     5
icculus@1982
     6
my @audiotypes = qw(
icculus@1982
     7
    U8
icculus@1982
     8
    S8
icculus@1982
     9
    U16LSB
icculus@1982
    10
    S16LSB
icculus@1982
    11
    U16MSB
icculus@1982
    12
    S16MSB
icculus@1982
    13
    S32LSB
icculus@1982
    14
    S32MSB
icculus@1982
    15
    F32LSB
icculus@1982
    16
    F32MSB
icculus@1982
    17
);
icculus@1982
    18
icculus@3008
    19
my @channels = ( 1, 2, 4, 6, 8 );
icculus@1982
    20
my %funcs;
icculus@3008
    21
my $custom_converters = 0;
icculus@3008
    22
icculus@1982
    23
icculus@3008
    24
sub getTypeConvertHashId {
icculus@3008
    25
    my ($from, $to) = @_;
icculus@3008
    26
    return "TYPECONVERTER $from/$to";
icculus@3008
    27
}
icculus@3008
    28
icculus@3008
    29
icculus@3008
    30
sub getResamplerHashId {
icculus@3008
    31
    my ($from, $channels, $upsample, $multiple) = @_;
icculus@3008
    32
    return "RESAMPLER $from/$channels/$upsample/$multiple";
icculus@3008
    33
}
icculus@1982
    34
icculus@1982
    35
icculus@1982
    36
sub outputHeader {
icculus@1982
    37
    print <<EOF;
slouken@1985
    38
/* DO NOT EDIT!  This file is generated by sdlgenaudiocvt.pl */
icculus@1982
    39
/*
icculus@1982
    40
    SDL - Simple DirectMedia Layer
slouken@2859
    41
    Copyright (C) 1997-2009 Sam Lantinga
icculus@1982
    42
icculus@1982
    43
    This library is free software; you can redistribute it and/or
icculus@1982
    44
    modify it under the terms of the GNU Lesser General Public
icculus@1982
    45
    License as published by the Free Software Foundation; either
icculus@1982
    46
    version 2.1 of the License, or (at your option) any later version.
icculus@1982
    47
icculus@1982
    48
    This library is distributed in the hope that it will be useful,
icculus@1982
    49
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1982
    50
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1982
    51
    Lesser General Public License for more details.
icculus@1982
    52
icculus@1982
    53
    You should have received a copy of the GNU Lesser General Public
icculus@1982
    54
    License along with this library; if not, write to the Free Software
icculus@1982
    55
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1982
    56
icculus@1982
    57
    Sam Lantinga
icculus@1982
    58
    slouken\@libsdl.org
icculus@1982
    59
*/
icculus@1982
    60
icculus@1982
    61
#include "SDL_config.h"
icculus@1982
    62
#include "SDL_audio.h"
icculus@1982
    63
#include "SDL_audio_c.h"
icculus@1982
    64
icculus@3019
    65
#ifndef DEBUG_CONVERT
icculus@3019
    66
#define DEBUG_CONVERT 0
icculus@3019
    67
#endif
icculus@3019
    68
icculus@3019
    69
icculus@3019
    70
/* If you can guarantee your data and need space, you can eliminate code... */
icculus@3019
    71
icculus@3019
    72
/* Just build the arbitrary resamplers if you're saving code space. */
icculus@3019
    73
#ifndef LESS_RESAMPLERS
icculus@3019
    74
#define LESS_RESAMPLERS 0
icculus@3019
    75
#endif
icculus@3019
    76
icculus@3019
    77
/* Don't build any resamplers if you're REALLY saving code space. */
icculus@3019
    78
#ifndef NO_RESAMPLERS
icculus@3019
    79
#define NO_RESAMPLERS 0
icculus@3019
    80
#endif
icculus@3019
    81
icculus@3019
    82
/* Don't build any type converters if you're saving code space. */
icculus@3019
    83
#ifndef NO_CONVERTERS
icculus@3019
    84
#define NO_CONVERTERS 0
icculus@3019
    85
#endif
icculus@3019
    86
icculus@3019
    87
slouken@1985
    88
/* *INDENT-OFF* */
icculus@1982
    89
icculus@1982
    90
EOF
icculus@1982
    91
icculus@2011
    92
    my @vals = ( 127, 32767, 2147483647 );
icculus@1982
    93
    foreach (@vals) {
icculus@1982
    94
        my $val = $_;
icculus@1982
    95
        my $fval = 1.0 / $val;
icculus@1982
    96
        print("#define DIVBY${val} ${fval}f\n");
icculus@1982
    97
    }
icculus@1982
    98
icculus@1982
    99
    print("\n");
icculus@1982
   100
}
icculus@1982
   101
slouken@1985
   102
sub outputFooter {
slouken@1985
   103
    print <<EOF;
icculus@3008
   104
/* $custom_converters converters generated. */
icculus@3008
   105
slouken@1985
   106
/* *INDENT-ON* */
slouken@1985
   107
slouken@1985
   108
/* vi: set ts=4 sw=4 expandtab: */
slouken@1985
   109
EOF
slouken@1985
   110
}
icculus@1982
   111
icculus@1982
   112
sub splittype {
icculus@1982
   113
    my $t = shift;
icculus@1982
   114
    my ($signed, $size, $endian) = $t =~ /([USF])(\d+)([LM]SB|)/;
icculus@1982
   115
    my $float = ($signed eq 'F') ? 1 : 0;
icculus@1982
   116
    $signed = (($float) or ($signed eq 'S')) ? 1 : 0;
icculus@1982
   117
    $endian = 'NONE' if ($endian eq '');
icculus@1982
   118
icculus@1982
   119
    my $ctype = '';
icculus@1982
   120
    if ($float) {
icculus@1982
   121
        $ctype = (($size == 32) ? 'float' : 'double');
icculus@1982
   122
    } else {
icculus@1982
   123
        $ctype = (($signed) ? 'S' : 'U') . "int${size}";
icculus@1982
   124
    }
icculus@1982
   125
icculus@1982
   126
    return ($signed, $float, $size, $endian, $ctype);
icculus@1982
   127
}
icculus@1982
   128
icculus@1982
   129
sub getSwapFunc {
icculus@1982
   130
    my ($size, $signed, $float, $endian, $val) = @_;
icculus@1982
   131
    my $BEorLE = (($endian eq 'MSB') ? 'BE' : 'LE');
icculus@1982
   132
    my $code = '';
icculus@1982
   133
icculus@1982
   134
    if ($float) {
icculus@1982
   135
        $code = "SDL_SwapFloat${BEorLE}($val)";
icculus@1982
   136
    } else {
icculus@1982
   137
        if ($size > 8) {
icculus@1982
   138
            $code = "SDL_Swap${BEorLE}${size}($val)";
icculus@1982
   139
        } else {
icculus@1982
   140
            $code = $val;
icculus@1982
   141
        }
icculus@1982
   142
icculus@1982
   143
        if (($signed) and (!$float)) {
icculus@1982
   144
            $code = "((Sint${size}) $code)";
icculus@1982
   145
        }
icculus@1982
   146
    }
icculus@1982
   147
icculus@1982
   148
    return "${code}";
icculus@1982
   149
}
icculus@1982
   150
icculus@1982
   151
icculus@1982
   152
sub maxIntVal {
icculus@2011
   153
    my $size = shift;
icculus@2011
   154
    if ($size == 8) {
icculus@2011
   155
        return 0x7F;
icculus@2011
   156
    } elsif ($size == 16) {
icculus@2011
   157
        return 0x7FFF;
icculus@2011
   158
    } elsif ($size == 32) {
icculus@2011
   159
        return 0x7FFFFFFF;
icculus@1982
   160
    }
icculus@1982
   161
icculus@1982
   162
    die("bug in script.\n");
icculus@1982
   163
}
icculus@1982
   164
icculus@1982
   165
sub getFloatToIntMult {
icculus@2011
   166
    my $size = shift;
icculus@2011
   167
    my $val = maxIntVal($size) . '.0';
icculus@1982
   168
    $val .= 'f' if ($size < 32);
icculus@1982
   169
    return $val;
icculus@1982
   170
}
icculus@1982
   171
icculus@1982
   172
sub getIntToFloatDivBy {
icculus@2011
   173
    my $size = shift;
icculus@2011
   174
    return 'DIVBY' . maxIntVal($size);
icculus@1982
   175
}
icculus@1982
   176
icculus@1982
   177
sub getSignFlipVal {
icculus@1982
   178
    my $size = shift;
icculus@1982
   179
    if ($size == 8) {
icculus@1982
   180
        return '0x80';
icculus@1982
   181
    } elsif ($size == 16) {
icculus@1982
   182
        return '0x8000';
icculus@1982
   183
    } elsif ($size == 32) {
icculus@1982
   184
        return '0x80000000';
icculus@1982
   185
    }
icculus@1982
   186
icculus@1982
   187
    die("bug in script.\n");
icculus@1982
   188
}
icculus@1982
   189
icculus@1982
   190
sub buildCvtFunc {
icculus@1982
   191
    my ($from, $to) = @_;
icculus@1982
   192
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@1982
   193
    my ($tsigned, $tfloat, $tsize, $tendian, $tctype) = splittype($to);
icculus@1982
   194
    my $diffs = 0;
icculus@1982
   195
    $diffs++ if ($fsize != $tsize);
icculus@1982
   196
    $diffs++ if ($fsigned != $tsigned);
icculus@1982
   197
    $diffs++ if ($ffloat != $tfloat);
icculus@1982
   198
    $diffs++ if ($fendian ne $tendian);
icculus@1982
   199
icculus@1982
   200
    return if ($diffs == 0);
icculus@1982
   201
icculus@3008
   202
    my $hashid = getTypeConvertHashId($from, $to);
icculus@1982
   203
    if (1) { # !!! FIXME: if ($diffs > 1) {
icculus@1982
   204
        my $sym = "SDL_Convert_${from}_to_${to}";
icculus@1982
   205
        $funcs{$hashid} = $sym;
icculus@1982
   206
        $custom_converters++;
icculus@1982
   207
icculus@1982
   208
        # Always unsigned for ints, for possible byteswaps.
icculus@1982
   209
        my $srctype = (($ffloat) ? 'float' : "Uint${fsize}");
icculus@1982
   210
icculus@1982
   211
        print <<EOF;
icculus@1982
   212
static void SDLCALL
icculus@1982
   213
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   214
{
icculus@1982
   215
    int i;
icculus@1982
   216
    const $srctype *src;
icculus@1982
   217
    $tctype *dst;
icculus@1982
   218
slouken@3032
   219
#if DEBUG_CONVERT
icculus@1982
   220
    fprintf(stderr, "Converting AUDIO_${from} to AUDIO_${to}.\\n");
icculus@1982
   221
#endif
icculus@1982
   222
icculus@1982
   223
EOF
icculus@1982
   224
icculus@1982
   225
        if ($fsize < $tsize) {
icculus@1982
   226
            my $mult = $tsize / $fsize;
icculus@1982
   227
            print <<EOF;
icculus@2956
   228
    src = ((const $srctype *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@2956
   229
    dst = (($tctype *) (cvt->buf + cvt->len_cvt * $mult)) - 1;
icculus@1982
   230
    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, --src, --dst) {
icculus@1982
   231
EOF
icculus@1982
   232
        } else {
icculus@1982
   233
            print <<EOF;
icculus@1982
   234
    src = (const $srctype *) cvt->buf;
icculus@1982
   235
    dst = ($tctype *) cvt->buf;
icculus@1982
   236
    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, ++src, ++dst) {
icculus@1982
   237
EOF
icculus@1982
   238
        }
icculus@1982
   239
icculus@1982
   240
        # Have to convert to/from float/int.
icculus@1982
   241
        # !!! FIXME: cast through double for int32<->float?
icculus@1982
   242
        my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
icculus@1982
   243
        if ($ffloat != $tfloat) {
icculus@1982
   244
            if ($ffloat) {
icculus@2011
   245
                my $mult = getFloatToIntMult($tsize);
icculus@2011
   246
                if (!$tsigned) {   # bump from -1.0f/1.0f to 0.0f/2.0f
icculus@2011
   247
                    $code = "($code + 1.0f)";
icculus@2011
   248
                }
icculus@1982
   249
                $code = "(($tctype) ($code * $mult))";
icculus@1982
   250
            } else {
icculus@1982
   251
                # $divby will be the reciprocal, to avoid pipeline stalls
icculus@1982
   252
                #  from floating point division...so multiply it.
icculus@2011
   253
                my $divby = getIntToFloatDivBy($fsize);
icculus@1982
   254
                $code = "(((float) $code) * $divby)";
icculus@2011
   255
                if (!$fsigned) {   # bump from 0.0f/2.0f to -1.0f/1.0f.
icculus@2011
   256
                    $code = "($code - 1.0f)";
icculus@2011
   257
                }
icculus@1982
   258
            }
icculus@1982
   259
        } else {
icculus@1982
   260
            # All integer conversions here.
icculus@1982
   261
            if ($fsigned != $tsigned) {
icculus@1982
   262
                my $signflipval = getSignFlipVal($fsize);
icculus@1982
   263
                $code = "(($code) ^ $signflipval)";
icculus@1982
   264
            }
icculus@1982
   265
icculus@1982
   266
            my $shiftval = abs($fsize - $tsize);
icculus@1982
   267
            if ($fsize < $tsize) {
icculus@1982
   268
                $code = "((($tctype) $code) << $shiftval)";
icculus@1982
   269
            } elsif ($fsize > $tsize) {
icculus@1982
   270
                $code = "(($tctype) ($code >> $shiftval))";
icculus@1982
   271
            }
icculus@1982
   272
        }
icculus@1982
   273
icculus@1982
   274
        my $swap = getSwapFunc($tsize, $tsigned, $tfloat, $tendian, 'val');
icculus@1982
   275
icculus@1982
   276
        print <<EOF;
icculus@1982
   277
        const $tctype val = $code;
icculus@1982
   278
        *dst = ${swap};
icculus@1982
   279
    }
icculus@1982
   280
icculus@1982
   281
EOF
icculus@1982
   282
icculus@1982
   283
        if ($fsize > $tsize) {
icculus@1982
   284
            my $divby = $fsize / $tsize;
icculus@1982
   285
            print("    cvt->len_cvt /= $divby;\n");
icculus@1982
   286
        } elsif ($fsize < $tsize) {
icculus@1982
   287
            my $mult = $tsize / $fsize;
icculus@1982
   288
            print("    cvt->len_cvt *= $mult;\n");
icculus@1982
   289
        }
icculus@1982
   290
icculus@1982
   291
        print <<EOF;
icculus@1982
   292
    if (cvt->filters[++cvt->filter_index]) {
icculus@2955
   293
        cvt->filters[cvt->filter_index] (cvt, AUDIO_$to);
icculus@1982
   294
    }
icculus@1982
   295
}
icculus@1982
   296
icculus@1982
   297
EOF
icculus@1982
   298
icculus@1982
   299
    } else {
icculus@1982
   300
        if ($fsigned != $tsigned) {
icculus@1982
   301
            $funcs{$hashid} = 'SDL_ConvertSigned';
icculus@1982
   302
        } elsif ($ffloat != $tfloat) {
icculus@1982
   303
            $funcs{$hashid} = 'SDL_ConvertFloat';
icculus@1982
   304
        } elsif ($fsize != $tsize) {
icculus@1982
   305
            $funcs{$hashid} = 'SDL_ConvertSize';
icculus@1982
   306
        } elsif ($fendian ne $tendian) {
icculus@1982
   307
            $funcs{$hashid} = 'SDL_ConvertEndian';
icculus@1982
   308
        } else {
icculus@1982
   309
            die("error in script.\n");
icculus@1982
   310
        }
icculus@1982
   311
    }
icculus@1982
   312
}
icculus@1982
   313
icculus@3008
   314
icculus@3008
   315
sub buildTypeConverters {
icculus@3019
   316
    print "#if !NO_CONVERTERS\n\n";
icculus@3008
   317
    foreach (@audiotypes) {
icculus@3008
   318
        my $from = $_;
icculus@3008
   319
        foreach (@audiotypes) {
icculus@3008
   320
            my $to = $_;
icculus@3008
   321
            buildCvtFunc($from, $to);
icculus@3008
   322
        }
icculus@3008
   323
    }
icculus@3019
   324
    print "#endif  /* !NO_CONVERTERS */\n\n\n";
icculus@3008
   325
icculus@3008
   326
    print "const SDL_AudioTypeFilters sdl_audio_type_filters[] =\n{\n";
icculus@3019
   327
    print "#if !NO_CONVERTERS\n";
icculus@3008
   328
    foreach (@audiotypes) {
icculus@3008
   329
        my $from = $_;
icculus@3008
   330
        foreach (@audiotypes) {
icculus@3008
   331
            my $to = $_;
icculus@3008
   332
            if ($from ne $to) {
icculus@3008
   333
                my $hashid = getTypeConvertHashId($from, $to);
icculus@3008
   334
                my $sym = $funcs{$hashid};
icculus@3008
   335
                print("    { AUDIO_$from, AUDIO_$to, $sym },\n");
icculus@3008
   336
            }
icculus@3008
   337
        }
icculus@3008
   338
    }
icculus@3019
   339
    print "#endif  /* !NO_CONVERTERS */\n";
icculus@3008
   340
icculus@3020
   341
    print("    { 0, 0, NULL }\n");
icculus@3008
   342
    print "};\n\n\n";
icculus@3008
   343
}
icculus@3008
   344
icculus@3008
   345
sub getBiggerCtype {
icculus@3008
   346
    my ($isfloat, $size) = @_;
icculus@3008
   347
icculus@3008
   348
    if ($isfloat) {
icculus@3008
   349
        if ($size == 32) {
icculus@3008
   350
            return 'double';
icculus@3008
   351
        }
icculus@3008
   352
        die("bug in script.\n");
icculus@3008
   353
    }
icculus@3008
   354
icculus@3008
   355
    if ($size == 8) {
icculus@3008
   356
        return 'Sint16';
icculus@3008
   357
    } elsif ($size == 16) {
icculus@3008
   358
        return 'Sint32'
icculus@3008
   359
    } elsif ($size == 32) {
icculus@3008
   360
        return 'Sint64'
icculus@3008
   361
    }
icculus@3008
   362
icculus@3008
   363
    die("bug in script.\n");
icculus@3008
   364
}
icculus@3008
   365
icculus@3008
   366
icculus@3008
   367
# These handle arbitrary resamples...44100Hz to 48000Hz, for example.
icculus@3008
   368
# Man, this code is skanky.
icculus@3008
   369
sub buildArbitraryResampleFunc {
icculus@3008
   370
    # !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
icculus@3008
   371
    my ($from, $channels, $upsample) = @_;
icculus@3008
   372
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@3008
   373
icculus@3008
   374
    my $bigger = getBiggerCtype($ffloat, $fsize);
icculus@3008
   375
    my $interp = ($ffloat) ? '* 0.5' : '>> 1';
icculus@3008
   376
icculus@3008
   377
    my $resample = ($upsample) ? 'Upsample' : 'Downsample';
icculus@3008
   378
    my $hashid = getResamplerHashId($from, $channels, $upsample, 0);
icculus@3008
   379
    my $sym = "SDL_${resample}_${from}_${channels}c";
icculus@3008
   380
    $funcs{$hashid} = $sym;
icculus@3008
   381
    $custom_converters++;
icculus@3008
   382
icculus@3008
   383
    my $fudge = $fsize * $channels * 2;  # !!! FIXME
icculus@3008
   384
    my $eps_adjust = ($upsample) ? 'dstsize' : 'srcsize';
icculus@3008
   385
    my $incr = '';
icculus@3008
   386
    my $incr2 = '';
icculus@3008
   387
icculus@3008
   388
icculus@3008
   389
    # !!! FIXME: DEBUG_CONVERT should report frequencies.
icculus@3008
   390
    print <<EOF;
icculus@3008
   391
static void SDLCALL
icculus@3008
   392
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@3008
   393
{
slouken@3032
   394
#if DEBUG_CONVERT
icculus@3008
   395
    fprintf(stderr, "$resample arbitrary (x%f) AUDIO_${from}, ${channels} channels.\\n", cvt->rate_incr);
icculus@3008
   396
#endif
icculus@3008
   397
icculus@3008
   398
    const int srcsize = cvt->len_cvt - $fudge;
icculus@3008
   399
    const int dstsize = (int) (((double)cvt->len_cvt) * cvt->rate_incr);
icculus@3008
   400
    register int eps = 0;
icculus@3008
   401
EOF
icculus@1982
   402
icculus@3008
   403
    # Upsampling (growing the buffer) needs to work backwards, since we
icculus@3008
   404
    #  overwrite the buffer as we go.
icculus@3008
   405
    if ($upsample) {
icculus@3008
   406
        print <<EOF;
icculus@3008
   407
    $fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
icculus@3008
   408
    const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
icculus@3008
   409
    const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
icculus@3008
   410
EOF
icculus@3008
   411
    } else {
icculus@3008
   412
        print <<EOF;
icculus@3008
   413
    $fctype *dst = ($fctype *) cvt->buf;
icculus@3008
   414
    const $fctype *src = ($fctype *) cvt->buf;
icculus@3008
   415
    const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
icculus@3008
   416
EOF
icculus@3008
   417
    }
icculus@3008
   418
icculus@3008
   419
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   420
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   421
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   422
        print <<EOF;
icculus@3008
   423
    $fctype sample${idx} = $val;
icculus@3008
   424
EOF
icculus@3008
   425
    }
icculus@3008
   426
icculus@3008
   427
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   428
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   429
        print <<EOF;
icculus@3008
   430
    $fctype last_sample${idx} = sample${idx};
icculus@3008
   431
EOF
icculus@3008
   432
    }
icculus@3008
   433
icculus@3008
   434
    print <<EOF;
icculus@3008
   435
    while (dst != target) {
icculus@3008
   436
EOF
icculus@3008
   437
icculus@3008
   438
    if ($upsample) {
icculus@3008
   439
        for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   440
            # !!! FIXME: don't do this swap every write, just when the samples change.
icculus@3008
   441
            my $idx = (($channels - $i) - 1);
icculus@3008
   442
            my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${idx}");
icculus@3008
   443
            print <<EOF;
icculus@3008
   444
        dst[$idx] = $val;
icculus@3008
   445
EOF
icculus@3008
   446
        }
icculus@3008
   447
icculus@3008
   448
        $incr = ($channels == 1) ? 'dst--' : "dst -= $channels";
icculus@3008
   449
        $incr2 = ($channels == 1) ? 'src--' : "src -= $channels";
icculus@3008
   450
icculus@3008
   451
        print <<EOF;
icculus@3008
   452
        $incr;
icculus@3008
   453
        eps += srcsize;
icculus@3008
   454
        if ((eps << 1) >= dstsize) {
icculus@3008
   455
            $incr2;
icculus@3008
   456
EOF
icculus@3008
   457
    } else {  # downsample.
icculus@3008
   458
        $incr = ($channels == 1) ? 'src++' : "src += $channels";
icculus@3008
   459
        print <<EOF;
icculus@3008
   460
        $incr;
icculus@3008
   461
        eps += dstsize;
icculus@3008
   462
        if ((eps << 1) >= srcsize) {
icculus@3008
   463
EOF
icculus@3008
   464
        for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   465
            my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${i}");
icculus@3008
   466
            print <<EOF;
icculus@3008
   467
            dst[$i] = $val;
icculus@3008
   468
EOF
icculus@3008
   469
        }
icculus@3008
   470
icculus@3008
   471
        $incr = ($channels == 1) ? 'dst++' : "dst += $channels";
icculus@3008
   472
        print <<EOF;
icculus@3008
   473
            $incr;
icculus@3008
   474
EOF
icculus@3008
   475
    }
icculus@3008
   476
icculus@3008
   477
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   478
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   479
        my $swapped = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   480
        print <<EOF;
icculus@3008
   481
            sample${idx} = ($fctype) (((($bigger) $swapped) + (($bigger) last_sample${idx})) $interp);
icculus@3008
   482
EOF
icculus@3008
   483
    }
icculus@3008
   484
icculus@3008
   485
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   486
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   487
        print <<EOF;
icculus@3008
   488
            last_sample${idx} = sample${idx};
icculus@3008
   489
EOF
icculus@3008
   490
    }
icculus@3008
   491
icculus@3008
   492
    print <<EOF;
icculus@3008
   493
            eps -= $eps_adjust;
icculus@3008
   494
        }
icculus@3008
   495
    }
icculus@3008
   496
EOF
icculus@3008
   497
icculus@3008
   498
        print <<EOF;
icculus@3008
   499
    cvt->len_cvt = dstsize;
icculus@3008
   500
    if (cvt->filters[++cvt->filter_index]) {
icculus@3008
   501
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   502
    }
icculus@1982
   503
}
icculus@1982
   504
icculus@3008
   505
EOF
icculus@3008
   506
icculus@3008
   507
}
icculus@3008
   508
icculus@3008
   509
# These handle clean resamples...doubling and quadrupling the sample rate, etc.
icculus@3008
   510
sub buildMultipleResampleFunc {
icculus@3008
   511
    # !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
icculus@3008
   512
    my ($from, $channels, $upsample, $multiple) = @_;
icculus@3008
   513
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@3008
   514
icculus@3008
   515
    my $bigger = getBiggerCtype($ffloat, $fsize);
icculus@3008
   516
    my $interp = ($ffloat) ? '* 0.5' : '>> 1';
icculus@3008
   517
    my $interp2 = ($ffloat) ? '* 0.25' : '>> 2';
icculus@3008
   518
    my $mult3 = ($ffloat) ? '3.0' : '3';
icculus@3008
   519
    my $lencvtop = ($upsample) ? '*' : '/';
icculus@3008
   520
icculus@3008
   521
    my $resample = ($upsample) ? 'Upsample' : 'Downsample';
icculus@3008
   522
    my $hashid = getResamplerHashId($from, $channels, $upsample, $multiple);
icculus@3008
   523
    my $sym = "SDL_${resample}_${from}_${channels}c_x${multiple}";
icculus@3008
   524
    $funcs{$hashid} = $sym;
icculus@3008
   525
    $custom_converters++;
icculus@3008
   526
icculus@3008
   527
    # !!! FIXME: DEBUG_CONVERT should report frequencies.
icculus@3008
   528
    print <<EOF;
icculus@3008
   529
static void SDLCALL
icculus@3008
   530
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   531
{
slouken@3032
   532
#if DEBUG_CONVERT
icculus@3008
   533
    fprintf(stderr, "$resample (x${multiple}) AUDIO_${from}, ${channels} channels.\\n");
icculus@3008
   534
#endif
icculus@3008
   535
icculus@3008
   536
    const int srcsize = cvt->len_cvt;
icculus@3008
   537
    const int dstsize = cvt->len_cvt $lencvtop $multiple;
icculus@3008
   538
EOF
icculus@3008
   539
icculus@3602
   540
    my $endcomparison = '!=';
icculus@3602
   541
icculus@3008
   542
    # Upsampling (growing the buffer) needs to work backwards, since we
icculus@3008
   543
    #  overwrite the buffer as we go.
icculus@3008
   544
    if ($upsample) {
icculus@3602
   545
        $endcomparison = '>';  # dst > target
icculus@3008
   546
        print <<EOF;
icculus@3008
   547
    $fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
icculus@3008
   548
    const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
icculus@3008
   549
    const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
icculus@3008
   550
EOF
icculus@3008
   551
    } else {
icculus@3602
   552
        $endcomparison = '<';  # dst < target
icculus@3008
   553
        print <<EOF;
icculus@3008
   554
    $fctype *dst = ($fctype *) cvt->buf;
icculus@3008
   555
    const $fctype *src = ($fctype *) cvt->buf;
icculus@3008
   556
    const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
icculus@3008
   557
EOF
icculus@3008
   558
    }
icculus@3008
   559
icculus@3008
   560
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   561
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   562
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   563
        print <<EOF;
icculus@3008
   564
    $bigger last_sample${idx} = ($bigger) $val;
icculus@3008
   565
EOF
icculus@3008
   566
    }
icculus@3008
   567
icculus@3008
   568
    print <<EOF;
icculus@3602
   569
    while (dst $endcomparison target) {
icculus@3008
   570
EOF
icculus@3008
   571
icculus@3008
   572
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   573
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   574
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   575
        print <<EOF;
icculus@3008
   576
        const $bigger sample${idx} = ($bigger) $val;
icculus@3008
   577
EOF
icculus@3008
   578
    }
icculus@3008
   579
icculus@3008
   580
    my $incr = '';
icculus@3008
   581
    if ($upsample) {
icculus@3008
   582
        $incr = ($channels == 1) ? 'src--' : "src -= $channels";
icculus@3008
   583
    } else {
icculus@3008
   584
        my $amount = $channels * $multiple;
icculus@3008
   585
        $incr = "src += $amount";  # can't ever be 1, so no "++" version.
icculus@3008
   586
    }
icculus@3008
   587
icculus@3008
   588
icculus@3008
   589
    print <<EOF;
icculus@3008
   590
        $incr;
icculus@1982
   591
EOF
icculus@1982
   592
icculus@3008
   593
    # !!! FIXME: This really begs for some Altivec or SSE, etc.
icculus@3008
   594
    if ($upsample) {
icculus@3008
   595
        if ($multiple == 2) {
icculus@3008
   596
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   597
                my $dsti = $i + $channels;
icculus@3008
   598
                print <<EOF;
icculus@3008
   599
        dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   600
EOF
icculus@3008
   601
            }
icculus@3008
   602
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   603
                my $dsti = $i;
icculus@3008
   604
                print <<EOF;
icculus@3008
   605
        dst[$dsti] = ($fctype) sample${i};
icculus@3008
   606
EOF
icculus@3008
   607
            }
icculus@3008
   608
        } elsif ($multiple == 4) {
icculus@3008
   609
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   610
                my $dsti = $i + ($channels * 3);
icculus@3008
   611
                print <<EOF;
icculus@3008
   612
        dst[$dsti] = ($fctype) sample${i};
icculus@3008
   613
EOF
icculus@3008
   614
            }
icculus@3008
   615
icculus@3008
   616
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   617
                my $dsti = $i + ($channels * 2);
icculus@3008
   618
                print <<EOF;
icculus@3008
   619
        dst[$dsti] = ($fctype) ((($mult3 * sample${i}) + last_sample${i}) $interp2);
icculus@3008
   620
EOF
icculus@3008
   621
            }
icculus@3008
   622
icculus@3008
   623
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   624
                my $dsti = $i + ($channels * 1);
icculus@3008
   625
                print <<EOF;
icculus@3008
   626
        dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   627
EOF
icculus@3008
   628
            }
icculus@3008
   629
icculus@3008
   630
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   631
                my $dsti = $i + ($channels * 0);
icculus@3008
   632
                print <<EOF;
icculus@3008
   633
        dst[$dsti] = ($fctype) ((sample${i} + ($mult3 * last_sample${i})) $interp2);
icculus@3008
   634
EOF
icculus@3008
   635
            }
icculus@3008
   636
        } else {
icculus@3008
   637
            die('bug in program.');  # we only handle x2 and x4.
icculus@1982
   638
        }
icculus@3008
   639
    } else {  # downsample.
icculus@3008
   640
        if ($multiple == 2) {
icculus@3008
   641
            for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   642
                print <<EOF;
icculus@3008
   643
        dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   644
EOF
icculus@3008
   645
            }
icculus@3008
   646
        } elsif ($multiple == 4) {
icculus@3008
   647
            # !!! FIXME: interpolate all 4 samples?
icculus@3008
   648
            for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   649
                print <<EOF;
icculus@3008
   650
        dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   651
EOF
icculus@3008
   652
            }
icculus@3008
   653
        } else {
icculus@3008
   654
            die('bug in program.');  # we only handle x2 and x4.
icculus@3008
   655
        }
icculus@3008
   656
    }
icculus@3008
   657
icculus@3008
   658
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   659
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   660
        print <<EOF;
icculus@3008
   661
        last_sample${idx} = sample${idx};
icculus@3008
   662
EOF
icculus@3008
   663
    }
icculus@3008
   664
icculus@3008
   665
    if ($upsample) {
icculus@3008
   666
        my $amount = $channels * $multiple;
icculus@3008
   667
        $incr = "dst -= $amount";  # can't ever be 1, so no "--" version.
icculus@3008
   668
    } else {
icculus@3008
   669
        $incr = ($channels == 1) ? 'dst++' : "dst += $channels";
icculus@3008
   670
    }
icculus@3008
   671
icculus@3008
   672
    print <<EOF;
icculus@3008
   673
        $incr;
icculus@3008
   674
    }
icculus@3008
   675
icculus@3008
   676
    cvt->len_cvt = dstsize;
icculus@3008
   677
    if (cvt->filters[++cvt->filter_index]) {
icculus@3008
   678
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   679
    }
icculus@1982
   680
}
icculus@1982
   681
icculus@3008
   682
EOF
icculus@3008
   683
icculus@3008
   684
}
icculus@3008
   685
icculus@3008
   686
sub buildResamplers {
icculus@3019
   687
    print "#if !NO_RESAMPLERS\n\n";
icculus@3019
   688
    foreach (@audiotypes) {
icculus@3019
   689
        my $from = $_;
icculus@3019
   690
        foreach (@channels) {
icculus@3019
   691
            my $channel = $_;
icculus@3019
   692
            buildArbitraryResampleFunc($from, $channel, 1);
icculus@3019
   693
            buildArbitraryResampleFunc($from, $channel, 0);
icculus@3019
   694
        }
icculus@3019
   695
    }
icculus@3019
   696
icculus@3019
   697
    print "\n#if !LESS_RESAMPLERS\n\n";
icculus@3008
   698
    foreach (@audiotypes) {
icculus@3008
   699
        my $from = $_;
icculus@3008
   700
        foreach (@channels) {
icculus@3008
   701
            my $channel = $_;
icculus@3008
   702
            for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
icculus@3008
   703
                buildMultipleResampleFunc($from, $channel, 1, $multiple);
icculus@3008
   704
                buildMultipleResampleFunc($from, $channel, 0, $multiple);
icculus@3008
   705
            }
icculus@3008
   706
        }
icculus@3008
   707
    }
icculus@3008
   708
icculus@3019
   709
    print "#endif  /* !LESS_RESAMPLERS */\n";
icculus@3019
   710
    print "#endif  /* !NO_RESAMPLERS */\n\n\n";
icculus@3019
   711
icculus@3008
   712
    print "const SDL_AudioRateFilters sdl_audio_rate_filters[] =\n{\n";
icculus@3019
   713
    print "#if !NO_RESAMPLERS\n";
icculus@3008
   714
    foreach (@audiotypes) {
icculus@3008
   715
        my $from = $_;
icculus@3008
   716
        foreach (@channels) {
icculus@3008
   717
            my $channel = $_;
icculus@3019
   718
            for (my $upsample = 0; $upsample <= 1; $upsample++) {
icculus@3019
   719
                my $hashid = getResamplerHashId($from, $channel, $upsample, 0);
icculus@3019
   720
                my $sym = $funcs{$hashid};
icculus@3019
   721
                print("    { AUDIO_$from, $channel, $upsample, 0, $sym },\n");
icculus@3019
   722
            }
icculus@3019
   723
        }
icculus@3019
   724
    }
icculus@3019
   725
icculus@3019
   726
    print "#if !LESS_RESAMPLERS\n";
icculus@3019
   727
    foreach (@audiotypes) {
icculus@3019
   728
        my $from = $_;
icculus@3019
   729
        foreach (@channels) {
icculus@3019
   730
            my $channel = $_;
icculus@3019
   731
            for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
icculus@3008
   732
                for (my $upsample = 0; $upsample <= 1; $upsample++) {
icculus@3008
   733
                    my $hashid = getResamplerHashId($from, $channel, $upsample, $multiple);
icculus@3008
   734
                    my $sym = $funcs{$hashid};
icculus@3008
   735
                    print("    { AUDIO_$from, $channel, $upsample, $multiple, $sym },\n");
icculus@3008
   736
                }
icculus@3008
   737
            }
icculus@3008
   738
        }
icculus@3008
   739
    }
icculus@3008
   740
icculus@3019
   741
    print "#endif  /* !LESS_RESAMPLERS */\n";
icculus@3019
   742
    print "#endif  /* !NO_RESAMPLERS */\n";
icculus@3020
   743
    print("    { 0, 0, 0, 0, NULL }\n");
icculus@3008
   744
    print "};\n\n";
icculus@3008
   745
}
icculus@1982
   746
icculus@1982
   747
icculus@3008
   748
# mainline ...
icculus@1982
   749
icculus@3008
   750
outputHeader();
icculus@3008
   751
buildTypeConverters();
icculus@3008
   752
buildResamplers();
slouken@1985
   753
outputFooter();
slouken@1985
   754
icculus@1982
   755
exit 0;
icculus@1982
   756
slouken@1985
   757
# end of sdlgenaudiocvt.pl ...
icculus@1982
   758