]> git.sesse.net Git - ffmpeg/blob - libavcodec/htmlsubtitles.c
Merge commit '1202b712690c14f0efb06e4ad8b06c5b3df6822a'
[ffmpeg] / libavcodec / htmlsubtitles.c
1 /*
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 "libavutil/common.h"
23 #include "libavutil/parseutils.h"
24 #include "htmlsubtitles.h"
25
26 static int html_color_parse(void *log_ctx, const char *str)
27 {
28     uint8_t rgba[4];
29     if (av_parse_color(rgba, str, strcspn(str, "\" >"), log_ctx) < 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 SrtStack {
43     char tag[128];
44     char param[PARAM_NUMBER][128];
45 } SrtStack;
46
47 static void rstrip_spaces_buf(AVBPrint *buf)
48 {
49     if (av_bprint_is_complete(buf))
50         while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
51             buf->str[--buf->len] = 0;
52 }
53
54 int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
55 {
56     char *param, buffer[128], tmp[128];
57     int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
58     SrtStack stack[16];
59
60     stack[0].tag[0] = 0;
61     strcpy(stack[0].param[PARAM_SIZE],  "{\\fs}");
62     strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
63     strcpy(stack[0].param[PARAM_FACE],  "{\\fn}");
64
65     for (; !end && *in; in++) {
66         switch (*in) {
67         case '\r':
68             break;
69         case '\n':
70             if (line_start) {
71                 end = 1;
72                 break;
73             }
74             rstrip_spaces_buf(dst);
75             av_bprintf(dst, "\\N");
76             line_start = 1;
77             break;
78         case ' ':
79             if (!line_start)
80                 av_bprint_chars(dst, *in, 1);
81             break;
82         case '{':    /* skip all {\xxx} substrings except for {\an%d}
83                         and all microdvd like styles such as {Y:xxx} */
84             len = 0;
85             an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0;
86             if ((an != 1 && (len = 0, sscanf(in, "{\\%*[^}]}%n", &len) >= 0 && len > 0)) ||
87                 (len = 0, sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n", &len) >= 0 && len > 0)) {
88                 in += len - 1;
89             } else
90                 av_bprint_chars(dst, *in, 1);
91             break;
92         case '<':
93             tag_close = in[1] == '/';
94             len = 0;
95             if (sscanf(in+tag_close+1, "%127[^>]>%n", buffer, &len) >= 1 && len > 0) {
96                 const char *tagname = buffer;
97                 while (*tagname == ' ')
98                     tagname++;
99                 if ((param = strchr(tagname, ' ')))
100                     *param++ = 0;
101                 if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
102                     ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, tagname))) {
103                     int i, j, unknown = 0;
104                     in += len + tag_close;
105                     if (!tag_close)
106                         memset(stack+sptr, 0, sizeof(*stack));
107                     if (!strcmp(tagname, "font")) {
108                         if (tag_close) {
109                             for (i=PARAM_NUMBER-1; i>=0; i--)
110                                 if (stack[sptr-1].param[i][0])
111                                     for (j=sptr-2; j>=0; j--)
112                                         if (stack[j].param[i][0]) {
113                                             av_bprintf(dst, "%s", stack[j].param[i]);
114                                             break;
115                                         }
116                         } else {
117                             while (param) {
118                                 if (!strncmp(param, "size=", 5)) {
119                                     unsigned font_size;
120                                     param += 5 + (param[5] == '"');
121                                     if (sscanf(param, "%u", &font_size) == 1) {
122                                         snprintf(stack[sptr].param[PARAM_SIZE],
123                                              sizeof(stack[0].param[PARAM_SIZE]),
124                                              "{\\fs%u}", font_size);
125                                     }
126                                 } else if (!strncmp(param, "color=", 6)) {
127                                     param += 6 + (param[6] == '"');
128                                     snprintf(stack[sptr].param[PARAM_COLOR],
129                                          sizeof(stack[0].param[PARAM_COLOR]),
130                                          "{\\c&H%X&}",
131                                          html_color_parse(log_ctx, param));
132                                 } else if (!strncmp(param, "face=", 5)) {
133                                     param += 5 + (param[5] == '"');
134                                     len = strcspn(param,
135                                                   param[-1] == '"' ? "\"" :" ");
136                                     av_strlcpy(tmp, param,
137                                                FFMIN(sizeof(tmp), len+1));
138                                     param += len;
139                                     snprintf(stack[sptr].param[PARAM_FACE],
140                                              sizeof(stack[0].param[PARAM_FACE]),
141                                              "{\\fn%s}", tmp);
142                                 }
143                                 if ((param = strchr(param, ' ')))
144                                     param++;
145                             }
146                             for (i=0; i<PARAM_NUMBER; i++)
147                                 if (stack[sptr].param[i][0])
148                                     av_bprintf(dst, "%s", stack[sptr].param[i]);
149                         }
150                     } else if (tagname[0] && !tagname[1] && strspn(tagname, "bisu") == 1) {
151                         av_bprintf(dst, "{\\%c%d}", tagname[0], !tag_close);
152                     } else {
153                         unknown = 1;
154                         snprintf(tmp, sizeof(tmp), "</%s>", tagname);
155                     }
156                     if (tag_close) {
157                         sptr--;
158                     } else if (unknown && !strstr(in, tmp)) {
159                         in -= len + tag_close;
160                         av_bprint_chars(dst, *in, 1);
161                     } else
162                         av_strlcpy(stack[sptr++].tag, tagname,
163                                    sizeof(stack[0].tag));
164                     break;
165                 }
166             }
167         default:
168             av_bprint_chars(dst, *in, 1);
169             break;
170         }
171         if (*in != ' ' && *in != '\r' && *in != '\n')
172             line_start = 0;
173     }
174
175     if (!av_bprint_is_complete(dst))
176         return AVERROR(ENOMEM);
177
178     while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
179         dst->len -= 2;
180     dst->str[dst->len] = 0;
181     rstrip_spaces_buf(dst);
182
183     return 0;
184 }