diff options
42 files changed, 5658 insertions, 3 deletions
@@ -1,3 +1,84 @@ +2007-09-06 Sebastian Dröge <slomo@circular-chaos.org> + + Reviewed by: Stefan Kost <ensonic@users.sf.net> + + * configure.ac: + * gst-libs/gst/Makefile.am: + * gst-libs/gst/fft/Makefile.am: + * gst-libs/gst/fft/_kiss_fft_guts_f32.h: + * gst-libs/gst/fft/_kiss_fft_guts_f64.h: + * gst-libs/gst/fft/_kiss_fft_guts_s16.h: + * gst-libs/gst/fft/_kiss_fft_guts_s32.h: + * gst-libs/gst/fft/gstfft.c: (gst_fft_next_fast_length): + * gst-libs/gst/fft/gstfft.h: + * gst-libs/gst/fft/gstfftf32.c: (gst_fft_f32_new), + (gst_fft_f32_fft), (gst_fft_f32_inverse_fft), (gst_fft_f32_free), + (gst_fft_f32_window), (gst_fft_f32_magnitude), (gst_fft_f32_phase): + * gst-libs/gst/fft/gstfftf32.h: + * gst-libs/gst/fft/gstfftf64.c: (gst_fft_f64_new), + (gst_fft_f64_fft), (gst_fft_f64_inverse_fft), (gst_fft_f64_free), + (gst_fft_f64_window), (gst_fft_f64_magnitude), (gst_fft_f64_phase): + * gst-libs/gst/fft/gstfftf64.h: + * gst-libs/gst/fft/gstffts16.c: (gst_fft_s16_new), + (gst_fft_s16_fft), (gst_fft_s16_inverse_fft), (gst_fft_s16_free), + (gst_fft_s16_window), (gst_fft_s16_magnitude), (gst_fft_s16_phase): + * gst-libs/gst/fft/gstffts16.h: + * gst-libs/gst/fft/gstffts32.c: (gst_fft_s32_new), + (gst_fft_s32_fft), (gst_fft_s32_inverse_fft), (gst_fft_s32_free), + (gst_fft_s32_window), (gst_fft_s32_magnitude), (gst_fft_s32_phase): + * gst-libs/gst/fft/gstffts32.h: + * gst-libs/gst/fft/kiss_fft_f32.c: (kf_bfly2), (kf_bfly4), + (kf_bfly3), (kf_bfly5), (kf_bfly_generic), (kf_work), (kf_factor), + (kiss_fft_f32_alloc), (kiss_fft_f32_stride), (kiss_fft_f32), + (kiss_fft_f32_cleanup), (kiss_fft_f32_next_fast_size): + * gst-libs/gst/fft/kiss_fft_f32.h: + * gst-libs/gst/fft/kiss_fft_f64.c: (kf_bfly2), (kf_bfly4), + (kf_bfly3), (kf_bfly5), (kf_bfly_generic), (kf_work), (kf_factor), + (kiss_fft_f64_alloc), (kiss_fft_f64_stride), (kiss_fft_f64), + (kiss_fft_f64_cleanup), (kiss_fft_f64_next_fast_size): + * gst-libs/gst/fft/kiss_fft_f64.h: + * gst-libs/gst/fft/kiss_fft_s16.c: (kf_bfly2), (kf_bfly4), + (kf_bfly3), (kf_bfly5), (kf_bfly_generic), (kf_work), (kf_factor), + (kiss_fft_s16_alloc), (kiss_fft_s16_stride), (kiss_fft_s16), + (kiss_fft_s16_cleanup), (kiss_fft_s16_next_fast_size): + * gst-libs/gst/fft/kiss_fft_s16.h: + * gst-libs/gst/fft/kiss_fft_s32.c: (kf_bfly2), (kf_bfly4), + (kf_bfly3), (kf_bfly5), (kf_bfly_generic), (kf_work), (kf_factor), + (kiss_fft_s32_alloc), (kiss_fft_s32_stride), (kiss_fft_s32), + (kiss_fft_s32_cleanup), (kiss_fft_s32_next_fast_size): + * gst-libs/gst/fft/kiss_fft_s32.h: + * gst-libs/gst/fft/kiss_fftr_f32.c: (kiss_fftr_f32_alloc), + (kiss_fftr_f32), (kiss_fftri_f32): + * gst-libs/gst/fft/kiss_fftr_f32.h: + * gst-libs/gst/fft/kiss_fftr_f64.c: (kiss_fftr_f64_alloc), + (kiss_fftr_f64), (kiss_fftri_f64): + * gst-libs/gst/fft/kiss_fftr_f64.h: + * gst-libs/gst/fft/kiss_fftr_s16.c: (kiss_fftr_s16_alloc), + (kiss_fftr_s16), (kiss_fftri_s16): + * gst-libs/gst/fft/kiss_fftr_s16.h: + * gst-libs/gst/fft/kiss_fftr_s32.c: (kiss_fftr_s32_alloc), + (kiss_fftr_s32), (kiss_fftri_s32): + * gst-libs/gst/fft/kiss_fftr_s32.h: + * gst-libs/gst/fft/kiss_version: + * pkgconfig/gstreamer-plugins-base-uninstalled.pc.in: + * pkgconfig/gstreamer-plugins-base.pc.in: + Add libgstfft, a FFT library based on Kiss FFT which is + BSD licensed. Supported sample formats are int16, int32, + float and double. For those formats a real FFT and IFFT + can be done, different windowing functions can be applied + and functions for extracting the magnitude and phase exist. + Fixes #468619. + + * docs/libs/Makefile.am: + * docs/libs/gst-plugins-base-libs-docs.sgml: + * docs/libs/gst-plugins-base-libs-sections.txt: + Integrate libgstfft into the docs. + + * tests/check/Makefile.am: + * tests/check/libs/fft.c: (GST_START_TEST), (fft_suite), (main): + Add unit tests for libgstfft, currently only testing the FFT. + Unit tests for IFFT will follow soon. + 2007-09-05 Wim Taymans <wim.taymans@gmail.com> Patch by: Peter Kjellerstedt <pkj at axis com> diff --git a/configure.ac b/configure.ac index 7feacf8a6..bcc5e90e6 100644 --- a/configure.ac +++ b/configure.ac @@ -676,6 +676,7 @@ gst-libs/Makefile gst-libs/gst/Makefile gst-libs/gst/audio/Makefile gst-libs/gst/cdda/Makefile +gst-libs/gst/fft/Makefile gst-libs/gst/floatcast/Makefile gst-libs/gst/interfaces/Makefile gst-libs/gst/netbuffer/Makefile diff --git a/docs/libs/Makefile.am b/docs/libs/Makefile.am index 42b57ccb7..310b69362 100644 --- a/docs/libs/Makefile.am +++ b/docs/libs/Makefile.am @@ -69,6 +69,7 @@ SCANOBJ_DEPS = \ $(top_builddir)/gst-libs/gst/interfaces/libgstinterfaces-@GST_MAJORMINOR@.la \ $(top_builddir)/gst-libs/gst/audio/libgstaudio-@GST_MAJORMINOR@.la \ $(top_builddir)/gst-libs/gst/cdda/libgstcdda-@GST_MAJORMINOR@.la \ + $(top_builddir)/gst-libs/gst/fft/libgstfft-@GST_MAJORMINOR@.la \ $(top_builddir)/gst-libs/gst/rtp/libgstrtp-@GST_MAJORMINOR@.la \ $(top_builddir)/gst-libs/gst/rtsp/libgstrtsp-@GST_MAJORMINOR@.la \ $(top_builddir)/gst-libs/gst/sdp/libgstsdp-@GST_MAJORMINOR@.la \ diff --git a/docs/libs/gst-plugins-base-libs-docs.sgml b/docs/libs/gst-plugins-base-libs-docs.sgml index 3ef293dc9..cb75caab6 100644 --- a/docs/libs/gst-plugins-base-libs-docs.sgml +++ b/docs/libs/gst-plugins-base-libs-docs.sgml @@ -17,6 +17,12 @@ <!ENTITY GstRingBuffer SYSTEM "xml/gstringbuffer.xml"> <!-- cdda --> <!ENTITY GstCddaBaseSrc SYSTEM "xml/gstcddabasesrc.xml"> +<!-- fft --> +<!ENTITY GstFFT SYSTEM "xml/gstfft.xml"> +<!ENTITY GstFFTS16 SYSTEM "xml/gstffts16.xml"> +<!ENTITY GstFFTS32 SYSTEM "xml/gstffts32.xml"> +<!ENTITY GstFFTF32 SYSTEM "xml/gstfftf32.xml"> +<!ENTITY GstFFTF64 SYSTEM "xml/gstfftf64.xml"> <!-- floatcast --> <!ENTITY GstFloatCast SYSTEM "xml/gstfloatcast.xml"> <!-- interfaces --> @@ -117,6 +123,20 @@ &GstCddaBaseSrc; </chapter> + <chapter id="gstreamer-ffft"> + <title>FFT Library</title> + <para> + This library should be linked to by getting cflags and libs from + <filename>gstreamer-plugins-base-&GST_MAJORMINOR;.pc</filename> and adding + <filename>-lgstfft-&GST_MAJORMINOR;</filename> to the library flags. + </para> + &GstFFT; + &GstFFTS16; + &GstFFTS32; + &GstFFTF32; + &GstFFTF64; + </chapter> + <chapter id="gstreamer-floatcast"> <title>FloatCast Library</title> <para> diff --git a/docs/libs/gst-plugins-base-libs-sections.txt b/docs/libs/gst-plugins-base-libs-sections.txt index b4078af53..35d2c7a5c 100644 --- a/docs/libs/gst-plugins-base-libs-sections.txt +++ b/docs/libs/gst-plugins-base-libs-sections.txt @@ -226,6 +226,70 @@ GST_TYPE_CDDA_BASE_SRC gst_cdda_base_src_get_type </SECTION> +# fft + +<SECTION> +<FILE>gstfft</FILE> +<INCLUDE>gst/fft/gstfft.h</INCLUDE> +GstFFTWindow +gst_fft_next_fast_length +</SECTION> + +<SECTION> +<FILE>gstffts16</FILE> +<INCLUDE>gst/fft/gstffts16.h</INCLUDE> +GstFFTS16 +GstFFTS16Complex +gst_fft_s16_new +gst_fft_s16_fft +gst_fft_s16_inverse_fft +gst_fft_s16_free +gst_fft_s16_magnitude +gst_fft_s16_phase +gst_fft_s16_window +</SECTION> + +<SECTION> +<FILE>gstffts32</FILE> +<INCLUDE>gst/fft/gstffts32.h</INCLUDE> +GstFFTS32 +GstFFTS32Complex +gst_fft_s32_new +gst_fft_s32_fft +gst_fft_s32_inverse_fft +gst_fft_s32_free +gst_fft_s32_magnitude +gst_fft_s32_phase +gst_fft_s32_window +</SECTION> + +<SECTION> +<FILE>gstfftf32</FILE> +<INCLUDE>gst/fft/gstfftf32.h</INCLUDE> +GstFFTF32 +GstFFTF32Complex +gst_fft_f32_new +gst_fft_f32_fft +gst_fft_f32_inverse_fft +gst_fft_f32_free +gst_fft_f32_magnitude +gst_fft_f32_phase +gst_fft_f32_window +</SECTION> + +<SECTION> +<FILE>gstfftf64</FILE> +<INCLUDE>gst/fft/gstfftf64.h</INCLUDE> +GstFFTF64 +GstFFTF64Complex +gst_fft_f64_new +gst_fft_f64_fft +gst_fft_f64_inverse_fft +gst_fft_f64_free +gst_fft_f64_magnitude +gst_fft_f64_phase +gst_fft_f64_window +</SECTION> # floatcast diff --git a/gst-libs/gst/Makefile.am b/gst-libs/gst/Makefile.am index f4db55ace..d935ae45d 100644 --- a/gst-libs/gst/Makefile.am +++ b/gst-libs/gst/Makefile.am @@ -7,6 +7,7 @@ SUBDIRS = \ tag \ audio \ cdda \ + fft \ floatcast \ netbuffer \ pbutils \ diff --git a/gst-libs/gst/fft/Makefile.am b/gst-libs/gst/fft/Makefile.am new file mode 100644 index 000000000..25f853cc0 --- /dev/null +++ b/gst-libs/gst/fft/Makefile.am @@ -0,0 +1,46 @@ +lib_LTLIBRARIES = libgstfft-@GST_MAJORMINOR@.la + +libgstfft_@GST_MAJORMINOR@_includedir = $(includedir)/gstreamer-@GST_MAJORMINOR@/gst/fft + +libgstfft_@GST_MAJORMINOR@_include_HEADERS = \ + gstfft.h \ + gstffts16.h \ + gstffts32.h \ + gstfftf32.h \ + gstfftf64.h + +noinst_HEADERS = \ + kiss_fft_s16.h \ + kiss_fft_s32.h \ + kiss_fft_f32.h \ + kiss_fft_f64.h \ + kiss_fftr_s16.h \ + kiss_fftr_s32.h \ + kiss_fftr_f32.h \ + kiss_fftr_f64.h \ + _kiss_fft_guts_s16.h \ + _kiss_fft_guts_s32.h \ + _kiss_fft_guts_f32.h \ + _kiss_fft_guts_f64.h + +libgstfft_@GST_MAJORMINOR@_la_SOURCES = \ + gstfft.c \ + gstffts16.c \ + gstffts32.c \ + gstfftf32.c \ + gstfftf64.c \ + kiss_fft_s16.c \ + kiss_fft_s32.c \ + kiss_fft_f32.c \ + kiss_fft_f64.c \ + kiss_fftr_s16.c \ + kiss_fftr_s32.c \ + kiss_fftr_f32.c \ + kiss_fftr_f64.c + +libgstfft_@GST_MAJORMINOR@_la_LIBADD = $(GST_LIBS) $(LIBM) +libgstfft_@GST_MAJORMINOR@_la_CFLAGS = $(GST_CFLAGS) +libgstfft_@GST_MAJORMINOR@_la_LDFLAGS = $(GST_LIB_LDFLAGS) $(GST_ALL_LDFLAGS) $(GST_LT_LDFLAGS) + +EXTRA_DIST = kiss_version + diff --git a/gst-libs/gst/fft/_kiss_fft_guts_f32.h b/gst-libs/gst/fft/_kiss_fft_guts_f32.h new file mode 100644 index 000000000..15dc06b41 --- /dev/null +++ b/gst-libs/gst/fft/_kiss_fft_guts_f32.h @@ -0,0 +1,98 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* kiss_fft.h + defines kiss_fft_f32_scalar as either short or a float type + and defines + typedef struct { kiss_fft_f32_scalar r; kiss_fft_f32_scalar i; }kiss_fft_f32_cpx; */ +#include "kiss_fft_f32.h" +#include <limits.h> + +#define MAXFACTORS 32 +/* e.g. an fft of length 128 has 4 factors + as far as kissfft is concerned + 4*4*4*2 + */ + +struct kiss_fft_f32_state{ + int nfft; + int inverse; + int factors[2*MAXFACTORS]; + kiss_fft_f32_cpx twiddles[1]; +}; + +/* + Explanation of macros dealing with complex math: + + C_MUL(m,a,b) : m = a*b + C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise + C_SUB( res, a,b) : res = a - b + C_SUBFROM( res , a) : res -= a + C_ADDTO( res , a) : res += a + * */ + +#define S_MUL(a,b) ( (a)*(b) ) +#define C_MUL(m,a,b) \ + do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ + (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) +#define C_FIXDIV(c,div) /* NOOP */ +#define C_MULBYSCALAR( c, s ) \ + do{ (c).r *= (s);\ + (c).i *= (s); }while(0) + +#ifndef CHECK_OVERFLOW_OP +# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ +#endif + +#define C_ADD( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,+,(b).r)\ + CHECK_OVERFLOW_OP((a).i,+,(b).i)\ + (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ + }while(0) +#define C_SUB( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,-,(b).r)\ + CHECK_OVERFLOW_OP((a).i,-,(b).i)\ + (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ + }while(0) +#define C_ADDTO( res , a)\ + do { \ + CHECK_OVERFLOW_OP((res).r,+,(a).r)\ + CHECK_OVERFLOW_OP((res).i,+,(a).i)\ + (res).r += (a).r; (res).i += (a).i;\ + }while(0) + +#define C_SUBFROM( res , a)\ + do {\ + CHECK_OVERFLOW_OP((res).r,-,(a).r)\ + CHECK_OVERFLOW_OP((res).i,-,(a).i)\ + (res).r -= (a).r; (res).i -= (a).i; \ + }while(0) + + +#define KISS_FFT_F32_COS(phase) (kiss_fft_f32_scalar) cos(phase) +#define KISS_FFT_F32_SIN(phase) (kiss_fft_f32_scalar) sin(phase) +#define HALF_OF(x) ((x)*.5) + +#define kf_cexp(x,phase) \ + do{ \ + (x)->r = KISS_FFT_F32_COS(phase);\ + (x)->i = KISS_FFT_F32_SIN(phase);\ + }while(0) + + +/* a debugging function */ +#define pcpx(c)\ + fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) diff --git a/gst-libs/gst/fft/_kiss_fft_guts_f64.h b/gst-libs/gst/fft/_kiss_fft_guts_f64.h new file mode 100644 index 000000000..276f65eab --- /dev/null +++ b/gst-libs/gst/fft/_kiss_fft_guts_f64.h @@ -0,0 +1,98 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* kiss_fft.h + defines kiss_fft_f64_scalar as either short or a float type + and defines + typedef struct { kiss_fft_f64_scalar r; kiss_fft_f64_scalar i; }kiss_fft_f64_cpx; */ +#include "kiss_fft_f64.h" +#include <limits.h> + +#define MAXFACTORS 32 +/* e.g. an fft of length 128 has 4 factors + as far as kissfft is concerned + 4*4*4*2 + */ + +struct kiss_fft_f64_state{ + int nfft; + int inverse; + int factors[2*MAXFACTORS]; + kiss_fft_f64_cpx twiddles[1]; +}; + +/* + Explanation of macros dealing with complex math: + + C_MUL(m,a,b) : m = a*b + C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise + C_SUB( res, a,b) : res = a - b + C_SUBFROM( res , a) : res -= a + C_ADDTO( res , a) : res += a + * */ + +#define S_MUL(a,b) ( (a)*(b) ) +#define C_MUL(m,a,b) \ + do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ + (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) +#define C_FIXDIV(c,div) /* NOOP */ +#define C_MULBYSCALAR( c, s ) \ + do{ (c).r *= (s);\ + (c).i *= (s); }while(0) + +#ifndef CHECK_OVERFLOW_OP +# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ +#endif + +#define C_ADD( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,+,(b).r)\ + CHECK_OVERFLOW_OP((a).i,+,(b).i)\ + (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ + }while(0) +#define C_SUB( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,-,(b).r)\ + CHECK_OVERFLOW_OP((a).i,-,(b).i)\ + (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ + }while(0) +#define C_ADDTO( res , a)\ + do { \ + CHECK_OVERFLOW_OP((res).r,+,(a).r)\ + CHECK_OVERFLOW_OP((res).i,+,(a).i)\ + (res).r += (a).r; (res).i += (a).i;\ + }while(0) + +#define C_SUBFROM( res , a)\ + do {\ + CHECK_OVERFLOW_OP((res).r,-,(a).r)\ + CHECK_OVERFLOW_OP((res).i,-,(a).i)\ + (res).r -= (a).r; (res).i -= (a).i; \ + }while(0) + + +#define KISS_FFT_F64_COS(phase) (kiss_fft_f64_scalar) cos(phase) +#define KISS_FFT_F64_SIN(phase) (kiss_fft_f64_scalar) sin(phase) +#define HALF_OF(x) ((x)*.5) + +#define kf_cexp(x,phase) \ + do{ \ + (x)->r = KISS_FFT_F64_COS(phase);\ + (x)->i = KISS_FFT_F64_SIN(phase);\ + }while(0) + + +/* a debugging function */ +#define pcpx(c)\ + fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) diff --git a/gst-libs/gst/fft/_kiss_fft_guts_s16.h b/gst-libs/gst/fft/_kiss_fft_guts_s16.h new file mode 100644 index 000000000..110ed7b4d --- /dev/null +++ b/gst-libs/gst/fft/_kiss_fft_guts_s16.h @@ -0,0 +1,121 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* kiss_fft.h + defines kiss_fft_s16_scalar as either short or a float type + and defines + typedef struct { kiss_fft_s16_scalar r; kiss_fft_s16_scalar i; }kiss_fft_s16_cpx; */ +#include "kiss_fft_s16.h" +#include <limits.h> + +#define MAXFACTORS 32 +/* e.g. an fft of length 128 has 4 factors + as far as kissfft is concerned + 4*4*4*2 + */ + +struct kiss_fft_s16_state{ + int nfft; + int inverse; + int factors[2*MAXFACTORS]; + kiss_fft_s16_cpx twiddles[1]; +}; + +/* + Explanation of macros dealing with complex math: + + C_MUL(m,a,b) : m = a*b + C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise + C_SUB( res, a,b) : res = a - b + C_SUBFROM( res , a) : res -= a + C_ADDTO( res , a) : res += a + * */ +#define FRACBITS 15 +#define SAMPPROD int32_t +#define SAMP_MAX 32767 + +#define SAMP_MIN -SAMP_MAX + +#if defined(CHECK_OVERFLOW) +# define CHECK_OVERFLOW_OP(a,op,b) \ + if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ + fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } +#endif + + +# define smul(a,b) ( (SAMPPROD)(a)*(b) ) +# define sround( x ) (kiss_fft_s16_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) + +# define S_MUL(a,b) sround( smul(a,b) ) + +# define C_MUL(m,a,b) \ + do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ + (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) + +# define DIVSCALAR(x,k) \ + (x) = sround( smul( x, SAMP_MAX/k ) ) + +# define C_FIXDIV(c,div) \ + do { DIVSCALAR( (c).r , div); \ + DIVSCALAR( (c).i , div); }while (0) + +# define C_MULBYSCALAR( c, s ) \ + do{ (c).r = sround( smul( (c).r , s ) ) ;\ + (c).i = sround( smul( (c).i , s ) ) ; }while(0) + +#ifndef CHECK_OVERFLOW_OP +# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ +#endif + +#define C_ADD( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,+,(b).r)\ + CHECK_OVERFLOW_OP((a).i,+,(b).i)\ + (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ + }while(0) +#define C_SUB( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,-,(b).r)\ + CHECK_OVERFLOW_OP((a).i,-,(b).i)\ + (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ + }while(0) +#define C_ADDTO( res , a)\ + do { \ + CHECK_OVERFLOW_OP((res).r,+,(a).r)\ + CHECK_OVERFLOW_OP((res).i,+,(a).i)\ + (res).r += (a).r; (res).i += (a).i;\ + }while(0) + +#define C_SUBFROM( res , a)\ + do {\ + CHECK_OVERFLOW_OP((res).r,-,(a).r)\ + CHECK_OVERFLOW_OP((res).i,-,(a).i)\ + (res).r -= (a).r; (res).i -= (a).i; \ + }while(0) + + +# define KISS_FFT_S16_COS(phase) floor(.5+SAMP_MAX * cos (phase)) +# define KISS_FFT_S16_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) +# define HALF_OF(x) ((x)>>1) + +#define kf_cexp(x,phase) \ + do{ \ + (x)->r = KISS_FFT_S16_COS(phase);\ + (x)->i = KISS_FFT_S16_SIN(phase);\ + }while(0) + + +/* a debugging function */ +#define pcpx(c)\ + fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) diff --git a/gst-libs/gst/fft/_kiss_fft_guts_s32.h b/gst-libs/gst/fft/_kiss_fft_guts_s32.h new file mode 100644 index 000000000..5da7ec396 --- /dev/null +++ b/gst-libs/gst/fft/_kiss_fft_guts_s32.h @@ -0,0 +1,122 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* kiss_fft.h + defines kiss_fft_s32_scalar as either short or a float type + and defines + typedef struct { kiss_fft_s32_scalar r; kiss_fft_s32_scalar i; }kiss_fft_s32_cpx; */ +#include "kiss_fft_s32.h" +#include <limits.h> + +#define MAXFACTORS 32 +/* e.g. an fft of length 128 has 4 factors + as far as kissfft is concerned + 4*4*4*2 + */ + +struct kiss_fft_s32_state{ + int nfft; + int inverse; + int factors[2*MAXFACTORS]; + kiss_fft_s32_cpx twiddles[1]; +}; + +/* + Explanation of macros dealing with complex math: + + C_MUL(m,a,b) : m = a*b + C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise + C_SUB( res, a,b) : res = a - b + C_SUBFROM( res , a) : res -= a + C_ADDTO( res , a) : res += a + * */ + +#define FRACBITS 31 +#define SAMPPROD int64_t +#define SAMP_MAX 2147483647 + +#define SAMP_MIN -SAMP_MAX + +#if defined(CHECK_OVERFLOW) +# define CHECK_OVERFLOW_OP(a,op,b) \ + if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ + fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } +#endif + + +# define smul(a,b) ( (SAMPPROD)(a)*(b) ) +# define sround( x ) (kiss_fft_s32_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) + +# define S_MUL(a,b) sround( smul(a,b) ) + +# define C_MUL(m,a,b) \ + do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ + (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) + +# define DIVSCALAR(x,k) \ + (x) = sround( smul( x, SAMP_MAX/k ) ) + +# define C_FIXDIV(c,div) \ + do { DIVSCALAR( (c).r , div); \ + DIVSCALAR( (c).i , div); }while (0) + +# define C_MULBYSCALAR( c, s ) \ + do{ (c).r = sround( smul( (c).r , s ) ) ;\ + (c).i = sround( smul( (c).i , s ) ) ; }while(0) + +#ifndef CHECK_OVERFLOW_OP +# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ +#endif + +#define C_ADD( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,+,(b).r)\ + CHECK_OVERFLOW_OP((a).i,+,(b).i)\ + (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ + }while(0) +#define C_SUB( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,-,(b).r)\ + CHECK_OVERFLOW_OP((a).i,-,(b).i)\ + (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ + }while(0) +#define C_ADDTO( res , a)\ + do { \ + CHECK_OVERFLOW_OP((res).r,+,(a).r)\ + CHECK_OVERFLOW_OP((res).i,+,(a).i)\ + (res).r += (a).r; (res).i += (a).i;\ + }while(0) + +#define C_SUBFROM( res , a)\ + do {\ + CHECK_OVERFLOW_OP((res).r,-,(a).r)\ + CHECK_OVERFLOW_OP((res).i,-,(a).i)\ + (res).r -= (a).r; (res).i -= (a).i; \ + }while(0) + + +# define KISS_FFT_S32_COS(phase) floor(.5+SAMP_MAX * cos (phase)) +# define KISS_FFT_S32_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) +# define HALF_OF(x) ((x)>>1) + +#define kf_cexp(x,phase) \ + do{ \ + (x)->r = KISS_FFT_S32_COS(phase);\ + (x)->i = KISS_FFT_S32_SIN(phase);\ + }while(0) + + +/* a debugging function */ +#define pcpx(c)\ + fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) diff --git a/gst-libs/gst/fft/gstfft.c b/gst-libs/gst/fft/gstfft.c new file mode 100644 index 000000000..e857384e2 --- /dev/null +++ b/gst-libs/gst/fft/gstfft.c @@ -0,0 +1,55 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/** + * SECTION:gstfft + * @short_description: General FFT functions and declarations + * + * This library includes general definitions and functions, useful for + * all typed FFT classes. + * + */ + +#include <glib.h> + +#include "gstfft.h" +#include "kiss_fft_s16.h" + +/** + * gst_fft_next_fast_length: + * @n: Number for which the next fast length should be returned + * + * Returns the next number to @n that is entirely a product + * of 2, 3 and 5. Using this as the @len parameter for + * the different GstFFT types will provide the best performance. + * + * Returns: the next fast FFT length. + * + */ +gint +gst_fft_next_fast_length (gint n) +{ + gint half = (n + 1) / 2; + + /* It's the same for all data types so call the s16 + * version */ + + /* The real FFT needs an even length so calculate that */ + return 2 * kiss_fft_s16_next_fast_size (half); +} diff --git a/gst-libs/gst/fft/gstfft.h b/gst-libs/gst/fft/gstfft.h new file mode 100644 index 000000000..f48f31230 --- /dev/null +++ b/gst-libs/gst/fft/gstfft.h @@ -0,0 +1,49 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <glib.h> +#include <math.h> + +#ifndef __GST_FFT_H__ +#define __GST_FFT_H__ + +/** + * GstFFTWindow: + * @GST_FFT_WINDOW_RECTANGULAR: Rectangular window + * @GST_FFT_WINDOW_HAMMING: Hamming window + * @GST_FFT_WINDOW_HANN: Hann (sometimes also called Hanning) window + * @GST_FFT_WINDOW_BARTLETT: Bartlett window + * @GST_FFT_WINDOW_BLACKMAN: Blackman window + * + * The various window functions available. + */ +typedef enum +{ + GST_FFT_WINDOW_RECTANGULAR, + GST_FFT_WINDOW_HAMMING, + GST_FFT_WINDOW_HANN, + GST_FFT_WINDOW_BARTLETT, + GST_FFT_WINDOW_BLACKMAN +} GstFFTWindow; + +/* Functions */ + +gint gst_fft_next_fast_length (gint n); + +#endif /* __GST_FFT_H__ */ diff --git a/gst-libs/gst/fft/gstfftf32.c b/gst-libs/gst/fft/gstfftf32.c new file mode 100644 index 000000000..53038e064 --- /dev/null +++ b/gst-libs/gst/fft/gstfftf32.c @@ -0,0 +1,270 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <glib.h> +#include <math.h> + +#include "kiss_fftr_f32.h" +#include "gstfft.h" +#include "gstfftf32.h" + +/** + * SECTION:gstfftf32 + * @short_description: FFT functions for 32 bit float samples + * + * #GstFFTF32 provides a FFT implementation and related functions for + * 32 bit float samples. To use this call gst_fft_f32_new() for + * allocating a #GstFFTF32 instance with the appropiate parameters and + * then call gst_fft_f32_fft() or gst_fft_f32_inverse_fft() to perform the + * FFT or inverse FFT on a buffer of samples. + * + * After use free the #GstFFTF32 instance with gst_fft_f32_free(). + * + * For the best performance use gst_fft_next_fast_length() to get a + * number that is entirely a product of 2, 3 and 5 and use this as the + * @len parameter for gst_fft_f32_new(). + * + * The @len parameter specifies the number of samples in the time domain that + * will be processed or generated. The number of samples in the frequency domain + * is @len/2 + 1. To get n samples in the frequency domain use 2*n - 2 as @len. + * + * Before performing the FFT on time domain data it usually makes sense + * to apply a window function to it. For this gst_fft_f32_window() can comfortably + * be used. + * + * For calculating the magnitude or phase of frequency data the functions + * gst_fft_f32_magnitude() and gst_fft_f32_phase() exist, if you want to calculate + * the magnitude yourself note that the magnitude of the frequency data is + * a value between 0 and 1 and is to be scaled by the length of the FFT. + * + */ + +/** + * gst_fft_f32_new: + * @len: Length of the FFT in the time domain + * @inverse: %TRUE if the #GstFFTF32 instance should be used for the inverse FFT + * + * This returns a new #GstFFTF32 instance with the given parameters. It makes + * sense to keep one instance for several calls for speed reasons. + * + * @len must be even and to get the best performance a product of + * 2, 3 and 5. To get the next number with this characteristics use + * gst_fft_next_fast_length(). + * + * Returns: a new #GstFFTF32 instance. + */ +GstFFTF32 * +gst_fft_f32_new (gint len, gboolean inverse) +{ + GstFFTF32 *self; + + g_return_val_if_fail (len > 0, NULL); + g_return_val_if_fail (len % 2 == 0, NULL); + + self = g_new (GstFFTF32, 1); + + self->cfg = kiss_fftr_f32_alloc (len, (inverse) ? 1 : 0, NULL, NULL); + g_assert (self->cfg); + + self->inverse = inverse; + self->len = len; + + return self; +} + +/** + * gst_fft_f32_fft: + * @self: #GstFFTF32 instance for this call + * @timedata: Buffer of the samples in the time domain + * @freqdata: Target buffer for the samples in the frequency domain + * + * This performs the FFT on @timedata and puts the result in @freqdata. + * + * @timedata must have as many samples as specified with the @len parameter while + * allocating the #GstFFTF32 instance with gst_fft_f32_new(). + * + * @freqdata must be large enough to hold @len/2 + 1 #GstFFTF32Complex frequency + * domain samples. + * + */ +void +gst_fft_f32_fft (GstFFTF32 * self, const gfloat * timedata, + GstFFTF32Complex * freqdata) +{ + g_return_if_fail (self); + g_return_if_fail (!self->inverse); + g_return_if_fail (timedata); + g_return_if_fail (freqdata); + + kiss_fftr_f32 (self->cfg, timedata, (kiss_fft_f32_cpx *) freqdata); +} + +/** + * gst_fft_f32_inverse_fft: + * @self: #GstFFTF32 instance for this call + * @freqdata: Buffer of the samples in the frequency domain + * @timedata: Target buffer for the samples in the time domain + * + * This performs the inverse FFT on @freqdata and puts the result in @timedata. + * + * @freqdata must have @len/2 + 1 samples, where @len is the parameter specified + * while allocating the #GstFFTF32 instance with gst_fft_f32_new(). + * + * @timedata must be large enough to hold @len time domain samples. + * + */ +void +gst_fft_f32_inverse_fft (GstFFTF32 * self, const GstFFTF32Complex * freqdata, + gfloat * timedata) +{ + g_return_if_fail (self); + g_return_if_fail (self->inverse); + g_return_if_fail (timedata); + g_return_if_fail (freqdata); + + kiss_fftri_f32 (self->cfg, (kiss_fft_f32_cpx *) freqdata, timedata); +} + +/** + * gst_fft_f32_free: + * @self: #GstFFTF32 instance for this call + * + * This frees the memory allocated for @self. + * + */ +void +gst_fft_f32_free (GstFFTF32 * self) +{ + kiss_fftr_f32_free (self->cfg); + g_free (self); +} + +/** + * gst_fft_f32_window: + * @self: #GstFFTF32 instance for this call + * @timedata: Time domain samples + * @window: Window function to apply + * + * This calls the window function @window on the @timedata sample buffer. + * + */ +void +gst_fft_f32_window (GstFFTF32 * self, gfloat * timedata, GstFFTWindow window) +{ + gint i, len; + + g_return_if_fail (self); + g_return_if_fail (timedata); + + len = self->len; + + switch (window) { + case GST_FFT_WINDOW_RECTANGULAR: + /* do nothing */ + break; + case GST_FFT_WINDOW_HAMMING: + for (i = 0; i < len; i++) + timedata[i] *= (0.53836 - 0.46164 * cos (2.0 * M_PI * i / len)); + break; + case GST_FFT_WINDOW_HANN: + for (i = 0; i < len; i++) + timedata[i] *= (0.5 - 0.5 * cos (2.0 * M_PI * i / len)); + break; + case GST_FFT_WINDOW_BARTLETT: + for (i = 0; i < len; i++) + timedata[i] *= (1.0 - fabs ((2.0 * i - len) / len)); + break; + case GST_FFT_WINDOW_BLACKMAN: + for (i = 0; i < len; i++) + timedata[i] *= (0.42 - 0.5 * cos ((2.0 * i) / len) + + 0.08 * cos ((4.0 * i) / len)); + break; + default: + g_assert_not_reached (); + break; + } +} + +/** + * gst_fft_f32_magnitude: + * @self: #GstFFTF32 instance for this call + * @freqdata: Frequency domain samples + * @magnitude: Target buffer for the magnitude + * @decibel: %TRUE if the magnitude should be in decibel, %FALSE if it should be an amplitude + * + * This calculates the magnitude of @freqdata in @magnitude. Depending on the value + * of @decibel the magnitude can be calculated in decibel or as amplitude between 0.0 + * and 1.0. + * + * @magnitude must be large enough to hold @len/2 + 1 values. + * + */ +void +gst_fft_f32_magnitude (GstFFTF32 * self, GstFFTF32Complex * freqdata, + gdouble * magnitude, gboolean decibel) +{ + gint i, len, nfft; + gdouble val; + + g_return_if_fail (self); + g_return_if_fail (freqdata); + g_return_if_fail (magnitude); + + len = self->len / 2 + 1; + nfft = self->len; + + for (i = 0; i < len; i++) { + val = (gdouble) freqdata[i].r * (gdouble) freqdata[i].r + + (gdouble) freqdata[i].i * (gdouble) freqdata[i].i; + val = sqrt (val) / nfft; + + if (decibel) + val = 20.0 * log10 (val); + + magnitude[i] = val; + } +} + +/** + * gst_fft_f32_phase: + * @self: #GstFFTF32 instance for this call + * @freqdata: Frequency domain samples + * @phase: Target buffer for the phase + * + * This calculates the phases of @freqdata in @phase. The returned + * phases will be values between -pi and pi. + * + * @phase must be large enough to hold @len/2 + 1 values. + * + */ +void +gst_fft_f32_phase (GstFFTF32 * self, GstFFTF32Complex * freqdata, + gdouble * phase) +{ + gint i, len; + + g_return_if_fail (self); + g_return_if_fail (freqdata); + g_return_if_fail (phase); + + len = self->len / 2 + 1; + + for (i = 0; i < len; i++) + phase[i] = atan2 (freqdata[i].i, freqdata[i].r); +} diff --git a/gst-libs/gst/fft/gstfftf32.h b/gst-libs/gst/fft/gstfftf32.h new file mode 100644 index 000000000..b0b6f673f --- /dev/null +++ b/gst-libs/gst/fft/gstfftf32.h @@ -0,0 +1,77 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_FFT_F32_H__ +#define __GST_FFT_F32_H__ + +#include <glib.h> +#include <gst/gst.h> + +#include "gstfft.h" + +typedef struct _GstFFTF32 GstFFTF32; +typedef struct _GstFFTF32Complex GstFFTF32Complex; + +/** + * GstFFTF32: + * + * Instance structure for #GstFFTF32. + * + */ +struct _GstFFTF32 { + /* <private> */ + void * cfg; + gboolean inverse; + gint len; + gpointer _padding[GST_PADDING]; +}; + +/* Copy of kiss_fft_f32_cpx for documentation reasons, + * do NOT change! */ + +/** + * GstFFTF32Complex: + * @r: Real part + * @i: Imaginary part + * + * Data type for complex numbers composed of + * 32 bit float. + * + */ +struct _GstFFTF32Complex +{ + gfloat r; + gfloat i; +}; + +/* Functions */ + +GstFFTF32 * gst_fft_f32_new (gint len, gboolean inverse); +void gst_fft_f32_fft (GstFFTF32 *self, const gfloat *timedata, GstFFTF32Complex *freqdata); +void gst_fft_f32_inverse_fft (GstFFTF32 *self, const GstFFTF32Complex *freqdata, gfloat *timedata); +void gst_fft_f32_free (GstFFTF32 *self); + +void gst_fft_f32_window (GstFFTF32 *self, gfloat *timedata, GstFFTWindow window); + +void gst_fft_f32_magnitude (GstFFTF32 *self, GstFFTF32Complex *freqdata, + gdouble *magnitude, gboolean decibel); +void gst_fft_f32_phase (GstFFTF32 *self, GstFFTF32Complex *freqdata, + gdouble *phase); + +#endif /* __GST_FFT_F32_H__ */ diff --git a/gst-libs/gst/fft/gstfftf64.c b/gst-libs/gst/fft/gstfftf64.c new file mode 100644 index 000000000..1a65014bf --- /dev/null +++ b/gst-libs/gst/fft/gstfftf64.c @@ -0,0 +1,270 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <glib.h> +#include <math.h> + +#include "kiss_fftr_f64.h" +#include "gstfft.h" +#include "gstfftf64.h" + +/** + * SECTION:gstfftf64 + * @short_description: FFT functions for 64 bit float samples + * + * #GstFFTF64 provides a FFT implementation and related functions for + * 64 bit float samples. To use this call gst_fft_f64_new() for + * allocating a #GstFFTF64 instance with the appropiate parameters and + * then call gst_fft_f64_fft() or gst_fft_f64_inverse_fft() to perform the + * FFT or inverse FFT on a buffer of samples. + * + * After use free the #GstFFTF64 instance with gst_fft_f64_free(). + * + * For the best performance use gst_fft_next_fast_length() to get a + * number that is entirely a product of 2, 3 and 5 and use this as the + * @len parameter for gst_fft_f64_new(). + * + * The @len parameter specifies the number of samples in the time domain that + * will be processed or generated. The number of samples in the frequency domain + * is @len/2 + 1. To get n samples in the frequency domain use 2*n - 2 as @len. + * + * Before performing the FFT on time domain data it usually makes sense + * to apply a window function to it. For this gst_fft_f64_window() can comfortably + * be used. + * + * For calculating the magnitude or phase of frequency data the functions + * gst_fft_f64_magnitude() and gst_fft_f64_phase() exist, if you want to calculate + * the magnitude yourself note that the magnitude of the frequency data is + * a value between 0 and 1 and is to be scaled by the length of the FFT. + * + */ + +/** + * gst_fft_f64_new: + * @len: Length of the FFT in the time domain + * @inverse: %TRUE if the #GstFFTF64 instance should be used for the inverse FFT + * + * This returns a new #GstFFTF64 instance with the given parameters. It makes + * sense to keep one instance for several calls for speed reasons. + * + * @len must be even and to get the best performance a product of + * 2, 3 and 5. To get the next number with this characteristics use + * gst_fft_next_fast_length(). + * + * Returns: a new #GstFFTF64 instance. + */ +GstFFTF64 * +gst_fft_f64_new (gint len, gboolean inverse) +{ + GstFFTF64 *self; + + g_return_val_if_fail (len > 0, NULL); + g_return_val_if_fail (len % 2 == 0, NULL); + + self = g_new (GstFFTF64, 1); + + self->cfg = kiss_fftr_f64_alloc (len, (inverse) ? 1 : 0, NULL, NULL); + g_assert (self->cfg); + + self->inverse = inverse; + self->len = len; + + return self; +} + +/** + * gst_fft_f64_fft: + * @self: #GstFFTF64 instance for this call + * @timedata: Buffer of the samples in the time domain + * @freqdata: Target buffer for the samples in the frequency domain + * + * This performs the FFT on @timedata and puts the result in @freqdata. + * + * @timedata must have as many samples as specified with the @len parameter while + * allocating the #GstFFTF64 instance with gst_fft_f64_new(). + * + * @freqdata must be large enough to hold @len/2 + 1 #GstFFTF64Complex frequency + * domain samples. + * + */ +void +gst_fft_f64_fft (GstFFTF64 * self, const gdouble * timedata, + GstFFTF64Complex * freqdata) +{ + g_return_if_fail (self); + g_return_if_fail (!self->inverse); + g_return_if_fail (timedata); + g_return_if_fail (freqdata); + + kiss_fftr_f64 (self->cfg, timedata, (kiss_fft_f64_cpx *) freqdata); +} + +/** + * gst_fft_f64_inverse_fft: + * @self: #GstFFTF64 instance for this call + * @freqdata: Buffer of the samples in the frequency domain + * @timedata: Target buffer for the samples in the time domain + * + * This performs the inverse FFT on @freqdata and puts the result in @timedata. + * + * @freqdata must have @len/2 + 1 samples, where @len is the parameter specified + * while allocating the #GstFFTF64 instance with gst_fft_f64_new(). + * + * @timedata must be large enough to hold @len time domain samples. + * + */ +void +gst_fft_f64_inverse_fft (GstFFTF64 * self, const GstFFTF64Complex * freqdata, + gdouble * timedata) +{ + g_return_if_fail (self); + g_return_if_fail (self->inverse); + g_return_if_fail (timedata); + g_return_if_fail (freqdata); + + kiss_fftri_f64 (self->cfg, (kiss_fft_f64_cpx *) freqdata, timedata); +} + +/** + * gst_fft_f64_free: + * @self: #GstFFTF64 instance for this call + * + * This frees the memory allocated for @self. + * + */ +void +gst_fft_f64_free (GstFFTF64 * self) +{ + kiss_fftr_f64_free (self->cfg); + g_free (self); +} + +/** + * gst_fft_f64_window: + * @self: #GstFFTF64 instance for this call + * @timedata: Time domain samples + * @window: Window function to apply + * + * This calls the window function @window on the @timedata sample buffer. + * + */ +void +gst_fft_f64_window (GstFFTF64 * self, gdouble * timedata, GstFFTWindow window) +{ + gint i, len; + + g_return_if_fail (self); + g_return_if_fail (timedata); + + len = self->len; + + switch (window) { + case GST_FFT_WINDOW_RECTANGULAR: + /* do nothing */ + break; + case GST_FFT_WINDOW_HAMMING: + for (i = 0; i < len; i++) + timedata[i] *= (0.53836 - 0.46164 * cos (2.0 * M_PI * i / len)); + break; + case GST_FFT_WINDOW_HANN: + for (i = 0; i < len; i++) + timedata[i] *= (0.5 - 0.5 * cos (2.0 * M_PI * i / len)); + break; + case GST_FFT_WINDOW_BARTLETT: + for (i = 0; i < len; i++) + timedata[i] *= (1.0 - fabs ((2.0 * i - len) / len)); + break; + case GST_FFT_WINDOW_BLACKMAN: + for (i = 0; i < len; i++) + timedata[i] *= (0.42 - 0.5 * cos ((2.0 * i) / len) + + 0.08 * cos ((4.0 * i) / len)); + break; + default: + g_assert_not_reached (); + break; + } +} + +/** + * gst_fft_f64_magnitude: + * @self: #GstFFTF64 instance for this call + * @freqdata: Frequency domain samples + * @magnitude: Target buffer for the magnitude + * @decibel: %TRUE if the magnitude should be in decibel, %FALSE if it should be an amplitude + * + * This calculates the magnitude of @freqdata in @magnitude. Depending on the value + * of @decibel the magnitude can be calculated in decibel or as amplitude between 0.0 + * and 1.0. + * + * @magnitude must be large enough to hold @len/2 + 1 values. + * + */ +void +gst_fft_f64_magnitude (GstFFTF64 * self, GstFFTF64Complex * freqdata, + gdouble * magnitude, gboolean decibel) +{ + gint i, len, nfft; + gdouble val; + + g_return_if_fail (self); + g_return_if_fail (freqdata); + g_return_if_fail (magnitude); + + len = self->len / 2 + 1; + nfft = self->len; + + for (i = 0; i < len; i++) { + val = (gdouble) freqdata[i].r * (gdouble) freqdata[i].r + + (gdouble) freqdata[i].i * (gdouble) freqdata[i].i; + val = sqrt (val) / nfft; + + if (decibel) + val = 20.0 * log10 (val); + + magnitude[i] = val; + } +} + +/** + * gst_fft_f64_phase: + * @self: #GstFFTF64 instance for this call + * @freqdata: Frequency domain samples + * @phase: Target buffer for the phase + * + * This calculates the phases of @freqdata in @phase. The returned + * phases will be values between -pi and pi. + * + * @phase must be large enough to hold @len/2 + 1 values. + * + */ +void +gst_fft_f64_phase (GstFFTF64 * self, GstFFTF64Complex * freqdata, + gdouble * phase) +{ + gint i, len; + + g_return_if_fail (self); + g_return_if_fail (freqdata); + g_return_if_fail (phase); + + len = self->len / 2 + 1; + + for (i = 0; i < len; i++) + phase[i] = atan2 (freqdata[i].i, freqdata[i].r); +} diff --git a/gst-libs/gst/fft/gstfftf64.h b/gst-libs/gst/fft/gstfftf64.h new file mode 100644 index 000000000..99c1de270 --- /dev/null +++ b/gst-libs/gst/fft/gstfftf64.h @@ -0,0 +1,77 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_FFT_F64_H__ +#define __GST_FFT_F64_H__ + +#include <glib.h> +#include <gst/gst.h> + +#include "gstfft.h" + +typedef struct _GstFFTF64 GstFFTF64; +typedef struct _GstFFTF64Complex GstFFTF64Complex; + +/** + * GstFFTF64: + * + * Instance structure for #GstFFTF64. + * + */ +struct _GstFFTF64 { + /* <private> */ + void * cfg; + gboolean inverse; + gint len; + gpointer _padding[GST_PADDING]; +}; + +/* Copy of kiss_fft_f64_cpx for documentation reasons, + * do NOT change! */ + +/** + * GstFFTF64Complex: + * @r: Real part + * @i: Imaginary part + * + * Data type for complex numbers composed of + * 64 bit float. + * + */ +struct _GstFFTF64Complex +{ + gdouble r; + gdouble i; +}; + +/* Functions */ + +GstFFTF64 * gst_fft_f64_new (gint len, gboolean inverse); +void gst_fft_f64_fft (GstFFTF64 *self, const gdouble *timedata, GstFFTF64Complex *freqdata); +void gst_fft_f64_inverse_fft (GstFFTF64 *self, const GstFFTF64Complex *freqdata, gdouble *timedata); +void gst_fft_f64_free (GstFFTF64 *self); + +void gst_fft_f64_window (GstFFTF64 *self, gdouble *timedata, GstFFTWindow window); + +void gst_fft_f64_magnitude (GstFFTF64 *self, GstFFTF64Complex *freqdata, + gdouble *magnitude, gboolean decibel); +void gst_fft_f64_phase (GstFFTF64 *self, GstFFTF64Complex *freqdata, + gdouble *phase); + +#endif /* __GST_FFT_F64_H__ */ diff --git a/gst-libs/gst/fft/gstffts16.c b/gst-libs/gst/fft/gstffts16.c new file mode 100644 index 000000000..482780304 --- /dev/null +++ b/gst-libs/gst/fft/gstffts16.c @@ -0,0 +1,269 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <glib.h> +#include <math.h> + +#include "kiss_fftr_s16.h" +#include "gstfft.h" +#include "gstffts16.h" + +/** + * SECTION:gstffts16 + * @short_description: FFT functions for signed 16 bit integer samples + * + * #GstFFTS16 provides a FFT implementation and related functions for + * signed 16 bit integer samples. To use this call gst_fft_s16_new() for + * allocating a #GstFFTS16 instance with the appropiate parameters and + * then call gst_fft_s16_fft() or gst_fft_s16_inverse_fft() to perform the + * FFT or inverse FFT on a buffer of samples. + * + * After use free the #GstFFTS16 instance with gst_fft_s16_free(). + * + * For the best performance use gst_fft_next_fast_length() to get a + * number that is entirely a product of 2, 3 and 5 and use this as the + * @len parameter for gst_fft_s16_new(). + * + * The @len parameter specifies the number of samples in the time domain that + * will be processed or generated. The number of samples in the frequency domain + * is @len/2 + 1. To get n samples in the frequency domain use 2*n - 2 as @len. + * + * Before performing the FFT on time domain data it usually makes sense + * to apply a window function to it. For this gst_fft_s16_window() can comfortably + * be used. + * + * For calculating the magnitude or phase of frequency data the functions + * gst_fft_s16_magnitude() and gst_fft_s16_phase() exist, if you want to calculate + * the magnitude yourself note that the magnitude of the frequency data is + * a value between 0 and 32767 and is not to be scaled by the length of the FFT. + * + */ + +/** + * gst_fft_s16_new: + * @len: Length of the FFT in the time domain + * @inverse: %TRUE if the #GstFFTS16 instance should be used for the inverse FFT + * + * This returns a new #GstFFTS16 instance with the given parameters. It makes + * sense to keep one instance for several calls for speed reasons. + * + * @len must be even and to get the best performance a product of + * 2, 3 and 5. To get the next number with this characteristics use + * gst_fft_next_fast_length(). + * + * Returns: a new #GstFFTS16 instance. + */ +GstFFTS16 * +gst_fft_s16_new (gint len, gboolean inverse) +{ + GstFFTS16 *self; + + g_return_val_if_fail (len > 0, NULL); + g_return_val_if_fail (len % 2 == 0, NULL); + + self = g_new (GstFFTS16, 1); + + self->cfg = kiss_fftr_s16_alloc (len, (inverse) ? 1 : 0, NULL, NULL); + g_assert (self->cfg); + + self->inverse = inverse; + self->len = len; + + return self; +} + +/** + * gst_fft_s16_fft: + * @self: #GstFFTS16 instance for this call + * @timedata: Buffer of the samples in the time domain + * @freqdata: Target buffer for the samples in the frequency domain + * + * This performs the FFT on @timedata and puts the result in @freqdata. + * + * @timedata must have as many samples as specified with the @len parameter while + * allocating the #GstFFTS16 instance with gst_fft_s16_new(). + * + * @freqdata must be large enough to hold @len/2 + 1 #GstFFTS16Complex frequency + * domain samples. + * + */ +void +gst_fft_s16_fft (GstFFTS16 * self, const gint16 * timedata, + GstFFTS16Complex * freqdata) +{ + g_return_if_fail (self); + g_return_if_fail (!self->inverse); + g_return_if_fail (timedata); + g_return_if_fail (freqdata); + + kiss_fftr_s16 (self->cfg, timedata, (kiss_fft_s16_cpx *) freqdata); +} + +/** + * gst_fft_s16_inverse_fft: + * @self: #GstFFTS16 instance for this call + * @freqdata: Buffer of the samples in the frequency domain + * @timedata: Target buffer for the samples in the time domain + * + * This performs the inverse FFT on @freqdata and puts the result in @timedata. + * + * @freqdata must have @len/2 + 1 samples, where @len is the parameter specified + * while allocating the #GstFFTS16 instance with gst_fft_s16_new(). + * + * @timedata must be large enough to hold @len time domain samples. + * + */ +void +gst_fft_s16_inverse_fft (GstFFTS16 * self, const GstFFTS16Complex * freqdata, + gint16 * timedata) +{ + g_return_if_fail (self); + g_return_if_fail (self->inverse); + g_return_if_fail (timedata); + g_return_if_fail (freqdata); + + kiss_fftri_s16 (self->cfg, (kiss_fft_s16_cpx *) freqdata, timedata); +} + +/** + * gst_fft_s16_free: + * @self: #GstFFTS16 instance for this call + * + * This frees the memory allocated for @self. + * + */ +void +gst_fft_s16_free (GstFFTS16 * self) +{ + kiss_fftr_s16_free (self->cfg); + g_free (self); +} + +/** + * gst_fft_s16_window: + * @self: #GstFFTS16 instance for this call + * @timedata: Time domain samples + * @window: Window function to apply + * + * This calls the window function @window on the @timedata sample buffer. + * + */ +void +gst_fft_s16_window (GstFFTS16 * self, gint16 * timedata, GstFFTWindow window) +{ + gint i, len; + + g_return_if_fail (self); + g_return_if_fail (timedata); + + len = self->len; + + switch (window) { + case GST_FFT_WINDOW_RECTANGULAR: + /* do nothing */ + break; + case GST_FFT_WINDOW_HAMMING: + for (i = 0; i < len; i++) + timedata[i] *= (0.53836 - 0.46164 * cos (2.0 * M_PI * i / len)); + break; + case GST_FFT_WINDOW_HANN: + for (i = 0; i < len; i++) + timedata[i] *= (0.5 - 0.5 * cos (2.0 * M_PI * i / len)); + break; + case GST_FFT_WINDOW_BARTLETT: + for (i = 0; i < len; i++) + timedata[i] *= (1.0 - fabs ((2.0 * i - len) / len)); + break; + case GST_FFT_WINDOW_BLACKMAN: + for (i = 0; i < len; i++) + timedata[i] *= (0.42 - 0.5 * cos ((2.0 * i) / len) + + 0.08 * cos ((4.0 * i) / len)); + break; + default: + g_assert_not_reached (); + break; + } +} + +/** + * gst_fft_s16_magnitude: + * @self: #GstFFTS16 instance for this call + * @freqdata: Frequency domain samples + * @magnitude: Target buffer for the magnitude + * @decibel: %TRUE if the magnitude should be in decibel, %FALSE if it should be an amplitude + * + * This calculates the magnitude of @freqdata in @magnitude. Depending on the value + * of @decibel the magnitude can be calculated in decibel or as amplitude between 0.0 + * and 1.0. + * + * @magnitude must be large enough to hold @len/2 + 1 values. + * + */ +void +gst_fft_s16_magnitude (GstFFTS16 * self, GstFFTS16Complex * freqdata, + gdouble * magnitude, gboolean decibel) +{ + gint i, len; + gdouble val; + + g_return_if_fail (self); + g_return_if_fail (freqdata); + g_return_if_fail (magnitude); + + len = self->len / 2 + 1; + + for (i = 0; i < len; i++) { + val = (gdouble) freqdata[i].r * (gdouble) freqdata[i].r + + (gdouble) freqdata[i].i * (gdouble) freqdata[i].i; + val = sqrt (val) / 32767.0; + + if (decibel) + val = 20.0 * log10 (val); + + magnitude[i] = val; + } +} + +/** + * gst_fft_s16_phase: + * @self: #GstFFTS16 instance for this call + * @freqdata: Frequency domain samples + * @phase: Target buffer for the phase + * + * This calculates the phases of @freqdata in @phase. The returned + * phases will be values between -pi and pi. + * + * @phase must be large enough to hold @len/2 + 1 values. + * + */ +void +gst_fft_s16_phase (GstFFTS16 * self, GstFFTS16Complex * freqdata, + gdouble * phase) +{ + gint i, len; + + g_return_if_fail (self); + g_return_if_fail (freqdata); + g_return_if_fail (phase); + + len = self->len / 2 + 1; + + for (i = 0; i < len; i++) + phase[i] = atan2 (freqdata[i].i, freqdata[i].r); +} diff --git a/gst-libs/gst/fft/gstffts16.h b/gst-libs/gst/fft/gstffts16.h new file mode 100644 index 000000000..4c8e35506 --- /dev/null +++ b/gst-libs/gst/fft/gstffts16.h @@ -0,0 +1,77 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_FFT_S16_H__ +#define __GST_FFT_S16_H__ + +#include <glib.h> +#include <gst/gst.h> + +#include "gstfft.h" + +typedef struct _GstFFTS16 GstFFTS16; +typedef struct _GstFFTS16Complex GstFFTS16Complex; + +/** + * GstFFTS16: + * + * Instance structure for #GstFFTS16. + * + */ +struct _GstFFTS16 { + /* <private> */ + void *cfg; + gboolean inverse; + gint len; + gpointer _padding[GST_PADDING]; +}; + +/* Copy of kiss_fft_s16_cpx for documentation reasons, + * do NOT change! */ + +/** + * GstFFTS16Complex: + * @r: Real part + * @i: Imaginary part + * + * Data type for complex numbers composed of + * signed 16 bit integers. + * + */ +struct _GstFFTS16Complex +{ + gint16 r; + gint16 i; +}; + +/* Functions */ + +GstFFTS16 * gst_fft_s16_new (gint len, gboolean inverse); +void gst_fft_s16_fft (GstFFTS16 *self, const gint16 *timedata, GstFFTS16Complex *freqdata); +void gst_fft_s16_inverse_fft (GstFFTS16 *self, const GstFFTS16Complex *freqdata, gint16 *timedata); +void gst_fft_s16_free (GstFFTS16 *self); + +void gst_fft_s16_window (GstFFTS16 *self, gint16 *timedata, GstFFTWindow window); + +void gst_fft_s16_magnitude (GstFFTS16 *self, GstFFTS16Complex *freqdata, + gdouble *magnitude, gboolean decibel); +void gst_fft_s16_phase (GstFFTS16 *self, GstFFTS16Complex *freqdata, + gdouble *phase); + +#endif /* __GST_FFT_S16_H__ */ diff --git a/gst-libs/gst/fft/gstffts32.c b/gst-libs/gst/fft/gstffts32.c new file mode 100644 index 000000000..2851ae397 --- /dev/null +++ b/gst-libs/gst/fft/gstffts32.c @@ -0,0 +1,269 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <glib.h> +#include <math.h> + +#include "kiss_fftr_s32.h" +#include "gstfft.h" +#include "gstffts32.h" + +/** + * SECTION:gstffts32 + * @short_description: FFT functions for signed 32 bit integer samples + * + * #GstFFTS32 provides a FFT implementation and related functions for + * signed 32 bit integer samples. To use this call gst_fft_s32_new() for + * allocating a #GstFFTS32 instance with the appropiate parameters and + * then call gst_fft_s32_fft() or gst_fft_s32_inverse_fft() to perform the + * FFT or inverse FFT on a buffer of samples. + * + * After use free the #GstFFTS32 instance with gst_fft_s32_free(). + * + * For the best performance use gst_fft_next_fast_length() to get a + * number that is entirely a product of 2, 3 and 5 and use this as the + * @len parameter for gst_fft_s32_new(). + * + * The @len parameter specifies the number of samples in the time domain that + * will be processed or generated. The number of samples in the frequency domain + * is @len/2 + 1. To get n samples in the frequency domain use 2*n - 2 as @len. + * + * Before performing the FFT on time domain data it usually makes sense + * to apply a window function to it. For this gst_fft_s32_window() can comfortably + * be used. + * + * For calculating the magnitude or phase of frequency data the functions + * gst_fft_s32_magnitude() and gst_fft_s32_phase() exist, if you want to calculate + * the magnitude yourself note that the magnitude of the frequency data is + * a value between 0 and 2147483647 and is not to be scaled by the length of the FFT. + * + */ + +/** + * gst_fft_s32_new: + * @len: Length of the FFT in the time domain + * @inverse: %TRUE if the #GstFFTS32 instance should be used for the inverse FFT + * + * This returns a new #GstFFTS32 instance with the given parameters. It makes + * sense to keep one instance for several calls for speed reasons. + * + * @len must be even and to get the best performance a product of + * 2, 3 and 5. To get the next number with this characteristics use + * gst_fft_next_fast_length(). + * + * Returns: a new #GstFFTS32 instance. + */ +GstFFTS32 * +gst_fft_s32_new (gint len, gboolean inverse) +{ + GstFFTS32 *self; + + g_return_val_if_fail (len > 0, NULL); + g_return_val_if_fail (len % 2 == 0, NULL); + + self = g_new (GstFFTS32, 1); + + self->cfg = kiss_fftr_s32_alloc (len, (inverse) ? 1 : 0, NULL, NULL); + g_assert (self->cfg); + + self->inverse = inverse; + self->len = len; + + return self; +} + +/** + * gst_fft_s32_fft: + * @self: #GstFFTS32 instance for this call + * @timedata: Buffer of the samples in the time domain + * @freqdata: Target buffer for the samples in the frequency domain + * + * This performs the FFT on @timedata and puts the result in @freqdata. + * + * @timedata must have as many samples as specified with the @len parameter while + * allocating the #GstFFTS32 instance with gst_fft_s32_new(). + * + * @freqdata must be large enough to hold @len/2 + 1 #GstFFTS32Complex frequency + * domain samples. + * + */ +void +gst_fft_s32_fft (GstFFTS32 * self, const gint32 * timedata, + GstFFTS32Complex * freqdata) +{ + g_return_if_fail (self); + g_return_if_fail (!self->inverse); + g_return_if_fail (timedata); + g_return_if_fail (freqdata); + + kiss_fftr_s32 (self->cfg, timedata, (kiss_fft_s32_cpx *) freqdata); +} + +/** + * gst_fft_s32_inverse_fft: + * @self: #GstFFTS32 instance for this call + * @freqdata: Buffer of the samples in the frequency domain + * @timedata: Target buffer for the samples in the time domain + * + * This performs the inverse FFT on @freqdata and puts the result in @timedata. + * + * @freqdata must have @len/2 + 1 samples, where @len is the parameter specified + * while allocating the #GstFFTS32 instance with gst_fft_s32_new(). + * + * @timedata must be large enough to hold @len time domain samples. + * + */ +void +gst_fft_s32_inverse_fft (GstFFTS32 * self, const GstFFTS32Complex * freqdata, + gint32 * timedata) +{ + g_return_if_fail (self); + g_return_if_fail (self->inverse); + g_return_if_fail (timedata); + g_return_if_fail (freqdata); + + kiss_fftri_s32 (self->cfg, (kiss_fft_s32_cpx *) freqdata, timedata); +} + +/** + * gst_fft_s32_free: + * @self: #GstFFTS32 instance for this call + * + * This frees the memory allocated for @self. + * + */ +void +gst_fft_s32_free (GstFFTS32 * self) +{ + kiss_fftr_s32_free (self->cfg); + g_free (self); +} + +/** + * gst_fft_s32_window: + * @self: #GstFFTS32 instance for this call + * @timedata: Time domain samples + * @window: Window function to apply + * + * This calls the window function @window on the @timedata sample buffer. + * + */ +void +gst_fft_s32_window (GstFFTS32 * self, gint32 * timedata, GstFFTWindow window) +{ + gint i, len; + + g_return_if_fail (self); + g_return_if_fail (timedata); + + len = self->len; + + switch (window) { + case GST_FFT_WINDOW_RECTANGULAR: + /* do nothing */ + break; + case GST_FFT_WINDOW_HAMMING: + for (i = 0; i < len; i++) + timedata[i] *= (0.53836 - 0.46164 * cos (2.0 * M_PI * i / len)); + break; + case GST_FFT_WINDOW_HANN: + for (i = 0; i < len; i++) + timedata[i] *= (0.5 - 0.5 * cos (2.0 * M_PI * i / len)); + break; + case GST_FFT_WINDOW_BARTLETT: + for (i = 0; i < len; i++) + timedata[i] *= (1.0 - fabs ((2.0 * i - len) / len)); + break; + case GST_FFT_WINDOW_BLACKMAN: + for (i = 0; i < len; i++) + timedata[i] *= (0.42 - 0.5 * cos ((2.0 * i) / len) + + 0.08 * cos ((4.0 * i) / len)); + break; + default: + g_assert_not_reached (); + break; + } +} + +/** + * gst_fft_s32_magnitude: + * @self: #GstFFTS32 instance for this call + * @freqdata: Frequency domain samples + * @magnitude: Target buffer for the magnitude + * @decibel: %TRUE if the magnitude should be in decibel, %FALSE if it should be an amplitude + * + * This calculates the magnitude of @freqdata in @magnitude. Depending on the value + * of @decibel the magnitude can be calculated in decibel or as amplitude between 0.0 + * and 1.0. + * + * @magnitude must be large enough to hold @len/2 + 1 values. + * + */ +void +gst_fft_s32_magnitude (GstFFTS32 * self, GstFFTS32Complex * freqdata, + gdouble * magnitude, gboolean decibel) +{ + gint i, len; + gdouble val; + + g_return_if_fail (self); + g_return_if_fail (freqdata); + g_return_if_fail (magnitude); + + len = self->len / 2 + 1; + + for (i = 0; i < len; i++) { + val = (gdouble) freqdata[i].r * (gdouble) freqdata[i].r + + (gdouble) freqdata[i].i * (gdouble) freqdata[i].i; + val = sqrt (val) / 2147483647.0; + + if (decibel) + val = 20.0 * log10 (val); + + magnitude[i] = val; + } +} + +/** + * gst_fft_s32_phase: + * @self: #GstFFTS32 instance for this call + * @freqdata: Frequency domain samples + * @phase: Target buffer for the phase + * + * This calculates the phases of @freqdata in @phase. The returned + * phases will be values between -pi and pi. + * + * @phase must be large enough to hold @len/2 + 1 values. + * + */ +void +gst_fft_s32_phase (GstFFTS32 * self, GstFFTS32Complex * freqdata, + gdouble * phase) +{ + gint i, len; + + g_return_if_fail (self); + g_return_if_fail (freqdata); + g_return_if_fail (phase); + + len = self->len / 2 + 1; + + for (i = 0; i < len; i++) + phase[i] = atan2 (freqdata[i].i, freqdata[i].r); +} diff --git a/gst-libs/gst/fft/gstffts32.h b/gst-libs/gst/fft/gstffts32.h new file mode 100644 index 000000000..a495fd9a2 --- /dev/null +++ b/gst-libs/gst/fft/gstffts32.h @@ -0,0 +1,77 @@ +/* GStreamer + * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_FFT_S32_H__ +#define __GST_FFT_S32_H__ + +#include <glib.h> +#include <gst/gst.h> + +#include "gstfft.h" + +typedef struct _GstFFTS32 GstFFTS32; +typedef struct _GstFFTS32Complex GstFFTS32Complex; + +/** + * GstFFTS32: + * + * Instance structure for #GstFFTS32. + * + */ +struct _GstFFTS32 { + /* <private> */ + void * cfg; + gboolean inverse; + gint len; + gpointer _padding[GST_PADDING]; +}; + +/* Copy of kiss_fft_s32_cpx for documentation reasons, + * do NOT change! */ + +/** + * GstFFTS32Complex: + * @r: Real part + * @i: Imaginary part + * + * Data type for complex numbers composed of + * signed 32 bit integers. + * + */ +struct _GstFFTS32Complex +{ + gint32 r; + gint32 i; +}; + +/* Functions */ + +GstFFTS32 * gst_fft_s32_new (gint len, gboolean inverse); +void gst_fft_s32_fft (GstFFTS32 *self, const gint32 *timedata, GstFFTS32Complex *freqdata); +void gst_fft_s32_inverse_fft (GstFFTS32 *self, const GstFFTS32Complex *freqdata, gint32 *timedata); +void gst_fft_s32_free (GstFFTS32 *self); + +void gst_fft_s32_window (GstFFTS32 *self, gint32 *timedata, GstFFTWindow window); + +void gst_fft_s32_magnitude (GstFFTS32 *self, GstFFTS32Complex *freqdata, + gdouble *magnitude, gboolean decibel); +void gst_fft_s32_phase (GstFFTS32 *self, GstFFTS32Complex *freqdata, + gdouble *phase); + +#endif /* __GST_FFT_S32_H__ */ diff --git a/gst-libs/gst/fft/kiss_fft_f32.c b/gst-libs/gst/fft/kiss_fft_f32.c new file mode 100644 index 000000000..f53d1d782 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fft_f32.c @@ -0,0 +1,433 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "_kiss_fft_guts_f32.h" +/* The guts header contains all the multiplication and addition macros that are defined for + fixed or floating point complex numbers. It also delares the kf_ internal functions. + */ + +static kiss_fft_f32_cpx *scratchbuf = NULL; +static size_t nscratchbuf = 0; +static kiss_fft_f32_cpx *tmpbuf = NULL; +static size_t ntmpbuf = 0; + +#define CHECKBUF(buf,nbuf,n) \ + do { \ + if ( nbuf < (size_t)(n) ) {\ + free(buf); \ + buf = (kiss_fft_f32_cpx*)KISS_FFT_F32_MALLOC(sizeof(kiss_fft_f32_cpx)*(n)); \ + nbuf = (size_t)(n); \ + } \ + }while(0) + + +static void +kf_bfly2 (kiss_fft_f32_cpx * Fout, + const size_t fstride, const kiss_fft_f32_cfg st, int m) +{ + kiss_fft_f32_cpx *Fout2; + kiss_fft_f32_cpx *tw1 = st->twiddles; + kiss_fft_f32_cpx t; + + Fout2 = Fout + m; + do { + C_FIXDIV (*Fout, 2); + C_FIXDIV (*Fout2, 2); + + C_MUL (t, *Fout2, *tw1); + tw1 += fstride; + C_SUB (*Fout2, *Fout, t); + C_ADDTO (*Fout, t); + ++Fout2; + ++Fout; + } while (--m); +} + +static void +kf_bfly4 (kiss_fft_f32_cpx * Fout, + const size_t fstride, const kiss_fft_f32_cfg st, const size_t m) +{ + kiss_fft_f32_cpx *tw1, *tw2, *tw3; + kiss_fft_f32_cpx scratch[6]; + size_t k = m; + const size_t m2 = 2 * m; + const size_t m3 = 3 * m; + + tw3 = tw2 = tw1 = st->twiddles; + + do { + C_FIXDIV (*Fout, 4); + C_FIXDIV (Fout[m], 4); + C_FIXDIV (Fout[m2], 4); + C_FIXDIV (Fout[m3], 4); + + C_MUL (scratch[0], Fout[m], *tw1); + C_MUL (scratch[1], Fout[m2], *tw2); + C_MUL (scratch[2], Fout[m3], *tw3); + + C_SUB (scratch[5], *Fout, scratch[1]); + C_ADDTO (*Fout, scratch[1]); + C_ADD (scratch[3], scratch[0], scratch[2]); + C_SUB (scratch[4], scratch[0], scratch[2]); + C_SUB (Fout[m2], *Fout, scratch[3]); + tw1 += fstride; + tw2 += fstride * 2; + tw3 += fstride * 3; + C_ADDTO (*Fout, scratch[3]); + + if (st->inverse) { + Fout[m].r = scratch[5].r - scratch[4].i; + Fout[m].i = scratch[5].i + scratch[4].r; + Fout[m3].r = scratch[5].r + scratch[4].i; + Fout[m3].i = scratch[5].i - scratch[4].r; + } else { + Fout[m].r = scratch[5].r + scratch[4].i; + Fout[m].i = scratch[5].i - scratch[4].r; + Fout[m3].r = scratch[5].r - scratch[4].i; + Fout[m3].i = scratch[5].i + scratch[4].r; + } + ++Fout; + } while (--k); +} + +static void +kf_bfly3 (kiss_fft_f32_cpx * Fout, + const size_t fstride, const kiss_fft_f32_cfg st, size_t m) +{ + size_t k = m; + const size_t m2 = 2 * m; + kiss_fft_f32_cpx *tw1, *tw2; + kiss_fft_f32_cpx scratch[5]; + kiss_fft_f32_cpx epi3; + + epi3 = st->twiddles[fstride * m]; + + tw1 = tw2 = st->twiddles; + + do { + C_FIXDIV (*Fout, 3); + C_FIXDIV (Fout[m], 3); + C_FIXDIV (Fout[m2], 3); + + C_MUL (scratch[1], Fout[m], *tw1); + C_MUL (scratch[2], Fout[m2], *tw2); + + C_ADD (scratch[3], scratch[1], scratch[2]); + C_SUB (scratch[0], scratch[1], scratch[2]); + tw1 += fstride; + tw2 += fstride * 2; + + Fout[m].r = Fout->r - HALF_OF (scratch[3].r); + Fout[m].i = Fout->i - HALF_OF (scratch[3].i); + + C_MULBYSCALAR (scratch[0], epi3.i); + + C_ADDTO (*Fout, scratch[3]); + + Fout[m2].r = Fout[m].r + scratch[0].i; + Fout[m2].i = Fout[m].i - scratch[0].r; + + Fout[m].r -= scratch[0].i; + Fout[m].i += scratch[0].r; + + ++Fout; + } while (--k); +} + +static void +kf_bfly5 (kiss_fft_f32_cpx * Fout, + const size_t fstride, const kiss_fft_f32_cfg st, int m) +{ + kiss_fft_f32_cpx *Fout0, *Fout1, *Fout2, *Fout3, *Fout4; + int u; + kiss_fft_f32_cpx scratch[13]; + kiss_fft_f32_cpx *twiddles = st->twiddles; + kiss_fft_f32_cpx *tw; + kiss_fft_f32_cpx ya, yb; + + ya = twiddles[fstride * m]; + yb = twiddles[fstride * 2 * m]; + + Fout0 = Fout; + Fout1 = Fout0 + m; + Fout2 = Fout0 + 2 * m; + Fout3 = Fout0 + 3 * m; + Fout4 = Fout0 + 4 * m; + + tw = st->twiddles; + for (u = 0; u < m; ++u) { + C_FIXDIV (*Fout0, 5); + C_FIXDIV (*Fout1, 5); + C_FIXDIV (*Fout2, 5); + C_FIXDIV (*Fout3, 5); + C_FIXDIV (*Fout4, 5); + scratch[0] = *Fout0; + + C_MUL (scratch[1], *Fout1, tw[u * fstride]); + C_MUL (scratch[2], *Fout2, tw[2 * u * fstride]); + C_MUL (scratch[3], *Fout3, tw[3 * u * fstride]); + C_MUL (scratch[4], *Fout4, tw[4 * u * fstride]); + + C_ADD (scratch[7], scratch[1], scratch[4]); + C_SUB (scratch[10], scratch[1], scratch[4]); + C_ADD (scratch[8], scratch[2], scratch[3]); + C_SUB (scratch[9], scratch[2], scratch[3]); + + Fout0->r += scratch[7].r + scratch[8].r; + Fout0->i += scratch[7].i + scratch[8].i; + + scratch[5].r = + scratch[0].r + S_MUL (scratch[7].r, ya.r) + S_MUL (scratch[8].r, yb.r); + scratch[5].i = + scratch[0].i + S_MUL (scratch[7].i, ya.r) + S_MUL (scratch[8].i, yb.r); + + scratch[6].r = S_MUL (scratch[10].i, ya.i) + S_MUL (scratch[9].i, yb.i); + scratch[6].i = -S_MUL (scratch[10].r, ya.i) - S_MUL (scratch[9].r, yb.i); + + C_SUB (*Fout1, scratch[5], scratch[6]); + C_ADD (*Fout4, scratch[5], scratch[6]); + + scratch[11].r = + scratch[0].r + S_MUL (scratch[7].r, yb.r) + S_MUL (scratch[8].r, ya.r); + scratch[11].i = + scratch[0].i + S_MUL (scratch[7].i, yb.r) + S_MUL (scratch[8].i, ya.r); + scratch[12].r = -S_MUL (scratch[10].i, yb.i) + S_MUL (scratch[9].i, ya.i); + scratch[12].i = S_MUL (scratch[10].r, yb.i) - S_MUL (scratch[9].r, ya.i); + + C_ADD (*Fout2, scratch[11], scratch[12]); + C_SUB (*Fout3, scratch[11], scratch[12]); + + ++Fout0; + ++Fout1; + ++Fout2; + ++Fout3; + ++Fout4; + } +} + +/* perform the butterfly for one stage of a mixed radix FFT */ +static void +kf_bfly_generic (kiss_fft_f32_cpx * Fout, + const size_t fstride, const kiss_fft_f32_cfg st, int m, int p) +{ + int u, k, q1, q; + kiss_fft_f32_cpx *twiddles = st->twiddles; + kiss_fft_f32_cpx t; + int Norig = st->nfft; + + CHECKBUF (scratchbuf, nscratchbuf, p); + + for (u = 0; u < m; ++u) { + k = u; + for (q1 = 0; q1 < p; ++q1) { + scratchbuf[q1] = Fout[k]; + C_FIXDIV (scratchbuf[q1], p); + k += m; + } + + k = u; + for (q1 = 0; q1 < p; ++q1) { + int twidx = 0; + + Fout[k] = scratchbuf[0]; + for (q = 1; q < p; ++q) { + twidx += fstride * k; + if (twidx >= Norig) + twidx -= Norig; + C_MUL (t, scratchbuf[q], twiddles[twidx]); + C_ADDTO (Fout[k], t); + } + k += m; + } + } +} + +static void +kf_work (kiss_fft_f32_cpx * Fout, + const kiss_fft_f32_cpx * f, + const size_t fstride, + int in_stride, int *factors, const kiss_fft_f32_cfg st) +{ + kiss_fft_f32_cpx *Fout_beg = Fout; + const int p = *factors++; /* the radix */ + const int m = *factors++; /* stage's fft length/p */ + const kiss_fft_f32_cpx *Fout_end = Fout + p * m; + + if (m == 1) { + do { + *Fout = *f; + f += fstride * in_stride; + } while (++Fout != Fout_end); + } else { + do { + kf_work (Fout, f, fstride * p, in_stride, factors, st); + f += fstride * in_stride; + } while ((Fout += m) != Fout_end); + } + + Fout = Fout_beg; + + switch (p) { + case 2: + kf_bfly2 (Fout, fstride, st, m); + break; + case 3: + kf_bfly3 (Fout, fstride, st, m); + break; + case 4: + kf_bfly4 (Fout, fstride, st, m); + break; + case 5: + kf_bfly5 (Fout, fstride, st, m); + break; + default: + kf_bfly_generic (Fout, fstride, st, m, p); + break; + } +} + +/* facbuf is populated by p1,m1,p2,m2, ... + where + p[i] * m[i] = m[i-1] + m0 = n */ +static void +kf_factor (int n, int *facbuf) +{ + int p = 4; + double floor_sqrt; + + floor_sqrt = floor (sqrt ((double) n)); + + /*factor out powers of 4, powers of 2, then any remaining primes */ + do { + while (n % p) { + switch (p) { + case 4: + p = 2; + break; + case 2: + p = 3; + break; + default: + p += 2; + break; + } + if (p > floor_sqrt) + p = n; /* no more factors, skip to end */ + } + n /= p; + *facbuf++ = p; + *facbuf++ = n; + } while (n > 1); +} + +/* + * + * User-callable function to allocate all necessary storage space for the fft. + * + * The return value is a contiguous block of memory, allocated with malloc. As such, + * It can be freed with free(), rather than a kiss_fft-specific function. + * */ +kiss_fft_f32_cfg +kiss_fft_f32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) +{ + kiss_fft_f32_cfg st = NULL; + size_t memneeded = sizeof (struct kiss_fft_f32_state) + + sizeof (kiss_fft_f32_cpx) * (nfft - 1); /* twiddle factors */ + + if (lenmem == NULL) { + st = (kiss_fft_f32_cfg) KISS_FFT_F32_MALLOC (memneeded); + } else { + if (mem != NULL && *lenmem >= memneeded) + st = (kiss_fft_f32_cfg) mem; + *lenmem = memneeded; + } + if (st) { + int i; + + st->nfft = nfft; + st->inverse = inverse_fft; + + for (i = 0; i < nfft; ++i) { + const double pi = + 3.141592653589793238462643383279502884197169399375105820974944; + double phase = -2 * pi * i / nfft; + + if (st->inverse) + phase *= -1; + kf_cexp (st->twiddles + i, phase); + } + + kf_factor (nfft, st->factors); + } + return st; +} + + + + +void +kiss_fft_f32_stride (kiss_fft_f32_cfg st, const kiss_fft_f32_cpx * fin, + kiss_fft_f32_cpx * fout, int in_stride) +{ + if (fin == fout) { + CHECKBUF (tmpbuf, ntmpbuf, st->nfft); + kf_work (tmpbuf, fin, 1, in_stride, st->factors, st); + memcpy (fout, tmpbuf, sizeof (kiss_fft_f32_cpx) * st->nfft); + } else { + kf_work (fout, fin, 1, in_stride, st->factors, st); + } +} + +void +kiss_fft_f32 (kiss_fft_f32_cfg cfg, const kiss_fft_f32_cpx * fin, + kiss_fft_f32_cpx * fout) +{ + kiss_fft_f32_stride (cfg, fin, fout, 1); +} + + +/* not really necessary to call, but if someone is doing in-place ffts, they may want to free the + buffers from CHECKBUF + */ +void +kiss_fft_f32_cleanup (void) +{ + free (scratchbuf); + scratchbuf = NULL; + nscratchbuf = 0; + free (tmpbuf); + tmpbuf = NULL; + ntmpbuf = 0; +} + +int +kiss_fft_f32_next_fast_size (int n) +{ + while (1) { + int m = n; + + while ((m % 2) == 0) + m /= 2; + while ((m % 3) == 0) + m /= 3; + while ((m % 5) == 0) + m /= 5; + if (m <= 1) + break; /* n is completely factorable by twos, threes, and fives */ + n++; + } + return n; +} diff --git a/gst-libs/gst/fft/kiss_fft_f32.h b/gst-libs/gst/fft/kiss_fft_f32.h new file mode 100644 index 000000000..cc3cdee02 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fft_f32.h @@ -0,0 +1,99 @@ +#ifndef KISS_FFT_F32_H +#define KISS_FFT_F32_H + +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <memory.h> +#include <malloc.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* + ATTENTION! + If you would like a : + -- a utility that will handle the caching of fft objects + -- real-only (no imaginary time component ) FFT + -- a multi-dimensional FFT + -- a command-line utility to perform ffts + -- a command-line utility to perform fast-convolution filtering + + Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c + in the tools/ directory. +*/ + +#define KISS_FFT_F32_MALLOC malloc +#define kiss_fft_f32_scalar float + +typedef struct { + kiss_fft_f32_scalar r; + kiss_fft_f32_scalar i; +}kiss_fft_f32_cpx; + +typedef struct kiss_fft_f32_state* kiss_fft_f32_cfg; + +/* + * kiss_fft_f32_alloc + * + * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. + * + * typical usage: kiss_fft_f32_cfg mycfg=kiss_fft_f32_alloc(1024,0,NULL,NULL); + * + * The return value from fft_alloc is a cfg buffer used internally + * by the fft routine or NULL. + * + * If lenmem is NULL, then kiss_fft_f32_alloc will allocate a cfg buffer using malloc. + * The returned value should be free()d when done to avoid memory leaks. + * + * The state can be placed in a user supplied buffer 'mem': + * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, + * then the function places the cfg in mem and the size used in *lenmem + * and returns mem. + * + * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), + * then the function returns NULL and places the minimum cfg + * buffer size in *lenmem. + * */ + +kiss_fft_f32_cfg kiss_fft_f32_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); + +/* + * kiss_fft(cfg,in_out_buf) + * + * Perform an FFT on a complex input buffer. + * for a forward FFT, + * fin should be f[0] , f[1] , ... ,f[nfft-1] + * fout will be F[0] , F[1] , ... ,F[nfft-1] + * Note that each element is complex and can be accessed like + f[k].r and f[k].i + * */ +void kiss_fft_f32(kiss_fft_f32_cfg cfg,const kiss_fft_f32_cpx *fin,kiss_fft_f32_cpx *fout); + +/* + A more generic version of the above function. It reads its input from every Nth sample. + * */ +void kiss_fft_f32_stride(kiss_fft_f32_cfg cfg,const kiss_fft_f32_cpx *fin,kiss_fft_f32_cpx *fout,int fin_stride); + +/* If kiss_fft_f32_alloc allocated a buffer, it is one contiguous + buffer and can be simply free()d when no longer needed*/ +#define kiss_fft_f32_free free + +/* + Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up + your compiler output to call this before you exit. +*/ +void kiss_fft_f32_cleanup(void); + + +/* + * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) + */ +int kiss_fft_f32_next_fast_size(int n); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/gst-libs/gst/fft/kiss_fft_f64.c b/gst-libs/gst/fft/kiss_fft_f64.c new file mode 100644 index 000000000..c75a4a0b4 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fft_f64.c @@ -0,0 +1,433 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "_kiss_fft_guts_f64.h" +/* The guts header contains all the multiplication and addition macros that are defined for + fixed or floating point complex numbers. It also delares the kf_ internal functions. + */ + +static kiss_fft_f64_cpx *scratchbuf = NULL; +static size_t nscratchbuf = 0; +static kiss_fft_f64_cpx *tmpbuf = NULL; +static size_t ntmpbuf = 0; + +#define CHECKBUF(buf,nbuf,n) \ + do { \ + if ( nbuf < (size_t)(n) ) {\ + free(buf); \ + buf = (kiss_fft_f64_cpx*)KISS_FFT_F64_MALLOC(sizeof(kiss_fft_f64_cpx)*(n)); \ + nbuf = (size_t)(n); \ + } \ + }while(0) + + +static void +kf_bfly2 (kiss_fft_f64_cpx * Fout, + const size_t fstride, const kiss_fft_f64_cfg st, int m) +{ + kiss_fft_f64_cpx *Fout2; + kiss_fft_f64_cpx *tw1 = st->twiddles; + kiss_fft_f64_cpx t; + + Fout2 = Fout + m; + do { + C_FIXDIV (*Fout, 2); + C_FIXDIV (*Fout2, 2); + + C_MUL (t, *Fout2, *tw1); + tw1 += fstride; + C_SUB (*Fout2, *Fout, t); + C_ADDTO (*Fout, t); + ++Fout2; + ++Fout; + } while (--m); +} + +static void +kf_bfly4 (kiss_fft_f64_cpx * Fout, + const size_t fstride, const kiss_fft_f64_cfg st, const size_t m) +{ + kiss_fft_f64_cpx *tw1, *tw2, *tw3; + kiss_fft_f64_cpx scratch[6]; + size_t k = m; + const size_t m2 = 2 * m; + const size_t m3 = 3 * m; + + tw3 = tw2 = tw1 = st->twiddles; + + do { + C_FIXDIV (*Fout, 4); + C_FIXDIV (Fout[m], 4); + C_FIXDIV (Fout[m2], 4); + C_FIXDIV (Fout[m3], 4); + + C_MUL (scratch[0], Fout[m], *tw1); + C_MUL (scratch[1], Fout[m2], *tw2); + C_MUL (scratch[2], Fout[m3], *tw3); + + C_SUB (scratch[5], *Fout, scratch[1]); + C_ADDTO (*Fout, scratch[1]); + C_ADD (scratch[3], scratch[0], scratch[2]); + C_SUB (scratch[4], scratch[0], scratch[2]); + C_SUB (Fout[m2], *Fout, scratch[3]); + tw1 += fstride; + tw2 += fstride * 2; + tw3 += fstride * 3; + C_ADDTO (*Fout, scratch[3]); + + if (st->inverse) { + Fout[m].r = scratch[5].r - scratch[4].i; + Fout[m].i = scratch[5].i + scratch[4].r; + Fout[m3].r = scratch[5].r + scratch[4].i; + Fout[m3].i = scratch[5].i - scratch[4].r; + } else { + Fout[m].r = scratch[5].r + scratch[4].i; + Fout[m].i = scratch[5].i - scratch[4].r; + Fout[m3].r = scratch[5].r - scratch[4].i; + Fout[m3].i = scratch[5].i + scratch[4].r; + } + ++Fout; + } while (--k); +} + +static void +kf_bfly3 (kiss_fft_f64_cpx * Fout, + const size_t fstride, const kiss_fft_f64_cfg st, size_t m) +{ + size_t k = m; + const size_t m2 = 2 * m; + kiss_fft_f64_cpx *tw1, *tw2; + kiss_fft_f64_cpx scratch[5]; + kiss_fft_f64_cpx epi3; + + epi3 = st->twiddles[fstride * m]; + + tw1 = tw2 = st->twiddles; + + do { + C_FIXDIV (*Fout, 3); + C_FIXDIV (Fout[m], 3); + C_FIXDIV (Fout[m2], 3); + + C_MUL (scratch[1], Fout[m], *tw1); + C_MUL (scratch[2], Fout[m2], *tw2); + + C_ADD (scratch[3], scratch[1], scratch[2]); + C_SUB (scratch[0], scratch[1], scratch[2]); + tw1 += fstride; + tw2 += fstride * 2; + + Fout[m].r = Fout->r - HALF_OF (scratch[3].r); + Fout[m].i = Fout->i - HALF_OF (scratch[3].i); + + C_MULBYSCALAR (scratch[0], epi3.i); + + C_ADDTO (*Fout, scratch[3]); + + Fout[m2].r = Fout[m].r + scratch[0].i; + Fout[m2].i = Fout[m].i - scratch[0].r; + + Fout[m].r -= scratch[0].i; + Fout[m].i += scratch[0].r; + + ++Fout; + } while (--k); +} + +static void +kf_bfly5 (kiss_fft_f64_cpx * Fout, + const size_t fstride, const kiss_fft_f64_cfg st, int m) +{ + kiss_fft_f64_cpx *Fout0, *Fout1, *Fout2, *Fout3, *Fout4; + int u; + kiss_fft_f64_cpx scratch[13]; + kiss_fft_f64_cpx *twiddles = st->twiddles; + kiss_fft_f64_cpx *tw; + kiss_fft_f64_cpx ya, yb; + + ya = twiddles[fstride * m]; + yb = twiddles[fstride * 2 * m]; + + Fout0 = Fout; + Fout1 = Fout0 + m; + Fout2 = Fout0 + 2 * m; + Fout3 = Fout0 + 3 * m; + Fout4 = Fout0 + 4 * m; + + tw = st->twiddles; + for (u = 0; u < m; ++u) { + C_FIXDIV (*Fout0, 5); + C_FIXDIV (*Fout1, 5); + C_FIXDIV (*Fout2, 5); + C_FIXDIV (*Fout3, 5); + C_FIXDIV (*Fout4, 5); + scratch[0] = *Fout0; + + C_MUL (scratch[1], *Fout1, tw[u * fstride]); + C_MUL (scratch[2], *Fout2, tw[2 * u * fstride]); + C_MUL (scratch[3], *Fout3, tw[3 * u * fstride]); + C_MUL (scratch[4], *Fout4, tw[4 * u * fstride]); + + C_ADD (scratch[7], scratch[1], scratch[4]); + C_SUB (scratch[10], scratch[1], scratch[4]); + C_ADD (scratch[8], scratch[2], scratch[3]); + C_SUB (scratch[9], scratch[2], scratch[3]); + + Fout0->r += scratch[7].r + scratch[8].r; + Fout0->i += scratch[7].i + scratch[8].i; + + scratch[5].r = + scratch[0].r + S_MUL (scratch[7].r, ya.r) + S_MUL (scratch[8].r, yb.r); + scratch[5].i = + scratch[0].i + S_MUL (scratch[7].i, ya.r) + S_MUL (scratch[8].i, yb.r); + + scratch[6].r = S_MUL (scratch[10].i, ya.i) + S_MUL (scratch[9].i, yb.i); + scratch[6].i = -S_MUL (scratch[10].r, ya.i) - S_MUL (scratch[9].r, yb.i); + + C_SUB (*Fout1, scratch[5], scratch[6]); + C_ADD (*Fout4, scratch[5], scratch[6]); + + scratch[11].r = + scratch[0].r + S_MUL (scratch[7].r, yb.r) + S_MUL (scratch[8].r, ya.r); + scratch[11].i = + scratch[0].i + S_MUL (scratch[7].i, yb.r) + S_MUL (scratch[8].i, ya.r); + scratch[12].r = -S_MUL (scratch[10].i, yb.i) + S_MUL (scratch[9].i, ya.i); + scratch[12].i = S_MUL (scratch[10].r, yb.i) - S_MUL (scratch[9].r, ya.i); + + C_ADD (*Fout2, scratch[11], scratch[12]); + C_SUB (*Fout3, scratch[11], scratch[12]); + + ++Fout0; + ++Fout1; + ++Fout2; + ++Fout3; + ++Fout4; + } +} + +/* perform the butterfly for one stage of a mixed radix FFT */ +static void +kf_bfly_generic (kiss_fft_f64_cpx * Fout, + const size_t fstride, const kiss_fft_f64_cfg st, int m, int p) +{ + int u, k, q1, q; + kiss_fft_f64_cpx *twiddles = st->twiddles; + kiss_fft_f64_cpx t; + int Norig = st->nfft; + + CHECKBUF (scratchbuf, nscratchbuf, p); + + for (u = 0; u < m; ++u) { + k = u; + for (q1 = 0; q1 < p; ++q1) { + scratchbuf[q1] = Fout[k]; + C_FIXDIV (scratchbuf[q1], p); + k += m; + } + + k = u; + for (q1 = 0; q1 < p; ++q1) { + int twidx = 0; + + Fout[k] = scratchbuf[0]; + for (q = 1; q < p; ++q) { + twidx += fstride * k; + if (twidx >= Norig) + twidx -= Norig; + C_MUL (t, scratchbuf[q], twiddles[twidx]); + C_ADDTO (Fout[k], t); + } + k += m; + } + } +} + +static void +kf_work (kiss_fft_f64_cpx * Fout, + const kiss_fft_f64_cpx * f, + const size_t fstride, + int in_stride, int *factors, const kiss_fft_f64_cfg st) +{ + kiss_fft_f64_cpx *Fout_beg = Fout; + const int p = *factors++; /* the radix */ + const int m = *factors++; /* stage's fft length/p */ + const kiss_fft_f64_cpx *Fout_end = Fout + p * m; + + if (m == 1) { + do { + *Fout = *f; + f += fstride * in_stride; + } while (++Fout != Fout_end); + } else { + do { + kf_work (Fout, f, fstride * p, in_stride, factors, st); + f += fstride * in_stride; + } while ((Fout += m) != Fout_end); + } + + Fout = Fout_beg; + + switch (p) { + case 2: + kf_bfly2 (Fout, fstride, st, m); + break; + case 3: + kf_bfly3 (Fout, fstride, st, m); + break; + case 4: + kf_bfly4 (Fout, fstride, st, m); + break; + case 5: + kf_bfly5 (Fout, fstride, st, m); + break; + default: + kf_bfly_generic (Fout, fstride, st, m, p); + break; + } +} + +/* facbuf is populated by p1,m1,p2,m2, ... + where + p[i] * m[i] = m[i-1] + m0 = n */ +static void +kf_factor (int n, int *facbuf) +{ + int p = 4; + double floor_sqrt; + + floor_sqrt = floor (sqrt ((double) n)); + + /*factor out powers of 4, powers of 2, then any remaining primes */ + do { + while (n % p) { + switch (p) { + case 4: + p = 2; + break; + case 2: + p = 3; + break; + default: + p += 2; + break; + } + if (p > floor_sqrt) + p = n; /* no more factors, skip to end */ + } + n /= p; + *facbuf++ = p; + *facbuf++ = n; + } while (n > 1); +} + +/* + * + * User-callable function to allocate all necessary storage space for the fft. + * + * The return value is a contiguous block of memory, allocated with malloc. As such, + * It can be freed with free(), rather than a kiss_fft-specific function. + * */ +kiss_fft_f64_cfg +kiss_fft_f64_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) +{ + kiss_fft_f64_cfg st = NULL; + size_t memneeded = sizeof (struct kiss_fft_f64_state) + + sizeof (kiss_fft_f64_cpx) * (nfft - 1); /* twiddle factors */ + + if (lenmem == NULL) { + st = (kiss_fft_f64_cfg) KISS_FFT_F64_MALLOC (memneeded); + } else { + if (mem != NULL && *lenmem >= memneeded) + st = (kiss_fft_f64_cfg) mem; + *lenmem = memneeded; + } + if (st) { + int i; + + st->nfft = nfft; + st->inverse = inverse_fft; + + for (i = 0; i < nfft; ++i) { + const double pi = + 3.141592653589793238462643383279502884197169399375105820974944; + double phase = -2 * pi * i / nfft; + + if (st->inverse) + phase *= -1; + kf_cexp (st->twiddles + i, phase); + } + + kf_factor (nfft, st->factors); + } + return st; +} + + + + +void +kiss_fft_f64_stride (kiss_fft_f64_cfg st, const kiss_fft_f64_cpx * fin, + kiss_fft_f64_cpx * fout, int in_stride) +{ + if (fin == fout) { + CHECKBUF (tmpbuf, ntmpbuf, st->nfft); + kf_work (tmpbuf, fin, 1, in_stride, st->factors, st); + memcpy (fout, tmpbuf, sizeof (kiss_fft_f64_cpx) * st->nfft); + } else { + kf_work (fout, fin, 1, in_stride, st->factors, st); + } +} + +void +kiss_fft_f64 (kiss_fft_f64_cfg cfg, const kiss_fft_f64_cpx * fin, + kiss_fft_f64_cpx * fout) +{ + kiss_fft_f64_stride (cfg, fin, fout, 1); +} + + +/* not really necessary to call, but if someone is doing in-place ffts, they may want to free the + buffers from CHECKBUF + */ +void +kiss_fft_f64_cleanup (void) +{ + free (scratchbuf); + scratchbuf = NULL; + nscratchbuf = 0; + free (tmpbuf); + tmpbuf = NULL; + ntmpbuf = 0; +} + +int +kiss_fft_f64_next_fast_size (int n) +{ + while (1) { + int m = n; + + while ((m % 2) == 0) + m /= 2; + while ((m % 3) == 0) + m /= 3; + while ((m % 5) == 0) + m /= 5; + if (m <= 1) + break; /* n is completely factorable by twos, threes, and fives */ + n++; + } + return n; +} diff --git a/gst-libs/gst/fft/kiss_fft_f64.h b/gst-libs/gst/fft/kiss_fft_f64.h new file mode 100644 index 000000000..b3fb15a9b --- /dev/null +++ b/gst-libs/gst/fft/kiss_fft_f64.h @@ -0,0 +1,99 @@ +#ifndef KISS_FFT_F64_H +#define KISS_FFT_F64_H + +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <memory.h> +#include <malloc.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* + ATTENTION! + If you would like a : + -- a utility that will handle the caching of fft objects + -- real-only (no imaginary time component ) FFT + -- a multi-dimensional FFT + -- a command-line utility to perform ffts + -- a command-line utility to perform fast-convolution filtering + + Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c + in the tools/ directory. +*/ + +#define KISS_FFT_F64_MALLOC malloc +#define kiss_fft_f64_scalar double + +typedef struct { + kiss_fft_f64_scalar r; + kiss_fft_f64_scalar i; +}kiss_fft_f64_cpx; + +typedef struct kiss_fft_f64_state* kiss_fft_f64_cfg; + +/* + * kiss_fft_f64_alloc + * + * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. + * + * typical usage: kiss_fft_f64_cfg mycfg=kiss_fft_f64_alloc(1024,0,NULL,NULL); + * + * The return value from fft_alloc is a cfg buffer used internally + * by the fft routine or NULL. + * + * If lenmem is NULL, then kiss_fft_f64_alloc will allocate a cfg buffer using malloc. + * The returned value should be free()d when done to avoid memory leaks. + * + * The state can be placed in a user supplied buffer 'mem': + * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, + * then the function places the cfg in mem and the size used in *lenmem + * and returns mem. + * + * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), + * then the function returns NULL and places the minimum cfg + * buffer size in *lenmem. + * */ + +kiss_fft_f64_cfg kiss_fft_f64_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); + +/* + * kiss_fft(cfg,in_out_buf) + * + * Perform an FFT on a complex input buffer. + * for a forward FFT, + * fin should be f[0] , f[1] , ... ,f[nfft-1] + * fout will be F[0] , F[1] , ... ,F[nfft-1] + * Note that each element is complex and can be accessed like + f[k].r and f[k].i + * */ +void kiss_fft_f64(kiss_fft_f64_cfg cfg,const kiss_fft_f64_cpx *fin,kiss_fft_f64_cpx *fout); + +/* + A more generic version of the above function. It reads its input from every Nth sample. + * */ +void kiss_fft_f64_stride(kiss_fft_f64_cfg cfg,const kiss_fft_f64_cpx *fin,kiss_fft_f64_cpx *fout,int fin_stride); + +/* If kiss_fft_f64_alloc allocated a buffer, it is one contiguous + buffer and can be simply free()d when no longer needed*/ +#define kiss_fft_f64_free free + +/* + Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up + your compiler output to call this before you exit. +*/ +void kiss_fft_f64_cleanup(void); + + +/* + * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) + */ +int kiss_fft_f64_next_fast_size(int n); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/gst-libs/gst/fft/kiss_fft_s16.c b/gst-libs/gst/fft/kiss_fft_s16.c new file mode 100644 index 000000000..ca20004b3 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fft_s16.c @@ -0,0 +1,433 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "_kiss_fft_guts_s16.h" +/* The guts header contains all the multiplication and addition macros that are defined for + fixed or floating point complex numbers. It also delares the kf_ internal functions. + */ + +static kiss_fft_s16_cpx *scratchbuf = NULL; +static size_t nscratchbuf = 0; +static kiss_fft_s16_cpx *tmpbuf = NULL; +static size_t ntmpbuf = 0; + +#define CHECKBUF(buf,nbuf,n) \ + do { \ + if ( nbuf < (size_t)(n) ) {\ + free(buf); \ + buf = (kiss_fft_s16_cpx*)KISS_FFT_S16_MALLOC(sizeof(kiss_fft_s16_cpx)*(n)); \ + nbuf = (size_t)(n); \ + } \ + }while(0) + + +static void +kf_bfly2 (kiss_fft_s16_cpx * Fout, + const size_t fstride, const kiss_fft_s16_cfg st, int m) +{ + kiss_fft_s16_cpx *Fout2; + kiss_fft_s16_cpx *tw1 = st->twiddles; + kiss_fft_s16_cpx t; + + Fout2 = Fout + m; + do { + C_FIXDIV (*Fout, 2); + C_FIXDIV (*Fout2, 2); + + C_MUL (t, *Fout2, *tw1); + tw1 += fstride; + C_SUB (*Fout2, *Fout, t); + C_ADDTO (*Fout, t); + ++Fout2; + ++Fout; + } while (--m); +} + +static void +kf_bfly4 (kiss_fft_s16_cpx * Fout, + const size_t fstride, const kiss_fft_s16_cfg st, const size_t m) +{ + kiss_fft_s16_cpx *tw1, *tw2, *tw3; + kiss_fft_s16_cpx scratch[6]; + size_t k = m; + const size_t m2 = 2 * m; + const size_t m3 = 3 * m; + + tw3 = tw2 = tw1 = st->twiddles; + + do { + C_FIXDIV (*Fout, 4); + C_FIXDIV (Fout[m], 4); + C_FIXDIV (Fout[m2], 4); + C_FIXDIV (Fout[m3], 4); + + C_MUL (scratch[0], Fout[m], *tw1); + C_MUL (scratch[1], Fout[m2], *tw2); + C_MUL (scratch[2], Fout[m3], *tw3); + + C_SUB (scratch[5], *Fout, scratch[1]); + C_ADDTO (*Fout, scratch[1]); + C_ADD (scratch[3], scratch[0], scratch[2]); + C_SUB (scratch[4], scratch[0], scratch[2]); + C_SUB (Fout[m2], *Fout, scratch[3]); + tw1 += fstride; + tw2 += fstride * 2; + tw3 += fstride * 3; + C_ADDTO (*Fout, scratch[3]); + + if (st->inverse) { + Fout[m].r = scratch[5].r - scratch[4].i; + Fout[m].i = scratch[5].i + scratch[4].r; + Fout[m3].r = scratch[5].r + scratch[4].i; + Fout[m3].i = scratch[5].i - scratch[4].r; + } else { + Fout[m].r = scratch[5].r + scratch[4].i; + Fout[m].i = scratch[5].i - scratch[4].r; + Fout[m3].r = scratch[5].r - scratch[4].i; + Fout[m3].i = scratch[5].i + scratch[4].r; + } + ++Fout; + } while (--k); +} + +static void +kf_bfly3 (kiss_fft_s16_cpx * Fout, + const size_t fstride, const kiss_fft_s16_cfg st, size_t m) +{ + size_t k = m; + const size_t m2 = 2 * m; + kiss_fft_s16_cpx *tw1, *tw2; + kiss_fft_s16_cpx scratch[5]; + kiss_fft_s16_cpx epi3; + + epi3 = st->twiddles[fstride * m]; + + tw1 = tw2 = st->twiddles; + + do { + C_FIXDIV (*Fout, 3); + C_FIXDIV (Fout[m], 3); + C_FIXDIV (Fout[m2], 3); + + C_MUL (scratch[1], Fout[m], *tw1); + C_MUL (scratch[2], Fout[m2], *tw2); + + C_ADD (scratch[3], scratch[1], scratch[2]); + C_SUB (scratch[0], scratch[1], scratch[2]); + tw1 += fstride; + tw2 += fstride * 2; + + Fout[m].r = Fout->r - HALF_OF (scratch[3].r); + Fout[m].i = Fout->i - HALF_OF (scratch[3].i); + + C_MULBYSCALAR (scratch[0], epi3.i); + + C_ADDTO (*Fout, scratch[3]); + + Fout[m2].r = Fout[m].r + scratch[0].i; + Fout[m2].i = Fout[m].i - scratch[0].r; + + Fout[m].r -= scratch[0].i; + Fout[m].i += scratch[0].r; + + ++Fout; + } while (--k); +} + +static void +kf_bfly5 (kiss_fft_s16_cpx * Fout, + const size_t fstride, const kiss_fft_s16_cfg st, int m) +{ + kiss_fft_s16_cpx *Fout0, *Fout1, *Fout2, *Fout3, *Fout4; + int u; + kiss_fft_s16_cpx scratch[13]; + kiss_fft_s16_cpx *twiddles = st->twiddles; + kiss_fft_s16_cpx *tw; + kiss_fft_s16_cpx ya, yb; + + ya = twiddles[fstride * m]; + yb = twiddles[fstride * 2 * m]; + + Fout0 = Fout; + Fout1 = Fout0 + m; + Fout2 = Fout0 + 2 * m; + Fout3 = Fout0 + 3 * m; + Fout4 = Fout0 + 4 * m; + + tw = st->twiddles; + for (u = 0; u < m; ++u) { + C_FIXDIV (*Fout0, 5); + C_FIXDIV (*Fout1, 5); + C_FIXDIV (*Fout2, 5); + C_FIXDIV (*Fout3, 5); + C_FIXDIV (*Fout4, 5); + scratch[0] = *Fout0; + + C_MUL (scratch[1], *Fout1, tw[u * fstride]); + C_MUL (scratch[2], *Fout2, tw[2 * u * fstride]); + C_MUL (scratch[3], *Fout3, tw[3 * u * fstride]); + C_MUL (scratch[4], *Fout4, tw[4 * u * fstride]); + + C_ADD (scratch[7], scratch[1], scratch[4]); + C_SUB (scratch[10], scratch[1], scratch[4]); + C_ADD (scratch[8], scratch[2], scratch[3]); + C_SUB (scratch[9], scratch[2], scratch[3]); + + Fout0->r += scratch[7].r + scratch[8].r; + Fout0->i += scratch[7].i + scratch[8].i; + + scratch[5].r = + scratch[0].r + S_MUL (scratch[7].r, ya.r) + S_MUL (scratch[8].r, yb.r); + scratch[5].i = + scratch[0].i + S_MUL (scratch[7].i, ya.r) + S_MUL (scratch[8].i, yb.r); + + scratch[6].r = S_MUL (scratch[10].i, ya.i) + S_MUL (scratch[9].i, yb.i); + scratch[6].i = -S_MUL (scratch[10].r, ya.i) - S_MUL (scratch[9].r, yb.i); + + C_SUB (*Fout1, scratch[5], scratch[6]); + C_ADD (*Fout4, scratch[5], scratch[6]); + + scratch[11].r = + scratch[0].r + S_MUL (scratch[7].r, yb.r) + S_MUL (scratch[8].r, ya.r); + scratch[11].i = + scratch[0].i + S_MUL (scratch[7].i, yb.r) + S_MUL (scratch[8].i, ya.r); + scratch[12].r = -S_MUL (scratch[10].i, yb.i) + S_MUL (scratch[9].i, ya.i); + scratch[12].i = S_MUL (scratch[10].r, yb.i) - S_MUL (scratch[9].r, ya.i); + + C_ADD (*Fout2, scratch[11], scratch[12]); + C_SUB (*Fout3, scratch[11], scratch[12]); + + ++Fout0; + ++Fout1; + ++Fout2; + ++Fout3; + ++Fout4; + } +} + +/* perform the butterfly for one stage of a mixed radix FFT */ +static void +kf_bfly_generic (kiss_fft_s16_cpx * Fout, + const size_t fstride, const kiss_fft_s16_cfg st, int m, int p) +{ + int u, k, q1, q; + kiss_fft_s16_cpx *twiddles = st->twiddles; + kiss_fft_s16_cpx t; + int Norig = st->nfft; + + CHECKBUF (scratchbuf, nscratchbuf, p); + + for (u = 0; u < m; ++u) { + k = u; + for (q1 = 0; q1 < p; ++q1) { + scratchbuf[q1] = Fout[k]; + C_FIXDIV (scratchbuf[q1], p); + k += m; + } + + k = u; + for (q1 = 0; q1 < p; ++q1) { + int twidx = 0; + + Fout[k] = scratchbuf[0]; + for (q = 1; q < p; ++q) { + twidx += fstride * k; + if (twidx >= Norig) + twidx -= Norig; + C_MUL (t, scratchbuf[q], twiddles[twidx]); + C_ADDTO (Fout[k], t); + } + k += m; + } + } +} + +static void +kf_work (kiss_fft_s16_cpx * Fout, + const kiss_fft_s16_cpx * f, + const size_t fstride, + int in_stride, int *factors, const kiss_fft_s16_cfg st) +{ + kiss_fft_s16_cpx *Fout_beg = Fout; + const int p = *factors++; /* the radix */ + const int m = *factors++; /* stage's fft length/p */ + const kiss_fft_s16_cpx *Fout_end = Fout + p * m; + + if (m == 1) { + do { + *Fout = *f; + f += fstride * in_stride; + } while (++Fout != Fout_end); + } else { + do { + kf_work (Fout, f, fstride * p, in_stride, factors, st); + f += fstride * in_stride; + } while ((Fout += m) != Fout_end); + } + + Fout = Fout_beg; + + switch (p) { + case 2: + kf_bfly2 (Fout, fstride, st, m); + break; + case 3: + kf_bfly3 (Fout, fstride, st, m); + break; + case 4: + kf_bfly4 (Fout, fstride, st, m); + break; + case 5: + kf_bfly5 (Fout, fstride, st, m); + break; + default: + kf_bfly_generic (Fout, fstride, st, m, p); + break; + } +} + +/* facbuf is populated by p1,m1,p2,m2, ... + where + p[i] * m[i] = m[i-1] + m0 = n */ +static void +kf_factor (int n, int *facbuf) +{ + int p = 4; + double floor_sqrt; + + floor_sqrt = floor (sqrt ((double) n)); + + /*factor out powers of 4, powers of 2, then any remaining primes */ + do { + while (n % p) { + switch (p) { + case 4: + p = 2; + break; + case 2: + p = 3; + break; + default: + p += 2; + break; + } + if (p > floor_sqrt) + p = n; /* no more factors, skip to end */ + } + n /= p; + *facbuf++ = p; + *facbuf++ = n; + } while (n > 1); +} + +/* + * + * User-callable function to allocate all necessary storage space for the fft. + * + * The return value is a contiguous block of memory, allocated with malloc. As such, + * It can be freed with free(), rather than a kiss_fft-specific function. + * */ +kiss_fft_s16_cfg +kiss_fft_s16_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) +{ + kiss_fft_s16_cfg st = NULL; + size_t memneeded = sizeof (struct kiss_fft_s16_state) + + sizeof (kiss_fft_s16_cpx) * (nfft - 1); /* twiddle factors */ + + if (lenmem == NULL) { + st = (kiss_fft_s16_cfg) KISS_FFT_S16_MALLOC (memneeded); + } else { + if (mem != NULL && *lenmem >= memneeded) + st = (kiss_fft_s16_cfg) mem; + *lenmem = memneeded; + } + if (st) { + int i; + + st->nfft = nfft; + st->inverse = inverse_fft; + + for (i = 0; i < nfft; ++i) { + const double pi = + 3.141592653589793238462643383279502884197169399375105820974944; + double phase = -2 * pi * i / nfft; + + if (st->inverse) + phase *= -1; + kf_cexp (st->twiddles + i, phase); + } + + kf_factor (nfft, st->factors); + } + return st; +} + + + + +void +kiss_fft_s16_stride (kiss_fft_s16_cfg st, const kiss_fft_s16_cpx * fin, + kiss_fft_s16_cpx * fout, int in_stride) +{ + if (fin == fout) { + CHECKBUF (tmpbuf, ntmpbuf, st->nfft); + kf_work (tmpbuf, fin, 1, in_stride, st->factors, st); + memcpy (fout, tmpbuf, sizeof (kiss_fft_s16_cpx) * st->nfft); + } else { + kf_work (fout, fin, 1, in_stride, st->factors, st); + } +} + +void +kiss_fft_s16 (kiss_fft_s16_cfg cfg, const kiss_fft_s16_cpx * fin, + kiss_fft_s16_cpx * fout) +{ + kiss_fft_s16_stride (cfg, fin, fout, 1); +} + + +/* not really necessary to call, but if someone is doing in-place ffts, they may want to free the + buffers from CHECKBUF + */ +void +kiss_fft_s16_cleanup (void) +{ + free (scratchbuf); + scratchbuf = NULL; + nscratchbuf = 0; + free (tmpbuf); + tmpbuf = NULL; + ntmpbuf = 0; +} + +int +kiss_fft_s16_next_fast_size (int n) +{ + while (1) { + int m = n; + + while ((m % 2) == 0) + m /= 2; + while ((m % 3) == 0) + m /= 3; + while ((m % 5) == 0) + m /= 5; + if (m <= 1) + break; /* n is completely factorable by twos, threes, and fives */ + n++; + } + return n; +} diff --git a/gst-libs/gst/fft/kiss_fft_s16.h b/gst-libs/gst/fft/kiss_fft_s16.h new file mode 100644 index 000000000..e607dfd6e --- /dev/null +++ b/gst-libs/gst/fft/kiss_fft_s16.h @@ -0,0 +1,101 @@ +#ifndef KISS_FFT_S16_H +#define KISS_FFT_S16_H + +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <memory.h> +#include <malloc.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* + ATTENTION! + If you would like a : + -- a utility that will handle the caching of fft objects + -- real-only (no imaginary time component ) FFT + -- a multi-dimensional FFT + -- a command-line utility to perform ffts + -- a command-line utility to perform fast-convolution filtering + + Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c + in the tools/ directory. +*/ + +#define KISS_FFT_S16_MALLOC malloc + +#include <sys/types.h> +#define kiss_fft_s16_scalar int16_t + +typedef struct { + kiss_fft_s16_scalar r; + kiss_fft_s16_scalar i; +}kiss_fft_s16_cpx; + +typedef struct kiss_fft_s16_state* kiss_fft_s16_cfg; + +/* + * kiss_fft_s16_alloc + * + * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. + * + * typical usage: kiss_fft_s16_cfg mycfg=kiss_fft_s16_alloc(1024,0,NULL,NULL); + * + * The return value from fft_alloc is a cfg buffer used internally + * by the fft routine or NULL. + * + * If lenmem is NULL, then kiss_fft_s16_alloc will allocate a cfg buffer using malloc. + * The returned value should be free()d when done to avoid memory leaks. + * + * The state can be placed in a user supplied buffer 'mem': + * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, + * then the function places the cfg in mem and the size used in *lenmem + * and returns mem. + * + * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), + * then the function returns NULL and places the minimum cfg + * buffer size in *lenmem. + * */ + +kiss_fft_s16_cfg kiss_fft_s16_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); + +/* + * kiss_fft(cfg,in_out_buf) + * + * Perform an FFT on a complex input buffer. + * for a forward FFT, + * fin should be f[0] , f[1] , ... ,f[nfft-1] + * fout will be F[0] , F[1] , ... ,F[nfft-1] + * Note that each element is complex and can be accessed like + f[k].r and f[k].i + * */ +void kiss_fft_s16(kiss_fft_s16_cfg cfg,const kiss_fft_s16_cpx *fin,kiss_fft_s16_cpx *fout); + +/* + A more generic version of the above function. It reads its input from every Nth sample. + * */ +void kiss_fft_s16_stride(kiss_fft_s16_cfg cfg,const kiss_fft_s16_cpx *fin,kiss_fft_s16_cpx *fout,int fin_stride); + +/* If kiss_fft_s16_alloc allocated a buffer, it is one contiguous + buffer and can be simply free()d when no longer needed*/ +#define kiss_fft_s16_free free + +/* + Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up + your compiler output to call this before you exit. +*/ +void kiss_fft_s16_cleanup(void); + + +/* + * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) + */ +int kiss_fft_s16_next_fast_size(int n); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/gst-libs/gst/fft/kiss_fft_s32.c b/gst-libs/gst/fft/kiss_fft_s32.c new file mode 100644 index 000000000..760013c25 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fft_s32.c @@ -0,0 +1,433 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "_kiss_fft_guts_s32.h" +/* The guts header contains all the multiplication and addition macros that are defined for + fixed or floating point complex numbers. It also delares the kf_ internal functions. + */ + +static kiss_fft_s32_cpx *scratchbuf = NULL; +static size_t nscratchbuf = 0; +static kiss_fft_s32_cpx *tmpbuf = NULL; +static size_t ntmpbuf = 0; + +#define CHECKBUF(buf,nbuf,n) \ + do { \ + if ( nbuf < (size_t)(n) ) {\ + free(buf); \ + buf = (kiss_fft_s32_cpx*)KISS_FFT_S32_MALLOC(sizeof(kiss_fft_s32_cpx)*(n)); \ + nbuf = (size_t)(n); \ + } \ + }while(0) + + +static void +kf_bfly2 (kiss_fft_s32_cpx * Fout, + const size_t fstride, const kiss_fft_s32_cfg st, int m) +{ + kiss_fft_s32_cpx *Fout2; + kiss_fft_s32_cpx *tw1 = st->twiddles; + kiss_fft_s32_cpx t; + + Fout2 = Fout + m; + do { + C_FIXDIV (*Fout, 2); + C_FIXDIV (*Fout2, 2); + + C_MUL (t, *Fout2, *tw1); + tw1 += fstride; + C_SUB (*Fout2, *Fout, t); + C_ADDTO (*Fout, t); + ++Fout2; + ++Fout; + } while (--m); +} + +static void +kf_bfly4 (kiss_fft_s32_cpx * Fout, + const size_t fstride, const kiss_fft_s32_cfg st, const size_t m) +{ + kiss_fft_s32_cpx *tw1, *tw2, *tw3; + kiss_fft_s32_cpx scratch[6]; + size_t k = m; + const size_t m2 = 2 * m; + const size_t m3 = 3 * m; + + tw3 = tw2 = tw1 = st->twiddles; + + do { + C_FIXDIV (*Fout, 4); + C_FIXDIV (Fout[m], 4); + C_FIXDIV (Fout[m2], 4); + C_FIXDIV (Fout[m3], 4); + + C_MUL (scratch[0], Fout[m], *tw1); + C_MUL (scratch[1], Fout[m2], *tw2); + C_MUL (scratch[2], Fout[m3], *tw3); + + C_SUB (scratch[5], *Fout, scratch[1]); + C_ADDTO (*Fout, scratch[1]); + C_ADD (scratch[3], scratch[0], scratch[2]); + C_SUB (scratch[4], scratch[0], scratch[2]); + C_SUB (Fout[m2], *Fout, scratch[3]); + tw1 += fstride; + tw2 += fstride * 2; + tw3 += fstride * 3; + C_ADDTO (*Fout, scratch[3]); + + if (st->inverse) { + Fout[m].r = scratch[5].r - scratch[4].i; + Fout[m].i = scratch[5].i + scratch[4].r; + Fout[m3].r = scratch[5].r + scratch[4].i; + Fout[m3].i = scratch[5].i - scratch[4].r; + } else { + Fout[m].r = scratch[5].r + scratch[4].i; + Fout[m].i = scratch[5].i - scratch[4].r; + Fout[m3].r = scratch[5].r - scratch[4].i; + Fout[m3].i = scratch[5].i + scratch[4].r; + } + ++Fout; + } while (--k); +} + +static void +kf_bfly3 (kiss_fft_s32_cpx * Fout, + const size_t fstride, const kiss_fft_s32_cfg st, size_t m) +{ + size_t k = m; + const size_t m2 = 2 * m; + kiss_fft_s32_cpx *tw1, *tw2; + kiss_fft_s32_cpx scratch[5]; + kiss_fft_s32_cpx epi3; + + epi3 = st->twiddles[fstride * m]; + + tw1 = tw2 = st->twiddles; + + do { + C_FIXDIV (*Fout, 3); + C_FIXDIV (Fout[m], 3); + C_FIXDIV (Fout[m2], 3); + + C_MUL (scratch[1], Fout[m], *tw1); + C_MUL (scratch[2], Fout[m2], *tw2); + + C_ADD (scratch[3], scratch[1], scratch[2]); + C_SUB (scratch[0], scratch[1], scratch[2]); + tw1 += fstride; + tw2 += fstride * 2; + + Fout[m].r = Fout->r - HALF_OF (scratch[3].r); + Fout[m].i = Fout->i - HALF_OF (scratch[3].i); + + C_MULBYSCALAR (scratch[0], epi3.i); + + C_ADDTO (*Fout, scratch[3]); + + Fout[m2].r = Fout[m].r + scratch[0].i; + Fout[m2].i = Fout[m].i - scratch[0].r; + + Fout[m].r -= scratch[0].i; + Fout[m].i += scratch[0].r; + + ++Fout; + } while (--k); +} + +static void +kf_bfly5 (kiss_fft_s32_cpx * Fout, + const size_t fstride, const kiss_fft_s32_cfg st, int m) +{ + kiss_fft_s32_cpx *Fout0, *Fout1, *Fout2, *Fout3, *Fout4; + int u; + kiss_fft_s32_cpx scratch[13]; + kiss_fft_s32_cpx *twiddles = st->twiddles; + kiss_fft_s32_cpx *tw; + kiss_fft_s32_cpx ya, yb; + + ya = twiddles[fstride * m]; + yb = twiddles[fstride * 2 * m]; + + Fout0 = Fout; + Fout1 = Fout0 + m; + Fout2 = Fout0 + 2 * m; + Fout3 = Fout0 + 3 * m; + Fout4 = Fout0 + 4 * m; + + tw = st->twiddles; + for (u = 0; u < m; ++u) { + C_FIXDIV (*Fout0, 5); + C_FIXDIV (*Fout1, 5); + C_FIXDIV (*Fout2, 5); + C_FIXDIV (*Fout3, 5); + C_FIXDIV (*Fout4, 5); + scratch[0] = *Fout0; + + C_MUL (scratch[1], *Fout1, tw[u * fstride]); + C_MUL (scratch[2], *Fout2, tw[2 * u * fstride]); + C_MUL (scratch[3], *Fout3, tw[3 * u * fstride]); + C_MUL (scratch[4], *Fout4, tw[4 * u * fstride]); + + C_ADD (scratch[7], scratch[1], scratch[4]); + C_SUB (scratch[10], scratch[1], scratch[4]); + C_ADD (scratch[8], scratch[2], scratch[3]); + C_SUB (scratch[9], scratch[2], scratch[3]); + + Fout0->r += scratch[7].r + scratch[8].r; + Fout0->i += scratch[7].i + scratch[8].i; + + scratch[5].r = + scratch[0].r + S_MUL (scratch[7].r, ya.r) + S_MUL (scratch[8].r, yb.r); + scratch[5].i = + scratch[0].i + S_MUL (scratch[7].i, ya.r) + S_MUL (scratch[8].i, yb.r); + + scratch[6].r = S_MUL (scratch[10].i, ya.i) + S_MUL (scratch[9].i, yb.i); + scratch[6].i = -S_MUL (scratch[10].r, ya.i) - S_MUL (scratch[9].r, yb.i); + + C_SUB (*Fout1, scratch[5], scratch[6]); + C_ADD (*Fout4, scratch[5], scratch[6]); + + scratch[11].r = + scratch[0].r + S_MUL (scratch[7].r, yb.r) + S_MUL (scratch[8].r, ya.r); + scratch[11].i = + scratch[0].i + S_MUL (scratch[7].i, yb.r) + S_MUL (scratch[8].i, ya.r); + scratch[12].r = -S_MUL (scratch[10].i, yb.i) + S_MUL (scratch[9].i, ya.i); + scratch[12].i = S_MUL (scratch[10].r, yb.i) - S_MUL (scratch[9].r, ya.i); + + C_ADD (*Fout2, scratch[11], scratch[12]); + C_SUB (*Fout3, scratch[11], scratch[12]); + + ++Fout0; + ++Fout1; + ++Fout2; + ++Fout3; + ++Fout4; + } +} + +/* perform the butterfly for one stage of a mixed radix FFT */ +static void +kf_bfly_generic (kiss_fft_s32_cpx * Fout, + const size_t fstride, const kiss_fft_s32_cfg st, int m, int p) +{ + int u, k, q1, q; + kiss_fft_s32_cpx *twiddles = st->twiddles; + kiss_fft_s32_cpx t; + int Norig = st->nfft; + + CHECKBUF (scratchbuf, nscratchbuf, p); + + for (u = 0; u < m; ++u) { + k = u; + for (q1 = 0; q1 < p; ++q1) { + scratchbuf[q1] = Fout[k]; + C_FIXDIV (scratchbuf[q1], p); + k += m; + } + + k = u; + for (q1 = 0; q1 < p; ++q1) { + int twidx = 0; + + Fout[k] = scratchbuf[0]; + for (q = 1; q < p; ++q) { + twidx += fstride * k; + if (twidx >= Norig) + twidx -= Norig; + C_MUL (t, scratchbuf[q], twiddles[twidx]); + C_ADDTO (Fout[k], t); + } + k += m; + } + } +} + +static void +kf_work (kiss_fft_s32_cpx * Fout, + const kiss_fft_s32_cpx * f, + const size_t fstride, + int in_stride, int *factors, const kiss_fft_s32_cfg st) +{ + kiss_fft_s32_cpx *Fout_beg = Fout; + const int p = *factors++; /* the radix */ + const int m = *factors++; /* stage's fft length/p */ + const kiss_fft_s32_cpx *Fout_end = Fout + p * m; + + if (m == 1) { + do { + *Fout = *f; + f += fstride * in_stride; + } while (++Fout != Fout_end); + } else { + do { + kf_work (Fout, f, fstride * p, in_stride, factors, st); + f += fstride * in_stride; + } while ((Fout += m) != Fout_end); + } + + Fout = Fout_beg; + + switch (p) { + case 2: + kf_bfly2 (Fout, fstride, st, m); + break; + case 3: + kf_bfly3 (Fout, fstride, st, m); + break; + case 4: + kf_bfly4 (Fout, fstride, st, m); + break; + case 5: + kf_bfly5 (Fout, fstride, st, m); + break; + default: + kf_bfly_generic (Fout, fstride, st, m, p); + break; + } +} + +/* facbuf is populated by p1,m1,p2,m2, ... + where + p[i] * m[i] = m[i-1] + m0 = n */ +static void +kf_factor (int n, int *facbuf) +{ + int p = 4; + double floor_sqrt; + + floor_sqrt = floor (sqrt ((double) n)); + + /*factor out powers of 4, powers of 2, then any remaining primes */ + do { + while (n % p) { + switch (p) { + case 4: + p = 2; + break; + case 2: + p = 3; + break; + default: + p += 2; + break; + } + if (p > floor_sqrt) + p = n; /* no more factors, skip to end */ + } + n /= p; + *facbuf++ = p; + *facbuf++ = n; + } while (n > 1); +} + +/* + * + * User-callable function to allocate all necessary storage space for the fft. + * + * The return value is a contiguous block of memory, allocated with malloc. As such, + * It can be freed with free(), rather than a kiss_fft-specific function. + * */ +kiss_fft_s32_cfg +kiss_fft_s32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) +{ + kiss_fft_s32_cfg st = NULL; + size_t memneeded = sizeof (struct kiss_fft_s32_state) + + sizeof (kiss_fft_s32_cpx) * (nfft - 1); /* twiddle factors */ + + if (lenmem == NULL) { + st = (kiss_fft_s32_cfg) KISS_FFT_S32_MALLOC (memneeded); + } else { + if (mem != NULL && *lenmem >= memneeded) + st = (kiss_fft_s32_cfg) mem; + *lenmem = memneeded; + } + if (st) { + int i; + + st->nfft = nfft; + st->inverse = inverse_fft; + + for (i = 0; i < nfft; ++i) { + const double pi = + 3.141592653589793238462643383279502884197169399375105820974944; + double phase = -2 * pi * i / nfft; + + if (st->inverse) + phase *= -1; + kf_cexp (st->twiddles + i, phase); + } + + kf_factor (nfft, st->factors); + } + return st; +} + + + + +void +kiss_fft_s32_stride (kiss_fft_s32_cfg st, const kiss_fft_s32_cpx * fin, + kiss_fft_s32_cpx * fout, int in_stride) +{ + if (fin == fout) { + CHECKBUF (tmpbuf, ntmpbuf, st->nfft); + kf_work (tmpbuf, fin, 1, in_stride, st->factors, st); + memcpy (fout, tmpbuf, sizeof (kiss_fft_s32_cpx) * st->nfft); + } else { + kf_work (fout, fin, 1, in_stride, st->factors, st); + } +} + +void +kiss_fft_s32 (kiss_fft_s32_cfg cfg, const kiss_fft_s32_cpx * fin, + kiss_fft_s32_cpx * fout) +{ + kiss_fft_s32_stride (cfg, fin, fout, 1); +} + + +/* not really necessary to call, but if someone is doing in-place ffts, they may want to free the + buffers from CHECKBUF + */ +void +kiss_fft_s32_cleanup (void) +{ + free (scratchbuf); + scratchbuf = NULL; + nscratchbuf = 0; + free (tmpbuf); + tmpbuf = NULL; + ntmpbuf = 0; +} + +int +kiss_fft_s32_next_fast_size (int n) +{ + while (1) { + int m = n; + + while ((m % 2) == 0) + m /= 2; + while ((m % 3) == 0) + m /= 3; + while ((m % 5) == 0) + m /= 5; + if (m <= 1) + break; /* n is completely factorable by twos, threes, and fives */ + n++; + } + return n; +} diff --git a/gst-libs/gst/fft/kiss_fft_s32.h b/gst-libs/gst/fft/kiss_fft_s32.h new file mode 100644 index 000000000..8a42768dc --- /dev/null +++ b/gst-libs/gst/fft/kiss_fft_s32.h @@ -0,0 +1,102 @@ +#ifndef KISS_FFT_S32_H +#define KISS_FFT_S32_H + +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <memory.h> +#include <malloc.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* + ATTENTION! + If you would like a : + -- a utility that will handle the caching of fft objects + -- real-only (no imaginary time component ) FFT + -- a multi-dimensional FFT + -- a command-line utility to perform ffts + -- a command-line utility to perform fast-convolution filtering + + Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c + in the tools/ directory. +*/ + +#define KISS_FFT_S32_MALLOC malloc + + +#include <sys/types.h> +#define kiss_fft_s32_scalar int32_t + +typedef struct { + kiss_fft_s32_scalar r; + kiss_fft_s32_scalar i; +}kiss_fft_s32_cpx; + +typedef struct kiss_fft_s32_state* kiss_fft_s32_cfg; + +/* + * kiss_fft_s32_alloc + * + * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. + * + * typical usage: kiss_fft_s32_cfg mycfg=kiss_fft_s32_alloc(1024,0,NULL,NULL); + * + * The return value from fft_alloc is a cfg buffer used internally + * by the fft routine or NULL. + * + * If lenmem is NULL, then kiss_fft_s32_alloc will allocate a cfg buffer using malloc. + * The returned value should be free()d when done to avoid memory leaks. + * + * The state can be placed in a user supplied buffer 'mem': + * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, + * then the function places the cfg in mem and the size used in *lenmem + * and returns mem. + * + * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), + * then the function returns NULL and places the minimum cfg + * buffer size in *lenmem. + * */ + +kiss_fft_s32_cfg kiss_fft_s32_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); + +/* + * kiss_fft(cfg,in_out_buf) + * + * Perform an FFT on a complex input buffer. + * for a forward FFT, + * fin should be f[0] , f[1] , ... ,f[nfft-1] + * fout will be F[0] , F[1] , ... ,F[nfft-1] + * Note that each element is complex and can be accessed like + f[k].r and f[k].i + * */ +void kiss_fft_s32(kiss_fft_s32_cfg cfg,const kiss_fft_s32_cpx *fin,kiss_fft_s32_cpx *fout); + +/* + A more generic version of the above function. It reads its input from every Nth sample. + * */ +void kiss_fft_s32_stride(kiss_fft_s32_cfg cfg,const kiss_fft_s32_cpx *fin,kiss_fft_s32_cpx *fout,int fin_stride); + +/* If kiss_fft_s32_alloc allocated a buffer, it is one contiguous + buffer and can be simply free()d when no longer needed*/ +#define kiss_fft_s32_free free + +/* + Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up + your compiler output to call this before you exit. +*/ +void kiss_fft_s32_cleanup(void); + + +/* + * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) + */ +int kiss_fft_s32_next_fast_size(int n); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/gst-libs/gst/fft/kiss_fftr_f32.c b/gst-libs/gst/fft/kiss_fftr_f32.c new file mode 100644 index 000000000..1f9fdb21a --- /dev/null +++ b/gst-libs/gst/fft/kiss_fftr_f32.c @@ -0,0 +1,168 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "kiss_fftr_f32.h" +#include "_kiss_fft_guts_f32.h" + +struct kiss_fftr_f32_state +{ + kiss_fft_f32_cfg substate; + kiss_fft_f32_cpx *tmpbuf; + kiss_fft_f32_cpx *super_twiddles; +#ifdef USE_SIMD + long pad; +#endif +}; + +kiss_fftr_f32_cfg +kiss_fftr_f32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) +{ + int i; + kiss_fftr_f32_cfg st = NULL; + size_t subsize, memneeded; + + if (nfft & 1) { + fprintf (stderr, "Real FFT optimization must be even.\n"); + return NULL; + } + nfft >>= 1; + + kiss_fft_f32_alloc (nfft, inverse_fft, NULL, &subsize); + memneeded = + sizeof (struct kiss_fftr_f32_state) + subsize + + sizeof (kiss_fft_f32_cpx) * (nfft * 2); + + if (lenmem == NULL) { + st = (kiss_fftr_f32_cfg) KISS_FFT_F32_MALLOC (memneeded); + } else { + if (*lenmem >= memneeded) + st = (kiss_fftr_f32_cfg) mem; + *lenmem = memneeded; + } + if (!st) + return NULL; + + st->substate = (kiss_fft_f32_cfg) (st + 1); /*just beyond kiss_fftr_f32_state struct */ + st->tmpbuf = (kiss_fft_f32_cpx *) (((char *) st->substate) + subsize); + st->super_twiddles = st->tmpbuf + nfft; + kiss_fft_f32_alloc (nfft, inverse_fft, st->substate, &subsize); + + for (i = 0; i < nfft; ++i) { + double phase = -3.14159265358979323846264338327 * ((double) i / nfft + .5); + + if (inverse_fft) + phase *= -1; + kf_cexp (st->super_twiddles + i, phase); + } + return st; +} + +void +kiss_fftr_f32 (kiss_fftr_f32_cfg st, const kiss_fft_f32_scalar * timedata, + kiss_fft_f32_cpx * freqdata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + kiss_fft_f32_cpx fpnk, fpk, f1k, f2k, tw, tdc; + + if (st->substate->inverse) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + /*perform the parallel fft of two real signals packed in real,imag */ + kiss_fft_f32 (st->substate, (const kiss_fft_f32_cpx *) timedata, st->tmpbuf); + /* The real part of the DC element of the frequency spectrum in st->tmpbuf + * contains the sum of the even-numbered elements of the input time sequence + * The imag part is the sum of the odd-numbered elements + * + * The sum of tdc.r and tdc.i is the sum of the input time sequence. + * yielding DC of input time sequence + * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... + * yielding Nyquist bin of input time sequence + */ + + tdc.r = st->tmpbuf[0].r; + tdc.i = st->tmpbuf[0].i; + C_FIXDIV (tdc, 2); + CHECK_OVERFLOW_OP (tdc.r, +, tdc.i); + CHECK_OVERFLOW_OP (tdc.r, -, tdc.i); + freqdata[0].r = tdc.r + tdc.i; + freqdata[ncfft].r = tdc.r - tdc.i; +#ifdef USE_SIMD + freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps (0); +#else + freqdata[ncfft].i = freqdata[0].i = 0; +#endif + + for (k = 1; k <= ncfft / 2; ++k) { + fpk = st->tmpbuf[k]; + fpnk.r = st->tmpbuf[ncfft - k].r; + fpnk.i = -st->tmpbuf[ncfft - k].i; + C_FIXDIV (fpk, 2); + C_FIXDIV (fpnk, 2); + + C_ADD (f1k, fpk, fpnk); + C_SUB (f2k, fpk, fpnk); + C_MUL (tw, f2k, st->super_twiddles[k]); + + freqdata[k].r = HALF_OF (f1k.r + tw.r); + freqdata[k].i = HALF_OF (f1k.i + tw.i); + freqdata[ncfft - k].r = HALF_OF (f1k.r - tw.r); + freqdata[ncfft - k].i = HALF_OF (tw.i - f1k.i); + } +} + +void +kiss_fftri_f32 (kiss_fftr_f32_cfg st, const kiss_fft_f32_cpx * freqdata, + kiss_fft_f32_scalar * timedata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + + if (st->substate->inverse == 0) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; + st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; + C_FIXDIV (st->tmpbuf[0], 2); + + for (k = 1; k <= ncfft / 2; ++k) { + kiss_fft_f32_cpx fk, fnkc, fek, fok, tmp; + + fk = freqdata[k]; + fnkc.r = freqdata[ncfft - k].r; + fnkc.i = -freqdata[ncfft - k].i; + C_FIXDIV (fk, 2); + C_FIXDIV (fnkc, 2); + + C_ADD (fek, fk, fnkc); + C_SUB (tmp, fk, fnkc); + C_MUL (fok, tmp, st->super_twiddles[k]); + C_ADD (st->tmpbuf[k], fek, fok); + C_SUB (st->tmpbuf[ncfft - k], fek, fok); +#ifdef USE_SIMD + st->tmpbuf[ncfft - k].i *= _mm_set1_ps (-1.0); +#else + st->tmpbuf[ncfft - k].i *= -1; +#endif + } + kiss_fft_f32 (st->substate, st->tmpbuf, (kiss_fft_f32_cpx *) timedata); +} diff --git a/gst-libs/gst/fft/kiss_fftr_f32.h b/gst-libs/gst/fft/kiss_fftr_f32.h new file mode 100644 index 000000000..91f2085e5 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fftr_f32.h @@ -0,0 +1,46 @@ +#ifndef KISS_FTR_F32_H +#define KISS_FTR_F32_H + +#include "kiss_fft_f32.h" +#ifdef __cplusplus +extern "C" { +#endif + + +/* + + Real optimized version can save about 45% cpu time vs. complex fft of a real seq. + + + + */ + +typedef struct kiss_fftr_f32_state *kiss_fftr_f32_cfg; + + +kiss_fftr_f32_cfg kiss_fftr_f32_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); +/* + nfft must be even + + If you don't care to allocate space, use mem = lenmem = NULL +*/ + + +void kiss_fftr_f32(kiss_fftr_f32_cfg cfg,const kiss_fft_f32_scalar *timedata,kiss_fft_f32_cpx *freqdata); +/* + input timedata has nfft scalar points + output freqdata has nfft/2+1 complex points +*/ + +void kiss_fftri_f32(kiss_fftr_f32_cfg cfg,const kiss_fft_f32_cpx *freqdata,kiss_fft_f32_scalar *timedata); +/* + input freqdata has nfft/2+1 complex points + output timedata has nfft scalar points +*/ + +#define kiss_fftr_f32_free free + +#ifdef __cplusplus +} +#endif +#endif diff --git a/gst-libs/gst/fft/kiss_fftr_f64.c b/gst-libs/gst/fft/kiss_fftr_f64.c new file mode 100644 index 000000000..d566336d3 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fftr_f64.c @@ -0,0 +1,168 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "kiss_fftr_f64.h" +#include "_kiss_fft_guts_f64.h" + +struct kiss_fftr_f64_state +{ + kiss_fft_f64_cfg substate; + kiss_fft_f64_cpx *tmpbuf; + kiss_fft_f64_cpx *super_twiddles; +#ifdef USE_SIMD + long pad; +#endif +}; + +kiss_fftr_f64_cfg +kiss_fftr_f64_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) +{ + int i; + kiss_fftr_f64_cfg st = NULL; + size_t subsize, memneeded; + + if (nfft & 1) { + fprintf (stderr, "Real FFT optimization must be even.\n"); + return NULL; + } + nfft >>= 1; + + kiss_fft_f64_alloc (nfft, inverse_fft, NULL, &subsize); + memneeded = + sizeof (struct kiss_fftr_f64_state) + subsize + + sizeof (kiss_fft_f64_cpx) * (nfft * 2); + + if (lenmem == NULL) { + st = (kiss_fftr_f64_cfg) KISS_FFT_F64_MALLOC (memneeded); + } else { + if (*lenmem >= memneeded) + st = (kiss_fftr_f64_cfg) mem; + *lenmem = memneeded; + } + if (!st) + return NULL; + + st->substate = (kiss_fft_f64_cfg) (st + 1); /*just beyond kiss_fftr_f64_state struct */ + st->tmpbuf = (kiss_fft_f64_cpx *) (((char *) st->substate) + subsize); + st->super_twiddles = st->tmpbuf + nfft; + kiss_fft_f64_alloc (nfft, inverse_fft, st->substate, &subsize); + + for (i = 0; i < nfft; ++i) { + double phase = -3.14159265358979323846264338327 * ((double) i / nfft + .5); + + if (inverse_fft) + phase *= -1; + kf_cexp (st->super_twiddles + i, phase); + } + return st; +} + +void +kiss_fftr_f64 (kiss_fftr_f64_cfg st, const kiss_fft_f64_scalar * timedata, + kiss_fft_f64_cpx * freqdata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + kiss_fft_f64_cpx fpnk, fpk, f1k, f2k, tw, tdc; + + if (st->substate->inverse) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + /*perform the parallel fft of two real signals packed in real,imag */ + kiss_fft_f64 (st->substate, (const kiss_fft_f64_cpx *) timedata, st->tmpbuf); + /* The real part of the DC element of the frequency spectrum in st->tmpbuf + * contains the sum of the even-numbered elements of the input time sequence + * The imag part is the sum of the odd-numbered elements + * + * The sum of tdc.r and tdc.i is the sum of the input time sequence. + * yielding DC of input time sequence + * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... + * yielding Nyquist bin of input time sequence + */ + + tdc.r = st->tmpbuf[0].r; + tdc.i = st->tmpbuf[0].i; + C_FIXDIV (tdc, 2); + CHECK_OVERFLOW_OP (tdc.r, +, tdc.i); + CHECK_OVERFLOW_OP (tdc.r, -, tdc.i); + freqdata[0].r = tdc.r + tdc.i; + freqdata[ncfft].r = tdc.r - tdc.i; +#ifdef USE_SIMD + freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps (0); +#else + freqdata[ncfft].i = freqdata[0].i = 0; +#endif + + for (k = 1; k <= ncfft / 2; ++k) { + fpk = st->tmpbuf[k]; + fpnk.r = st->tmpbuf[ncfft - k].r; + fpnk.i = -st->tmpbuf[ncfft - k].i; + C_FIXDIV (fpk, 2); + C_FIXDIV (fpnk, 2); + + C_ADD (f1k, fpk, fpnk); + C_SUB (f2k, fpk, fpnk); + C_MUL (tw, f2k, st->super_twiddles[k]); + + freqdata[k].r = HALF_OF (f1k.r + tw.r); + freqdata[k].i = HALF_OF (f1k.i + tw.i); + freqdata[ncfft - k].r = HALF_OF (f1k.r - tw.r); + freqdata[ncfft - k].i = HALF_OF (tw.i - f1k.i); + } +} + +void +kiss_fftri_f64 (kiss_fftr_f64_cfg st, const kiss_fft_f64_cpx * freqdata, + kiss_fft_f64_scalar * timedata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + + if (st->substate->inverse == 0) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; + st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; + C_FIXDIV (st->tmpbuf[0], 2); + + for (k = 1; k <= ncfft / 2; ++k) { + kiss_fft_f64_cpx fk, fnkc, fek, fok, tmp; + + fk = freqdata[k]; + fnkc.r = freqdata[ncfft - k].r; + fnkc.i = -freqdata[ncfft - k].i; + C_FIXDIV (fk, 2); + C_FIXDIV (fnkc, 2); + + C_ADD (fek, fk, fnkc); + C_SUB (tmp, fk, fnkc); + C_MUL (fok, tmp, st->super_twiddles[k]); + C_ADD (st->tmpbuf[k], fek, fok); + C_SUB (st->tmpbuf[ncfft - k], fek, fok); +#ifdef USE_SIMD + st->tmpbuf[ncfft - k].i *= _mm_set1_ps (-1.0); +#else + st->tmpbuf[ncfft - k].i *= -1; +#endif + } + kiss_fft_f64 (st->substate, st->tmpbuf, (kiss_fft_f64_cpx *) timedata); +} diff --git a/gst-libs/gst/fft/kiss_fftr_f64.h b/gst-libs/gst/fft/kiss_fftr_f64.h new file mode 100644 index 000000000..0b35cb6ad --- /dev/null +++ b/gst-libs/gst/fft/kiss_fftr_f64.h @@ -0,0 +1,46 @@ +#ifndef KISS_FTR_F64_H +#define KISS_FTR_F64_H + +#include "kiss_fft_f64.h" +#ifdef __cplusplus +extern "C" { +#endif + + +/* + + Real optimized version can save about 45% cpu time vs. complex fft of a real seq. + + + + */ + +typedef struct kiss_fftr_f64_state *kiss_fftr_f64_cfg; + + +kiss_fftr_f64_cfg kiss_fftr_f64_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); +/* + nfft must be even + + If you don't care to allocate space, use mem = lenmem = NULL +*/ + + +void kiss_fftr_f64(kiss_fftr_f64_cfg cfg,const kiss_fft_f64_scalar *timedata,kiss_fft_f64_cpx *freqdata); +/* + input timedata has nfft scalar points + output freqdata has nfft/2+1 complex points +*/ + +void kiss_fftri_f64(kiss_fftr_f64_cfg cfg,const kiss_fft_f64_cpx *freqdata,kiss_fft_f64_scalar *timedata); +/* + input freqdata has nfft/2+1 complex points + output timedata has nfft scalar points +*/ + +#define kiss_fftr_f64_free free + +#ifdef __cplusplus +} +#endif +#endif diff --git a/gst-libs/gst/fft/kiss_fftr_s16.c b/gst-libs/gst/fft/kiss_fftr_s16.c new file mode 100644 index 000000000..2df51557a --- /dev/null +++ b/gst-libs/gst/fft/kiss_fftr_s16.c @@ -0,0 +1,168 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "kiss_fftr_s16.h" +#include "_kiss_fft_guts_s16.h" + +struct kiss_fftr_s16_state +{ + kiss_fft_s16_cfg substate; + kiss_fft_s16_cpx *tmpbuf; + kiss_fft_s16_cpx *super_twiddles; +#ifdef USE_SIMD + long pad; +#endif +}; + +kiss_fftr_s16_cfg +kiss_fftr_s16_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) +{ + int i; + kiss_fftr_s16_cfg st = NULL; + size_t subsize, memneeded; + + if (nfft & 1) { + fprintf (stderr, "Real FFT optimization must be even.\n"); + return NULL; + } + nfft >>= 1; + + kiss_fft_s16_alloc (nfft, inverse_fft, NULL, &subsize); + memneeded = + sizeof (struct kiss_fftr_s16_state) + subsize + + sizeof (kiss_fft_s16_cpx) * (nfft * 2); + + if (lenmem == NULL) { + st = (kiss_fftr_s16_cfg) KISS_FFT_S16_MALLOC (memneeded); + } else { + if (*lenmem >= memneeded) + st = (kiss_fftr_s16_cfg) mem; + *lenmem = memneeded; + } + if (!st) + return NULL; + + st->substate = (kiss_fft_s16_cfg) (st + 1); /*just beyond kiss_fftr_s16_state struct */ + st->tmpbuf = (kiss_fft_s16_cpx *) (((char *) st->substate) + subsize); + st->super_twiddles = st->tmpbuf + nfft; + kiss_fft_s16_alloc (nfft, inverse_fft, st->substate, &subsize); + + for (i = 0; i < nfft; ++i) { + double phase = -3.14159265358979323846264338327 * ((double) i / nfft + .5); + + if (inverse_fft) + phase *= -1; + kf_cexp (st->super_twiddles + i, phase); + } + return st; +} + +void +kiss_fftr_s16 (kiss_fftr_s16_cfg st, const kiss_fft_s16_scalar * timedata, + kiss_fft_s16_cpx * freqdata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + kiss_fft_s16_cpx fpnk, fpk, f1k, f2k, tw, tdc; + + if (st->substate->inverse) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + /*perform the parallel fft of two real signals packed in real,imag */ + kiss_fft_s16 (st->substate, (const kiss_fft_s16_cpx *) timedata, st->tmpbuf); + /* The real part of the DC element of the frequency spectrum in st->tmpbuf + * contains the sum of the even-numbered elements of the input time sequence + * The imag part is the sum of the odd-numbered elements + * + * The sum of tdc.r and tdc.i is the sum of the input time sequence. + * yielding DC of input time sequence + * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... + * yielding Nyquist bin of input time sequence + */ + + tdc.r = st->tmpbuf[0].r; + tdc.i = st->tmpbuf[0].i; + C_FIXDIV (tdc, 2); + CHECK_OVERFLOW_OP (tdc.r, +, tdc.i); + CHECK_OVERFLOW_OP (tdc.r, -, tdc.i); + freqdata[0].r = tdc.r + tdc.i; + freqdata[ncfft].r = tdc.r - tdc.i; +#ifdef USE_SIMD + freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps (0); +#else + freqdata[ncfft].i = freqdata[0].i = 0; +#endif + + for (k = 1; k <= ncfft / 2; ++k) { + fpk = st->tmpbuf[k]; + fpnk.r = st->tmpbuf[ncfft - k].r; + fpnk.i = -st->tmpbuf[ncfft - k].i; + C_FIXDIV (fpk, 2); + C_FIXDIV (fpnk, 2); + + C_ADD (f1k, fpk, fpnk); + C_SUB (f2k, fpk, fpnk); + C_MUL (tw, f2k, st->super_twiddles[k]); + + freqdata[k].r = HALF_OF (f1k.r + tw.r); + freqdata[k].i = HALF_OF (f1k.i + tw.i); + freqdata[ncfft - k].r = HALF_OF (f1k.r - tw.r); + freqdata[ncfft - k].i = HALF_OF (tw.i - f1k.i); + } +} + +void +kiss_fftri_s16 (kiss_fftr_s16_cfg st, const kiss_fft_s16_cpx * freqdata, + kiss_fft_s16_scalar * timedata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + + if (st->substate->inverse == 0) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; + st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; + C_FIXDIV (st->tmpbuf[0], 2); + + for (k = 1; k <= ncfft / 2; ++k) { + kiss_fft_s16_cpx fk, fnkc, fek, fok, tmp; + + fk = freqdata[k]; + fnkc.r = freqdata[ncfft - k].r; + fnkc.i = -freqdata[ncfft - k].i; + C_FIXDIV (fk, 2); + C_FIXDIV (fnkc, 2); + + C_ADD (fek, fk, fnkc); + C_SUB (tmp, fk, fnkc); + C_MUL (fok, tmp, st->super_twiddles[k]); + C_ADD (st->tmpbuf[k], fek, fok); + C_SUB (st->tmpbuf[ncfft - k], fek, fok); +#ifdef USE_SIMD + st->tmpbuf[ncfft - k].i *= _mm_set1_ps (-1.0); +#else + st->tmpbuf[ncfft - k].i *= -1; +#endif + } + kiss_fft_s16 (st->substate, st->tmpbuf, (kiss_fft_s16_cpx *) timedata); +} diff --git a/gst-libs/gst/fft/kiss_fftr_s16.h b/gst-libs/gst/fft/kiss_fftr_s16.h new file mode 100644 index 000000000..20bc9ef57 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fftr_s16.h @@ -0,0 +1,46 @@ +#ifndef KISS_FTR_S16_H +#define KISS_FTR_S16_H + +#include "kiss_fft_s16.h" +#ifdef __cplusplus +extern "C" { +#endif + + +/* + + Real optimized version can save about 45% cpu time vs. complex fft of a real seq. + + + + */ + +typedef struct kiss_fftr_s16_state *kiss_fftr_s16_cfg; + + +kiss_fftr_s16_cfg kiss_fftr_s16_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); +/* + nfft must be even + + If you don't care to allocate space, use mem = lenmem = NULL +*/ + + +void kiss_fftr_s16(kiss_fftr_s16_cfg cfg,const kiss_fft_s16_scalar *timedata,kiss_fft_s16_cpx *freqdata); +/* + input timedata has nfft scalar points + output freqdata has nfft/2+1 complex points +*/ + +void kiss_fftri_s16(kiss_fftr_s16_cfg cfg,const kiss_fft_s16_cpx *freqdata,kiss_fft_s16_scalar *timedata); +/* + input freqdata has nfft/2+1 complex points + output timedata has nfft scalar points +*/ + +#define kiss_fftr_s16_free free + +#ifdef __cplusplus +} +#endif +#endif diff --git a/gst-libs/gst/fft/kiss_fftr_s32.c b/gst-libs/gst/fft/kiss_fftr_s32.c new file mode 100644 index 000000000..47dc18703 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fftr_s32.c @@ -0,0 +1,168 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "kiss_fftr_s32.h" +#include "_kiss_fft_guts_s32.h" + +struct kiss_fftr_s32_state +{ + kiss_fft_s32_cfg substate; + kiss_fft_s32_cpx *tmpbuf; + kiss_fft_s32_cpx *super_twiddles; +#ifdef USE_SIMD + long pad; +#endif +}; + +kiss_fftr_s32_cfg +kiss_fftr_s32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem) +{ + int i; + kiss_fftr_s32_cfg st = NULL; + size_t subsize, memneeded; + + if (nfft & 1) { + fprintf (stderr, "Real FFT optimization must be even.\n"); + return NULL; + } + nfft >>= 1; + + kiss_fft_s32_alloc (nfft, inverse_fft, NULL, &subsize); + memneeded = + sizeof (struct kiss_fftr_s32_state) + subsize + + sizeof (kiss_fft_s32_cpx) * (nfft * 2); + + if (lenmem == NULL) { + st = (kiss_fftr_s32_cfg) KISS_FFT_S32_MALLOC (memneeded); + } else { + if (*lenmem >= memneeded) + st = (kiss_fftr_s32_cfg) mem; + *lenmem = memneeded; + } + if (!st) + return NULL; + + st->substate = (kiss_fft_s32_cfg) (st + 1); /*just beyond kiss_fftr_s32_state struct */ + st->tmpbuf = (kiss_fft_s32_cpx *) (((char *) st->substate) + subsize); + st->super_twiddles = st->tmpbuf + nfft; + kiss_fft_s32_alloc (nfft, inverse_fft, st->substate, &subsize); + + for (i = 0; i < nfft; ++i) { + double phase = -3.14159265358979323846264338327 * ((double) i / nfft + .5); + + if (inverse_fft) + phase *= -1; + kf_cexp (st->super_twiddles + i, phase); + } + return st; +} + +void +kiss_fftr_s32 (kiss_fftr_s32_cfg st, const kiss_fft_s32_scalar * timedata, + kiss_fft_s32_cpx * freqdata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + kiss_fft_s32_cpx fpnk, fpk, f1k, f2k, tw, tdc; + + if (st->substate->inverse) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + /*perform the parallel fft of two real signals packed in real,imag */ + kiss_fft_s32 (st->substate, (const kiss_fft_s32_cpx *) timedata, st->tmpbuf); + /* The real part of the DC element of the frequency spectrum in st->tmpbuf + * contains the sum of the even-numbered elements of the input time sequence + * The imag part is the sum of the odd-numbered elements + * + * The sum of tdc.r and tdc.i is the sum of the input time sequence. + * yielding DC of input time sequence + * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... + * yielding Nyquist bin of input time sequence + */ + + tdc.r = st->tmpbuf[0].r; + tdc.i = st->tmpbuf[0].i; + C_FIXDIV (tdc, 2); + CHECK_OVERFLOW_OP (tdc.r, +, tdc.i); + CHECK_OVERFLOW_OP (tdc.r, -, tdc.i); + freqdata[0].r = tdc.r + tdc.i; + freqdata[ncfft].r = tdc.r - tdc.i; +#ifdef USE_SIMD + freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps (0); +#else + freqdata[ncfft].i = freqdata[0].i = 0; +#endif + + for (k = 1; k <= ncfft / 2; ++k) { + fpk = st->tmpbuf[k]; + fpnk.r = st->tmpbuf[ncfft - k].r; + fpnk.i = -st->tmpbuf[ncfft - k].i; + C_FIXDIV (fpk, 2); + C_FIXDIV (fpnk, 2); + + C_ADD (f1k, fpk, fpnk); + C_SUB (f2k, fpk, fpnk); + C_MUL (tw, f2k, st->super_twiddles[k]); + + freqdata[k].r = HALF_OF (f1k.r + tw.r); + freqdata[k].i = HALF_OF (f1k.i + tw.i); + freqdata[ncfft - k].r = HALF_OF (f1k.r - tw.r); + freqdata[ncfft - k].i = HALF_OF (tw.i - f1k.i); + } +} + +void +kiss_fftri_s32 (kiss_fftr_s32_cfg st, const kiss_fft_s32_cpx * freqdata, + kiss_fft_s32_scalar * timedata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + + if (st->substate->inverse == 0) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; + st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; + C_FIXDIV (st->tmpbuf[0], 2); + + for (k = 1; k <= ncfft / 2; ++k) { + kiss_fft_s32_cpx fk, fnkc, fek, fok, tmp; + + fk = freqdata[k]; + fnkc.r = freqdata[ncfft - k].r; + fnkc.i = -freqdata[ncfft - k].i; + C_FIXDIV (fk, 2); + C_FIXDIV (fnkc, 2); + + C_ADD (fek, fk, fnkc); + C_SUB (tmp, fk, fnkc); + C_MUL (fok, tmp, st->super_twiddles[k]); + C_ADD (st->tmpbuf[k], fek, fok); + C_SUB (st->tmpbuf[ncfft - k], fek, fok); +#ifdef USE_SIMD + st->tmpbuf[ncfft - k].i *= _mm_set1_ps (-1.0); +#else + st->tmpbuf[ncfft - k].i *= -1; +#endif + } + kiss_fft_s32 (st->substate, st->tmpbuf, (kiss_fft_s32_cpx *) timedata); +} diff --git a/gst-libs/gst/fft/kiss_fftr_s32.h b/gst-libs/gst/fft/kiss_fftr_s32.h new file mode 100644 index 000000000..8e8fecb13 --- /dev/null +++ b/gst-libs/gst/fft/kiss_fftr_s32.h @@ -0,0 +1,46 @@ +#ifndef KISS_FTR_S32_H +#define KISS_FTR_S32_H + +#include "kiss_fft_s32.h" +#ifdef __cplusplus +extern "C" { +#endif + + +/* + + Real optimized version can save about 45% cpu time vs. complex fft of a real seq. + + + + */ + +typedef struct kiss_fftr_s32_state *kiss_fftr_s32_cfg; + + +kiss_fftr_s32_cfg kiss_fftr_s32_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); +/* + nfft must be even + + If you don't care to allocate space, use mem = lenmem = NULL +*/ + + +void kiss_fftr_s32(kiss_fftr_s32_cfg cfg,const kiss_fft_s32_scalar *timedata,kiss_fft_s32_cpx *freqdata); +/* + input timedata has nfft scalar points + output freqdata has nfft/2+1 complex points +*/ + +void kiss_fftri_s32(kiss_fftr_s32_cfg cfg,const kiss_fft_s32_cpx *freqdata,kiss_fft_s32_scalar *timedata); +/* + input freqdata has nfft/2+1 complex points + output timedata has nfft scalar points +*/ + +#define kiss_fftr_s32_free free + +#ifdef __cplusplus +} +#endif +#endif diff --git a/gst-libs/gst/fft/kiss_version b/gst-libs/gst/fft/kiss_version new file mode 100644 index 000000000..c55188bb2 --- /dev/null +++ b/gst-libs/gst/fft/kiss_version @@ -0,0 +1,4 @@ +Based on Kiss FFT version 1.2.6. http://kissfft.sourceforge.net + +Only changes are limitation to one data type for each version in +kiss_fft.h and _kiss_fft_guts.h. diff --git a/pkgconfig/gstreamer-plugins-base-uninstalled.pc.in b/pkgconfig/gstreamer-plugins-base-uninstalled.pc.in index 2431d4e24..815040033 100644 --- a/pkgconfig/gstreamer-plugins-base-uninstalled.pc.in +++ b/pkgconfig/gstreamer-plugins-base-uninstalled.pc.in @@ -9,7 +9,7 @@ Description: Streaming media framework, base plugins libraries, uninstalled Version: @VERSION@ Requires: gstreamer-@GST_MAJORMINOR@ -Libs: -L${libdir}/audio -L${libdir}/cdda -L${libdir}/floatcast -L${libdir}/interfaces -L${libdir}/netbuffer -L${libdir}/riff -L${libdir}/rtp -L${libdir}/rtsp -L${libdir}/sdp -L${libdir}/tag -L${libdir}/pbutils -L${libdir}/video +Libs: -L${libdir}/audio -L${libdir}/cdda -L${libdir}/fft -L${libdir}/floatcast -L${libdir}/interfaces -L${libdir}/netbuffer -L${libdir}/riff -L${libdir}/rtp -L${libdir}/rtsp -L${libdir}/sdp -L${libdir}/tag -L${libdir}/pbutils -L${libdir}/video Cflags: -I${includedir} -libraries=audio cdda floatcast interfaces netbuffer riff tag pbutils video +libraries=audio cdda fft floatcast interfaces netbuffer riff tag pbutils video diff --git a/pkgconfig/gstreamer-plugins-base.pc.in b/pkgconfig/gstreamer-plugins-base.pc.in index 397681d84..4bc6088c2 100644 --- a/pkgconfig/gstreamer-plugins-base.pc.in +++ b/pkgconfig/gstreamer-plugins-base.pc.in @@ -11,4 +11,4 @@ Version: @VERSION@ Libs: -L${libdir} Cflags: -I${includedir} -libraries=audio cdda floatcast interfaces netbuffer riff rtp tag pbutils video +libraries=audio cdda fft floatcast interfaces netbuffer riff rtp tag pbutils video diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 2b457deeb..b4e7e8cc0 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -89,6 +89,7 @@ check_PROGRAMS = \ gst/typefindfunctions \ libs/audio \ libs/cddabasesrc \ + libs/fft \ libs/mixer \ libs/netbuffer \ libs/pbutils \ @@ -139,6 +140,16 @@ libs_cddabasesrc_LDADD = \ $(GST_BASE_LIBS) \ $(LDADD) +libs_fft_CFLAGS = \ + $(GST_PLUGINS_BASE_CFLAGS) \ + $(GST_BASE_CFLAGS) \ + $(AM_CFLAGS) + +libs_fft_LDADD = \ + $(top_builddir)/gst-libs/gst/fft/libgstfft-@GST_MAJORMINOR@.la \ + $(GST_BASE_LIBS) \ + $(LDADD) + libs_mixer_CFLAGS = \ $(GST_PLUGINS_BASE_CFLAGS) \ $(GST_BASE_CFLAGS) \ @@ -249,6 +260,10 @@ libs_video_LDADD = \ $(GST_BASE_LIBS) \ $(LDADD) +pipelines_vorbisenc_CFLAGS = \ + $(GST_PLUGINS_BASE_CFLAGS) \ + $(AM_CFLAGS) + # this seemingly useless CFLAGS line is here only to avoid # vorbisdec.$(OBJEXT) by triggering creation of pipelines_vorbisdec.$(OBJEXT) # instead diff --git a/tests/check/libs/fft.c b/tests/check/libs/fft.c new file mode 100644 index 000000000..3a30111d4 --- /dev/null +++ b/tests/check/libs/fft.c @@ -0,0 +1,504 @@ +/* GStreamer + * + * unit test for FFT library + * + * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/check/gstcheck.h> + +#include <gst/fft/gstfft.h> +#include <gst/fft/gstffts16.h> +#include <gst/fft/gstffts32.h> +#include <gst/fft/gstfftf32.h> +#include <gst/fft/gstfftf64.h> + +GST_START_TEST (test_next_fast_length) +{ + fail_unless_equals_int (gst_fft_next_fast_length (13), 16); + fail_unless_equals_int (gst_fft_next_fast_length (30), 30); + fail_unless_equals_int (gst_fft_next_fast_length (31), 32); + fail_unless_equals_int (gst_fft_next_fast_length (1), 2); +} + +GST_END_TEST; + +GST_START_TEST (test_s16_0hz) +{ + gint i; + gint16 *in; + GstFFTS16Complex *out; + gdouble *mag; + GstFFTS16 *ctx; + + in = g_new (gint16, 2048); + out = g_new (GstFFTS16Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_s16_new (2048, FALSE); + + for (i = 0; i < 2048; i++) + in[i] = G_MAXINT16; + + gst_fft_s16_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_s16_fft (ctx, in, out); + gst_fft_s16_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (i < 2 && mag[i] < -15.0); + fail_if (i >= 2 && mag[i] > -60.0); + } + + gst_fft_s16_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_s16_11025hz) +{ + gint i; + gint16 *in; + GstFFTS16Complex *out; + gdouble *mag; + GstFFTS16 *ctx; + + in = g_new (gint16, 2048); + out = g_new (GstFFTS16Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_s16_new (2048, FALSE); + + for (i = 0; i < 2048; i += 4) { + in[i] = 0; + in[i + 1] = G_MAXINT16; + in[i + 2] = 0; + in[i + 3] = G_MININT16; + } + + gst_fft_s16_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_s16_fft (ctx, in, out); + gst_fft_s16_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (abs (512 - i) < 2 && mag[i] < -20.0); + fail_if (abs (512 - i) >= 2 && mag[i] > -60.0); + } + + gst_fft_s16_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_s16_22050hz) +{ + gint i; + gint16 *in; + GstFFTS16Complex *out; + gdouble *mag; + GstFFTS16 *ctx; + + in = g_new (gint16, 2048); + out = g_new (GstFFTS16Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_s16_new (2048, FALSE); + + for (i = 0; i < 2048; i += 2) { + in[i] = G_MAXINT16; + in[i + 1] = G_MININT16; + } + + gst_fft_s16_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_s16_fft (ctx, in, out); + gst_fft_s16_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (i > 1022 && mag[i] < -20.0); + fail_if (i <= 1022 && mag[i] > -60.0); + } + + gst_fft_s16_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_s32_0hz) +{ + gint i; + gint32 *in; + GstFFTS32Complex *out; + gdouble *mag; + GstFFTS32 *ctx; + + in = g_new (gint32, 2048); + out = g_new (GstFFTS32Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_s32_new (2048, FALSE); + + for (i = 0; i < 2048; i++) + in[i] = 2147483647; + + gst_fft_s32_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_s32_fft (ctx, in, out); + gst_fft_s32_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (i < 2 && mag[i] < -15.0); + fail_if (i >= 2 && mag[i] > -60.0); + } + + gst_fft_s32_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_s32_11025hz) +{ + gint i; + gint32 *in; + GstFFTS32Complex *out; + gdouble *mag; + GstFFTS32 *ctx; + + in = g_new (gint32, 2048); + out = g_new (GstFFTS32Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_s32_new (2048, FALSE); + + for (i = 0; i < 2048; i += 4) { + in[i] = 0; + in[i + 1] = G_MAXINT32; + in[i + 2] = 0; + in[i + 3] = G_MININT32; + } + + gst_fft_s32_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_s32_fft (ctx, in, out); + gst_fft_s32_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (abs (512 - i) < 2 && mag[i] < -20.0); + fail_if (abs (512 - i) >= 2 && mag[i] > -60.0); + } + + gst_fft_s32_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_s32_22050hz) +{ + gint i; + gint32 *in; + GstFFTS32Complex *out; + gdouble *mag; + GstFFTS32 *ctx; + + in = g_new (gint32, 2048); + out = g_new (GstFFTS32Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_s32_new (2048, FALSE); + + for (i = 0; i < 2048; i += 2) { + in[i] = G_MAXINT32; + in[i + 1] = G_MININT32; + } + + gst_fft_s32_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_s32_fft (ctx, in, out); + gst_fft_s32_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (i > 1022 && mag[i] < -20.0); + fail_if (i <= 1022 && mag[i] > -60.0); + } + + gst_fft_s32_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_f32_0hz) +{ + gint i; + gfloat *in; + GstFFTF32Complex *out; + gdouble *mag; + GstFFTF32 *ctx; + + in = g_new (gfloat, 2048); + out = g_new (GstFFTF32Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_f32_new (2048, FALSE); + + for (i = 0; i < 2048; i++) + in[i] = 1.0; + + gst_fft_f32_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_f32_fft (ctx, in, out); + gst_fft_f32_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (i < 2 && mag[i] < -15.0); + fail_if (i >= 2 && mag[i] > -60.0); + } + + gst_fft_f32_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_f32_11025hz) +{ + gint i; + gfloat *in; + GstFFTF32Complex *out; + gdouble *mag; + GstFFTF32 *ctx; + + in = g_new (gfloat, 2048); + out = g_new (GstFFTF32Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_f32_new (2048, FALSE); + + for (i = 0; i < 2048; i += 4) { + in[i] = 0.0; + in[i + 1] = 1.0; + in[i + 2] = 0.0; + in[i + 3] = -1.0; + } + + gst_fft_f32_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_f32_fft (ctx, in, out); + gst_fft_f32_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (abs (512 - i) < 2 && mag[i] < -20.0); + fail_if (abs (512 - i) >= 2 && mag[i] > -60.0); + } + + gst_fft_f32_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_f32_22050hz) +{ + gint i; + gfloat *in; + GstFFTF32Complex *out; + gdouble *mag; + GstFFTF32 *ctx; + + in = g_new (gfloat, 2048); + out = g_new (GstFFTF32Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_f32_new (2048, FALSE); + + for (i = 0; i < 2048; i += 2) { + in[i] = 1.0; + in[i + 1] = -1.0; + } + + gst_fft_f32_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_f32_fft (ctx, in, out); + gst_fft_f32_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (i > 1022 && mag[i] < -20.0); + fail_if (i <= 1022 && mag[i] > -60.0); + } + + gst_fft_f32_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_f64_0hz) +{ + gint i; + gdouble *in; + GstFFTF64Complex *out; + gdouble *mag; + GstFFTF64 *ctx; + + in = g_new (gdouble, 2048); + out = g_new (GstFFTF64Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_f64_new (2048, FALSE); + + for (i = 0; i < 2048; i++) + in[i] = 1.0; + + gst_fft_f64_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_f64_fft (ctx, in, out); + gst_fft_f64_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (i < 2 && mag[i] < -15.0); + fail_if (i >= 2 && mag[i] > -60.0); + } + + gst_fft_f64_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_f64_11025hz) +{ + gint i; + gdouble *in; + GstFFTF64Complex *out; + gdouble *mag; + GstFFTF64 *ctx; + + in = g_new (gdouble, 2048); + out = g_new (GstFFTF64Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_f64_new (2048, FALSE); + + for (i = 0; i < 2048; i += 4) { + in[i] = 0.0; + in[i + 1] = 1.0; + in[i + 2] = 0.0; + in[i + 3] = -1.0; + } + + gst_fft_f64_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_f64_fft (ctx, in, out); + gst_fft_f64_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (abs (512 - i) < 2 && mag[i] < -20.0); + fail_if (abs (512 - i) >= 2 && mag[i] > -60.0); + } + + gst_fft_f64_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +GST_START_TEST (test_f64_22050hz) +{ + gint i; + gdouble *in; + GstFFTF64Complex *out; + gdouble *mag; + GstFFTF64 *ctx; + + in = g_new (gdouble, 2048); + out = g_new (GstFFTF64Complex, 1025); + mag = g_new (gdouble, 1025); + ctx = gst_fft_f64_new (2048, FALSE); + + for (i = 0; i < 2048; i += 2) { + in[i] = 1.0; + in[i + 1] = -1.0; + } + + gst_fft_f64_window (ctx, in, GST_FFT_WINDOW_HAMMING); + gst_fft_f64_fft (ctx, in, out); + gst_fft_f64_magnitude (ctx, out, mag, TRUE); + + for (i = 0; i < 1025; i++) { + fail_if (i > 1022 && mag[i] < -20.0); + fail_if (i <= 1022 && mag[i] > -60.0); + } + + gst_fft_f64_free (ctx); + g_free (in); + g_free (out); + g_free (mag); +} + +GST_END_TEST; + +static Suite * +fft_suite (void) +{ + Suite *s = suite_create ("fft library"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_next_fast_length); + tcase_add_test (tc_chain, test_s16_0hz); + tcase_add_test (tc_chain, test_s16_11025hz); + tcase_add_test (tc_chain, test_s16_22050hz); + tcase_add_test (tc_chain, test_s32_0hz); + tcase_add_test (tc_chain, test_s32_11025hz); + tcase_add_test (tc_chain, test_s32_22050hz); + tcase_add_test (tc_chain, test_f32_0hz); + tcase_add_test (tc_chain, test_f32_11025hz); + tcase_add_test (tc_chain, test_f32_22050hz); + tcase_add_test (tc_chain, test_f64_0hz); + tcase_add_test (tc_chain, test_f64_11025hz); + tcase_add_test (tc_chain, test_f64_22050hz); + + return s; +} + +int +main (int argc, char **argv) +{ + int nf; + + Suite *s = fft_suite (); + SRunner *sr = srunner_create (s); + + gst_check_init (&argc, &argv); + + srunner_run_all (sr, CK_NORMAL); + nf = srunner_ntests_failed (sr); + srunner_free (sr); + + return nf; +} |