]> git.sesse.net Git - ffmpeg/blob - libavcodec/srtdec.c
afq: update remaining samples variable.
[ffmpeg] / libavcodec / srtdec.c
1 /*
2  * SubRip subtitle decoder
3  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/avstring.h"
23 #include "libavutil/common.h"
24 #include "libavutil/parseutils.h"
25 #include "avcodec.h"
26 #include "ass.h"
27
28 static int html_color_parse(AVCodecContext *avctx, const char *str)
29 {
30     uint8_t rgba[4];
31     if (av_parse_color(rgba, str, strcspn(str, "\" >"), avctx) < 0)
32         return -1;
33     return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
34 }
35
36 enum {
37     PARAM_UNKNOWN = -1,
38     PARAM_SIZE,
39     PARAM_COLOR,
40     PARAM_FACE,
41     PARAM_NUMBER
42 };
43
44 typedef struct {
45     char tag[128];
46     char param[PARAM_NUMBER][128];
47 } SrtStack;
48
49 static const char *srt_to_ass(AVCodecContext *avctx, char *out, char *out_end,
50                               const char *in, int x1, int y1, int x2, int y2)
51 {
52     char c, *param, buffer[128], tmp[128];
53     int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
54     SrtStack stack[16];
55
56     stack[0].tag[0] = 0;
57     strcpy(stack[0].param[PARAM_SIZE],  "{\\fs}");
58     strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
59     strcpy(stack[0].param[PARAM_FACE],  "{\\fn}");
60
61     if (x1 >= 0 && y1 >= 0) {
62         if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1))
63             snprintf(out, out_end-out,
64                             "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2);
65         else
66             snprintf(out, out_end-out, "{\\an1}{\\pos(%d,%d)}", x1, y1);
67         out += strlen(out);
68     }
69
70     for (; out < out_end && !end && *in; in++) {
71         switch (*in) {
72         case '\r':
73             break;
74         case '\n':
75             if (line_start) {
76                 end = 1;
77                 break;
78             }
79             while (out[-1] == ' ')
80                 out--;
81             snprintf(out, out_end-out, "\\N");
82             if(out<out_end) out += strlen(out);
83             line_start = 1;
84             break;
85         case ' ':
86             if (!line_start)
87                 *out++ = *in;
88             break;
89         case '{':    /* skip all {\xxx} substrings except for {\an%d}
90                         and all microdvd like styles such as {Y:xxx} */
91             an += sscanf(in, "{\\an%*1u}%c", &c) == 1;
92             if ((an != 1 && sscanf(in, "{\\%*[^}]}%n%c", &len, &c) > 0) ||
93                 sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n%c", &len, &c) > 0) {
94                 in += len - 1;
95             } else
96                 *out++ = *in;
97             break;
98         case '<':
99             tag_close = in[1] == '/';
100             if (sscanf(in+tag_close+1, "%127[^>]>%n%c", buffer, &len,&c) >= 2) {
101                 if ((param = strchr(buffer, ' ')))
102                     *param++ = 0;
103                 if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
104                     ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, buffer))) {
105                     int i, j, unknown = 0;
106                     in += len + tag_close;
107                     if (!tag_close)
108                         memset(stack+sptr, 0, sizeof(*stack));
109                     if (!strcmp(buffer, "font")) {
110                         if (tag_close) {
111                             for (i=PARAM_NUMBER-1; i>=0; i--)
112                                 if (stack[sptr-1].param[i][0])
113                                     for (j=sptr-2; j>=0; j--)
114                                         if (stack[j].param[i][0]) {
115                                             snprintf(out, out_end-out,
116                                                             "%s", stack[j].param[i]);
117                                             if(out<out_end) out += strlen(out);
118                                             break;
119                                         }
120                         } else {
121                             while (param) {
122                                 if (!strncmp(param, "size=", 5)) {
123                                     unsigned font_size;
124                                     param += 5 + (param[5] == '"');
125                                     if (sscanf(param, "%u", &font_size) == 1) {
126                                         snprintf(stack[sptr].param[PARAM_SIZE],
127                                              sizeof(stack[0].param[PARAM_SIZE]),
128                                              "{\\fs%u}", font_size);
129                                     }
130                                 } else if (!strncmp(param, "color=", 6)) {
131                                     param += 6 + (param[6] == '"');
132                                     snprintf(stack[sptr].param[PARAM_COLOR],
133                                          sizeof(stack[0].param[PARAM_COLOR]),
134                                          "{\\c&H%X&}",
135                                          html_color_parse(avctx, param));
136                                 } else if (!strncmp(param, "face=", 5)) {
137                                     param += 5 + (param[5] == '"');
138                                     len = strcspn(param,
139                                                   param[-1] == '"' ? "\"" :" ");
140                                     av_strlcpy(tmp, param,
141                                                FFMIN(sizeof(tmp), len+1));
142                                     param += len;
143                                     snprintf(stack[sptr].param[PARAM_FACE],
144                                              sizeof(stack[0].param[PARAM_FACE]),
145                                              "{\\fn%s}", tmp);
146                                 }
147                                 if ((param = strchr(param, ' ')))
148                                     param++;
149                             }
150                             for (i=0; i<PARAM_NUMBER; i++)
151                                 if (stack[sptr].param[i][0]) {
152                                     snprintf(out, out_end-out,
153                                                     "%s", stack[sptr].param[i]);
154                                     if(out<out_end) out += strlen(out);
155                                 }
156                         }
157                     } else if (!buffer[1] && strspn(buffer, "bisu") == 1) {
158                         snprintf(out, out_end-out,
159                                         "{\\%c%d}", buffer[0], !tag_close);
160                         if(out<out_end) out += strlen(out);
161                     } else {
162                         unknown = 1;
163                         snprintf(tmp, sizeof(tmp), "</%s>", buffer);
164                     }
165                     if (tag_close) {
166                         sptr--;
167                     } else if (unknown && !strstr(in, tmp)) {
168                         in -= len + tag_close;
169                         *out++ = *in;
170                     } else
171                         av_strlcpy(stack[sptr++].tag, buffer,
172                                    sizeof(stack[0].tag));
173                     break;
174                 }
175             }
176         default:
177             *out++ = *in;
178             break;
179         }
180         if (*in != ' ' && *in != '\r' && *in != '\n')
181             line_start = 0;
182     }
183
184     out = FFMIN(out, out_end-3);
185     while (!strncmp(out-2, "\\N", 2))
186         out -= 2;
187     while (out[-1] == ' ')
188         out--;
189     snprintf(out, out_end-out, "\r\n");
190     return in;
191 }
192
193 static const char *read_ts(const char *buf, int *ts_start, int *ts_end,
194                            int *x1, int *y1, int *x2, int *y2)
195 {
196     int i, hs, ms, ss, he, me, se;
197
198     for (i=0; i<2; i++) {
199         /* try to read timestamps in either the first or second line */
200         int c = sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
201                        "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
202                        &hs, &ms, &ss, ts_start, &he, &me, &se, ts_end,
203                        x1, x2, y1, y2);
204         buf += strcspn(buf, "\n") + 1;
205         if (c >= 8) {
206             *ts_start = 100*(ss + 60*(ms + 60*hs)) + *ts_start/10;
207             *ts_end   = 100*(se + 60*(me + 60*he)) + *ts_end  /10;
208             return buf;
209         }
210     }
211     return NULL;
212 }
213
214 static int srt_decode_frame(AVCodecContext *avctx,
215                             void *data, int *got_sub_ptr, AVPacket *avpkt)
216 {
217     AVSubtitle *sub = data;
218     int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
219     char buffer[2048];
220     const char *ptr = avpkt->data;
221     const char *end = avpkt->data + avpkt->size;
222
223     if (avpkt->size <= 0)
224         return avpkt->size;
225
226     while (ptr < end && *ptr) {
227         if (avctx->codec->id == CODEC_ID_SRT) {
228             ptr = read_ts(ptr, &ts_start, &ts_end, &x1, &y1, &x2, &y2);
229             if (!ptr)
230                 break;
231         } else {
232             // Do final divide-by-10 outside rescale to force rounding down.
233             ts_start = av_rescale_q(avpkt->pts,
234                                     avctx->time_base,
235                                     (AVRational){1,100});
236             ts_end   = av_rescale_q(avpkt->pts + avpkt->duration,
237                                     avctx->time_base,
238                                     (AVRational){1,100});
239         }
240         ptr = srt_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr,
241                          x1, y1, x2, y2);
242         ff_ass_add_rect(sub, buffer, ts_start, ts_end-ts_start, 0);
243     }
244
245     *got_sub_ptr = sub->num_rects > 0;
246     return avpkt->size;
247 }
248
249 #if CONFIG_SRT_DECODER
250 AVCodec ff_srt_decoder = {
251     .name         = "srt",
252     .long_name    = NULL_IF_CONFIG_SMALL("SubRip subtitle with embedded timing"),
253     .type         = AVMEDIA_TYPE_SUBTITLE,
254     .id           = AV_CODEC_ID_SRT,
255     .init         = ff_ass_subtitle_header_default,
256     .decode       = srt_decode_frame,
257 };
258 #endif
259
260 #if CONFIG_SUBRIP_DECODER
261 AVCodec ff_subrip_decoder = {
262     .name         = "subrip",
263     .long_name    = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
264     .type         = AVMEDIA_TYPE_SUBTITLE,
265     .id           = AV_CODEC_ID_SUBRIP,
266     .init         = ff_ass_subtitle_header_default,
267     .decode       = srt_decode_frame,
268 };
269 #endif