zxing: add support for zxing-c++ 2.0
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / zxing / gstzxing.cpp
1 /* GStreamer
2  * Copyright (C) 2019 Stéphane Cerveau <scerveau@collabora.com>
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-zxing
22  * @title: zxing
23  *
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).
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  * * #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)
38  *
39  *   This element is based on the c++ implementation of zxing which can found
40  *   at https://github.com/nu-book/zxing-cpp.
41  *
42  * ## Example launch lines
43  * |[
44  * gst-launch-1.0 -m v4l2src ! videoconvert ! zxing ! videoconvert ! xvimagesink
45  * ]| This pipeline will detect barcodes and send them as messages.
46  * |[
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
49  *
50  */
51
52 #ifdef HAVE_CONFIG_H
53 #include "config.h"
54 #endif
55
56 #include "gstzxing.h"
57
58 #include <string.h>
59 #include <math.h>
60
61 #include <gst/video/video.h>
62
63 #include "ReadBarcode.h"
64 #include "TextUtfEncoding.h"
65 #include "ZXVersion.h"
66
67 using namespace ZXing;
68
69 GST_DEBUG_CATEGORY_STATIC (zxing_debug);
70 #define GST_CAT_DEFAULT zxing_debug
71
72 #define DEFAULT_MESSAGE  TRUE
73 #define DEFAULT_ATTACH_FRAME FALSE
74 #define DEFAULT_TRY_ROTATE FALSE
75 #define DEFAULT_TRY_FASTER FALSE
76
77 enum
78 {
79   PROP_0,
80   PROP_MESSAGE,
81   PROP_ATTACH_FRAME,
82   PROP_TRY_ROTATE,
83   PROP_TRY_FASTER,
84   PROP_FORMAT,
85 };
86
87 enum
88 {
89   BARCODE_FORMAT_ALL,
90   BARCODE_FORMAT_AZTEC,
91   BARCODE_FORMAT_CODABAR,
92   BARCODE_FORMAT_CODE_39,
93   BARCODE_FORMAT_CODE_93,
94   BARCODE_FORMAT_CODE_128,
95   BARCODE_FORMAT_DATA_MATRIX,
96   BARCODE_FORMAT_EAN_8,
97   BARCODE_FORMAT_EAN_13,
98   BARCODE_FORMAT_ITF,
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
107 };
108
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"},
128   {0, NULL, NULL},
129 };
130
131 #define GST_TYPE_BARCODE_FORMAT (gst_barcode_format_get_type())
132 static GType
133 gst_barcode_format_get_type (void)
134 {
135   static GType barcode_format_type = 0;
136
137   if (!barcode_format_type) {
138     barcode_format_type =
139         g_enum_register_static ("GstBarCodeFormat", barcode_formats);
140   }
141   return barcode_format_type;
142 }
143
144 #define ZXING_YUV_CAPS \
145     "{ARGB, xRGB, Y444, Y42B, I420, Y41B, YUV9, YV12}"
146
147
148 static GstStaticPadTemplate gst_zxing_src_template =
149 GST_STATIC_PAD_TEMPLATE ("src",
150     GST_PAD_SRC,
151     GST_PAD_ALWAYS,
152     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZXING_YUV_CAPS))
153     );
154
155 static GstStaticPadTemplate gst_zxing_sink_template =
156 GST_STATIC_PAD_TEMPLATE ("sink",
157     GST_PAD_SINK,
158     GST_PAD_ALWAYS,
159     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZXING_YUV_CAPS))
160     );
161
162 /**
163  * GstZXing:
164  *
165  * Opaque data structure.
166  */
167 struct _GstZXing
168 {
169   /*< private > */
170   GstVideoFilter videofilter;
171
172   /* properties */
173   gboolean message;
174   gboolean attach_frame;
175   gboolean rotate;
176   gboolean faster;
177   ImageFormat image_format;
178   guint barcode_format;
179 };
180
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);
189
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);
196
197 static void
198 gst_zxing_class_init (GstZXingClass * g_class)
199 {
200   GObjectClass *gobject_class;
201   GstElementClass *gstelement_class;
202   GstVideoFilterClass *vfilter_class;
203
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);
207
208   gobject_class->set_property = gst_zxing_set_property;
209   gobject_class->get_property = gst_zxing_get_property;
210
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",
214           DEFAULT_MESSAGE,
215           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
216
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)));
222
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)",
226           DEFAULT_TRY_ROTATE,
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)));
236
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>");
241
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);
246
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);
251 }
252
253 static void
254 gst_zxing_init (GstZXing * zxing)
255 {
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;
262 }
263
264 static void
265 gst_zxing_set_property (GObject * object, guint prop_id, const GValue * value,
266     GParamSpec * pspec)
267 {
268   GstZXing *zxing;
269
270   g_return_if_fail (GST_IS_ZXING (object));
271   zxing = GST_ZXING (object);
272
273   switch (prop_id) {
274     case PROP_MESSAGE:
275       zxing->message = g_value_get_boolean (value);
276       break;
277     case PROP_ATTACH_FRAME:
278       zxing->attach_frame = g_value_get_boolean (value);
279       break;
280     case PROP_TRY_ROTATE:
281       zxing->rotate = g_value_get_boolean (value);
282       break;
283     case PROP_TRY_FASTER:
284       zxing->faster = g_value_get_boolean (value);
285       break;
286     case PROP_FORMAT:
287       zxing->barcode_format = g_value_get_enum (value);
288       break;
289     default:
290       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291       break;
292   }
293 }
294
295 static void
296 gst_zxing_get_property (GObject * object, guint prop_id, GValue * value,
297     GParamSpec * pspec)
298 {
299   GstZXing *zxing;
300
301   g_return_if_fail (GST_IS_ZXING (object));
302   zxing = GST_ZXING (object);
303
304   switch (prop_id) {
305     case PROP_MESSAGE:
306       g_value_set_boolean (value, zxing->message);
307       break;
308     case PROP_ATTACH_FRAME:
309       g_value_set_boolean (value, zxing->attach_frame);
310       break;
311     case PROP_TRY_ROTATE:
312       g_value_set_boolean (value, zxing->rotate);
313       break;
314     case PROP_TRY_FASTER:
315       g_value_set_boolean (value, zxing->faster);
316       break;
317     case PROP_FORMAT:
318       g_value_set_enum (value, zxing->barcode_format);
319       break;
320     default:
321       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322       break;
323   }
324 }
325
326 static gboolean
327 gst_zxing_set_info (GstVideoFilter * vfilter, GstCaps * in,
328     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
329 {
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;
335       break;
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;
343       break;
344     default:
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));
347   }
348   return TRUE;
349 }
350
351 static GstFlowReturn
352 gst_zxing_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
353 {
354   GstZXing *zxing = GST_ZXING (vfilter);
355   gpointer data;
356   gint height, width;
357   DecodeHints hints;
358
359   hints.setTryRotate(zxing->rotate);
360   hints.setTryHarder(!zxing->faster);
361   hints.setFormats(BarcodeFormatFromString (barcode_formats[zxing->barcode_format].value_name));
362
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);
368
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 ());
375 #else
376         ToString (result.format ()));
377 #endif
378   } else {
379     goto out;
380   }
381
382   /* extract results */
383   if (zxing->message) {
384     GstMessage *m;
385     GstStructure *s;
386     GstSample *sample;
387     GstCaps *sample_caps;
388     GstClockTime timestamp, running_time, stream_time;
389
390     timestamp = GST_BUFFER_TIMESTAMP (frame->buffer);
391     running_time =
392         gst_segment_to_running_time (&GST_BASE_TRANSFORM (zxing)->segment,
393         GST_FORMAT_TIME, timestamp);
394     stream_time =
395         gst_segment_to_stream_time (&GST_BASE_TRANSFORM (zxing)->segment,
396         GST_FORMAT_TIME, timestamp);
397
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 (),
404 #else
405         "type", G_TYPE_STRING, ToString (result.format ()),
406 #endif
407         "symbol", G_TYPE_STRING,
408         result.text ().c_str (), NULL);
409
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);
417     }
418     m = gst_message_new_element (GST_OBJECT (zxing), s);
419     gst_element_post_message (GST_ELEMENT (zxing), m);
420
421   } else if (zxing->attach_frame)
422     GST_WARNING_OBJECT (zxing,
423         "attach-frame=true has no effect if message=false");
424
425 out:
426   return GST_FLOW_OK;
427 }