tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / ext / gdk_pixbuf / pixbufscale.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2004> Jan Schmidt <thaytan@mad.scientist.com>
4  * Copyright (C) <2004> Tim-Philipp Mueller <t.i.m@orange.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <pixbufscale.h>
27 #include <gst/video/video.h>
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29
30 #define ROUND_UP_2(x)  (((x)+1)&~1)
31 #define ROUND_UP_4(x)  (((x)+3)&~3)
32 #define ROUND_UP_8(x)  (((x)+7)&~7)
33
34 /* These match the ones gstffmpegcolorspace uses (Tim) */
35 #define GST_RGB24_ROWSTRIDE(width)    (ROUND_UP_4 ((width)*3))
36 #define GST_RGB24_SIZE(width,height)  ((height)*GST_RGB24_ROWSTRIDE(width))
37
38
39 GST_DEBUG_CATEGORY_STATIC (pixbufscale_debug);
40 #define GST_CAT_DEFAULT pixbufscale_debug
41
42 /* GstPixbufScale signals and args */
43 enum
44 {
45   /* FILL ME */
46   LAST_SIGNAL
47 };
48
49 enum
50 {
51   ARG_0,
52   ARG_METHOD
53       /* FILL ME */
54 };
55
56 static GstStaticPadTemplate gst_pixbufscale_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
61     );
62
63 static GstStaticPadTemplate gst_pixbufscale_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
68     );
69
70 #define GST_TYPE_PIXBUFSCALE_METHOD (gst_pixbufscale_method_get_type())
71 static GType
72 gst_pixbufscale_method_get_type (void)
73 {
74   static GType pixbufscale_method_type = 0;
75   static const GEnumValue pixbufscale_methods[] = {
76     {GST_PIXBUFSCALE_NEAREST, "0", "Nearest Neighbour"},
77     {GST_PIXBUFSCALE_TILES, "1", "Tiles"},
78     {GST_PIXBUFSCALE_BILINEAR, "2", "Bilinear"},
79     {GST_PIXBUFSCALE_HYPER, "3", "Hyper"},
80     {0, NULL, NULL},
81   };
82
83   if (!pixbufscale_method_type) {
84     pixbufscale_method_type =
85         g_enum_register_static ("GstPixbufScaleMethod", pixbufscale_methods);
86   }
87   return pixbufscale_method_type;
88 }
89
90 static void gst_pixbufscale_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec);
92 static void gst_pixbufscale_get_property (GObject * object, guint prop_id,
93     GValue * value, GParamSpec * pspec);
94
95 static GstCaps *gst_pixbufscale_transform_caps (GstBaseTransform * trans,
96     GstPadDirection direction, GstCaps * caps);
97 static gboolean gst_pixbufscale_set_caps (GstBaseTransform * trans,
98     GstCaps * in, GstCaps * out);
99 static gboolean gst_pixbufscale_get_unit_size (GstBaseTransform * trans,
100     GstCaps * caps, guint * size);
101 static void gst_pixbufscale_fixate_caps (GstBaseTransform * base,
102     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
103 static GstFlowReturn gst_pixbufscale_transform (GstBaseTransform * trans,
104     GstBuffer * in, GstBuffer * out);
105 static gboolean gst_pixbufscale_handle_src_event (GstPad * pad,
106     GstEvent * event);
107
108
109 static gboolean parse_caps (GstCaps * caps, gint * width, gint * height);
110
111 GST_BOILERPLATE (GstPixbufScale, gst_pixbufscale, GstBaseTransform,
112     GST_TYPE_BASE_TRANSFORM);
113
114 static void
115 gst_pixbufscale_base_init (gpointer g_class)
116 {
117   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
118
119   gst_element_class_set_details_simple (element_class, "GdkPixbuf image scaler",
120       "Filter/Effect/Video", "Resizes video",
121       "Jan Schmidt <thaytan@mad.scientist.com>, "
122       "Wim Taymans <wim.taymans@chello.be>, "
123       "Renato Filho <renato.filho@indt.org.br>");
124
125   gst_element_class_add_static_pad_template (element_class,
126       &gst_pixbufscale_src_template);
127   gst_element_class_add_static_pad_template (element_class,
128       &gst_pixbufscale_sink_template);
129 }
130
131 static void
132 gst_pixbufscale_class_init (GstPixbufScaleClass * klass)
133 {
134   GObjectClass *gobject_class;
135   GstBaseTransformClass *trans_class;
136
137   gobject_class = (GObjectClass *) klass;
138   trans_class = (GstBaseTransformClass *) klass;
139
140   gobject_class->set_property = gst_pixbufscale_set_property;
141   gobject_class->get_property = gst_pixbufscale_get_property;
142
143   g_object_class_install_property (gobject_class,
144       ARG_METHOD,
145       g_param_spec_enum ("method", "method", "method",
146           GST_TYPE_PIXBUFSCALE_METHOD, GST_PIXBUFSCALE_BILINEAR,
147           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148
149   trans_class->transform_caps =
150       GST_DEBUG_FUNCPTR (gst_pixbufscale_transform_caps);
151   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_pixbufscale_set_caps);
152   trans_class->get_unit_size =
153       GST_DEBUG_FUNCPTR (gst_pixbufscale_get_unit_size);
154   trans_class->transform = GST_DEBUG_FUNCPTR (gst_pixbufscale_transform);
155   trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_pixbufscale_fixate_caps);
156   trans_class->passthrough_on_same_caps = TRUE;
157
158   parent_class = g_type_class_peek_parent (klass);
159 }
160
161 static void
162 gst_pixbufscale_init (GstPixbufScale * pixbufscale,
163     GstPixbufScaleClass * kclass)
164 {
165   GstBaseTransform *trans;
166
167   GST_DEBUG_OBJECT (pixbufscale, "_init");
168   trans = GST_BASE_TRANSFORM (pixbufscale);
169
170   gst_pad_set_event_function (trans->srcpad, gst_pixbufscale_handle_src_event);
171
172   pixbufscale->method = GST_PIXBUFSCALE_TILES;
173   pixbufscale->gdk_method = GDK_INTERP_TILES;
174 }
175
176 static void
177 gst_pixbufscale_set_property (GObject * object, guint prop_id,
178     const GValue * value, GParamSpec * pspec)
179 {
180   GstPixbufScale *src;
181
182   g_return_if_fail (GST_IS_PIXBUFSCALE (object));
183   src = GST_PIXBUFSCALE (object);
184
185   switch (prop_id) {
186     case ARG_METHOD:
187       src->method = g_value_get_enum (value);
188       switch (src->method) {
189         case GST_PIXBUFSCALE_NEAREST:
190           src->gdk_method = GDK_INTERP_NEAREST;
191           break;
192         case GST_PIXBUFSCALE_TILES:
193           src->gdk_method = GDK_INTERP_TILES;
194           break;
195         case GST_PIXBUFSCALE_BILINEAR:
196           src->gdk_method = GDK_INTERP_BILINEAR;
197           break;
198         case GST_PIXBUFSCALE_HYPER:
199           src->gdk_method = GDK_INTERP_HYPER;
200           break;
201       }
202       break;
203     default:
204       break;
205   }
206 }
207
208 static void
209 gst_pixbufscale_get_property (GObject * object, guint prop_id, GValue * value,
210     GParamSpec * pspec)
211 {
212   GstPixbufScale *src;
213
214   g_return_if_fail (GST_IS_PIXBUFSCALE (object));
215   src = GST_PIXBUFSCALE (object);
216
217   switch (prop_id) {
218     case ARG_METHOD:
219       g_value_set_enum (value, src->method);
220       break;
221     default:
222       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223       break;
224   }
225 }
226
227
228 static GstCaps *
229 gst_pixbufscale_transform_caps (GstBaseTransform * trans,
230     GstPadDirection direction, GstCaps * caps)
231 {
232   GstCaps *ret;
233   int i;
234
235   ret = gst_caps_copy (caps);
236
237   for (i = 0; i < gst_caps_get_size (ret); i++) {
238     GstStructure *structure = gst_caps_get_structure (ret, i);
239
240     gst_structure_set (structure,
241         "width", GST_TYPE_INT_RANGE, 16, 4096,
242         "height", GST_TYPE_INT_RANGE, 16, 4096, NULL);
243     gst_structure_remove_field (structure, "pixel-aspect-ratio");
244   }
245
246   GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, ret);
247   return ret;
248 }
249
250 static gboolean
251 parse_caps (GstCaps * caps, gint * width, gint * height)
252 {
253   gboolean ret;
254   GstStructure *structure;
255
256   structure = gst_caps_get_structure (caps, 0);
257   ret = gst_structure_get_int (structure, "width", width);
258   ret &= gst_structure_get_int (structure, "height", height);
259
260   return ret;
261 }
262
263 static gboolean
264 gst_pixbufscale_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
265 {
266   GstPixbufScale *pixbufscale;
267   gboolean ret;
268
269   pixbufscale = GST_PIXBUFSCALE (trans);
270   ret = parse_caps (in, &pixbufscale->from_width, &pixbufscale->from_height);
271   ret &= parse_caps (out, &pixbufscale->to_width, &pixbufscale->to_height);
272   if (!ret)
273     goto done;
274
275   pixbufscale->from_stride = GST_ROUND_UP_4 (pixbufscale->from_width * 3);
276   pixbufscale->from_buf_size =
277       pixbufscale->from_stride * pixbufscale->from_height;
278
279   pixbufscale->to_stride = GST_ROUND_UP_4 (pixbufscale->to_width * 3);
280   pixbufscale->to_buf_size = pixbufscale->to_stride * pixbufscale->to_height;
281
282   GST_DEBUG_OBJECT (pixbufscale, "from=%dx%d, size %d -> to=%dx%d, size %d",
283       pixbufscale->from_width, pixbufscale->from_height,
284       pixbufscale->from_buf_size, pixbufscale->to_width, pixbufscale->to_height,
285       pixbufscale->to_buf_size);
286
287 done:
288   return ret;
289 }
290
291
292 static gboolean
293 gst_pixbufscale_get_unit_size (GstBaseTransform * trans,
294     GstCaps * caps, guint * size)
295 {
296   gint width, height;
297
298   g_assert (size);
299
300   if (!parse_caps (caps, &width, &height))
301     return FALSE;
302
303   *size = GST_ROUND_UP_4 (width * 3) * height;
304
305   return TRUE;
306 }
307
308 static void
309 gst_pixbufscale_fixate_caps (GstBaseTransform * base, GstPadDirection direction,
310     GstCaps * caps, GstCaps * othercaps)
311 {
312   GstStructure *ins, *outs;
313   const GValue *from_par, *to_par;
314
315   g_return_if_fail (gst_caps_is_fixed (caps));
316
317   GST_DEBUG_OBJECT (base, "trying to fixate othercaps %" GST_PTR_FORMAT
318       " based on caps %" GST_PTR_FORMAT, othercaps, caps);
319
320   ins = gst_caps_get_structure (caps, 0);
321   outs = gst_caps_get_structure (othercaps, 0);
322
323   from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
324   to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
325
326   if (from_par && to_par) {
327     GValue to_ratio = { 0, };   /* w/h of output video */
328     int from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
329     int count = 0, w = 0, h = 0, num, den;
330
331     /* if both width and height are already fixed, we can't do anything
332      *      * about it anymore */
333     if (gst_structure_get_int (outs, "width", &w))
334       ++count;
335     if (gst_structure_get_int (outs, "height", &h))
336       ++count;
337     if (count == 2) {
338       GST_DEBUG_OBJECT (base, "dimensions already set to %dx%d, not fixating",
339           w, h);
340       return;
341     }
342
343     gst_structure_get_int (ins, "width", &from_w);
344     gst_structure_get_int (ins, "height", &from_h);
345     from_par_n = gst_value_get_fraction_numerator (from_par);
346     from_par_d = gst_value_get_fraction_denominator (from_par);
347     to_par_n = gst_value_get_fraction_numerator (to_par);
348     to_par_d = gst_value_get_fraction_denominator (to_par);
349
350     g_value_init (&to_ratio, GST_TYPE_FRACTION);
351     gst_value_set_fraction (&to_ratio, from_w * from_par_n * to_par_d,
352         from_h * from_par_d * to_par_n);
353     num = gst_value_get_fraction_numerator (&to_ratio);
354     den = gst_value_get_fraction_denominator (&to_ratio);
355     GST_DEBUG_OBJECT (base,
356         "scaling input with %dx%d and PAR %d/%d to output PAR %d/%d",
357         from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d);
358     GST_DEBUG_OBJECT (base,
359         "resulting output should respect ratio of %d/%d", num, den);
360
361     /* now find a width x height that respects this display ratio.
362      *      * prefer those that have one of w/h the same as the incoming video
363      *           * using wd / hd = num / den */
364
365     /* start with same height, because of interlaced video */
366     /* check hd / den is an integer scale factor, and scale wd with the PAR */
367     if (from_h % den == 0) {
368       GST_DEBUG_OBJECT (base, "keeping video height");
369       h = from_h;
370       w = h * num / den;
371     } else if (from_w % num == 0) {
372       GST_DEBUG_OBJECT (base, "keeping video width");
373       w = from_w;
374       h = w * den / num;
375     } else {
376       GST_DEBUG_OBJECT (base, "approximating but keeping video height");
377       h = from_h;
378       w = h * num / den;
379     }
380     GST_DEBUG_OBJECT (base, "scaling to %dx%d", w, h);
381     /* now fixate */
382     gst_structure_fixate_field_nearest_int (outs, "width", w);
383     gst_structure_fixate_field_nearest_int (outs, "height", h);
384   } else {
385     gint width, height;
386
387     if (gst_structure_get_int (ins, "width", &width)) {
388       if (gst_structure_has_field (outs, "width")) {
389         gst_structure_fixate_field_nearest_int (outs, "width", width);
390       }
391     }
392     if (gst_structure_get_int (ins, "height", &height)) {
393       if (gst_structure_has_field (outs, "height")) {
394         gst_structure_fixate_field_nearest_int (outs, "height", height);
395       }
396     }
397   }
398
399   GST_DEBUG_OBJECT (base, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
400 }
401
402 static GstFlowReturn
403 gst_pixbufscale_transform (GstBaseTransform * trans,
404     GstBuffer * in, GstBuffer * out)
405 {
406   GstPixbufScale *scale;
407   GdkPixbuf *src_pixbuf, *dest_pixbuf;
408
409   scale = GST_PIXBUFSCALE (trans);
410
411   src_pixbuf =
412       gdk_pixbuf_new_from_data (GST_BUFFER_DATA (in), GDK_COLORSPACE_RGB, FALSE,
413       8, scale->from_width, scale->from_height,
414       GST_RGB24_ROWSTRIDE (scale->from_width), NULL, NULL);
415
416   dest_pixbuf =
417       gdk_pixbuf_new_from_data (GST_BUFFER_DATA (out), GDK_COLORSPACE_RGB,
418       FALSE, 8, scale->to_width, scale->to_height,
419       GST_RGB24_ROWSTRIDE (scale->to_width), NULL, NULL);
420
421   gdk_pixbuf_scale (src_pixbuf, dest_pixbuf, 0, 0,
422       scale->to_width,
423       scale->to_height, 0, 0,
424       (double) scale->to_width / scale->from_width,
425       (double) scale->to_height / scale->from_height, scale->gdk_method);
426
427   g_object_unref (src_pixbuf);
428   g_object_unref (dest_pixbuf);
429
430   return GST_FLOW_OK;
431 }
432
433 static gboolean
434 gst_pixbufscale_handle_src_event (GstPad * pad, GstEvent * event)
435 {
436   GstPixbufScale *pixbufscale;
437   gboolean ret;
438   double a;
439   GstStructure *structure;
440
441   pixbufscale = GST_PIXBUFSCALE (gst_pad_get_parent (pad));
442
443   GST_DEBUG_OBJECT (pixbufscale, "handling %s event",
444       GST_EVENT_TYPE_NAME (event));
445
446   switch (GST_EVENT_TYPE (event)) {
447     case GST_EVENT_NAVIGATION:
448       event =
449           GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
450
451       structure = (GstStructure *) gst_event_get_structure (event);
452       if (gst_structure_get_double (structure, "pointer_x", &a)) {
453         gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
454             a * pixbufscale->from_width / pixbufscale->to_width, NULL);
455       }
456       if (gst_structure_get_double (structure, "pointer_y", &a)) {
457         gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
458             a * pixbufscale->from_height / pixbufscale->to_height, NULL);
459       }
460       break;
461     default:
462       break;
463   }
464
465   ret = gst_pad_event_default (pad, event);
466
467   gst_object_unref (pixbufscale);
468
469   return ret;
470 }
471
472 gboolean
473 pixbufscale_init (GstPlugin * plugin)
474 {
475   if (!gst_element_register (plugin, "gdkpixbufscale", GST_RANK_NONE,
476           GST_TYPE_PIXBUFSCALE))
477     return FALSE;
478
479   GST_DEBUG_CATEGORY_INIT (pixbufscale_debug, "gdkpixbufscale", 0,
480       "pixbufscale element");
481
482   return TRUE;
483 }