Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 /* the formats we support */
81 #define GST_VIDEO_CAPS_GRAY "video/x-raw-gray, "                        \
82     "bpp = (int) 8, "                                                  \
83     "width = " GST_VIDEO_SIZE_RANGE ", "                                \
84     "height = " GST_VIDEO_SIZE_RANGE ", "                               \
85     "framerate = " GST_VIDEO_FPS_RANGE
86
87 #define VIDEO_CROP_CAPS                          \
88   GST_VIDEO_CAPS_RGBx ";"                        \
89   GST_VIDEO_CAPS_xRGB ";"                        \
90   GST_VIDEO_CAPS_BGRx ";"                        \
91   GST_VIDEO_CAPS_xBGR ";"                        \
92   GST_VIDEO_CAPS_RGBA ";"                        \
93   GST_VIDEO_CAPS_ARGB ";"                        \
94   GST_VIDEO_CAPS_BGRA ";"                        \
95   GST_VIDEO_CAPS_ABGR ";"                        \
96   GST_VIDEO_CAPS_RGB ";"                         \
97   GST_VIDEO_CAPS_BGR ";"                         \
98   GST_VIDEO_CAPS_YUV ("AYUV") ";"                \
99   GST_VIDEO_CAPS_YUV ("YUY2") ";"                \
100   GST_VIDEO_CAPS_YUV ("YVYU") ";"                \
101   GST_VIDEO_CAPS_YUV ("UYVY") ";"                \
102   GST_VIDEO_CAPS_YUV ("Y800") ";"                \
103   GST_VIDEO_CAPS_YUV ("I420") ";"                \
104   GST_VIDEO_CAPS_YUV ("YV12") ";"                \
105   GST_VIDEO_CAPS_RGB_16 ";"                      \
106   GST_VIDEO_CAPS_RGB_15 ";"                      \
107   GST_VIDEO_CAPS_GRAY
108
109 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
110     GST_PAD_SRC,
111     GST_PAD_ALWAYS,
112     GST_STATIC_CAPS (VIDEO_CROP_CAPS)
113     );
114
115 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
116     GST_PAD_SINK,
117     GST_PAD_ALWAYS,
118     GST_STATIC_CAPS (VIDEO_CROP_CAPS)
119     );
120
121 GST_BOILERPLATE (GstVideoCrop, gst_video_crop, GstBaseTransform,
122     GST_TYPE_BASE_TRANSFORM);
123
124 static void gst_video_crop_set_property (GObject * object, guint prop_id,
125     const GValue * value, GParamSpec * pspec);
126 static void gst_video_crop_get_property (GObject * object, guint prop_id,
127     GValue * value, GParamSpec * pspec);
128
129 static GstCaps *gst_video_crop_transform_caps (GstBaseTransform * trans,
130     GstPadDirection direction, GstCaps * caps);
131 static GstFlowReturn gst_video_crop_transform (GstBaseTransform * trans,
132     GstBuffer * inbuf, GstBuffer * outbuf);
133 static gboolean gst_video_crop_get_unit_size (GstBaseTransform * trans,
134     GstCaps * caps, guint * size);
135 static gboolean gst_video_crop_set_caps (GstBaseTransform * trans,
136     GstCaps * in_caps, GstCaps * outcaps);
137 static gboolean gst_video_crop_src_event (GstBaseTransform * trans,
138     GstEvent * event);
139
140 static void
141 gst_video_crop_base_init (gpointer g_class)
142 {
143   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
144
145   gst_element_class_set_details_simple (element_class, "Crop",
146       "Filter/Effect/Video",
147       "Crops video into a user-defined region",
148       "Tim-Philipp Müller <tim centricular net>");
149
150   gst_element_class_add_static_pad_template (element_class,
151       &sink_template);
152   gst_element_class_add_static_pad_template (element_class, &src_template);
153 }
154
155 static gboolean
156 gst_video_crop_src_event (GstBaseTransform * trans, GstEvent * event)
157 {
158   GstEvent *new_event;
159   GstStructure *new_structure;
160   const GstStructure *structure;
161   const gchar *event_name;
162   double pointer_x;
163   double pointer_y;
164
165   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
166   new_event = NULL;
167
168   GST_OBJECT_LOCK (vcrop);
169   if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION &&
170       (vcrop->crop_left != 0 || vcrop->crop_top != 0)) {
171     structure = gst_event_get_structure (event);
172     event_name = gst_structure_get_string (structure, "event");
173
174     if (event_name &&
175         (strcmp (event_name, "mouse-move") == 0 ||
176             strcmp (event_name, "mouse-button-press") == 0 ||
177             strcmp (event_name, "mouse-button-release") == 0)) {
178
179       if (gst_structure_get_double (structure, "pointer_x", &pointer_x) &&
180           gst_structure_get_double (structure, "pointer_y", &pointer_y)) {
181
182         new_structure = gst_structure_copy (structure);
183         gst_structure_set (new_structure,
184             "pointer_x", G_TYPE_DOUBLE, (double) (pointer_x + vcrop->crop_left),
185             "pointer_y", G_TYPE_DOUBLE, (double) (pointer_y + vcrop->crop_top),
186             NULL);
187
188         new_event = gst_event_new_navigation (new_structure);
189         gst_event_unref (event);
190       } else {
191         GST_WARNING_OBJECT (vcrop, "Failed to read navigation event");
192       }
193     }
194   }
195
196   GST_OBJECT_UNLOCK (vcrop);
197   return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans,
198       (new_event ? new_event : event));
199 }
200
201 static void
202 gst_video_crop_class_init (GstVideoCropClass * klass)
203 {
204   GObjectClass *gobject_class;
205   GstBaseTransformClass *basetransform_class;
206
207   gobject_class = (GObjectClass *) klass;
208   basetransform_class = (GstBaseTransformClass *) klass;
209
210   gobject_class->set_property = gst_video_crop_set_property;
211   gobject_class->get_property = gst_video_crop_get_property;
212
213   g_object_class_install_property (gobject_class, ARG_LEFT,
214       g_param_spec_int ("left", "Left", "Pixels to crop at left",
215           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216   g_object_class_install_property (gobject_class, ARG_RIGHT,
217       g_param_spec_int ("right", "Right", "Pixels to crop at right",
218           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219   g_object_class_install_property (gobject_class, ARG_TOP,
220       g_param_spec_int ("top", "Top", "Pixels to crop at top",
221           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222   g_object_class_install_property (gobject_class, ARG_BOTTOM,
223       g_param_spec_int ("bottom", "Bottom", "Pixels to crop at bottom",
224           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225
226   basetransform_class->transform = GST_DEBUG_FUNCPTR (gst_video_crop_transform);
227   basetransform_class->transform_caps =
228       GST_DEBUG_FUNCPTR (gst_video_crop_transform_caps);
229   basetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_crop_set_caps);
230   basetransform_class->get_unit_size =
231       GST_DEBUG_FUNCPTR (gst_video_crop_get_unit_size);
232
233   basetransform_class->passthrough_on_same_caps = FALSE;
234   basetransform_class->src_event = GST_DEBUG_FUNCPTR (gst_video_crop_src_event);
235 }
236
237 static void
238 gst_video_crop_init (GstVideoCrop * vcrop, GstVideoCropClass * klass)
239 {
240   vcrop->crop_right = 0;
241   vcrop->crop_left = 0;
242   vcrop->crop_top = 0;
243   vcrop->crop_bottom = 0;
244 }
245
246 static gboolean
247 gst_video_crop_get_image_details_from_caps (GstVideoCrop * vcrop,
248     GstVideoCropImageDetails * details, GstCaps * caps)
249 {
250   GstStructure *structure;
251   gint width, height;
252
253   structure = gst_caps_get_structure (caps, 0);
254   if (!gst_structure_get_int (structure, "width", &width) ||
255       !gst_structure_get_int (structure, "height", &height)) {
256     goto incomplete_format;
257   }
258
259   details->width = width;
260   details->height = height;
261
262   if (gst_structure_has_name (structure, "video/x-raw-rgb") ||
263       gst_structure_has_name (structure, "video/x-raw-gray")) {
264     gint bpp = 0;
265
266     if (!gst_structure_get_int (structure, "bpp", &bpp) || (bpp & 0x07) != 0)
267       goto incomplete_format;
268
269     details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
270     details->bytes_per_pixel = bpp / 8;
271     details->stride = GST_ROUND_UP_4 (width * details->bytes_per_pixel);
272     details->size = details->stride * height;
273   } else if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
274     guint32 format = 0;
275
276     if (!gst_structure_get_fourcc (structure, "format", &format))
277       goto incomplete_format;
278
279     switch (format) {
280       case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
281         details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
282         details->bytes_per_pixel = 4;
283         details->stride = GST_ROUND_UP_4 (width * 4);
284         details->size = details->stride * height;
285         break;
286       case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
287       case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
288       case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
289         details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX;
290         details->bytes_per_pixel = 2;
291         details->stride = GST_ROUND_UP_4 (width * 2);
292         details->size = details->stride * height;
293         if (format == GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y')) {
294           /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5] */
295           details->macro_y_off = 1;
296         } else {
297           /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
298           details->macro_y_off = 0;
299         }
300         break;
301       case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
302         details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
303         details->bytes_per_pixel = 1;
304         details->stride = GST_ROUND_UP_4 (width);
305         details->size = details->stride * height;
306         break;
307       case GST_MAKE_FOURCC ('I', '4', '2', '0'):
308       case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):{
309         details->packing = VIDEO_CROP_PIXEL_FORMAT_PLANAR;
310
311         details->y_stride = GST_ROUND_UP_4 (width);
312         details->u_stride = GST_ROUND_UP_8 (width) / 2;
313         details->v_stride = GST_ROUND_UP_8 (width) / 2;
314
315         /* I420 and YV12 have U/V planes swapped, but doesn't matter for us */
316         details->y_off = 0;
317         details->u_off = 0 + details->y_stride * GST_ROUND_UP_2 (height);
318         details->v_off = details->u_off +
319             details->u_stride * (GST_ROUND_UP_2 (height) / 2);
320         details->size = details->v_off +
321             details->v_stride * (GST_ROUND_UP_2 (height) / 2);
322         break;
323       }
324       default:
325         goto unknown_format;
326     }
327   } else {
328     goto unknown_format;
329   }
330
331   return TRUE;
332
333   /* ERRORS */
334 unknown_format:
335   {
336     GST_ELEMENT_ERROR (vcrop, STREAM, NOT_IMPLEMENTED, (NULL),
337         ("Unsupported format"));
338     return FALSE;
339   }
340
341 incomplete_format:
342   {
343     GST_ELEMENT_ERROR (vcrop, CORE, NEGOTIATION, (NULL),
344         ("Incomplete caps, some required field is missing"));
345     return FALSE;
346   }
347 }
348
349 static gboolean
350 gst_video_crop_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
351     guint * size)
352 {
353   GstVideoCropImageDetails img_details = { 0, };
354   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
355
356   if (!gst_video_crop_get_image_details_from_caps (vcrop, &img_details, caps))
357     return FALSE;
358
359   *size = img_details.size;
360   return TRUE;
361 }
362
363 #define ROUND_DOWN_2(n)  ((n)&(~1))
364
365 static void
366 gst_video_crop_transform_packed_complex (GstVideoCrop * vcrop,
367     GstBuffer * inbuf, GstBuffer * outbuf)
368 {
369   guint8 *in_data, *out_data;
370   guint i, dx;
371
372   in_data = GST_BUFFER_DATA (inbuf);
373   out_data = GST_BUFFER_DATA (outbuf);
374
375   in_data += vcrop->crop_top * vcrop->in.stride;
376
377   /* rounding down here so we end up at the start of a macro-pixel and not
378    * in the middle of one */
379   in_data += ROUND_DOWN_2 (vcrop->crop_left) * vcrop->in.bytes_per_pixel;
380
381   dx = vcrop->out.width * vcrop->out.bytes_per_pixel;
382
383   /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5]
384    * YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
385   if ((vcrop->crop_left % 2) != 0) {
386     for (i = 0; i < vcrop->out.height; ++i) {
387       gint j;
388
389       memcpy (out_data, in_data, dx);
390
391       /* move just the Y samples one pixel to the left, don't worry about
392        * chroma shift */
393       for (j = vcrop->in.macro_y_off; j < vcrop->out.stride - 2; j += 2)
394         out_data[j] = in_data[j + 2];
395
396       in_data += vcrop->in.stride;
397       out_data += vcrop->out.stride;
398     }
399   } else {
400     for (i = 0; i < vcrop->out.height; ++i) {
401       memcpy (out_data, in_data, dx);
402       in_data += vcrop->in.stride;
403       out_data += vcrop->out.stride;
404     }
405   }
406 }
407
408 static void
409 gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
410     GstBuffer * inbuf, GstBuffer * outbuf)
411 {
412   guint8 *in_data, *out_data;
413   guint i, dx;
414
415   in_data = GST_BUFFER_DATA (inbuf);
416   out_data = GST_BUFFER_DATA (outbuf);
417
418   in_data += vcrop->crop_top * vcrop->in.stride;
419   in_data += vcrop->crop_left * vcrop->in.bytes_per_pixel;
420
421   dx = vcrop->out.width * vcrop->out.bytes_per_pixel;
422
423   for (i = 0; i < vcrop->out.height; ++i) {
424     memcpy (out_data, in_data, dx);
425     in_data += vcrop->in.stride;
426     out_data += vcrop->out.stride;
427   }
428 }
429
430 static void
431 gst_video_crop_transform_planar (GstVideoCrop * vcrop, GstBuffer * inbuf,
432     GstBuffer * outbuf)
433 {
434   guint8 *y_out, *u_out, *v_out;
435   guint8 *y_in, *u_in, *v_in;
436   guint i, dx;
437
438   /* Y plane */
439   y_in = GST_BUFFER_DATA (inbuf);
440   y_out = GST_BUFFER_DATA (outbuf);
441
442   y_in += (vcrop->crop_top * vcrop->in.y_stride) + vcrop->crop_left;
443   dx = vcrop->out.width * 1;
444
445   for (i = 0; i < vcrop->out.height; ++i) {
446     memcpy (y_out, y_in, dx);
447     y_in += vcrop->in.y_stride;
448     y_out += vcrop->out.y_stride;
449   }
450
451   /* U + V planes */
452   u_in = GST_BUFFER_DATA (inbuf) + vcrop->in.u_off;
453   u_out = GST_BUFFER_DATA (outbuf) + vcrop->out.u_off;
454
455   u_in += (vcrop->crop_top / 2) * vcrop->in.u_stride;
456   u_in += vcrop->crop_left / 2;
457
458   v_in = GST_BUFFER_DATA (inbuf) + vcrop->in.v_off;
459   v_out = GST_BUFFER_DATA (outbuf) + vcrop->out.v_off;
460
461   v_in += (vcrop->crop_top / 2) * vcrop->in.v_stride;
462   v_in += vcrop->crop_left / 2;
463
464   dx = GST_ROUND_UP_2 (vcrop->out.width) / 2;
465
466   for (i = 0; i < GST_ROUND_UP_2 (vcrop->out.height) / 2; ++i) {
467     memcpy (u_out, u_in, dx);
468     memcpy (v_out, v_in, dx);
469     u_in += vcrop->in.u_stride;
470     u_out += vcrop->out.u_stride;
471     v_in += vcrop->in.v_stride;
472     v_out += vcrop->out.v_stride;
473   }
474 }
475
476 static GstFlowReturn
477 gst_video_crop_transform (GstBaseTransform * trans, GstBuffer * inbuf,
478     GstBuffer * outbuf)
479 {
480   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
481
482   switch (vcrop->in.packing) {
483     case VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE:
484       gst_video_crop_transform_packed_simple (vcrop, inbuf, outbuf);
485       break;
486     case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
487       gst_video_crop_transform_packed_complex (vcrop, inbuf, outbuf);
488       break;
489     case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
490       gst_video_crop_transform_planar (vcrop, inbuf, outbuf);
491       break;
492     default:
493       g_assert_not_reached ();
494   }
495
496   return GST_FLOW_OK;
497 }
498
499 static gint
500 gst_video_crop_transform_dimension (gint val, gint delta)
501 {
502   gint64 new_val = (gint64) val + (gint64) delta;
503
504   new_val = CLAMP (new_val, 1, G_MAXINT);
505
506   return (gint) new_val;
507 }
508
509 static gboolean
510 gst_video_crop_transform_dimension_value (const GValue * src_val,
511     gint delta, GValue * dest_val)
512 {
513   gboolean ret = TRUE;
514
515   g_value_init (dest_val, G_VALUE_TYPE (src_val));
516
517   if (G_VALUE_HOLDS_INT (src_val)) {
518     gint ival = g_value_get_int (src_val);
519
520     ival = gst_video_crop_transform_dimension (ival, delta);
521     g_value_set_int (dest_val, ival);
522   } else if (GST_VALUE_HOLDS_INT_RANGE (src_val)) {
523     gint min = gst_value_get_int_range_min (src_val);
524     gint max = gst_value_get_int_range_max (src_val);
525
526     min = gst_video_crop_transform_dimension (min, delta);
527     max = gst_video_crop_transform_dimension (max, delta);
528     gst_value_set_int_range (dest_val, min, max);
529   } else if (GST_VALUE_HOLDS_LIST (src_val)) {
530     gint i;
531
532     for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
533       const GValue *list_val;
534       GValue newval = { 0, };
535
536       list_val = gst_value_list_get_value (src_val, i);
537       if (gst_video_crop_transform_dimension_value (list_val, delta, &newval))
538         gst_value_list_append_value (dest_val, &newval);
539       g_value_unset (&newval);
540     }
541
542     if (gst_value_list_get_size (dest_val) == 0) {
543       g_value_unset (dest_val);
544       ret = FALSE;
545     }
546   } else {
547     g_value_unset (dest_val);
548     ret = FALSE;
549   }
550
551   return ret;
552 }
553
554 static GstCaps *
555 gst_video_crop_transform_caps (GstBaseTransform * trans,
556     GstPadDirection direction, GstCaps * caps)
557 {
558   GstVideoCrop *vcrop;
559   GstCaps *other_caps;
560   gint dy, dx, i;
561
562   vcrop = GST_VIDEO_CROP (trans);
563
564   GST_OBJECT_LOCK (vcrop);
565
566   GST_LOG_OBJECT (vcrop, "l=%d,r=%d,b=%d,t=%d",
567       vcrop->crop_left, vcrop->crop_right, vcrop->crop_bottom, vcrop->crop_top);
568
569   if (direction == GST_PAD_SRC) {
570     dx = vcrop->crop_left + vcrop->crop_right;
571     dy = vcrop->crop_top + vcrop->crop_bottom;
572   } else {
573     dx = 0 - (vcrop->crop_left + vcrop->crop_right);
574     dy = 0 - (vcrop->crop_top + vcrop->crop_bottom);
575   }
576   GST_OBJECT_UNLOCK (vcrop);
577
578   GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
579
580   other_caps = gst_caps_new_empty ();
581
582   for (i = 0; i < gst_caps_get_size (caps); ++i) {
583     const GValue *v;
584     GstStructure *structure, *new_structure;
585     GValue w_val = { 0, }, h_val = {
586     0,};
587
588     structure = gst_caps_get_structure (caps, i);
589
590     v = gst_structure_get_value (structure, "width");
591     if (!gst_video_crop_transform_dimension_value (v, dx, &w_val)) {
592       GST_WARNING_OBJECT (vcrop, "could not tranform width value with dx=%d"
593           ", caps structure=%" GST_PTR_FORMAT, dx, structure);
594       continue;
595     }
596
597     v = gst_structure_get_value (structure, "height");
598     if (!gst_video_crop_transform_dimension_value (v, dy, &h_val)) {
599       g_value_unset (&w_val);
600       GST_WARNING_OBJECT (vcrop, "could not tranform height value with dy=%d"
601           ", caps structure=%" GST_PTR_FORMAT, dy, structure);
602       continue;
603     }
604
605     new_structure = gst_structure_copy (structure);
606     gst_structure_set_value (new_structure, "width", &w_val);
607     gst_structure_set_value (new_structure, "height", &h_val);
608     g_value_unset (&w_val);
609     g_value_unset (&h_val);
610     GST_LOG_OBJECT (vcrop, "transformed structure %2d: %" GST_PTR_FORMAT
611         " => %" GST_PTR_FORMAT, i, structure, new_structure);
612     gst_caps_append_structure (other_caps, new_structure);
613   }
614
615   if (gst_caps_is_empty (other_caps)) {
616     gst_caps_unref (other_caps);
617     other_caps = NULL;
618   }
619
620   return other_caps;
621 }
622
623 static gboolean
624 gst_video_crop_set_caps (GstBaseTransform * trans, GstCaps * incaps,
625     GstCaps * outcaps)
626 {
627   GstVideoCrop *crop = GST_VIDEO_CROP (trans);
628
629   if (!gst_video_crop_get_image_details_from_caps (crop, &crop->in, incaps))
630     goto wrong_input;
631
632   if (!gst_video_crop_get_image_details_from_caps (crop, &crop->out, outcaps))
633     goto wrong_output;
634
635   if (G_UNLIKELY ((crop->crop_left + crop->crop_right) >= crop->in.width ||
636           (crop->crop_top + crop->crop_bottom) >= crop->in.height))
637     goto cropping_too_much;
638
639   GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
640       GST_PTR_FORMAT, incaps, outcaps);
641
642   if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
643           crop_bottom) == 0) {
644     GST_LOG_OBJECT (crop, "we are using passthrough");
645     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
646   } else {
647     GST_LOG_OBJECT (crop, "we are not using passthrough");
648     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
649   }
650
651   return TRUE;
652
653   /* ERROR */
654 wrong_input:
655   {
656     GST_DEBUG_OBJECT (crop, "failed to parse input caps %" GST_PTR_FORMAT,
657         incaps);
658     return FALSE;
659   }
660 wrong_output:
661   {
662     GST_DEBUG_OBJECT (crop, "failed to parse output caps %" GST_PTR_FORMAT,
663         outcaps);
664     return FALSE;
665   }
666 cropping_too_much:
667   {
668     GST_DEBUG_OBJECT (crop, "we are cropping too much");
669     return FALSE;
670   }
671 }
672
673 static void
674 gst_video_crop_set_property (GObject * object, guint prop_id,
675     const GValue * value, GParamSpec * pspec)
676 {
677   GstVideoCrop *video_crop;
678
679   video_crop = GST_VIDEO_CROP (object);
680
681   /* don't modify while we are transforming */
682   GST_BASE_TRANSFORM_LOCK (GST_BASE_TRANSFORM_CAST (video_crop));
683
684   /* protect with the object lock so that we can read them */
685   GST_OBJECT_LOCK (video_crop);
686   switch (prop_id) {
687     case ARG_LEFT:
688       video_crop->crop_left = g_value_get_int (value);
689       break;
690     case ARG_RIGHT:
691       video_crop->crop_right = g_value_get_int (value);
692       break;
693     case ARG_TOP:
694       video_crop->crop_top = g_value_get_int (value);
695       break;
696     case ARG_BOTTOM:
697       video_crop->crop_bottom = g_value_get_int (value);
698       break;
699     default:
700       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
701       break;
702   }
703   GST_OBJECT_UNLOCK (video_crop);
704
705   GST_LOG_OBJECT (video_crop, "l=%d,r=%d,b=%d,t=%d",
706       video_crop->crop_left, video_crop->crop_right, video_crop->crop_bottom,
707       video_crop->crop_top);
708
709   gst_base_transform_reconfigure (GST_BASE_TRANSFORM (video_crop));
710   GST_BASE_TRANSFORM_UNLOCK (GST_BASE_TRANSFORM_CAST (video_crop));
711 }
712
713 static void
714 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
715     GParamSpec * pspec)
716 {
717   GstVideoCrop *video_crop;
718
719   video_crop = GST_VIDEO_CROP (object);
720
721   GST_OBJECT_LOCK (video_crop);
722   switch (prop_id) {
723     case ARG_LEFT:
724       g_value_set_int (value, video_crop->crop_left);
725       break;
726     case ARG_RIGHT:
727       g_value_set_int (value, video_crop->crop_right);
728       break;
729     case ARG_TOP:
730       g_value_set_int (value, video_crop->crop_top);
731       break;
732     case ARG_BOTTOM:
733       g_value_set_int (value, video_crop->crop_bottom);
734       break;
735     default:
736       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
737       break;
738   }
739   GST_OBJECT_UNLOCK (video_crop);
740 }
741
742 static gboolean
743 plugin_init (GstPlugin * plugin)
744 {
745   GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
746
747   if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
748           GST_TYPE_VIDEO_CROP)
749       && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
750           GST_TYPE_ASPECT_RATIO_CROP))
751     return TRUE;
752
753   return FALSE;
754 }
755
756 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
757     GST_VERSION_MINOR,
758     "videocrop",
759     "Crops video into a user-defined region",
760     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)