2 * Copyright (C) 2006-2009 Mark Nauwelaerts <mnauw@users.sourceforge.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1307, USA.
21 * SECTION:element-capssetter
25 * Sets or merges caps on a stream's buffers.
26 * That is, a buffer's caps are updated using (fields of)
27 * <link linkend="GstCapsSetter--caps">caps</link>. Note that this may
28 * contain multiple structures (though not likely recommended), but each
29 * of these must be fixed (or will otherwise be rejected).
32 * If <link linkend="GstCapsSetter--join">join</link>
33 * is TRUE, then the incoming caps' mime-type is compared to the mime-type(s)
34 * of provided caps and only matching structure(s) are considered for updating.
37 * If <link linkend="GstCapsSetter--replace">replace</link>
38 * is TRUE, then any caps update is preceded by clearing existing fields,
39 * making provided fields (as a whole) replace incoming ones.
40 * Otherwise, no clearing is performed, in which case provided fields are
41 * added/merged onto incoming caps
44 * Although this element might mainly serve as debug helper,
45 * it can also practically be used to correct a faulty pixel-aspect-ratio,
46 * or to modify a yuv fourcc value to effectively swap chroma components or such
58 #include "gstcapssetter.h"
63 GST_DEBUG_CATEGORY_STATIC (caps_setter_debug);
64 #define GST_CAT_DEFAULT caps_setter_debug
67 /* signals and args */
83 #define DEFAULT_JOIN TRUE
84 #define DEFAULT_REPLACE FALSE
86 static GstStaticPadTemplate gst_caps_setter_src_template =
87 GST_STATIC_PAD_TEMPLATE (GST_BASE_TRANSFORM_SRC_NAME,
92 static GstStaticPadTemplate gst_caps_setter_sink_template =
93 GST_STATIC_PAD_TEMPLATE (GST_BASE_TRANSFORM_SINK_NAME,
99 static gboolean gst_caps_setter_transform_size (GstBaseTransform * trans,
100 GstPadDirection direction, GstCaps * caps, guint size,
101 GstCaps * othercaps, guint * othersize);
102 static GstCaps *gst_caps_setter_transform_caps (GstBaseTransform * trans,
103 GstPadDirection direction, GstCaps * caps);
104 static GstFlowReturn gst_caps_setter_transform_ip (GstBaseTransform * btrans,
107 static void gst_caps_setter_finalize (GObject * object);
109 static void gst_caps_setter_set_property (GObject * object, guint prop_id,
110 const GValue * value, GParamSpec * pspec);
111 static void gst_caps_setter_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec);
114 GST_BOILERPLATE (GstCapsSetter, gst_caps_setter, GstBaseTransform,
115 GST_TYPE_BASE_TRANSFORM);
118 gst_caps_setter_base_init (gpointer g_class)
120 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
122 gst_element_class_set_details_simple (element_class, "CapsSetter",
124 "Set/merge caps on stream",
125 "Mark Nauwelaerts <mnauw@users.sourceforge.net>");
127 gst_element_class_add_pad_template (element_class,
128 gst_static_pad_template_get (&gst_caps_setter_sink_template));
129 gst_element_class_add_pad_template (element_class,
130 gst_static_pad_template_get (&gst_caps_setter_src_template));
134 gst_caps_setter_class_init (GstCapsSetterClass * g_class)
136 GObjectClass *gobject_class = (GObjectClass *) g_class;
137 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) g_class;
139 GST_DEBUG_CATEGORY_INIT (caps_setter_debug, "capssetter", 0, "capssetter");
141 gobject_class->set_property = gst_caps_setter_set_property;
142 gobject_class->get_property = gst_caps_setter_get_property;
144 gobject_class->finalize = gst_caps_setter_finalize;
146 g_object_class_install_property (gobject_class, PROP_CAPS,
147 g_param_spec_boxed ("caps", "Merge caps",
148 "Merge these caps (thereby overwriting) in the stream",
149 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150 g_object_class_install_property (gobject_class, PROP_JOIN,
151 g_param_spec_boolean ("join", "Join",
152 "Match incoming caps' mime-type to mime-type of provided caps",
153 DEFAULT_JOIN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154 g_object_class_install_property (gobject_class, PROP_REPLACE,
155 g_param_spec_boolean ("replace", "Replace",
156 "Drop fields of incoming caps", DEFAULT_REPLACE,
157 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159 trans_class->transform_size =
160 GST_DEBUG_FUNCPTR (gst_caps_setter_transform_size);
161 trans_class->transform_caps =
162 GST_DEBUG_FUNCPTR (gst_caps_setter_transform_caps);
163 /* dummy seems needed */
164 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_caps_setter_transform_ip);
168 gst_caps_setter_init (GstCapsSetter * filter, GstCapsSetterClass * g_class)
170 filter->caps = gst_caps_new_any ();
171 filter->join = DEFAULT_JOIN;
172 filter->replace = DEFAULT_REPLACE;
176 gst_caps_setter_finalize (GObject * object)
178 GstCapsSetter *filter = GST_CAPS_SETTER (object);
180 gst_caps_replace (&filter->caps, NULL);
182 G_OBJECT_CLASS (parent_class)->finalize (object);
186 gst_caps_setter_transform_size (GstBaseTransform * trans,
187 GstPadDirection direction, GstCaps * caps, guint size,
188 GstCaps * othercaps, guint * othersize)
196 gst_caps_setter_transform_caps (GstBaseTransform * trans,
197 GstPadDirection direction, GstCaps * caps)
199 GstCapsSetter *filter = GST_CAPS_SETTER (trans);
200 GstCaps *ret, *filter_caps;
201 GstStructure *structure, *merge;
205 GST_DEBUG_OBJECT (trans, "receiving caps: %" GST_PTR_FORMAT, caps);
207 ret = gst_caps_copy (caps);
209 /* this function is always called with a simple caps */
210 if (!GST_CAPS_IS_SIMPLE (ret) || direction != GST_PAD_SINK)
213 structure = gst_caps_get_structure (ret, 0);
214 name = gst_structure_get_name (structure);
216 GST_OBJECT_LOCK (filter);
217 filter_caps = gst_caps_ref (filter->caps);
218 GST_OBJECT_UNLOCK (filter);
220 for (i = 0; i < gst_caps_get_size (filter_caps); ++i) {
221 merge = gst_caps_get_structure (filter_caps, i);
222 if (gst_structure_has_name (merge, name) || !filter->join) {
225 gst_structure_set_name (structure, gst_structure_get_name (merge));
228 gst_structure_remove_all_fields (structure);
230 for (j = 0; j < gst_structure_n_fields (merge); ++j) {
233 fname = gst_structure_nth_field_name (merge, j);
234 gst_structure_set_value (structure, fname,
235 gst_structure_get_value (merge, fname));
240 GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
242 gst_caps_unref (filter_caps);
248 gst_caps_setter_transform_ip (GstBaseTransform * btrans, GstBuffer * in)
254 gst_caps_is_fixed_foreach (GQuark field_id, const GValue * value,
257 return gst_value_is_fixed (value);
261 gst_caps_setter_set_property (GObject * object, guint prop_id,
262 const GValue * value, GParamSpec * pspec)
264 GstCapsSetter *filter = GST_CAPS_SETTER (object);
269 const GstCaps *new_caps_val = gst_value_get_caps (value);
272 if (new_caps_val == NULL) {
273 new_caps = gst_caps_new_any ();
275 new_caps = gst_caps_copy (new_caps_val);
278 for (i = 0; new_caps && (i < gst_caps_get_size (new_caps)); ++i) {
281 s = gst_caps_get_structure (new_caps, i);
282 if (!gst_structure_foreach (s, gst_caps_is_fixed_foreach, NULL)) {
283 GST_ERROR_OBJECT (filter, "rejected unfixed caps: %" GST_PTR_FORMAT,
285 gst_caps_unref (new_caps);
292 GST_OBJECT_LOCK (filter);
293 gst_caps_replace (&filter->caps, new_caps);
295 gst_caps_unref (new_caps);
296 GST_OBJECT_UNLOCK (filter);
298 GST_DEBUG_OBJECT (filter, "set new caps %" GST_PTR_FORMAT, new_caps);
301 /* try to activate these new caps next time around */
302 gst_base_transform_reconfigure (GST_BASE_TRANSFORM (filter));
306 filter->join = g_value_get_boolean (value);
309 filter->replace = g_value_get_boolean (value);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318 gst_caps_setter_get_property (GObject * object, guint prop_id, GValue * value,
321 GstCapsSetter *filter = GST_CAPS_SETTER (object);
325 gst_value_set_caps (value, filter->caps);
328 g_value_set_boolean (value, filter->join);
331 g_value_set_boolean (value, filter->replace);
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);