ext/mpeg2dec/gstmpeg2dec.c: Don't send things to NULL PAD_PEERs
[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 GstElementStateReturn gst_video_crop_change_state (GstElement * element);
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   /* it's not null if we got it, but it might not be ours */
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 = gst_pad_alloc_buffer (video_crop->srcpad, GST_BUFFER_OFFSET (buffer),
525       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 GstElementStateReturn
536 gst_video_crop_change_state (GstElement * element)
537 {
538   GstVideoCrop *video_crop;
539
540   video_crop = GST_VIDEO_CROP (element);
541
542   switch (GST_STATE_TRANSITION (element)) {
543     case GST_STATE_NULL_TO_READY:
544       video_crop->renegotiate_src_caps = TRUE;
545       break;
546     case GST_STATE_READY_TO_PAUSED:
547       break;
548     case GST_STATE_PAUSED_TO_PLAYING:
549       break;
550     case GST_STATE_PLAYING_TO_PAUSED:
551       break;
552     case GST_STATE_PAUSED_TO_READY:
553       break;
554     case GST_STATE_READY_TO_NULL:
555       break;
556   }
557
558   if (parent_class->change_state != NULL)
559     return parent_class->change_state (element);
560
561   return GST_STATE_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, GST_ORIGIN)