2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
24 #include <gst/video/video.h>
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))
39 typedef struct _GstVideoCrop GstVideoCrop;
40 typedef struct _GstVideoCropClass GstVideoCropClass;
42 struct _GstVideoCrop {
52 gint crop_left, crop_right, crop_top, crop_bottom;
55 struct _GstVideoCropClass {
56 GstElementClass parent_class;
59 /* elementfactory information */
60 static GstElementDetails gst_video_crop_details = {
64 "Crops video into a user defined region",
66 "Wim Taymans <wim.taymans@chello.be>",
71 /* VideoCrop signals and args */
86 GST_PAD_TEMPLATE_FACTORY (video_crop_src_template_factory,
93 GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
94 GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
98 GST_PAD_TEMPLATE_FACTORY (video_crop_sink_template_factory,
105 GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
106 GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
111 static void gst_video_crop_class_init (GstVideoCropClass *klass);
112 static void gst_video_crop_init (GstVideoCrop *video_crop);
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);
119 static GstPadLinkReturn
120 gst_video_crop_sink_connect (GstPad *pad, GstCaps *caps);
121 static void gst_video_crop_chain (GstPad *pad, GstBuffer *buffer);
123 static GstElementStateReturn
124 gst_video_crop_change_state (GstElement *element);
127 static GstElementClass *parent_class = NULL;
128 /* static guint gst_video_crop_signals[LAST_SIGNAL] = { 0 }; */
131 gst_video_crop_get_type (void)
133 static GType video_crop_type = 0;
135 if (!video_crop_type) {
136 static const GTypeInfo video_crop_info = {
137 sizeof(GstVideoCropClass),
140 (GClassInitFunc)gst_video_crop_class_init,
143 sizeof(GstVideoCrop),
145 (GInstanceInitFunc)gst_video_crop_init,
147 video_crop_type = g_type_register_static(GST_TYPE_ELEMENT, "GstVideoCrop", &video_crop_info, 0);
149 return video_crop_type;
153 gst_video_crop_class_init (GstVideoCropClass *klass)
155 GObjectClass *gobject_class;
156 GstElementClass *gstelement_class;
158 gobject_class = (GObjectClass*) klass;
159 gstelement_class = (GstElementClass*) klass;
161 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
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));
176 gobject_class->set_property = gst_video_crop_set_property;
177 gobject_class->get_property = gst_video_crop_get_property;
179 gstelement_class->change_state = gst_video_crop_change_state;
183 gst_video_crop_init (GstVideoCrop *video_crop)
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));
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);
196 video_crop->crop_right = 0;
197 video_crop->crop_left = 0;
198 video_crop->crop_top = 0;
199 video_crop->crop_bottom = 0;
201 GST_FLAG_SET (video_crop, GST_ELEMENT_EVENT_AWARE);
204 /* do we need this function? */
206 gst_video_crop_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
208 GstVideoCrop *video_crop;
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));
213 video_crop = GST_VIDEO_CROP (object);
217 video_crop->crop_left = g_value_get_int (value);
220 video_crop->crop_right = g_value_get_int (value);
223 video_crop->crop_top = g_value_get_int (value);
226 video_crop->crop_bottom = g_value_get_int (value);
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 gst_video_crop_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
236 GstVideoCrop *video_crop;
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));
241 video_crop = GST_VIDEO_CROP (object);
245 g_value_set_int (value, video_crop->crop_left);
248 g_value_set_int (value, video_crop->crop_right);
251 g_value_set_int (value, video_crop->crop_top);
254 g_value_set_int (value, video_crop->crop_bottom);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 static GstPadLinkReturn
263 gst_video_crop_sink_connect (GstPad *pad, GstCaps *caps)
265 GstVideoCrop *video_crop;
267 /* we are not going to act on variable caps */
268 if (!GST_CAPS_IS_FIXED (caps))
269 return GST_PAD_LINK_DELAYED;
271 video_crop = GST_VIDEO_CROP (gst_pad_get_parent (pad));
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);
277 return GST_PAD_LINK_OK;
280 #define GST_VIDEO_I420_SIZE(width,height) ((width)*(height) + ((width)/2)*((height)/2)*2)
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)))
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)
291 gst_video_crop_i420 (GstVideoCrop *video_crop, GstBuffer *src_buffer, GstBuffer *dest_buffer)
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);
304 src = GST_BUFFER_DATA (src_buffer);
305 dest = GST_BUFFER_DATA (dest_buffer);
307 g_return_if_fail(GST_BUFFER_SIZE (dest_buffer) == GST_VIDEO_I420_SIZE(out_width,out_height));
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);
312 src_stride = GST_VIDEO_I420_Y_ROWSTRIDE(video_crop->width);
314 /* copy Y plane first */
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);
323 src_stride = GST_VIDEO_I420_U_ROWSTRIDE(video_crop->width);
325 destU = dest + GST_VIDEO_I420_U_OFFSET(out_width, out_height);
326 destV = dest + GST_VIDEO_I420_V_OFFSET(out_width, out_height);
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);
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);
334 for (j = 0; j < out_height/2; j++) {
336 memcpy (destU, srcU, out_width/2);
338 destU += out_width/2;
341 memcpy (destV, srcV, out_width/2);
343 destV += out_width/2;
348 gst_video_crop_chain (GstPad *pad, GstBuffer *buffer)
350 GstVideoCrop *video_crop;
352 gint new_width, new_height;
354 video_crop = GST_VIDEO_CROP (gst_pad_get_parent (pad));
356 if (GST_IS_EVENT (buffer)) {
357 GstEvent *event = GST_EVENT (buffer);
359 switch (GST_EVENT_TYPE (event)) {
361 gst_pad_event_default (pad, event);
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);
372 if (GST_PAD_CAPS (video_crop->srcpad) == NULL) {
373 if (gst_pad_try_set_caps (video_crop->srcpad,
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)
383 gst_element_error (GST_ELEMENT (video_crop), "could not negotiate pads");
388 outbuf = gst_buffer_new_and_alloc ((new_width * new_height * 3) / 2);
389 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
391 gst_video_crop_i420 (video_crop, buffer, outbuf);
392 gst_buffer_unref (buffer);
394 gst_pad_push (video_crop->srcpad, outbuf);
397 static GstElementStateReturn
398 gst_video_crop_change_state (GstElement *element)
400 GstVideoCrop *video_crop;
402 video_crop = GST_VIDEO_CROP (element);
404 switch (GST_STATE_TRANSITION (element)) {
405 case GST_STATE_NULL_TO_READY:
407 case GST_STATE_READY_TO_PAUSED:
409 case GST_STATE_PAUSED_TO_PLAYING:
411 case GST_STATE_PLAYING_TO_PAUSED:
413 case GST_STATE_PAUSED_TO_READY:
415 case GST_STATE_READY_TO_NULL:
419 parent_class->change_state (element);
421 return GST_STATE_SUCCESS;
425 plugin_init (GModule *module, GstPlugin *plugin)
427 GstElementFactory *factory;
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);
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);
439 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
444 GstPluginDesc plugin_desc = {