1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

avcodec/webvttdec: strip classes

If a supported tag has a class, e.g "<i.bold>" it is ignored entirely;
so for example "<i.bold>Hello</i>" would be converted to "Hello{\i0}"
instead of the intended "{\i1}Hello{\i0}".

Signed-off-by: Leon Grutters <gruttersleonbot2@gmail.com>
This commit is contained in:
Leon Grutters
2025-03-20 08:10:33 +01:00
committed by Leo Izen
parent 09c372a323
commit 9a32b86307

View File

@ -34,20 +34,41 @@ static const struct {
const char *from; const char *from;
const char *to; const char *to;
} webvtt_tag_replace[] = { } webvtt_tag_replace[] = {
{"<i>", "{\\i1}"}, {"</i>", "{\\i0}"},
{"<b>", "{\\b1}"}, {"</b>", "{\\b0}"},
{"<u>", "{\\u1}"}, {"</u>", "{\\u0}"},
{"{", "\\{{}"}, {"\\", "\\\xe2\x81\xa0"}, // escape to avoid ASS markup conflicts {"{", "\\{{}"}, {"\\", "\\\xe2\x81\xa0"}, // escape to avoid ASS markup conflicts
{"&gt;", ">"}, {"&lt;", "<"}, {"&gt;", ">"}, {"&lt;", "<"},
{"&lrm;", "\xe2\x80\x8e"}, {"&rlm;", "\xe2\x80\x8f"}, {"&lrm;", "\xe2\x80\x8e"}, {"&rlm;", "\xe2\x80\x8f"},
{"&amp;", "&"}, {"&nbsp;", "\\h"}, {"&amp;", "&"}, {"&nbsp;", "\\h"},
}; };
static const struct {
const char from[6];
const char to[6];
} webvtt_valid_tags[] = {
{"i", "{\\i1}"}, {"/i", "{\\i0}"},
{"b", "{\\b1}"}, {"/b", "{\\b0}"},
{"u", "{\\u1}"}, {"/u", "{\\u0}"},
};
static int webvtt_event_to_ass(AVBPrint *buf, const char *p) static int webvtt_event_to_ass(AVBPrint *buf, const char *p)
{ {
int i, again = 0, skip = 0; int i, again = 0;
while (*p) { while (*p) {
if (*p == '<') {
const char *tag_end = strchr(p, '>');
ptrdiff_t len;
if (!tag_end)
break;
len = tag_end - p + 1;
for (i = 0; i < FF_ARRAY_ELEMS(webvtt_valid_tags); i++) {
const char *from = webvtt_valid_tags[i].from;
if(!strncmp(p + 1, from, strlen(from))) {
av_bprintf(buf, "%s", webvtt_valid_tags[i].to);
break;
}
}
p += len;
again = 1;
}
for (i = 0; i < FF_ARRAY_ELEMS(webvtt_tag_replace); i++) { for (i = 0; i < FF_ARRAY_ELEMS(webvtt_tag_replace); i++) {
const char *from = webvtt_tag_replace[i].from; const char *from = webvtt_tag_replace[i].from;
@ -59,21 +80,14 @@ static int webvtt_event_to_ass(AVBPrint *buf, const char *p)
break; break;
} }
} }
if (!*p)
break;
if (again) { if (again) {
again = 0; again = 0;
skip = 0;
continue; continue;
} }
if (*p == '<') if (p[0] == '\n' && p[1])
skip = 1;
else if (*p == '>')
skip = 0;
else if (p[0] == '\n' && p[1])
av_bprintf(buf, "\\N"); av_bprintf(buf, "\\N");
else if (!skip && *p != '\r') else if (*p != '\r')
av_bprint_chars(buf, *p, 1); av_bprint_chars(buf, *p, 1);
p++; p++;
} }