Fix usage of C99
[platform/upstream/gstreamer.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  * @title: videocrop
23  * @see_also: #GstVideoBox
24  *
25  * This element crops video frames, meaning it can remove parts of the
26  * picture on the left, right, top or bottom of the picture and output
27  * a smaller picture than the input picture, with the unwanted parts at the
28  * border removed.
29  *
30  * The videocrop element is similar to the videobox element, but its main
31  * goal is to support a multitude of formats as efficiently as possible.
32  * Unlike videbox, it cannot add borders to the picture and unlike videbox
33  * it will always output images in exactly the same format as the input image.
34  *
35  * If there is nothing to crop, the element will operate in pass-through mode.
36  *
37  * Note that no special efforts are made to handle chroma-subsampled formats
38  * in the case of odd-valued cropping and compensate for sub-unit chroma plane
39  * shifts for such formats in the case where the #GstVideoCrop:left or
40  * #GstVideoCrop:top property is set to an odd number. This doesn't matter for
41  * most use cases, but it might matter for yours.
42  *
43  * ## Example launch line
44  * |[
45  * gst-launch-1.0 -v videotestsrc ! videocrop top=42 left=1 right=4 bottom=0 ! ximagesink
46  * ]|
47  *
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, Y444, " \
84       "Y42B, Y41B, YVYU, UYVY, I420, YV12, RGB16, RGB15, "  \
85       "GRAY8, 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 void gst_video_crop_before_transform (GstBaseTransform * trans,
108     GstBuffer * in);
109 static GstCaps *gst_video_crop_transform_caps (GstBaseTransform * trans,
110     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps);
111 static gboolean gst_video_crop_src_event (GstBaseTransform * trans,
112     GstEvent * event);
113
114 static gboolean gst_video_crop_set_info (GstVideoFilter * vfilter, GstCaps * in,
115     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info);
116 static GstFlowReturn gst_video_crop_transform_frame (GstVideoFilter * vfilter,
117     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
118
119 static gboolean gst_video_crop_decide_allocation (GstBaseTransform * trans,
120     GstQuery * query);
121 static gboolean gst_video_crop_propose_allocation (GstBaseTransform * trans,
122     GstQuery * decide_query, GstQuery * query);
123 static GstFlowReturn gst_video_crop_transform_ip (GstBaseTransform * trans,
124     GstBuffer * buf);
125
126 static gboolean
127 gst_video_crop_src_event (GstBaseTransform * trans, GstEvent * event)
128 {
129   GstEvent *new_event;
130   GstStructure *new_structure;
131   const GstStructure *structure;
132   const gchar *event_name;
133   double pointer_x;
134   double pointer_y;
135
136   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
137   new_event = NULL;
138
139   GST_OBJECT_LOCK (vcrop);
140   if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION &&
141       (vcrop->crop_left != 0 || vcrop->crop_top != 0)) {
142     structure = gst_event_get_structure (event);
143     event_name = gst_structure_get_string (structure, "event");
144
145     if (event_name &&
146         (strcmp (event_name, "mouse-move") == 0 ||
147             strcmp (event_name, "mouse-button-press") == 0 ||
148             strcmp (event_name, "mouse-button-release") == 0)) {
149
150       if (gst_structure_get_double (structure, "pointer_x", &pointer_x) &&
151           gst_structure_get_double (structure, "pointer_y", &pointer_y)) {
152
153         new_structure = gst_structure_copy (structure);
154         gst_structure_set (new_structure,
155             "pointer_x", G_TYPE_DOUBLE, (double) (pointer_x + vcrop->crop_left),
156             "pointer_y", G_TYPE_DOUBLE, (double) (pointer_y + vcrop->crop_top),
157             NULL);
158
159         new_event = gst_event_new_navigation (new_structure);
160         gst_event_unref (event);
161       } else {
162         GST_WARNING_OBJECT (vcrop, "Failed to read navigation event");
163       }
164     }
165   }
166
167   GST_OBJECT_UNLOCK (vcrop);
168
169   return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans,
170       (new_event ? new_event : event));
171 }
172
173 static void
174 gst_video_crop_class_init (GstVideoCropClass * klass)
175 {
176   GObjectClass *gobject_class;
177   GstElementClass *element_class;
178   GstBaseTransformClass *basetransform_class;
179   GstVideoFilterClass *vfilter_class;
180
181   gobject_class = (GObjectClass *) klass;
182   element_class = (GstElementClass *) klass;
183   basetransform_class = (GstBaseTransformClass *) klass;
184   vfilter_class = (GstVideoFilterClass *) klass;
185
186   gobject_class->set_property = gst_video_crop_set_property;
187   gobject_class->get_property = gst_video_crop_get_property;
188
189   g_object_class_install_property (gobject_class, PROP_LEFT,
190       g_param_spec_int ("left", "Left",
191           "Pixels to crop at left (-1 to auto-crop)", -1, G_MAXINT, 0,
192           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
193           GST_PARAM_MUTABLE_PLAYING | GST_PARAM_CONTROLLABLE));
194   g_object_class_install_property (gobject_class, PROP_RIGHT,
195       g_param_spec_int ("right", "Right",
196           "Pixels to crop at right (-1 to auto-crop)", -1, G_MAXINT, 0,
197           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
198           GST_PARAM_MUTABLE_PLAYING | GST_PARAM_CONTROLLABLE));
199   g_object_class_install_property (gobject_class, PROP_TOP,
200       g_param_spec_int ("top", "Top", "Pixels to crop at top (-1 to auto-crop)",
201           -1, G_MAXINT, 0,
202           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
203           GST_PARAM_MUTABLE_PLAYING | GST_PARAM_CONTROLLABLE));
204   g_object_class_install_property (gobject_class, PROP_BOTTOM,
205       g_param_spec_int ("bottom", "Bottom",
206           "Pixels to crop at bottom (-1 to auto-crop)", -1, G_MAXINT, 0,
207           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
208           GST_PARAM_MUTABLE_PLAYING | GST_PARAM_CONTROLLABLE));
209
210   gst_element_class_add_static_pad_template (element_class, &sink_template);
211   gst_element_class_add_static_pad_template (element_class, &src_template);
212   gst_element_class_set_static_metadata (element_class, "Crop",
213       "Filter/Effect/Video",
214       "Crops video into a user-defined region",
215       "Tim-Philipp Müller <tim centricular net>");
216
217   basetransform_class->before_transform =
218       GST_DEBUG_FUNCPTR (gst_video_crop_before_transform);
219   basetransform_class->transform_ip_on_passthrough = FALSE;
220   basetransform_class->transform_caps =
221       GST_DEBUG_FUNCPTR (gst_video_crop_transform_caps);
222   basetransform_class->src_event = GST_DEBUG_FUNCPTR (gst_video_crop_src_event);
223   basetransform_class->decide_allocation =
224       GST_DEBUG_FUNCPTR (gst_video_crop_decide_allocation);
225   basetransform_class->propose_allocation =
226       GST_DEBUG_FUNCPTR (gst_video_crop_propose_allocation);
227   basetransform_class->transform_ip =
228       GST_DEBUG_FUNCPTR (gst_video_crop_transform_ip);
229
230   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_crop_set_info);
231   vfilter_class->transform_frame =
232       GST_DEBUG_FUNCPTR (gst_video_crop_transform_frame);
233 }
234
235 static void
236 gst_video_crop_init (GstVideoCrop * vcrop)
237 {
238   vcrop->crop_right = 0;
239   vcrop->crop_left = 0;
240   vcrop->crop_top = 0;
241   vcrop->crop_bottom = 0;
242 }
243
244 #define ROUND_DOWN_2(n)  ((n)&(~1))
245
246 static void
247 gst_video_crop_transform_packed_complex (GstVideoCrop * vcrop,
248     GstVideoFrame * in_frame, GstVideoFrame * out_frame, gint x, gint y)
249 {
250   guint8 *in_data, *out_data;
251   guint i, dx;
252   gint width, height;
253   gint in_stride;
254   gint out_stride;
255
256   width = GST_VIDEO_FRAME_WIDTH (out_frame);
257   height = GST_VIDEO_FRAME_HEIGHT (out_frame);
258
259   in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
260   out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
261
262   in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
263   out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
264
265   in_data += vcrop->crop_top * in_stride;
266
267   /* rounding down here so we end up at the start of a macro-pixel and not
268    * in the middle of one */
269   in_data += ROUND_DOWN_2 (vcrop->crop_left) *
270       GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, 0);
271
272   dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
273
274   /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5]
275    * YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
276   if ((vcrop->crop_left % 2) != 0) {
277     for (i = 0; i < height; ++i) {
278       gint j;
279
280       memcpy (out_data, in_data, dx);
281
282       /* move just the Y samples one pixel to the left, don't worry about
283        * chroma shift */
284       for (j = vcrop->macro_y_off; j < out_stride - 2; j += 2)
285         out_data[j] = in_data[j + 2];
286
287       in_data += in_stride;
288       out_data += out_stride;
289     }
290   } else {
291     for (i = 0; i < height; ++i) {
292       memcpy (out_data, in_data, dx);
293       in_data += in_stride;
294       out_data += out_stride;
295     }
296   }
297 }
298
299 static void
300 gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
301     GstVideoFrame * in_frame, GstVideoFrame * out_frame, gint x, gint y)
302 {
303   guint8 *in_data, *out_data;
304   gint width, height;
305   guint i, dx;
306   gint in_stride, out_stride;
307
308   width = GST_VIDEO_FRAME_WIDTH (out_frame);
309   height = GST_VIDEO_FRAME_HEIGHT (out_frame);
310
311   in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
312   out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
313
314   in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
315   out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
316
317   in_data += (vcrop->crop_top + y) * in_stride;
318   in_data +=
319       (vcrop->crop_left + x) * GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, 0);
320
321   dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
322
323   for (i = 0; i < height; ++i) {
324     memcpy (out_data, in_data, dx);
325     in_data += in_stride;
326     out_data += out_stride;
327   }
328 }
329
330 static void
331 gst_video_crop_transform_planar (GstVideoCrop * vcrop,
332     GstVideoFrame * in_frame, GstVideoFrame * out_frame, gint x, gint y)
333 {
334   const GstVideoFormatInfo *format_info;
335   gint crop_top, crop_left;
336   guint p;
337
338   format_info = in_frame->info.finfo;
339   crop_left = vcrop->crop_left + x;
340   crop_top = vcrop->crop_top + y;
341
342   for (p = 0; p < GST_VIDEO_FRAME_N_PLANES (in_frame); ++p) {
343     guint8 *plane_in, *plane_out;
344     guint sub_w_factor, sub_h_factor;
345     guint subsampled_crop_left, subsampled_crop_top;
346     guint copy_width;
347     gint i;
348
349     /* plane */
350     plane_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, p);
351     plane_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, p);
352
353     /* apply crop top/left
354      * crop_top and crop_left have to be rounded down to the corresponding
355      * subsampling factor, since, e.g.: the first line in a subsampled plane
356      * describes 2 lines in the actual image. A crop_top of 1 thus should
357      * not shift the pointer of the input plane. */
358     sub_w_factor = 1 << GST_VIDEO_FORMAT_INFO_W_SUB (format_info, p);
359     sub_h_factor = 1 << GST_VIDEO_FORMAT_INFO_H_SUB (format_info, p);
360     subsampled_crop_left = GST_ROUND_DOWN_N ((guint) crop_left, sub_w_factor);
361     subsampled_crop_top = GST_ROUND_DOWN_N ((guint) crop_top, sub_h_factor);
362
363     plane_in +=
364         GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (format_info, p,
365         subsampled_crop_top) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, p);
366     plane_in +=
367         GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (format_info, p,
368         subsampled_crop_left);
369     copy_width = (guint) GST_VIDEO_FRAME_COMP_WIDTH (out_frame, p);
370
371     for (i = 0; i < GST_VIDEO_FRAME_COMP_HEIGHT (out_frame, p); ++i) {
372       memcpy (plane_out, plane_in, copy_width);
373       plane_in += GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, p);
374       plane_out += GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, p);
375     }
376   }
377 }
378
379 static void
380 gst_video_crop_transform_semi_planar (GstVideoCrop * vcrop,
381     GstVideoFrame * in_frame, GstVideoFrame * out_frame, gint x, gint y)
382 {
383   gint width, height;
384   gint crop_top, crop_left;
385   guint8 *y_out, *uv_out;
386   guint8 *y_in, *uv_in;
387   guint i, dx;
388
389   width = GST_VIDEO_FRAME_WIDTH (out_frame);
390   height = GST_VIDEO_FRAME_HEIGHT (out_frame);
391   crop_left = vcrop->crop_left + x;
392   crop_top = vcrop->crop_top + y;
393
394   /* Y plane */
395   y_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
396   y_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
397
398   /* UV plane */
399   uv_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 1);
400   uv_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 1);
401
402   y_in += crop_top * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0) + crop_left;
403   dx = width;
404
405   for (i = 0; i < height; ++i) {
406     memcpy (y_out, y_in, dx);
407     y_in += GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
408     y_out += GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
409   }
410
411   uv_in += (crop_top / 2) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 1);
412   uv_in += GST_ROUND_DOWN_2 (crop_left);
413   dx = GST_ROUND_UP_2 (width);
414
415   for (i = 0; i < GST_ROUND_UP_2 (height) / 2; i++) {
416     memcpy (uv_out, uv_in, dx);
417     uv_in += GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 1);
418     uv_out += GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 1);
419   }
420 }
421
422 static GstFlowReturn
423 gst_video_crop_transform_frame (GstVideoFilter * vfilter,
424     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
425 {
426   GstVideoCrop *vcrop = GST_VIDEO_CROP (vfilter);
427   GstVideoCropMeta *meta = gst_buffer_get_video_crop_meta (in_frame->buffer);
428   gint x = 0, y = 0;
429
430   if (G_UNLIKELY (vcrop->need_update)) {
431     if (!gst_video_crop_set_info (vfilter, NULL, &vcrop->in_info, NULL,
432             &vcrop->out_info)) {
433       return GST_FLOW_ERROR;
434     }
435   }
436
437   if (meta) {
438     x = meta->x;
439     y = meta->y;
440   }
441
442   switch (vcrop->packing) {
443     case VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE:
444       gst_video_crop_transform_packed_simple (vcrop, in_frame, out_frame, x, y);
445       break;
446     case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
447       gst_video_crop_transform_packed_complex (vcrop, in_frame, out_frame, x,
448           y);
449       break;
450     case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
451       gst_video_crop_transform_planar (vcrop, in_frame, out_frame, x, y);
452       break;
453     case VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR:
454       gst_video_crop_transform_semi_planar (vcrop, in_frame, out_frame, x, y);
455       break;
456     default:
457       g_assert_not_reached ();
458   }
459
460   return GST_FLOW_OK;
461 }
462
463 static gboolean
464 gst_video_crop_decide_allocation (GstBaseTransform * trans, GstQuery * query)
465 {
466   GstVideoCrop *crop = GST_VIDEO_CROP (trans);
467   gboolean use_crop_meta;
468
469   use_crop_meta = (gst_query_find_allocation_meta (query,
470           GST_VIDEO_CROP_META_API_TYPE, NULL) &&
471       gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL));
472
473   if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
474           crop_bottom) == 0) {
475     GST_INFO_OBJECT (crop, "we are using passthrough");
476     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
477     gst_base_transform_set_in_place (GST_BASE_TRANSFORM (crop), FALSE);
478   } else if (use_crop_meta) {
479     GST_INFO_OBJECT (crop, "we are doing in-place transform using crop meta");
480     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
481     gst_base_transform_set_in_place (GST_BASE_TRANSFORM (crop), TRUE);
482   } else {
483     GST_INFO_OBJECT (crop, "we are not using passthrough");
484     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
485     gst_base_transform_set_in_place (GST_BASE_TRANSFORM (crop), FALSE);
486   }
487
488   return GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
489       query);
490 }
491
492 static gboolean
493 gst_video_crop_propose_allocation (GstBaseTransform * trans,
494     GstQuery * decide_query, GstQuery * query)
495 {
496   /* if we are not passthrough, we can handle video meta and crop meta */
497   if (decide_query) {
498     GST_DEBUG_OBJECT (trans, "Advertising video meta and crop meta support");
499     gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
500     gst_query_add_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE, NULL);
501   }
502
503   return GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
504       decide_query, query);
505 }
506
507 static void
508 gst_video_crop_before_transform (GstBaseTransform * trans, GstBuffer * in)
509 {
510   GstVideoCrop *video_crop = GST_VIDEO_CROP (trans);
511   GstClockTime timestamp, stream_time;
512
513   timestamp = GST_BUFFER_TIMESTAMP (in);
514   stream_time =
515       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
516
517   GST_DEBUG_OBJECT (video_crop, "sync to %" GST_TIME_FORMAT,
518       GST_TIME_ARGS (timestamp));
519
520   if (GST_CLOCK_TIME_IS_VALID (stream_time))
521     gst_object_sync_values (GST_OBJECT (video_crop), stream_time);
522 }
523
524 static GstFlowReturn
525 gst_video_crop_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
526 {
527   GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
528   GstVideoFilter *vfilter = GST_VIDEO_FILTER (trans);
529   GstVideoMeta *video_meta;
530   GstVideoCropMeta *crop_meta;
531
532   GST_LOG_OBJECT (trans, "Transforming in-place");
533
534   if (G_UNLIKELY (vcrop->need_update)) {
535     if (!gst_video_crop_set_info (vfilter, NULL, &vcrop->in_info, NULL,
536             &vcrop->out_info)) {
537       return GST_FLOW_ERROR;
538     }
539   }
540
541   /* The video meta is required since we are going to make the caps
542    * width/height smaller, which would not result in a usable GstVideoInfo for
543    * mapping the buffer. */
544   video_meta = gst_buffer_get_video_meta (buf);
545   if (!video_meta) {
546     video_meta = gst_buffer_add_video_meta (buf, GST_VIDEO_FRAME_FLAG_NONE,
547         GST_VIDEO_INFO_FORMAT (&vcrop->in_info), vcrop->in_info.width,
548         vcrop->in_info.height);
549   }
550
551   crop_meta = gst_buffer_get_video_crop_meta (buf);
552   if (!crop_meta)
553     crop_meta = gst_buffer_add_video_crop_meta (buf);
554
555   crop_meta->x += vcrop->crop_left;
556   crop_meta->y += vcrop->crop_top;
557   crop_meta->width = GST_VIDEO_INFO_WIDTH (&vcrop->out_info);
558   crop_meta->height = GST_VIDEO_INFO_HEIGHT (&vcrop->out_info);
559
560   return GST_FLOW_OK;
561 }
562
563 static gint
564 gst_video_crop_transform_dimension (gint val, gint delta)
565 {
566   gint64 new_val = (gint64) val + (gint64) delta;
567
568   new_val = CLAMP (new_val, 1, G_MAXINT);
569
570   return (gint) new_val;
571 }
572
573 static gboolean
574 gst_video_crop_transform_dimension_value (const GValue * src_val,
575     gint delta, GValue * dest_val, GstPadDirection direction, gboolean dynamic)
576 {
577   gboolean ret = TRUE;
578
579   if (G_VALUE_HOLDS_INT (src_val)) {
580     gint ival = g_value_get_int (src_val);
581     ival = gst_video_crop_transform_dimension (ival, delta);
582
583     if (dynamic) {
584       if (direction == GST_PAD_SRC) {
585         if (ival == G_MAXINT) {
586           g_value_init (dest_val, G_TYPE_INT);
587           g_value_set_int (dest_val, ival);
588         } else {
589           g_value_init (dest_val, GST_TYPE_INT_RANGE);
590           gst_value_set_int_range (dest_val, ival, G_MAXINT);
591         }
592       } else {
593         if (ival == 1) {
594           g_value_init (dest_val, G_TYPE_INT);
595           g_value_set_int (dest_val, ival);
596         } else {
597           g_value_init (dest_val, GST_TYPE_INT_RANGE);
598           gst_value_set_int_range (dest_val, 1, ival);
599         }
600       }
601     } else {
602       g_value_init (dest_val, G_TYPE_INT);
603       g_value_set_int (dest_val, ival);
604     }
605   } else if (GST_VALUE_HOLDS_INT_RANGE (src_val)) {
606     gint min = gst_value_get_int_range_min (src_val);
607     gint max = gst_value_get_int_range_max (src_val);
608
609     min = gst_video_crop_transform_dimension (min, delta);
610     max = gst_video_crop_transform_dimension (max, delta);
611
612     if (dynamic) {
613       if (direction == GST_PAD_SRC)
614         max = G_MAXINT;
615       else
616         min = 1;
617     }
618
619     if (min == max) {
620       g_value_init (dest_val, G_TYPE_INT);
621       g_value_set_int (dest_val, min);
622     } else {
623       g_value_init (dest_val, GST_TYPE_INT_RANGE);
624       gst_value_set_int_range (dest_val, min, max);
625     }
626   } else if (GST_VALUE_HOLDS_LIST (src_val)) {
627     gint i;
628
629     g_value_init (dest_val, GST_TYPE_LIST);
630
631     for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
632       const GValue *list_val;
633       GValue newval = G_VALUE_INIT;
634
635       list_val = gst_value_list_get_value (src_val, i);
636       if (gst_video_crop_transform_dimension_value (list_val, delta, &newval,
637               direction, dynamic))
638         gst_value_list_append_value (dest_val, &newval);
639       g_value_unset (&newval);
640     }
641
642     if (gst_value_list_get_size (dest_val) == 0) {
643       g_value_unset (dest_val);
644       ret = FALSE;
645     }
646   } else {
647     ret = FALSE;
648   }
649
650   return ret;
651 }
652
653 static GstCaps *
654 gst_video_crop_transform_caps (GstBaseTransform * trans,
655     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
656 {
657   GstVideoCrop *vcrop;
658   GstCaps *other_caps;
659   gint dy, dx, i, left, right, bottom, top;
660   gboolean w_dynamic, h_dynamic;
661
662   vcrop = GST_VIDEO_CROP (trans);
663
664   GST_OBJECT_LOCK (vcrop);
665
666   GST_LOG_OBJECT (vcrop, "l=%d,r=%d,b=%d,t=%d",
667       vcrop->prop_left, vcrop->prop_right, vcrop->prop_bottom, vcrop->prop_top);
668
669   w_dynamic = (vcrop->prop_left == -1 || vcrop->prop_right == -1);
670   h_dynamic = (vcrop->prop_top == -1 || vcrop->prop_bottom == -1);
671
672   left = (vcrop->prop_left == -1) ? 0 : vcrop->prop_left;
673   right = (vcrop->prop_right == -1) ? 0 : vcrop->prop_right;
674   bottom = (vcrop->prop_bottom == -1) ? 0 : vcrop->prop_bottom;
675   top = (vcrop->prop_top == -1) ? 0 : vcrop->prop_top;
676
677   GST_OBJECT_UNLOCK (vcrop);
678
679   if (direction == GST_PAD_SRC) {
680     dx = left + right;
681     dy = top + bottom;
682   } else {
683     dx = 0 - (left + right);
684     dy = 0 - (top + bottom);
685   }
686
687   GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
688
689   other_caps = gst_caps_new_empty ();
690
691   for (i = 0; i < gst_caps_get_size (caps); ++i) {
692     const GValue *v;
693     GstStructure *structure, *new_structure;
694     GValue w_val = G_VALUE_INIT, h_val = G_VALUE_INIT;
695
696     structure = gst_caps_get_structure (caps, i);
697
698     v = gst_structure_get_value (structure, "width");
699     if (!gst_video_crop_transform_dimension_value (v, dx, &w_val, direction,
700             w_dynamic)) {
701       GST_WARNING_OBJECT (vcrop, "could not transform width value with dx=%d"
702           ", caps structure=%" GST_PTR_FORMAT, dx, structure);
703       continue;
704     }
705
706     v = gst_structure_get_value (structure, "height");
707     if (!gst_video_crop_transform_dimension_value (v, dy, &h_val, direction,
708             h_dynamic)) {
709       g_value_unset (&w_val);
710       GST_WARNING_OBJECT (vcrop, "could not transform height value with dy=%d"
711           ", caps structure=%" GST_PTR_FORMAT, dy, structure);
712       continue;
713     }
714
715     new_structure = gst_structure_copy (structure);
716     gst_structure_set_value (new_structure, "width", &w_val);
717     gst_structure_set_value (new_structure, "height", &h_val);
718     g_value_unset (&w_val);
719     g_value_unset (&h_val);
720     GST_LOG_OBJECT (vcrop, "transformed structure %2d: %" GST_PTR_FORMAT
721         " => %" GST_PTR_FORMAT, i, structure, new_structure);
722     gst_caps_append_structure (other_caps, new_structure);
723   }
724
725   if (!gst_caps_is_empty (other_caps) && filter_caps) {
726     GstCaps *tmp = gst_caps_intersect_full (filter_caps, other_caps,
727         GST_CAPS_INTERSECT_FIRST);
728     gst_caps_replace (&other_caps, tmp);
729     gst_caps_unref (tmp);
730   }
731
732   return other_caps;
733 }
734
735 static gboolean
736 gst_video_crop_set_info (GstVideoFilter * vfilter, GstCaps * in,
737     GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
738 {
739   GstVideoCrop *crop = GST_VIDEO_CROP (vfilter);
740   int dx, dy;
741
742   GST_OBJECT_LOCK (crop);
743   crop->need_update = FALSE;
744   crop->crop_left = crop->prop_left;
745   crop->crop_right = crop->prop_right;
746   crop->crop_top = crop->prop_top;
747   crop->crop_bottom = crop->prop_bottom;
748   GST_OBJECT_UNLOCK (crop);
749
750   dx = GST_VIDEO_INFO_WIDTH (in_info) - GST_VIDEO_INFO_WIDTH (out_info);
751   dy = GST_VIDEO_INFO_HEIGHT (in_info) - GST_VIDEO_INFO_HEIGHT (out_info);
752
753   if (crop->crop_left == -1 && crop->crop_right == -1) {
754     crop->crop_left = dx / 2;
755     crop->crop_right = dx / 2 + (dx & 1);
756   } else if (crop->crop_left == -1) {
757     if (G_UNLIKELY (crop->crop_right > dx))
758       goto cropping_too_much;
759     crop->crop_left = dx - crop->crop_right;
760   } else if (crop->crop_right == -1) {
761     if (G_UNLIKELY (crop->crop_left > dx))
762       goto cropping_too_much;
763     crop->crop_right = dx - crop->crop_left;
764   }
765
766   if (crop->crop_top == -1 && crop->crop_bottom == -1) {
767     crop->crop_top = dy / 2;
768     crop->crop_bottom = dy / 2 + (dy & 1);
769   } else if (crop->crop_top == -1) {
770     if (G_UNLIKELY (crop->crop_bottom > dy))
771       goto cropping_too_much;
772     crop->crop_top = dy - crop->crop_bottom;
773   } else if (crop->crop_bottom == -1) {
774     if (G_UNLIKELY (crop->crop_top > dy))
775       goto cropping_too_much;
776     crop->crop_bottom = dy - crop->crop_top;
777   }
778
779   if (G_UNLIKELY ((crop->crop_left + crop->crop_right) >=
780           GST_VIDEO_INFO_WIDTH (in_info)
781           || (crop->crop_top + crop->crop_bottom) >=
782           GST_VIDEO_INFO_HEIGHT (in_info)))
783     goto cropping_too_much;
784
785   if (in && out)
786     GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
787         GST_PTR_FORMAT, in, out);
788
789   if (GST_VIDEO_INFO_IS_RGB (in_info)
790       || GST_VIDEO_INFO_IS_GRAY (in_info)) {
791     crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
792   } else {
793     switch (GST_VIDEO_INFO_FORMAT (in_info)) {
794       case GST_VIDEO_FORMAT_AYUV:
795         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
796         break;
797       case GST_VIDEO_FORMAT_YVYU:
798       case GST_VIDEO_FORMAT_YUY2:
799       case GST_VIDEO_FORMAT_UYVY:
800         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX;
801         if (GST_VIDEO_INFO_FORMAT (in_info) == GST_VIDEO_FORMAT_UYVY) {
802           /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5] */
803           crop->macro_y_off = 1;
804         } else {
805           /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
806           crop->macro_y_off = 0;
807         }
808         break;
809       case GST_VIDEO_FORMAT_I420:
810       case GST_VIDEO_FORMAT_YV12:
811       case GST_VIDEO_FORMAT_Y444:
812       case GST_VIDEO_FORMAT_Y42B:
813       case GST_VIDEO_FORMAT_Y41B:
814         crop->packing = VIDEO_CROP_PIXEL_FORMAT_PLANAR;
815         break;
816       case GST_VIDEO_FORMAT_NV12:
817       case GST_VIDEO_FORMAT_NV21:
818         crop->packing = VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR;
819         break;
820       default:
821         goto unknown_format;
822     }
823   }
824
825   crop->in_info = *in_info;
826   crop->out_info = *out_info;
827
828   /* Ensure our decide_allocation will be called again when needed */
829   if (gst_base_transform_is_passthrough (GST_BASE_TRANSFORM (crop))) {
830     gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
831     gst_base_transform_set_in_place (GST_BASE_TRANSFORM (crop), FALSE);
832   }
833
834   return TRUE;
835
836   /* ERROR */
837 cropping_too_much:
838   {
839     GST_WARNING_OBJECT (crop, "we are cropping too much");
840     return FALSE;
841   }
842 unknown_format:
843   {
844     GST_WARNING_OBJECT (crop, "Unsupported format");
845     return FALSE;
846   }
847 }
848
849 /* called with object lock */
850 static inline void
851 gst_video_crop_set_crop (GstVideoCrop * vcrop, gint new_value, gint * prop)
852 {
853   if (*prop != new_value) {
854     *prop = new_value;
855     vcrop->need_update = TRUE;
856   }
857 }
858
859 static void
860 gst_video_crop_set_property (GObject * object, guint prop_id,
861     const GValue * value, GParamSpec * pspec)
862 {
863   GstVideoCrop *video_crop;
864
865   video_crop = GST_VIDEO_CROP (object);
866
867   GST_OBJECT_LOCK (video_crop);
868   switch (prop_id) {
869     case PROP_LEFT:
870       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
871           &video_crop->prop_left);
872       break;
873     case PROP_RIGHT:
874       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
875           &video_crop->prop_right);
876       break;
877     case PROP_TOP:
878       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
879           &video_crop->prop_top);
880       break;
881     case PROP_BOTTOM:
882       gst_video_crop_set_crop (video_crop, g_value_get_int (value),
883           &video_crop->prop_bottom);
884       break;
885     default:
886       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
887       break;
888   }
889   GST_LOG_OBJECT (video_crop, "l=%d,r=%d,b=%d,t=%d, need_update:%d",
890       video_crop->prop_left, video_crop->prop_right, video_crop->prop_bottom,
891       video_crop->prop_top, video_crop->need_update);
892
893   GST_OBJECT_UNLOCK (video_crop);
894
895   gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (video_crop));
896 }
897
898 static void
899 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
900     GParamSpec * pspec)
901 {
902   GstVideoCrop *video_crop;
903
904   video_crop = GST_VIDEO_CROP (object);
905
906   GST_OBJECT_LOCK (video_crop);
907   switch (prop_id) {
908     case PROP_LEFT:
909       g_value_set_int (value, video_crop->prop_left);
910       break;
911     case PROP_RIGHT:
912       g_value_set_int (value, video_crop->prop_right);
913       break;
914     case PROP_TOP:
915       g_value_set_int (value, video_crop->prop_top);
916       break;
917     case PROP_BOTTOM:
918       g_value_set_int (value, video_crop->prop_bottom);
919       break;
920     default:
921       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
922       break;
923   }
924   GST_OBJECT_UNLOCK (video_crop);
925 }
926
927 static gboolean
928 plugin_init (GstPlugin * plugin)
929 {
930   GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
931
932   if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
933           GST_TYPE_VIDEO_CROP)
934       && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
935           GST_TYPE_ASPECT_RATIO_CROP))
936     return TRUE;
937
938   return FALSE;
939 }
940
941 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
942     GST_VERSION_MINOR,
943     videocrop,
944     "Crops video into a user-defined region",
945     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)