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