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