From 5dfcb63252fa9cd6e2ffe2049848cffd357a8ede Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 23 Jan 2009 12:31:06 +0100 Subject: [PATCH] Rename files and types from speexresample to audioresample Rename files and types from speexresample to audioresample to finish the move and to prevent any confusion. --- configure.ac | 4 +- docs/plugins/Makefile.am | 2 +- docs/plugins/gst-plugins-base-plugins-sections.txt | 18 +- docs/plugins/gst-plugins-base-plugins.args | 66 +- docs/plugins/gst-plugins-base-plugins.hierarchy | 132 +- docs/plugins/gst-plugins-base-plugins.interfaces | 24 +- .../plugins/gst-plugins-base-plugins.prerequisites | 7 +- docs/plugins/inspect/plugin-adder.xml | 4 +- docs/plugins/inspect/plugin-alsa.xml | 4 +- docs/plugins/inspect/plugin-app.xml | 4 +- docs/plugins/inspect/plugin-audioconvert.xml | 4 +- docs/plugins/inspect/plugin-audiorate.xml | 4 +- docs/plugins/inspect/plugin-audioresample.xml | 6 +- docs/plugins/inspect/plugin-audiotestsrc.xml | 4 +- docs/plugins/inspect/plugin-cdparanoia.xml | 4 +- docs/plugins/inspect/plugin-decodebin.xml | 4 +- docs/plugins/inspect/plugin-ffmpegcolorspace.xml | 2 +- docs/plugins/inspect/plugin-gdp.xml | 4 +- docs/plugins/inspect/plugin-gio.xml | 2 +- docs/plugins/inspect/plugin-gnomevfs.xml | 4 +- docs/plugins/inspect/plugin-libvisual.xml | 4 +- docs/plugins/inspect/plugin-ogg.xml | 4 +- docs/plugins/inspect/plugin-pango.xml | 4 +- docs/plugins/inspect/plugin-playback.xml | 4 +- docs/plugins/inspect/plugin-queue2.xml | 4 +- docs/plugins/inspect/plugin-subparse.xml | 4 +- docs/plugins/inspect/plugin-tcp.xml | 4 +- docs/plugins/inspect/plugin-theora.xml | 4 +- docs/plugins/inspect/plugin-typefindfunctions.xml | 4 +- docs/plugins/inspect/plugin-uridecodebin.xml | 4 +- docs/plugins/inspect/plugin-video4linux.xml | 4 +- docs/plugins/inspect/plugin-videorate.xml | 4 +- docs/plugins/inspect/plugin-videoscale.xml | 4 +- docs/plugins/inspect/plugin-videotestsrc.xml | 4 +- docs/plugins/inspect/plugin-volume.xml | 4 +- docs/plugins/inspect/plugin-vorbis.xml | 4 +- docs/plugins/inspect/plugin-ximagesink.xml | 4 +- docs/plugins/inspect/plugin-xvimagesink.xml | 4 +- gst/audioresample/Makefile.am | 46 +- gst/{speexresample => audioresample}/README | 0 gst/{speexresample => audioresample}/arch.h | 0 gst/audioresample/buffer.c | 253 ---- gst/audioresample/buffer.h | 51 - gst/audioresample/debug.c | 65 - gst/audioresample/debug.h | 51 - gst/{speexresample => audioresample}/fixed_arm4.h | 0 gst/{speexresample => audioresample}/fixed_arm5e.h | 0 gst/{speexresample => audioresample}/fixed_bfin.h | 0 gst/{speexresample => audioresample}/fixed_debug.h | 0 .../fixed_generic.h | 0 gst/audioresample/functable.c | 254 ---- gst/audioresample/functable.h | 61 - gst/audioresample/gstaudioresample.c | 1415 +++++++++++++------ gst/audioresample/gstaudioresample.h | 73 +- gst/audioresample/resample.c | 1441 +++++++++++++++++--- gst/audioresample/resample.h | 128 -- gst/audioresample/resample_chunk.c | 209 --- gst/audioresample/resample_functable.c | 271 ---- gst/audioresample/resample_ref.c | 223 --- .../resample_sse.h | 0 .../speex_resampler.h | 0 .../speex_resampler_double.c | 0 .../speex_resampler_float.c | 0 .../speex_resampler_int.c | 0 .../speex_resampler_wrapper.h | 0 gst/speexresample/Makefile.am | 37 - gst/speexresample/gstspeexresample.c | 1404 ------------------- gst/speexresample/gstspeexresample.h | 90 -- gst/speexresample/resample.c | 1340 ------------------ gst/typefind/gsttypefindfunctions.c | 35 +- tests/check/Makefile.am | 6 +- tests/check/elements/audioresample.c | 158 ++- tests/check/elements/speexresample.c | 700 ---------- 73 files changed, 2685 insertions(+), 6001 deletions(-) rename gst/{speexresample => audioresample}/README (100%) rename gst/{speexresample => audioresample}/arch.h (100%) delete mode 100644 gst/audioresample/buffer.c delete mode 100644 gst/audioresample/buffer.h delete mode 100644 gst/audioresample/debug.c delete mode 100644 gst/audioresample/debug.h rename gst/{speexresample => audioresample}/fixed_arm4.h (100%) rename gst/{speexresample => audioresample}/fixed_arm5e.h (100%) rename gst/{speexresample => audioresample}/fixed_bfin.h (100%) rename gst/{speexresample => audioresample}/fixed_debug.h (100%) rename gst/{speexresample => audioresample}/fixed_generic.h (100%) delete mode 100644 gst/audioresample/functable.c delete mode 100644 gst/audioresample/functable.h delete mode 100644 gst/audioresample/resample.h delete mode 100644 gst/audioresample/resample_chunk.c delete mode 100644 gst/audioresample/resample_functable.c delete mode 100644 gst/audioresample/resample_ref.c rename gst/{speexresample => audioresample}/resample_sse.h (100%) rename gst/{speexresample => audioresample}/speex_resampler.h (100%) rename gst/{speexresample => audioresample}/speex_resampler_double.c (100%) rename gst/{speexresample => audioresample}/speex_resampler_float.c (100%) rename gst/{speexresample => audioresample}/speex_resampler_int.c (100%) rename gst/{speexresample => audioresample}/speex_resampler_wrapper.h (100%) delete mode 100644 gst/speexresample/Makefile.am delete mode 100644 gst/speexresample/gstspeexresample.c delete mode 100644 gst/speexresample/gstspeexresample.h delete mode 100644 gst/speexresample/resample.c delete mode 100644 tests/check/elements/speexresample.c diff --git a/configure.ac b/configure.ac index 8ea9889..970ad89 100644 --- a/configure.ac +++ b/configure.ac @@ -305,7 +305,7 @@ AG_GST_CHECK_PLUGIN(audiotestsrc) AG_GST_CHECK_PLUGIN(ffmpegcolorspace) AG_GST_CHECK_PLUGIN(gdp) AG_GST_CHECK_PLUGIN(playback) -AG_GST_CHECK_PLUGIN(speexresample) +AG_GST_CHECK_PLUGIN(audioresample) AG_GST_CHECK_PLUGIN(subparse) AG_GST_CHECK_PLUGIN(tcp) AG_GST_CHECK_PLUGIN(typefind) @@ -690,7 +690,7 @@ gst/audiotestsrc/Makefile gst/ffmpegcolorspace/Makefile gst/gdp/Makefile gst/playback/Makefile -gst/speexresample/Makefile +gst/audioresample/Makefile gst/subparse/Makefile gst/tcp/Makefile gst/typefind/Makefile diff --git a/docs/plugins/Makefile.am b/docs/plugins/Makefile.am index 90f9410..f2e6272 100644 --- a/docs/plugins/Makefile.am +++ b/docs/plugins/Makefile.am @@ -106,7 +106,7 @@ EXTRA_HFILES = \ $(top_srcdir)/gst/gdp/gstgdpdepay.h \ $(top_srcdir)/gst/gdp/gstgdppay.h \ $(top_srcdir)/gst/playback/gstplay-enum.h \ - $(top_srcdir)/gst/speexresample/gstspeexresample.h \ + $(top_srcdir)/gst/audioresample/gstaudioresample.h \ $(top_srcdir)/gst/tcp/gstmultifdsink.h \ $(top_srcdir)/gst/tcp/gsttcpclientsrc.h \ $(top_srcdir)/gst/tcp/gsttcpclientsink.h \ diff --git a/docs/plugins/gst-plugins-base-plugins-sections.txt b/docs/plugins/gst-plugins-base-plugins-sections.txt index b3d87a4..e06d412 100644 --- a/docs/plugins/gst-plugins-base-plugins-sections.txt +++ b/docs/plugins/gst-plugins-base-plugins-sections.txt @@ -107,15 +107,15 @@ audio_convert_prepare_context
element-audioresample audioresample -GstSpeexResample - -GST_SPEEX_RESAMPLE -GST_IS_SPEEX_RESAMPLE -GST_TYPE_SPEEX_RESAMPLE -gst_speex_resample_get_type -GST_SPEEX_RESAMPLE_CLASS -GST_IS_SPEEX_RESAMPLE_CLASS -GstSpeexResampleClass +GstAudioResample + +GST_AUDIO_RESAMPLE +GST_IS_AUDIO_RESAMPLE +GST_TYPE_AUDIO_RESAMPLE +gst_audio_resample_get_type +GST_AUDIO_RESAMPLE_CLASS +GST_IS_AUDIO_RESAMPLE_CLASS +GstAudioResampleClass
diff --git a/docs/plugins/gst-plugins-base-plugins.args b/docs/plugins/gst-plugins-base-plugins.args index da48aa4..6904c85 100644 --- a/docs/plugins/gst-plugins-base-plugins.args +++ b/docs/plugins/gst-plugins-base-plugins.args @@ -641,7 +641,7 @@ GstMultiFdSink::buffers-max gint ->= -1 +>= G_MAXULONG rw Buffers max max number of buffers to queue for a client (-1 = no limit). @@ -661,7 +661,7 @@ GstMultiFdSink::buffers-soft-max gint ->= -1 +>= G_MAXULONG rw Buffers soft max Recover client when going over this limit (-1 = no limit). @@ -751,7 +751,7 @@ GstMultiFdSink::buffers-min gint ->= -1 +>= G_MAXULONG rw Buffers min min number of buffers to queue (-1 = as few as possible). @@ -781,7 +781,7 @@ GstMultiFdSink::bytes-min gint ->= -1 +>= G_MAXULONG rw Bytes min min number of bytes to queue (-1 = as little as possible). @@ -791,7 +791,7 @@ GstMultiFdSink::time-min gint64 ->= -1 +>= G_MAXULONG rw Time min min number of time to queue (-1 = as little as possible). @@ -811,7 +811,7 @@ GstMultiFdSink::units-max gint64 ->= -1 +>= G_MAXULONG rw Units max max number of units to queue (-1 = no limit). @@ -821,7 +821,7 @@ GstMultiFdSink::units-soft-max gint64 ->= -1 +>= G_MAXULONG rw Units soft max Recover client when going over this limit (-1 = no limit). @@ -831,7 +831,7 @@ GstMultiFdSink::qos-dscp gint -[-1,63] +[G_MAXULONG,63] rw QoS diff srv code point Quality of Service, differentiated services code point (-1 default). @@ -981,7 +981,7 @@ GstVorbisEnc::bitrate gint -[-1,250001] +[G_MAXULONG,250001] rw Target Bitrate Attempt to encode at a bitrate averaging this (in bps). This uses the bitrate management engine, and is not recommended for most users. Quality is a better alternative. (-1 == disabled). @@ -1011,7 +1011,7 @@ GstVorbisEnc::max-bitrate gint -[-1,250001] +[G_MAXULONG,250001] rw Maximum Bitrate Specify a maximum bitrate (in bps). Useful for streaming applications. (-1 == disabled). @@ -1021,7 +1021,7 @@ GstVorbisEnc::min-bitrate gint -[-1,250001] +[G_MAXULONG,250001] rw Minimum Bitrate Specify a minimum bitrate (in bps). Useful for encoding for a fixed-size channel. (-1 == disabled). @@ -1861,7 +1861,7 @@ GstCdParanoiaSrc::read-speed gint ->= -1 +>= G_MAXULONG rw Read speed Read from device at specified speed. @@ -1871,7 +1871,7 @@ GstCdParanoiaSrc::search-overlap gint -[-1,75] +[G_MAXULONG,75] rw Search overlap Force minimum overlap search during verification to n sectors. @@ -2011,7 +2011,7 @@ GstURIDecodeBin::buffer-duration gint64 ->= -1 +>= G_MAXULONG rw Buffer duration (ns) Buffer duration when buffering network streams. @@ -2021,7 +2021,7 @@ GstURIDecodeBin::buffer-size gint ->= -1 +>= G_MAXULONG rw Buffer size (bytes) Buffer size when buffering network streams. @@ -2181,7 +2181,7 @@ GstPlayBin2::current-audio gint ->= -1 +>= G_MAXULONG rw Current audio Currently playing audio stream (-1 = auto). @@ -2191,7 +2191,7 @@ GstPlayBin2::current-text gint ->= -1 +>= G_MAXULONG rw Current Text Currently playing text stream (-1 = auto). @@ -2201,7 +2201,7 @@ GstPlayBin2::current-video gint ->= -1 +>= G_MAXULONG rw Current Video Currently playing video stream (-1 = auto). @@ -2361,7 +2361,7 @@ GstPlayBin2::buffer-duration gint64 ->= -1 +>= G_MAXULONG rw Buffer duration (ns) Buffer duration when buffering network streams. @@ -2371,7 +2371,7 @@ GstPlayBin2::buffer-size gint ->= -1 +>= G_MAXULONG rw Buffer size (bytes) Buffer size when buffering network streams. @@ -2711,7 +2711,7 @@ GstAppSrc::max-latency gint64 ->= -1 +>= G_MAXULONG rw Max Latency The maximum latency (-1 = unlimited). @@ -2721,7 +2721,7 @@ GstAppSrc::min-latency gint64 ->= -1 +>= G_MAXULONG rw Min Latency The minimum latency (-1 = default). @@ -2731,7 +2731,7 @@ GstAppSrc::size gint64 ->= -1 +>= G_MAXULONG rw Size The size of the data stream in bytes (-1 if unknown). @@ -2748,3 +2748,23 @@ Stream + +GstAudioResample::filter-length +gint +>= 0 +rwx +Filter length +DEPRECATED, DON'T USE THIS! Length of the resample filter. +64 + + + +GstAudioResample::quality +gint +[0,10] +rwx +Quality +Resample quality with 0 being the lowest and 10 being the best. +4 + + diff --git a/docs/plugins/gst-plugins-base-plugins.hierarchy b/docs/plugins/gst-plugins-base-plugins.hierarchy index f464e81..efa8d7a 100644 --- a/docs/plugins/gst-plugins-base-plugins.hierarchy +++ b/docs/plugins/gst-plugins-base-plugins.hierarchy @@ -12,28 +12,9 @@ GObject GstPlayBaseBin GstPlayBin GstPlayBin2 + GstDecodeBin GstDecodeBin2 GstURIDecodeBin - GstDecodeBin - GstBaseSrc - GstPushSrc - GstCddaBaseSrc - GstCdParanoiaSrc - GstBaseAudioSrc - GstAudioSrc - GstAlsaSrc - GstV4lElement - GstV4lSrc - GstTCPClientSrc - GstTCPServerSrc - GstVideoTestSrc - GstGnomeVFSSrc - GstAppSrc - GstAudioTestSrc - GstVorbisEnc - GstVorbisDec - GstVorbisParse - GstVorbisTag GstOggDemux GstOggMux GstOgmParse @@ -43,49 +24,74 @@ GObject GstOggParse GstOggAviParse GstBaseSink - GstGnomeVFSSink + GstGioBaseSink + GstGioSink + GstGioStreamSink GstBaseAudioSink GstAudioSink GstAlsaSink + GstGnomeVFSSink GstVideoSink GstXvImageSink GstXImageSink + GstAppSink GstTCPClientSink GstMultiFdSink GstTCPServerSink - GstAppSink + GstBaseSrc + GstGioBaseSrc + GstGioSrc + GstGioStreamSrc + GstPushSrc + GstBaseAudioSrc + GstAudioSrc + GstAlsaSrc + GstCddaBaseSrc + GstCdParanoiaSrc + GstV4lElement + GstV4lSrc + GstTCPClientSrc + GstTCPServerSrc + GstVideoTestSrc + GstGnomeVFSSrc + GstAudioTestSrc + GstAppSrc + GstVorbisEnc + GstVorbisDec + GstVorbisParse + GstVorbisTag + GstTextOverlay + GstTimeOverlay + GstClockOverlay + GstTextRender + GstTheoraDec + GstTheoraEnc + GstTheoraParse + GstAlsaMixerElement GstVisual + GstVisualjess GstVisualbumpscope GstVisualcorona GstVisualinfinite GstVisualjakdaw - GstVisualjess GstVisuallv_analyzer GstVisuallv_scope GstVisualoinksie - GstTheoraDec - GstTheoraEnc - GstTheoraParse - GstTextOverlay - GstTimeOverlay - GstClockOverlay - GstTextRender - GstAlsaMixerElement - GstGDPDepay - GstGDPPay + GstSubParse + GstSsaParse + GstAudioRate GstBaseTransform + GstAudioConvert GstFFMpegCsp - GstVideoScale GstAudioFilter GstVolume - GstSpeexResample - GstAudioConvert - GstSubParse - GstSsaParse + GstAudioResample + GstVideoScale GstAdder + GstGDPDepay + GstGDPPay GstStreamSelector GstQueue2 - GstAudioRate GstVideoRate GstBus GstTask @@ -96,26 +102,62 @@ GObject GstRegistry GstRingBuffer GstSignalObject - GConfClient + GFileMonitor + GLocalDirectoryMonitor + GFamDirectoryMonitor + GInotifyDirectoryMonitor + GLocalFileMonitor + GFamFileMonitor + GInotifyFileMonitor + GVolumeMonitor + GNativeVolumeMonitor + GProxyVolumeMonitor + GProxyVolumeMonitorHal + GProxyVolumeMonitorGPhoto2 + GUnixVolumeMonitor + GDaemonVolumeMonitor + GVfs + GDaemonVfs + GLocalVfs + GTypeModule + GIOModule + GVfsUriMapper + GVfsUriMapperSmb + GVfsUriMapperHttp + GAppLookupGConf + GProxyDrive + GProxyMount + GProxyVolume + GOutputStream + GInputStream PangoFontMap PangoFcFontMap PangoFT2FontMap PangoContext - GstMixerTrack - GstMixerOptions + LinkConnection + GIOPConnection + LinkServer + GIOPServer + GConfClient + GstColorBalanceChannel GstTunerNorm GstTunerChannel - GstColorBalanceChannel + GstMixerTrack GstStreamInfo GInterface GTypePlugin GstChildProxy GstURIHandler + GFile + GDesktopAppInfoLookup + GDrive + GMount + GVolume GstTagSetter GstImplementsInterface GstMixer GstPropertyProbe - GstTuner + GstNavigation GstXOverlay GstColorBalance - GstNavigation + GstTuner diff --git a/docs/plugins/gst-plugins-base-plugins.interfaces b/docs/plugins/gst-plugins-base-plugins.interfaces index d7fbd47..7423f7d 100644 --- a/docs/plugins/gst-plugins-base-plugins.interfaces +++ b/docs/plugins/gst-plugins-base-plugins.interfaces @@ -3,21 +3,29 @@ GstPipeline GstChildProxy GstPlayBaseBin GstChildProxy GstPlayBin GstChildProxy GstPlayBin2 GstChildProxy +GstDecodeBin GstChildProxy GstDecodeBin2 GstChildProxy GstURIDecodeBin GstChildProxy -GstDecodeBin GstChildProxy +GstGioSink GstURIHandler +GstAlsaSink GstPropertyProbe +GstGnomeVFSSink GstURIHandler +GstXvImageSink GstImplementsInterface GstPropertyProbe GstNavigation GstXOverlay GstColorBalance +GstXImageSink GstImplementsInterface GstNavigation GstXOverlay +GstGioSrc GstURIHandler +GstAlsaSrc GstImplementsInterface GstMixer GstPropertyProbe GstCddaBaseSrc GstURIHandler GstCdParanoiaSrc GstURIHandler -GstAlsaSrc GstImplementsInterface GstMixer GstPropertyProbe -GstV4lElement GstImplementsInterface GstPropertyProbe GstTuner GstXOverlay GstColorBalance -GstV4lSrc GstImplementsInterface GstPropertyProbe GstTuner GstXOverlay GstColorBalance +GstV4lElement GstImplementsInterface GstPropertyProbe GstXOverlay GstColorBalance GstTuner +GstV4lSrc GstImplementsInterface GstPropertyProbe GstXOverlay GstColorBalance GstTuner GstGnomeVFSSrc GstURIHandler GstAppSrc GstURIHandler GstVorbisEnc GstTagSetter GstVorbisTag GstTagSetter -GstGnomeVFSSink GstURIHandler -GstAlsaSink GstPropertyProbe -GstXvImageSink GstImplementsInterface GstPropertyProbe GstXOverlay GstColorBalance GstNavigation -GstXImageSink GstImplementsInterface GstXOverlay GstNavigation GstAlsaMixerElement GstImplementsInterface GstMixer GstPropertyProbe GstVolume GstImplementsInterface GstMixer +GTypeModule GTypePlugin +GIOModule GTypePlugin +GAppLookupGConf GDesktopAppInfoLookup +GProxyDrive GDrive +GProxyMount GMount +GProxyVolume GVolume diff --git a/docs/plugins/gst-plugins-base-plugins.prerequisites b/docs/plugins/gst-plugins-base-plugins.prerequisites index 3883426..cb7d34f 100644 --- a/docs/plugins/gst-plugins-base-plugins.prerequisites +++ b/docs/plugins/gst-plugins-base-plugins.prerequisites @@ -1,7 +1,12 @@ GstChildProxy GstObject +GFile GObject +GDesktopAppInfoLookup GObject +GDrive GObject +GMount GObject +GVolume GObject GstTagSetter GstObject GstElement GstImplementsInterface GstObject GstElement GstMixer GstObject GstImplementsInterface GstElement -GstTuner GstObject GstImplementsInterface GstElement GstXOverlay GstObject GstImplementsInterface GstElement GstColorBalance GstObject GstImplementsInterface GstElement +GstTuner GstObject GstImplementsInterface GstElement diff --git a/docs/plugins/inspect/plugin-adder.xml b/docs/plugins/inspect/plugin-adder.xml index aa18856..5f9bcc3 100644 --- a/docs/plugins/inspect/plugin-adder.xml +++ b/docs/plugins/inspect/plugin-adder.xml @@ -3,10 +3,10 @@ Adds multiple streams ../../gst/adder/.libs/libgstadder.so libgstadder.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-alsa.xml b/docs/plugins/inspect/plugin-alsa.xml index aae8872..d799053 100644 --- a/docs/plugins/inspect/plugin-alsa.xml +++ b/docs/plugins/inspect/plugin-alsa.xml @@ -3,10 +3,10 @@ ALSA plugin library ../../ext/alsa/.libs/libgstalsa.so libgstalsa.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-app.xml b/docs/plugins/inspect/plugin-app.xml index 2ef1335..938c329 100644 --- a/docs/plugins/inspect/plugin-app.xml +++ b/docs/plugins/inspect/plugin-app.xml @@ -3,10 +3,10 @@ Elements used to communicate with applications ../../gst/app/.libs/libgstapp.so libgstapp.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-audioconvert.xml b/docs/plugins/inspect/plugin-audioconvert.xml index c8e77c2..f023535 100644 --- a/docs/plugins/inspect/plugin-audioconvert.xml +++ b/docs/plugins/inspect/plugin-audioconvert.xml @@ -3,10 +3,10 @@ Convert audio to different formats ../../gst/audioconvert/.libs/libgstaudioconvert.so libgstaudioconvert.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-audiorate.xml b/docs/plugins/inspect/plugin-audiorate.xml index b275604..dab7695 100644 --- a/docs/plugins/inspect/plugin-audiorate.xml +++ b/docs/plugins/inspect/plugin-audiorate.xml @@ -3,10 +3,10 @@ Adjusts audio frames ../../gst/audiorate/.libs/libgstaudiorate.so libgstaudiorate.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-audioresample.xml b/docs/plugins/inspect/plugin-audioresample.xml index 25ddf2d..da6ce84 100644 --- a/docs/plugins/inspect/plugin-audioresample.xml +++ b/docs/plugins/inspect/plugin-audioresample.xml @@ -1,12 +1,12 @@ audioresample Resamples audio - ../../gst/speexresample/.libs/libgstaudioresample.so + ../../gst/audioresample/.libs/libgstaudioresample.so libgstaudioresample.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-audiotestsrc.xml b/docs/plugins/inspect/plugin-audiotestsrc.xml index aa88472..201ac03 100644 --- a/docs/plugins/inspect/plugin-audiotestsrc.xml +++ b/docs/plugins/inspect/plugin-audiotestsrc.xml @@ -3,10 +3,10 @@ Creates audio test signals of given frequency and volume ../../gst/audiotestsrc/.libs/libgstaudiotestsrc.so libgstaudiotestsrc.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-cdparanoia.xml b/docs/plugins/inspect/plugin-cdparanoia.xml index 18b990c..3f5c789 100644 --- a/docs/plugins/inspect/plugin-cdparanoia.xml +++ b/docs/plugins/inspect/plugin-cdparanoia.xml @@ -3,10 +3,10 @@ Read audio from CD in paranoid mode ../../ext/cdparanoia/.libs/libgstcdparanoia.so libgstcdparanoia.so - 0.10.22 + 0.10.22.1 GPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-decodebin.xml b/docs/plugins/inspect/plugin-decodebin.xml index 8c6bf5f..7e52a1b 100644 --- a/docs/plugins/inspect/plugin-decodebin.xml +++ b/docs/plugins/inspect/plugin-decodebin.xml @@ -3,10 +3,10 @@ decoder bin ../../gst/playback/.libs/libgstdecodebin.so libgstdecodebin.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-ffmpegcolorspace.xml b/docs/plugins/inspect/plugin-ffmpegcolorspace.xml index e618e93..c73bc74 100644 --- a/docs/plugins/inspect/plugin-ffmpegcolorspace.xml +++ b/docs/plugins/inspect/plugin-ffmpegcolorspace.xml @@ -3,7 +3,7 @@ colorspace conversion copied from FFMpeg 0.4.9-pre1 ../../gst/ffmpegcolorspace/.libs/libgstffmpegcolorspace.so libgstffmpegcolorspace.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base FFMpeg diff --git a/docs/plugins/inspect/plugin-gdp.xml b/docs/plugins/inspect/plugin-gdp.xml index e334c91..892be32 100644 --- a/docs/plugins/inspect/plugin-gdp.xml +++ b/docs/plugins/inspect/plugin-gdp.xml @@ -3,10 +3,10 @@ Payload/depayload GDP packets ../../gst/gdp/.libs/libgstgdp.so libgstgdp.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-gio.xml b/docs/plugins/inspect/plugin-gio.xml index 751b84c..ebc7cbd 100644 --- a/docs/plugins/inspect/plugin-gio.xml +++ b/docs/plugins/inspect/plugin-gio.xml @@ -3,7 +3,7 @@ GIO elements ../../ext/gio/.libs/libgstgio.so libgstgio.so - 0.10.21.1 + 0.10.22.1 LGPL gst-plugins-base GStreamer Base Plug-ins CVS/prerelease diff --git a/docs/plugins/inspect/plugin-gnomevfs.xml b/docs/plugins/inspect/plugin-gnomevfs.xml index e77676f..994ac77 100644 --- a/docs/plugins/inspect/plugin-gnomevfs.xml +++ b/docs/plugins/inspect/plugin-gnomevfs.xml @@ -3,10 +3,10 @@ elements to read from and write to Gnome-VFS uri's ../../ext/gnomevfs/.libs/libgstgnomevfs.so libgstgnomevfs.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-libvisual.xml b/docs/plugins/inspect/plugin-libvisual.xml index da1732f..a58fc15 100644 --- a/docs/plugins/inspect/plugin-libvisual.xml +++ b/docs/plugins/inspect/plugin-libvisual.xml @@ -3,10 +3,10 @@ libvisual visualization plugins ../../ext/libvisual/.libs/libgstlibvisual.so libgstlibvisual.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-ogg.xml b/docs/plugins/inspect/plugin-ogg.xml index 1c18aa3..aa5f3d1 100644 --- a/docs/plugins/inspect/plugin-ogg.xml +++ b/docs/plugins/inspect/plugin-ogg.xml @@ -3,10 +3,10 @@ ogg stream manipulation (info about ogg: http://xiph.org) ../../ext/ogg/.libs/libgstogg.so libgstogg.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-pango.xml b/docs/plugins/inspect/plugin-pango.xml index cb057f7..0e71468 100644 --- a/docs/plugins/inspect/plugin-pango.xml +++ b/docs/plugins/inspect/plugin-pango.xml @@ -3,10 +3,10 @@ Pango-based text rendering and overlay ../../ext/pango/.libs/libgstpango.so libgstpango.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-playback.xml b/docs/plugins/inspect/plugin-playback.xml index 0bc1ab7..dde0dfc 100644 --- a/docs/plugins/inspect/plugin-playback.xml +++ b/docs/plugins/inspect/plugin-playback.xml @@ -3,10 +3,10 @@ various playback elements ../../gst/playback/.libs/libgstplaybin.so libgstplaybin.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-queue2.xml b/docs/plugins/inspect/plugin-queue2.xml index 6de1184..1d21bf7 100644 --- a/docs/plugins/inspect/plugin-queue2.xml +++ b/docs/plugins/inspect/plugin-queue2.xml @@ -3,10 +3,10 @@ Queue newer version ../../gst/playback/.libs/libgstqueue2.so libgstqueue2.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-subparse.xml b/docs/plugins/inspect/plugin-subparse.xml index 2a3c0a8..21ee85c 100644 --- a/docs/plugins/inspect/plugin-subparse.xml +++ b/docs/plugins/inspect/plugin-subparse.xml @@ -3,10 +3,10 @@ Subtitle parsing ../../gst/subparse/.libs/libgstsubparse.so libgstsubparse.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-tcp.xml b/docs/plugins/inspect/plugin-tcp.xml index 0a7c2ba..1fb4f12 100644 --- a/docs/plugins/inspect/plugin-tcp.xml +++ b/docs/plugins/inspect/plugin-tcp.xml @@ -3,10 +3,10 @@ transfer data over the network via TCP ../../gst/tcp/.libs/libgsttcp.so libgsttcp.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-theora.xml b/docs/plugins/inspect/plugin-theora.xml index 14bcb47..799ed26 100644 --- a/docs/plugins/inspect/plugin-theora.xml +++ b/docs/plugins/inspect/plugin-theora.xml @@ -3,10 +3,10 @@ Theora plugin library ../../ext/theora/.libs/libgsttheora.so libgsttheora.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-typefindfunctions.xml b/docs/plugins/inspect/plugin-typefindfunctions.xml index 424fed9..2c9801e 100644 --- a/docs/plugins/inspect/plugin-typefindfunctions.xml +++ b/docs/plugins/inspect/plugin-typefindfunctions.xml @@ -3,10 +3,10 @@ default typefind functions ../../gst/typefind/.libs/libgsttypefindfunctions.so libgsttypefindfunctions.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-uridecodebin.xml b/docs/plugins/inspect/plugin-uridecodebin.xml index dc83171..7b38290 100644 --- a/docs/plugins/inspect/plugin-uridecodebin.xml +++ b/docs/plugins/inspect/plugin-uridecodebin.xml @@ -3,10 +3,10 @@ URI Decoder bin ../../gst/playback/.libs/libgstdecodebin2.so libgstdecodebin2.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-video4linux.xml b/docs/plugins/inspect/plugin-video4linux.xml index c0b9446..b71b136 100644 --- a/docs/plugins/inspect/plugin-video4linux.xml +++ b/docs/plugins/inspect/plugin-video4linux.xml @@ -3,10 +3,10 @@ elements for Video 4 Linux ../../sys/v4l/.libs/libgstvideo4linux.so libgstvideo4linux.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-videorate.xml b/docs/plugins/inspect/plugin-videorate.xml index b222967..a5c667d 100644 --- a/docs/plugins/inspect/plugin-videorate.xml +++ b/docs/plugins/inspect/plugin-videorate.xml @@ -3,10 +3,10 @@ Adjusts video frames ../../gst/videorate/.libs/libgstvideorate.so libgstvideorate.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-videoscale.xml b/docs/plugins/inspect/plugin-videoscale.xml index d391cad..7e31ae9 100644 --- a/docs/plugins/inspect/plugin-videoscale.xml +++ b/docs/plugins/inspect/plugin-videoscale.xml @@ -3,10 +3,10 @@ Resizes video ../../gst/videoscale/.libs/libgstvideoscale.so libgstvideoscale.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-videotestsrc.xml b/docs/plugins/inspect/plugin-videotestsrc.xml index 4b5b2c0..f2b2bee 100644 --- a/docs/plugins/inspect/plugin-videotestsrc.xml +++ b/docs/plugins/inspect/plugin-videotestsrc.xml @@ -3,10 +3,10 @@ Creates a test video stream ../../gst/videotestsrc/.libs/libgstvideotestsrc.so libgstvideotestsrc.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-volume.xml b/docs/plugins/inspect/plugin-volume.xml index c43861e..3411e10 100644 --- a/docs/plugins/inspect/plugin-volume.xml +++ b/docs/plugins/inspect/plugin-volume.xml @@ -3,10 +3,10 @@ plugin for controlling audio volume ../../gst/volume/.libs/libgstvolume.so libgstvolume.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-vorbis.xml b/docs/plugins/inspect/plugin-vorbis.xml index 3906f77..31d1326 100644 --- a/docs/plugins/inspect/plugin-vorbis.xml +++ b/docs/plugins/inspect/plugin-vorbis.xml @@ -3,10 +3,10 @@ Vorbis plugin library ../../ext/vorbis/.libs/libgstvorbis.so libgstvorbis.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-ximagesink.xml b/docs/plugins/inspect/plugin-ximagesink.xml index ff7737b..be2bfe7 100644 --- a/docs/plugins/inspect/plugin-ximagesink.xml +++ b/docs/plugins/inspect/plugin-ximagesink.xml @@ -3,10 +3,10 @@ X11 video output element based on standard Xlib calls ../../sys/ximage/.libs/libgstximagesink.so libgstximagesink.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/docs/plugins/inspect/plugin-xvimagesink.xml b/docs/plugins/inspect/plugin-xvimagesink.xml index b29a0a3..7ecee71 100644 --- a/docs/plugins/inspect/plugin-xvimagesink.xml +++ b/docs/plugins/inspect/plugin-xvimagesink.xml @@ -3,10 +3,10 @@ XFree86 video output plugin using Xv extension ../../sys/xvimage/.libs/libgstxvimagesink.so libgstxvimagesink.so - 0.10.22 + 0.10.22.1 LGPL gst-plugins-base - GStreamer Base Plug-ins source release + GStreamer Base Plug-ins CVS/prerelease Unknown package origin diff --git a/gst/audioresample/Makefile.am b/gst/audioresample/Makefile.am index f9c4c52..fdb56a2 100644 --- a/gst/audioresample/Makefile.am +++ b/gst/audioresample/Makefile.am @@ -1,21 +1,37 @@ plugin_LTLIBRARIES = libgstaudioresample.la -resample_SOURCES = \ - functable.c \ - resample.c \ - resample_functable.c \ - resample_ref.c \ - resample_chunk.c \ - resample.h \ - buffer.c +libgstaudioresample_la_SOURCES = \ + gstaudioresample.c \ + speex_resampler_int.c \ + speex_resampler_float.c \ + speex_resampler_double.c + +libgstaudioresample_la_CFLAGS = \ + $(GST_PLUGINS_BASE_CFLAGS) \ + $(GST_BASE_CFLAGS) \ + $(GST_CFLAGS) \ + $(LIBOIL_CFLAGS) + +libgstaudioresample_la_LIBADD = \ + $(GST_PLUGINS_BASE_LIBS) \ + $(GST_BASE_LIBS) \ + $(GST_LIBS) \ + $(LIBOIL_LIBS) \ + $(LIBM) + +libgstaudioresample_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) +libgstaudioresample_la_LIBTOOLFLAGS = --tag=disable-static noinst_HEADERS = \ + arch.h \ + fixed_arm4.h \ + fixed_arm5e.h \ + fixed_bfin.h \ + fixed_debug.h \ + fixed_generic.h \ gstaudioresample.h \ - functable.h \ - debug.h \ - buffer.h + resample.c \ + resample_sse.h \ + speex_resampler.h \ + speex_resampler_wrapper.h -libgstaudioresample_la_SOURCES = gstaudioresample.c $(resample_SOURCES) -libgstaudioresample_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CFLAGS) $(LIBOIL_CFLAGS) -libgstaudioresample_la_LIBADD = $(GST_BASE_LIBS) $(GST_LIBS) $(LIBOIL_LIBS) -libgstaudioresample_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) diff --git a/gst/speexresample/README b/gst/audioresample/README similarity index 100% rename from gst/speexresample/README rename to gst/audioresample/README diff --git a/gst/speexresample/arch.h b/gst/audioresample/arch.h similarity index 100% rename from gst/speexresample/arch.h rename to gst/audioresample/arch.h diff --git a/gst/audioresample/buffer.c b/gst/audioresample/buffer.c deleted file mode 100644 index 442b4f8..0000000 --- a/gst/audioresample/buffer.c +++ /dev/null @@ -1,253 +0,0 @@ - -#ifndef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include - -#include "buffer.h" -#include "debug.h" - -static void audioresample_buffer_free_mem (AudioresampleBuffer * buffer, - void *); -static void audioresample_buffer_free_subbuffer (AudioresampleBuffer * buffer, - void *priv); - - -AudioresampleBuffer * -audioresample_buffer_new (void) -{ - AudioresampleBuffer *buffer; - - buffer = g_new0 (AudioresampleBuffer, 1); - buffer->ref_count = 1; - return buffer; -} - -AudioresampleBuffer * -audioresample_buffer_new_and_alloc (int size) -{ - AudioresampleBuffer *buffer = audioresample_buffer_new (); - - buffer->data = g_malloc (size); - buffer->length = size; - buffer->free = audioresample_buffer_free_mem; - - return buffer; -} - -AudioresampleBuffer * -audioresample_buffer_new_with_data (void *data, int size) -{ - AudioresampleBuffer *buffer = audioresample_buffer_new (); - - buffer->data = data; - buffer->length = size; - buffer->free = audioresample_buffer_free_mem; - - return buffer; -} - -AudioresampleBuffer * -audioresample_buffer_new_subbuffer (AudioresampleBuffer * buffer, int offset, - int length) -{ - AudioresampleBuffer *subbuffer = audioresample_buffer_new (); - - if (buffer->parent) { - audioresample_buffer_ref (buffer->parent); - subbuffer->parent = buffer->parent; - } else { - audioresample_buffer_ref (buffer); - subbuffer->parent = buffer; - } - subbuffer->data = buffer->data + offset; - subbuffer->length = length; - subbuffer->free = audioresample_buffer_free_subbuffer; - - return subbuffer; -} - -void -audioresample_buffer_ref (AudioresampleBuffer * buffer) -{ - buffer->ref_count++; -} - -void -audioresample_buffer_unref (AudioresampleBuffer * buffer) -{ - buffer->ref_count--; - if (buffer->ref_count == 0) { - if (buffer->free) - buffer->free (buffer, buffer->priv); - g_free (buffer); - } -} - -static void -audioresample_buffer_free_mem (AudioresampleBuffer * buffer, void *priv) -{ - g_free (buffer->data); -} - -static void -audioresample_buffer_free_subbuffer (AudioresampleBuffer * buffer, void *priv) -{ - audioresample_buffer_unref (buffer->parent); -} - - -AudioresampleBufferQueue * -audioresample_buffer_queue_new (void) -{ - return g_new0 (AudioresampleBufferQueue, 1); -} - -int -audioresample_buffer_queue_get_depth (AudioresampleBufferQueue * queue) -{ - return queue->depth; -} - -int -audioresample_buffer_queue_get_offset (AudioresampleBufferQueue * queue) -{ - return queue->offset; -} - -void -audioresample_buffer_queue_free (AudioresampleBufferQueue * queue) -{ - GList *g; - - for (g = g_list_first (queue->buffers); g; g = g_list_next (g)) { - audioresample_buffer_unref ((AudioresampleBuffer *) g->data); - } - g_list_free (queue->buffers); - g_free (queue); -} - -void -audioresample_buffer_queue_push (AudioresampleBufferQueue * queue, - AudioresampleBuffer * buffer) -{ - queue->buffers = g_list_append (queue->buffers, buffer); - queue->depth += buffer->length; -} - -AudioresampleBuffer * -audioresample_buffer_queue_pull (AudioresampleBufferQueue * queue, int length) -{ - GList *g; - AudioresampleBuffer *newbuffer; - AudioresampleBuffer *buffer; - AudioresampleBuffer *subbuffer; - - g_return_val_if_fail (length > 0, NULL); - - if (queue->depth < length) { - return NULL; - } - - RESAMPLE_LOG ("pulling %d, %d available", length, queue->depth); - - g = g_list_first (queue->buffers); - buffer = g->data; - - if (buffer->length > length) { - newbuffer = audioresample_buffer_new_subbuffer (buffer, 0, length); - - subbuffer = audioresample_buffer_new_subbuffer (buffer, length, - buffer->length - length); - g->data = subbuffer; - audioresample_buffer_unref (buffer); - } else { - int offset = 0; - - newbuffer = audioresample_buffer_new_and_alloc (length); - - while (offset < length) { - g = g_list_first (queue->buffers); - buffer = g->data; - - if (buffer->length > length - offset) { - int n = length - offset; - - memcpy (newbuffer->data + offset, buffer->data, n); - subbuffer = - audioresample_buffer_new_subbuffer (buffer, n, buffer->length - n); - g->data = subbuffer; - audioresample_buffer_unref (buffer); - offset += n; - } else { - memcpy (newbuffer->data + offset, buffer->data, buffer->length); - - queue->buffers = g_list_delete_link (queue->buffers, g); - offset += buffer->length; - audioresample_buffer_unref (buffer); - } - } - } - - queue->depth -= length; - queue->offset += length; - - return newbuffer; -} - -AudioresampleBuffer * -audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int length) -{ - GList *g; - AudioresampleBuffer *newbuffer; - AudioresampleBuffer *buffer; - int offset = 0; - - g_return_val_if_fail (length > 0, NULL); - - if (queue->depth < length) { - return NULL; - } - - RESAMPLE_LOG ("peeking %d, %d available", length, queue->depth); - - g = g_list_first (queue->buffers); - buffer = g->data; - if (buffer->length > length) { - newbuffer = audioresample_buffer_new_subbuffer (buffer, 0, length); - } else { - newbuffer = audioresample_buffer_new_and_alloc (length); - while (offset < length) { - buffer = g->data; - - if (buffer->length > length - offset) { - int n = length - offset; - - memcpy (newbuffer->data + offset, buffer->data, n); - offset += n; - } else { - memcpy (newbuffer->data + offset, buffer->data, buffer->length); - offset += buffer->length; - } - g = g_list_next (g); - } - } - - return newbuffer; -} - -void -audioresample_buffer_queue_flush (AudioresampleBufferQueue * queue) -{ - GList *g; - - for (g = g_list_first (queue->buffers); g; g = g_list_next (g)) { - audioresample_buffer_unref ((AudioresampleBuffer *) g->data); - } - g_list_free (queue->buffers); - queue->buffers = NULL; - queue->depth = 0; - queue->offset = 0; -} diff --git a/gst/audioresample/buffer.h b/gst/audioresample/buffer.h deleted file mode 100644 index 4cf1fd9..0000000 --- a/gst/audioresample/buffer.h +++ /dev/null @@ -1,51 +0,0 @@ - -#ifndef __AUDIORESAMPLE_BUFFER_H__ -#define __AUDIORESAMPLE_BUFFER_H__ - -#include - -typedef struct _AudioresampleBuffer AudioresampleBuffer; -typedef struct _AudioresampleBufferQueue AudioresampleBufferQueue; - -struct _AudioresampleBuffer -{ - unsigned char *data; - int length; - - int ref_count; - - AudioresampleBuffer *parent; - - void (*free) (AudioresampleBuffer *, void *); - void *priv; - void *priv2; -}; - -struct _AudioresampleBufferQueue -{ - GList *buffers; - int depth; - int offset; -}; - -AudioresampleBuffer * audioresample_buffer_new (void); -AudioresampleBuffer * audioresample_buffer_new_and_alloc (int size); -AudioresampleBuffer * audioresample_buffer_new_with_data (void *data, int size); -AudioresampleBuffer * audioresample_buffer_new_subbuffer (AudioresampleBuffer * buffer, - int offset, - int length); -void audioresample_buffer_ref (AudioresampleBuffer * buffer); -void audioresample_buffer_unref (AudioresampleBuffer * buffer); - -AudioresampleBufferQueue * - audioresample_buffer_queue_new (void); -void audioresample_buffer_queue_free (AudioresampleBufferQueue * queue); -int audioresample_buffer_queue_get_depth (AudioresampleBufferQueue * queue); -int audioresample_buffer_queue_get_offset (AudioresampleBufferQueue * queue); -void audioresample_buffer_queue_push (AudioresampleBufferQueue * queue, - AudioresampleBuffer * buffer); -AudioresampleBuffer * audioresample_buffer_queue_pull (AudioresampleBufferQueue * queue, int len); -AudioresampleBuffer * audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int len); -void audioresample_buffer_queue_flush (AudioresampleBufferQueue * queue); - -#endif diff --git a/gst/audioresample/debug.c b/gst/audioresample/debug.c deleted file mode 100644 index 2787727..0000000 --- a/gst/audioresample/debug.c +++ /dev/null @@ -1,65 +0,0 @@ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include -#include - -static const char *resample_debug_level_names[] = { - "NONE", - "ERROR", - "WARNING", - "INFO", - "DEBUG", - "LOG" -}; - -static int resample_debug_level = RESAMPLE_LEVEL_ERROR; - -void -resample_debug_log (int level, const char *file, const char *function, - int line, const char *format, ...) -{ -#ifndef GLIB_COMPAT - va_list varargs; - char *s; - - if (level > resample_debug_level) - return; - - va_start (varargs, format); - s = g_strdup_vprintf (format, varargs); - va_end (varargs); - - fprintf (stderr, "RESAMPLE: %s: %s(%d): %s: %s\n", - resample_debug_level_names[level], file, line, function, s); - g_free (s); -#else - va_list varargs; - char s[1000]; - - if (level > resample_debug_level) - return; - - va_start (varargs, format); - vsnprintf (s, 999, format, varargs); - va_end (varargs); - - fprintf (stderr, "RESAMPLE: %s: %s(%d): %s: %s\n", - resample_debug_level_names[level], file, line, function, s); -#endif -} - -void -resample_debug_set_level (int level) -{ - resample_debug_level = level; -} - -int -resample_debug_get_level (void) -{ - return resample_debug_level; -} diff --git a/gst/audioresample/debug.h b/gst/audioresample/debug.h deleted file mode 100644 index ff7deaf..0000000 --- a/gst/audioresample/debug.h +++ /dev/null @@ -1,51 +0,0 @@ - -#ifndef __RESAMPLE_DEBUG_H__ -#define __RESAMPLE_DEBUG_H__ - -#if 0 -enum -{ - RESAMPLE_LEVEL_NONE = 0, - RESAMPLE_LEVEL_ERROR, - RESAMPLE_LEVEL_WARNING, - RESAMPLE_LEVEL_INFO, - RESAMPLE_LEVEL_DEBUG, - RESAMPLE_LEVEL_LOG -}; - -#define RESAMPLE_ERROR(...) \ - RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_ERROR, __VA_ARGS__) -#define RESAMPLE_WARNING(...) \ - RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_WARNING, __VA_ARGS__) -#define RESAMPLE_INFO(...) \ - RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_INFO, __VA_ARGS__) -#define RESAMPLE_DEBUG(...) \ - RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_DEBUG, __VA_ARGS__) -#define RESAMPLE_LOG(...) \ - RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_LOG, __VA_ARGS__) - -#define RESAMPLE_DEBUG_LEVEL(level,...) \ - resample_debug_log ((level), __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) - -void resample_debug_log (int level, const char *file, const char *function, - int line, const char *format, ...); -void resample_debug_set_level (int level); -int resample_debug_get_level (void); -#else - -#include - -GST_DEBUG_CATEGORY_EXTERN (libaudioresample_debug); -#define GST_CAT_DEFAULT libaudioresample_debug - -#define RESAMPLE_ERROR GST_ERROR -#define RESAMPLE_WARNING GST_WARNING -#define RESAMPLE_INFO GST_INFO -#define RESAMPLE_DEBUG GST_DEBUG -#define RESAMPLE_LOG GST_LOG - -#define resample_debug_set_level(x) do { } while (0) - -#endif - -#endif diff --git a/gst/speexresample/fixed_arm4.h b/gst/audioresample/fixed_arm4.h similarity index 100% rename from gst/speexresample/fixed_arm4.h rename to gst/audioresample/fixed_arm4.h diff --git a/gst/speexresample/fixed_arm5e.h b/gst/audioresample/fixed_arm5e.h similarity index 100% rename from gst/speexresample/fixed_arm5e.h rename to gst/audioresample/fixed_arm5e.h diff --git a/gst/speexresample/fixed_bfin.h b/gst/audioresample/fixed_bfin.h similarity index 100% rename from gst/speexresample/fixed_bfin.h rename to gst/audioresample/fixed_bfin.h diff --git a/gst/speexresample/fixed_debug.h b/gst/audioresample/fixed_debug.h similarity index 100% rename from gst/speexresample/fixed_debug.h rename to gst/audioresample/fixed_debug.h diff --git a/gst/speexresample/fixed_generic.h b/gst/audioresample/fixed_generic.h similarity index 100% rename from gst/speexresample/fixed_generic.h rename to gst/audioresample/fixed_generic.h diff --git a/gst/audioresample/functable.c b/gst/audioresample/functable.c deleted file mode 100644 index d627361..0000000 --- a/gst/audioresample/functable.c +++ /dev/null @@ -1,254 +0,0 @@ -/* Resampling library - * Copyright (C) <2001> David A. Schleef - * - * 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 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 -#endif - -#include -#include -#include -#include - -#include "functable.h" -#include "debug.h" - - - -void -functable_func_sinc (double *fx, double *dfx, double x, void *closure) -{ - if (x == 0) { - *fx = 1; - *dfx = 0; - return; - } - - *fx = sin (x) / x; - *dfx = (cos (x) - sin (x) / x) / x; -} - -void -functable_func_boxcar (double *fx, double *dfx, double x, void *closure) -{ - double width = *(double *) closure; - - if (x < width && x > -width) { - *fx = 1; - } else { - *fx = 0; - } - *dfx = 0; -} - -void -functable_func_hanning (double *fx, double *dfx, double x, void *closure) -{ - double width = *(double *) closure; - - if (x < width && x > -width) { - x /= width; - *fx = (1 - x * x) * (1 - x * x); - *dfx = -2 * 2 * x / width * (1 - x * x); - } else { - *fx = 0; - *dfx = 0; - } -} - - -Functable * -functable_new (void) -{ - Functable *ft; - - ft = malloc (sizeof (Functable)); - memset (ft, 0, sizeof (Functable)); - - return ft; -} - -void -functable_free (Functable * ft) -{ - free (ft); -} - -void -functable_set_length (Functable * t, int length) -{ - t->length = length; -} - -void -functable_set_offset (Functable * t, double offset) -{ - t->offset = offset; -} - -void -functable_set_multiplier (Functable * t, double multiplier) -{ - t->multiplier = multiplier; -} - -void -functable_calculate (Functable * t, FunctableFunc func, void *closure) -{ - int i; - double x; - - if (t->fx) - free (t->fx); - if (t->dfx) - free (t->dfx); - - t->fx = malloc (sizeof (double) * (t->length + 1)); - t->dfx = malloc (sizeof (double) * (t->length + 1)); - - t->inv_multiplier = 1.0 / t->multiplier; - - for (i = 0; i < t->length + 1; i++) { - x = t->offset + t->multiplier * i; - - func (&t->fx[i], &t->dfx[i], x, closure); - } -} - -void -functable_calculate_multiply (Functable * t, FunctableFunc func, void *closure) -{ - int i; - double x; - - for (i = 0; i < t->length + 1; i++) { - double afx, adfx, bfx, bdfx; - - afx = t->fx[i]; - adfx = t->dfx[i]; - x = t->offset + t->multiplier * i; - func (&bfx, &bdfx, x, closure); - t->fx[i] = afx * bfx; - t->dfx[i] = afx * bdfx + adfx * bfx; - } - -} - -double -functable_evaluate (Functable * t, double x) -{ - int i; - double f0, f1, w0, w1; - double x2, x3; - double w; - - if (x < t->offset || x > (t->offset + t->length * t->multiplier)) { - RESAMPLE_DEBUG ("x out of range %g", x); - } - - x -= t->offset; - x *= t->inv_multiplier; - i = floor (x); - x -= i; - - x2 = x * x; - x3 = x2 * x; - - f1 = 3 * x2 - 2 * x3; - f0 = 1 - f1; - w0 = (x - 2 * x2 + x3) * t->multiplier; - w1 = (-x2 + x3) * t->multiplier; - - w = t->fx[i] * f0 + t->fx[i + 1] * f1 + t->dfx[i] * w0 + t->dfx[i + 1] * w1; - - /*w = t->fx[i] * (1-x) + t->fx[i+1] * x; */ - - return w; -} - - -double -functable_fir (Functable * t, double x, int n, double *data, int len) -{ - int i, j; - double f0, f1, w0, w1; - double x2, x3; - double w; - double sum; - - x -= t->offset; - x /= t->multiplier; - i = floor (x); - x -= i; - - x2 = x * x; - x3 = x2 * x; - - f1 = 3 * x2 - 2 * x3; - f0 = 1 - f1; - w0 = (x - 2 * x2 + x3) * t->multiplier; - w1 = (-x2 + x3) * t->multiplier; - - sum = 0; - for (j = 0; j < len; j++) { - w = t->fx[i] * f0 + t->fx[i + 1] * f1 + t->dfx[i] * w0 + t->dfx[i + 1] * w1; - sum += data[j * 2] * w; - i += n; - } - - return sum; -} - -void -functable_fir2 (Functable * t, double *r0, double *r1, double x, - int n, double *data, int len) -{ - int i, j; - double f0, f1, w0, w1; - double x2, x3; - double w; - double sum0, sum1; - double floor_x; - - x -= t->offset; - x *= t->inv_multiplier; - floor_x = floor (x); - i = floor_x; - x -= floor_x; - - x2 = x * x; - x3 = x2 * x; - - f1 = 3 * x2 - 2 * x3; - f0 = 1 - f1; - w0 = (x - 2 * x2 + x3) * t->multiplier; - w1 = (-x2 + x3) * t->multiplier; - - sum0 = 0; - sum1 = 0; - for (j = 0; j < len; j++) { - w = t->fx[i] * f0 + t->fx[i + 1] * f1 + t->dfx[i] * w0 + t->dfx[i + 1] * w1; - sum0 += data[j * 2] * w; - sum1 += data[j * 2 + 1] * w; - i += n; - } - - *r0 = sum0; - *r1 = sum1; -} diff --git a/gst/audioresample/functable.h b/gst/audioresample/functable.h deleted file mode 100644 index 5f56e2b..0000000 --- a/gst/audioresample/functable.h +++ /dev/null @@ -1,61 +0,0 @@ -/* Resampling library - * Copyright (C) <2001> David Schleef - * - * 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 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 __FUNCTABLE_H__ -#define __FUNCTABLE_H__ - -typedef void FunctableFunc (double *fx, double *dfx, double x, void *closure); - -typedef struct _Functable Functable; -struct _Functable { - int length; - - double offset; - double multiplier; - - double inv_multiplier; - - double *fx; - double *dfx; -}; - -Functable *functable_new (void); -void functable_setup (Functable *t); -void functable_free (Functable *t); - -void functable_set_length (Functable *t, int length); -void functable_set_offset (Functable *t, double offset); -void functable_set_multiplier (Functable *t, double multiplier); -void functable_calculate (Functable *t, FunctableFunc func, void *closure); -void functable_calculate_multiply (Functable *t, FunctableFunc func, void *closure); - - -double functable_evaluate (Functable *t,double x); - -double functable_fir(Functable *t,double x0,int n,double *data,int len); -void functable_fir2(Functable *t,double *r0, double *r1, double x0, - int n,double *data,int len); - -void functable_func_sinc(double *fx, double *dfx, double x, void *closure); -void functable_func_boxcar(double *fx, double *dfx, double x, void *closure); -void functable_func_hanning(double *fx, double *dfx, double x, void *closure); - -#endif /* __PRIVATE_H__ */ - diff --git a/gst/audioresample/gstaudioresample.c b/gst/audioresample/gstaudioresample.c index 9aa9bf7..5677bb3 100644 --- a/gst/audioresample/gstaudioresample.c +++ b/gst/audioresample/gstaudioresample.c @@ -1,6 +1,7 @@ /* GStreamer * Copyright (C) 1999 Erik Walthinsen * Copyright (C) 2003,2004 David A. Schleef + * Copyright (C) 2007-2008 Sebastian Dröge * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -17,23 +18,24 @@ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ -/* Element-Checklist-Version: 5 */ /** * SECTION:element-audioresample * - * Audioresample resamples raw audio buffers to different sample rates using + * audioresample resamples raw audio buffers to different sample rates using * a configurable windowing function to enhance quality. * * * Example launch line * |[ * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! audio/x-raw-int, rate=8000 ! alsasink - * ]| Decode an Ogg/Vorbis downsample to 8Khz and play sound through alsa. + * ]| Decode an Ogg/Vorbis downsample to 8Khz and play sound through alsa. * To create the Ogg/Vorbis file refer to the documentation of vorbisenc. * - * - * Last reviewed on 2006-03-02 (0.10.4) + */ + +/* TODO: + * - Enable SSE/ARM optimizations and select at runtime */ #ifdef HAVE_CONFIG_H @@ -43,221 +45,240 @@ #include #include -/*#define DEBUG_ENABLED */ #include "gstaudioresample.h" #include #include -GST_DEBUG_CATEGORY_STATIC (audioresample_debug); -#define GST_CAT_DEFAULT audioresample_debug +#define OIL_ENABLE_UNSTABLE_API +#include +#include -/* elementfactory information */ -static const GstElementDetails gst_audioresample_details = -GST_ELEMENT_DETAILS ("Audio scaler", - "Filter/Converter/Audio", - "Resample audio", - "David Schleef "); - -#define DEFAULT_FILTERLEN 16 +GST_DEBUG_CATEGORY (audio_resample_debug); +#define GST_CAT_DEFAULT audio_resample_debug enum { PROP_0, - PROP_FILTERLEN + PROP_QUALITY, + PROP_FILTER_LENGTH }; #define SUPPORTED_CAPS \ GST_STATIC_CAPS ( \ - "audio/x-raw-int, " \ - "rate = (int) [ 1, MAX ], " \ + "audio/x-raw-float, " \ + "rate = (int) [ 1, MAX ], " \ "channels = (int) [ 1, MAX ], " \ "endianness = (int) BYTE_ORDER, " \ - "width = (int) 16, " \ - "depth = (int) 16, " \ - "signed = (boolean) true;" \ + "width = (int) { 32, 64 }; " \ "audio/x-raw-int, " \ "rate = (int) [ 1, MAX ], " \ "channels = (int) [ 1, MAX ], " \ "endianness = (int) BYTE_ORDER, " \ "width = (int) 32, " \ "depth = (int) 32, " \ - "signed = (boolean) true;" \ - "audio/x-raw-float, " \ - "rate = (int) [ 1, MAX ], " \ + "signed = (boolean) true; " \ + "audio/x-raw-int, " \ + "rate = (int) [ 1, MAX ], " \ "channels = (int) [ 1, MAX ], " \ "endianness = (int) BYTE_ORDER, " \ - "width = (int) 32; " \ - "audio/x-raw-float, " \ - "rate = (int) [ 1, MAX ], " \ + "width = (int) 24, " \ + "depth = (int) 24, " \ + "signed = (boolean) true; " \ + "audio/x-raw-int, " \ + "rate = (int) [ 1, MAX ], " \ "channels = (int) [ 1, MAX ], " \ "endianness = (int) BYTE_ORDER, " \ - "width = (int) 64" \ + "width = (int) 16, " \ + "depth = (int) 16, " \ + "signed = (boolean) true; " \ + "audio/x-raw-int, " \ + "rate = (int) [ 1, MAX ], " \ + "channels = (int) [ 1, MAX ], " \ + "endianness = (int) BYTE_ORDER, " \ + "width = (int) 8, " \ + "depth = (int) 8, " \ + "signed = (boolean) true" \ ) -static GstStaticPadTemplate gst_audioresample_sink_template = +/* If TRUE integer arithmetic resampling is faster and will be used if appropiate */ +static gboolean gst_audio_resample_use_int = FALSE; + +static GstStaticPadTemplate gst_audio_resample_sink_template = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, SUPPORTED_CAPS); -static GstStaticPadTemplate gst_audioresample_src_template = +static GstStaticPadTemplate gst_audio_resample_src_template = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, SUPPORTED_CAPS); -static void gst_audioresample_set_property (GObject * object, +static void gst_audio_resample_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); -static void gst_audioresample_get_property (GObject * object, +static void gst_audio_resample_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); /* vmethods */ -static gboolean audioresample_get_unit_size (GstBaseTransform * base, +static gboolean gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps, guint * size); -static GstCaps *audioresample_transform_caps (GstBaseTransform * base, +static GstCaps *gst_audio_resample_transform_caps (GstBaseTransform * base, GstPadDirection direction, GstCaps * caps); -static gboolean audioresample_transform_size (GstBaseTransform * trans, +static void gst_audio_resample_fixate_caps (GstBaseTransform * base, + GstPadDirection direction, GstCaps * caps, GstCaps * othercaps); +static gboolean gst_audio_resample_transform_size (GstBaseTransform * trans, GstPadDirection direction, GstCaps * incaps, guint insize, GstCaps * outcaps, guint * outsize); -static gboolean audioresample_set_caps (GstBaseTransform * base, +static gboolean gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps); -static GstFlowReturn audioresample_pushthrough (GstAudioresample * - audioresample); -static GstFlowReturn audioresample_transform (GstBaseTransform * base, +static GstFlowReturn gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, GstBuffer * outbuf); -static gboolean audioresample_event (GstBaseTransform * base, GstEvent * event); -static gboolean audioresample_start (GstBaseTransform * base); -static gboolean audioresample_stop (GstBaseTransform * base); - -static gboolean audioresample_query (GstPad * pad, GstQuery * query); -static const GstQueryType *audioresample_query_type (GstPad * pad); - -#define DEBUG_INIT(bla) \ - GST_DEBUG_CATEGORY_INIT (audioresample_debug, "audioresample", 0, "audio resampling element"); +static gboolean gst_audio_resample_event (GstBaseTransform * base, + GstEvent * event); +static gboolean gst_audio_resample_start (GstBaseTransform * base); +static gboolean gst_audio_resample_stop (GstBaseTransform * base); +static gboolean gst_audio_resample_query (GstPad * pad, GstQuery * query); +static const GstQueryType *gst_audio_resample_query_type (GstPad * pad); -GST_BOILERPLATE_FULL (GstAudioresample, gst_audioresample, GstBaseTransform, - GST_TYPE_BASE_TRANSFORM, DEBUG_INIT); +GST_BOILERPLATE (GstAudioResample, gst_audio_resample, GstBaseTransform, + GST_TYPE_BASE_TRANSFORM); static void -gst_audioresample_base_init (gpointer g_class) +gst_audio_resample_base_init (gpointer g_class) { GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class); gst_element_class_add_pad_template (gstelement_class, - gst_static_pad_template_get (&gst_audioresample_src_template)); + gst_static_pad_template_get (&gst_audio_resample_src_template)); gst_element_class_add_pad_template (gstelement_class, - gst_static_pad_template_get (&gst_audioresample_sink_template)); + gst_static_pad_template_get (&gst_audio_resample_sink_template)); - gst_element_class_set_details (gstelement_class, &gst_audioresample_details); + gst_element_class_set_details_simple (gstelement_class, "Audio resampler", + "Filter/Converter/Audio", "Resamples audio", + "Sebastian Dröge "); } static void -gst_audioresample_class_init (GstAudioresampleClass * klass) +gst_audio_resample_class_init (GstAudioResampleClass * klass) { - GObjectClass *gobject_class; - - gobject_class = (GObjectClass *) klass; - - gobject_class->set_property = gst_audioresample_set_property; - gobject_class->get_property = gst_audioresample_get_property; - - g_object_class_install_property (gobject_class, PROP_FILTERLEN, - g_param_spec_int ("filter-length", "filter length", - "Length of the resample filter", 0, G_MAXINT, DEFAULT_FILTERLEN, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); + GObjectClass *gobject_class = (GObjectClass *) klass; + + gobject_class->set_property = gst_audio_resample_set_property; + gobject_class->get_property = gst_audio_resample_get_property; + + g_object_class_install_property (gobject_class, PROP_QUALITY, + g_param_spec_int ("quality", "Quality", "Resample quality with 0 being " + "the lowest and 10 being the best", + SPEEX_RESAMPLER_QUALITY_MIN, SPEEX_RESAMPLER_QUALITY_MAX, + SPEEX_RESAMPLER_QUALITY_DEFAULT, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + + /* FIXME 0.11: Remove this property, it's just for compatibility + * with old audioresample + */ + g_object_class_install_property (gobject_class, PROP_FILTER_LENGTH, + g_param_spec_int ("filter-length", "Filter length", + "DEPRECATED, DON'T USE THIS! " "Length of the resample filter", 0, + G_MAXINT, 64, G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); GST_BASE_TRANSFORM_CLASS (klass)->start = - GST_DEBUG_FUNCPTR (audioresample_start); + GST_DEBUG_FUNCPTR (gst_audio_resample_start); GST_BASE_TRANSFORM_CLASS (klass)->stop = - GST_DEBUG_FUNCPTR (audioresample_stop); + GST_DEBUG_FUNCPTR (gst_audio_resample_stop); GST_BASE_TRANSFORM_CLASS (klass)->transform_size = - GST_DEBUG_FUNCPTR (audioresample_transform_size); + GST_DEBUG_FUNCPTR (gst_audio_resample_transform_size); GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size = - GST_DEBUG_FUNCPTR (audioresample_get_unit_size); + GST_DEBUG_FUNCPTR (gst_audio_resample_get_unit_size); GST_BASE_TRANSFORM_CLASS (klass)->transform_caps = - GST_DEBUG_FUNCPTR (audioresample_transform_caps); + GST_DEBUG_FUNCPTR (gst_audio_resample_transform_caps); + GST_BASE_TRANSFORM_CLASS (klass)->fixate_caps = + GST_DEBUG_FUNCPTR (gst_audio_resample_fixate_caps); GST_BASE_TRANSFORM_CLASS (klass)->set_caps = - GST_DEBUG_FUNCPTR (audioresample_set_caps); + GST_DEBUG_FUNCPTR (gst_audio_resample_set_caps); GST_BASE_TRANSFORM_CLASS (klass)->transform = - GST_DEBUG_FUNCPTR (audioresample_transform); + GST_DEBUG_FUNCPTR (gst_audio_resample_transform); GST_BASE_TRANSFORM_CLASS (klass)->event = - GST_DEBUG_FUNCPTR (audioresample_event); + GST_DEBUG_FUNCPTR (gst_audio_resample_event); GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE; } static void -gst_audioresample_init (GstAudioresample * audioresample, - GstAudioresampleClass * klass) +gst_audio_resample_init (GstAudioResample * resample, + GstAudioResampleClass * klass) { - GstBaseTransform *trans; - - trans = GST_BASE_TRANSFORM (audioresample); + GstBaseTransform *trans = GST_BASE_TRANSFORM (resample); - /* buffer alloc passthrough is too impossible. FIXME, it - * is trivial in the passthrough case. */ - gst_pad_set_bufferalloc_function (trans->sinkpad, NULL); + resample->quality = SPEEX_RESAMPLER_QUALITY_DEFAULT; - audioresample->filter_length = DEFAULT_FILTERLEN; + resample->need_discont = FALSE; - audioresample->need_discont = FALSE; - - gst_pad_set_query_function (trans->srcpad, audioresample_query); - gst_pad_set_query_type_function (trans->srcpad, audioresample_query_type); + gst_pad_set_query_function (trans->srcpad, gst_audio_resample_query); + gst_pad_set_query_type_function (trans->srcpad, + gst_audio_resample_query_type); } /* vmethods */ static gboolean -audioresample_start (GstBaseTransform * base) +gst_audio_resample_start (GstBaseTransform * base) { - GstAudioresample *audioresample = GST_AUDIORESAMPLE (base); - - audioresample->resample = resample_new (); - audioresample->ts_offset = -1; - audioresample->offset = -1; - audioresample->next_ts = -1; + GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); - resample_set_filter_length (audioresample->resample, - audioresample->filter_length); + resample->next_offset = -1; + resample->next_ts = -1; + resample->next_upstream_ts = -1; return TRUE; } static gboolean -audioresample_stop (GstBaseTransform * base) +gst_audio_resample_stop (GstBaseTransform * base) { - GstAudioresample *audioresample = GST_AUDIORESAMPLE (base); + GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); - if (audioresample->resample) { - resample_free (audioresample->resample); - audioresample->resample = NULL; + if (resample->state) { + resample->funcs->destroy (resample->state); + resample->state = NULL; } - gst_caps_replace (&audioresample->sinkcaps, NULL); - gst_caps_replace (&audioresample->srccaps, NULL); + resample->funcs = NULL; + + g_free (resample->tmp_in); + resample->tmp_in = NULL; + resample->tmp_in_size = 0; + + g_free (resample->tmp_out); + resample->tmp_out = NULL; + resample->tmp_out_size = 0; + + gst_caps_replace (&resample->sinkcaps, NULL); + gst_caps_replace (&resample->srccaps, NULL); return TRUE; } static gboolean -audioresample_get_unit_size (GstBaseTransform * base, GstCaps * caps, +gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps, guint * size) { gint width, channels; GstStructure *structure; gboolean ret; - g_assert (size); + g_return_val_if_fail (size != NULL, FALSE); /* this works for both float and int */ structure = gst_caps_get_structure (caps, 0); ret = gst_structure_get_int (structure, "width", &width); ret &= gst_structure_get_int (structure, "channels", &channels); - g_return_val_if_fail (ret, FALSE); - *size = width * channels / 8; + if (G_UNLIKELY (!ret)) + return FALSE; + + *size = (width / 8) * channels; return TRUE; } static GstCaps * -audioresample_transform_caps (GstBaseTransform * base, +gst_audio_resample_transform_caps (GstBaseTransform * base, GstPadDirection direction, GstCaps * caps) { GstCaps *res; @@ -272,56 +293,154 @@ audioresample_transform_caps (GstBaseTransform * base, return res; } +/* Fixate rate to the allowed rate that has the smallest difference */ +static void +gst_audio_resample_fixate_caps (GstBaseTransform * base, + GstPadDirection direction, GstCaps * caps, GstCaps * othercaps) +{ + GstStructure *s; + gint rate; + + s = gst_caps_get_structure (caps, 0); + if (G_UNLIKELY (!gst_structure_get_int (s, "rate", &rate))) + return; + + s = gst_caps_get_structure (othercaps, 0); + gst_structure_fixate_field_nearest_int (s, "rate", rate); +} + +static const SpeexResampleFuncs * +gst_audio_resample_get_funcs (gint width, gboolean fp) +{ + const SpeexResampleFuncs *funcs = NULL; + + if (gst_audio_resample_use_int && (width == 8 || width == 16) && !fp) + funcs = &int_funcs; + else if ((!gst_audio_resample_use_int && (width == 8 || width == 16) && !fp) + || (width == 32 && fp)) + funcs = &float_funcs; + else if ((width == 64 && fp) || ((width == 32 || width == 24) && !fp)) + funcs = &double_funcs; + else + g_assert_not_reached (); + + return funcs; +} + +static SpeexResamplerState * +gst_audio_resample_init_state (GstAudioResample * resample, gint width, + gint channels, gint inrate, gint outrate, gint quality, gboolean fp) +{ + SpeexResamplerState *ret = NULL; + gint err = RESAMPLER_ERR_SUCCESS; + const SpeexResampleFuncs *funcs = gst_audio_resample_get_funcs (width, fp); + + ret = funcs->init (channels, inrate, outrate, quality, &err); + + if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { + GST_ERROR_OBJECT (resample, "Failed to create resampler state: %s", + funcs->strerror (err)); + return NULL; + } + + funcs->skip_zeros (ret); + + return ret; +} + static gboolean -resample_set_state_from_caps (ResampleState * state, GstCaps * incaps, - GstCaps * outcaps, gint * channels, gint * inrate, gint * outrate) +gst_audio_resample_update_state (GstAudioResample * resample, gint width, + gint channels, gint inrate, gint outrate, gint quality, gboolean fp) +{ + gboolean ret = TRUE; + gboolean updated_latency = FALSE; + + updated_latency = (resample->inrate != inrate + || quality != resample->quality) && resample->state != NULL; + + if (resample->state == NULL) { + ret = TRUE; + } else if (resample->channels != channels || fp != resample->fp + || width != resample->width) { + resample->funcs->destroy (resample->state); + resample->state = + gst_audio_resample_init_state (resample, width, channels, inrate, + outrate, quality, fp); + + resample->funcs = gst_audio_resample_get_funcs (width, fp); + ret = (resample->state != NULL); + } else if (resample->inrate != inrate || resample->outrate != outrate) { + gint err = RESAMPLER_ERR_SUCCESS; + + err = resample->funcs->set_rate (resample->state, inrate, outrate); + + if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) + GST_ERROR_OBJECT (resample, "Failed to update rate: %s", + resample->funcs->strerror (err)); + + ret = (err == RESAMPLER_ERR_SUCCESS); + } else if (quality != resample->quality) { + gint err = RESAMPLER_ERR_SUCCESS; + + err = resample->funcs->set_quality (resample->state, quality); + + if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) + GST_ERROR_OBJECT (resample, "Failed to update quality: %s", + resample->funcs->strerror (err)); + + ret = (err == RESAMPLER_ERR_SUCCESS); + } + + resample->width = width; + resample->channels = channels; + resample->fp = fp; + resample->quality = quality; + resample->inrate = inrate; + resample->outrate = outrate; + + if (updated_latency) + gst_element_post_message (GST_ELEMENT (resample), + gst_message_new_latency (GST_OBJECT (resample))); + + return ret; +} + +static void +gst_audio_resample_reset_state (GstAudioResample * resample) +{ + if (resample->state) + resample->funcs->reset_mem (resample->state); +} + +static gboolean +gst_audio_resample_parse_caps (GstCaps * incaps, + GstCaps * outcaps, gint * width, gint * channels, gint * inrate, + gint * outrate, gboolean * fp) { GstStructure *structure; gboolean ret; - gint myinrate, myoutrate; - int mychannels; - gint width, depth; - ResampleFormat format; + gint mywidth, myinrate, myoutrate, mychannels; + gboolean myfp; GST_DEBUG ("incaps %" GST_PTR_FORMAT ", outcaps %" GST_PTR_FORMAT, incaps, outcaps); structure = gst_caps_get_structure (incaps, 0); - /* get width */ - ret = gst_structure_get_int (structure, "width", &width); - if (!ret) - goto no_width; - - /* figure out the format */ - if (g_str_equal (gst_structure_get_name (structure), "audio/x-raw-float")) { - if (width == 32) - format = RESAMPLE_FORMAT_F32; - else if (width == 64) - format = RESAMPLE_FORMAT_F64; - else - goto wrong_depth; - } else { - /* for int, depth and width must be the same */ - ret = gst_structure_get_int (structure, "depth", &depth); - if (!ret || width != depth) - goto not_equal; - - if (width == 16) - format = RESAMPLE_FORMAT_S16; - else if (width == 32) - format = RESAMPLE_FORMAT_S32; - else - goto wrong_depth; - } + if (g_str_equal (gst_structure_get_name (structure), "audio/x-raw-float")) + myfp = TRUE; + else + myfp = FALSE; + ret = gst_structure_get_int (structure, "rate", &myinrate); ret &= gst_structure_get_int (structure, "channels", &mychannels); - if (!ret) + ret &= gst_structure_get_int (structure, "width", &mywidth); + if (G_UNLIKELY (!ret)) goto no_in_rate_channels; structure = gst_caps_get_structure (outcaps, 0); ret = gst_structure_get_int (structure, "rate", &myoutrate); - if (!ret) + if (G_UNLIKELY (!ret)) goto no_out_rate; if (channels) @@ -330,30 +449,14 @@ resample_set_state_from_caps (ResampleState * state, GstCaps * incaps, *inrate = myinrate; if (outrate) *outrate = myoutrate; - - resample_set_format (state, format); - resample_set_n_channels (state, mychannels); - resample_set_input_rate (state, myinrate); - resample_set_output_rate (state, myoutrate); + if (width) + *width = mywidth; + if (fp) + *fp = myfp; return TRUE; /* ERRORS */ -no_width: - { - GST_DEBUG ("failed to get width from caps"); - return FALSE; - } -not_equal: - { - GST_DEBUG ("width %d and depth %d must be the same", width, depth); - return FALSE; - } -wrong_depth: - { - GST_DEBUG ("unknown depth %d found", depth); - return FALSE; - } no_in_rate_channels: { GST_DEBUG ("could not get input rate and channels"); @@ -366,18 +469,32 @@ no_out_rate: } } +static gint +_gcd (gint a, gint b) +{ + while (b != 0) { + int temp = a; + + a = b; + b = temp % b; + } + + return ABS (a); +} + static gboolean -audioresample_transform_size (GstBaseTransform * base, +gst_audio_resample_transform_size (GstBaseTransform * base, GstPadDirection direction, GstCaps * caps, guint size, GstCaps * othercaps, guint * othersize) { - GstAudioresample *audioresample = GST_AUDIORESAMPLE (base); - ResampleState *state; + GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); GstCaps *srccaps, *sinkcaps; - gboolean use_internal = FALSE; /* whether we use the internal state */ gboolean ret = TRUE; + guint32 ratio_den, ratio_num; + gint inrate, outrate, gcd; + gint width; - GST_LOG_OBJECT (base, "asked to transform size %d in direction %s", + GST_LOG_OBJECT (resample, "asked to transform size %d in direction %s", size, direction == GST_PAD_SINK ? "SINK" : "SRC"); if (direction == GST_PAD_SINK) { sinkcaps = caps; @@ -387,216 +504,384 @@ audioresample_transform_size (GstBaseTransform * base, srccaps = caps; } - /* if the caps are the ones that _set_caps got called with; we can use - * our own state; otherwise we'll have to create a state */ - if (gst_caps_is_equal (sinkcaps, audioresample->sinkcaps) && - gst_caps_is_equal (srccaps, audioresample->srccaps)) { - use_internal = TRUE; - state = audioresample->resample; - } else { - GST_DEBUG_OBJECT (audioresample, - "caps are not the set caps, creating state"); - state = resample_new (); - resample_set_filter_length (state, audioresample->filter_length); - resample_set_state_from_caps (state, sinkcaps, srccaps, NULL, NULL, NULL); + ret = + gst_audio_resample_parse_caps (caps, othercaps, &width, NULL, &inrate, + &outrate, NULL); + if (G_UNLIKELY (!ret)) { + GST_ERROR_OBJECT (resample, "Wrong caps"); + return FALSE; } + gcd = _gcd (inrate, outrate); + ratio_num = inrate / gcd; + ratio_den = outrate / gcd; + if (direction == GST_PAD_SINK) { + gint fac = width / 8; + /* asked to convert size of an incoming buffer */ - *othersize = resample_get_output_size_for_input (state, size); + size /= fac; + *othersize = (size * ratio_den + ratio_num - 1) / ratio_num; + *othersize *= fac; + size *= fac; } else { + gint fac = width / 8; + /* asked to convert size of an outgoing buffer */ - *othersize = resample_get_input_size_for_output (state, size); + size /= fac; + *othersize = (size * ratio_num + ratio_den - 1) / ratio_den; + *othersize *= fac; + size *= fac; } - g_assert (*othersize % state->sample_size == 0); - /* we make room for one extra sample, given that the resampling filter - * can output an extra one for non-integral i_rate/o_rate */ - GST_LOG_OBJECT (base, "transformed size %d to %d", size, *othersize); - - if (!use_internal) { - resample_free (state); - } + GST_LOG_OBJECT (resample, "transformed size %d to %d", size, *othersize); return ret; } static gboolean -audioresample_set_caps (GstBaseTransform * base, GstCaps * incaps, +gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) { gboolean ret; - gint inrate, outrate; - int channels; - GstAudioresample *audioresample = GST_AUDIORESAMPLE (base); + gint width = 0, inrate = 0, outrate = 0, channels = 0; + gboolean fp; + GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); - GST_DEBUG_OBJECT (base, "incaps %" GST_PTR_FORMAT ", outcaps %" + GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %" GST_PTR_FORMAT, incaps, outcaps); - ret = resample_set_state_from_caps (audioresample->resample, incaps, outcaps, - &channels, &inrate, &outrate); + ret = gst_audio_resample_parse_caps (incaps, outcaps, + &width, &channels, &inrate, &outrate, &fp); - g_return_val_if_fail (ret, FALSE); + if (G_UNLIKELY (!ret)) + return FALSE; - audioresample->channels = channels; - GST_DEBUG_OBJECT (audioresample, "set channels to %d", channels); - audioresample->i_rate = inrate; - GST_DEBUG_OBJECT (audioresample, "set i_rate to %d", inrate); - audioresample->o_rate = outrate; - GST_DEBUG_OBJECT (audioresample, "set o_rate to %d", outrate); + ret = + gst_audio_resample_update_state (resample, width, channels, inrate, + outrate, resample->quality, fp); + + if (G_UNLIKELY (!ret)) + return FALSE; /* save caps so we can short-circuit in the size_transform if the caps * are the same */ - gst_caps_replace (&audioresample->sinkcaps, incaps); - gst_caps_replace (&audioresample->srccaps, outcaps); + gst_caps_replace (&resample->sinkcaps, incaps); + gst_caps_replace (&resample->srccaps, outcaps); return TRUE; } -static gboolean -audioresample_event (GstBaseTransform * base, GstEvent * event) +#define GST_MAXINT24 (8388607) +#define GST_MININT24 (-8388608) + +#if (G_BYTE_ORDER == G_LITTLE_ENDIAN) +#define GST_READ_UINT24 GST_READ_UINT24_LE +#define GST_WRITE_UINT24 GST_WRITE_UINT24_LE +#else +#define GST_READ_UINT24 GST_READ_UINT24_BE +#define GST_WRITE_UINT24 GST_WRITE_UINT24_BE +#endif + +static void +gst_audio_resample_convert_buffer (GstAudioResample * resample, + const guint8 * in, guint8 * out, guint len, gboolean inverse) +{ + len *= resample->channels; + + if (inverse) { + if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) { + gint8 *o = (gint8 *) out; + gint16 *i = (gint16 *) in; + gint32 tmp; + + while (len) { + tmp = *i + (G_MAXINT8 >> 1); + *o = CLAMP (tmp >> 8, G_MININT8, G_MAXINT8); + o++; + i++; + len--; + } + } else if (!gst_audio_resample_use_int && resample->width == 8 + && !resample->fp) { + gint8 *o = (gint8 *) out; + gfloat *i = (gfloat *) in; + gfloat tmp; + + while (len) { + tmp = *i; + *o = (gint8) CLAMP (tmp * G_MAXINT8 + 0.5, G_MININT8, G_MAXINT8); + o++; + i++; + len--; + } + } else if (!gst_audio_resample_use_int && resample->width == 16 + && !resample->fp) { + gint16 *o = (gint16 *) out; + gfloat *i = (gfloat *) in; + gfloat tmp; + + while (len) { + tmp = *i; + *o = (gint16) CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16); + o++; + i++; + len--; + } + } else if (resample->width == 24 && !resample->fp) { + guint8 *o = (guint8 *) out; + gdouble *i = (gdouble *) in; + gdouble tmp; + + while (len) { + tmp = *i; + GST_WRITE_UINT24 (o, (gint32) CLAMP (tmp * GST_MAXINT24 + 0.5, + GST_MININT24, GST_MAXINT24)); + o += 3; + i++; + len--; + } + } else if (resample->width == 32 && !resample->fp) { + gint32 *o = (gint32 *) out; + gdouble *i = (gdouble *) in; + gdouble tmp; + + while (len) { + tmp = *i; + *o = (gint32) CLAMP (tmp * G_MAXINT32 + 0.5, G_MININT32, G_MAXINT32); + o++; + i++; + len--; + } + } else { + g_assert_not_reached (); + } + } else { + if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) { + gint8 *i = (gint8 *) in; + gint16 *o = (gint16 *) out; + gint32 tmp; + + while (len) { + tmp = *i; + *o = tmp << 8; + o++; + i++; + len--; + } + } else if (!gst_audio_resample_use_int && resample->width == 8 + && !resample->fp) { + gint8 *i = (gint8 *) in; + gfloat *o = (gfloat *) out; + gfloat tmp; + + while (len) { + tmp = *i; + *o = tmp / G_MAXINT8; + o++; + i++; + len--; + } + } else if (!gst_audio_resample_use_int && resample->width == 16 + && !resample->fp) { + gint16 *i = (gint16 *) in; + gfloat *o = (gfloat *) out; + gfloat tmp; + + while (len) { + tmp = *i; + *o = tmp / G_MAXINT16; + o++; + i++; + len--; + } + } else if (resample->width == 24 && !resample->fp) { + guint8 *i = (guint8 *) in; + gdouble *o = (gdouble *) out; + gdouble tmp; + guint32 tmp2; + + while (len) { + tmp2 = GST_READ_UINT24 (i); + if (tmp2 & 0x00800000) + tmp2 |= 0xff000000; + tmp = (gint32) tmp2; + *o = tmp / GST_MAXINT24; + o++; + i += 3; + len--; + } + } else if (resample->width == 32 && !resample->fp) { + gint32 *i = (gint32 *) in; + gdouble *o = (gdouble *) out; + gdouble tmp; + + while (len) { + tmp = *i; + *o = tmp / G_MAXINT32; + o++; + i++; + len--; + } + } else { + g_assert_not_reached (); + } + } +} + +static void +gst_audio_resample_push_drain (GstAudioResample * resample) { - GstAudioresample *audioresample; + GstBuffer *buf; + GstBaseTransform *trans = GST_BASE_TRANSFORM (resample); + GstFlowReturn res; + gint outsize; + guint out_len, out_processed; + gint err; + guint num, den, len; + guint8 *outtmp = NULL; + gboolean need_convert = FALSE; + + if (!resample->state) + return; + + need_convert = (resample->funcs->width != resample->width); + + resample->funcs->get_ratio (resample->state, &num, &den); + + out_len = resample->funcs->get_input_latency (resample->state); + out_len = out_processed = (out_len * den + num - 1) / num; + outsize = (resample->width / 8) * out_len * resample->channels; + + if (need_convert) { + guint outsize_tmp = + (resample->funcs->width / 8) * out_len * resample->channels; + if (outsize_tmp <= resample->tmp_out_size) { + outtmp = resample->tmp_out; + } else { + resample->tmp_out_size = outsize_tmp; + resample->tmp_out = outtmp = g_realloc (resample->tmp_out, outsize_tmp); + } + } + + res = + gst_pad_alloc_buffer_and_set_caps (trans->srcpad, GST_BUFFER_OFFSET_NONE, + outsize, GST_PAD_CAPS (trans->srcpad), &buf); + + if (G_UNLIKELY (res != GST_FLOW_OK)) { + GST_WARNING_OBJECT (resample, "failed allocating buffer of %d bytes", + outsize); + return; + } - audioresample = GST_AUDIORESAMPLE (base); + len = resample->funcs->get_input_latency (resample->state); + + err = + resample->funcs->process (resample->state, + NULL, &len, (need_convert) ? outtmp : GST_BUFFER_DATA (buf), + &out_processed); + + if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { + GST_WARNING_OBJECT (resample, "Failed to process drain: %s", + resample->funcs->strerror (err)); + gst_buffer_unref (buf); + return; + } + + if (G_UNLIKELY (out_processed == 0)) { + GST_WARNING_OBJECT (resample, "Failed to get drain, dropping buffer"); + gst_buffer_unref (buf); + return; + } + + /* If we wrote more than allocated something is really wrong now + * and we should better abort immediately */ + g_assert (out_len >= out_processed); + + if (need_convert) + gst_audio_resample_convert_buffer (resample, outtmp, GST_BUFFER_DATA (buf), + out_processed, TRUE); + + GST_BUFFER_DURATION (buf) = + GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate); + GST_BUFFER_SIZE (buf) = + out_processed * resample->channels * (resample->width / 8); + + if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { + GST_BUFFER_OFFSET (buf) = resample->next_offset; + GST_BUFFER_OFFSET_END (buf) = resample->next_offset + out_processed; + GST_BUFFER_TIMESTAMP (buf) = resample->next_ts; + + resample->next_ts += GST_BUFFER_DURATION (buf); + resample->next_offset += out_processed; + } + + GST_LOG_OBJECT (resample, + "Pushing drain buffer of %u bytes with timestamp %" GST_TIME_FORMAT + " duration %" GST_TIME_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %" + G_GUINT64_FORMAT, GST_BUFFER_SIZE (buf), + GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), + GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf), + GST_BUFFER_OFFSET_END (buf)); + + res = gst_pad_push (trans->srcpad, buf); + + if (G_UNLIKELY (res != GST_FLOW_OK)) + GST_WARNING_OBJECT (resample, "Failed to push drain: %s", + gst_flow_get_name (res)); + + return; +} + +static gboolean +gst_audio_resample_event (GstBaseTransform * base, GstEvent * event) +{ + GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_FLUSH_START: break; case GST_EVENT_FLUSH_STOP: - resample_input_flush (audioresample->resample); - audioresample->ts_offset = -1; - audioresample->next_ts = -1; - audioresample->offset = -1; - break; + gst_audio_resample_reset_state (resample); + resample->next_offset = -1; + resample->next_ts = -1; + resample->next_upstream_ts = -1; case GST_EVENT_NEWSEGMENT: - resample_input_pushthrough (audioresample->resample); - audioresample_pushthrough (audioresample); - audioresample->ts_offset = -1; - audioresample->next_ts = -1; - audioresample->offset = -1; + gst_audio_resample_push_drain (resample); + gst_audio_resample_reset_state (resample); + resample->next_offset = -1; + resample->next_ts = -1; + resample->next_upstream_ts = -1; break; - case GST_EVENT_EOS: - resample_input_eos (audioresample->resample); - audioresample_pushthrough (audioresample); + case GST_EVENT_EOS:{ + gst_audio_resample_push_drain (resample); + gst_audio_resample_reset_state (resample); break; + } default: break; } - parent_class->event (base, event); - - return TRUE; -} - -static GstFlowReturn -audioresample_do_output (GstAudioresample * audioresample, GstBuffer * outbuf) -{ - int outsize; - int outsamples; - ResampleState *r; - - r = audioresample->resample; - - outsize = resample_get_output_size (r); - GST_LOG_OBJECT (audioresample, "audioresample can give me %d bytes", outsize); - - /* protect against mem corruption */ - if (outsize > GST_BUFFER_SIZE (outbuf)) { - GST_WARNING_OBJECT (audioresample, - "overriding audioresample's outsize %d with outbuffer's size %d", - outsize, GST_BUFFER_SIZE (outbuf)); - outsize = GST_BUFFER_SIZE (outbuf); - } - /* catch possibly wrong size differences */ - if (GST_BUFFER_SIZE (outbuf) - outsize > r->sample_size) { - GST_WARNING_OBJECT (audioresample, - "audioresample's outsize %d too far from outbuffer's size %d", - outsize, GST_BUFFER_SIZE (outbuf)); - } - - outsize = resample_get_output_data (r, GST_BUFFER_DATA (outbuf), outsize); - outsamples = outsize / r->sample_size; - GST_LOG_OBJECT (audioresample, "resample gave me %d bytes or %d samples", - outsize, outsamples); - - GST_BUFFER_OFFSET (outbuf) = audioresample->offset; - GST_BUFFER_TIMESTAMP (outbuf) = audioresample->next_ts; - - if (audioresample->ts_offset != -1) { - audioresample->offset += outsamples; - audioresample->ts_offset += outsamples; - audioresample->next_ts = - gst_util_uint64_scale_int (audioresample->ts_offset, GST_SECOND, - audioresample->o_rate); - GST_BUFFER_OFFSET_END (outbuf) = audioresample->offset; - - /* we calculate DURATION as the difference between "next" timestamp - * and current timestamp so we ensure a contiguous stream, instead of - * having rounding errors. */ - GST_BUFFER_DURATION (outbuf) = audioresample->next_ts - - GST_BUFFER_TIMESTAMP (outbuf); - } else { - /* no valid offset know, we can still sortof calculate the duration though */ - GST_BUFFER_DURATION (outbuf) = - gst_util_uint64_scale_int (outsamples, GST_SECOND, - audioresample->o_rate); - } - - /* check for possible mem corruption */ - if (outsize > GST_BUFFER_SIZE (outbuf)) { - /* this is an error that when it happens, would need fixing in the - * resample library; we told it we wanted only GST_BUFFER_SIZE (outbuf), - * and it gave us more ! */ - GST_WARNING_OBJECT (audioresample, - "audioresample, you memory corrupting bastard. " - "you gave me outsize %d while my buffer was size %d", - outsize, GST_BUFFER_SIZE (outbuf)); - return GST_FLOW_ERROR; - } - /* catch possibly wrong size differences */ - if (GST_BUFFER_SIZE (outbuf) - outsize > r->sample_size) { - GST_WARNING_OBJECT (audioresample, - "audioresample's written outsize %d too far from outbuffer's size %d", - outsize, GST_BUFFER_SIZE (outbuf)); - } - GST_BUFFER_SIZE (outbuf) = outsize; - - if (G_UNLIKELY (audioresample->need_discont)) { - GST_DEBUG_OBJECT (audioresample, - "marking this buffer with the DISCONT flag"); - GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT); - audioresample->need_discont = FALSE; - } - - GST_LOG_OBJECT (audioresample, "transformed to buffer of %d bytes, ts %" - GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %" - G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT, - outsize, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), - GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), - GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf)); - - return GST_FLOW_OK; + return parent_class->event (base, event); } static gboolean -audioresample_check_discont (GstAudioresample * audioresample, +gst_audio_resample_check_discont (GstAudioResample * resample, GstClockTime timestamp) { if (timestamp != GST_CLOCK_TIME_NONE && - audioresample->prev_ts != GST_CLOCK_TIME_NONE && - audioresample->prev_duration != GST_CLOCK_TIME_NONE && - timestamp != audioresample->prev_ts + audioresample->prev_duration) { + resample->next_upstream_ts != GST_CLOCK_TIME_NONE && + timestamp != resample->next_upstream_ts) { /* Potentially a discontinuous buffer. However, it turns out that many * elements generate imperfect streams due to rounding errors, so we permit * a small error (up to one sample) without triggering a filter * flush/restart (if triggered incorrectly, this will be audible) */ - GstClockTimeDiff diff = timestamp - - (audioresample->prev_ts + audioresample->prev_duration); + GstClockTimeDiff diff = timestamp - resample->next_upstream_ts; - if (ABS (diff) > GST_SECOND / audioresample->i_rate) { - GST_WARNING_OBJECT (audioresample, - "encountered timestamp discontinuity of %" G_GINT64_FORMAT, diff); + if (ABS (diff) > (GST_SECOND + resample->inrate - 1) / resample->inrate) { + GST_WARNING_OBJECT (resample, + "encountered timestamp discontinuity of %s%" GST_TIME_FORMAT, + (diff < 0) ? "-" : "", GST_TIME_ARGS ((GstClockTime) ABS (diff))); return TRUE; } } @@ -605,23 +890,136 @@ audioresample_check_discont (GstAudioresample * audioresample, } static GstFlowReturn -audioresample_transform (GstBaseTransform * base, GstBuffer * inbuf, +gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, GstBuffer * outbuf) { - GstAudioresample *audioresample; - ResampleState *r; - guchar *data, *datacopy; + guint32 in_len, in_processed; + guint32 out_len, out_processed; + gint err = RESAMPLER_ERR_SUCCESS; + guint8 *in_tmp = NULL, *out_tmp = NULL; + gboolean need_convert = (resample->funcs->width != resample->width); + + in_len = GST_BUFFER_SIZE (inbuf) / resample->channels; + out_len = GST_BUFFER_SIZE (outbuf) / resample->channels; + + in_len /= (resample->width / 8); + out_len /= (resample->width / 8); + + in_processed = in_len; + out_processed = out_len; + + if (need_convert) { + guint in_size_tmp = + in_len * resample->channels * (resample->funcs->width / 8); + guint out_size_tmp = + out_len * resample->channels * (resample->funcs->width / 8); + + if (in_size_tmp <= resample->tmp_in_size) { + in_tmp = resample->tmp_in; + } else { + resample->tmp_in = in_tmp = g_realloc (resample->tmp_in, in_size_tmp); + resample->tmp_in_size = in_size_tmp; + } + + gst_audio_resample_convert_buffer (resample, GST_BUFFER_DATA (inbuf), + in_tmp, in_len, FALSE); + + if (out_size_tmp <= resample->tmp_out_size) { + out_tmp = resample->tmp_out; + } else { + resample->tmp_out = out_tmp = g_realloc (resample->tmp_out, out_size_tmp); + resample->tmp_out_size = out_size_tmp; + } + } + + if (need_convert) { + err = resample->funcs->process (resample->state, + in_tmp, &in_processed, out_tmp, &out_processed); + } else { + err = resample->funcs->process (resample->state, + (const guint8 *) GST_BUFFER_DATA (inbuf), &in_processed, + (guint8 *) GST_BUFFER_DATA (outbuf), &out_processed); + } + + if (G_UNLIKELY (in_len != in_processed)) + GST_WARNING_OBJECT (resample, "Converted %d of %d input samples", + in_processed, in_len); + + if (out_len != out_processed) { + if (out_processed == 0) { + GST_DEBUG_OBJECT (resample, "Converted to 0 samples, buffer dropped"); + + return GST_BASE_TRANSFORM_FLOW_DROPPED; + } + + /* If we wrote more than allocated something is really wrong now + * and we should better abort immediately */ + g_assert (out_len >= out_processed); + } + + if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { + GST_ERROR_OBJECT (resample, "Failed to convert data: %s", + resample->funcs->strerror (err)); + return GST_FLOW_ERROR; + } else { + + if (need_convert) + gst_audio_resample_convert_buffer (resample, out_tmp, + GST_BUFFER_DATA (outbuf), out_processed, TRUE); + + GST_BUFFER_DURATION (outbuf) = + GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate); + GST_BUFFER_SIZE (outbuf) = + out_processed * resample->channels * (resample->width / 8); + + if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { + GST_BUFFER_TIMESTAMP (outbuf) = resample->next_ts; + GST_BUFFER_OFFSET (outbuf) = resample->next_offset; + GST_BUFFER_OFFSET_END (outbuf) = resample->next_offset + out_processed; + + resample->next_ts += GST_BUFFER_DURATION (outbuf); + resample->next_offset += out_processed; + } + + GST_LOG_OBJECT (resample, + "Converted to buffer of %u bytes with timestamp %" GST_TIME_FORMAT + ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT + ", offset_end %" G_GUINT64_FORMAT, GST_BUFFER_SIZE (outbuf), + GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), + GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), + GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf)); + + return GST_FLOW_OK; + } +} + +static GstFlowReturn +gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, + GstBuffer * outbuf) +{ + GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); + guint8 *data; gulong size; GstClockTime timestamp; - - audioresample = GST_AUDIORESAMPLE (base); - r = audioresample->resample; + guint outsamples, insamples; + GstFlowReturn ret; + + if (resample->state == NULL) { + if (G_UNLIKELY (!(resample->state = + gst_audio_resample_init_state (resample, resample->width, + resample->channels, resample->inrate, resample->outrate, + resample->quality, resample->fp)))) + return GST_FLOW_ERROR; + + resample->funcs = + gst_audio_resample_get_funcs (resample->width, resample->fp); + } data = GST_BUFFER_DATA (inbuf); size = GST_BUFFER_SIZE (inbuf); timestamp = GST_BUFFER_TIMESTAMP (inbuf); - GST_LOG_OBJECT (audioresample, "transforming buffer of %ld bytes, ts %" + GST_LOG_OBJECT (resample, "transforming buffer of %ld bytes, ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT, size, GST_TIME_ARGS (timestamp), @@ -629,88 +1027,57 @@ audioresample_transform (GstBaseTransform * base, GstBuffer * inbuf, GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf)); /* check for timestamp discontinuities and flush/reset if needed */ - if (G_UNLIKELY (audioresample_check_discont (audioresample, timestamp))) { + if (G_UNLIKELY (gst_audio_resample_check_discont (resample, timestamp) + || GST_BUFFER_IS_DISCONT (inbuf))) { /* Flush internal samples */ - audioresample_pushthrough (audioresample); + gst_audio_resample_reset_state (resample); /* Inform downstream element about discontinuity */ - audioresample->need_discont = TRUE; - /* We want to recalculate the offset */ - audioresample->ts_offset = -1; - } - - if (audioresample->ts_offset == -1) { - /* if we don't know the initial offset yet, calculate it based on the - * input timestamp. */ - if (GST_CLOCK_TIME_IS_VALID (timestamp)) { - GstClockTime stime; - - /* offset used to calculate the timestamps. We use the sample offset for - * this to make it more accurate. We want the first buffer to have the - * same timestamp as the incoming timestamp. */ - audioresample->next_ts = timestamp; - audioresample->ts_offset = - gst_util_uint64_scale_int (timestamp, r->o_rate, GST_SECOND); - /* offset used to set as the buffer offset, this offset is always - * relative to the stream time, note that timestamp is not... */ - stime = (timestamp - base->segment.start) + base->segment.time; - audioresample->offset = - gst_util_uint64_scale_int (stime, r->o_rate, GST_SECOND); - } + resample->need_discont = TRUE; + /* We want to recalculate the timestamps */ + resample->next_ts = -1; + resample->next_upstream_ts = -1; + resample->next_offset = -1; } - audioresample->prev_ts = timestamp; - audioresample->prev_duration = GST_BUFFER_DURATION (inbuf); - /* need to memdup, resample takes ownership. */ - datacopy = g_memdup (data, size); - resample_add_input_data (r, datacopy, size, g_free, datacopy); - - return audioresample_do_output (audioresample, outbuf); -} - -/* push remaining data in the buffers out */ -static GstFlowReturn -audioresample_pushthrough (GstAudioresample * audioresample) -{ - int outsize; - ResampleState *r; - GstBuffer *outbuf; - GstFlowReturn res = GST_FLOW_OK; - GstBaseTransform *trans; + insamples = GST_BUFFER_SIZE (inbuf) / resample->channels; + insamples /= (resample->width / 8); - r = audioresample->resample; + outsamples = GST_BUFFER_SIZE (outbuf) / resample->channels; + outsamples /= (resample->width / 8); - outsize = resample_get_output_size (r); - if (outsize == 0) { - GST_DEBUG_OBJECT (audioresample, "no internal buffers needing flush"); - goto done; + if (GST_CLOCK_TIME_IS_VALID (timestamp) + && !GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { + resample->next_ts = timestamp; + resample->next_offset = + GST_CLOCK_TIME_TO_FRAMES (timestamp, resample->outrate); } - trans = GST_BASE_TRANSFORM (audioresample); - - res = gst_pad_alloc_buffer (trans->srcpad, GST_BUFFER_OFFSET_NONE, outsize, - GST_PAD_CAPS (trans->srcpad), &outbuf); - if (G_UNLIKELY (res != GST_FLOW_OK)) { - GST_WARNING_OBJECT (audioresample, "failed allocating buffer of %d bytes", - outsize); - goto done; + if (G_UNLIKELY (resample->need_discont)) { + GST_DEBUG_OBJECT (resample, "marking this buffer with the DISCONT flag"); + GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT); + resample->need_discont = FALSE; } - res = audioresample_do_output (audioresample, outbuf); - if (G_UNLIKELY (res != GST_FLOW_OK)) - goto done; + ret = gst_audio_resample_process (resample, inbuf, outbuf); + if (G_UNLIKELY (ret != GST_FLOW_OK)) + return ret; - res = gst_pad_push (trans->srcpad, outbuf); + if (GST_CLOCK_TIME_IS_VALID (timestamp) + && !GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts)) + resample->next_upstream_ts = timestamp; -done: - return res; + if (GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts)) + resample->next_upstream_ts += + GST_FRAMES_TO_CLOCK_TIME (insamples, resample->inrate); + + return GST_FLOW_OK; } static gboolean -audioresample_query (GstPad * pad, GstQuery * query) +gst_audio_resample_query (GstPad * pad, GstQuery * query) { - GstAudioresample *audioresample = - GST_AUDIORESAMPLE (gst_pad_get_parent (pad)); - GstBaseTransform *trans = GST_BASE_TRANSFORM (audioresample); + GstAudioResample *resample = GST_AUDIO_RESAMPLE (gst_pad_get_parent (pad)); + GstBaseTransform *trans = GST_BASE_TRANSFORM (resample); gboolean res = TRUE; switch (GST_QUERY_TYPE (query)) { @@ -720,8 +1087,14 @@ audioresample_query (GstPad * pad, GstQuery * query) gboolean live; guint64 latency; GstPad *peer; - gint rate = audioresample->i_rate; - gint resampler_latency = audioresample->filter_length / 2; + gint rate = resample->inrate; + gint resampler_latency; + + if (resample->state) + resampler_latency = + resample->funcs->get_input_latency (resample->state); + else + resampler_latency = 0; if (gst_base_transform_is_passthrough (trans)) resampler_latency = 0; @@ -730,7 +1103,7 @@ audioresample_query (GstPad * pad, GstQuery * query) if ((res = gst_pad_query (peer, query))) { gst_query_parse_latency (query, &live, &min, &max); - GST_DEBUG ("Peer latency: min %" + GST_DEBUG_OBJECT (resample, "Peer latency: min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT, GST_TIME_ARGS (min), GST_TIME_ARGS (max)); @@ -741,13 +1114,14 @@ audioresample_query (GstPad * pad, GstQuery * query) else latency = 0; - GST_DEBUG ("Our latency: %" GST_TIME_FORMAT, GST_TIME_ARGS (latency)); + GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT, + GST_TIME_ARGS (latency)); min += latency; if (max != GST_CLOCK_TIME_NONE) max += latency; - GST_DEBUG ("Calculated total latency : min %" + GST_DEBUG_OBJECT (resample, "Calculated total latency : min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT, GST_TIME_ARGS (min), GST_TIME_ARGS (max)); @@ -761,12 +1135,12 @@ audioresample_query (GstPad * pad, GstQuery * query) res = gst_pad_query_default (pad, query); break; } - gst_object_unref (audioresample); + gst_object_unref (resample); return res; } static const GstQueryType * -audioresample_query_type (GstPad * pad) +gst_audio_resample_query_type (GstPad * pad) { static const GstQueryType types[] = { GST_QUERY_LATENCY, @@ -777,25 +1151,55 @@ audioresample_query_type (GstPad * pad) } static void -gst_audioresample_set_property (GObject * object, guint prop_id, +gst_audio_resample_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { - GstAudioresample *audioresample; + GstAudioResample *resample; - audioresample = GST_AUDIORESAMPLE (object); + resample = GST_AUDIO_RESAMPLE (object); switch (prop_id) { - case PROP_FILTERLEN: - audioresample->filter_length = g_value_get_int (value); - GST_DEBUG_OBJECT (GST_ELEMENT (audioresample), "new filter length %d", - audioresample->filter_length); - if (audioresample->resample) { - resample_set_filter_length (audioresample->resample, - audioresample->filter_length); - gst_element_post_message (GST_ELEMENT (audioresample), - gst_message_new_latency (GST_OBJECT (audioresample))); - } + case PROP_QUALITY: + resample->quality = g_value_get_int (value); + GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality); + + gst_audio_resample_update_state (resample, resample->width, + resample->channels, resample->inrate, resample->outrate, + resample->quality, resample->fp); + break; + case PROP_FILTER_LENGTH:{ + gint filter_length = g_value_get_int (value); + + if (filter_length <= 8) + resample->quality = 0; + else if (filter_length <= 16) + resample->quality = 1; + else if (filter_length <= 32) + resample->quality = 2; + else if (filter_length <= 48) + resample->quality = 3; + else if (filter_length <= 64) + resample->quality = 4; + else if (filter_length <= 80) + resample->quality = 5; + else if (filter_length <= 96) + resample->quality = 6; + else if (filter_length <= 128) + resample->quality = 7; + else if (filter_length <= 160) + resample->quality = 8; + else if (filter_length <= 192) + resample->quality = 9; + else + resample->quality = 10; + + GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality); + + gst_audio_resample_update_state (resample, resample->width, + resample->channels, resample->inrate, resample->outrate, + resample->quality, resample->fp); break; + } default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -803,16 +1207,53 @@ gst_audioresample_set_property (GObject * object, guint prop_id, } static void -gst_audioresample_get_property (GObject * object, guint prop_id, +gst_audio_resample_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { - GstAudioresample *audioresample; + GstAudioResample *resample; - audioresample = GST_AUDIORESAMPLE (object); + resample = GST_AUDIO_RESAMPLE (object); switch (prop_id) { - case PROP_FILTERLEN: - g_value_set_int (value, audioresample->filter_length); + case PROP_QUALITY: + g_value_set_int (value, resample->quality); + break; + case PROP_FILTER_LENGTH: + switch (resample->quality) { + case 0: + g_value_set_int (value, 8); + break; + case 1: + g_value_set_int (value, 16); + break; + case 2: + g_value_set_int (value, 32); + break; + case 3: + g_value_set_int (value, 48); + break; + case 4: + g_value_set_int (value, 64); + break; + case 5: + g_value_set_int (value, 80); + break; + case 6: + g_value_set_int (value, 96); + break; + case 7: + g_value_set_int (value, 128); + break; + case 8: + g_value_set_int (value, 160); + break; + case 9: + g_value_set_int (value, 192); + break; + case 10: + g_value_set_int (value, 256); + break; + } break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -820,14 +1261,136 @@ gst_audioresample_get_property (GObject * object, guint prop_id, } } +#define BENCHMARK_SIZE 512 + +static gboolean +_benchmark_int_float (SpeexResamplerState * st) +{ + gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2]; + gfloat in_tmp[BENCHMARK_SIZE], out_tmp[BENCHMARK_SIZE / 2]; + gint i; + guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2; + + for (i = 0; i < BENCHMARK_SIZE; i++) { + gfloat tmp = in[i]; + in_tmp[i] = tmp / G_MAXINT16; + } + + resample_float_resampler_process_interleaved_float (st, + (const guint8 *) in_tmp, &inlen, (guint8 *) out_tmp, &outlen); + + if (outlen == 0) { + GST_ERROR ("Failed to use float resampler"); + return FALSE; + } + + for (i = 0; i < outlen; i++) { + gfloat tmp = out_tmp[i]; + out[i] = CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16); + } + + return TRUE; +} + +static gboolean +_benchmark_int_int (SpeexResamplerState * st) +{ + gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2]; + guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2; + + resample_int_resampler_process_interleaved_int (st, (const guint8 *) in, + &inlen, (guint8 *) out, &outlen); + + if (outlen == 0) { + GST_ERROR ("Failed to use int resampler"); + return FALSE; + } + + return TRUE; +} + +static gboolean +_benchmark_integer_resampling (void) +{ + OilProfile a, b; + gdouble av, bv; + SpeexResamplerState *sta, *stb; + + oil_profile_init (&a); + oil_profile_init (&b); + + sta = resample_float_resampler_init (1, 48000, 24000, 4, NULL); + if (sta == NULL) { + GST_ERROR ("Failed to create float resampler state"); + return FALSE; + } + + stb = resample_int_resampler_init (1, 48000, 24000, 4, NULL); + if (stb == NULL) { + resample_float_resampler_destroy (sta); + GST_ERROR ("Failed to create int resampler state"); + return FALSE; + } + + /* Warm up cache */ + if (!_benchmark_int_float (sta)) + goto error; + if (!_benchmark_int_float (sta)) + goto error; + + /* Benchmark */ + oil_profile_start (&a); + if (!_benchmark_int_float (sta)) + goto error; + oil_profile_stop (&a); + + /* Warm up cache */ + if (!_benchmark_int_int (stb)) + goto error; + if (!_benchmark_int_int (stb)) + goto error; + + /* Benchmark */ + oil_profile_start (&b); + if (!_benchmark_int_int (stb)) + goto error; + oil_profile_stop (&b); + + /* Handle results */ + oil_profile_get_ave_std (&a, &av, NULL); + oil_profile_get_ave_std (&b, &bv, NULL); + + gst_audio_resample_use_int = (av > bv); + resample_float_resampler_destroy (sta); + resample_float_resampler_destroy (stb); + + if (av > bv) + GST_DEBUG ("Using integer resampler if appropiate: %lf < %lf", bv, av); + else + GST_DEBUG ("Using float resampler for everything: %lf <= %lf", av, bv); + + return TRUE; + +error: + resample_float_resampler_destroy (sta); + resample_float_resampler_destroy (stb); + + return FALSE; +} static gboolean plugin_init (GstPlugin * plugin) { - resample_init (); + GST_DEBUG_CATEGORY_INIT (audio_resample_debug, "audioresample", 0, + "audio resampling element"); + + oil_init (); + + if (!_benchmark_integer_resampling ()) + return FALSE; if (!gst_element_register (plugin, "audioresample", GST_RANK_PRIMARY, - GST_TYPE_AUDIORESAMPLE)) { + GST_TYPE_AUDIO_RESAMPLE)) { return FALSE; } diff --git a/gst/audioresample/gstaudioresample.h b/gst/audioresample/gstaudioresample.h index c969ccd..fb6ef6c 100644 --- a/gst/audioresample/gstaudioresample.h +++ b/gst/audioresample/gstaudioresample.h @@ -1,5 +1,6 @@ /* GStreamer * Copyright (C) <1999> Erik Walthinsen + * Copyright (C) <2007-2008> Sebastian Dröge * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -18,62 +19,72 @@ */ -#ifndef __AUDIORESAMPLE_H__ -#define __AUDIORESAMPLE_H__ +#ifndef __AUDIO_RESAMPLE_H__ +#define __AUDIO_RESAMPLE_H__ #include #include +#include -#include "resample.h" +#include "speex_resampler_wrapper.h" G_BEGIN_DECLS -#define GST_TYPE_AUDIORESAMPLE \ - (gst_audioresample_get_type()) -#define GST_AUDIORESAMPLE(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIORESAMPLE,GstAudioresample)) -#define GST_AUDIORESAMPLE_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIORESAMPLE,GstAudioresampleClass)) -#define GST_IS_AUDIORESAMPLE(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIORESAMPLE)) -#define GST_IS_AUDIORESAMPLE_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIORESAMPLE)) +#define GST_TYPE_AUDIO_RESAMPLE \ + (gst_audio_resample_get_type()) +#define GST_AUDIO_RESAMPLE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_RESAMPLE,GstAudioResample)) +#define GST_AUDIO_RESAMPLE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_RESAMPLE,GstAudioResampleClass)) +#define GST_IS_AUDIO_RESAMPLE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_RESAMPLE)) +#define GST_IS_AUDIO_RESAMPLE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_RESAMPLE)) -typedef struct _GstAudioresample GstAudioresample; -typedef struct _GstAudioresampleClass GstAudioresampleClass; +typedef struct _GstAudioResample GstAudioResample; +typedef struct _GstAudioResampleClass GstAudioResampleClass; /** - * GstAudioresample: + * GstAudioResample: * * Opaque data structure. */ -struct _GstAudioresample { +struct _GstAudioResample { GstBaseTransform element; + /* */ + GstCaps *srccaps, *sinkcaps; - gboolean passthru; gboolean need_discont; - guint64 offset; - guint64 ts_offset; + guint64 next_offset; GstClockTime next_ts; - GstClockTime prev_ts, prev_duration; - int channels; - - int i_rate; - int o_rate; - int filter_length; - - ResampleState * resample; + GstClockTime next_upstream_ts; + + gint channels; + gint inrate; + gint outrate; + gint quality; + gint width; + gboolean fp; + + guint8 *tmp_in; + guint tmp_in_size; + + guint8 *tmp_out; + guint tmp_out_size; + + SpeexResamplerState *state; + const SpeexResampleFuncs *funcs; }; -struct _GstAudioresampleClass { +struct _GstAudioResampleClass { GstBaseTransformClass parent_class; }; -GType gst_audioresample_get_type(void); +GType gst_audio_resample_get_type(void); G_END_DECLS -#endif /* __AUDIORESAMPLE_H__ */ +#endif /* __AUDIO_RESAMPLE_H__ */ diff --git a/gst/audioresample/resample.c b/gst/audioresample/resample.c index c464adf..f29e614 100644 --- a/gst/audioresample/resample.c +++ b/gst/audioresample/resample.c @@ -1,317 +1,1340 @@ -/* Resampling library - * Copyright (C) <2001> David A. Schleef - * - * 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 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. - */ +/* Copyright (C) 2007-2008 Jean-Marc Valin + Copyright (C) 2008 Thorvald Natvig + + File: resample.c + Arbitrary resampling code + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. 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. + + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. +*/ + +/* + The design goals of this code are: + - Very fast algorithm + - SIMD-friendly algorithm + - Low memory requirement + - Good *perceptual* quality (and not best SNR) + + Warning: This resampler is relatively new. Although I think I got rid of + all the major bugs and I don't expect the API to change anymore, there + may be something I've missed. So use with caution. + + This algorithm is based on this original resampling algorithm: + Smith, Julius O. Digital Audio Resampling Home Page + Center for Computer Research in Music and Acoustics (CCRMA), + Stanford University, 2007. + Web published at http://www-ccrma.stanford.edu/~jos/resample/. + + There is one main difference, though. This resampler uses cubic + interpolation instead of linear interpolation in the above paper. This + makes the table much smaller and makes it possible to compute that table + on a per-stream basis. In turn, being able to tweak the table for each + stream makes it possible to both reduce complexity on simple ratios + (e.g. 2/3), and get rid of the rounding operations in the inner loop. + The latter both reduces CPU time and makes the algorithm more SIMD-friendly. +*/ #ifdef HAVE_CONFIG_H -#include +#include "config.h" #endif +#ifdef OUTSIDE_SPEEX +#include + +#include + +#define EXPORT G_GNUC_INTERNAL + +static inline void * +speex_alloc (int size) +{ + return g_malloc0 (size); +} + +static inline void * +speex_realloc (void *ptr, int size) +{ + return g_realloc (ptr, size); +} + +static inline void +speex_free (void *ptr) +{ + g_free (ptr); +} + +#include "speex_resampler.h" +#include "arch.h" +#else /* OUTSIDE_SPEEX */ + +#include "../include/speex/speex_resampler.h" +#include "arch.h" +#include "os_support.h" +#endif /* OUTSIDE_SPEEX */ -#include #include -#include -#include -#include -#include -#include "resample.h" -#include "buffer.h" -#include "debug.h" +#ifndef M_PI +#define M_PI 3.14159263 +#endif + +#ifdef FIXED_POINT +#define WORD2INT(x) ((x) < -32767 ? -32768 : ((x) > 32766 ? 32767 : (x))) +#else +#define WORD2INT(x) ((x) < -32767.5f ? -32768 : ((x) > 32766.5f ? 32767 : floor(.5+(x)))) +#endif -void resample_scale_ref (ResampleState * r); -void resample_scale_functable (ResampleState * r); +#define IMAX(a,b) ((a) > (b) ? (a) : (b)) +#define IMIN(a,b) ((a) < (b) ? (a) : (b)) -GST_DEBUG_CATEGORY (libaudioresample_debug); +#ifndef NULL +#define NULL 0 +#endif -void -resample_init (void) +#ifdef _USE_SSE +#include "resample_sse.h" +#endif + +/* Numer of elements to allocate on the stack */ +#ifdef VAR_ARRAYS +#define FIXED_STACK_ALLOC 8192 +#else +#define FIXED_STACK_ALLOC 1024 +#endif + +typedef int (*resampler_basic_func) (SpeexResamplerState *, spx_uint32_t, + const spx_word16_t *, spx_uint32_t *, spx_word16_t *, spx_uint32_t *); + +struct SpeexResamplerState_ { - static int inited = 0; + spx_uint32_t in_rate; + spx_uint32_t out_rate; + spx_uint32_t num_rate; + spx_uint32_t den_rate; + + int quality; + spx_uint32_t nb_channels; + spx_uint32_t filt_len; + spx_uint32_t mem_alloc_size; + spx_uint32_t buffer_size; + int int_advance; + int frac_advance; + float cutoff; + spx_uint32_t oversample; + int initialised; + int started; + + /* These are per-channel */ + spx_int32_t *last_sample; + spx_uint32_t *samp_frac_num; + spx_uint32_t *magic_samples; + + spx_word16_t *mem; + spx_word16_t *sinc_table; + spx_uint32_t sinc_table_length; + resampler_basic_func resampler_ptr; + + int in_stride; + int out_stride; +}; - if (!inited) { - oil_init (); - inited = 1; - GST_DEBUG_CATEGORY_INIT (libaudioresample_debug, "libaudioresample", 0, - "audio resampling library"); +static double kaiser12_table[68] = { + 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076, + 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014, + 0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601, + 0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014, + 0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490, + 0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546, + 0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178, + 0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947, + 0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058, + 0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438, + 0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734, + 0.00001000, 0.00000000 +}; +/* +static double kaiser12_table[36] = { + 0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741, + 0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762, + 0.49704014, 0.43304576, 0.37206735, 0.31506490, 0.26276832, 0.21567274, + 0.17404546, 0.13794294, 0.10723616, 0.08164178, 0.06075685, 0.04409466, + 0.03111947, 0.02127838, 0.01402878, 0.00886058, 0.00531256, 0.00298291, + 0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000}; +*/ +static double kaiser10_table[36] = { + 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446, + 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347, + 0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962, + 0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451, + 0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739, + 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000 +}; + +static double kaiser8_table[36] = { + 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200, + 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126, + 0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272, + 0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758, + 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490, + 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000 +}; + +static double kaiser6_table[36] = { + 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003, + 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565, + 0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561, + 0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058, + 0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600, + 0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000 +}; + +struct FuncDef +{ + double *table; + int oversample; +}; + +static struct FuncDef _KAISER12 = { kaiser12_table, 64 }; + +#define KAISER12 (&_KAISER12) +/*static struct FuncDef _KAISER12 = {kaiser12_table, 32}; +#define KAISER12 (&_KAISER12)*/ +static struct FuncDef _KAISER10 = { kaiser10_table, 32 }; + +#define KAISER10 (&_KAISER10) +static struct FuncDef _KAISER8 = { kaiser8_table, 32 }; + +#define KAISER8 (&_KAISER8) +static struct FuncDef _KAISER6 = { kaiser6_table, 32 }; + +#define KAISER6 (&_KAISER6) + +struct QualityMapping +{ + int base_length; + int oversample; + float downsample_bandwidth; + float upsample_bandwidth; + struct FuncDef *window_func; +}; + + +/* This table maps conversion quality to internal parameters. There are two + reasons that explain why the up-sampling bandwidth is larger than the + down-sampling bandwidth: + 1) When up-sampling, we can assume that the spectrum is already attenuated + close to the Nyquist rate (from an A/D or a previous resampling filter) + 2) Any aliasing that occurs very close to the Nyquist rate will be masked + by the sinusoids/noise just below the Nyquist rate (guaranteed only for + up-sampling). +*/ +static const struct QualityMapping quality_map[11] = { + {8, 4, 0.830f, 0.860f, KAISER6}, /* Q0 */ + {16, 4, 0.850f, 0.880f, KAISER6}, /* Q1 */ + {32, 4, 0.882f, 0.910f, KAISER6}, /* Q2 *//* 82.3% cutoff ( ~60 dB stop) 6 */ + {48, 8, 0.895f, 0.917f, KAISER8}, /* Q3 *//* 84.9% cutoff ( ~80 dB stop) 8 */ + {64, 8, 0.921f, 0.940f, KAISER8}, /* Q4 *//* 88.7% cutoff ( ~80 dB stop) 8 */ + {80, 16, 0.922f, 0.940f, KAISER10}, /* Q5 *//* 89.1% cutoff (~100 dB stop) 10 */ + {96, 16, 0.940f, 0.945f, KAISER10}, /* Q6 *//* 91.5% cutoff (~100 dB stop) 10 */ + {128, 16, 0.950f, 0.950f, KAISER10}, /* Q7 *//* 93.1% cutoff (~100 dB stop) 10 */ + {160, 16, 0.960f, 0.960f, KAISER10}, /* Q8 *//* 94.5% cutoff (~100 dB stop) 10 */ + {192, 32, 0.968f, 0.968f, KAISER12}, /* Q9 *//* 95.5% cutoff (~100 dB stop) 10 */ + {256, 32, 0.975f, 0.975f, KAISER12}, /* Q10 *//* 96.6% cutoff (~100 dB stop) 10 */ +}; + +/*8,24,40,56,80,104,128,160,200,256,320*/ +#ifdef DOUBLE_PRECISION +static double +compute_func (double x, struct FuncDef *func) +{ + double y, frac; +#else +static double +compute_func (float x, struct FuncDef *func) +{ + float y, frac; +#endif + double interp[4]; + int ind; + y = x * func->oversample; + ind = (int) floor (y); + frac = (y - ind); + /* CSE with handle the repeated powers */ + interp[3] = -0.1666666667 * frac + 0.1666666667 * (frac * frac * frac); + interp[2] = frac + 0.5 * (frac * frac) - 0.5 * (frac * frac * frac); + /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac; */ + interp[0] = + -0.3333333333 * frac + 0.5 * (frac * frac) - + 0.1666666667 * (frac * frac * frac); + /* Just to make sure we don't have rounding problems */ + interp[1] = 1.f - interp[3] - interp[2] - interp[0]; + + /*sum = frac*accum[1] + (1-frac)*accum[2]; */ + return interp[0] * func->table[ind] + interp[1] * func->table[ind + 1] + + interp[2] * func->table[ind + 2] + interp[3] * func->table[ind + 3]; +} + +#if 0 +#include +int +main (int argc, char **argv) +{ + int i; + for (i = 0; i < 256; i++) { + printf ("%f\n", compute_func (i / 256., KAISER12)); } + return 0; +} +#endif + +#ifdef FIXED_POINT +/* The slow way of computing a sinc for the table. Should improve that some day */ +static spx_word16_t +sinc (float cutoff, float x, int N, struct FuncDef *window_func) +{ + /*fprintf (stderr, "%f ", x); */ + float xx = x * cutoff; + if (fabs (x) < 1e-6f) + return WORD2INT (32768. * cutoff); + else if (fabs (x) > .5f * N) + return 0; + /*FIXME: Can it really be any slower than this? */ + return WORD2INT (32768. * cutoff * sin (M_PI * xx) / (M_PI * xx) * + compute_func (fabs (2. * x / N), window_func)); +} +#else +/* The slow way of computing a sinc for the table. Should improve that some day */ +#ifdef DOUBLE_PRECISION +static spx_word16_t +sinc (double cutoff, double x, int N, struct FuncDef *window_func) +{ + /*fprintf (stderr, "%f ", x); */ + double xx = x * cutoff; +#else +static spx_word16_t +sinc (float cutoff, float x, int N, struct FuncDef *window_func) +{ + /*fprintf (stderr, "%f ", x); */ + float xx = x * cutoff; +#endif + if (fabs (x) < 1e-6) + return cutoff; + else if (fabs (x) > .5 * N) + return 0; + /*FIXME: Can it really be any slower than this? */ + return cutoff * sin (M_PI * xx) / (M_PI * xx) * compute_func (fabs (2. * x / + N), window_func); +} +#endif + +#ifdef FIXED_POINT +static void +cubic_coef (spx_word16_t x, spx_word16_t interp[4]) +{ + /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation + but I know it's MMSE-optimal on a sinc */ + spx_word16_t x2, x3; + x2 = MULT16_16_P15 (x, x); + x3 = MULT16_16_P15 (x, x2); + interp[0] = + PSHR32 (MULT16_16 (QCONST16 (-0.16667f, 15), + x) + MULT16_16 (QCONST16 (0.16667f, 15), x3), 15); + interp[1] = + EXTRACT16 (EXTEND32 (x) + SHR32 (SUB32 (EXTEND32 (x2), EXTEND32 (x3)), + 1)); + interp[3] = + PSHR32 (MULT16_16 (QCONST16 (-0.33333f, 15), + x) + MULT16_16 (QCONST16 (.5f, 15), + x2) - MULT16_16 (QCONST16 (0.16667f, 15), x3), 15); + /* Just to make sure we don't have rounding problems */ + interp[2] = Q15_ONE - interp[0] - interp[1] - interp[3]; + if (interp[2] < 32767) + interp[2] += 1; } +#else +static void +cubic_coef (spx_word16_t frac, spx_word16_t interp[4]) +{ + /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation + but I know it's MMSE-optimal on a sinc */ + interp[0] = -0.16667f * frac + 0.16667f * frac * frac * frac; + interp[1] = frac + 0.5f * frac * frac - 0.5f * frac * frac * frac; + /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac; */ + interp[3] = + -0.33333f * frac + 0.5f * frac * frac - 0.16667f * frac * frac * frac; + /* Just to make sure we don't have rounding problems */ + interp[2] = 1. - interp[0] - interp[1] - interp[3]; +} +#endif -ResampleState * -resample_new (void) +#ifndef DOUBLE_PRECISION +static int +resampler_basic_direct_single (SpeexResamplerState * st, + spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, + spx_word16_t * out, spx_uint32_t * out_len) { - ResampleState *r; + const int N = st->filt_len; + int out_sample = 0; + int last_sample = st->last_sample[channel_index]; + spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; + const spx_word16_t *sinc_table = st->sinc_table; + const int out_stride = st->out_stride; + const int int_advance = st->int_advance; + const int frac_advance = st->frac_advance; + const spx_uint32_t den_rate = st->den_rate; + spx_word32_t sum; + int j; - r = malloc (sizeof (ResampleState)); - memset (r, 0, sizeof (ResampleState)); + while (!(last_sample >= (spx_int32_t) * in_len + || out_sample >= (spx_int32_t) * out_len)) { + const spx_word16_t *sinc = &sinc_table[samp_frac_num * N]; + const spx_word16_t *iptr = &in[last_sample]; - r->filter_length = 16; +#ifndef OVERRIDE_INNER_PRODUCT_SINGLE + float accum[4] = { 0, 0, 0, 0 }; - r->i_start = 0; - if (r->filter_length & 1) { - r->o_start = 0; - } else { - r->o_start = r->o_inc * 0.5; + for (j = 0; j < N; j += 4) { + accum[0] += sinc[j] * iptr[j]; + accum[1] += sinc[j + 1] * iptr[j + 1]; + accum[2] += sinc[j + 2] * iptr[j + 2]; + accum[3] += sinc[j + 3] * iptr[j + 3]; + } + sum = accum[0] + accum[1] + accum[2] + accum[3]; +#else + sum = inner_product_single (sinc, iptr, N); +#endif + + out[out_stride * out_sample++] = PSHR32 (sum, 15); + last_sample += int_advance; + samp_frac_num += frac_advance; + if (samp_frac_num >= den_rate) { + samp_frac_num -= den_rate; + last_sample++; + } } - r->queue = audioresample_buffer_queue_new (); - r->out_tmp = malloc (10000 * sizeof (double)); + st->last_sample[channel_index] = last_sample; + st->samp_frac_num[channel_index] = samp_frac_num; + return out_sample; +} +#endif - r->need_reinit = 1; +#ifdef FIXED_POINT +#else +/* This is the same as the previous function, except with a double-precision accumulator */ +static int +resampler_basic_direct_double (SpeexResamplerState * st, + spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, + spx_word16_t * out, spx_uint32_t * out_len) +{ + const int N = st->filt_len; + int out_sample = 0; + int last_sample = st->last_sample[channel_index]; + spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; + const spx_word16_t *sinc_table = st->sinc_table; + const int out_stride = st->out_stride; + const int int_advance = st->int_advance; + const int frac_advance = st->frac_advance; + const spx_uint32_t den_rate = st->den_rate; + double sum; + int j; - return r; + while (!(last_sample >= (spx_int32_t) * in_len + || out_sample >= (spx_int32_t) * out_len)) { + const spx_word16_t *sinc = &sinc_table[samp_frac_num * N]; + const spx_word16_t *iptr = &in[last_sample]; + +#ifndef OVERRIDE_INNER_PRODUCT_DOUBLE + double accum[4] = { 0, 0, 0, 0 }; + + for (j = 0; j < N; j += 4) { + accum[0] += sinc[j] * iptr[j]; + accum[1] += sinc[j + 1] * iptr[j + 1]; + accum[2] += sinc[j + 2] * iptr[j + 2]; + accum[3] += sinc[j + 3] * iptr[j + 3]; + } + sum = accum[0] + accum[1] + accum[2] + accum[3]; +#else + sum = inner_product_double (sinc, iptr, N); +#endif + + out[out_stride * out_sample++] = PSHR32 (sum, 15); + last_sample += int_advance; + samp_frac_num += frac_advance; + if (samp_frac_num >= den_rate) { + samp_frac_num -= den_rate; + last_sample++; + } + } + + st->last_sample[channel_index] = last_sample; + st->samp_frac_num[channel_index] = samp_frac_num; + return out_sample; } +#endif -void -resample_free (ResampleState * r) +#ifndef DOUBLE_PRECISION +static int +resampler_basic_interpolate_single (SpeexResamplerState * st, + spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, + spx_word16_t * out, spx_uint32_t * out_len) { - if (r->buffer) { - free (r->buffer); - } - if (r->ft) { - functable_free (r->ft); - } - if (r->queue) { - audioresample_buffer_queue_free (r->queue); + const int N = st->filt_len; + int out_sample = 0; + int last_sample = st->last_sample[channel_index]; + spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; + const int out_stride = st->out_stride; + const int int_advance = st->int_advance; + const int frac_advance = st->frac_advance; + const spx_uint32_t den_rate = st->den_rate; + int j; + spx_word32_t sum; + + while (!(last_sample >= (spx_int32_t) * in_len + || out_sample >= (spx_int32_t) * out_len)) { + const spx_word16_t *iptr = &in[last_sample]; + + const int offset = samp_frac_num * st->oversample / st->den_rate; +#ifdef FIXED_POINT + const spx_word16_t frac = + PDIV32 (SHL32 ((samp_frac_num * st->oversample) % st->den_rate, 15), + st->den_rate); +#else + const spx_word16_t frac = + ((float) ((samp_frac_num * st->oversample) % st->den_rate)) / + st->den_rate; +#endif + spx_word16_t interp[4]; + + +#ifndef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE + spx_word32_t accum[4] = { 0, 0, 0, 0 }; + + for (j = 0; j < N; j++) { + const spx_word16_t curr_in = iptr[j]; + accum[0] += + MULT16_16 (curr_in, + st->sinc_table[4 + (j + 1) * st->oversample - offset - 2]); + accum[1] += + MULT16_16 (curr_in, + st->sinc_table[4 + (j + 1) * st->oversample - offset - 1]); + accum[2] += + MULT16_16 (curr_in, + st->sinc_table[4 + (j + 1) * st->oversample - offset]); + accum[3] += + MULT16_16 (curr_in, + st->sinc_table[4 + (j + 1) * st->oversample - offset + 1]); + } + + cubic_coef (frac, interp); + sum = + MULT16_32_Q15 (interp[0], accum[0]) + MULT16_32_Q15 (interp[1], + accum[1]) + MULT16_32_Q15 (interp[2], + accum[2]) + MULT16_32_Q15 (interp[3], accum[3]); +#else + cubic_coef (frac, interp); + sum = + interpolate_product_single (iptr, + st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, + interp); +#endif + + out[out_stride * out_sample++] = PSHR32 (sum, 15); + last_sample += int_advance; + samp_frac_num += frac_advance; + if (samp_frac_num >= den_rate) { + samp_frac_num -= den_rate; + last_sample++; + } } - if (r->out_tmp) { - free (r->out_tmp); + + st->last_sample[channel_index] = last_sample; + st->samp_frac_num[channel_index] = samp_frac_num; + return out_sample; +} +#endif + +#ifdef FIXED_POINT +#else +/* This is the same as the previous function, except with a double-precision accumulator */ +static int +resampler_basic_interpolate_double (SpeexResamplerState * st, + spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, + spx_word16_t * out, spx_uint32_t * out_len) +{ + const int N = st->filt_len; + int out_sample = 0; + int last_sample = st->last_sample[channel_index]; + spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; + const int out_stride = st->out_stride; + const int int_advance = st->int_advance; + const int frac_advance = st->frac_advance; + const spx_uint32_t den_rate = st->den_rate; + int j; + spx_word32_t sum; + + while (!(last_sample >= (spx_int32_t) * in_len + || out_sample >= (spx_int32_t) * out_len)) { + const spx_word16_t *iptr = &in[last_sample]; + + const int offset = samp_frac_num * st->oversample / st->den_rate; +#ifdef FIXED_POINT + const spx_word16_t frac = + PDIV32 (SHL32 ((samp_frac_num * st->oversample) % st->den_rate, 15), + st->den_rate); +#else +#ifdef DOUBLE_PRECISION + const spx_word16_t frac = + ((double) ((samp_frac_num * st->oversample) % st->den_rate)) / + st->den_rate; +#else + const spx_word16_t frac = + ((float) ((samp_frac_num * st->oversample) % st->den_rate)) / + st->den_rate; +#endif +#endif + spx_word16_t interp[4]; + + +#ifndef OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE + double accum[4] = { 0, 0, 0, 0 }; + + for (j = 0; j < N; j++) { + const double curr_in = iptr[j]; + accum[0] += + MULT16_16 (curr_in, + st->sinc_table[4 + (j + 1) * st->oversample - offset - 2]); + accum[1] += + MULT16_16 (curr_in, + st->sinc_table[4 + (j + 1) * st->oversample - offset - 1]); + accum[2] += + MULT16_16 (curr_in, + st->sinc_table[4 + (j + 1) * st->oversample - offset]); + accum[3] += + MULT16_16 (curr_in, + st->sinc_table[4 + (j + 1) * st->oversample - offset + 1]); + } + + cubic_coef (frac, interp); + sum = + MULT16_32_Q15 (interp[0], accum[0]) + MULT16_32_Q15 (interp[1], + accum[1]) + MULT16_32_Q15 (interp[2], + accum[2]) + MULT16_32_Q15 (interp[3], accum[3]); +#else + cubic_coef (frac, interp); + sum = + interpolate_product_double (iptr, + st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, + interp); +#endif + + out[out_stride * out_sample++] = PSHR32 (sum, 15); + last_sample += int_advance; + samp_frac_num += frac_advance; + if (samp_frac_num >= den_rate) { + samp_frac_num -= den_rate; + last_sample++; + } } - free (r); + st->last_sample[channel_index] = last_sample; + st->samp_frac_num[channel_index] = samp_frac_num; + return out_sample; } +#endif static void -resample_buffer_free (AudioresampleBuffer * buffer, void *priv) +update_filter (SpeexResamplerState * st) { - if (buffer->priv2) { - ((void (*)(void *)) buffer->priv2) (buffer->priv); + spx_uint32_t old_length; + + old_length = st->filt_len; + st->oversample = quality_map[st->quality].oversample; + st->filt_len = quality_map[st->quality].base_length; + + if (st->num_rate > st->den_rate) { + /* down-sampling */ + st->cutoff = + quality_map[st->quality].downsample_bandwidth * st->den_rate / + st->num_rate; + /* FIXME: divide the numerator and denominator by a certain amount if they're too large */ + st->filt_len = st->filt_len * st->num_rate / st->den_rate; + /* Round down to make sure we have a multiple of 4 */ + st->filt_len &= (~0x3); + if (2 * st->den_rate < st->num_rate) + st->oversample >>= 1; + if (4 * st->den_rate < st->num_rate) + st->oversample >>= 1; + if (8 * st->den_rate < st->num_rate) + st->oversample >>= 1; + if (16 * st->den_rate < st->num_rate) + st->oversample >>= 1; + if (st->oversample < 1) + st->oversample = 1; + } else { + /* up-sampling */ + st->cutoff = quality_map[st->quality].upsample_bandwidth; } + + /* Choose the resampling type that requires the least amount of memory */ + if (st->den_rate <= st->oversample) { + spx_uint32_t i; + if (!st->sinc_table) + st->sinc_table = + (spx_word16_t *) speex_alloc (st->filt_len * st->den_rate * + sizeof (spx_word16_t)); + else if (st->sinc_table_length < st->filt_len * st->den_rate) { + st->sinc_table = + (spx_word16_t *) speex_realloc (st->sinc_table, + st->filt_len * st->den_rate * sizeof (spx_word16_t)); + st->sinc_table_length = st->filt_len * st->den_rate; + } + for (i = 0; i < st->den_rate; i++) { + spx_int32_t j; + for (j = 0; j < st->filt_len; j++) { + st->sinc_table[i * st->filt_len + j] = + sinc (st->cutoff, ((j - (spx_int32_t) st->filt_len / 2 + 1) - +#ifdef DOUBLE_PRECISION + ((double) i) / st->den_rate), st->filt_len, +#else + ((float) i) / st->den_rate), st->filt_len, +#endif + quality_map[st->quality].window_func); + } + } +#ifdef FIXED_POINT + st->resampler_ptr = resampler_basic_direct_single; +#else +#ifdef DOUBLE_PRECISION + st->resampler_ptr = resampler_basic_direct_double; +#else + if (st->quality > 8) + st->resampler_ptr = resampler_basic_direct_double; + else + st->resampler_ptr = resampler_basic_direct_single; +#endif +#endif + /*fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff); */ + } else { + spx_int32_t i; + if (!st->sinc_table) + st->sinc_table = + (spx_word16_t *) speex_alloc ((st->filt_len * st->oversample + + 8) * sizeof (spx_word16_t)); + else if (st->sinc_table_length < st->filt_len * st->oversample + 8) { + st->sinc_table = + (spx_word16_t *) speex_realloc (st->sinc_table, + (st->filt_len * st->oversample + 8) * sizeof (spx_word16_t)); + st->sinc_table_length = st->filt_len * st->oversample + 8; + } + for (i = -4; i < (spx_int32_t) (st->oversample * st->filt_len + 4); i++) + st->sinc_table[i + 4] = +#ifdef DOUBLE_PRECISION + sinc (st->cutoff, (i / (double) st->oversample - st->filt_len / 2), +#else + sinc (st->cutoff, (i / (float) st->oversample - st->filt_len / 2), +#endif + st->filt_len, quality_map[st->quality].window_func); +#ifdef FIXED_POINT + st->resampler_ptr = resampler_basic_interpolate_single; +#else +#ifdef DOUBLE_PRECISION + st->resampler_ptr = resampler_basic_interpolate_double; +#else + if (st->quality > 8) + st->resampler_ptr = resampler_basic_interpolate_double; + else + st->resampler_ptr = resampler_basic_interpolate_single; +#endif +#endif + /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff); */ + } + st->int_advance = st->num_rate / st->den_rate; + st->frac_advance = st->num_rate % st->den_rate; + + + /* Here's the place where we update the filter memory to take into account + the change in filter length. It's probably the messiest part of the code + due to handling of lots of corner cases. */ + if (!st->mem) { + spx_uint32_t i; + st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; + st->mem = + (spx_word16_t *) speex_alloc (st->nb_channels * st->mem_alloc_size * + sizeof (spx_word16_t)); + for (i = 0; i < st->nb_channels * st->mem_alloc_size; i++) + st->mem[i] = 0; + /*speex_warning("init filter"); */ + } else if (!st->started) { + spx_uint32_t i; + st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; + st->mem = + (spx_word16_t *) speex_realloc (st->mem, + st->nb_channels * st->mem_alloc_size * sizeof (spx_word16_t)); + for (i = 0; i < st->nb_channels * st->mem_alloc_size; i++) + st->mem[i] = 0; + /*speex_warning("reinit filter"); */ + } else if (st->filt_len > old_length) { + spx_int32_t i; + /* Increase the filter length */ + /*speex_warning("increase filter size"); */ + int old_alloc_size = st->mem_alloc_size; + if ((st->filt_len - 1 + st->buffer_size) > st->mem_alloc_size) { + st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; + st->mem = + (spx_word16_t *) speex_realloc (st->mem, + st->nb_channels * st->mem_alloc_size * sizeof (spx_word16_t)); + } + for (i = st->nb_channels - 1; i >= 0; i--) { + spx_int32_t j; + spx_uint32_t olen = old_length; + /*if (st->magic_samples[i]) */ + { + /* Try and remove the magic samples as if nothing had happened */ + + /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */ + olen = old_length + 2 * st->magic_samples[i]; + for (j = old_length - 2 + st->magic_samples[i]; j >= 0; j--) + st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]] = + st->mem[i * old_alloc_size + j]; + for (j = 0; j < st->magic_samples[i]; j++) + st->mem[i * st->mem_alloc_size + j] = 0; + st->magic_samples[i] = 0; + } + if (st->filt_len > olen) { + /* If the new filter length is still bigger than the "augmented" length */ + /* Copy data going backward */ + for (j = 0; j < olen - 1; j++) + st->mem[i * st->mem_alloc_size + (st->filt_len - 2 - j)] = + st->mem[i * st->mem_alloc_size + (olen - 2 - j)]; + /* Then put zeros for lack of anything better */ + for (; j < st->filt_len - 1; j++) + st->mem[i * st->mem_alloc_size + (st->filt_len - 2 - j)] = 0; + /* Adjust last_sample */ + st->last_sample[i] += (st->filt_len - olen) / 2; + } else { + /* Put back some of the magic! */ + st->magic_samples[i] = (olen - st->filt_len) / 2; + for (j = 0; j < st->filt_len - 1 + st->magic_samples[i]; j++) + st->mem[i * st->mem_alloc_size + j] = + st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]]; + } + } + } else if (st->filt_len < old_length) { + spx_uint32_t i; + /* Reduce filter length, this a bit tricky. We need to store some of the memory as "magic" + samples so they can be used directly as input the next time(s) */ + for (i = 0; i < st->nb_channels; i++) { + spx_uint32_t j; + spx_uint32_t old_magic = st->magic_samples[i]; + st->magic_samples[i] = (old_length - st->filt_len) / 2; + /* We must copy some of the memory that's no longer used */ + /* Copy data going backward */ + for (j = 0; j < st->filt_len - 1 + st->magic_samples[i] + old_magic; j++) + st->mem[i * st->mem_alloc_size + j] = + st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]]; + st->magic_samples[i] += old_magic; + } + } + } -/* - * free_func: a function that frees the given closure. If NULL, caller is - * responsible for freeing. - */ -void -resample_add_input_data (ResampleState * r, void *data, int size, - void (*free_func) (void *), void *closure) +EXPORT SpeexResamplerState * +speex_resampler_init (spx_uint32_t nb_channels, spx_uint32_t in_rate, + spx_uint32_t out_rate, int quality, int *err) +{ + return speex_resampler_init_frac (nb_channels, in_rate, out_rate, in_rate, + out_rate, quality, err); +} + +EXPORT SpeexResamplerState * +speex_resampler_init_frac (spx_uint32_t nb_channels, spx_uint32_t ratio_num, + spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate, + int quality, int *err) { - AudioresampleBuffer *buffer; + spx_uint32_t i; + SpeexResamplerState *st; + if (quality > 10 || quality < 0) { + if (err) + *err = RESAMPLER_ERR_INVALID_ARG; + return NULL; + } + st = (SpeexResamplerState *) speex_alloc (sizeof (SpeexResamplerState)); + st->initialised = 0; + st->started = 0; + st->in_rate = 0; + st->out_rate = 0; + st->num_rate = 0; + st->den_rate = 0; + st->quality = -1; + st->sinc_table_length = 0; + st->mem_alloc_size = 0; + st->filt_len = 0; + st->mem = 0; + st->resampler_ptr = 0; + + st->cutoff = 1.f; + st->nb_channels = nb_channels; + st->in_stride = 1; + st->out_stride = 1; + +#ifdef FIXED_POINT + st->buffer_size = 160; +#else + st->buffer_size = 160; +#endif + + /* Per channel data */ + st->last_sample = (spx_int32_t *) speex_alloc (nb_channels * sizeof (int)); + st->magic_samples = (spx_uint32_t *) speex_alloc (nb_channels * sizeof (int)); + st->samp_frac_num = (spx_uint32_t *) speex_alloc (nb_channels * sizeof (int)); + for (i = 0; i < nb_channels; i++) { + st->last_sample[i] = 0; + st->magic_samples[i] = 0; + st->samp_frac_num[i] = 0; + } + + speex_resampler_set_quality (st, quality); + speex_resampler_set_rate_frac (st, ratio_num, ratio_den, in_rate, out_rate); + - RESAMPLE_DEBUG ("data %p size %d", data, size); + update_filter (st); - buffer = audioresample_buffer_new_with_data (data, size); - buffer->free = resample_buffer_free; - buffer->priv2 = (void *) free_func; - buffer->priv = closure; + st->initialised = 1; + if (err) + *err = RESAMPLER_ERR_SUCCESS; - audioresample_buffer_queue_push (r->queue, buffer); + return st; } -void -resample_input_flush (ResampleState * r) +EXPORT void +speex_resampler_destroy (SpeexResamplerState * st) { - RESAMPLE_DEBUG ("flush"); - - audioresample_buffer_queue_flush (r->queue); - r->buffer_filled = 0; - r->need_reinit = 1; + speex_free (st->mem); + speex_free (st->sinc_table); + speex_free (st->last_sample); + speex_free (st->magic_samples); + speex_free (st->samp_frac_num); + speex_free (st); } -void -resample_input_pushthrough (ResampleState * r) +static int +speex_resampler_process_native (SpeexResamplerState * st, + spx_uint32_t channel_index, spx_uint32_t * in_len, spx_word16_t * out, + spx_uint32_t * out_len) { - AudioresampleBuffer *buffer; - int filter_bytes; - int buffer_filled; + int j = 0; + const int N = st->filt_len; + int out_sample = 0; + spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; + spx_uint32_t ilen; + + st->started = 1; - if (r->sample_size == 0) - return; + /* Call the right resampler through the function ptr */ + out_sample = st->resampler_ptr (st, channel_index, mem, in_len, out, out_len); - filter_bytes = r->filter_length * r->sample_size; - buffer_filled = r->buffer_filled; + if (st->last_sample[channel_index] < (spx_int32_t) * in_len) + *in_len = st->last_sample[channel_index]; + *out_len = out_sample; + st->last_sample[channel_index] -= *in_len; - RESAMPLE_DEBUG ("pushthrough filter_bytes %d, filled %d", - filter_bytes, buffer_filled); + ilen = *in_len; - /* if we have no pending samples, we don't need to do anything. */ - if (buffer_filled <= 0) - return; + for (j = 0; j < N - 1; ++j) + mem[j] = mem[j + ilen]; - /* send filter_length/2 number of samples so we can get to the - * last queued samples */ - buffer = audioresample_buffer_new_and_alloc (filter_bytes / 2); - memset (buffer->data, 0, buffer->length); + return RESAMPLER_ERR_SUCCESS; +} - RESAMPLE_DEBUG ("pushthrough %u", buffer->length); +static int +speex_resampler_magic (SpeexResamplerState * st, spx_uint32_t channel_index, + spx_word16_t ** out, spx_uint32_t out_len) +{ + spx_uint32_t tmp_in_len = st->magic_samples[channel_index]; + spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; + const int N = st->filt_len; - audioresample_buffer_queue_push (r->queue, buffer); + speex_resampler_process_native (st, channel_index, &tmp_in_len, *out, + &out_len); + + st->magic_samples[channel_index] -= tmp_in_len; + + /* If we couldn't process all "magic" input samples, save the rest for next time */ + if (st->magic_samples[channel_index]) { + spx_uint32_t i; + for (i = 0; i < st->magic_samples[channel_index]; i++) + mem[N - 1 + i] = mem[N - 1 + i + tmp_in_len]; + } + *out += out_len * st->out_stride; + return out_len; } -void -resample_input_eos (ResampleState * r) +#ifdef FIXED_POINT +EXPORT int +speex_resampler_process_int (SpeexResamplerState * st, + spx_uint32_t channel_index, const spx_int16_t * in, spx_uint32_t * in_len, + spx_int16_t * out, spx_uint32_t * out_len) +#else +#ifdef DOUBLE_PRECISION +EXPORT int +speex_resampler_process_float (SpeexResamplerState * st, + spx_uint32_t channel_index, const double *in, spx_uint32_t * in_len, + double *out, spx_uint32_t * out_len) +#else +EXPORT int +speex_resampler_process_float (SpeexResamplerState * st, + spx_uint32_t channel_index, const float *in, spx_uint32_t * in_len, + float *out, spx_uint32_t * out_len) +#endif +#endif { - RESAMPLE_DEBUG ("EOS"); - resample_input_pushthrough (r); - r->eos = 1; + int j; + spx_uint32_t ilen = *in_len; + spx_uint32_t olen = *out_len; + spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size; + const int filt_offs = st->filt_len - 1; + const spx_uint32_t xlen = st->mem_alloc_size - filt_offs; + const int istride = st->in_stride; + + if (st->magic_samples[channel_index]) + olen -= speex_resampler_magic (st, channel_index, &out, olen); + if (!st->magic_samples[channel_index]) { + while (ilen && olen) { + spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen; + spx_uint32_t ochunk = olen; + + if (in) { + for (j = 0; j < ichunk; ++j) + x[j + filt_offs] = in[j * istride]; + } else { + for (j = 0; j < ichunk; ++j) + x[j + filt_offs] = 0; + } + speex_resampler_process_native (st, channel_index, &ichunk, out, &ochunk); + ilen -= ichunk; + olen -= ochunk; + out += ochunk * st->out_stride; + if (in) + in += ichunk * istride; + } + } + *in_len -= ilen; + *out_len -= olen; + return RESAMPLER_ERR_SUCCESS; } -int -resample_get_output_size_for_input (ResampleState * r, int size) +#ifdef FIXED_POINT +EXPORT int +speex_resampler_process_float (SpeexResamplerState * st, + spx_uint32_t channel_index, const float *in, spx_uint32_t * in_len, + float *out, spx_uint32_t * out_len) +#else +EXPORT int +speex_resampler_process_int (SpeexResamplerState * st, + spx_uint32_t channel_index, const spx_int16_t * in, spx_uint32_t * in_len, + spx_int16_t * out, spx_uint32_t * out_len) +#endif { - int outsize; - double outd; - int avail; - int filter_bytes; - int buffer_filled; + int j; + const int istride_save = st->in_stride; + const int ostride_save = st->out_stride; + spx_uint32_t ilen = *in_len; + spx_uint32_t olen = *out_len; + spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size; + const spx_uint32_t xlen = st->mem_alloc_size - (st->filt_len - 1); +#ifdef VAR_ARRAYS + const unsigned int ylen = + (olen < FIXED_STACK_ALLOC) ? olen : FIXED_STACK_ALLOC; + VARDECL (spx_word16_t * ystack); + ALLOC (ystack, ylen, spx_word16_t); +#else + const unsigned int ylen = FIXED_STACK_ALLOC; + spx_word16_t ystack[FIXED_STACK_ALLOC]; +#endif - if (r->sample_size == 0) - return 0; + st->out_stride = 1; - filter_bytes = r->filter_length * r->sample_size; - buffer_filled = filter_bytes / 2 - r->buffer_filled / 2; + while (ilen && olen) { + spx_word16_t *y = ystack; + spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen; + spx_uint32_t ochunk = (olen > ylen) ? ylen : olen; + spx_uint32_t omagic = 0; - avail = - audioresample_buffer_queue_get_depth (r->queue) + size - buffer_filled; + if (st->magic_samples[channel_index]) { + omagic = speex_resampler_magic (st, channel_index, &y, ochunk); + ochunk -= omagic; + olen -= omagic; + } + if (!st->magic_samples[channel_index]) { + if (in) { + for (j = 0; j < ichunk; ++j) +#ifdef FIXED_POINT + x[j + st->filt_len - 1] = WORD2INT (in[j * istride_save]); +#else + x[j + st->filt_len - 1] = in[j * istride_save]; +#endif + } else { + for (j = 0; j < ichunk; ++j) + x[j + st->filt_len - 1] = 0; + } - RESAMPLE_DEBUG ("avail %d, o_rate %f, i_rate %f, filter_bytes %d, filled %d", - avail, r->o_rate, r->i_rate, filter_bytes, buffer_filled); - if (avail <= 0) - return 0; + speex_resampler_process_native (st, channel_index, &ichunk, y, &ochunk); + } else { + ichunk = 0; + ochunk = 0; + } - outd = (double) avail *r->o_rate / r->i_rate; + for (j = 0; j < ochunk + omagic; ++j) +#ifdef FIXED_POINT + out[j * ostride_save] = ystack[j]; +#else + out[j * ostride_save] = WORD2INT (ystack[j]); +#endif - outsize = (int) floor (outd); + ilen -= ichunk; + olen -= ochunk; + out += (ochunk + omagic) * ostride_save; + if (in) + in += ichunk * istride_save; + } + st->out_stride = ostride_save; + *in_len -= ilen; + *out_len -= olen; - /* round off for sample size */ - outsize -= outsize % r->sample_size; + return RESAMPLER_ERR_SUCCESS; +} - return outsize; +#ifdef DOUBLE_PRECISION +EXPORT int +speex_resampler_process_interleaved_float (SpeexResamplerState * st, + const double *in, spx_uint32_t * in_len, double *out, + spx_uint32_t * out_len) +#else +EXPORT int +speex_resampler_process_interleaved_float (SpeexResamplerState * st, + const float *in, spx_uint32_t * in_len, float *out, spx_uint32_t * out_len) +#endif +{ + spx_uint32_t i; + int istride_save, ostride_save; + spx_uint32_t bak_len = *out_len; + istride_save = st->in_stride; + ostride_save = st->out_stride; + st->in_stride = st->out_stride = st->nb_channels; + for (i = 0; i < st->nb_channels; i++) { + *out_len = bak_len; + if (in != NULL) + speex_resampler_process_float (st, i, in + i, in_len, out + i, out_len); + else + speex_resampler_process_float (st, i, NULL, in_len, out + i, out_len); + } + st->in_stride = istride_save; + st->out_stride = ostride_save; + return RESAMPLER_ERR_SUCCESS; } -int -resample_get_input_size_for_output (ResampleState * r, int size) +EXPORT int +speex_resampler_process_interleaved_int (SpeexResamplerState * st, + const spx_int16_t * in, spx_uint32_t * in_len, spx_int16_t * out, + spx_uint32_t * out_len) { - int outsize; - double outd; - int avail; + spx_uint32_t i; + int istride_save, ostride_save; + spx_uint32_t bak_len = *out_len; + istride_save = st->in_stride; + ostride_save = st->out_stride; + st->in_stride = st->out_stride = st->nb_channels; + for (i = 0; i < st->nb_channels; i++) { + *out_len = bak_len; + if (in != NULL) + speex_resampler_process_int (st, i, in + i, in_len, out + i, out_len); + else + speex_resampler_process_int (st, i, NULL, in_len, out + i, out_len); + } + st->in_stride = istride_save; + st->out_stride = ostride_save; + return RESAMPLER_ERR_SUCCESS; +} - if (r->sample_size == 0) - return 0; +EXPORT int +speex_resampler_set_rate (SpeexResamplerState * st, spx_uint32_t in_rate, + spx_uint32_t out_rate) +{ + return speex_resampler_set_rate_frac (st, in_rate, out_rate, in_rate, + out_rate); +} - avail = size; +EXPORT void +speex_resampler_get_rate (SpeexResamplerState * st, spx_uint32_t * in_rate, + spx_uint32_t * out_rate) +{ + *in_rate = st->in_rate; + *out_rate = st->out_rate; +} - RESAMPLE_DEBUG ("size %d, o_rate %f, i_rate %f", avail, r->o_rate, r->i_rate); - outd = (double) avail *r->i_rate / r->o_rate; +EXPORT int +speex_resampler_set_rate_frac (SpeexResamplerState * st, spx_uint32_t ratio_num, + spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate) +{ + spx_uint32_t fact; + spx_uint32_t old_den; + spx_uint32_t i; + if (st->in_rate == in_rate && st->out_rate == out_rate + && st->num_rate == ratio_num && st->den_rate == ratio_den) + return RESAMPLER_ERR_SUCCESS; - outsize = (int) ceil (outd); + old_den = st->den_rate; + st->in_rate = in_rate; + st->out_rate = out_rate; + st->num_rate = ratio_num; + st->den_rate = ratio_den; + /* FIXME: This is terribly inefficient, but who cares (at least for now)? */ + for (fact = 2; fact <= IMIN (st->num_rate, st->den_rate); fact++) { + while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0)) { + st->num_rate /= fact; + st->den_rate /= fact; + } + } - /* round off for sample size */ - outsize -= outsize % r->sample_size; + if (old_den > 0) { + for (i = 0; i < st->nb_channels; i++) { + st->samp_frac_num[i] = st->samp_frac_num[i] * st->den_rate / old_den; + /* Safety net */ + if (st->samp_frac_num[i] >= st->den_rate) + st->samp_frac_num[i] = st->den_rate - 1; + } + } - return outsize; + if (st->initialised) + update_filter (st); + return RESAMPLER_ERR_SUCCESS; } -int -resample_get_output_size (ResampleState * r) +EXPORT void +speex_resampler_get_ratio (SpeexResamplerState * st, spx_uint32_t * ratio_num, + spx_uint32_t * ratio_den) { - return resample_get_output_size_for_input (r, 0); + *ratio_num = st->num_rate; + *ratio_den = st->den_rate; } -int -resample_get_output_data (ResampleState * r, void *data, int size) +EXPORT int +speex_resampler_set_quality (SpeexResamplerState * st, int quality) { - r->o_buf = data; - r->o_size = size; + if (quality > 10 || quality < 0) + return RESAMPLER_ERR_INVALID_ARG; + if (st->quality == quality) + return RESAMPLER_ERR_SUCCESS; + st->quality = quality; + if (st->initialised) + update_filter (st); + return RESAMPLER_ERR_SUCCESS; +} - if (size == 0) - return 0; +EXPORT void +speex_resampler_get_quality (SpeexResamplerState * st, int *quality) +{ + *quality = st->quality; +} - switch (r->method) { - case 0: - resample_scale_ref (r); - break; - case 1: - resample_scale_functable (r); - break; - default: - break; - } +EXPORT void +speex_resampler_set_input_stride (SpeexResamplerState * st, spx_uint32_t stride) +{ + st->in_stride = stride; +} - return size - r->o_size; +EXPORT void +speex_resampler_get_input_stride (SpeexResamplerState * st, + spx_uint32_t * stride) +{ + *stride = st->in_stride; } -void -resample_set_filter_length (ResampleState * r, int length) +EXPORT void +speex_resampler_set_output_stride (SpeexResamplerState * st, + spx_uint32_t stride) { - r->filter_length = length; - r->need_reinit = 1; + st->out_stride = stride; } -void -resample_set_input_rate (ResampleState * r, double rate) +EXPORT void +speex_resampler_get_output_stride (SpeexResamplerState * st, + spx_uint32_t * stride) { - r->i_rate = rate; - r->need_reinit = 1; + *stride = st->out_stride; } -void -resample_set_output_rate (ResampleState * r, double rate) +EXPORT int +speex_resampler_get_input_latency (SpeexResamplerState * st) { - r->o_rate = rate; - r->need_reinit = 1; + return st->filt_len / 2; } -void -resample_set_n_channels (ResampleState * r, int n_channels) +EXPORT int +speex_resampler_get_output_latency (SpeexResamplerState * st) { - r->n_channels = n_channels; - r->sample_size = r->n_channels * resample_format_size (r->format); - r->need_reinit = 1; + return ((st->filt_len / 2) * st->den_rate + + (st->num_rate >> 1)) / st->num_rate; } -void -resample_set_format (ResampleState * r, ResampleFormat format) +EXPORT int +speex_resampler_skip_zeros (SpeexResamplerState * st) { - r->format = format; - r->sample_size = r->n_channels * resample_format_size (r->format); - r->need_reinit = 1; + spx_uint32_t i; + for (i = 0; i < st->nb_channels; i++) + st->last_sample[i] = st->filt_len / 2; + return RESAMPLER_ERR_SUCCESS; } -void -resample_set_method (ResampleState * r, int method) +EXPORT int +speex_resampler_reset_mem (SpeexResamplerState * st) { - r->method = method; - r->need_reinit = 1; + spx_uint32_t i; + for (i = 0; i < st->nb_channels * (st->filt_len - 1); i++) + st->mem[i] = 0; + return RESAMPLER_ERR_SUCCESS; } -int -resample_format_size (ResampleFormat format) -{ - switch (format) { - case RESAMPLE_FORMAT_S16: - return 2; - case RESAMPLE_FORMAT_S32: - case RESAMPLE_FORMAT_F32: - return 4; - case RESAMPLE_FORMAT_F64: - return 8; +EXPORT const char * +speex_resampler_strerror (int err) +{ + switch (err) { + case RESAMPLER_ERR_SUCCESS: + return "Success."; + case RESAMPLER_ERR_ALLOC_FAILED: + return "Memory allocation failed."; + case RESAMPLER_ERR_BAD_STATE: + return "Bad resampler state."; + case RESAMPLER_ERR_INVALID_ARG: + return "Invalid argument."; + case RESAMPLER_ERR_PTR_OVERLAP: + return "Input and output buffers overlap."; + default: + return "Unknown error. Bad error code or strange version mismatch."; } - return 0; } diff --git a/gst/audioresample/resample.h b/gst/audioresample/resample.h deleted file mode 100644 index 84bf8f0..0000000 --- a/gst/audioresample/resample.h +++ /dev/null @@ -1,128 +0,0 @@ -/* Resampling library - * Copyright (C) <2001> David Schleef - * - * 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 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 __RESAMPLE_H__ -#define __RESAMPLE_H__ - -#include "functable.h" -#include "buffer.h" - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -#ifdef WIN32 -#define rint(x) (floor((x)+0.5)) -#endif - -typedef enum { - RESAMPLE_FORMAT_S16 = 0, - RESAMPLE_FORMAT_S32, - RESAMPLE_FORMAT_F32, - RESAMPLE_FORMAT_F64 -} ResampleFormat; - -typedef void (*ResampleCallback) (void *); - -typedef struct _ResampleState ResampleState; - -struct _ResampleState { - /* parameters */ - - int n_channels; - ResampleFormat format; - - int filter_length; - - double i_rate; - double o_rate; - - int method; - - /* internal parameters */ - - int need_reinit; - - double halftaps; - - /* filter state */ - - unsigned char *o_buf; - int o_size; - - AudioresampleBufferQueue *queue; - int eos; - int started; - - int sample_size; - - unsigned char *buffer; - int buffer_len; - int buffer_filled; - - double i_start; - double o_start; - - double i_inc; - double o_inc; - - double sinc_scale; - - double i_end; - double o_end; - - int i_samples; - int o_samples; - - //void *i_buf; - - Functable *ft; - - double *out_tmp; -}; - -void resample_init (void); -void resample_cleanup (void); - -ResampleState *resample_new (void); -void resample_free (ResampleState *state); - -void resample_add_input_data (ResampleState * r, void *data, int size, - ResampleCallback free_func, void *closure); -void resample_input_eos (ResampleState *r); -void resample_input_flush (ResampleState *r); -void resample_input_pushthrough (ResampleState *r); - -int resample_get_output_size_for_input (ResampleState * r, int size); -int resample_get_input_size_for_output (ResampleState * r, int size); - -int resample_get_output_size (ResampleState *r); -int resample_get_output_data (ResampleState *r, void *data, int size); - -void resample_set_filter_length (ResampleState *r, int length); -void resample_set_input_rate (ResampleState *r, double rate); -void resample_set_output_rate (ResampleState *r, double rate); -void resample_set_n_channels (ResampleState *r, int n_channels); -void resample_set_format (ResampleState *r, ResampleFormat format); -void resample_set_method (ResampleState *r, int method); -int resample_format_size (ResampleFormat format); - -#endif /* __RESAMPLE_H__ */ - diff --git a/gst/audioresample/resample_chunk.c b/gst/audioresample/resample_chunk.c deleted file mode 100644 index 1cf9f09..0000000 --- a/gst/audioresample/resample_chunk.c +++ /dev/null @@ -1,209 +0,0 @@ -/* Resampling library - * Copyright (C) <2001> David A. Schleef - * - * 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 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 -#endif - - -#include -#include -#include -#include -#include -#include - -#include "resample.h" -#include "buffer.h" -#include "debug.h" - - -static double -resample_sinc_window (double x, double halfwidth, double scale) -{ - double y; - - if (x == 0) - return 1.0; - if (x < -halfwidth || x > halfwidth) - return 0.0; - - y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale; - - x /= halfwidth; - y *= (1 - x * x) * (1 - x * x); - - return y; -} - -void -resample_scale_chunk (ResampleState * r) -{ - if (r->need_reinit) { - RESAMPLE_DEBUG ("sample size %d", r->sample_size); - - if (r->buffer) - free (r->buffer); - r->buffer_len = r->sample_size * 1000; - r->buffer = malloc (r->buffer_len); - memset (r->buffer, 0, r->buffer_len); - - r->i_inc = r->o_rate / r->i_rate; - r->o_inc = r->i_rate / r->o_rate; - RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc); - - r->i_start = -r->i_inc * r->filter_length; - - r->need_reinit = 0; - -#if 0 - if (r->i_inc < 1.0) { - r->sinc_scale = r->i_inc; - if (r->sinc_scale == 0.5) { - /* strange things happen at integer multiples */ - r->sinc_scale = 1.0; - } - } else { - r->sinc_scale = 1.0; - } -#else - r->sinc_scale = 1.0; -#endif - } - - while (r->o_size > 0) { - double midpoint; - int i; - int j; - - RESAMPLE_DEBUG ("i_start %g", r->i_start); - midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc; - if (midpoint > 0.5 * r->i_inc) { - RESAMPLE_ERROR ("inconsistent state"); - } - while (midpoint < -0.5 * r->i_inc) { - AudioresampleBuffer *buffer; - - buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size); - if (buffer == NULL) { - RESAMPLE_ERROR ("buffer_queue_pull returned NULL"); - return; - } - - r->i_start += r->i_inc; - RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start); - - midpoint += r->i_inc; - memmove (r->buffer, r->buffer + r->sample_size, - r->buffer_len - r->sample_size); - - memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data, - r->sample_size); - audioresample_buffer_unref (buffer); - } - - switch (r->format) { - case RESAMPLE_FORMAT_S16: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(int16_t *) (r->buffer + i * sizeof (int16_t) + - j * r->sample_size); - acc += - resample_sinc_window (offset, r->filter_length * 0.5, - r->sinc_scale) * x; - } - if (acc < -32768.0) - acc = -32768.0; - if (acc > 32767.0) - acc = 32767.0; - - *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc); - } - break; - case RESAMPLE_FORMAT_S32: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(int32_t *) (r->buffer + i * sizeof (int32_t) + - j * r->sample_size); - acc += - resample_sinc_window (offset, r->filter_length * 0.5, - r->sinc_scale) * x; - } - if (acc < -2147483648.0) - acc = -2147483648.0; - if (acc > 2147483647.0) - acc = 2147483647.0; - - *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc); - } - break; - case RESAMPLE_FORMAT_F32: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(float *) (r->buffer + i * sizeof (float) + - j * r->sample_size); - acc += - resample_sinc_window (offset, r->filter_length * 0.5, - r->sinc_scale) * x; - } - - *(float *) (r->o_buf + i * sizeof (float)) = acc; - } - break; - case RESAMPLE_FORMAT_F64: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(double *) (r->buffer + i * sizeof (double) + - j * r->sample_size); - acc += - resample_sinc_window (offset, r->filter_length * 0.5, - r->sinc_scale) * x; - } - - *(double *) (r->o_buf + i * sizeof (double)) = acc; - } - break; - } - - r->i_start -= 1.0; - r->o_buf += r->sample_size; - r->o_size -= r->sample_size; - } - -} diff --git a/gst/audioresample/resample_functable.c b/gst/audioresample/resample_functable.c deleted file mode 100644 index af12427..0000000 --- a/gst/audioresample/resample_functable.c +++ /dev/null @@ -1,271 +0,0 @@ -/* Resampling library - * Copyright (C) <2001> David A. Schleef - * - * 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 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 -#endif - - -#include -#include -#include -#include -#include -#include - -#include "resample.h" -#include "buffer.h" -#include "debug.h" - -static void -func_sinc (double *fx, double *dfx, double x, void *closure) -{ - //double scale = *(double *)closure; - double scale = M_PI; - - if (x == 0) { - *fx = 1; - *dfx = 0; - return; - } - - x *= scale; - *fx = sin (x) / x; - *dfx = scale * (cos (x) - sin (x) / x) / x; -} - -static void -func_hanning (double *fx, double *dfx, double x, void *closure) -{ - double width = *(double *) closure; - - if (x < width && x > -width) { - x /= width; - *fx = (1 - x * x) * (1 - x * x); - *dfx = -2 * 2 * x / width * (1 - x * x); - } else { - *fx = 0; - *dfx = 0; - } -} - -#if 0 -static double -resample_sinc_window (double x, double halfwidth, double scale) -{ - double y; - - if (x == 0) - return 1.0; - if (x < -halfwidth || x > halfwidth) - return 0.0; - - y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale; - - x /= halfwidth; - y *= (1 - x * x) * (1 - x * x); - - return y; -} -#endif - -#if 0 -static void -functable_test (Functable * ft, double halfwidth) -{ - int i; - double x; - - for (i = 0; i < 100; i++) { - x = i * 0.1; - printf ("%d %g %g\n", i, resample_sinc_window (x, halfwidth, 1.0), - functable_evaluate (ft, x)); - } - exit (0); - -} -#endif - - -void -resample_scale_functable (ResampleState * r) -{ - if (r->need_reinit) { - double hanning_width; - - RESAMPLE_DEBUG ("sample size %d", r->sample_size); - - if (r->buffer) - free (r->buffer); - r->buffer_len = r->sample_size * r->filter_length; - r->buffer = malloc (r->buffer_len); - memset (r->buffer, 0, r->buffer_len); - - r->i_inc = r->o_rate / r->i_rate; - r->o_inc = r->i_rate / r->o_rate; - RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc); - - r->i_start = -r->i_inc * r->filter_length; - - if (r->ft) { - functable_free (r->ft); - } - r->ft = functable_new (); - functable_set_length (r->ft, r->filter_length * 16); - functable_set_offset (r->ft, -r->filter_length / 2); - functable_set_multiplier (r->ft, 1 / 16.0); - - hanning_width = r->filter_length / 2; - functable_calculate (r->ft, func_sinc, NULL); - functable_calculate_multiply (r->ft, func_hanning, &hanning_width); - - //functable_test(r->ft, 0.5 * r->filter_length); -#if 0 - if (r->i_inc < 1.0) { - r->sinc_scale = r->i_inc; - if (r->sinc_scale == 0.5) { - /* strange things happen at integer multiples */ - r->sinc_scale = 1.0; - } - } else { - r->sinc_scale = 1.0; - } -#else - r->sinc_scale = 1.0; -#endif - - r->need_reinit = 0; - } - - while (r->o_size > 0) { - double midpoint; - int i; - int j; - - RESAMPLE_DEBUG ("i_start %g", r->i_start); - midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc; - if (midpoint > 0.5 * r->i_inc) { - RESAMPLE_ERROR ("inconsistent state"); - } - while (midpoint < -0.5 * r->i_inc) { - AudioresampleBuffer *buffer; - - buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size); - if (buffer == NULL) { - RESAMPLE_ERROR ("buffer_queue_pull returned NULL"); - return; - } - - r->i_start += r->i_inc; - RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start); - - midpoint += r->i_inc; - memmove (r->buffer, r->buffer + r->sample_size, - r->buffer_len - r->sample_size); - - memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data, - r->sample_size); - audioresample_buffer_unref (buffer); - } - - switch (r->format) { - case RESAMPLE_FORMAT_S16: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(int16_t *) (r->buffer + i * sizeof (int16_t) + - j * r->sample_size); - acc += functable_evaluate (r->ft, offset) * x; - //acc += resample_sinc_window (offset, r->filter_length * 0.5, r->sinc_scale) * x; - } - if (acc < -32768.0) - acc = -32768.0; - if (acc > 32767.0) - acc = 32767.0; - - *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc); - } - break; - case RESAMPLE_FORMAT_S32: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(int32_t *) (r->buffer + i * sizeof (int32_t) + - j * r->sample_size); - acc += functable_evaluate (r->ft, offset) * x; - //acc += resample_sinc_window (offset, r->filter_length * 0.5, r->sinc_scale) * x; - } - if (acc < -2147483648.0) - acc = -2147483648.0; - if (acc > 2147483647.0) - acc = 2147483647.0; - - *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc); - } - break; - case RESAMPLE_FORMAT_F32: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(float *) (r->buffer + i * sizeof (float) + - j * r->sample_size); - acc += functable_evaluate (r->ft, offset) * x; - //acc += resample_sinc_window (offset, r->filter_length * 0.5, r->sinc_scale) * x; - } - - *(float *) (r->o_buf + i * sizeof (float)) = acc; - } - break; - case RESAMPLE_FORMAT_F64: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(double *) (r->buffer + i * sizeof (double) + - j * r->sample_size); - acc += functable_evaluate (r->ft, offset) * x; - //acc += resample_sinc_window (offset, r->filter_length * 0.5, r->sinc_scale) * x; - } - - *(double *) (r->o_buf + i * sizeof (double)) = acc; - } - break; - } - - r->i_start -= 1.0; - r->o_buf += r->sample_size; - r->o_size -= r->sample_size; - } - -} diff --git a/gst/audioresample/resample_ref.c b/gst/audioresample/resample_ref.c deleted file mode 100644 index bb8d241..0000000 --- a/gst/audioresample/resample_ref.c +++ /dev/null @@ -1,223 +0,0 @@ -/* Resampling library - * Copyright (C) <2001> David A. Schleef - * - * 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 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 -#endif - - -#include -#include -#include -#include -#include -#include - -#include "resample.h" -#include "buffer.h" -#include "debug.h" - - -static double -resample_sinc_window (double x, double halfwidth, double scale) -{ - double y; - - if (x == 0) - return 1.0; - if (x < -halfwidth || x > halfwidth) - return 0.0; - - y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale; - - x /= halfwidth; - y *= (1 - x * x) * (1 - x * x); - - return y; -} - -void -resample_scale_ref (ResampleState * r) -{ - if (r->need_reinit) { - RESAMPLE_DEBUG ("sample size %d", r->sample_size); - - if (r->buffer) - free (r->buffer); - r->buffer_len = r->sample_size * r->filter_length; - r->buffer = malloc (r->buffer_len); - memset (r->buffer, 0, r->buffer_len); - r->buffer_filled = 0; - - r->i_inc = r->o_rate / r->i_rate; - r->o_inc = r->i_rate / r->o_rate; - RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc); - - r->i_start = -r->i_inc * r->filter_length; - - r->need_reinit = 0; - -#if 0 - if (r->i_inc < 1.0) { - r->sinc_scale = r->i_inc; - if (r->sinc_scale == 0.5) { - /* strange things happen at integer multiples */ - r->sinc_scale = 1.0; - } - } else { - r->sinc_scale = 1.0; - } -#else - r->sinc_scale = 1.0; -#endif - } - - RESAMPLE_DEBUG ("asked to resample %d bytes", r->o_size); - RESAMPLE_DEBUG ("%d bytes in queue", - audioresample_buffer_queue_get_depth (r->queue)); - - while (r->o_size >= r->sample_size) { - double midpoint; - int i; - int j; - - midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc; - RESAMPLE_DEBUG - ("still need to output %d bytes, %d input left, i_start %g, midpoint %f", - r->o_size, audioresample_buffer_queue_get_depth (r->queue), r->i_start, - midpoint); - if (midpoint > 0.5 * r->i_inc) { - RESAMPLE_ERROR ("inconsistent state"); - } - while (midpoint < -0.5 * r->i_inc) { - AudioresampleBuffer *buffer; - - RESAMPLE_DEBUG ("midpoint %f < %f, r->i_inc %f", midpoint, - -0.5 * r->i_inc, r->i_inc); - buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size); - if (buffer == NULL) { - /* FIXME: for the first buffer, this isn't necessarily an error, - * since because of the filter length we'll output less buffers. - * deal with that so we don't print to console */ - RESAMPLE_ERROR ("buffer_queue_pull returned NULL"); - return; - } - - r->i_start += r->i_inc; - RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start); - - midpoint += r->i_inc; - memmove (r->buffer, r->buffer + r->sample_size, - r->buffer_len - r->sample_size); - - memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data, - r->sample_size); - r->buffer_filled = MIN (r->buffer_filled + r->sample_size, r->buffer_len); - - audioresample_buffer_unref (buffer); - } - - switch (r->format) { - case RESAMPLE_FORMAT_S16: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(int16_t *) (r->buffer + i * sizeof (int16_t) + - j * r->sample_size); - acc += - resample_sinc_window (offset, r->filter_length * 0.5, - r->sinc_scale) * x; - } - if (acc < -32768.0) - acc = -32768.0; - if (acc > 32767.0) - acc = 32767.0; - - *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc); - } - break; - case RESAMPLE_FORMAT_S32: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(int32_t *) (r->buffer + i * sizeof (int32_t) + - j * r->sample_size); - acc += - resample_sinc_window (offset, r->filter_length * 0.5, - r->sinc_scale) * x; - } - if (acc < -2147483648.0) - acc = -2147483648.0; - if (acc > 2147483647.0) - acc = 2147483647.0; - - *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc); - } - break; - case RESAMPLE_FORMAT_F32: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(float *) (r->buffer + i * sizeof (float) + - j * r->sample_size); - acc += - resample_sinc_window (offset, r->filter_length * 0.5, - r->sinc_scale) * x; - } - - *(float *) (r->o_buf + i * sizeof (float)) = acc; - } - break; - case RESAMPLE_FORMAT_F64: - for (i = 0; i < r->n_channels; i++) { - double acc = 0; - double offset; - double x; - - for (j = 0; j < r->filter_length; j++) { - offset = (r->i_start + j * r->i_inc) * r->o_inc; - x = *(double *) (r->buffer + i * sizeof (double) + - j * r->sample_size); - acc += - resample_sinc_window (offset, r->filter_length * 0.5, - r->sinc_scale) * x; - } - - *(double *) (r->o_buf + i * sizeof (double)) = acc; - } - break; - } - - r->i_start -= 1.0; - r->o_buf += r->sample_size; - r->o_size -= r->sample_size; - } -} diff --git a/gst/speexresample/resample_sse.h b/gst/audioresample/resample_sse.h similarity index 100% rename from gst/speexresample/resample_sse.h rename to gst/audioresample/resample_sse.h diff --git a/gst/speexresample/speex_resampler.h b/gst/audioresample/speex_resampler.h similarity index 100% rename from gst/speexresample/speex_resampler.h rename to gst/audioresample/speex_resampler.h diff --git a/gst/speexresample/speex_resampler_double.c b/gst/audioresample/speex_resampler_double.c similarity index 100% rename from gst/speexresample/speex_resampler_double.c rename to gst/audioresample/speex_resampler_double.c diff --git a/gst/speexresample/speex_resampler_float.c b/gst/audioresample/speex_resampler_float.c similarity index 100% rename from gst/speexresample/speex_resampler_float.c rename to gst/audioresample/speex_resampler_float.c diff --git a/gst/speexresample/speex_resampler_int.c b/gst/audioresample/speex_resampler_int.c similarity index 100% rename from gst/speexresample/speex_resampler_int.c rename to gst/audioresample/speex_resampler_int.c diff --git a/gst/speexresample/speex_resampler_wrapper.h b/gst/audioresample/speex_resampler_wrapper.h similarity index 100% rename from gst/speexresample/speex_resampler_wrapper.h rename to gst/audioresample/speex_resampler_wrapper.h diff --git a/gst/speexresample/Makefile.am b/gst/speexresample/Makefile.am deleted file mode 100644 index 0545971..0000000 --- a/gst/speexresample/Makefile.am +++ /dev/null @@ -1,37 +0,0 @@ -plugin_LTLIBRARIES = libgstaudioresample.la - -libgstaudioresample_la_SOURCES = \ - gstspeexresample.c \ - speex_resampler_int.c \ - speex_resampler_float.c \ - speex_resampler_double.c - -libgstaudioresample_la_CFLAGS = \ - $(GST_PLUGINS_BASE_CFLAGS) \ - $(GST_BASE_CFLAGS) \ - $(GST_CFLAGS) \ - $(LIBOIL_CFLAGS) - -libgstaudioresample_la_LIBADD = \ - $(GST_PLUGINS_BASE_LIBS) \ - $(GST_BASE_LIBS) \ - $(GST_LIBS) \ - $(LIBOIL_LIBS) \ - $(LIBM) - -libgstaudioresample_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) -libgstaudioresample_la_LIBTOOLFLAGS = --tag=disable-static - -noinst_HEADERS = \ - arch.h \ - fixed_arm4.h \ - fixed_arm5e.h \ - fixed_bfin.h \ - fixed_debug.h \ - fixed_generic.h \ - gstspeexresample.h \ - resample.c \ - resample_sse.h \ - speex_resampler.h \ - speex_resampler_wrapper.h - diff --git a/gst/speexresample/gstspeexresample.c b/gst/speexresample/gstspeexresample.c deleted file mode 100644 index 674dec8..0000000 --- a/gst/speexresample/gstspeexresample.c +++ /dev/null @@ -1,1404 +0,0 @@ -/* GStreamer - * Copyright (C) 1999 Erik Walthinsen - * Copyright (C) 2003,2004 David A. Schleef - * Copyright (C) 2007-2008 Sebastian Dröge - * - * 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:element-audioresample - * - * audioresample resamples raw audio buffers to different sample rates using - * a configurable windowing function to enhance quality. - * - * - * Example launch line - * |[ - * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! audio/x-raw-int, rate=8000 ! alsasink - * ]| Decode an Ogg/Vorbis downsample to 8Khz and play sound through alsa. - * To create the Ogg/Vorbis file refer to the documentation of vorbisenc. - * - */ - -/* TODO: - * - Enable SSE/ARM optimizations and select at runtime - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include - -#include "gstspeexresample.h" -#include -#include - -#define OIL_ENABLE_UNSTABLE_API -#include -#include - -GST_DEBUG_CATEGORY (speex_resample_debug); -#define GST_CAT_DEFAULT speex_resample_debug - -enum -{ - PROP_0, - PROP_QUALITY, - PROP_FILTER_LENGTH -}; - -#define SUPPORTED_CAPS \ -GST_STATIC_CAPS ( \ - "audio/x-raw-float, " \ - "rate = (int) [ 1, MAX ], " \ - "channels = (int) [ 1, MAX ], " \ - "endianness = (int) BYTE_ORDER, " \ - "width = (int) { 32, 64 }; " \ - "audio/x-raw-int, " \ - "rate = (int) [ 1, MAX ], " \ - "channels = (int) [ 1, MAX ], " \ - "endianness = (int) BYTE_ORDER, " \ - "width = (int) 32, " \ - "depth = (int) 32, " \ - "signed = (boolean) true; " \ - "audio/x-raw-int, " \ - "rate = (int) [ 1, MAX ], " \ - "channels = (int) [ 1, MAX ], " \ - "endianness = (int) BYTE_ORDER, " \ - "width = (int) 24, " \ - "depth = (int) 24, " \ - "signed = (boolean) true; " \ - "audio/x-raw-int, " \ - "rate = (int) [ 1, MAX ], " \ - "channels = (int) [ 1, MAX ], " \ - "endianness = (int) BYTE_ORDER, " \ - "width = (int) 16, " \ - "depth = (int) 16, " \ - "signed = (boolean) true; " \ - "audio/x-raw-int, " \ - "rate = (int) [ 1, MAX ], " \ - "channels = (int) [ 1, MAX ], " \ - "endianness = (int) BYTE_ORDER, " \ - "width = (int) 8, " \ - "depth = (int) 8, " \ - "signed = (boolean) true" \ -) - -/* If TRUE integer arithmetic resampling is faster and will be used if appropiate */ -static gboolean gst_speex_resample_use_int = FALSE; - -static GstStaticPadTemplate gst_speex_resample_sink_template = -GST_STATIC_PAD_TEMPLATE ("sink", - GST_PAD_SINK, GST_PAD_ALWAYS, SUPPORTED_CAPS); - -static GstStaticPadTemplate gst_speex_resample_src_template = -GST_STATIC_PAD_TEMPLATE ("src", - GST_PAD_SRC, GST_PAD_ALWAYS, SUPPORTED_CAPS); - -static void gst_speex_resample_set_property (GObject * object, - guint prop_id, const GValue * value, GParamSpec * pspec); -static void gst_speex_resample_get_property (GObject * object, - guint prop_id, GValue * value, GParamSpec * pspec); - -/* vmethods */ -static gboolean gst_speex_resample_get_unit_size (GstBaseTransform * base, - GstCaps * caps, guint * size); -static GstCaps *gst_speex_resample_transform_caps (GstBaseTransform * base, - GstPadDirection direction, GstCaps * caps); -static void gst_speex_resample_fixate_caps (GstBaseTransform * base, - GstPadDirection direction, GstCaps * caps, GstCaps * othercaps); -static gboolean gst_speex_resample_transform_size (GstBaseTransform * trans, - GstPadDirection direction, GstCaps * incaps, guint insize, - GstCaps * outcaps, guint * outsize); -static gboolean gst_speex_resample_set_caps (GstBaseTransform * base, - GstCaps * incaps, GstCaps * outcaps); -static GstFlowReturn gst_speex_resample_transform (GstBaseTransform * base, - GstBuffer * inbuf, GstBuffer * outbuf); -static gboolean gst_speex_resample_event (GstBaseTransform * base, - GstEvent * event); -static gboolean gst_speex_resample_start (GstBaseTransform * base); -static gboolean gst_speex_resample_stop (GstBaseTransform * base); -static gboolean gst_speex_resample_query (GstPad * pad, GstQuery * query); -static const GstQueryType *gst_speex_resample_query_type (GstPad * pad); - -GST_BOILERPLATE (GstSpeexResample, gst_speex_resample, GstBaseTransform, - GST_TYPE_BASE_TRANSFORM); - -static void -gst_speex_resample_base_init (gpointer g_class) -{ - GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class); - - gst_element_class_add_pad_template (gstelement_class, - gst_static_pad_template_get (&gst_speex_resample_src_template)); - gst_element_class_add_pad_template (gstelement_class, - gst_static_pad_template_get (&gst_speex_resample_sink_template)); - - gst_element_class_set_details_simple (gstelement_class, "Audio resampler", - "Filter/Converter/Audio", "Resamples audio", - "Sebastian Dröge "); -} - -static void -gst_speex_resample_class_init (GstSpeexResampleClass * klass) -{ - GObjectClass *gobject_class = (GObjectClass *) klass; - - gobject_class->set_property = gst_speex_resample_set_property; - gobject_class->get_property = gst_speex_resample_get_property; - - g_object_class_install_property (gobject_class, PROP_QUALITY, - g_param_spec_int ("quality", "Quality", "Resample quality with 0 being " - "the lowest and 10 being the best", - SPEEX_RESAMPLER_QUALITY_MIN, SPEEX_RESAMPLER_QUALITY_MAX, - SPEEX_RESAMPLER_QUALITY_DEFAULT, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); - - /* FIXME 0.11: Remove this property, it's just for compatibility - * with old audioresample - */ - g_object_class_install_property (gobject_class, PROP_FILTER_LENGTH, - g_param_spec_int ("filter-length", "Filter length", - "DEPRECATED, DON'T USE THIS! " "Length of the resample filter", 0, - G_MAXINT, 64, G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); - - GST_BASE_TRANSFORM_CLASS (klass)->start = - GST_DEBUG_FUNCPTR (gst_speex_resample_start); - GST_BASE_TRANSFORM_CLASS (klass)->stop = - GST_DEBUG_FUNCPTR (gst_speex_resample_stop); - GST_BASE_TRANSFORM_CLASS (klass)->transform_size = - GST_DEBUG_FUNCPTR (gst_speex_resample_transform_size); - GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size = - GST_DEBUG_FUNCPTR (gst_speex_resample_get_unit_size); - GST_BASE_TRANSFORM_CLASS (klass)->transform_caps = - GST_DEBUG_FUNCPTR (gst_speex_resample_transform_caps); - GST_BASE_TRANSFORM_CLASS (klass)->fixate_caps = - GST_DEBUG_FUNCPTR (gst_speex_resample_fixate_caps); - GST_BASE_TRANSFORM_CLASS (klass)->set_caps = - GST_DEBUG_FUNCPTR (gst_speex_resample_set_caps); - GST_BASE_TRANSFORM_CLASS (klass)->transform = - GST_DEBUG_FUNCPTR (gst_speex_resample_transform); - GST_BASE_TRANSFORM_CLASS (klass)->event = - GST_DEBUG_FUNCPTR (gst_speex_resample_event); - - GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE; -} - -static void -gst_speex_resample_init (GstSpeexResample * resample, - GstSpeexResampleClass * klass) -{ - GstBaseTransform *trans = GST_BASE_TRANSFORM (resample); - - resample->quality = SPEEX_RESAMPLER_QUALITY_DEFAULT; - - resample->need_discont = FALSE; - - gst_pad_set_query_function (trans->srcpad, gst_speex_resample_query); - gst_pad_set_query_type_function (trans->srcpad, - gst_speex_resample_query_type); -} - -/* vmethods */ -static gboolean -gst_speex_resample_start (GstBaseTransform * base) -{ - GstSpeexResample *resample = GST_SPEEX_RESAMPLE (base); - - resample->next_offset = -1; - resample->next_ts = -1; - resample->next_upstream_ts = -1; - - return TRUE; -} - -static gboolean -gst_speex_resample_stop (GstBaseTransform * base) -{ - GstSpeexResample *resample = GST_SPEEX_RESAMPLE (base); - - if (resample->state) { - resample->funcs->destroy (resample->state); - resample->state = NULL; - } - - resample->funcs = NULL; - - g_free (resample->tmp_in); - resample->tmp_in = NULL; - resample->tmp_in_size = 0; - - g_free (resample->tmp_out); - resample->tmp_out = NULL; - resample->tmp_out_size = 0; - - gst_caps_replace (&resample->sinkcaps, NULL); - gst_caps_replace (&resample->srccaps, NULL); - - return TRUE; -} - -static gboolean -gst_speex_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps, - guint * size) -{ - gint width, channels; - GstStructure *structure; - gboolean ret; - - g_return_val_if_fail (size != NULL, FALSE); - - /* this works for both float and int */ - structure = gst_caps_get_structure (caps, 0); - ret = gst_structure_get_int (structure, "width", &width); - ret &= gst_structure_get_int (structure, "channels", &channels); - - if (G_UNLIKELY (!ret)) - return FALSE; - - *size = (width / 8) * channels; - - return TRUE; -} - -static GstCaps * -gst_speex_resample_transform_caps (GstBaseTransform * base, - GstPadDirection direction, GstCaps * caps) -{ - GstCaps *res; - GstStructure *structure; - - /* transform caps gives one single caps so we can just replace - * the rate property with our range. */ - res = gst_caps_copy (caps); - structure = gst_caps_get_structure (res, 0); - gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL); - - return res; -} - -/* Fixate rate to the allowed rate that has the smallest difference */ -static void -gst_speex_resample_fixate_caps (GstBaseTransform * base, - GstPadDirection direction, GstCaps * caps, GstCaps * othercaps) -{ - GstStructure *s; - gint rate; - - s = gst_caps_get_structure (caps, 0); - if (G_UNLIKELY (!gst_structure_get_int (s, "rate", &rate))) - return; - - s = gst_caps_get_structure (othercaps, 0); - gst_structure_fixate_field_nearest_int (s, "rate", rate); -} - -static const SpeexResampleFuncs * -gst_speex_resample_get_funcs (gint width, gboolean fp) -{ - const SpeexResampleFuncs *funcs = NULL; - - if (gst_speex_resample_use_int && (width == 8 || width == 16) && !fp) - funcs = &int_funcs; - else if ((!gst_speex_resample_use_int && (width == 8 || width == 16) && !fp) - || (width == 32 && fp)) - funcs = &float_funcs; - else if ((width == 64 && fp) || ((width == 32 || width == 24) && !fp)) - funcs = &double_funcs; - else - g_assert_not_reached (); - - return funcs; -} - -static SpeexResamplerState * -gst_speex_resample_init_state (GstSpeexResample * resample, gint width, - gint channels, gint inrate, gint outrate, gint quality, gboolean fp) -{ - SpeexResamplerState *ret = NULL; - gint err = RESAMPLER_ERR_SUCCESS; - const SpeexResampleFuncs *funcs = gst_speex_resample_get_funcs (width, fp); - - ret = funcs->init (channels, inrate, outrate, quality, &err); - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { - GST_ERROR_OBJECT (resample, "Failed to create resampler state: %s", - funcs->strerror (err)); - return NULL; - } - - funcs->skip_zeros (ret); - - return ret; -} - -static gboolean -gst_speex_resample_update_state (GstSpeexResample * resample, gint width, - gint channels, gint inrate, gint outrate, gint quality, gboolean fp) -{ - gboolean ret = TRUE; - gboolean updated_latency = FALSE; - - updated_latency = (resample->inrate != inrate - || quality != resample->quality) && resample->state != NULL; - - if (resample->state == NULL) { - ret = TRUE; - } else if (resample->channels != channels || fp != resample->fp - || width != resample->width) { - resample->funcs->destroy (resample->state); - resample->state = - gst_speex_resample_init_state (resample, width, channels, inrate, - outrate, quality, fp); - - resample->funcs = gst_speex_resample_get_funcs (width, fp); - ret = (resample->state != NULL); - } else if (resample->inrate != inrate || resample->outrate != outrate) { - gint err = RESAMPLER_ERR_SUCCESS; - - err = resample->funcs->set_rate (resample->state, inrate, outrate); - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) - GST_ERROR_OBJECT (resample, "Failed to update rate: %s", - resample->funcs->strerror (err)); - - ret = (err == RESAMPLER_ERR_SUCCESS); - } else if (quality != resample->quality) { - gint err = RESAMPLER_ERR_SUCCESS; - - err = resample->funcs->set_quality (resample->state, quality); - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) - GST_ERROR_OBJECT (resample, "Failed to update quality: %s", - resample->funcs->strerror (err)); - - ret = (err == RESAMPLER_ERR_SUCCESS); - } - - resample->width = width; - resample->channels = channels; - resample->fp = fp; - resample->quality = quality; - resample->inrate = inrate; - resample->outrate = outrate; - - if (updated_latency) - gst_element_post_message (GST_ELEMENT (resample), - gst_message_new_latency (GST_OBJECT (resample))); - - return ret; -} - -static void -gst_speex_resample_reset_state (GstSpeexResample * resample) -{ - if (resample->state) - resample->funcs->reset_mem (resample->state); -} - -static gboolean -gst_speex_resample_parse_caps (GstCaps * incaps, - GstCaps * outcaps, gint * width, gint * channels, gint * inrate, - gint * outrate, gboolean * fp) -{ - GstStructure *structure; - gboolean ret; - gint mywidth, myinrate, myoutrate, mychannels; - gboolean myfp; - - GST_DEBUG ("incaps %" GST_PTR_FORMAT ", outcaps %" - GST_PTR_FORMAT, incaps, outcaps); - - structure = gst_caps_get_structure (incaps, 0); - - if (g_str_equal (gst_structure_get_name (structure), "audio/x-raw-float")) - myfp = TRUE; - else - myfp = FALSE; - - ret = gst_structure_get_int (structure, "rate", &myinrate); - ret &= gst_structure_get_int (structure, "channels", &mychannels); - ret &= gst_structure_get_int (structure, "width", &mywidth); - if (G_UNLIKELY (!ret)) - goto no_in_rate_channels; - - structure = gst_caps_get_structure (outcaps, 0); - ret = gst_structure_get_int (structure, "rate", &myoutrate); - if (G_UNLIKELY (!ret)) - goto no_out_rate; - - if (channels) - *channels = mychannels; - if (inrate) - *inrate = myinrate; - if (outrate) - *outrate = myoutrate; - if (width) - *width = mywidth; - if (fp) - *fp = myfp; - - return TRUE; - - /* ERRORS */ -no_in_rate_channels: - { - GST_DEBUG ("could not get input rate and channels"); - return FALSE; - } -no_out_rate: - { - GST_DEBUG ("could not get output rate"); - return FALSE; - } -} - -static gint -_gcd (gint a, gint b) -{ - while (b != 0) { - int temp = a; - - a = b; - b = temp % b; - } - - return ABS (a); -} - -static gboolean -gst_speex_resample_transform_size (GstBaseTransform * base, - GstPadDirection direction, GstCaps * caps, guint size, GstCaps * othercaps, - guint * othersize) -{ - GstSpeexResample *resample = GST_SPEEX_RESAMPLE (base); - GstCaps *srccaps, *sinkcaps; - gboolean ret = TRUE; - guint32 ratio_den, ratio_num; - gint inrate, outrate, gcd; - gint width; - - GST_LOG_OBJECT (resample, "asked to transform size %d in direction %s", - size, direction == GST_PAD_SINK ? "SINK" : "SRC"); - if (direction == GST_PAD_SINK) { - sinkcaps = caps; - srccaps = othercaps; - } else { - sinkcaps = othercaps; - srccaps = caps; - } - - ret = - gst_speex_resample_parse_caps (caps, othercaps, &width, NULL, &inrate, - &outrate, NULL); - if (G_UNLIKELY (!ret)) { - GST_ERROR_OBJECT (resample, "Wrong caps"); - return FALSE; - } - - gcd = _gcd (inrate, outrate); - ratio_num = inrate / gcd; - ratio_den = outrate / gcd; - - if (direction == GST_PAD_SINK) { - gint fac = width / 8; - - /* asked to convert size of an incoming buffer */ - size /= fac; - *othersize = (size * ratio_den + ratio_num - 1) / ratio_num; - *othersize *= fac; - size *= fac; - } else { - gint fac = width / 8; - - /* asked to convert size of an outgoing buffer */ - size /= fac; - *othersize = (size * ratio_num + ratio_den - 1) / ratio_den; - *othersize *= fac; - size *= fac; - } - - GST_LOG_OBJECT (resample, "transformed size %d to %d", size, *othersize); - - return ret; -} - -static gboolean -gst_speex_resample_set_caps (GstBaseTransform * base, GstCaps * incaps, - GstCaps * outcaps) -{ - gboolean ret; - gint width = 0, inrate = 0, outrate = 0, channels = 0; - gboolean fp; - GstSpeexResample *resample = GST_SPEEX_RESAMPLE (base); - - GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %" - GST_PTR_FORMAT, incaps, outcaps); - - ret = gst_speex_resample_parse_caps (incaps, outcaps, - &width, &channels, &inrate, &outrate, &fp); - - if (G_UNLIKELY (!ret)) - return FALSE; - - ret = - gst_speex_resample_update_state (resample, width, channels, inrate, - outrate, resample->quality, fp); - - if (G_UNLIKELY (!ret)) - return FALSE; - - /* save caps so we can short-circuit in the size_transform if the caps - * are the same */ - gst_caps_replace (&resample->sinkcaps, incaps); - gst_caps_replace (&resample->srccaps, outcaps); - - return TRUE; -} - -#define GST_MAXINT24 (8388607) -#define GST_MININT24 (-8388608) - -#if (G_BYTE_ORDER == G_LITTLE_ENDIAN) -#define GST_READ_UINT24 GST_READ_UINT24_LE -#define GST_WRITE_UINT24 GST_WRITE_UINT24_LE -#else -#define GST_READ_UINT24 GST_READ_UINT24_BE -#define GST_WRITE_UINT24 GST_WRITE_UINT24_BE -#endif - -static void -gst_speex_resample_convert_buffer (GstSpeexResample * resample, - const guint8 * in, guint8 * out, guint len, gboolean inverse) -{ - len *= resample->channels; - - if (inverse) { - if (gst_speex_resample_use_int && resample->width == 8 && !resample->fp) { - gint8 *o = (gint8 *) out; - gint16 *i = (gint16 *) in; - gint32 tmp; - - while (len) { - tmp = *i + (G_MAXINT8 >> 1); - *o = CLAMP (tmp >> 8, G_MININT8, G_MAXINT8); - o++; - i++; - len--; - } - } else if (!gst_speex_resample_use_int && resample->width == 8 - && !resample->fp) { - gint8 *o = (gint8 *) out; - gfloat *i = (gfloat *) in; - gfloat tmp; - - while (len) { - tmp = *i; - *o = (gint8) CLAMP (tmp * G_MAXINT8 + 0.5, G_MININT8, G_MAXINT8); - o++; - i++; - len--; - } - } else if (!gst_speex_resample_use_int && resample->width == 16 - && !resample->fp) { - gint16 *o = (gint16 *) out; - gfloat *i = (gfloat *) in; - gfloat tmp; - - while (len) { - tmp = *i; - *o = (gint16) CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16); - o++; - i++; - len--; - } - } else if (resample->width == 24 && !resample->fp) { - guint8 *o = (guint8 *) out; - gdouble *i = (gdouble *) in; - gdouble tmp; - - while (len) { - tmp = *i; - GST_WRITE_UINT24 (o, (gint32) CLAMP (tmp * GST_MAXINT24 + 0.5, - GST_MININT24, GST_MAXINT24)); - o += 3; - i++; - len--; - } - } else if (resample->width == 32 && !resample->fp) { - gint32 *o = (gint32 *) out; - gdouble *i = (gdouble *) in; - gdouble tmp; - - while (len) { - tmp = *i; - *o = (gint32) CLAMP (tmp * G_MAXINT32 + 0.5, G_MININT32, G_MAXINT32); - o++; - i++; - len--; - } - } else { - g_assert_not_reached (); - } - } else { - if (gst_speex_resample_use_int && resample->width == 8 && !resample->fp) { - gint8 *i = (gint8 *) in; - gint16 *o = (gint16 *) out; - gint32 tmp; - - while (len) { - tmp = *i; - *o = tmp << 8; - o++; - i++; - len--; - } - } else if (!gst_speex_resample_use_int && resample->width == 8 - && !resample->fp) { - gint8 *i = (gint8 *) in; - gfloat *o = (gfloat *) out; - gfloat tmp; - - while (len) { - tmp = *i; - *o = tmp / G_MAXINT8; - o++; - i++; - len--; - } - } else if (!gst_speex_resample_use_int && resample->width == 16 - && !resample->fp) { - gint16 *i = (gint16 *) in; - gfloat *o = (gfloat *) out; - gfloat tmp; - - while (len) { - tmp = *i; - *o = tmp / G_MAXINT16; - o++; - i++; - len--; - } - } else if (resample->width == 24 && !resample->fp) { - guint8 *i = (guint8 *) in; - gdouble *o = (gdouble *) out; - gdouble tmp; - guint32 tmp2; - - while (len) { - tmp2 = GST_READ_UINT24 (i); - if (tmp2 & 0x00800000) - tmp2 |= 0xff000000; - tmp = (gint32) tmp2; - *o = tmp / GST_MAXINT24; - o++; - i += 3; - len--; - } - } else if (resample->width == 32 && !resample->fp) { - gint32 *i = (gint32 *) in; - gdouble *o = (gdouble *) out; - gdouble tmp; - - while (len) { - tmp = *i; - *o = tmp / G_MAXINT32; - o++; - i++; - len--; - } - } else { - g_assert_not_reached (); - } - } -} - -static void -gst_speex_resample_push_drain (GstSpeexResample * resample) -{ - GstBuffer *buf; - GstBaseTransform *trans = GST_BASE_TRANSFORM (resample); - GstFlowReturn res; - gint outsize; - guint out_len, out_processed; - gint err; - guint num, den, len; - guint8 *outtmp = NULL; - gboolean need_convert = FALSE; - - if (!resample->state) - return; - - need_convert = (resample->funcs->width != resample->width); - - resample->funcs->get_ratio (resample->state, &num, &den); - - out_len = resample->funcs->get_input_latency (resample->state); - out_len = out_processed = (out_len * den + num - 1) / num; - outsize = (resample->width / 8) * out_len * resample->channels; - - if (need_convert) { - guint outsize_tmp = - (resample->funcs->width / 8) * out_len * resample->channels; - if (outsize_tmp <= resample->tmp_out_size) { - outtmp = resample->tmp_out; - } else { - resample->tmp_out_size = outsize_tmp; - resample->tmp_out = outtmp = g_realloc (resample->tmp_out, outsize_tmp); - } - } - - res = - gst_pad_alloc_buffer_and_set_caps (trans->srcpad, GST_BUFFER_OFFSET_NONE, - outsize, GST_PAD_CAPS (trans->srcpad), &buf); - - if (G_UNLIKELY (res != GST_FLOW_OK)) { - GST_WARNING_OBJECT (resample, "failed allocating buffer of %d bytes", - outsize); - return; - } - - len = resample->funcs->get_input_latency (resample->state); - - err = - resample->funcs->process (resample->state, - NULL, &len, (need_convert) ? outtmp : GST_BUFFER_DATA (buf), - &out_processed); - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { - GST_WARNING_OBJECT (resample, "Failed to process drain: %s", - resample->funcs->strerror (err)); - gst_buffer_unref (buf); - return; - } - - if (G_UNLIKELY (out_processed == 0)) { - GST_WARNING_OBJECT (resample, "Failed to get drain, dropping buffer"); - gst_buffer_unref (buf); - return; - } - - /* If we wrote more than allocated something is really wrong now - * and we should better abort immediately */ - g_assert (out_len >= out_processed); - - if (need_convert) - gst_speex_resample_convert_buffer (resample, outtmp, GST_BUFFER_DATA (buf), - out_processed, TRUE); - - GST_BUFFER_DURATION (buf) = - GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate); - GST_BUFFER_SIZE (buf) = - out_processed * resample->channels * (resample->width / 8); - - if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { - GST_BUFFER_OFFSET (buf) = resample->next_offset; - GST_BUFFER_OFFSET_END (buf) = resample->next_offset + out_processed; - GST_BUFFER_TIMESTAMP (buf) = resample->next_ts; - - resample->next_ts += GST_BUFFER_DURATION (buf); - resample->next_offset += out_processed; - } - - GST_LOG_OBJECT (resample, - "Pushing drain buffer of %u bytes with timestamp %" GST_TIME_FORMAT - " duration %" GST_TIME_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %" - G_GUINT64_FORMAT, GST_BUFFER_SIZE (buf), - GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), - GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf), - GST_BUFFER_OFFSET_END (buf)); - - res = gst_pad_push (trans->srcpad, buf); - - if (G_UNLIKELY (res != GST_FLOW_OK)) - GST_WARNING_OBJECT (resample, "Failed to push drain: %s", - gst_flow_get_name (res)); - - return; -} - -static gboolean -gst_speex_resample_event (GstBaseTransform * base, GstEvent * event) -{ - GstSpeexResample *resample = GST_SPEEX_RESAMPLE (base); - - switch (GST_EVENT_TYPE (event)) { - case GST_EVENT_FLUSH_START: - break; - case GST_EVENT_FLUSH_STOP: - gst_speex_resample_reset_state (resample); - resample->next_offset = -1; - resample->next_ts = -1; - resample->next_upstream_ts = -1; - case GST_EVENT_NEWSEGMENT: - gst_speex_resample_push_drain (resample); - gst_speex_resample_reset_state (resample); - resample->next_offset = -1; - resample->next_ts = -1; - resample->next_upstream_ts = -1; - break; - case GST_EVENT_EOS:{ - gst_speex_resample_push_drain (resample); - gst_speex_resample_reset_state (resample); - break; - } - default: - break; - } - - return parent_class->event (base, event); -} - -static gboolean -gst_speex_resample_check_discont (GstSpeexResample * resample, - GstClockTime timestamp) -{ - if (timestamp != GST_CLOCK_TIME_NONE && - resample->next_upstream_ts != GST_CLOCK_TIME_NONE && - timestamp != resample->next_upstream_ts) { - /* Potentially a discontinuous buffer. However, it turns out that many - * elements generate imperfect streams due to rounding errors, so we permit - * a small error (up to one sample) without triggering a filter - * flush/restart (if triggered incorrectly, this will be audible) */ - GstClockTimeDiff diff = timestamp - resample->next_upstream_ts; - - if (ABS (diff) > (GST_SECOND + resample->inrate - 1) / resample->inrate) { - GST_WARNING_OBJECT (resample, - "encountered timestamp discontinuity of %s%" GST_TIME_FORMAT, - (diff < 0) ? "-" : "", GST_TIME_ARGS ((GstClockTime) ABS (diff))); - return TRUE; - } - } - - return FALSE; -} - -static GstFlowReturn -gst_speex_resample_process (GstSpeexResample * resample, GstBuffer * inbuf, - GstBuffer * outbuf) -{ - guint32 in_len, in_processed; - guint32 out_len, out_processed; - gint err = RESAMPLER_ERR_SUCCESS; - guint8 *in_tmp = NULL, *out_tmp = NULL; - gboolean need_convert = (resample->funcs->width != resample->width); - - in_len = GST_BUFFER_SIZE (inbuf) / resample->channels; - out_len = GST_BUFFER_SIZE (outbuf) / resample->channels; - - in_len /= (resample->width / 8); - out_len /= (resample->width / 8); - - in_processed = in_len; - out_processed = out_len; - - if (need_convert) { - guint in_size_tmp = - in_len * resample->channels * (resample->funcs->width / 8); - guint out_size_tmp = - out_len * resample->channels * (resample->funcs->width / 8); - - if (in_size_tmp <= resample->tmp_in_size) { - in_tmp = resample->tmp_in; - } else { - resample->tmp_in = in_tmp = g_realloc (resample->tmp_in, in_size_tmp); - resample->tmp_in_size = in_size_tmp; - } - - gst_speex_resample_convert_buffer (resample, GST_BUFFER_DATA (inbuf), - in_tmp, in_len, FALSE); - - if (out_size_tmp <= resample->tmp_out_size) { - out_tmp = resample->tmp_out; - } else { - resample->tmp_out = out_tmp = g_realloc (resample->tmp_out, out_size_tmp); - resample->tmp_out_size = out_size_tmp; - } - } - - if (need_convert) { - err = resample->funcs->process (resample->state, - in_tmp, &in_processed, out_tmp, &out_processed); - } else { - err = resample->funcs->process (resample->state, - (const guint8 *) GST_BUFFER_DATA (inbuf), &in_processed, - (guint8 *) GST_BUFFER_DATA (outbuf), &out_processed); - } - - if (G_UNLIKELY (in_len != in_processed)) - GST_WARNING_OBJECT (resample, "Converted %d of %d input samples", - in_processed, in_len); - - if (out_len != out_processed) { - if (out_processed == 0) { - GST_DEBUG_OBJECT (resample, "Converted to 0 samples, buffer dropped"); - - return GST_BASE_TRANSFORM_FLOW_DROPPED; - } - - /* If we wrote more than allocated something is really wrong now - * and we should better abort immediately */ - g_assert (out_len >= out_processed); - } - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { - GST_ERROR_OBJECT (resample, "Failed to convert data: %s", - resample->funcs->strerror (err)); - return GST_FLOW_ERROR; - } else { - - if (need_convert) - gst_speex_resample_convert_buffer (resample, out_tmp, - GST_BUFFER_DATA (outbuf), out_processed, TRUE); - - GST_BUFFER_DURATION (outbuf) = - GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate); - GST_BUFFER_SIZE (outbuf) = - out_processed * resample->channels * (resample->width / 8); - - if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { - GST_BUFFER_TIMESTAMP (outbuf) = resample->next_ts; - GST_BUFFER_OFFSET (outbuf) = resample->next_offset; - GST_BUFFER_OFFSET_END (outbuf) = resample->next_offset + out_processed; - - resample->next_ts += GST_BUFFER_DURATION (outbuf); - resample->next_offset += out_processed; - } - - GST_LOG_OBJECT (resample, - "Converted to buffer of %u bytes with timestamp %" GST_TIME_FORMAT - ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT - ", offset_end %" G_GUINT64_FORMAT, GST_BUFFER_SIZE (outbuf), - GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), - GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), - GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf)); - - return GST_FLOW_OK; - } -} - -static GstFlowReturn -gst_speex_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, - GstBuffer * outbuf) -{ - GstSpeexResample *resample = GST_SPEEX_RESAMPLE (base); - guint8 *data; - gulong size; - GstClockTime timestamp; - guint outsamples, insamples; - GstFlowReturn ret; - - if (resample->state == NULL) { - if (G_UNLIKELY (!(resample->state = - gst_speex_resample_init_state (resample, resample->width, - resample->channels, resample->inrate, resample->outrate, - resample->quality, resample->fp)))) - return GST_FLOW_ERROR; - - resample->funcs = - gst_speex_resample_get_funcs (resample->width, resample->fp); - } - - data = GST_BUFFER_DATA (inbuf); - size = GST_BUFFER_SIZE (inbuf); - timestamp = GST_BUFFER_TIMESTAMP (inbuf); - - GST_LOG_OBJECT (resample, "transforming buffer of %ld bytes, ts %" - GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %" - G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT, - size, GST_TIME_ARGS (timestamp), - GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)), - GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf)); - - /* check for timestamp discontinuities and flush/reset if needed */ - if (G_UNLIKELY (gst_speex_resample_check_discont (resample, timestamp) - || GST_BUFFER_IS_DISCONT (inbuf))) { - /* Flush internal samples */ - gst_speex_resample_reset_state (resample); - /* Inform downstream element about discontinuity */ - resample->need_discont = TRUE; - /* We want to recalculate the timestamps */ - resample->next_ts = -1; - resample->next_upstream_ts = -1; - resample->next_offset = -1; - } - - insamples = GST_BUFFER_SIZE (inbuf) / resample->channels; - insamples /= (resample->width / 8); - - outsamples = GST_BUFFER_SIZE (outbuf) / resample->channels; - outsamples /= (resample->width / 8); - - if (GST_CLOCK_TIME_IS_VALID (timestamp) - && !GST_CLOCK_TIME_IS_VALID (resample->next_ts)) { - resample->next_ts = timestamp; - resample->next_offset = - GST_CLOCK_TIME_TO_FRAMES (timestamp, resample->outrate); - } - - if (G_UNLIKELY (resample->need_discont)) { - GST_DEBUG_OBJECT (resample, "marking this buffer with the DISCONT flag"); - GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT); - resample->need_discont = FALSE; - } - - ret = gst_speex_resample_process (resample, inbuf, outbuf); - if (G_UNLIKELY (ret != GST_FLOW_OK)) - return ret; - - if (GST_CLOCK_TIME_IS_VALID (timestamp) - && !GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts)) - resample->next_upstream_ts = timestamp; - - if (GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts)) - resample->next_upstream_ts += - GST_FRAMES_TO_CLOCK_TIME (insamples, resample->inrate); - - return GST_FLOW_OK; -} - -static gboolean -gst_speex_resample_query (GstPad * pad, GstQuery * query) -{ - GstSpeexResample *resample = GST_SPEEX_RESAMPLE (gst_pad_get_parent (pad)); - GstBaseTransform *trans = GST_BASE_TRANSFORM (resample); - gboolean res = TRUE; - - switch (GST_QUERY_TYPE (query)) { - case GST_QUERY_LATENCY: - { - GstClockTime min, max; - gboolean live; - guint64 latency; - GstPad *peer; - gint rate = resample->inrate; - gint resampler_latency; - - if (resample->state) - resampler_latency = - resample->funcs->get_input_latency (resample->state); - else - resampler_latency = 0; - - if (gst_base_transform_is_passthrough (trans)) - resampler_latency = 0; - - if ((peer = gst_pad_get_peer (trans->sinkpad))) { - if ((res = gst_pad_query (peer, query))) { - gst_query_parse_latency (query, &live, &min, &max); - - GST_DEBUG_OBJECT (resample, "Peer latency: min %" - GST_TIME_FORMAT " max %" GST_TIME_FORMAT, - GST_TIME_ARGS (min), GST_TIME_ARGS (max)); - - /* add our own latency */ - if (rate != 0 && resampler_latency != 0) - latency = - gst_util_uint64_scale (resampler_latency, GST_SECOND, rate); - else - latency = 0; - - GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT, - GST_TIME_ARGS (latency)); - - min += latency; - if (max != GST_CLOCK_TIME_NONE) - max += latency; - - GST_DEBUG_OBJECT (resample, "Calculated total latency : min %" - GST_TIME_FORMAT " max %" GST_TIME_FORMAT, - GST_TIME_ARGS (min), GST_TIME_ARGS (max)); - - gst_query_set_latency (query, live, min, max); - } - gst_object_unref (peer); - } - break; - } - default: - res = gst_pad_query_default (pad, query); - break; - } - gst_object_unref (resample); - return res; -} - -static const GstQueryType * -gst_speex_resample_query_type (GstPad * pad) -{ - static const GstQueryType types[] = { - GST_QUERY_LATENCY, - 0 - }; - - return types; -} - -static void -gst_speex_resample_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec) -{ - GstSpeexResample *resample; - - resample = GST_SPEEX_RESAMPLE (object); - - switch (prop_id) { - case PROP_QUALITY: - resample->quality = g_value_get_int (value); - GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality); - - gst_speex_resample_update_state (resample, resample->width, - resample->channels, resample->inrate, resample->outrate, - resample->quality, resample->fp); - break; - case PROP_FILTER_LENGTH:{ - gint filter_length = g_value_get_int (value); - - if (filter_length <= 8) - resample->quality = 0; - else if (filter_length <= 16) - resample->quality = 1; - else if (filter_length <= 32) - resample->quality = 2; - else if (filter_length <= 48) - resample->quality = 3; - else if (filter_length <= 64) - resample->quality = 4; - else if (filter_length <= 80) - resample->quality = 5; - else if (filter_length <= 96) - resample->quality = 6; - else if (filter_length <= 128) - resample->quality = 7; - else if (filter_length <= 160) - resample->quality = 8; - else if (filter_length <= 192) - resample->quality = 9; - else - resample->quality = 10; - - GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality); - - gst_speex_resample_update_state (resample, resample->width, - resample->channels, resample->inrate, resample->outrate, - resample->quality, resample->fp); - break; - } - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -gst_speex_resample_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec) -{ - GstSpeexResample *resample; - - resample = GST_SPEEX_RESAMPLE (object); - - switch (prop_id) { - case PROP_QUALITY: - g_value_set_int (value, resample->quality); - break; - case PROP_FILTER_LENGTH: - switch (resample->quality) { - case 0: - g_value_set_int (value, 8); - break; - case 1: - g_value_set_int (value, 16); - break; - case 2: - g_value_set_int (value, 32); - break; - case 3: - g_value_set_int (value, 48); - break; - case 4: - g_value_set_int (value, 64); - break; - case 5: - g_value_set_int (value, 80); - break; - case 6: - g_value_set_int (value, 96); - break; - case 7: - g_value_set_int (value, 128); - break; - case 8: - g_value_set_int (value, 160); - break; - case 9: - g_value_set_int (value, 192); - break; - case 10: - g_value_set_int (value, 256); - break; - } - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -#define BENCHMARK_SIZE 512 - -static gboolean -_benchmark_int_float (SpeexResamplerState * st) -{ - gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2]; - gfloat in_tmp[BENCHMARK_SIZE], out_tmp[BENCHMARK_SIZE / 2]; - gint i; - guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2; - - for (i = 0; i < BENCHMARK_SIZE; i++) { - gfloat tmp = in[i]; - in_tmp[i] = tmp / G_MAXINT16; - } - - resample_float_resampler_process_interleaved_float (st, - (const guint8 *) in_tmp, &inlen, (guint8 *) out_tmp, &outlen); - - if (outlen == 0) { - GST_ERROR ("Failed to use float resampler"); - return FALSE; - } - - for (i = 0; i < outlen; i++) { - gfloat tmp = out_tmp[i]; - out[i] = CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16); - } - - return TRUE; -} - -static gboolean -_benchmark_int_int (SpeexResamplerState * st) -{ - gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2]; - guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2; - - resample_int_resampler_process_interleaved_int (st, (const guint8 *) in, - &inlen, (guint8 *) out, &outlen); - - if (outlen == 0) { - GST_ERROR ("Failed to use int resampler"); - return FALSE; - } - - return TRUE; -} - -static gboolean -_benchmark_integer_resampling (void) -{ - OilProfile a, b; - gdouble av, bv; - SpeexResamplerState *sta, *stb; - - oil_profile_init (&a); - oil_profile_init (&b); - - sta = resample_float_resampler_init (1, 48000, 24000, 4, NULL); - if (sta == NULL) { - GST_ERROR ("Failed to create float resampler state"); - return FALSE; - } - - stb = resample_int_resampler_init (1, 48000, 24000, 4, NULL); - if (stb == NULL) { - resample_float_resampler_destroy (sta); - GST_ERROR ("Failed to create int resampler state"); - return FALSE; - } - - /* Warm up cache */ - if (!_benchmark_int_float (sta)) - goto error; - if (!_benchmark_int_float (sta)) - goto error; - - /* Benchmark */ - oil_profile_start (&a); - if (!_benchmark_int_float (sta)) - goto error; - oil_profile_stop (&a); - - /* Warm up cache */ - if (!_benchmark_int_int (stb)) - goto error; - if (!_benchmark_int_int (stb)) - goto error; - - /* Benchmark */ - oil_profile_start (&b); - if (!_benchmark_int_int (stb)) - goto error; - oil_profile_stop (&b); - - /* Handle results */ - oil_profile_get_ave_std (&a, &av, NULL); - oil_profile_get_ave_std (&b, &bv, NULL); - - gst_speex_resample_use_int = (av > bv); - resample_float_resampler_destroy (sta); - resample_float_resampler_destroy (stb); - - if (av > bv) - GST_DEBUG ("Using integer resampler if appropiate: %lf < %lf", bv, av); - else - GST_DEBUG ("Using float resampler for everything: %lf <= %lf", av, bv); - - return TRUE; - -error: - resample_float_resampler_destroy (sta); - resample_float_resampler_destroy (stb); - - return FALSE; -} - -static gboolean -plugin_init (GstPlugin * plugin) -{ - GST_DEBUG_CATEGORY_INIT (speex_resample_debug, "audioresample", 0, - "audio resampling element"); - - oil_init (); - - if (!_benchmark_integer_resampling ()) - return FALSE; - - if (!gst_element_register (plugin, "audioresample", GST_RANK_PRIMARY, - GST_TYPE_SPEEX_RESAMPLE)) { - return FALSE; - } - - return TRUE; -} - -GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, - GST_VERSION_MINOR, - "audioresample", - "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, - GST_PACKAGE_ORIGIN); diff --git a/gst/speexresample/gstspeexresample.h b/gst/speexresample/gstspeexresample.h deleted file mode 100644 index e39148a..0000000 --- a/gst/speexresample/gstspeexresample.h +++ /dev/null @@ -1,90 +0,0 @@ -/* GStreamer - * Copyright (C) <1999> Erik Walthinsen - * Copyright (C) <2007-2008> Sebastian Dröge - * - * 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 __SPEEX_RESAMPLE_H__ -#define __SPEEX_RESAMPLE_H__ - -#include -#include -#include - -#include "speex_resampler_wrapper.h" - -G_BEGIN_DECLS - -#define GST_TYPE_SPEEX_RESAMPLE \ - (gst_speex_resample_get_type()) -#define GST_SPEEX_RESAMPLE(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SPEEX_RESAMPLE,GstSpeexResample)) -#define GST_SPEEX_RESAMPLE_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SPEEX_RESAMPLE,GstSpeexResampleClass)) -#define GST_IS_SPEEX_RESAMPLE(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SPEEX_RESAMPLE)) -#define GST_IS_SPEEX_RESAMPLE_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SPEEX_RESAMPLE)) - -typedef struct _GstSpeexResample GstSpeexResample; -typedef struct _GstSpeexResampleClass GstSpeexResampleClass; - -/** - * GstSpeexResample: - * - * Opaque data structure. - */ -struct _GstSpeexResample { - GstBaseTransform element; - - /* */ - - GstCaps *srccaps, *sinkcaps; - - gboolean need_discont; - - guint64 next_offset; - GstClockTime next_ts; - GstClockTime next_upstream_ts; - - gint channels; - gint inrate; - gint outrate; - gint quality; - gint width; - gboolean fp; - - guint8 *tmp_in; - guint tmp_in_size; - - guint8 *tmp_out; - guint tmp_out_size; - - SpeexResamplerState *state; - const SpeexResampleFuncs *funcs; -}; - -struct _GstSpeexResampleClass { - GstBaseTransformClass parent_class; -}; - -GType gst_speex_resample_get_type(void); - -G_END_DECLS - -#endif /* __SPEEX_RESAMPLE_H__ */ diff --git a/gst/speexresample/resample.c b/gst/speexresample/resample.c deleted file mode 100644 index f29e614..0000000 --- a/gst/speexresample/resample.c +++ /dev/null @@ -1,1340 +0,0 @@ -/* Copyright (C) 2007-2008 Jean-Marc Valin - Copyright (C) 2008 Thorvald Natvig - - File: resample.c - Arbitrary resampling code - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. 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. - - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. -*/ - -/* - The design goals of this code are: - - Very fast algorithm - - SIMD-friendly algorithm - - Low memory requirement - - Good *perceptual* quality (and not best SNR) - - Warning: This resampler is relatively new. Although I think I got rid of - all the major bugs and I don't expect the API to change anymore, there - may be something I've missed. So use with caution. - - This algorithm is based on this original resampling algorithm: - Smith, Julius O. Digital Audio Resampling Home Page - Center for Computer Research in Music and Acoustics (CCRMA), - Stanford University, 2007. - Web published at http://www-ccrma.stanford.edu/~jos/resample/. - - There is one main difference, though. This resampler uses cubic - interpolation instead of linear interpolation in the above paper. This - makes the table much smaller and makes it possible to compute that table - on a per-stream basis. In turn, being able to tweak the table for each - stream makes it possible to both reduce complexity on simple ratios - (e.g. 2/3), and get rid of the rounding operations in the inner loop. - The latter both reduces CPU time and makes the algorithm more SIMD-friendly. -*/ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#ifdef OUTSIDE_SPEEX -#include - -#include - -#define EXPORT G_GNUC_INTERNAL - -static inline void * -speex_alloc (int size) -{ - return g_malloc0 (size); -} - -static inline void * -speex_realloc (void *ptr, int size) -{ - return g_realloc (ptr, size); -} - -static inline void -speex_free (void *ptr) -{ - g_free (ptr); -} - -#include "speex_resampler.h" -#include "arch.h" -#else /* OUTSIDE_SPEEX */ - -#include "../include/speex/speex_resampler.h" -#include "arch.h" -#include "os_support.h" -#endif /* OUTSIDE_SPEEX */ - -#include - -#ifndef M_PI -#define M_PI 3.14159263 -#endif - -#ifdef FIXED_POINT -#define WORD2INT(x) ((x) < -32767 ? -32768 : ((x) > 32766 ? 32767 : (x))) -#else -#define WORD2INT(x) ((x) < -32767.5f ? -32768 : ((x) > 32766.5f ? 32767 : floor(.5+(x)))) -#endif - -#define IMAX(a,b) ((a) > (b) ? (a) : (b)) -#define IMIN(a,b) ((a) < (b) ? (a) : (b)) - -#ifndef NULL -#define NULL 0 -#endif - -#ifdef _USE_SSE -#include "resample_sse.h" -#endif - -/* Numer of elements to allocate on the stack */ -#ifdef VAR_ARRAYS -#define FIXED_STACK_ALLOC 8192 -#else -#define FIXED_STACK_ALLOC 1024 -#endif - -typedef int (*resampler_basic_func) (SpeexResamplerState *, spx_uint32_t, - const spx_word16_t *, spx_uint32_t *, spx_word16_t *, spx_uint32_t *); - -struct SpeexResamplerState_ -{ - spx_uint32_t in_rate; - spx_uint32_t out_rate; - spx_uint32_t num_rate; - spx_uint32_t den_rate; - - int quality; - spx_uint32_t nb_channels; - spx_uint32_t filt_len; - spx_uint32_t mem_alloc_size; - spx_uint32_t buffer_size; - int int_advance; - int frac_advance; - float cutoff; - spx_uint32_t oversample; - int initialised; - int started; - - /* These are per-channel */ - spx_int32_t *last_sample; - spx_uint32_t *samp_frac_num; - spx_uint32_t *magic_samples; - - spx_word16_t *mem; - spx_word16_t *sinc_table; - spx_uint32_t sinc_table_length; - resampler_basic_func resampler_ptr; - - int in_stride; - int out_stride; -}; - -static double kaiser12_table[68] = { - 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076, - 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014, - 0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601, - 0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014, - 0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490, - 0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546, - 0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178, - 0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947, - 0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058, - 0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438, - 0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734, - 0.00001000, 0.00000000 -}; - -/* -static double kaiser12_table[36] = { - 0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741, - 0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762, - 0.49704014, 0.43304576, 0.37206735, 0.31506490, 0.26276832, 0.21567274, - 0.17404546, 0.13794294, 0.10723616, 0.08164178, 0.06075685, 0.04409466, - 0.03111947, 0.02127838, 0.01402878, 0.00886058, 0.00531256, 0.00298291, - 0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000}; -*/ -static double kaiser10_table[36] = { - 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446, - 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347, - 0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962, - 0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451, - 0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739, - 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000 -}; - -static double kaiser8_table[36] = { - 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200, - 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126, - 0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272, - 0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758, - 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490, - 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000 -}; - -static double kaiser6_table[36] = { - 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003, - 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565, - 0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561, - 0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058, - 0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600, - 0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000 -}; - -struct FuncDef -{ - double *table; - int oversample; -}; - -static struct FuncDef _KAISER12 = { kaiser12_table, 64 }; - -#define KAISER12 (&_KAISER12) -/*static struct FuncDef _KAISER12 = {kaiser12_table, 32}; -#define KAISER12 (&_KAISER12)*/ -static struct FuncDef _KAISER10 = { kaiser10_table, 32 }; - -#define KAISER10 (&_KAISER10) -static struct FuncDef _KAISER8 = { kaiser8_table, 32 }; - -#define KAISER8 (&_KAISER8) -static struct FuncDef _KAISER6 = { kaiser6_table, 32 }; - -#define KAISER6 (&_KAISER6) - -struct QualityMapping -{ - int base_length; - int oversample; - float downsample_bandwidth; - float upsample_bandwidth; - struct FuncDef *window_func; -}; - - -/* This table maps conversion quality to internal parameters. There are two - reasons that explain why the up-sampling bandwidth is larger than the - down-sampling bandwidth: - 1) When up-sampling, we can assume that the spectrum is already attenuated - close to the Nyquist rate (from an A/D or a previous resampling filter) - 2) Any aliasing that occurs very close to the Nyquist rate will be masked - by the sinusoids/noise just below the Nyquist rate (guaranteed only for - up-sampling). -*/ -static const struct QualityMapping quality_map[11] = { - {8, 4, 0.830f, 0.860f, KAISER6}, /* Q0 */ - {16, 4, 0.850f, 0.880f, KAISER6}, /* Q1 */ - {32, 4, 0.882f, 0.910f, KAISER6}, /* Q2 *//* 82.3% cutoff ( ~60 dB stop) 6 */ - {48, 8, 0.895f, 0.917f, KAISER8}, /* Q3 *//* 84.9% cutoff ( ~80 dB stop) 8 */ - {64, 8, 0.921f, 0.940f, KAISER8}, /* Q4 *//* 88.7% cutoff ( ~80 dB stop) 8 */ - {80, 16, 0.922f, 0.940f, KAISER10}, /* Q5 *//* 89.1% cutoff (~100 dB stop) 10 */ - {96, 16, 0.940f, 0.945f, KAISER10}, /* Q6 *//* 91.5% cutoff (~100 dB stop) 10 */ - {128, 16, 0.950f, 0.950f, KAISER10}, /* Q7 *//* 93.1% cutoff (~100 dB stop) 10 */ - {160, 16, 0.960f, 0.960f, KAISER10}, /* Q8 *//* 94.5% cutoff (~100 dB stop) 10 */ - {192, 32, 0.968f, 0.968f, KAISER12}, /* Q9 *//* 95.5% cutoff (~100 dB stop) 10 */ - {256, 32, 0.975f, 0.975f, KAISER12}, /* Q10 *//* 96.6% cutoff (~100 dB stop) 10 */ -}; - -/*8,24,40,56,80,104,128,160,200,256,320*/ -#ifdef DOUBLE_PRECISION -static double -compute_func (double x, struct FuncDef *func) -{ - double y, frac; -#else -static double -compute_func (float x, struct FuncDef *func) -{ - float y, frac; -#endif - double interp[4]; - int ind; - y = x * func->oversample; - ind = (int) floor (y); - frac = (y - ind); - /* CSE with handle the repeated powers */ - interp[3] = -0.1666666667 * frac + 0.1666666667 * (frac * frac * frac); - interp[2] = frac + 0.5 * (frac * frac) - 0.5 * (frac * frac * frac); - /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac; */ - interp[0] = - -0.3333333333 * frac + 0.5 * (frac * frac) - - 0.1666666667 * (frac * frac * frac); - /* Just to make sure we don't have rounding problems */ - interp[1] = 1.f - interp[3] - interp[2] - interp[0]; - - /*sum = frac*accum[1] + (1-frac)*accum[2]; */ - return interp[0] * func->table[ind] + interp[1] * func->table[ind + 1] + - interp[2] * func->table[ind + 2] + interp[3] * func->table[ind + 3]; -} - -#if 0 -#include -int -main (int argc, char **argv) -{ - int i; - for (i = 0; i < 256; i++) { - printf ("%f\n", compute_func (i / 256., KAISER12)); - } - return 0; -} -#endif - -#ifdef FIXED_POINT -/* The slow way of computing a sinc for the table. Should improve that some day */ -static spx_word16_t -sinc (float cutoff, float x, int N, struct FuncDef *window_func) -{ - /*fprintf (stderr, "%f ", x); */ - float xx = x * cutoff; - if (fabs (x) < 1e-6f) - return WORD2INT (32768. * cutoff); - else if (fabs (x) > .5f * N) - return 0; - /*FIXME: Can it really be any slower than this? */ - return WORD2INT (32768. * cutoff * sin (M_PI * xx) / (M_PI * xx) * - compute_func (fabs (2. * x / N), window_func)); -} -#else -/* The slow way of computing a sinc for the table. Should improve that some day */ -#ifdef DOUBLE_PRECISION -static spx_word16_t -sinc (double cutoff, double x, int N, struct FuncDef *window_func) -{ - /*fprintf (stderr, "%f ", x); */ - double xx = x * cutoff; -#else -static spx_word16_t -sinc (float cutoff, float x, int N, struct FuncDef *window_func) -{ - /*fprintf (stderr, "%f ", x); */ - float xx = x * cutoff; -#endif - if (fabs (x) < 1e-6) - return cutoff; - else if (fabs (x) > .5 * N) - return 0; - /*FIXME: Can it really be any slower than this? */ - return cutoff * sin (M_PI * xx) / (M_PI * xx) * compute_func (fabs (2. * x / - N), window_func); -} -#endif - -#ifdef FIXED_POINT -static void -cubic_coef (spx_word16_t x, spx_word16_t interp[4]) -{ - /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation - but I know it's MMSE-optimal on a sinc */ - spx_word16_t x2, x3; - x2 = MULT16_16_P15 (x, x); - x3 = MULT16_16_P15 (x, x2); - interp[0] = - PSHR32 (MULT16_16 (QCONST16 (-0.16667f, 15), - x) + MULT16_16 (QCONST16 (0.16667f, 15), x3), 15); - interp[1] = - EXTRACT16 (EXTEND32 (x) + SHR32 (SUB32 (EXTEND32 (x2), EXTEND32 (x3)), - 1)); - interp[3] = - PSHR32 (MULT16_16 (QCONST16 (-0.33333f, 15), - x) + MULT16_16 (QCONST16 (.5f, 15), - x2) - MULT16_16 (QCONST16 (0.16667f, 15), x3), 15); - /* Just to make sure we don't have rounding problems */ - interp[2] = Q15_ONE - interp[0] - interp[1] - interp[3]; - if (interp[2] < 32767) - interp[2] += 1; -} -#else -static void -cubic_coef (spx_word16_t frac, spx_word16_t interp[4]) -{ - /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation - but I know it's MMSE-optimal on a sinc */ - interp[0] = -0.16667f * frac + 0.16667f * frac * frac * frac; - interp[1] = frac + 0.5f * frac * frac - 0.5f * frac * frac * frac; - /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac; */ - interp[3] = - -0.33333f * frac + 0.5f * frac * frac - 0.16667f * frac * frac * frac; - /* Just to make sure we don't have rounding problems */ - interp[2] = 1. - interp[0] - interp[1] - interp[3]; -} -#endif - -#ifndef DOUBLE_PRECISION -static int -resampler_basic_direct_single (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, - spx_word16_t * out, spx_uint32_t * out_len) -{ - const int N = st->filt_len; - int out_sample = 0; - int last_sample = st->last_sample[channel_index]; - spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; - const spx_word16_t *sinc_table = st->sinc_table; - const int out_stride = st->out_stride; - const int int_advance = st->int_advance; - const int frac_advance = st->frac_advance; - const spx_uint32_t den_rate = st->den_rate; - spx_word32_t sum; - int j; - - while (!(last_sample >= (spx_int32_t) * in_len - || out_sample >= (spx_int32_t) * out_len)) { - const spx_word16_t *sinc = &sinc_table[samp_frac_num * N]; - const spx_word16_t *iptr = &in[last_sample]; - -#ifndef OVERRIDE_INNER_PRODUCT_SINGLE - float accum[4] = { 0, 0, 0, 0 }; - - for (j = 0; j < N; j += 4) { - accum[0] += sinc[j] * iptr[j]; - accum[1] += sinc[j + 1] * iptr[j + 1]; - accum[2] += sinc[j + 2] * iptr[j + 2]; - accum[3] += sinc[j + 3] * iptr[j + 3]; - } - sum = accum[0] + accum[1] + accum[2] + accum[3]; -#else - sum = inner_product_single (sinc, iptr, N); -#endif - - out[out_stride * out_sample++] = PSHR32 (sum, 15); - last_sample += int_advance; - samp_frac_num += frac_advance; - if (samp_frac_num >= den_rate) { - samp_frac_num -= den_rate; - last_sample++; - } - } - - st->last_sample[channel_index] = last_sample; - st->samp_frac_num[channel_index] = samp_frac_num; - return out_sample; -} -#endif - -#ifdef FIXED_POINT -#else -/* This is the same as the previous function, except with a double-precision accumulator */ -static int -resampler_basic_direct_double (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, - spx_word16_t * out, spx_uint32_t * out_len) -{ - const int N = st->filt_len; - int out_sample = 0; - int last_sample = st->last_sample[channel_index]; - spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; - const spx_word16_t *sinc_table = st->sinc_table; - const int out_stride = st->out_stride; - const int int_advance = st->int_advance; - const int frac_advance = st->frac_advance; - const spx_uint32_t den_rate = st->den_rate; - double sum; - int j; - - while (!(last_sample >= (spx_int32_t) * in_len - || out_sample >= (spx_int32_t) * out_len)) { - const spx_word16_t *sinc = &sinc_table[samp_frac_num * N]; - const spx_word16_t *iptr = &in[last_sample]; - -#ifndef OVERRIDE_INNER_PRODUCT_DOUBLE - double accum[4] = { 0, 0, 0, 0 }; - - for (j = 0; j < N; j += 4) { - accum[0] += sinc[j] * iptr[j]; - accum[1] += sinc[j + 1] * iptr[j + 1]; - accum[2] += sinc[j + 2] * iptr[j + 2]; - accum[3] += sinc[j + 3] * iptr[j + 3]; - } - sum = accum[0] + accum[1] + accum[2] + accum[3]; -#else - sum = inner_product_double (sinc, iptr, N); -#endif - - out[out_stride * out_sample++] = PSHR32 (sum, 15); - last_sample += int_advance; - samp_frac_num += frac_advance; - if (samp_frac_num >= den_rate) { - samp_frac_num -= den_rate; - last_sample++; - } - } - - st->last_sample[channel_index] = last_sample; - st->samp_frac_num[channel_index] = samp_frac_num; - return out_sample; -} -#endif - -#ifndef DOUBLE_PRECISION -static int -resampler_basic_interpolate_single (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, - spx_word16_t * out, spx_uint32_t * out_len) -{ - const int N = st->filt_len; - int out_sample = 0; - int last_sample = st->last_sample[channel_index]; - spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; - const int out_stride = st->out_stride; - const int int_advance = st->int_advance; - const int frac_advance = st->frac_advance; - const spx_uint32_t den_rate = st->den_rate; - int j; - spx_word32_t sum; - - while (!(last_sample >= (spx_int32_t) * in_len - || out_sample >= (spx_int32_t) * out_len)) { - const spx_word16_t *iptr = &in[last_sample]; - - const int offset = samp_frac_num * st->oversample / st->den_rate; -#ifdef FIXED_POINT - const spx_word16_t frac = - PDIV32 (SHL32 ((samp_frac_num * st->oversample) % st->den_rate, 15), - st->den_rate); -#else - const spx_word16_t frac = - ((float) ((samp_frac_num * st->oversample) % st->den_rate)) / - st->den_rate; -#endif - spx_word16_t interp[4]; - - -#ifndef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE - spx_word32_t accum[4] = { 0, 0, 0, 0 }; - - for (j = 0; j < N; j++) { - const spx_word16_t curr_in = iptr[j]; - accum[0] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset - 2]); - accum[1] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset - 1]); - accum[2] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset]); - accum[3] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset + 1]); - } - - cubic_coef (frac, interp); - sum = - MULT16_32_Q15 (interp[0], accum[0]) + MULT16_32_Q15 (interp[1], - accum[1]) + MULT16_32_Q15 (interp[2], - accum[2]) + MULT16_32_Q15 (interp[3], accum[3]); -#else - cubic_coef (frac, interp); - sum = - interpolate_product_single (iptr, - st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, - interp); -#endif - - out[out_stride * out_sample++] = PSHR32 (sum, 15); - last_sample += int_advance; - samp_frac_num += frac_advance; - if (samp_frac_num >= den_rate) { - samp_frac_num -= den_rate; - last_sample++; - } - } - - st->last_sample[channel_index] = last_sample; - st->samp_frac_num[channel_index] = samp_frac_num; - return out_sample; -} -#endif - -#ifdef FIXED_POINT -#else -/* This is the same as the previous function, except with a double-precision accumulator */ -static int -resampler_basic_interpolate_double (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, - spx_word16_t * out, spx_uint32_t * out_len) -{ - const int N = st->filt_len; - int out_sample = 0; - int last_sample = st->last_sample[channel_index]; - spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; - const int out_stride = st->out_stride; - const int int_advance = st->int_advance; - const int frac_advance = st->frac_advance; - const spx_uint32_t den_rate = st->den_rate; - int j; - spx_word32_t sum; - - while (!(last_sample >= (spx_int32_t) * in_len - || out_sample >= (spx_int32_t) * out_len)) { - const spx_word16_t *iptr = &in[last_sample]; - - const int offset = samp_frac_num * st->oversample / st->den_rate; -#ifdef FIXED_POINT - const spx_word16_t frac = - PDIV32 (SHL32 ((samp_frac_num * st->oversample) % st->den_rate, 15), - st->den_rate); -#else -#ifdef DOUBLE_PRECISION - const spx_word16_t frac = - ((double) ((samp_frac_num * st->oversample) % st->den_rate)) / - st->den_rate; -#else - const spx_word16_t frac = - ((float) ((samp_frac_num * st->oversample) % st->den_rate)) / - st->den_rate; -#endif -#endif - spx_word16_t interp[4]; - - -#ifndef OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE - double accum[4] = { 0, 0, 0, 0 }; - - for (j = 0; j < N; j++) { - const double curr_in = iptr[j]; - accum[0] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset - 2]); - accum[1] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset - 1]); - accum[2] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset]); - accum[3] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset + 1]); - } - - cubic_coef (frac, interp); - sum = - MULT16_32_Q15 (interp[0], accum[0]) + MULT16_32_Q15 (interp[1], - accum[1]) + MULT16_32_Q15 (interp[2], - accum[2]) + MULT16_32_Q15 (interp[3], accum[3]); -#else - cubic_coef (frac, interp); - sum = - interpolate_product_double (iptr, - st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, - interp); -#endif - - out[out_stride * out_sample++] = PSHR32 (sum, 15); - last_sample += int_advance; - samp_frac_num += frac_advance; - if (samp_frac_num >= den_rate) { - samp_frac_num -= den_rate; - last_sample++; - } - } - - st->last_sample[channel_index] = last_sample; - st->samp_frac_num[channel_index] = samp_frac_num; - return out_sample; -} -#endif - -static void -update_filter (SpeexResamplerState * st) -{ - spx_uint32_t old_length; - - old_length = st->filt_len; - st->oversample = quality_map[st->quality].oversample; - st->filt_len = quality_map[st->quality].base_length; - - if (st->num_rate > st->den_rate) { - /* down-sampling */ - st->cutoff = - quality_map[st->quality].downsample_bandwidth * st->den_rate / - st->num_rate; - /* FIXME: divide the numerator and denominator by a certain amount if they're too large */ - st->filt_len = st->filt_len * st->num_rate / st->den_rate; - /* Round down to make sure we have a multiple of 4 */ - st->filt_len &= (~0x3); - if (2 * st->den_rate < st->num_rate) - st->oversample >>= 1; - if (4 * st->den_rate < st->num_rate) - st->oversample >>= 1; - if (8 * st->den_rate < st->num_rate) - st->oversample >>= 1; - if (16 * st->den_rate < st->num_rate) - st->oversample >>= 1; - if (st->oversample < 1) - st->oversample = 1; - } else { - /* up-sampling */ - st->cutoff = quality_map[st->quality].upsample_bandwidth; - } - - /* Choose the resampling type that requires the least amount of memory */ - if (st->den_rate <= st->oversample) { - spx_uint32_t i; - if (!st->sinc_table) - st->sinc_table = - (spx_word16_t *) speex_alloc (st->filt_len * st->den_rate * - sizeof (spx_word16_t)); - else if (st->sinc_table_length < st->filt_len * st->den_rate) { - st->sinc_table = - (spx_word16_t *) speex_realloc (st->sinc_table, - st->filt_len * st->den_rate * sizeof (spx_word16_t)); - st->sinc_table_length = st->filt_len * st->den_rate; - } - for (i = 0; i < st->den_rate; i++) { - spx_int32_t j; - for (j = 0; j < st->filt_len; j++) { - st->sinc_table[i * st->filt_len + j] = - sinc (st->cutoff, ((j - (spx_int32_t) st->filt_len / 2 + 1) - -#ifdef DOUBLE_PRECISION - ((double) i) / st->den_rate), st->filt_len, -#else - ((float) i) / st->den_rate), st->filt_len, -#endif - quality_map[st->quality].window_func); - } - } -#ifdef FIXED_POINT - st->resampler_ptr = resampler_basic_direct_single; -#else -#ifdef DOUBLE_PRECISION - st->resampler_ptr = resampler_basic_direct_double; -#else - if (st->quality > 8) - st->resampler_ptr = resampler_basic_direct_double; - else - st->resampler_ptr = resampler_basic_direct_single; -#endif -#endif - /*fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff); */ - } else { - spx_int32_t i; - if (!st->sinc_table) - st->sinc_table = - (spx_word16_t *) speex_alloc ((st->filt_len * st->oversample + - 8) * sizeof (spx_word16_t)); - else if (st->sinc_table_length < st->filt_len * st->oversample + 8) { - st->sinc_table = - (spx_word16_t *) speex_realloc (st->sinc_table, - (st->filt_len * st->oversample + 8) * sizeof (spx_word16_t)); - st->sinc_table_length = st->filt_len * st->oversample + 8; - } - for (i = -4; i < (spx_int32_t) (st->oversample * st->filt_len + 4); i++) - st->sinc_table[i + 4] = -#ifdef DOUBLE_PRECISION - sinc (st->cutoff, (i / (double) st->oversample - st->filt_len / 2), -#else - sinc (st->cutoff, (i / (float) st->oversample - st->filt_len / 2), -#endif - st->filt_len, quality_map[st->quality].window_func); -#ifdef FIXED_POINT - st->resampler_ptr = resampler_basic_interpolate_single; -#else -#ifdef DOUBLE_PRECISION - st->resampler_ptr = resampler_basic_interpolate_double; -#else - if (st->quality > 8) - st->resampler_ptr = resampler_basic_interpolate_double; - else - st->resampler_ptr = resampler_basic_interpolate_single; -#endif -#endif - /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff); */ - } - st->int_advance = st->num_rate / st->den_rate; - st->frac_advance = st->num_rate % st->den_rate; - - - /* Here's the place where we update the filter memory to take into account - the change in filter length. It's probably the messiest part of the code - due to handling of lots of corner cases. */ - if (!st->mem) { - spx_uint32_t i; - st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; - st->mem = - (spx_word16_t *) speex_alloc (st->nb_channels * st->mem_alloc_size * - sizeof (spx_word16_t)); - for (i = 0; i < st->nb_channels * st->mem_alloc_size; i++) - st->mem[i] = 0; - /*speex_warning("init filter"); */ - } else if (!st->started) { - spx_uint32_t i; - st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; - st->mem = - (spx_word16_t *) speex_realloc (st->mem, - st->nb_channels * st->mem_alloc_size * sizeof (spx_word16_t)); - for (i = 0; i < st->nb_channels * st->mem_alloc_size; i++) - st->mem[i] = 0; - /*speex_warning("reinit filter"); */ - } else if (st->filt_len > old_length) { - spx_int32_t i; - /* Increase the filter length */ - /*speex_warning("increase filter size"); */ - int old_alloc_size = st->mem_alloc_size; - if ((st->filt_len - 1 + st->buffer_size) > st->mem_alloc_size) { - st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; - st->mem = - (spx_word16_t *) speex_realloc (st->mem, - st->nb_channels * st->mem_alloc_size * sizeof (spx_word16_t)); - } - for (i = st->nb_channels - 1; i >= 0; i--) { - spx_int32_t j; - spx_uint32_t olen = old_length; - /*if (st->magic_samples[i]) */ - { - /* Try and remove the magic samples as if nothing had happened */ - - /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */ - olen = old_length + 2 * st->magic_samples[i]; - for (j = old_length - 2 + st->magic_samples[i]; j >= 0; j--) - st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]] = - st->mem[i * old_alloc_size + j]; - for (j = 0; j < st->magic_samples[i]; j++) - st->mem[i * st->mem_alloc_size + j] = 0; - st->magic_samples[i] = 0; - } - if (st->filt_len > olen) { - /* If the new filter length is still bigger than the "augmented" length */ - /* Copy data going backward */ - for (j = 0; j < olen - 1; j++) - st->mem[i * st->mem_alloc_size + (st->filt_len - 2 - j)] = - st->mem[i * st->mem_alloc_size + (olen - 2 - j)]; - /* Then put zeros for lack of anything better */ - for (; j < st->filt_len - 1; j++) - st->mem[i * st->mem_alloc_size + (st->filt_len - 2 - j)] = 0; - /* Adjust last_sample */ - st->last_sample[i] += (st->filt_len - olen) / 2; - } else { - /* Put back some of the magic! */ - st->magic_samples[i] = (olen - st->filt_len) / 2; - for (j = 0; j < st->filt_len - 1 + st->magic_samples[i]; j++) - st->mem[i * st->mem_alloc_size + j] = - st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]]; - } - } - } else if (st->filt_len < old_length) { - spx_uint32_t i; - /* Reduce filter length, this a bit tricky. We need to store some of the memory as "magic" - samples so they can be used directly as input the next time(s) */ - for (i = 0; i < st->nb_channels; i++) { - spx_uint32_t j; - spx_uint32_t old_magic = st->magic_samples[i]; - st->magic_samples[i] = (old_length - st->filt_len) / 2; - /* We must copy some of the memory that's no longer used */ - /* Copy data going backward */ - for (j = 0; j < st->filt_len - 1 + st->magic_samples[i] + old_magic; j++) - st->mem[i * st->mem_alloc_size + j] = - st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]]; - st->magic_samples[i] += old_magic; - } - } - -} - -EXPORT SpeexResamplerState * -speex_resampler_init (spx_uint32_t nb_channels, spx_uint32_t in_rate, - spx_uint32_t out_rate, int quality, int *err) -{ - return speex_resampler_init_frac (nb_channels, in_rate, out_rate, in_rate, - out_rate, quality, err); -} - -EXPORT SpeexResamplerState * -speex_resampler_init_frac (spx_uint32_t nb_channels, spx_uint32_t ratio_num, - spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate, - int quality, int *err) -{ - spx_uint32_t i; - SpeexResamplerState *st; - if (quality > 10 || quality < 0) { - if (err) - *err = RESAMPLER_ERR_INVALID_ARG; - return NULL; - } - st = (SpeexResamplerState *) speex_alloc (sizeof (SpeexResamplerState)); - st->initialised = 0; - st->started = 0; - st->in_rate = 0; - st->out_rate = 0; - st->num_rate = 0; - st->den_rate = 0; - st->quality = -1; - st->sinc_table_length = 0; - st->mem_alloc_size = 0; - st->filt_len = 0; - st->mem = 0; - st->resampler_ptr = 0; - - st->cutoff = 1.f; - st->nb_channels = nb_channels; - st->in_stride = 1; - st->out_stride = 1; - -#ifdef FIXED_POINT - st->buffer_size = 160; -#else - st->buffer_size = 160; -#endif - - /* Per channel data */ - st->last_sample = (spx_int32_t *) speex_alloc (nb_channels * sizeof (int)); - st->magic_samples = (spx_uint32_t *) speex_alloc (nb_channels * sizeof (int)); - st->samp_frac_num = (spx_uint32_t *) speex_alloc (nb_channels * sizeof (int)); - for (i = 0; i < nb_channels; i++) { - st->last_sample[i] = 0; - st->magic_samples[i] = 0; - st->samp_frac_num[i] = 0; - } - - speex_resampler_set_quality (st, quality); - speex_resampler_set_rate_frac (st, ratio_num, ratio_den, in_rate, out_rate); - - - update_filter (st); - - st->initialised = 1; - if (err) - *err = RESAMPLER_ERR_SUCCESS; - - return st; -} - -EXPORT void -speex_resampler_destroy (SpeexResamplerState * st) -{ - speex_free (st->mem); - speex_free (st->sinc_table); - speex_free (st->last_sample); - speex_free (st->magic_samples); - speex_free (st->samp_frac_num); - speex_free (st); -} - -static int -speex_resampler_process_native (SpeexResamplerState * st, - spx_uint32_t channel_index, spx_uint32_t * in_len, spx_word16_t * out, - spx_uint32_t * out_len) -{ - int j = 0; - const int N = st->filt_len; - int out_sample = 0; - spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; - spx_uint32_t ilen; - - st->started = 1; - - /* Call the right resampler through the function ptr */ - out_sample = st->resampler_ptr (st, channel_index, mem, in_len, out, out_len); - - if (st->last_sample[channel_index] < (spx_int32_t) * in_len) - *in_len = st->last_sample[channel_index]; - *out_len = out_sample; - st->last_sample[channel_index] -= *in_len; - - ilen = *in_len; - - for (j = 0; j < N - 1; ++j) - mem[j] = mem[j + ilen]; - - return RESAMPLER_ERR_SUCCESS; -} - -static int -speex_resampler_magic (SpeexResamplerState * st, spx_uint32_t channel_index, - spx_word16_t ** out, spx_uint32_t out_len) -{ - spx_uint32_t tmp_in_len = st->magic_samples[channel_index]; - spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; - const int N = st->filt_len; - - speex_resampler_process_native (st, channel_index, &tmp_in_len, *out, - &out_len); - - st->magic_samples[channel_index] -= tmp_in_len; - - /* If we couldn't process all "magic" input samples, save the rest for next time */ - if (st->magic_samples[channel_index]) { - spx_uint32_t i; - for (i = 0; i < st->magic_samples[channel_index]; i++) - mem[N - 1 + i] = mem[N - 1 + i + tmp_in_len]; - } - *out += out_len * st->out_stride; - return out_len; -} - -#ifdef FIXED_POINT -EXPORT int -speex_resampler_process_int (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_int16_t * in, spx_uint32_t * in_len, - spx_int16_t * out, spx_uint32_t * out_len) -#else -#ifdef DOUBLE_PRECISION -EXPORT int -speex_resampler_process_float (SpeexResamplerState * st, - spx_uint32_t channel_index, const double *in, spx_uint32_t * in_len, - double *out, spx_uint32_t * out_len) -#else -EXPORT int -speex_resampler_process_float (SpeexResamplerState * st, - spx_uint32_t channel_index, const float *in, spx_uint32_t * in_len, - float *out, spx_uint32_t * out_len) -#endif -#endif -{ - int j; - spx_uint32_t ilen = *in_len; - spx_uint32_t olen = *out_len; - spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size; - const int filt_offs = st->filt_len - 1; - const spx_uint32_t xlen = st->mem_alloc_size - filt_offs; - const int istride = st->in_stride; - - if (st->magic_samples[channel_index]) - olen -= speex_resampler_magic (st, channel_index, &out, olen); - if (!st->magic_samples[channel_index]) { - while (ilen && olen) { - spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen; - spx_uint32_t ochunk = olen; - - if (in) { - for (j = 0; j < ichunk; ++j) - x[j + filt_offs] = in[j * istride]; - } else { - for (j = 0; j < ichunk; ++j) - x[j + filt_offs] = 0; - } - speex_resampler_process_native (st, channel_index, &ichunk, out, &ochunk); - ilen -= ichunk; - olen -= ochunk; - out += ochunk * st->out_stride; - if (in) - in += ichunk * istride; - } - } - *in_len -= ilen; - *out_len -= olen; - return RESAMPLER_ERR_SUCCESS; -} - -#ifdef FIXED_POINT -EXPORT int -speex_resampler_process_float (SpeexResamplerState * st, - spx_uint32_t channel_index, const float *in, spx_uint32_t * in_len, - float *out, spx_uint32_t * out_len) -#else -EXPORT int -speex_resampler_process_int (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_int16_t * in, spx_uint32_t * in_len, - spx_int16_t * out, spx_uint32_t * out_len) -#endif -{ - int j; - const int istride_save = st->in_stride; - const int ostride_save = st->out_stride; - spx_uint32_t ilen = *in_len; - spx_uint32_t olen = *out_len; - spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size; - const spx_uint32_t xlen = st->mem_alloc_size - (st->filt_len - 1); -#ifdef VAR_ARRAYS - const unsigned int ylen = - (olen < FIXED_STACK_ALLOC) ? olen : FIXED_STACK_ALLOC; - VARDECL (spx_word16_t * ystack); - ALLOC (ystack, ylen, spx_word16_t); -#else - const unsigned int ylen = FIXED_STACK_ALLOC; - spx_word16_t ystack[FIXED_STACK_ALLOC]; -#endif - - st->out_stride = 1; - - while (ilen && olen) { - spx_word16_t *y = ystack; - spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen; - spx_uint32_t ochunk = (olen > ylen) ? ylen : olen; - spx_uint32_t omagic = 0; - - if (st->magic_samples[channel_index]) { - omagic = speex_resampler_magic (st, channel_index, &y, ochunk); - ochunk -= omagic; - olen -= omagic; - } - if (!st->magic_samples[channel_index]) { - if (in) { - for (j = 0; j < ichunk; ++j) -#ifdef FIXED_POINT - x[j + st->filt_len - 1] = WORD2INT (in[j * istride_save]); -#else - x[j + st->filt_len - 1] = in[j * istride_save]; -#endif - } else { - for (j = 0; j < ichunk; ++j) - x[j + st->filt_len - 1] = 0; - } - - speex_resampler_process_native (st, channel_index, &ichunk, y, &ochunk); - } else { - ichunk = 0; - ochunk = 0; - } - - for (j = 0; j < ochunk + omagic; ++j) -#ifdef FIXED_POINT - out[j * ostride_save] = ystack[j]; -#else - out[j * ostride_save] = WORD2INT (ystack[j]); -#endif - - ilen -= ichunk; - olen -= ochunk; - out += (ochunk + omagic) * ostride_save; - if (in) - in += ichunk * istride_save; - } - st->out_stride = ostride_save; - *in_len -= ilen; - *out_len -= olen; - - return RESAMPLER_ERR_SUCCESS; -} - -#ifdef DOUBLE_PRECISION -EXPORT int -speex_resampler_process_interleaved_float (SpeexResamplerState * st, - const double *in, spx_uint32_t * in_len, double *out, - spx_uint32_t * out_len) -#else -EXPORT int -speex_resampler_process_interleaved_float (SpeexResamplerState * st, - const float *in, spx_uint32_t * in_len, float *out, spx_uint32_t * out_len) -#endif -{ - spx_uint32_t i; - int istride_save, ostride_save; - spx_uint32_t bak_len = *out_len; - istride_save = st->in_stride; - ostride_save = st->out_stride; - st->in_stride = st->out_stride = st->nb_channels; - for (i = 0; i < st->nb_channels; i++) { - *out_len = bak_len; - if (in != NULL) - speex_resampler_process_float (st, i, in + i, in_len, out + i, out_len); - else - speex_resampler_process_float (st, i, NULL, in_len, out + i, out_len); - } - st->in_stride = istride_save; - st->out_stride = ostride_save; - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT int -speex_resampler_process_interleaved_int (SpeexResamplerState * st, - const spx_int16_t * in, spx_uint32_t * in_len, spx_int16_t * out, - spx_uint32_t * out_len) -{ - spx_uint32_t i; - int istride_save, ostride_save; - spx_uint32_t bak_len = *out_len; - istride_save = st->in_stride; - ostride_save = st->out_stride; - st->in_stride = st->out_stride = st->nb_channels; - for (i = 0; i < st->nb_channels; i++) { - *out_len = bak_len; - if (in != NULL) - speex_resampler_process_int (st, i, in + i, in_len, out + i, out_len); - else - speex_resampler_process_int (st, i, NULL, in_len, out + i, out_len); - } - st->in_stride = istride_save; - st->out_stride = ostride_save; - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT int -speex_resampler_set_rate (SpeexResamplerState * st, spx_uint32_t in_rate, - spx_uint32_t out_rate) -{ - return speex_resampler_set_rate_frac (st, in_rate, out_rate, in_rate, - out_rate); -} - -EXPORT void -speex_resampler_get_rate (SpeexResamplerState * st, spx_uint32_t * in_rate, - spx_uint32_t * out_rate) -{ - *in_rate = st->in_rate; - *out_rate = st->out_rate; -} - -EXPORT int -speex_resampler_set_rate_frac (SpeexResamplerState * st, spx_uint32_t ratio_num, - spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate) -{ - spx_uint32_t fact; - spx_uint32_t old_den; - spx_uint32_t i; - if (st->in_rate == in_rate && st->out_rate == out_rate - && st->num_rate == ratio_num && st->den_rate == ratio_den) - return RESAMPLER_ERR_SUCCESS; - - old_den = st->den_rate; - st->in_rate = in_rate; - st->out_rate = out_rate; - st->num_rate = ratio_num; - st->den_rate = ratio_den; - /* FIXME: This is terribly inefficient, but who cares (at least for now)? */ - for (fact = 2; fact <= IMIN (st->num_rate, st->den_rate); fact++) { - while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0)) { - st->num_rate /= fact; - st->den_rate /= fact; - } - } - - if (old_den > 0) { - for (i = 0; i < st->nb_channels; i++) { - st->samp_frac_num[i] = st->samp_frac_num[i] * st->den_rate / old_den; - /* Safety net */ - if (st->samp_frac_num[i] >= st->den_rate) - st->samp_frac_num[i] = st->den_rate - 1; - } - } - - if (st->initialised) - update_filter (st); - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT void -speex_resampler_get_ratio (SpeexResamplerState * st, spx_uint32_t * ratio_num, - spx_uint32_t * ratio_den) -{ - *ratio_num = st->num_rate; - *ratio_den = st->den_rate; -} - -EXPORT int -speex_resampler_set_quality (SpeexResamplerState * st, int quality) -{ - if (quality > 10 || quality < 0) - return RESAMPLER_ERR_INVALID_ARG; - if (st->quality == quality) - return RESAMPLER_ERR_SUCCESS; - st->quality = quality; - if (st->initialised) - update_filter (st); - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT void -speex_resampler_get_quality (SpeexResamplerState * st, int *quality) -{ - *quality = st->quality; -} - -EXPORT void -speex_resampler_set_input_stride (SpeexResamplerState * st, spx_uint32_t stride) -{ - st->in_stride = stride; -} - -EXPORT void -speex_resampler_get_input_stride (SpeexResamplerState * st, - spx_uint32_t * stride) -{ - *stride = st->in_stride; -} - -EXPORT void -speex_resampler_set_output_stride (SpeexResamplerState * st, - spx_uint32_t stride) -{ - st->out_stride = stride; -} - -EXPORT void -speex_resampler_get_output_stride (SpeexResamplerState * st, - spx_uint32_t * stride) -{ - *stride = st->out_stride; -} - -EXPORT int -speex_resampler_get_input_latency (SpeexResamplerState * st) -{ - return st->filt_len / 2; -} - -EXPORT int -speex_resampler_get_output_latency (SpeexResamplerState * st) -{ - return ((st->filt_len / 2) * st->den_rate + - (st->num_rate >> 1)) / st->num_rate; -} - -EXPORT int -speex_resampler_skip_zeros (SpeexResamplerState * st) -{ - spx_uint32_t i; - for (i = 0; i < st->nb_channels; i++) - st->last_sample[i] = st->filt_len / 2; - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT int -speex_resampler_reset_mem (SpeexResamplerState * st) -{ - spx_uint32_t i; - for (i = 0; i < st->nb_channels * (st->filt_len - 1); i++) - st->mem[i] = 0; - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT const char * -speex_resampler_strerror (int err) -{ - switch (err) { - case RESAMPLER_ERR_SUCCESS: - return "Success."; - case RESAMPLER_ERR_ALLOC_FAILED: - return "Memory allocation failed."; - case RESAMPLER_ERR_BAD_STATE: - return "Bad resampler state."; - case RESAMPLER_ERR_INVALID_ARG: - return "Invalid argument."; - case RESAMPLER_ERR_PTR_OVERLAP: - return "Input and output buffers overlap."; - default: - return "Unknown error. Bad error code or strange version mismatch."; - } -} diff --git a/gst/typefind/gsttypefindfunctions.c b/gst/typefind/gsttypefindfunctions.c index 235b11b..d352f6b 100644 --- a/gst/typefind/gsttypefindfunctions.c +++ b/gst/typefind/gsttypefindfunctions.c @@ -412,6 +412,7 @@ mid_type_find (GstTypeFind * tf, gpointer unused) && data[3] == 'd') gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MID_CAPS); } + /*** audio/mobile-xmf ***/ static GstStaticCaps mxmf_caps = GST_STATIC_CAPS ("audio/mobile-xmf"); @@ -422,21 +423,21 @@ mxmf_type_find (GstTypeFind * tf, gpointer unused) { guint8 *data = NULL; - /* Search FileId "XMF_" 4 bytes */ + /* Search FileId "XMF_" 4 bytes */ data = gst_type_find_peek (tf, 0, 4); if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F' - && data[3] == '_') { - /* Search Format version "2.00" 4 bytes */ - data = gst_type_find_peek (tf, 4, 4); - if (data && data[0] == '2' && data[1] == '.' && data[2] == '0' - && data[3] == '0') { - /* Search TypeId 2 1 byte */ - data = gst_type_find_peek (tf, 11, 1); - if (data && data[0] == 2 ) { - gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXMF_CAPS); - } - } - } + && data[3] == '_') { + /* Search Format version "2.00" 4 bytes */ + data = gst_type_find_peek (tf, 4, 4); + if (data && data[0] == '2' && data[1] == '.' && data[2] == '0' + && data[3] == '0') { + /* Search TypeId 2 1 byte */ + data = gst_type_find_peek (tf, 11, 1); + if (data && data[0] == 2) { + gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXMF_CAPS); + } + } + } } @@ -3208,8 +3209,8 @@ plugin_init (GstPlugin * plugin) #endif TYPE_FIND_REGISTER (plugin, "audio/midi", GST_RANK_PRIMARY, mid_type_find, mid_exts, MID_CAPS, NULL, NULL); - TYPE_FIND_REGISTER (plugin, "audio/mobile-xmf", GST_RANK_PRIMARY, mxmf_type_find, - mxmf_exts, MXMF_CAPS, NULL, NULL); + TYPE_FIND_REGISTER (plugin, "audio/mobile-xmf", GST_RANK_PRIMARY, + mxmf_type_find, mxmf_exts, MXMF_CAPS, NULL, NULL); TYPE_FIND_REGISTER (plugin, "video/x-fli", GST_RANK_MARGINAL, flx_type_find, flx_exts, FLX_CAPS, NULL, NULL); TYPE_FIND_REGISTER (plugin, "application/x-id3v2", GST_RANK_PRIMARY + 103, @@ -3241,8 +3242,8 @@ plugin_init (GstPlugin * plugin) mpeg4_video_type_find, m4v_exts, MPEG_VIDEO_CAPS, NULL, NULL); TYPE_FIND_REGISTER (plugin, "video/x-h264", GST_RANK_PRIMARY, h264_video_type_find, h264_exts, MPEG_VIDEO_CAPS, NULL, NULL); - TYPE_FIND_REGISTER (plugin, "video/x-nuv", GST_RANK_SECONDARY, - nuv_type_find, nuv_exts, NUV_CAPS, NULL, NULL); + TYPE_FIND_REGISTER (plugin, "video/x-nuv", GST_RANK_SECONDARY, nuv_type_find, + nuv_exts, NUV_CAPS, NULL, NULL); /* ISO formats */ TYPE_FIND_REGISTER (plugin, "audio/x-m4a", GST_RANK_PRIMARY, m4a_type_find, diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 1ba9b51..c83679e 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -91,6 +91,7 @@ check_PROGRAMS = \ elements/adder \ elements/audioconvert \ elements/audiorate \ + elements/audioresample \ elements/audiotestsrc \ elements/decodebin \ elements/ffmpegcolorspace \ @@ -99,7 +100,6 @@ check_PROGRAMS = \ elements/multifdsink \ elements/playbin \ $(check_subparse) \ - elements/speexresample \ elements/videorate \ elements/videotestsrc \ elements/volume \ @@ -241,12 +241,12 @@ elements_decodebin_CFLAGS = $(GST_BASE_CFLAGS) $(AM_CFLAGS) elements_subparse_LDADD = $(LDADD) elements_subparse_CFLAGS = $(CFLAGS) $(AM_CFLAGS) -elements_speexresample_CFLAGS = \ +elements_audioresample_CFLAGS = \ $(GST_PLUGINS_BASE_CFLAGS) \ $(GST_BASE_CFLAGS) \ $(AM_CFLAGS) -elements_speexresample_LDADD = \ +elements_audioresample_LDADD = \ $(top_builddir)/gst-libs/gst/audio/libgstaudio-@GST_MAJORMINOR@.la \ $(top_builddir)/gst-libs/gst/interfaces/libgstinterfaces-@GST_MAJORMINOR@.la \ $(GST_BASE_LIBS) \ diff --git a/tests/check/elements/audioresample.c b/tests/check/elements/audioresample.c index 199520e..7b8408d 100644 --- a/tests/check/elements/audioresample.c +++ b/tests/check/elements/audioresample.c @@ -1,6 +1,6 @@ /* GStreamer * - * unit test for audioresample + * unit test for audioresample, based on the audioresample unit test * * Copyright (C) <2005> Thomas Vander Stichele * Copyright (C) <2006> Tim-Philipp Müller @@ -25,13 +25,21 @@ #include +#include + /* For ease of programming we use globals to keep refs for our floating * src and sink pads we create; otherwise we always have to do get_pad, * get_peer, and then remove references in every test function */ static GstPad *mysrcpad, *mysinkpad; +#define RESAMPLE_CAPS_FLOAT \ + "audio/x-raw-float, " \ + "channels = (int) [ 1, MAX ], " \ + "rate = (int) [ 1, MAX ], " \ + "endianness = (int) BYTE_ORDER, " \ + "width = (int) { 32, 64 }" -#define RESAMPLE_CAPS_TEMPLATE_STRING \ +#define RESAMPLE_CAPS_INT \ "audio/x-raw-int, " \ "channels = (int) [ 1, MAX ], " \ "rate = (int) [ 1, MAX ], " \ @@ -40,6 +48,10 @@ static GstPad *mysrcpad, *mysinkpad; "depth = (int) 16, " \ "signed = (bool) TRUE" +#define RESAMPLE_CAPS_TEMPLATE_STRING \ + RESAMPLE_CAPS_FLOAT " ; " \ + RESAMPLE_CAPS_INT + static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, @@ -52,7 +64,8 @@ static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", ); static GstElement * -setup_audioresample (int channels, int inrate, int outrate) +setup_audioresample (int channels, int inrate, int outrate, int width, + gboolean fp) { GstElement *audioresample; GstCaps *caps; @@ -61,10 +74,15 @@ setup_audioresample (int channels, int inrate, int outrate) GST_DEBUG ("setup_audioresample"); audioresample = gst_check_setup_element ("audioresample"); - caps = gst_caps_from_string (RESAMPLE_CAPS_TEMPLATE_STRING); + if (fp) + caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT); + else + caps = gst_caps_from_string (RESAMPLE_CAPS_INT); structure = gst_caps_get_structure (caps, 0); gst_structure_set (structure, "channels", G_TYPE_INT, channels, - "rate", G_TYPE_INT, inrate, NULL); + "rate", G_TYPE_INT, inrate, "width", G_TYPE_INT, width, NULL); + if (!fp) + gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL); fail_unless (gst_caps_is_fixed (caps)); fail_unless (gst_element_set_state (audioresample, @@ -75,10 +93,15 @@ setup_audioresample (int channels, int inrate, int outrate) gst_pad_set_caps (mysrcpad, caps); gst_caps_unref (caps); - caps = gst_caps_from_string (RESAMPLE_CAPS_TEMPLATE_STRING); + if (fp) + caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT); + else + caps = gst_caps_from_string (RESAMPLE_CAPS_INT); structure = gst_caps_get_structure (caps, 0); gst_structure_set (structure, "channels", G_TYPE_INT, channels, - "rate", G_TYPE_INT, outrate, NULL); + "rate", G_TYPE_INT, outrate, "width", G_TYPE_INT, width, NULL); + if (!fp) + gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL); fail_unless (gst_caps_is_fixed (caps)); mysinkpad = gst_check_setup_sink_pad (audioresample, &sinktemplate, caps); @@ -90,6 +113,8 @@ setup_audioresample (int channels, int inrate, int outrate) gst_pad_set_active (mysinkpad, TRUE); gst_pad_set_active (mysrcpad, TRUE); + gst_caps_unref (caps); + return audioresample; } @@ -153,7 +178,7 @@ test_perfect_stream_instance (int inrate, int outrate, int samples, int i, j; gint16 *p; - audioresample = setup_audioresample (2, inrate, outrate); + audioresample = setup_audioresample (2, inrate, outrate, 16, FALSE); caps = gst_pad_get_negotiated_caps (mysrcpad); fail_unless (gst_caps_is_fixed (caps)); @@ -164,7 +189,7 @@ test_perfect_stream_instance (int inrate, int outrate, int samples, for (j = 1; j <= numbuffers; ++j) { inbuffer = gst_buffer_new_and_alloc (samples * 4); - GST_BUFFER_DURATION (inbuffer) = samples * GST_SECOND / inrate; + GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (samples, inrate); GST_BUFFER_TIMESTAMP (inbuffer) = GST_BUFFER_DURATION (inbuffer) * (j - 1); GST_BUFFER_OFFSET (inbuffer) = offset; offset += samples; @@ -240,7 +265,7 @@ test_discont_stream_instance (int inrate, int outrate, int samples, GST_DEBUG ("inrate:%d outrate:%d samples:%d numbuffers:%d", inrate, outrate, samples, numbuffers); - audioresample = setup_audioresample (2, inrate, outrate); + audioresample = setup_audioresample (2, inrate, outrate, 16, FALSE); caps = gst_pad_get_negotiated_caps (mysrcpad); fail_unless (gst_caps_is_fixed (caps)); @@ -326,7 +351,7 @@ GST_START_TEST (test_reuse) GstBuffer *inbuffer; GstCaps *caps; - audioresample = setup_audioresample (1, 9343, 48000); + audioresample = setup_audioresample (1, 9343, 48000, 16, FALSE); caps = gst_pad_get_negotiated_caps (mysrcpad); fail_unless (gst_caps_is_fixed (caps)); @@ -477,6 +502,7 @@ live_switch_push (int rate, GstCaps * caps) desired = gst_caps_copy (caps); gst_caps_set_simple (desired, "rate", G_TYPE_INT, rate, NULL); + gst_pad_set_caps (mysrcpad, desired); fail_unless (gst_pad_alloc_buffer_and_set_caps (mysrcpad, GST_BUFFER_OFFSET_NONE, rate * 4, desired, &inbuffer) == GST_FLOW_OK); @@ -485,6 +511,8 @@ live_switch_push (int rate, GstCaps * caps) * returns a buffer with exactly the same caps as we requested so the actual * renegotiation (if needed) will be done in the _chain*/ fail_unless (inbuffer != NULL); + GST_DEBUG ("desired: %" GST_PTR_FORMAT ".... got: %" GST_PTR_FORMAT, + desired, GST_BUFFER_CAPS (inbuffer)); fail_unless (gst_caps_is_equal (desired, GST_BUFFER_CAPS (inbuffer))); memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer)); @@ -516,7 +544,7 @@ GST_START_TEST (test_live_switch) GstEvent *newseg; GstCaps *caps; - audioresample = setup_audioresample (4, 48000, 48000); + audioresample = setup_audioresample (4, 48000, 48000, 16, FALSE); /* Let the sinkpad act like something that can only handle things of * rate 48000- and can only allocate buffers for that rate, but if someone @@ -525,6 +553,8 @@ GST_START_TEST (test_live_switch) gst_pad_set_bufferalloc_function (mysinkpad, live_switch_alloc_only_48000); gst_pad_set_getcaps_function (mysinkpad, live_switch_get_sink_caps); + gst_pad_use_fixed_caps (mysrcpad); + caps = gst_pad_get_negotiated_caps (mysrcpad); fail_unless (gst_caps_is_fixed (caps)); @@ -549,7 +579,104 @@ GST_START_TEST (test_live_switch) gst_caps_unref (caps); } -GST_END_TEST static Suite * +GST_END_TEST; + +#ifndef GST_DISABLE_PARSE + +static GMainLoop *loop; +static gint messages = 0; + +static void +element_message_cb (GstBus * bus, GstMessage * message, gpointer user_data) +{ + gchar *s; + + s = gst_structure_to_string (gst_message_get_structure (message)); + GST_DEBUG ("Received message: %s", s); + g_free (s); + + messages++; +} + +static void +eos_message_cb (GstBus * bus, GstMessage * message, gpointer user_data) +{ + GST_DEBUG ("Received eos"); + g_main_loop_quit (loop); +} + +static void +test_pipeline (gint width, gboolean fp, gint inrate, gint outrate, gint quality) +{ + GstElement *pipeline; + GstBus *bus; + GError *error = NULL; + gchar *pipe_str; + + pipe_str = + g_strdup_printf + ("audiotestsrc num-buffers=10 ! audioconvert ! audio/x-raw-%s,rate=%d,width=%d,channels=2 ! audioresample quality=%d ! audio/x-raw-%s,rate=%d,width=%d ! identity check-imperfect-timestamp=TRUE ! fakesink", + (fp) ? "float" : "int", inrate, width, quality, (fp) ? "float" : "int", + outrate, width); + + pipeline = gst_parse_launch (pipe_str, &error); + fail_unless (pipeline != NULL, "Error parsing pipeline: %s", + error ? error->message : "(invalid error)"); + g_free (pipe_str); + + bus = gst_element_get_bus (pipeline); + fail_if (bus == NULL); + gst_bus_add_signal_watch (bus); + g_signal_connect (bus, "message::element", (GCallback) element_message_cb, + NULL); + g_signal_connect (bus, "message::eos", (GCallback) eos_message_cb, NULL); + + gst_element_set_state (pipeline, GST_STATE_PLAYING); + + /* run until we receive EOS */ + loop = g_main_loop_new (NULL, FALSE); + + g_main_loop_run (loop); + + g_main_loop_unref (loop); + loop = NULL; + + gst_element_set_state (pipeline, GST_STATE_NULL); + + fail_if (messages > 0, "Received imperfect timestamp messages"); + gst_object_unref (pipeline); +} + +GST_START_TEST (test_pipelines) +{ + gint quality; + + /* Test qualities 0, 5 and 10 */ + for (quality = 0; quality < 11; quality += 5) { + test_pipeline (8, FALSE, 44100, 48000, quality); + test_pipeline (8, FALSE, 48000, 44100, quality); + + test_pipeline (16, FALSE, 44100, 48000, quality); + test_pipeline (16, FALSE, 48000, 44100, quality); + + test_pipeline (24, FALSE, 44100, 48000, quality); + test_pipeline (24, FALSE, 48000, 44100, quality); + + test_pipeline (32, FALSE, 44100, 48000, quality); + test_pipeline (32, FALSE, 48000, 44100, quality); + + test_pipeline (32, TRUE, 44100, 48000, quality); + test_pipeline (32, TRUE, 48000, 44100, quality); + + test_pipeline (64, TRUE, 44100, 48000, quality); + test_pipeline (64, TRUE, 48000, 44100, quality); + } +} + +GST_END_TEST; +#endif + +static Suite * audioresample_suite (void) { Suite *s = suite_create ("audioresample"); @@ -562,6 +689,11 @@ audioresample_suite (void) tcase_add_test (tc_chain, test_shutdown); tcase_add_test (tc_chain, test_live_switch); +#ifndef GST_DISABLE_PARSE + tcase_set_timeout (tc_chain, 360); + tcase_add_test (tc_chain, test_pipelines); +#endif + return s; } diff --git a/tests/check/elements/speexresample.c b/tests/check/elements/speexresample.c deleted file mode 100644 index 565fc21..0000000 --- a/tests/check/elements/speexresample.c +++ /dev/null @@ -1,700 +0,0 @@ -/* GStreamer - * - * unit test for speexresample, based on the audioresample unit test - * - * Copyright (C) <2005> Thomas Vander Stichele - * Copyright (C) <2006> Tim-Philipp Müller - * - * 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 - -#include - -#include - -/* For ease of programming we use globals to keep refs for our floating - * src and sink pads we create; otherwise we always have to do get_pad, - * get_peer, and then remove references in every test function */ -static GstPad *mysrcpad, *mysinkpad; - -#define RESAMPLE_CAPS_FLOAT \ - "audio/x-raw-float, " \ - "channels = (int) [ 1, MAX ], " \ - "rate = (int) [ 1, MAX ], " \ - "endianness = (int) BYTE_ORDER, " \ - "width = (int) { 32, 64 }" - -#define RESAMPLE_CAPS_INT \ - "audio/x-raw-int, " \ - "channels = (int) [ 1, MAX ], " \ - "rate = (int) [ 1, MAX ], " \ - "endianness = (int) BYTE_ORDER, " \ - "width = (int) 16, " \ - "depth = (int) 16, " \ - "signed = (bool) TRUE" - -#define RESAMPLE_CAPS_TEMPLATE_STRING \ - RESAMPLE_CAPS_FLOAT " ; " \ - RESAMPLE_CAPS_INT - -static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", - GST_PAD_SINK, - GST_PAD_ALWAYS, - GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING) - ); -static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", - GST_PAD_SRC, - GST_PAD_ALWAYS, - GST_STATIC_CAPS (RESAMPLE_CAPS_TEMPLATE_STRING) - ); - -static GstElement * -setup_speexresample (int channels, int inrate, int outrate, int width, - gboolean fp) -{ - GstElement *speexresample; - GstCaps *caps; - GstStructure *structure; - - GST_DEBUG ("setup_speexresample"); - speexresample = gst_check_setup_element ("audioresample"); - - if (fp) - caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT); - else - caps = gst_caps_from_string (RESAMPLE_CAPS_INT); - structure = gst_caps_get_structure (caps, 0); - gst_structure_set (structure, "channels", G_TYPE_INT, channels, - "rate", G_TYPE_INT, inrate, "width", G_TYPE_INT, width, NULL); - if (!fp) - gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL); - fail_unless (gst_caps_is_fixed (caps)); - - fail_unless (gst_element_set_state (speexresample, - GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS, - "could not set to paused"); - - mysrcpad = gst_check_setup_src_pad (speexresample, &srctemplate, caps); - gst_pad_set_caps (mysrcpad, caps); - gst_caps_unref (caps); - - if (fp) - caps = gst_caps_from_string (RESAMPLE_CAPS_FLOAT); - else - caps = gst_caps_from_string (RESAMPLE_CAPS_INT); - structure = gst_caps_get_structure (caps, 0); - gst_structure_set (structure, "channels", G_TYPE_INT, channels, - "rate", G_TYPE_INT, outrate, "width", G_TYPE_INT, width, NULL); - if (!fp) - gst_structure_set (structure, "depth", G_TYPE_INT, width, NULL); - fail_unless (gst_caps_is_fixed (caps)); - - mysinkpad = gst_check_setup_sink_pad (speexresample, &sinktemplate, caps); - /* this installs a getcaps func that will always return the caps we set - * later */ - gst_pad_set_caps (mysinkpad, caps); - gst_pad_use_fixed_caps (mysinkpad); - - gst_pad_set_active (mysinkpad, TRUE); - gst_pad_set_active (mysrcpad, TRUE); - - gst_caps_unref (caps); - - return speexresample; -} - -static void -cleanup_speexresample (GstElement * speexresample) -{ - GST_DEBUG ("cleanup_speexresample"); - - fail_unless (gst_element_set_state (speexresample, - GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL"); - - gst_pad_set_active (mysrcpad, FALSE); - gst_pad_set_active (mysinkpad, FALSE); - gst_check_teardown_src_pad (speexresample); - gst_check_teardown_sink_pad (speexresample); - gst_check_teardown_element (speexresample); -} - -static void -fail_unless_perfect_stream (void) -{ - guint64 timestamp = 0L, duration = 0L; - guint64 offset = 0L, offset_end = 0L; - - GList *l; - GstBuffer *buffer; - - for (l = buffers; l; l = l->next) { - buffer = GST_BUFFER (l->data); - ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1); - GST_DEBUG ("buffer timestamp %" G_GUINT64_FORMAT ", duration %" - G_GUINT64_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %" - G_GUINT64_FORMAT, - GST_BUFFER_TIMESTAMP (buffer), - GST_BUFFER_DURATION (buffer), - GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET_END (buffer)); - - fail_unless_equals_uint64 (timestamp, GST_BUFFER_TIMESTAMP (buffer)); - fail_unless_equals_uint64 (offset, GST_BUFFER_OFFSET (buffer)); - duration = GST_BUFFER_DURATION (buffer); - offset_end = GST_BUFFER_OFFSET_END (buffer); - - timestamp += duration; - offset = offset_end; - gst_buffer_unref (buffer); - } - g_list_free (buffers); - buffers = NULL; -} - -/* this tests that the output is a perfect stream if the input is */ -static void -test_perfect_stream_instance (int inrate, int outrate, int samples, - int numbuffers) -{ - GstElement *speexresample; - GstBuffer *inbuffer, *outbuffer; - GstCaps *caps; - guint64 offset = 0; - - int i, j; - gint16 *p; - - speexresample = setup_speexresample (2, inrate, outrate, 16, FALSE); - caps = gst_pad_get_negotiated_caps (mysrcpad); - fail_unless (gst_caps_is_fixed (caps)); - - fail_unless (gst_element_set_state (speexresample, - GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, - "could not set to playing"); - - for (j = 1; j <= numbuffers; ++j) { - - inbuffer = gst_buffer_new_and_alloc (samples * 4); - GST_BUFFER_DURATION (inbuffer) = GST_FRAMES_TO_CLOCK_TIME (samples, inrate); - GST_BUFFER_TIMESTAMP (inbuffer) = GST_BUFFER_DURATION (inbuffer) * (j - 1); - GST_BUFFER_OFFSET (inbuffer) = offset; - offset += samples; - GST_BUFFER_OFFSET_END (inbuffer) = offset; - - gst_buffer_set_caps (inbuffer, caps); - - p = (gint16 *) GST_BUFFER_DATA (inbuffer); - - /* create a 16 bit signed ramp */ - for (i = 0; i < samples; ++i) { - *p = -32767 + i * (65535 / samples); - ++p; - *p = -32767 + i * (65535 / samples); - ++p; - } - - /* pushing gives away my reference ... */ - fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); - /* ... but it ends up being collected on the global buffer list */ - fail_unless_equals_int (g_list_length (buffers), j); - } - - /* FIXME: we should make speexresample handle eos by flushing out the last - * samples, which will give us one more, small, buffer */ - fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); - ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1); - - fail_unless_perfect_stream (); - - /* cleanup */ - gst_caps_unref (caps); - cleanup_speexresample (speexresample); -} - - -/* make sure that outgoing buffers are contiguous in timestamp/duration and - * offset/offsetend - */ -GST_START_TEST (test_perfect_stream) -{ - /* integral scalings */ - test_perfect_stream_instance (48000, 24000, 500, 20); - test_perfect_stream_instance (48000, 12000, 500, 20); - test_perfect_stream_instance (12000, 24000, 500, 20); - test_perfect_stream_instance (12000, 48000, 500, 20); - - /* non-integral scalings */ - test_perfect_stream_instance (44100, 8000, 500, 20); - test_perfect_stream_instance (8000, 44100, 500, 20); - - /* wacky scalings */ - test_perfect_stream_instance (12345, 54321, 500, 20); - test_perfect_stream_instance (101, 99, 500, 20); -} - -GST_END_TEST; - -/* this tests that the output is a correct discontinuous stream - * if the input is; ie input drops in time come out the same way */ -static void -test_discont_stream_instance (int inrate, int outrate, int samples, - int numbuffers) -{ - GstElement *speexresample; - GstBuffer *inbuffer, *outbuffer; - GstCaps *caps; - GstClockTime ints; - - int i, j; - gint16 *p; - - GST_DEBUG ("inrate:%d outrate:%d samples:%d numbuffers:%d", - inrate, outrate, samples, numbuffers); - - speexresample = setup_speexresample (2, inrate, outrate, 16, FALSE); - caps = gst_pad_get_negotiated_caps (mysrcpad); - fail_unless (gst_caps_is_fixed (caps)); - - fail_unless (gst_element_set_state (speexresample, - GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, - "could not set to playing"); - - for (j = 1; j <= numbuffers; ++j) { - - inbuffer = gst_buffer_new_and_alloc (samples * 4); - GST_BUFFER_DURATION (inbuffer) = samples * GST_SECOND / inrate; - /* "drop" half the buffers */ - ints = GST_BUFFER_DURATION (inbuffer) * 2 * (j - 1); - GST_BUFFER_TIMESTAMP (inbuffer) = ints; - GST_BUFFER_OFFSET (inbuffer) = (j - 1) * 2 * samples; - GST_BUFFER_OFFSET_END (inbuffer) = j * 2 * samples + samples; - - gst_buffer_set_caps (inbuffer, caps); - - p = (gint16 *) GST_BUFFER_DATA (inbuffer); - - /* create a 16 bit signed ramp */ - for (i = 0; i < samples; ++i) { - *p = -32767 + i * (65535 / samples); - ++p; - *p = -32767 + i * (65535 / samples); - ++p; - } - - GST_DEBUG ("Sending Buffer time:%" G_GUINT64_FORMAT " duration:%" - G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%" - G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (inbuffer), - GST_BUFFER_DURATION (inbuffer), GST_BUFFER_IS_DISCONT (inbuffer), - GST_BUFFER_OFFSET (inbuffer), GST_BUFFER_OFFSET_END (inbuffer)); - /* pushing gives away my reference ... */ - fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); - - /* check if the timestamp of the pushed buffer matches the incoming one */ - outbuffer = g_list_nth_data (buffers, g_list_length (buffers) - 1); - fail_if (outbuffer == NULL); - fail_unless_equals_uint64 (ints, GST_BUFFER_TIMESTAMP (outbuffer)); - GST_DEBUG ("Got Buffer time:%" G_GUINT64_FORMAT " duration:%" - G_GINT64_FORMAT " discont:%d offset:%" G_GUINT64_FORMAT " offset_end:%" - G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (outbuffer), - GST_BUFFER_DURATION (outbuffer), GST_BUFFER_IS_DISCONT (outbuffer), - GST_BUFFER_OFFSET (outbuffer), GST_BUFFER_OFFSET_END (outbuffer)); - if (j > 1) { - fail_unless (GST_BUFFER_IS_DISCONT (outbuffer), - "expected discont for buffer #%d", j); - } - } - - /* cleanup */ - gst_caps_unref (caps); - cleanup_speexresample (speexresample); -} - -GST_START_TEST (test_discont_stream) -{ - /* integral scalings */ - test_discont_stream_instance (48000, 24000, 500, 20); - test_discont_stream_instance (48000, 12000, 500, 20); - test_discont_stream_instance (12000, 24000, 500, 20); - test_discont_stream_instance (12000, 48000, 500, 20); - - /* non-integral scalings */ - test_discont_stream_instance (44100, 8000, 500, 20); - test_discont_stream_instance (8000, 44100, 500, 20); - - /* wacky scalings */ - test_discont_stream_instance (12345, 54321, 500, 20); - test_discont_stream_instance (101, 99, 500, 20); -} - -GST_END_TEST; - - - -GST_START_TEST (test_reuse) -{ - GstElement *speexresample; - GstEvent *newseg; - GstBuffer *inbuffer; - GstCaps *caps; - - speexresample = setup_speexresample (1, 9343, 48000, 16, FALSE); - caps = gst_pad_get_negotiated_caps (mysrcpad); - fail_unless (gst_caps_is_fixed (caps)); - - fail_unless (gst_element_set_state (speexresample, - GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, - "could not set to playing"); - - newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0); - fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE); - - inbuffer = gst_buffer_new_and_alloc (9343 * 4); - memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer)); - GST_BUFFER_DURATION (inbuffer) = GST_SECOND; - GST_BUFFER_TIMESTAMP (inbuffer) = 0; - GST_BUFFER_OFFSET (inbuffer) = 0; - gst_buffer_set_caps (inbuffer, caps); - - /* pushing gives away my reference ... */ - fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); - - /* ... but it ends up being collected on the global buffer list */ - fail_unless_equals_int (g_list_length (buffers), 1); - - /* now reset and try again ... */ - fail_unless (gst_element_set_state (speexresample, - GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to NULL"); - - fail_unless (gst_element_set_state (speexresample, - GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, - "could not set to playing"); - - newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0); - fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE); - - inbuffer = gst_buffer_new_and_alloc (9343 * 4); - memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer)); - GST_BUFFER_DURATION (inbuffer) = GST_SECOND; - GST_BUFFER_TIMESTAMP (inbuffer) = 0; - GST_BUFFER_OFFSET (inbuffer) = 0; - gst_buffer_set_caps (inbuffer, caps); - - fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); - - /* ... it also ends up being collected on the global buffer list. If we - * now have more than 2 buffers, then speexresample probably didn't clean - * up its internal buffer properly and tried to push the remaining samples - * when it got the second NEWSEGMENT event */ - fail_unless_equals_int (g_list_length (buffers), 2); - - cleanup_speexresample (speexresample); - gst_caps_unref (caps); -} - -GST_END_TEST; - -GST_START_TEST (test_shutdown) -{ - GstElement *pipeline, *src, *cf1, *ar, *cf2, *sink; - GstCaps *caps; - guint i; - - /* create pipeline, force speexresample to actually resample */ - pipeline = gst_pipeline_new (NULL); - - src = gst_check_setup_element ("audiotestsrc"); - cf1 = gst_check_setup_element ("capsfilter"); - ar = gst_check_setup_element ("audioresample"); - cf2 = gst_check_setup_element ("capsfilter"); - g_object_set (cf2, "name", "capsfilter2", NULL); - sink = gst_check_setup_element ("fakesink"); - - caps = - gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 11025, NULL); - g_object_set (cf1, "caps", caps, NULL); - gst_caps_unref (caps); - - caps = - gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 48000, NULL); - g_object_set (cf2, "caps", caps, NULL); - gst_caps_unref (caps); - - /* don't want to sync against the clock, the more throughput the better */ - g_object_set (src, "is-live", FALSE, NULL); - g_object_set (sink, "sync", FALSE, NULL); - - gst_bin_add_many (GST_BIN (pipeline), src, cf1, ar, cf2, sink, NULL); - fail_if (!gst_element_link_many (src, cf1, ar, cf2, sink, NULL)); - - /* now, wait until pipeline is running and then shut it down again; repeat */ - for (i = 0; i < 20; ++i) { - gst_element_set_state (pipeline, GST_STATE_PAUSED); - gst_element_get_state (pipeline, NULL, NULL, -1); - gst_element_set_state (pipeline, GST_STATE_PLAYING); - g_usleep (100); - gst_element_set_state (pipeline, GST_STATE_NULL); - } - - gst_object_unref (pipeline); -} - -GST_END_TEST; - -static GstFlowReturn -live_switch_alloc_only_48000 (GstPad * pad, guint64 offset, - guint size, GstCaps * caps, GstBuffer ** buf) -{ - GstStructure *structure; - gint rate; - gint channels; - GstCaps *desired; - - structure = gst_caps_get_structure (caps, 0); - fail_unless (gst_structure_get_int (structure, "rate", &rate)); - fail_unless (gst_structure_get_int (structure, "channels", &channels)); - - if (rate < 48000) - return GST_FLOW_NOT_NEGOTIATED; - - desired = gst_caps_copy (caps); - gst_caps_set_simple (desired, "rate", G_TYPE_INT, 48000, NULL); - - *buf = gst_buffer_new_and_alloc (channels * 48000); - gst_buffer_set_caps (*buf, desired); - gst_caps_unref (desired); - - return GST_FLOW_OK; -} - -static GstCaps * -live_switch_get_sink_caps (GstPad * pad) -{ - GstCaps *result; - - result = gst_caps_copy (GST_PAD_CAPS (pad)); - - gst_caps_set_simple (result, - "rate", GST_TYPE_INT_RANGE, 48000, G_MAXINT, NULL); - - return result; -} - -static void -live_switch_push (int rate, GstCaps * caps) -{ - GstBuffer *inbuffer; - GstCaps *desired; - GList *l; - - desired = gst_caps_copy (caps); - gst_caps_set_simple (desired, "rate", G_TYPE_INT, rate, NULL); - gst_pad_set_caps (mysrcpad, desired); - - fail_unless (gst_pad_alloc_buffer_and_set_caps (mysrcpad, - GST_BUFFER_OFFSET_NONE, rate * 4, desired, &inbuffer) == GST_FLOW_OK); - - /* When the basetransform hits the non-configured case it always - * returns a buffer with exactly the same caps as we requested so the actual - * renegotiation (if needed) will be done in the _chain*/ - fail_unless (inbuffer != NULL); - GST_DEBUG ("desired: %" GST_PTR_FORMAT ".... got: %" GST_PTR_FORMAT, - desired, GST_BUFFER_CAPS (inbuffer)); - fail_unless (gst_caps_is_equal (desired, GST_BUFFER_CAPS (inbuffer))); - - memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer)); - GST_BUFFER_DURATION (inbuffer) = GST_SECOND; - GST_BUFFER_TIMESTAMP (inbuffer) = 0; - GST_BUFFER_OFFSET (inbuffer) = 0; - - /* pushing gives away my reference ... */ - fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); - - /* ... but it ends up being collected on the global buffer list */ - fail_unless_equals_int (g_list_length (buffers), 1); - - for (l = buffers; l; l = l->next) { - GstBuffer *buffer = GST_BUFFER (l->data); - - gst_buffer_unref (buffer); - } - - g_list_free (buffers); - buffers = NULL; - - gst_caps_unref (desired); -} - -GST_START_TEST (test_live_switch) -{ - GstElement *speexresample; - GstEvent *newseg; - GstCaps *caps; - - speexresample = setup_speexresample (4, 48000, 48000, 16, FALSE); - - /* Let the sinkpad act like something that can only handle things of - * rate 48000- and can only allocate buffers for that rate, but if someone - * tries to get a buffer with a rate higher then 48000 tries to renegotiate - * */ - gst_pad_set_bufferalloc_function (mysinkpad, live_switch_alloc_only_48000); - gst_pad_set_getcaps_function (mysinkpad, live_switch_get_sink_caps); - - gst_pad_use_fixed_caps (mysrcpad); - - caps = gst_pad_get_negotiated_caps (mysrcpad); - fail_unless (gst_caps_is_fixed (caps)); - - fail_unless (gst_element_set_state (speexresample, - GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, - "could not set to playing"); - - newseg = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0); - fail_unless (gst_pad_push_event (mysrcpad, newseg) != FALSE); - - /* downstream can provide the requested rate, a buffer alloc will be passed - * on */ - live_switch_push (48000, caps); - - /* Downstream can never accept this rate, buffer alloc isn't passed on */ - live_switch_push (40000, caps); - - /* Downstream can provide the requested rate but will re-negotiate */ - live_switch_push (50000, caps); - - cleanup_speexresample (speexresample); - gst_caps_unref (caps); -} - -GST_END_TEST; - -#ifndef GST_DISABLE_PARSE - -static GMainLoop *loop; -static gint messages = 0; - -static void -element_message_cb (GstBus * bus, GstMessage * message, gpointer user_data) -{ - gchar *s; - - s = gst_structure_to_string (gst_message_get_structure (message)); - GST_DEBUG ("Received message: %s", s); - g_free (s); - - messages++; -} - -static void -eos_message_cb (GstBus * bus, GstMessage * message, gpointer user_data) -{ - GST_DEBUG ("Received eos"); - g_main_loop_quit (loop); -} - -static void -test_pipeline (gint width, gboolean fp, gint inrate, gint outrate, gint quality) -{ - GstElement *pipeline; - GstBus *bus; - GError *error = NULL; - gchar *pipe_str; - - pipe_str = - g_strdup_printf - ("audiotestsrc num-buffers=10 ! audioconvert ! audio/x-raw-%s,rate=%d,width=%d,channels=2 ! audioresample quality=%d ! audio/x-raw-%s,rate=%d,width=%d ! identity check-imperfect-timestamp=TRUE ! fakesink", - (fp) ? "float" : "int", inrate, width, quality, (fp) ? "float" : "int", - outrate, width); - - pipeline = gst_parse_launch (pipe_str, &error); - fail_unless (pipeline != NULL, "Error parsing pipeline: %s", - error ? error->message : "(invalid error)"); - g_free (pipe_str); - - bus = gst_element_get_bus (pipeline); - fail_if (bus == NULL); - gst_bus_add_signal_watch (bus); - g_signal_connect (bus, "message::element", (GCallback) element_message_cb, - NULL); - g_signal_connect (bus, "message::eos", (GCallback) eos_message_cb, NULL); - - gst_element_set_state (pipeline, GST_STATE_PLAYING); - - /* run until we receive EOS */ - loop = g_main_loop_new (NULL, FALSE); - - g_main_loop_run (loop); - - g_main_loop_unref (loop); - loop = NULL; - - gst_element_set_state (pipeline, GST_STATE_NULL); - - fail_if (messages > 0, "Received imperfect timestamp messages"); - gst_object_unref (pipeline); -} - -GST_START_TEST (test_pipelines) -{ - gint quality; - - /* Test qualities 0, 5 and 10 */ - for (quality = 0; quality < 11; quality += 5) { - test_pipeline (8, FALSE, 44100, 48000, quality); - test_pipeline (8, FALSE, 48000, 44100, quality); - - test_pipeline (16, FALSE, 44100, 48000, quality); - test_pipeline (16, FALSE, 48000, 44100, quality); - - test_pipeline (24, FALSE, 44100, 48000, quality); - test_pipeline (24, FALSE, 48000, 44100, quality); - - test_pipeline (32, FALSE, 44100, 48000, quality); - test_pipeline (32, FALSE, 48000, 44100, quality); - - test_pipeline (32, TRUE, 44100, 48000, quality); - test_pipeline (32, TRUE, 48000, 44100, quality); - - test_pipeline (64, TRUE, 44100, 48000, quality); - test_pipeline (64, TRUE, 48000, 44100, quality); - } -} - -GST_END_TEST; -#endif - -static Suite * -speexresample_suite (void) -{ - Suite *s = suite_create ("speexresample"); - TCase *tc_chain = tcase_create ("general"); - - suite_add_tcase (s, tc_chain); - tcase_add_test (tc_chain, test_perfect_stream); - tcase_add_test (tc_chain, test_discont_stream); - tcase_add_test (tc_chain, test_reuse); - tcase_add_test (tc_chain, test_shutdown); - tcase_add_test (tc_chain, test_live_switch); - -#ifndef GST_DISABLE_PARSE - tcase_set_timeout (tc_chain, 360); - tcase_add_test (tc_chain, test_pipelines); -#endif - - return s; -} - -GST_CHECK_MAIN (speexresample); -- 2.7.4