2 * Subtitle reader with format autodetection
4 * Copyright (c) 2001 laaz
5 * Some code cleanup & realloc() by A'rpi/ESP-team
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
32 #include "subreader.h"
34 #include "subassconvert.h"
36 #include "stream/stream.h"
37 #include "libavutil/common.h"
38 #include "libavutil/avstring.h"
44 #define ERR ((void *) -1)
51 int suboverlap_enabled
= 1;
53 // Parameter struct for the format-specific readline functions
54 struct readline_args
{
59 /* Maximal length of line of a subtitle */
61 static float mpsub_position
=0;
62 static float mpsub_multiplier
=1.;
63 static int sub_slacktime
= 20000; //20 sec
65 int sub_no_text_pp
=0; // 1 => do not apply text post-processing
66 // like {\...} elimination in SSA format.
68 int sub_match_fuzziness
=0; // level of sub name matching fuzziness
70 /* Use the SUB_* constant defined in the header file */
71 int sub_format
=SUB_INVALID
;
74 Some subtitling formats, namely AQT and Subrip09, define the end of a
75 subtitle as the beginning of the following. Since currently we read one
76 subtitle at time, for these format we keep two global *subtitle,
77 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
78 so we can change its end when we read current subtitle starting time.
79 When CONFIG_SORTSUB is defined, we use a single global unsigned long,
80 previous_sub_end, for both (and even future) formats, to store the end of
81 the previous sub: it is initialized to 0 in sub_read_file and eventually
82 modified by sub_read_aqt_line or sub_read_subrip09_line.
84 unsigned long previous_sub_end
;
87 static int eol(char p
) {
88 return p
=='\r' || p
=='\n' || p
=='\0';
91 /* Remove leading and trailing space */
92 static void trail_space(char *s
) {
94 while (isspace(s
[i
])) ++i
;
95 if (i
) strcpy(s
, s
+ i
);
97 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
100 static char *stristr(const char *haystack
, const char *needle
) {
102 const char *p
= haystack
;
104 if (!(haystack
&& needle
)) return NULL
;
108 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
115 static void sami_add_line(subtitle
*current
, char *buffer
, char **pos
) {
119 if (*buffer
&& current
->lines
< SUB_MAX_TEXT
)
120 current
->text
[current
->lines
++] = strdup(buffer
);
124 static subtitle
*sub_read_line_sami(stream_t
* st
, subtitle
*current
,
125 struct readline_args
*args
)
127 int utf16
= args
->utf16
;
128 static char line
[LINE_LEN
+1];
129 static char *s
= NULL
, *slacktime_s
;
130 char text
[LINE_LEN
+1], *p
=NULL
, *q
;
133 current
->lines
= current
->start
= current
->end
= 0;
134 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
137 /* read the first line */
139 if (!(s
= stream_read_line(st
, line
, LINE_LEN
, utf16
))) return 0;
144 case 0: /* find "START=" or "Slacktime:" */
145 slacktime_s
= stristr (s
, "Slacktime:");
147 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
149 s
= stristr (s
, "Start=");
151 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
153 for (; *s
!= '>' && *s
!= '\0'; s
++);
159 case 1: /* find (optional) "<P", skip other TAGs */
160 for (; *s
== ' ' || *s
== '\t'; s
++); /* strip blanks, if any */
161 if (*s
== '\0') break;
162 if (*s
!= '<') { state
= 3; p
= text
; continue; } /* not a TAG */
164 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
165 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
171 case 2: /* find ">" */
172 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
175 case 3: /* get all text until '<' appears */
176 if (p
- text
>= LINE_LEN
)
177 sami_add_line(current
, text
, &p
);
178 if (*s
== '\0') break;
179 else if (!strncasecmp (s
, "<br>", 4)) {
180 sami_add_line(current
, text
, &p
);
183 else if ((*s
== '{') && !sub_no_text_pp
) { state
= 5; ++s
; continue; }
184 else if (*s
== '<') { state
= 4; }
185 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
186 else if (*s
== '\t') { *p
++ = ' '; s
++; }
187 else if (*s
== '\r' || *s
== '\n') { s
++; }
190 /* skip duplicated space */
191 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
195 case 4: /* get current->end or skip <TAG> */
196 q
= stristr (s
, "Start=");
198 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
199 *p
= '\0'; trail_space (text
);
201 current
->text
[current
->lines
++] = strdup (text
);
202 if (current
->lines
> 0) { state
= 99; break; }
206 if (s
) { s
++; state
= 3; continue; }
208 case 5: /* get rid of {...} text, but read the alignment code */
209 if ((*s
== '\\') && (*(s
+ 1) == 'a') && !sub_no_text_pp
) {
210 if (stristr(s
, "\\a1") != NULL
) {
211 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
214 if (stristr(s
, "\\a2") != NULL
) {
215 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
217 } else if (stristr(s
, "\\a3") != NULL
) {
218 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
220 } else if ((stristr(s
, "\\a4") != NULL
) || (stristr(s
, "\\a5") != NULL
) || (stristr(s
, "\\a8") != NULL
)) {
221 current
->alignment
= SUB_ALIGNMENT_TOPLEFT
;
223 } else if (stristr(s
, "\\a6") != NULL
) {
224 current
->alignment
= SUB_ALIGNMENT_TOPCENTER
;
226 } else if (stristr(s
, "\\a7") != NULL
) {
227 current
->alignment
= SUB_ALIGNMENT_TOPRIGHT
;
229 } else if (stristr(s
, "\\a9") != NULL
) {
230 current
->alignment
= SUB_ALIGNMENT_MIDDLELEFT
;
232 } else if (stristr(s
, "\\a10") != NULL
) {
233 current
->alignment
= SUB_ALIGNMENT_MIDDLECENTER
;
235 } else if (stristr(s
, "\\a11") != NULL
) {
236 current
->alignment
= SUB_ALIGNMENT_MIDDLERIGHT
;
240 if (*s
== '}') state
= 3;
246 if (state
!= 99 && !(s
= stream_read_line (st
, line
, LINE_LEN
, utf16
))) {
247 if (current
->start
> 0) {
248 break; // if it is the last subtitle
254 } while (state
!= 99);
256 // For the last subtitle
257 if (current
->end
<= 0) {
258 current
->end
= current
->start
+ sub_slacktime
;
259 sami_add_line(current
, text
, &p
);
266 static char *sub_readtext(char *source
, char **dest
) {
270 // printf("src=%p dest=%p \n",source,dest);
272 while ( !eol(*p
) && *p
!= '|' ) {
276 *dest
= malloc (len
+1);
277 if (!dest
) {return ERR
;}
279 strncpy(*dest
, source
, len
);
282 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
284 if (*p
) return p
; // not-last text field
285 else return NULL
; // last text field
288 static subtitle
*sub_read_line_microdvd(stream_t
*st
,subtitle
*current
,
289 struct readline_args
*args
)
291 int utf16
= args
->utf16
;
292 char line
[LINE_LEN
+1];
293 char line2
[LINE_LEN
+1];
298 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
299 } while ((sscanf (line
,
301 &(current
->start
), line2
) < 2) &&
303 "{%ld}{%ld}%[^\r\n]",
304 &(current
->start
), &(current
->end
), line2
) < 3));
306 if (args
->opts
->ass_enabled
) {
307 subassconvert_microdvd(line2
, line
, LINE_LEN
+ 1);
313 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
314 if (current
->text
[i
]==ERR
) {return ERR
;}
316 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
323 static subtitle
*sub_read_line_mpl2(stream_t
*st
,subtitle
*current
,
324 struct readline_args
*args
)
326 int utf16
= args
->utf16
;
327 char line
[LINE_LEN
+1];
328 char line2
[LINE_LEN
+1];
333 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
334 } while ((sscanf (line
,
335 "[%ld][%ld]%[^\r\n]",
336 &(current
->start
), &(current
->end
), line2
) < 3));
337 current
->start
*= 10;
342 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
343 if (current
->text
[i
]==ERR
) {return ERR
;}
345 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
352 static subtitle
*sub_read_line_subrip(stream_t
* st
, subtitle
*current
,
353 struct readline_args
*args
)
355 int utf16
= args
->utf16
;
356 char line
[LINE_LEN
+1];
357 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
358 char *p
=NULL
, *q
=NULL
;
362 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
363 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
364 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
365 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
367 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
370 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
371 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
372 current
->text
[current
->lines
-1]=malloc (len
+1);
373 if (!current
->text
[current
->lines
-1]) return ERR
;
374 strncpy (current
->text
[current
->lines
-1], q
, len
);
375 current
->text
[current
->lines
-1][len
]='\0';
376 if (!*p
|| *p
=='\r' || *p
=='\n') break;
378 else while (*p
++!=']');
385 static subtitle
*sub_ass_read_line_subviewer(stream_t
*st
, subtitle
*current
,
386 struct readline_args
*args
)
388 int utf16
= args
->utf16
;
389 int a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, j
= 0;
391 while (!current
->text
[0]) {
392 char line
[LINE_LEN
+ 1], full_line
[LINE_LEN
+ 1], sep
;
395 /* Parse SubRip header */
396 if (!stream_read_line(st
, line
, LINE_LEN
, utf16
))
398 if (sscanf(line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",
399 &a1
, &a2
, &a3
, &sep
, &a4
, &b1
, &b2
, &b3
, &sep
, &b4
) < 10)
402 current
->start
= a1
* 360000 + a2
* 6000 + a3
* 100 + a4
/ 10;
403 current
->end
= b1
* 360000 + b2
* 6000 + b3
* 100 + b4
/ 10;
407 for (i
= 0; i
< SUB_MAX_TEXT
; i
++) {
408 int blank
= 1, len
= 0;
411 if (!stream_read_line(st
, line
, LINE_LEN
, utf16
))
414 for (p
= line
; *p
!= '\n' && *p
!= '\r' && *p
; p
++, len
++)
415 if (*p
!= ' ' && *p
!= '\t')
423 if (!(j
+ 1 + len
< sizeof(full_line
) - 1))
427 full_line
[j
++] = '\n';
428 strcpy(&full_line
[j
], line
);
432 /* Use the ASS/SSA converter to transform the whole lines */
434 char converted_line
[LINE_LEN
+ 1];
435 subassconvert_subrip(full_line
, converted_line
, LINE_LEN
+ 1);
436 current
->text
[0] = strdup(converted_line
);
443 static subtitle
*sub_read_line_subviewer(stream_t
*st
,subtitle
*current
,
444 struct readline_args
*args
)
446 int utf16
= args
->utf16
;
447 char line
[LINE_LEN
+1];
448 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
452 if (args
->opts
->ass_enabled
)
453 return sub_ass_read_line_subviewer(st
, current
, args
);
454 while (!current
->text
[0]) {
455 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
456 if ((len
=sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",&a1
,&a2
,&a3
,(char *)&i
,&a4
,&b1
,&b2
,&b3
,(char *)&i
,&b4
)) < 10)
458 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
459 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
460 for (i
=0; i
<SUB_MAX_TEXT
;) {
462 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) break;
464 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++)
465 if (*p
!= ' ' && *p
!= '\t')
469 char *curptr
=current
->text
[i
]=malloc (len
+1);
470 if (!current
->text
[i
]) return ERR
;
471 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
473 /* let's filter html tags ::atmos */
500 static subtitle
*sub_read_line_subviewer2(stream_t
*st
,subtitle
*current
,
501 struct readline_args
*args
)
503 int utf16
= args
->utf16
;
504 char line
[LINE_LEN
+1];
509 while (!current
->text
[0]) {
510 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
513 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
515 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
516 for (i
=0; i
<SUB_MAX_TEXT
;) {
517 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) break;
518 if (line
[0]=='}') break;
520 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
522 current
->text
[i
]=malloc (len
+1);
523 if (!current
->text
[i
]) return ERR
;
524 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
536 static subtitle
*sub_read_line_vplayer(stream_t
*st
,subtitle
*current
,
537 struct readline_args
*args
)
539 int utf16
= args
->utf16
;
540 char line
[LINE_LEN
+1];
542 char *p
=NULL
, *next
,separator
;
545 while (!current
->text
[0]) {
546 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
547 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
550 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
554 // finds the body of the subtitle
561 printf("SUB: Skipping incorrect subtitle line!\n");
565 // by wodzu: hey! this time we know what length it has! what is
566 // that magic for? it can't deal with space instead of third
567 // colon! look, what simple it can be:
574 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
575 if (current
->text
[i
]==ERR
) {return ERR
;}
577 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
585 static subtitle
*sub_read_line_rt(stream_t
*st
,subtitle
*current
,
586 struct readline_args
*args
)
588 int utf16
= args
->utf16
;
590 //TODO: This format uses quite rich (sub/super)set of xhtml
591 // I couldn't check it since DTD is not included.
592 // WARNING: full XML parses can be required for proper parsing
593 char line
[LINE_LEN
+1];
594 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
595 char *p
=NULL
,*next
=NULL
;
598 while (!current
->text
[0]) {
599 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
600 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
601 //to describe the same moment in time. Maybe there are even more formats in use.
602 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
603 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
605 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
606 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
607 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
608 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
609 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
610 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 6) &&
611 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
,&plen
)) < 8) &&
612 //now try it without end time
613 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
614 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
615 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
616 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
619 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
620 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
621 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
622 current
->end
= current
->start
+200;
624 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
625 next
= strstr(line
,"<clear/>");
626 if(next
&& strlen(next
)>8){
628 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
629 if (current
->text
[i
]==ERR
) {return ERR
;}
631 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
639 static subtitle
*sub_read_line_ssa(stream_t
*st
,subtitle
*current
,
640 struct readline_args
*args
)
643 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
644 * other Sub Station Alpha scripts have only 8 commas before subtitle
645 * Reading the "ScriptType:" field is not reliable since many scripts appear
648 * http://www.scriptclub.org is a good place to find more examples
649 * http://www.eswat.demon.co.uk is where the SSA specs can be found
651 int utf16
= args
->utf16
;
653 static int max_comma
= 32; /* let's use 32 for the case that the */
654 /* amount of commas increase with newer SSA versions */
656 int hour1
, min1
, sec1
, hunsec1
,
657 hour2
, min2
, sec2
, hunsec2
, nothing
;
660 char line
[LINE_LEN
+1],
666 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
667 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d"
668 "%[^\n\r]", ¬hing
,
669 &hour1
, &min1
, &sec1
, &hunsec1
,
670 &hour2
, &min2
, &sec2
, &hunsec2
,
673 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d"
674 "%[^\n\r]", ¬hing
,
675 &hour1
, &min1
, &sec1
, &hunsec1
,
676 &hour2
, &min2
, &sec2
, &hunsec2
,
679 line2
=strchr(line3
, ',');
680 if (!line2
) return NULL
;
682 for (comma
= 4; comma
< max_comma
; comma
++)
685 if(!(tmp
=strchr(++tmp
, ','))) break;
686 if(*(++tmp
) == ' ') break;
687 /* a space after a comma means we're already in a sentence */
691 if(comma
< max_comma
)max_comma
= comma
;
692 /* eliminate the trailing comma */
693 if(*line2
== ',') line2
++;
695 current
->lines
=0;num
=0;
696 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
697 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
699 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
700 current
->text
[num
]=malloc(tmp
-line2
+1);
701 strncpy (current
->text
[num
], line2
, tmp
-line2
);
702 current
->text
[num
][tmp
-line2
]='\0';
706 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
709 current
->text
[num
]=strdup(line2
);
715 static void sub_pp_ssa(subtitle
*sub
) {
720 /* eliminate any text enclosed with {}, they are font and color settings */
721 so
=de
=sub
->text
[--l
];
723 if(*so
== '{' && so
[1]=='\\') {
724 for (start
=so
; *so
&& *so
!='}'; so
++);
725 if(*so
) so
++; else so
=start
;
737 * PJS subtitles reader.
738 * That's the "Phoenix Japanimation Society" format.
739 * I found some of them in http://www.scriptsclub.org/ (used for anime).
740 * The time is in tenths of second.
742 * by set, based on code by szabi (dunnowhat sub format ;-)
744 static subtitle
*sub_read_line_pjs(stream_t
*st
,subtitle
*current
,
745 struct readline_args
*args
)
747 int utf16
= args
->utf16
;
748 char line
[LINE_LEN
+1];
749 char text
[LINE_LEN
+1], *s
, *d
;
751 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
754 for (s
=line
; *s
&& isspace(*s
); s
++);
755 /* allow empty lines at the end of the file */
759 if (sscanf (s
, "%ld,%ld,", &(current
->start
),
760 &(current
->end
)) <2) {
763 /* the files I have are in tenths of second */
764 current
->start
*= 10;
766 /* walk to the beggining of the string */
767 for (; *s
; s
++) if (*s
==',') break;
769 for (s
++; *s
; s
++) if (*s
==',') break;
775 /* copy the string to the text buffer */
776 for (s
++, d
=text
; *s
&& *s
!='"'; s
++, d
++)
779 current
->text
[0] = strdup(text
);
785 static subtitle
*sub_read_line_mpsub(stream_t
*st
, subtitle
*current
,
786 struct readline_args
*args
)
788 int utf16
= args
->utf16
;
789 char line
[LINE_LEN
+1];
796 if (!stream_read_line(st
, line
, LINE_LEN
, utf16
)) return NULL
;
797 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
799 mpsub_position
+= a
*mpsub_multiplier
;
800 current
->start
=(int) mpsub_position
;
801 mpsub_position
+= b
*mpsub_multiplier
;
802 current
->end
=(int) mpsub_position
;
804 while (num
< SUB_MAX_TEXT
) {
805 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) {
806 if (num
== 0) return NULL
;
810 while (isspace(*p
)) p
++;
811 if (eol(*p
) && num
> 0) return current
;
812 if (eol(*p
)) return NULL
;
814 for (q
=p
; !eol(*q
); q
++);
817 current
->text
[num
]=strdup(p
);
818 // printf (">%s<\n",p);
819 current
->lines
= ++num
;
821 if (num
) return current
;
825 return NULL
; // we should have returned before if it's OK
828 #ifndef CONFIG_SORTSUB
829 //we don't need this if we use previous_sub_end
830 subtitle
*previous_aqt_sub
= NULL
;
833 static subtitle
*sub_read_line_aqt(stream_t
*st
,subtitle
*current
,
834 struct readline_args
*args
)
836 int utf16
= args
->utf16
;
837 char line
[LINE_LEN
+1];
842 // try to locate next subtitle
843 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
845 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
849 #ifdef CONFIG_SORTSUB
850 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
852 if (previous_aqt_sub
!= NULL
)
853 previous_aqt_sub
->end
= current
->start
-1;
855 previous_aqt_sub
= current
;
858 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
861 sub_readtext((char *) &line
,¤t
->text
[0]);
863 current
->end
= current
->start
; // will be corrected by next subtitle
865 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
869 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
870 if (current
->text
[i
]==ERR
) {return ERR
;}
872 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
876 if (!strlen(current
->text
[0]) && !strlen(current
->text
[1])) {
877 #ifdef CONFIG_SORTSUB
878 previous_sub_end
= 0;
880 // void subtitle -> end of previous marked and exit
881 previous_aqt_sub
= NULL
;
889 #ifndef CONFIG_SORTSUB
890 subtitle
*previous_subrip09_sub
= NULL
;
893 static subtitle
*sub_read_line_subrip09(stream_t
*st
,subtitle
*current
,
894 struct readline_args
*args
)
896 int utf16
= args
->utf16
;
897 char line
[LINE_LEN
+1];
903 // try to locate next subtitle
904 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
906 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
910 current
->start
= a1
*360000+a2
*6000+a3
*100;
912 #ifdef CONFIG_SORTSUB
913 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
915 if (previous_subrip09_sub
!= NULL
)
916 previous_subrip09_sub
->end
= current
->start
-1;
918 previous_subrip09_sub
= current
;
921 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
926 current
->text
[0]=""; // just to be sure that string is clear
928 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
929 if (current
->text
[i
]==ERR
) {return ERR
;}
931 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
935 if (!strlen(current
->text
[0]) && (i
==0)) {
936 #ifdef CONFIG_SORTSUB
937 previous_sub_end
= 0;
939 // void subtitle -> end of previous marked and exit
940 previous_subrip09_sub
= NULL
;
948 static subtitle
*sub_read_line_jacosub(stream_t
* st
, subtitle
* current
,
949 struct readline_args
*args
)
951 int utf16
= args
->utf16
;
952 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
953 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
954 static unsigned jacoTimeres
= 30;
955 static int jacoShift
= 0;
957 memset(current
, 0, sizeof(subtitle
));
958 memset(line1
, 0, LINE_LEN
);
959 memset(line2
, 0, LINE_LEN
);
960 memset(directive
, 0, LINE_LEN
);
961 while (!current
->text
[0]) {
962 if (!stream_read_line(st
, line1
, LINE_LEN
, utf16
)) {
966 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
967 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
968 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
969 if (line1
[0] == '#') {
970 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
972 unsigned units
= jacoShift
;
973 switch (toupper(line1
[1])) {
975 if (isalpha(line1
[2])) {
980 if (sscanf(&line1
[delta
], "%d", &hours
)) {
985 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
987 (&line1
[delta
], "%*d:%*d:%d",
989 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
993 sscanf(&line1
[delta
], "%d:%d.%d",
994 &minutes
, &seconds
, &units
);
999 sscanf(&line1
[delta
], "%d.%d", &seconds
,
1001 seconds
*= inverter
;
1004 ((hours
* 3600 + minutes
* 60 +
1005 seconds
) * jacoTimeres
+
1010 if (isalpha(line1
[2])) {
1015 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
1022 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
1025 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
1031 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
1032 jacoShift
) * 100.0 / jacoTimeres
);
1035 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
1036 jacoShift
) * 100.0 / jacoTimeres
);
1040 while ((*p
== ' ') || (*p
== '\t')) {
1043 if (isalpha(*p
)||*p
== '[') {
1046 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
1047 return (subtitle
*) ERR
;
1048 jLength
= strlen(directive
);
1049 for (cont
= 0; cont
< jLength
; ++cont
) {
1050 if (isalpha(*(directive
+ cont
)))
1051 *(directive
+ cont
) = toupper(*(directive
+ cont
));
1053 if ((strstr(directive
, "RDB") != NULL
)
1054 || (strstr(directive
, "RDC") != NULL
)
1055 || (strstr(directive
, "RLB") != NULL
)
1056 || (strstr(directive
, "RLG") != NULL
)) {
1059 if (strstr(directive
, "JL") != NULL
) {
1060 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
1061 } else if (strstr(directive
, "JR") != NULL
) {
1062 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
1064 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
1066 strcpy(line2
, line1
);
1069 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
1077 //the next line to get rid of a blank after the comment
1078 if ((*(p
+ 1)) == ' ')
1090 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
1098 if (*(p
+ 1) == 'n') {
1101 current
->text
[current
->lines
++] = strdup(line1
);
1105 if ((toupper(*(p
+ 1)) == 'C')
1106 || (toupper(*(p
+ 1)) == 'F')) {
1110 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
1111 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
1112 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
1116 if ((*(p
+ 1) == '\\') ||
1117 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
1119 } else if (eol(*(p
+ 1))) {
1120 if (!stream_read_line(st
, directive
, LINE_LEN
, utf16
))
1122 trail_space(directive
);
1123 av_strlcat(line2
, directive
, LINE_LEN
);
1134 if (current
->lines
< SUB_MAX_TEXT
)
1135 current
->text
[current
->lines
] = strdup(line1
);
1137 if (current
->lines
< SUB_MAX_TEXT
)
1142 static int sub_autodetect (stream_t
* st
, int *uses_time
, int utf16
) {
1143 char line
[LINE_LEN
+1];
1148 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
1151 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
1152 {*uses_time
=0;return SUB_MICRODVD
;}
1153 if (sscanf (line
, "{%d}{}", &i
)==1)
1154 {*uses_time
=0;return SUB_MICRODVD
;}
1155 if (sscanf (line
, "[%d][%d]", &i
, &i
)==2)
1156 {*uses_time
=1;return SUB_MPL2
;}
1157 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
1158 {*uses_time
=1;return SUB_SUBRIP
;}
1159 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
1160 {*uses_time
=1;return SUB_SUBVIEWER
;}
1161 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
1162 {*uses_time
=1;return SUB_SUBVIEWER2
;}
1163 if (strstr (line
, "<SAMI>"))
1164 {*uses_time
=1; return SUB_SAMI
;}
1165 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
1166 {*uses_time
= 1; return SUB_JACOSUB
;}
1167 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
1168 {*uses_time
= 1; return SUB_JACOSUB
;}
1169 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
1170 {*uses_time
=1;return SUB_VPLAYER
;}
1171 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
1172 {*uses_time
=1;return SUB_VPLAYER
;}
1173 if (!strncasecmp(line
, "<window", 7))
1174 {*uses_time
=1;return SUB_RT
;}
1175 if (!memcmp(line
, "Dialogue: Marked", 16))
1176 {*uses_time
=1; return SUB_SSA
;}
1177 if (!memcmp(line
, "Dialogue: ", 10))
1178 {*uses_time
=1; return SUB_SSA
;}
1179 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
1180 {*uses_time
=1;return SUB_PJS
;}
1181 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
1182 {*uses_time
=0; return SUB_MPSUB
;}
1183 if (!memcmp(line
, "FORMAT=TIME", 11))
1184 {*uses_time
=1; return SUB_MPSUB
;}
1185 if (strstr (line
, "-->>"))
1186 {*uses_time
=0; return SUB_AQTITLE
;}
1187 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
1188 {*uses_time
=1;return SUB_SUBRIP09
;}
1191 return SUB_INVALID
; // too many bad lines
1194 extern int sub_utf8
;
1195 int sub_utf8_prev
=0;
1197 extern float sub_delay
;
1198 extern float sub_fps
;
1201 static iconv_t icdsc
= (iconv_t
)(-1);
1203 void subcp_open (stream_t
*st
)
1205 char *tocp
= "UTF-8";
1208 const char *cp_tmp
= sub_cp
;
1210 char enca_lang
[3], enca_fallback
[100];
1211 if (sscanf(sub_cp
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
1212 || sscanf(sub_cp
, "ENCA:%2s:%99s", enca_lang
, enca_fallback
) == 2) {
1213 if (st
&& st
->flags
& MP_STREAM_SEEK
) {
1214 cp_tmp
= guess_cp(st
, enca_lang
, enca_fallback
);
1216 cp_tmp
= enca_fallback
;
1218 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: enca failed, stream must be seekable.\n");
1222 if ((icdsc
= iconv_open (tocp
, cp_tmp
)) != (iconv_t
)(-1)){
1223 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1226 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1230 void subcp_close (void)
1232 if (icdsc
!= (iconv_t
)(-1)){
1233 (void) iconv_close (icdsc
);
1234 icdsc
= (iconv_t
)(-1);
1235 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
1239 subtitle
* subcp_recode (subtitle
*sub
)
1242 size_t ileft
, oleft
;
1244 if(icdsc
== (iconv_t
)(-1)) return sub
;
1247 ip
= sub
->text
[--l
];
1251 if (!(ot
= malloc(oleft
+ 1))){
1252 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1256 if (iconv(icdsc
, &ip
, &ileft
,
1257 &op
, &oleft
) == (size_t)(-1)) {
1258 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line.\n");
1262 // In some stateful encodings, we must clear the state to handle the last character
1263 if (iconv(icdsc
, NULL
, NULL
,
1264 &op
, &oleft
) == (size_t)(-1)) {
1265 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line, can't clear encoding state.\n");
1268 free (sub
->text
[l
]);
1275 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1276 int sub_num
, int sub_uses_time
) {
1280 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1281 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1285 if (sub
->end
<= sub
->start
){
1286 sub
->end
= sub
->start
+ subfms
;
1293 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1294 // these subtitles overlap for less than 0.2 seconds
1295 // and would result in very short overlapping subtitle
1296 // so let's fix the problem here, before overlapping code
1297 // get its hands on them
1298 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1299 sub
->end
-= half
+ 1;
1300 nextsub
->start
+= delta
- half
;
1302 if (sub
->end
>= nextsub
->start
){
1303 sub
->end
= nextsub
->start
- 1;
1304 if (sub
->end
- sub
->start
> subfms
)
1305 sub
->end
= sub
->start
+ subfms
;
1312 * Movies are often converted from FILM (24 fps)
1313 * to PAL (25) by simply speeding it up, so we
1314 * to multiply the original timestmaps by
1315 * (Movie's FPS / Subtitle's (guessed) FPS)
1316 * so eg. for 23.98 fps movie and PAL time based
1317 * subtitles we say -subfps 25 and we're fine!
1320 /* timed sub fps correction ::atmos */
1321 /* the frame-based case is handled in mpcommon.c
1322 * where find_sub is called */
1323 if(sub_uses_time
&& sub_fps
) {
1324 sub
->start
*= sub_fps
/fps
;
1325 sub
->end
*= sub_fps
/fps
;
1331 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: Adjusted %d subtitle(s).\n", n
);
1335 subtitle
* (*read
)(stream_t
*st
, subtitle
*dest
,
1336 struct readline_args
*args
);
1337 void (*post
)(subtitle
*dest
);
1342 const char* guess_buffer_cp(unsigned char* buffer
, int buflen
, const char *preferred_language
, const char *fallback
)
1344 const char **languages
;
1346 EncaAnalyser analyser
;
1347 EncaEncoding encoding
;
1348 const char *detected_sub_cp
= NULL
;
1351 languages
= enca_get_languages(&langcnt
);
1352 mp_msg(MSGT_SUBREADER
, MSGL_V
, "ENCA supported languages: ");
1353 for (i
= 0; i
< langcnt
; i
++) {
1354 mp_msg(MSGT_SUBREADER
, MSGL_V
, "%s ", languages
[i
]);
1356 mp_msg(MSGT_SUBREADER
, MSGL_V
, "\n");
1358 for (i
= 0; i
< langcnt
; i
++) {
1359 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1360 analyser
= enca_analyser_alloc(languages
[i
]);
1361 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1362 enca_analyser_free(analyser
);
1363 if (encoding
.charset
!= ENCA_CS_UNKNOWN
) {
1364 detected_sub_cp
= enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
);
1371 if (!detected_sub_cp
) {
1372 detected_sub_cp
= fallback
;
1373 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detection failed: fallback to %s\n", fallback
);
1375 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", detected_sub_cp
);
1378 return detected_sub_cp
;
1381 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1382 const char* guess_cp(stream_t
*st
, const char *preferred_language
, const char *fallback
)
1385 unsigned char *buffer
;
1386 const char *detected_sub_cp
= NULL
;
1388 buffer
= malloc(MAX_GUESS_BUFFER_SIZE
);
1389 buflen
= stream_read(st
,buffer
, MAX_GUESS_BUFFER_SIZE
);
1391 detected_sub_cp
= guess_buffer_cp(buffer
, buflen
, preferred_language
, fallback
);
1397 return detected_sub_cp
;
1399 #undef MAX_GUESS_BUFFER_SIZE
1402 sub_data
* sub_read_file(char *filename
, float fps
, struct MPOpts
*opts
)
1406 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1407 subtitle
*first
, *second
, *sub
, *return_sub
, *alloced_sub
= NULL
;
1408 sub_data
*subt_data
;
1409 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1410 static const struct subreader sr
[]=
1412 { sub_read_line_microdvd
, NULL
, "microdvd" },
1413 { sub_read_line_subrip
, NULL
, "subrip" },
1414 { sub_read_line_subviewer
, NULL
, "subviewer" },
1415 { sub_read_line_sami
, NULL
, "sami" },
1416 { sub_read_line_vplayer
, NULL
, "vplayer" },
1417 { sub_read_line_rt
, NULL
, "rt" },
1418 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1419 { sub_read_line_pjs
, NULL
, "pjs" },
1420 { sub_read_line_mpsub
, NULL
, "mpsub" },
1421 { sub_read_line_aqt
, NULL
, "aqt" },
1422 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1423 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1424 { sub_read_line_jacosub
, NULL
, "jacosub" },
1425 { sub_read_line_mpl2
, NULL
, "mpl2" }
1427 const struct subreader
*srp
;
1429 if(filename
==NULL
) return NULL
; //qnx segfault
1430 fd
=open_stream (filename
, NULL
, NULL
); if (!fd
) return NULL
;
1432 sub_format
= SUB_INVALID
;
1433 for (utf16
= 0; sub_format
== SUB_INVALID
&& utf16
< 3; utf16
++) {
1434 sub_format
=sub_autodetect (fd
, &uses_time
, utf16
);
1440 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1441 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1443 mp_msg(MSGT_SUBREADER
, MSGL_V
, "SUB: Detected subtitle file format: %s\n", srp
->name
);
1446 sub_utf8_prev
=sub_utf8
;
1450 if ((l
=strlen(filename
))>4){
1451 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1453 if (l
>= strlen(exts
[k
]) && !strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1458 if (k
<0) subcp_open(fd
);
1463 first
=malloc(n_max
*sizeof(subtitle
));
1467 sub_utf8
=sub_utf8_prev
;
1472 #ifdef CONFIG_SORTSUB
1474 sub
= malloc(sizeof(subtitle
));
1475 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1476 //as the beginning of the following
1477 previous_sub_end
= 0;
1482 first
=realloc(first
,n_max
*sizeof(subtitle
));
1484 #ifndef CONFIG_SORTSUB
1485 sub
= &first
[sub_num
];
1487 memset(sub
, '\0', sizeof(subtitle
));
1488 sub
=srp
->read(fd
, sub
, &(struct readline_args
){utf16
, opts
});
1489 if(!sub
) break; // EOF
1491 if ((sub
!=ERR
) && sub_utf8
== 2) sub
=subcp_recode(sub
);
1502 // Apply any post processing that needs recoding first
1503 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1504 #ifdef CONFIG_SORTSUB
1505 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1506 first
[sub_num
].start
= sub
->start
;
1507 first
[sub_num
].end
= sub
->end
;
1508 first
[sub_num
].lines
= sub
->lines
;
1509 first
[sub_num
].alignment
= sub
->alignment
;
1510 for(i
= 0; i
< sub
->lines
; ++i
){
1511 first
[sub_num
].text
[i
] = sub
->text
[i
];
1513 if (previous_sub_end
){
1514 first
[sub_num
- 1].end
= previous_sub_end
;
1515 previous_sub_end
= 0;
1518 for(j
= sub_num
- 1; j
>= 0; --j
){
1519 first
[j
+ 1].start
= first
[j
].start
;
1520 first
[j
+ 1].end
= first
[j
].end
;
1521 first
[j
+ 1].lines
= first
[j
].lines
;
1522 first
[j
+ 1].alignment
= first
[j
].alignment
;
1523 for(i
= 0; i
< first
[j
].lines
; ++i
){
1524 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1526 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1527 first
[j
].start
= sub
->start
;
1528 first
[j
].end
= sub
->end
;
1529 first
[j
].lines
= sub
->lines
;
1530 first
[j
].alignment
= sub
->alignment
;
1531 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1532 first
[j
].text
[i
] = sub
->text
[i
];
1534 if (previous_sub_end
){
1535 first
[j
].end
= first
[j
- 1].end
;
1536 first
[j
- 1].end
= previous_sub_end
;
1537 previous_sub_end
= 0;
1544 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1554 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1555 mp_msg(MSGT_SUBREADER
, MSGL_V
,"SUB: Read %i subtitles, %i bad line(s).\n",
1563 // we do overlap if the user forced it (suboverlap_enable == 2) or
1564 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1565 // this is because usually overlapping subtitles are found in these formats,
1566 // while in others they are probably result of bad timing
1567 if ((suboverlap_enabled
== 2) ||
1568 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1569 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1570 // here we manage overlapping subtitles
1575 // for each subtitle in first[] we deal with its 'block' of
1577 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1578 unsigned long global_start
= first
[sub_first
].start
,
1579 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1580 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1581 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1582 char real_block
= 1;
1584 // here we find the number of subtitles inside the 'block'
1585 // and its span interval. this works well only with sorted
1587 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1589 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1590 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1591 global_start
= first
[sub_first
+ sub_to_add
].start
;
1593 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1594 global_end
= first
[sub_first
+ sub_to_add
].end
;
1598 /* Avoid n^2 memory use for the "placeholder" data structure
1599 * below with subtitles that have a huge number of
1600 * consecutive overlapping lines. */
1601 lines_to_add
= FFMIN(lines_to_add
, SUB_MAX_TEXT
);
1603 // we need a structure to keep trace of the screen lines
1604 // used by the subs, a 'placeholder'
1605 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1606 // from a block of sub_to_add+1 subs
1607 placeholder
= malloc(sizeof(int *) * counter
);
1608 for (i
= 0; i
< counter
; ++i
) {
1609 placeholder
[i
] = malloc(sizeof(int) * lines_to_add
);
1610 for (j
= 0; j
< lines_to_add
; ++j
) {
1611 placeholder
[i
][j
] = -1;
1616 local_end
= global_start
- 1;
1620 // here we find the beginning and the end of a new
1621 // subtitle in the block
1622 local_start
= local_end
+ 1;
1623 local_end
= global_end
;
1624 for (j
= 0; j
<= sub_to_add
; ++j
) {
1625 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1626 local_end
= first
[sub_first
+ j
].start
- 1;
1627 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1628 local_end
= first
[sub_first
+ j
].end
;
1631 // here we allocate the screen lines to subs we must
1632 // display in current local_start-local_end interval.
1633 // if the subs were yet presents in the previous interval
1634 // they keep the same lines, otherside they get unused lines
1635 for (j
= 0; j
<= sub_to_add
; ++j
) {
1636 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1637 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1640 int fragment_position
= -1;
1642 // if this is not the first new sub of the block
1643 // we find if this sub was present in the previous
1646 for (i
= 0; i
< lines_to_add
; ++i
) {
1647 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1648 placeholder
[counter
][i
] = sub_first
+ j
;
1655 // we are looking for the shortest among all groups of
1656 // sequential blank lines whose length is greater than or
1657 // equal to sub_lines. we store in fragment_position the
1658 // position of the shortest group, in fragment_length its
1659 // length, and in tmp the length of the group currently
1661 for (i
= 0; i
< lines_to_add
; ++i
) {
1662 if (placeholder
[counter
][i
] == -1) {
1663 // placeholder[counter][i] is part of the current group
1667 if (tmp
== sub_lines
) {
1668 // current group's size fits exactly the one we
1669 // need, so we stop looking
1670 fragment_position
= i
- tmp
;
1674 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1675 // current group is the best we found till here,
1676 // but is still bigger than the one we are looking
1677 // for, so we keep on looking
1678 fragment_length
= tmp
;
1679 fragment_position
= i
- tmp
;
1682 // current group doesn't fit at all, so we forget it
1688 // last screen line is blank, a group ends with it
1689 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1690 fragment_position
= i
- tmp
;
1693 if (fragment_position
== -1) {
1694 // it was not possible to find free screen line(s) for a subtitle,
1695 // usually this means a bug in the code; however we do not overlap
1696 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1697 higher_line
= SUB_MAX_TEXT
+ 1;
1700 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1701 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1706 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1707 if (placeholder
[counter
][j
] != -1)
1712 if (higher_line
>= SUB_MAX_TEXT
) {
1713 // the 'block' has too much lines, so we don't overlap the
1715 second
= realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1716 for (j
= 0; j
<= sub_to_add
; ++j
) {
1718 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1719 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1720 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1721 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1722 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1723 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1724 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1727 sub_num
+= sub_to_add
+ 1;
1728 sub_first
+= sub_to_add
;
1733 // we read the placeholder structure and create the new
1735 second
= realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1736 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1737 second
[sub_num
].start
= local_start
;
1738 second
[sub_num
].end
= local_end
;
1739 second
[sub_num
].alignment
= first
[sub_first
].alignment
;
1740 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1741 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1742 if (placeholder
[counter
][j
] != -1) {
1743 int lines
= first
[placeholder
[counter
][j
]].lines
;
1744 for (ls
= 0; ls
< lines
; ++ls
) {
1745 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1749 second
[sub_num
].text
[i
++] = strdup(" ");
1754 } while (local_end
< global_end
);
1756 for (i
= 0; i
< counter
; ++i
)
1757 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1759 counter
= 2 * sub_to_add
+ 1;
1760 for (i
= 0; i
< counter
; ++i
) {
1761 free(placeholder
[i
]);
1764 sub_first
+= sub_to_add
;
1767 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1768 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1769 free(first
[j
].text
[i
]);
1774 return_sub
= second
;
1775 } else { //if(suboverlap_enabled)
1776 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1779 if (return_sub
== NULL
) return NULL
;
1780 subt_data
= malloc(sizeof(sub_data
));
1781 subt_data
->filename
= strdup(filename
);
1782 subt_data
->sub_uses_time
= uses_time
;
1783 subt_data
->sub_num
= sub_num
;
1784 subt_data
->sub_errs
= sub_errs
;
1785 subt_data
->subtitles
= return_sub
;
1789 void list_sub_file(sub_data
* subd
){
1791 subtitle
*subs
= subd
->subtitles
;
1793 for(j
=0; j
< subd
->sub_num
; j
++){
1794 subtitle
* egysub
=&subs
[j
];
1795 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"%i line%c (%li-%li)\n",
1797 (1==egysub
->lines
)?' ':'s',
1800 for (i
=0; i
<egysub
->lines
; i
++) {
1801 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
1803 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\n");
1806 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Subtitle format %s time.\n",
1807 subd
->sub_uses_time
? "uses":"doesn't use");
1808 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
1811 void dump_srt(sub_data
* subd
, float fps
){
1817 subtitle
*subs
= subd
->subtitles
;
1819 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1821 fd
=fopen("dumpsub.srt","w");
1824 perror("dump_srt: fopen");
1827 for(i
=0; i
< subd
->sub_num
; i
++)
1829 onesub
=subs
+i
; //=&subs[i];
1830 fprintf(fd
,"%d\n",i
+1);//line number
1833 if (!subd
->sub_uses_time
)
1834 temp
= temp
* 100 / sub_fps
;
1835 temp
-= sub_delay
* 100;
1836 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
1837 m
=temp
/6000; temp
%=6000; //m =1*100*60
1838 s
=temp
/100; temp
%=100; //s =1*100
1839 ms
=temp
*10; //ms=1*10
1840 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
1843 if (!subd
->sub_uses_time
)
1844 temp
= temp
* 100 / sub_fps
;
1845 temp
-= sub_delay
* 100;
1846 h
=temp
/360000;temp
%=360000;
1847 m
=temp
/6000; temp
%=6000;
1848 s
=temp
/100; temp
%=100;
1850 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
1852 for(j
=0;j
<onesub
->lines
;j
++)
1853 fprintf(fd
,"%s\n",onesub
->text
[j
]);
1858 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
1861 void dump_mpsub(sub_data
* subd
, float fps
){
1865 subtitle
*subs
= subd
->subtitles
;
1867 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
1868 if (sub_fps
==0) sub_fps
=fps
;
1870 fd
=fopen ("dump.mpsub", "w");
1872 perror ("dump_mpsub: fopen");
1877 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
1878 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
1880 for(j
=0; j
< subd
->sub_num
; j
++){
1881 subtitle
* egysub
=&subs
[j
];
1882 if (subd
->sub_uses_time
) {
1883 a
=((egysub
->start
-mpsub_position
)/100.0);
1884 b
=((egysub
->end
-egysub
->start
)/100.0);
1885 if ( (float)((int)a
) == a
)
1886 fprintf (fd
, "%.0f",a
);
1888 fprintf (fd
, "%.2f",a
);
1890 if ( (float)((int)b
) == b
)
1891 fprintf (fd
, " %.0f\n",b
);
1893 fprintf (fd
, " %.2f\n",b
);
1895 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
1896 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
1899 mpsub_position
= egysub
->end
;
1900 for (i
=0; i
<egysub
->lines
; i
++) {
1901 fprintf (fd
, "%s\n",egysub
->text
[i
]);
1906 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
1909 void dump_microdvd(sub_data
* subd
, float fps
) {
1912 subtitle
*subs
= subd
->subtitles
;
1915 fd
= fopen("dumpsub.sub", "w");
1917 perror("dumpsub.sub: fopen");
1920 delay
= sub_delay
* sub_fps
;
1921 for (i
= 0; i
< subd
->sub_num
; ++i
) {
1923 start
= subs
[i
].start
;
1925 if (subd
->sub_uses_time
) {
1926 start
= start
* sub_fps
/ 100 ;
1927 end
= end
* sub_fps
/ 100;
1930 start
= start
* sub_fps
/ fps
;
1931 end
= end
* sub_fps
/ fps
;
1935 fprintf(fd
, "{%d}{%d}", start
, end
);
1936 for (j
= 0; j
< subs
[i
].lines
; ++j
)
1937 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
1941 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.sub\'.\n");
1944 void dump_jacosub(sub_data
* subd
, float fps
) {
1950 subtitle
*subs
= subd
->subtitles
;
1952 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1954 fd
=fopen("dumpsub.jss","w");
1957 perror("dump_jacosub: fopen");
1960 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
1961 for(i
=0; i
< subd
->sub_num
; i
++)
1963 onesub
=subs
+i
; //=&subs[i];
1966 if (!subd
->sub_uses_time
)
1967 temp
= temp
* 100 / sub_fps
;
1968 temp
-= sub_delay
* 100;
1969 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
1970 m
=temp
/6000; temp
%=6000; //m =1*100*60
1971 s
=temp
/100; temp
%=100; //s =1*100
1973 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
1976 if (!subd
->sub_uses_time
)
1977 temp
= temp
* 100 / sub_fps
;
1978 temp
-= sub_delay
* 100;
1979 h
=temp
/360000;temp
%=360000;
1980 m
=temp
/6000; temp
%=6000;
1981 s
=temp
/100; temp
%=100;
1983 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
1985 for(j
=0;j
<onesub
->lines
;j
++)
1986 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
1991 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
1994 void dump_sami(sub_data
* subd
, float fps
) {
1999 subtitle
*subs
= subd
->subtitles
;
2001 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2003 fd
=fopen("dumpsub.smi","w");
2006 perror("dump_jacosub: fopen");
2009 fprintf(fd
, "<SAMI>\n"
2011 " <STYLE TYPE=\"Text/css\">\n"
2013 " P {margin-left: 29pt; margin-right: 29pt; font-size: 24pt; text-align: center; font-family: Tahoma; font-weight: bold; color: #FCDD03; background-color: #000000;}\n"
2014 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2019 for(i
=0; i
< subd
->sub_num
; i
++)
2021 onesub
=subs
+i
; //=&subs[i];
2024 if (!subd
->sub_uses_time
)
2025 temp
= temp
* 100 / sub_fps
;
2026 temp
-= sub_delay
* 100;
2027 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2028 "\t <P>", temp
* 10);
2030 for(j
=0;j
<onesub
->lines
;j
++)
2031 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2036 if (!subd
->sub_uses_time
)
2037 temp
= temp
* 100 / sub_fps
;
2038 temp
-= sub_delay
* 100;
2039 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2040 "\t <P> \n", temp
* 10);
2042 fprintf(fd
, "</BODY>\n"
2045 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2048 void sub_free( sub_data
* subd
)
2052 if ( !subd
) return;
2054 for (i
= 0; i
< subd
->sub_num
; i
++)
2055 for (j
= 0; j
< subd
->subtitles
[i
].lines
; j
++)
2056 free( subd
->subtitles
[i
].text
[j
] );
2057 free( subd
->subtitles
);
2058 free( subd
->filename
);
2062 #define MAX_SUBLINE 512
2064 * \brief parse text and append it to subtitle in sub
2065 * \param sub subtitle struct to add text to
2066 * \param txt text to parse
2067 * \param len length of text in txt
2068 * \param endpts pts at which this subtitle text should be removed again
2070 * <> and {} are interpreted as comment delimiters, "\n", "\N", '\n', '\r'
2071 * and '\0' are interpreted as newlines, duplicate, leading and trailing
2072 * newlines are ignored.
2074 void sub_add_text(subtitle
*sub
, const char *txt
, int len
, double endpts
) {
2076 int double_newline
= 1; // ignore newlines at the beginning
2079 if (sub
->lines
>= SUB_MAX_TEXT
) return;
2081 buf
= malloc(MAX_SUBLINE
+ 1);
2082 sub
->text
[sub
->lines
] = buf
;
2083 sub
->endpts
[sub
->lines
] = endpts
;
2084 for (i
= 0; i
< len
&& pos
< MAX_SUBLINE
; i
++) {
2086 if (c
== '<') comment
|= 1;
2087 if (c
== '{') comment
|= 2;
2089 if (c
== '}') comment
&= ~2;
2090 if (c
== '>') comment
&= ~1;
2093 if (pos
== MAX_SUBLINE
- 1) {
2097 if (c
== '\\' && i
+ 1 < len
) {
2099 if (c
== 'n' || c
== 'N') c
= 0;
2101 if (c
== '\n' || c
== '\r') c
= 0;
2105 } else if (!double_newline
) {
2106 if (sub
->lines
>= SUB_MAX_TEXT
- 1) {
2107 mp_msg(MSGT_VO
, MSGL_WARN
, "Too many subtitle lines\n");
2114 buf
= malloc(MAX_SUBLINE
+ 1);
2115 sub
->text
[sub
->lines
] = buf
;
2116 sub
->endpts
[sub
->lines
] = endpts
;
2120 if (sub
->lines
< SUB_MAX_TEXT
&&
2121 strlen(sub
->text
[sub
->lines
]))
2126 * \brief remove outdated subtitle lines.
2127 * \param sub subtitle struct to modify
2128 * \param pts current pts. All lines with endpts <= this will be removed.
2129 * Use MP_NOPTS_VALUE to remove all lines
2130 * \return 1 if sub was modified, 0 otherwise.
2132 int sub_clear_text(subtitle
*sub
, double pts
) {
2135 while (i
< sub
->lines
) {
2136 double endpts
= sub
->endpts
[i
];
2137 if (pts
== MP_NOPTS_VALUE
|| (endpts
!= MP_NOPTS_VALUE
&& pts
>= endpts
)) {
2140 for (j
= i
+ 1; j
< sub
->lines
; j
++) {
2141 sub
->text
[j
- 1] = sub
->text
[j
];
2142 sub
->endpts
[j
- 1] = sub
->endpts
[j
];