From 42e9f302df65ee02598f6efe92bde1541654a5b4 Mon Sep 17 00:00:00 2001 From: nanahi <130121847+na-na-hi@users.noreply.github.com> Date: Mon, 10 Jun 2024 01:03:02 -0400 Subject: [PATCH] sd_ass: fix sub scale with window when use_margins is enabled By default, libass scales subtitle with video size. When --sub-scale-with-window is enabled, mpv attempts to undo this scale and use window size instead. However, the current bahavior is incorrect when use_margins is enabled, because in this case libass uses the size of video as if it's "fit" to the window, instead of the displayed size. As a result, subtitle scale is broken when video-zoom is used in this case: when zooming out video, the subtitle is scaled up. Fix this by using the correct factor when use_margins is enabled. --- sub/sd_ass.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/sub/sd_ass.c b/sub/sd_ass.c index d04a40a1de..0333216379 100644 --- a/sub/sd_ass.c +++ b/sub/sd_ass.c @@ -470,6 +470,21 @@ static void decode(struct sd *sd, struct demux_packet *packet) } } +// Calculate the height used for scaling subtitle text size so --sub-scale-with-window +// can undo this scale and use frame size instead. The algorithm used is the following: +// - If use_margins is disabled, the text is scaled with the visual size of the video. +// - If use_margins is enabled, the text is scaled with the size of the video +// as if the video is resized to "fit" the size of the frame. +static float get_libass_scale_height(struct mp_osd_res *dim, bool use_margins) +{ + float vidw = dim->w - (dim->ml + dim->mr); + float vidh = dim->h - (dim->mt + dim->mb); + if (!use_margins || vidw < 1.0) + return vidh; + else + return MPMIN(dim->h, dim->w / vidw * vidh); +} + static void configure_ass(struct sd *sd, struct mp_osd_res *dim, bool converted, ASS_Track *track) { @@ -508,8 +523,7 @@ static void configure_ass(struct sd *sd, struct mp_osd_res *dim, set_font_scale = opts->sub_scale; } if (set_scale_with_window) { - int vidh = dim->h - (dim->mt + dim->mb); - set_font_scale *= dim->h / (float)MPMAX(vidh, 1); + set_font_scale *= dim->h / MPMAX(get_libass_scale_height(dim, set_use_margins), 1); } if (!set_scale_by_window) { double factor = dim->h / 720.0;