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