summaryrefslogtreecommitdiffstats
path: root/sub/sd_ass.c
diff options
context:
space:
mode:
authorAvi Halachmi (:avih) <avihpit@yahoo.com>2021-07-22 20:23:13 +0300
committeravih <avih@users.noreply.github.com>2021-08-05 21:32:22 +0300
commitab689a33a86d806726ee551fb25994fd0918a2a2 (patch)
tree4f4c8381209c44788e80215495990a021cc71402 /sub/sd_ass.c
parent24357cb7b5a1a8ca4407c2e6d9316f07a7a5ef0d (diff)
downloadmpv-ab689a33a86d806726ee551fb25994fd0918a2a2.tar.bz2
mpv-ab689a33a86d806726ee551fb25994fd0918a2a2.tar.xz
sub: add filter text utils, use from filter-regex (no-op)
Add two stand-alone function to help with the text-extraction task which ass filters need. Makes it easier to add new filters without cargo-culting this functionality. Currently, on malformed event (which shouldn't happen), a warning is printed when a filter tries to extract the text, so if few filters are enabled, we'll get multiple warnings (like before) - not critical. The regex filter now uses these utils, the SDH filter not yet.
Diffstat (limited to 'sub/sd_ass.c')
-rw-r--r--sub/sd_ass.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/sub/sd_ass.c b/sub/sd_ass.c
index e5e12cbc04..0da6df41c8 100644
--- a/sub/sd_ass.c
+++ b/sub/sd_ass.c
@@ -945,3 +945,28 @@ static void mangle_colors(struct sd *sd, struct sub_bitmaps *parts)
sb->libass.color = MP_ASS_RGBA(rgb[0], rgb[1], rgb[2], a);
}
}
+
+int sd_ass_fmt_offset(const char *evt_fmt)
+{
+ // "Text" is always last (as it's arbitrary content in buf), e.g. format:
+ // "Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"
+ int n = 0;
+ while (evt_fmt && (evt_fmt = strchr(evt_fmt, ',')))
+ evt_fmt++, n++;
+ return n-1; // buffer is without the format's Start/End, with ReadOrder
+}
+
+bstr sd_ass_pkt_text(struct sd_filter *ft, struct demux_packet *pkt, int offset)
+{
+ // e.g. pkt->buffer ("4" is ReadOrder): "4,0,Default,,0,0,0,,fifth line"
+ bstr txt = {(char *)pkt->buffer, pkt->len}, t0 = txt;
+ while (offset-- > 0) {
+ int n = bstrchr(txt, ',');
+ if (n < 0) { // shouldn't happen
+ MP_WARN(ft, "Malformed event '%.*s'\n", BSTR_P(t0));
+ return (bstr){NULL, 0};
+ }
+ txt = bstr_cut(txt, n+1);
+ }
+ return txt;
+}