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 #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 ARG_ASPECT_RATIO_CROP,
57 /* we support the same caps as videocrop */
58 #define ASPECT_RATIO_CROP_CAPS \
59 GST_VIDEO_CAPS_RGBx ";" \
60 GST_VIDEO_CAPS_xRGB ";" \
61 GST_VIDEO_CAPS_BGRx ";" \
62 GST_VIDEO_CAPS_xBGR ";" \
63 GST_VIDEO_CAPS_RGBA ";" \
64 GST_VIDEO_CAPS_ARGB ";" \
65 GST_VIDEO_CAPS_BGRA ";" \
66 GST_VIDEO_CAPS_ABGR ";" \
67 GST_VIDEO_CAPS_RGB ";" \
68 GST_VIDEO_CAPS_BGR ";" \
69 GST_VIDEO_CAPS_YUV ("AYUV") ";" \
70 GST_VIDEO_CAPS_YUV ("YUY2") ";" \
71 GST_VIDEO_CAPS_YUV ("YVYU") ";" \
72 GST_VIDEO_CAPS_YUV ("UYVY") ";" \
73 GST_VIDEO_CAPS_YUV ("Y800") ";" \
74 GST_VIDEO_CAPS_YUV ("I420") ";" \
75 GST_VIDEO_CAPS_YUV ("YV12") ";" \
76 GST_VIDEO_CAPS_RGB_16 ";" \
79 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
82 GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
85 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
88 GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
91 GST_BOILERPLATE (GstAspectRatioCrop, gst_aspect_ratio_crop, GstBin,
94 static void gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
98 static void gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop *
99 aspect_ratio_crop, gint top, gint right, gint bottom, gint left);
100 static GstCaps *gst_aspect_ratio_crop_get_caps (GstPad * pad);
101 static gboolean gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps);
102 static void gst_aspect_ratio_crop_finalize (GObject * object);
103 static void gst_aspect_ratio_transform_structure (GstAspectRatioCrop *
104 aspect_ratio_crop, GstStructure * structure, GstStructure ** new_structure,
105 gboolean set_videocrop);
108 gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop,
109 gint top, gint right, gint bottom, gint left)
111 GValue value = { 0 };
112 if (G_UNLIKELY (!aspect_ratio_crop->videocrop)) {
113 GST_WARNING_OBJECT (aspect_ratio_crop,
114 "Can't set the settings if there is no cropping element");
118 g_value_init (&value, G_TYPE_INT);
119 g_value_set_int (&value, top);
120 GST_DEBUG_OBJECT (aspect_ratio_crop, "set top cropping to: %d", top);
121 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "top",
123 g_value_set_int (&value, right);
124 GST_DEBUG_OBJECT (aspect_ratio_crop, "set right cropping to: %d", right);
125 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "right",
127 g_value_set_int (&value, bottom);
128 GST_DEBUG_OBJECT (aspect_ratio_crop, "set bottom cropping to: %d", bottom);
129 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "bottom",
131 g_value_set_int (&value, left);
132 GST_DEBUG_OBJECT (aspect_ratio_crop, "set left cropping to: %d", left);
133 g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "left",
136 g_value_unset (&value);
140 gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps)
142 GstAspectRatioCrop *aspect_ratio_crop;
144 GstStructure *structure;
147 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
149 g_mutex_lock (aspect_ratio_crop->crop_lock);
151 structure = gst_caps_get_structure (caps, 0);
152 gst_aspect_ratio_transform_structure (aspect_ratio_crop, structure, NULL,
155 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
157 ret = gst_pad_set_caps (peer_pad, caps);
158 gst_object_unref (peer_pad);
159 gst_object_unref (aspect_ratio_crop);
160 g_mutex_unlock (aspect_ratio_crop->crop_lock);
165 gst_aspect_ratio_crop_base_init (gpointer g_class)
167 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
169 gst_element_class_set_details_simple (element_class, "aspectratiocrop",
170 "Filter/Effect/Video",
171 "Crops video into a user-defined aspect-ratio",
172 "Thijs Vermeir <thijsvermeir@gmail.com>");
174 gst_element_class_add_static_pad_template (element_class, &sink_template);
175 gst_element_class_add_static_pad_template (element_class, &src_template);
179 gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass)
181 GObjectClass *gobject_class;
183 gobject_class = (GObjectClass *) klass;
185 gobject_class->set_property = gst_aspect_ratio_crop_set_property;
186 gobject_class->get_property = gst_aspect_ratio_crop_get_property;
187 gobject_class->finalize = gst_aspect_ratio_crop_finalize;
189 g_object_class_install_property (gobject_class, ARG_ASPECT_RATIO_CROP,
190 gst_param_spec_fraction ("aspect-ratio", "aspect-ratio",
191 "Target aspect-ratio of video", 0, 1, G_MAXINT, 1, 0, 1,
192 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 gst_aspect_ratio_crop_finalize (GObject * object)
198 GstAspectRatioCrop *aspect_ratio_crop;
200 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
202 if (aspect_ratio_crop->crop_lock)
203 g_mutex_free (aspect_ratio_crop->crop_lock);
205 G_OBJECT_CLASS (parent_class)->finalize (object);
209 gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop,
210 GstAspectRatioCropClass * klass)
215 GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0,
218 aspect_ratio_crop->ar_num = 0;
219 aspect_ratio_crop->ar_denom = 1;
221 aspect_ratio_crop->crop_lock = g_mutex_new ();
223 /* add the transform element */
224 aspect_ratio_crop->videocrop = gst_element_factory_make ("videocrop", NULL);
225 gst_bin_add (GST_BIN (aspect_ratio_crop), aspect_ratio_crop->videocrop);
227 /* create ghost pad src */
229 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
231 src_pad = gst_ghost_pad_new ("src", link_pad);
232 gst_pad_set_getcaps_function (src_pad,
233 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_get_caps));
234 gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), src_pad);
235 gst_object_unref (link_pad);
236 /* create ghost pad sink */
238 gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
240 aspect_ratio_crop->sink = gst_ghost_pad_new ("sink", link_pad);
241 gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop),
242 aspect_ratio_crop->sink);
243 gst_object_unref (link_pad);
244 gst_pad_set_setcaps_function (aspect_ratio_crop->sink,
245 GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_set_caps));
249 gst_aspect_ratio_transform_structure (GstAspectRatioCrop * aspect_ratio_crop,
250 GstStructure * structure, GstStructure ** new_structure,
251 gboolean set_videocrop)
254 gdouble requested_ar;
259 /* Check if we need to change the aspect ratio */
260 if (aspect_ratio_crop->ar_num < 1) {
261 GST_DEBUG_OBJECT (aspect_ratio_crop, "No cropping requested");
265 /* get the information from the caps */
266 if (!gst_structure_get_int (structure, "width", &width) ||
267 !gst_structure_get_int (structure, "height", &height))
270 if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
275 incoming_ar = ((gdouble) (width * par_n)) / (height * par_d);
276 GST_LOG_OBJECT (aspect_ratio_crop,
277 "incoming caps width(%d), height(%d), par (%d/%d) : ar = %f", width,
278 height, par_n, par_d, incoming_ar);
281 (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom;
283 /* check if the original aspect-ratio is the aspect-ratio that we want */
284 if (requested_ar == incoming_ar) {
285 GST_DEBUG_OBJECT (aspect_ratio_crop,
286 "Input video already has the correct aspect ratio (%.3f == %.3f)",
287 incoming_ar, requested_ar);
289 } else if (requested_ar > incoming_ar) {
290 /* fix aspect ratio with cropping on top and bottom */
292 ((((double) aspect_ratio_crop->ar_denom /
293 (double) (aspect_ratio_crop->ar_num)) * ((double) par_n /
294 (double) par_d) * width) - height) / 2;
298 if (cropvalue >= (height / 2))
301 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0,
305 *new_structure = gst_structure_copy (structure);
306 gst_structure_set (*new_structure,
307 "height", G_TYPE_INT, (int) (height - (cropvalue * 2)), NULL);
310 /* fix aspect ratio with cropping on left and right */
312 ((((double) aspect_ratio_crop->ar_num /
313 (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d /
314 (double) par_n) * height) - width) / 2;
318 if (cropvalue >= (width / 2))
321 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue,
325 *new_structure = gst_structure_copy (structure);
326 gst_structure_set (*new_structure,
327 "width", G_TYPE_INT, (int) (width - (cropvalue * 2)), NULL);
334 GST_WARNING_OBJECT (aspect_ratio_crop,
335 "can't crop to aspect ratio requested");
339 gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0);
343 *new_structure = gst_structure_copy (structure);
348 gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop,
354 transform = gst_caps_new_empty ();
356 size = gst_caps_get_size (caps);
358 for (i = 0; i < size; i++) {
360 GstStructure *trans_s;
362 s = gst_caps_get_structure (caps, i);
364 gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s,
366 gst_caps_append_structure (transform, trans_s);
373 gst_aspect_ratio_crop_get_caps (GstPad * pad)
376 GstAspectRatioCrop *aspect_ratio_crop;
377 GstCaps *return_caps;
379 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
381 g_mutex_lock (aspect_ratio_crop->crop_lock);
383 peer = gst_pad_get_peer (aspect_ratio_crop->sink);
385 return_caps = gst_static_pad_template_get_caps (&src_template);
386 gst_caps_ref (return_caps);
390 peer_caps = gst_pad_get_caps (peer);
392 gst_aspect_ratio_crop_transform_caps (aspect_ratio_crop, peer_caps);
393 gst_caps_unref (peer_caps);
394 gst_object_unref (peer);
397 g_mutex_unlock (aspect_ratio_crop->crop_lock);
398 gst_object_unref (aspect_ratio_crop);
404 gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
405 const GValue * value, GParamSpec * pspec)
407 GstAspectRatioCrop *aspect_ratio_crop;
408 gboolean recheck = FALSE;
410 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
412 GST_OBJECT_LOCK (aspect_ratio_crop);
414 case ARG_ASPECT_RATIO_CROP:
415 if (GST_VALUE_HOLDS_FRACTION (value)) {
416 aspect_ratio_crop->ar_num = gst_value_get_fraction_numerator (value);
417 aspect_ratio_crop->ar_denom =
418 gst_value_get_fraction_denominator (value);
419 recheck = (GST_PAD_CAPS (aspect_ratio_crop->sink) != NULL);
423 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
426 GST_OBJECT_UNLOCK (aspect_ratio_crop);
429 gst_aspect_ratio_crop_set_caps (aspect_ratio_crop->sink,
430 GST_PAD_CAPS (aspect_ratio_crop->sink));
435 gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
436 GValue * value, GParamSpec * pspec)
438 GstAspectRatioCrop *aspect_ratio_crop;
440 aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
442 GST_OBJECT_LOCK (aspect_ratio_crop);
444 case ARG_ASPECT_RATIO_CROP:
445 gst_value_set_fraction (value, aspect_ratio_crop->ar_num,
446 aspect_ratio_crop->ar_denom);
449 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452 GST_OBJECT_UNLOCK (aspect_ratio_crop);