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-simplevideomark
21 * @title: simplevideomark
22 * @see_also: #GstSimpleVideoMarkDetect
24 * This plugin produces #GstSimpleVideoMark:pattern-count squares in the bottom left
25 * corner of the video frames. The squares have a width and height of
26 * respectively #GstSimpleVideoMark:pattern-width and #GstSimpleVideoMark:pattern-height.
27 * Even squares will be black and odd squares will be white.
29 * After writing the pattern, #GstSimpleVideoMark:pattern-data-count squares after the
30 * pattern squares are produced as the bitarray given in
31 * #GstSimpleVideoMark:pattern-data. 1 bits will produce white squares and 0 bits will
32 * produce black squares.
34 * The element can be enabled with the #GstSimpleVideoMark:enabled property. It is
35 * mostly used together with the #GstSimpleVideoMarkDetect plugin.
37 * ## Example launch line
39 * gst-launch-1.0 videotestsrc ! simplevideomark ! videoconvert ! ximagesink
40 * ]| Add the default black/white squares at the bottom left of the video frames.
49 #include <gst/video/video.h>
50 #include <gst/video/gstvideofilter.h>
51 #include "gstsimplevideomark.h"
53 GST_DEBUG_CATEGORY_STATIC (gst_video_mark_debug_category);
54 #define GST_CAT_DEFAULT gst_video_mark_debug_category
59 static void gst_video_mark_set_property (GObject * object,
60 guint property_id, const GValue * value, GParamSpec * pspec);
61 static void gst_video_mark_get_property (GObject * object,
62 guint property_id, GValue * value, GParamSpec * pspec);
63 static void gst_video_mark_dispose (GObject * object);
64 static void gst_video_mark_finalize (GObject * object);
66 static gboolean gst_video_mark_start (GstBaseTransform * trans);
67 static gboolean gst_video_mark_stop (GstBaseTransform * trans);
68 static gboolean gst_video_mark_set_info (GstVideoFilter * filter,
69 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
70 GstVideoInfo * out_info);
71 static GstFlowReturn gst_video_mark_transform_frame_ip (GstVideoFilter * filter,
72 GstVideoFrame * frame);
80 PROP_PATTERN_DATA_COUNT,
87 #define DEFAULT_PATTERN_WIDTH 4
88 #define DEFAULT_PATTERN_HEIGHT 16
89 #define DEFAULT_PATTERN_COUNT 4
90 #define DEFAULT_PATTERN_DATA_COUNT 5
91 #define DEFAULT_PATTERN_DATA 10
92 #define DEFAULT_ENABLED TRUE
93 #define DEFAULT_LEFT_OFFSET 0
94 #define DEFAULT_BOTTOM_OFFSET 0
99 GST_VIDEO_CAPS_MAKE( \
100 "{ I420, YV12, Y41B, Y42B, Y444, YUY2, UYVY, AYUV, YVYU }")
103 /* class initialization */
105 G_DEFINE_TYPE_WITH_CODE (GstSimpleVideoMark, gst_video_mark,
106 GST_TYPE_VIDEO_FILTER,
107 GST_DEBUG_CATEGORY_INIT (gst_video_mark_debug_category, "simplevideomark",
108 0, "debug category for simplevideomark element"));
109 GST_ELEMENT_REGISTER_DEFINE (simplevideomark, "simplevideomark",
110 GST_RANK_NONE, GST_TYPE_SIMPLE_VIDEO_MARK);
113 gst_video_mark_class_init (GstSimpleVideoMarkClass * klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 GstBaseTransformClass *base_transform_class =
117 GST_BASE_TRANSFORM_CLASS (klass);
118 GstVideoFilterClass *video_filter_class = GST_VIDEO_FILTER_CLASS (klass);
121 gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
122 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
123 tmp = gst_caps_from_string (VIDEO_CAPS)));
124 gst_caps_unref (tmp);
125 gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
126 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
127 tmp = gst_caps_from_string (VIDEO_CAPS)));
128 gst_caps_unref (tmp);
130 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
131 "Video marker", "Filter/Effect/Video",
132 "Marks a video signal with a pattern", "Wim Taymans <wim@fluendo.com>");
134 gobject_class->set_property = gst_video_mark_set_property;
135 gobject_class->get_property = gst_video_mark_get_property;
136 gobject_class->dispose = gst_video_mark_dispose;
137 gobject_class->finalize = gst_video_mark_finalize;
138 base_transform_class->start = GST_DEBUG_FUNCPTR (gst_video_mark_start);
139 base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_video_mark_stop);
140 video_filter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_mark_set_info);
141 video_filter_class->transform_frame_ip =
142 GST_DEBUG_FUNCPTR (gst_video_mark_transform_frame_ip);
144 g_object_class_install_property (gobject_class, PROP_PATTERN_WIDTH,
145 g_param_spec_int ("pattern-width", "Pattern width",
146 "The width of the pattern markers", 1, G_MAXINT,
147 DEFAULT_PATTERN_WIDTH,
148 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
149 g_object_class_install_property (gobject_class, PROP_PATTERN_HEIGHT,
150 g_param_spec_int ("pattern-height", "Pattern height",
151 "The height of the pattern markers", 1, G_MAXINT,
152 DEFAULT_PATTERN_HEIGHT,
153 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
154 g_object_class_install_property (gobject_class, PROP_PATTERN_COUNT,
155 g_param_spec_int ("pattern-count", "Pattern count",
156 "The number of pattern markers", 0, G_MAXINT,
157 DEFAULT_PATTERN_COUNT,
158 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_class, PROP_PATTERN_DATA_COUNT,
160 g_param_spec_int ("pattern-data-count", "Pattern data count",
161 "The number of extra data pattern markers", 0, 64,
162 DEFAULT_PATTERN_DATA_COUNT,
163 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
164 g_object_class_install_property (gobject_class, PROP_PATTERN_DATA,
165 g_param_spec_uint64 ("pattern-data", "Pattern data",
166 "The extra data pattern markers", 0, G_MAXUINT64,
167 DEFAULT_PATTERN_DATA,
168 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class, PROP_ENABLED,
170 g_param_spec_boolean ("enabled", "Enabled",
171 "Enable or disable the filter",
173 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
174 g_object_class_install_property (gobject_class, PROP_LEFT_OFFSET,
175 g_param_spec_int ("left-offset", "Left Offset",
176 "The offset from the left border where the pattern starts", 0,
177 G_MAXINT, DEFAULT_LEFT_OFFSET,
178 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
179 g_object_class_install_property (gobject_class, PROP_BOTTOM_OFFSET,
180 g_param_spec_int ("bottom-offset", "Bottom Offset",
181 "The offset from the bottom border where the pattern starts", 0,
182 G_MAXINT, DEFAULT_BOTTOM_OFFSET,
183 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
188 gst_video_mark_init (GstSimpleVideoMark * simplevideomark)
193 gst_video_mark_set_property (GObject * object, guint property_id,
194 const GValue * value, GParamSpec * pspec)
196 GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (object);
198 GST_DEBUG_OBJECT (simplevideomark, "set_property");
200 switch (property_id) {
201 case PROP_PATTERN_WIDTH:
202 simplevideomark->pattern_width = g_value_get_int (value);
204 case PROP_PATTERN_HEIGHT:
205 simplevideomark->pattern_height = g_value_get_int (value);
207 case PROP_PATTERN_COUNT:
208 simplevideomark->pattern_count = g_value_get_int (value);
210 case PROP_PATTERN_DATA_COUNT:
211 simplevideomark->pattern_data_count = g_value_get_int (value);
213 case PROP_PATTERN_DATA:
214 simplevideomark->pattern_data = g_value_get_uint64 (value);
217 simplevideomark->enabled = g_value_get_boolean (value);
219 case PROP_LEFT_OFFSET:
220 simplevideomark->left_offset = g_value_get_int (value);
222 case PROP_BOTTOM_OFFSET:
223 simplevideomark->bottom_offset = g_value_get_int (value);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
232 gst_video_mark_get_property (GObject * object, guint property_id,
233 GValue * value, GParamSpec * pspec)
235 GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (object);
237 GST_DEBUG_OBJECT (simplevideomark, "get_property");
239 switch (property_id) {
240 case PROP_PATTERN_WIDTH:
241 g_value_set_int (value, simplevideomark->pattern_width);
243 case PROP_PATTERN_HEIGHT:
244 g_value_set_int (value, simplevideomark->pattern_height);
246 case PROP_PATTERN_COUNT:
247 g_value_set_int (value, simplevideomark->pattern_count);
249 case PROP_PATTERN_DATA_COUNT:
250 g_value_set_int (value, simplevideomark->pattern_data_count);
252 case PROP_PATTERN_DATA:
253 g_value_set_uint64 (value, simplevideomark->pattern_data);
256 g_value_set_boolean (value, simplevideomark->enabled);
258 case PROP_LEFT_OFFSET:
259 g_value_set_int (value, simplevideomark->left_offset);
261 case PROP_BOTTOM_OFFSET:
262 g_value_set_int (value, simplevideomark->bottom_offset);
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
271 gst_video_mark_dispose (GObject * object)
273 GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (object);
275 GST_DEBUG_OBJECT (simplevideomark, "dispose");
277 /* clean up as possible. may be called multiple times */
279 G_OBJECT_CLASS (gst_video_mark_parent_class)->dispose (object);
283 gst_video_mark_finalize (GObject * object)
285 GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (object);
287 GST_DEBUG_OBJECT (simplevideomark, "finalize");
289 /* clean up object here */
291 G_OBJECT_CLASS (gst_video_mark_parent_class)->finalize (object);
295 gst_video_mark_start (GstBaseTransform * trans)
297 GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (trans);
299 GST_DEBUG_OBJECT (simplevideomark, "start");
305 gst_video_mark_stop (GstBaseTransform * trans)
307 GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (trans);
309 GST_DEBUG_OBJECT (simplevideomark, "stop");
315 gst_video_mark_set_info (GstVideoFilter * filter, GstCaps * incaps,
316 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
318 GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (filter);
320 GST_DEBUG_OBJECT (simplevideomark, "set_info");
326 gst_video_mark_draw_box (GstSimpleVideoMark * simplevideomark, guint8 * data,
327 gint width, gint height, gint row_stride, gint pixel_stride, guint8 color)
331 for (i = 0; i < height; i++) {
332 for (j = 0; j < width; j++) {
333 data[pixel_stride * j] = color;
340 calculate_pw (gint pw, gint x, gint width)
344 else if ((x + pw) > width)
351 gst_video_mark_yuv (GstSimpleVideoMark * simplevideomark, GstVideoFrame * frame)
353 gint i, pw, ph, row_stride, pixel_stride;
354 gint width, height, offset_calc, x, y;
356 guint64 pattern_shift;
360 width = frame->info.width;
361 height = frame->info.height;
363 pw = simplevideomark->pattern_width;
364 ph = simplevideomark->pattern_height;
365 row_stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
366 pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
368 d = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
370 row_stride * (height - ph - simplevideomark->bottom_offset) +
371 pixel_stride * simplevideomark->left_offset;
372 x = simplevideomark->left_offset;
373 y = height - ph - simplevideomark->bottom_offset;
376 simplevideomark->pattern_count + simplevideomark->pattern_data_count;
377 /* If x and y offset values are outside the video, no need to draw */
378 if ((x + (pw * total_pattern)) < 0 || x > width || (y + height) < 0
380 GST_ERROR_OBJECT (simplevideomark,
381 "simplevideomark pattern is outside the video. Not drawing.");
385 /* Offset calculation less than 0, then reset to 0 */
388 /* Y position of mark is negative or pattern exceeds the video height,
389 then recalculate pattern height for partial display */
392 else if ((y + ph) > height)
394 /* If pattern height is less than 0, need not draw anything */
398 /* move to start of bottom left */
401 /* draw the bottom left pixels */
402 for (i = 0; i < simplevideomark->pattern_count; i++) {
406 /* odd pixels must be white */
411 /* X position of mark is negative or pattern exceeds the video width,
412 then recalculate pattern width for partial display */
413 draw_pw = calculate_pw (pw, x, width);
414 /* If pattern width is less than 0, continue with the next pattern */
418 /* draw box of width * height */
419 gst_video_mark_draw_box (simplevideomark, d, draw_pw, ph, row_stride,
420 pixel_stride, color);
422 /* move to i-th pattern */
423 d += pixel_stride * draw_pw;
426 if ((x + (pw * (total_pattern - i - 1))) < 0 || x >= width)
431 G_GUINT64_CONSTANT (1) << (simplevideomark->pattern_data_count - 1);
433 /* get the data of the pattern */
434 for (i = 0; i < simplevideomark->pattern_data_count; i++) {
436 if (simplevideomark->pattern_data & pattern_shift)
441 /* X position of mark is negative or pattern exceeds the video width,
442 then recalculate pattern width for partial display */
443 draw_pw = calculate_pw (pw, x, width);
444 /* If pattern width is less than 0, continue with the next pattern */
448 gst_video_mark_draw_box (simplevideomark, d, draw_pw, ph, row_stride,
449 pixel_stride, color);
453 /* move to i-th pattern data */
454 d += pixel_stride * draw_pw;
457 if ((x + (pw * (simplevideomark->pattern_data_count - i - 1))) < 0
467 gst_video_mark_transform_frame_ip (GstVideoFilter * filter,
468 GstVideoFrame * frame)
470 GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (filter);
472 GST_DEBUG_OBJECT (simplevideomark, "transform_frame_ip");
474 if (simplevideomark->enabled)
475 return gst_video_mark_yuv (simplevideomark, frame);