2 * Copyright (C) 2009 Stefan Kost <ensonic@users.sf.net>
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.
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.
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.
21 * SECTION:element-zbar
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).
29 * The element generate messages named`barcode`. The structure containes these fields:
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
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)
39 * ## Example launch lines
41 * gst-launch-1.0 -m v4l2src ! videoconvert ! zbar ! videoconvert ! xvimagesink
42 * ]| This pipeline will detect barcodes and send them as messages.
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
58 #include <gst/video/video.h>
61 GST_DEBUG_CATEGORY_STATIC (zbar_debug);
62 #define GST_CAT_DEFAULT zbar_debug
64 /* GstZBar signals and args */
79 #define DEFAULT_CACHE FALSE
80 #define DEFAULT_MESSAGE TRUE
81 #define DEFAULT_ATTACH_FRAME FALSE
83 #define ZBAR_YUV_CAPS \
84 "{ Y800, I420, YV12, NV12, NV21, Y41B, Y42B, YUV9, YVU9 }"
86 static GstStaticPadTemplate gst_zbar_src_template =
87 GST_STATIC_PAD_TEMPLATE ("src",
90 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZBAR_YUV_CAPS))
93 static GstStaticPadTemplate gst_zbar_sink_template =
94 GST_STATIC_PAD_TEMPLATE ("sink",
97 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZBAR_YUV_CAPS))
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);
106 static gboolean gst_zbar_start (GstBaseTransform * base);
107 static gboolean gst_zbar_stop (GstBaseTransform * base);
109 static GstFlowReturn gst_zbar_transform_frame_ip (GstVideoFilter * vfilter,
110 GstVideoFrame * frame);
112 #define gst_zbar_parent_class parent_class
113 G_DEFINE_TYPE (GstZBar, gst_zbar, GST_TYPE_VIDEO_FILTER);
116 gst_zbar_class_init (GstZBarClass * g_class)
118 GObjectClass *gobject_class;
119 GstElementClass *gstelement_class;
120 GstBaseTransformClass *trans_class;
121 GstVideoFilterClass *vfilter_class;
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);
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;
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));
138 * GstZBar::attach-frame:
140 * Attach the frame in which the barcode was detected to the posted
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));
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",
154 G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
155 G_PARAM_STATIC_STRINGS));
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>");
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);
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;
171 vfilter_class->transform_frame_ip =
172 GST_DEBUG_FUNCPTR (gst_zbar_transform_frame_ip);
176 gst_zbar_init (GstZBar * zbar)
178 zbar->cache = DEFAULT_CACHE;
179 zbar->message = DEFAULT_MESSAGE;
180 zbar->attach_frame = DEFAULT_ATTACH_FRAME;
182 zbar->scanner = zbar_image_scanner_create ();
186 gst_zbar_finalize (GObject * object)
188 GstZBar *zbar = GST_ZBAR (object);
190 zbar_image_scanner_destroy (zbar->scanner);
192 G_OBJECT_CLASS (parent_class)->finalize (object);
196 gst_zbar_set_property (GObject * object, guint prop_id, const GValue * value,
201 g_return_if_fail (GST_IS_ZBAR (object));
202 zbar = GST_ZBAR (object);
206 zbar->cache = g_value_get_boolean (value);
209 zbar->message = g_value_get_boolean (value);
211 case PROP_ATTACH_FRAME:
212 zbar->attach_frame = g_value_get_boolean (value);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gst_zbar_get_property (GObject * object, guint prop_id, GValue * value,
226 g_return_if_fail (GST_IS_ZBAR (object));
227 zbar = GST_ZBAR (object);
231 g_value_set_boolean (value, zbar->cache);
234 g_value_set_boolean (value, zbar->message);
236 case PROP_ATTACH_FRAME:
237 g_value_set_boolean (value, zbar->attach_frame);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 gst_zbar_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
248 GstZBar *zbar = GST_ZBAR (vfilter);
252 const zbar_symbol_t *symbol;
255 image = zbar_image_create ();
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);
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);
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");
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);
283 GST_DEBUG_OBJECT (zbar, "decoded %s symbol \"%s\" at quality %d",
284 zbar_get_symbol_name (typ), data, quality);
286 if (zbar->cache && zbar_symbol_get_count (symbol) != 0)
293 GstCaps *sample_caps;
294 GstClockTime timestamp, running_time, stream_time, duration;
296 timestamp = GST_BUFFER_TIMESTAMP (frame->buffer);
297 duration = GST_BUFFER_DURATION (frame->buffer);
299 gst_segment_to_running_time (&GST_BASE_TRANSFORM (zbar)->segment,
300 GST_FORMAT_TIME, timestamp);
302 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (zbar)->segment,
303 GST_FORMAT_TIME, timestamp);
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);
312 if (GST_CLOCK_TIME_IS_VALID (duration))
313 gst_structure_set (s, "duration", G_TYPE_UINT64, duration, NULL);
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);
324 m = gst_message_new_element (GST_OBJECT (zbar), s);
325 gst_element_post_message (GST_ELEMENT (zbar), m);
327 } else if (zbar->attach_frame)
328 GST_WARNING_OBJECT (zbar,
329 "attach-frame=true has no effect if message=false");
334 zbar_image_scanner_recycle_image (zbar->scanner, image);
335 zbar_image_destroy (image);
341 gst_zbar_start (GstBaseTransform * base)
343 GstZBar *zbar = GST_ZBAR (base);
345 /* start the cache if enabled (e.g. for filtering dupes) */
346 zbar_image_scanner_enable_cache (zbar->scanner, zbar->cache);
352 gst_zbar_stop (GstBaseTransform * base)
354 GstZBar *zbar = GST_ZBAR (base);
356 /* stop the cache if enabled (e.g. for filtering dupes) */
357 zbar_image_scanner_enable_cache (zbar->scanner, zbar->cache);
363 plugin_init (GstPlugin * plugin)
365 GST_DEBUG_CATEGORY_INIT (zbar_debug, "zbar", 0, "zbar");
367 return gst_element_register (plugin, "zbar", GST_RANK_NONE, GST_TYPE_ZBAR);
370 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
373 "zbar barcode scanner",
374 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);