Browse code

add SubRip muxer and demuxer

Originally committed as revision 24488 to svn://svn.ffmpeg.org/ffmpeg/trunk

Aurelien Jacobs authored on 2010/07/25 07:50:12
Showing 8 changed files
... ...
@@ -24,6 +24,7 @@ version <next>:
24 24
 - ANSI/ASCII art playback system
25 25
 - Lego Mindstorms RSO de/muxer
26 26
 - libavcore added
27
+- SubRip subtitle file muxer and demuxer
27 28
 
28 29
 
29 30
 
... ...
@@ -684,6 +684,7 @@ performance on systems without hardware floating point support).
684 684
 @item DVB          @tab X @tab X @tab X @tab X
685 685
 @item DVD          @tab X @tab X @tab X @tab X
686 686
 @item PGS          @tab   @tab   @tab   @tab X
687
+@item SubRip (SRT) @tab X @tab X
687 688
 @item XSUB         @tab   @tab   @tab X @tab X
688 689
 @end multitable
689 690
 
... ...
@@ -348,6 +348,7 @@ enum CodecID {
348 348
     CODEC_ID_MOV_TEXT,
349 349
     CODEC_ID_HDMV_PGS_SUBTITLE,
350 350
     CODEC_ID_DVB_TELETEXT,
351
+    CODEC_ID_SRT,
351 352
 
352 353
     /* other specific kind of codecs (generally used for attachments) */
353 354
     CODEC_ID_TTF= 0x18000,
... ...
@@ -242,6 +242,8 @@ OBJS-$(CONFIG_SOL_DEMUXER)               += sol.o raw.o
242 242
 OBJS-$(CONFIG_SOX_DEMUXER)               += soxdec.o raw.o
243 243
 OBJS-$(CONFIG_SOX_MUXER)                 += soxenc.o
244 244
 OBJS-$(CONFIG_SPDIF_MUXER)               += spdif.o
245
+OBJS-$(CONFIG_SRT_DEMUXER)               += srtdec.o
246
+OBJS-$(CONFIG_SRT_MUXER)                 += raw.o
245 247
 OBJS-$(CONFIG_STR_DEMUXER)               += psxstr.o
246 248
 OBJS-$(CONFIG_SWF_DEMUXER)               += swfdec.o
247 249
 OBJS-$(CONFIG_SWF_MUXER)                 += swfenc.o
... ...
@@ -189,6 +189,7 @@ void av_register_all(void)
189 189
     REGISTER_DEMUXER  (SOL, sol);
190 190
     REGISTER_MUXDEMUX (SOX, sox);
191 191
     REGISTER_MUXER    (SPDIF, spdif);
192
+    REGISTER_MUXDEMUX (SRT, srt);
192 193
     REGISTER_DEMUXER  (STR, str);
193 194
     REGISTER_MUXDEMUX (SWF, swf);
194 195
     REGISTER_MUXER    (TG2, tg2);
... ...
@@ -22,7 +22,7 @@
22 22
 #define AVFORMAT_AVFORMAT_H
23 23
 
24 24
 #define LIBAVFORMAT_VERSION_MAJOR 52
25
-#define LIBAVFORMAT_VERSION_MINOR 76
25
+#define LIBAVFORMAT_VERSION_MINOR 77
26 26
 #define LIBAVFORMAT_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
... ...
@@ -1082,6 +1082,18 @@ AVOutputFormat mlp_muxer = {
1082 1082
 };
1083 1083
 #endif
1084 1084
 
1085
+#if CONFIG_SRT_MUXER
1086
+AVOutputFormat srt_muxer = {
1087
+    .name           = "srt",
1088
+    .long_name      = NULL_IF_CONFIG_SMALL("SubRip subtitle format"),
1089
+    .mime_type      = "application/x-subrip",
1090
+    .extensions     = "srt",
1091
+    .write_packet   = raw_write_packet,
1092
+    .flags          = AVFMT_NOTIMESTAMPS,
1093
+    .subtitle_codec = CODEC_ID_SRT,
1094
+};
1095
+#endif
1096
+
1085 1097
 #if CONFIG_TRUEHD_DEMUXER
1086 1098
 AVInputFormat truehd_demuxer = {
1087 1099
     "truehd",
1088 1100
new file mode 100644
... ...
@@ -0,0 +1,102 @@
0
+/*
1
+ * SubRip subtitle demuxer
2
+ * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "internal.h"
23
+#include "libavutil/intreadwrite.h"
24
+
25
+static int srt_probe(AVProbeData *p)
26
+{
27
+    unsigned char *ptr = p->buf;
28
+    int i, v, num = 0;
29
+
30
+    if (AV_RB24(ptr) == 0xEFBBBF)
31
+        ptr += 3;  /* skip UTF-8 BOM */
32
+
33
+    for (i=0; i<2; i++) {
34
+        if (num == i && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
35
+            return AVPROBE_SCORE_MAX;
36
+        num = atoi(ptr);
37
+        ptr += strcspn(ptr, "\n") + 1;
38
+    }
39
+    return 0;
40
+}
41
+
42
+static int srt_read_header(AVFormatContext *s, AVFormatParameters *ap)
43
+{
44
+    AVStream *st = av_new_stream(s, 0);
45
+    if (!st)
46
+        return -1;
47
+    av_set_pts_info(st, 64, 1, 1000);
48
+    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
49
+    st->codec->codec_id   = CODEC_ID_SRT;
50
+    return 0;
51
+}
52
+
53
+static int64_t get_pts(const char *buf)
54
+{
55
+    int i, v, hour, min, sec, hsec;
56
+
57
+    for (i=0; i<2; i++) {
58
+        if (sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %*d:%*2d:%*2d%*1[,.]%3d",
59
+                   &hour, &min, &sec, &hsec, &v) == 5) {
60
+            min += 60*hour;
61
+            sec += 60*min;
62
+            return sec*1000+hsec;
63
+        }
64
+        buf += strcspn(buf, "\n") + 1;
65
+    }
66
+    return AV_NOPTS_VALUE;
67
+}
68
+
69
+static inline int is_eol(char c)
70
+{
71
+    return c == '\r' || c == '\n';
72
+}
73
+
74
+static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
75
+{
76
+    char buffer[2048], *ptr = buffer, *ptr2;
77
+    int64_t pos = url_ftell(s->pb);
78
+    int res = AVERROR_EOF;
79
+
80
+    do {
81
+        ptr2 = ptr;
82
+        ptr += ff_get_line(s->pb, ptr, sizeof(buffer)+buffer-ptr);
83
+    } while (!is_eol(*ptr2) && !url_feof(s->pb) && ptr-buffer<sizeof(buffer)-1);
84
+
85
+    if (buffer[0] && !(res = av_new_packet(pkt, ptr-buffer))) {
86
+        memcpy(pkt->data, buffer, pkt->size);
87
+        pkt->flags |= AV_PKT_FLAG_KEY;
88
+        pkt->pos = pos;
89
+        pkt->pts = pkt->dts = get_pts(pkt->data);
90
+    }
91
+    return res;
92
+}
93
+
94
+AVInputFormat srt_demuxer = {
95
+    .name        = "srt",
96
+    .long_name   = NULL_IF_CONFIG_SMALL("SubRip subtitle format"),
97
+    .read_probe  = srt_probe,
98
+    .read_header = srt_read_header,
99
+    .read_packet = srt_read_packet,
100
+    .flags       = AVFMT_GENERIC_INDEX,
101
+};