Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / videocrop / gstvideocrop.c
1 /* GStreamer video frame cropping
2  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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 /**
21  * SECTION:element-videocrop
22  * @see_also: #GstVideoBox
23  *
24  * This element crops video frames, meaning it can remove parts of the
25  * picture on the left, right, top or bottom of the picture and output
26  * a smaller picture than the input picture, with the unwanted parts at the
27  * border removed.
28  *
29  * The videocrop element is similar to the videobox element, but its main
30  * goal is to support a multitude of formats as efficiently as possible.
31  * Unlike videbox, it cannot add borders to the picture and unlike videbox
32  * it will always output images in exactly the same format as the input image.
33  *
34  * If there is nothing to crop, the element will operate in pass-through mode.
35  *
36  * Note that no special efforts are made to handle chroma-subsampled formats
37  * in the case of odd-valued cropping and compensate for sub-unit chroma plane
38  * shifts for such formats in the case where the #GstVideoCrop:left or
39  * #GstVideoCrop:top property is set to an odd number. This doesn't matter for 
40  * most use cases, but it might matter for yours.
41  *
42  * <refsect2>
43  * <title>Example launch line</title>
44  * |[
45  * gst-launch -v videotestsrc ! videocrop top=42 left=1 right=4 bottom=0 ! ximagesink
46  * ]|
47  * </refsect2>
48  */
49
50 /* TODO:
51  *  - for packed formats, we could avoid memcpy() in case crop_left
52  *    and crop_right are 0 and just create a sub-buffer of the input
53  *    buffer
54  */
55
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59
60 #include <gst/gst.h>
61 #include <gst/video/video.h>
62
63 #include "gstvideocrop.h"
64 #include "gstaspectratiocrop.h"
65
66 #include <string.h>
67
68 GST_DEBUG_CATEGORY_STATIC (videocrop_debug);
69 #define GST_CAT_DEFAULT videocrop_debug
70
71 enum
72 {
73   ARG_0,
74   ARG_LEFT,
75   ARG_RIGHT,
76   ARG_TOP,
77   ARG_BOTTOM
78 };
79
80 #define VIDEO_CROP_CAPS                                \
81   GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, "    \
82       "RGBA, ARGB, BGRA, ABGR, RGB, BGR, AYUV, YUY2, " \
83       "YVYU, UYVY, Y800, I420, RGB16, RGB15, GRAY8 }")
84
85 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
86     GST_PAD_SRC,
87     GST_PAD_ALWAYS,
88     GST_STATIC_CAPS (VIDEO_CROP_CAPS)
89     );
90
91 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
92     GST_PAD_SINK,
93     GST_PAD_ALWAYS,
94     GST_STATIC_CAPS (VIDEO_CROP_CAPS)
95     );
96
97 #define gst_video_crop_parent_class parent_class
98 G_DEFINE_TYPE (GstVideoCrop, gst_video_crop, GST_TYPE_BASE_TRANSFORM);
99
100 static void gst_video_crop_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * pspec);
102 static void gst_video_crop_get_property (GObject * object, guint prop_id,
103     GValue * value, GParamSpec * pspec);
104
105 static GstCaps *gst_video_crop_transform_caps (GstBaseTransform * trans,
106     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps);
107 static GstFlowReturn gst_video_crop_transform (GstBaseTransform * trans,
108     GstBuffer * inbuf, GstBuffer * outbuf);
109 static gboolean gst_video_crop_get_unit_size (GstBaseTransform * trans,
110     GstCaps * caps, gsize * size);
111 static gboolean gst_video_crop_set_caps (GstBaseTransform * trans,
112     GstCaps * in_caps, GstCaps * outcaps);
113 static gboolean gst_video_crop_src_event (GstBaseTransform * trans,
114     GstEvent * event);
115
116 static gboolean
117 gst_video_crop_src_event (GstBaseTransform * trans, GstEvent * event)
118 {
119   GstEvent *new_event;
120   GstStructure *new_structure;
121   const GstStructure *structure;
122   const gchar *event_name;
123   double pointer_x;
124   double pointer_y;
125
126   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
127   new_event = NULL;
128
129   GST_OBJECT_LOCK (vcrop);
130   if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION &&
131       (vcrop->crop_left != 0 || vcrop->crop_top != 0)) {
132     structure = gst_event_get_structure (event);
133     event_name = gst_structure_get_string (structure, "event");
134
135     if (event_name &&
136         (strcmp (event_name, "mouse-move") == 0 ||
137             strcmp (event_name, "mouse-button-press") == 0 ||
138             strcmp (event_name, "mouse-button-release") == 0)) {
139
140       if (gst_structure_get_double (structure, "pointer_x", &pointer_x) &&
141           gst_structure_get_double (structure, "pointer_y", &pointer_y)) {
142
143         new_structure = gst_structure_copy (structure);
144         gst_structure_set (new_structure,
145             "pointer_x", G_TYPE_DOUBLE, (double) (pointer_x + vcrop->crop_left),
146             "pointer_y", G_TYPE_DOUBLE, (double) (pointer_y + vcrop->crop_top),
147             NULL);
148
149         new_event = gst_event_new_navigation (new_structure);
150         gst_event_unref (event);
151       } else {
152         GST_WARNING_OBJECT (vcrop, "Failed to read navigation event");
153       }
154     }
155   }
156
157   GST_OBJECT_UNLOCK (vcrop);
158   return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans,
159       (new_event ? new_event : event));
160 }
161
162 static void
163 gst_video_crop_class_init (GstVideoCropClass * klass)
164 {
165   GObjectClass *gobject_class;
166   GstElementClass *element_class;
167   GstBaseTransformClass *basetransform_class;
168
169   gobject_class = (GObjectClass *) klass;
170   element_class = (GstElementClass *) klass;
171   basetransform_class = (GstBaseTransformClass *) klass;
172
173   gst_element_class_set_details_simple (element_class, "Crop",
174       "Filter/Effect/Video",
175       "Crops video into a user-defined region",
176       "Tim-Philipp Müller <tim centricular net>");
177
178   gst_element_class_add_pad_template (element_class,
179       gst_static_pad_template_get (&sink_template));
180   gst_element_class_add_pad_template (element_class,
181       gst_static_pad_template_get (&src_template));
182   gst_element_class_set_details_simple (element_class, "Crop",
183       "Filter/Effect/Video",
184       "Crops video into a user-defined region",
185       "Tim-Philipp Müller <tim centricular net>");
186
187   gst_element_class_add_pad_template (element_class,
188       gst_static_pad_template_get (&sink_template));
189   gst_element_class_add_pad_template (element_class,
190       gst_static_pad_template_get (&src_template));
191
192   gobject_class->set_property = gst_video_crop_set_property;
193   gobject_class->get_property = gst_video_crop_get_property;
194
195   g_object_class_install_property (gobject_class, ARG_LEFT,
196       g_param_spec_int ("left", "Left", "Pixels to crop at left",
197           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198   g_object_class_install_property (gobject_class, ARG_RIGHT,
199       g_param_spec_int ("right", "Right", "Pixels to crop at right",
200           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201   g_object_class_install_property (gobject_class, ARG_TOP,
202       g_param_spec_int ("top", "Top", "Pixels to crop at top",
203           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204   g_object_class_install_property (gobject_class, ARG_BOTTOM,
205       g_param_spec_int ("bottom", "Bottom", "Pixels to crop at bottom",
206           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207
208   basetransform_class->transform = GST_DEBUG_FUNCPTR (gst_video_crop_transform);
209   basetransform_class->transform_caps =
210       GST_DEBUG_FUNCPTR (gst_video_crop_transform_caps);
211   basetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_crop_set_caps);
212   basetransform_class->get_unit_size =
213       GST_DEBUG_FUNCPTR (gst_video_crop_get_unit_size);
214
215   basetransform_class->passthrough_on_same_caps = FALSE;
216   basetransform_class->src_event = GST_DEBUG_FUNCPTR (gst_video_crop_src_event);
217 }
218
219 static void
220 gst_video_crop_init (GstVideoCrop * vcrop)
221 {
222   vcrop->crop_right = 0;
223   vcrop->crop_left = 0;
224   vcrop->crop_top = 0;
225   vcrop->crop_bottom = 0;
226 }
227
228 static gboolean
229 gst_video_crop_get_image_details_from_caps (GstVideoCrop * vcrop,
230     GstVideoCropImageDetails * details, GstCaps * caps)
231 {
232   gst_video_info_init (&details->info);
233   if (!gst_video_info_from_caps (&details->info, caps)) {
234     goto incomplete_format;
235   }
236
237   if (details->info.width == 0 && details->info.height == 0) {
238     goto incomplete_format;
239   }
240
241   if (GST_VIDEO_INFO_IS_RGB (&details->info)
242       || GST_VIDEO_INFO_IS_GRAY (&details->info)) {
243     details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
244   } else {
245     switch (GST_VIDEO_INFO_FORMAT (&details->info)) {
246       case GST_VIDEO_FORMAT_AYUV:
247         details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
248         break;
249       case GST_VIDEO_FORMAT_YVYU:
250       case GST_VIDEO_FORMAT_YUY2:
251       case GST_VIDEO_FORMAT_UYVY:
252         details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX;
253         if (GST_VIDEO_INFO_FORMAT (&details->info) == GST_VIDEO_FORMAT_UYVY) {
254           /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5] */
255           details->macro_y_off = 1;
256         } else {
257           /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
258           details->macro_y_off = 0;
259         }
260         break;
261       case GST_VIDEO_FORMAT_Y800:
262         details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
263         break;
264       case GST_VIDEO_FORMAT_I420:
265       case GST_VIDEO_FORMAT_YV12:
266         details->packing = VIDEO_CROP_PIXEL_FORMAT_PLANAR;
267         break;
268       default:
269         goto unknown_format;
270     }
271   }
272
273   return TRUE;
274
275   /* ERRORS */
276 unknown_format:
277   {
278     GST_ELEMENT_ERROR (vcrop, STREAM, NOT_IMPLEMENTED, (NULL),
279         ("Unsupported format"));
280     return FALSE;
281   }
282
283 incomplete_format:
284   {
285     GST_ELEMENT_ERROR (vcrop, CORE, NEGOTIATION, (NULL),
286         ("Incomplete caps, some required field is missing"));
287     return FALSE;
288   }
289 }
290
291 static gboolean
292 gst_video_crop_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
293     gsize * size)
294 {
295   GstVideoCropImageDetails img_details = { 0, };
296   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
297
298   if (!gst_video_crop_get_image_details_from_caps (vcrop, &img_details, caps))
299     return FALSE;
300
301   *size = GST_VIDEO_INFO_SIZE (&img_details.info);
302   return TRUE;
303 }
304
305 #define ROUND_DOWN_2(n)  ((n)&(~1))
306
307 static void
308 gst_video_crop_transform_packed_complex (GstVideoCrop * vcrop,
309     GstBuffer * inbuf, GstBuffer * outbuf)
310 {
311   guint8 *in_data, *out_data;
312   gpointer in_bufdata, out_bufdata;
313   gsize insize, outsize;
314   guint i, dx;
315   gint in_stride;
316   gint out_stride;
317
318   in_bufdata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
319   out_bufdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
320
321   in_data = in_bufdata;
322   out_data = out_bufdata;
323
324   in_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
325   out_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);
326
327   in_data += vcrop->crop_top * in_stride;
328
329   /* rounding down here so we end up at the start of a macro-pixel and not
330    * in the middle of one */
331   in_data +=
332       ROUND_DOWN_2 (vcrop->crop_left) *
333       GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->in.info, 0);
334
335   dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) *
336       GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->out.info, 0);
337
338   /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5]
339    * YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
340   if ((vcrop->crop_left % 2) != 0) {
341     for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
342       gint j;
343
344       memcpy (out_data, in_data, dx);
345
346       /* move just the Y samples one pixel to the left, don't worry about
347        * chroma shift */
348       for (j = vcrop->in.macro_y_off; j < out_stride - 2; j += 2)
349         out_data[j] = in_data[j + 2];
350
351       in_data += in_stride;
352       out_data += out_stride;
353     }
354   } else {
355     for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
356       memcpy (out_data, in_data, dx);
357       in_data += in_stride;
358       out_data += out_stride;
359     }
360   }
361   gst_buffer_unmap (inbuf, in_bufdata, insize);
362   gst_buffer_unmap (outbuf, out_bufdata, outsize);
363 }
364
365 static void
366 gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
367     GstBuffer * inbuf, GstBuffer * outbuf)
368 {
369   guint8 *in_data, *out_data;
370   gpointer in_bufdata, out_bufdata;
371   gsize insize, outsize;
372   guint i, dx;
373   gint in_stride, out_stride;
374
375   in_bufdata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
376   out_bufdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
377
378   in_data = in_bufdata;
379   out_data = out_bufdata;
380
381   in_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
382   out_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);
383
384   in_data += vcrop->crop_top * in_stride;
385   in_data +=
386       vcrop->crop_left * GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->in.info, 0);
387
388   dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) *
389       GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->out.info, 0);
390
391   for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
392     memcpy (out_data, in_data, dx);
393     in_data += in_stride;
394     out_data += out_stride;
395   }
396   gst_buffer_unmap (inbuf, in_bufdata, insize);
397   gst_buffer_unmap (outbuf, out_bufdata, outsize);
398 }
399
400 static void
401 gst_video_crop_transform_planar (GstVideoCrop * vcrop, GstBuffer * inbuf,
402     GstBuffer * outbuf)
403 {
404   guint8 *y_out, *u_out, *v_out;
405   guint8 *y_in, *u_in, *v_in;
406   guint i, dx;
407   gpointer in_bufdata, out_bufdata;
408   gsize insize, outsize;
409
410   in_bufdata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
411   out_bufdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
412
413   /* Y plane */
414   y_in = in_bufdata;
415   y_out = out_bufdata;
416
417   y_in +=
418       (vcrop->crop_top * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info,
419           0)) + vcrop->crop_left;
420   dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) * 1;
421
422   for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
423     memcpy (y_out, y_in, dx);
424     y_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
425     y_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);
426   }
427
428   /* U + V planes */
429   u_in =
430       (guint8 *) in_bufdata + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->in.info, 1);
431   u_out =
432       (guint8 *) out_bufdata + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->out.info,
433       1);
434
435   u_in +=
436       (vcrop->crop_top / 2) * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 1);
437   u_in += vcrop->crop_left / 2;
438
439   v_in =
440       (guint8 *) in_bufdata + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->in.info, 2);
441   v_out =
442       (guint8 *) out_bufdata + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->out.info,
443       2);
444
445   v_in +=
446       (vcrop->crop_top / 2) * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 2);
447   v_in += vcrop->crop_left / 2;
448
449   dx = GST_ROUND_UP_2 (GST_VIDEO_INFO_WIDTH (&vcrop->out.info)) / 2;
450
451   for (i = 0; i < GST_ROUND_UP_2 (GST_VIDEO_INFO_HEIGHT (&vcrop->out.info)) / 2;
452       ++i) {
453     memcpy (u_out, u_in, dx);
454     memcpy (v_out, v_in, dx);
455     u_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 1);
456     u_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 1);
457     v_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 2);
458     v_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 2);
459   }
460
461   gst_buffer_unmap (inbuf, in_bufdata, insize);
462   gst_buffer_unmap (outbuf, out_bufdata, outsize);
463 }
464
465 static GstFlowReturn
466 gst_video_crop_transform (GstBaseTransform * trans, GstBuffer * inbuf,
467     GstBuffer * outbuf)
468 {
469   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
470
471   switch (vcrop->in.packing) {
472     case VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE:
473       gst_video_crop_transform_packed_simple (vcrop, inbuf, outbuf);
474       break;
475     case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
476       gst_video_crop_transform_packed_complex (vcrop, inbuf, outbuf);
477       break;
478     case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
479       gst_video_crop_transform_planar (vcrop, inbuf, outbuf);
480       break;
481     default:
482       g_assert_not_reached ();
483   }
484
485   return GST_FLOW_OK;
486 }
487
488 static gint
489 gst_video_crop_transform_dimension (gint val, gint delta)
490 {
491   gint64 new_val = (gint64) val + (gint64) delta;
492
493   new_val = CLAMP (new_val, 1, G_MAXINT);
494
495   return (gint) new_val;
496 }
497
498 static gboolean
499 gst_video_crop_transform_dimension_value (const GValue * src_val,
500     gint delta, GValue * dest_val)
501 {
502   gboolean ret = TRUE;
503
504   g_value_init (dest_val, G_VALUE_TYPE (src_val));
505
506   if (G_VALUE_HOLDS_INT (src_val)) {
507     gint ival = g_value_get_int (src_val);
508
509     ival = gst_video_crop_transform_dimension (ival, delta);
510     g_value_set_int (dest_val, ival);
511   } else if (GST_VALUE_HOLDS_INT_RANGE (src_val)) {
512     gint min = gst_value_get_int_range_min (src_val);
513     gint max = gst_value_get_int_range_max (src_val);
514
515     min = gst_video_crop_transform_dimension (min, delta);
516     max = gst_video_crop_transform_dimension (max, delta);
517     gst_value_set_int_range (dest_val, min, max);
518   } else if (GST_VALUE_HOLDS_LIST (src_val)) {
519     gint i;
520
521     for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
522       const GValue *list_val;
523       GValue newval = { 0, };
524
525       list_val = gst_value_list_get_value (src_val, i);
526       if (gst_video_crop_transform_dimension_value (list_val, delta, &newval))
527         gst_value_list_append_value (dest_val, &newval);
528       g_value_unset (&newval);
529     }
530
531     if (gst_value_list_get_size (dest_val) == 0) {
532       g_value_unset (dest_val);
533       ret = FALSE;
534     }
535   } else {
536     g_value_unset (dest_val);
537     ret = FALSE;
538   }
539
540   return ret;
541 }
542
543 /* TODO use filter_caps */
544 static GstCaps *
545 gst_video_crop_transform_caps (GstBaseTransform * trans,
546     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
547 {
548   GstVideoCrop *vcrop;
549   GstCaps *other_caps;
550   gint dy, dx, i;
551
552   vcrop = GST_VIDEO_CROP (trans);
553
554   GST_OBJECT_LOCK (vcrop);
555
556   GST_LOG_OBJECT (vcrop, "l=%d,r=%d,b=%d,t=%d",
557       vcrop->crop_left, vcrop->crop_right, vcrop->crop_bottom, vcrop->crop_top);
558
559   if (direction == GST_PAD_SRC) {
560     dx = vcrop->crop_left + vcrop->crop_right;
561     dy = vcrop->crop_top + vcrop->crop_bottom;
562   } else {
563     dx = 0 - (vcrop->crop_left + vcrop->crop_right);
564     dy = 0 - (vcrop->crop_top + vcrop->crop_bottom);
565   }
566   GST_OBJECT_UNLOCK (vcrop);
567
568   GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
569
570   other_caps = gst_caps_new_empty ();
571
572   for (i = 0; i < gst_caps_get_size (caps); ++i) {
573     const GValue *v;
574     GstStructure *structure, *new_structure;
575     GValue w_val = { 0, }, h_val = {
576     0,};
577
578     structure = gst_caps_get_structure (caps, i);
579
580     v = gst_structure_get_value (structure, "width");
581     if (!gst_video_crop_transform_dimension_value (v, dx, &w_val)) {
582       GST_WARNING_OBJECT (vcrop, "could not tranform width value with dx=%d"
583           ", caps structure=%" GST_PTR_FORMAT, dx, structure);
584       continue;
585     }
586
587     v = gst_structure_get_value (structure, "height");
588     if (!gst_video_crop_transform_dimension_value (v, dy, &h_val)) {
589       g_value_unset (&w_val);
590       GST_WARNING_OBJECT (vcrop, "could not tranform height value with dy=%d"
591           ", caps structure=%" GST_PTR_FORMAT, dy, structure);
592       continue;
593     }
594
595     new_structure = gst_structure_copy (structure);
596     gst_structure_set_value (new_structure, "width", &w_val);
597     gst_structure_set_value (new_structure, "height", &h_val);
598     g_value_unset (&w_val);
599     g_value_unset (&h_val);
600     GST_LOG_OBJECT (vcrop, "transformed structure %2d: %" GST_PTR_FORMAT
601         " => %" GST_PTR_FORMAT, i, structure, new_structure);
602     gst_caps_append_structure (other_caps, new_structure);
603   }
604
605   if (gst_caps_is_empty (other_caps)) {
606     gst_caps_unref (other_caps);
607     other_caps = NULL;
608   }
609
610   if (other_caps && filter_caps) {
611     GstCaps *tmp = gst_caps_intersect_full (filter_caps, other_caps,
612         GST_CAPS_INTERSECT_FIRST);
613     gst_caps_replace (&other_caps, tmp);
614     gst_caps_unref (tmp);
615   }
616
617   return other_caps;
618 }
619
620 static gboolean
621 gst_video_crop_set_caps (GstBaseTransform * trans, GstCaps * incaps,
622     GstCaps * outcaps)
623 {
624   GstVideoCrop *crop = GST_VIDEO_CROP (trans);
625
626   if (!gst_video_crop_get_image_details_from_caps (crop, &crop->in, incaps))
627     goto wrong_input;
628
629   if (!gst_video_crop_get_image_details_from_caps (crop, &crop->out, outcaps))
630     goto wrong_output;
631
632   if (G_UNLIKELY ((crop->crop_left + crop->crop_right) >=
633           GST_VIDEO_INFO_WIDTH (&crop->in.info)
634           || (crop->crop_top + crop->crop_bottom) >=
635           GST_VIDEO_INFO_HEIGHT (&crop->in.info)))
636     goto cropping_too_much;
637
638   GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
639       GST_PTR_FORMAT, incaps, outcaps);
640
641   if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
642           crop_bottom) == 0) {
643     GST_LOG_OBJECT (crop, "we are using passthrough");
644     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
645   } else {
646     GST_LOG_OBJECT (crop, "we are not using passthrough");
647     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
648   }
649
650   return TRUE;
651
652   /* ERROR */
653 wrong_input:
654   {
655     GST_DEBUG_OBJECT (crop, "failed to parse input caps %" GST_PTR_FORMAT,
656         incaps);
657     return FALSE;
658   }
659 wrong_output:
660   {
661     GST_DEBUG_OBJECT (crop, "failed to parse output caps %" GST_PTR_FORMAT,
662         outcaps);
663     return FALSE;
664   }
665 cropping_too_much:
666   {
667     GST_DEBUG_OBJECT (crop, "we are cropping too much");
668     return FALSE;
669   }
670 }
671
672 static void
673 gst_video_crop_set_property (GObject * object, guint prop_id,
674     const GValue * value, GParamSpec * pspec)
675 {
676   GstVideoCrop *video_crop;
677
678   video_crop = GST_VIDEO_CROP (object);
679
680   /* don't modify while we are transforming */
681   GST_BASE_TRANSFORM_LOCK (GST_BASE_TRANSFORM_CAST (video_crop));
682
683   /* protect with the object lock so that we can read them */
684   GST_OBJECT_LOCK (video_crop);
685   switch (prop_id) {
686     case ARG_LEFT:
687       video_crop->crop_left = g_value_get_int (value);
688       break;
689     case ARG_RIGHT:
690       video_crop->crop_right = g_value_get_int (value);
691       break;
692     case ARG_TOP:
693       video_crop->crop_top = g_value_get_int (value);
694       break;
695     case ARG_BOTTOM:
696       video_crop->crop_bottom = g_value_get_int (value);
697       break;
698     default:
699       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
700       break;
701   }
702   GST_OBJECT_UNLOCK (video_crop);
703
704   GST_LOG_OBJECT (video_crop, "l=%d,r=%d,b=%d,t=%d",
705       video_crop->crop_left, video_crop->crop_right, video_crop->crop_bottom,
706       video_crop->crop_top);
707
708   gst_base_transform_reconfigure (GST_BASE_TRANSFORM (video_crop));
709   GST_BASE_TRANSFORM_UNLOCK (GST_BASE_TRANSFORM_CAST (video_crop));
710 }
711
712 static void
713 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
714     GParamSpec * pspec)
715 {
716   GstVideoCrop *video_crop;
717
718   video_crop = GST_VIDEO_CROP (object);
719
720   GST_OBJECT_LOCK (video_crop);
721   switch (prop_id) {
722     case ARG_LEFT:
723       g_value_set_int (value, video_crop->crop_left);
724       break;
725     case ARG_RIGHT:
726       g_value_set_int (value, video_crop->crop_right);
727       break;
728     case ARG_TOP:
729       g_value_set_int (value, video_crop->crop_top);
730       break;
731     case ARG_BOTTOM:
732       g_value_set_int (value, video_crop->crop_bottom);
733       break;
734     default:
735       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
736       break;
737   }
738   GST_OBJECT_UNLOCK (video_crop);
739 }
740
741 static gboolean
742 plugin_init (GstPlugin * plugin)
743 {
744   GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
745
746   if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
747           GST_TYPE_VIDEO_CROP)
748       && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
749           GST_TYPE_ASPECT_RATIO_CROP))
750     return TRUE;
751
752   return FALSE;
753 }
754
755 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
756     GST_VERSION_MINOR,
757     "videocrop",
758     "Crops video into a user-defined region",
759     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)