1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/* GStreamer
* Copyright 2010 ST-Ericsson SA
* @author: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* test autovideoconvert:
* if rgb2bayer is present
* gst-launch videotestsrc num-buffers=2 ! "video/x-raw,width=100,height=100,framerate=10/1" ! autovideoconvert ! "video/x-raw-bayer,width=100,height=100,format=bggr,framerate=10/1" ! fakesink -v
* if bayer2rgb is present
* gst-launch videotestsrc num-buffers=2 ! "video/x-raw-bayer,width=100,height=100,format=bggr,framerate=10/1" ! autovideoconvert ! "video/x-raw,width=100,height=100,framerate=10/1" ! fakesink -v
* test with videoconvert
* gst-launch videotestsrc num-buffers=2 ! "video/x-raw,format=RGBx,width=100,height=100,framerate=10/1" ! autovideoconvert ! "video/x-raw,format=RGB16,width=100,height=100,framerate=10/1" ! fakesink -v
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include "gstautovideoconvert.h"
GST_DEBUG_CATEGORY (autovideoconvert_debug);
#define GST_CAT_DEFAULT (autovideoconvert_debug)
GStaticMutex factories_mutex = G_STATIC_MUTEX_INIT;
guint32 factories_cookie = 0; /* Cookie from last time when factories was updated */
GList *factories = NULL; /* factories we can use for selecting elements */
/* element factory information */
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
static GstStateChangeReturn gst_auto_video_convert_change_state (GstElement *
element, GstStateChange transition);
void gst_auto_video_convert_update_factory_list (GstAutoVideoConvert *
autovideoconvert);
static gboolean
gst_auto_video_convert_element_filter (GstPluginFeature * feature,
GstAutoVideoConvert * autovideoconvert)
{
const gchar *klass;
/* we only care about element factories */
if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
return FALSE;
klass = gst_element_factory_get_metadata (GST_ELEMENT_FACTORY_CAST (feature),
GST_ELEMENT_METADATA_KLASS);
/* only select color space converter */
if (strstr (klass, "Filter") &&
strstr (klass, "Converter") && strstr (klass, "Video")) {
GST_DEBUG_OBJECT (autovideoconvert,
"gst_auto_video_convert_element_filter found %s\n",
gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (feature)));
return TRUE;
}
return FALSE;
}
static GList *
gst_auto_video_convert_create_factory_list (GstAutoVideoConvert *
autovideoconvert)
{
GList *result = NULL;
/* get the feature list using the filter */
result = gst_registry_feature_filter (gst_registry_get (),
(GstPluginFeatureFilter) gst_auto_video_convert_element_filter,
FALSE, autovideoconvert);
/* sort on rank and name */
result = g_list_sort (result, gst_plugin_feature_rank_compare_func);
return result;
}
void
gst_auto_video_convert_update_factory_list (GstAutoVideoConvert *
autovideoconvert)
{
/* use a static mutex to protect factories list and factories cookie */
g_static_mutex_lock (&factories_mutex);
/* test if a factories list already exist or not */
if (!factories) {
/* no factories list create it */
factories_cookie =
gst_registry_get_feature_list_cookie (gst_registry_get ());
factories = gst_auto_video_convert_create_factory_list (autovideoconvert);
} else {
/* a factories list exist but is it up to date? */
if (factories_cookie !=
gst_registry_get_feature_list_cookie (gst_registry_get ())) {
/* we need to update the factories list */
/* first free the old one */
gst_plugin_feature_list_free (factories);
/* then create an updated one */
factories_cookie =
gst_registry_get_feature_list_cookie (gst_registry_get ());
factories = gst_auto_video_convert_create_factory_list (autovideoconvert);
}
}
g_static_mutex_unlock (&factories_mutex);
}
G_DEFINE_TYPE (GstAutoVideoConvert, gst_auto_video_convert, GST_TYPE_BIN);
static void
gst_auto_video_convert_class_init (GstAutoVideoConvertClass * klass)
{
GstElementClass *gstelement_class = (GstElementClass *) klass;
GST_DEBUG_CATEGORY_INIT (autovideoconvert_debug, "autovideoconvert", 0,
"Auto color space converter");
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&srctemplate));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&sinktemplate));
gst_element_class_set_static_metadata (gstelement_class,
"Select color space convertor based on caps", "Generic/Bin",
"Selects the right color space convertor based on the caps",
"Benjamin Gaignard <benjamin.gaignard@stericsson.com>");
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_auto_video_convert_change_state);
}
static gboolean
gst_auto_video_convert_add_autoconvert (GstAutoVideoConvert * autovideoconvert)
{
GstPad *pad;
if (autovideoconvert->autoconvert)
return TRUE;
autovideoconvert->autoconvert =
gst_element_factory_make ("autoconvert", "autoconvertchild");
if (!autovideoconvert->autoconvert) {
GST_ERROR_OBJECT (autovideoconvert,
"Could not create autoconvert instance");
return FALSE;
}
/* first add autoconvert in bin */
gst_bin_add (GST_BIN (autovideoconvert),
gst_object_ref (autovideoconvert->autoconvert));
/* get sinkpad and link it to ghost sink pad */
pad = gst_element_get_static_pad (autovideoconvert->autoconvert, "sink");
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->sinkpad),
pad);
gst_object_unref (pad);
/* get srcpad and link it to ghost src pad */
pad = gst_element_get_static_pad (autovideoconvert->autoconvert, "src");
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->srcpad), pad);
gst_object_unref (pad);
return TRUE;
}
static void
gst_auto_video_convert_remove_autoconvert (GstAutoVideoConvert *
autovideoconvert)
{
if (!autovideoconvert->autoconvert)
return;
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->srcpad),
NULL);
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->sinkpad),
NULL);
gst_bin_remove (GST_BIN (autovideoconvert), autovideoconvert->autoconvert);
gst_object_unref (autovideoconvert->autoconvert);
autovideoconvert->autoconvert = NULL;
}
static void
gst_auto_video_convert_init (GstAutoVideoConvert * autovideoconvert)
{
GstPadTemplate *pad_tmpl;
/* get sink pad template */
pad_tmpl = gst_static_pad_template_get (&sinktemplate);
autovideoconvert->sinkpad =
gst_ghost_pad_new_no_target_from_template ("sink", pad_tmpl);
/* add sink ghost pad */
gst_element_add_pad (GST_ELEMENT (autovideoconvert),
autovideoconvert->sinkpad);
gst_object_unref (pad_tmpl);
/* get src pad template */
pad_tmpl = gst_static_pad_template_get (&srctemplate);
autovideoconvert->srcpad =
gst_ghost_pad_new_no_target_from_template ("src", pad_tmpl);
/* add src ghost pad */
gst_element_add_pad (GST_ELEMENT (autovideoconvert),
autovideoconvert->srcpad);
gst_object_unref (pad_tmpl);
return;
}
static GstStateChangeReturn
gst_auto_video_convert_change_state (GstElement * element,
GstStateChange transition)
{
GstAutoVideoConvert *autovideoconvert = GST_AUTO_VIDEO_CONVERT (element);
GstStateChangeReturn ret;
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
{
/* create and add autoconvert in bin */
if (!gst_auto_video_convert_add_autoconvert (autovideoconvert)) {
ret = GST_STATE_CHANGE_FAILURE;
return ret;
}
/* get an updated list of factories */
gst_auto_video_convert_update_factory_list (autovideoconvert);
GST_DEBUG_OBJECT (autovideoconvert, "set factories list");
/* give factory list to autoconvert */
g_object_set (GST_ELEMENT (autovideoconvert->autoconvert), "factories",
factories, NULL);
break;
}
default:
break;
}
ret = GST_ELEMENT_CLASS (gst_auto_video_convert_parent_class)->change_state
(element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
switch (transition) {
case GST_STATE_CHANGE_READY_TO_NULL:
{
gst_auto_video_convert_remove_autoconvert (autovideoconvert);
break;
}
default:
break;
}
return ret;
}
|