[FFmpeg-devel] [PATCH 2/2] srtdec: Add TEXT subtitle decoding mode.

Philip Langdale philipl at overt.org
Wed May 23 07:10:20 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/srtdec.c    |   25 ++++++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 631802b..e13cc1e 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -402,6 +402,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (MICRODVD, microdvd);
     REGISTER_DECODER (PGSSUB, pgssub);
     REGISTER_ENCDEC  (SRT, srt);
+    REGISTER_DECODER (TEXT, text);
     REGISTER_ENCDEC  (XSUB, xsub);
 
     /* external libraries */
diff --git a/libavcodec/srtdec.c b/libavcodec/srtdec.c
index b6f2dad..3d42c19 100644
--- a/libavcodec/srtdec.c
+++ b/libavcodec/srtdec.c
@@ -217,9 +217,19 @@ static int srt_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 = srt_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr,
                          x1, y1, x2, y2);
         ff_ass_add_rect(sub, buffer, ts_start, ts_end, 0);
@@ -237,3 +247,12 @@ AVCodec ff_srt_decoder = {
     .init         = ff_ass_subtitle_header_default,
     .decode       = srt_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       = srt_decode_frame,
+};
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list