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
23 * @see_also: #GstVideoBox
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
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.
35 * If there is nothing to crop, the element will operate in pass-through mode.
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.
43 * ## Example launch line
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, Y444, " \
84 "Y42B, Y41B, YVYU, UYVY, I420, YV12, RGB16, RGB15, " \
85 "GRAY8, 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 void gst_video_crop_before_transform (GstBaseTransform * trans,
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,
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);
119 static gboolean gst_video_crop_decide_allocation (GstBaseTransform * trans,
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,
127 gst_video_crop_src_event (GstBaseTransform * trans, GstEvent * event)
130 GstStructure *new_structure;
131 const GstStructure *structure;
132 const gchar *event_name;
136 GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
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");
146 (strcmp (event_name, "mouse-move") == 0 ||
147 strcmp (event_name, "mouse-button-press") == 0 ||
148 strcmp (event_name, "mouse-button-release") == 0)) {
150 if (gst_structure_get_double (structure, "pointer_x", &pointer_x) &&
151 gst_structure_get_double (structure, "pointer_y", &pointer_y)) {
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),
159 new_event = gst_event_new_navigation (new_structure);
160 gst_event_unref (event);
162 GST_WARNING_OBJECT (vcrop, "Failed to read navigation event");
167 GST_OBJECT_UNLOCK (vcrop);
169 return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans,
170 (new_event ? new_event : event));
174 gst_video_crop_class_init (GstVideoCropClass * klass)
176 GObjectClass *gobject_class;
177 GstElementClass *element_class;
178 GstBaseTransformClass *basetransform_class;
179 GstVideoFilterClass *vfilter_class;
181 gobject_class = (GObjectClass *) klass;
182 element_class = (GstElementClass *) klass;
183 basetransform_class = (GstBaseTransformClass *) klass;
184 vfilter_class = (GstVideoFilterClass *) klass;
186 gobject_class->set_property = gst_video_crop_set_property;
187 gobject_class->get_property = gst_video_crop_get_property;
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)",
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));
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>");
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);
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);
236 gst_video_crop_init (GstVideoCrop * vcrop)
238 vcrop->crop_right = 0;
239 vcrop->crop_left = 0;
241 vcrop->crop_bottom = 0;
244 #define ROUND_DOWN_2(n) ((n)&(~1))
247 gst_video_crop_transform_packed_complex (GstVideoCrop * vcrop,
248 GstVideoFrame * in_frame, GstVideoFrame * out_frame, gint x, gint y)
250 guint8 *in_data, *out_data;
256 width = GST_VIDEO_FRAME_WIDTH (out_frame);
257 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
259 in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
260 out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
262 in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
263 out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
265 in_data += vcrop->crop_top * in_stride;
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);
272 dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
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) {
280 memcpy (out_data, in_data, dx);
282 /* move just the Y samples one pixel to the left, don't worry about
284 for (j = vcrop->macro_y_off; j < out_stride - 2; j += 2)
285 out_data[j] = in_data[j + 2];
287 in_data += in_stride;
288 out_data += out_stride;
291 for (i = 0; i < height; ++i) {
292 memcpy (out_data, in_data, dx);
293 in_data += in_stride;
294 out_data += out_stride;
300 gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
301 GstVideoFrame * in_frame, GstVideoFrame * out_frame, gint x, gint y)
303 guint8 *in_data, *out_data;
306 gint in_stride, out_stride;
308 width = GST_VIDEO_FRAME_WIDTH (out_frame);
309 height = GST_VIDEO_FRAME_HEIGHT (out_frame);
311 in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
312 out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
314 in_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
315 out_stride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
317 in_data += (vcrop->crop_top + y) * in_stride;
319 (vcrop->crop_left + x) * GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, 0);
321 dx = width * GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, 0);
323 for (i = 0; i < height; ++i) {
324 memcpy (out_data, in_data, dx);
325 in_data += in_stride;
326 out_data += out_stride;
331 gst_video_crop_transform_planar (GstVideoCrop * vcrop,
332 GstVideoFrame * in_frame, GstVideoFrame * out_frame, gint x, gint y)
334 const GstVideoFormatInfo *format_info;
335 gint crop_top, crop_left;
338 format_info = in_frame->info.finfo;
339 crop_left = vcrop->crop_left + x;
340 crop_top = vcrop->crop_top + y;
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;
350 plane_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, p);
351 plane_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, p);
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);
364 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (format_info, p,
365 subsampled_crop_top) * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, p);
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);
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);
380 gst_video_crop_transform_semi_planar (GstVideoCrop * vcrop,
381 GstVideoFrame * in_frame, GstVideoFrame * out_frame, gint x, gint y)
384 gint crop_top, crop_left;
385 guint8 *y_out, *uv_out;
386 guint8 *y_in, *uv_in;
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;
395 y_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
396 y_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
399 uv_in = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 1);
400 uv_out = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 1);
402 y_in += crop_top * GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0) + crop_left;
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);
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);
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);
423 gst_video_crop_transform_frame (GstVideoFilter * vfilter,
424 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
426 GstVideoCrop *vcrop = GST_VIDEO_CROP (vfilter);
427 GstVideoCropMeta *meta = gst_buffer_get_video_crop_meta (in_frame->buffer);
430 if (G_UNLIKELY (vcrop->need_update)) {
431 if (!gst_video_crop_set_info (vfilter, NULL, &vcrop->in_info, NULL,
433 return GST_FLOW_ERROR;
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);
446 case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
447 gst_video_crop_transform_packed_complex (vcrop, in_frame, out_frame, x,
450 case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
451 gst_video_crop_transform_planar (vcrop, in_frame, out_frame, x, y);
453 case VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR:
454 gst_video_crop_transform_semi_planar (vcrop, in_frame, out_frame, x, y);
457 g_assert_not_reached ();
464 gst_video_crop_decide_allocation (GstBaseTransform * trans, GstQuery * query)
466 GstVideoCrop *crop = GST_VIDEO_CROP (trans);
467 gboolean use_crop_meta;
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));
473 if ((crop->crop_left | crop->crop_right | crop->crop_top | crop->
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);
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);
488 return GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
493 gst_video_crop_propose_allocation (GstBaseTransform * trans,
494 GstQuery * decide_query, GstQuery * query)
496 /* if we are not passthrough, we can handle video meta and crop meta */
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);
503 return GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
504 decide_query, query);
508 gst_video_crop_before_transform (GstBaseTransform * trans, GstBuffer * in)
510 GstVideoCrop *video_crop = GST_VIDEO_CROP (trans);
511 GstClockTime timestamp, stream_time;
513 timestamp = GST_BUFFER_TIMESTAMP (in);
515 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
517 GST_DEBUG_OBJECT (video_crop, "sync to %" GST_TIME_FORMAT,
518 GST_TIME_ARGS (timestamp));
520 if (GST_CLOCK_TIME_IS_VALID (stream_time))
521 gst_object_sync_values (GST_OBJECT (video_crop), stream_time);
525 gst_video_crop_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
527 GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
528 GstVideoFilter *vfilter = GST_VIDEO_FILTER (trans);
529 GstVideoMeta *video_meta;
530 GstVideoCropMeta *crop_meta;
532 GST_LOG_OBJECT (trans, "Transforming in-place");
534 if (G_UNLIKELY (vcrop->need_update)) {
535 if (!gst_video_crop_set_info (vfilter, NULL, &vcrop->in_info, NULL,
537 return GST_FLOW_ERROR;
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);
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);
551 crop_meta = gst_buffer_get_video_crop_meta (buf);
553 crop_meta = gst_buffer_add_video_crop_meta (buf);
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);
564 gst_video_crop_transform_dimension (gint val, gint delta)
566 gint64 new_val = (gint64) val + (gint64) delta;
568 new_val = CLAMP (new_val, 1, G_MAXINT);
570 return (gint) new_val;
574 gst_video_crop_transform_dimension_value (const GValue * src_val,
575 gint delta, GValue * dest_val, GstPadDirection direction, gboolean dynamic)
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);
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);
589 g_value_init (dest_val, GST_TYPE_INT_RANGE);
590 gst_value_set_int_range (dest_val, ival, G_MAXINT);
594 g_value_init (dest_val, G_TYPE_INT);
595 g_value_set_int (dest_val, ival);
597 g_value_init (dest_val, GST_TYPE_INT_RANGE);
598 gst_value_set_int_range (dest_val, 1, ival);
602 g_value_init (dest_val, G_TYPE_INT);
603 g_value_set_int (dest_val, ival);
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);
609 min = gst_video_crop_transform_dimension (min, delta);
610 max = gst_video_crop_transform_dimension (max, delta);
613 if (direction == GST_PAD_SRC)
620 g_value_init (dest_val, G_TYPE_INT);
621 g_value_set_int (dest_val, min);
623 g_value_init (dest_val, GST_TYPE_INT_RANGE);
624 gst_value_set_int_range (dest_val, min, max);
626 } else if (GST_VALUE_HOLDS_LIST (src_val)) {
629 g_value_init (dest_val, GST_TYPE_LIST);
631 for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
632 const GValue *list_val;
633 GValue newval = G_VALUE_INIT;
635 list_val = gst_value_list_get_value (src_val, i);
636 if (gst_video_crop_transform_dimension_value (list_val, delta, &newval,
638 gst_value_list_append_value (dest_val, &newval);
639 g_value_unset (&newval);
642 if (gst_value_list_get_size (dest_val) == 0) {
643 g_value_unset (dest_val);
654 gst_video_crop_transform_caps (GstBaseTransform * trans,
655 GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
659 gint dy, dx, i, left, right, bottom, top;
660 gboolean w_dynamic, h_dynamic;
662 vcrop = GST_VIDEO_CROP (trans);
664 GST_OBJECT_LOCK (vcrop);
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);
669 w_dynamic = (vcrop->prop_left == -1 || vcrop->prop_right == -1);
670 h_dynamic = (vcrop->prop_top == -1 || vcrop->prop_bottom == -1);
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;
677 GST_OBJECT_UNLOCK (vcrop);
679 if (direction == GST_PAD_SRC) {
683 dx = 0 - (left + right);
684 dy = 0 - (top + bottom);
687 GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
689 other_caps = gst_caps_new_empty ();
691 for (i = 0; i < gst_caps_get_size (caps); ++i) {
693 GstStructure *structure, *new_structure;
694 GValue w_val = G_VALUE_INIT, h_val = G_VALUE_INIT;
696 structure = gst_caps_get_structure (caps, i);
698 v = gst_structure_get_value (structure, "width");
699 if (!gst_video_crop_transform_dimension_value (v, dx, &w_val, direction,
701 GST_WARNING_OBJECT (vcrop, "could not transform width value with dx=%d"
702 ", caps structure=%" GST_PTR_FORMAT, dx, structure);
706 v = gst_structure_get_value (structure, "height");
707 if (!gst_video_crop_transform_dimension_value (v, dy, &h_val, direction,
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);
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);
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);
736 gst_video_crop_set_info (GstVideoFilter * vfilter, GstCaps * in,
737 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
739 GstVideoCrop *crop = GST_VIDEO_CROP (vfilter);
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);
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);
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;
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;
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;
786 GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
787 GST_PTR_FORMAT, in, out);
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;
793 switch (GST_VIDEO_INFO_FORMAT (in_info)) {
794 case GST_VIDEO_FORMAT_AYUV:
795 crop->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
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;
805 /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
806 crop->macro_y_off = 0;
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;
816 case GST_VIDEO_FORMAT_NV12:
817 case GST_VIDEO_FORMAT_NV21:
818 crop->packing = VIDEO_CROP_PIXEL_FORMAT_SEMI_PLANAR;
825 crop->in_info = *in_info;
826 crop->out_info = *out_info;
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);
839 GST_WARNING_OBJECT (crop, "we are cropping too much");
844 GST_WARNING_OBJECT (crop, "Unsupported format");
849 /* called with object lock */
851 gst_video_crop_set_crop (GstVideoCrop * vcrop, gint new_value, gint * prop)
853 if (*prop != new_value) {
855 vcrop->need_update = TRUE;
860 gst_video_crop_set_property (GObject * object, guint prop_id,
861 const GValue * value, GParamSpec * pspec)
863 GstVideoCrop *video_crop;
865 video_crop = GST_VIDEO_CROP (object);
867 GST_OBJECT_LOCK (video_crop);
870 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
871 &video_crop->prop_left);
874 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
875 &video_crop->prop_right);
878 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
879 &video_crop->prop_top);
882 gst_video_crop_set_crop (video_crop, g_value_get_int (value),
883 &video_crop->prop_bottom);
886 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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);
893 GST_OBJECT_UNLOCK (video_crop);
895 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (video_crop));
899 gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
902 GstVideoCrop *video_crop;
904 video_crop = GST_VIDEO_CROP (object);
906 GST_OBJECT_LOCK (video_crop);
909 g_value_set_int (value, video_crop->prop_left);
912 g_value_set_int (value, video_crop->prop_right);
915 g_value_set_int (value, video_crop->prop_top);
918 g_value_set_int (value, video_crop->prop_bottom);
921 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
924 GST_OBJECT_UNLOCK (video_crop);
928 plugin_init (GstPlugin * plugin)
930 GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");
932 if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
934 && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
935 GST_TYPE_ASPECT_RATIO_CROP))
941 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
944 "Crops video into a user-defined region",
945 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)