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