[FFmpeg-devel] [PATCH] Added STL demuxer and decoder

Eejya Singh singh.eejya at gmail.com
Wed Oct 15 21:07:28 CEST 2014


From: Eejya <singh.eejya at gmail.com>

---
 Changelog                |   1 +
 doc/general.texi         |   1 +
 libavcodec/Makefile      |   1 +
 libavcodec/allcodecs.c   |   1 +
 libavcodec/avcodec.h     |   1 +
 libavcodec/codec_desc.c  |   7 +++
 libavcodec/textdec.c     |  18 ++++++-
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/stldec.c     | 136 +++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/stldec.c

diff --git a/Changelog b/Changelog
index b59058b..9626d4a 100644
--- a/Changelog
+++ b/Changelog
@@ -5,6 +5,7 @@ version <next>:
 - HEVC/H.265 RTP payload format (draft v6) packetizer
 - SUP/PGS subtitle demuxer
 - ffprobe -show_pixel_formats option
+- STL subtitle demuxer and decoder
 
 version 2.4:
 - Icecast protocol
diff --git a/doc/general.texi b/doc/general.texi
index 2252f7b..681c5c9 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -1032,6 +1032,7 @@ performance on systems without hardware floating point support).
 @item RealText         @tab   @tab X @tab   @tab X
 @item SAMI             @tab   @tab X @tab   @tab X
 @item SSA/ASS          @tab X @tab X @tab X @tab X
+ at item STL              @tab   @tab X @tab   @tab X
 @item SubRip (SRT)     @tab X @tab X @tab X @tab X
 @item SubViewer v1     @tab   @tab X @tab   @tab X
 @item SubViewer        @tab   @tab X @tab   @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3d70313..1a0072a 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -429,6 +429,7 @@ OBJS-$(CONFIG_SONIC_LS_ENCODER)        += sonic.o
 OBJS-$(CONFIG_SP5X_DECODER)            += sp5xdec.o
 OBJS-$(CONFIG_SRT_DECODER)             += srtdec.o ass.o
 OBJS-$(CONFIG_SRT_ENCODER)             += srtenc.o ass_split.o
+OBJS-$(CONFIG_STL_DECODER)             += textdec.o ass.o
 OBJS-$(CONFIG_SUBRIP_DECODER)          += srtdec.o ass.o
 OBJS-$(CONFIG_SUBRIP_ENCODER)          += srtenc.o ass_split.o
 OBJS-$(CONFIG_SUBVIEWER1_DECODER)      += textdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 7650543..d08abd8 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -490,6 +490,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER(REALTEXT,          realtext);
     REGISTER_DECODER(SAMI,              sami);
     REGISTER_ENCDEC (SRT,               srt);
