[FFmpeg-devel] [PATCH 3/3] textdec: Add TEXT subtitle decoding mode.

Philip Langdale philipl at overt.org
Wed Aug 1 18:14:31 CEST 2012


The concensus opinion seems to be that a TEXT decoder where we
assume the packet contains pure text and timing information is
carried in the packet pts/duration has value.

Given that the bulk of the work in this case is exactly the same
as for SRT (only the time extraction is different), it seems
reasonable for them to be in the same .c file.

I have left the html-like styling supported by the decoder in place
for TEXT content, on the basis that it works just fine, and it's not
as if it was a clearly defined part of the SRT spec in the first place.

Signed-off-by: Philip Langdale <philipl at overt.org>
---
 libavcodec/allcodecs.c |    1 +
 libavcodec/textdec.c   |   25 ++++++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index bc37907..1db3800 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -414,6 +414,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (REALTEXT, realtext);
     REGISTER_DECODER (SAMI, sami);
     REGISTER_ENCDEC  (SRT, srt);
+    REGISTER_DECODER (TEXT, text);
     REGISTER_ENCDEC  (XSUB, xsub);
 
     /* external libraries */
diff --git a/libavcodec/textdec.c b/libavcodec/textdec.c
index 6acecc1..7553deb 100644
--- a/libavcodec/textdec.c
+++ b/libavcodec/textdec.c
@@ -217,9 +217,19 @@ static int text_decode_frame(AVCodecContext *avctx,
         return avpkt->size;
 
     while (ptr < end && *ptr) {
-        ptr = read_ts(ptr, &ts_start, &ts_end, &x1, &y1, &x2, &y2);
-        if (!ptr)
-            break;
+        if (avctx->codec->id == CODEC_ID_SRT) {
+            ptr = read_ts(ptr, &ts_start, &ts_end, &x1, &y1, &x2, &y2);
+            if (!ptr)
+                break;
+        } else {
+            // Do final divide-by-10 outside rescale to force rounding down.
+            ts_start = av_rescale_q(avpkt->pts,
+                                    avctx->time_base,
+                                    (AVRational){1,1000}) / 10;
+            ts_end   = av_rescale_q(avpkt->pts + avpkt->duration,
+                                    avctx->time_base,
+                                    (AVRational){1,1000}) / 10;
+        }
         ptr = text_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr,
                           x1, y1, x2, y2);
         ff_ass_add_rect(sub, buffer, ts_start, ts_end-ts_start, 0);
@@ -237,3 +247,12 @@ AVCodec ff_srt_decoder = {
     .init         = ff_ass_subtitle_header_default,
     .decode       = text_decode_frame,
 };
+
+AVCodec ff_text_decoder = {
+    .name         = "text",
+    .long_name    = NULL_IF_CONFIG_SMALL("Text subtitle"),
+    .type         = AVMEDIA_TYPE_SUBTITLE,
+    .id           = CODEC_ID_TEXT,
+    .init         = ff_ass_subtitle_header_default,
+    .decode       = text_decode_frame,
+};
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list