From 5289b2f0a48eb500dc100ec8027955755181056b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Fri, 27 May 2022 19:38:02 +0200 Subject: [PATCH] plugin: Fix scaled subtitles position on anamorph video GStreamer does not take pixel aspect ratio into account for overlay rectangles. If we do so, we end up with subtitles that are displayed off-center. Behave like any other GStreamer sink on this matter. --- lib/gst/plugin/gstclapperimporter.c | 21 +++++++++++++++------ lib/gst/plugin/gstclapperimporter.h | 2 +- lib/gst/plugin/gstclapperpaintable.c | 6 +----- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/gst/plugin/gstclapperimporter.c b/lib/gst/plugin/gstclapperimporter.c index 2bc0f1d0..6caf333f 100644 --- a/lib/gst/plugin/gstclapperimporter.c +++ b/lib/gst/plugin/gstclapperimporter.c @@ -380,7 +380,7 @@ gst_clapper_importer_handle_context_query (GstClapperImporter *self, void gst_clapper_importer_snapshot (GstClapperImporter *self, GdkSnapshot *snapshot, - gdouble width, gdouble height, gfloat scale_x, gfloat scale_y) + gdouble width, gdouble height) { guint i; gboolean buffer_changed; @@ -424,12 +424,21 @@ gst_clapper_importer_snapshot (GstClapperImporter *self, GdkSnapshot *snapshot, if (G_LIKELY (self->texture)) { gtk_snapshot_append_texture (snapshot, self->texture, &GRAPHENE_RECT_INIT (0, 0, width, height)); - for (i = 0; i < self->overlays->len; i++) { - GstClapperGdkOverlay *overlay = g_ptr_array_index (self->overlays, i); + if (self->overlays->len > 0) { + gfloat scale_x, scale_y; - gtk_snapshot_append_texture (snapshot, overlay->texture, - &GRAPHENE_RECT_INIT (overlay->x * scale_x, overlay->y * scale_y, - overlay->width * scale_x, overlay->height * scale_y)); + /* FIXME: GStreamer scales subtitles without considering pixel aspect ratio. + * See: https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/issues/20 */ + scale_x = (gfloat) width / GST_VIDEO_INFO_WIDTH (&self->v_info); + scale_y = (gfloat) height / GST_VIDEO_INFO_HEIGHT (&self->v_info); + + for (i = 0; i < self->overlays->len; i++) { + GstClapperGdkOverlay *overlay = g_ptr_array_index (self->overlays, i); + + gtk_snapshot_append_texture (snapshot, overlay->texture, + &GRAPHENE_RECT_INIT (overlay->x * scale_x, overlay->y * scale_y, + overlay->width * scale_x, overlay->height * scale_y)); + } } } else { GST_ERROR_OBJECT (self, "Failed import of %" GST_PTR_FORMAT, self->buffer); diff --git a/lib/gst/plugin/gstclapperimporter.h b/lib/gst/plugin/gstclapperimporter.h index be462de8..8ba21e74 100644 --- a/lib/gst/plugin/gstclapperimporter.h +++ b/lib/gst/plugin/gstclapperimporter.h @@ -99,6 +99,6 @@ void gst_clapper_importer_add_allocation_metas (GstClapperImporter void gst_clapper_importer_set_caps (GstClapperImporter *importer, GstCaps *caps); void gst_clapper_importer_set_buffer (GstClapperImporter *importer, GstBuffer *buffer); -void gst_clapper_importer_snapshot (GstClapperImporter *importer, GdkSnapshot *snapshot, gdouble width, gdouble height, gfloat scale_x, gfloat scale_y); +void gst_clapper_importer_snapshot (GstClapperImporter *importer, GdkSnapshot *snapshot, gdouble width, gdouble height); G_END_DECLS diff --git a/lib/gst/plugin/gstclapperpaintable.c b/lib/gst/plugin/gstclapperpaintable.c index ebd756a5..5b2a93c3 100644 --- a/lib/gst/plugin/gstclapperpaintable.c +++ b/lib/gst/plugin/gstclapperpaintable.c @@ -59,7 +59,6 @@ gst_clapper_paintable_init (GstClapperPaintable *self) self->par_n = DEFAULT_PAR_N; self->par_d = DEFAULT_PAR_D; - self->pixel_aspect = ((gdouble) self->par_d / self->par_n); g_mutex_init (&self->lock); g_mutex_init (&self->importer_lock); @@ -159,8 +158,6 @@ invalidate_paintable_size_internal (GstClapperPaintable *self) display_ratio_num = self->display_ratio_num; display_ratio_den = self->display_ratio_den; - self->pixel_aspect = ((gdouble) self->par_d / self->par_n); - GST_CLAPPER_PAINTABLE_UNLOCK (self); if (video_height % display_ratio_den == 0) { @@ -361,8 +358,7 @@ gst_clapper_paintable_snapshot_internal (GstClapperPaintable *self, GST_CLAPPER_PAINTABLE_IMPORTER_LOCK (self); if (self->importer) { - gst_clapper_importer_snapshot (self->importer, snapshot, width, height, - scale_x * self->pixel_aspect, scale_y); + gst_clapper_importer_snapshot (self->importer, snapshot, width, height); } else { GST_LOG_OBJECT (self, "No texture importer, drawing black"); gtk_snapshot_append_color (snapshot, &self->bg, &GRAPHENE_RECT_INIT (0, 0, width, height));