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.
This commit is contained in:
Rafał Dzięgiel
2022-05-27 19:38:02 +02:00
parent 87b3fbc15a
commit 5289b2f0a4
3 changed files with 17 additions and 12 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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));