diff subreader.c @ 21827:b0bc0d81f91b

Subtitle handling cleanup: factor out code for parsing embedded subtitles and adding and removing of lines in subtitle struct into subreader.c.
author reimar
date Sat, 06 Jan 2007 19:07:58 +0000
parents 4f5c3fbbbba5
children 326ddc4f8366
line wrap: on
line diff
--- a/subreader.c	Sat Jan 06 19:02:19 2007 +0000
+++ b/subreader.c	Sat Jan 06 19:07:58 2007 +0000
@@ -2254,6 +2254,77 @@
     free( subd );
 }
 
+#define MAX_SUBLINE 512
+void sub_add_text(subtitle *sub, const char *txt, int len, double endpts) {
+  int comment = 0;
+  int double_newline = 1; // ignore newlines at the beginning
+  int i, pos;
+  char *buf;
+  if (sub->lines >= SUB_MAX_TEXT) return;
+  pos = 0;
+  buf = malloc(MAX_SUBLINE + 1);
+  sub->text[sub->lines] = buf;
+  sub->endpts[sub->lines] = endpts;
+  for (i = 0; i < len && pos < MAX_SUBLINE; i++) {
+    char c = txt[i];
+    if (c == '<') comment |= 1;
+    if (c == '{') comment |= 2;
+    if (comment) {
+      if (c == '}') comment &= ~2;
+      if (c == '>') comment &= ~1;
+      continue;
+    }
+    if (pos == MAX_SUBLINE - 1) {
+      i--;
+      c = 0;
+    }
+    if (c == '\\' && i + 1 < len) {
+      c = txt[++i];
+      if (c == 'n' || c == 'N') c = 0;
+    }
+    if (c == '\n' || c == '\r') c = 0;
+    if (c) {
+      double_newline = 0;
+      buf[pos++] = c;
+    } else if (!double_newline) {
+      if (sub->lines >= SUB_MAX_TEXT - 1) {
+        mp_msg(MSGT_VO, MSGL_WARN, "Too many subtitle lines\n");
+        break;
+      }
+      double_newline = 1;
+      buf[pos] = 0;
+      sub->lines++;
+      pos = 0;
+      buf = malloc(MAX_SUBLINE + 1);      
+      sub->text[sub->lines] = buf;
+      sub->endpts[sub->lines] = endpts;
+    }
+  }
+  buf[pos] = 0;
+  if (sub->lines < SUB_MAX_TEXT &&
+      strlen(sub->text[sub->lines]))
+    sub->lines++;
+}
+
+#define MP_NOPTS_VALUE (-1LL<<63)
+int sub_clear_text(subtitle *sub, double pts) {
+  int i = 0;
+  int changed = 0;
+  while (i < sub->lines) {
+    double endpts = sub->endpts[i];
+    if (pts == MP_NOPTS_VALUE || (endpts != MP_NOPTS_VALUE && pts >= endpts)) {
+      int j;
+      free(sub->text[i]);
+      for (j = i + 1; j < sub->lines; j++)
+        sub->text[j - 1] = sub->text[j];
+      sub->lines--;
+      changed = 1;
+    } else
+      i++;
+  }
+  return changed;
+}
+
 #ifdef DUMPSUBS
 int main(int argc, char **argv) {  // for testing
     sub_data *subd;