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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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-1.0 -v videotestsrc ! video/x-raw,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 #include "gst/glib-compat-private.h"
48 GST_DEBUG_CATEGORY_STATIC (aspect_ratio_crop_debug);
49 #define GST_CAT_DEFAULT aspect_ratio_crop_debug
54 PROP_ASPECT_RATIO_CROP,
57 /* we support the same caps as videocrop (sync changes) */
58 #define ASPECT_RATIO_CROP_CAPS \
59 GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, " \
60 "RGBA, ARGB, BGRA, ABGR, RGB, BGR, AYUV, YUY2, " \
61 "YVYU, UYVY, I420, YV12, RGB16, RGB15, GRAY8, " \
62 "NV12, NV21, GRAY16_LE, GRAY16_BE }")
64 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
67 GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
70 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
76 #define gst_aspect_ratio_crop_parent_class parent_class
77 G_DEFINE_TYPE (GstAspectRatioCrop, gst_aspect_ratio_crop, GST_TYPE_BIN);
79 static void gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
81 static void gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
83 static void gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop *
84 aspect_ratio_crop, gint top, gint right, gint bottom, gint left);
85 static GstCaps *gst_aspect_ratio_crop_get_caps (GstPad * pad, GstCaps * filter);
86 static gboolean gst_aspect_ratio_crop_src_query (GstPad * pad,
87 GstObject * parent, GstQuery * query);
88 static gboolean gst_aspect_ratio_crop_set_caps (GstAspectRatioCrop *
89 aspect_ratio_crop, GstCaps * caps);
90 static gboolean gst_aspect_ratio_crop_sink_event (GstPad * pad,
91 GstObject * parent, GstEvent * evt);
92 static void gst_aspect_ratio_crop_finalize (GObject * object);
93 static void gst_aspect_ratio_transform_structure (GstAspectRatioCrop *
94 aspect_ratio_crop, GstStructure * structure, GstStructure ** new_structure,
95 gboolean set_videocrop);
98 gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop,
99 gint top, gint right, gint bottom, gint left)
101 GValue value = { 0 };
102 if (G_UNLIKELY (!aspect_ratio_crop->videocrop)) {
103 GST_WARNING_OBJECT (aspect_ratio_crop,
104 "Can't set the settings if there is no cropping element");
108 g_value_init (&value, G_TYPE_INT);
109 g_value_set_int (&value, top);
110 GST_DEBUG_OBJECT (aspect_ratio_crop, "set top cropping to: %d", top);
111 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "top",
113 g_value_set_int (&value, right);
114 GST_DEBUG_OBJECT (aspect_ratio_crop, "set right cropping to: %d", right);
115 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "right",
117 g_value_set_int (&value, bottom);
118 GST_DEBUG_OBJECT (aspect_ratio_crop, "set bottom cropping to: %d", bottom);
119 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "bottom",
121 g_value_set_int (&value, left);
122 GST_DEBUG_OBJECT (aspect_ratio_crop, "set left cropping to: %d", left);
123 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "left",
126 g_value_unset (&value);
130 gst_aspect_ratio_crop_set_caps (GstAspectRatioCrop * aspect_ratio_crop,
134 GstStructure *structure;
137 g_mutex_lock (&aspect_ratio_crop->crop_lock);
139 structure = gst_caps_get_structure (caps, 0);
140 gst_aspect_ratio_transform_structure (aspect_ratio_crop, structure, NULL,
143 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
145 ret = gst_pad_set_caps (peer_pad, caps);
146 gst_object_unref (peer_pad);
147 g_mutex_unlock (&aspect_ratio_crop->crop_lock);
152 gst_aspect_ratio_crop_sink_event (GstPad * pad, GstObject * parent,
155 GstAspectRatioCrop *aspect_ratio_crop = GST_ASPECT_RATIO_CROP (parent);
157 switch (GST_EVENT_TYPE (evt)) {
162 gst_event_parse_caps (evt, &caps);
163 gst_aspect_ratio_crop_set_caps (aspect_ratio_crop, caps);
170 return gst_pad_event_default (pad, parent, evt);
174 gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass)
176 GObjectClass *gobject_class;
177 GstElementClass *element_class;
179 gobject_class = (GObjectClass *) klass;
180 element_class = (GstElementClass *) klass;
182 gobject_class->set_property = gst_aspect_ratio_crop_set_property;
183 gobject_class->get_property = gst_aspect_ratio_crop_get_property;
184 gobject_class->finalize = gst_aspect_ratio_crop_finalize;
186 g_object_class_install_property (gobject_class, PROP_ASPECT_RATIO_CROP,
187 gst_param_spec_fraction ("aspect-ratio", "aspect-ratio",
188 "Target aspect-ratio of video", 0, 1, G_MAXINT, 1, 0, 1,
189 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 gst_element_class_set_static_metadata (element_class, "aspectratiocrop",
192 "Filter/Effect/Video",
193 "Crops video into a user-defined aspect-ratio",
194 "Thijs Vermeir <thijsvermeir@gmail.com>");
196 gst_element_class_add_static_pad_template (element_class, &sink_template);
197 gst_element_class_add_static_pad_template (element_class, &src_template);
201 gst_aspect_ratio_crop_finalize (GObject * object)
203 GstAspectRatioCrop *aspect_ratio_crop;
205 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
207 g_mutex_clear (&aspect_ratio_crop->crop_lock);
208 gst_clear_caps (&aspect_ratio_crop->renegotiation_caps);
210 G_OBJECT_CLASS (parent_class)->finalize (object);
214 gst_aspect_ratio_crop_sink_chain (GstPad * pad, GstObject * parent,
217 GstCaps *caps = NULL;
218 GstAspectRatioCrop *aspect_ratio_crop = GST_ASPECT_RATIO_CROP (parent);
220 GST_OBJECT_LOCK (parent);
221 caps = aspect_ratio_crop->renegotiation_caps;
222 aspect_ratio_crop->renegotiation_caps = NULL;
223 GST_OBJECT_UNLOCK (parent);
226 gst_aspect_ratio_crop_set_caps (GST_ASPECT_RATIO_CROP (parent), caps);
227 gst_caps_unref (caps);
230 return gst_proxy_pad_chain_default (pad, parent, buffer);
235 gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop)
240 GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0,
243 aspect_ratio_crop->ar_num = 0;
244 aspect_ratio_crop->ar_denom = 1;
246 g_mutex_init (&aspect_ratio_crop->crop_lock);
248 /* add the transform element */
249 aspect_ratio_crop->videocrop = gst_element_factory_make ("videocrop", NULL);
250 gst_bin_add (GST_BIN (aspect_ratio_crop), aspect_ratio_crop->videocrop);
252 /* create ghost pad src */
254 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
256 src_pad = gst_ghost_pad_new ("src", link_pad);
257 gst_pad_set_query_function (src_pad,
258 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_src_query));
259 gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), src_pad);
260 gst_object_unref (link_pad);
261 /* create ghost pad sink */
263 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
265 aspect_ratio_crop->sink = gst_ghost_pad_new ("sink", link_pad);
266 gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop),
267 aspect_ratio_crop->sink);
268 gst_object_unref (link_pad);
270 gst_pad_set_event_function (aspect_ratio_crop->sink,
271 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_sink_event));
272 gst_pad_set_chain_function (aspect_ratio_crop->sink,
273 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_sink_chain));
277 gst_aspect_ratio_transform_structure (GstAspectRatioCrop * aspect_ratio_crop,
278 GstStructure * structure, GstStructure ** new_structure,
279 gboolean set_videocrop)
282 gdouble requested_ar;
287 /* Check if we need to change the aspect ratio */
288 if (aspect_ratio_crop->ar_num < 1) {
289 GST_DEBUG_OBJECT (aspect_ratio_crop, "No cropping requested");
293 /* get the information from the caps */
294 if (!gst_structure_get_int (structure, "width", &width) ||
295 !gst_structure_get_int (structure, "height", &height))
298 if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
303 incoming_ar = ((gdouble) (width * par_n)) / (height * par_d);
304 GST_LOG_OBJECT (aspect_ratio_crop,
305 "incoming caps width(%d), height(%d), par (%d/%d) : ar = %f", width,
306 height, par_n, par_d, incoming_ar);
309 (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom;
311 /* check if the original aspect-ratio is the aspect-ratio that we want */
312 if (requested_ar == incoming_ar) {
313 GST_DEBUG_OBJECT (aspect_ratio_crop,
314 "Input video already has the correct aspect ratio (%.3f == %.3f)",
315 incoming_ar, requested_ar);
317 } else if (requested_ar > incoming_ar) {
318 /* fix aspect ratio with cropping on top and bottom */
320 ((((double) aspect_ratio_crop->ar_denom /
321 (double) (aspect_ratio_crop->ar_num)) * ((double) par_n /
322 (double) par_d) * width) - height) / 2;
326 if (cropvalue >= (height / 2))
329 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0,
333 *new_structure = gst_structure_copy (structure);
334 gst_structure_set (*new_structure,
335 "height", G_TYPE_INT, (int) (height - (cropvalue * 2)), NULL);
338 /* fix aspect ratio with cropping on left and right */
340 ((((double) aspect_ratio_crop->ar_num /
341 (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d /
342 (double) par_n) * height) - width) / 2;
346 if (cropvalue >= (width / 2))
349 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue,
353 *new_structure = gst_structure_copy (structure);
354 gst_structure_set (*new_structure,
355 "width", G_TYPE_INT, (int) (width - (cropvalue * 2)), NULL);
362 GST_WARNING_OBJECT (aspect_ratio_crop,
363 "can't crop to aspect ratio requested");
367 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0);
371 *new_structure = gst_structure_copy (structure);
376 gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop,
382 transform = gst_caps_new_empty ();
384 size = gst_caps_get_size (caps);
386 for (i = 0; i < size; i++) {
388 GstStructure *trans_s;
390 s = gst_caps_get_structure (caps, i);
392 gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s,
394 gst_caps_append_structure (transform, trans_s);
401 gst_aspect_ratio_crop_get_caps (GstPad * pad, GstCaps * filter)
404 GstAspectRatioCrop *aspect_ratio_crop;
405 GstCaps *return_caps;
407 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
409 g_mutex_lock (&aspect_ratio_crop->crop_lock);
411 peer = gst_pad_get_peer (aspect_ratio_crop->sink);
413 return_caps = gst_static_pad_template_get_caps (&src_template);
417 peer_caps = gst_pad_query_caps (peer, filter);
419 gst_aspect_ratio_crop_transform_caps (aspect_ratio_crop, peer_caps);
420 gst_caps_unref (peer_caps);
421 gst_object_unref (peer);
424 g_mutex_unlock (&aspect_ratio_crop->crop_lock);
425 gst_object_unref (aspect_ratio_crop);
427 if (return_caps && filter) {
429 gst_caps_intersect_full (filter, return_caps, GST_CAPS_INTERSECT_FIRST);
430 gst_caps_replace (&return_caps, tmp);
431 gst_caps_unref (tmp);
438 gst_aspect_ratio_crop_src_query (GstPad * pad, GstObject * parent,
441 gboolean res = FALSE;
443 switch (GST_QUERY_TYPE (query)) {
446 GstCaps *filter, *caps;
448 gst_query_parse_caps (query, &filter);
449 caps = gst_aspect_ratio_crop_get_caps (pad, filter);
450 gst_query_set_caps_result (query, caps);
451 gst_caps_unref (caps);
456 res = gst_pad_query_default (pad, parent, query);
463 gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
464 const GValue * value, GParamSpec * pspec)
466 GstAspectRatioCrop *aspect_ratio_crop;
467 gboolean recheck = FALSE;
469 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
471 GST_OBJECT_LOCK (aspect_ratio_crop);
473 case PROP_ASPECT_RATIO_CROP:
474 if (GST_VALUE_HOLDS_FRACTION (value)) {
475 aspect_ratio_crop->ar_num = gst_value_get_fraction_numerator (value);
476 aspect_ratio_crop->ar_denom =
477 gst_value_get_fraction_denominator (value);
478 recheck = gst_pad_has_current_caps (aspect_ratio_crop->sink);
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 GST_OBJECT_UNLOCK (aspect_ratio_crop);
488 GST_OBJECT_LOCK (aspect_ratio_crop);
489 gst_clear_caps (&aspect_ratio_crop->renegotiation_caps);
490 aspect_ratio_crop->renegotiation_caps =
491 gst_pad_get_current_caps (aspect_ratio_crop->sink);
492 GST_OBJECT_UNLOCK (aspect_ratio_crop);
497 gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
498 GValue * value, GParamSpec * pspec)
500 GstAspectRatioCrop *aspect_ratio_crop;
502 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
504 GST_OBJECT_LOCK (aspect_ratio_crop);
506 case PROP_ASPECT_RATIO_CROP:
507 gst_value_set_fraction (value, aspect_ratio_crop->ar_num,
508 aspect_ratio_crop->ar_denom);
511 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514 GST_OBJECT_UNLOCK (aspect_ratio_crop);