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_RGBx ";" \
58 GST_VIDEO_CAPS_xRGB ";" \
59 GST_VIDEO_CAPS_BGRx ";" \
60 GST_VIDEO_CAPS_xBGR ";" \
61 GST_VIDEO_CAPS_RGBA ";" \
62 GST_VIDEO_CAPS_ARGB ";" \
63 GST_VIDEO_CAPS_BGRA ";" \
64 GST_VIDEO_CAPS_ABGR ";" \
65 GST_VIDEO_CAPS_RGB ";" \
66 GST_VIDEO_CAPS_BGR ";" \
67 GST_VIDEO_CAPS_YUV ("AYUV") ";" \
68 GST_VIDEO_CAPS_YUV ("YUY2") ";" \
69 GST_VIDEO_CAPS_YUV ("YVYU") ";" \
70 GST_VIDEO_CAPS_YUV ("UYVY") ";" \
71 GST_VIDEO_CAPS_YUV ("Y800") ";" \
72 GST_VIDEO_CAPS_YUV ("I420") ";" \
73 GST_VIDEO_CAPS_YUV ("YV12") ";" \
74 GST_VIDEO_CAPS_RGB_16 ";" \
77 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
80 GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
83 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
86 GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
89 GST_BOILERPLATE (GstAspectRatioCrop, gst_aspect_ratio_crop, GstBin,
92 static void gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
94 static void gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
96 static void gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop *
97 aspect_ratio_crop, gint top, gint right, gint bottom, gint left);
98 static GstCaps *gst_aspect_ratio_crop_get_caps (GstPad * pad);
99 static gboolean gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps);
100 static void gst_aspect_ratio_crop_finalize (GObject * object);
101 static void gst_aspect_ratio_transform_structure (GstAspectRatioCrop *
102 aspect_ratio_crop, GstStructure * structure, GstStructure ** new_structure,
103 gboolean set_videocrop);
106 gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop,
107 gint top, gint right, gint bottom, gint left)
109 GValue value = { 0 };
110 if (G_UNLIKELY (!aspect_ratio_crop->videocrop)) {
111 GST_WARNING_OBJECT (aspect_ratio_crop,
112 "Can't set the settings if there is no cropping element");
116 g_value_init (&value, G_TYPE_INT);
117 g_value_set_int (&value, top);
118 GST_DEBUG_OBJECT (aspect_ratio_crop, "set top cropping to: %d", top);
119 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "top",
121 g_value_set_int (&value, right);
122 GST_DEBUG_OBJECT (aspect_ratio_crop, "set right cropping to: %d", right);
123 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "right",
125 g_value_set_int (&value, bottom);
126 GST_DEBUG_OBJECT (aspect_ratio_crop, "set bottom cropping to: %d", bottom);
127 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "bottom",
129 g_value_set_int (&value, left);
130 GST_DEBUG_OBJECT (aspect_ratio_crop, "set left cropping to: %d", left);
131 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "left",
134 g_value_unset (&value);
138 gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps)
140 GstAspectRatioCrop *aspect_ratio_crop;
142 GstStructure *structure;
145 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
147 g_mutex_lock (aspect_ratio_crop->crop_lock);
149 structure = gst_caps_get_structure (caps, 0);
150 gst_aspect_ratio_transform_structure (aspect_ratio_crop, structure, NULL,
153 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
155 ret = gst_pad_set_caps (peer_pad, caps);
156 gst_object_unref (peer_pad);
157 gst_object_unref (aspect_ratio_crop);
158 g_mutex_unlock (aspect_ratio_crop->crop_lock);
163 gst_aspect_ratio_crop_base_init (gpointer g_class)
165 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
167 gst_element_class_set_details_simple (element_class, "aspectratiocrop",
168 "Filter/Effect/Video",
169 "Crops video into a user-defined aspect-ratio",
170 "Thijs Vermeir <thijsvermeir@gmail.com>");
172 gst_element_class_add_static_pad_template (element_class,
174 gst_element_class_add_static_pad_template (element_class, &src_template);
178 gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass)
180 GObjectClass *gobject_class;
182 gobject_class = (GObjectClass *) klass;
184 gobject_class->set_property = gst_aspect_ratio_crop_set_property;
185 gobject_class->get_property = gst_aspect_ratio_crop_get_property;
186 gobject_class->finalize = gst_aspect_ratio_crop_finalize;
188 g_object_class_install_property (gobject_class, ARG_ASPECT_RATIO_CROP,
189 gst_param_spec_fraction ("aspect-ratio", "aspect-ratio",
190 "Target aspect-ratio of video", 0, 1, G_MAXINT, 1, 0, 1,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 gst_aspect_ratio_crop_finalize (GObject * object)
197 GstAspectRatioCrop *aspect_ratio_crop;
199 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
201 if (aspect_ratio_crop->crop_lock)
202 g_mutex_free (aspect_ratio_crop->crop_lock);
204 G_OBJECT_CLASS (parent_class)->finalize (object);
208 gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop,
209 GstAspectRatioCropClass * klass)
214 GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0,
217 aspect_ratio_crop->ar_num = 0;
218 aspect_ratio_crop->ar_denom = 1;
220 aspect_ratio_crop->crop_lock = g_mutex_new ();
222 /* add the transform element */
223 aspect_ratio_crop->videocrop = gst_element_factory_make ("videocrop", NULL);
224 gst_bin_add (GST_BIN (aspect_ratio_crop), aspect_ratio_crop->videocrop);
226 /* create ghost pad src */
228 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
230 src_pad = gst_ghost_pad_new ("src", link_pad);
231 gst_pad_set_getcaps_function (src_pad,
232 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_get_caps));
233 gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), src_pad);
234 gst_object_unref (link_pad);
235 /* create ghost pad sink */
237 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
239 aspect_ratio_crop->sink = gst_ghost_pad_new ("sink", link_pad);
240 gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop),
241 aspect_ratio_crop->sink);
242 gst_object_unref (link_pad);
243 gst_pad_set_setcaps_function (aspect_ratio_crop->sink,
244 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_set_caps));
248 gst_aspect_ratio_transform_structure (GstAspectRatioCrop * aspect_ratio_crop,
249 GstStructure * structure, GstStructure ** new_structure,
250 gboolean set_videocrop)
253 gdouble requested_ar;
258 /* Check if we need to change the aspect ratio */
259 if (aspect_ratio_crop->ar_num < 1) {
260 GST_DEBUG_OBJECT (aspect_ratio_crop, "No cropping requested");
264 /* get the information from the caps */
265 if (!gst_structure_get_int (structure, "width", &width) ||
266 !gst_structure_get_int (structure, "height", &height))
269 if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
274 incoming_ar = ((gdouble) (width * par_n)) / (height * par_d);
275 GST_LOG_OBJECT (aspect_ratio_crop,
276 "incoming caps width(%d), height(%d), par (%d/%d) : ar = %f", width,
277 height, par_n, par_d, incoming_ar);
280 (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom;
282 /* check if the original aspect-ratio is the aspect-ratio that we want */
283 if (requested_ar == incoming_ar) {
284 GST_DEBUG_OBJECT (aspect_ratio_crop,
285 "Input video already has the correct aspect ratio (%.3f == %.3f)",
286 incoming_ar, requested_ar);
288 } else if (requested_ar > incoming_ar) {
289 /* fix aspect ratio with cropping on top and bottom */
291 ((((double) aspect_ratio_crop->ar_denom /
292 (double) (aspect_ratio_crop->ar_num)) * ((double) par_n /
293 (double) par_d) * width) - height) / 2;
297 if (cropvalue >= (height / 2))
300 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0,
304 *new_structure = gst_structure_copy (structure);
305 gst_structure_set (*new_structure,
306 "height", G_TYPE_INT, (int) (height - (cropvalue * 2)), NULL);
309 /* fix aspect ratio with cropping on left and right */
311 ((((double) aspect_ratio_crop->ar_num /
312 (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d /
313 (double) par_n) * height) - width) / 2;
317 if (cropvalue >= (width / 2))
320 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue,
324 *new_structure = gst_structure_copy (structure);
325 gst_structure_set (*new_structure,
326 "width", G_TYPE_INT, (int) (width - (cropvalue * 2)), NULL);
333 GST_WARNING_OBJECT (aspect_ratio_crop,
334 "can't crop to aspect ratio requested");
338 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0);
342 *new_structure = gst_structure_copy (structure);
347 gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop,
353 transform = gst_caps_new_empty ();
355 size = gst_caps_get_size (caps);
357 for (i = 0; i < size; i++) {
359 GstStructure *trans_s;
361 s = gst_caps_get_structure (caps, i);
363 gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s,
365 gst_caps_append_structure (transform, trans_s);
372 gst_aspect_ratio_crop_get_caps (GstPad * pad)
375 GstAspectRatioCrop *aspect_ratio_crop;
376 GstCaps *return_caps;
378 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
380 g_mutex_lock (aspect_ratio_crop->crop_lock);
382 peer = gst_pad_get_peer (aspect_ratio_crop->sink);
384 return_caps = gst_static_pad_template_get_caps (&src_template);
385 gst_caps_ref (return_caps);
389 peer_caps = gst_pad_get_caps (peer);
391 gst_aspect_ratio_crop_transform_caps (aspect_ratio_crop, peer_caps);
392 gst_caps_unref (peer_caps);
393 gst_object_unref (peer);
396 g_mutex_unlock (aspect_ratio_crop->crop_lock);
397 gst_object_unref (aspect_ratio_crop);
403 gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
404 const GValue * value, GParamSpec * pspec)
406 GstAspectRatioCrop *aspect_ratio_crop;
407 gboolean recheck = FALSE;
409 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
411 GST_OBJECT_LOCK (aspect_ratio_crop);
413 case ARG_ASPECT_RATIO_CROP:
414 if (GST_VALUE_HOLDS_FRACTION (value)) {
415 aspect_ratio_crop->ar_num = gst_value_get_fraction_numerator (value);
416 aspect_ratio_crop->ar_denom =
417 gst_value_get_fraction_denominator (value);
418 recheck = (GST_PAD_CAPS (aspect_ratio_crop->sink) != NULL);
422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
425 GST_OBJECT_UNLOCK (aspect_ratio_crop);
428 gst_aspect_ratio_crop_set_caps (aspect_ratio_crop->sink,
429 GST_PAD_CAPS (aspect_ratio_crop->sink));
434 gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
435 GValue * value, GParamSpec * pspec)
437 GstAspectRatioCrop *aspect_ratio_crop;
439 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
441 GST_OBJECT_LOCK (aspect_ratio_crop);
443 case ARG_ASPECT_RATIO_CROP:
444 gst_value_set_fraction (value, aspect_ratio_crop->ar_num,
445 aspect_ratio_crop->ar_denom);
448 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
451 GST_OBJECT_UNLOCK (aspect_ratio_crop);