gstreamer/gst/equalizer/gstiirequalizer.c

439 lines
15 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) <2004> Benjamin Otte <otte@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <string.h>
#include <gst/gst.h>
#include <gst/audio/audio.h>
#include <gst/audio/gstaudiofilter.h>
typedef struct _GstIirEqualizer GstIirEqualizer;
typedef struct _GstIirEqualizerClass GstIirEqualizerClass;
#define GST_TYPE_IIR_EQUALIZER \
(gst_iir_equalizer_get_type())
#define GST_IIR_EQUALIZER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_IIR_EQUALIZER,GstIirEqualizer))
#define GST_IIR_EQUALIZER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_IIR_EQUALIZER,GstIirEqualizerClass))
#define GST_IS_IIR_EQUALIZER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_IIR_EQUALIZER))
#define GST_IS_IIR_EQUALIZER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_IIR_EQUALIZER))
#define LOWEST_FREQ (20.0)
#define HIGHEST_FREQ (20000.0)
typedef void (*ProcessFunc) (GstIirEqualizer * equ, guint8 * data, guint size,
guint channels);
typedef struct
{
gdouble alpha; /* IIR coefficients for outputs */
gdouble beta; /* IIR coefficients for inputs */
gdouble gamma; /* IIR coefficients for inputs */
} SecondOrderFilter;
struct _GstIirEqualizer
{
GstAudiofilter audiofilter;
/* properties */
guint freq_count;
gdouble bandwidth;
gdouble *freqs;
gdouble *values;
/* data */
SecondOrderFilter *filter;
gpointer history;
ProcessFunc process;
guint history_size;
};
struct _GstIirEqualizerClass
{
GstAudiofilterClass audiofilter_class;
};
enum
{
ARG_0,
ARG_BANDS,
ARG_BANDWIDTH,
ARG_VALUES
/* FILL ME */
};
static void gst_iir_equalizer_base_init (gpointer g_class);
static void gst_iir_equalizer_class_init (gpointer g_class,
gpointer class_data);
static void gst_iir_equalizer_init (GTypeInstance * instance, gpointer g_class);
static void gst_iir_equalizer_finalize (GObject * object);
static void gst_iir_equalizer_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_iir_equalizer_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
static void gst_iir_equalizer_setup (GstAudiofilter * iir_equalizer);
static void gst_iir_equalizer_filter_inplace (GstAudiofilter *
iir_equalizer, GstBuffer * buf);
static GstAudiofilterClass *parent_class;
GType
gst_iir_equalizer_get_type (void)
{
static GType iir_equalizer_type = 0;
if (!iir_equalizer_type) {
static const GTypeInfo iir_equalizer_info = {
sizeof (GstIirEqualizerClass),
gst_iir_equalizer_base_init,
NULL,
gst_iir_equalizer_class_init,
NULL,
gst_iir_equalizer_init,
sizeof (GstIirEqualizer),
0,
NULL,
};
iir_equalizer_type = g_type_register_static (GST_TYPE_AUDIOFILTER,
"GstIirEqualizer", &iir_equalizer_info, 0);
}
return iir_equalizer_type;
}
static void
gst_iir_equalizer_base_init (gpointer g_class)
{
Define GstElementDetails as const and also static (when defined as global) Original commit message from CVS: * ext/amrwb/gstamrwbdec.c: * ext/amrwb/gstamrwbenc.c: * ext/amrwb/gstamrwbparse.c: * ext/arts/gst_arts.c: * ext/artsd/gstartsdsink.c: * ext/audiofile/gstafparse.c: * ext/audiofile/gstafsink.c: * ext/audiofile/gstafsrc.c: * ext/audioresample/gstaudioresample.c: * ext/bz2/gstbz2dec.c: * ext/bz2/gstbz2enc.c: * ext/cdaudio/gstcdaudio.c: * ext/directfb/dfbvideosink.c: * ext/divx/gstdivxdec.c: * ext/divx/gstdivxenc.c: * ext/dts/gstdtsdec.c: (gst_dtsdec_base_init): * ext/faac/gstfaac.c: (gst_faac_base_init): * ext/faad/gstfaad.c: * ext/gsm/gstgsmdec.c: * ext/gsm/gstgsmenc.c: * ext/hermes/gsthermescolorspace.c: * ext/ivorbis/vorbisfile.c: * ext/lcs/gstcolorspace.c: * ext/libfame/gstlibfame.c: * ext/libmms/gstmms.c: (gst_mms_base_init): * ext/musepack/gstmusepackdec.c: (gst_musepackdec_base_init): * ext/musicbrainz/gsttrm.c: (gst_musicbrainz_base_init): * ext/nas/nassink.c: (gst_nassink_base_init): * ext/neon/gstneonhttpsrc.c: * ext/sdl/sdlaudiosink.c: * ext/sdl/sdlvideosink.c: * ext/shout/gstshout.c: * ext/snapshot/gstsnapshot.c: * ext/sndfile/gstsf.c: * ext/swfdec/gstswfdec.c: * ext/tarkin/gsttarkindec.c: * ext/tarkin/gsttarkinenc.c: * ext/theora/theoradec.c: * ext/wavpack/gstwavpackdec.c: (gst_wavpack_dec_base_init): * ext/wavpack/gstwavpackparse.c: (gst_wavpack_parse_base_init): * ext/xvid/gstxviddec.c: * ext/xvid/gstxvidenc.c: * gst/cdxaparse/gstcdxaparse.c: (gst_cdxa_parse_base_init): * gst/cdxaparse/gstcdxastrip.c: (gst_cdxastrip_base_init): * gst/chart/gstchart.c: * gst/colorspace/gstcolorspace.c: * gst/deinterlace/gstdeinterlace.c: * gst/equalizer/gstiirequalizer.c: (gst_iir_equalizer_base_init): * gst/festival/gstfestival.c: * gst/filter/gstbpwsinc.c: * gst/filter/gstiir.c: * gst/filter/gstlpwsinc.c: * gst/freeze/gstfreeze.c: * gst/games/gstpuzzle.c: (gst_puzzle_base_init): * gst/librfb/gstrfbsrc.c: * gst/mixmatrix/mixmatrix.c: * gst/mpeg1sys/gstmpeg1systemencode.c: * gst/mpeg1videoparse/gstmp1videoparse.c: * gst/mpeg2sub/gstmpeg2subt.c: * gst/mpegaudioparse/gstmpegaudioparse.c: * gst/multifilesink/gstmultifilesink.c: * gst/overlay/gstoverlay.c: * gst/passthrough/gstpassthrough.c: * gst/playondemand/gstplayondemand.c: * gst/qtdemux/qtdemux.c: * gst/rtjpeg/gstrtjpegdec.c: * gst/rtjpeg/gstrtjpegenc.c: * gst/smooth/gstsmooth.c: * gst/smoothwave/gstsmoothwave.c: * gst/spectrum/gstspectrum.c: * gst/speed/gstspeed.c: * gst/stereo/gststereo.c: * gst/switch/gstswitch.c: * gst/tta/gstttadec.c: (gst_tta_dec_base_init): * gst/tta/gstttaparse.c: (gst_tta_parse_base_init): * gst/vbidec/gstvbidec.c: * gst/videocrop/gstvideocrop.c: * gst/videodrop/gstvideodrop.c: * gst/virtualdub/gstxsharpen.c: * gst/xingheader/gstxingmux.c: (gst_xing_mux_base_init): * gst/y4m/gsty4mencode.c: * sys/cdrom/gstcdplayer.c: * sys/directdraw/gstdirectdrawsink.c: * sys/directsound/gstdirectsoundsink.c: * sys/glsink/glimagesink.c: * sys/qcam/gstqcamsrc.c: * sys/v4l2/gstv4l2src.c: * sys/vcd/vcdsrc.c: (gst_vcdsrc_base_init): * sys/ximagesrc/ximagesrc.c: Define GstElementDetails as const and also static (when defined as global)
2006-04-25 21:56:38 +00:00
static const GstElementDetails iir_equalizer_details =
Unify the long descriptions in the plugin details (#337263). Original commit message from CVS: Patch by: j^ <j at bootlab dot org> * ext/amrwb/gstamrwbdec.c: * ext/amrwb/gstamrwbenc.c: * ext/amrwb/gstamrwbparse.c: * ext/arts/gst_arts.c: * ext/artsd/gstartsdsink.c: * ext/audiofile/gstafparse.c: * ext/audiofile/gstafsink.c: * ext/audiofile/gstafsrc.c: * ext/cdaudio/gstcdaudio.c: * ext/directfb/dfbvideosink.c: * ext/divx/gstdivxdec.c: * ext/divx/gstdivxenc.c: * ext/dts/gstdtsdec.c: (gst_dtsdec_base_init): * ext/faac/gstfaac.c: (gst_faac_base_init): * ext/faad/gstfaad.c: * ext/gsm/gstgsmdec.c: * ext/gsm/gstgsmenc.c: * ext/hermes/gsthermescolorspace.c: * ext/ivorbis/vorbisfile.c: * ext/lcs/gstcolorspace.c: * ext/libfame/gstlibfame.c: * ext/libmms/gstmms.c: (gst_mms_base_init): * ext/musicbrainz/gsttrm.c: (gst_musicbrainz_base_init): * ext/nas/nassink.c: (gst_nassink_base_init): * ext/neon/gstneonhttpsrc.c: * ext/polyp/polypsink.c: (gst_polypsink_base_init): * ext/sdl/sdlaudiosink.c: * ext/sdl/sdlvideosink.c: * ext/shout/gstshout.c: * ext/snapshot/gstsnapshot.c: * ext/sndfile/gstsf.c: * ext/tarkin/gsttarkindec.c: * ext/tarkin/gsttarkinenc.c: * ext/theora/theoradec.c: * ext/wavpack/gstwavpackdec.c: (gst_wavpack_dec_base_init): * ext/wavpack/gstwavpackparse.c: (gst_wavpack_parse_base_init): * ext/xvid/gstxviddec.c: * ext/xvid/gstxvidenc.c: * gst/cdxaparse/gstcdxaparse.c: (gst_cdxa_parse_base_init): * gst/cdxaparse/gstcdxastrip.c: (gst_cdxastrip_base_init): * gst/chart/gstchart.c: * gst/equalizer/gstiirequalizer.c: (gst_iir_equalizer_base_init): * gst/festival/gstfestival.c: * gst/filter/gstiir.c: * gst/filter/gstlpwsinc.c: * gst/freeze/gstfreeze.c: * gst/games/gstpuzzle.c: (gst_puzzle_base_init): * gst/mixmatrix/mixmatrix.c: * gst/mpeg1sys/gstmpeg1systemencode.c: * gst/mpeg1videoparse/gstmp1videoparse.c: * gst/mpeg2sub/gstmpeg2subt.c: * gst/mpegaudioparse/gstmpegaudioparse.c: * gst/multifilesink/gstmultifilesink.c: * gst/overlay/gstoverlay.c: * gst/passthrough/gstpassthrough.c: * gst/playondemand/gstplayondemand.c: * gst/qtdemux/qtdemux.c: * gst/rtjpeg/gstrtjpegdec.c: * gst/rtjpeg/gstrtjpegenc.c: * gst/smooth/gstsmooth.c: * gst/tta/gstttadec.c: (gst_tta_dec_base_init): * gst/tta/gstttaparse.c: (gst_tta_parse_base_init): * gst/videocrop/gstvideocrop.c: * gst/videodrop/gstvideodrop.c: * gst/virtualdub/gstxsharpen.c: * gst/xingheader/gstxingmux.c: (gst_xing_mux_base_init): * gst/y4m/gsty4mencode.c: Unify the long descriptions in the plugin details (#337263).
2006-04-06 11:35:26 +00:00
GST_ELEMENT_DETAILS ("Equalizer",
"Filter/Effect/Audio",
"Direct Form IIR equalizer",
"Benjamin Otte <otte@gnome.org>");
GstIirEqualizerClass *klass = (GstIirEqualizerClass *) g_class;
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstCaps *caps;
gst_element_class_set_details (element_class, &iir_equalizer_details);
caps = gst_caps_from_string ("audio/x-raw-int, depth=(int)16, width=(int)16, "
"endianness=(int)BYTE_ORDER, signed=(bool)TRUE, "
"rate=(int)[1000,MAX], channels=(int)[1,6];"
"audio/x-raw-float, width=(int)32, endianness=(int)BYTE_ORDER,"
"rate=(int)[1000,MAX], channels=(int)[1,6]");
gst_audiofilter_class_add_pad_templates (GST_AUDIOFILTER_CLASS (g_class),
caps);
gst_caps_free (caps);
}
static void
gst_iir_equalizer_class_init (gpointer g_class, gpointer class_data)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstIirEqualizerClass *klass;
GstAudiofilterClass *audiofilter_class;
klass = (GstIirEqualizerClass *) g_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
audiofilter_class = (GstAudiofilterClass *) g_class;
gobject_class->set_property = gst_iir_equalizer_set_property;
gobject_class->get_property = gst_iir_equalizer_get_property;
gobject_class->finalize = gst_iir_equalizer_finalize;
parent_class = g_type_class_peek_parent (g_class);
g_object_class_install_property (gobject_class, ARG_BANDS,
g_param_spec_uint ("bands", "bands", "number of different bands to use",
2, 64, 15, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class, ARG_BANDWIDTH,
g_param_spec_double ("bandwidth", "bandwidth",
"bandwidth calculated as distance between bands * this value", 0.1,
5.0, 1.0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
/* FIXME FIXME FIXME */
g_object_class_install_property (gobject_class, ARG_VALUES,
g_param_spec_pointer ("values", "values",
"expects a gdouble* of values to use for the bands",
G_PARAM_WRITABLE));
audiofilter_class->setup = gst_iir_equalizer_setup;
audiofilter_class->filter_inplace = gst_iir_equalizer_filter_inplace;
}
static void
gst_iir_equalizer_init (GTypeInstance * instance, gpointer g_class)
{
}
static void
gst_iir_equalizer_finalize (GObject * object)
{
GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
g_free (equ->freqs);
g_free (equ->values);
g_free (equ->filter);
g_free (equ->history);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
/* args are in the range [-1 ... 1] with 0 meaning "no action"
* convert to [-0.2 ... 1] with 0 meaning no action via the function
* f(x) = 0.25 * 5 ^ x - 0.25
*/
static gdouble
arg_to_scale (gdouble arg)
{
return 0.25 * exp (log (5) * arg) - 0.25;
}
static void
setup_filter (GstIirEqualizer * equ, SecondOrderFilter * filter, gdouble gain,
gdouble frequency)
{
gdouble q = pow (HIGHEST_FREQ / LOWEST_FREQ,
1.0 / (equ->freq_count - 1)) * equ->bandwidth;
gdouble theta = frequency * 2 * M_PI;
filter->beta = (q - theta / 2) / (2 * q + theta);
filter->gamma = (0.5 + filter->beta) * cos (theta);
filter->alpha = (0.5 - filter->beta) / 2;
filter->beta *= 2.0;
filter->alpha *= 2.0 * gain;
filter->gamma *= 2.0;
GST_INFO ("gain = %g, frequency = %g, alpha = %g, beta = %g, gamma=%g\n",
gain, frequency, filter->alpha, filter->beta, filter->gamma);
}
static void
gst_iir_equalizer_compute_frequencies (GstIirEqualizer * equ, guint band_count)
{
gdouble *old_values;
guint old_count, i;
gdouble step = pow (HIGHEST_FREQ / LOWEST_FREQ, 1.0 / (band_count - 1));
GstAudiofilter *audio = GST_AUDIOFILTER (equ);
old_count = equ->freq_count;
equ->freq_count = band_count;
old_values = equ->values;
if (old_count < band_count) {
equ->freqs = g_realloc (equ->freqs, sizeof (gdouble) * band_count);
memset (equ->freqs + sizeof (gdouble) * old_count, 0,
sizeof (gdouble) * (band_count - old_count));
equ->values = g_realloc (equ->values, sizeof (gdouble) * band_count);
memset (equ->values + sizeof (gdouble) * old_count, 0,
sizeof (gdouble) * (band_count - old_count));
equ->filter =
g_realloc (equ->filter, sizeof (SecondOrderFilter) * band_count);
memset (equ->filter + sizeof (SecondOrderFilter) * old_count, 0,
sizeof (SecondOrderFilter) * (band_count - old_count));
}
equ->history =
g_realloc (equ->history,
equ->history_size * audio->channels * band_count);
memset (equ->history, 0, equ->history_size * audio->channels * band_count);
equ->freqs[0] = LOWEST_FREQ;
for (i = 1; i < band_count; i++) {
equ->freqs[i] = equ->freqs[i - 1] * step;
}
if (audio->rate) {
guint i;
for (i = 0; i < band_count; i++) {
setup_filter (equ, &equ->filter[i], arg_to_scale (equ->values[i]),
equ->freqs[i] / audio->rate);
}
}
}
static void
gst_iir_equalizer_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
switch (prop_id) {
case ARG_BANDS:
gst_iir_equalizer_compute_frequencies (equ, g_value_get_uint (value));
break;
case ARG_BANDWIDTH:
if (g_value_get_double (value) != equ->bandwidth) {
equ->bandwidth = g_value_get_double (value);
if (GST_AUDIOFILTER (equ)->rate) {
guint i;
for (i = 0; i < equ->freq_count; i++) {
setup_filter (equ, &equ->filter[i], arg_to_scale (equ->values[i]),
equ->freqs[i] / GST_AUDIOFILTER (equ)->rate);
}
}
}
break;
case ARG_VALUES:
{
gdouble *new = g_value_get_pointer (value);
guint i;
for (i = 0; i < equ->freq_count; i++) {
if (new[i] != equ->values[i]) {
equ->values[i] = new[i];
setup_filter (equ, &equ->filter[i], arg_to_scale (new[i]),
equ->freqs[i] / GST_AUDIOFILTER (equ)->rate);
}
}
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_iir_equalizer_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstIirEqualizer *equ = GST_IIR_EQUALIZER (object);
switch (prop_id) {
case ARG_BANDS:
g_value_set_uint (value, equ->freq_count);
break;
case ARG_BANDWIDTH:
g_value_set_double (value, equ->bandwidth);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* start of code that is type specific */
#define CREATE_OPTIMIZED_FUNCTIONS(TYPE,BIG_TYPE,MIN_VAL,MAX_VAL) \
typedef struct { \
TYPE x1, x2; /* history of input values for a filter */ \
TYPE y1, y2; /* history of output values for a filter */ \
} SecondOrderHistory ## TYPE; \
\
static inline TYPE \
one_step_ ## TYPE (SecondOrderFilter *filter, \
SecondOrderHistory ## TYPE *history, TYPE input) \
{ \
/* calculate output */ \
TYPE output = filter->alpha * (input - history->x2) + \
filter->gamma * history->y1 - filter->beta * history->y2; \
/* update history */ \
history->y2 = history->y1; \
history->y1 = output; \
history->x2 = history->x1; \
history->x1 = input; \
\
return output; \
} \
\
static const guint \
history_size_ ## TYPE = sizeof (SecondOrderHistory ## TYPE); \
\
static void \
gst_iir_equ_process_ ## TYPE (GstIirEqualizer *equ, guint8 *data, \
guint size, guint channels) \
{ \
guint frames = size / channels / sizeof (TYPE); \
guint i, c, f; \
BIG_TYPE cur; \
TYPE val; \
\
for (i = 0; i < frames; i++) { \
for (c = 0; c < channels; c++) { \
SecondOrderHistory ## TYPE *history = equ->history; \
val = *((TYPE *) data); \
cur = 0; \
for (f = 0; f < equ->freq_count; f++) { \
SecondOrderFilter *filter = &equ->filter[f]; \
\
cur += one_step_ ## TYPE (filter, history, val); \
history++; \
} \
cur += val * 0.25; \
cur = CLAMP (cur, MIN_VAL, MAX_VAL); \
*((TYPE *) data) = (TYPE) cur; \
data += sizeof (TYPE); \
} \
} \
}
CREATE_OPTIMIZED_FUNCTIONS (gint16, gint, -32768, 32767);
CREATE_OPTIMIZED_FUNCTIONS (gfloat, gfloat, -1.0, 1.0);
static void
gst_iir_equalizer_filter_inplace (GstAudiofilter * filter, GstBuffer * buf)
{
GstIirEqualizer *equ = GST_IIR_EQUALIZER (filter);
equ->process (equ, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf),
filter->channels);
}
static void
gst_iir_equalizer_setup (GstAudiofilter * audio)
{
GstIirEqualizer *equ = GST_IIR_EQUALIZER (audio);
if (audio->width == 16) {
equ->history_size = history_size_gint16;
equ->process = gst_iir_equ_process_gint16;
} else if (audio->width == 32) {
equ->history_size = history_size_gfloat;
equ->process = gst_iir_equ_process_gfloat;
} else {
g_assert_not_reached ();
}
gst_iir_equalizer_compute_frequencies (equ, equ->freq_count);
}
static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_library_load ("gstaudiofilter"))
return FALSE;
return gst_element_register (plugin, "equalizer", GST_RANK_NONE,
GST_TYPE_IIR_EQUALIZER);
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"equalizer",
"GStreamer equalizers",
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)