David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 1 | /* GStreamer |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 2 | * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu> |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003,2004 David A. Schleef <ds@schleef.org> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Library General Public |
| 16 | * License along with this library; if not, write to the |
| 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 | * Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | /* Element-Checklist-Version: 5 */ |
| 21 | |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 22 | /** |
Sebastian Dröge | 6ff7689 | 2008-11-28 09:49:56 +0000 | [diff] [blame] | 23 | * SECTION:element-legacyresample |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 24 | * |
Sebastian Dröge | 6ff7689 | 2008-11-28 09:49:56 +0000 | [diff] [blame] | 25 | * legacyresample resamples raw audio buffers to different sample rates using |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 26 | * a configurable windowing function to enhance quality. |
Stefan Kost | 9549b4e | 2008-07-10 21:06:03 +0000 | [diff] [blame] | 27 | * |
| 28 | * <refsect2> |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 29 | * <title>Example launch line</title> |
Stefan Kost | 9549b4e | 2008-07-10 21:06:03 +0000 | [diff] [blame] | 30 | * |[ |
Sebastian Dröge | 6ff7689 | 2008-11-28 09:49:56 +0000 | [diff] [blame] | 31 | * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! legacyresample ! audio/x-raw-int, rate=8000 ! alsasink |
Stefan Kost | 9549b4e | 2008-07-10 21:06:03 +0000 | [diff] [blame] | 32 | * ]| Decode an Ogg/Vorbis downsample to 8Khz and play sound through alsa. |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 33 | * To create the Ogg/Vorbis file refer to the documentation of vorbisenc. |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 34 | * </refsect2> |
| 35 | * |
| 36 | * Last reviewed on 2006-03-02 (0.10.4) |
| 37 | */ |
| 38 | |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 39 | #ifdef HAVE_CONFIG_H |
| 40 | #include "config.h" |
| 41 | #endif |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 42 | |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | #include <math.h> |
| 45 | |
| 46 | /*#define DEBUG_ENABLED */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 47 | #include "gstlegacyresample.h" |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 48 | #include <gst/audio/audio.h> |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 49 | #include <gst/base/gstbasetransform.h> |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 50 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 51 | GST_DEBUG_CATEGORY_STATIC (legacyresample_debug); |
| 52 | #define GST_CAT_DEFAULT legacyresample_debug |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 53 | |
| 54 | /* elementfactory information */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 55 | static const GstElementDetails gst_legacyresample_details = |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 56 | GST_ELEMENT_DETAILS ("Audio scaler", |
| 57 | "Filter/Converter/Audio", |
| 58 | "Resample audio", |
| 59 | "David Schleef <ds@schleef.org>"); |
| 60 | |
Thomas Vander Stichele | cefa838 | 2005-12-06 19:42:02 +0000 | [diff] [blame] | 61 | #define DEFAULT_FILTERLEN 16 |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 62 | |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 63 | enum |
| 64 | { |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 65 | PROP_0, |
| 66 | PROP_FILTERLEN |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | #define SUPPORTED_CAPS \ |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 70 | GST_STATIC_CAPS ( \ |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 71 | "audio/x-raw-int, " \ |
| 72 | "rate = (int) [ 1, MAX ], " \ |
| 73 | "channels = (int) [ 1, MAX ], " \ |
| 74 | "endianness = (int) BYTE_ORDER, " \ |
| 75 | "width = (int) 16, " \ |
| 76 | "depth = (int) 16, " \ |
Wim Taymans | c19601e | 2006-04-28 14:17:00 +0000 | [diff] [blame] | 77 | "signed = (boolean) true;" \ |
| 78 | "audio/x-raw-int, " \ |
| 79 | "rate = (int) [ 1, MAX ], " \ |
| 80 | "channels = (int) [ 1, MAX ], " \ |
| 81 | "endianness = (int) BYTE_ORDER, " \ |
| 82 | "width = (int) 32, " \ |
| 83 | "depth = (int) 32, " \ |
| 84 | "signed = (boolean) true;" \ |
| 85 | "audio/x-raw-float, " \ |
| 86 | "rate = (int) [ 1, MAX ], " \ |
| 87 | "channels = (int) [ 1, MAX ], " \ |
| 88 | "endianness = (int) BYTE_ORDER, " \ |
| 89 | "width = (int) 32; " \ |
| 90 | "audio/x-raw-float, " \ |
| 91 | "rate = (int) [ 1, MAX ], " \ |
| 92 | "channels = (int) [ 1, MAX ], " \ |
| 93 | "endianness = (int) BYTE_ORDER, " \ |
| 94 | "width = (int) 64" \ |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 95 | ) |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 96 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 97 | static GstStaticPadTemplate gst_legacyresample_sink_template = |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 98 | GST_STATIC_PAD_TEMPLATE ("sink", |
| 99 | GST_PAD_SINK, GST_PAD_ALWAYS, SUPPORTED_CAPS); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 100 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 101 | static GstStaticPadTemplate gst_legacyresample_src_template = |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 102 | GST_STATIC_PAD_TEMPLATE ("src", |
| 103 | GST_PAD_SRC, GST_PAD_ALWAYS, SUPPORTED_CAPS); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 104 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 105 | static void gst_legacyresample_set_property (GObject * object, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 106 | guint prop_id, const GValue * value, GParamSpec * pspec); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 107 | static void gst_legacyresample_get_property (GObject * object, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 108 | guint prop_id, GValue * value, GParamSpec * pspec); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 109 | |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 110 | /* vmethods */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 111 | static gboolean legacyresample_get_unit_size (GstBaseTransform * base, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 112 | GstCaps * caps, guint * size); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 113 | static GstCaps *legacyresample_transform_caps (GstBaseTransform * base, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 114 | GstPadDirection direction, GstCaps * caps); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 115 | static void legacyresample_fixate_caps (GstBaseTransform * base, |
Sebastian Dröge | e449388 | 2008-10-28 16:25:00 +0000 | [diff] [blame] | 116 | GstPadDirection direction, GstCaps * caps, GstCaps * othercaps); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 117 | static gboolean legacyresample_transform_size (GstBaseTransform * trans, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 118 | GstPadDirection direction, GstCaps * incaps, guint insize, |
| 119 | GstCaps * outcaps, guint * outsize); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 120 | static gboolean legacyresample_set_caps (GstBaseTransform * base, |
Tim-Philipp Müller | 86d6bc0 | 2007-04-21 14:14:24 +0000 | [diff] [blame] | 121 | GstCaps * incaps, GstCaps * outcaps); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 122 | static GstFlowReturn legacyresample_pushthrough (GstLegacyresample * |
| 123 | legacyresample); |
| 124 | static GstFlowReturn legacyresample_transform (GstBaseTransform * base, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 125 | GstBuffer * inbuf, GstBuffer * outbuf); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 126 | static gboolean legacyresample_event (GstBaseTransform * base, |
| 127 | GstEvent * event); |
| 128 | static gboolean legacyresample_start (GstBaseTransform * base); |
| 129 | static gboolean legacyresample_stop (GstBaseTransform * base); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 130 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 131 | static gboolean legacyresample_query (GstPad * pad, GstQuery * query); |
| 132 | static const GstQueryType *legacyresample_query_type (GstPad * pad); |
Sebastian Dröge | c7cc386 | 2007-11-23 10:21:11 +0000 | [diff] [blame] | 133 | |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 134 | #define DEBUG_INIT(bla) \ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 135 | GST_DEBUG_CATEGORY_INIT (legacyresample_debug, "legacyresample", 0, "audio resampling element"); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 136 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 137 | GST_BOILERPLATE_FULL (GstLegacyresample, gst_legacyresample, GstBaseTransform, |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 138 | GST_TYPE_BASE_TRANSFORM, DEBUG_INIT); |
| 139 | |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 140 | static void |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 141 | gst_legacyresample_base_init (gpointer g_class) |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 142 | { |
| 143 | GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 144 | |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 145 | gst_element_class_add_pad_template (gstelement_class, |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 146 | gst_static_pad_template_get (&gst_legacyresample_src_template)); |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 147 | gst_element_class_add_pad_template (gstelement_class, |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 148 | gst_static_pad_template_get (&gst_legacyresample_sink_template)); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 149 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 150 | gst_element_class_set_details (gstelement_class, &gst_legacyresample_details); |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 151 | } |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 152 | |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 153 | static void |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 154 | gst_legacyresample_class_init (GstLegacyresampleClass * klass) |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 155 | { |
| 156 | GObjectClass *gobject_class; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 157 | |
| 158 | gobject_class = (GObjectClass *) klass; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 159 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 160 | gobject_class->set_property = gst_legacyresample_set_property; |
| 161 | gobject_class->get_property = gst_legacyresample_get_property; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 162 | |
Cody Russell | ea0de11 | 2006-06-22 10:10:51 +0000 | [diff] [blame] | 163 | g_object_class_install_property (gobject_class, PROP_FILTERLEN, |
Sebastian Dröge | 180aee5 | 2008-03-22 15:00:47 +0000 | [diff] [blame] | 164 | g_param_spec_int ("filter-length", "filter length", |
| 165 | "Length of the resample filter", 0, G_MAXINT, DEFAULT_FILTERLEN, |
| 166 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 167 | |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 168 | GST_BASE_TRANSFORM_CLASS (klass)->start = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 169 | GST_DEBUG_FUNCPTR (legacyresample_start); |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 170 | GST_BASE_TRANSFORM_CLASS (klass)->stop = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 171 | GST_DEBUG_FUNCPTR (legacyresample_stop); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 172 | GST_BASE_TRANSFORM_CLASS (klass)->transform_size = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 173 | GST_DEBUG_FUNCPTR (legacyresample_transform_size); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 174 | GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 175 | GST_DEBUG_FUNCPTR (legacyresample_get_unit_size); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 176 | GST_BASE_TRANSFORM_CLASS (klass)->transform_caps = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 177 | GST_DEBUG_FUNCPTR (legacyresample_transform_caps); |
Sebastian Dröge | e449388 | 2008-10-28 16:25:00 +0000 | [diff] [blame] | 178 | GST_BASE_TRANSFORM_CLASS (klass)->fixate_caps = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 179 | GST_DEBUG_FUNCPTR (legacyresample_fixate_caps); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 180 | GST_BASE_TRANSFORM_CLASS (klass)->set_caps = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 181 | GST_DEBUG_FUNCPTR (legacyresample_set_caps); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 182 | GST_BASE_TRANSFORM_CLASS (klass)->transform = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 183 | GST_DEBUG_FUNCPTR (legacyresample_transform); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 184 | GST_BASE_TRANSFORM_CLASS (klass)->event = |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 185 | GST_DEBUG_FUNCPTR (legacyresample_event); |
Jan Schmidt | 37fb8d0 | 2005-09-09 17:53:47 +0000 | [diff] [blame] | 186 | |
| 187 | GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 190 | static void |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 191 | gst_legacyresample_init (GstLegacyresample * legacyresample, |
| 192 | GstLegacyresampleClass * klass) |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 193 | { |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 194 | GstBaseTransform *trans; |
| 195 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 196 | trans = GST_BASE_TRANSFORM (legacyresample); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 197 | |
Tim-Philipp Müller | 8ed88a3 | 2008-05-14 13:57:41 +0000 | [diff] [blame] | 198 | /* buffer alloc passthrough is too impossible. FIXME, it |
| 199 | * is trivial in the passthrough case. */ |
| 200 | gst_pad_set_bufferalloc_function (trans->sinkpad, NULL); |
| 201 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 202 | legacyresample->filter_length = DEFAULT_FILTERLEN; |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 203 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 204 | legacyresample->need_discont = FALSE; |
Sebastian Dröge | c7cc386 | 2007-11-23 10:21:11 +0000 | [diff] [blame] | 205 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 206 | gst_pad_set_query_function (trans->srcpad, legacyresample_query); |
| 207 | gst_pad_set_query_type_function (trans->srcpad, legacyresample_query_type); |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* vmethods */ |
| 211 | static gboolean |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 212 | legacyresample_start (GstBaseTransform * base) |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 213 | { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 214 | GstLegacyresample *legacyresample = GST_LEGACYRESAMPLE (base); |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 215 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 216 | legacyresample->resample = resample_new (); |
| 217 | legacyresample->ts_offset = -1; |
| 218 | legacyresample->offset = -1; |
| 219 | legacyresample->next_ts = -1; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 220 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 221 | resample_set_filter_length (legacyresample->resample, |
| 222 | legacyresample->filter_length); |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 223 | |
| 224 | return TRUE; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 227 | static gboolean |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 228 | legacyresample_stop (GstBaseTransform * base) |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 229 | { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 230 | GstLegacyresample *legacyresample = GST_LEGACYRESAMPLE (base); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 231 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 232 | if (legacyresample->resample) { |
| 233 | resample_free (legacyresample->resample); |
| 234 | legacyresample->resample = NULL; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 237 | gst_caps_replace (&legacyresample->sinkcaps, NULL); |
| 238 | gst_caps_replace (&legacyresample->srccaps, NULL); |
Jan Schmidt | ec5ed04 | 2006-07-28 17:17:24 +0000 | [diff] [blame] | 239 | |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 240 | return TRUE; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Tim-Philipp Müller | 86d6bc0 | 2007-04-21 14:14:24 +0000 | [diff] [blame] | 243 | static gboolean |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 244 | legacyresample_get_unit_size (GstBaseTransform * base, GstCaps * caps, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 245 | guint * size) |
| 246 | { |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 247 | gint width, channels; |
| 248 | GstStructure *structure; |
| 249 | gboolean ret; |
| 250 | |
Stefan Kost | f59ca21 | 2006-08-20 13:05:43 +0000 | [diff] [blame] | 251 | g_assert (size); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 252 | |
| 253 | /* this works for both float and int */ |
| 254 | structure = gst_caps_get_structure (caps, 0); |
| 255 | ret = gst_structure_get_int (structure, "width", &width); |
| 256 | ret &= gst_structure_get_int (structure, "channels", &channels); |
| 257 | g_return_val_if_fail (ret, FALSE); |
| 258 | |
| 259 | *size = width * channels / 8; |
| 260 | |
| 261 | return TRUE; |
| 262 | } |
| 263 | |
Tim-Philipp Müller | 86d6bc0 | 2007-04-21 14:14:24 +0000 | [diff] [blame] | 264 | static GstCaps * |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 265 | legacyresample_transform_caps (GstBaseTransform * base, |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 266 | GstPadDirection direction, GstCaps * caps) |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 267 | { |
Tim-Philipp Müller | fffc3ba | 2009-04-01 15:45:22 +0100 | [diff] [blame] | 268 | const GValue *val; |
| 269 | GstStructure *s; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 270 | GstCaps *res; |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 271 | |
Tim-Philipp Müller | fffc3ba | 2009-04-01 15:45:22 +0100 | [diff] [blame] | 272 | /* transform single caps into input_caps + input_caps with the rate |
| 273 | * field set to our supported range. This ensures that upstream knows |
| 274 | * about downstream's prefered rate(s) and can negotiate accordingly. */ |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 275 | res = gst_caps_copy (caps); |
Tim-Philipp Müller | fffc3ba | 2009-04-01 15:45:22 +0100 | [diff] [blame] | 276 | |
| 277 | /* first, however, check if the caps contain a range for the rate field, in |
| 278 | * which case that side isn't going to care much about the exact sample rate |
| 279 | * chosen and we should just assume things will get fixated to something sane |
| 280 | * and we may just as well offer our full range instead of the range in the |
| 281 | * caps. If the rate is not an int range value, it's likely to express a |
| 282 | * real preference or limitation and we should maintain that structure as |
| 283 | * preference by putting it first into the transformed caps, and only add |
| 284 | * our full rate range as second option */ |
| 285 | s = gst_caps_get_structure (res, 0); |
| 286 | val = gst_structure_get_value (s, "rate"); |
| 287 | if (val == NULL || GST_VALUE_HOLDS_INT_RANGE (val)) { |
| 288 | /* overwrite existing range, or add field if it doesn't exist yet */ |
| 289 | gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL); |
| 290 | } else { |
| 291 | /* append caps with full range to existing caps with non-range rate field */ |
| 292 | s = gst_structure_copy (s); |
| 293 | gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL); |
| 294 | gst_caps_append_structure (res, s); |
| 295 | } |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 296 | |
| 297 | return res; |
| 298 | } |
| 299 | |
Sebastian Dröge | e449388 | 2008-10-28 16:25:00 +0000 | [diff] [blame] | 300 | /* Fixate rate to the allowed rate that has the smallest difference */ |
| 301 | static void |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 302 | legacyresample_fixate_caps (GstBaseTransform * base, |
Sebastian Dröge | e449388 | 2008-10-28 16:25:00 +0000 | [diff] [blame] | 303 | GstPadDirection direction, GstCaps * caps, GstCaps * othercaps) |
| 304 | { |
| 305 | GstStructure *s; |
| 306 | gint rate; |
| 307 | |
| 308 | s = gst_caps_get_structure (caps, 0); |
| 309 | if (!gst_structure_get_int (s, "rate", &rate)) |
| 310 | return; |
| 311 | |
| 312 | s = gst_caps_get_structure (othercaps, 0); |
| 313 | gst_structure_fixate_field_nearest_int (s, "rate", rate); |
| 314 | } |
| 315 | |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 316 | static gboolean |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 317 | resample_set_state_from_caps (ResampleState * state, GstCaps * incaps, |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 318 | GstCaps * outcaps, gint * channels, gint * inrate, gint * outrate) |
| 319 | { |
| 320 | GstStructure *structure; |
| 321 | gboolean ret; |
| 322 | gint myinrate, myoutrate; |
| 323 | int mychannels; |
Wim Taymans | c19601e | 2006-04-28 14:17:00 +0000 | [diff] [blame] | 324 | gint width, depth; |
| 325 | ResampleFormat format; |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 326 | |
| 327 | GST_DEBUG ("incaps %" GST_PTR_FORMAT ", outcaps %" |
| 328 | GST_PTR_FORMAT, incaps, outcaps); |
| 329 | |
| 330 | structure = gst_caps_get_structure (incaps, 0); |
| 331 | |
Wim Taymans | c19601e | 2006-04-28 14:17:00 +0000 | [diff] [blame] | 332 | /* get width */ |
| 333 | ret = gst_structure_get_int (structure, "width", &width); |
| 334 | if (!ret) |
| 335 | goto no_width; |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 336 | |
Wim Taymans | c19601e | 2006-04-28 14:17:00 +0000 | [diff] [blame] | 337 | /* figure out the format */ |
| 338 | if (g_str_equal (gst_structure_get_name (structure), "audio/x-raw-float")) { |
| 339 | if (width == 32) |
| 340 | format = RESAMPLE_FORMAT_F32; |
| 341 | else if (width == 64) |
| 342 | format = RESAMPLE_FORMAT_F64; |
| 343 | else |
| 344 | goto wrong_depth; |
| 345 | } else { |
| 346 | /* for int, depth and width must be the same */ |
| 347 | ret = gst_structure_get_int (structure, "depth", &depth); |
| 348 | if (!ret || width != depth) |
| 349 | goto not_equal; |
| 350 | |
| 351 | if (width == 16) |
| 352 | format = RESAMPLE_FORMAT_S16; |
| 353 | else if (width == 32) |
| 354 | format = RESAMPLE_FORMAT_S32; |
| 355 | else |
| 356 | goto wrong_depth; |
| 357 | } |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 358 | ret = gst_structure_get_int (structure, "rate", &myinrate); |
| 359 | ret &= gst_structure_get_int (structure, "channels", &mychannels); |
Wim Taymans | c19601e | 2006-04-28 14:17:00 +0000 | [diff] [blame] | 360 | if (!ret) |
| 361 | goto no_in_rate_channels; |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 362 | |
| 363 | structure = gst_caps_get_structure (outcaps, 0); |
| 364 | ret = gst_structure_get_int (structure, "rate", &myoutrate); |
Wim Taymans | c19601e | 2006-04-28 14:17:00 +0000 | [diff] [blame] | 365 | if (!ret) |
| 366 | goto no_out_rate; |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 367 | |
| 368 | if (channels) |
| 369 | *channels = mychannels; |
| 370 | if (inrate) |
| 371 | *inrate = myinrate; |
| 372 | if (outrate) |
| 373 | *outrate = myoutrate; |
| 374 | |
Wim Taymans | c19601e | 2006-04-28 14:17:00 +0000 | [diff] [blame] | 375 | resample_set_format (state, format); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 376 | resample_set_n_channels (state, mychannels); |
| 377 | resample_set_input_rate (state, myinrate); |
| 378 | resample_set_output_rate (state, myoutrate); |
| 379 | |
| 380 | return TRUE; |
Wim Taymans | c19601e | 2006-04-28 14:17:00 +0000 | [diff] [blame] | 381 | |
| 382 | /* ERRORS */ |
| 383 | no_width: |
| 384 | { |
| 385 | GST_DEBUG ("failed to get width from caps"); |
| 386 | return FALSE; |
| 387 | } |
| 388 | not_equal: |
| 389 | { |
| 390 | GST_DEBUG ("width %d and depth %d must be the same", width, depth); |
| 391 | return FALSE; |
| 392 | } |
| 393 | wrong_depth: |
| 394 | { |
| 395 | GST_DEBUG ("unknown depth %d found", depth); |
| 396 | return FALSE; |
| 397 | } |
| 398 | no_in_rate_channels: |
| 399 | { |
| 400 | GST_DEBUG ("could not get input rate and channels"); |
| 401 | return FALSE; |
| 402 | } |
| 403 | no_out_rate: |
| 404 | { |
| 405 | GST_DEBUG ("could not get output rate"); |
| 406 | return FALSE; |
| 407 | } |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Tim-Philipp Müller | 86d6bc0 | 2007-04-21 14:14:24 +0000 | [diff] [blame] | 410 | static gboolean |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 411 | legacyresample_transform_size (GstBaseTransform * base, |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 412 | GstPadDirection direction, GstCaps * caps, guint size, GstCaps * othercaps, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 413 | guint * othersize) |
| 414 | { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 415 | GstLegacyresample *legacyresample = GST_LEGACYRESAMPLE (base); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 416 | ResampleState *state; |
| 417 | GstCaps *srccaps, *sinkcaps; |
| 418 | gboolean use_internal = FALSE; /* whether we use the internal state */ |
| 419 | gboolean ret = TRUE; |
| 420 | |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 421 | GST_LOG_OBJECT (base, "asked to transform size %d in direction %s", |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 422 | size, direction == GST_PAD_SINK ? "SINK" : "SRC"); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 423 | if (direction == GST_PAD_SINK) { |
| 424 | sinkcaps = caps; |
| 425 | srccaps = othercaps; |
| 426 | } else { |
| 427 | sinkcaps = othercaps; |
| 428 | srccaps = caps; |
| 429 | } |
| 430 | |
| 431 | /* if the caps are the ones that _set_caps got called with; we can use |
| 432 | * our own state; otherwise we'll have to create a state */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 433 | if (gst_caps_is_equal (sinkcaps, legacyresample->sinkcaps) && |
| 434 | gst_caps_is_equal (srccaps, legacyresample->srccaps)) { |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 435 | use_internal = TRUE; |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 436 | state = legacyresample->resample; |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 437 | } else { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 438 | GST_DEBUG_OBJECT (legacyresample, |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 439 | "caps are not the set caps, creating state"); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 440 | state = resample_new (); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 441 | resample_set_filter_length (state, legacyresample->filter_length); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 442 | resample_set_state_from_caps (state, sinkcaps, srccaps, NULL, NULL, NULL); |
| 443 | } |
| 444 | |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 445 | if (direction == GST_PAD_SINK) { |
| 446 | /* asked to convert size of an incoming buffer */ |
| 447 | *othersize = resample_get_output_size_for_input (state, size); |
| 448 | } else { |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 449 | /* asked to convert size of an outgoing buffer */ |
| 450 | *othersize = resample_get_input_size_for_output (state, size); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 451 | } |
Thomas Vander Stichele | fb39749 | 2005-08-26 17:35:28 +0000 | [diff] [blame] | 452 | g_assert (*othersize % state->sample_size == 0); |
| 453 | |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 454 | /* we make room for one extra sample, given that the resampling filter |
| 455 | * can output an extra one for non-integral i_rate/o_rate */ |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 456 | GST_LOG_OBJECT (base, "transformed size %d to %d", size, *othersize); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 457 | |
| 458 | if (!use_internal) { |
| 459 | resample_free (state); |
| 460 | } |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | |
Tim-Philipp Müller | 86d6bc0 | 2007-04-21 14:14:24 +0000 | [diff] [blame] | 465 | static gboolean |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 466 | legacyresample_set_caps (GstBaseTransform * base, GstCaps * incaps, |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 467 | GstCaps * outcaps) |
| 468 | { |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 469 | gboolean ret; |
| 470 | gint inrate, outrate; |
| 471 | int channels; |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 472 | GstLegacyresample *legacyresample = GST_LEGACYRESAMPLE (base); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 473 | |
| 474 | GST_DEBUG_OBJECT (base, "incaps %" GST_PTR_FORMAT ", outcaps %" |
| 475 | GST_PTR_FORMAT, incaps, outcaps); |
| 476 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 477 | ret = resample_set_state_from_caps (legacyresample->resample, incaps, outcaps, |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 478 | &channels, &inrate, &outrate); |
| 479 | |
| 480 | g_return_val_if_fail (ret, FALSE); |
| 481 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 482 | legacyresample->channels = channels; |
| 483 | GST_DEBUG_OBJECT (legacyresample, "set channels to %d", channels); |
| 484 | legacyresample->i_rate = inrate; |
| 485 | GST_DEBUG_OBJECT (legacyresample, "set i_rate to %d", inrate); |
| 486 | legacyresample->o_rate = outrate; |
| 487 | GST_DEBUG_OBJECT (legacyresample, "set o_rate to %d", outrate); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 488 | |
| 489 | /* save caps so we can short-circuit in the size_transform if the caps |
| 490 | * are the same */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 491 | gst_caps_replace (&legacyresample->sinkcaps, incaps); |
| 492 | gst_caps_replace (&legacyresample->srccaps, outcaps); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 493 | |
| 494 | return TRUE; |
| 495 | } |
| 496 | |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 497 | static gboolean |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 498 | legacyresample_event (GstBaseTransform * base, GstEvent * event) |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 499 | { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 500 | GstLegacyresample *legacyresample; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 501 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 502 | legacyresample = GST_LEGACYRESAMPLE (base); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 503 | |
| 504 | switch (GST_EVENT_TYPE (event)) { |
| 505 | case GST_EVENT_FLUSH_START: |
| 506 | break; |
| 507 | case GST_EVENT_FLUSH_STOP: |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 508 | if (legacyresample->resample) |
| 509 | resample_input_flush (legacyresample->resample); |
| 510 | legacyresample->ts_offset = -1; |
| 511 | legacyresample->next_ts = -1; |
| 512 | legacyresample->offset = -1; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 513 | break; |
| 514 | case GST_EVENT_NEWSEGMENT: |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 515 | resample_input_pushthrough (legacyresample->resample); |
| 516 | legacyresample_pushthrough (legacyresample); |
| 517 | legacyresample->ts_offset = -1; |
| 518 | legacyresample->next_ts = -1; |
| 519 | legacyresample->offset = -1; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 520 | break; |
| 521 | case GST_EVENT_EOS: |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 522 | resample_input_eos (legacyresample->resample); |
| 523 | legacyresample_pushthrough (legacyresample); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 524 | break; |
| 525 | default: |
| 526 | break; |
| 527 | } |
Stefan Kost | 896ef6c | 2008-10-30 11:43:12 +0000 | [diff] [blame] | 528 | return parent_class->event (base, event); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 531 | static GstFlowReturn |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 532 | legacyresample_do_output (GstLegacyresample * legacyresample, |
| 533 | GstBuffer * outbuf) |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 534 | { |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 535 | int outsize; |
Thomas Vander Stichele | 3d95afd | 2005-08-25 15:44:58 +0000 | [diff] [blame] | 536 | int outsamples; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 537 | ResampleState *r; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 538 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 539 | r = legacyresample->resample; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 540 | |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 541 | outsize = resample_get_output_size (r); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 542 | GST_LOG_OBJECT (legacyresample, "legacyresample can give me %d bytes", |
| 543 | outsize); |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 544 | |
| 545 | /* protect against mem corruption */ |
| 546 | if (outsize > GST_BUFFER_SIZE (outbuf)) { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 547 | GST_WARNING_OBJECT (legacyresample, |
| 548 | "overriding legacyresample's outsize %d with outbuffer's size %d", |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 549 | outsize, GST_BUFFER_SIZE (outbuf)); |
| 550 | outsize = GST_BUFFER_SIZE (outbuf); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 551 | } |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 552 | /* catch possibly wrong size differences */ |
| 553 | if (GST_BUFFER_SIZE (outbuf) - outsize > r->sample_size) { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 554 | GST_WARNING_OBJECT (legacyresample, |
| 555 | "legacyresample's outsize %d too far from outbuffer's size %d", |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 556 | outsize, GST_BUFFER_SIZE (outbuf)); |
| 557 | } |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 558 | |
| 559 | outsize = resample_get_output_data (r, GST_BUFFER_DATA (outbuf), outsize); |
Thomas Vander Stichele | 3d95afd | 2005-08-25 15:44:58 +0000 | [diff] [blame] | 560 | outsamples = outsize / r->sample_size; |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 561 | GST_LOG_OBJECT (legacyresample, "resample gave me %d bytes or %d samples", |
Thomas Vander Stichele | 3d95afd | 2005-08-25 15:44:58 +0000 | [diff] [blame] | 562 | outsize, outsamples); |
| 563 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 564 | GST_BUFFER_OFFSET (outbuf) = legacyresample->offset; |
| 565 | GST_BUFFER_TIMESTAMP (outbuf) = legacyresample->next_ts; |
Thomas Vander Stichele | 7d1ce17 | 2005-08-25 17:20:02 +0000 | [diff] [blame] | 566 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 567 | if (legacyresample->ts_offset != -1) { |
| 568 | legacyresample->offset += outsamples; |
| 569 | legacyresample->ts_offset += outsamples; |
| 570 | legacyresample->next_ts = |
| 571 | gst_util_uint64_scale_int (legacyresample->ts_offset, GST_SECOND, |
| 572 | legacyresample->o_rate); |
| 573 | GST_BUFFER_OFFSET_END (outbuf) = legacyresample->offset; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 574 | |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 575 | /* we calculate DURATION as the difference between "next" timestamp |
| 576 | * and current timestamp so we ensure a contiguous stream, instead of |
| 577 | * having rounding errors. */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 578 | GST_BUFFER_DURATION (outbuf) = legacyresample->next_ts - |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 579 | GST_BUFFER_TIMESTAMP (outbuf); |
| 580 | } else { |
| 581 | /* no valid offset know, we can still sortof calculate the duration though */ |
| 582 | GST_BUFFER_DURATION (outbuf) = |
| 583 | gst_util_uint64_scale_int (outsamples, GST_SECOND, |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 584 | legacyresample->o_rate); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 585 | } |
Thomas Vander Stichele | 7d1ce17 | 2005-08-25 17:20:02 +0000 | [diff] [blame] | 586 | |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 587 | /* check for possible mem corruption */ |
| 588 | if (outsize > GST_BUFFER_SIZE (outbuf)) { |
| 589 | /* this is an error that when it happens, would need fixing in the |
Thomas Vander Stichele | 0dcd5d3 | 2007-03-14 14:09:21 +0000 | [diff] [blame] | 590 | * resample library; we told it we wanted only GST_BUFFER_SIZE (outbuf), |
| 591 | * and it gave us more ! */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 592 | GST_WARNING_OBJECT (legacyresample, |
| 593 | "legacyresample, you memory corrupting bastard. " |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 594 | "you gave me outsize %d while my buffer was size %d", |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 595 | outsize, GST_BUFFER_SIZE (outbuf)); |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 596 | return GST_FLOW_ERROR; |
| 597 | } |
| 598 | /* catch possibly wrong size differences */ |
| 599 | if (GST_BUFFER_SIZE (outbuf) - outsize > r->sample_size) { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 600 | GST_WARNING_OBJECT (legacyresample, |
| 601 | "legacyresample's written outsize %d too far from outbuffer's size %d", |
Thomas Vander Stichele | 0daade2 | 2005-08-25 12:31:31 +0000 | [diff] [blame] | 602 | outsize, GST_BUFFER_SIZE (outbuf)); |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 603 | } |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 604 | GST_BUFFER_SIZE (outbuf) = outsize; |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 605 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 606 | if (G_UNLIKELY (legacyresample->need_discont)) { |
| 607 | GST_DEBUG_OBJECT (legacyresample, |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 608 | "marking this buffer with the DISCONT flag"); |
| 609 | GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT); |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 610 | legacyresample->need_discont = FALSE; |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 613 | GST_LOG_OBJECT (legacyresample, "transformed to buffer of %d bytes, ts %" |
Thomas Vander Stichele | 0dcd5d3 | 2007-03-14 14:09:21 +0000 | [diff] [blame] | 614 | GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %" |
| 615 | G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT, |
| 616 | outsize, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), |
| 617 | GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), |
| 618 | GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf)); |
| 619 | |
| 620 | |
Thomas Vander Stichele | c4763db | 2005-08-24 14:08:58 +0000 | [diff] [blame] | 621 | return GST_FLOW_OK; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Michael Smith | 03ab530 | 2007-03-15 10:52:21 +0000 | [diff] [blame] | 624 | static gboolean |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 625 | legacyresample_check_discont (GstLegacyresample * legacyresample, |
Michael Smith | 03ab530 | 2007-03-15 10:52:21 +0000 | [diff] [blame] | 626 | GstClockTime timestamp) |
| 627 | { |
| 628 | if (timestamp != GST_CLOCK_TIME_NONE && |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 629 | legacyresample->prev_ts != GST_CLOCK_TIME_NONE && |
| 630 | legacyresample->prev_duration != GST_CLOCK_TIME_NONE && |
| 631 | timestamp != legacyresample->prev_ts + legacyresample->prev_duration) { |
Michael Smith | 03ab530 | 2007-03-15 10:52:21 +0000 | [diff] [blame] | 632 | /* Potentially a discontinuous buffer. However, it turns out that many |
| 633 | * elements generate imperfect streams due to rounding errors, so we permit |
| 634 | * a small error (up to one sample) without triggering a filter |
| 635 | * flush/restart (if triggered incorrectly, this will be audible) */ |
| 636 | GstClockTimeDiff diff = timestamp - |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 637 | (legacyresample->prev_ts + legacyresample->prev_duration); |
Michael Smith | 03ab530 | 2007-03-15 10:52:21 +0000 | [diff] [blame] | 638 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 639 | if (ABS (diff) > GST_SECOND / legacyresample->i_rate) { |
| 640 | GST_WARNING_OBJECT (legacyresample, |
Michael Smith | 03ab530 | 2007-03-15 10:52:21 +0000 | [diff] [blame] | 641 | "encountered timestamp discontinuity of %" G_GINT64_FORMAT, diff); |
| 642 | return TRUE; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | return FALSE; |
| 647 | } |
| 648 | |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 649 | static GstFlowReturn |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 650 | legacyresample_transform (GstBaseTransform * base, GstBuffer * inbuf, |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 651 | GstBuffer * outbuf) |
| 652 | { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 653 | GstLegacyresample *legacyresample; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 654 | ResampleState *r; |
Michael Smith | e1eae40 | 2005-12-15 10:30:14 +0000 | [diff] [blame] | 655 | guchar *data, *datacopy; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 656 | gulong size; |
| 657 | GstClockTime timestamp; |
| 658 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 659 | legacyresample = GST_LEGACYRESAMPLE (base); |
| 660 | r = legacyresample->resample; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 661 | |
| 662 | data = GST_BUFFER_DATA (inbuf); |
| 663 | size = GST_BUFFER_SIZE (inbuf); |
| 664 | timestamp = GST_BUFFER_TIMESTAMP (inbuf); |
| 665 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 666 | GST_LOG_OBJECT (legacyresample, "transforming buffer of %ld bytes, ts %" |
Thomas Vander Stichele | 0dcd5d3 | 2007-03-14 14:09:21 +0000 | [diff] [blame] | 667 | GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %" |
| 668 | G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT, |
| 669 | size, GST_TIME_ARGS (timestamp), |
| 670 | GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)), |
| 671 | GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf)); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 672 | |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 673 | /* check for timestamp discontinuities and flush/reset if needed */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 674 | if (G_UNLIKELY (legacyresample_check_discont (legacyresample, timestamp))) { |
Michael Smith | 03ab530 | 2007-03-15 10:52:21 +0000 | [diff] [blame] | 675 | /* Flush internal samples */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 676 | legacyresample_pushthrough (legacyresample); |
Michael Smith | 03ab530 | 2007-03-15 10:52:21 +0000 | [diff] [blame] | 677 | /* Inform downstream element about discontinuity */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 678 | legacyresample->need_discont = TRUE; |
Michael Smith | 03ab530 | 2007-03-15 10:52:21 +0000 | [diff] [blame] | 679 | /* We want to recalculate the offset */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 680 | legacyresample->ts_offset = -1; |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 683 | if (legacyresample->ts_offset == -1) { |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 684 | /* if we don't know the initial offset yet, calculate it based on the |
| 685 | * input timestamp. */ |
| 686 | if (GST_CLOCK_TIME_IS_VALID (timestamp)) { |
| 687 | GstClockTime stime; |
| 688 | |
Thomas Vander Stichele | 0dcd5d3 | 2007-03-14 14:09:21 +0000 | [diff] [blame] | 689 | /* offset used to calculate the timestamps. We use the sample offset for |
| 690 | * this to make it more accurate. We want the first buffer to have the |
| 691 | * same timestamp as the incoming timestamp. */ |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 692 | legacyresample->next_ts = timestamp; |
| 693 | legacyresample->ts_offset = |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 694 | gst_util_uint64_scale_int (timestamp, r->o_rate, GST_SECOND); |
Thomas Vander Stichele | 0dcd5d3 | 2007-03-14 14:09:21 +0000 | [diff] [blame] | 695 | /* offset used to set as the buffer offset, this offset is always |
| 696 | * relative to the stream time, note that timestamp is not... */ |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 697 | stime = (timestamp - base->segment.start) + base->segment.time; |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 698 | legacyresample->offset = |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 699 | gst_util_uint64_scale_int (stime, r->o_rate, GST_SECOND); |
| 700 | } |
| 701 | } |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 702 | legacyresample->prev_ts = timestamp; |
| 703 | legacyresample->prev_duration = GST_BUFFER_DURATION (inbuf); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 704 | |
| 705 | /* need to memdup, resample takes ownership. */ |
Michael Smith | e1eae40 | 2005-12-15 10:30:14 +0000 | [diff] [blame] | 706 | datacopy = g_memdup (data, size); |
| 707 | resample_add_input_data (r, datacopy, size, g_free, datacopy); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 708 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 709 | return legacyresample_do_output (legacyresample, outbuf); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* push remaining data in the buffers out */ |
| 713 | static GstFlowReturn |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 714 | legacyresample_pushthrough (GstLegacyresample * legacyresample) |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 715 | { |
| 716 | int outsize; |
| 717 | ResampleState *r; |
| 718 | GstBuffer *outbuf; |
| 719 | GstFlowReturn res = GST_FLOW_OK; |
| 720 | GstBaseTransform *trans; |
| 721 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 722 | r = legacyresample->resample; |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 723 | |
| 724 | outsize = resample_get_output_size (r); |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 725 | if (outsize == 0) { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 726 | GST_DEBUG_OBJECT (legacyresample, "no internal buffers needing flush"); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 727 | goto done; |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 728 | } |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 729 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 730 | trans = GST_BASE_TRANSFORM (legacyresample); |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 731 | |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 732 | res = gst_pad_alloc_buffer (trans->srcpad, GST_BUFFER_OFFSET_NONE, outsize, |
| 733 | GST_PAD_CAPS (trans->srcpad), &outbuf); |
| 734 | if (G_UNLIKELY (res != GST_FLOW_OK)) { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 735 | GST_WARNING_OBJECT (legacyresample, "failed allocating buffer of %d bytes", |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 736 | outsize); |
| 737 | goto done; |
| 738 | } |
| 739 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 740 | res = legacyresample_do_output (legacyresample, outbuf); |
Julien Moutte | e3ef9cd | 2007-03-14 17:16:30 +0000 | [diff] [blame] | 741 | if (G_UNLIKELY (res != GST_FLOW_OK)) |
| 742 | goto done; |
| 743 | |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 744 | res = gst_pad_push (trans->srcpad, outbuf); |
| 745 | |
| 746 | done: |
| 747 | return res; |
| 748 | } |
| 749 | |
Sebastian Dröge | c7cc386 | 2007-11-23 10:21:11 +0000 | [diff] [blame] | 750 | static gboolean |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 751 | legacyresample_query (GstPad * pad, GstQuery * query) |
Sebastian Dröge | c7cc386 | 2007-11-23 10:21:11 +0000 | [diff] [blame] | 752 | { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 753 | GstLegacyresample *legacyresample = |
| 754 | GST_LEGACYRESAMPLE (gst_pad_get_parent (pad)); |
| 755 | GstBaseTransform *trans = GST_BASE_TRANSFORM (legacyresample); |
Sebastian Dröge | c7cc386 | 2007-11-23 10:21:11 +0000 | [diff] [blame] | 756 | gboolean res = TRUE; |
| 757 | |
| 758 | switch (GST_QUERY_TYPE (query)) { |
| 759 | case GST_QUERY_LATENCY: |
| 760 | { |
| 761 | GstClockTime min, max; |
| 762 | gboolean live; |
| 763 | guint64 latency; |
| 764 | GstPad *peer; |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 765 | gint rate = legacyresample->i_rate; |
| 766 | gint resampler_latency = legacyresample->filter_length / 2; |
Sebastian Dröge | c7cc386 | 2007-11-23 10:21:11 +0000 | [diff] [blame] | 767 | |
| 768 | if (gst_base_transform_is_passthrough (trans)) |
| 769 | resampler_latency = 0; |
| 770 | |
| 771 | if ((peer = gst_pad_get_peer (trans->sinkpad))) { |
| 772 | if ((res = gst_pad_query (peer, query))) { |
| 773 | gst_query_parse_latency (query, &live, &min, &max); |
| 774 | |
| 775 | GST_DEBUG ("Peer latency: min %" |
| 776 | GST_TIME_FORMAT " max %" GST_TIME_FORMAT, |
| 777 | GST_TIME_ARGS (min), GST_TIME_ARGS (max)); |
| 778 | |
| 779 | /* add our own latency */ |
| 780 | if (rate != 0 && resampler_latency != 0) |
| 781 | latency = |
| 782 | gst_util_uint64_scale (resampler_latency, GST_SECOND, rate); |
| 783 | else |
| 784 | latency = 0; |
| 785 | |
| 786 | GST_DEBUG ("Our latency: %" GST_TIME_FORMAT, GST_TIME_ARGS (latency)); |
| 787 | |
| 788 | min += latency; |
| 789 | if (max != GST_CLOCK_TIME_NONE) |
| 790 | max += latency; |
| 791 | |
| 792 | GST_DEBUG ("Calculated total latency : min %" |
| 793 | GST_TIME_FORMAT " max %" GST_TIME_FORMAT, |
| 794 | GST_TIME_ARGS (min), GST_TIME_ARGS (max)); |
| 795 | |
| 796 | gst_query_set_latency (query, live, min, max); |
| 797 | } |
| 798 | gst_object_unref (peer); |
| 799 | } |
| 800 | break; |
| 801 | } |
| 802 | default: |
| 803 | res = gst_pad_query_default (pad, query); |
| 804 | break; |
| 805 | } |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 806 | gst_object_unref (legacyresample); |
Sebastian Dröge | c7cc386 | 2007-11-23 10:21:11 +0000 | [diff] [blame] | 807 | return res; |
| 808 | } |
| 809 | |
| 810 | static const GstQueryType * |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 811 | legacyresample_query_type (GstPad * pad) |
Sebastian Dröge | c7cc386 | 2007-11-23 10:21:11 +0000 | [diff] [blame] | 812 | { |
| 813 | static const GstQueryType types[] = { |
| 814 | GST_QUERY_LATENCY, |
| 815 | 0 |
| 816 | }; |
| 817 | |
| 818 | return types; |
| 819 | } |
Wim Taymans | c962e65 | 2005-12-02 11:34:50 +0000 | [diff] [blame] | 820 | |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 821 | static void |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 822 | gst_legacyresample_set_property (GObject * object, guint prop_id, |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 823 | const GValue * value, GParamSpec * pspec) |
| 824 | { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 825 | GstLegacyresample *legacyresample; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 826 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 827 | legacyresample = GST_LEGACYRESAMPLE (object); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 828 | |
| 829 | switch (prop_id) { |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 830 | case PROP_FILTERLEN: |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 831 | legacyresample->filter_length = g_value_get_int (value); |
| 832 | GST_DEBUG_OBJECT (GST_ELEMENT (legacyresample), "new filter length %d", |
| 833 | legacyresample->filter_length); |
| 834 | if (legacyresample->resample) { |
| 835 | resample_set_filter_length (legacyresample->resample, |
| 836 | legacyresample->filter_length); |
| 837 | gst_element_post_message (GST_ELEMENT (legacyresample), |
| 838 | gst_message_new_latency (GST_OBJECT (legacyresample))); |
Tim-Philipp Müller | ab84128 | 2006-06-16 15:17:44 +0000 | [diff] [blame] | 839 | } |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 840 | break; |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 841 | default: |
| 842 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 843 | break; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | static void |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 848 | gst_legacyresample_get_property (GObject * object, guint prop_id, |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 849 | GValue * value, GParamSpec * pspec) |
| 850 | { |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 851 | GstLegacyresample *legacyresample; |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 852 | |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 853 | legacyresample = GST_LEGACYRESAMPLE (object); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 854 | |
| 855 | switch (prop_id) { |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 856 | case PROP_FILTERLEN: |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 857 | g_value_set_int (value, legacyresample->filter_length); |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 858 | break; |
| 859 | default: |
| 860 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 861 | break; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | |
Wim Taymans | 4636900 | 2006-03-02 18:23:55 +0000 | [diff] [blame] | 866 | static gboolean |
| 867 | plugin_init (GstPlugin * plugin) |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 868 | { |
| 869 | resample_init (); |
| 870 | |
Sebastian Dröge | 5da0360 | 2008-11-27 16:58:31 +0000 | [diff] [blame] | 871 | if (!gst_element_register (plugin, "legacyresample", GST_RANK_MARGINAL, |
Sebastian Dröge | e4e3b44 | 2009-01-23 12:46:28 +0100 | [diff] [blame] | 872 | GST_TYPE_LEGACYRESAMPLE)) { |
David Schleef | bde8ec9 | 2005-08-23 19:29:38 +0000 | [diff] [blame] | 873 | return FALSE; |
| 874 | } |
| 875 | |
| 876 | return TRUE; |
| 877 | } |
| 878 | |
| 879 | GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, |
| 880 | GST_VERSION_MINOR, |
Sebastian Dröge | 5da0360 | 2008-11-27 16:58:31 +0000 | [diff] [blame] | 881 | "legacyresample", |
Thomas Vander Stichele | e575d00 | 2005-10-16 13:54:38 +0000 | [diff] [blame] | 882 | "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, |
| 883 | GST_PACKAGE_ORIGIN); |