[Libva] [PATCH] vaapisink: Allow scaling to ignore aspect ratio

Simon Farnsworth simon.farnsworth at onelan.co.uk
Fri Sep 28 06:05:02 PDT 2012


Other GStreamer sinks, like xvimagesink, have a force-aspect-ratio property,
which allows you to say that you don't want the sink to respect aspect
ratio. Add the same property to vaapisink.

Signed-off-by: Simon Farnsworth <simon.farnsworth at onelan.co.uk>
---

We do have some problem video (mostly MPEG-2) where the video is coded as
4:3, but the content is 16:9 anamorphic. There's no way for the decoder to
detect this (you just have to know that it's wrong), but this option lets us
override the detected aspect ratio.

 gst/vaapi/gstvaapisink.c | 82 +++++++++++++++++++++++++++++++++---------------
 gst/vaapi/gstvaapisink.h |  1 +
 2 files changed, 57 insertions(+), 26 deletions(-)

diff --git a/gst/vaapi/gstvaapisink.c b/gst/vaapi/gstvaapisink.c
index 2c7f268..c8213b7 100644
--- a/gst/vaapi/gstvaapisink.c
+++ b/gst/vaapi/gstvaapisink.c
@@ -103,7 +103,8 @@ enum {
     PROP_USE_GLX,
     PROP_FULLSCREEN,
     PROP_SYNCHRONOUS,
-    PROP_USE_REFLECTION
+    PROP_USE_REFLECTION,
+    PROP_FORCE_ASPECT_RATIO
 };
 
 /* GstImplementsInterface interface */
@@ -306,34 +307,41 @@ gst_vaapisink_ensure_render_rect(GstVaapiSink *sink, guint width, guint height)
 
     GST_DEBUG("ensure render rect within %ux%u bounds", width, height);
 
-    gst_vaapi_display_get_pixel_aspect_ratio(
-        sink->display,
-        &display_par_n, &display_par_d
-    );
-    GST_DEBUG("display pixel-aspect-ratio %d/%d",
-              display_par_n, display_par_d);
-
-    success = gst_video_calculate_display_ratio(
-        &num, &den,
-        sink->video_width, sink->video_height,
-        sink->video_par_n, sink->video_par_d,
-        display_par_n, display_par_d
-    );
-    if (!success)
-        return FALSE;
-    GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
-              sink->video_width, sink->video_height, num, den);
+    if (sink->keep_aspect) {
+        gst_vaapi_display_get_pixel_aspect_ratio(
+            sink->display,
+            &display_par_n, &display_par_d
+            );
+        GST_DEBUG("display pixel-aspect-ratio %d/%d",
+                  display_par_n, display_par_d);
+
+        success = gst_video_calculate_display_ratio(
+            &num, &den,
+            sink->video_width, sink->video_height,
+            sink->video_par_n, sink->video_par_d,
+            display_par_n, display_par_d
+            );
+        if (!success)
+            return FALSE;
+        GST_DEBUG("video size %dx%d, calculated ratio %d/%d",
+                  sink->video_width, sink->video_height, num, den);
 
-    display_rect->width = gst_util_uint64_scale_int(height, num, den);
-    if (display_rect->width <= width) {
-        GST_DEBUG("keeping window height");
-        display_rect->height = height;
+        display_rect->width = gst_util_uint64_scale_int(height, num, den);
+        if (display_rect->width <= width) {
+            GST_DEBUG("keeping window height");
+            display_rect->height = height;
+        }
+        else {
+            GST_DEBUG("keeping window width");
+            display_rect->width  = width;
+            display_rect->height =
+                gst_util_uint64_scale_int(width, den, num);
+        }
     }
     else {
-        GST_DEBUG("keeping window width");
-        display_rect->width  = width;
-        display_rect->height =
-            gst_util_uint64_scale_int(width, den, num);
+        GST_DEBUG("force-aspect-ratio is false; distorting while scaling video");
+        display_rect->width = width;
+        display_rect->height = height;
     }
     GST_DEBUG("scaling video to %ux%u", display_rect->width, display_rect->height);
 
@@ -757,6 +765,9 @@ gst_vaapisink_set_property(
     case PROP_USE_REFLECTION:
         sink->use_reflection = g_value_get_boolean(value);
         break;
+    case PROP_FORCE_ASPECT_RATIO:
+        sink->keep_aspect = g_value_get_boolean(value);
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
         break;
@@ -786,6 +797,9 @@ gst_vaapisink_get_property(
     case PROP_USE_REFLECTION:
         g_value_set_boolean(value, sink->use_reflection);
         break;
+    case PROP_FORCE_ASPECT_RATIO:
+        g_value_set_boolean(value, sink->keep_aspect);
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
         break;
@@ -869,6 +883,21 @@ gst_vaapisink_class_init(GstVaapiSinkClass *klass)
                               "Toggles X display synchronous mode",
                               FALSE,
                               G_PARAM_READWRITE));
+
+    /**
+     * GstVaapiSink:force-aspect-ratio:
+     *
+     * When enabled, scaling respects video aspect ratio; when disabled, the
+     * video is distorted to fit the window.
+     */
+    g_object_class_install_property
+        (object_class,
+         PROP_FORCE_ASPECT_RATIO,
+         g_param_spec_boolean("force-aspect-ratio",
+                              "Force aspect ratio",
+                              "When enabled, scaling will respect original aspect ratio",
+                              FALSE,
+                              G_PARAM_READWRITE));
 }
 
 static void
@@ -889,4 +918,5 @@ gst_vaapisink_init(GstVaapiSink *sink)
     sink->synchronous    = FALSE;
     sink->use_glx        = USE_VAAPISINK_GLX;
     sink->use_reflection = FALSE;
+    sink->keep_aspect    = TRUE;
 }
diff --git a/gst/vaapi/gstvaapisink.h b/gst/vaapi/gstvaapisink.h
index b22a8d3..3be28bb 100644
--- a/gst/vaapi/gstvaapisink.h
+++ b/gst/vaapi/gstvaapisink.h
@@ -84,6 +84,7 @@ struct _GstVaapiSink {
     guint               synchronous     : 1;
     guint               use_glx         : 1;
     guint               use_reflection  : 1;
+    guint               keep_aspect     : 1;
 };
 
 struct _GstVaapiSinkClass {
-- 
1.7.11.2



More information about the Libva mailing list