1 /* GStreamer video frame cropping to aspect-ratio
2 * Copyright (C) 2009 Thijs Vermeir <thijsvermeir@gmail.com>
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-aspectratiocrop
22 * @see_also: #GstVideoCrop
24 * This element crops video frames to a specified #GstAspectRatioCrop:aspect-ratio.
26 * If the aspect-ratio is already correct, the element will operate
27 * in pass-through mode.
30 * <title>Example launch line</title>
32 * gst-launch -v videotestsrc ! video/x-raw-rgb,height=640,width=480 ! aspectratiocrop aspect-ratio=16/9 ! ximagesink
33 * ]| This pipeline generates a videostream in 4/3 and crops it to 16/9.
42 #include <gst/video/video.h>
44 #include "gstaspectratiocrop.h"
46 GST_DEBUG_CATEGORY_STATIC (aspect_ratio_crop_debug);
47 #define GST_CAT_DEFAULT aspect_ratio_crop_debug
52 ARG_ASPECT_RATIO_CROP,
55 /* we support the same caps as videocrop */
56 #define ASPECT_RATIO_CROP_CAPS \
57 GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, " \
58 "RGBA, ARGB, BGRA, ABGR, RGB, BGR, AYUV, YUY2, " \
59 "YVYU, UYVY, Y800, I420, RGB16, RGB15, GRAY8 }")
61 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
64 GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
67 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
70 GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
73 #define gst_aspect_ratio_crop_parent_class parent_class
74 G_DEFINE_TYPE (GstAspectRatioCrop, gst_aspect_ratio_crop, GST_TYPE_BIN);
76 static void gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
77 const GValue * value, GParamSpec * pspec);
78 static void gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
79 GValue * value, GParamSpec * pspec);
80 static void gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop *
81 aspect_ratio_crop, gint top, gint right, gint bottom, gint left);
82 static GstCaps *gst_aspect_ratio_crop_get_caps (GstPad * pad, GstCaps * filter);
83 static gboolean gst_aspect_ratio_crop_src_query (GstPad * pad,
84 GstObject * parent, GstQuery * query);
85 static gboolean gst_aspect_ratio_crop_set_caps (GstAspectRatioCrop *
86 aspect_ratio_crop, GstCaps * caps);
87 static gboolean gst_aspect_ratio_crop_sink_event (GstPad * pad,
88 GstObject * parent, GstEvent * evt);
89 static void gst_aspect_ratio_crop_finalize (GObject * object);
90 static void gst_aspect_ratio_transform_structure (GstAspectRatioCrop *
91 aspect_ratio_crop, GstStructure * structure, GstStructure ** new_structure,
92 gboolean set_videocrop);
95 gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop,
96 gint top, gint right, gint bottom, gint left)
99 if (G_UNLIKELY (!aspect_ratio_crop->videocrop)) {
100 GST_WARNING_OBJECT (aspect_ratio_crop,
101 "Can't set the settings if there is no cropping element");
105 g_value_init (&value, G_TYPE_INT);
106 g_value_set_int (&value, top);
107 GST_DEBUG_OBJECT (aspect_ratio_crop, "set top cropping to: %d", top);
108 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "top",
110 g_value_set_int (&value, right);
111 GST_DEBUG_OBJECT (aspect_ratio_crop, "set right cropping to: %d", right);
112 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "right",
114 g_value_set_int (&value, bottom);
115 GST_DEBUG_OBJECT (aspect_ratio_crop, "set bottom cropping to: %d", bottom);
116 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "bottom",
118 g_value_set_int (&value, left);
119 GST_DEBUG_OBJECT (aspect_ratio_crop, "set left cropping to: %d", left);
120 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "left",
123 g_value_unset (&value);
127 gst_aspect_ratio_crop_set_caps (GstAspectRatioCrop * aspect_ratio_crop,
131 GstStructure *structure;
134 g_mutex_lock (aspect_ratio_crop->crop_lock);
136 structure = gst_caps_get_structure (caps, 0);
137 gst_aspect_ratio_transform_structure (aspect_ratio_crop, structure, NULL,
140 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
142 ret = gst_pad_set_caps (peer_pad, caps);
143 gst_object_unref (peer_pad);
144 g_mutex_unlock (aspect_ratio_crop->crop_lock);
149 gst_aspect_ratio_crop_sink_event (GstPad * pad, GstObject * parent,
153 GstAspectRatioCrop *aspect_ratio_crop = GST_ASPECT_RATIO_CROP (parent);
156 aspect_ratio_crop->sinkpad_old_eventfunc (pad, parent,
157 gst_event_ref (evt));
159 switch (GST_EVENT_TYPE (evt)) {
164 gst_event_parse_caps (evt, &caps);
165 ret = gst_aspect_ratio_crop_set_caps (aspect_ratio_crop, caps);
166 gst_caps_unref (caps);
172 gst_event_unref (evt);
178 gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass)
180 GObjectClass *gobject_class;
181 GstElementClass *element_class;
183 gobject_class = (GObjectClass *) klass;
184 element_class = (GstElementClass *) klass;
186 gobject_class->set_property = gst_aspect_ratio_crop_set_property;
187 gobject_class->get_property = gst_aspect_ratio_crop_get_property;
188 gobject_class->finalize = gst_aspect_ratio_crop_finalize;
190 g_object_class_install_property (gobject_class, ARG_ASPECT_RATIO_CROP,
191 gst_param_spec_fraction ("aspect-ratio", "aspect-ratio",
192 "Target aspect-ratio of video", 0, 1, G_MAXINT, 1, 0, 1,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 gst_element_class_set_details_simple (element_class, "aspectratiocrop",
196 "Filter/Effect/Video",
197 "Crops video into a user-defined aspect-ratio",
198 "Thijs Vermeir <thijsvermeir@gmail.com>");
200 gst_element_class_add_pad_template (element_class,
201 gst_static_pad_template_get (&sink_template));
202 gst_element_class_add_pad_template (element_class,
203 gst_static_pad_template_get (&src_template));
207 gst_aspect_ratio_crop_finalize (GObject * object)
209 GstAspectRatioCrop *aspect_ratio_crop;
211 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
213 if (aspect_ratio_crop->crop_lock)
214 g_mutex_free (aspect_ratio_crop->crop_lock);
216 G_OBJECT_CLASS (parent_class)->finalize (object);
220 gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop)
225 GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0,
228 aspect_ratio_crop->ar_num = 0;
229 aspect_ratio_crop->ar_denom = 1;
231 aspect_ratio_crop->crop_lock = g_mutex_new ();
233 /* add the transform element */
234 aspect_ratio_crop->videocrop = gst_element_factory_make ("videocrop", NULL);
235 gst_bin_add (GST_BIN (aspect_ratio_crop), aspect_ratio_crop->videocrop);
237 /* create ghost pad src */
239 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
241 src_pad = gst_ghost_pad_new ("src", link_pad);
242 gst_pad_set_query_function (src_pad,
243 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_src_query));
244 gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), src_pad);
245 gst_object_unref (link_pad);
246 /* create ghost pad sink */
248 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
250 aspect_ratio_crop->sink = gst_ghost_pad_new ("sink", link_pad);
251 gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop),
252 aspect_ratio_crop->sink);
253 gst_object_unref (link_pad);
255 aspect_ratio_crop->sinkpad_old_eventfunc =
256 GST_PAD_EVENTFUNC (aspect_ratio_crop->sink);
257 gst_pad_set_event_function (aspect_ratio_crop->sink,
258 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_sink_event));
262 gst_aspect_ratio_transform_structure (GstAspectRatioCrop * aspect_ratio_crop,
263 GstStructure * structure, GstStructure ** new_structure,
264 gboolean set_videocrop)
267 gdouble requested_ar;
272 /* Check if we need to change the aspect ratio */
273 if (aspect_ratio_crop->ar_num < 1) {
274 GST_DEBUG_OBJECT (aspect_ratio_crop, "No cropping requested");
278 /* get the information from the caps */
279 if (!gst_structure_get_int (structure, "width", &width) ||
280 !gst_structure_get_int (structure, "height", &height))
283 if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
288 incoming_ar = ((gdouble) (width * par_n)) / (height * par_d);
289 GST_LOG_OBJECT (aspect_ratio_crop,
290 "incoming caps width(%d), height(%d), par (%d/%d) : ar = %f", width,
291 height, par_n, par_d, incoming_ar);
294 (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom;
296 /* check if the original aspect-ratio is the aspect-ratio that we want */
297 if (requested_ar == incoming_ar) {
298 GST_DEBUG_OBJECT (aspect_ratio_crop,
299 "Input video already has the correct aspect ratio (%.3f == %.3f)",
300 incoming_ar, requested_ar);
302 } else if (requested_ar > incoming_ar) {
303 /* fix aspect ratio with cropping on top and bottom */
305 ((((double) aspect_ratio_crop->ar_denom /
306 (double) (aspect_ratio_crop->ar_num)) * ((double) par_n /
307 (double) par_d) * width) - height) / 2;
311 if (cropvalue >= (height / 2))
314 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0,
318 *new_structure = gst_structure_copy (structure);
319 gst_structure_set (*new_structure,
320 "height", G_TYPE_INT, (int) (height - (cropvalue * 2)), NULL);
323 /* fix aspect ratio with cropping on left and right */
325 ((((double) aspect_ratio_crop->ar_num /
326 (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d /
327 (double) par_n) * height) - width) / 2;
331 if (cropvalue >= (width / 2))
334 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue,
338 *new_structure = gst_structure_copy (structure);
339 gst_structure_set (*new_structure,
340 "width", G_TYPE_INT, (int) (width - (cropvalue * 2)), NULL);
347 GST_WARNING_OBJECT (aspect_ratio_crop,
348 "can't crop to aspect ratio requested");
352 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0);
356 *new_structure = gst_structure_copy (structure);
361 gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop,
367 transform = gst_caps_new_empty ();
369 size = gst_caps_get_size (caps);
371 for (i = 0; i < size; i++) {
373 GstStructure *trans_s;
375 s = gst_caps_get_structure (caps, i);
377 gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s,
379 gst_caps_append_structure (transform, trans_s);
386 gst_aspect_ratio_crop_get_caps (GstPad * pad, GstCaps * filter)
389 GstAspectRatioCrop *aspect_ratio_crop;
390 GstCaps *return_caps;
392 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
394 g_mutex_lock (aspect_ratio_crop->crop_lock);
396 peer = gst_pad_get_peer (aspect_ratio_crop->sink);
398 return_caps = gst_static_pad_template_get_caps (&src_template);
399 gst_caps_ref (return_caps);
403 peer_caps = gst_pad_query_caps (peer, filter);
405 gst_aspect_ratio_crop_transform_caps (aspect_ratio_crop, peer_caps);
406 gst_caps_unref (peer_caps);
407 gst_object_unref (peer);
410 g_mutex_unlock (aspect_ratio_crop->crop_lock);
411 gst_object_unref (aspect_ratio_crop);
413 if (return_caps && filter) {
415 gst_caps_intersect_full (filter, return_caps, GST_CAPS_INTERSECT_FIRST);
416 gst_caps_replace (&return_caps, tmp);
417 gst_caps_unref (tmp);
424 gst_aspect_ratio_crop_src_query (GstPad * pad, GstObject * parent,
427 gboolean res = FALSE;
429 switch (GST_QUERY_TYPE (query)) {
432 GstCaps *filter, *caps;
434 gst_query_parse_caps (query, &filter);
435 caps = gst_aspect_ratio_crop_get_caps (pad, filter);
436 gst_query_set_caps_result (query, caps);
437 gst_caps_unref (caps);
442 res = gst_pad_query_default (pad, parent, query);
449 gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
450 const GValue * value, GParamSpec * pspec)
452 GstAspectRatioCrop *aspect_ratio_crop;
453 gboolean recheck = FALSE;
455 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
457 GST_OBJECT_LOCK (aspect_ratio_crop);
459 case ARG_ASPECT_RATIO_CROP:
460 if (GST_VALUE_HOLDS_FRACTION (value)) {
461 aspect_ratio_crop->ar_num = gst_value_get_fraction_numerator (value);
462 aspect_ratio_crop->ar_denom =
463 gst_value_get_fraction_denominator (value);
464 recheck = gst_pad_has_current_caps (aspect_ratio_crop->sink);
468 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
471 GST_OBJECT_UNLOCK (aspect_ratio_crop);
474 GstCaps *caps = gst_pad_get_current_caps (aspect_ratio_crop->sink);
475 gst_aspect_ratio_crop_set_caps (aspect_ratio_crop, caps);
476 gst_caps_unref (caps);
481 gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
482 GValue * value, GParamSpec * pspec)
484 GstAspectRatioCrop *aspect_ratio_crop;
486 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
488 GST_OBJECT_LOCK (aspect_ratio_crop);
490 case ARG_ASPECT_RATIO_CROP:
491 gst_value_set_fraction (value, aspect_ratio_crop->ar_num,
492 aspect_ratio_crop->ar_denom);
495 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 GST_OBJECT_UNLOCK (aspect_ratio_crop);