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