History

sd_ass: allow get_text to return more than 500 bytes

rcombs committed 2 years ago

Commit aa0a9ce1 parent e3fd2444 files · +56 −58Browse files

Unified · Split

4 files
  1. sub/filter_jsre.c +1 −1
  2. sub/filter_regex.c +1 −1
  3. sub/sd.h +5 −3
  4. sub/sd_ass.c +49 −53
sub/filter_jsre.c+1−1
@@ -115,7 +115,7 @@ static struct demux_packet *jsre_filter(struct sd_filter *ft, · 114 unmodified lines
115115 bool drop = false;
116116
117117 if (ft->opts->rf_plain)
118− sd_ass_to_plaintext(text, strlen(text), text);
118+ sd_ass_to_plaintext(&text, text);
119119
120120 for (int n = 0; n < p->num_regexes; n++) {
121121 int found, err = p_regexec(p->J, n, text, &found);
sub/filter_regex.c+1−1
@@ -64,7 +64,7 @@ static struct demux_packet *rf_filter(struct sd_filter *ft, · 63 unmodified lines
6464 bool drop = false;
6565
6666 if (ft->opts->rf_plain)
67− sd_ass_to_plaintext(text, strlen(text), text);
67+ sd_ass_to_plaintext(&text, text);
6868
6969 for (int n = 0; n < p->num_regexes; n++) {
7070 int err = regexec(&p->regexes[n], text, 0, NULL, 0);
sub/sd.h+5−3
@@ -107,8 +107,10 @@ int sd_ass_fmt_offset(const char *event_format); · 106 unmodified lines
107107 bstr sd_ass_pkt_text(struct sd_filter *ft, struct demux_packet *pkt, int offset);
108108
109109 // convert \0-terminated "Text" (ass) content to plaintext, possibly in-place.
110−// result.start is out, result.len is MIN(out_siz, strlen(in)) or smaller.
111−// if there's room: out[result.len] is set to \0. out == in is allowed.
112−bstr sd_ass_to_plaintext(char *out, size_t out_siz, const char *in);
110+// result.start is *out, result.len is strlen(in) or smaller.
111+// (*out)[result.len] is always set to \0. *out == in is allowed.
112+// *out must be a talloc-allocated buffer or NULL, and will be reallocated if needed.
113+// *out will not be reallocated if *out == in.
114+bstr sd_ass_to_plaintext(char **out, const char *in);
113115
114116 #endif
sub/sd_ass.c+49−53
@@ -51,7 +51,7 @@ struct sd_ass_priv { · 50 unmodified lines
5151 bool clear_once;
5252 struct mp_ass_packer *packer;
5353 struct sub_bitmap_copy_cache *copy_cache;
54− char last_text[500];
54+ bstr last_text;
5555 struct mp_image_params video_params;
5656 struct mp_image_params last_params;
5757 struct mp_osd_res osd;
@@ -709,30 +709,23 @@ done: · 651 unmodified lines
709709 return res;
710710 }
711711
712−struct buf {
713− char *start;
714− int size;
715− int len;
716−};
712+#define MAX_BUF_SIZE 1024 * 1024
713+#define MIN_EXPAND_SIZE 4096
717714
718−static void append(struct buf *b, char c)
715+static void append(bstr *b, char c)
719716 {
720− if (b->len < b->size) {
721− b->start[b->len] = c;
722− b->len++;
723− }
717+ bstr_xappend(NULL, b, (bstr){&c, 1});
724718 }
725719
726−static void ass_to_plaintext(struct buf *b, const char *in)
720+static void ass_to_plaintext(bstr *b, const char *in)
727721 {
728− bool in_tag = false;
729722 const char *open_tag_pos = NULL;
730723 bool in_drawing = false;
731724 while (*in) {
732− if (in_tag) {
725+ if (open_tag_pos) {
733726 if (in[0] == '}') {
734727 in += 1;
735− in_tag = false;
728+ open_tag_pos = NULL;
736729 } else if (in[0] == '\\' && in[1] == 'p' && in[2] != 'o') {
737730 in += 2;
738731 // Skip text between \pN and \p0 tags. A \p without a number
@@ -756,7 +749,6 @@ static void ass_to_plaintext(struct buf *b, const char *in) · 17 unmodified lines
756749 } else if (in[0] == '{') {
757750 open_tag_pos = in;
758751 in += 1;
759− in_tag = true;
760752 } else {
761753 if (!in_drawing)
762754 append(b, in[0]);
@@ -765,65 +757,64 @@ static void ass_to_plaintext(struct buf *b, const char *in) · 2 unmodified lines
765757 }
766758 }
767759 // A '{' without a closing '}' is always visible.
768− if (in_tag) {
769− while (*open_tag_pos)
770− append(b, *open_tag_pos++);
760+ if (open_tag_pos) {
761+ bstr_xappend(NULL, b, bstr0(open_tag_pos));
771762 }
772763 }
773764
774−// Empty string counts as whitespace. Reads s[len-1] even if there are \0s.
775−static bool is_whitespace_only(char *s, int len)
765+// Empty string counts as whitespace.
766+static bool is_whitespace_only(bstr b)
776767 {
777− for (int n = 0; n < len; n++) {
778− if (s[n] != ' ' && s[n] != '\t')
768+ for (int n = 0; n < b.len; n++) {
769+ if (b.start[n] != ' ' && b.start[n] != '\t')
779770 return false;
780771 }
781772 return true;
782773 }
783774
784−static char *get_text_buf(struct sd *sd, double pts, enum sd_text_type type)
775+static bstr get_text_buf(struct sd *sd, double pts, enum sd_text_type type)
785776 {
786777 struct sd_ass_priv *ctx = sd->priv;
787778 ASS_Track *track = ctx->ass_track;
788779
789780 if (pts == MP_NOPTS_VALUE)
790− return NULL;
781+ return (bstr){0};
791782 long long ipts = find_timestamp(sd, pts);
792783
793− struct buf b = {ctx->last_text, sizeof(ctx->last_text) - 1};
784+ bstr *b = &ctx->last_text;
785+
786+ if (!b->start)
787+ b->start = talloc_size(ctx, 4096);
788+
789+ b->len = 0;
794790
795791 for (int i = 0; i < track->n_events; ++i) {
796792 ASS_Event *event = track->events + i;
797793 if (ipts >= event->Start && ipts < event->Start + event->Duration) {
798794 if (event->Text) {
799− int start = b.len;
795+ int start = b->len;
800796 if (type == SD_TEXT_TYPE_PLAIN) {
801− ass_to_plaintext(&b, event->Text);
797+ ass_to_plaintext(b, event->Text);
802798 } else {
803− char *t = event->Text;
804− while (*t)
805− append(&b, *t++);
799+ bstr_xappend(NULL, b, bstr0(event->Text));
806800 }
807− if (is_whitespace_only(&b.start[start], b.len - start)) {
808− b.len = start;
801+ if (is_whitespace_only(bstr_cut(*b, start))) {
802+ b->len = start;
809803 } else {
810− append(&b, '\n');
804+ append(b, '\n');
811805 }
812806 }
813807 }
814808 }
815809
816− b.start[b.len] = '\0';
817−
818− if (b.len > 0 && b.start[b.len - 1] == '\n')
819− b.start[b.len - 1] = '\0';
810+ bstr_eatend(b, (bstr)bstr0_lit("\n"));
820811
821− return ctx->last_text;
812+ return *b;
822813 }
823814
824815 static char *get_text(struct sd *sd, double pts, enum sd_text_type type)
825816 {
826− return talloc_strdup(NULL, get_text_buf(sd, pts, type));
817+ return bstrto0(NULL, get_text_buf(sd, pts, type));
827818 }
828819
829820 static struct sd_times get_times(struct sd *sd, double pts)
@@ -862,20 +853,26 @@ static void fill_plaintext(struct sd *sd, double pts) · 32 unmodified lines
862853
863854 ass_flush_events(track);
864855
865− char *text = get_text_buf(sd, pts, SD_TEXT_TYPE_PLAIN);
866− if (!text)
856+ bstr text = get_text_buf(sd, pts, SD_TEXT_TYPE_PLAIN);
857+ if (!text.len)
867858 return;
868859
869860 bstr dst = {0};
870861
871− while (*text) {
872− if (*text == '{')
862+ while (text.len) {
863+ if (*text.start == '{') {
864+ bstr_xappend(NULL, &dst, bstr0("\\{"));
865+ text = bstr_cut(text, 1);
866+ } else if (*text.start == '\\') {
873867 bstr_xappend(NULL, &dst, bstr0("\\"));
874− bstr_xappend(NULL, &dst, (bstr){text, 1});
875− // Break ASS escapes with U+2060 WORD JOINER
876− if (*text == '\\')
868+ // Break ASS escapes with U+2060 WORD JOINER
877869 mp_append_utf8_bstr(NULL, &dst, 0x2060);
878− text++;
870+ text = bstr_cut(text, 1);
871+ }
872+
873+ int i = bstrcspn(text, "{\\");
874+ bstr_xappend(NULL, &dst, (bstr){text.start, i});
875+ text = bstr_cut(text, i);
879876 }
880877
881878 if (!dst.start)
@@ -1103,11 +1100,10 @@ bstr sd_ass_pkt_text(struct sd_filter *ft, struct demux_packet *pkt, int offset) · 221 unmodified lines
11031100 return txt;
11041101 }
11051102
1106−bstr sd_ass_to_plaintext(char *out, size_t out_siz, const char *in)
1103+bstr sd_ass_to_plaintext(char **out, const char *in)
11071104 {
1108− struct buf b = {out, out_siz, 0};
1105+ bstr b = {*out};
11091106 ass_to_plaintext(&b, in);
1110− if (b.len < out_siz)
1111− out[b.len] = 0;
1112− return (bstr){out, b.len};
1107+ *out = b.start;
1108+ return b;
11131109 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t