Merge branch '0.11' of ssh://git.freedesktop.org/git/gstreamer/gst-plugins-good into...
[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   GstMapInfo in_map, out_map;
312   guint8 *in_data, *out_data;
313   guint i, dx;
314   gint in_stride;
315   gint out_stride;
316
317   gst_buffer_map (inbuf, &in_map, GST_MAP_READ);
318   gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
319
320   in_data = in_map.data;
321   out_data = out_map.data;
322
323   in_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
324   out_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);
325
326   in_data += vcrop->crop_top * in_stride;
327
328   /* rounding down here so we end up at the start of a macro-pixel and not
329    * in the middle of one */
330   in_data +=
331       ROUND_DOWN_2 (vcrop->crop_left) *
332       GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->in.info, 0);
333
334   dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) *
335       GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->out.info, 0);
336
337   /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5]
338    * YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
339   if ((vcrop->crop_left % 2) != 0) {
340     for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
341       gint j;
342
343       memcpy (out_data, in_data, dx);
344
345       /* move just the Y samples one pixel to the left, don't worry about
346        * chroma shift */
347       for (j = vcrop->in.macro_y_off; j < out_stride - 2; j += 2)
348         out_data[j] = in_data[j + 2];
349
350       in_data += in_stride;
351       out_data += out_stride;
352     }
353   } else {
354     for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
355       memcpy (out_data, in_data, dx);
356       in_data += in_stride;
357       out_data += out_stride;
358     }
359   }
360   gst_buffer_unmap (inbuf, &in_map);
361   gst_buffer_unmap (outbuf, &out_map);
362 }
363
364 static void
365 gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
366     GstBuffer * inbuf, GstBuffer * outbuf)
367 {
368   GstMapInfo in_map, out_map;
369   guint8 *in_data, *out_data;
370   guint i, dx;
371   gint in_stride, out_stride;
372
373   gst_buffer_map (inbuf, &in_map, GST_MAP_READ);
374   gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
375
376   in_data = in_map.data;
377   out_data = out_map.data;
378
379   in_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
380   out_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);
381
382   in_data += vcrop->crop_top * in_stride;
383   in_data +=
384       vcrop->crop_left * GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->in.info, 0);
385
386   dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) *
387       GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->out.info, 0);
388
389   for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
390     memcpy (out_data, in_data, dx);
391     in_data += in_stride;
392     out_data += out_stride;
393   }
394   gst_buffer_unmap (inbuf, &in_map);
395   gst_buffer_unmap (outbuf, &out_map);
396 }
397
398 static void
399 gst_video_crop_transform_planar (GstVideoCrop * vcrop, GstBuffer * inbuf,
400     GstBuffer * outbuf)
401 {
402   GstMapInfo in_map, out_map;
403   guint8 *y_out, *u_out, *v_out;
404   guint8 *y_in, *u_in, *v_in;
405   guint i, dx;
406
407   gst_buffer_map (inbuf, &in_map, GST_MAP_READ);
408   gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
409
410   /* Y plane */
411   y_in = in_map.data;
412   y_out = out_map.data;
413
414   y_in +=
415       (vcrop->crop_top * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info,
416           0)) + vcrop->crop_left;
417   dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) * 1;
418
419   for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
420     memcpy (y_out, y_in, dx);
421     y_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
422     y_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);
423   }
424
425   /* U + V planes */
426   u_in =
427       (guint8 *) in_map.data + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->in.info, 1);
428   u_out =
429       (guint8 *) out_map.data + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->out.info,
430       1);
431
432   u_in +=
433       (vcrop->crop_top / 2) * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 1);
434   u_in += vcrop->crop_left / 2;
435
436   v_in =
437       (guint8 *) in_map.data + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->in.info, 2);
438   v_out =
439       (guint8 *) out_map.data + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->out.info,
440       2);
441
442   v_in +=
443       (vcrop->crop_top / 2) * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 2);
444   v_in += vcrop->crop_left / 2;
445
446   dx = GST_ROUND_UP_2 (GST_VIDEO_INFO_WIDTH (&vcrop->out.info)) / 2;
447
448   for (i = 0; i < GST_ROUND_UP_2 (GST_VIDEO_INFO_HEIGHT (&vcrop->out.info)) / 2;
449       ++i) {
450     memcpy (u_out, u_in, dx);
451     memcpy (v_out, v_in, dx);
452     u_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 1);
453     u_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 1);
454     v_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 2);
455     v_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 2);
456   }
457
458   gst_buffer_unmap (inbuf, &in_map);
459   gst_buffer_unmap (outbuf, &out_map);
460 }
461
462 static GstFlowReturn
463 gst_video_crop_transform (GstBaseTransform * trans, GstBuffer * inbuf,
464     GstBuffer * outbuf)
465 {
466   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
467
468   switch (vcrop->in.packing) {
469     case VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE:
470       gst_video_crop_transform_packed_simple (vcrop, inbuf, outbuf);
471       break;
472     case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
473       gst_video_crop_transform_packed_complex (vcrop, inbuf, outbuf);
474       break;
475     case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
476       gst_video_crop_transform_planar (vcrop, inbuf, outbuf);
477       break;
478     default:
479       g_assert_not_reached ();
480   }
481
482   return GST_FLOW_OK;
483 }
484
485 static gint
486 gst_video_crop_transform_dimension (gint val, gint delta)
487 {
488   gint64 new_val = (gint64) val + (gint64) delta;
489
490   new_val = CLAMP (new_val, 1, G_MAXINT);
491
492   return (gint) new_val;
493 }
494
495 static gboolean
496 gst_video_crop_transform_dimension_value (const GValue * src_val,
497     gint delta, GValue * dest_val)
498 {
499   gboolean ret = TRUE;
500
501   g_value_init (dest_val, G_VALUE_TYPE (src_val));
502
503   if (G_VALUE_HOLDS_INT (src_val)) {
504     gint ival = g_value_get_int (src_val);
505
506     ival = gst_video_crop_transform_dimension (ival, delta);
507     g_value_set_int (dest_val, ival);
508   } else if (GST_VALUE_HOLDS_INT_RANGE (src_val)) {
509     gint min = gst_value_get_int_range_min (src_val);
510     gint max = gst_value_get_int_range_max (src_val);
511
512     min = gst_video_crop_transform_dimension (min, delta);
513     max = gst_video_crop_transform_dimension (max, delta);
514     gst_value_set_int_range (dest_val, min, max);
515   } else if (GST_VALUE_HOLDS_LIST (src_val)) {
516     gint i;
517
518     for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
519       const GValue *list_val;
520       GValue newval = { 0, };
521
522       list_val = gst_value_list_get_value (src_val, i);
523       if (gst_video_crop_transform_dimension_value (list_val, delta, &newval))
524         gst_value_list_append_value (dest_val, &newval);
525       g_value_unset (&newval);
526     }
527
528     if (gst_value_list_get_size (dest_val) == 0) {
529       g_value_unset (dest_val);
530       ret = FALSE;
531     }
532   } else {
533     g_value_unset (dest_val);
534     ret = FALSE;
535   }
536
537   return ret;
538 }
539
540 /* TODO use filter_caps */
541 static GstCaps *
542 gst_video_crop_transform_caps (GstBaseTransform * trans,
543     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
544 {
545   GstVideoCrop *vcrop;
546   GstCaps *other_caps;
547   gint dy, dx, i;
548
549   vcrop = GST_VIDEO_CROP (trans);
550
551   GST_OBJECT_LOCK (vcrop);
552
553   GST_LOG_OBJECT (vcrop, "l=%d,r=%d,b=%d,t=%d",
554       vcrop->crop_left, vcrop->crop_right, vcrop->crop_bottom, vcrop->crop_top);
555
556   if (direction == GST_PAD_SRC) {
557     dx = vcrop->crop_left + vcrop->crop_right;
558     dy = vcrop->crop_top + vcrop->crop_bottom;
559   } else {
560     dx = 0 - (vcrop->crop_left + vcrop->crop_right);
561     dy = 0 - (vcrop->crop_top + vcrop->crop_bottom);
562   }
563   GST_OBJECT_UNLOCK (vcrop);
564
565   GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
566
567   other_caps = gst_caps_new_empty ();
568
569   for (i = 0; i < gst_caps_get_size (caps); ++i) {
570     const GValue *v;
571     GstStructure *structure, *new_structure;
572     GValue w_val = { 0, }, h_val = {
573     0,};
574
575     structure = gst_caps_get_structure (caps, i);
576
577     v = gst_structure_get_value (structure, "width");
578     if (!gst_video_crop_transform_dimension_value (v, dx, &w_val)) {
579       GST_WARNING_OBJECT (vcrop, "could not tranform width value with dx=%d"
580           ", caps structure=%" GST_PTR_FORMAT, dx, structure);
581       continue;
582     }
583
584     v = gst_structure_get_value (structure, "height");
585     if (!gst_video_crop_transform_dimension_value (v, dy, &h_val)) {
586       g_value_unset (&w_val);
587       GST_WARNING_OBJECT (vcrop, "could not tranform height value with dy=%d"
588           ", caps structure=%" GST_PTR_FORMAT, dy, structure);
589       continue;
590     }
591
592     new_structure = gst_structure_copy (structure);
593     gst_structure_set_value (new_structure, "width", &w_val);
594     gst_structure_set_value (new_structure, "height", &h_val);
595     g_value_unset (&w_val);
596     g_value_unset (&h_val);
597     GST_LOG_OBJECT (vcrop, "transformed structure %2d: %" GST_PTR_FORMAT
598         " => %" GST_PTR_FORMAT, i, structure, new_structure);
599     gst_caps_append_structure (other_caps, new_structure);
600   }
601
602   if (gst_caps_is_empty (other_caps)) {
603     gst_caps_unref (other_caps);
604     other_caps = NULL;
605   }
606
607   if (other_caps && filter_caps) {
608     GstCaps *tmp = gst_caps_intersect_full (filter_caps, other_caps,
609         GST_CAPS_INTERSECT_FIRST);
610     gst_caps_replace (&other_caps, tmp);
611     gst_caps_unref (tmp);
612   }
613
614   return other_caps;
615 }
616
617 static gboolean
618 gst_video_crop_set_caps (GstBaseTransform * trans, GstCaps * incaps,
619     GstCaps * outcaps)
620 {
621   GstVideoCrop *crop = GST_VIDEO_CROP (trans);
622
623   if (!gst_video_crop_get_image_details_from_caps (crop, &crop->in, incaps))
624     goto wrong_input;
625
626   if (!gst_video_crop_get_image_details_from_caps (crop, &crop->out, outcaps))
627     goto wrong_output;
628
629   if (G_UNLIKELY ((crop->crop_left + crop->crop_right) >=
630           GST_VIDEO_INFO_WIDTH (&crop->in.info)
631           || (crop->crop_top + crop->crop_bottom) >=
632           GST_VIDEO_INFO_HEIGHT (&crop->in.info)))
633     goto cropping_too_much;
634
635   GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
636       GST_PTR_FORMAT, incaps, outcaps);
637
638   if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
639           crop_bottom) == 0) {
640     GST_LOG_OBJECT (crop, "we are using passthrough");
641     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
642   } else {
643     GST_LOG_OBJECT (crop, "we are not using passthrough");
644     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
645   }
646
647   return TRUE;
648
649   /* ERROR */
650 wrong_input:
651   {
652     GST_DEBUG_OBJECT (crop, "failed to parse input caps %" GST_PTR_FORMAT,
653         incaps);
654     return FALSE;
655   }
656 wrong_output:
657   {
658     GST_DEBUG_OBJECT (crop, "failed to parse output caps %" GST_PTR_FORMAT,
659         outcaps);
660     return FALSE;
661   }
662 cropping_too_much:
663   {
664     GST_DEBUG_OBJECT (crop, "we are cropping too much");
665     return FALSE;
666   }
667 }
668
669 static void
670 gst_video_crop_set_property (GObject * object, guint prop_id,
671     const GValue * value, GParamSpec * pspec)
672 {
673   GstVideoCrop *video_crop;
674
675   video_crop = GST_VIDEO_CROP (object);
676
677   /* don't modify while we are transforming */
678   GST_BASE_TRANSFORM_LOCK (GST_BASE_TRANSFORM_CAST (video_crop));
679
680   /* protect with the object lock so that we can read them */
681   GST_OBJECT_LOCK (video_crop);
682   switch (prop_id) {
683     case ARG_LEFT:
684       video_crop->crop_left = g_value_get_int (value);
685       break;
686     case ARG_RIGHT:
687       video_crop->crop_right = g_value_get_int (value);
688       break;
689     case ARG_TOP:
690       video_crop->crop_top = g_value_get_int (value);
691       break;
692     case ARG_BOTTOM:
693       video_crop->crop_bottom = g_value_get_int (value);
694       break;
695     default:
696       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
697       break;
698   }
699   GST_OBJECT_UNLOCK (video_crop);
700
701   GST_LOG_OBJECT (video_crop, "l=%d,r=%d,b=%d,t=%d",
702       video_crop->crop_left, video_crop->crop_right, video_crop->crop_bottom,
703       video_crop->crop_top);
704
705   gst_base_transform_reconfigure (GST_BASE_TRANSFORM (video_crop));
706   GST_BASE_TRANSFORM_UNLOCK (GST_BASE_TRANSFORM_CAST (video_crop));
707 }
708
709 static void
710 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
711     GParamSpec * pspec)
712 {
713   GstVideoCrop *video_crop;
714
715   video_crop = GST_VIDEO_CROP (object);
716
717   GST_OBJECT_LOCK (video_crop);
718   switch (prop_id) {
719     case ARG_LEFT:
720       g_value_set_int (value, video_crop->crop_left);
721       break;
722     case ARG_RIGHT:
723       g_value_set_int (value, video_crop->crop_right);
724       break;
725     case ARG_TOP:
726       g_value_set_int (value, video_crop->crop_top);
727       break;
728     case ARG_BOTTOM:
729       g_value_set_int (value, video_crop->crop_bottom);
730       break;
731     default:
732       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
733       break;
734   }
735   GST_OBJECT_UNLOCK (video_crop);
736 }
737
738 static gboolean
739 plugin_init (GstPlugin * plugin)
740 {
741   GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
742
743   if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
744           GST_TYPE_VIDEO_CROP)
745       && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
746           GST_TYPE_ASPECT_RATIO_CROP))
747     return TRUE;
748
749   return FALSE;
750 }
751
752 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
753     GST_VERSION_MINOR,
754     "videocrop",
755     "Crops video into a user-defined region",
756     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)