videocrop: Make sure new crop is applied
[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   ARG_0,
74   ARG_LEFT,
75   ARG_RIGHT,
76   ARG_TOP,
77   ARG_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, ARG_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, ARG_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, ARG_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, ARG_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 /* TODO use filter_caps */
526 static GstCaps *
527 gst_video_crop_transform_caps (GstBaseTransform * trans,
528     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
529 {
530   GstVideoCrop *vcrop;
531   GstCaps *other_caps;
532   gint dy, dx, i, left, right, bottom, top;
533   gboolean w_dynamic, h_dynamic;
534
535   vcrop = GST_VIDEO_CROP (trans);
536
537   GST_OBJECT_LOCK (vcrop);
538
539   GST_LOG_OBJECT (vcrop, "l=%d,r=%d,b=%d,t=%d",
540       vcrop->prop_left, vcrop->prop_right, vcrop->prop_bottom, vcrop->prop_top);
541
542   w_dynamic = (vcrop->prop_left == -1 || vcrop->prop_right == -1);
543   h_dynamic = (vcrop->prop_top == -1 || vcrop->prop_bottom == -1);
544
545   left = (vcrop->prop_left == -1) ? 0 : vcrop->prop_left;
546   right = (vcrop->prop_right == -1) ? 0 : vcrop->prop_right;
547   bottom = (vcrop->prop_bottom == -1) ? 0 : vcrop->prop_bottom;
548   top = (vcrop->prop_top == -1) ? 0 : vcrop->prop_top;
549
550   GST_OBJECT_UNLOCK (vcrop);
551
552   if (direction == GST_PAD_SRC) {
553     dx = left + right;
554     dy = top + bottom;
555   } else {
556     dx = 0 - (left + right);
557     dy = 0 - (top + bottom);
558   }
559
560   GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
561
562   other_caps = gst_caps_new_empty ();
563
564   for (i = 0; i < gst_caps_get_size (caps); ++i) {
565     const GValue *v;
566     GstStructure *structure, *new_structure;
567     GValue w_val = { 0, }, h_val = {
568     0,};
569
570     structure = gst_caps_get_structure (caps, i);
571
572     v = gst_structure_get_value (structure, "width");
573     if (!gst_video_crop_transform_dimension_value (v, dx, &w_val, direction,
574             w_dynamic)) {
575       GST_WARNING_OBJECT (vcrop, "could not tranform width value with dx=%d"
576           ", caps structure=%" GST_PTR_FORMAT, dx, structure);
577       continue;
578     }
579
580     v = gst_structure_get_value (structure, "height");
581     if (!gst_video_crop_transform_dimension_value (v, dy, &h_val, direction,
582             h_dynamic)) {
583       g_value_unset (&w_val);
584       GST_WARNING_OBJECT (vcrop, "could not tranform height value with dy=%d"
585           ", caps structure=%" GST_PTR_FORMAT, dy, structure);
586       continue;
587     }
588
589     new_structure = gst_structure_copy (structure);
590     gst_structure_set_value (new_structure, "width", &w_val);
591     gst_structure_set_value (new_structure, "height", &h_val);
592     g_value_unset (&w_val);
593     g_value_unset (&h_val);
594     GST_LOG_OBJECT (vcrop, "transformed structure %2d: %" GST_PTR_FORMAT
595         " => %" GST_PTR_FORMAT, i, structure, new_structure);
596     gst_caps_append_structure (other_caps, new_structure);
597   }
598
599   if (!gst_caps_is_empty (other_caps) && filter_caps) {
600     GstCaps *tmp = gst_caps_intersect_full (filter_caps, other_caps,
601         GST_CAPS_INTERSECT_FIRST);
602     gst_caps_replace (&other_caps, tmp);
603     gst_caps_unref (tmp);
604   }
605
606   return other_caps;
607 }
608
609 static gboolean
610 gst_video_crop_set_info (GstVideoFilter * vfilter, GstCaps * in,
611     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
612 {
613   GstVideoCrop *crop = GST_VIDEO_CROP (vfilter);
614   int dx, dy;
615
616   GST_OBJECT_LOCK (crop);
617   crop->need_update = FALSE;
618   crop->crop_left = crop->prop_left;
619   crop->crop_right = crop->prop_right;
620   crop->crop_top = crop->prop_top;
621   crop->crop_bottom = crop->prop_bottom;
622   GST_OBJECT_UNLOCK (crop);
623
624   dx = GST_VIDEO_INFO_WIDTH (in_info) - GST_VIDEO_INFO_WIDTH (out_info);
625   dy = GST_VIDEO_INFO_HEIGHT (in_info) - GST_VIDEO_INFO_HEIGHT (out_info);
626
627   if (crop->crop_left == -1 && crop->crop_right == -1) {
628     crop->crop_left = dx / 2;
629     crop->crop_right = dx / 2 + (dx & 1);
630   } else if (crop->crop_left == -1) {
631     if (G_UNLIKELY (crop->crop_right > dx))
632       goto cropping_too_much;
633     crop->crop_left = dx - crop->crop_right;
634   } else if (crop->crop_right == -1) {
635     if (G_UNLIKELY (crop->crop_left > dx))
636       goto cropping_too_much;
637     crop->crop_right = dx - crop->crop_left;
638   }
639
640   if (crop->crop_top == -1 && crop->crop_bottom == -1) {
641     crop->crop_top = dy / 2;
642     crop->crop_bottom = dy / 2 + (dy & 1);
643   } else if (crop->crop_top == -1) {
644     if (G_UNLIKELY (crop->crop_bottom > dy))
645       goto cropping_too_much;
646     crop->crop_top = dy - crop->crop_bottom;
647   } else if (crop->crop_bottom == -1) {
648     if (G_UNLIKELY (crop->crop_top > dy))
649       goto cropping_too_much;
650     crop->crop_bottom = dy - crop->crop_top;
651   }
652
653   if (G_UNLIKELY ((crop->crop_left + crop->crop_right) >=
654           GST_VIDEO_INFO_WIDTH (in_info)
655           || (crop->crop_top + crop->crop_bottom) >=
656           GST_VIDEO_INFO_HEIGHT (in_info)))
657     goto cropping_too_much;
658
659   if (in && out)
660     GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
661         GST_PTR_FORMAT, in, out);
662
663   if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
664           crop_bottom) == 0) {
665     GST_LOG_OBJECT (crop, "we are using passthrough");
666     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
667   } else {
668     GST_LOG_OBJECT (crop, "we are not using passthrough");
669     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
670   }
671
672   if (GST_VIDEO_INFO_IS_RGB (in_info)
673       || GST_VIDEO_INFO_IS_GRAY (in_info)) {
674     crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
675   } else {
676     switch (GST_VIDEO_INFO_FORMAT (in_info)) {
677       case GST_VIDEO_FORMAT_AYUV:
678         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
679         break;
680       case GST_VIDEO_FORMAT_YVYU:
681       case GST_VIDEO_FORMAT_YUY2:
682       case GST_VIDEO_FORMAT_UYVY:
683         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX;
684         if (GST_VIDEO_INFO_FORMAT (in_info) == GST_VIDEO_FORMAT_UYVY) {
685           /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5] */
686           crop->macro_y_off = 1;
687         } else {
688           /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
689           crop->macro_y_off = 0;
690         }
691         break;
692       case GST_VIDEO_FORMAT_I420:
693       case GST_VIDEO_FORMAT_YV12:
694         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PLANAR;
695         break;
696       case GST_VIDEO_FORMAT_NV12:
697       case GST_VIDEO_FORMAT_NV21:
698         crop->packing = VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR;
699         break;
700       default:
701         goto unknown_format;
702     }
703   }
704
705   crop->in_info = *in_info;
706   crop->out_info = *out_info;
707
708   return TRUE;
709
710   /* ERROR */
711 cropping_too_much:
712   {
713     GST_WARNING_OBJECT (crop, "we are cropping too much");
714     return FALSE;
715   }
716 unknown_format:
717   {
718     GST_WARNING_OBJECT (crop, "Unsupported format");
719     return FALSE;
720   }
721 }
722
723 /* called with object lock */
724 static inline void
725 gst_video_crop_set_crop (GstVideoCrop * vcrop, gint new_value, gint * prop)
726 {
727   if (*prop != new_value) {
728     *prop = new_value;
729     vcrop->need_update = TRUE;
730   }
731 }
732
733 static void
734 gst_video_crop_set_property (GObject * object, guint prop_id,
735     const GValue * value, GParamSpec * pspec)
736 {
737   GstVideoCrop *video_crop;
738
739   video_crop = GST_VIDEO_CROP (object);
740
741   GST_OBJECT_LOCK (video_crop);
742   switch (prop_id) {
743     case ARG_LEFT:
744       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
745           &video_crop->prop_left);
746       break;
747     case ARG_RIGHT:
748       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
749           &video_crop->prop_right);
750       break;
751     case ARG_TOP:
752       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
753           &video_crop->prop_top);
754       break;
755     case ARG_BOTTOM:
756       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
757           &video_crop->prop_bottom);
758       break;
759     default:
760       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
761       break;
762   }
763   GST_LOG_OBJECT (video_crop, "l=%d,r=%d,b=%d,t=%d",
764       video_crop->crop_left, video_crop->crop_right, video_crop->crop_bottom,
765       video_crop->crop_top);
766
767   GST_OBJECT_UNLOCK (video_crop);
768
769   gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (video_crop));
770 }
771
772 static void
773 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
774     GParamSpec * pspec)
775 {
776   GstVideoCrop *video_crop;
777
778   video_crop = GST_VIDEO_CROP (object);
779
780   GST_OBJECT_LOCK (video_crop);
781   switch (prop_id) {
782     case ARG_LEFT:
783       g_value_set_int (value, video_crop->prop_left);
784       break;
785     case ARG_RIGHT:
786       g_value_set_int (value, video_crop->prop_right);
787       break;
788     case ARG_TOP:
789       g_value_set_int (value, video_crop->prop_top);
790       break;
791     case ARG_BOTTOM:
792       g_value_set_int (value, video_crop->prop_bottom);
793       break;
794     default:
795       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
796       break;
797   }
798   GST_OBJECT_UNLOCK (video_crop);
799 }
800
801 static gboolean
802 plugin_init (GstPlugin * plugin)
803 {
804   GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
805
806   if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
807           GST_TYPE_VIDEO_CROP)
808       && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
809           GST_TYPE_ASPECT_RATIO_CROP))
810     return TRUE;
811
812   return FALSE;
813 }
814
815 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
816     GST_VERSION_MINOR,
817     videocrop,
818     "Crops video into a user-defined region",
819     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)