1 /* GStreamer
2 * Copyright (C) 2009 Stefan Kost <ensonic@users.sf.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 St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-zbar
22 * @title: zbar
23 *
24 * Detect bar codes in the video streams and send them as element messages to
25 * the #GstBus if .#GstZBar:message property is %TRUE.
26 * If the .#GstZBar:attach-frame property is %TRUE, the posted barcode message
27 * includes a sample of the frame where the barcode was detected (Since 1.6).
28 *
29 * The element generate messages named`barcode`. The structure containes these fields:
30 *
31 * * #GstClockTime `timestamp`: the timestamp of the buffer that triggered the message.
32 * * gchar * `type`: the symbol type.
33 * * gchar * `symbol`: the deteted bar code data.
34 * * gint `quality`: an unscaled, relative quantity: larger values are better than smaller
35 * values.
36 * * GstSample `frame`: the frame in which the barcode message was detected, if
37 * the .#GstZBar:attach-frame property was set to %TRUE (Since 1.6)
38 *
39 * ## Example launch lines
40 * |[
41 * gst-launch-1.0 -m v4l2src ! videoconvert ! zbar ! videoconvert ! xvimagesink
42 * ]| This pipeline will detect barcodes and send them as messages.
43 * |[
44 * gst-launch-1.0 -m v4l2src ! tee name=t ! queue ! videoconvert ! zbar ! fakesink t. ! queue ! xvimagesink
45 * ]| Same as above, but running the filter on a branch to keep the display in color
46 *
47 */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "gstzbar.h"
54
55 #include <string.h>
56 #include <math.h>
57
58 #include <gst/video/video.h>
59
60
61 GST_DEBUG_CATEGORY_STATIC (zbar_debug);
62 #define GST_CAT_DEFAULT zbar_debug
63
64 /* GstZBar signals and args */
65 enum
66 {
67 /* FILL ME */
68 LAST_SIGNAL
69 };
70
71 enum
72 {
73 PROP_0,
74 PROP_MESSAGE,
75 PROP_ATTACH_FRAME,
76 PROP_CACHE
77 };
78
79 #define DEFAULT_CACHE FALSE
80 #define DEFAULT_MESSAGE TRUE
81 #define DEFAULT_ATTACH_FRAME FALSE
82
83 #define ZBAR_YUV_CAPS \
84 "{ Y800, I420, YV12, NV12, NV21, Y41B, Y42B, YUV9, YVU9 }"
85
86 static GstStaticPadTemplate gst_zbar_src_template =
87 GST_STATIC_PAD_TEMPLATE ("src",
88 GST_PAD_SRC,
89 GST_PAD_ALWAYS,
90 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZBAR_YUV_CAPS))
91 );
92
93 static GstStaticPadTemplate gst_zbar_sink_template =
94 GST_STATIC_PAD_TEMPLATE ("sink",
95 GST_PAD_SINK,
96 GST_PAD_ALWAYS,
97 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZBAR_YUV_CAPS))
98 );
99
100 static void gst_zbar_finalize (GObject * object);
101 static void gst_zbar_set_property (GObject * object, guint prop_id,
102 const GValue * value, GParamSpec * pspec);
103 static void gst_zbar_get_property (GObject * object, guint prop_id,
104 GValue * value, GParamSpec * pspec);
105
106 static gboolean gst_zbar_start (GstBaseTransform * base);
107 static gboolean gst_zbar_stop (GstBaseTransform * base);
108
109 static GstFlowReturn gst_zbar_transform_frame_ip (GstVideoFilter * vfilter,
110 GstVideoFrame * frame);
111
112 #define gst_zbar_parent_class parent_class
113 G_DEFINE_TYPE (GstZBar, gst_zbar, GST_TYPE_VIDEO_FILTER);
114
115 static void
gst_zbar_class_init(GstZBarClass * g_class)116 gst_zbar_class_init (GstZBarClass * g_class)
117 {
118 GObjectClass *gobject_class;
119 GstElementClass *gstelement_class;
120 GstBaseTransformClass *trans_class;
121 GstVideoFilterClass *vfilter_class;
122
123 gobject_class = G_OBJECT_CLASS (g_class);
124 gstelement_class = GST_ELEMENT_CLASS (g_class);
125 trans_class = GST_BASE_TRANSFORM_CLASS (g_class);
126 vfilter_class = GST_VIDEO_FILTER_CLASS (g_class);
127
128 gobject_class->set_property = gst_zbar_set_property;
129 gobject_class->get_property = gst_zbar_get_property;
130 gobject_class->finalize = gst_zbar_finalize;
131
132 g_object_class_install_property (gobject_class, PROP_MESSAGE,
133 g_param_spec_boolean ("message", "message",
134 "Post a barcode message for each detected code",
135 DEFAULT_MESSAGE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
136
137 /**
138 * GstZBar::attach-frame:
139 *
140 * Attach the frame in which the barcode was detected to the posted
141 * barcode message.
142 *
143 * Since: 1.6
144 */
145 g_object_class_install_property (gobject_class, PROP_ATTACH_FRAME,
146 g_param_spec_boolean ("attach-frame", "Attach frame",
147 "Attach a frame dump to each barcode message",
148 DEFAULT_ATTACH_FRAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149
150 g_object_class_install_property (gobject_class, PROP_CACHE,
151 g_param_spec_boolean ("cache", "cache",
152 "Enable or disable the inter-image result cache",
153 DEFAULT_CACHE,
154 G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
155 G_PARAM_STATIC_STRINGS));
156
157 gst_element_class_set_static_metadata (gstelement_class, "Barcode detector",
158 "Filter/Analyzer/Video",
159 "Detect bar codes in the video streams",
160 "Stefan Kost <ensonic@users.sf.net>");
161
162 gst_element_class_add_static_pad_template (gstelement_class,
163 &gst_zbar_sink_template);
164 gst_element_class_add_static_pad_template (gstelement_class,
165 &gst_zbar_src_template);
166
167 trans_class->start = GST_DEBUG_FUNCPTR (gst_zbar_start);
168 trans_class->stop = GST_DEBUG_FUNCPTR (gst_zbar_stop);
169 trans_class->transform_ip_on_passthrough = FALSE;
170
171 vfilter_class->transform_frame_ip =
172 GST_DEBUG_FUNCPTR (gst_zbar_transform_frame_ip);
173 }
174
175 static void
gst_zbar_init(GstZBar * zbar)176 gst_zbar_init (GstZBar * zbar)
177 {
178 zbar->cache = DEFAULT_CACHE;
179 zbar->message = DEFAULT_MESSAGE;
180 zbar->attach_frame = DEFAULT_ATTACH_FRAME;
181
182 zbar->scanner = zbar_image_scanner_create ();
183 }
184
185 static void
gst_zbar_finalize(GObject * object)186 gst_zbar_finalize (GObject * object)
187 {
188 GstZBar *zbar = GST_ZBAR (object);
189
190 zbar_image_scanner_destroy (zbar->scanner);
191
192 G_OBJECT_CLASS (parent_class)->finalize (object);
193 }
194
195 static void
gst_zbar_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)196 gst_zbar_set_property (GObject * object, guint prop_id, const GValue * value,
197 GParamSpec * pspec)
198 {
199 GstZBar *zbar;
200
201 g_return_if_fail (GST_IS_ZBAR (object));
202 zbar = GST_ZBAR (object);
203
204 switch (prop_id) {
205 case PROP_CACHE:
206 zbar->cache = g_value_get_boolean (value);
207 break;
208 case PROP_MESSAGE:
209 zbar->message = g_value_get_boolean (value);
210 break;
211 case PROP_ATTACH_FRAME:
212 zbar->attach_frame = g_value_get_boolean (value);
213 break;
214 default:
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 break;
217 }
218 }
219
220 static void
gst_zbar_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)221 gst_zbar_get_property (GObject * object, guint prop_id, GValue * value,
222 GParamSpec * pspec)
223 {
224 GstZBar *zbar;
225
226 g_return_if_fail (GST_IS_ZBAR (object));
227 zbar = GST_ZBAR (object);
228
229 switch (prop_id) {
230 case PROP_CACHE:
231 g_value_set_boolean (value, zbar->cache);
232 break;
233 case PROP_MESSAGE:
234 g_value_set_boolean (value, zbar->message);
235 break;
236 case PROP_ATTACH_FRAME:
237 g_value_set_boolean (value, zbar->attach_frame);
238 break;
239 default:
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 break;
242 }
243 }
244
245 static GstFlowReturn
gst_zbar_transform_frame_ip(GstVideoFilter * vfilter,GstVideoFrame * frame)246 gst_zbar_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
247 {
248 GstZBar *zbar = GST_ZBAR (vfilter);
249 gpointer data;
250 gint stride, height;
251 zbar_image_t *image;
252 const zbar_symbol_t *symbol;
253 int n;
254
255 image = zbar_image_create ();
256
257 /* all formats we support start with an 8-bit Y plane. zbar doesn't need
258 * to know about the chroma plane(s) */
259 data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
260 stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
261 height = GST_VIDEO_FRAME_HEIGHT (frame);
262
263 zbar_image_set_format (image, GST_MAKE_FOURCC ('Y', '8', '0', '0'));
264 zbar_image_set_size (image, stride, height);
265 zbar_image_set_data (image, (gpointer) data, stride * height, NULL);
266
267 /* scan the image for barcodes */
268 n = zbar_scan_image (zbar->scanner, image);
269 if (G_UNLIKELY (n == -1)) {
270 GST_WARNING_OBJECT (zbar, "Error trying to scan frame. Skipping");
271 goto out;
272 }
273 if (n == 0)
274 goto out;
275
276 /* extract results */
277 symbol = zbar_image_first_symbol (image);
278 for (; symbol; symbol = zbar_symbol_next (symbol)) {
279 zbar_symbol_type_t typ = zbar_symbol_get_type (symbol);
280 const char *data = zbar_symbol_get_data (symbol);
281 gint quality = zbar_symbol_get_quality (symbol);
282
283 GST_DEBUG_OBJECT (zbar, "decoded %s symbol \"%s\" at quality %d",
284 zbar_get_symbol_name (typ), data, quality);
285
286 if (zbar->cache && zbar_symbol_get_count (symbol) != 0)
287 continue;
288
289 if (zbar->message) {
290 GstMessage *m;
291 GstStructure *s;
292 GstSample *sample;
293 GstCaps *sample_caps;
294 GstClockTime timestamp, running_time, stream_time, duration;
295
296 timestamp = GST_BUFFER_TIMESTAMP (frame->buffer);
297 duration = GST_BUFFER_DURATION (frame->buffer);
298 running_time =
299 gst_segment_to_running_time (&GST_BASE_TRANSFORM (zbar)->segment,
300 GST_FORMAT_TIME, timestamp);
301 stream_time =
302 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (zbar)->segment,
303 GST_FORMAT_TIME, timestamp);
304
305 s = gst_structure_new ("barcode",
306 "timestamp", G_TYPE_UINT64, timestamp,
307 "stream-time", G_TYPE_UINT64, stream_time,
308 "running-time", G_TYPE_UINT64, running_time,
309 "type", G_TYPE_STRING, zbar_get_symbol_name (typ),
310 "symbol", G_TYPE_STRING, data, "quality", G_TYPE_INT, quality, NULL);
311
312 if (GST_CLOCK_TIME_IS_VALID (duration))
313 gst_structure_set (s, "duration", G_TYPE_UINT64, duration, NULL);
314
315 if (zbar->attach_frame) {
316 /* create a sample from image */
317 sample_caps = gst_video_info_to_caps (&frame->info);
318 sample = gst_sample_new (frame->buffer, sample_caps, NULL, NULL);
319 gst_caps_unref (sample_caps);
320 gst_structure_set (s, "frame", GST_TYPE_SAMPLE, sample, NULL);
321 gst_sample_unref (sample);
322 }
323
324 m = gst_message_new_element (GST_OBJECT (zbar), s);
325 gst_element_post_message (GST_ELEMENT (zbar), m);
326
327 } else if (zbar->attach_frame)
328 GST_WARNING_OBJECT (zbar,
329 "attach-frame=true has no effect if message=false");
330 }
331
332 out:
333 /* clean up */
334 zbar_image_scanner_recycle_image (zbar->scanner, image);
335 zbar_image_destroy (image);
336
337 return GST_FLOW_OK;
338 }
339
340 static gboolean
gst_zbar_start(GstBaseTransform * base)341 gst_zbar_start (GstBaseTransform * base)
342 {
343 GstZBar *zbar = GST_ZBAR (base);
344
345 /* start the cache if enabled (e.g. for filtering dupes) */
346 zbar_image_scanner_enable_cache (zbar->scanner, zbar->cache);
347
348 return TRUE;
349 }
350
351 static gboolean
gst_zbar_stop(GstBaseTransform * base)352 gst_zbar_stop (GstBaseTransform * base)
353 {
354 GstZBar *zbar = GST_ZBAR (base);
355
356 /* stop the cache if enabled (e.g. for filtering dupes) */
357 zbar_image_scanner_enable_cache (zbar->scanner, zbar->cache);
358
359 return TRUE;
360 }
361
362 static gboolean
plugin_init(GstPlugin * plugin)363 plugin_init (GstPlugin * plugin)
364 {
365 GST_DEBUG_CATEGORY_INIT (zbar_debug, "zbar", 0, "zbar");
366
367 return gst_element_register (plugin, "zbar", GST_RANK_NONE, GST_TYPE_ZBAR);
368 }
369
370 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
371 GST_VERSION_MINOR,
372 zbar,
373 "zbar barcode scanner",
374 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
375