From dbd1b8490f2b64085e5c56bf1b2aaf430eab5b0f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 8 Mar 2007 10:02:12 +0000 Subject: [PATCH] gst/audiofx/: Add new audiodynamic element which can act as a compressor or expander. Supported are hard-knee and sof... Original commit message from CVS: reviewed by: Stefan Kost * gst/audiofx/Makefile.am: * gst/audiofx/audiodynamic.c: (gst_audio_dynamic_characteristics_get_type), (gst_audio_dynamic_mode_get_type), (gst_audio_dynamic_set_process_function), (gst_audio_dynamic_base_init), (gst_audio_dynamic_class_init), (gst_audio_dynamic_init), (gst_audio_dynamic_set_property), (gst_audio_dynamic_get_property), (gst_audio_dynamic_setup), (gst_audio_dynamic_transform_hard_knee_compressor_int), (gst_audio_dynamic_transform_hard_knee_compressor_float), (gst_audio_dynamic_transform_soft_knee_compressor_int), (gst_audio_dynamic_transform_soft_knee_compressor_float), (gst_audio_dynamic_transform_hard_knee_expander_int), (gst_audio_dynamic_transform_hard_knee_expander_float), (gst_audio_dynamic_transform_soft_knee_expander_int), (gst_audio_dynamic_transform_soft_knee_expander_float), (gst_audio_dynamic_transform_ip): * gst/audiofx/audiodynamic.h: * gst/audiofx/audiofx.c: (plugin_init): Add new audiodynamic element which can act as a compressor or expander. Supported are hard-knee and soft-knee operation modes with user-specified ratio and threshold. Attack and release parameters are not yet implemented but will follow. * docs/plugins/Makefile.am: * docs/plugins/gst-plugins-good-plugins-docs.sgml: * docs/plugins/gst-plugins-good-plugins-sections.txt: * docs/plugins/gst-plugins-good-plugins.args: * docs/plugins/inspect/plugin-audiofx.xml: Integrate audiodynamic into the docs. * tests/check/Makefile.am: * tests/check/elements/audiodynamic.c: (setup_dynamic), (cleanup_dynamic), (GST_START_TEST), (dynamic_suite), (main): Add unit test for audiodynamic. --- ChangeLog | 38 ++ docs/plugins/Makefile.am | 1 + docs/plugins/gst-plugins-good-plugins-docs.sgml | 1 + docs/plugins/gst-plugins-good-plugins-sections.txt | 10 + docs/plugins/gst-plugins-good-plugins.args | 40 ++ docs/plugins/inspect/plugin-audiofx.xml | 7 + gst/audiofx/Makefile.am | 7 +- gst/audiofx/audiodynamic.c | 711 +++++++++++++++++++++ gst/audiofx/audiodynamic.h | 67 ++ gst/audiofx/audiofx.c | 5 +- tests/check/Makefile.am | 1 + tests/check/elements/audiodynamic.c | 459 +++++++++++++ 12 files changed, 1344 insertions(+), 3 deletions(-) create mode 100644 gst/audiofx/audiodynamic.c create mode 100644 gst/audiofx/audiodynamic.h create mode 100644 tests/check/elements/audiodynamic.c diff --git a/ChangeLog b/ChangeLog index aa4e94c..7d6a490 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,41 @@ +2007-03-08 Sebastian Dröge + + reviewed by: Stefan Kost + + * gst/audiofx/Makefile.am: + * gst/audiofx/audiodynamic.c: + (gst_audio_dynamic_characteristics_get_type), + (gst_audio_dynamic_mode_get_type), + (gst_audio_dynamic_set_process_function), + (gst_audio_dynamic_base_init), (gst_audio_dynamic_class_init), + (gst_audio_dynamic_init), (gst_audio_dynamic_set_property), + (gst_audio_dynamic_get_property), (gst_audio_dynamic_setup), + (gst_audio_dynamic_transform_hard_knee_compressor_int), + (gst_audio_dynamic_transform_hard_knee_compressor_float), + (gst_audio_dynamic_transform_soft_knee_compressor_int), + (gst_audio_dynamic_transform_soft_knee_compressor_float), + (gst_audio_dynamic_transform_hard_knee_expander_int), + (gst_audio_dynamic_transform_hard_knee_expander_float), + (gst_audio_dynamic_transform_soft_knee_expander_int), + (gst_audio_dynamic_transform_soft_knee_expander_float), + (gst_audio_dynamic_transform_ip): + * gst/audiofx/audiodynamic.h: + * gst/audiofx/audiofx.c: (plugin_init): + Add new audiodynamic element which can act as a compressor or + expander. Supported are hard-knee and soft-knee operation modes with + user-specified ratio and threshold. + Attack and release parameters are not yet implemented but will follow. + * docs/plugins/Makefile.am: + * docs/plugins/gst-plugins-good-plugins-docs.sgml: + * docs/plugins/gst-plugins-good-plugins-sections.txt: + * docs/plugins/gst-plugins-good-plugins.args: + * docs/plugins/inspect/plugin-audiofx.xml: + Integrate audiodynamic into the docs. + * tests/check/Makefile.am: + * tests/check/elements/audiodynamic.c: (setup_dynamic), + (cleanup_dynamic), (GST_START_TEST), (dynamic_suite), (main): + Add unit test for audiodynamic. + 2007-03-07 Jan Schmidt * ext/raw1394/gstdv1394src.c: (gst_dv1394src_start): diff --git a/docs/plugins/Makefile.am b/docs/plugins/Makefile.am index 640edbb..174bb99 100644 --- a/docs/plugins/Makefile.am +++ b/docs/plugins/Makefile.am @@ -81,6 +81,7 @@ EXTRA_HFILES = \ $(top_srcdir)/gst/audiofx/audiopanorama.h \ $(top_srcdir)/gst/audiofx/audioinvert.h \ $(top_srcdir)/gst/audiofx/audioamplify.h \ + $(top_srcdir)/gst/audiofx/audiodynamic.h \ $(top_srcdir)/gst/autodetect/gstautoaudiosink.h \ $(top_srcdir)/gst/autodetect/gstautovideosink.h \ $(top_srcdir)/gst/avi/gstavidemux.h \ diff --git a/docs/plugins/gst-plugins-good-plugins-docs.sgml b/docs/plugins/gst-plugins-good-plugins-docs.sgml index 37d17dd..291cc46 100644 --- a/docs/plugins/gst-plugins-good-plugins-docs.sgml +++ b/docs/plugins/gst-plugins-good-plugins-docs.sgml @@ -17,6 +17,7 @@ + diff --git a/docs/plugins/gst-plugins-good-plugins-sections.txt b/docs/plugins/gst-plugins-good-plugins-sections.txt index 066566c..a092e48 100644 --- a/docs/plugins/gst-plugins-good-plugins-sections.txt +++ b/docs/plugins/gst-plugins-good-plugins-sections.txt @@ -58,6 +58,16 @@ GST_AUDIO_AMPLIFY_CLASS
+element-audiodynamic +GstAudioDynamic +audiodynamic + +GstAudioDynamicClass +GST_AUDIO_DYNAMIC +GST_AUDIO_DYNAMIC_CLASS +
+ +
element-autoaudiosink GstAutoAudioSink autoaudiosink diff --git a/docs/plugins/gst-plugins-good-plugins.args b/docs/plugins/gst-plugins-good-plugins.args index 19537ee..0486e22 100644 --- a/docs/plugins/gst-plugins-good-plugins.args +++ b/docs/plugins/gst-plugins-good-plugins.args @@ -16668,3 +16668,43 @@ Normal Clipping (default) + +GstAudioDynamic::characteristics +GstAudioDynamicCharacteristics + +rw +Characteristics +Selects whether the ratio should be applied smooth (soft-knee) or hard (hard-knee). +Hard Knee (default) + + + +GstAudioDynamic::mode +GstAudioDynamicMode + +rw +Mode +Selects whether the filter should work on loud samples (compressor) orquiet samples (expander). +Compressor (default) + + + +GstAudioDynamic::ratio +gfloat +[0,inf] +rw +Ratio +Ratio that should be applied. +1 + + + +GstAudioDynamic::threshold +gfloat +[0,1] +rw +Threshold +Threshold until the filter is activated. +0 + + diff --git a/docs/plugins/inspect/plugin-audiofx.xml b/docs/plugins/inspect/plugin-audiofx.xml index f860bd2..9740da9 100644 --- a/docs/plugins/inspect/plugin-audiofx.xml +++ b/docs/plugins/inspect/plugin-audiofx.xml @@ -17,6 +17,13 @@ Sebastian Dröge <slomo@circular-chaos.org> + audiodynamic + AudioDynamic + Filter/Effect/Audio + Compressor and Expander + Sebastian Dröge <slomo@circular-chaos.org> + + audioinvert AudioInvert Filter/Effect/Audio diff --git a/gst/audiofx/Makefile.am b/gst/audiofx/Makefile.am index cb2a180..b5bf0bf 100644 --- a/gst/audiofx/Makefile.am +++ b/gst/audiofx/Makefile.am @@ -6,7 +6,8 @@ plugin_LTLIBRARIES = libgstaudiofx.la libgstaudiofx_la_SOURCES = audiofx.c\ audiopanorama.c \ audioinvert.c \ - audioamplify.c + audioamplify.c \ + audiodynamic.c # flags used to compile this plugin libgstaudiofx_la_CFLAGS = $(GST_CFLAGS) \ @@ -23,4 +24,6 @@ libgstaudiofx_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) # headers we need but don't want installed noinst_HEADERS = audiopanorama.h \ audioinvert.h \ - audioamplify.h + audioamplify.h \ + audiodynamic.h + diff --git a/gst/audiofx/audiodynamic.c b/gst/audiofx/audiodynamic.c new file mode 100644 index 0000000..3527e34 --- /dev/null +++ b/gst/audiofx/audiodynamic.c @@ -0,0 +1,711 @@ +/* + * GStreamer + * Copyright (C) 2007 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-audiodynamic + * @short_description: Compressor and Expander + * + * + * This element can act as a compressor or expander. A compressor changes the + * amplitude of all samples above a specific threshold with a specific ratio, + * a expander does the same for all samples below a specific threshold. If + * soft-knee mode is selected the ratio is applied smoothly. + * Example launch line + * + * + * gst-launch audiotestsrc wave=saw ! audiodynamic characteristics=soft-knee mode=compressor threshold=0.5 rate=0.5 ! alsasink + * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiodynamic characteristics=hard-knee mode=expander threshold=0.2 rate=4.0 ! alsasink + * gst-launch audiotestsrc wave=saw ! audioconvert ! audiodynamic ! audioconvert ! alsasink + * + * + * + */ + +/* TODO: Implement attack and release parameters */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#include "audiodynamic.h" + +#define GST_CAT_DEFAULT gst_audio_dynamic_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +static const GstElementDetails element_details = +GST_ELEMENT_DETAILS ("AudioDynamic", + "Filter/Effect/Audio", + "Compressor and Expander", + "Sebastian Dröge "); + +/* Filter signals and args */ +enum +{ + /* FILL ME */ + LAST_SIGNAL +}; + +enum +{ + PROP_0, + PROP_CHARACTERISTICS, + PROP_MODE, + PROP_THRESHOLD, + PROP_RATIO +}; + +#define ALLOWED_CAPS \ + "audio/x-raw-int," \ + " depth=(int)16," \ + " width=(int)16," \ + " endianness=(int)BYTE_ORDER," \ + " signed=(bool)TRUE," \ + " rate=(int)[1,MAX]," \ + " channels=(int)[1,MAX]; " \ + "audio/x-raw-float," \ + " width=(int)32," \ + " endianness=(int)BYTE_ORDER," \ + " rate=(int)[1,MAX]," \ + " channels=(int)[1,MAX]" + +#define DEBUG_INIT(bla) \ + GST_DEBUG_CATEGORY_INIT (gst_audio_dynamic_debug, "audiodynamic", 0, "audiodynamic element"); + +GST_BOILERPLATE_FULL (GstAudioDynamic, gst_audio_dynamic, GstAudioFilter, + GST_TYPE_AUDIO_FILTER, DEBUG_INIT); + +static void gst_audio_dynamic_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_audio_dynamic_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static gboolean gst_audio_dynamic_setup (GstAudioFilter * filter, + GstRingBufferSpec * format); +static GstFlowReturn gst_audio_dynamic_transform_ip (GstBaseTransform * base, + GstBuffer * buf); + +static void +gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter, + gint16 * data, guint num_samples); +static void +gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic * + filter, gfloat * data, guint num_samples); +static void +gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter, + gint16 * data, guint num_samples); +static void +gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic * + filter, gfloat * data, guint num_samples); +static void gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic + * filter, gint16 * data, guint num_samples); +static void +gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter, + gfloat * data, guint num_samples); +static void gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic + * filter, gint16 * data, guint num_samples); +static void +gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter, + gfloat * data, guint num_samples); + +static GstAudioDynamicProcessFunc process_functions[] = { + (GstAudioDynamicProcessFunc) + gst_audio_dynamic_transform_hard_knee_compressor_int, + (GstAudioDynamicProcessFunc) + gst_audio_dynamic_transform_hard_knee_compressor_float, + (GstAudioDynamicProcessFunc) + gst_audio_dynamic_transform_soft_knee_compressor_int, + (GstAudioDynamicProcessFunc) + gst_audio_dynamic_transform_soft_knee_compressor_float, + (GstAudioDynamicProcessFunc) + gst_audio_dynamic_transform_hard_knee_expander_int, + (GstAudioDynamicProcessFunc) + gst_audio_dynamic_transform_hard_knee_expander_float, + (GstAudioDynamicProcessFunc) + gst_audio_dynamic_transform_soft_knee_expander_int, + (GstAudioDynamicProcessFunc) + gst_audio_dynamic_transform_soft_knee_expander_float +}; + +enum +{ + CHARACTERISTICS_HARD_KNEE = 0, + CHARACTERISTICS_SOFT_KNEE +}; + +#define GST_TYPE_AUDIO_DYNAMIC_CHARACTERISTICS (gst_audio_dynamic_characteristics_get_type ()) +static GType +gst_audio_dynamic_characteristics_get_type (void) +{ + static GType gtype = 0; + + if (gtype == 0) { + static const GEnumValue values[] = { + {CHARACTERISTICS_HARD_KNEE, "Hard Knee (default)", + "hard-knee"}, + {CHARACTERISTICS_SOFT_KNEE, "Soft Knee (smooth)", + "soft-knee"}, + {0, NULL, NULL} + }; + + gtype = g_enum_register_static ("GstAudioDynamicCharacteristics", values); + } + return gtype; +} + +enum +{ + MODE_COMPRESSOR = 0, + MODE_EXPANDER +}; + +#define GST_TYPE_AUDIO_DYNAMIC_MODE (gst_audio_dynamic_mode_get_type ()) +static GType +gst_audio_dynamic_mode_get_type (void) +{ + static GType gtype = 0; + + if (gtype == 0) { + static const GEnumValue values[] = { + {MODE_COMPRESSOR, "Compressor (default)", + "compressor"}, + {MODE_EXPANDER, "Expander", "expander"}, + {0, NULL, NULL} + }; + + gtype = g_enum_register_static ("GstAudioDynamicMode", values); + } + return gtype; +} + +static gboolean +gst_audio_dynamic_set_process_function (GstAudioDynamic * filter) +{ + gint func_index; + + func_index = (filter->mode == MODE_COMPRESSOR) ? 0 : 4; + func_index += (filter->characteristics == CHARACTERISTICS_HARD_KNEE) ? 0 : 2; + func_index += (!filter->is_float) ? 0 : 1; + + if (func_index >= 0 && func_index < 8) { + filter->process = process_functions[func_index]; + return TRUE; + } + + return FALSE; +} + +/* GObject vmethod implementations */ + +static void +gst_audio_dynamic_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + GstCaps *caps; + + gst_element_class_set_details (element_class, &element_details); + + caps = gst_caps_from_string (ALLOWED_CAPS); + gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass), + caps); + gst_caps_unref (caps); +} + +static void +gst_audio_dynamic_class_init (GstAudioDynamicClass * klass) +{ + GObjectClass *gobject_class; + + gobject_class = (GObjectClass *) klass; + gobject_class->set_property = gst_audio_dynamic_set_property; + gobject_class->get_property = gst_audio_dynamic_get_property; + + g_object_class_install_property (gobject_class, PROP_CHARACTERISTICS, + g_param_spec_enum ("characteristics", "Characteristics", + "Selects whether the ratio should be applied smooth (soft-knee) " + "or hard (hard-knee).", + GST_TYPE_AUDIO_DYNAMIC_CHARACTERISTICS, CHARACTERISTICS_HARD_KNEE, + G_PARAM_READWRITE)); + + g_object_class_install_property (gobject_class, PROP_MODE, + g_param_spec_enum ("mode", "Mode", + "Selects whether the filter should work on loud samples (compressor) or" + "quiet samples (expander).", + GST_TYPE_AUDIO_DYNAMIC_MODE, MODE_COMPRESSOR, G_PARAM_READWRITE)); + + g_object_class_install_property (gobject_class, PROP_THRESHOLD, + g_param_spec_float ("threshold", "Threshold", + "Threshold until the filter is activated", 0.0, 1.0, + 0.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE)); + + g_object_class_install_property (gobject_class, PROP_RATIO, + g_param_spec_float ("ratio", "Ratio", + "Ratio that should be applied", 0.0, G_MAXDOUBLE, + 1.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE)); + + GST_AUDIO_FILTER_CLASS (klass)->setup = + GST_DEBUG_FUNCPTR (gst_audio_dynamic_setup); + GST_BASE_TRANSFORM_CLASS (klass)->transform_ip = + GST_DEBUG_FUNCPTR (gst_audio_dynamic_transform_ip); +} + +static void +gst_audio_dynamic_init (GstAudioDynamic * filter, GstAudioDynamicClass * klass) +{ + filter->ratio = 1.0; + filter->threshold = 0.0; + filter->characteristics = CHARACTERISTICS_HARD_KNEE; + filter->mode = MODE_COMPRESSOR; + filter->width = 0; + filter->is_float = FALSE; + gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE); +} + +static void +gst_audio_dynamic_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object); + + switch (prop_id) { + case PROP_CHARACTERISTICS: + filter->characteristics = g_value_get_enum (value); + gst_audio_dynamic_set_process_function (filter); + break; + case PROP_MODE: + filter->mode = g_value_get_enum (value); + gst_audio_dynamic_set_process_function (filter); + break; + case PROP_THRESHOLD: + filter->threshold = g_value_get_float (value); + break; + case PROP_RATIO: + filter->ratio = g_value_get_float (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_audio_dynamic_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (object); + + switch (prop_id) { + case PROP_CHARACTERISTICS: + g_value_set_enum (value, filter->characteristics); + break; + case PROP_MODE: + g_value_set_enum (value, filter->mode); + break; + case PROP_THRESHOLD: + g_value_set_float (value, filter->threshold); + break; + case PROP_RATIO: + g_value_set_float (value, filter->ratio); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +/* GstAudioFilter vmethod implementations */ + +static gboolean +gst_audio_dynamic_setup (GstAudioFilter * base, GstRingBufferSpec * format) +{ + GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base); + gboolean ret = TRUE; + + filter->width = format->width / 8; + filter->is_float = (format->type == GST_BUFTYPE_FLOAT) ? TRUE : FALSE; + + ret = gst_audio_dynamic_set_process_function (filter); + + return ret; +} + +static void +gst_audio_dynamic_transform_hard_knee_compressor_int (GstAudioDynamic * filter, + gint16 * data, guint num_samples) +{ + glong val; + glong thr_p = filter->threshold * G_MAXINT16; + glong thr_n = filter->threshold * G_MININT16; + + /* Nothing to do for us if ratio is 1.0 or if the threshold + * equals 1.0. */ + if (filter->threshold == 1.0 || filter->ratio == 1.0) + return; + + for (; num_samples; num_samples--) { + val = *data; + + if (val > thr_p) { + val = thr_p + (val - thr_p) * filter->ratio; + } else if (val < thr_n) { + val = thr_n + (val - thr_n) * filter->ratio; + } + *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16); + } +} + +static void +gst_audio_dynamic_transform_hard_knee_compressor_float (GstAudioDynamic * + filter, gfloat * data, guint num_samples) +{ + gdouble val, threshold = filter->threshold; + + /* Nothing to do for us if ratio == 1.0. + * As float values can be above 1.0 we have to do something + * if threshold is greater than 1.0. */ + if (filter->ratio == 1.0) + return; + + for (; num_samples; num_samples--) { + val = *data; + + if (val > threshold) { + val = threshold + (val - threshold) * filter->ratio; + } else if (val < -threshold) { + val = -threshold + (val + threshold) * filter->ratio; + } + *data++ = (gfloat) val; + } +} + +static void +gst_audio_dynamic_transform_soft_knee_compressor_int (GstAudioDynamic * filter, + gint16 * data, guint num_samples) +{ + glong val; + glong thr_p = filter->threshold * G_MAXINT16; + glong thr_n = filter->threshold * G_MININT16; + gdouble a_p, b_p, c_p; + gdouble a_n, b_n, c_n; + + /* Nothing to do for us if ratio is 1.0 or if the threshold + * equals 1.0. */ + if (filter->threshold == 1.0 || filter->ratio == 1.0) + return; + + /* We build a 2nd degree polynomial here for + * values greater than threshold or small than + * -threshold with: + * f(t) = t, f'(t) = 1, f'(m) = r + * => + * a = (1-r)/(2*(t-m)) + * b = (r*t - m)/(t-m) + * c = t * (1 - b - a*t) + * f(x) = ax^2 + bx + c + */ + + /* shouldn't happen because this would only be the case + * for threshold == 1.0 which we catch above */ + g_assert (thr_p - G_MAXINT16 != 0); + g_assert (thr_n - G_MININT != 0); + + a_p = (1 - filter->ratio) / (2 * (thr_p - G_MAXINT16)); + b_p = (filter->ratio * thr_p - G_MAXINT16) / (thr_p - G_MAXINT16); + c_p = thr_p * (1 - b_p - a_p * thr_p); + a_n = (1 - filter->ratio) / (2 * (thr_n - G_MININT16)); + b_n = (filter->ratio * thr_n - G_MININT16) / (thr_n - G_MININT16); + c_n = thr_n * (1 - b_n - a_n * thr_n); + + for (; num_samples; num_samples--) { + val = *data; + + if (val > thr_p) { + val = a_p * val * val + b_p * val + c_p; + } else if (val < thr_n) { + val = a_n * val * val + b_n * val + c_n; + } + *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16); + } +} + +static void +gst_audio_dynamic_transform_soft_knee_compressor_float (GstAudioDynamic * + filter, gfloat * data, guint num_samples) +{ + gdouble val; + gdouble threshold = filter->threshold; + gdouble a_p, b_p, c_p; + gdouble a_n, b_n, c_n; + + /* Nothing to do for us if ratio == 1.0. + * As float values can be above 1.0 we have to do something + * if threshold is greater than 1.0. */ + if (filter->ratio == 1.0) + return; + + /* We build a 2nd degree polynomial here for + * values greater than threshold or small than + * -threshold with: + * f(t) = t, f'(t) = 1, f'(m) = r + * => + * a = (1-r)/(2*(t-m)) + * b = (r*t - m)/(t-m) + * c = t * (1 - b - a*t) + * f(x) = ax^2 + bx + c + */ + + /* FIXME: If treshold is the same as the maximum + * we need to raise it a bit to prevent + * division by zero. */ + if (threshold == 1.0) + threshold = 1.0 + 0.00001; + + a_p = (1.0 - filter->ratio) / (2.0 * (threshold - 1.0)); + b_p = (filter->ratio * threshold - 1.0) / (threshold - 1.0); + c_p = threshold * (1.0 - b_p - a_p * threshold); + a_n = (1.0 - filter->ratio) / (2.0 * (-threshold + 1.0)); + b_n = (-filter->ratio * threshold + 1.0) / (-threshold + 1.0); + c_n = -threshold * (1.0 - b_n + a_n * threshold); + + for (; num_samples; num_samples--) { + val = *data; + + if (val > 1.0) { + val = 1.0 + (val - 1.0) * filter->ratio; + } else if (val > threshold) { + val = a_p * val * val + b_p * val + c_p; + } else if (val < -1.0) { + val = -1.0 + (val + 1.0) * filter->ratio; + } else if (val < -threshold) { + val = a_n * val * val + b_n * val + c_n; + } + *data++ = (gfloat) val; + } +} + +static void +gst_audio_dynamic_transform_hard_knee_expander_int (GstAudioDynamic * filter, + gint16 * data, guint num_samples) +{ + glong val; + glong thr_p = filter->threshold * G_MAXINT16; + glong thr_n = filter->threshold * G_MININT16; + gdouble zero_p, zero_n; + + /* Nothing to do for us here if threshold equals 0.0 + * or ratio equals 1.0 */ + if (filter->threshold == 0.0 || filter->ratio == 1.0) + return; + + /* zero crossing of our function */ + if (filter->ratio != 0.0) { + zero_p = thr_p - thr_p / filter->ratio; + zero_n = thr_n - thr_n / filter->ratio; + } else { + zero_p = zero_n = 0.0; + } + + if (zero_p < 0.0) + zero_p = 0.0; + if (zero_n > 0.0) + zero_n = 0.0; + + for (; num_samples; num_samples--) { + val = *data; + + if (val < thr_p && val > zero_p) { + val = filter->ratio * val + thr_p * (1 - filter->ratio); + } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) { + val = 0; + } else if (val > thr_n && val < zero_n) { + val = filter->ratio * val + thr_n * (1 - filter->ratio); + } + *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16); + } +} + +static void +gst_audio_dynamic_transform_hard_knee_expander_float (GstAudioDynamic * filter, + gfloat * data, guint num_samples) +{ + gdouble val, threshold = filter->threshold, zero; + + /* Nothing to do for us here if threshold equals 0.0 + * or ratio equals 1.0 */ + if (filter->threshold == 0.0 || filter->ratio == 1.0) + return; + + /* zero crossing of our function */ + if (filter->ratio != 0.0) + zero = threshold - threshold / filter->ratio; + else + zero = 0.0; + + if (zero < 0.0) + zero = 0.0; + + for (; num_samples; num_samples--) { + val = *data; + + if (val < threshold && val > zero) { + val = filter->ratio * val + threshold * (1.0 - filter->ratio); + } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) { + val = 0.0; + } else if (val > -threshold && val < -zero) { + val = filter->ratio * val - threshold * (1.0 - filter->ratio); + } + *data++ = (gfloat) val; + } +} + +static void +gst_audio_dynamic_transform_soft_knee_expander_int (GstAudioDynamic * filter, + gint16 * data, guint num_samples) +{ + glong val; + glong thr_p = filter->threshold * G_MAXINT16; + glong thr_n = filter->threshold * G_MININT16; + gdouble zero_p, zero_n; + gdouble a_p, b_p, c_p; + gdouble a_n, b_n, c_n; + + /* Nothing to do for us here if threshold equals 0.0 + * or ratio equals 1.0 */ + if (filter->threshold == 0.0 || filter->ratio == 1.0) + return; + + /* zero crossing of our function */ + zero_p = (thr_p * (filter->ratio - 1.0)) / (1.0 + filter->ratio); + zero_n = (thr_n * (filter->ratio - 1.0)) / (1.0 + filter->ratio); + + if (zero_p < 0.0) + zero_p = 0.0; + if (zero_n > 0.0) + zero_n = 0.0; + + /* shouldn't happen as this would only happen + * with threshold == 0.0 */ + g_assert (thr_p != 0); + g_assert (thr_n != 0); + + /* We build a 2n degree polynomial here for values between + * 0 and threshold or 0 and -threshold with: + * f(t) = t, f'(t) = 1, f(z) = 0, f'(z) = r + * z between 0 and t + * => + * a = (1 - r^2) / (4 * t) + * b = (1 + r^2) / 2 + * c = t * (1.0 - b - a*t) + * f(x) = ax^2 + bx + c */ + a_p = (1.0 - filter->ratio * filter->ratio) / (4.0 * thr_p); + b_p = (1.0 + filter->ratio * filter->ratio) / 2.0; + c_p = thr_p * (1.0 - b_p - a_p * thr_p); + a_n = (1.0 - filter->ratio * filter->ratio) / (4.0 * thr_n); + b_n = (1.0 + filter->ratio * filter->ratio) / 2.0; + c_n = thr_n * (1.0 - b_n - a_n * thr_n); + + for (; num_samples; num_samples--) { + val = *data; + + if (val < thr_p && val > zero_p) { + val = a_p * val * val + b_p * val + c_p; + } else if ((val <= zero_p && val > 0) || (val >= zero_n && val < 0)) { + val = 0; + } else if (val > thr_n && val < zero_n) { + val = a_n * val * val + b_n * val + c_n; + } + *data++ = (gint16) CLAMP (val, G_MININT16, G_MAXINT16); + } +} + +static void +gst_audio_dynamic_transform_soft_knee_expander_float (GstAudioDynamic * filter, + gfloat * data, guint num_samples) +{ + gdouble val; + gdouble threshold = filter->threshold; + gdouble zero; + gdouble a_p, b_p, c_p; + gdouble a_n, b_n, c_n; + + /* Nothing to do for us here if threshold equals 0.0 + * or ratio equals 1.0 */ + if (filter->threshold == 0.0 || filter->ratio == 1.0) + return; + + /* zero crossing of our function */ + zero = (threshold * (filter->ratio - 1.0)) / (1.0 + filter->ratio); + + if (zero < 0.0) + zero = 0.0; + + /* shouldn't happen as this only happens with + * threshold == 0.0 */ + g_assert (threshold != 0.0); + + /* We build a 2n degree polynomial here for values between + * 0 and threshold or 0 and -threshold with: + * f(t) = t, f'(t) = 1, f(z) = 0, f'(z) = r + * z between 0 and t + * => + * a = (1 - r^2) / (4 * t) + * b = (1 + r^2) / 2 + * c = t * (1.0 - b - a*t) + * f(x) = ax^2 + bx + c */ + a_p = (1.0 - filter->ratio * filter->ratio) / (4.0 * threshold); + b_p = (1.0 + filter->ratio * filter->ratio) / 2.0; + c_p = threshold * (1.0 - b_p - a_p * threshold); + a_n = (1.0 - filter->ratio * filter->ratio) / (-4.0 * threshold); + b_n = (1.0 + filter->ratio * filter->ratio) / 2.0; + c_n = -threshold * (1.0 - b_n + a_n * threshold); + + for (; num_samples; num_samples--) { + val = *data; + + if (val < threshold && val > zero) { + val = a_p * val * val + b_p * val + c_p; + } else if ((val <= zero && val > 0.0) || (val >= -zero && val < 0.0)) { + val = 0.0; + } else if (val > -threshold && val < -zero) { + val = a_n * val * val + b_n * val + c_n; + } + *data++ = (gfloat) val; + } +} + +/* GstBaseTransform vmethod implementations */ +static GstFlowReturn +gst_audio_dynamic_transform_ip (GstBaseTransform * base, GstBuffer * buf) +{ + GstAudioDynamic *filter = GST_AUDIO_DYNAMIC (base); + guint num_samples = GST_BUFFER_SIZE (buf) / filter->width; + + if (!gst_buffer_is_writable (buf)) + return GST_FLOW_OK; + + filter->process (filter, GST_BUFFER_DATA (buf), num_samples); + + return GST_FLOW_OK; +} diff --git a/gst/audiofx/audiodynamic.h b/gst/audiofx/audiodynamic.h new file mode 100644 index 0000000..3f8572c --- /dev/null +++ b/gst/audiofx/audiodynamic.h @@ -0,0 +1,67 @@ +/* + * GStreamer + * Copyright (C) 2007 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 __GST_AUDIO_DYNAMIC_H__ +#define __GST_AUDIO_DYNAMIC_H__ + +#include +#include +#include +#include + +G_BEGIN_DECLS + +#define GST_TYPE_AUDIO_DYNAMIC (gst_audio_dynamic_get_type()) +#define GST_AUDIO_DYNAMIC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_DYNAMIC,GstAudioDynamic)) +#define GST_IS_AUDIO_DYNAMIC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_DYNAMIC)) +#define GST_AUDIO_DYNAMIC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_AUDIO_DYNAMIC,GstAudioDynamicClass)) +#define GST_IS_AUDIO_DYNAMIC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_AUDIO_DYNAMIC)) +#define GST_AUDIO_DYNAMIC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_AUDIO_DYNAMIC,GstAudioDynamicClass)) +typedef struct _GstAudioDynamic GstAudioDynamic; +typedef struct _GstAudioDynamicClass GstAudioDynamicClass; + +typedef void (*GstAudioDynamicProcessFunc) (GstAudioDynamic *, guint8 *, guint); + +struct _GstAudioDynamic +{ + GstAudioFilter audiofilter; + + gfloat degree; + + /* < private > */ + GstAudioDynamicProcessFunc process; + gint width; + gboolean is_float; + gint characteristics; + gint mode; + gfloat threshold; + gfloat ratio; +}; + +struct _GstAudioDynamicClass +{ + GstAudioFilterClass parent; +}; + +GType gst_audio_dynamic_get_type (void); + +G_END_DECLS + +#endif /* __GST_AUDIO_DYNAMIC_H__ */ diff --git a/gst/audiofx/audiofx.c b/gst/audiofx/audiofx.c index 1ee1da6..e6d84d2 100644 --- a/gst/audiofx/audiofx.c +++ b/gst/audiofx/audiofx.c @@ -28,6 +28,7 @@ #include "audiopanorama.h" #include "audioinvert.h" #include "audioamplify.h" +#include "audiodynamic.h" /* entry point to initialize the plug-in * initialize the plug-in itself @@ -45,7 +46,9 @@ plugin_init (GstPlugin * plugin) gst_element_register (plugin, "audioinvert", GST_RANK_NONE, GST_TYPE_AUDIO_INVERT) && gst_element_register (plugin, "audioamplify", GST_RANK_NONE, - GST_TYPE_AUDIO_AMPLIFY)); + GST_TYPE_AUDIO_AMPLIFY) && + gst_element_register (plugin, "audiodynamic", GST_RANK_NONE, + GST_TYPE_AUDIO_DYNAMIC)); } GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 9fd70f3..fe67304 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -40,6 +40,7 @@ check_PROGRAMS = \ elements/audiopanorama \ elements/audioinvert \ elements/audioamplify \ + elements/audiodynamic \ elements/avimux \ elements/id3demux \ elements/level \ diff --git a/tests/check/elements/audiodynamic.c b/tests/check/elements/audiodynamic.c new file mode 100644 index 0000000..eead6b9 --- /dev/null +++ b/tests/check/elements/audiodynamic.c @@ -0,0 +1,459 @@ +/* GStreamer + * + * unit test for audiodynamic + * + * Copyright (C) 2007 Sebastian Dröge + * + * Greatly based on the audiopanorama unit test + * Copyright (C) 2006 Stefan Kost + * + * 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 + +gboolean have_eos = FALSE; + +/* 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 */ +GstPad *mysrcpad, *mysinkpad; + + +#define DYNAMIC_CAPS_STRING \ + "audio/x-raw-int, " \ + "channels = (int) 1, " \ + "rate = (int) 44100, " \ + "endianness = (int) BYTE_ORDER, " \ + "width = (int) 16, " \ + "depth = (int) 16, " \ + "signed = (bool) TRUE" + +static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-raw-int, " + "channels = (int) 1, " + "rate = (int) [ 1, MAX ], " + "endianness = (int) BYTE_ORDER, " + "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE") + ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-raw-int, " + "channels = (int) 1, " + "rate = (int) [ 1, MAX ], " + "endianness = (int) BYTE_ORDER, " + "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE") + ); + +GstElement * +setup_dynamic () +{ + GstElement *dynamic; + + GST_DEBUG ("setup_dynamic"); + dynamic = gst_check_setup_element ("audiodynamic"); + mysrcpad = gst_check_setup_src_pad (dynamic, &srctemplate, NULL); + mysinkpad = gst_check_setup_sink_pad (dynamic, &sinktemplate, NULL); + gst_pad_set_active (mysrcpad, TRUE); + gst_pad_set_active (mysinkpad, TRUE); + + return dynamic; +} + +void +cleanup_dynamic (GstElement * dynamic) +{ + GST_DEBUG ("cleanup_dynamic"); + + g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL); + g_list_free (buffers); + buffers = NULL; + + gst_pad_set_active (mysrcpad, FALSE); + gst_pad_set_active (mysinkpad, FALSE); + gst_check_teardown_src_pad (dynamic); + gst_check_teardown_sink_pad (dynamic); + gst_check_teardown_element (dynamic); +} + +GST_START_TEST (test_passthrough) +{ + GstElement *dynamic; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + gint16 in[6] = { 24576, -16384, 256, -128, 0, -24576 }; + gint16 *res; + + dynamic = setup_dynamic (); + fail_unless (gst_element_set_state (dynamic, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (12); + memcpy (GST_BUFFER_DATA (inbuffer), in, 12); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 12) == 0); + caps = gst_caps_from_string (DYNAMIC_CAPS_STRING); + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* 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); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + + res = (gint16 *) GST_BUFFER_DATA (outbuffer); + GST_INFO + ("expected %+5ld %+5ld %+5ld %+5ld %+5ld %+5ld real %+5ld %+5ld %+5ld %+5ld %+5ld %+5ld", + in[0], in[1], in[2], in[3], in[4], in[5], res[0], res[1], res[2], res[3], + res[4], res[5]); + fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), in, 12) == 0); + + /* cleanup */ + cleanup_dynamic (dynamic); +} + +GST_END_TEST; + +GST_START_TEST (test_compress_hard_50_50) +{ + GstElement *dynamic; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + gint16 in[8] = { -30000, 24576, -16384, 256, -128, 0, -24576, 30000 }; + gint16 *res; + + dynamic = setup_dynamic (); + g_object_set (G_OBJECT (dynamic), "mode", 0, NULL); + g_object_set (G_OBJECT (dynamic), "characteristics", 0, NULL); + g_object_set (G_OBJECT (dynamic), "ratio", 0.5, NULL); + g_object_set (G_OBJECT (dynamic), "threshold", 0.5, NULL); + fail_unless (gst_element_set_state (dynamic, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (16); + memcpy (GST_BUFFER_DATA (inbuffer), in, 16); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 16) == 0); + caps = gst_caps_from_string (DYNAMIC_CAPS_STRING); + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + /* ... and puts a new buffer on the global list */ + fail_unless_equals_int (g_list_length (buffers), 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + + res = (gint16 *) GST_BUFFER_DATA (outbuffer); + + fail_unless (res[0] > in[0]); + fail_unless (res[1] < in[1]); + fail_unless (res[2] == in[2]); + fail_unless (res[3] == in[3]); + fail_unless (res[4] == in[4]); + fail_unless (res[5] == in[5]); + fail_unless (res[6] > in[6]); + fail_unless (res[7] < in[7]); + + /* cleanup */ + cleanup_dynamic (dynamic); +} + +GST_END_TEST; + +GST_START_TEST (test_compress_soft_50_50) +{ + GstElement *dynamic; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + gint16 in[8] = { -30000, 24576, -16384, 256, -128, 0, -24576, 30000 }; + gint16 *res; + + dynamic = setup_dynamic (); + g_object_set (G_OBJECT (dynamic), "mode", 0, NULL); + g_object_set (G_OBJECT (dynamic), "characteristics", 1, NULL); + g_object_set (G_OBJECT (dynamic), "ratio", 0.5, NULL); + g_object_set (G_OBJECT (dynamic), "threshold", 0.5, NULL); + fail_unless (gst_element_set_state (dynamic, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (16); + memcpy (GST_BUFFER_DATA (inbuffer), in, 16); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 16) == 0); + caps = gst_caps_from_string (DYNAMIC_CAPS_STRING); + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + /* ... and puts a new buffer on the global list */ + fail_unless_equals_int (g_list_length (buffers), 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + + res = (gint16 *) GST_BUFFER_DATA (outbuffer); + + fail_unless (res[0] > in[0]); + fail_unless (res[1] < in[1]); + fail_unless (res[2] == in[2]); + fail_unless (res[3] == in[3]); + fail_unless (res[4] == in[4]); + fail_unless (res[5] == in[5]); + fail_unless (res[6] > in[6]); + fail_unless (res[7] < in[7]); + + /* cleanup */ + cleanup_dynamic (dynamic); +} + +GST_END_TEST; + +GST_START_TEST (test_compress_hard_100_50) +{ + GstElement *dynamic; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + gint16 in[8] = { -30000, 24576, -16384, 256, -128, 0, -24576, 30000 }; + gint16 *res; + + dynamic = setup_dynamic (); + g_object_set (G_OBJECT (dynamic), "mode", 0, NULL); + g_object_set (G_OBJECT (dynamic), "characteristics", 0, NULL); + g_object_set (G_OBJECT (dynamic), "ratio", 0.5, NULL); + g_object_set (G_OBJECT (dynamic), "threshold", 1.0, NULL); + fail_unless (gst_element_set_state (dynamic, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (16); + memcpy (GST_BUFFER_DATA (inbuffer), in, 16); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 16) == 0); + caps = gst_caps_from_string (DYNAMIC_CAPS_STRING); + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + /* ... and puts a new buffer on the global list */ + fail_unless_equals_int (g_list_length (buffers), 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + + res = (gint16 *) GST_BUFFER_DATA (outbuffer); + + fail_unless (res[0] == in[0]); + fail_unless (res[1] == in[1]); + fail_unless (res[2] == in[2]); + fail_unless (res[3] == in[3]); + fail_unless (res[4] == in[4]); + fail_unless (res[5] == in[5]); + fail_unless (res[6] == in[6]); + fail_unless (res[7] == in[7]); + + /* cleanup */ + cleanup_dynamic (dynamic); +} + +GST_END_TEST; + + +GST_START_TEST (test_expand_hard_50_200) +{ + GstElement *dynamic; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + gint16 in[8] = { -30000, 24576, -16383, 256, -128, 0, -24576, 30000 }; + gint16 *res; + + dynamic = setup_dynamic (); + g_object_set (G_OBJECT (dynamic), "mode", 1, NULL); + g_object_set (G_OBJECT (dynamic), "characteristics", 0, NULL); + g_object_set (G_OBJECT (dynamic), "ratio", 2.0, NULL); + g_object_set (G_OBJECT (dynamic), "threshold", 0.5, NULL); + fail_unless (gst_element_set_state (dynamic, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (16); + memcpy (GST_BUFFER_DATA (inbuffer), in, 16); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 16) == 0); + caps = gst_caps_from_string (DYNAMIC_CAPS_STRING); + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + /* ... and puts a new buffer on the global list */ + fail_unless_equals_int (g_list_length (buffers), 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + + res = (gint16 *) GST_BUFFER_DATA (outbuffer); + + fail_unless (res[0] == in[0]); + fail_unless (res[1] == in[1]); + fail_unless (res[2] > in[2]); + fail_unless (res[3] < in[3]); + fail_unless (res[4] > in[4]); + fail_unless (res[5] == in[5]); + fail_unless (res[6] == in[6]); + fail_unless (res[7] == in[7]); + + /* cleanup */ + cleanup_dynamic (dynamic); +} + +GST_END_TEST; + +GST_START_TEST (test_expand_soft_50_200) +{ + GstElement *dynamic; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + gint16 in[8] = { -30000, 24576, -16383, 256, -128, 0, -24576, 30000 }; + gint16 *res; + + dynamic = setup_dynamic (); + g_object_set (G_OBJECT (dynamic), "mode", 1, NULL); + g_object_set (G_OBJECT (dynamic), "characteristics", 1, NULL); + g_object_set (G_OBJECT (dynamic), "ratio", 2.0, NULL); + g_object_set (G_OBJECT (dynamic), "threshold", 0.5, NULL); + fail_unless (gst_element_set_state (dynamic, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (16); + memcpy (GST_BUFFER_DATA (inbuffer), in, 16); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 16) == 0); + caps = gst_caps_from_string (DYNAMIC_CAPS_STRING); + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + /* ... and puts a new buffer on the global list */ + fail_unless_equals_int (g_list_length (buffers), 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + + res = (gint16 *) GST_BUFFER_DATA (outbuffer); + + fail_unless (res[0] == in[0]); + fail_unless (res[1] == in[1]); + fail_unless (res[2] > in[2]); + fail_unless (res[3] < in[3]); + fail_unless (res[4] > in[4]); + fail_unless (res[5] == in[5]); + fail_unless (res[6] == in[6]); + fail_unless (res[7] == in[7]); + + /* cleanup */ + cleanup_dynamic (dynamic); +} + +GST_END_TEST; + +GST_START_TEST (test_expand_hard_0_200) +{ + GstElement *dynamic; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + gint16 in[8] = { -30000, 24576, -16383, 256, -128, 0, -24576, 30000 }; + gint16 *res; + + dynamic = setup_dynamic (); + g_object_set (G_OBJECT (dynamic), "mode", 1, NULL); + g_object_set (G_OBJECT (dynamic), "characteristics", 0, NULL); + g_object_set (G_OBJECT (dynamic), "ratio", 2.0, NULL); + g_object_set (G_OBJECT (dynamic), "threshold", 0.0, NULL); + fail_unless (gst_element_set_state (dynamic, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + inbuffer = gst_buffer_new_and_alloc (16); + memcpy (GST_BUFFER_DATA (inbuffer), in, 16); + fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 16) == 0); + caps = gst_caps_from_string (DYNAMIC_CAPS_STRING); + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + /* pushing gives away my reference ... */ + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + /* ... and puts a new buffer on the global list */ + fail_unless_equals_int (g_list_length (buffers), 1); + fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL); + + res = (gint16 *) GST_BUFFER_DATA (outbuffer); + + fail_unless (res[0] == in[0]); + fail_unless (res[1] == in[1]); + fail_unless (res[2] == in[2]); + fail_unless (res[3] == in[3]); + fail_unless (res[4] == in[4]); + fail_unless (res[5] == in[5]); + fail_unless (res[6] == in[6]); + fail_unless (res[7] == in[7]); + + /* cleanup */ + cleanup_dynamic (dynamic); +} + +GST_END_TEST; + +Suite * +dynamic_suite (void) +{ + Suite *s = suite_create ("dynamic"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_passthrough); + tcase_add_test (tc_chain, test_compress_hard_50_50); + tcase_add_test (tc_chain, test_compress_soft_50_50); + tcase_add_test (tc_chain, test_compress_hard_100_50); + tcase_add_test (tc_chain, test_expand_hard_50_200); + tcase_add_test (tc_chain, test_expand_soft_50_200); + tcase_add_test (tc_chain, test_expand_hard_0_200); + return s; +} + +int +main (int argc, char **argv) +{ + int nf; + + Suite *s = dynamic_suite (); + SRunner *sr = srunner_create (s); + + gst_check_init (&argc, &argv); + + srunner_run_all (sr, CK_NORMAL); + nf = srunner_ntests_failed (sr); + srunner_free (sr); + + return nf; +} -- 2.7.4