Merge branch 'plugin-move-rtp-h265'
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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-1.0 -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   PROP_0,
74   PROP_LEFT,
75   PROP_RIGHT,
76   PROP_TOP,
77   PROP_BOTTOM
78 };
79
80 /* we support the same caps as aspectratiocrop (sync changes) */
81 #define VIDEO_CROP_CAPS                                \
82   GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, "    \
83       "RGBA, ARGB, BGRA, ABGR, RGB, BGR, AYUV, YUY2, " \
84       "YVYU, UYVY, I420, YV12, RGB16, RGB15, GRAY8, "  \
85       "NV12, NV21, GRAY16_LE, GRAY16_BE }")
86
87 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
88     GST_PAD_SRC,
89     GST_PAD_ALWAYS,
90     GST_STATIC_CAPS (VIDEO_CROP_CAPS)
91     );
92
93 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
94     GST_PAD_SINK,
95     GST_PAD_ALWAYS,
96     GST_STATIC_CAPS (VIDEO_CROP_CAPS)
97     );
98
99 #define gst_video_crop_parent_class parent_class
100 G_DEFINE_TYPE (GstVideoCrop, gst_video_crop, GST_TYPE_VIDEO_FILTER);
101
102 static void gst_video_crop_set_property (GObject * object, guint prop_id,
103     const GValue * value, GParamSpec * pspec);
104 static void gst_video_crop_get_property (GObject * object, guint prop_id,
105     GValue * value, GParamSpec * pspec);
106
107 static GstCaps *gst_video_crop_transform_caps (GstBaseTransform * trans,
108     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps);
109 static gboolean gst_video_crop_src_event (GstBaseTransform * trans,
110     GstEvent * event);
111
112 static gboolean gst_video_crop_set_info (GstVideoFilter * vfilter, GstCaps * in,
113     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info);
114 static GstFlowReturn gst_video_crop_transform_frame (GstVideoFilter * vfilter,
115     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
116
117 static gboolean
118 gst_video_crop_src_event (GstBaseTransform * trans, GstEvent * event)
119 {
120   GstEvent *new_event;
121   GstStructure *new_structure;
122   const GstStructure *structure;
123   const gchar *event_name;
124   double pointer_x;
125   double pointer_y;
126
127   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
128   new_event = NULL;
129
130   GST_OBJECT_LOCK (vcrop);
131   if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION &&
132       (vcrop->crop_left != 0 || vcrop->crop_top != 0)) {
133     structure = gst_event_get_structure (event);
134     event_name = gst_structure_get_string (structure, "event");
135
136     if (event_name &&
137         (strcmp (event_name, "mouse-move") == 0 ||
138             strcmp (event_name, "mouse-button-press") == 0 ||
139             strcmp (event_name, "mouse-button-release") == 0)) {
140
141       if (gst_structure_get_double (structure, "pointer_x", &pointer_x) &&
142           gst_structure_get_double (structure, "pointer_y", &pointer_y)) {
143
144         new_structure = gst_structure_copy (structure);
145         gst_structure_set (new_structure,
146             "pointer_x", G_TYPE_DOUBLE, (double) (pointer_x + vcrop->crop_left),
147             "pointer_y", G_TYPE_DOUBLE, (double) (pointer_y + vcrop->crop_top),
148             NULL);
149
150         new_event = gst_event_new_navigation (new_structure);
151         gst_event_unref (event);
152       } else {
153         GST_WARNING_OBJECT (vcrop, "Failed to read navigation event");
154       }
155     }
156   }
157
158   GST_OBJECT_UNLOCK (vcrop);
159
160   return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans,
161       (new_event ? new_event : event));
162 }
163
164 static void
165 gst_video_crop_class_init (GstVideoCropClass * klass)
166 {
167   GObjectClass *gobject_class;
168   GstElementClass *element_class;
169   GstBaseTransformClass *basetransform_class;
170   GstVideoFilterClass *vfilter_class;
171
172   gobject_class = (GObjectClass *) klass;
173   element_class = (GstElementClass *) klass;
174   basetransform_class = (GstBaseTransformClass *) klass;
175   vfilter_class = (GstVideoFilterClass *) klass;
176
177   gobject_class->set_property = gst_video_crop_set_property;
178   gobject_class->get_property = gst_video_crop_get_property;
179
180   g_object_class_install_property (gobject_class, PROP_LEFT,
181       g_param_spec_int ("left", "Left",
182           "Pixels to crop at left (-1 to auto-crop)", -1, G_MAXINT, 0,
183           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184   g_object_class_install_property (gobject_class, PROP_RIGHT,
185       g_param_spec_int ("right", "Right",
186           "Pixels to crop at right (-1 to auto-crop)", -1, G_MAXINT, 0,
187           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188   g_object_class_install_property (gobject_class, PROP_TOP,
189       g_param_spec_int ("top", "Top",
190           "Pixels to crop at top (-1 to auto-crop)", -1, G_MAXINT, 0,
191           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192   g_object_class_install_property (gobject_class, PROP_BOTTOM,
193       g_param_spec_int ("bottom", "Bottom",
194           "Pixels to crop at bottom (-1 to auto-crop)", -1, G_MAXINT, 0,
195           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196
197   gst_element_class_add_pad_template (element_class,
198       gst_static_pad_template_get (&sink_template));
199   gst_element_class_add_pad_template (element_class,
200       gst_static_pad_template_get (&src_template));
201   gst_element_class_set_static_metadata (element_class, "Crop",
202       "Filter/Effect/Video",
203       "Crops video into a user-defined region",
204       "Tim-Philipp Müller <tim centricular net>");
205
206   basetransform_class->transform_caps =
207       GST_DEBUG_FUNCPTR (gst_video_crop_transform_caps);
208   basetransform_class->src_event = GST_DEBUG_FUNCPTR (gst_video_crop_src_event);
209
210   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_crop_set_info);
211   vfilter_class->transform_frame =
212       GST_DEBUG_FUNCPTR (gst_video_crop_transform_frame);
213 }
214
215 static void
216 gst_video_crop_init (GstVideoCrop * vcrop)
217 {
218   vcrop->crop_right = 0;
219   vcrop->crop_left = 0;
220   vcrop->crop_top = 0;
221   vcrop->crop_bottom = 0;
222 }
223
224 #define ROUND_DOWN_2(n)  ((n)&(~1))
225
226 static void
227 gst_video_crop_transform_packed_complex (GstVideoCrop * vcrop,
228     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
229 {
230   guint8 *in_data, *out_data;
231   guint i, dx;
232   gint width, height;
233   gint in_stride;
234   gint out_stride;
235
236   width = GST_VIDEO_FRAME_WIDTH (out_frame);
237   height = GST_VIDEO_FRAME_HEIGHT (out_frame);
238
239   in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
240   out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
241
242   in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
243   out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
244
245   in_data += vcrop->crop_top * in_stride;
246
247   /* rounding down here so we end up at the start of a macro-pixel and not
248    * in the middle of one */
249   in_data += ROUND_DOWN_2 (vcrop->crop_left) *
250       GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, 0);
251
252   dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
253
254   /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5]
255    * YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
256   if ((vcrop->crop_left % 2) != 0) {
257     for (i = 0; i < height; ++i) {
258       gint j;
259
260       memcpy (out_data, in_data, dx);
261
262       /* move just the Y samples one pixel to the left, don't worry about
263        * chroma shift */
264       for (j = vcrop->macro_y_off; j < out_stride - 2; j += 2)
265         out_data[j] = in_data[j + 2];
266
267       in_data += in_stride;
268       out_data += out_stride;
269     }
270   } else {
271     for (i = 0; i < height; ++i) {
272       memcpy (out_data, in_data, dx);
273       in_data += in_stride;
274       out_data += out_stride;
275     }
276   }
277 }
278
279 static void
280 gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
281     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
282 {
283   guint8 *in_data, *out_data;
284   gint width, height;
285   guint i, dx;
286   gint in_stride, out_stride;
287
288   width = GST_VIDEO_FRAME_WIDTH (out_frame);
289   height = GST_VIDEO_FRAME_HEIGHT (out_frame);
290
291   in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
292   out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
293
294   in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
295   out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
296
297   in_data += vcrop->crop_top * in_stride;
298   in_data += vcrop->crop_left * GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, 0);
299
300   dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
301
302   for (i = 0; i < height; ++i) {
303     memcpy (out_data, in_data, dx);
304     in_data += in_stride;
305     out_data += out_stride;
306   }
307 }
308
309 static void
310 gst_video_crop_transform_planar (GstVideoCrop * vcrop,
311     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
312 {
313   gint width, height;
314   guint8 *y_out, *u_out, *v_out;
315   guint8 *y_in, *u_in, *v_in;
316   guint i, dx;
317
318   width = GST_VIDEO_FRAME_WIDTH (out_frame);
319   height = GST_VIDEO_FRAME_HEIGHT (out_frame);
320
321   /* Y plane */
322   y_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
323   y_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
324
325   y_in +=
326       (vcrop->crop_top * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame,
327           0)) + vcrop->crop_left;
328   dx = width;
329
330   for (i = 0; i < height; ++i) {
331     memcpy (y_out, y_in, dx);
332     y_in += GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
333     y_out += GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
334   }
335
336   /* U + V planes */
337   u_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 1);
338   u_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 1);
339
340   u_in += (vcrop->crop_top / 2) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 1);
341   u_in += vcrop->crop_left / 2;
342
343   v_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 2);
344   v_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 2);
345
346   v_in += (vcrop->crop_top / 2) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 2);
347   v_in += vcrop->crop_left / 2;
348
349   dx = GST_ROUND_UP_2 (width) / 2;
350
351   for (i = 0; i < GST_ROUND_UP_2 (height) / 2; ++i) {
352     memcpy (u_out, u_in, dx);
353     memcpy (v_out, v_in, dx);
354     u_in += GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 1);
355     u_out += GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 1);
356     v_in += GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 2);
357     v_out += GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 2);
358   }
359 }
360
361 static void
362 gst_video_crop_transform_semi_planar (GstVideoCrop * vcrop,
363     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
364 {
365   gint width, height;
366   guint8 *y_out, *uv_out;
367   guint8 *y_in, *uv_in;
368   guint i, dx;
369
370   width = GST_VIDEO_FRAME_WIDTH (out_frame);
371   height = GST_VIDEO_FRAME_HEIGHT (out_frame);
372
373   /* Y plane */
374   y_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
375   y_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
376
377   /* UV plane */
378   uv_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 1);
379   uv_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 1);
380
381   y_in += vcrop->crop_top * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0) +
382       vcrop->crop_left;
383   dx = width;
384
385   for (i = 0; i < height; ++i) {
386     memcpy (y_out, y_in, dx);
387     y_in += GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
388     y_out += GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
389   }
390
391   uv_in += (vcrop->crop_top / 2) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 1);
392   uv_in += GST_ROUND_DOWN_2 (vcrop->crop_left);
393   dx = GST_ROUND_UP_2 (width);
394
395   for (i = 0; i < GST_ROUND_UP_2 (height) / 2; i++) {
396     memcpy (uv_out, uv_in, dx);
397     uv_in += GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 1);
398     uv_out += GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 1);
399   }
400 }
401
402 static GstFlowReturn
403 gst_video_crop_transform_frame (GstVideoFilter * vfilter,
404     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
405 {
406   GstVideoCrop *vcrop = GST_VIDEO_CROP (vfilter);
407
408   if (G_UNLIKELY (vcrop->need_update)) {
409     if (!gst_video_crop_set_info (vfilter, NULL, &vcrop->in_info, NULL,
410             &vcrop->out_info)) {
411       return GST_FLOW_ERROR;
412     }
413   }
414
415   switch (vcrop->packing) {
416     case VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE:
417       gst_video_crop_transform_packed_simple (vcrop, in_frame, out_frame);
418       break;
419     case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
420       gst_video_crop_transform_packed_complex (vcrop, in_frame, out_frame);
421       break;
422     case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
423       gst_video_crop_transform_planar (vcrop, in_frame, out_frame);
424       break;
425     case VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR:
426       gst_video_crop_transform_semi_planar (vcrop, in_frame, out_frame);
427       break;
428     default:
429       g_assert_not_reached ();
430   }
431
432   return GST_FLOW_OK;
433 }
434
435 static gint
436 gst_video_crop_transform_dimension (gint val, gint delta)
437 {
438   gint64 new_val = (gint64) val + (gint64) delta;
439
440   new_val = CLAMP (new_val, 1, G_MAXINT);
441
442   return (gint) new_val;
443 }
444
445 static gboolean
446 gst_video_crop_transform_dimension_value (const GValue * src_val,
447     gint delta, GValue * dest_val, GstPadDirection direction, gboolean dynamic)
448 {
449   gboolean ret = TRUE;
450
451   if (G_VALUE_HOLDS_INT (src_val)) {
452     gint ival = g_value_get_int (src_val);
453     ival = gst_video_crop_transform_dimension (ival, delta);
454
455     if (dynamic) {
456       if (direction == GST_PAD_SRC) {
457         if (ival == G_MAXINT) {
458           g_value_init (dest_val, G_TYPE_INT);
459           g_value_set_int (dest_val, ival);
460         } else {
461           g_value_init (dest_val, GST_TYPE_INT_RANGE);
462           gst_value_set_int_range (dest_val, ival, G_MAXINT);
463         }
464       } else {
465         if (ival == 1) {
466           g_value_init (dest_val, G_TYPE_INT);
467           g_value_set_int (dest_val, ival);
468         } else {
469           g_value_init (dest_val, GST_TYPE_INT_RANGE);
470           gst_value_set_int_range (dest_val, 1, ival);
471         }
472       }
473     } else {
474       g_value_init (dest_val, G_TYPE_INT);
475       g_value_set_int (dest_val, ival);
476     }
477   } else if (GST_VALUE_HOLDS_INT_RANGE (src_val)) {
478     gint min = gst_value_get_int_range_min (src_val);
479     gint max = gst_value_get_int_range_max (src_val);
480
481     min = gst_video_crop_transform_dimension (min, delta);
482     max = gst_video_crop_transform_dimension (max, delta);
483
484     if (dynamic) {
485       if (direction == GST_PAD_SRC)
486         max = G_MAXINT;
487       else
488         min = 1;
489     }
490
491     if (min == max) {
492       g_value_init (dest_val, G_TYPE_INT);
493       g_value_set_int (dest_val, min);
494     } else {
495       g_value_init (dest_val, GST_TYPE_INT_RANGE);
496       gst_value_set_int_range (dest_val, min, max);
497     }
498   } else if (GST_VALUE_HOLDS_LIST (src_val)) {
499     gint i;
500
501     g_value_init (dest_val, GST_TYPE_LIST);
502
503     for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
504       const GValue *list_val;
505       GValue newval = { 0, };
506
507       list_val = gst_value_list_get_value (src_val, i);
508       if (gst_video_crop_transform_dimension_value (list_val, delta, &newval,
509               direction, dynamic))
510         gst_value_list_append_value (dest_val, &newval);
511       g_value_unset (&newval);
512     }
513
514     if (gst_value_list_get_size (dest_val) == 0) {
515       g_value_unset (dest_val);
516       ret = FALSE;
517     }
518   } else {
519     ret = FALSE;
520   }
521
522   return ret;
523 }
524
525 static GstCaps *
526 gst_video_crop_transform_caps (GstBaseTransform * trans,
527     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
528 {
529   GstVideoCrop *vcrop;
530   GstCaps *other_caps;
531   gint dy, dx, i, left, right, bottom, top;
532   gboolean w_dynamic, h_dynamic;
533
534   vcrop = GST_VIDEO_CROP (trans);
535
536   GST_OBJECT_LOCK (vcrop);
537
538   GST_LOG_OBJECT (vcrop, "l=%d,r=%d,b=%d,t=%d",
539       vcrop->prop_left, vcrop->prop_right, vcrop->prop_bottom, vcrop->prop_top);
540
541   w_dynamic = (vcrop->prop_left == -1 || vcrop->prop_right == -1);
542   h_dynamic = (vcrop->prop_top == -1 || vcrop->prop_bottom == -1);
543
544   left = (vcrop->prop_left == -1) ? 0 : vcrop->prop_left;
545   right = (vcrop->prop_right == -1) ? 0 : vcrop->prop_right;
546   bottom = (vcrop->prop_bottom == -1) ? 0 : vcrop->prop_bottom;
547   top = (vcrop->prop_top == -1) ? 0 : vcrop->prop_top;
548
549   GST_OBJECT_UNLOCK (vcrop);
550
551   if (direction == GST_PAD_SRC) {
552     dx = left + right;
553     dy = top + bottom;
554   } else {
555     dx = 0 - (left + right);
556     dy = 0 - (top + bottom);
557   }
558
559   GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
560
561   other_caps = gst_caps_new_empty ();
562
563   for (i = 0; i < gst_caps_get_size (caps); ++i) {
564     const GValue *v;
565     GstStructure *structure, *new_structure;
566     GValue w_val = { 0, }, h_val = {
567     0,};
568
569     structure = gst_caps_get_structure (caps, i);
570
571     v = gst_structure_get_value (structure, "width");
572     if (!gst_video_crop_transform_dimension_value (v, dx, &w_val, direction,
573             w_dynamic)) {
574       GST_WARNING_OBJECT (vcrop, "could not tranform width value with dx=%d"
575           ", caps structure=%" GST_PTR_FORMAT, dx, structure);
576       continue;
577     }
578
579     v = gst_structure_get_value (structure, "height");
580     if (!gst_video_crop_transform_dimension_value (v, dy, &h_val, direction,
581             h_dynamic)) {
582       g_value_unset (&w_val);
583       GST_WARNING_OBJECT (vcrop, "could not tranform height value with dy=%d"
584           ", caps structure=%" GST_PTR_FORMAT, dy, structure);
585       continue;
586     }
587
588     new_structure = gst_structure_copy (structure);
589     gst_structure_set_value (new_structure, "width", &w_val);
590     gst_structure_set_value (new_structure, "height", &h_val);
591     g_value_unset (&w_val);
592     g_value_unset (&h_val);
593     GST_LOG_OBJECT (vcrop, "transformed structure %2d: %" GST_PTR_FORMAT
594         " => %" GST_PTR_FORMAT, i, structure, new_structure);
595     gst_caps_append_structure (other_caps, new_structure);
596   }
597
598   if (!gst_caps_is_empty (other_caps) && filter_caps) {
599     GstCaps *tmp = gst_caps_intersect_full (filter_caps, other_caps,
600         GST_CAPS_INTERSECT_FIRST);
601     gst_caps_replace (&other_caps, tmp);
602     gst_caps_unref (tmp);
603   }
604
605   return other_caps;
606 }
607
608 static gboolean
609 gst_video_crop_set_info (GstVideoFilter * vfilter, GstCaps * in,
610     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
611 {
612   GstVideoCrop *crop = GST_VIDEO_CROP (vfilter);
613   int dx, dy;
614
615   GST_OBJECT_LOCK (crop);
616   crop->need_update = FALSE;
617   crop->crop_left = crop->prop_left;
618   crop->crop_right = crop->prop_right;
619   crop->crop_top = crop->prop_top;
620   crop->crop_bottom = crop->prop_bottom;
621   GST_OBJECT_UNLOCK (crop);
622
623   dx = GST_VIDEO_INFO_WIDTH (in_info) - GST_VIDEO_INFO_WIDTH (out_info);
624   dy = GST_VIDEO_INFO_HEIGHT (in_info) - GST_VIDEO_INFO_HEIGHT (out_info);
625
626   if (crop->crop_left == -1 && crop->crop_right == -1) {
627     crop->crop_left = dx / 2;
628     crop->crop_right = dx / 2 + (dx & 1);
629   } else if (crop->crop_left == -1) {
630     if (G_UNLIKELY (crop->crop_right > dx))
631       goto cropping_too_much;
632     crop->crop_left = dx - crop->crop_right;
633   } else if (crop->crop_right == -1) {
634     if (G_UNLIKELY (crop->crop_left > dx))
635       goto cropping_too_much;
636     crop->crop_right = dx - crop->crop_left;
637   }
638
639   if (crop->crop_top == -1 && crop->crop_bottom == -1) {
640     crop->crop_top = dy / 2;
641     crop->crop_bottom = dy / 2 + (dy & 1);
642   } else if (crop->crop_top == -1) {
643     if (G_UNLIKELY (crop->crop_bottom > dy))
644       goto cropping_too_much;
645     crop->crop_top = dy - crop->crop_bottom;
646   } else if (crop->crop_bottom == -1) {
647     if (G_UNLIKELY (crop->crop_top > dy))
648       goto cropping_too_much;
649     crop->crop_bottom = dy - crop->crop_top;
650   }
651
652   if (G_UNLIKELY ((crop->crop_left + crop->crop_right) >=
653           GST_VIDEO_INFO_WIDTH (in_info)
654           || (crop->crop_top + crop->crop_bottom) >=
655           GST_VIDEO_INFO_HEIGHT (in_info)))
656     goto cropping_too_much;
657
658   if (in && out)
659     GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
660         GST_PTR_FORMAT, in, out);
661
662   if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
663           crop_bottom) == 0) {
664     GST_LOG_OBJECT (crop, "we are using passthrough");
665     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
666   } else {
667     GST_LOG_OBJECT (crop, "we are not using passthrough");
668     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
669   }
670
671   if (GST_VIDEO_INFO_IS_RGB (in_info)
672       || GST_VIDEO_INFO_IS_GRAY (in_info)) {
673     crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
674   } else {
675     switch (GST_VIDEO_INFO_FORMAT (in_info)) {
676       case GST_VIDEO_FORMAT_AYUV:
677         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
678         break;
679       case GST_VIDEO_FORMAT_YVYU:
680       case GST_VIDEO_FORMAT_YUY2:
681       case GST_VIDEO_FORMAT_UYVY:
682         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX;
683         if (GST_VIDEO_INFO_FORMAT (in_info) == GST_VIDEO_FORMAT_UYVY) {
684           /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5] */
685           crop->macro_y_off = 1;
686         } else {
687           /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
688           crop->macro_y_off = 0;
689         }
690         break;
691       case GST_VIDEO_FORMAT_I420:
692       case GST_VIDEO_FORMAT_YV12:
693         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PLANAR;
694         break;
695       case GST_VIDEO_FORMAT_NV12:
696       case GST_VIDEO_FORMAT_NV21:
697         crop->packing = VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR;
698         break;
699       default:
700         goto unknown_format;
701     }
702   }
703
704   crop->in_info = *in_info;
705   crop->out_info = *out_info;
706
707   return TRUE;
708
709   /* ERROR */
710 cropping_too_much:
711   {
712     GST_WARNING_OBJECT (crop, "we are cropping too much");
713     return FALSE;
714   }
715 unknown_format:
716   {
717     GST_WARNING_OBJECT (crop, "Unsupported format");
718     return FALSE;
719   }
720 }
721
722 /* called with object lock */
723 static inline void
724 gst_video_crop_set_crop (GstVideoCrop * vcrop, gint new_value, gint * prop)
725 {
726   if (*prop != new_value) {
727     *prop = new_value;
728     vcrop->need_update = TRUE;
729   }
730 }
731
732 static void
733 gst_video_crop_set_property (GObject * object, guint prop_id,
734     const GValue * value, GParamSpec * pspec)
735 {
736   GstVideoCrop *video_crop;
737
738   video_crop = GST_VIDEO_CROP (object);
739
740   GST_OBJECT_LOCK (video_crop);
741   switch (prop_id) {
742     case PROP_LEFT:
743       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
744           &video_crop->prop_left);
745       break;
746     case PROP_RIGHT:
747       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
748           &video_crop->prop_right);
749       break;
750     case PROP_TOP:
751       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
752           &video_crop->prop_top);
753       break;
754     case PROP_BOTTOM:
755       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
756           &video_crop->prop_bottom);
757       break;
758     default:
759       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
760       break;
761   }
762   GST_LOG_OBJECT (video_crop, "l=%d,r=%d,b=%d,t=%d, need_update:%d",
763       video_crop->prop_left, video_crop->prop_right, video_crop->prop_bottom,
764       video_crop->prop_top, video_crop->need_update);
765
766   GST_OBJECT_UNLOCK (video_crop);
767
768   gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (video_crop));
769 }
770
771 static void
772 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
773     GParamSpec * pspec)
774 {
775   GstVideoCrop *video_crop;
776
777   video_crop = GST_VIDEO_CROP (object);
778
779   GST_OBJECT_LOCK (video_crop);
780   switch (prop_id) {
781     case PROP_LEFT:
782       g_value_set_int (value, video_crop->prop_left);
783       break;
784     case PROP_RIGHT:
785       g_value_set_int (value, video_crop->prop_right);
786       break;
787     case PROP_TOP:
788       g_value_set_int (value, video_crop->prop_top);
789       break;
790     case PROP_BOTTOM:
791       g_value_set_int (value, video_crop->prop_bottom);
792       break;
793     default:
794       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
795       break;
796   }
797   GST_OBJECT_UNLOCK (video_crop);
798 }
799
800 static gboolean
801 plugin_init (GstPlugin * plugin)
802 {
803   GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
804
805   if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
806           GST_TYPE_VIDEO_CROP)
807       && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
808           GST_TYPE_ASPECT_RATIO_CROP))
809     return TRUE;
810
811   return FALSE;
812 }
813
814 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
815     GST_VERSION_MINOR,
816     videocrop,
817     "Crops video into a user-defined region",
818     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)