Browse code

add SubRip decoder

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

Aurelien Jacobs authored on 2010/12/29 08:52:53
Showing 11 changed files
... ...
@@ -69,6 +69,7 @@ version <next>:
69 69
 - replace the ocv_smooth filter with a more generic ocv filter
70 70
 - Windows Televison (WTV) demuxer
71 71
 - FFmpeg metadata format muxer and demuxer
72
+- SubRip (srt) subtitle decoder
72 73
 
73 74
 
74 75
 version 0.6:
... ...
@@ -198,6 +198,7 @@ Codecs:
198 198
   smc.c                                 Mike Melanson
199 199
   snow.c                                Michael Niedermayer, Loren Merritt
200 200
   sonic.c                               Alex Beregszaszi
201
+  srtdec.c                              Aurelien Jacobs
201 202
   sunrast.c                             Ivo van Poorten
202 203
   svq3.c                                Michael Niedermayer
203 204
   targa.c                               Kostya Shishkov
... ...
@@ -694,7 +694,7 @@ performance on systems without hardware floating point support).
694 694
 @item DVB          @tab X @tab X @tab X @tab X
695 695
 @item DVD          @tab X @tab X @tab X @tab X
696 696
 @item PGS          @tab   @tab   @tab   @tab X
697
-@item SubRip (SRT) @tab X @tab X
697
+@item SubRip (SRT) @tab X @tab X @tab   @tab X
698 698
 @item XSUB         @tab   @tab   @tab X @tab X
699 699
 @end multitable
700 700
 
... ...
@@ -337,6 +337,7 @@ OBJS-$(CONFIG_SONIC_DECODER)           += sonic.o
337 337
 OBJS-$(CONFIG_SONIC_ENCODER)           += sonic.o
338 338
 OBJS-$(CONFIG_SONIC_LS_ENCODER)        += sonic.o
339 339
 OBJS-$(CONFIG_SP5X_DECODER)            += sp5xdec.o mjpegdec.o mjpeg.o
340
+OBJS-$(CONFIG_SRT_DECODER)             += srtdec.o ass.o
340 341
 OBJS-$(CONFIG_SUNRAST_DECODER)         += sunrast.o
341 342
 OBJS-$(CONFIG_SVQ1_DECODER)            += svq1dec.o svq1.o h263.o \
342 343
                                           mpegvideo.o error_resilience.o
... ...
@@ -347,6 +347,7 @@ void avcodec_register_all(void)
347 347
     REGISTER_ENCDEC  (DVBSUB, dvbsub);
348 348
     REGISTER_ENCDEC  (DVDSUB, dvdsub);
349 349
     REGISTER_DECODER (PGSSUB, pgssub);
350
+    REGISTER_DECODER (SRT, srt);
350 351
     REGISTER_ENCDEC  (XSUB, xsub);
351 352
 
352 353
     /* external libraries */
... ...
@@ -22,6 +22,46 @@
22 22
 #include "avcodec.h"
23 23
 #include "ass.h"
24 24
 
25
+int ff_ass_subtitle_header(AVCodecContext *avctx,
26
+                           const char *font, int font_size,
27
+                           int color, int back_color,
28
+                           int bold, int italic, int underline,
29
+                           int alignment)
30
+{
31
+    char header[512];
32
+
33
+    snprintf(header, sizeof(header),
34
+             "[Script Info]\r\n"
35
+             "ScriptType: v4.00+\r\n"
36
+             "\r\n"
37
+             "[V4+ Styles]\r\n"
38
+             "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
39
+             "Style: Default,%s,%d,&H%x,&H%x,&H%x,&H%x,%d,%d,%d,1,1,0,%d,10,10,10,0,0\r\n"
40
+             "\r\n"
41
+             "[Events]\r\n"
42
+             "Format: Layer, Start, End, Text\r\n",
43
+             font, font_size, color, color, back_color, back_color,
44
+             -bold, -italic, -underline, alignment);
45
+
46
+    avctx->subtitle_header = av_strdup(header);
47
+    if (!avctx->subtitle_header)
48
+        return AVERROR(ENOMEM);
49
+    avctx->subtitle_header_size = strlen(avctx->subtitle_header);
50
+    return 0;
51
+}
52
+
53
+int ff_ass_subtitle_header_default(AVCodecContext *avctx)
54
+{
55
+    return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
56
+                                         ASS_DEFAULT_FONT_SIZE,
57
+                                         ASS_DEFAULT_COLOR,
58
+                                         ASS_DEFAULT_BACK_COLOR,
59
+                                         ASS_DEFAULT_BOLD,
60
+                                         ASS_DEFAULT_ITALIC,
61
+                                         ASS_DEFAULT_UNDERLINE,
62
+                                         ASS_DEFAULT_ALIGNMENT);
63
+}
64
+
25 65
 void ff_ass_init(AVSubtitle *sub)
