diff options
author | reimar <reimar@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2007-01-06 19:07:58 +0000 |
---|---|---|
committer | reimar <reimar@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2007-01-06 19:07:58 +0000 |
commit | 61e4a801913f76695aa8445206d27720ba7bac8b (patch) | |
tree | fbf798e232b50df044361d232d3899e69e90b3f8 /subreader.c | |
parent | 20a7d46a88825b08eea0a3641bedfd53fcd5998d (diff) | |
download | mpv-61e4a801913f76695aa8445206d27720ba7bac8b.tar.bz2 mpv-61e4a801913f76695aa8445206d27720ba7bac8b.tar.xz |
Subtitle handling cleanup: factor out code for parsing embedded subtitles
and adding and removing of lines in subtitle struct into subreader.c.
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@21845 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'subreader.c')
-rw-r--r-- | subreader.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/subreader.c b/subreader.c index f7c714349a..932cf1f96f 100644 --- a/subreader.c +++ b/subreader.c @@ -2254,6 +2254,77 @@ void sub_free( sub_data * subd ) 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; |