Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / 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
66 using namespace ZXing;
67
68 GST_DEBUG_CATEGORY_STATIC (zxing_debug);
69 #define GST_CAT_DEFAULT zxing_debug
70
71 #define DEFAULT_MESSAGE  TRUE
72 #define DEFAULT_ATTACH_FRAME FALSE
73 #define DEFAULT_TRY_ROTATE FALSE
74 #define DEFAULT_TRY_FASTER FALSE
75
76 enum
77 {
78   PROP_0,
79   PROP_MESSAGE,
80   PROP_ATTACH_FRAME,
81   PROP_TRY_ROTATE,
82   PROP_TRY_FASTER,
83   PROP_FORMAT,
84 };
85
86 enum
87 {
88   BARCODE_FORMAT_ALL,
89   BARCODE_FORMAT_AZTEC,
90   BARCODE_FORMAT_CODABAR,
91   BARCODE_FORMAT_CODE_39,
92   BARCODE_FORMAT_CODE_93,
93   BARCODE_FORMAT_CODE_128,
94   BARCODE_FORMAT_DATA_MATRIX,
95   BARCODE_FORMAT_EAN_8,
96   BARCODE_FORMAT_EAN_13,
97   BARCODE_FORMAT_ITF,
98   BARCODE_FORMAT_MAXICODE,
99   BARCODE_FORMAT_PDF_417,
100   BARCODE_FORMAT_QR_CODE,
101   BARCODE_FORMAT_RSS_14,
102   BARCODE_FORMAT_RSS_EXPANDED,
103   BARCODE_FORMAT_UPC_A,
104   BARCODE_FORMAT_UPC_E,
105   BARCODE_FORMAT_UPC_EAN_EXTENSION
106 };
107
108 static const GEnumValue barcode_formats[] = {
109   {BARCODE_FORMAT_ALL, "ALL", "all"},
110   {BARCODE_FORMAT_AZTEC, "AZTEC", "aztec"},
111   {BARCODE_FORMAT_CODABAR, "CODABAR", "codabar"},
112   {BARCODE_FORMAT_CODE_39, "CODE_39", "code_39"},
113   {BARCODE_FORMAT_CODE_93, "CODE_93", "code_93"},
114   {BARCODE_FORMAT_CODE_128, "CODE_128", "code_128"},
115   {BARCODE_FORMAT_DATA_MATRIX, "PNG", "png"},
116   {BARCODE_FORMAT_EAN_8, "EAN_8", "ean_8"},
117   {BARCODE_FORMAT_EAN_13, "EAN_13", "ean_13"},
118   {BARCODE_FORMAT_ITF, "ITF", "itf"},
119   {BARCODE_FORMAT_MAXICODE, "MAXICODE", "maxicode"},
120   {BARCODE_FORMAT_PDF_417, "PDF_417", "pdf_417"},
121   {BARCODE_FORMAT_QR_CODE, "QR_CODE", "qr_code"},
122   {BARCODE_FORMAT_RSS_14, "RSS_14", "rss_14"},
123   {BARCODE_FORMAT_RSS_EXPANDED, "RSS_EXPANDED", "rss_expanded"},
124   {BARCODE_FORMAT_UPC_A, "UPC_A", "upc_a"},
125   {BARCODE_FORMAT_UPC_E, "UPC_E", "upc_e"},
126   {BARCODE_FORMAT_UPC_EAN_EXTENSION, "UPC_EAN_EXTENSION", "upc_ean_expansion"},
127   {0, NULL, NULL},
128 };
129
130 #define GST_TYPE_BARCODE_FORMAT (gst_barcode_format_get_type())
131 static GType
132 gst_barcode_format_get_type (void)
133 {
134   static GType barcode_format_type = 0;
135
136   if (!barcode_format_type) {
137     barcode_format_type =
138         g_enum_register_static ("GstBarCodeFormat", barcode_formats);
139   }
140   return barcode_format_type;
141 }
142
143 #define ZXING_YUV_CAPS \
144     "{ARGB, xRGB, Y444, Y42B, I420, Y41B, YUV9, YV12}"
145
146
147 static GstStaticPadTemplate gst_zxing_src_template =
148 GST_STATIC_PAD_TEMPLATE ("src",
149     GST_PAD_SRC,
150     GST_PAD_ALWAYS,
151     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZXING_YUV_CAPS))
152     );
153
154 static GstStaticPadTemplate gst_zxing_sink_template =
155 GST_STATIC_PAD_TEMPLATE ("sink",
156     GST_PAD_SINK,
157     GST_PAD_ALWAYS,
158     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZXING_YUV_CAPS))
159     );
160
161 /**
162  * GstZXing:
163  *
164  * Opaque data structure.
165  */
166 struct _GstZXing
167 {
168   /*< private > */
169   GstVideoFilter videofilter;
170
171   /* properties */
172   gboolean message;
173   gboolean attach_frame;
174   gboolean rotate;
175   gboolean faster;
176   ImageFormat image_format;
177   guint barcode_format;
178 };
179
180 static void gst_zxing_set_property (GObject * object, guint prop_id,
181     const GValue * value, GParamSpec * pspec);
182 static void gst_zxing_get_property (GObject * object, guint prop_id,
183     GValue * value, GParamSpec * pspec);
184 static gboolean gst_zxing_set_info (GstVideoFilter * vfilter, GstCaps * in,
185     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info);
186 static GstFlowReturn gst_zxing_transform_frame_ip (GstVideoFilter * vfilter,
187     GstVideoFrame * frame);
188
189 #define gst_zxing_parent_class parent_class
190 G_DEFINE_TYPE_WITH_CODE (GstZXing, gst_zxing,
191     GST_TYPE_VIDEO_FILTER,
192     GST_DEBUG_CATEGORY_INIT (zxing_debug, "zxing", 0,
193         "debug category for zxing element"));
194 GST_ELEMENT_REGISTER_DEFINE (zxing, "zxing", GST_RANK_MARGINAL, GST_TYPE_ZXING);
195
196 static void
197 gst_zxing_class_init (GstZXingClass * g_class)
198 {
199   GObjectClass *gobject_class;
200   GstElementClass *gstelement_class;
201   GstVideoFilterClass *vfilter_class;
202
203   gobject_class = G_OBJECT_CLASS (g_class);
204   gstelement_class = GST_ELEMENT_CLASS (g_class);
205   vfilter_class = GST_VIDEO_FILTER_CLASS (g_class);
206
207   gobject_class->set_property = gst_zxing_set_property;
208   gobject_class->get_property = gst_zxing_get_property;
209
210   g_object_class_install_property (gobject_class, PROP_MESSAGE,
211       g_param_spec_boolean ("message",
212           "message", "Post a barcode message for each detected code",
213           DEFAULT_MESSAGE,
214           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
215
216   g_object_class_install_property (gobject_class, PROP_ATTACH_FRAME,
217       g_param_spec_boolean ("attach-frame", "Attach frame",
218           "Attach a frame dump to each barcode message",
219           DEFAULT_ATTACH_FRAME,
220           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
221
222   g_object_class_install_property (gobject_class, PROP_TRY_ROTATE,
223       g_param_spec_boolean ("try-rotate", "Try rotate",
224           "Try to rotate the frame to detect barcode (slower)",
225           DEFAULT_TRY_ROTATE,
226           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
227   g_object_class_install_property (gobject_class, PROP_TRY_FASTER,
228       g_param_spec_boolean ("try-faster", "Try faster",
229           "Try faster to analyze the frame", DEFAULT_TRY_FASTER,
230           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
231   g_object_class_install_property (gobject_class, PROP_FORMAT,
232       g_param_spec_enum ("format", "barcode format", "Barcode image format",
233           GST_TYPE_BARCODE_FORMAT, BARCODE_FORMAT_ALL,
234           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
235
236   gst_element_class_set_static_metadata (gstelement_class, "Barcode detector",
237       "Filter/Analyzer/Video",
238       "Detect bar codes in the video streams with zxing library",
239       "Stéphane Cerveau <scerveau@collabora.com>");
240
241   gst_element_class_add_static_pad_template (gstelement_class,
242       &gst_zxing_sink_template);
243   gst_element_class_add_static_pad_template (gstelement_class,
244       &gst_zxing_src_template);
245
246   vfilter_class->transform_frame_ip =
247       GST_DEBUG_FUNCPTR (gst_zxing_transform_frame_ip);
248   vfilter_class->set_info =
249       GST_DEBUG_FUNCPTR (gst_zxing_set_info);
250 }
251
252 static void
253 gst_zxing_init (GstZXing * zxing)
254 {
255   zxing->message = DEFAULT_MESSAGE;
256   zxing->attach_frame = DEFAULT_ATTACH_FRAME;
257   zxing->rotate = DEFAULT_TRY_ROTATE;
258   zxing->faster = DEFAULT_TRY_FASTER;
259   zxing->image_format = ImageFormat::None;
260   zxing->barcode_format = BARCODE_FORMAT_ALL;
261 }
262
263 static void
264 gst_zxing_set_property (GObject * object, guint prop_id, const GValue * value,
265     GParamSpec * pspec)
266 {
267   GstZXing *zxing;
268
269   g_return_if_fail (GST_IS_ZXING (object));
270   zxing = GST_ZXING (object);
271
272   switch (prop_id) {
273     case PROP_MESSAGE:
274       zxing->message = g_value_get_boolean (value);
275       break;
276     case PROP_ATTACH_FRAME:
277       zxing->attach_frame = g_value_get_boolean (value);
278       break;
279     case PROP_TRY_ROTATE:
280       zxing->rotate = g_value_get_boolean (value);
281       break;
282     case PROP_TRY_FASTER:
283       zxing->faster = g_value_get_boolean (value);
284       break;
285     case PROP_FORMAT:
286       zxing->barcode_format = g_value_get_enum (value);
287       break;
288     default:
289       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290       break;
291   }
292 }
293
294 static void
295 gst_zxing_get_property (GObject * object, guint prop_id, GValue * value,
296     GParamSpec * pspec)
297 {
298   GstZXing *zxing;
299
300   g_return_if_fail (GST_IS_ZXING (object));
301   zxing = GST_ZXING (object);
302
303   switch (prop_id) {
304     case PROP_MESSAGE:
305       g_value_set_boolean (value, zxing->message);
306       break;
307     case PROP_ATTACH_FRAME:
308       g_value_set_boolean (value, zxing->attach_frame);
309       break;
310     case PROP_TRY_ROTATE:
311       g_value_set_boolean (value, zxing->rotate);
312       break;
313     case PROP_TRY_FASTER:
314       g_value_set_boolean (value, zxing->faster);
315       break;
316     case PROP_FORMAT:
317       g_value_set_enum (value, zxing->barcode_format);
318       break;
319     default:
320       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321       break;
322   }
323 }
324
325 static gboolean
326 gst_zxing_set_info (GstVideoFilter * vfilter, GstCaps * in,
327     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
328 {
329   GstZXing *zxing = GST_ZXING (vfilter);
330   switch (in_info->finfo->format) {
331     case GST_VIDEO_FORMAT_ARGB:
332     case GST_VIDEO_FORMAT_xRGB:
333       zxing->image_format = ImageFormat::XRGB;
334       break;
335     case GST_VIDEO_FORMAT_Y444:
336     case GST_VIDEO_FORMAT_Y42B:
337     case GST_VIDEO_FORMAT_I420:
338     case GST_VIDEO_FORMAT_Y41B:
339     case GST_VIDEO_FORMAT_YUV9:
340     case GST_VIDEO_FORMAT_YV12:
341       zxing->image_format = ImageFormat::Lum;
342       break;
343     default:
344       zxing->image_format = ImageFormat::None;
345       GST_WARNING_OBJECT (zxing, "This format is not supported %s", gst_video_format_to_string(in_info->finfo->format));
346   }
347   return TRUE;
348 }
349
350 static GstFlowReturn
351 gst_zxing_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
352 {
353   GstZXing *zxing = GST_ZXING (vfilter);
354   gpointer data;
355   gint height, width;
356   DecodeHints hints;
357
358   hints.setTryRotate(zxing->rotate);
359   hints.setTryHarder(!zxing->faster);
360   hints.setFormats(BarcodeFormatFromString (barcode_formats[zxing->barcode_format].value_name));
361
362   /* all formats we support start with an 8-bit Y plane. zxing doesn't need
363    * to know about the chroma plane(s) */
364   data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
365   width = GST_VIDEO_FRAME_WIDTH (frame);
366   height = GST_VIDEO_FRAME_HEIGHT (frame);
367
368   auto result = ReadBarcode ({(unsigned char *)data, width, height, zxing->image_format}, hints);
369   if (result.isValid ()) {
370     GST_DEBUG_OBJECT (zxing, "Symbol found. Text: %s Format: %s",
371         TextUtfEncoding::ToUtf8 (result.text ()).c_str (),
372         ToString (result.format ()));
373   } else {
374     goto out;
375   }
376
377   /* extract results */
378   if (zxing->message) {
379     GstMessage *m;
380     GstStructure *s;
381     GstSample *sample;
382     GstCaps *sample_caps;
383     GstClockTime timestamp, running_time, stream_time;
384
385     timestamp = GST_BUFFER_TIMESTAMP (frame->buffer);
386     running_time =
387         gst_segment_to_running_time (&GST_BASE_TRANSFORM (zxing)->segment,
388         GST_FORMAT_TIME, timestamp);
389     stream_time =
390         gst_segment_to_stream_time (&GST_BASE_TRANSFORM (zxing)->segment,
391         GST_FORMAT_TIME, timestamp);
392
393     s = gst_structure_new ("barcode",
394         "timestamp", G_TYPE_UINT64, timestamp,
395         "stream-time", G_TYPE_UINT64, stream_time,
396         "running-time", G_TYPE_UINT64, running_time,
397         "type", G_TYPE_STRING, ToString (result.format ()),
398         "symbol", G_TYPE_STRING,
399         TextUtfEncoding::ToUtf8 (result.text ()).c_str (), NULL);
400
401     if (zxing->attach_frame) {
402       /* create a sample from image */
403       sample_caps = gst_video_info_to_caps (&frame->info);
404       sample = gst_sample_new (frame->buffer, sample_caps, NULL, NULL);
405       gst_caps_unref (sample_caps);
406       gst_structure_set (s, "frame", GST_TYPE_SAMPLE, sample, NULL);
407       gst_sample_unref (sample);
408     }
409     m = gst_message_new_element (GST_OBJECT (zxing), s);
410     gst_element_post_message (GST_ELEMENT (zxing), m);
411
412   } else if (zxing->attach_frame)
413     GST_WARNING_OBJECT (zxing,
414         "attach-frame=true has no effect if message=false");
415
416 out:
417   return GST_FLOW_OK;
418 }