Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / debugutils / gstcapssetter.c
1 /* GStreamer Element
2  * Copyright (C) 2006-2009 Mark Nauwelaerts <mnauw@users.sourceforge.net>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:element-capssetter
22  *
23  * <refsect2>
24  * <para>
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).
30  * </para>
31  * <para>
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.
35  * </para>
36  * <para>
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
42  * </para>
43  * <para>
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
47  * alike.
48  * </para>
49  * </refsect2>
50  *
51  */
52
53
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
57
58 #include "gstcapssetter.h"
59
60 #include <string.h>
61
62
63 GST_DEBUG_CATEGORY_STATIC (caps_setter_debug);
64 #define GST_CAT_DEFAULT caps_setter_debug
65
66
67 /* signals and args */
68 enum
69 {
70   /* FILL ME */
71   LAST_SIGNAL
72 };
73
74 enum
75 {
76   PROP_0,
77   PROP_CAPS,
78   PROP_JOIN,
79   PROP_REPLACE
80       /* FILL ME */
81 };
82
83 #define DEFAULT_JOIN              TRUE
84 #define DEFAULT_REPLACE           FALSE
85
86 static GstStaticPadTemplate gst_caps_setter_src_template =
87 GST_STATIC_PAD_TEMPLATE (GST_BASE_TRANSFORM_SRC_NAME,
88     GST_PAD_SRC,
89     GST_PAD_ALWAYS,
90     GST_STATIC_CAPS_ANY);
91
92 static GstStaticPadTemplate gst_caps_setter_sink_template =
93 GST_STATIC_PAD_TEMPLATE (GST_BASE_TRANSFORM_SINK_NAME,
94     GST_PAD_SINK,
95     GST_PAD_ALWAYS,
96     GST_STATIC_CAPS_ANY);
97
98
99 static gboolean gst_caps_setter_transform_size (GstBaseTransform * trans,
100     GstPadDirection direction, GstCaps * caps, gsize size,
101     GstCaps * othercaps, gsize * othersize);
102 static GstCaps *gst_caps_setter_transform_caps (GstBaseTransform * trans,
103     GstPadDirection direction, GstCaps * caps, GstCaps * cfilter);
104 static GstFlowReturn gst_caps_setter_transform_ip (GstBaseTransform * btrans,
105     GstBuffer * in);
106
107 static void gst_caps_setter_finalize (GObject * object);
108
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);
113
114 #define gst_caps_setter_parent_class parent_class
115 G_DEFINE_TYPE (GstCapsSetter, gst_caps_setter, GST_TYPE_BASE_TRANSFORM);
116
117 static void
118 gst_caps_setter_class_init (GstCapsSetterClass * g_class)
119 {
120   GObjectClass *gobject_class = (GObjectClass *) g_class;
121   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
122   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) g_class;
123
124   GST_DEBUG_CATEGORY_INIT (caps_setter_debug, "capssetter", 0, "capssetter");
125
126   gobject_class->set_property = gst_caps_setter_set_property;
127   gobject_class->get_property = gst_caps_setter_get_property;
128
129   gobject_class->finalize = gst_caps_setter_finalize;
130
131   g_object_class_install_property (gobject_class, PROP_CAPS,
132       g_param_spec_boxed ("caps", "Merge caps",
133           "Merge these caps (thereby overwriting) in the stream",
134           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
135   g_object_class_install_property (gobject_class, PROP_JOIN,
136       g_param_spec_boolean ("join", "Join",
137           "Match incoming caps' mime-type to mime-type of provided caps",
138           DEFAULT_JOIN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139   g_object_class_install_property (gobject_class, PROP_REPLACE,
140       g_param_spec_boolean ("replace", "Replace",
141           "Drop fields of incoming caps", DEFAULT_REPLACE,
142           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
143
144   gst_element_class_set_details_simple (element_class, "CapsSetter",
145       "Generic",
146       "Set/merge caps on stream",
147       "Mark Nauwelaerts <mnauw@users.sourceforge.net>");
148
149   gst_element_class_add_pad_template (element_class,
150       gst_static_pad_template_get (&gst_caps_setter_sink_template));
151   gst_element_class_add_pad_template (element_class,
152       gst_static_pad_template_get (&gst_caps_setter_src_template));
153
154   trans_class->transform_size =
155       GST_DEBUG_FUNCPTR (gst_caps_setter_transform_size);
156   trans_class->transform_caps =
157       GST_DEBUG_FUNCPTR (gst_caps_setter_transform_caps);
158   /* dummy seems needed */
159   trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_caps_setter_transform_ip);
160 }
161
162 static void
163 gst_caps_setter_init (GstCapsSetter * filter)
164 {
165   filter->caps = gst_caps_new_any ();
166   filter->join = DEFAULT_JOIN;
167   filter->replace = DEFAULT_REPLACE;
168 }
169
170 static void
171 gst_caps_setter_finalize (GObject * object)
172 {
173   GstCapsSetter *filter = GST_CAPS_SETTER (object);
174
175   gst_caps_replace (&filter->caps, NULL);
176
177   G_OBJECT_CLASS (parent_class)->finalize (object);
178 }
179
180 static gboolean
181 gst_caps_setter_transform_size (GstBaseTransform * trans,
182     GstPadDirection direction, GstCaps * caps, gsize size,
183     GstCaps * othercaps, gsize * othersize)
184 {
185   *othersize = size;
186
187   return TRUE;
188 }
189
190 static GstCaps *
191 gst_caps_setter_transform_caps (GstBaseTransform * trans,
192     GstPadDirection direction, GstCaps * caps, GstCaps * cfilter)
193 {
194   GstCapsSetter *filter = GST_CAPS_SETTER (trans);
195   GstCaps *ret, *filter_caps;
196   GstStructure *structure, *merge;
197   const gchar *name;
198   gint i, j;
199
200   GST_DEBUG_OBJECT (trans, "receiving caps: %" GST_PTR_FORMAT, caps);
201
202   ret = gst_caps_copy (caps);
203
204   /* this function is always called with a simple caps */
205   if (!GST_CAPS_IS_SIMPLE (ret) || direction != GST_PAD_SINK)
206     return ret;
207
208   structure = gst_caps_get_structure (ret, 0);
209   name = gst_structure_get_name (structure);
210
211   GST_OBJECT_LOCK (filter);
212   filter_caps = gst_caps_ref (filter->caps);
213   GST_OBJECT_UNLOCK (filter);
214
215   for (i = 0; i < gst_caps_get_size (filter_caps); ++i) {
216     merge = gst_caps_get_structure (filter_caps, i);
217     if (gst_structure_has_name (merge, name) || !filter->join) {
218
219       if (!filter->join)
220         gst_structure_set_name (structure, gst_structure_get_name (merge));
221
222       if (filter->replace)
223         gst_structure_remove_all_fields (structure);
224
225       for (j = 0; j < gst_structure_n_fields (merge); ++j) {
226         const gchar *fname;
227
228         fname = gst_structure_nth_field_name (merge, j);
229         gst_structure_set_value (structure, fname,
230             gst_structure_get_value (merge, fname));
231       }
232     }
233   }
234
235   GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
236
237   gst_caps_unref (filter_caps);
238
239   return ret;
240 }
241
242 static GstFlowReturn
243 gst_caps_setter_transform_ip (GstBaseTransform * btrans, GstBuffer * in)
244 {
245   return GST_FLOW_OK;
246 }
247
248 static gboolean
249 gst_caps_is_fixed_foreach (GQuark field_id, const GValue * value,
250     gpointer unused)
251 {
252   return gst_value_is_fixed (value);
253 }
254
255 static void
256 gst_caps_setter_set_property (GObject * object, guint prop_id,
257     const GValue * value, GParamSpec * pspec)
258 {
259   GstCapsSetter *filter = GST_CAPS_SETTER (object);
260
261   switch (prop_id) {
262     case PROP_CAPS:{
263       GstCaps *new_caps;
264       const GstCaps *new_caps_val = gst_value_get_caps (value);
265       gint i;
266
267       if (new_caps_val == NULL) {
268         new_caps = gst_caps_new_any ();
269       } else {
270         new_caps = gst_caps_copy (new_caps_val);
271       }
272
273       for (i = 0; new_caps && (i < gst_caps_get_size (new_caps)); ++i) {
274         GstStructure *s;
275
276         s = gst_caps_get_structure (new_caps, i);
277         if (!gst_structure_foreach (s, gst_caps_is_fixed_foreach, NULL)) {
278           GST_ERROR_OBJECT (filter, "rejected unfixed caps: %" GST_PTR_FORMAT,
279               new_caps);
280           gst_caps_unref (new_caps);
281           new_caps = NULL;
282           break;
283         }
284       }
285
286       if (new_caps) {
287         GST_OBJECT_LOCK (filter);
288         gst_caps_replace (&filter->caps, new_caps);
289         /* drop extra ref */
290         gst_caps_unref (new_caps);
291         GST_OBJECT_UNLOCK (filter);
292
293         GST_DEBUG_OBJECT (filter, "set new caps %" GST_PTR_FORMAT, new_caps);
294       }
295
296       /* try to activate these new caps next time around */
297       gst_base_transform_reconfigure (GST_BASE_TRANSFORM (filter));
298       break;
299     }
300     case PROP_JOIN:
301       filter->join = g_value_get_boolean (value);
302       break;
303     case PROP_REPLACE:
304       filter->replace = g_value_get_boolean (value);
305       break;
306     default:
307       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308       break;
309   }
310 }
311
312 static void
313 gst_caps_setter_get_property (GObject * object, guint prop_id, GValue * value,
314     GParamSpec * pspec)
315 {
316   GstCapsSetter *filter = GST_CAPS_SETTER (object);
317
318   switch (prop_id) {
319     case PROP_CAPS:
320       gst_value_set_caps (value, filter->caps);
321       break;
322     case PROP_JOIN:
323       g_value_set_boolean (value, filter->join);
324       break;
325     case PROP_REPLACE:
326       g_value_set_boolean (value, filter->replace);
327       break;
328     default:
329       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330       break;
331   }
332 }