-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgstopencvfilter.c
273 lines (217 loc) · 8.12 KB
/
gstopencvfilter.c
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
/* GStreamer
* Copyright (C) 2019 Peter J. Torelli <peter.j.torelli@gmail.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., 51 Franklin Street, Suite 500,
* Boston, MA 02110-1335, USA.
*/
/**
* SECTION:element-gstopencvfilter
*
* The opencvfilter element inserts OpenCV into GStreamer.
*
* <refsect2>
* <title>Example launch line</title>
* |[
* gst-launch-1.0 -v videotestsrc ! opencvfilter ! videoconvert ! ximagesink
* ]|
* The pipeline is a simple transform call.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/video/gstvideofilter.h>
#include "gstopencvfilter.h"
GST_DEBUG_CATEGORY_STATIC (gst_opencvfilter_debug_category);
#define GST_CAT_DEFAULT gst_opencvfilter_debug_category
/* prototypes */
int openCvInterface(int height, int width, uint8_t *iuffer, uint8_t *obuffer);
static void gst_opencvfilter_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_opencvfilter_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
static void gst_opencvfilter_dispose (GObject * object);
static void gst_opencvfilter_finalize (GObject * object);
static gboolean gst_opencvfilter_start (GstBaseTransform * trans);
static gboolean gst_opencvfilter_stop (GstBaseTransform * trans);
static gboolean gst_opencvfilter_set_info (GstVideoFilter * filter, GstCaps * incaps,
GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info);
static GstFlowReturn gst_opencvfilter_transform_frame (GstVideoFilter * filter,
GstVideoFrame * inframe, GstVideoFrame * outframe);
static GstFlowReturn gst_opencvfilter_transform_frame_ip (GstVideoFilter * filter,
GstVideoFrame * frame);
enum
{
PROP_0
};
/* pad templates */
/* FIXME: add/remove formats you can handle */
#define VIDEO_SRC_CAPS \
GST_VIDEO_CAPS_MAKE("{ BGRA }")
/* FIXME: add/remove formats you can handle */
#define VIDEO_SINK_CAPS \
GST_VIDEO_CAPS_MAKE("{ BGRA }")
/* class initialization */
G_DEFINE_TYPE_WITH_CODE (GstOpencvfilter, gst_opencvfilter, GST_TYPE_VIDEO_FILTER,
GST_DEBUG_CATEGORY_INIT (gst_opencvfilter_debug_category, "opencvfilter", 0,
"debug category for opencvfilter element"));
static void
gst_opencvfilter_class_init (GstOpencvfilterClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstBaseTransformClass *base_transform_class = GST_BASE_TRANSFORM_CLASS (klass);
GstVideoFilterClass *video_filter_class = GST_VIDEO_FILTER_CLASS (klass);
/* Setting up pads and setting metadata should be moved to
base_class_init if you intend to subclass this class. */
gst_element_class_add_pad_template (GST_ELEMENT_CLASS(klass),
gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
gst_caps_from_string (VIDEO_SRC_CAPS)));
gst_element_class_add_pad_template (GST_ELEMENT_CLASS(klass),
gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
gst_caps_from_string (VIDEO_SINK_CAPS)));
gst_element_class_set_static_metadata (GST_ELEMENT_CLASS(klass),
"opencvfilter", "Generic", "OpenCV Video Filter",
"Peter J. Torelli <peter.j.torelli@gmail.com>");
gobject_class->set_property = gst_opencvfilter_set_property;
gobject_class->get_property = gst_opencvfilter_get_property;
gobject_class->dispose = gst_opencvfilter_dispose;
gobject_class->finalize = gst_opencvfilter_finalize;
base_transform_class->start = GST_DEBUG_FUNCPTR (gst_opencvfilter_start);
base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_opencvfilter_stop);
video_filter_class->set_info = GST_DEBUG_FUNCPTR (gst_opencvfilter_set_info);
video_filter_class->transform_frame = GST_DEBUG_FUNCPTR (gst_opencvfilter_transform_frame);
video_filter_class->transform_frame_ip = GST_DEBUG_FUNCPTR (gst_opencvfilter_transform_frame_ip);
}
static void
gst_opencvfilter_init (GstOpencvfilter *opencvfilter)
{
}
void
gst_opencvfilter_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (object);
GST_DEBUG_OBJECT (opencvfilter, "set_property");
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
gst_opencvfilter_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (object);
GST_DEBUG_OBJECT (opencvfilter, "get_property");
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
gst_opencvfilter_dispose (GObject * object)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (object);
GST_DEBUG_OBJECT (opencvfilter, "dispose");
/* clean up as possible. may be called multiple times */
G_OBJECT_CLASS (gst_opencvfilter_parent_class)->dispose (object);
}
void
gst_opencvfilter_finalize (GObject * object)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (object);
GST_DEBUG_OBJECT (opencvfilter, "finalize");
/* clean up object here */
G_OBJECT_CLASS (gst_opencvfilter_parent_class)->finalize (object);
}
static gboolean
gst_opencvfilter_start (GstBaseTransform * trans)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (trans);
GST_DEBUG_OBJECT (opencvfilter, "start");
return TRUE;
}
static gboolean
gst_opencvfilter_stop (GstBaseTransform * trans)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (trans);
GST_DEBUG_OBJECT (opencvfilter, "stop");
return TRUE;
}
static gboolean
gst_opencvfilter_set_info (GstVideoFilter * filter, GstCaps * incaps,
GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (filter);
GST_DEBUG_OBJECT (opencvfilter, "set_info");
return TRUE;
}
/* transform */
static GstFlowReturn
gst_opencvfilter_transform_frame (GstVideoFilter * filter, GstVideoFrame * inframe,
GstVideoFrame * outframe)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (filter);
gint height;
gint width;
guint8 *idata;
guint8 *odata;
idata = (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (inframe, 0);
odata = (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (outframe, 0);
width = GST_VIDEO_FRAME_COMP_WIDTH (inframe, 0);
height = GST_VIDEO_FRAME_COMP_HEIGHT (inframe, 0);
openCvInterface(height, width, idata, odata);
return GST_FLOW_OK;
}
static GstFlowReturn
gst_opencvfilter_transform_frame_ip (GstVideoFilter * filter, GstVideoFrame * frame)
{
GstOpencvfilter *opencvfilter = GST_OPENCVFILTER (filter);
GST_DEBUG_OBJECT (opencvfilter, "transform_frame_ip");
return GST_FLOW_OK;
}
static gboolean
plugin_init (GstPlugin * plugin)
{
/* FIXME Remember to set the rank if it's an element that is meant
to be autoplugged by decodebin. */
return gst_element_register (plugin, "opencvfilter", GST_RANK_NONE,
GST_TYPE_OPENCVFILTER);
}
/* FIXME: these are normally defined by the GStreamer build system.
If you are creating an element to be included in gst-plugins-*,
remove these, as they're always defined. Otherwise, edit as
appropriate for your external plugin package. */
#ifndef VERSION
#define VERSION "0.0.1"
#endif
#ifndef PACKAGE
#define PACKAGE "opencv"
#endif
#ifndef PACKAGE_NAME
#define PACKAGE_NAME "OpenCV Tools"
#endif
#ifndef GST_PACKAGE_ORIGIN
#define GST_PACKAGE_ORIGIN "no origin"
#endif
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
opencvfilter,
"OpenCV Video Filter",
plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)