• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer video frame cropping to aspect-ratio
2  * Copyright (C) 2009 Thijs Vermeir <thijsvermeir@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 
20 /**
21  * SECTION:element-aspectratiocrop
22  * @title: aspectratiocrop
23  * @see_also: #GstVideoCrop
24  *
25  * This element crops video frames to a specified #GstAspectRatioCrop:aspect-ratio.
26  *
27  * If the aspect-ratio is already correct, the element will operate
28  * in pass-through mode.
29  *
30  * ## Example launch line
31  * |[
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.
34  *
35  */
36 
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 
41 #include <gst/gst.h>
42 #include <gst/video/video.h>
43 
44 #include "gstvideocrop.h"
45 #include "gstaspectratiocrop.h"
46 /* include private header which contains the supported formats */
47 #include "gstvideocrop-private.h"
48 
49 #include "gst/glib-compat-private.h"
50 
51 GST_DEBUG_CATEGORY_STATIC (aspect_ratio_crop_debug);
52 #define GST_CAT_DEFAULT aspect_ratio_crop_debug
53 
54 enum
55 {
56   PROP_0,
57   PROP_ASPECT_RATIO_CROP,
58 };
59 
60 /* we support the same caps as videocrop */
61 #define ASPECT_RATIO_CROP_CAPS VIDEO_CROP_CAPS
62 
63 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
67     );
68 
69 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
70     GST_PAD_SINK,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
73     );
74 
75 #define gst_aspect_ratio_crop_parent_class parent_class
76 G_DEFINE_TYPE (GstAspectRatioCrop, gst_aspect_ratio_crop, GST_TYPE_BIN);
77 GST_ELEMENT_REGISTER_DEFINE (aspectratiocrop, "aspectratiocrop", GST_RANK_NONE,
78     GST_TYPE_ASPECT_RATIO_CROP);
79 
80 static void gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
81     const GValue * value, GParamSpec * pspec);
82 static void gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
83     GValue * value, GParamSpec * pspec);
84 static void gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop *
85     aspect_ratio_crop, gint top, gint right, gint bottom, gint left);
86 static GstCaps *gst_aspect_ratio_crop_get_caps (GstPad * pad, GstCaps * filter);
87 static gboolean gst_aspect_ratio_crop_src_query (GstPad * pad,
88     GstObject * parent, GstQuery * query);
89 static gboolean gst_aspect_ratio_crop_set_caps (GstAspectRatioCrop *
90     aspect_ratio_crop, GstCaps * caps);
91 static gboolean gst_aspect_ratio_crop_sink_event (GstPad * pad,
92     GstObject * parent, GstEvent * evt);
93 static void gst_aspect_ratio_crop_finalize (GObject * object);
94 static void gst_aspect_ratio_transform_structure (GstAspectRatioCrop *
95     aspect_ratio_crop, GstStructure * structure, GstStructure ** new_structure,
96     gboolean set_videocrop);
97 
98 static void
gst_aspect_ratio_crop_set_cropping(GstAspectRatioCrop * aspect_ratio_crop,gint top,gint right,gint bottom,gint left)99 gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop,
100     gint top, gint right, gint bottom, gint left)
101 {
102   GValue value = { 0 };
103   if (G_UNLIKELY (!aspect_ratio_crop->videocrop)) {
104     GST_WARNING_OBJECT (aspect_ratio_crop,
105         "Can't set the settings if there is no cropping element");
106     return;
107   }
108 
109   g_value_init (&value, G_TYPE_INT);
110   g_value_set_int (&value, top);
111   GST_DEBUG_OBJECT (aspect_ratio_crop, "set top cropping to: %d", top);
112   g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "top",
113       &value);
114   g_value_set_int (&value, right);
115   GST_DEBUG_OBJECT (aspect_ratio_crop, "set right cropping to: %d", right);
116   g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "right",
117       &value);
118   g_value_set_int (&value, bottom);
119   GST_DEBUG_OBJECT (aspect_ratio_crop, "set bottom cropping to: %d", bottom);
120   g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "bottom",
121       &value);
122   g_value_set_int (&value, left);
123   GST_DEBUG_OBJECT (aspect_ratio_crop, "set left cropping to: %d", left);
124   g_object_set_property (G_OBJECT (aspect_ratio_crop->videocrop), "left",
125       &value);
126 
127   g_value_unset (&value);
128 }
129 
130 static gboolean
gst_aspect_ratio_crop_set_caps(GstAspectRatioCrop * aspect_ratio_crop,GstCaps * caps)131 gst_aspect_ratio_crop_set_caps (GstAspectRatioCrop * aspect_ratio_crop,
132     GstCaps * caps)
133 {
134   GstPad *peer_pad;
135   GstStructure *structure;
136   gboolean ret;
137 
138   g_mutex_lock (&aspect_ratio_crop->crop_lock);
139 
140   structure = gst_caps_get_structure (caps, 0);
141   gst_aspect_ratio_transform_structure (aspect_ratio_crop, structure, NULL,
142       TRUE);
143   peer_pad =
144       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
145       "sink");
146   ret = gst_pad_set_caps (peer_pad, caps);
147   gst_object_unref (peer_pad);
148   g_mutex_unlock (&aspect_ratio_crop->crop_lock);
149   return ret;
150 }
151 
152 static gboolean
gst_aspect_ratio_crop_sink_event(GstPad * pad,GstObject * parent,GstEvent * evt)153 gst_aspect_ratio_crop_sink_event (GstPad * pad, GstObject * parent,
154     GstEvent * evt)
155 {
156   GstAspectRatioCrop *aspect_ratio_crop = GST_ASPECT_RATIO_CROP (parent);
157 
158   switch (GST_EVENT_TYPE (evt)) {
159     case GST_EVENT_CAPS:
160     {
161       GstCaps *caps;
162 
163       gst_event_parse_caps (evt, &caps);
164       gst_aspect_ratio_crop_set_caps (aspect_ratio_crop, caps);
165       break;
166     }
167     default:
168       break;
169   }
170 
171   return gst_pad_event_default (pad, parent, evt);
172 }
173 
174 static void
gst_aspect_ratio_crop_class_init(GstAspectRatioCropClass * klass)175 gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass)
176 {
177   GObjectClass *gobject_class;
178   GstElementClass *element_class;
179 
180   gobject_class = (GObjectClass *) klass;
181   element_class = (GstElementClass *) klass;
182 
183   gobject_class->set_property = gst_aspect_ratio_crop_set_property;
184   gobject_class->get_property = gst_aspect_ratio_crop_get_property;
185   gobject_class->finalize = gst_aspect_ratio_crop_finalize;
186 
187   g_object_class_install_property (gobject_class, PROP_ASPECT_RATIO_CROP,
188       gst_param_spec_fraction ("aspect-ratio", "aspect-ratio",
189           "Target aspect-ratio of video", 0, 1, G_MAXINT, 1, 0, 1,
190           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 
192   gst_element_class_set_static_metadata (element_class, "aspectratiocrop",
193       "Filter/Effect/Video",
194       "Crops video into a user-defined aspect-ratio",
195       "Thijs Vermeir <thijsvermeir@gmail.com>");
196 
197   gst_element_class_add_static_pad_template (element_class, &sink_template);
198   gst_element_class_add_static_pad_template (element_class, &src_template);
199 }
200 
201 static void
gst_aspect_ratio_crop_finalize(GObject * object)202 gst_aspect_ratio_crop_finalize (GObject * object)
203 {
204   GstAspectRatioCrop *aspect_ratio_crop;
205 
206   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
207 
208   g_mutex_clear (&aspect_ratio_crop->crop_lock);
209   gst_clear_caps (&aspect_ratio_crop->renegotiation_caps);
210 
211   G_OBJECT_CLASS (parent_class)->finalize (object);
212 }
213 
214 static GstFlowReturn
gst_aspect_ratio_crop_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)215 gst_aspect_ratio_crop_sink_chain (GstPad * pad, GstObject * parent,
216     GstBuffer * buffer)
217 {
218   GstCaps *caps = NULL;
219   GstAspectRatioCrop *aspect_ratio_crop = GST_ASPECT_RATIO_CROP (parent);
220 
221   GST_OBJECT_LOCK (parent);
222   caps = aspect_ratio_crop->renegotiation_caps;
223   aspect_ratio_crop->renegotiation_caps = NULL;
224   GST_OBJECT_UNLOCK (parent);
225 
226   if (caps) {
227     gst_aspect_ratio_crop_set_caps (GST_ASPECT_RATIO_CROP (parent), caps);
228     gst_caps_unref (caps);
229   }
230 
231   return gst_proxy_pad_chain_default (pad, parent, buffer);
232 
233 }
234 
235 static void
gst_aspect_ratio_crop_init(GstAspectRatioCrop * aspect_ratio_crop)236 gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop)
237 {
238   GstPad *link_pad;
239   GstPad *src_pad;
240 
241   GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0,
242       "aspectratiocrop");
243 
244   aspect_ratio_crop->ar_num = 0;
245   aspect_ratio_crop->ar_denom = 1;
246 
247   g_mutex_init (&aspect_ratio_crop->crop_lock);
248 
249   /* add the transform element */
250   aspect_ratio_crop->videocrop = gst_element_factory_make ("videocrop", NULL);
251   gst_bin_add (GST_BIN (aspect_ratio_crop), aspect_ratio_crop->videocrop);
252 
253   /* create ghost pad src */
254   link_pad =
255       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
256       "src");
257   src_pad = gst_ghost_pad_new ("src", link_pad);
258   gst_pad_set_query_function (src_pad,
259       GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_src_query));
260   gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), src_pad);
261   gst_object_unref (link_pad);
262   /* create ghost pad sink */
263   link_pad =
264       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
265       "sink");
266   aspect_ratio_crop->sink = gst_ghost_pad_new ("sink", link_pad);
267   gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop),
268       aspect_ratio_crop->sink);
269   gst_object_unref (link_pad);
270 
271   gst_pad_set_event_function (aspect_ratio_crop->sink,
272       GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_sink_event));
273   gst_pad_set_chain_function (aspect_ratio_crop->sink,
274       GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_sink_chain));
275 }
276 
277 static void
gst_aspect_ratio_transform_structure(GstAspectRatioCrop * aspect_ratio_crop,GstStructure * structure,GstStructure ** new_structure,gboolean set_videocrop)278 gst_aspect_ratio_transform_structure (GstAspectRatioCrop * aspect_ratio_crop,
279     GstStructure * structure, GstStructure ** new_structure,
280     gboolean set_videocrop)
281 {
282   gdouble incoming_ar;
283   gdouble requested_ar;
284   gint width, height;
285   gint cropvalue;
286   gint par_d, par_n;
287 
288   /* Check if we need to change the aspect ratio */
289   if (aspect_ratio_crop->ar_num < 1) {
290     GST_DEBUG_OBJECT (aspect_ratio_crop, "No cropping requested");
291     goto beach;
292   }
293 
294   /* get the information from the caps */
295   if (!gst_structure_get_int (structure, "width", &width) ||
296       !gst_structure_get_int (structure, "height", &height))
297     goto beach;
298 
299   if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
300           &par_n, &par_d)) {
301     par_d = par_n = 1;
302   }
303 
304   incoming_ar = ((gdouble) (width * par_n)) / (height * par_d);
305   GST_LOG_OBJECT (aspect_ratio_crop,
306       "incoming caps width(%d), height(%d), par (%d/%d) : ar = %f", width,
307       height, par_n, par_d, incoming_ar);
308 
309   requested_ar =
310       (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom;
311 
312   /* check if the original aspect-ratio is the aspect-ratio that we want */
313   if (requested_ar == incoming_ar) {
314     GST_DEBUG_OBJECT (aspect_ratio_crop,
315         "Input video already has the correct aspect ratio (%.3f == %.3f)",
316         incoming_ar, requested_ar);
317     goto beach;
318   } else if (requested_ar > incoming_ar) {
319     /* fix aspect ratio with cropping on top and bottom */
320     cropvalue =
321         ((((double) aspect_ratio_crop->ar_denom /
322                 (double) (aspect_ratio_crop->ar_num)) * ((double) par_n /
323                 (double) par_d) * width) - height) / 2;
324     if (cropvalue < 0) {
325       cropvalue *= -1;
326     }
327     if (cropvalue >= (height / 2))
328       goto crop_failed;
329     if (set_videocrop) {
330       gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0,
331           cropvalue, 0);
332     }
333     if (new_structure) {
334       *new_structure = gst_structure_copy (structure);
335       gst_structure_set (*new_structure,
336           "height", G_TYPE_INT, (int) (height - (cropvalue * 2)), NULL);
337     }
338   } else {
339     /* fix aspect ratio with cropping on left and right */
340     cropvalue =
341         ((((double) aspect_ratio_crop->ar_num /
342                 (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d /
343                 (double) par_n) * height) - width) / 2;
344     if (cropvalue < 0) {
345       cropvalue *= -1;
346     }
347     if (cropvalue >= (width / 2))
348       goto crop_failed;
349     if (set_videocrop) {
350       gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue,
351           0, cropvalue);
352     }
353     if (new_structure) {
354       *new_structure = gst_structure_copy (structure);
355       gst_structure_set (*new_structure,
356           "width", G_TYPE_INT, (int) (width - (cropvalue * 2)), NULL);
357     }
358   }
359 
360   return;
361 
362 crop_failed:
363   GST_WARNING_OBJECT (aspect_ratio_crop,
364       "can't crop to aspect ratio requested");
365   goto beach;
366 beach:
367   if (set_videocrop) {
368     gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0);
369   }
370 
371   if (new_structure) {
372     *new_structure = gst_structure_copy (structure);
373   }
374 }
375 
376 static GstCaps *
gst_aspect_ratio_crop_transform_caps(GstAspectRatioCrop * aspect_ratio_crop,GstCaps * caps)377 gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop,
378     GstCaps * caps)
379 {
380   GstCaps *transform;
381   gint size, i;
382 
383   transform = gst_caps_new_empty ();
384 
385   size = gst_caps_get_size (caps);
386 
387   for (i = 0; i < size; i++) {
388     GstStructure *s;
389     GstStructure *trans_s;
390 
391     s = gst_caps_get_structure (caps, i);
392 
393     gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s,
394         FALSE);
395     gst_caps_append_structure (transform, trans_s);
396   }
397 
398   return transform;
399 }
400 
401 static GstCaps *
gst_aspect_ratio_crop_get_caps(GstPad * pad,GstCaps * filter)402 gst_aspect_ratio_crop_get_caps (GstPad * pad, GstCaps * filter)
403 {
404   GstPad *peer;
405   GstAspectRatioCrop *aspect_ratio_crop;
406   GstCaps *return_caps;
407 
408   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
409 
410   g_mutex_lock (&aspect_ratio_crop->crop_lock);
411 
412   peer = gst_pad_get_peer (aspect_ratio_crop->sink);
413   if (peer == NULL) {
414     return_caps = gst_static_pad_template_get_caps (&src_template);
415   } else {
416     GstCaps *peer_caps;
417 
418     peer_caps = gst_pad_query_caps (peer, filter);
419     return_caps =
420         gst_aspect_ratio_crop_transform_caps (aspect_ratio_crop, peer_caps);
421     gst_caps_unref (peer_caps);
422     gst_object_unref (peer);
423   }
424 
425   g_mutex_unlock (&aspect_ratio_crop->crop_lock);
426   gst_object_unref (aspect_ratio_crop);
427 
428   if (return_caps && filter) {
429     GstCaps *tmp =
430         gst_caps_intersect_full (filter, return_caps, GST_CAPS_INTERSECT_FIRST);
431     gst_caps_replace (&return_caps, tmp);
432     gst_caps_unref (tmp);
433   }
434 
435   return return_caps;
436 }
437 
438 static gboolean
gst_aspect_ratio_crop_src_query(GstPad * pad,GstObject * parent,GstQuery * query)439 gst_aspect_ratio_crop_src_query (GstPad * pad, GstObject * parent,
440     GstQuery * query)
441 {
442   gboolean res = FALSE;
443 
444   switch (GST_QUERY_TYPE (query)) {
445     case GST_QUERY_CAPS:
446     {
447       GstCaps *filter, *caps;
448 
449       gst_query_parse_caps (query, &filter);
450       caps = gst_aspect_ratio_crop_get_caps (pad, filter);
451       gst_query_set_caps_result (query, caps);
452       gst_caps_unref (caps);
453       res = TRUE;
454       break;
455     }
456     default:
457       res = gst_pad_query_default (pad, parent, query);
458       break;
459   }
460   return res;
461 }
462 
463 static void
gst_aspect_ratio_crop_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)464 gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
465     const GValue * value, GParamSpec * pspec)
466 {
467   GstAspectRatioCrop *aspect_ratio_crop;
468   gboolean recheck = FALSE;
469 
470   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
471 
472   GST_OBJECT_LOCK (aspect_ratio_crop);
473   switch (prop_id) {
474     case PROP_ASPECT_RATIO_CROP:
475       if (GST_VALUE_HOLDS_FRACTION (value)) {
476         aspect_ratio_crop->ar_num = gst_value_get_fraction_numerator (value);
477         aspect_ratio_crop->ar_denom =
478             gst_value_get_fraction_denominator (value);
479         recheck = gst_pad_has_current_caps (aspect_ratio_crop->sink);
480       }
481       break;
482     default:
483       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
484       break;
485   }
486   GST_OBJECT_UNLOCK (aspect_ratio_crop);
487 
488   if (recheck) {
489     GST_OBJECT_LOCK (aspect_ratio_crop);
490     gst_clear_caps (&aspect_ratio_crop->renegotiation_caps);
491     aspect_ratio_crop->renegotiation_caps =
492         gst_pad_get_current_caps (aspect_ratio_crop->sink);
493     GST_OBJECT_UNLOCK (aspect_ratio_crop);
494   }
495 }
496 
497 static void
gst_aspect_ratio_crop_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)498 gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
499     GValue * value, GParamSpec * pspec)
500 {
501   GstAspectRatioCrop *aspect_ratio_crop;
502 
503   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
504 
505   GST_OBJECT_LOCK (aspect_ratio_crop);
506   switch (prop_id) {
507     case PROP_ASPECT_RATIO_CROP:
508       gst_value_set_fraction (value, aspect_ratio_crop->ar_num,
509           aspect_ratio_crop->ar_denom);
510       break;
511     default:
512       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
513       break;
514   }
515   GST_OBJECT_UNLOCK (aspect_ratio_crop);
516 }
517