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 contains 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 detected 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_WITH_CODE (GstZBar, gst_zbar, GST_TYPE_VIDEO_FILTER,
114 GST_DEBUG_CATEGORY_INIT (zbar_debug, "zbar", 0, "zbar"););
115 GST_ELEMENT_REGISTER_DEFINE (zbar, "zbar", GST_RANK_NONE, GST_TYPE_ZBAR);
116
117 static void
gst_zbar_class_init(GstZBarClass * g_class)118 gst_zbar_class_init (GstZBarClass * g_class)
119 {
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
122 GstBaseTransformClass *trans_class;
123 GstVideoFilterClass *vfilter_class;
124
125 gobject_class = G_OBJECT_CLASS (g_class);
126 gstelement_class = GST_ELEMENT_CLASS (g_class);
127 trans_class = GST_BASE_TRANSFORM_CLASS (g_class);
128 vfilter_class = GST_VIDEO_FILTER_CLASS (g_class);
129
130 gobject_class->set_property = gst_zbar_set_property;
131 gobject_class->get_property = gst_zbar_get_property;
132 gobject_class->finalize = gst_zbar_finalize;
133
134 g_object_class_install_property (gobject_class, PROP_MESSAGE,
135 g_param_spec_boolean ("message", "message",
136 "Post a barcode message for each detected code",
137 DEFAULT_MESSAGE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138
139 /**
140 * GstZBar::attach-frame:
141 *
142 * Attach the frame in which the barcode was detected to the posted
143 * barcode message.
144 *
145 * Since: 1.6
146 */
147 g_object_class_install_property (gobject_class, PROP_ATTACH_FRAME,
148 g_param_spec_boolean ("attach-frame", "Attach frame",
149 "Attach a frame dump to each barcode message",
150 DEFAULT_ATTACH_FRAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151
152 g_object_class_install_property (gobject_class, PROP_CACHE,
153 g_param_spec_boolean ("cache", "cache",
154 "Enable or disable the inter-image result cache",
155 DEFAULT_CACHE,
156 G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
157 G_PARAM_STATIC_STRINGS));
158
159 gst_element_class_set_static_metadata (gstelement_class, "Barcode detector",
160 "Filter/Analyzer/Video",
161 "Detect bar codes in the video streams",
162 "Stefan Kost <ensonic@users.sf.net>");
163
164 gst_element_class_add_static_pad_template (gstelement_class,
165 &gst_zbar_sink_template);
166 gst_element_class_add_static_pad_template (gstelement_class,
167 &gst_zbar_src_template);
168
169 trans_class->start = GST_DEBUG_FUNCPTR (gst_zbar_start);
170 trans_class->stop = GST_DEBUG_FUNCPTR (gst_zbar_stop);
171
172 vfilter_class->transform_frame_ip =
173 GST_DEBUG_FUNCPTR (gst_zbar_transform_frame_ip);
174 }
175
176 static void
gst_zbar_init(GstZBar * zbar)177 gst_zbar_init (GstZBar * zbar)
178 {
179 zbar->cache = DEFAULT_CACHE;
180 zbar->message = DEFAULT_MESSAGE;
181 zbar->attach_frame = DEFAULT_ATTACH_FRAME;
182
183 zbar->scanner = zbar_image_scanner_create ();
184 }
185
186 static void
gst_zbar_finalize(GObject * object)187 gst_zbar_finalize (GObject * object)
188 {
189 GstZBar *zbar = GST_ZBAR (object);
190
191 zbar_image_scanner_destroy (zbar->scanner);
192
193 G_OBJECT_CLASS (parent_class)->finalize (object);
194 }
195
196 static void
gst_zbar_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)197 gst_zbar_set_property (GObject * object, guint prop_id, const GValue * value,
198 GParamSpec * pspec)
199 {
200 GstZBar *zbar;
201
202 g_return_if_fail (GST_IS_ZBAR (object));
203 zbar = GST_ZBAR (object);
204
205 switch (prop_id) {
206 case PROP_CACHE:
207 zbar->cache = g_value_get_boolean (value);
208 break;
209 case PROP_MESSAGE:
210 zbar->message = g_value_get_boolean (value);
211 break;
212 case PROP_ATTACH_FRAME:
213 zbar->attach_frame = g_value_get_boolean (value);
214 break;
215 default:
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 break;
218 }
219 }
220
221 static void
gst_zbar_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)222 gst_zbar_get_property (GObject * object, guint prop_id, GValue * value,
223 GParamSpec * pspec)
224 {
225 GstZBar *zbar;
226
227 g_return_if_fail (GST_IS_ZBAR (object));
228 zbar = GST_ZBAR (object);
229
230 switch (prop_id) {
231 case PROP_CACHE:
232 g_value_set_boolean (value, zbar->cache);
233 break;
234 case PROP_MESSAGE:
235 g_value_set_boolean (value, zbar->message);
236 break;
237 case PROP_ATTACH_FRAME:
238 g_value_set_boolean (value, zbar->attach_frame);
239 break;
240 default:
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 break;
243 }
244 }
245
246 static GstFlowReturn
gst_zbar_transform_frame_ip(GstVideoFilter * vfilter,GstVideoFrame * frame)247 gst_zbar_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
248 {
249 GstZBar *zbar = GST_ZBAR (vfilter);
250 gpointer data;
251 gint stride, height;
252 zbar_image_t *image;
253 const zbar_symbol_t *symbol;
254 int n;
255
256 image = zbar_image_create ();
257
258 /* all formats we support start with an 8-bit Y plane. zbar doesn't need
259 * to know about the chroma plane(s) */
260 data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
261 stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
262 height = GST_VIDEO_FRAME_HEIGHT (frame);
263
264 zbar_image_set_format (image, GST_MAKE_FOURCC ('Y', '8', '0', '0'));
265 zbar_image_set_size (image, stride, height);
266 zbar_image_set_data (image, (gpointer) data, stride * height, NULL);
267
268 /* scan the image for barcodes */
269 n = zbar_scan_image (zbar->scanner, image);
270 if (G_UNLIKELY (n == -1)) {
271 GST_WARNING_OBJECT (zbar, "Error trying to scan frame. Skipping");
272 goto out;
273 }
274 if (n == 0)
275 goto out;
276
277 /* extract results */
278 symbol = zbar_image_first_symbol (image);
279 for (; symbol; symbol = zbar_symbol_next (symbol)) {
280 zbar_symbol_type_t typ = zbar_symbol_get_type (symbol);
281 const char *data = zbar_symbol_get_data (symbol);
282 gint quality = zbar_symbol_get_quality (symbol);
283
284 GST_DEBUG_OBJECT (zbar, "decoded %s symbol \"%s\" at quality %d",
285 zbar_get_symbol_name (typ), data, quality);
286
287 if (zbar->cache && zbar_symbol_get_count (symbol) != 0)
288 continue;
289
290 if (zbar->message) {
291 GstMessage *m;
292 GstStructure *s;
293 GstSample *sample;
294 GstCaps *sample_caps;
295 GstClockTime timestamp, running_time, stream_time, duration;
296
297 timestamp = GST_BUFFER_TIMESTAMP (frame->buffer);
298 duration = GST_BUFFER_DURATION (frame->buffer);
299 running_time =
300 gst_segment_to_running_time (&GST_BASE_TRANSFORM (zbar)->segment,
301 GST_FORMAT_TIME, timestamp);
302 stream_time =
303 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (zbar)->segment,
304 GST_FORMAT_TIME, timestamp);
305
306 s = gst_structure_new ("barcode",
307 "timestamp", G_TYPE_UINT64, timestamp,
308 "stream-time", G_TYPE_UINT64, stream_time,
309 "running-time", G_TYPE_UINT64, running_time,
310 "type", G_TYPE_STRING, zbar_get_symbol_name (typ),
311 "symbol", G_TYPE_STRING, data, "quality", G_TYPE_INT, quality, NULL);
312
313 if (GST_CLOCK_TIME_IS_VALID (duration))
314 gst_structure_set (s, "duration", G_TYPE_UINT64, duration, NULL);
315
316 if (zbar->attach_frame) {
317 /* create a sample from image */
318 sample_caps = gst_video_info_to_caps (&frame->info);
319 sample = gst_sample_new (frame->buffer, sample_caps, NULL, NULL);
320 gst_caps_unref (sample_caps);
321 gst_structure_set (s, "frame", GST_TYPE_SAMPLE, sample, NULL);
322 gst_sample_unref (sample);
323 }
324
325 m = gst_message_new_element (GST_OBJECT (zbar), s);
326 gst_element_post_message (GST_ELEMENT (zbar), m);
327
328 } else if (zbar->attach_frame)
329 GST_WARNING_OBJECT (zbar,
330 "attach-frame=true has no effect if message=false");
331 }
332
333 out:
334 /* clean up */
335 zbar_image_scanner_recycle_image (zbar->scanner, image);
336 zbar_image_destroy (image);
337
338 return GST_FLOW_OK;
339 }
340
341 static gboolean
gst_zbar_start(GstBaseTransform * base)342 gst_zbar_start (GstBaseTransform * base)
343 {
344 GstZBar *zbar = GST_ZBAR (base);
345
346 /* start the cache if enabled (e.g. for filtering dupes) */
347 zbar_image_scanner_enable_cache (zbar->scanner, zbar->cache);
348
349 return TRUE;
350 }
351
352 static gboolean
gst_zbar_stop(GstBaseTransform * base)353 gst_zbar_stop (GstBaseTransform * base)
354 {
355 GstZBar *zbar = GST_ZBAR (base);
356
357 /* stop the cache if enabled (e.g. for filtering dupes) */
358 zbar_image_scanner_enable_cache (zbar->scanner, zbar->cache);
359
360 return TRUE;
361 }
362
363 static gboolean
plugin_init(GstPlugin * plugin)364 plugin_init (GstPlugin * plugin)
365 {
366 return GST_ELEMENT_REGISTER (zbar, plugin);
367 }
368
369 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
370 GST_VERSION_MINOR,
371 zbar,
372 "zbar barcode scanner",
373 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
374