Browse code

Added STL demuxer and decoder

Signed-off-by: Clément Bœsch <u@pkh.me>

Eejya Singh authored on 2014/10/21 05:25:39
Showing 14 changed files
... ...
@@ -6,6 +6,7 @@ version <next>:
6 6
 - SUP/PGS subtitle demuxer
7 7
 - ffprobe -show_pixel_formats option
8 8
 - CAST128 symmetric block cipher, ECB mode
9
+- STL subtitle demuxer and decoder
9 10
 
10 11
 version 2.4:
11 12
 - Icecast protocol
... ...
@@ -1031,6 +1031,7 @@ performance on systems without hardware floating point support).
1031 1031
 @item PJS (Phoenix)    @tab   @tab X @tab   @tab X
1032 1032
 @item RealText         @tab   @tab X @tab   @tab X
1033 1033
 @item SAMI             @tab   @tab X @tab   @tab X
1034
+@item Spruce format (STL) @tab   @tab X @tab   @tab X
1034 1035
 @item SSA/ASS          @tab X @tab X @tab X @tab X
1035 1036
 @item SubRip (SRT)     @tab X @tab X @tab X @tab X
1036 1037
 @item SubViewer v1     @tab   @tab X @tab   @tab X
... ...
@@ -429,6 +429,7 @@ OBJS-$(CONFIG_SONIC_LS_ENCODER)        += sonic.o
429 429
 OBJS-$(CONFIG_SP5X_DECODER)            += sp5xdec.o
430 430
 OBJS-$(CONFIG_SRT_DECODER)             += srtdec.o ass.o
431 431
 OBJS-$(CONFIG_SRT_ENCODER)             += srtenc.o ass_split.o
432
+OBJS-$(CONFIG_STL_DECODER)             += textdec.o ass.o
432 433
 OBJS-$(CONFIG_SUBRIP_DECODER)          += srtdec.o ass.o
433 434
 OBJS-$(CONFIG_SUBRIP_ENCODER)          += srtenc.o ass_split.o
434 435
 OBJS-$(CONFIG_SUBVIEWER1_DECODER)      += textdec.o ass.o
... ...
@@ -490,6 +490,7 @@ void avcodec_register_all(void)
490 490
     REGISTER_DECODER(REALTEXT,          realtext);
491 491
     REGISTER_DECODER(SAMI,              sami);
492 492
     REGISTER_ENCDEC (SRT,               srt);
493
+    REGISTER_DECODER(STL,               stl);
493 494
     REGISTER_ENCDEC (SUBRIP,            subrip);
494 495
     REGISTER_DECODER(SUBVIEWER,         subviewer);
495 496
     REGISTER_DECODER(SUBVIEWER1,        subviewer1);
... ...
@@ -509,6 +509,7 @@ enum AVCodecID {
509 509
     AV_CODEC_ID_JACOSUB    = MKBETAG('J','S','U','B'),
510 510
     AV_CODEC_ID_SAMI       = MKBETAG('S','A','M','I'),
511 511
     AV_CODEC_ID_REALTEXT   = MKBETAG('R','T','X','T'),
512
+    AV_CODEC_ID_STL        = MKBETAG('S','p','T','L'),
512 513
     AV_CODEC_ID_SUBVIEWER1 = MKBETAG('S','b','V','1'),
513 514
     AV_CODEC_ID_SUBVIEWER  = MKBETAG('S','u','b','V'),
514 515
     AV_CODEC_ID_SUBRIP     = MKBETAG('S','R','i','p'),
... ...
@@ -2635,6 +2635,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
2635 2635
         .props     = AV_CODEC_PROP_TEXT_SUB,
2636 2636
     },
