[FFmpeg-devel] [PATCH 2/2] avcodec/srtdec: attempt to correct SubRip positioning

Clément Bœsch u at pkh.me
Sun Feb 8 12:40:19 CET 2015


The positioning was completely wrong. First, the coordinates are
expressed in ASS playback resolution (which is by default 384x288).
Secondly, the coordinates define a drawing rectangle, not a moving area.
The previous code was making subtitles move from a random position to
another random position.

Here we rescale assuming the video resolution is a DVD one (720x480). We
can't really do anything better so far, but since these positioning are
often from a DVD rip we can't consider them relatively safe.
---
 libavcodec/srtdec.c    | 20 ++++++++++++++++----
 tests/ref/fate/sub-srt |  2 +-
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/libavcodec/srtdec.c b/libavcodec/srtdec.c
index 6d0c9bb..1f3e896 100644
--- a/libavcodec/srtdec.c
+++ b/libavcodec/srtdec.c
@@ -66,10 +66,22 @@ static void srt_to_ass(AVCodecContext *avctx, AVBPrint *dst,
     strcpy(stack[0].param[PARAM_FACE],  "{\\fn}");
 
     if (x1 >= 0 && y1 >= 0) {
-        if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1))
-            av_bprintf(dst, "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2);
-        else
-            av_bprintf(dst, "{\\an1}{\\pos(%d,%d)}", x1, y1);
+        /* XXX: here we rescale coordinate assuming they are in DVD resolution
+         * (720x480) since we don't have anything better */
+
+        if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1) && x2 >= x1 && y2 >= y1) {
+            /* text rectangle defined, write the text at the center of the rectangle */
+            const int cx = x1 + (x2 - x1)/2;
+            const int cy = y1 + (y2 - y1)/2;
+            const int scaled_x = cx * ASS_DEFAULT_PLAYRESX / 720;
+            const int scaled_y = cy * ASS_DEFAULT_PLAYRESY / 480;
+            av_bprintf(dst, "{\\an5}{\\pos(%d,%d)}", scaled_x, scaled_y);
+        } else {
+            /* only the top left corner, assume the text starts in that corner */
+            const int scaled_x = x1 * ASS_DEFAULT_PLAYRESX / 720;
+            const int scaled_y = y1 * ASS_DEFAULT_PLAYRESY / 480;
+            av_bprintf(dst, "{\\an1}{\\pos(%d,%d)}", scaled_x, scaled_y);
+        }
     }
 
     for (; !end && *in; in++) {
diff --git a/tests/ref/fate/sub-srt b/tests/ref/fate/sub-srt
index 4439857..40b20cd 100644
--- a/tests/ref/fate/sub-srt
+++ b/tests/ref/fate/sub-srt
@@ -19,7 +19,7 @@ Dialogue: 0,0:00:11.50,0:00:14.50,Default,,0,0,0,,{\b1}This line should be bold{
 Dialogue: 0,0:00:14.50,0:00:17.50,Default,,0,0,0,,>\NIt would be a good thing to\Nhide invalid html tags that are closed and show the text in them\N<invalid_tag_unclosed>but show un-closed invalid html tags\NShow not opened tags</invalid_tag_not_opened>\N<
 Dialogue: 0,0:00:17.50,0:00:20.50,Default,,0,0,0,,and also\Nhide invalid html tags with parameters that are closed and show the text in them\N<invalid_tag_uc par=5>but show un-closed invalid html tags\N{\u1}This text should be showed underlined without problems also: 2<3,5>1,4<6{\u0}\NThis shouldn't be underlined
 Dialogue: 0,0:00:20.50,0:00:21.50,Default,,0,0,0,,This text should be in the normal position...
-Dialogue: 0,0:00:21.50,0:00:22.50,Default,,0,0,0,,{\an1}{\move(0,50,0,100)}This text should NOT be in the normal position
+Dialogue: 0,0:00:21.50,0:00:22.50,Default,,0,0,0,,{\an5}{\pos(0,45)}This text should NOT be in the normal position
 Dialogue: 0,0:00:22.50,0:00:24.50,Default,,0,0,0,,Implementation is the same of the ASS tag\N{\an8}This text should be at the\Ntop and horizontally centered
 Dialogue: 0,0:00:22.50,0:00:24.50,Default,,0,0,0,,{\an5}This text should be at the\Nmiddle and horizontally centered
 Dialogue: 0,0:00:22.50,0:00:24.50,Default,,0,0,0,,{\an2}This text should be at the\Nbottom and horizontally centered
-- 
2.3.0



More information about the ffmpeg-devel mailing list