various: fix pad template leaks
[platform/upstream/gstreamer.git] / gst / videocrop / gstaspectratiocrop.c
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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 -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.
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 GST_DEBUG_CATEGORY_STATIC (aspect_ratio_crop_debug);
47 #define GST_CAT_DEFAULT aspect_ratio_crop_debug
48
49 enum
50 {
51   ARG_0,
52   ARG_ASPECT_RATIO_CROP,
53 };
54
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 ";"                      \
75   GST_VIDEO_CAPS_RGB_15
76
77 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
78     GST_PAD_SRC,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
81     );
82
83 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
84     GST_PAD_SINK,
85     GST_PAD_ALWAYS,
86     GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
87     );
88
89 GST_BOILERPLATE (GstAspectRatioCrop, gst_aspect_ratio_crop, GstBin,
90     GST_TYPE_BIN);
91
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);
104
105 static void
106 gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop,
107     gint top, gint right, gint bottom, gint left)
108 {
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");
113     return;
114   }
115
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",
120       &value);
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",
124       &value);
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",
128       &value);
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",
132       &value);
133
134   g_value_unset (&value);
135 }
136
137 static gboolean
138 gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps)
139 {
140   GstAspectRatioCrop *aspect_ratio_crop;
141   GstPad *peer_pad;
142   GstStructure *structure;
143   gboolean ret;
144
145   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
146
147   g_mutex_lock (aspect_ratio_crop->crop_lock);
148
149   structure = gst_caps_get_structure (caps, 0);
150   gst_aspect_ratio_transform_structure (aspect_ratio_crop, structure, NULL,
151       TRUE);
152   peer_pad =
153       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
154       "sink");
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);
159   return ret;
160 }
161
162 static void
163 gst_aspect_ratio_crop_base_init (gpointer g_class)
164 {
165   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
166
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>");
171
172   gst_element_class_add_static_pad_template (element_class,
173       &sink_template);
174   gst_element_class_add_static_pad_template (element_class, &src_template);
175 }
176
177 static void
178 gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass)
179 {
180   GObjectClass *gobject_class;
181
182   gobject_class = (GObjectClass *) klass;
183
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;
187
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));
192 }
193
194 static void
195 gst_aspect_ratio_crop_finalize (GObject * object)
196 {
197   GstAspectRatioCrop *aspect_ratio_crop;
198
199   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
200
201   if (aspect_ratio_crop->crop_lock)
202     g_mutex_free (aspect_ratio_crop->crop_lock);
203
204   G_OBJECT_CLASS (parent_class)->finalize (object);
205 }
206
207 static void
208 gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop,
209     GstAspectRatioCropClass * klass)
210 {
211   GstPad *link_pad;
212   GstPad *src_pad;
213
214   GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0,
215       "aspectratiocrop");
216
217   aspect_ratio_crop->ar_num = 0;
218   aspect_ratio_crop->ar_denom = 1;
219
220   aspect_ratio_crop->crop_lock = g_mutex_new ();
221
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);
225
226   /* create ghost pad src */
227   link_pad =
228       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
229       "src");
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 */
236   link_pad =
237       gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop),
238       "sink");
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));
245 }
246
247 static void
248 gst_aspect_ratio_transform_structure (GstAspectRatioCrop * aspect_ratio_crop,
249     GstStructure * structure, GstStructure ** new_structure,
250     gboolean set_videocrop)
251 {
252   gdouble incoming_ar;
253   gdouble requested_ar;
254   gint width, height;
255   gint cropvalue;
256   gint par_d, par_n;
257
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");
261     goto beach;
262   }
263
264   /* get the information from the caps */
265   if (!gst_structure_get_int (structure, "width", &width) ||
266       !gst_structure_get_int (structure, "height", &height))
267     goto beach;
268
269   if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
270           &par_n, &par_d)) {
271     par_d = par_n = 1;
272   }
273
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);
278
279   requested_ar =
280       (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom;
281
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);
287     goto beach;
288   } else if (requested_ar > incoming_ar) {
289     /* fix aspect ratio with cropping on top and bottom */
290     cropvalue =
291         ((((double) aspect_ratio_crop->ar_denom /
292                 (double) (aspect_ratio_crop->ar_num)) * ((double) par_n /
293                 (double) par_d) * width) - height) / 2;
294     if (cropvalue < 0) {
295       cropvalue *= -1;
296     }
297     if (cropvalue >= (height / 2))
298       goto crop_failed;
299     if (set_videocrop) {
300       gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0,
301           cropvalue, 0);
302     }
303     if (new_structure) {
304       *new_structure = gst_structure_copy (structure);
305       gst_structure_set (*new_structure,
306           "height", G_TYPE_INT, (int) (height - (cropvalue * 2)), NULL);
307     }
308   } else {
309     /* fix aspect ratio with cropping on left and right */
310     cropvalue =
311         ((((double) aspect_ratio_crop->ar_num /
312                 (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d /
313                 (double) par_n) * height) - width) / 2;
314     if (cropvalue < 0) {
315       cropvalue *= -1;
316     }
317     if (cropvalue >= (width / 2))
318       goto crop_failed;
319     if (set_videocrop) {
320       gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue,
321           0, cropvalue);
322     }
323     if (new_structure) {
324       *new_structure = gst_structure_copy (structure);
325       gst_structure_set (*new_structure,
326           "width", G_TYPE_INT, (int) (width - (cropvalue * 2)), NULL);
327     }
328   }
329
330   return;
331
332 crop_failed:
333   GST_WARNING_OBJECT (aspect_ratio_crop,
334       "can't crop to aspect ratio requested");
335   goto beach;
336 beach:
337   if (set_videocrop) {
338     gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0);
339   }
340
341   if (new_structure) {
342     *new_structure = gst_structure_copy (structure);
343   }
344 }
345
346 static GstCaps *
347 gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop,
348     GstCaps * caps)
349 {
350   GstCaps *transform;
351   gint size, i;
352
353   transform = gst_caps_new_empty ();
354
355   size = gst_caps_get_size (caps);
356
357   for (i = 0; i < size; i++) {
358     GstStructure *s;
359     GstStructure *trans_s;
360
361     s = gst_caps_get_structure (caps, i);
362
363     gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s,
364         FALSE);
365     gst_caps_append_structure (transform, trans_s);
366   }
367
368   return transform;
369 }
370
371 static GstCaps *
372 gst_aspect_ratio_crop_get_caps (GstPad * pad)
373 {
374   GstPad *peer;
375   GstAspectRatioCrop *aspect_ratio_crop;
376   GstCaps *return_caps;
377
378   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad));
379
380   g_mutex_lock (aspect_ratio_crop->crop_lock);
381
382   peer = gst_pad_get_peer (aspect_ratio_crop->sink);
383   if (peer == NULL) {
384     return_caps = gst_static_pad_template_get_caps (&src_template);
385     gst_caps_ref (return_caps);
386   } else {
387     GstCaps *peer_caps;
388
389     peer_caps = gst_pad_get_caps (peer);
390     return_caps =
391         gst_aspect_ratio_crop_transform_caps (aspect_ratio_crop, peer_caps);
392     gst_caps_unref (peer_caps);
393     gst_object_unref (peer);
394   }
395
396   g_mutex_unlock (aspect_ratio_crop->crop_lock);
397   gst_object_unref (aspect_ratio_crop);
398
399   return return_caps;
400 }
401
402 static void
403 gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id,
404     const GValue * value, GParamSpec * pspec)
405 {
406   GstAspectRatioCrop *aspect_ratio_crop;
407   gboolean recheck = FALSE;
408
409   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
410
411   GST_OBJECT_LOCK (aspect_ratio_crop);
412   switch (prop_id) {
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);
419       }
420       break;
421     default:
422       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
423       break;
424   }
425   GST_OBJECT_UNLOCK (aspect_ratio_crop);
426
427   if (recheck) {
428     gst_aspect_ratio_crop_set_caps (aspect_ratio_crop->sink,
429         GST_PAD_CAPS (aspect_ratio_crop->sink));
430   }
431 }
432
433 static void
434 gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id,
435     GValue * value, GParamSpec * pspec)
436 {
437   GstAspectRatioCrop *aspect_ratio_crop;
438
439   aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object);
440
441   GST_OBJECT_LOCK (aspect_ratio_crop);
442   switch (prop_id) {
443     case ARG_ASPECT_RATIO_CROP:
444       gst_value_set_fraction (value, aspect_ratio_crop->ar_num,
445           aspect_ratio_crop->ar_denom);
446       break;
447     default:
448       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449       break;
450   }
451   GST_OBJECT_UNLOCK (aspect_ratio_crop);
452 }