2637 2637
     {
2638
+        .id        = AV_CODEC_ID_STL,
2639
+        .type      = AVMEDIA_TYPE_SUBTITLE,
2640
+        .name      = "stl",
2641
+        .long_name = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
2642
+        .props     = AV_CODEC_PROP_TEXT_SUB,
2643
+    },
2644
+    {
2638 2645
         .id        = AV_CODEC_ID_SUBVIEWER1,
2639 2646
         .type      = AVMEDIA_TYPE_SUBTITLE,
2640 2647
         .name      = "subviewer1",
... ...
@@ -88,7 +88,7 @@ AVCodec ff_text_decoder = {
88 88
 };
89 89
 #endif
90 90
 
91
-#if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER
91
+#if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER || CONFIG_STL_DECODER
92 92
 
93 93
 static int linebreak_init(AVCodecContext *avctx)
94 94
 {
... ...
@@ -113,6 +113,22 @@ AVCodec ff_vplayer_decoder = {
113 113
 };
114 114
 #endif
115 115
 
116
+#if CONFIG_STL_DECODER
117
+#define stl_options options
118
+DECLARE_CLASS(stl);
119
+
120
+AVCodec ff_stl_decoder = {
121
+    .name           = "stl",
122
+    .long_name      = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
123
+    .priv_data_size = sizeof(TextContext),
124
+    .type           = AVMEDIA_TYPE_SUBTITLE,
125
+    .id             = AV_CODEC_ID_STL,
126
+    .decode         = text_decode_frame,
127
+    .init           = linebreak_init,
128
+    .priv_class     = &stl_decoder_class,
129
+};
130
+#endif
131
+
116 132
 #if CONFIG_PJS_DECODER
117 133
 #define pjs_options options
118 134
 DECLARE_CLASS(pjs);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 56
32
-#define LIBAVCODEC_VERSION_MINOR  8
33
-#define LIBAVCODEC_VERSION_MICRO 102
32
+#define LIBAVCODEC_VERSION_MINOR  9
33
+#define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
36 36
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -404,6 +404,7 @@ OBJS-$(CONFIG_SPEEX_MUXER)               += oggenc.o \
404 404
                                             vorbiscomment.o
405 405
 OBJS-$(CONFIG_SRT_DEMUXER)               += srtdec.o subtitles.o
406 406
 OBJS-$(CONFIG_SRT_MUXER)                 += srtenc.o
407
+OBJS-$(CONFIG_STL_DEMUXER)               += stldec.o subtitles.o
407 408
 OBJS-$(CONFIG_STR_DEMUXER)               += psxstr.o
408 409
 OBJS-$(CONFIG_SUBVIEWER1_DEMUXER)        += subviewer1dec.o subtitles.o
409 410
 OBJS-$(CONFIG_SUBVIEWER_DEMUXER)         += subviewerdec.o subtitles.o
... ...
@@ -278,6 +278,7 @@ void av_register_all(void)
278 278
     REGISTER_MUXDEMUX(SPDIF,            spdif);
279 279
     REGISTER_MUXDEMUX(SRT,              srt);
280 280
     REGISTER_DEMUXER (STR,              str);
281
+    REGISTER_DEMUXER (STL,              stl);
281 282
     REGISTER_DEMUXER (SUBVIEWER1,       subviewer1);
282 283
     REGISTER_DEMUXER (SUBVIEWER,        subviewer);
283 284
     REGISTER_DEMUXER (SUP,              sup);
284 285
new file mode 100644
... ...
@@ -0,0 +1,141 @@
0
+/*
1
+ * Copyright (c) 2014 Eejya Singh
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * STL subtitles format demuxer
23
+ * @see https://documentation.apple.com/en/dvdstudiopro/usermanual/index.html#chapter=19%26section=13%26tasks=true
24
+ */
25
+
26
+#include "avformat.h"
27
+#include "internal.h"
28
+#include "subtitles.h"
29
+#include "libavutil/intreadwrite.h"
30
+#include "libavutil/avstring.h"
31
+
32
+typedef struct {
33
+    FFDemuxSubtitlesQueue q;
34
+} STLContext;
35
+
36
+static int stl_probe(AVProbeData *p)
37
+{
38
+    char c;
39
+    const unsigned char *ptr = p->buf;
40
+
41
+    if (AV_RB24(ptr) == 0xEFBBBF)
42
+        ptr += 3;  /* skip UTF-8 BOM */
43
+
44
+    while (*ptr == '\r' || *ptr == '\n' || *ptr == '$' || !strncmp(ptr, "//" , 2))
45
+        ptr += ff_subtitles_next_line(ptr);
46
+
47
+    if (sscanf(ptr, "%*d:%*d:%*d:%*d , %*d:%*d:%*d:%*d , %c", &c) == 1)
48
+        return AVPROBE_SCORE_MAX;
49
+
50
+    return 0;
51
+}
52
+
53
+static int64_t get_pts(char **buf, int *duration)
54
+{
55
+    int hh1, mm1, ss1, ms1;
56
+    int hh2, mm2, ss2, ms2;
57
+    int len = 0;
58
+
59
+    if (sscanf(*buf, "%2d:%2d:%2d:%2d , %2d:%2d:%2d:%2d , %n",
60
+                &hh1, &mm1, &ss1, &ms1,
61
+                &hh2, &mm2, &ss2, &ms2, &len) >= 8 && len > 0) {
62
+        int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
63
+        int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
64
+        *duration = end - start;
65
+        *buf += len;
66
+        return start;
67
+    }
68
+    return AV_NOPTS_VALUE;
69
+}
70
+
71
+static int stl_read_header(AVFormatContext *s)
72
+{
73
+    STLContext *stl = s->priv_data;
74
+    AVStream *st = avformat_new_stream(s, NULL);
75
+
76
+    if (!st)
77
+        return AVERROR(ENOMEM);
78
+    avpriv_set_pts_info(st, 64, 1, 100);
79
+    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
80
+    st->codec->codec_id   = AV_CODEC_ID_STL;
81
+
82
+    while (!avio_feof(s->pb)) {
83
+        char line[4096];
84
+        char *p = line;
85
+        const int64_t pos = avio_tell(s->pb);
86
+        int len = ff_get_line(s->pb, line, sizeof(line));
87
+        int64_t pts_start;
88
+        int duration;
89
+
90
+        if (!len)
91
+            break;
92
+
93
+        line[strcspn(line, "\r\n")] = 0;
94
+        pts_start = get_pts(&p , &duration);
95
+
96
+        if (pts_start != AV_NOPTS_VALUE) {
97
+            AVPacket *sub;
98
+            sub = ff_subtitles_queue_insert(&stl->q, p, strlen(p), 0);
99
+            if (!sub)
100
+                return AVERROR(ENOMEM);
101
+            sub->pos = pos;
102
+            sub->pts = pts_start;
103
+            sub->duration = duration;
104
+        }
105
+    }
106
+    ff_subtitles_queue_finalize(&stl->q);
107
+    return 0;
108
+}
109
+static int stl_read_packet(AVFormatContext *s, AVPacket *pkt)
110
+{
111
+    STLContext *stl = s->priv_data;
112
+    return ff_subtitles_queue_read_packet(&stl->q, pkt);
113
+}
114
+
115
+static int stl_read_seek(AVFormatContext *s, int stream_index,
116
+                             int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
117
+{
118
+    STLContext *stl = s->priv_data;
119
+    return ff_subtitles_queue_seek(&stl->q, s, stream_index,
120
+                                   min_ts, ts, max_ts, flags);
121
+}
122
+
123
+static int stl_read_close(AVFormatContext *s)
124
+{
125
+    STLContext *stl = s->priv_data;
126
+    ff_subtitles_queue_clean(&stl->q);
127
+    return 0;
128
+}
129
+
130
+AVInputFormat ff_stl_demuxer = {
131
+    .name           = "stl",
132
+    .long_name      = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
133
+    .priv_data_size = sizeof(STLContext),
134
+    .read_probe     = stl_probe,
135
+    .read_header    = stl_read_header,
136
+    .read_packet    = stl_read_packet,
137
+    .read_seek2     = stl_read_seek,
138
+    .read_close     = stl_read_close,
139
+    .extensions     = "stl",
140
+};
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 56
33
-#define LIBAVFORMAT_VERSION_MINOR  9
34
-#define LIBAVFORMAT_VERSION_MICRO 101
33
+#define LIBAVFORMAT_VERSION_MINOR  10
34
+#define LIBAVFORMAT_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \
... ...
@@ -37,6 +37,9 @@ fate-sub-sami: CMD = fmtstdout ass -i $(TARGET_SAMPLES)/sub/SAMI_capability_test
37 37
 FATE_SUBTITLES_ASS-$(call DEMDEC, SRT, SUBRIP) += fate-sub-srt
38 38
 fate-sub-srt: CMD = fmtstdout ass -i $(TARGET_SAMPLES)/sub/SubRip_capability_tester.srt
39 39
 
40
+FATE_SUBTITLES_ASS-$(call DEMDEC, STL, STL) += fate-sub-stl
41
+fate-sub-stl: CMD = fmtstdout ass -i $(TARGET_SAMPLES)/sub/STL_capability_tester.stl
42
+
40 43
 FATE_SUBTITLES-$(call ALLYES, MOV_DEMUXER MOVTEXT_DECODER SUBRIP_ENCODER SRT_MUXER) += fate-sub-subripenc
41 44
 fate-sub-subripenc: CMD = fmtstdout srt -i $(TARGET_SAMPLES)/sub/MovText_capability_tester.mp4 -scodec subrip
42 45
 
43 46
new file mode 100644
... ...
@@ -0,0 +1,29 @@
0
+[Script Info]
1
+; Script generated by FFmpeg/Lavc
2
+ScriptType: v4.00+
3
+PlayResX: 384
4
+PlayResY: 288
5
+
6
+[V4+ Styles]
7
+Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
8
+Style: Default,Arial,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0
9
+
10
+[Events]
11
+Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
12
+Dialogue: 0,0:00:31.02,0:00:33.00,Default,,0,0,0,,Hello, my name is Axel Kornmesser.
13
+Dialogue: 0,0:00:45.02,0:00:49.13,Default,,0,0,0,,It is always a pleasure to work with ESA astronomers.
14
+Dialogue: 0,0:00:49.13,0:00:52.03,Default,,0,0,0,,The "Eyes on The Skies" documentary
15
+Dialogue: 0,0:00:52.03,0:00:55.09,Default,,0,0,0,,was our second collaboration
16
+Dialogue: 0,0:00:55.09,0:00:58.07,Default,,0,0,0,,after a great \Nexperience in 2005,
17
+Dialogue: 0,0:00:58.07,0:00:59.20,Default,,0,0,0,,when \Nwe did the story about the
18
+Dialogue: 0,0:00:59.20,0:01:04.01,Default,,0,0,0,,Hubble Telescope "15 Years of Discovery".
19
+Dialogue: 0,0:01:04.16,0:01:07.04,Default,,0,0,0,,It was a lot of fun again.
20
+Dialogue: 0,0:01:15.04,0:01:18.16,Default,,0,0,0,,We usually \N don't get the final film \Nbefore we start composing
21
+Dialogue: 0,0:01:18.21,0:01:22.02,Default,,0,0,0,,We had a script and many details about the story,
22
+Dialogue: 0,0:01:22.10,0:01:26.08,Default,,0,0,0,,and so we worked\N in parallel \Nin the movie production
23
+Dialogue: 0,0:01:27.04,0:01:30.17,Default,,0,0,0,,The largest part of \N the soundtrack \Nwas done without seeing a movie
24
+Dialogue: 0,0:01:30.17,0:01:36.06,Default,,0,0,0,,It was no problem, but very inspiring \Nand a free working process.
25
+Dialogue: 0,0:02:08.13,0:02:10.23,Default,,0,0,0,,Galileo's theme is one of my favourites.
26
+Dialogue: 0,0:02:10.23,0:02:14.10,Default,,0,0,0,,We did a lot of different versions \Nabout the central theme.
27
+Dialogue: 0,0:02:14.10,0:02:18.02,Default,,0,0,0,,For the 17th century \N we used a nice harpsichord
28
+Dialogue: 0,0:02:19.05,0:02:22.09,Default,,0,0,0,,and so we landed directly into Galileo's time.