2007-02-03 20:19:35 +00:00
|
|
|
/* GStreamer audio filter base class
|
2003-12-22 01:47:09 +00:00
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* Copyright (C) <2003> David Schleef <ds@schleef.org>
|
2007-02-03 20:19:35 +00:00
|
|
|
* Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
|
2003-12-22 01:47:09 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2003-12-22 01:47:09 +00:00
|
|
|
*/
|
|
|
|
|
2007-02-03 20:19:35 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstaudiofilter
|
2017-01-23 19:36:11 +00:00
|
|
|
* @title: GstAudioFilter
|
2007-02-03 20:19:35 +00:00
|
|
|
* @short_description: Base class for simple audio filters
|
|
|
|
*
|
2011-08-20 17:16:42 +00:00
|
|
|
* #GstAudioFilter is a #GstBaseTransform<!-- -->-derived base class for simple audio
|
2007-02-03 20:19:35 +00:00
|
|
|
* filters, ie. those that output the same format that they get as input.
|
|
|
|
*
|
|
|
|
* #GstAudioFilter will parse the input format for you (with error checking)
|
|
|
|
* before calling your setup function. Also, elements deriving from
|
|
|
|
* #GstAudioFilter may use gst_audio_filter_class_add_pad_templates() from
|
2011-04-19 12:31:20 +00:00
|
|
|
* their class_init function to easily configure the set of caps/formats that
|
2007-02-03 20:19:35 +00:00
|
|
|
* the element is able to handle.
|
|
|
|
*
|
2009-01-31 16:44:32 +00:00
|
|
|
* Derived classes should override the #GstAudioFilterClass.setup() and
|
|
|
|
* #GstBaseTransformClass.transform_ip() and/or
|
|
|
|
* #GstBaseTransformClass.transform()
|
2007-02-03 20:19:35 +00:00
|
|
|
* virtual functions in their class_init function.
|
|
|
|
*/
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2004-07-25 10:28:05 +00:00
|
|
|
#include "gstaudiofilter.h"
|
2003-12-22 01:47:09 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-02-03 20:19:35 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (audiofilter_dbg);
|
|
|
|
#define GST_CAT_DEFAULT audiofilter_dbg
|
|
|
|
|
2007-02-06 09:42:05 +00:00
|
|
|
static GstStateChangeReturn gst_audio_filter_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
2007-02-03 20:19:35 +00:00
|
|
|
static gboolean gst_audio_filter_set_caps (GstBaseTransform * btrans,
|
|
|
|
GstCaps * incaps, GstCaps * outcaps);
|
2008-01-02 12:09:48 +00:00
|
|
|
static gboolean gst_audio_filter_get_unit_size (GstBaseTransform * btrans,
|
2011-03-27 11:55:15 +00:00
|
|
|
GstCaps * caps, gsize * size);
|
2015-10-24 17:02:13 +00:00
|
|
|
static GstFlowReturn gst_audio_filter_submit_input_buffer (GstBaseTransform *
|
|
|
|
btrans, gboolean is_discont, GstBuffer * input);
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2009-11-26 09:30:25 +00:00
|
|
|
#define do_init G_STMT_START { \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (audiofilter_dbg, "audiofilter", 0, "audiofilter"); \
|
|
|
|
} G_STMT_END
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2009-11-26 09:30:25 +00:00
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstAudioFilter, gst_audio_filter,
|
|
|
|
GST_TYPE_BASE_TRANSFORM, do_init);
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2015-06-04 17:00:45 +00:00
|
|
|
static gboolean
|
|
|
|
gst_audio_filter_transform_meta (GstBaseTransform * trans, GstBuffer * inbuf,
|
|
|
|
GstMeta * meta, GstBuffer * outbuf)
|
|
|
|
{
|
|
|
|
const GstMetaInfo *info = meta->info;
|
|
|
|
const gchar *const *tags;
|
|
|
|
|
|
|
|
tags = gst_meta_api_type_get_tags (info->api);
|
|
|
|
|
2015-06-29 11:06:33 +00:00
|
|
|
if (!tags || (g_strv_length ((gchar **) tags) == 1
|
|
|
|
&& gst_meta_api_type_has_tag (info->api,
|
|
|
|
g_quark_from_string (GST_META_TAG_AUDIO_STR))))
|
2015-06-04 17:00:45 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return
|
|
|
|
GST_BASE_TRANSFORM_CLASS (gst_audio_filter_parent_class)->transform_meta
|
|
|
|
(trans, inbuf, meta, outbuf);
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static void
|
2009-11-26 09:30:25 +00:00
|
|
|
gst_audio_filter_class_init (GstAudioFilterClass * klass)
|
2003-12-22 01:47:09 +00:00
|
|
|
{
|
2009-11-26 09:30:25 +00:00
|
|
|
GstBaseTransformClass *basetrans_class = (GstBaseTransformClass *) klass;
|
|
|
|
GstElementClass *gstelement_class = (GstElementClass *) klass;
|
2004-01-16 00:55:29 +00:00
|
|
|
|
2007-02-06 09:42:05 +00:00
|
|
|
gstelement_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_filter_change_state);
|
2007-02-03 20:19:35 +00:00
|
|
|
basetrans_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_filter_set_caps);
|
2008-01-02 12:09:48 +00:00
|
|
|
basetrans_class->get_unit_size =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_audio_filter_get_unit_size);
|
2015-06-04 17:00:45 +00:00
|
|
|
basetrans_class->transform_meta = gst_audio_filter_transform_meta;
|
2015-10-24 17:02:13 +00:00
|
|
|
basetrans_class->submit_input_buffer = gst_audio_filter_submit_input_buffer;
|
2003-12-22 01:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-26 09:30:25 +00:00
|
|
|
gst_audio_filter_init (GstAudioFilter * self)
|
2003-12-22 01:47:09 +00:00
|
|
|
{
|
2011-10-28 09:13:52 +00:00
|
|
|
gst_audio_info_init (&self->info);
|
2007-02-06 09:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we override the state change vfunc here instead of GstBaseTransform's stop
|
|
|
|
* vfunc, so GstAudioFilter-derived elements can override ::stop() for their
|
|
|
|
* own purposes without having to worry about chaining up */
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_audio_filter_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstStateChangeReturn ret;
|
2009-11-26 09:30:25 +00:00
|
|
|
GstAudioFilter *filter = GST_AUDIO_FILTER (element);
|
2007-02-03 20:19:35 +00:00
|
|
|
|
2009-11-26 09:30:25 +00:00
|
|
|
ret =
|
|
|
|
GST_ELEMENT_CLASS (gst_audio_filter_parent_class)->change_state (element,
|
|
|
|
transition);
|
2007-02-06 09:42:05 +00:00
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
2011-08-18 17:15:03 +00:00
|
|
|
gst_audio_info_init (&filter->info);
|
2007-02-06 09:42:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2003-12-22 01:47:09 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 20:19:35 +00:00
|
|
|
static gboolean
|
|
|
|
gst_audio_filter_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
|
|
|
|
GstCaps * outcaps)
|
2003-12-22 01:47:09 +00:00
|
|
|
{
|
2007-02-03 20:19:35 +00:00
|
|
|
GstAudioFilterClass *klass;
|
2009-11-26 09:30:25 +00:00
|
|
|
GstAudioFilter *filter = GST_AUDIO_FILTER (btrans);
|
2012-02-06 12:22:14 +00:00
|
|
|
GstAudioInfo info;
|
2007-02-03 20:19:35 +00:00
|
|
|
gboolean ret = TRUE;
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2007-02-03 20:19:35 +00:00
|
|
|
GST_LOG_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
|
2012-02-06 12:22:14 +00:00
|
|
|
GST_LOG_OBJECT (filter, "info: %d", GST_AUDIO_FILTER_RATE (filter));
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2012-02-06 12:22:14 +00:00
|
|
|
if (!gst_audio_info_from_caps (&info, incaps))
|
2011-08-18 17:15:03 +00:00
|
|
|
goto invalid_format;
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2012-02-06 12:22:14 +00:00
|
|
|
klass = GST_AUDIO_FILTER_GET_CLASS (filter);
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2007-02-03 20:19:35 +00:00
|
|
|
if (klass->setup)
|
2012-02-06 12:22:14 +00:00
|
|
|
ret = klass->setup (filter, &info);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
filter->info = info;
|
|
|
|
GST_LOG_OBJECT (filter, "configured caps: %" GST_PTR_FORMAT, incaps);
|
|
|
|
}
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2007-02-03 20:19:35 +00:00
|
|
|
return ret;
|
2011-08-18 17:15:03 +00:00
|
|
|
|
|
|
|
/* ERROR */
|
|
|
|
invalid_format:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (filter, "couldn't parse %" GST_PTR_FORMAT, incaps);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-12-22 01:47:09 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 17:02:13 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_audio_filter_submit_input_buffer (GstBaseTransform * btrans,
|
|
|
|
gboolean is_discont, GstBuffer * input)
|
|
|
|
{
|
|
|
|
GstAudioFilter *filter = GST_AUDIO_FILTER (btrans);
|
|
|
|
|
|
|
|
if (btrans->segment.format == GST_FORMAT_TIME) {
|
|
|
|
input =
|
|
|
|
gst_audio_buffer_clip (input, &btrans->segment, filter->info.rate,
|
|
|
|
filter->info.bpf);
|
|
|
|
|
|
|
|
if (!input)
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
GST_BASE_TRANSFORM_CLASS
|
|
|
|
(gst_audio_filter_parent_class)->submit_input_buffer (btrans, is_discont,
|
|
|
|
input);
|
|
|
|
}
|
|
|
|
|
2008-01-02 12:09:48 +00:00
|
|
|
static gboolean
|
|
|
|
gst_audio_filter_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
|
2011-03-27 11:55:15 +00:00
|
|
|
gsize * size)
|
2008-01-02 12:09:48 +00:00
|
|
|
{
|
2011-10-28 08:44:38 +00:00
|
|
|
GstAudioInfo info;
|
2008-01-02 12:09:48 +00:00
|
|
|
|
2011-10-28 08:44:38 +00:00
|
|
|
if (!gst_audio_info_from_caps (&info, caps))
|
|
|
|
return FALSE;
|
2008-01-02 12:09:48 +00:00
|
|
|
|
2011-10-28 09:37:31 +00:00
|
|
|
*size = GST_AUDIO_INFO_BPF (&info);
|
2008-01-02 12:09:48 +00:00
|
|
|
|
2011-10-28 08:44:38 +00:00
|
|
|
return TRUE;
|
2008-01-02 12:09:48 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 20:19:35 +00:00
|
|
|
/**
|
|
|
|
* gst_audio_filter_class_add_pad_templates:
|
|
|
|
* @klass: an #GstAudioFilterClass
|
|
|
|
* @allowed_caps: what formats the filter can handle, as #GstCaps
|
|
|
|
*
|
|
|
|
* Convenience function to add pad templates to this element class, with
|
|
|
|
* @allowed_caps as the caps that can be handled.
|
|
|
|
*
|
2011-04-19 12:31:20 +00:00
|
|
|
* This function is usually used from within a GObject class_init function.
|
2007-02-03 20:19:35 +00:00
|
|
|
*/
|
2004-03-14 22:34:34 +00:00
|
|
|
void
|
2007-02-03 20:19:35 +00:00
|
|
|
gst_audio_filter_class_add_pad_templates (GstAudioFilterClass * klass,
|
2011-05-17 10:29:24 +00:00
|
|
|
GstCaps * allowed_caps)
|
2003-12-22 01:47:09 +00:00
|
|
|
{
|
2007-02-03 20:19:35 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2009-04-07 19:38:29 +00:00
|
|
|
GstPadTemplate *pad_template;
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2007-02-03 20:19:35 +00:00
|
|
|
g_return_if_fail (GST_IS_AUDIO_FILTER_CLASS (klass));
|
|
|
|
g_return_if_fail (GST_IS_CAPS (allowed_caps));
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2009-04-07 19:38:29 +00:00
|
|
|
pad_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
2011-05-17 10:29:24 +00:00
|
|
|
allowed_caps);
|
2009-04-07 19:38:29 +00:00
|
|
|
gst_element_class_add_pad_template (element_class, pad_template);
|
2003-12-22 01:47:09 +00:00
|
|
|
|
2009-04-07 19:38:29 +00:00
|
|
|
pad_template = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
2011-05-17 10:29:24 +00:00
|
|
|
allowed_caps);
|
2009-04-07 19:38:29 +00:00
|
|
|
gst_element_class_add_pad_template (element_class, pad_template);
|
2003-12-22 01:47:09 +00:00
|
|
|
}
|