2 * Copyright (C) <2007> Wim Taymans <wim@fluendo.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 Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
20 * SECTION:element-simplevideomarkdetect
21 * @title: simplevideomarkdetect
22 * @see_also: #GstVideoMark
24 * This plugin detects #GstSimpleVideoMarkDetect:pattern-count squares in the bottom left
25 * corner of the video frames. The squares have a width and height of
26 * respectively #GstSimpleVideoMarkDetect:pattern-width and #GstSimpleVideoMarkDetect:pattern-height.
27 * Even squares must be black and odd squares must be white.
29 * When the pattern has been found, #GstSimpleVideoMarkDetect:pattern-data-count squares
30 * after the pattern squares are read as a bitarray. White squares represent a 1
31 * bit and black squares a 0 bit. The bitarray will will included in the element
32 * message that is posted (see below).
34 * After the pattern has been found and the data pattern has been read, an
35 * element message called `GstSimpleVideoMarkDetect` will
36 * be posted on the bus. If the pattern is no longer found in the frame, the
37 * same element message is posted with the have-pattern field set to %FALSE.
38 * The message is only posted if the #GstSimpleVideoMarkDetect:message property is %TRUE.
40 * The message's structure contains these fields:
42 * * #gboolean`have-pattern`: if the pattern was found. This field will be set to %TRUE for as long as
43 * the pattern was found in the frame and set to FALSE for the first frame
44 * that does not contain the pattern anymore.
46 * * #GstClockTime `timestamp`: the timestamp of the buffer that triggered the message.
48 * * #GstClockTime `stream-time`: the stream time of the buffer.
50 * * #GstClockTime `running-time`: the running_time of the buffer.
52 * * #GstClockTime `duration`: the duration of the buffer.
54 * * #guint64 `data`: the data-pattern found after the pattern or 0 when have-signal is %FALSE.
56 * ## Example launch line
58 * gst-launch-1.0 videotestsrc ! simplevideomarkdetect ! videoconvert ! ximagesink
68 #include <gst/video/video.h>
69 #include <gst/video/gstvideofilter.h>
70 #include "gstsimplevideomarkdetect.h"
72 GST_DEBUG_CATEGORY_STATIC (gst_video_detect_debug_category);
73 #define GST_CAT_DEFAULT gst_video_detect_debug_category
78 static void gst_video_detect_set_property (GObject * object,
79 guint property_id, const GValue * value, GParamSpec * pspec);
80 static void gst_video_detect_get_property (GObject * object,
81 guint property_id, GValue * value, GParamSpec * pspec);
82 static void gst_video_detect_dispose (GObject * object);
83 static void gst_video_detect_finalize (GObject * object);
85 static gboolean gst_video_detect_start (GstBaseTransform * trans);
86 static gboolean gst_video_detect_stop (GstBaseTransform * trans);
87 static gboolean gst_video_detect_set_info (GstVideoFilter * filter,
88 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
89 GstVideoInfo * out_info);
90 static GstFlowReturn gst_video_detect_transform_frame_ip (GstVideoFilter *
91 filter, GstVideoFrame * frame);
100 PROP_PATTERN_DATA_COUNT,
102 PROP_PATTERN_SENSITIVITY,
107 #define DEFAULT_MESSAGE TRUE
108 #define DEFAULT_PATTERN_WIDTH 4
109 #define DEFAULT_PATTERN_HEIGHT 16
110 #define DEFAULT_PATTERN_COUNT 4
111 #define DEFAULT_PATTERN_DATA_COUNT 5
112 #define DEFAULT_PATTERN_CENTER 0.5
113 #define DEFAULT_PATTERN_SENSITIVITY 0.3
114 #define DEFAULT_LEFT_OFFSET 0
115 #define DEFAULT_BOTTOM_OFFSET 0
120 GST_VIDEO_CAPS_MAKE( \
121 "{ I420, YV12, Y41B, Y42B, Y444, YUY2, UYVY, AYUV, YVYU }")
124 /* class initialization */
126 G_DEFINE_TYPE_WITH_CODE (GstSimpleVideoMarkDetect, gst_video_detect,
127 GST_TYPE_VIDEO_FILTER,
128 GST_DEBUG_CATEGORY_INIT (gst_video_detect_debug_category,
129 "simplevideomarkdetect", 0,
130 "debug category for simplevideomarkdetect element"));
131 GST_ELEMENT_REGISTER_DEFINE (simplevideomarkdetect,
132 "simplevideomarkdetect", GST_RANK_NONE, GST_TYPE_SIMPLE_VIDEO_MARK_DETECT);
135 gst_video_detect_class_init (GstSimpleVideoMarkDetectClass * klass)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
138 GstBaseTransformClass *base_transform_class =
139 GST_BASE_TRANSFORM_CLASS (klass);
140 GstVideoFilterClass *video_filter_class = GST_VIDEO_FILTER_CLASS (klass);
142 gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
143 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
144 gst_caps_from_string (VIDEO_CAPS)));
145 gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
146 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
147 gst_caps_from_string (VIDEO_CAPS)));
149 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
150 "Video detecter", "Filter/Effect/Video",
151 "Detect patterns in a video signal", "Wim Taymans <wim@fluendo.com>");
153 gobject_class->set_property = gst_video_detect_set_property;
154 gobject_class->get_property = gst_video_detect_get_property;
155 gobject_class->dispose = gst_video_detect_dispose;
156 gobject_class->finalize = gst_video_detect_finalize;
157 base_transform_class->start = GST_DEBUG_FUNCPTR (gst_video_detect_start);
158 base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_video_detect_stop);
159 video_filter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_detect_set_info);
160 video_filter_class->transform_frame_ip =
161 GST_DEBUG_FUNCPTR (gst_video_detect_transform_frame_ip);
163 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MESSAGE,
164 g_param_spec_boolean ("message", "Message",
165 "Post detected data as bus messages",
167 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (gobject_class, PROP_PATTERN_WIDTH,
169 g_param_spec_int ("pattern-width", "Pattern width",
170 "The width of the pattern markers", 1, G_MAXINT,
171 DEFAULT_PATTERN_WIDTH,
172 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
173 g_object_class_install_property (gobject_class, PROP_PATTERN_HEIGHT,
174 g_param_spec_int ("pattern-height", "Pattern height",
175 "The height of the pattern markers", 1, G_MAXINT,
176 DEFAULT_PATTERN_HEIGHT,
177 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
178 g_object_class_install_property (gobject_class, PROP_PATTERN_COUNT,
179 g_param_spec_int ("pattern-count", "Pattern count",
180 "The number of pattern markers", 0, G_MAXINT,
181 DEFAULT_PATTERN_COUNT,
182 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (gobject_class, PROP_PATTERN_DATA_COUNT,
184 g_param_spec_int ("pattern-data-count", "Pattern data count",
185 "The number of extra data pattern markers", 0, G_MAXINT,
186 DEFAULT_PATTERN_DATA_COUNT,
187 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
188 g_object_class_install_property (gobject_class, PROP_PATTERN_CENTER,
189 g_param_spec_double ("pattern-center", "Pattern center",
190 "The center of the black/white separation (0.0 = lowest, 1.0 highest)",
191 0.0, 1.0, DEFAULT_PATTERN_CENTER,
192 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, PROP_PATTERN_SENSITIVITY,
194 g_param_spec_double ("pattern-sensitivity", "Pattern sensitivity",
195 "The sensitivity around the center for detecting the markers "
196 "(0.0 = lowest, 1.0 highest)", 0.0, 1.0, DEFAULT_PATTERN_SENSITIVITY,
197 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_LEFT_OFFSET,
199 g_param_spec_int ("left-offset", "Left Offset",
200 "The offset from the left border where the pattern starts", 0,
201 G_MAXINT, DEFAULT_LEFT_OFFSET,
202 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_BOTTOM_OFFSET,
204 g_param_spec_int ("bottom-offset", "Bottom Offset",
205 "The offset from the bottom border where the pattern starts", 0,
206 G_MAXINT, DEFAULT_BOTTOM_OFFSET,
207 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
211 gst_video_detect_init (GstSimpleVideoMarkDetect * simplevideomarkdetect)
216 gst_video_detect_set_property (GObject * object, guint property_id,
217 const GValue * value, GParamSpec * pspec)
219 GstSimpleVideoMarkDetect *simplevideomarkdetect =
220 GST_SIMPLE_VIDEO_MARK_DETECT (object);
222 GST_DEBUG_OBJECT (simplevideomarkdetect, "set_property");
224 switch (property_id) {
226 simplevideomarkdetect->message = g_value_get_boolean (value);
228 case PROP_PATTERN_WIDTH:
229 simplevideomarkdetect->pattern_width = g_value_get_int (value);
231 case PROP_PATTERN_HEIGHT:
232 simplevideomarkdetect->pattern_height = g_value_get_int (value);
234 case PROP_PATTERN_COUNT:
235 simplevideomarkdetect->pattern_count = g_value_get_int (value);
237 case PROP_PATTERN_DATA_COUNT:
238 simplevideomarkdetect->pattern_data_count = g_value_get_int (value);
240 case PROP_PATTERN_CENTER:
241 simplevideomarkdetect->pattern_center = g_value_get_double (value);
243 case PROP_PATTERN_SENSITIVITY:
244 simplevideomarkdetect->pattern_sensitivity = g_value_get_double (value);
246 case PROP_LEFT_OFFSET:
247 simplevideomarkdetect->left_offset = g_value_get_int (value);
249 case PROP_BOTTOM_OFFSET:
250 simplevideomarkdetect->bottom_offset = g_value_get_int (value);
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
259 gst_video_detect_get_property (GObject * object, guint property_id,
260 GValue * value, GParamSpec * pspec)
262 GstSimpleVideoMarkDetect *simplevideomarkdetect =
263 GST_SIMPLE_VIDEO_MARK_DETECT (object);
265 GST_DEBUG_OBJECT (simplevideomarkdetect, "get_property");
267 switch (property_id) {
269 g_value_set_boolean (value, simplevideomarkdetect->message);
271 case PROP_PATTERN_WIDTH:
272 g_value_set_int (value, simplevideomarkdetect->pattern_width);
274 case PROP_PATTERN_HEIGHT:
275 g_value_set_int (value, simplevideomarkdetect->pattern_height);
277 case PROP_PATTERN_COUNT:
278 g_value_set_int (value, simplevideomarkdetect->pattern_count);
280 case PROP_PATTERN_DATA_COUNT:
281 g_value_set_int (value, simplevideomarkdetect->pattern_data_count);
283 case PROP_PATTERN_CENTER:
284 g_value_set_double (value, simplevideomarkdetect->pattern_center);
286 case PROP_PATTERN_SENSITIVITY:
287 g_value_set_double (value, simplevideomarkdetect->pattern_sensitivity);
289 case PROP_LEFT_OFFSET:
290 g_value_set_int (value, simplevideomarkdetect->left_offset);
292 case PROP_BOTTOM_OFFSET:
293 g_value_set_int (value, simplevideomarkdetect->bottom_offset);
296 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
302 gst_video_detect_dispose (GObject * object)
304 GstSimpleVideoMarkDetect *simplevideomarkdetect =
305 GST_SIMPLE_VIDEO_MARK_DETECT (object);
307 GST_DEBUG_OBJECT (simplevideomarkdetect, "dispose");
309 /* clean up as possible. may be called multiple times */
311 G_OBJECT_CLASS (gst_video_detect_parent_class)->dispose (object);
315 gst_video_detect_finalize (GObject * object)
317 GstSimpleVideoMarkDetect *simplevideomarkdetect =
318 GST_SIMPLE_VIDEO_MARK_DETECT (object);
320 GST_DEBUG_OBJECT (simplevideomarkdetect, "finalize");
322 /* clean up object here */
324 G_OBJECT_CLASS (gst_video_detect_parent_class)->finalize (object);
328 gst_video_detect_start (GstBaseTransform * trans)
330 GstSimpleVideoMarkDetect *simplevideomarkdetect =
331 GST_SIMPLE_VIDEO_MARK_DETECT (trans);
333 GST_DEBUG_OBJECT (simplevideomarkdetect, "start");
339 gst_video_detect_stop (GstBaseTransform * trans)
341 GstSimpleVideoMarkDetect *simplevideomarkdetect =
342 GST_SIMPLE_VIDEO_MARK_DETECT (trans);
344 GST_DEBUG_OBJECT (simplevideomarkdetect, "stop");
350 gst_video_detect_set_info (GstVideoFilter * filter, GstCaps * incaps,
351 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
353 GstSimpleVideoMarkDetect *simplevideomarkdetect =
354 GST_SIMPLE_VIDEO_MARK_DETECT (filter);
356 GST_DEBUG_OBJECT (simplevideomarkdetect, "set_info");
362 gst_video_detect_post_message (GstSimpleVideoMarkDetect * simplevideomarkdetect,
363 GstBuffer * buffer, guint64 data)
365 GstBaseTransform *trans;
367 guint64 duration, timestamp, running_time, stream_time;
369 trans = GST_BASE_TRANSFORM_CAST (simplevideomarkdetect);
372 timestamp = GST_BUFFER_TIMESTAMP (buffer);
373 duration = GST_BUFFER_DURATION (buffer);
374 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
376 stream_time = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
380 m = gst_message_new_element (GST_OBJECT_CAST (simplevideomarkdetect),
381 gst_structure_new ("GstSimpleVideoMarkDetect",
382 "have-pattern", G_TYPE_BOOLEAN, simplevideomarkdetect->in_pattern,
383 "timestamp", G_TYPE_UINT64, timestamp,
384 "stream-time", G_TYPE_UINT64, stream_time,
385 "running-time", G_TYPE_UINT64, running_time,
386 "duration", G_TYPE_UINT64, duration,
387 "data", G_TYPE_UINT64, data, NULL));
388 gst_element_post_message (GST_ELEMENT_CAST (simplevideomarkdetect), m);
392 gst_video_detect_calc_brightness (GstSimpleVideoMarkDetect *
393 simplevideomarkdetect, guint8 * data, gint width, gint height,
394 gint row_stride, gint pixel_stride)
400 for (i = 0; i < height; i++) {
401 for (j = 0; j < width; j++) {
402 sum += data[pixel_stride * j];
406 return sum / (255.0 * width * height);
410 calculate_pw (gint pw, gint x, gint width)
414 else if ((x + pw) > width)
421 gst_video_detect_yuv (GstSimpleVideoMarkDetect * simplevideomarkdetect,
422 GstVideoFrame * frame)
425 gint i, pw, ph, row_stride, pixel_stride;
426 gint width, height, offset_calc, x, y;
428 guint64 pattern_data;
431 width = frame->info.width;
432 height = frame->info.height;
434 pw = simplevideomarkdetect->pattern_width;
435 ph = simplevideomarkdetect->pattern_height;
436 row_stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
437 pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
439 d = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
440 /* move to start of bottom left, adjust for offsets */
442 row_stride * (height - ph - simplevideomarkdetect->bottom_offset) +
443 pixel_stride * simplevideomarkdetect->left_offset;
444 x = simplevideomarkdetect->left_offset;
445 y = height - ph - simplevideomarkdetect->bottom_offset;
448 simplevideomarkdetect->pattern_count +
449 simplevideomarkdetect->pattern_data_count;
450 /* If x and y offset values are outside the video, no need to analyze */
451 if ((x + (pw * total_pattern)) < 0 || x > width || (y + height) < 0
453 GST_ERROR_OBJECT (simplevideomarkdetect,
454 "simplevideomarkdetect pattern is outside the video. Not Analyzing.");
458 /* Offset calculation less than 0, then reset to 0 */
461 /* Y position of mark is negative or pattern exceeds the video height,
462 then recalculate pattern height for partial display */
465 else if ((y + ph) > height)
467 /* If pattern height is less than 0, need not analyze anything */
471 /* move to start of bottom left */
474 /* analyze the bottom left pixels */
475 for (i = 0; i < simplevideomarkdetect->pattern_count; i++) {
477 /* calc brightness of width * height box */
479 gst_video_detect_calc_brightness (simplevideomarkdetect, d, pw, ph,
480 row_stride, pixel_stride);
482 GST_DEBUG_OBJECT (simplevideomarkdetect, "brightness %f", brightness);
485 /* odd pixels must be white, all pixels darker than the center +
486 * sensitivity are considered wrong. */
488 (simplevideomarkdetect->pattern_center +
489 simplevideomarkdetect->pattern_sensitivity))
492 /* even pixels must be black, pixels lighter than the center - sensitivity
493 * are considered wrong. */
495 (simplevideomarkdetect->pattern_center -
496 simplevideomarkdetect->pattern_sensitivity))
500 /* X position of mark is negative or pattern exceeds the video width,
501 then recalculate pattern width for partial display */
502 draw_pw = calculate_pw (pw, x, width);
503 /* If pattern width is less than 0, continue with the next pattern */
507 /* move to i-th pattern */
508 d += pixel_stride * draw_pw;
511 if ((x + (pw * (total_pattern - i - 1))) < 0 || x >= width)
514 GST_DEBUG_OBJECT (simplevideomarkdetect, "found pattern");
518 /* get the data of the pattern */
519 for (i = 0; i < simplevideomarkdetect->pattern_data_count; i++) {
521 /* calc brightness of width * height box */
523 gst_video_detect_calc_brightness (simplevideomarkdetect, d, pw, ph,
524 row_stride, pixel_stride);
525 /* update pattern, we just use the center to decide between black and white. */
527 if (brightness > simplevideomarkdetect->pattern_center)
530 /* X position of mark is negative or pattern exceeds the video width,
531 then recalculate pattern width for partial display */
532 draw_pw = calculate_pw (pw, x, width);
533 /* If pattern width is less than 0, continue with the next pattern */
537 /* move to i-th pattern data */
538 d += pixel_stride * draw_pw;
541 if ((x + (pw * (simplevideomarkdetect->pattern_data_count - i - 1))) < 0
546 GST_DEBUG_OBJECT (simplevideomarkdetect, "have data %" G_GUINT64_FORMAT,
549 simplevideomarkdetect->in_pattern = TRUE;
550 gst_video_detect_post_message (simplevideomarkdetect, frame->buffer,
557 GST_DEBUG_OBJECT (simplevideomarkdetect, "no pattern found");
558 if (simplevideomarkdetect->in_pattern) {
559 simplevideomarkdetect->in_pattern = FALSE;
560 gst_video_detect_post_message (simplevideomarkdetect, frame->buffer, 0);
567 gst_video_detect_transform_frame_ip (GstVideoFilter * filter,
568 GstVideoFrame * frame)
570 GstSimpleVideoMarkDetect *simplevideomarkdetect =
571 GST_SIMPLE_VIDEO_MARK_DETECT (filter);
573 GST_DEBUG_OBJECT (simplevideomarkdetect, "transform_frame_ip");
575 gst_video_detect_yuv (simplevideomarkdetect, frame);