2 * Copyright (C) 2019 Stéphane Cerveau <scerveau@collabora.com>
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-zxing
24 * Detect bar codes in the video streams and send them as element messages to
25 * the #GstBus if .#GstZXing:message property is %TRUE.
26 * If the .#GstZXing:attach-frame property is %TRUE, the posted barcode message
27 * includes a sample of the frame where the barcode was detected (Since 1.18).
29 * The element generate messages named `barcode`. The structure contains these fields:
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 * * #GstClockTime `stream-time`: timestamp converted to stream-time.
35 * * #GstClockTime `running-time`: timestamp converted to running-time.
36 * * #GstSample `frame`: the frame in which the barcode message was detected, if
37 * the .#GstZXing:attach-frame property was set to %TRUE (Since 1.18)
39 * This element is based on the c++ implementation of zxing which can found
40 * at https://github.com/nu-book/zxing-cpp.
42 * ## Example launch lines
44 * gst-launch-1.0 -m v4l2src ! videoconvert ! zxing ! videoconvert ! xvimagesink
45 * ]| This pipeline will detect barcodes and send them as messages.
47 * gst-launch-1.0 -m v4l2src ! tee name=t ! queue ! videoconvert ! zxing ! fakesink t. ! queue ! xvimagesink
48 * ]| Same as above, but running the filter on a branch to keep the display in color
61 #include <gst/video/video.h>
63 #include "ReadBarcode.h"
64 #include "TextUtfEncoding.h"
65 #include "ZXVersion.h"
67 using namespace ZXing;
69 GST_DEBUG_CATEGORY_STATIC (zxing_debug);
70 #define GST_CAT_DEFAULT zxing_debug
72 #define DEFAULT_MESSAGE TRUE
73 #define DEFAULT_ATTACH_FRAME FALSE
74 #define DEFAULT_TRY_ROTATE FALSE
75 #define DEFAULT_TRY_FASTER FALSE
91 BARCODE_FORMAT_CODABAR,
92 BARCODE_FORMAT_CODE_39,
93 BARCODE_FORMAT_CODE_93,
94 BARCODE_FORMAT_CODE_128,
95 BARCODE_FORMAT_DATA_MATRIX,
97 BARCODE_FORMAT_EAN_13,
99 BARCODE_FORMAT_MAXICODE,
100 BARCODE_FORMAT_PDF_417,
101 BARCODE_FORMAT_QR_CODE,
102 BARCODE_FORMAT_RSS_14,
103 BARCODE_FORMAT_RSS_EXPANDED,
104 BARCODE_FORMAT_UPC_A,
105 BARCODE_FORMAT_UPC_E,
106 BARCODE_FORMAT_UPC_EAN_EXTENSION
109 static const GEnumValue barcode_formats[] = {
110 {BARCODE_FORMAT_ALL, "ALL", "all"},
111 {BARCODE_FORMAT_AZTEC, "AZTEC", "aztec"},
112 {BARCODE_FORMAT_CODABAR, "CODABAR", "codabar"},
113 {BARCODE_FORMAT_CODE_39, "CODE_39", "code_39"},
114 {BARCODE_FORMAT_CODE_93, "CODE_93", "code_93"},
115 {BARCODE_FORMAT_CODE_128, "CODE_128", "code_128"},
116 {BARCODE_FORMAT_DATA_MATRIX, "PNG", "png"},
117 {BARCODE_FORMAT_EAN_8, "EAN_8", "ean_8"},
118 {BARCODE_FORMAT_EAN_13, "EAN_13", "ean_13"},
119 {BARCODE_FORMAT_ITF, "ITF", "itf"},
120 {BARCODE_FORMAT_MAXICODE, "MAXICODE", "maxicode"},
121 {BARCODE_FORMAT_PDF_417, "PDF_417", "pdf_417"},
122 {BARCODE_FORMAT_QR_CODE, "QR_CODE", "qr_code"},
123 {BARCODE_FORMAT_RSS_14, "RSS_14", "rss_14"},
124 {BARCODE_FORMAT_RSS_EXPANDED, "RSS_EXPANDED", "rss_expanded"},
125 {BARCODE_FORMAT_UPC_A, "UPC_A", "upc_a"},
126 {BARCODE_FORMAT_UPC_E, "UPC_E", "upc_e"},
127 {BARCODE_FORMAT_UPC_EAN_EXTENSION, "UPC_EAN_EXTENSION", "upc_ean_expansion"},
131 #define GST_TYPE_BARCODE_FORMAT (gst_barcode_format_get_type())
133 gst_barcode_format_get_type (void)
135 static GType barcode_format_type = 0;
137 if (!barcode_format_type) {
138 barcode_format_type =
139 g_enum_register_static ("GstBarCodeFormat", barcode_formats);
141 return barcode_format_type;
144 #define ZXING_YUV_CAPS \
145 "{ARGB, xRGB, Y444, Y42B, I420, Y41B, YUV9, YV12}"
148 static GstStaticPadTemplate gst_zxing_src_template =
149 GST_STATIC_PAD_TEMPLATE ("src",
152 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZXING_YUV_CAPS))
155 static GstStaticPadTemplate gst_zxing_sink_template =
156 GST_STATIC_PAD_TEMPLATE ("sink",
159 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZXING_YUV_CAPS))
165 * Opaque data structure.
170 GstVideoFilter videofilter;
174 gboolean attach_frame;
177 ImageFormat image_format;
178 guint barcode_format;
181 static void gst_zxing_set_property (GObject * object, guint prop_id,
182 const GValue * value, GParamSpec * pspec);
183 static void gst_zxing_get_property (GObject * object, guint prop_id,
184 GValue * value, GParamSpec * pspec);
185 static gboolean gst_zxing_set_info (GstVideoFilter * vfilter, GstCaps * in,
186 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info);
187 static GstFlowReturn gst_zxing_transform_frame_ip (GstVideoFilter * vfilter,
188 GstVideoFrame * frame);
190 #define gst_zxing_parent_class parent_class
191 G_DEFINE_TYPE_WITH_CODE (GstZXing, gst_zxing,
192 GST_TYPE_VIDEO_FILTER,
193 GST_DEBUG_CATEGORY_INIT (zxing_debug, "zxing", 0,
194 "debug category for zxing element"));
195 GST_ELEMENT_REGISTER_DEFINE (zxing, "zxing", GST_RANK_MARGINAL, GST_TYPE_ZXING);
198 gst_zxing_class_init (GstZXingClass * g_class)
200 GObjectClass *gobject_class;
201 GstElementClass *gstelement_class;
202 GstVideoFilterClass *vfilter_class;
204 gobject_class = G_OBJECT_CLASS (g_class);
205 gstelement_class = GST_ELEMENT_CLASS (g_class);
206 vfilter_class = GST_VIDEO_FILTER_CLASS (g_class);
208 gobject_class->set_property = gst_zxing_set_property;
209 gobject_class->get_property = gst_zxing_get_property;
211 g_object_class_install_property (gobject_class, PROP_MESSAGE,
212 g_param_spec_boolean ("message",
213 "message", "Post a barcode message for each detected code",
215 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
217 g_object_class_install_property (gobject_class, PROP_ATTACH_FRAME,
218 g_param_spec_boolean ("attach-frame", "Attach frame",
219 "Attach a frame dump to each barcode message",
220 DEFAULT_ATTACH_FRAME,
221 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
223 g_object_class_install_property (gobject_class, PROP_TRY_ROTATE,
224 g_param_spec_boolean ("try-rotate", "Try rotate",
225 "Try to rotate the frame to detect barcode (slower)",
227 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
228 g_object_class_install_property (gobject_class, PROP_TRY_FASTER,
229 g_param_spec_boolean ("try-faster", "Try faster",
230 "Try faster to analyze the frame", DEFAULT_TRY_FASTER,
231 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
232 g_object_class_install_property (gobject_class, PROP_FORMAT,
233 g_param_spec_enum ("format", "barcode format", "Barcode image format",
234 GST_TYPE_BARCODE_FORMAT, BARCODE_FORMAT_ALL,
235 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
237 gst_element_class_set_static_metadata (gstelement_class, "Barcode detector",
238 "Filter/Analyzer/Video",
239 "Detect bar codes in the video streams with zxing library",
240 "Stéphane Cerveau <scerveau@collabora.com>");
242 gst_element_class_add_static_pad_template (gstelement_class,
243 &gst_zxing_sink_template);
244 gst_element_class_add_static_pad_template (gstelement_class,
245 &gst_zxing_src_template);
247 vfilter_class->transform_frame_ip =
248 GST_DEBUG_FUNCPTR (gst_zxing_transform_frame_ip);
249 vfilter_class->set_info =
250 GST_DEBUG_FUNCPTR (gst_zxing_set_info);
254 gst_zxing_init (GstZXing * zxing)
256 zxing->message = DEFAULT_MESSAGE;
257 zxing->attach_frame = DEFAULT_ATTACH_FRAME;
258 zxing->rotate = DEFAULT_TRY_ROTATE;
259 zxing->faster = DEFAULT_TRY_FASTER;
260 zxing->image_format = ImageFormat::None;
261 zxing->barcode_format = BARCODE_FORMAT_ALL;
265 gst_zxing_set_property (GObject * object, guint prop_id, const GValue * value,
270 g_return_if_fail (GST_IS_ZXING (object));
271 zxing = GST_ZXING (object);
275 zxing->message = g_value_get_boolean (value);
277 case PROP_ATTACH_FRAME:
278 zxing->attach_frame = g_value_get_boolean (value);
280 case PROP_TRY_ROTATE:
281 zxing->rotate = g_value_get_boolean (value);
283 case PROP_TRY_FASTER:
284 zxing->faster = g_value_get_boolean (value);
287 zxing->barcode_format = g_value_get_enum (value);
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 gst_zxing_get_property (GObject * object, guint prop_id, GValue * value,
301 g_return_if_fail (GST_IS_ZXING (object));
302 zxing = GST_ZXING (object);
306 g_value_set_boolean (value, zxing->message);
308 case PROP_ATTACH_FRAME:
309 g_value_set_boolean (value, zxing->attach_frame);
311 case PROP_TRY_ROTATE:
312 g_value_set_boolean (value, zxing->rotate);
314 case PROP_TRY_FASTER:
315 g_value_set_boolean (value, zxing->faster);
318 g_value_set_enum (value, zxing->barcode_format);
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 gst_zxing_set_info (GstVideoFilter * vfilter, GstCaps * in,
328 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
330 GstZXing *zxing = GST_ZXING (vfilter);
331 switch (in_info->finfo->format) {
332 case GST_VIDEO_FORMAT_ARGB:
333 case GST_VIDEO_FORMAT_xRGB:
334 zxing->image_format = ImageFormat::XRGB;
336 case GST_VIDEO_FORMAT_Y444:
337 case GST_VIDEO_FORMAT_Y42B:
338 case GST_VIDEO_FORMAT_I420:
339 case GST_VIDEO_FORMAT_Y41B:
340 case GST_VIDEO_FORMAT_YUV9:
341 case GST_VIDEO_FORMAT_YV12:
342 zxing->image_format = ImageFormat::Lum;
345 zxing->image_format = ImageFormat::None;
346 GST_WARNING_OBJECT (zxing, "This format is not supported %s", gst_video_format_to_string(in_info->finfo->format));
352 gst_zxing_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
354 GstZXing *zxing = GST_ZXING (vfilter);
359 hints.setTryRotate(zxing->rotate);
360 hints.setTryHarder(!zxing->faster);
361 hints.setFormats(BarcodeFormatFromString (barcode_formats[zxing->barcode_format].value_name));
363 /* all formats we support start with an 8-bit Y plane. zxing doesn't need
364 * to know about the chroma plane(s) */
365 data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
366 width = GST_VIDEO_FRAME_WIDTH (frame);
367 height = GST_VIDEO_FRAME_HEIGHT (frame);
369 auto result = ReadBarcode ({(unsigned char *)data, width, height, zxing->image_format}, hints);
370 if (result.isValid ()) {
371 GST_DEBUG_OBJECT (zxing, "Symbol found. Text: %s Format: %s",
372 result.text ().c_str (),
373 #if ZXING_VERSION_MAJOR >= 2
374 ToString (result.format ()).c_str ());
376 ToString (result.format ()));
382 /* extract results */
383 if (zxing->message) {
387 GstCaps *sample_caps;
388 GstClockTime timestamp, running_time, stream_time;
390 timestamp = GST_BUFFER_TIMESTAMP (frame->buffer);
392 gst_segment_to_running_time (&GST_BASE_TRANSFORM (zxing)->segment,
393 GST_FORMAT_TIME, timestamp);
395 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (zxing)->segment,
396 GST_FORMAT_TIME, timestamp);
398 s = gst_structure_new ("barcode",
399 "timestamp", G_TYPE_UINT64, timestamp,
400 "stream-time", G_TYPE_UINT64, stream_time,
401 "running-time", G_TYPE_UINT64, running_time,
402 #if ZXING_VERSION_MAJOR >= 2
403 "type", G_TYPE_STRING, ToString (result.format ()).c_str (),
405 "type", G_TYPE_STRING, ToString (result.format ()),
407 "symbol", G_TYPE_STRING,
408 result.text ().c_str (), NULL);
410 if (zxing->attach_frame) {
411 /* create a sample from image */
412 sample_caps = gst_video_info_to_caps (&frame->info);
413 sample = gst_sample_new (frame->buffer, sample_caps, NULL, NULL);
414 gst_caps_unref (sample_caps);
415 gst_structure_set (s, "frame", GST_TYPE_SAMPLE, sample, NULL);
416 gst_sample_unref (sample);
418 m = gst_message_new_element (GST_OBJECT (zxing), s);
419 gst_element_post_message (GST_ELEMENT (zxing), m);
421 } else if (zxing->attach_frame)
422 GST_WARNING_OBJECT (zxing,
423 "attach-frame=true has no effect if message=false");