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 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)",
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));
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>");
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);
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);
218 gst_video_crop_init (GstVideoCrop * vcrop)
220 vcrop->crop_right = 0;
221 vcrop->crop_left = 0;
223 vcrop->crop_bottom = 0;
226 #define ROUND_DOWN_2(n) ((n)&(~1))
229 gst_video_crop_transform_packed_complex (GstVideoCrop * vcrop,
230 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
232 guint8 *in_data, *out_data;
238 width = GST_VIDEO_FRAME_WIDTH (out_frame);
239 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
241 in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
242 out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
244 in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
245 out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
247 in_data += vcrop->crop_top * in_stride;
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);
254 dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
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) {
262 memcpy (out_data, in_data, dx);
264 /* move just the Y samples one pixel to the left, don't worry about
266 for (j = vcrop->macro_y_off; j < out_stride - 2; j += 2)
267 out_data[j] = in_data[j + 2];
269 in_data += in_stride;
270 out_data += out_stride;
273 for (i = 0; i < height; ++i) {
274 memcpy (out_data, in_data, dx);
275 in_data += in_stride;
276 out_data += out_stride;
282 gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
283 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
285 guint8 *in_data, *out_data;
288 gint in_stride, out_stride;
290 width = GST_VIDEO_FRAME_WIDTH (out_frame);
291 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
293 in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
294 out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
296 in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
297 out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
299 in_data += vcrop->crop_top * in_stride;
300 in_data += vcrop->crop_left * GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, 0);
302 dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
304 for (i = 0; i < height; ++i) {
305 memcpy (out_data, in_data, dx);
306 in_data += in_stride;
307 out_data += out_stride;
312 gst_video_crop_transform_planar (GstVideoCrop * vcrop,
313 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
316 guint8 *y_out, *u_out, *v_out;
317 guint8 *y_in, *u_in, *v_in;
320 width = GST_VIDEO_FRAME_WIDTH (out_frame);
321 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
324 y_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
325 y_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
328 (vcrop->crop_top * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame,
329 0)) + vcrop->crop_left;
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);
339 u_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 1);
340 u_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 1);
342 u_in += (vcrop->crop_top / 2) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 1);
343 u_in += vcrop->crop_left / 2;
345 v_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 2);
346 v_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 2);
348 v_in += (vcrop->crop_top / 2) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 2);
349 v_in += vcrop->crop_left / 2;
351 dx = GST_ROUND_UP_2 (width) / 2;
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);
364 gst_video_crop_transform_semi_planar (GstVideoCrop * vcrop,
365 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
368 guint8 *y_out, *uv_out;
369 guint8 *y_in, *uv_in;
372 width = GST_VIDEO_FRAME_WIDTH (out_frame);
373 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
376 y_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
377 y_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
380 uv_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 1);
381 uv_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 1);
383 y_in += vcrop->crop_top * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0) +
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);
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);
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);
405 gst_video_crop_transform_frame (GstVideoFilter * vfilter,
406 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
408 GstVideoCrop *vcrop = GST_VIDEO_CROP (vfilter);
410 if (G_UNLIKELY (vcrop->need_update)) {
411 if (!gst_video_crop_set_info (vfilter, NULL, &vcrop->in_info, NULL,
413 return GST_FLOW_ERROR;
417 switch (vcrop->packing) {
418 case VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE:
419 gst_video_crop_transform_packed_simple (vcrop, in_frame, out_frame);
421 case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
422 gst_video_crop_transform_packed_complex (vcrop, in_frame, out_frame);
424 case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
425 gst_video_crop_transform_planar (vcrop, in_frame, out_frame);
427 case VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR:
428 gst_video_crop_transform_semi_planar (vcrop, in_frame, out_frame);
431 g_assert_not_reached ();
438 gst_video_crop_transform_dimension (gint val, gint delta)
440 gint64 new_val = (gint64) val + (gint64) delta;
442 new_val = CLAMP (new_val, 1, G_MAXINT);
444 return (gint) new_val;
448 gst_video_crop_transform_dimension_value (const GValue * src_val,
449 gint delta, GValue * dest_val, GstPadDirection direction, gboolean dynamic)
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);
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);
463 g_value_init (dest_val, GST_TYPE_INT_RANGE);
464 gst_value_set_int_range (dest_val, ival, G_MAXINT);
468 g_value_init (dest_val, G_TYPE_INT);
469 g_value_set_int (dest_val, ival);
471 g_value_init (dest_val, GST_TYPE_INT_RANGE);
472 gst_value_set_int_range (dest_val, 1, ival);
476 g_value_init (dest_val, G_TYPE_INT);
477 g_value_set_int (dest_val, ival);
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);
483 min = gst_video_crop_transform_dimension (min, delta);
484 max = gst_video_crop_transform_dimension (max, delta);
487 if (direction == GST_PAD_SRC)
494 g_value_init (dest_val, G_TYPE_INT);
495 g_value_set_int (dest_val, min);
497 g_value_init (dest_val, GST_TYPE_INT_RANGE);
498 gst_value_set_int_range (dest_val, min, max);
500 } else if (GST_VALUE_HOLDS_LIST (src_val)) {
503 g_value_init (dest_val, GST_TYPE_LIST);
505 for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
506 const GValue *list_val;
507 GValue newval = { 0, };
509 list_val = gst_value_list_get_value (src_val, i);
510 if (gst_video_crop_transform_dimension_value (list_val, delta, &newval,
512 gst_value_list_append_value (dest_val, &newval);
513 g_value_unset (&newval);
516 if (gst_value_list_get_size (dest_val) == 0) {
517 g_value_unset (dest_val);
528 gst_video_crop_transform_caps (GstBaseTransform * trans,
529 GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
533 gint dy, dx, i, left, right, bottom, top;
534 gboolean w_dynamic, h_dynamic;
536 vcrop = GST_VIDEO_CROP (trans);
538 GST_OBJECT_LOCK (vcrop);
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);
543 w_dynamic = (vcrop->prop_left == -1 || vcrop->prop_right == -1);
544 h_dynamic = (vcrop->prop_top == -1 || vcrop->prop_bottom == -1);
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;
551 GST_OBJECT_UNLOCK (vcrop);
553 if (direction == GST_PAD_SRC) {
557 dx = 0 - (left + right);
558 dy = 0 - (top + bottom);
561 GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
563 other_caps = gst_caps_new_empty ();
565 for (i = 0; i < gst_caps_get_size (caps); ++i) {
567 GstStructure *structure, *new_structure;
568 GValue w_val = { 0, }, h_val = {
571 structure = gst_caps_get_structure (caps, i);
573 v = gst_structure_get_value (structure, "width");
574 if (!gst_video_crop_transform_dimension_value (v, dx, &w_val, direction,
576 GST_WARNING_OBJECT (vcrop, "could not tranform width value with dx=%d"
577 ", caps structure=%" GST_PTR_FORMAT, dx, structure);
581 v = gst_structure_get_value (structure, "height");
582 if (!gst_video_crop_transform_dimension_value (v, dy, &h_val, direction,
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);
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);
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);
611 gst_video_crop_set_info (GstVideoFilter * vfilter, GstCaps * in,
612 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
614 GstVideoCrop *crop = GST_VIDEO_CROP (vfilter);
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);
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);
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;
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;
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;
661 GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
662 GST_PTR_FORMAT, in, out);
664 if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
666 GST_LOG_OBJECT (crop, "we are using passthrough");
667 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
669 GST_LOG_OBJECT (crop, "we are not using passthrough");
670 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
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;
677 switch (GST_VIDEO_INFO_FORMAT (in_info)) {
678 case GST_VIDEO_FORMAT_AYUV:
679 crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
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;
689 /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
690 crop->macro_y_off = 0;
693 case GST_VIDEO_FORMAT_I420:
694 case GST_VIDEO_FORMAT_YV12:
695 crop->packing = VIDEO_CROP_PIXEL_FORMAT_PLANAR;
697 case GST_VIDEO_FORMAT_NV12:
698 case GST_VIDEO_FORMAT_NV21:
699 crop->packing = VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR;
706 crop->in_info = *in_info;
707 crop->out_info = *out_info;
714 GST_WARNING_OBJECT (crop, "we are cropping too much");
719 GST_WARNING_OBJECT (crop, "Unsupported format");
724 /* called with object lock */
726 gst_video_crop_set_crop (GstVideoCrop * vcrop, gint new_value, gint * prop)
728 if (*prop != new_value) {
730 vcrop->need_update = TRUE;
735 gst_video_crop_set_property (GObject * object, guint prop_id,
736 const GValue * value, GParamSpec * pspec)
738 GstVideoCrop *video_crop;
740 video_crop = GST_VIDEO_CROP (object);
742 GST_OBJECT_LOCK (video_crop);
745 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
746 &video_crop->prop_left);
749 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
750 &video_crop->prop_right);
753 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
754 &video_crop->prop_top);
757 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
758 &video_crop->prop_bottom);
761 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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);
768 GST_OBJECT_UNLOCK (video_crop);
770 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (video_crop));
774 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
777 GstVideoCrop *video_crop;
779 video_crop = GST_VIDEO_CROP (object);
781 GST_OBJECT_LOCK (video_crop);
784 g_value_set_int (value, video_crop->prop_left);
787 g_value_set_int (value, video_crop->prop_right);
790 g_value_set_int (value, video_crop->prop_top);
793 g_value_set_int (value, video_crop->prop_bottom);
796 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
799 GST_OBJECT_UNLOCK (video_crop);
803 plugin_init (GstPlugin * plugin)
805 GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
807 if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
809 && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
810 GST_TYPE_ASPECT_RATIO_CROP))
816 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
819 "Crops video into a user-defined region",
820 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)