+    REGISTER_DECODER(STL,               stl);
     REGISTER_ENCDEC (SUBRIP,            subrip);
     REGISTER_DECODER(SUBVIEWER,         subviewer);
     REGISTER_DECODER(SUBVIEWER1,        subviewer1);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index a5a651a..acd9b03 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -509,6 +509,7 @@ enum AVCodecID {
     AV_CODEC_ID_JACOSUB    = MKBETAG('J','S','U','B'),
     AV_CODEC_ID_SAMI       = MKBETAG('S','A','M','I'),
     AV_CODEC_ID_REALTEXT   = MKBETAG('R','T','X','T'),
+    AV_CODEC_ID_STL        = MKBETAG('S','p','T','L'),
     AV_CODEC_ID_SUBVIEWER1 = MKBETAG('S','b','V','1'),
     AV_CODEC_ID_SUBVIEWER  = MKBETAG('S','u','b','V'),
     AV_CODEC_ID_SUBRIP     = MKBETAG('S','R','i','p'),
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9e9728b..eeb4505 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2635,6 +2635,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
+        .id        = AV_CODEC_ID_STL,
+        .type      = AVMEDIA_TYPE_SUBTITLE,
+        .name      = "stl",
+        .long_name = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
+    },
+    {
         .id        = AV_CODEC_ID_SUBVIEWER1,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "subviewer1",
diff --git a/libavcodec/textdec.c b/libavcodec/textdec.c
index ba189e8..aa84257 100644
--- a/libavcodec/textdec.c
+++ b/libavcodec/textdec.c
@@ -88,7 +88,7 @@ AVCodec ff_text_decoder = {
 };
 #endif
 
-#if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER
+#if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER || CONFIG_STL_DECODER
 
 static int linebreak_init(AVCodecContext *avctx)
 {
@@ -113,6 +113,22 @@ AVCodec ff_vplayer_decoder = {
 };
 #endif
 
+#if CONFIG_STL_DECODER
+#define stl_options options
+DECLARE_CLASS(stl);
+
+AVCodec ff_stl_decoder = {
+    .name           = "stl",
+    .long_name      = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
+    .priv_data_size = sizeof(TextContext),
+    .type           = AVMEDIA_TYPE_SUBTITLE,
+    .id             = AV_CODEC_ID_STL,
+    .decode         = text_decode_frame,
+    .init           = linebreak_init,
+    .priv_class     = &stl_decoder_class,
+};
+#endif
+
 #if CONFIG_PJS_DECODER
 #define pjs_options options
 DECLARE_CLASS(pjs);
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 86064ea..a64e7ad 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -433,6 +433,7 @@ OBJS-$(CONFIG_VOBSUB_DEMUXER)            += subtitles.o # mpeg demuxer is in the
 OBJS-$(CONFIG_VOC_DEMUXER)               += vocdec.o voc.o
 OBJS-$(CONFIG_VOC_MUXER)                 += vocenc.o voc.o
 OBJS-$(CONFIG_VPLAYER_DEMUXER)           += vplayerdec.o subtitles.o
+OBJS-$(CONFIG_STL_DEMUXER)               += stldec.o subtitles.o
 OBJS-$(CONFIG_VQF_DEMUXER)               += vqf.o
 OBJS-$(CONFIG_W64_DEMUXER)               += wavdec.o w64.o pcm.o
 OBJS-$(CONFIG_W64_MUXER)                 += wavenc.o w64.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index d54ed9b..c1a3a4e 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -258,6 +258,7 @@ void av_register_all(void)
     REGISTER_DEMUXER (SBG,              sbg);
     REGISTER_DEMUXER (SDP,              sdp);
     REGISTER_DEMUXER (SDR2,             sdr2);
+    REGISTER_DEMUXER (STL,              stl);
 #if CONFIG_RTPDEC
     ff_register_rtp_dynamic_payload_handlers();
     ff_register_rdt_dynamic_payload_handlers();
diff --git a/libavformat/stldec.c b/libavformat/stldec.c
new file mode 100644
index 0000000..9e79720
--- /dev/null
+++ b/libavformat/stldec.c
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2014 Eejya Singh
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * STL subtitles format demuxer
+ */
+#include "avformat.h"
+#include "internal.h"
+#include "subtitles.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/avstring.h"
+
+typedef struct {
+    FFDemuxSubtitlesQueue q;
+} STLContext;
+
+static int stl_probe(AVProbeData *p)
+{
+    char c;
+    const unsigned char *ptr = p->buf;
+
+    if (AV_RB24(ptr) == 0xEFBBBF)
+        ptr += 3;  /* skip UTF-8 BOM */
+    while(*ptr=='\r' || *ptr=='\n' || *ptr=='$' || !strncmp(ptr,"//",2))
+        ptr+=ff_subtitles_next_line(ptr);
+    if (sscanf(ptr, "%*d:%*d:%*d:%*d , %*d:%*d:%*d:%*d , %c", &c) == 1)
+        return AVPROBE_SCORE_MAX;
+    return 0;
+}
+static int64_t get_pts(char **buf, int *duration)
+{
+        int hh1, mm1, ss1, ms1;
+        int hh2, mm2, ss2, ms2;
+        int len=0;
+        if (sscanf(*buf, "%2d:%2d:%2d:%2d , %2d:%2d:%2d:%2d , %n",
+                   &hh1, &mm1, &ss1, &ms1,
+                   &hh2, &mm2, &ss2, &ms2, &len) >= 8 && len>0) {
+            int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
+            int64_t end   = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
+            *duration = end - start;
+            *buf+=len;
+            return start;
+         }
+        return AV_NOPTS_VALUE;
+}
+
+static int stl_read_header(AVFormatContext *s)
+{
+    STLContext *stl = s->priv_data;
+    AVStream *st = avformat_new_stream(s, NULL);
+
+    if (!st)
+        return AVERROR(ENOMEM);
+    avpriv_set_pts_info(st, 64, 1, 100);
+    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+    st->codec->codec_id   = AV_CODEC_ID_STL;
+
+    while (!avio_feof(s->pb)) {
+        char line[4096];
+        char *p = line;
+        const int64_t pos = avio_tell(s->pb);
+        int len = ff_get_line(s->pb, line, sizeof(line));
+        int64_t pts_start;
+        int duration;
+        if (!len)
+            break;
+
+        line[strcspn(line, "\r\n")] = 0;
+
+        pts_start = get_pts(&p , &duration);
+        if (pts_start != AV_NOPTS_VALUE) {
+            AVPacket *sub;
+
+            sub = ff_subtitles_queue_insert(&stl->q, p, strlen(p), 0);
+            if (!sub)
+                return AVERROR(ENOMEM);
+            sub->pos = pos;
+            sub->pts = pts_start;
+            sub->duration = duration;
+        }
+    }
+
+    ff_subtitles_queue_finalize(&stl->q);
+    return 0;
+
+}
+static int stl_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    STLContext *stl = s->priv_data;
+    return ff_subtitles_queue_read_packet(&stl->q, pkt);
+}
+
+static int stl_read_seek(AVFormatContext *s, int stream_index,
+                             int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
+{
+    STLContext *stl = s->priv_data;
+    return ff_subtitles_queue_seek(&stl->q, s, stream_index,
+                                   min_ts, ts, max_ts, flags);
+}
+
+static int stl_read_close(AVFormatContext *s)
+{
+    STLContext *stl = s->priv_data;
+    ff_subtitles_queue_clean(&stl->q);
+    return 0;
+}
+
+AVInputFormat ff_stl_demuxer = {
+    .name           = "stl",
+    .long_name      = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
+    .priv_data_size = sizeof(STLContext),
+    .read_probe     = stl_probe,
+    .read_header    = stl_read_header,
+    .read_packet    = stl_read_packet,
+    .read_seek2     = stl_read_seek,
+    .read_close     = stl_read_close,
+    .extensions     = "stl",
+};
-- 
1.9.1



More information about the ffmpeg-devel mailing list