[FFmpeg-devel] [PATCH] SubViewer demuxer and decoder.

Clément Bœsch ubitux at gmail.com
Thu Jul 19 19:21:56 CEST 2012


---
The styles are currently ignored because I don't know how to interpret them
(especially concerning the inheritance of the styles between events). The
author of SubViewer wasn't able to told me either, but it should not be common
(classic samples are actually not common anyway).
---
 Changelog                    |   3 +-
 doc/general.texi             |   1 +
 libavcodec/Makefile          |   1 +
 libavcodec/allcodecs.c       |   1 +
 libavcodec/avcodec.h         |   1 +
 libavcodec/subviewerdec.c    |  78 +++++++++++++++++++
 libavformat/Makefile         |   1 +
 libavformat/allformats.c     |   1 +
 libavformat/subviewerdec.c   | 177 +++++++++++++++++++++++++++++++++++++++++++
 tests/fate/subtitles.mak     |   3 +
 tests/ref/fate/sub-subviewer |   1 +
 11 files changed, 266 insertions(+), 2 deletions(-)
 create mode 100644 libavcodec/subviewerdec.c
 create mode 100644 libavformat/subviewerdec.c
 create mode 100644 tests/ref/fate/sub-subviewer

diff --git a/Changelog b/Changelog
index 4242cea..f230cee 100644
--- a/Changelog
+++ b/Changelog
@@ -23,8 +23,7 @@ version next:
 - RTMPTS protocol support
 - showwaves filter
 - LucasArts SMUSH playback support
-- SAMI demuxer and decoder
-- RealText demuxer and decoder
+- SAMI, RealText and SubViewer demuxer and decoder
 - Heart Of Darkness PAF playback support
 - iec61883 device
 - asettb filter
diff --git a/doc/general.texi b/doc/general.texi
index 370765e..3919da6 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -885,6 +885,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 SubRip (SRT)     @tab X @tab X @tab X @tab X
+ at item SubViewer        @tab   @tab X @tab   @tab X
 @item 3GPP Timed Text  @tab   @tab   @tab   @tab X
 @item XSUB             @tab   @tab   @tab X @tab X
 @end multitable
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 45dee19..560b1bc 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -426,6 +426,7 @@ OBJS-$(CONFIG_SONIC_LS_ENCODER)        += sonic.o
 OBJS-$(CONFIG_SP5X_DECODER)            += sp5xdec.o mjpegdec.o mjpeg.o
 OBJS-$(CONFIG_SRT_DECODER)             += srtdec.o ass.o
 OBJS-$(CONFIG_SRT_ENCODER)             += srtenc.o ass_split.o
