updating some plugin categories
[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   GstElement     element;
44
45   /* pads */
46   GstPad        *sinkpad;
47   GstPad        *srcpad;
48
49   /* caps */
50   gint           width, height;
51   gfloat         fps;
52   gint           crop_left, crop_right, crop_top, crop_bottom;
53 };
54
55 struct _GstVideoCropClass {
56   GstElementClass parent_class;
57 };
58
59 /* elementfactory information */
60 static GstElementDetails gst_video_crop_details = {
61   "video crop filter",
62   "Filter/Video",
63   "LGPL",
64   "Crops video into a user defined region",
65   VERSION,
66   "Wim Taymans <wim.taymans@chello.be>",
67   "(C) 2002",
68 };
69
70
71 /* VideoCrop signals and args */
72 enum {
73   /* FILL ME */
74   LAST_SIGNAL
75 };
76
77 enum {
78   ARG_0,
79   ARG_LEFT,
80   ARG_RIGHT,
81   ARG_TOP,
82   ARG_BOTTOM,
83   /* FILL ME */
84 };
85
86 GST_PAD_TEMPLATE_FACTORY (video_crop_src_template_factory,
87   "src",
88   GST_PAD_SRC,
89   GST_PAD_ALWAYS,
90   gst_caps_new (
91     "video_crop_src",
92     "video/x-raw-yuv",
93       GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
94               GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
95   )
96 )
97
98 GST_PAD_TEMPLATE_FACTORY (video_crop_sink_template_factory,
99   "sink",
100   GST_PAD_SINK,
101   GST_PAD_ALWAYS,
102   gst_caps_new (
103     "video_crop_sink",
104     "video/x-raw-yuv",
105       GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
106               GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
107   )
108 )
109
110
111 static void             gst_video_crop_class_init       (GstVideoCropClass *klass);
112 static void             gst_video_crop_init             (GstVideoCrop *video_crop);
113
114 static void             gst_video_crop_set_property     (GObject *object, guint prop_id, 
115                                                          const GValue *value, GParamSpec *pspec);
116 static void             gst_video_crop_get_property     (GObject *object, guint prop_id, 
117                                                          GValue *value, GParamSpec *pspec);
118
119 static GstPadLinkReturn
120                         gst_video_crop_sink_connect     (GstPad *pad, GstCaps *caps);
121 static void             gst_video_crop_chain            (GstPad *pad, GstBuffer *buffer);
122
123 static GstElementStateReturn
124                         gst_video_crop_change_state     (GstElement *element);
125
126
127 static GstElementClass *parent_class = NULL;
128 /* static guint gst_video_crop_signals[LAST_SIGNAL] = { 0 }; */
129
130 GType
131 gst_video_crop_get_type (void)
132 {
133   static GType video_crop_type = 0;
134
135   if (!video_crop_type) {
136     static const GTypeInfo video_crop_info = {
137       sizeof(GstVideoCropClass),      
138       NULL,
139       NULL,
140       (GClassInitFunc)gst_video_crop_class_init,
141       NULL,
142       NULL,
143       sizeof(GstVideoCrop),
144       0,
145       (GInstanceInitFunc)gst_video_crop_init,
146     };
147     video_crop_type = g_type_register_static(GST_TYPE_ELEMENT, "GstVideoCrop", &video_crop_info, 0);
148   }
149   return video_crop_type;
150 }
151
152 static void
153 gst_video_crop_class_init (GstVideoCropClass *klass)
154 {
155   GObjectClass *gobject_class;
156   GstElementClass *gstelement_class;
157
158   gobject_class = (GObjectClass*) klass;
159   gstelement_class = (GstElementClass*) klass;
160
161   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
162
163   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEFT,
164     g_param_spec_int ("left", "Left", "Pixels to crop at left",
165                       0, G_MAXINT, 0, G_PARAM_READWRITE));
166   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RIGHT,
167     g_param_spec_int ("right", "Right", "Pixels to crop at right",
168                       0, G_MAXINT, 0, G_PARAM_READWRITE));
169   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOP,
170     g_param_spec_int ("top", "Top", "Pixels to crop at top",
171                       0, G_MAXINT, 0, G_PARAM_READWRITE));
172   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOTTOM,
173     g_param_spec_int ("bottom", "Bottom", "Pixels to crop at bottom",
174                       0, G_MAXINT, 0, G_PARAM_READWRITE));
175
176   gobject_class->set_property = gst_video_crop_set_property;
177   gobject_class->get_property = gst_video_crop_get_property;
178
179   gstelement_class->change_state = gst_video_crop_change_state;
180 }
181
182 static void
183 gst_video_crop_init (GstVideoCrop *video_crop)
184 {
185   /* create the sink and src pads */
186   video_crop->sinkpad = gst_pad_new_from_template(
187                   GST_PAD_TEMPLATE_GET (video_crop_sink_template_factory), "sink");
188   gst_element_add_pad (GST_ELEMENT (video_crop), video_crop->sinkpad);
189   gst_pad_set_chain_function (video_crop->sinkpad, GST_DEBUG_FUNCPTR (gst_video_crop_chain));
190   gst_pad_set_link_function (video_crop->sinkpad, GST_DEBUG_FUNCPTR (gst_video_crop_sink_connect));
191
192   video_crop->srcpad = gst_pad_new_from_template(
193                   GST_PAD_TEMPLATE_GET (video_crop_src_template_factory), "src");
194   gst_element_add_pad (GST_ELEMENT (video_crop), video_crop->srcpad);
195
196   video_crop->crop_right = 0;
197   video_crop->crop_left = 0;
198   video_crop->crop_top = 0;
199   video_crop->crop_bottom = 0;
200
201   GST_FLAG_SET (video_crop, GST_ELEMENT_EVENT_AWARE);
202 }
203
204 /* do we need this function? */
205 static void
206 gst_video_crop_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
207 {
208   GstVideoCrop *video_crop;
209             
210   /* it's not null if we got it, but it might not be ours */
211   g_return_if_fail (GST_IS_VIDEO_CROP (object));
212               
213   video_crop = GST_VIDEO_CROP (object);
214
215   switch (prop_id) {
216     case ARG_LEFT:
217       video_crop->crop_left = g_value_get_int (value);
218       break;
219     case ARG_RIGHT:
220       video_crop->crop_right = g_value_get_int (value);
221       break;
222     case ARG_TOP:
223       video_crop->crop_top = g_value_get_int (value);
224       break;
225     case ARG_BOTTOM:
226       video_crop->crop_bottom = g_value_get_int (value);
227       break;
228     default:
229       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       break;
231   }
232 }
233 static void
234 gst_video_crop_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
235 {
236   GstVideoCrop *video_crop;
237             
238   /* it's not null if we got it, but it might not be ours */
239   g_return_if_fail (GST_IS_VIDEO_CROP (object));
240               
241   video_crop = GST_VIDEO_CROP (object);
242
243   switch (prop_id) {
244     case ARG_LEFT:
245       g_value_set_int (value, video_crop->crop_left);
246       break;
247     case ARG_RIGHT:
248       g_value_set_int (value, video_crop->crop_right);
249       break;
250     case ARG_TOP:
251       g_value_set_int (value, video_crop->crop_top);
252       break;
253     case ARG_BOTTOM:
254       g_value_set_int (value, video_crop->crop_bottom);
255       break;
256     default:
257       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258       break;
259   }
260 }
261
262 static GstPadLinkReturn
263 gst_video_crop_sink_connect (GstPad *pad, GstCaps *caps)
264 {
265   GstVideoCrop *video_crop;
266
267   /* we are not going to act on variable caps */
268   if (!GST_CAPS_IS_FIXED (caps))
269     return GST_PAD_LINK_DELAYED;
270
271   video_crop = GST_VIDEO_CROP (gst_pad_get_parent (pad));
272
273   gst_caps_get_int (caps, "width",  &video_crop->width);
274   gst_caps_get_int (caps, "height", &video_crop->height);
275   gst_caps_get_float (caps, "framerate", &video_crop->fps);
276
277   return GST_PAD_LINK_OK;
278 }
279
280 #define GST_VIDEO_I420_SIZE(width,height) ((width)*(height) + ((width)/2)*((height)/2)*2)
281
282 #define GST_VIDEO_I420_Y_OFFSET(width,height) (0)
283 #define GST_VIDEO_I420_U_OFFSET(width,height) ((width)*(height))
284 #define GST_VIDEO_I420_V_OFFSET(width,height) ((width)*(height) + ((width/2)*(height/2)))
285
286 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (width)
287 #define GST_VIDEO_I420_U_ROWSTRIDE(width) ((width)/2)
288 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((width)/2)
289
290 static void
291 gst_video_crop_i420 (GstVideoCrop *video_crop, GstBuffer *src_buffer, GstBuffer *dest_buffer)
292 {
293   guint8 *src;
294   guint8 *dest;
295   guint8 *srcY, *srcU, *srcV;
296   guint8 *destY, *destU, *destV;
297   gint out_width = video_crop->width -
298         (video_crop->crop_left + video_crop->crop_right);
299   gint out_height = video_crop->height -
300         (video_crop->crop_top + video_crop->crop_bottom);
301   gint src_stride;
302   gint j;
303
304   src = GST_BUFFER_DATA (src_buffer);
305   dest = GST_BUFFER_DATA (dest_buffer);
306
307   g_return_if_fail(GST_BUFFER_SIZE (dest_buffer) == GST_VIDEO_I420_SIZE(out_width,out_height));
308
309   srcY = src + GST_VIDEO_I420_Y_OFFSET(video_crop->width, video_crop->height);
310   destY = dest + GST_VIDEO_I420_Y_OFFSET(out_width, out_height);
311
312   src_stride = GST_VIDEO_I420_Y_ROWSTRIDE(video_crop->width);
313
314   /* copy Y plane first */
315
316   srcY += src_stride * video_crop->crop_top + video_crop->crop_left;
317   for (j = 0; j < out_height; j++) {
318     memcpy (destY, srcY, out_width);
319     srcY += src_stride;
320     destY += out_width;
321   }
322
323   src_stride = GST_VIDEO_I420_U_ROWSTRIDE(video_crop->width);
324
325   destU = dest + GST_VIDEO_I420_U_OFFSET(out_width, out_height);
326   destV = dest + GST_VIDEO_I420_V_OFFSET(out_width, out_height);
327
328   srcU = src + GST_VIDEO_I420_U_OFFSET(video_crop->width, video_crop->height);
329   srcV = src + GST_VIDEO_I420_V_OFFSET(video_crop->width, video_crop->height);
330
331   srcU += src_stride * (video_crop->crop_top/2) + (video_crop->crop_left/2);
332   srcV += src_stride * (video_crop->crop_top/2) + (video_crop->crop_left/2);
333
334   for (j = 0; j < out_height/2; j++) {
335     /* copy U plane */
336     memcpy (destU, srcU, out_width/2);
337     srcU += src_stride;
338     destU += out_width/2;
339
340     /* copy V plane */
341     memcpy (destV, srcV, out_width/2);
342     srcV += src_stride;
343     destV += out_width/2;
344   }
345 }
346
347 static void
348 gst_video_crop_chain (GstPad *pad, GstBuffer *buffer)
349 {
350   GstVideoCrop *video_crop;
351   GstBuffer *outbuf;
352   gint new_width, new_height;
353
354   video_crop = GST_VIDEO_CROP (gst_pad_get_parent (pad));
355
356   if (GST_IS_EVENT (buffer)) {
357     GstEvent *event = GST_EVENT (buffer);
358
359     switch (GST_EVENT_TYPE (event)) {
360       default:
361         gst_pad_event_default (pad, event);
362         break;
363     }
364     return;
365   }
366
367   new_width = video_crop->width -
368         (video_crop->crop_left + video_crop->crop_right);
369   new_height = video_crop->height -
370         (video_crop->crop_top + video_crop->crop_bottom);
371
372   if (GST_PAD_CAPS (video_crop->srcpad) == NULL) {
373     if (gst_pad_try_set_caps (video_crop->srcpad,
374                                GST_CAPS_NEW (
375                                        "video_crop_caps",
376                                        "video/x-raw-yuv",
377                                         "format",   GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")),
378                                          "width",   GST_PROPS_INT (new_width),
379                                          "height",  GST_PROPS_INT (new_height),
380                                          "framerate", GST_PROPS_FLOAT (video_crop->fps)
381                                        )) <= 0)
382     {
383       gst_element_error (GST_ELEMENT (video_crop), "could not negotiate pads");
384       return;
385     }
386   }
387
388   outbuf = gst_buffer_new_and_alloc ((new_width * new_height * 3) / 2);
389   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
390
391   gst_video_crop_i420 (video_crop, buffer, outbuf);
392   gst_buffer_unref (buffer);
393
394   gst_pad_push (video_crop->srcpad, outbuf);
395 }
396
397 static GstElementStateReturn
398 gst_video_crop_change_state (GstElement *element)
399 {
400   GstVideoCrop *video_crop;
401
402   video_crop = GST_VIDEO_CROP (element);
403
404   switch (GST_STATE_TRANSITION (element)) {
405     case GST_STATE_NULL_TO_READY:
406       break;
407     case GST_STATE_READY_TO_PAUSED:
408       break;
409     case GST_STATE_PAUSED_TO_PLAYING:
410       break;
411     case GST_STATE_PLAYING_TO_PAUSED:
412       break;
413     case GST_STATE_PAUSED_TO_READY:
414       break;
415     case GST_STATE_READY_TO_NULL:
416       break;
417   }
418
419   parent_class->change_state (element);
420
421   return GST_STATE_SUCCESS;
422 }
423
424 static gboolean
425 plugin_init (GModule *module, GstPlugin *plugin)
426 {
427   GstElementFactory *factory;
428
429   /* create an elementfactory for the videocrop element */
430   factory = gst_element_factory_new ("videocrop", GST_TYPE_VIDEO_CROP, &gst_video_crop_details);
431   g_return_val_if_fail (factory != NULL, FALSE);
432
433   gst_element_factory_add_pad_template (factory, 
434                   GST_PAD_TEMPLATE_GET (video_crop_sink_template_factory));
435   gst_element_factory_add_pad_template (factory, 
436                   GST_PAD_TEMPLATE_GET (video_crop_src_template_factory));
437   gst_element_factory_set_rank (factory, GST_ELEMENT_RANK_PRIMARY);
438
439   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
440
441   return TRUE;
442 }
443
444 GstPluginDesc plugin_desc = {
445   GST_VERSION_MAJOR,
446   GST_VERSION_MINOR,
447   "videocrop",
448   plugin_init
449 };