Unify the long descriptions in the plugin details (#337263).
[platform/upstream/gst-plugins-good.git] / gst / videocrop / gstvideocrop.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <gst/gst.h>
24 #include <gst/video/video.h>
25
26 #include <string.h>
27
28 #define GST_TYPE_VIDEO_CROP \
29   (gst_video_crop_get_type())
30 #define GST_VIDEO_CROP(obj) \
31   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_CROP,GstVideoCrop))
32 #define GST_VIDEO_CROP_CLASS(klass) \
33   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_CROP,GstVideoCropClass))
34 #define GST_IS_VIDEO_CROP(obj) \
35   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_CROP))
36 #define GST_IS_VIDEO_CROP_CLASS(obj) \
37   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_CROP))
38
39 typedef struct _GstVideoCrop GstVideoCrop;
40 typedef struct _GstVideoCropClass GstVideoCropClass;
41
42 struct _GstVideoCrop
43 {
44   GstElement element;
45
46   /* pads */
47   GstPad *sinkpad;
48   GstPad *srcpad;
49
50   /* caps */
51   gint width, height;
52   gint crop_left, crop_right, crop_top, crop_bottom;
53   gboolean renegotiate_src_caps;
54 };
55
56 struct _GstVideoCropClass
57 {
58   GstElementClass parent_class;
59 };
60
61 /* elementfactory information */
62 static GstElementDetails gst_video_crop_details = GST_ELEMENT_DETAILS ("Crop",
63     "Filter/Effect/Video",
64     "Crops video into a user defined region",
65     "Wim Taymans <wim.taymans@chello.be>");
66
67
68 /* VideoCrop args */
69 enum
70 {
71   ARG_0,
72   ARG_LEFT,
73   ARG_RIGHT,
74   ARG_TOP,
75   ARG_BOTTOM
76       /* FILL ME */
77 };
78
79 static GstStaticPadTemplate gst_video_crop_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
81     GST_PAD_SRC,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
84     );
85
86 static GstStaticPadTemplate gst_video_crop_sink_template =
87 GST_STATIC_PAD_TEMPLATE ("sink",
88     GST_PAD_SINK,
89     GST_PAD_ALWAYS,
90     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
91     );
92
93
94 static void gst_video_crop_base_init (gpointer g_class);
95 static void gst_video_crop_class_init (GstVideoCropClass * klass);
96 static void gst_video_crop_init (GstVideoCrop * video_crop);
97
98 static void gst_video_crop_set_property (GObject * object, guint prop_id,
99     const GValue * value, GParamSpec * pspec);
100 static void gst_video_crop_get_property (GObject * object, guint prop_id,
101     GValue * value, GParamSpec * pspec);
102
103 static GstCaps *gst_video_crop_getcaps (GstPad * pad);
104
105 static GstPadLinkReturn
106 gst_video_crop_link (GstPad * pad, const GstCaps * caps);
107 static void gst_video_crop_chain (GstPad * pad, GstData * _data);
108
109 static GstStateChangeReturn gst_video_crop_change_state (GstElement * element,
110     GstStateChange transition);
111
112
113 static GstElementClass *parent_class = NULL;
114
115 /* static guint gst_video_crop_signals[LAST_SIGNAL] = { 0 }; */
116
117 GType
118 gst_video_crop_get_type (void)
119 {
120   static GType video_crop_type = 0;
121
122   if (!video_crop_type) {
123     static const GTypeInfo video_crop_info = {
124       sizeof (GstVideoCropClass),
125       gst_video_crop_base_init,
126       NULL,
127       (GClassInitFunc) gst_video_crop_class_init,
128       NULL,
129       NULL,
130       sizeof (GstVideoCrop),
131       0,
132       (GInstanceInitFunc) gst_video_crop_init,
133     };
134
135     video_crop_type =
136         g_type_register_static (GST_TYPE_ELEMENT, "GstVideoCrop",
137         &video_crop_info, 0);
138   }
139   return video_crop_type;
140 }
141
142 static void
143 gst_video_crop_base_init (gpointer g_class)
144 {
145   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
146
147   gst_element_class_set_details (element_class, &gst_video_crop_details);
148
149   gst_element_class_add_pad_template (element_class,
150       gst_static_pad_template_get (&gst_video_crop_sink_template));
151   gst_element_class_add_pad_template (element_class,
152       gst_static_pad_template_get (&gst_video_crop_src_template));
153 }
154 static void
155 gst_video_crop_class_init (GstVideoCropClass * klass)
156 {
157   GObjectClass *gobject_class;
158   GstElementClass *gstelement_class;
159
160   gobject_class = (GObjectClass *) klass;
161   gstelement_class = (GstElementClass *) klass;
162
163   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
164
165   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEFT,
166       g_param_spec_int ("left", "Left", "Pixels to crop at left",
167           0, G_MAXINT, 0, G_PARAM_READWRITE));
168   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RIGHT,
169       g_param_spec_int ("right", "Right", "Pixels to crop at right",
170           0, G_MAXINT, 0, G_PARAM_READWRITE));
171   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOP,
172       g_param_spec_int ("top", "Top", "Pixels to crop at top",
173           0, G_MAXINT, 0, G_PARAM_READWRITE));
174   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOTTOM,
175       g_param_spec_int ("bottom", "Bottom", "Pixels to crop at bottom",
176           0, G_MAXINT, 0, G_PARAM_READWRITE));
177
178   gobject_class->set_property = gst_video_crop_set_property;
179   gobject_class->get_property = gst_video_crop_get_property;
180
181   gstelement_class->change_state = gst_video_crop_change_state;
182 }
183
184 static void
185 gst_video_crop_init (GstVideoCrop * video_crop)
186 {
187   /* create the sink and src pads */
188   video_crop->sinkpad =
189       gst_pad_new_from_template (gst_static_pad_template_get
190       (&gst_video_crop_sink_template), "sink");
191   gst_element_add_pad (GST_ELEMENT (video_crop), video_crop->sinkpad);
192   gst_pad_set_chain_function (video_crop->sinkpad, gst_video_crop_chain);
193   gst_pad_set_getcaps_function (video_crop->sinkpad, gst_video_crop_getcaps);
194   gst_pad_set_link_function (video_crop->sinkpad, gst_video_crop_link);
195
196   video_crop->srcpad =
197       gst_pad_new_from_template (gst_static_pad_template_get
198       (&gst_video_crop_src_template), "src");
199   gst_element_add_pad (GST_ELEMENT (video_crop), video_crop->srcpad);
200   gst_pad_set_getcaps_function (video_crop->srcpad, gst_video_crop_getcaps);
201   gst_pad_set_link_function (video_crop->srcpad, gst_video_crop_link);
202
203   video_crop->crop_right = 0;
204   video_crop->crop_left = 0;
205   video_crop->crop_top = 0;
206   video_crop->crop_bottom = 0;
207 }
208
209 /* do we need this function? */
210 static void
211 gst_video_crop_set_property (GObject * object, guint prop_id,
212     const GValue * value, GParamSpec * pspec)
213 {
214   GstVideoCrop *video_crop;
215
216   g_return_if_fail (GST_IS_VIDEO_CROP (object));
217
218   video_crop = GST_VIDEO_CROP (object);
219
220   switch (prop_id) {
221     case ARG_LEFT:
222       video_crop->crop_left = g_value_get_int (value);
223       break;
224     case ARG_RIGHT:
225       video_crop->crop_right = g_value_get_int (value);
226       break;
227     case ARG_TOP:
228       video_crop->crop_top = g_value_get_int (value);
229       break;
230     case ARG_BOTTOM:
231       video_crop->crop_bottom = g_value_get_int (value);
232       break;
233     default:
234       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235       break;
236   }
237 }
238 static void
239 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
240     GParamSpec * pspec)
241 {
242   GstVideoCrop *video_crop;
243
244   g_return_if_fail (GST_IS_VIDEO_CROP (object));
245
246   video_crop = GST_VIDEO_CROP (object);
247
248   switch (prop_id) {
249     case ARG_LEFT:
250       g_value_set_int (value, video_crop->crop_left);
251       break;
252     case ARG_RIGHT:
253       g_value_set_int (value, video_crop->crop_right);
254       break;
255     case ARG_TOP:
256       g_value_set_int (value, video_crop->crop_top);
257       break;
258     case ARG_BOTTOM:
259       g_value_set_int (value, video_crop->crop_bottom);
260       break;
261     default:
262       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263       break;
264   }
265
266   if (gst_pad_is_negotiated (video_crop->srcpad))
267     video_crop->renegotiate_src_caps = TRUE;
268 }
269
270 static void
271 gst_video_crop_add_to_struct_val (GstStructure * s, const gchar * field_name,
272     gint addval)
273 {
274   const GValue *val;
275
276   val = gst_structure_get_value (s, field_name);
277
278   if (G_VALUE_HOLDS_INT (val)) {
279     gint ival = g_value_get_int (val);
280
281     gst_structure_set (s, field_name, G_TYPE_INT, ival + addval, NULL);
282     return;
283   }
284
285   if (GST_VALUE_HOLDS_INT_RANGE (val)) {
286     gint min = gst_value_get_int_range_min (val);
287     gint max = gst_value_get_int_range_max (val);
288
289     gst_structure_set (s, field_name, GST_TYPE_INT_RANGE, min + addval,
290         max + addval, NULL);
291     return;
292   }
293
294   if (GST_VALUE_HOLDS_LIST (val)) {
295     GValue newlist = { 0, };
296     gint i;
297
298     g_value_init (&newlist, GST_TYPE_LIST);
299     for (i = 0; i < gst_value_list_get_size (val); ++i) {
300       GValue newval = { 0, };
301       g_value_init (&newval, G_VALUE_TYPE (val));
302       g_value_copy (val, &newval);
303       if (G_VALUE_HOLDS_INT (val)) {
304         gint ival = g_value_get_int (val);
305
306         g_value_set_int (&newval, ival + addval);
307       } else if (GST_VALUE_HOLDS_INT_RANGE (val)) {
308         gint min = gst_value_get_int_range_min (val);
309         gint max = gst_value_get_int_range_max (val);
310
311         gst_value_set_int_range (&newval, min + addval, max + addval);
312       } else {
313         g_return_if_reached ();
314       }
315       gst_value_list_append_value (&newlist, &newval);
316       g_value_unset (&newval);
317     }
318     gst_structure_set_value (s, field_name, &newlist);
319     g_value_unset (&newlist);
320     return;
321   }
322
323   g_return_if_reached ();
324 }
325
326 static GstCaps *
327 gst_video_crop_getcaps (GstPad * pad)
328 {
329   GstVideoCrop *vc;
330   GstCaps *othercaps, *caps;
331   GstPad *otherpad;
332   gint i, delta_w, delta_h;
333
334   vc = GST_VIDEO_CROP (gst_pad_get_parent (pad));
335   otherpad = (pad == vc->srcpad) ? vc->sinkpad : vc->srcpad;
336   othercaps = gst_pad_get_allowed_caps (otherpad);
337
338   GST_DEBUG_OBJECT (pad, "othercaps of otherpad %s:%s are: %" GST_PTR_FORMAT,
339       GST_DEBUG_PAD_NAME (otherpad), othercaps);
340
341   if (pad == vc->srcpad) {
342     delta_w = 0 - vc->crop_left - vc->crop_right;
343     delta_h = 0 - vc->crop_top - vc->crop_bottom;
344   } else {
345     delta_w = vc->crop_left + vc->crop_right;
346     delta_h = vc->crop_top + vc->crop_bottom;
347   }
348
349   for (i = 0; i < gst_caps_get_size (othercaps); i++) {
350     GstStructure *s = gst_caps_get_structure (othercaps, i);
351
352     gst_video_crop_add_to_struct_val (s, "width", delta_w);
353     gst_video_crop_add_to_struct_val (s, "height", delta_h);
354   }
355
356   caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
357   gst_caps_free (othercaps);
358
359   GST_DEBUG_OBJECT (pad, "returning caps: %" GST_PTR_FORMAT, caps);
360   return caps;
361 }
362
363 static GstPadLinkReturn
364 gst_video_crop_link (GstPad * pad, const GstCaps * caps)
365 {
366   GstPadLinkReturn ret;
367   GstStructure *structure;
368   GstVideoCrop *vc;
369   GstCaps *newcaps;
370   GstPad *otherpad;
371   gint w, h, other_w, other_h;
372
373   vc = GST_VIDEO_CROP (gst_pad_get_parent (pad));
374
375   structure = gst_caps_get_structure (caps, 0);
376   if (!gst_structure_get_int (structure, "width", &w)
377       || !gst_structure_get_int (structure, "height", &h))
378     return GST_PAD_LINK_DELAYED;
379
380   if (pad == vc->srcpad) {
381     other_w = w + vc->crop_left + vc->crop_right;
382     other_h = h + vc->crop_top + vc->crop_bottom;
383     otherpad = vc->sinkpad;
384     vc->width = other_w;
385     vc->height = other_h;
386   } else {
387     other_w = w - vc->crop_left - vc->crop_right;
388     other_h = h - vc->crop_top - vc->crop_bottom;
389     vc->width = w;
390     vc->height = h;
391     otherpad = vc->srcpad;
392   }
393
394   newcaps = gst_caps_copy (caps);
395
396   gst_caps_set_simple (newcaps,
397       "width", G_TYPE_INT, other_w, "height", G_TYPE_INT, other_h, NULL);
398
399   ret = gst_pad_try_set_caps (otherpad, newcaps);
400   gst_caps_free (newcaps);
401
402   if (ret == GST_PAD_LINK_REFUSED)
403     return GST_PAD_LINK_REFUSED;
404
405   return GST_PAD_LINK_OK;
406 }
407
408 /* these macros are adapted from videotestsrc.c, paint_setup_I420() */
409 #define ROUND_UP_2(x)  (((x)+1)&~1)
410 #define ROUND_UP_4(x)  (((x)+3)&~3)
411 #define ROUND_UP_8(x)  (((x)+7)&~7)
412
413 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (ROUND_UP_4(width))
414 #define GST_VIDEO_I420_U_ROWSTRIDE(width) (ROUND_UP_8(width)/2)
415 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)
416
417 #define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
418 #define GST_VIDEO_I420_U_OFFSET(w,h) (GST_VIDEO_I420_Y_OFFSET(w,h)+(GST_VIDEO_I420_Y_ROWSTRIDE(w)*ROUND_UP_2(h)))
419 #define GST_VIDEO_I420_V_OFFSET(w,h) (GST_VIDEO_I420_U_OFFSET(w,h)+(GST_VIDEO_I420_U_ROWSTRIDE(w)*ROUND_UP_2(h)/2))
420
421 #define GST_VIDEO_I420_SIZE(w,h) (GST_VIDEO_I420_V_OFFSET(w,h)+(GST_VIDEO_I420_V_ROWSTRIDE(w)*ROUND_UP_2(h)/2))
422
423 static void
424 gst_video_crop_i420 (GstVideoCrop * video_crop, GstBuffer * src_buffer,
425     GstBuffer * dest_buffer)
426 {
427   guint8 *src;
428   guint8 *dest;
429   guint8 *srcY, *srcU, *srcV;
430   guint8 *destY, *destU, *destV;
431   gint out_width = video_crop->width -
432       (video_crop->crop_left + video_crop->crop_right);
433   gint out_height = video_crop->height -
434       (video_crop->crop_top + video_crop->crop_bottom);
435   gint j;
436
437   src = GST_BUFFER_DATA (src_buffer);
438   dest = GST_BUFFER_DATA (dest_buffer);
439
440   srcY = src + GST_VIDEO_I420_Y_OFFSET (video_crop->width, video_crop->height);
441   destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);
442
443   /* copy Y plane first */
444   srcY +=
445       (GST_VIDEO_I420_Y_ROWSTRIDE (video_crop->width) * video_crop->crop_top) +
446       video_crop->crop_left;
447   for (j = 0; j < out_height; j++) {
448     memcpy (destY, srcY, out_width);
449     srcY += GST_VIDEO_I420_Y_ROWSTRIDE (video_crop->width);
450     destY += GST_VIDEO_I420_Y_ROWSTRIDE (out_width);
451   }
452
453   destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);
454   destV = dest + GST_VIDEO_I420_V_OFFSET (out_width, out_height);
455
456   srcU = src + GST_VIDEO_I420_U_OFFSET (video_crop->width, video_crop->height);
457   srcV = src + GST_VIDEO_I420_V_OFFSET (video_crop->width, video_crop->height);
458
459   srcU +=
460       (GST_VIDEO_I420_U_ROWSTRIDE (video_crop->width) * (video_crop->crop_top /
461           2)) + (video_crop->crop_left / 2);
462   srcV +=
463       (GST_VIDEO_I420_V_ROWSTRIDE (video_crop->width) * (video_crop->crop_top /
464           2)) + (video_crop->crop_left / 2);
465
466   for (j = 0; j < out_height / 2; j++) {
467     /* copy U plane */
468     memcpy (destU, srcU, out_width / 2);
469     srcU += GST_VIDEO_I420_U_ROWSTRIDE (video_crop->width);
470     destU += GST_VIDEO_I420_U_ROWSTRIDE (out_width);
471
472     /* copy V plane */
473     memcpy (destV, srcV, out_width / 2);
474     srcV += GST_VIDEO_I420_V_ROWSTRIDE (video_crop->width);
475     destV += GST_VIDEO_I420_V_ROWSTRIDE (out_width);
476   }
477 }
478
479 static void
480 gst_video_crop_chain (GstPad * pad, GstData * _data)
481 {
482   GstBuffer *buffer = GST_BUFFER (_data);
483   GstVideoCrop *video_crop;
484   GstBuffer *outbuf;
485   gint new_width, new_height;
486
487   video_crop = GST_VIDEO_CROP (gst_pad_get_parent (pad));
488
489   new_width = video_crop->width -
490       (video_crop->crop_left + video_crop->crop_right);
491   new_height = video_crop->height -
492       (video_crop->crop_top + video_crop->crop_bottom);
493
494   if (video_crop->renegotiate_src_caps || !GST_PAD_CAPS (video_crop->srcpad)) {
495     GstCaps *newcaps;
496
497     newcaps = gst_caps_copy (gst_pad_get_negotiated_caps (video_crop->sinkpad));
498
499     gst_caps_set_simple (newcaps,
500         "width", G_TYPE_INT, new_width, "height", G_TYPE_INT, new_height, NULL);
501
502     if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_crop->srcpad,
503                 newcaps))) {
504       GST_ELEMENT_ERROR (video_crop, CORE, NEGOTIATION, (NULL), (NULL));
505       gst_caps_free (newcaps);
506       return;
507     }
508
509     gst_caps_free (newcaps);
510
511     video_crop->renegotiate_src_caps = FALSE;
512   }
513
514   /* passthrough if nothing to do */
515   if (new_width == video_crop->width && new_height == video_crop->height) {
516     gst_pad_push (video_crop->srcpad, GST_DATA (buffer));
517     return;
518   }
519
520   g_return_if_fail (GST_BUFFER_SIZE (buffer) >=
521       GST_VIDEO_I420_SIZE (video_crop->width, video_crop->height));
522
523   outbuf =
524       gst_pad_alloc_buffer_and_set_caps (video_crop->srcpad,
525       GST_BUFFER_OFFSET (buffer), GST_VIDEO_I420_SIZE (new_width, new_height));
526
527   gst_buffer_stamp (outbuf, buffer);
528
529   gst_video_crop_i420 (video_crop, buffer, outbuf);
530   gst_buffer_unref (buffer);
531
532   gst_pad_push (video_crop->srcpad, GST_DATA (outbuf));
533 }
534
535 static GstStateChangeReturn
536 gst_video_crop_change_state (GstElement * element, GstStateChange transition)
537 {
538   GstVideoCrop *video_crop;
539
540   video_crop = GST_VIDEO_CROP (element);
541
542   switch (transition) {
543     case GST_STATE_CHANGE_NULL_TO_READY:
544       video_crop->renegotiate_src_caps = TRUE;
545       break;
546     case GST_STATE_CHANGE_READY_TO_PAUSED:
547       break;
548     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
549       break;
550     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
551       break;
552     case GST_STATE_CHANGE_PAUSED_TO_READY:
553       break;
554     case GST_STATE_CHANGE_READY_TO_NULL:
555       break;
556   }
557
558   if (parent_class->change_state != NULL)
559     return parent_class->change_state (element, transition);
560
561   return GST_STATE_CHANGE_SUCCESS;
562 }
563
564 static gboolean
565 plugin_init (GstPlugin * plugin)
566 {
567   return gst_element_register (plugin, "videocrop", GST_RANK_NONE,
568       GST_TYPE_VIDEO_CROP);
569 }
570
571 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
572     GST_VERSION_MINOR,
573     "videocrop",
574     "Crops video into a user defined region",
575     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)