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  * @see_also: #GstVideoCrop
23  *
24  * This element crops video frames to a specified #GstAspectRatioCrop:aspect-ratio.
25  *
26  * If the aspect-ratio is already correct, the element will operate
27  * in pass-through mode.
28  *
29  * <refsect2>
30  * <title>Example launch line</title>
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  * </refsect2>
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 "gstaspectratiocrop.h"
45 
46 #include "gst/glib-compat-private.h"
47 
48 GST_DEBUG_CATEGORY_STATIC (aspect_ratio_crop_debug);
49 #define GST_CAT_DEFAULT aspect_ratio_crop_debug
50 
51 enum
52 {
53   PROP_0,
54   PROP_ASPECT_RATIO_CROP,
55 };
56 
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 }")
63 
64 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
68     );
69 
70 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
74     );
75 
76 #define gst_aspect_ratio_crop_parent_class parent_class
77 G_DEFINE_TYPE (GstAspectRatioCrop, gst_aspect_ratio_crop, GST_TYPE_BIN);
78 
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);
96 
97 static void
gst_aspect_ratio_crop_set_cropping(GstAspectRatioCrop * aspect_ratio_crop,gint top,gint right,gint bottom,gint left)98 gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop,
99     gint top, gint right, gint bottom, gint left)
100 {
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");
105     return;
106   }
107 
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",
112       &value);
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",
116       &value);
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",
120       &value);
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",
124       &value);
125 
126   g_value_unset (&value);
127 }
128 
129 static gboolean
gst_aspect_ratio_crop_set_caps(GstAspectRatioCrop * aspect_ratio_crop,GstCaps * caps)130 gst_aspect_ratio_crop_set_caps (GstAspectRatioCrop * aspect_ratio_crop,
131     GstCaps * caps)
132 {
133   GstPad *peer_pad;
134   GstStructure *structure;
135   gboolean ret;
136 
137   g_mutex_lock (&aspect_ratio_crop->crop_lock);
138 
139   structure = gst_caps_get_structure (caps, 0);
140   gst_aspect_ratio_transform_structure (aspect_ratio_crop, structure, NULL,
141       TRUE);
142   peer_pad =
143       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
144       "sink");
145   ret = gst_pad_set_caps (peer_pad, caps);
146   gst_object_unref (peer_pad);
147   g_mutex_unlock (&aspect_ratio_crop->crop_lock);
148   return ret;
149 }
150 
151 static gboolean
gst_aspect_ratio_crop_sink_event(GstPad * pad,GstObject * parent,GstEvent * evt)152 gst_aspect_ratio_crop_sink_event (GstPad * pad, GstObject * parent,
153     GstEvent * evt)
154 {
155   GstAspectRatioCrop *aspect_ratio_crop = GST_ASPECT_RATIO_CROP (parent);
156 
157   switch (GST_EVENT_TYPE (evt)) {
158     case GST_EVENT_CAPS:
159     {
160       GstCaps *caps;
161 
162       gst_event_parse_caps (evt, &caps);
163       gst_aspect_ratio_crop_set_caps (aspect_ratio_crop, caps);
164       break;
165     }
166     default:
167       break;
168   }
169 
170   return gst_pad_event_default (pad, parent, evt);
171 }
172 
173 static void
gst_aspect_ratio_crop_class_init(GstAspectRatioCropClass * klass)174 gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass)
175 {
176   GObjectClass *gobject_class;
177   GstElementClass *element_class;
178 
179   gobject_class = (GObjectClass *) klass;
180   element_class = (GstElementClass *) klass;
181 
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;
185 
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));
190 
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>");
195 
196   gst_element_class_add_static_pad_template (element_class, &sink_template);
197   gst_element_class_add_static_pad_template (element_class, &src_template);
198 }
199 
200 static void
gst_aspect_ratio_crop_finalize(GObject * object)201 gst_aspect_ratio_crop_finalize (GObject * object)
202 {
203   GstAspectRatioCrop *aspect_ratio_crop;
204 
205   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
206 
207   g_mutex_clear (&aspect_ratio_crop->crop_lock);
208   gst_clear_caps (&aspect_ratio_crop->renegotiation_caps);
209 
210   G_OBJECT_CLASS (parent_class)->finalize (object);
211 }
212 
213 static GstFlowReturn
gst_aspect_ratio_crop_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)214 gst_aspect_ratio_crop_sink_chain (GstPad * pad, GstObject * parent,
215     GstBuffer * buffer)
216 {
217   GstCaps *caps = NULL;
218   GstAspectRatioCrop *aspect_ratio_crop = GST_ASPECT_RATIO_CROP (parent);
219 
220   GST_OBJECT_LOCK (parent);
221   caps = aspect_ratio_crop->renegotiation_caps;
222   aspect_ratio_crop->renegotiation_caps = NULL;
223   GST_OBJECT_UNLOCK (parent);
224 
225   if (caps) {
226     gst_aspect_ratio_crop_set_caps (GST_ASPECT_RATIO_CROP (parent), caps);
227     gst_caps_unref (caps);
228   }
229 
230   return gst_proxy_pad_chain_default (pad, parent, buffer);
231 
232 }
233 
234 static void
gst_aspect_ratio_crop_init(GstAspectRatioCrop * aspect_ratio_crop)235 gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop)
236 {
237   GstPad *link_pad;
238   GstPad *src_pad;
239 
240   GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0,
241       "aspectratiocrop");
242 
243   aspect_ratio_crop->ar_num = 0;
244   aspect_ratio_crop->ar_denom = 1;
245 
246   g_mutex_init (&aspect_ratio_crop->crop_lock);
247 
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);
251 
252   /* create ghost pad src */
253   link_pad =
254       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
255       "src");
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 */
262   link_pad =
263       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
264       "sink");
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);
269 
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));
274 }
275 
276 static void
gst_aspect_ratio_transform_structure(GstAspectRatioCrop * aspect_ratio_crop,GstStructure * structure,GstStructure ** new_structure,gboolean set_videocrop)277 gst_aspect_ratio_transform_structure (GstAspectRatioCrop * aspect_ratio_crop,
278     GstStructure * structure, GstStructure ** new_structure,
279     gboolean set_videocrop)
280 {
281   gdouble incoming_ar;
282   gdouble requested_ar;
283   gint width, height;
284   gint cropvalue;
285   gint par_d, par_n;
286 
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");
290     goto beach;
291   }
292 
293   /* get the information from the caps */
294   if (!gst_structure_get_int (structure, "width", &width) ||
295       !gst_structure_get_int (structure, "height", &height))
296     goto beach;
297 
298   if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
299           &par_n, &par_d)) {
300     par_d = par_n = 1;
301   }
302 
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);
307 
308   requested_ar =
309       (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom;
310 
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);
316     goto beach;
317   } else if (requested_ar > incoming_ar) {
318     /* fix aspect ratio with cropping on top and bottom */
319     cropvalue =
320         ((((double) aspect_ratio_crop->ar_denom /
321                 (double) (aspect_ratio_crop->ar_num)) * ((double) par_n /
322                 (double) par_d) * width) - height) / 2;
323     if (cropvalue < 0) {
324       cropvalue *= -1;
325     }
326     if (cropvalue >= (height / 2))
327       goto crop_failed;
328     if (set_videocrop) {
329       gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0,
330           cropvalue, 0);
331     }
332     if (new_structure) {
333       *new_structure = gst_structure_copy (structure);
334       gst_structure_set (*new_structure,
335           "height", G_TYPE_INT, (int) (height - (cropvalue * 2)), NULL);
336     }
337   } else {
338     /* fix aspect ratio with cropping on left and right */
339     cropvalue =
340         ((((double) aspect_ratio_crop->ar_num /
341                 (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d /
342                 (double) par_n) * height) - width) / 2;
343     if (cropvalue < 0) {
344       cropvalue *= -1;
345     }
346     if (cropvalue >= (width / 2))
347       goto crop_failed;
348     if (set_videocrop) {
349       gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue,
350           0, cropvalue);
351     }
352     if (new_structure) {
353       *new_structure = gst_structure_copy (structure);
354       gst_structure_set (*new_structure,
355           "width", G_TYPE_INT, (int) (width - (cropvalue * 2)), NULL);
356     }
357   }
358 
359   return;
360 
361 crop_failed:
362   GST_WARNING_OBJECT (aspect_ratio_crop,
363       "can't crop to aspect ratio requested");
364   goto beach;
365 beach:
366   if (set_videocrop) {
367     gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0);
368   }
369 
370   if (new_structure) {
371     *new_structure = gst_structure_copy (structure);
372   }
373 }
374 
375 static GstCaps *
gst_aspect_ratio_crop_transform_caps(GstAspectRatioCrop * aspect_ratio_crop,GstCaps * caps)376 gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop,
377     GstCaps * caps)
378 {
379   GstCaps *transform;
380   gint size, i;
381 
382   transform = gst_caps_new_empty ();
383 
384   size = gst_caps_get_size (caps);
385 
386   for (i = 0; i < size; i++) {
387     GstStructure *s;
388     GstStructure *trans_s;
389 
390     s = gst_caps_get_structure (caps, i);
391 
392     gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s,
393         FALSE);
394     gst_caps_append_structure (transform, trans_s);
395   }
396 
397   return transform;
398 }
399 
400 static GstCaps *
gst_aspect_ratio_crop_get_caps(GstPad * pad,GstCaps * filter)401 gst_aspect_ratio_crop_get_caps (GstPad * pad, GstCaps * filter)
402 {
403   GstPad *peer;
404   GstAspectRatioCrop *aspect_ratio_crop;
405   GstCaps *return_caps;
406 
407   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
408 
409   g_mutex_lock (&aspect_ratio_crop->crop_lock);
410 
411   peer = gst_pad_get_peer (aspect_ratio_crop->sink);
412   if (peer == NULL) {
413     return_caps = gst_static_pad_template_get_caps (&src_template);
414   } else {
415     GstCaps *peer_caps;
416 
417     peer_caps = gst_pad_query_caps (peer, filter);
418     return_caps =
419         gst_aspect_ratio_crop_transform_caps (aspect_ratio_crop, peer_caps);
420     gst_caps_unref (peer_caps);
421     gst_object_unref (peer);
422   }
423 
424   g_mutex_unlock (&aspect_ratio_crop->crop_lock);
425   gst_object_unref (aspect_ratio_crop);
426 
427   if (return_caps && filter) {
428     GstCaps *tmp =
429         gst_caps_intersect_full (filter, return_caps, GST_CAPS_INTERSECT_FIRST);
430     gst_caps_replace (&return_caps, tmp);
431     gst_caps_unref (tmp);
432   }
433 
434   return return_caps;
435 }
436 
437 static gboolean
gst_aspect_ratio_crop_src_query(GstPad * pad,GstObject * parent,GstQuery * query)438 gst_aspect_ratio_crop_src_query (GstPad * pad, GstObject * parent,
439     GstQuery * query)
440 {
441   gboolean res = FALSE;
442 
443   switch (GST_QUERY_TYPE (query)) {
444     case GST_QUERY_CAPS:
445     {
446       GstCaps *filter, *caps;
447 
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);
452       res = TRUE;
453       break;
454     }
455     default:
456       res = gst_pad_query_default (pad, parent, query);
457       break;
458   }
459   return res;
460 }
461 
462 static void
gst_aspect_ratio_crop_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)463 gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
464     const GValue * value, GParamSpec * pspec)
465 {
466   GstAspectRatioCrop *aspect_ratio_crop;
467   gboolean recheck = FALSE;
468 
469   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
470 
471   GST_OBJECT_LOCK (aspect_ratio_crop);
472   switch (prop_id) {
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);
479       }
480       break;
481     default:
482       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
483       break;
484   }
485   GST_OBJECT_UNLOCK (aspect_ratio_crop);
486 
487   if (recheck) {
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);
493   }
494 }
495 
496 static void
gst_aspect_ratio_crop_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)497 gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
498     GValue * value, GParamSpec * pspec)
499 {
500   GstAspectRatioCrop *aspect_ratio_crop;
501 
502   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
503 
504   GST_OBJECT_LOCK (aspect_ratio_crop);
505   switch (prop_id) {
506     case PROP_ASPECT_RATIO_CROP:
507       gst_value_set_fraction (value, aspect_ratio_crop->ar_num,
508           aspect_ratio_crop->ar_denom);
509       break;
510     default:
511       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
512       break;
513   }
514   GST_OBJECT_UNLOCK (aspect_ratio_crop);
515 }
516