Safe version.

This commit is contained in:
J.D. Purcell 2015-09-02 14:20:41 -04:00
parent eb6bdee01b
commit 384973c80f

View File

@ -30,37 +30,27 @@ namespace Nikse.SubtitleEdit.Core
ReverseScaleFactor = 0.5;
}
public unsafe void ComputeForward(double[] buff)
public void ComputeForward(double[] buff)
{
Compute(buff, false);
}
public void ComputeReverse(double[] buff)
{
Compute(buff, true);
}
private void Compute(double[] buff, bool reverse)
{
if (buff.Length < _length)
{
throw new ArgumentException("buff", "Buffer length must be greater than or equal to the FFT length.");
}
fixed (double* a = buff)
fixed (int* ip = _ip)
fixed (double* w = _w)
{
rdft(_length, false, a, ip, w);
}
rdft(_length, reverse, buff, _ip, _w);
}
public unsafe void ComputeReverse(double[] buff)
{
if (buff.Length < _length)
{
throw new ArgumentException("buff", "Buffer length must be greater than or equal to the FFT length.");
}
fixed (double* a = buff)
fixed (int* ip = _ip)
fixed (double* w = _w)
{
rdft(_length, true, a, ip, w);
}
}
private static unsafe void rdft(int n, bool rev, double* a, int* ip, double* w)
private static void rdft(int n, bool rev, double[] a, int[] ip, double[] w)
{
int nw, nc;
double xi;
@ -75,15 +65,15 @@ namespace Nikse.SubtitleEdit.Core
if (n > (nc << 2))
{
nc = n >> 2;
makect(nc, ip, w + nw);
makect(nc, ip, w, nw);
}
if (!rev)
{
if (n > 4)
{
bitrv2(n, ip + 2, a);
bitrv2(n, ip, a);
cftfsub(n, a, w);
rftfsub(n, a, nc, w + nw);
rftfsub(n, a, nc, w, nw);
}
else if (n == 4)
{
@ -99,8 +89,8 @@ namespace Nikse.SubtitleEdit.Core
a[0] -= a[1];
if (n > 4)
{
rftbsub(n, a, nc, w + nw);
bitrv2(n, ip + 2, a);
rftbsub(n, a, nc, w, nw);
bitrv2(n, ip, a);
cftbsub(n, a, w);
}
else if (n == 4)
@ -112,7 +102,7 @@ namespace Nikse.SubtitleEdit.Core
/* -------- initializing routines -------- */
private static unsafe void makewt(int nw, int* ip, double* w)
private static void makewt(int nw, int[] ip, double[] w)
{
int j, nwh;
double delta, x, y;
@ -138,12 +128,12 @@ namespace Nikse.SubtitleEdit.Core
w[nw - j] = y;
w[nw - j + 1] = x;
}
bitrv2(nw, ip + 2, w);
bitrv2(nw, ip, w);
}
}
}
private static unsafe void makect(int nc, int* ip, double* c)
private static void makect(int nc, int[] ip, double[] c, int nw)
{
int j, nch;
double delta;
@ -153,24 +143,24 @@ namespace Nikse.SubtitleEdit.Core
{
nch = nc >> 1;
delta = Math.Atan(1.0) / nch;
c[0] = Math.Cos(delta * nch);
c[nch] = 0.5 * c[0];
c[nw] = Math.Cos(delta * nch);
c[nw + nch] = 0.5 * c[nw];
for (j = 1; j < nch; j++)
{
c[j] = 0.5 * Math.Cos(delta * j);
c[nc - j] = 0.5 * Math.Sin(delta * j);
c[nw + j] = 0.5 * Math.Cos(delta * j);
c[nw + nc - j] = 0.5 * Math.Sin(delta * j);
}
}
}
/* -------- child routines -------- */
private static unsafe void bitrv2(int n, int* ip, double* a)
private static void bitrv2(int n, int[] ip, double[] a)
{
int j, j1, k, k1, l, m, m2;
double xr, xi, yr, yi;
ip[0] = 0;
ip[2] = 0;
l = n;
m = 1;
while ((m << 3) < l)
@ -178,7 +168,7 @@ namespace Nikse.SubtitleEdit.Core
l >>= 1;
for (j = 0; j < m; j++)
{
ip[m + j] = ip[j] + l;
ip[m + j + 2] = ip[j + 2] + l;
}
m <<= 1;
}
@ -189,8 +179,8 @@ namespace Nikse.SubtitleEdit.Core
{
for (j = 0; j < k; j++)
{
j1 = 2 * j + ip[k];
k1 = 2 * k + ip[j];
j1 = 2 * j + ip[k + 2];
k1 = 2 * k + ip[j + 2];
xr = a[j1];
xi = a[j1 + 1];
yr = a[k1];
@ -230,7 +220,7 @@ namespace Nikse.SubtitleEdit.Core
a[k1] = xr;
a[k1 + 1] = xi;
}
j1 = 2 * k + m2 + ip[k];
j1 = 2 * k + m2 + ip[k + 2];
k1 = j1 + m2;
xr = a[j1];
xi = a[j1 + 1];
@ -248,8 +238,8 @@ namespace Nikse.SubtitleEdit.Core
{
for (j = 0; j < k; j++)
{
j1 = 2 * j + ip[k];
k1 = 2 * k + ip[j];
j1 = 2 * j + ip[k + 2];
k1 = 2 * k + ip[j + 2];
xr = a[j1];
xi = a[j1 + 1];
yr = a[k1];
@ -273,7 +263,7 @@ namespace Nikse.SubtitleEdit.Core
}
}
private static unsafe void cftfsub(int n, double* a, double* w)
private static void cftfsub(int n, double[] a, double[] w)
{
int j, j1, j2, j3, l;
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
@ -329,7 +319,7 @@ namespace Nikse.SubtitleEdit.Core
}
}
private static unsafe void cftbsub(int n, double* a, double* w)
private static void cftbsub(int n, double[] a, double[] w)
{
int j, j1, j2, j3, l;
double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
@ -385,7 +375,7 @@ namespace Nikse.SubtitleEdit.Core
}
}
private static unsafe void cft1st(int n, double* a, double* w)
private static void cft1st(int n, double[] a, double[] w)
{
int j, k1, k2;
double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
@ -490,7 +480,7 @@ namespace Nikse.SubtitleEdit.Core
}
}
private static unsafe void cftmdl(int n, int l, double* a, double* w)
private static void cftmdl(int n, int l, double[] a, double[] w)
{
int j, j1, j2, j3, k, k1, k2, m, m2;
double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
@ -621,7 +611,7 @@ namespace Nikse.SubtitleEdit.Core
}
}
private static unsafe void rftfsub(int n, double* a, int nc, double* c)
private static void rftfsub(int n, double[] a, int nc, double[] c, int nw)
{
int j, k, kk, ks, m;
double wkr, wki, xr, xi, yr, yi;
@ -633,8 +623,8 @@ namespace Nikse.SubtitleEdit.Core
{
k = n - j;
kk += ks;
wkr = 0.5 - c[nc - kk];
wki = c[kk];
wkr = 0.5 - c[nw + nc - kk];
wki = c[nw + kk];
xr = a[j] - a[k];
xi = a[j + 1] + a[k + 1];
yr = wkr * xr - wki * xi;
@ -646,7 +636,7 @@ namespace Nikse.SubtitleEdit.Core
}
}
private static unsafe void rftbsub(int n, double* a, int nc, double* c)
private static void rftbsub(int n, double[] a, int nc, double[] c, int nw)
{
int j, k, kk, ks, m;
double wkr, wki, xr, xi, yr, yi;
@ -659,8 +649,8 @@ namespace Nikse.SubtitleEdit.Core
{
k = n - j;
kk += ks;
wkr = 0.5 - c[nc - kk];
wki = c[kk];
wkr = 0.5 - c[nw + nc - kk];
wki = c[nw + kk];
xr = a[j] - a[k];
xi = a[j + 1] + a[k + 1];
yr = wkr * xr + wki * xi;