26 66
 {
27 67
     memset(sub, 0, sizeof(*sub));
... ...
@@ -25,6 +25,51 @@
25 25
 #include "avcodec.h"
26 26
 
27 27
 /**
28
+ * Default values for ASS style.
29
+ * @defgroup ass_default
30
+ * @{
31
+ */
32
+#define ASS_DEFAULT_FONT        "Arial"
33
+#define ASS_DEFAULT_FONT_SIZE   16
34
+#define ASS_DEFAULT_COLOR       0xffffff
35
+#define ASS_DEFAULT_BACK_COLOR  0
36
+#define ASS_DEFAULT_BOLD        0
37
+#define ASS_DEFAULT_ITALIC      0
38
+#define ASS_DEFAULT_UNDERLINE   0
39
+#define ASS_DEFAULT_ALIGNMENT   2
40
+/** @} */
41
+
42
+/**
43
+ * Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS.
44
+ *
45
+ * @param avctx pointer to the AVCodecContext
46
+ * @param font name of the default font face to use
47
+ * @param font_size default font size to use
48
+ * @param color default text color to use (ABGR)
49
+ * @param back_color default background color to use (ABGR)
50
+ * @param bold 1 for bold text, 0 for normal text
51
+ * @param italic 1 for italic text, 0 for normal text
52
+ * @param underline 1 for underline text, 0 for normal text
53
+ * @param alignment position of the text (left, center, top...), defined after
54
+ *                  the layout of the numpad (1-3 sub, 4-6 mid, 7-9 top)
55
+ * @return >= 0 on success otherwise an error code <0
56
+ */
57
+int ff_ass_subtitle_header(AVCodecContext *avctx,
58
+                           const char *font, int font_size,
59
+                           int color, int back_color,
60
+                           int bold, int italic, int underline,
61
+                           int alignment);
62
+
63
+/**
64
+ * Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS
65
+ * with default style.
66
+ *
67
+ * @param avctx pointer to the AVCodecContext
68
+ * @return >= 0 on success otherwise an error code <0
69
+ */
70
+int ff_ass_subtitle_header_default(AVCodecContext *avctx);
71
+
72
+/**
28 73
  * Initialize an AVSubtitle structure for use with ff_ass_add_rect().
29 74
  *
30 75
  * @param sub pointer to the AVSubtitle
... ...
@@ -32,8 +32,8 @@
32 32
 #include "libavutil/cpu.h"
33 33
 
34 34
 #define LIBAVCODEC_VERSION_MAJOR 52
35
-#define LIBAVCODEC_VERSION_MINOR 100
36
-#define LIBAVCODEC_VERSION_MICRO  1
35
+#define LIBAVCODEC_VERSION_MINOR 101
36
+#define LIBAVCODEC_VERSION_MICRO  0
37 37
 
38 38
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
39 39
                                                LIBAVCODEC_VERSION_MINOR, \
40 40
new file mode 100644
... ...
@@ -0,0 +1,240 @@
0
+/*
1
+ * SubRip subtitle decoder
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 "libavutil/avstring.h"
22
+#include "libavcore/parseutils.h"
23
+#include "avcodec.h"
24
+#include "ass.h"
25
+
26
+static int html_color_parse(AVCodecContext *avctx, const char *str)
27
+{
28
+    uint8_t rgba[4];
29
+    if (av_parse_color(rgba, str, strcspn(str, "\" >"), avctx) < 0)
30
+        return -1;
31
+    return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
32
+}
33
+
34
+enum {
35
+    PARAM_UNKNOWN = -1,
36
+    PARAM_SIZE,
37
+    PARAM_COLOR,
38
+    PARAM_FACE,
39
+    PARAM_NUMBER
40
+};
41
+
42
+typedef struct {
43
+    char tag[128];
44
+    char param[PARAM_NUMBER][128];
45
+} SrtStack;
46
+
47
+static const char *srt_to_ass(AVCodecContext *avctx, char *out, char *out_end,
48
+                              const char *in, int x1, int y1, int x2, int y2)
49
+{
50
+    char c, *param, buffer[128], tmp[128];
51
+    int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
52
+    SrtStack stack[16];
53
+
54
+    stack[0].tag[0] = 0;
55
+    strcpy(stack[0].param[PARAM_SIZE],  "{\\fs}");
56
+    strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
57
+    strcpy(stack[0].param[PARAM_FACE],  "{\\fn}");
58
+
59
+    if (x1 >= 0 && y1 >= 0) {
60
+        if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1))
61
+            out += snprintf(out, out_end-out,
62
+                            "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2);
63
+        else
64
+            out += snprintf(out, out_end-out, "{\\an1}{\\pos(%d,%d)}", x1, y1);
65
+    }
66
+
67
+    for (; *in && out < out_end && !end; in++) {
68
+        switch (*in) {
69
+        case '\r':
70
+            break;
71
+        case '\n':
72
+            if (line_start) {
73
+                end = 1;
74
+                break;
75
+            }
76
+            while (out[-1] == ' ')
77
+                out--;
78
+            out += snprintf(out, out_end-out, "\\N");
79
+            line_start = 1;
80
+            break;
81
+        case ' ':
82
+            if (!line_start)
83
+                *out++ = *in;
84
+            break;
85
+        case '{':    /* skip all {\xxx} substrings except for {\an%d}
86
+                        and all microdvd like styles such as {Y:xxx} */
87
+            an += sscanf(in, "{\\an%*1u}%c", &c) == 1;
88
+            if ((an != 1 && sscanf(in, "{\\%*[^}]}%n%c", &len, &c) > 0) ||
89
+                sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n%c", &len, &c) > 0) {
90
+                in += len - 1;
91
+            } else
92
+                *out++ = *in;
93
+            break;
94
+        case '<':
95
+            tag_close = in[1] == '/';
96
+            if (sscanf(in+tag_close+1, "%128[^>]>%n%c", buffer, &len,&c) >= 2) {
97
+                if ((param = strchr(buffer, ' ')))
98
+                    *param++ = 0;
99
+                if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
100
+                    ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, buffer))) {
101
+                    int i, j, unknown = 0;
102
+                    in += len + tag_close;
103
+                    if (!tag_close)
104
+                        memset(stack+sptr, 0, sizeof(*stack));
105
+                    if (!strcmp(buffer, "font")) {
106
+                        if (tag_close) {
107
+                            for (i=PARAM_NUMBER-1; i>=0; i--)
108
+                                if (stack[sptr-1].param[i][0])
109
+                                    for (j=sptr-2; j>=0; j--)
110
+                                        if (stack[j].param[i][0]) {
111
+                                            out += snprintf(out, out_end-out,
112
+                                                            stack[j].param[i]);
113
+                                            break;
114
+                                        }
115
+                        } else {
116
+                            while (param) {
117
+                                if (!strncmp(param, "size=", 5)) {
118
+                                    unsigned font_size;
119
+                                    param += 5 + (param[5] == '"');
120
+                                    if (sscanf(param, "%u", &font_size) == 1) {
121
+                                        snprintf(stack[sptr].param[PARAM_SIZE],
122
+                                             sizeof(stack[0].param[PARAM_SIZE]),
123
+                                             "{\\fs%u}", font_size);
124
+                                    }
125
+                                } else if (!strncmp(param, "color=", 6)) {
126
+                                    param += 6 + (param[6] == '"');
127
+                                    snprintf(stack[sptr].param[PARAM_COLOR],
128
+                                         sizeof(stack[0].param[PARAM_COLOR]),
129
+                                         "{\\c&H%X&}",
130
+                                         html_color_parse(avctx, param));
131
+                                } else if (!strncmp(param, "face=", 5)) {
132
+                                    param += 5 + (param[5] == '"');
133
+                                    len = strcspn(param,
134
+                                                  param[-1] == '"' ? "\"" :" ");
135
+                                    av_strlcpy(tmp, param,
136
+                                               FFMIN(sizeof(tmp), len+1));
137
+                                    param += len;
138
+                                    snprintf(stack[sptr].param[PARAM_FACE],
139
+                                             sizeof(stack[0].param[PARAM_FACE]),
140
+                                             "{\\fn%s}", tmp);
141
+                                }
142
+                                if ((param = strchr(param, ' ')))
143
+                                    param++;
144
+                            }
145
+                            for (i=0; i<PARAM_NUMBER; i++)
146
+                                if (stack[sptr].param[i][0])
147
+                                    out += snprintf(out, out_end-out,
148
+                                                    stack[sptr].param[i]);
149
+                        }
150
+                    } else if (!buffer[1] && strspn(buffer, "bisu") == 1) {
151
+                        out += snprintf(out, out_end-out,
152
+                                        "{\\%c%d}", buffer[0], !tag_close);
153
+                    } else {
154
+                        unknown = 1;
155
+                        snprintf(tmp, sizeof(tmp), "</%s>", buffer);
156
+                    }
157
+                    if (tag_close) {
158
+                        sptr--;
159
+                    } else if (unknown && !strstr(in, tmp)) {
160
+                        in -= len + tag_close;
161
+                        *out++ = *in;
162
+                    } else
163
+                        av_strlcpy(stack[sptr++].tag, buffer,
164
+                                   sizeof(stack[0].tag));
165
+                    break;
166
+                }
167
+            }
168
+        default:
169
+            *out++ = *in;
170
+            break;
171
+        }
172
+        if (*in != ' ' && *in != '\r' && *in != '\n')
173
+            line_start = 0;
174
+    }
175
+
176
+    out = FFMIN(out, out_end-3);
177
+    while (!strncmp(out-2, "\\N", 2))
178
+        out -= 2;
179
+    while (out[-1] == ' ')
180
+        out--;
181
+    out += snprintf(out, out_end-out, "\r\n");
182
+    return in;
183
+}
184
+
185
+static const char *read_ts(const char *buf, int *ts_start, int *ts_end,
186
+                           int *x1, int *y1, int *x2, int *y2)
187
+{
188
+    int i, hs, ms, ss, he, me, se;
189
+
190
+    for (i=0; i<2; i++) {
191
+        /* try to read timestamps in either the first or second line */
192
+        int c = sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
193
+                       "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
194
+                       &hs, &ms, &ss, ts_start, &he, &me, &se, ts_end,
195
+                       x1, x2, y1, y2);
196
+        buf += strcspn(buf, "\n") + 1;
197
+        if (c >= 8) {
198
+            *ts_start = 100*(ss + 60*(ms + 60*hs)) + *ts_start/10;
199
+            *ts_end   = 100*(se + 60*(me + 60*he)) + *ts_end  /10;
200
+            return buf;
201
+        }
202
+    }
203
+    return NULL;
204
+}
205
+
206
+static int srt_decode_frame(AVCodecContext *avctx,
207
+                            void *data, int *got_sub_ptr, AVPacket *avpkt)
208
+{
209
+    AVSubtitle *sub = data;
210
+    int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
211
+    char buffer[2048];
212
+    const char *ptr = avpkt->data;
213
+
214
+    if (avpkt->size <= 0)
215
+        return avpkt->size;
216
+
217
+    ff_ass_init(sub);
218
+
219
+    while (*ptr) {
220
+        ptr = read_ts(ptr, &ts_start, &ts_end, &x1, &y1, &x2, &y2);
221
+        if (!ptr)
222
+            break;
223
+        ptr = srt_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr,
224
+                         x1, y1, x2, y2);
225
+        ff_ass_add_rect(sub, buffer, ts_start, ts_end, 0);
226
+    }
227
+
228
+    *got_sub_ptr = sub->num_rects > 0;
229
+    return avpkt->size;
230
+}
231
+
232
+AVCodec srt_decoder = {
233
+    .name         = "srt",
234
+    .long_name    = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
235
+    .type         = AVMEDIA_TYPE_SUBTITLE,
236
+    .id           = CODEC_ID_SRT,
237
+    .init         = ff_ass_subtitle_header_default,
238
+    .decode       = srt_decode_frame,
239
+};
... ...
@@ -282,6 +282,8 @@ FATE_TESTS += fate-smc
282 282
 fate-smc: CMD = framecrc  -i $(SAMPLES)/smc/cass_schi.qt -vsync 0 -pix_fmt rgb24
283 283
 FATE_TESTS += fate-sp5x
284 284
 fate-sp5x: CMD = framecrc  -idct simple -i $(SAMPLES)/sp5x/sp5x_problem.avi
285
+FATE_TESTS += fate-sub-srt
286
+fate-sub-srt: CMD = md5  -i $(SAMPLES)/sub/SubRip_capability_tester.srt -f ass
285 287
 FATE_TESTS += fate-sunraster-1bit-raw
286 288
 fate-sunraster-1bit-raw: CMD = framecrc  -i $(SAMPLES)/sunraster/lena-1bit-raw.sun
287 289
 FATE_TESTS += fate-sunraster-1bit-rle
288 290
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+03b2a3f7e7e83624c8e4d1b5569df758