Merge branch 'move_subdir_rtsp-server' into tizen_gst_1.19.2_mono
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst / videosignal / gstsimplevideomark.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim@fluendo.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 Street, Suite 500,
17  * Boston, MA 02110-1335, USA.
18  */
19 /**
20  * SECTION:element-simplevideomark
21  * @title: simplevideomark
22  * @see_also: #GstSimpleVideoMarkDetect
23  *
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.
28  *
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.
33  *
34  * The element can be enabled with the #GstSimpleVideoMark:enabled property. It is
35  * mostly used together with the #GstSimpleVideoMarkDetect plugin.
36  *
37  * ## Example launch line
38  * |[
39  * gst-launch-1.0 videotestsrc ! simplevideomark ! videoconvert ! ximagesink
40  * ]| Add the default black/white squares at the bottom left of the video frames.
41  *
42  */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <gst/gst.h>
49 #include <gst/video/video.h>
50 #include <gst/video/gstvideofilter.h>
51 #include "gstsimplevideomark.h"
52
53 GST_DEBUG_CATEGORY_STATIC (gst_video_mark_debug_category);
54 #define GST_CAT_DEFAULT gst_video_mark_debug_category
55
56 /* prototypes */
57
58
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);
65
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);
73
74 enum
75 {
76   PROP_0,
77   PROP_PATTERN_WIDTH,
78   PROP_PATTERN_HEIGHT,
79   PROP_PATTERN_COUNT,
80   PROP_PATTERN_DATA_COUNT,
81   PROP_PATTERN_DATA,
82   PROP_ENABLED,
83   PROP_LEFT_OFFSET,
84   PROP_BOTTOM_OFFSET
85 };
86
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
95
96 /* pad templates */
97
98 #define VIDEO_CAPS \
99     GST_VIDEO_CAPS_MAKE( \
100         "{ I420, YV12, Y41B, Y42B, Y444, YUY2, UYVY, AYUV, YVYU }")
101
102
103 /* class initialization */
104
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);
111
112 static void
113 gst_video_mark_class_init (GstSimpleVideoMarkClass * klass)
114 {
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);
119   GstCaps *tmp = NULL;
120
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);
129
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>");
133
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);
143
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",
172           DEFAULT_ENABLED,
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));
184
185 }
186
187 static void
188 gst_video_mark_init (GstSimpleVideoMark * simplevideomark)
189 {
190 }
191
192 void
193 gst_video_mark_set_property (GObject * object, guint property_id,
194     const GValue * value, GParamSpec * pspec)
195 {
196   GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (object);
197
198   GST_DEBUG_OBJECT (simplevideomark, "set_property");
199
200   switch (property_id) {
201     case PROP_PATTERN_WIDTH:
202       simplevideomark->pattern_width = g_value_get_int (value);
203       break;
204     case PROP_PATTERN_HEIGHT:
205       simplevideomark->pattern_height = g_value_get_int (value);
206       break;
207     case PROP_PATTERN_COUNT:
208       simplevideomark->pattern_count = g_value_get_int (value);
209       break;
210     case PROP_PATTERN_DATA_COUNT:
211       simplevideomark->pattern_data_count = g_value_get_int (value);
212       break;
213     case PROP_PATTERN_DATA:
214       simplevideomark->pattern_data = g_value_get_uint64 (value);
215       break;
216     case PROP_ENABLED:
217       simplevideomark->enabled = g_value_get_boolean (value);
218       break;
219     case PROP_LEFT_OFFSET:
220       simplevideomark->left_offset = g_value_get_int (value);
221       break;
222     case PROP_BOTTOM_OFFSET:
223       simplevideomark->bottom_offset = g_value_get_int (value);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
227       break;
228   }
229 }
230
231 void
232 gst_video_mark_get_property (GObject * object, guint property_id,
233     GValue * value, GParamSpec * pspec)
234 {
235   GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (object);
236
237   GST_DEBUG_OBJECT (simplevideomark, "get_property");
238
239   switch (property_id) {
240     case PROP_PATTERN_WIDTH:
241       g_value_set_int (value, simplevideomark->pattern_width);
242       break;
243     case PROP_PATTERN_HEIGHT:
244       g_value_set_int (value, simplevideomark->pattern_height);
245       break;
246     case PROP_PATTERN_COUNT:
247       g_value_set_int (value, simplevideomark->pattern_count);
248       break;
249     case PROP_PATTERN_DATA_COUNT:
250       g_value_set_int (value, simplevideomark->pattern_data_count);
251       break;
252     case PROP_PATTERN_DATA:
253       g_value_set_uint64 (value, simplevideomark->pattern_data);
254       break;
255     case PROP_ENABLED:
256       g_value_set_boolean (value, simplevideomark->enabled);
257       break;
258     case PROP_LEFT_OFFSET:
259       g_value_set_int (value, simplevideomark->left_offset);
260       break;
261     case PROP_BOTTOM_OFFSET:
262       g_value_set_int (value, simplevideomark->bottom_offset);
263       break;
264     default:
265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
266       break;
267   }
268 }
269
270 void
271 gst_video_mark_dispose (GObject * object)
272 {
273   GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (object);
274
275   GST_DEBUG_OBJECT (simplevideomark, "dispose");
276
277   /* clean up as possible.  may be called multiple times */
278
279   G_OBJECT_CLASS (gst_video_mark_parent_class)->dispose (object);
280 }
281
282 void
283 gst_video_mark_finalize (GObject * object)
284 {
285   GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (object);
286
287   GST_DEBUG_OBJECT (simplevideomark, "finalize");
288
289   /* clean up object here */
290
291   G_OBJECT_CLASS (gst_video_mark_parent_class)->finalize (object);
292 }
293
294 static gboolean
295 gst_video_mark_start (GstBaseTransform * trans)
296 {
297   GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (trans);
298
299   GST_DEBUG_OBJECT (simplevideomark, "start");
300
301   return TRUE;
302 }
303
304 static gboolean
305 gst_video_mark_stop (GstBaseTransform * trans)
306 {
307   GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (trans);
308
309   GST_DEBUG_OBJECT (simplevideomark, "stop");
310
311   return TRUE;
312 }
313
314 static gboolean
315 gst_video_mark_set_info (GstVideoFilter * filter, GstCaps * incaps,
316     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
317 {
318   GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (filter);
319
320   GST_DEBUG_OBJECT (simplevideomark, "set_info");
321
322   return TRUE;
323 }
324
325 static void
326 gst_video_mark_draw_box (GstSimpleVideoMark * simplevideomark, guint8 * data,
327     gint width, gint height, gint row_stride, gint pixel_stride, guint8 color)
328 {
329   gint i, j;
330
331   for (i = 0; i < height; i++) {
332     for (j = 0; j < width; j++) {
333       data[pixel_stride * j] = color;
334     }
335     data += row_stride;
336   }
337 }
338
339 static gint
340 calculate_pw (gint pw, gint x, gint width)
341 {
342   if (x < 0)
343     pw += x;
344   else if ((x + pw) > width)
345     pw = width - x;
346
347   return pw;
348 }
349
350 static GstFlowReturn
351 gst_video_mark_yuv (GstSimpleVideoMark * simplevideomark, GstVideoFrame * frame)
352 {
353   gint i, pw, ph, row_stride, pixel_stride;
354   gint width, height, offset_calc, x, y;
355   guint8 *d;
356   guint64 pattern_shift;
357   guint8 color;
358   gint total_pattern;
359
360   width = frame->info.width;
361   height = frame->info.height;
362
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);
367
368   d = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
369   offset_calc =
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;
374
375   total_pattern =
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
379       || y > height) {
380     GST_ERROR_OBJECT (simplevideomark,
381         "simplevideomark pattern is outside the video. Not drawing.");
382     return GST_FLOW_OK;
383   }
384
385   /* Offset calculation less than 0, then reset to 0 */
386   if (offset_calc < 0)
387     offset_calc = 0;
388   /* Y position of mark is negative or pattern exceeds the video height,
389      then recalculate pattern height for partial display */
390   if (y < 0)
391     ph += y;
392   else if ((y + ph) > height)
393     ph = height - y;
394   /* If pattern height is less than 0, need not draw anything */
395   if (ph < 0)
396     return GST_FLOW_OK;
397
398   /* move to start of bottom left */
399   d += offset_calc;
400
401   /* draw the bottom left pixels */
402   for (i = 0; i < simplevideomark->pattern_count; i++) {
403     gint draw_pw;
404
405     if (i & 1)
406       /* odd pixels must be white */
407       color = 255;
408     else
409       color = 0;
410
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 */
415     if (draw_pw < 0)
416       continue;
417
418     /* draw box of width * height */
419     gst_video_mark_draw_box (simplevideomark, d, draw_pw, ph, row_stride,
420         pixel_stride, color);
421
422     /* move to i-th pattern */
423     d += pixel_stride * draw_pw;
424     x += draw_pw;
425
426     if ((x + (pw * (total_pattern - i - 1))) < 0 || x >= width)
427       return GST_FLOW_OK;
428   }
429
430   pattern_shift =
431       G_GUINT64_CONSTANT (1) << (simplevideomark->pattern_data_count - 1);
432
433   /* get the data of the pattern */
434   for (i = 0; i < simplevideomark->pattern_data_count; i++) {
435     gint draw_pw;
436     if (simplevideomark->pattern_data & pattern_shift)
437       color = 255;
438     else
439       color = 0;
440
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 */
445     if (draw_pw < 0)
446       continue;
447
448     gst_video_mark_draw_box (simplevideomark, d, draw_pw, ph, row_stride,
449         pixel_stride, color);
450
451     pattern_shift >>= 1;
452
453     /* move to i-th pattern data */
454     d += pixel_stride * draw_pw;
455     x += draw_pw;
456
457     if ((x + (pw * (simplevideomark->pattern_data_count - i - 1))) < 0
458         || x >= width)
459       return GST_FLOW_OK;
460   }
461
462   return GST_FLOW_OK;
463 }
464
465
466 static GstFlowReturn
467 gst_video_mark_transform_frame_ip (GstVideoFilter * filter,
468     GstVideoFrame * frame)
469 {
470   GstSimpleVideoMark *simplevideomark = GST_SIMPLE_VIDEO_MARK (filter);
471
472   GST_DEBUG_OBJECT (simplevideomark, "transform_frame_ip");
473
474   if (simplevideomark->enabled)
475     return gst_video_mark_yuv (simplevideomark, frame);
476
477   return GST_FLOW_OK;
478 }