+OBJS-$(CONFIG_SUBVIEWER_DECODER)       += subviewerdec.o ass.o
 OBJS-$(CONFIG_SUNRAST_DECODER)         += sunrast.o
 OBJS-$(CONFIG_SUNRAST_ENCODER)         += sunrastenc.o
 OBJS-$(CONFIG_SVQ1_DECODER)            += svq1dec.o svq1.o h263.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 3b18f57..3bdac79 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 (SUBVIEWER, subviewer);
     REGISTER_ENCDEC  (XSUB, xsub);
 
     /* external libraries */
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 3c18040..84a2360 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -437,6 +437,7 @@ enum CodecID {
     CODEC_ID_JACOSUB    = MKBETAG('J','S','U','B'),
     CODEC_ID_SAMI       = MKBETAG('S','A','M','I'),
     CODEC_ID_REALTEXT   = MKBETAG('R','T','X','T'),
+    CODEC_ID_SUBVIEWER  = MKBETAG('S','u','b','V'),
 
     /* other specific kind of codecs (generally used for attachments) */
     CODEC_ID_FIRST_UNKNOWN = 0x18000,           ///< A dummy ID pointing at the start of various fake codecs.
diff --git a/libavcodec/subviewerdec.c b/libavcodec/subviewerdec.c
new file mode 100644
index 0000000..9ddbe02
--- /dev/null
+++ b/libavcodec/subviewerdec.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2012 Clément Bœsch
+ *
+ * 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
+ * SubViewer subtitle decoder
+ * @see https://en.wikipedia.org/wiki/SubViewer
+ */
+
+#include "avcodec.h"
+#include "ass.h"
+#include "libavutil/bprint.h"
+
+static int subviewer_event_to_ass(AVBPrint *buf, const char *p)
+{
+    while (*p) {
+        char c;
+
+        if (sscanf(p, "%*u:%*u:%*u.%*u,%*u:%*u:%*u.%*u%c", &c) == 1)
+            p += strcspn(p, "\n") + 1;
+        if (!strncmp(p, "[br]", 4)) {
+            av_bprintf(buf, "\\N");
+            p += 4;
+        } else {
+            if (p[0] == '\n' && p[1])
+                av_bprintf(buf, "\\N");
+            else if (*p != '\r')
+                av_bprint_chars(buf, *p, 1);
+            p++;
+        }
+    }
+
+    av_bprintf(buf, "\r\n");
+    return 0;
+}
+
+static int subviewer_decode_frame(AVCodecContext *avctx,
+                                  void *data, int *got_sub_ptr, AVPacket *avpkt)
+{
+    AVSubtitle *sub = data;
+    const char *ptr = avpkt->data;
+    AVBPrint buf;
+
+    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+    // note: no need to rescale pts & duration since they are in the same
+    // timebase than ASS (1/100)
+    if (ptr && avpkt->size > 0 && !subviewer_event_to_ass(&buf, ptr))
+        ff_ass_add_rect(sub, buf.str, avpkt->pts, avpkt->duration, 0);
+    *got_sub_ptr = sub->num_rects > 0;
+    av_bprint_finalize(&buf, NULL);
+    return avpkt->size;
+}
+
+AVCodec ff_subviewer_decoder = {
+    .name           = "subviewer",
+    .long_name      = NULL_IF_CONFIG_SMALL("SubViewer subtitle"),
+    .type           = AVMEDIA_TYPE_SUBTITLE,
+    .id             = CODEC_ID_SUBVIEWER,
+    .decode         = subviewer_decode_frame,
+    .init           = ff_ass_subtitle_header_default,
+};
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 45ae6ea..602d94a 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -322,6 +322,7 @@ OBJS-$(CONFIG_SPDIF_MUXER)               += spdif.o spdifenc.o
 OBJS-$(CONFIG_SRT_DEMUXER)               += srtdec.o
 OBJS-$(CONFIG_SRT_MUXER)                 += srtenc.o
 OBJS-$(CONFIG_STR_DEMUXER)               += psxstr.o
+OBJS-$(CONFIG_SUBVIEWER_DEMUXER)         += subviewerdec.o
 OBJS-$(CONFIG_SWF_DEMUXER)               += swfdec.o
 OBJS-$(CONFIG_SWF_MUXER)                 += swfenc.o
 OBJS-$(CONFIG_THP_DEMUXER)               += thp.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 94dc347..7c6c312 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -229,6 +229,7 @@ void av_register_all(void)
     REGISTER_MUXDEMUX (SPDIF, spdif);
     REGISTER_MUXDEMUX (SRT, srt);
     REGISTER_DEMUXER  (STR, str);
+    REGISTER_DEMUXER  (SUBVIEWER, subviewer);
     REGISTER_MUXDEMUX (SWF, swf);
     REGISTER_MUXER    (TG2, tg2);
     REGISTER_MUXER    (TGP, tgp);
diff --git a/libavformat/subviewerdec.c b/libavformat/subviewerdec.c
new file mode 100644
index 0000000..ac68fc1
--- /dev/null
+++ b/libavformat/subviewerdec.c
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2012 Clément Bœsch
+ *
+ * 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
+ * SubViewer subtitle demuxer
+ * @see https://en.wikipedia.org/wiki/SubViewer
+ */
+
+#include "avformat.h"
+#include "internal.h"
+#include "subtitles.h"
+#include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
+#include "libavutil/intreadwrite.h"
+
+typedef struct {
+    FFDemuxSubtitlesQueue q;
+} SubViewerContext;
+
+static int subviewer_probe(AVProbeData *p)
+{
+    char c;
+    const unsigned char *ptr = p->buf;
+
+    if (AV_RB24(ptr) == 0xEFBBBF)
+        ptr += 3;  /* skip UTF-8 BOM */
+    if (sscanf(ptr, "%*u:%*u:%*u.%*u,%*u:%*u:%*u.%*u%c", &c) == 1)
+        return AVPROBE_SCORE_MAX/2;
+    if (!strncmp(ptr, "[INFORMATION]", 13))
+        return AVPROBE_SCORE_MAX/3;
+    return 0;
+}
+
+static int read_ts(const char *s, int64_t *start, int *duration)
+{
+    int64_t end;
+    int hh1, mm1, ss1, ms1;
+    int hh2, mm2, ss2, ms2;
+
+    if (sscanf(s, "%u:%u:%u.%u,%u:%u:%u.%u",
+               &hh1, &mm1, &ss1, &ms1, &hh2, &mm2, &ss2, &ms2) == 8) {
+        end    = (hh2*3600 + mm2*60 + ss2) * 100 + ms2;
+        *start = (hh1*3600 + mm1*60 + ss1) * 100 + ms1;
+        *duration = end - *start;
+        return 0;
+    }
+    return -1;
+}
+
+static int subviewer_read_header(AVFormatContext *s)
+{
+    SubViewerContext *subviewer = s->priv_data;
+    AVStream *st = avformat_new_stream(s, NULL);
+    AVBPrint header;
+    int res = 0;
+
+    if (!st)
+        return AVERROR(ENOMEM);
+    avpriv_set_pts_info(st, 64, 1, 100);
+    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+    st->codec->codec_id   = CODEC_ID_SUBVIEWER;
+
+    av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+    while (!url_feof(s->pb)) {
+        char line[2048];
+        const int64_t pos = avio_tell(s->pb);
+        int len = ff_get_line(s->pb, line, sizeof(line));
+
+        if (!len)
+            break;
+
+        if (line[0] == '[' && strncmp(line, "[br]", 4)) {
+
+            /* ignore event style, XXX: add to side_data? */
+            if (strstr(line, "[COLF]") || strstr(line, "[SIZE]") ||
+                strstr(line, "[FONT]") || strstr(line, "[STYLE]"))
+                continue;
+
+            if (!st->codec->extradata) { // header not finalized yet
+                av_bprintf(&header, "%s", line);
+                if (!strncmp(line, "[END INFORMATION]", 17) || !strncmp(line, "[SUBTITLE]", 10)) {
+                    /* end of header */
+                    av_bprint_finalize(&header, (char **)&st->codec->extradata);
+                    if (!st->codec->extradata) {
+                        res = AVERROR(ENOMEM);
+                        goto end;
+                    }
+                    st->codec->extradata_size = header.len + 1;
+                } else if (strncmp(line, "[INFORMATION]", 13)) {
+                    /* assume file metadata at this point */
+                    int i, j = 0;
+                    char key[32], value[128];
+
+                    for (i = 1; i < sizeof(key) - 1 && line[i] && line[i] != ']'; i++)
+                        key[i - 1] = av_tolower(line[i]);
+                    key[i - 1] = 0;
+
+                    if (line[i] == ']')
+                        i++;
+                    while (line[i] == ' ')
+                        i++;
+                    while (j < sizeof(value) - 1 && line[i] && !strchr("]\r\n", line[i]))
+                        value[j++] = line[i++];
+                    value[j] = 0;
+
+                    av_dict_set(&s->metadata, key, value, 0);
+                }
+            }
+        } else {
+            int64_t pts_start = AV_NOPTS_VALUE;
+            int duration = -1;
+            int timed_line = !read_ts(line, &pts_start, &duration);
+            AVPacket *sub;
+
+            sub = ff_subtitles_queue_insert(&subviewer->q, line, len, !timed_line);
+            if (!sub) {
+                res = AVERROR(ENOMEM);
+                goto end;
+            }
+            if (timed_line) {
+                sub->pos = pos;
+                sub->pts = pts_start;
+                sub->duration = duration;
+            }
+        }
+    }
+
+    ff_subtitles_queue_finalize(&subviewer->q);
+
+end:
+    av_bprint_finalize(&header, NULL);
+    return res;
+}
+
+static int subviewer_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    SubViewerContext *subviewer = s->priv_data;
+    return ff_subtitles_queue_read_packet(&subviewer->q, pkt);
+}
+
+static int subviewer_read_close(AVFormatContext *s)
+{
+    SubViewerContext *subviewer = s->priv_data;
+    ff_subtitles_queue_clean(&subviewer->q);
+    return 0;
+}
+
+AVInputFormat ff_subviewer_demuxer = {
+    .name           = "subviewer",
+    .long_name      = NULL_IF_CONFIG_SMALL("SubViewer subtitle format"),
+    .priv_data_size = sizeof(SubViewerContext),
+    .read_probe     = subviewer_probe,
+    .read_header    = subviewer_read_header,
+    .read_packet    = subviewer_read_packet,
+    .read_close     = subviewer_read_close,
+    .flags          = AVFMT_GENERIC_INDEX,
+    .extensions     = "sub",
+};
diff --git a/tests/fate/subtitles.mak b/tests/fate/subtitles.mak
index 84f451d..2f6e37f 100644
--- a/tests/fate/subtitles.mak
+++ b/tests/fate/subtitles.mak
@@ -16,5 +16,8 @@ fate-sub-sami: CMD = md5 -i $(SAMPLES)/sub/SAMI_capability_tester.smi -f ass
 FATE_SUBTITLES += fate-sub-srt
 fate-sub-srt: CMD = md5 -i $(SAMPLES)/sub/SubRip_capability_tester.srt -f ass
 
+FATE_SUBTITLES += fate-sub-subviewer
+fate-sub-subviewer: CMD = md5 -i $(SAMPLES)/sub/SubViewer_capability_tester.sub -f ass
+
 FATE_SAMPLES_FFMPEG += $(FATE_SUBTITLES)
 fate-subtitles: $(FATE_SUBTITLES)
diff --git a/tests/ref/fate/sub-subviewer b/tests/ref/fate/sub-subviewer
new file mode 100644
index 0000000..3b5327f
--- /dev/null
+++ b/tests/ref/fate/sub-subviewer
@@ -0,0 +1 @@
+303c25863d2283928c19db58a53c93e2
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list