gtk: implement pixel and display aspect ratio handling
[platform/upstream/gst-plugins-good.git] / ext / gtk / gtkgstwidget.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include "gtkgstwidget.h"
28 #include <gst/video/video.h>
29
30 /**
31  * SECTION:gtkgstwidget
32  * @short_description: a #GtkWidget that renders GStreamer video #GstBuffers
33  * @see_also: #GtkDrawingArea, #GstBuffer
34  *
35  * #GtkGstWidget is an #GtkWidget that renders GStreamer video buffers.
36  */
37
38 G_DEFINE_TYPE (GtkGstWidget, gtk_gst_widget, GTK_TYPE_DRAWING_AREA);
39
40 #define GTK_GST_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41     GTK_TYPE_GST_WIDGET, GtkGstWidgetPrivate))
42
43 #define DEFAULT_FORCE_ASPECT_RATIO  TRUE
44 #define DEFAULT_PAR_N               0
45 #define DEFAULT_PAR_D               1
46
47 enum
48 {
49   PROP_0,
50   PROP_FORCE_ASPECT_RATIO,
51   PROP_PIXEL_ASPECT_RATIO,
52 };
53
54 struct _GtkGstWidgetPrivate
55 {
56   GMutex lock;
57
58   /* properties */
59   gboolean force_aspect_ratio;
60   gint par_n, par_d;
61
62   gint display_width;
63   gint display_height;
64
65   gboolean negotiated;
66   GstBuffer *buffer;
67   GstCaps *caps;
68   GstVideoInfo v_info;
69 };
70
71 static void
72 gtk_gst_widget_get_preferred_width (GtkWidget * widget, gint * min,
73     gint * natural)
74 {
75   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
76   gint video_width = GST_VIDEO_INFO_WIDTH (&gst_widget->priv->v_info);
77
78   if (!gst_widget->priv->negotiated)
79     video_width = 10;
80
81   if (min)
82     *min = 1;
83   if (natural)
84     *natural = video_width;
85 }
86
87 static void
88 gtk_gst_widget_get_preferred_height (GtkWidget * widget, gint * min,
89     gint * natural)
90 {
91   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
92   gint video_height = GST_VIDEO_INFO_HEIGHT (&gst_widget->priv->v_info);
93
94   if (!gst_widget->priv->negotiated)
95     video_height = 10;
96
97   if (min)
98     *min = 1;
99   if (natural)
100     *natural = video_height;
101 }
102
103 static gboolean
104 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
105 {
106   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
107   guint widget_width, widget_height;
108   cairo_surface_t *surface;
109   GstVideoFrame frame;
110
111   widget_width = gtk_widget_get_allocated_width (widget);
112   widget_height = gtk_widget_get_allocated_height (widget);
113
114   g_mutex_lock (&gst_widget->priv->lock);
115
116   /* failed to map the video frame */
117   if (gst_widget->priv->negotiated && gst_widget->priv->buffer
118       && gst_video_frame_map (&frame, &gst_widget->priv->v_info,
119           gst_widget->priv->buffer, GST_MAP_READ)) {
120     gdouble scale_x = (gdouble) widget_width / gst_widget->priv->display_width;
121     gdouble scale_y =
122         (gdouble) widget_height / gst_widget->priv->display_height;
123     GstVideoRectangle result;
124
125     gst_widget->priv->v_info = frame.info;
126
127     surface = cairo_image_surface_create_for_data (frame.data[0],
128         CAIRO_FORMAT_ARGB32, frame.info.width, frame.info.height,
129         frame.info.stride[0]);
130
131     if (gst_widget->priv->force_aspect_ratio) {
132       GstVideoRectangle src, dst;
133
134       src.x = 0;
135       src.y = 0;
136       src.w = gst_widget->priv->display_width;
137       src.h = gst_widget->priv->display_height;
138
139       dst.x = 0;
140       dst.y = 0;
141       dst.w = widget_width;
142       dst.h = widget_height;
143
144       gst_video_sink_center_rect (src, dst, &result, TRUE);
145
146       scale_x = scale_y = MIN (scale_x, scale_y);
147     } else {
148       result.x = 0;
149       result.y = 0;
150       result.w = widget_width;
151       result.h = widget_height;
152     }
153
154     scale_x *=
155         (gdouble) gst_widget->priv->display_width / (gdouble) frame.info.width;
156     scale_y *=
157         (gdouble) gst_widget->priv->display_height /
158         (gdouble) frame.info.height;
159
160     cairo_translate (cr, result.x, result.y);
161     cairo_scale (cr, scale_x, scale_y);
162     cairo_rectangle (cr, 0, 0, result.w, result.h);
163     cairo_set_source_surface (cr, surface, 0, 0);
164     cairo_paint (cr);
165
166     cairo_surface_destroy (surface);
167
168     gst_video_frame_unmap (&frame);
169   } else {
170     GdkRGBA color;
171
172     gtk_style_context_get_color (gtk_widget_get_style_context (widget), 0,
173         &color);
174     gdk_cairo_set_source_rgba (cr, &color);
175     cairo_rectangle (cr, 0, 0, widget_width, widget_height);
176     cairo_fill (cr);
177   }
178
179   g_mutex_unlock (&gst_widget->priv->lock);
180   return FALSE;
181 }
182
183 static void
184 gtk_gst_widget_finalize (GObject * object)
185 {
186   GtkGstWidget *widget = GTK_GST_WIDGET_CAST (object);
187
188   gst_buffer_replace (&widget->priv->buffer, NULL);
189   g_mutex_clear (&widget->priv->lock);
190
191   G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
192 }
193
194 static void
195 gtk_gst_widget_set_property (GObject * object, guint prop_id,
196     const GValue * value, GParamSpec * pspec)
197 {
198   GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
199
200   switch (prop_id) {
201     case PROP_FORCE_ASPECT_RATIO:
202       gtk_widget->priv->force_aspect_ratio = g_value_get_boolean (value);
203       break;
204     case PROP_PIXEL_ASPECT_RATIO:
205       gtk_widget->priv->par_n = gst_value_get_fraction_numerator (value);
206       gtk_widget->priv->par_d = gst_value_get_fraction_denominator (value);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210       break;
211   }
212 }
213
214 static void
215 gtk_gst_widget_get_property (GObject * object, guint prop_id, GValue * value,
216     GParamSpec * pspec)
217 {
218   GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
219
220   switch (prop_id) {
221     case PROP_FORCE_ASPECT_RATIO:
222       g_value_set_boolean (value, gtk_widget->priv->force_aspect_ratio);
223       break;
224     case PROP_PIXEL_ASPECT_RATIO:
225       gst_value_set_fraction (value, gtk_widget->priv->par_n,
226           gtk_widget->priv->par_d);
227       break;
228     default:
229       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       break;
231   }
232 }
233
234 static void
235 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
236 {
237   GObjectClass *gobject_klass = (GObjectClass *) klass;
238   GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
239
240   g_type_class_add_private (klass, sizeof (GtkGstWidgetPrivate));
241
242   gobject_klass->set_property = gtk_gst_widget_set_property;
243   gobject_klass->get_property = gtk_gst_widget_get_property;
244   gobject_klass->finalize = gtk_gst_widget_finalize;
245
246   g_object_class_install_property (gobject_klass, PROP_FORCE_ASPECT_RATIO,
247       g_param_spec_boolean ("force-aspect-ratio",
248           "Force aspect ratio",
249           "When enabled, scaling will respect original aspect ratio",
250           DEFAULT_FORCE_ASPECT_RATIO,
251           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252
253   g_object_class_install_property (gobject_klass, PROP_PIXEL_ASPECT_RATIO,
254       gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
255           "The pixel aspect ratio of the device", DEFAULT_PAR_N, DEFAULT_PAR_D,
256           G_MAXINT, 1, 1, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257
258   widget_klass->draw = gtk_gst_widget_draw;
259   widget_klass->get_preferred_width = gtk_gst_widget_get_preferred_width;
260   widget_klass->get_preferred_height = gtk_gst_widget_get_preferred_height;
261 }
262
263 static void
264 gtk_gst_widget_init (GtkGstWidget * widget)
265 {
266   widget->priv = GTK_GST_WIDGET_GET_PRIVATE (widget);
267
268   widget->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
269   widget->priv->par_n = DEFAULT_PAR_N;
270   widget->priv->par_d = DEFAULT_PAR_D;
271
272   g_mutex_init (&widget->priv->lock);
273 }
274
275 GtkWidget *
276 gtk_gst_widget_new (void)
277 {
278   return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
279 }
280
281 static gboolean
282 _queue_draw (GtkGstWidget * widget)
283 {
284   gtk_widget_queue_draw (GTK_WIDGET (widget));
285
286   return G_SOURCE_REMOVE;
287 }
288
289 void
290 gtk_gst_widget_set_buffer (GtkGstWidget * widget, GstBuffer * buffer)
291 {
292   GMainContext *main_context = g_main_context_default ();
293
294   g_return_if_fail (GTK_IS_GST_WIDGET (widget));
295   g_return_if_fail (widget->priv->negotiated);
296
297   g_mutex_lock (&widget->priv->lock);
298
299   gst_buffer_replace (&widget->priv->buffer, buffer);
300
301   g_mutex_unlock (&widget->priv->lock);
302
303   g_main_context_invoke (main_context, (GSourceFunc) _queue_draw, widget);
304 }
305
306 static gboolean
307 _queue_resize (GtkGstWidget * widget)
308 {
309   gtk_widget_queue_resize (GTK_WIDGET (widget));
310
311   return G_SOURCE_REMOVE;
312 }
313
314 static gboolean
315 _calculate_par (GtkGstWidget * widget, GstVideoInfo * info)
316 {
317   gboolean ok;
318   gint width, height;
319   gint par_n, par_d;
320   gint display_par_n, display_par_d;
321   guint display_ratio_num, display_ratio_den;
322
323   width = GST_VIDEO_INFO_WIDTH (info);
324   height = GST_VIDEO_INFO_HEIGHT (info);
325
326   par_n = GST_VIDEO_INFO_PAR_N (info);
327   par_d = GST_VIDEO_INFO_PAR_D (info);
328
329   if (!par_n)
330     par_n = 1;
331
332   /* get display's PAR */
333   if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
334     display_par_n = widget->priv->par_n;
335     display_par_d = widget->priv->par_d;
336   } else {
337     display_par_n = 1;
338     display_par_d = 1;
339   }
340
341   ok = gst_video_calculate_display_ratio (&display_ratio_num,
342       &display_ratio_den, width, height, par_n, par_d, display_par_n,
343       display_par_d);
344
345   if (!ok)
346     return FALSE;
347
348   GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
349
350   if (height % display_ratio_den == 0) {
351     GST_DEBUG ("keeping video height");
352     widget->priv->display_width = (guint)
353         gst_util_uint64_scale_int (height, display_ratio_num,
354         display_ratio_den);
355     widget->priv->display_height = height;
356   } else if (width % display_ratio_num == 0) {
357     GST_DEBUG ("keeping video width");
358     widget->priv->display_width = width;
359     widget->priv->display_height = (guint)
360         gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
361   } else {
362     GST_DEBUG ("approximating while keeping video height");
363     widget->priv->display_width = (guint)
364         gst_util_uint64_scale_int (height, display_ratio_num,
365         display_ratio_den);
366     widget->priv->display_height = height;
367   }
368   GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
369       widget->priv->display_height);
370
371   return TRUE;
372 }
373
374 gboolean
375 gtk_gst_widget_set_caps (GtkGstWidget * widget, GstCaps * caps)
376 {
377   GMainContext *main_context = g_main_context_default ();
378   GstVideoInfo v_info;
379
380   g_return_val_if_fail (GTK_IS_GST_WIDGET (widget), FALSE);
381   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
382   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
383
384   if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
385     return TRUE;
386
387   if (!gst_video_info_from_caps (&v_info, caps))
388     return FALSE;
389
390   /* FIXME: support other formats */
391   g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
392       GST_VIDEO_FORMAT_BGRA, FALSE);
393
394   g_mutex_lock (&widget->priv->lock);
395
396   if (!_calculate_par (widget, &v_info)) {
397     g_mutex_unlock (&widget->priv->lock);
398     return FALSE;
399   }
400
401   gst_caps_replace (&widget->priv->caps, caps);
402   widget->priv->v_info = v_info;
403   widget->priv->negotiated = TRUE;
404
405   g_mutex_unlock (&widget->priv->lock);
406
407   gtk_widget_queue_resize (GTK_WIDGET (widget));
408
409   g_main_context_invoke (main_context, (GSourceFunc) _queue_resize, widget);
410
411   return TRUE;
412 }