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 current
->text
[current
->lines
] = strdup(line1
);
1140 static int sub_autodetect (stream_t
* st
, int *uses_time
, int utf16
) {
1141 char line
[LINE_LEN
+1];
1146 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
1149 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
1150 {*uses_time
=0;return SUB_MICRODVD
;}
1151 if (sscanf (line
, "{%d}{}", &i
)==1)
1152 {*uses_time
=0;return SUB_MICRODVD
;}
1153 if (sscanf (line
, "[%d][%d]", &i
, &i
)==2)
1154 {*uses_time
=1;return SUB_MPL2
;}
1155 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
1156 {*uses_time
=1;return SUB_SUBRIP
;}
1157 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
1158 {*uses_time
=1;return SUB_SUBVIEWER
;}
1159 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
1160 {*uses_time
=1;return SUB_SUBVIEWER2
;}
1161 if (strstr (line
, "<SAMI>"))
1162 {*uses_time
=1; return SUB_SAMI
;}
1163 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
1164 {*uses_time
= 1; return SUB_JACOSUB
;}
1165 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
1166 {*uses_time
= 1; return SUB_JACOSUB
;}
1167 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
1168 {*uses_time
=1;return SUB_VPLAYER
;}
1169 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
1170 {*uses_time
=1;return SUB_VPLAYER
;}
1171 if (!strncasecmp(line
, "<window", 7))
1172 {*uses_time
=1;return SUB_RT
;}
1173 if (!memcmp(line
, "Dialogue: Marked", 16))
1174 {*uses_time
=1; return SUB_SSA
;}
1175 if (!memcmp(line
, "Dialogue: ", 10))
1176 {*uses_time
=1; return SUB_SSA
;}
1177 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
1178 {*uses_time
=1;return SUB_PJS
;}
1179 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
1180 {*uses_time
=0; return SUB_MPSUB
;}
1181 if (!memcmp(line
, "FORMAT=TIME", 11))
1182 {*uses_time
=1; return SUB_MPSUB
;}
1183 if (strstr (line
, "-->>"))
1184 {*uses_time
=0; return SUB_AQTITLE
;}
1185 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
1186 {*uses_time
=1;return SUB_SUBRIP09
;}
1189 return SUB_INVALID
; // too many bad lines
1192 extern int sub_utf8
;
1193 int sub_utf8_prev
=0;
1195 extern float sub_delay
;
1196 extern float sub_fps
;
1199 static iconv_t icdsc
= (iconv_t
)(-1);
1201 void subcp_open (stream_t
*st
)
1203 char *tocp
= "UTF-8";
1206 const char *cp_tmp
= sub_cp
;
1208 char enca_lang
[3], enca_fallback
[100];
1209 if (sscanf(sub_cp
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
1210 || sscanf(sub_cp
, "ENCA:%2s:%99s", enca_lang
, enca_fallback
) == 2) {
1211 if (st
&& st
->flags
& MP_STREAM_SEEK
) {
1212 cp_tmp
= guess_cp(st
, enca_lang
, enca_fallback
);
1214 cp_tmp
= enca_fallback
;
1216 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: enca failed, stream must be seekable.\n");
1220 if ((icdsc
= iconv_open (tocp
, cp_tmp
)) != (iconv_t
)(-1)){
1221 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1224 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1228 void subcp_close (void)
1230 if (icdsc
!= (iconv_t
)(-1)){
1231 (void) iconv_close (icdsc
);
1232 icdsc
= (iconv_t
)(-1);
1233 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
1237 subtitle
* subcp_recode (subtitle
*sub
)
1240 size_t ileft
, oleft
;
1242 if(icdsc
== (iconv_t
)(-1)) return sub
;
1245 ip
= sub
->text
[--l
];
1249 if (!(ot
= malloc(oleft
+ 1))){
1250 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1254 if (iconv(icdsc
, &ip
, &ileft
,
1255 &op
, &oleft
) == (size_t)(-1)) {
1256 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line.\n");
1260 // In some stateful encodings, we must clear the state to handle the last character
1261 if (iconv(icdsc
, NULL
, NULL
,
1262 &op
, &oleft
) == (size_t)(-1)) {
1263 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line, can't clear encoding state.\n");
1266 free (sub
->text
[l
]);
1273 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1274 int sub_num
, int sub_uses_time
) {
1278 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1279 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1283 if (sub
->end
<= sub
->start
){
1284 sub
->end
= sub
->start
+ subfms
;
1291 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1292 // these subtitles overlap for less than 0.2 seconds
1293 // and would result in very short overlapping subtitle
1294 // so let's fix the problem here, before overlapping code
1295 // get its hands on them
1296 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1297 sub
->end
-= half
+ 1;
1298 nextsub
->start
+= delta
- half
;
1300 if (sub
->end
>= nextsub
->start
){
1301 sub
->end
= nextsub
->start
- 1;
1302 if (sub
->end
- sub
->start
> subfms
)
1303 sub
->end
= sub
->start
+ subfms
;
1310 * Movies are often converted from FILM (24 fps)
1311 * to PAL (25) by simply speeding it up, so we
1312 * to multiply the original timestmaps by
1313 * (Movie's FPS / Subtitle's (guessed) FPS)
1314 * so eg. for 23.98 fps movie and PAL time based
1315 * subtitles we say -subfps 25 and we're fine!
1318 /* timed sub fps correction ::atmos */
1319 /* the frame-based case is handled in mpcommon.c
1320 * where find_sub is called */
1321 if(sub_uses_time
&& sub_fps
) {
1322 sub
->start
*= sub_fps
/fps
;
1323 sub
->end
*= sub_fps
/fps
;
1329 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: Adjusted %d subtitle(s).\n", n
);
1333 subtitle
* (*read
)(stream_t
*st
, subtitle
*dest
,
1334 struct readline_args
*args
);
1335 void (*post
)(subtitle
*dest
);
1340 const char* guess_buffer_cp(unsigned char* buffer
, int buflen
, const char *preferred_language
, const char *fallback
)
1342 const char **languages
;
1344 EncaAnalyser analyser
;
1345 EncaEncoding encoding
;
1346 const char *detected_sub_cp
= NULL
;
1349 languages
= enca_get_languages(&langcnt
);
1350 mp_msg(MSGT_SUBREADER
, MSGL_V
, "ENCA supported languages: ");
1351 for (i
= 0; i
< langcnt
; i
++) {
1352 mp_msg(MSGT_SUBREADER
, MSGL_V
, "%s ", languages
[i
]);
1354 mp_msg(MSGT_SUBREADER
, MSGL_V
, "\n");
1356 for (i
= 0; i
< langcnt
; i
++) {
1357 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1358 analyser
= enca_analyser_alloc(languages
[i
]);
1359 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1360 enca_analyser_free(analyser
);
1361 if (encoding
.charset
!= ENCA_CS_UNKNOWN
) {
1362 detected_sub_cp
= enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
);
1369 if (!detected_sub_cp
) {
1370 detected_sub_cp
= fallback
;
1371 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detection failed: fallback to %s\n", fallback
);
1373 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", detected_sub_cp
);
1376 return detected_sub_cp
;
1379 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1380 const char* guess_cp(stream_t
*st
, const char *preferred_language
, const char *fallback
)
1383 unsigned char *buffer
;
1384 const char *detected_sub_cp
= NULL
;
1386 buffer
= malloc(MAX_GUESS_BUFFER_SIZE
);
1387 buflen
= stream_read(st
,buffer
, MAX_GUESS_BUFFER_SIZE
);
1389 detected_sub_cp
= guess_buffer_cp(buffer
, buflen
, preferred_language
, fallback
);
1395 return detected_sub_cp
;
1397 #undef MAX_GUESS_BUFFER_SIZE
1400 sub_data
* sub_read_file(char *filename
, float fps
, struct MPOpts
*opts
)
1404 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1405 subtitle
*first
, *second
, *sub
, *return_sub
, *alloced_sub
= NULL
;
1406 sub_data
*subt_data
;
1407 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1408 static const struct subreader sr
[]=
1410 { sub_read_line_microdvd
, NULL
, "microdvd" },
1411 { sub_read_line_subrip
, NULL
, "subrip" },
1412 { sub_read_line_subviewer
, NULL
, "subviewer" },
1413 { sub_read_line_sami
, NULL
, "sami" },
1414 { sub_read_line_vplayer
, NULL
, "vplayer" },
1415 { sub_read_line_rt
, NULL
, "rt" },
1416 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1417 { sub_read_line_pjs
, NULL
, "pjs" },
1418 { sub_read_line_mpsub
, NULL
, "mpsub" },
1419 { sub_read_line_aqt
, NULL
, "aqt" },
1420 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1421 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1422 { sub_read_line_jacosub
, NULL
, "jacosub" },
1423 { sub_read_line_mpl2
, NULL
, "mpl2" }
1425 const struct subreader
*srp
;
1427 if(filename
==NULL
) return NULL
; //qnx segfault
1428 fd
=open_stream (filename
, NULL
, NULL
); if (!fd
) return NULL
;
1430 sub_format
= SUB_INVALID
;
1431 for (utf16
= 0; sub_format
== SUB_INVALID
&& utf16
< 3; utf16
++) {
1432 sub_format
=sub_autodetect (fd
, &uses_time
, utf16
);
1438 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1439 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1441 mp_msg(MSGT_SUBREADER
, MSGL_V
, "SUB: Detected subtitle file format: %s\n", srp
->name
);
1444 sub_utf8_prev
=sub_utf8
;
1448 if ((l
=strlen(filename
))>4){
1449 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1451 if (l
>= strlen(exts
[k
]) && !strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1456 if (k
<0) subcp_open(fd
);
1461 first
=malloc(n_max
*sizeof(subtitle
));
1465 sub_utf8
=sub_utf8_prev
;
1470 #ifdef CONFIG_SORTSUB
1472 sub
= malloc(sizeof(subtitle
));
1473 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1474 //as the beginning of the following
1475 previous_sub_end
= 0;
1480 first
=realloc(first
,n_max
*sizeof(subtitle
));
1482 #ifndef CONFIG_SORTSUB
1483 sub
= &first
[sub_num
];
1485 memset(sub
, '\0', sizeof(subtitle
));
1486 sub
=srp
->read(fd
, sub
, &(struct readline_args
){utf16
, opts
});
1487 if(!sub
) break; // EOF
1489 if ((sub
!=ERR
) && sub_utf8
== 2) sub
=subcp_recode(sub
);
1500 // Apply any post processing that needs recoding first
1501 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1502 #ifdef CONFIG_SORTSUB
1503 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1504 first
[sub_num
].start
= sub
->start
;
1505 first
[sub_num
].end
= sub
->end
;
1506 first
[sub_num
].lines
= sub
->lines
;
1507 first
[sub_num
].alignment
= sub
->alignment
;
1508 for(i
= 0; i
< sub
->lines
; ++i
){
1509 first
[sub_num
].text
[i
] = sub
->text
[i
];
1511 if (previous_sub_end
){
1512 first
[sub_num
- 1].end
= previous_sub_end
;
1513 previous_sub_end
= 0;
1516 for(j
= sub_num
- 1; j
>= 0; --j
){
1517 first
[j
+ 1].start
= first
[j
].start
;
1518 first
[j
+ 1].end
= first
[j
].end
;
1519 first
[j
+ 1].lines
= first
[j
].lines
;
1520 first
[j
+ 1].alignment
= first
[j
].alignment
;
1521 for(i
= 0; i
< first
[j
].lines
; ++i
){
1522 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1524 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1525 first
[j
].start
= sub
->start
;
1526 first
[j
].end
= sub
->end
;
1527 first
[j
].lines
= sub
->lines
;
1528 first
[j
].alignment
= sub
->alignment
;
1529 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1530 first
[j
].text
[i
] = sub
->text
[i
];
1532 if (previous_sub_end
){
1533 first
[j
].end
= first
[j
- 1].end
;
1534 first
[j
- 1].end
= previous_sub_end
;
1535 previous_sub_end
= 0;
1542 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1552 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1553 mp_msg(MSGT_SUBREADER
, MSGL_V
,"SUB: Read %i subtitles, %i bad line(s).\n",
1561 // we do overlap if the user forced it (suboverlap_enable == 2) or
1562 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1563 // this is because usually overlapping subtitles are found in these formats,
1564 // while in others they are probably result of bad timing
1565 if ((suboverlap_enabled
== 2) ||
1566 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1567 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1568 // here we manage overlapping subtitles
1573 // for each subtitle in first[] we deal with its 'block' of
1575 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1576 unsigned long global_start
= first
[sub_first
].start
,
1577 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1578 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1579 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1580 char real_block
= 1;
1582 // here we find the number of subtitles inside the 'block'
1583 // and its span interval. this works well only with sorted
1585 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1587 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1588 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1589 global_start
= first
[sub_first
+ sub_to_add
].start
;
1591 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1592 global_end
= first
[sub_first
+ sub_to_add
].end
;
1596 /* Avoid n^2 memory use for the "placeholder" data structure
1597 * below with subtitles that have a huge number of
1598 * consecutive overlapping lines. */
1599 lines_to_add
= FFMIN(lines_to_add
, SUB_MAX_TEXT
);
1601 // we need a structure to keep trace of the screen lines
1602 // used by the subs, a 'placeholder'
1603 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1604 // from a block of sub_to_add+1 subs
1605 placeholder
= malloc(sizeof(int *) * counter
);
1606 for (i
= 0; i
< counter
; ++i
) {
1607 placeholder
[i
] = malloc(sizeof(int) * lines_to_add
);
1608 for (j
= 0; j
< lines_to_add
; ++j
) {
1609 placeholder
[i
][j
] = -1;
1614 local_end
= global_start
- 1;
1618 // here we find the beginning and the end of a new
1619 // subtitle in the block
1620 local_start
= local_end
+ 1;
1621 local_end
= global_end
;
1622 for (j
= 0; j
<= sub_to_add
; ++j
) {
1623 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1624 local_end
= first
[sub_first
+ j
].start
- 1;
1625 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1626 local_end
= first
[sub_first
+ j
].end
;
1629 // here we allocate the screen lines to subs we must
1630 // display in current local_start-local_end interval.
1631 // if the subs were yet presents in the previous interval
1632 // they keep the same lines, otherside they get unused lines
1633 for (j
= 0; j
<= sub_to_add
; ++j
) {
1634 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1635 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1638 int fragment_position
= -1;
1640 // if this is not the first new sub of the block
1641 // we find if this sub was present in the previous
1644 for (i
= 0; i
< lines_to_add
; ++i
) {
1645 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1646 placeholder
[counter
][i
] = sub_first
+ j
;
1653 // we are looking for the shortest among all groups of
1654 // sequential blank lines whose length is greater than or
1655 // equal to sub_lines. we store in fragment_position the
1656 // position of the shortest group, in fragment_length its
1657 // length, and in tmp the length of the group currently
1659 for (i
= 0; i
< lines_to_add
; ++i
) {
1660 if (placeholder
[counter
][i
] == -1) {
1661 // placeholder[counter][i] is part of the current group
1665 if (tmp
== sub_lines
) {
1666 // current group's size fits exactly the one we
1667 // need, so we stop looking
1668 fragment_position
= i
- tmp
;
1672 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1673 // current group is the best we found till here,
1674 // but is still bigger than the one we are looking
1675 // for, so we keep on looking
1676 fragment_length
= tmp
;
1677 fragment_position
= i
- tmp
;
1680 // current group doesn't fit at all, so we forget it
1686 // last screen line is blank, a group ends with it
1687 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1688 fragment_position
= i
- tmp
;
1691 if (fragment_position
== -1) {
1692 // it was not possible to find free screen line(s) for a subtitle,
1693 // usually this means a bug in the code; however we do not overlap
1694 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1695 higher_line
= SUB_MAX_TEXT
+ 1;
1698 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1699 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1704 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1705 if (placeholder
[counter
][j
] != -1)
1710 if (higher_line
>= SUB_MAX_TEXT
) {
1711 // the 'block' has too much lines, so we don't overlap the
1713 second
= realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1714 for (j
= 0; j
<= sub_to_add
; ++j
) {
1716 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1717 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1718 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1719 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1720 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1721 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1722 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1725 sub_num
+= sub_to_add
+ 1;
1726 sub_first
+= sub_to_add
;
1731 // we read the placeholder structure and create the new
1733 second
= realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1734 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1735 second
[sub_num
].start
= local_start
;
1736 second
[sub_num
].end
= local_end
;
1737 second
[sub_num
].alignment
= first
[sub_first
].alignment
;
1738 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1739 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1740 if (placeholder
[counter
][j
] != -1) {
1741 int lines
= first
[placeholder
[counter
][j
]].lines
;
1742 for (ls
= 0; ls
< lines
; ++ls
) {
1743 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1747 second
[sub_num
].text
[i
++] = strdup(" ");
1752 } while (local_end
< global_end
);
1754 for (i
= 0; i
< counter
; ++i
)
1755 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1757 counter
= 2 * sub_to_add
+ 1;
1758 for (i
= 0; i
< counter
; ++i
) {
1759 free(placeholder
[i
]);
1762 sub_first
+= sub_to_add
;
1765 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1766 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1767 free(first
[j
].text
[i
]);
1772 return_sub
= second
;
1773 } else { //if(suboverlap_enabled)
1774 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1777 if (return_sub
== NULL
) return NULL
;
1778 subt_data
= malloc(sizeof(sub_data
));
1779 subt_data
->filename
= strdup(filename
);
1780 subt_data
->sub_uses_time
= uses_time
;
1781 subt_data
->sub_num
= sub_num
;
1782 subt_data
->sub_errs
= sub_errs
;
1783 subt_data
->subtitles
= return_sub
;
1787 void list_sub_file(sub_data
* subd
){
1789 subtitle
*subs
= subd
->subtitles
;
1791 for(j
=0; j
< subd
->sub_num
; j
++){
1792 subtitle
* egysub
=&subs
[j
];
1793 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"%i line%c (%li-%li)\n",
1795 (1==egysub
->lines
)?' ':'s',
1798 for (i
=0; i
<egysub
->lines
; i
++) {
1799 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
1801 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\n");
1804 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Subtitle format %s time.\n",
1805 subd
->sub_uses_time
? "uses":"doesn't use");
1806 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
1809 void dump_srt(sub_data
* subd
, float fps
){
1815 subtitle
*subs
= subd
->subtitles
;
1817 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1819 fd
=fopen("dumpsub.srt","w");
1822 perror("dump_srt: fopen");
1825 for(i
=0; i
< subd
->sub_num
; i
++)
1827 onesub
=subs
+i
; //=&subs[i];
1828 fprintf(fd
,"%d\n",i
+1);//line number
1831 if (!subd
->sub_uses_time
)
1832 temp
= temp
* 100 / sub_fps
;
1833 temp
-= sub_delay
* 100;
1834 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
1835 m
=temp
/6000; temp
%=6000; //m =1*100*60
1836 s
=temp
/100; temp
%=100; //s =1*100
1837 ms
=temp
*10; //ms=1*10
1838 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
1841 if (!subd
->sub_uses_time
)
1842 temp
= temp
* 100 / sub_fps
;
1843 temp
-= sub_delay
* 100;
1844 h
=temp
/360000;temp
%=360000;
1845 m
=temp
/6000; temp
%=6000;
1846 s
=temp
/100; temp
%=100;
1848 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
1850 for(j
=0;j
<onesub
->lines
;j
++)
1851 fprintf(fd
,"%s\n",onesub
->text
[j
]);
1856 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
1859 void dump_mpsub(sub_data
* subd
, float fps
){
1863 subtitle
*subs
= subd
->subtitles
;
1865 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
1866 if (sub_fps
==0) sub_fps
=fps
;
1868 fd
=fopen ("dump.mpsub", "w");
1870 perror ("dump_mpsub: fopen");
1875 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
1876 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
1878 for(j
=0; j
< subd
->sub_num
; j
++){
1879 subtitle
* egysub
=&subs
[j
];
1880 if (subd
->sub_uses_time
) {
1881 a
=((egysub
->start
-mpsub_position
)/100.0);
1882 b
=((egysub
->end
-egysub
->start
)/100.0);
1883 if ( (float)((int)a
) == a
)
1884 fprintf (fd
, "%.0f",a
);
1886 fprintf (fd
, "%.2f",a
);
1888 if ( (float)((int)b
) == b
)
1889 fprintf (fd
, " %.0f\n",b
);
1891 fprintf (fd
, " %.2f\n",b
);
1893 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
1894 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
1897 mpsub_position
= egysub
->end
;
1898 for (i
=0; i
<egysub
->lines
; i
++) {
1899 fprintf (fd
, "%s\n",egysub
->text
[i
]);
1904 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
1907 void dump_microdvd(sub_data
* subd
, float fps
) {
1910 subtitle
*subs
= subd
->subtitles
;
1913 fd
= fopen("dumpsub.sub", "w");
1915 perror("dumpsub.sub: fopen");
1918 delay
= sub_delay
* sub_fps
;
1919 for (i
= 0; i
< subd
->sub_num
; ++i
) {
1921 start
= subs
[i
].start
;
1923 if (subd
->sub_uses_time
) {
1924 start
= start
* sub_fps
/ 100 ;
1925 end
= end
* sub_fps
/ 100;
1928 start
= start
* sub_fps
/ fps
;
1929 end
= end
* sub_fps
/ fps
;
1933 fprintf(fd
, "{%d}{%d}", start
, end
);
1934 for (j
= 0; j
< subs
[i
].lines
; ++j
)
1935 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
1939 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.sub\'.\n");
1942 void dump_jacosub(sub_data
* subd
, float fps
) {
1948 subtitle
*subs
= subd
->subtitles
;
1950 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1952 fd
=fopen("dumpsub.jss","w");
1955 perror("dump_jacosub: fopen");
1958 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
1959 for(i
=0; i
< subd
->sub_num
; i
++)
1961 onesub
=subs
+i
; //=&subs[i];
1964 if (!subd
->sub_uses_time
)
1965 temp
= temp
* 100 / sub_fps
;
1966 temp
-= sub_delay
* 100;
1967 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
1968 m
=temp
/6000; temp
%=6000; //m =1*100*60
1969 s
=temp
/100; temp
%=100; //s =1*100
1971 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
1974 if (!subd
->sub_uses_time
)
1975 temp
= temp
* 100 / sub_fps
;
1976 temp
-= sub_delay
* 100;
1977 h
=temp
/360000;temp
%=360000;
1978 m
=temp
/6000; temp
%=6000;
1979 s
=temp
/100; temp
%=100;
1981 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
1983 for(j
=0;j
<onesub
->lines
;j
++)
1984 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
1989 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
1992 void dump_sami(sub_data
* subd
, float fps
) {
1997 subtitle
*subs
= subd
->subtitles
;
1999 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2001 fd
=fopen("dumpsub.smi","w");
2004 perror("dump_jacosub: fopen");
2007 fprintf(fd
, "<SAMI>\n"
2009 " <STYLE TYPE=\"Text/css\">\n"
2011 " 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"
2012 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2017 for(i
=0; i
< subd
->sub_num
; i
++)
2019 onesub
=subs
+i
; //=&subs[i];
2022 if (!subd
->sub_uses_time
)
2023 temp
= temp
* 100 / sub_fps
;
2024 temp
-= sub_delay
* 100;
2025 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2026 "\t <P>", temp
* 10);
2028 for(j
=0;j
<onesub
->lines
;j
++)
2029 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2034 if (!subd
->sub_uses_time
)
2035 temp
= temp
* 100 / sub_fps
;
2036 temp
-= sub_delay
* 100;
2037 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2038 "\t <P> \n", temp
* 10);
2040 fprintf(fd
, "</BODY>\n"
2043 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2046 void sub_free( sub_data
* subd
)
2050 if ( !subd
) return;
2052 for (i
= 0; i
< subd
->sub_num
; i
++)
2053 for (j
= 0; j
< subd
->subtitles
[i
].lines
; j
++)
2054 free( subd
->subtitles
[i
].text
[j
] );
2055 free( subd
->subtitles
);
2056 free( subd
->filename
);
2060 #define MAX_SUBLINE 512
2062 * \brief parse text and append it to subtitle in sub
2063 * \param sub subtitle struct to add text to
2064 * \param txt text to parse
2065 * \param len length of text in txt
2066 * \param endpts pts at which this subtitle text should be removed again
2068 * <> and {} are interpreted as comment delimiters, "\n", "\N", '\n', '\r'
2069 * and '\0' are interpreted as newlines, duplicate, leading and trailing
2070 * newlines are ignored.
2072 void sub_add_text(subtitle
*sub
, const char *txt
, int len
, double endpts
) {
2074 int double_newline
= 1; // ignore newlines at the beginning
2077 if (sub
->lines
>= SUB_MAX_TEXT
) return;
2079 buf
= malloc(MAX_SUBLINE
+ 1);
2080 sub
->text
[sub
->lines
] = buf
;
2081 sub
->endpts
[sub
->lines
] = endpts
;
2082 for (i
= 0; i
< len
&& pos
< MAX_SUBLINE
; i
++) {
2084 if (c
== '<') comment
|= 1;
2085 if (c
== '{') comment
|= 2;
2087 if (c
== '}') comment
&= ~2;
2088 if (c
== '>') comment
&= ~1;
2091 if (pos
== MAX_SUBLINE
- 1) {
2095 if (c
== '\\' && i
+ 1 < len
) {
2097 if (c
== 'n' || c
== 'N') c
= 0;
2099 if (c
== '\n' || c
== '\r') c
= 0;
2103 } else if (!double_newline
) {
2104 if (sub
->lines
>= SUB_MAX_TEXT
- 1) {
2105 mp_msg(MSGT_VO
, MSGL_WARN
, "Too many subtitle lines\n");
2112 buf
= malloc(MAX_SUBLINE
+ 1);
2113 sub
->text
[sub
->lines
] = buf
;
2114 sub
->endpts
[sub
->lines
] = endpts
;
2118 if (sub
->lines
< SUB_MAX_TEXT
&&
2119 strlen(sub
->text
[sub
->lines
]))
2124 * \brief remove outdated subtitle lines.
2125 * \param sub subtitle struct to modify
2126 * \param pts current pts. All lines with endpts <= this will be removed.
2127 * Use MP_NOPTS_VALUE to remove all lines
2128 * \return 1 if sub was modified, 0 otherwise.
2130 int sub_clear_text(subtitle
*sub
, double pts
) {
2133 while (i
< sub
->lines
) {
2134 double endpts
= sub
->endpts
[i
];
2135 if (pts
== MP_NOPTS_VALUE
|| (endpts
!= MP_NOPTS_VALUE
&& pts
>= endpts
)) {
2138 for (j
= i
+ 1; j
< sub
->lines
; j
++) {
2139 sub
->text
[j
- 1] = sub
->text
[j
];
2140 sub
->endpts
[j
- 1] = sub
->endpts
[j
];