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 = GST_ELEMENT_DETAILS (
62 "Filter/Effect/Video",
63 "Crops video into a user defined region",
64 "Wim Taymans <wim.taymans@chello.be>"
68 /* VideoCrop signals and args */
83 GST_PAD_TEMPLATE_FACTORY (video_crop_src_template_factory,
90 GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
91 GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
95 GST_PAD_TEMPLATE_FACTORY (video_crop_sink_template_factory,
102 GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
103 GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
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);
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);
117 static GstPadLinkReturn
118 gst_video_crop_sink_connect (GstPad *pad, GstCaps *caps);
119 static void gst_video_crop_chain (GstPad *pad, GstData *_data);
121 static GstElementStateReturn
122 gst_video_crop_change_state (GstElement *element);
125 static GstElementClass *parent_class = NULL;
126 /* static guint gst_video_crop_signals[LAST_SIGNAL] = { 0 }; */
129 gst_video_crop_get_type (void)
131 static GType video_crop_type = 0;
133 if (!video_crop_type) {
134 static const GTypeInfo video_crop_info = {
135 sizeof(GstVideoCropClass),
136 gst_video_crop_base_init,
138 (GClassInitFunc)gst_video_crop_class_init,
141 sizeof(GstVideoCrop),
143 (GInstanceInitFunc)gst_video_crop_init,
145 video_crop_type = g_type_register_static(GST_TYPE_ELEMENT, "GstVideoCrop", &video_crop_info, 0);
147 return video_crop_type;
151 gst_video_crop_base_init (gpointer g_class)
153 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
155 gst_element_class_set_details (element_class, &gst_video_crop_details);
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));
163 gst_video_crop_class_init (GstVideoCropClass *klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
168 gobject_class = (GObjectClass*) klass;
169 gstelement_class = (GstElementClass*) klass;
171 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
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));
186 gobject_class->set_property = gst_video_crop_set_property;
187 gobject_class->get_property = gst_video_crop_get_property;
189 gstelement_class->change_state = gst_video_crop_change_state;
193 gst_video_crop_init (GstVideoCrop *video_crop)
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));
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);
206 video_crop->crop_right = 0;
207 video_crop->crop_left = 0;
208 video_crop->crop_top = 0;
209 video_crop->crop_bottom = 0;
211 GST_FLAG_SET (video_crop, GST_ELEMENT_EVENT_AWARE);
214 /* do we need this function? */
216 gst_video_crop_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
218 GstVideoCrop *video_crop;
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));
223 video_crop = GST_VIDEO_CROP (object);
227 video_crop->crop_left = g_value_get_int (value);
230 video_crop->crop_right = g_value_get_int (value);
233 video_crop->crop_top = g_value_get_int (value);
236 video_crop->crop_bottom = g_value_get_int (value);
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244 gst_video_crop_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
246 GstVideoCrop *video_crop;
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));
251 video_crop = GST_VIDEO_CROP (object);
255 g_value_set_int (value, video_crop->crop_left);
258 g_value_set_int (value, video_crop->crop_right);
261 g_value_set_int (value, video_crop->crop_top);
264 g_value_set_int (value, video_crop->crop_bottom);
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 static GstPadLinkReturn
273 gst_video_crop_sink_connect (GstPad *pad, GstCaps *caps)
275 GstVideoCrop *video_crop;
277 /* we are not going to act on variable caps */
278 if (!GST_CAPS_IS_FIXED (caps))
279 return GST_PAD_LINK_DELAYED;
281 video_crop = GST_VIDEO_CROP (gst_pad_get_parent (pad));
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);
287 return GST_PAD_LINK_OK;
290 #define GST_VIDEO_I420_SIZE(width,height) ((width)*(height) + ((width)/2)*((height)/2)*2)
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)))
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)
301 gst_video_crop_i420 (GstVideoCrop *video_crop, GstBuffer *src_buffer, GstBuffer *dest_buffer)
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);
314 src = GST_BUFFER_DATA (src_buffer);
315 dest = GST_BUFFER_DATA (dest_buffer);
317 g_return_if_fail(GST_BUFFER_SIZE (dest_buffer) == GST_VIDEO_I420_SIZE(out_width,out_height));
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);
322 src_stride = GST_VIDEO_I420_Y_ROWSTRIDE(video_crop->width);
324 /* copy Y plane first */
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);
333 src_stride = GST_VIDEO_I420_U_ROWSTRIDE(video_crop->width);
335 destU = dest + GST_VIDEO_I420_U_OFFSET(out_width, out_height);
336 destV = dest + GST_VIDEO_I420_V_OFFSET(out_width, out_height);
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);
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);
344 for (j = 0; j < out_height/2; j++) {
346 memcpy (destU, srcU, out_width/2);
348 destU += out_width/2;
351 memcpy (destV, srcV, out_width/2);
353 destV += out_width/2;
358 gst_video_crop_chain (GstPad *pad, GstData *_data)
360 GstBuffer *buffer = GST_BUFFER (_data);
361 GstVideoCrop *video_crop;
363 gint new_width, new_height;
365 video_crop = GST_VIDEO_CROP (gst_pad_get_parent (pad));
367 if (GST_IS_EVENT (buffer)) {
368 GstEvent *event = GST_EVENT (buffer);
370 switch (GST_EVENT_TYPE (event)) {
372 gst_pad_event_default (pad, event);
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);
383 if (GST_PAD_CAPS (video_crop->srcpad) == NULL) {
384 if (gst_pad_try_set_caps (video_crop->srcpad,
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)
394 gst_element_error (GST_ELEMENT (video_crop), "could not negotiate pads");
399 outbuf = gst_buffer_new_and_alloc ((new_width * new_height * 3) / 2);
400 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
402 gst_video_crop_i420 (video_crop, buffer, outbuf);
403 gst_buffer_unref (buffer);
405 gst_pad_push (video_crop->srcpad, GST_DATA (outbuf));
408 static GstElementStateReturn
409 gst_video_crop_change_state (GstElement *element)
411 GstVideoCrop *video_crop;
413 video_crop = GST_VIDEO_CROP (element);
415 switch (GST_STATE_TRANSITION (element)) {
416 case GST_STATE_NULL_TO_READY:
418 case GST_STATE_READY_TO_PAUSED:
420 case GST_STATE_PAUSED_TO_PLAYING:
422 case GST_STATE_PLAYING_TO_PAUSED:
424 case GST_STATE_PAUSED_TO_READY:
426 case GST_STATE_READY_TO_NULL:
430 parent_class->change_state (element);
432 return GST_STATE_SUCCESS;
436 plugin_init (GstPlugin *plugin)
438 return gst_element_register (plugin, "videocrop", GST_RANK_PRIMARY, GST_TYPE_VIDEO_CROP);
445 "Crops video into a user defined region",