1 /* GStreamer video frame cropping
2 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
21 * SECTION:element-videocrop
22 * @see_also: #GstVideoBox
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
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.
34 * If there is nothing to crop, the element will operate in pass-through mode.
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.
43 * <title>Example launch line</title>
45 * gst-launch-1.0 -v videotestsrc ! videocrop top=42 left=1 right=4 bottom=0 ! ximagesink
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
61 #include <gst/video/video.h>
63 #include "gstvideocrop.h"
64 #include "gstaspectratiocrop.h"
68 GST_DEBUG_CATEGORY_STATIC (videocrop_debug);
69 #define GST_CAT_DEFAULT videocrop_debug
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 }")
87 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
90 GST_STATIC_CAPS (VIDEO_CROP_CAPS)
93 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
96 GST_STATIC_CAPS (VIDEO_CROP_CAPS)
99 #define gst_video_crop_parent_class parent_class
100 G_DEFINE_TYPE (GstVideoCrop, gst_video_crop, GST_TYPE_VIDEO_FILTER);
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);
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,
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);
118 gst_video_crop_src_event (GstBaseTransform * trans, GstEvent * event)
121 GstStructure *new_structure;
122 const GstStructure *structure;
123 const gchar *event_name;
127 GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
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");
137 (strcmp (event_name, "mouse-move") == 0 ||
138 strcmp (event_name, "mouse-button-press") == 0 ||
139 strcmp (event_name, "mouse-button-release") == 0)) {
141 if (gst_structure_get_double (structure, "pointer_x", &pointer_x) &&
142 gst_structure_get_double (structure, "pointer_y", &pointer_y)) {
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),
150 new_event = gst_event_new_navigation (new_structure);
151 gst_event_unref (event);
153 GST_WARNING_OBJECT (vcrop, "Failed to read navigation event");
158 GST_OBJECT_UNLOCK (vcrop);
160 return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans,
161 (new_event ? new_event : event));
165 gst_video_crop_class_init (GstVideoCropClass * klass)
167 GObjectClass *gobject_class;
168 GstElementClass *element_class;
169 GstBaseTransformClass *basetransform_class;
170 GstVideoFilterClass *vfilter_class;
172 gobject_class = (GObjectClass *) klass;
173 element_class = (GstElementClass *) klass;
174 basetransform_class = (GstBaseTransformClass *) klass;
175 vfilter_class = (GstVideoFilterClass *) klass;
177 gobject_class->set_property = gst_video_crop_set_property;
178 gobject_class->get_property = gst_video_crop_get_property;
180 g_object_class_install_property (gobject_class, PROP_LEFT,
181 g_param_spec_int ("left", "Left",
182 "Pixels to crop at left (-1 to auto-crop)", -1, G_MAXINT, 0,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_class, PROP_RIGHT,
185 g_param_spec_int ("right", "Right",
186 "Pixels to crop at right (-1 to auto-crop)", -1, G_MAXINT, 0,
187 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 g_object_class_install_property (gobject_class, PROP_TOP,
189 g_param_spec_int ("top", "Top",
190 "Pixels to crop at top (-1 to auto-crop)", -1, G_MAXINT, 0,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_class, PROP_BOTTOM,
193 g_param_spec_int ("bottom", "Bottom",
194 "Pixels to crop at bottom (-1 to auto-crop)", -1, G_MAXINT, 0,
195 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
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>");
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);
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);
216 gst_video_crop_init (GstVideoCrop * vcrop)
218 vcrop->crop_right = 0;
219 vcrop->crop_left = 0;
221 vcrop->crop_bottom = 0;
224 #define ROUND_DOWN_2(n) ((n)&(~1))
227 gst_video_crop_transform_packed_complex (GstVideoCrop * vcrop,
228 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
230 guint8 *in_data, *out_data;
236 width = GST_VIDEO_FRAME_WIDTH (out_frame);
237 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
239 in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
240 out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
242 in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
243 out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
245 in_data += vcrop->crop_top * in_stride;
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);
252 dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
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) {
260 memcpy (out_data, in_data, dx);
262 /* move just the Y samples one pixel to the left, don't worry about
264 for (j = vcrop->macro_y_off; j < out_stride - 2; j += 2)
265 out_data[j] = in_data[j + 2];
267 in_data += in_stride;
268 out_data += out_stride;
271 for (i = 0; i < height; ++i) {
272 memcpy (out_data, in_data, dx);
273 in_data += in_stride;
274 out_data += out_stride;
280 gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
281 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
283 guint8 *in_data, *out_data;
286 gint in_stride, out_stride;
288 width = GST_VIDEO_FRAME_WIDTH (out_frame);
289 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
291 in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
292 out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
294 in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
295 out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
297 in_data += vcrop->crop_top * in_stride;
298 in_data += vcrop->crop_left * GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, 0);
300 dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
302 for (i = 0; i < height; ++i) {
303 memcpy (out_data, in_data, dx);
304 in_data += in_stride;
305 out_data += out_stride;
310 gst_video_crop_transform_planar (GstVideoCrop * vcrop,
311 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
314 guint8 *y_out, *u_out, *v_out;
315 guint8 *y_in, *u_in, *v_in;
318 width = GST_VIDEO_FRAME_WIDTH (out_frame);
319 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
322 y_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
323 y_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
326 (vcrop->crop_top * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame,
327 0)) + vcrop->crop_left;
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);
337 u_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 1);
338 u_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 1);
340 u_in += (vcrop->crop_top / 2) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 1);
341 u_in += vcrop->crop_left / 2;
343 v_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 2);
344 v_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 2);
346 v_in += (vcrop->crop_top / 2) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 2);
347 v_in += vcrop->crop_left / 2;
349 dx = GST_ROUND_UP_2 (width) / 2;
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);
362 gst_video_crop_transform_semi_planar (GstVideoCrop * vcrop,
363 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
366 guint8 *y_out, *uv_out;
367 guint8 *y_in, *uv_in;
370 width = GST_VIDEO_FRAME_WIDTH (out_frame);
371 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
374 y_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
375 y_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
378 uv_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 1);
379 uv_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 1);
381 y_in += vcrop->crop_top * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0) +
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);
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);
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);
403 gst_video_crop_transform_frame (GstVideoFilter * vfilter,
404 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
406 GstVideoCrop *vcrop = GST_VIDEO_CROP (vfilter);
408 if (G_UNLIKELY (vcrop->need_update)) {
409 if (!gst_video_crop_set_info (vfilter, NULL, &vcrop->in_info, NULL,
411 return GST_FLOW_ERROR;
415 switch (vcrop->packing) {
416 case VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE:
417 gst_video_crop_transform_packed_simple (vcrop, in_frame, out_frame);
419 case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
420 gst_video_crop_transform_packed_complex (vcrop, in_frame, out_frame);
422 case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
423 gst_video_crop_transform_planar (vcrop, in_frame, out_frame);
425 case VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR:
426 gst_video_crop_transform_semi_planar (vcrop, in_frame, out_frame);
429 g_assert_not_reached ();
436 gst_video_crop_transform_dimension (gint val, gint delta)
438 gint64 new_val = (gint64) val + (gint64) delta;
440 new_val = CLAMP (new_val, 1, G_MAXINT);
442 return (gint) new_val;
446 gst_video_crop_transform_dimension_value (const GValue * src_val,
447 gint delta, GValue * dest_val, GstPadDirection direction, gboolean dynamic)
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);
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);
461 g_value_init (dest_val, GST_TYPE_INT_RANGE);
462 gst_value_set_int_range (dest_val, ival, G_MAXINT);
466 g_value_init (dest_val, G_TYPE_INT);
467 g_value_set_int (dest_val, ival);
469 g_value_init (dest_val, GST_TYPE_INT_RANGE);
470 gst_value_set_int_range (dest_val, 1, ival);
474 g_value_init (dest_val, G_TYPE_INT);
475 g_value_set_int (dest_val, ival);
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);
481 min = gst_video_crop_transform_dimension (min, delta);
482 max = gst_video_crop_transform_dimension (max, delta);
485 if (direction == GST_PAD_SRC)
492 g_value_init (dest_val, G_TYPE_INT);
493 g_value_set_int (dest_val, min);
495 g_value_init (dest_val, GST_TYPE_INT_RANGE);
496 gst_value_set_int_range (dest_val, min, max);
498 } else if (GST_VALUE_HOLDS_LIST (src_val)) {
501 g_value_init (dest_val, GST_TYPE_LIST);
503 for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
504 const GValue *list_val;
505 GValue newval = { 0, };
507 list_val = gst_value_list_get_value (src_val, i);
508 if (gst_video_crop_transform_dimension_value (list_val, delta, &newval,
510 gst_value_list_append_value (dest_val, &newval);
511 g_value_unset (&newval);
514 if (gst_value_list_get_size (dest_val) == 0) {
515 g_value_unset (dest_val);
526 gst_video_crop_transform_caps (GstBaseTransform * trans,
527 GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
531 gint dy, dx, i, left, right, bottom, top;
532 gboolean w_dynamic, h_dynamic;
534 vcrop = GST_VIDEO_CROP (trans);
536 GST_OBJECT_LOCK (vcrop);
538 GST_LOG_OBJECT (vcrop, "l=%d,r=%d,b=%d,t=%d",
539 vcrop->prop_left, vcrop->prop_right, vcrop->prop_bottom, vcrop->prop_top);
541 w_dynamic = (vcrop->prop_left == -1 || vcrop->prop_right == -1);
542 h_dynamic = (vcrop->prop_top == -1 || vcrop->prop_bottom == -1);
544 left = (vcrop->prop_left == -1) ? 0 : vcrop->prop_left;
545 right = (vcrop->prop_right == -1) ? 0 : vcrop->prop_right;
546 bottom = (vcrop->prop_bottom == -1) ? 0 : vcrop->prop_bottom;
547 top = (vcrop->prop_top == -1) ? 0 : vcrop->prop_top;
549 GST_OBJECT_UNLOCK (vcrop);
551 if (direction == GST_PAD_SRC) {
555 dx = 0 - (left + right);
556 dy = 0 - (top + bottom);
559 GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
561 other_caps = gst_caps_new_empty ();
563 for (i = 0; i < gst_caps_get_size (caps); ++i) {
565 GstStructure *structure, *new_structure;
566 GValue w_val = { 0, }, h_val = {
569 structure = gst_caps_get_structure (caps, i);
571 v = gst_structure_get_value (structure, "width");
572 if (!gst_video_crop_transform_dimension_value (v, dx, &w_val, direction,
574 GST_WARNING_OBJECT (vcrop, "could not tranform width value with dx=%d"
575 ", caps structure=%" GST_PTR_FORMAT, dx, structure);
579 v = gst_structure_get_value (structure, "height");
580 if (!gst_video_crop_transform_dimension_value (v, dy, &h_val, direction,
582 g_value_unset (&w_val);
583 GST_WARNING_OBJECT (vcrop, "could not tranform height value with dy=%d"
584 ", caps structure=%" GST_PTR_FORMAT, dy, structure);
588 new_structure = gst_structure_copy (structure);
589 gst_structure_set_value (new_structure, "width", &w_val);
590 gst_structure_set_value (new_structure, "height", &h_val);
591 g_value_unset (&w_val);
592 g_value_unset (&h_val);
593 GST_LOG_OBJECT (vcrop, "transformed structure %2d: %" GST_PTR_FORMAT
594 " => %" GST_PTR_FORMAT, i, structure, new_structure);
595 gst_caps_append_structure (other_caps, new_structure);
598 if (!gst_caps_is_empty (other_caps) && filter_caps) {
599 GstCaps *tmp = gst_caps_intersect_full (filter_caps, other_caps,
600 GST_CAPS_INTERSECT_FIRST);
601 gst_caps_replace (&other_caps, tmp);
602 gst_caps_unref (tmp);
609 gst_video_crop_set_info (GstVideoFilter * vfilter, GstCaps * in,
610 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
612 GstVideoCrop *crop = GST_VIDEO_CROP (vfilter);
615 GST_OBJECT_LOCK (crop);
616 crop->need_update = FALSE;
617 crop->crop_left = crop->prop_left;
618 crop->crop_right = crop->prop_right;
619 crop->crop_top = crop->prop_top;
620 crop->crop_bottom = crop->prop_bottom;
621 GST_OBJECT_UNLOCK (crop);
623 dx = GST_VIDEO_INFO_WIDTH (in_info) - GST_VIDEO_INFO_WIDTH (out_info);
624 dy = GST_VIDEO_INFO_HEIGHT (in_info) - GST_VIDEO_INFO_HEIGHT (out_info);
626 if (crop->crop_left == -1 && crop->crop_right == -1) {
627 crop->crop_left = dx / 2;
628 crop->crop_right = dx / 2 + (dx & 1);
629 } else if (crop->crop_left == -1) {
630 if (G_UNLIKELY (crop->crop_right > dx))
631 goto cropping_too_much;
632 crop->crop_left = dx - crop->crop_right;
633 } else if (crop->crop_right == -1) {
634 if (G_UNLIKELY (crop->crop_left > dx))
635 goto cropping_too_much;
636 crop->crop_right = dx - crop->crop_left;
639 if (crop->crop_top == -1 && crop->crop_bottom == -1) {
640 crop->crop_top = dy / 2;
641 crop->crop_bottom = dy / 2 + (dy & 1);
642 } else if (crop->crop_top == -1) {
643 if (G_UNLIKELY (crop->crop_bottom > dy))
644 goto cropping_too_much;
645 crop->crop_top = dy - crop->crop_bottom;
646 } else if (crop->crop_bottom == -1) {
647 if (G_UNLIKELY (crop->crop_top > dy))
648 goto cropping_too_much;
649 crop->crop_bottom = dy - crop->crop_top;
652 if (G_UNLIKELY ((crop->crop_left + crop->crop_right) >=
653 GST_VIDEO_INFO_WIDTH (in_info)
654 || (crop->crop_top + crop->crop_bottom) >=
655 GST_VIDEO_INFO_HEIGHT (in_info)))
656 goto cropping_too_much;
659 GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
660 GST_PTR_FORMAT, in, out);
662 if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
664 GST_LOG_OBJECT (crop, "we are using passthrough");
665 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
667 GST_LOG_OBJECT (crop, "we are not using passthrough");
668 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
671 if (GST_VIDEO_INFO_IS_RGB (in_info)
672 || GST_VIDEO_INFO_IS_GRAY (in_info)) {
673 crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
675 switch (GST_VIDEO_INFO_FORMAT (in_info)) {
676 case GST_VIDEO_FORMAT_AYUV:
677 crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
679 case GST_VIDEO_FORMAT_YVYU:
680 case GST_VIDEO_FORMAT_YUY2:
681 case GST_VIDEO_FORMAT_UYVY:
682 crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX;
683 if (GST_VIDEO_INFO_FORMAT (in_info) == GST_VIDEO_FORMAT_UYVY) {
684 /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5] */
685 crop->macro_y_off = 1;
687 /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
688 crop->macro_y_off = 0;
691 case GST_VIDEO_FORMAT_I420:
692 case GST_VIDEO_FORMAT_YV12:
693 crop->packing = VIDEO_CROP_PIXEL_FORMAT_PLANAR;
695 case GST_VIDEO_FORMAT_NV12:
696 case GST_VIDEO_FORMAT_NV21:
697 crop->packing = VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR;
704 crop->in_info = *in_info;
705 crop->out_info = *out_info;
712 GST_WARNING_OBJECT (crop, "we are cropping too much");
717 GST_WARNING_OBJECT (crop, "Unsupported format");
722 /* called with object lock */
724 gst_video_crop_set_crop (GstVideoCrop * vcrop, gint new_value, gint * prop)
726 if (*prop != new_value) {
728 vcrop->need_update = TRUE;
733 gst_video_crop_set_property (GObject * object, guint prop_id,
734 const GValue * value, GParamSpec * pspec)
736 GstVideoCrop *video_crop;
738 video_crop = GST_VIDEO_CROP (object);
740 GST_OBJECT_LOCK (video_crop);
743 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
744 &video_crop->prop_left);
747 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
748 &video_crop->prop_right);
751 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
752 &video_crop->prop_top);
755 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
756 &video_crop->prop_bottom);
759 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
762 GST_LOG_OBJECT (video_crop, "l=%d,r=%d,b=%d,t=%d, need_update:%d",
763 video_crop->prop_left, video_crop->prop_right, video_crop->prop_bottom,
764 video_crop->prop_top, video_crop->need_update);
766 GST_OBJECT_UNLOCK (video_crop);
768 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (video_crop));
772 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
775 GstVideoCrop *video_crop;
777 video_crop = GST_VIDEO_CROP (object);
779 GST_OBJECT_LOCK (video_crop);
782 g_value_set_int (value, video_crop->prop_left);
785 g_value_set_int (value, video_crop->prop_right);
788 g_value_set_int (value, video_crop->prop_top);
791 g_value_set_int (value, video_crop->prop_bottom);
794 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
797 GST_OBJECT_UNLOCK (video_crop);
801 plugin_init (GstPlugin * plugin)
803 GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
805 if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
807 && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
808 GST_TYPE_ASPECT_RATIO_CROP))
814 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
817 "Crops video into a user-defined region",
818 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)