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 #include <fribidi/fribidi.h>
52 char *fribidi_charset
= NULL
; ///character set that will be passed to FriBiDi
53 int flip_hebrew
= 1; ///flip subtitles using fribidi
54 int fribidi_flip_commas
= 0; ///flip comma when fribidi is used
57 // Parameter struct for the format-specific readline functions
58 struct readline_args
{
63 /* Maximal length of line of a subtitle */
65 static float mpsub_position
=0;
66 static float mpsub_multiplier
=1.;
67 static int sub_slacktime
= 20000; //20 sec
69 int sub_no_text_pp
=0; // 1 => do not apply text post-processing
70 // like {\...} elimination in SSA format.
72 int sub_match_fuzziness
=0; // level of sub name matching fuzziness
74 /* Use the SUB_* constant defined in the header file */
75 int sub_format
=SUB_INVALID
;
78 Some subtitling formats, namely AQT and Subrip09, define the end of a
79 subtitle as the beginning of the following. Since currently we read one
80 subtitle at time, for these format we keep two global *subtitle,
81 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
82 so we can change its end when we read current subtitle starting time.
83 When CONFIG_SORTSUB is defined, we use a single global unsigned long,
84 previous_sub_end, for both (and even future) formats, to store the end of
85 the previous sub: it is initialized to 0 in sub_read_file and eventually
86 modified by sub_read_aqt_line or sub_read_subrip09_line.
88 unsigned long previous_sub_end
;
91 static int eol(char p
) {
92 return p
=='\r' || p
=='\n' || p
=='\0';
95 /* Remove leading and trailing space */
96 static void trail_space(char *s
) {
98 while (isspace(s
[i
])) ++i
;
99 if (i
) strcpy(s
, s
+ i
);
101 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
104 static char *stristr(const char *haystack
, const char *needle
) {
106 const char *p
= haystack
;
108 if (!(haystack
&& needle
)) return NULL
;
112 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
119 static void sami_add_line(subtitle
*current
, char *buffer
, char **pos
) {
123 if (*buffer
&& current
->lines
< SUB_MAX_TEXT
)
124 current
->text
[current
->lines
++] = strdup(buffer
);
128 static subtitle
*sub_read_line_sami(stream_t
* st
, subtitle
*current
,
129 struct readline_args
*args
)
131 int utf16
= args
->utf16
;
132 static char line
[LINE_LEN
+1];
133 static char *s
= NULL
, *slacktime_s
;
134 char text
[LINE_LEN
+1], *p
=NULL
, *q
;
137 current
->lines
= current
->start
= current
->end
= 0;
138 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
141 /* read the first line */
143 if (!(s
= stream_read_line(st
, line
, LINE_LEN
, utf16
))) return 0;
148 case 0: /* find "START=" or "Slacktime:" */
149 slacktime_s
= stristr (s
, "Slacktime:");
151 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
153 s
= stristr (s
, "Start=");
155 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
157 for (; *s
!= '>' && *s
!= '\0'; s
++);
163 case 1: /* find (optional) "<P", skip other TAGs */
164 for (; *s
== ' ' || *s
== '\t'; s
++); /* strip blanks, if any */
165 if (*s
== '\0') break;
166 if (*s
!= '<') { state
= 3; p
= text
; continue; } /* not a TAG */
168 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
169 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
175 case 2: /* find ">" */
176 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
179 case 3: /* get all text until '<' appears */
180 if (p
- text
>= LINE_LEN
)
181 sami_add_line(current
, text
, &p
);
182 if (*s
== '\0') break;
183 else if (!strncasecmp (s
, "<br>", 4)) {
184 sami_add_line(current
, text
, &p
);
187 else if ((*s
== '{') && !sub_no_text_pp
) { state
= 5; ++s
; continue; }
188 else if (*s
== '<') { state
= 4; }
189 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
190 else if (*s
== '\t') { *p
++ = ' '; s
++; }
191 else if (*s
== '\r' || *s
== '\n') { s
++; }
194 /* skip duplicated space */
195 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
199 case 4: /* get current->end or skip <TAG> */
200 q
= stristr (s
, "Start=");
202 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
203 *p
= '\0'; trail_space (text
);
205 current
->text
[current
->lines
++] = strdup (text
);
206 if (current
->lines
> 0) { state
= 99; break; }
210 if (s
) { s
++; state
= 3; continue; }
212 case 5: /* get rid of {...} text, but read the alignment code */
213 if ((*s
== '\\') && (*(s
+ 1) == 'a') && !sub_no_text_pp
) {
214 if (stristr(s
, "\\a1") != NULL
) {
215 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
218 if (stristr(s
, "\\a2") != NULL
) {
219 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
221 } else if (stristr(s
, "\\a3") != NULL
) {
222 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
224 } else if ((stristr(s
, "\\a4") != NULL
) || (stristr(s
, "\\a5") != NULL
) || (stristr(s
, "\\a8") != NULL
)) {
225 current
->alignment
= SUB_ALIGNMENT_TOPLEFT
;
227 } else if (stristr(s
, "\\a6") != NULL
) {
228 current
->alignment
= SUB_ALIGNMENT_TOPCENTER
;
230 } else if (stristr(s
, "\\a7") != NULL
) {
231 current
->alignment
= SUB_ALIGNMENT_TOPRIGHT
;
233 } else if (stristr(s
, "\\a9") != NULL
) {
234 current
->alignment
= SUB_ALIGNMENT_MIDDLELEFT
;
236 } else if (stristr(s
, "\\a10") != NULL
) {
237 current
->alignment
= SUB_ALIGNMENT_MIDDLECENTER
;
239 } else if (stristr(s
, "\\a11") != NULL
) {
240 current
->alignment
= SUB_ALIGNMENT_MIDDLERIGHT
;
244 if (*s
== '}') state
= 3;
250 if (state
!= 99 && !(s
= stream_read_line (st
, line
, LINE_LEN
, utf16
))) {
251 if (current
->start
> 0) {
252 break; // if it is the last subtitle
258 } while (state
!= 99);
260 // For the last subtitle
261 if (current
->end
<= 0) {
262 current
->end
= current
->start
+ sub_slacktime
;
263 sami_add_line(current
, text
, &p
);
270 static char *sub_readtext(char *source
, char **dest
) {
274 // printf("src=%p dest=%p \n",source,dest);
276 while ( !eol(*p
) && *p
!= '|' ) {
280 *dest
= malloc (len
+1);
281 if (!dest
) {return ERR
;}
283 strncpy(*dest
, source
, len
);
286 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
288 if (*p
) return p
; // not-last text field
289 else return NULL
; // last text field
292 static subtitle
*sub_read_line_microdvd(stream_t
*st
,subtitle
*current
,
293 struct readline_args
*args
)
295 int utf16
= args
->utf16
;
296 char line
[LINE_LEN
+1];
297 char line2
[LINE_LEN
+1];
302 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
303 } while ((sscanf (line
,
305 &(current
->start
), line2
) < 2) &&
307 "{%ld}{%ld}%[^\r\n]",
308 &(current
->start
), &(current
->end
), line2
) < 3));
310 if (args
->opts
->ass_enabled
) {
311 subassconvert_microdvd(line2
, line
, LINE_LEN
+ 1);
317 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
318 if (current
->text
[i
]==ERR
) {return ERR
;}
320 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
327 static subtitle
*sub_read_line_mpl2(stream_t
*st
,subtitle
*current
,
328 struct readline_args
*args
)
330 int utf16
= args
->utf16
;
331 char line
[LINE_LEN
+1];
332 char line2
[LINE_LEN
+1];
337 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
338 } while ((sscanf (line
,
339 "[%ld][%ld]%[^\r\n]",
340 &(current
->start
), &(current
->end
), line2
) < 3));
341 current
->start
*= 10;
346 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
347 if (current
->text
[i
]==ERR
) {return ERR
;}
349 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
356 static subtitle
*sub_read_line_subrip(stream_t
* st
, subtitle
*current
,
357 struct readline_args
*args
)
359 int utf16
= args
->utf16
;
360 char line
[LINE_LEN
+1];
361 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
362 char *p
=NULL
, *q
=NULL
;
366 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
367 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
368 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
369 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
371 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
374 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
375 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
376 current
->text
[current
->lines
-1]=malloc (len
+1);
377 if (!current
->text
[current
->lines
-1]) return ERR
;
378 strncpy (current
->text
[current
->lines
-1], q
, len
);
379 current
->text
[current
->lines
-1][len
]='\0';
380 if (!*p
|| *p
=='\r' || *p
=='\n') break;
382 else while (*p
++!=']');
389 static subtitle
*sub_ass_read_line_subviewer(stream_t
*st
, subtitle
*current
,
390 struct readline_args
*args
)
392 int utf16
= args
->utf16
;
393 int a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, j
= 0;
395 while (!current
->text
[0]) {
396 char line
[LINE_LEN
+ 1], full_line
[LINE_LEN
+ 1], sep
;
399 /* Parse SubRip header */
400 if (!stream_read_line(st
, line
, LINE_LEN
, utf16
))
402 if (sscanf(line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",
403 &a1
, &a2
, &a3
, &sep
, &a4
, &b1
, &b2
, &b3
, &sep
, &b4
) < 10)
406 current
->start
= a1
* 360000 + a2
* 6000 + a3
* 100 + a4
/ 10;
407 current
->end
= b1
* 360000 + b2
* 6000 + b3
* 100 + b4
/ 10;
411 for (i
= 0; i
< SUB_MAX_TEXT
; i
++) {
412 int blank
= 1, len
= 0;
415 if (!stream_read_line(st
, line
, LINE_LEN
, utf16
))
418 for (p
= line
; *p
!= '\n' && *p
!= '\r' && *p
; p
++, len
++)
419 if (*p
!= ' ' && *p
!= '\t')
427 if (!(j
+ 1 + len
< sizeof(full_line
) - 1))
431 full_line
[j
++] = '\n';
432 strcpy(&full_line
[j
], line
);
436 /* Use the ASS/SSA converter to transform the whole lines */
438 char converted_line
[LINE_LEN
+ 1];
439 subassconvert_subrip(full_line
, converted_line
, LINE_LEN
+ 1);
440 current
->text
[0] = strdup(converted_line
);
447 static subtitle
*sub_read_line_subviewer(stream_t
*st
,subtitle
*current
,
448 struct readline_args
*args
)
450 int utf16
= args
->utf16
;
451 char line
[LINE_LEN
+1];
452 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
456 if (args
->opts
->ass_enabled
)
457 return sub_ass_read_line_subviewer(st
, current
, args
);
458 while (!current
->text
[0]) {
459 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
460 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)
462 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
463 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
464 for (i
=0; i
<SUB_MAX_TEXT
;) {
466 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) break;
468 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++)
469 if (*p
!= ' ' && *p
!= '\t')
473 char *curptr
=current
->text
[i
]=malloc (len
+1);
474 if (!current
->text
[i
]) return ERR
;
475 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
477 /* let's filter html tags ::atmos */
504 static subtitle
*sub_read_line_subviewer2(stream_t
*st
,subtitle
*current
,
505 struct readline_args
*args
)
507 int utf16
= args
->utf16
;
508 char line
[LINE_LEN
+1];
513 while (!current
->text
[0]) {
514 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
517 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
519 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
520 for (i
=0; i
<SUB_MAX_TEXT
;) {
521 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) break;
522 if (line
[0]=='}') break;
524 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
526 current
->text
[i
]=malloc (len
+1);
527 if (!current
->text
[i
]) return ERR
;
528 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
540 static subtitle
*sub_read_line_vplayer(stream_t
*st
,subtitle
*current
,
541 struct readline_args
*args
)
543 int utf16
= args
->utf16
;
544 char line
[LINE_LEN
+1];
546 char *p
=NULL
, *next
,separator
;
549 while (!current
->text
[0]) {
550 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
551 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
554 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
558 // finds the body of the subtitle
565 printf("SUB: Skipping incorrect subtitle line!\n");
569 // by wodzu: hey! this time we know what length it has! what is
570 // that magic for? it can't deal with space instead of third
571 // colon! look, what simple it can be:
578 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
579 if (current
->text
[i
]==ERR
) {return ERR
;}
581 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
589 static subtitle
*sub_read_line_rt(stream_t
*st
,subtitle
*current
,
590 struct readline_args
*args
)
592 int utf16
= args
->utf16
;
594 //TODO: This format uses quite rich (sub/super)set of xhtml
595 // I couldn't check it since DTD is not included.
596 // WARNING: full XML parses can be required for proper parsing
597 char line
[LINE_LEN
+1];
598 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
599 char *p
=NULL
,*next
=NULL
;
602 while (!current
->text
[0]) {
603 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
604 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
605 //to describe the same moment in time. Maybe there are even more formats in use.
606 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
607 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
609 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
610 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
611 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
612 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
613 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
614 ((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) &&
615 ((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) &&
616 //now try it without end time
617 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
618 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
619 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
620 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
623 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
624 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
625 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
626 current
->end
= current
->start
+200;
628 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
629 next
= strstr(line
,"<clear/>");
630 if(next
&& strlen(next
)>8){
632 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
633 if (current
->text
[i
]==ERR
) {return ERR
;}
635 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
643 static subtitle
*sub_read_line_ssa(stream_t
*st
,subtitle
*current
,
644 struct readline_args
*args
)
647 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
648 * other Sub Station Alpha scripts have only 8 commas before subtitle
649 * Reading the "ScriptType:" field is not reliable since many scripts appear
652 * http://www.scriptclub.org is a good place to find more examples
653 * http://www.eswat.demon.co.uk is where the SSA specs can be found
655 int utf16
= args
->utf16
;
657 static int max_comma
= 32; /* let's use 32 for the case that the */
658 /* amount of commas increase with newer SSA versions */
660 int hour1
, min1
, sec1
, hunsec1
,
661 hour2
, min2
, sec2
, hunsec2
, nothing
;
664 char line
[LINE_LEN
+1],
670 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) return NULL
;
671 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d"
672 "%[^\n\r]", ¬hing
,
673 &hour1
, &min1
, &sec1
, &hunsec1
,
674 &hour2
, &min2
, &sec2
, &hunsec2
,
677 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d"
678 "%[^\n\r]", ¬hing
,
679 &hour1
, &min1
, &sec1
, &hunsec1
,
680 &hour2
, &min2
, &sec2
, &hunsec2
,
683 line2
=strchr(line3
, ',');
684 if (!line2
) return NULL
;
686 for (comma
= 4; comma
< max_comma
; comma
++)
689 if(!(tmp
=strchr(++tmp
, ','))) break;
690 if(*(++tmp
) == ' ') break;
691 /* a space after a comma means we're already in a sentence */
695 if(comma
< max_comma
)max_comma
= comma
;
696 /* eliminate the trailing comma */
697 if(*line2
== ',') line2
++;
699 current
->lines
=0;num
=0;
700 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
701 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
703 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
704 current
->text
[num
]=malloc(tmp
-line2
+1);
705 strncpy (current
->text
[num
], line2
, tmp
-line2
);
706 current
->text
[num
][tmp
-line2
]='\0';
710 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
713 current
->text
[num
]=strdup(line2
);
719 static void sub_pp_ssa(subtitle
*sub
) {
724 /* eliminate any text enclosed with {}, they are font and color settings */
725 so
=de
=sub
->text
[--l
];
727 if(*so
== '{' && so
[1]=='\\') {
728 for (start
=so
; *so
&& *so
!='}'; so
++);
729 if(*so
) so
++; else so
=start
;
741 * PJS subtitles reader.
742 * That's the "Phoenix Japanimation Society" format.
743 * I found some of them in http://www.scriptsclub.org/ (used for anime).
744 * The time is in tenths of second.
746 * by set, based on code by szabi (dunnowhat sub format ;-)
748 static subtitle
*sub_read_line_pjs(stream_t
*st
,subtitle
*current
,
749 struct readline_args
*args
)
751 int utf16
= args
->utf16
;
752 char line
[LINE_LEN
+1];
753 char text
[LINE_LEN
+1], *s
, *d
;
755 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
758 for (s
=line
; *s
&& isspace(*s
); s
++);
759 /* allow empty lines at the end of the file */
763 if (sscanf (s
, "%ld,%ld,", &(current
->start
),
764 &(current
->end
)) <2) {
767 /* the files I have are in tenths of second */
768 current
->start
*= 10;
770 /* walk to the beggining of the string */
771 for (; *s
; s
++) if (*s
==',') break;
773 for (s
++; *s
; s
++) if (*s
==',') break;
779 /* copy the string to the text buffer */
780 for (s
++, d
=text
; *s
&& *s
!='"'; s
++, d
++)
783 current
->text
[0] = strdup(text
);
789 static subtitle
*sub_read_line_mpsub(stream_t
*st
, subtitle
*current
,
790 struct readline_args
*args
)
792 int utf16
= args
->utf16
;
793 char line
[LINE_LEN
+1];
800 if (!stream_read_line(st
, line
, LINE_LEN
, utf16
)) return NULL
;
801 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
803 mpsub_position
+= a
*mpsub_multiplier
;
804 current
->start
=(int) mpsub_position
;
805 mpsub_position
+= b
*mpsub_multiplier
;
806 current
->end
=(int) mpsub_position
;
808 while (num
< SUB_MAX_TEXT
) {
809 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
)) {
810 if (num
== 0) return NULL
;
814 while (isspace(*p
)) p
++;
815 if (eol(*p
) && num
> 0) return current
;
816 if (eol(*p
)) return NULL
;
818 for (q
=p
; !eol(*q
); q
++);
821 current
->text
[num
]=strdup(p
);
822 // printf (">%s<\n",p);
823 current
->lines
= ++num
;
825 if (num
) return current
;
829 return NULL
; // we should have returned before if it's OK
832 #ifndef CONFIG_SORTSUB
833 //we don't need this if we use previous_sub_end
834 subtitle
*previous_aqt_sub
= NULL
;
837 static subtitle
*sub_read_line_aqt(stream_t
*st
,subtitle
*current
,
838 struct readline_args
*args
)
840 int utf16
= args
->utf16
;
841 char line
[LINE_LEN
+1];
846 // try to locate next subtitle
847 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
849 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
853 #ifdef CONFIG_SORTSUB
854 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
856 if (previous_aqt_sub
!= NULL
)
857 previous_aqt_sub
->end
= current
->start
-1;
859 previous_aqt_sub
= current
;
862 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
865 sub_readtext((char *) &line
,¤t
->text
[0]);
867 current
->end
= current
->start
; // will be corrected by next subtitle
869 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
873 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
874 if (current
->text
[i
]==ERR
) {return ERR
;}
876 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
880 if (!strlen(current
->text
[0]) && !strlen(current
->text
[1])) {
881 #ifdef CONFIG_SORTSUB
882 previous_sub_end
= 0;
884 // void subtitle -> end of previous marked and exit
885 previous_aqt_sub
= NULL
;
893 #ifndef CONFIG_SORTSUB
894 subtitle
*previous_subrip09_sub
= NULL
;
897 static subtitle
*sub_read_line_subrip09(stream_t
*st
,subtitle
*current
,
898 struct readline_args
*args
)
900 int utf16
= args
->utf16
;
901 char line
[LINE_LEN
+1];
907 // try to locate next subtitle
908 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
910 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
914 current
->start
= a1
*360000+a2
*6000+a3
*100;
916 #ifdef CONFIG_SORTSUB
917 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
919 if (previous_subrip09_sub
!= NULL
)
920 previous_subrip09_sub
->end
= current
->start
-1;
922 previous_subrip09_sub
= current
;
925 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
930 current
->text
[0]=""; // just to be sure that string is clear
932 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
933 if (current
->text
[i
]==ERR
) {return ERR
;}
935 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
939 if (!strlen(current
->text
[0]) && (i
==0)) {
940 #ifdef CONFIG_SORTSUB
941 previous_sub_end
= 0;
943 // void subtitle -> end of previous marked and exit
944 previous_subrip09_sub
= NULL
;
952 static subtitle
*sub_read_line_jacosub(stream_t
* st
, subtitle
* current
,
953 struct readline_args
*args
)
955 int utf16
= args
->utf16
;
956 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
957 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
958 static unsigned jacoTimeres
= 30;
959 static int jacoShift
= 0;
961 memset(current
, 0, sizeof(subtitle
));
962 memset(line1
, 0, LINE_LEN
);
963 memset(line2
, 0, LINE_LEN
);
964 memset(directive
, 0, LINE_LEN
);
965 while (!current
->text
[0]) {
966 if (!stream_read_line(st
, line1
, LINE_LEN
, utf16
)) {
970 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
971 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
972 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
973 if (line1
[0] == '#') {
974 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
976 unsigned units
= jacoShift
;
977 switch (toupper(line1
[1])) {
979 if (isalpha(line1
[2])) {
984 if (sscanf(&line1
[delta
], "%d", &hours
)) {
989 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
991 (&line1
[delta
], "%*d:%*d:%d",
993 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
997 sscanf(&line1
[delta
], "%d:%d.%d",
998 &minutes
, &seconds
, &units
);
1002 hours
= minutes
= 0;
1003 sscanf(&line1
[delta
], "%d.%d", &seconds
,
1005 seconds
*= inverter
;
1008 ((hours
* 3600 + minutes
* 60 +
1009 seconds
) * jacoTimeres
+
1014 if (isalpha(line1
[2])) {
1019 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
1026 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
1029 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
1035 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
1036 jacoShift
) * 100.0 / jacoTimeres
);
1039 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
1040 jacoShift
) * 100.0 / jacoTimeres
);
1044 while ((*p
== ' ') || (*p
== '\t')) {
1047 if (isalpha(*p
)||*p
== '[') {
1050 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
1051 return (subtitle
*) ERR
;
1052 jLength
= strlen(directive
);
1053 for (cont
= 0; cont
< jLength
; ++cont
) {
1054 if (isalpha(*(directive
+ cont
)))
1055 *(directive
+ cont
) = toupper(*(directive
+ cont
));
1057 if ((strstr(directive
, "RDB") != NULL
)
1058 || (strstr(directive
, "RDC") != NULL
)
1059 || (strstr(directive
, "RLB") != NULL
)
1060 || (strstr(directive
, "RLG") != NULL
)) {
1063 if (strstr(directive
, "JL") != NULL
) {
1064 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
1065 } else if (strstr(directive
, "JR") != NULL
) {
1066 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
1068 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
1070 strcpy(line2
, line1
);
1073 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
1081 //the next line to get rid of a blank after the comment
1082 if ((*(p
+ 1)) == ' ')
1094 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
1102 if (*(p
+ 1) == 'n') {
1105 current
->text
[current
->lines
++] = strdup(line1
);
1109 if ((toupper(*(p
+ 1)) == 'C')
1110 || (toupper(*(p
+ 1)) == 'F')) {
1114 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
1115 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
1116 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
1120 if ((*(p
+ 1) == '\\') ||
1121 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
1123 } else if (eol(*(p
+ 1))) {
1124 if (!stream_read_line(st
, directive
, LINE_LEN
, utf16
))
1126 trail_space(directive
);
1127 av_strlcat(line2
, directive
, LINE_LEN
);
1138 current
->text
[current
->lines
] = strdup(line1
);
1144 static int sub_autodetect (stream_t
* st
, int *uses_time
, int utf16
) {
1145 char line
[LINE_LEN
+1];
1150 if (!stream_read_line (st
, line
, LINE_LEN
, utf16
))
1153 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
1154 {*uses_time
=0;return SUB_MICRODVD
;}
1155 if (sscanf (line
, "{%d}{}", &i
)==1)
1156 {*uses_time
=0;return SUB_MICRODVD
;}
1157 if (sscanf (line
, "[%d][%d]", &i
, &i
)==2)
1158 {*uses_time
=1;return SUB_MPL2
;}
1159 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
1160 {*uses_time
=1;return SUB_SUBRIP
;}
1161 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
1162 {*uses_time
=1;return SUB_SUBVIEWER
;}
1163 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
1164 {*uses_time
=1;return SUB_SUBVIEWER2
;}
1165 if (strstr (line
, "<SAMI>"))
1166 {*uses_time
=1; return SUB_SAMI
;}
1167 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
1168 {*uses_time
= 1; return SUB_JACOSUB
;}
1169 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
1170 {*uses_time
= 1; return SUB_JACOSUB
;}
1171 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
1172 {*uses_time
=1;return SUB_VPLAYER
;}
1173 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
1174 {*uses_time
=1;return SUB_VPLAYER
;}
1175 if (!strncasecmp(line
, "<window", 7))
1176 {*uses_time
=1;return SUB_RT
;}
1177 if (!memcmp(line
, "Dialogue: Marked", 16))
1178 {*uses_time
=1; return SUB_SSA
;}
1179 if (!memcmp(line
, "Dialogue: ", 10))
1180 {*uses_time
=1; return SUB_SSA
;}
1181 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
1182 {*uses_time
=1;return SUB_PJS
;}
1183 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
1184 {*uses_time
=0; return SUB_MPSUB
;}
1185 if (!memcmp(line
, "FORMAT=TIME", 11))
1186 {*uses_time
=1; return SUB_MPSUB
;}
1187 if (strstr (line
, "-->>"))
1188 {*uses_time
=0; return SUB_AQTITLE
;}
1189 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
1190 {*uses_time
=1;return SUB_SUBRIP09
;}
1193 return SUB_INVALID
; // too many bad lines
1196 extern int sub_utf8
;
1197 int sub_utf8_prev
=0;
1199 extern float sub_delay
;
1200 extern float sub_fps
;
1203 static iconv_t icdsc
= (iconv_t
)(-1);
1205 void subcp_open (stream_t
*st
)
1207 char *tocp
= "UTF-8";
1210 const char *cp_tmp
= sub_cp
;
1212 char enca_lang
[3], enca_fallback
[100];
1213 if (sscanf(sub_cp
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
1214 || sscanf(sub_cp
, "ENCA:%2s:%99s", enca_lang
, enca_fallback
) == 2) {
1215 if (st
&& st
->flags
& MP_STREAM_SEEK
) {
1216 cp_tmp
= guess_cp(st
, enca_lang
, enca_fallback
);
1218 cp_tmp
= enca_fallback
;
1220 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: enca failed, stream must be seekable.\n");
1224 if ((icdsc
= iconv_open (tocp
, cp_tmp
)) != (iconv_t
)(-1)){
1225 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1228 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1232 void subcp_close (void)
1234 if (icdsc
!= (iconv_t
)(-1)){
1235 (void) iconv_close (icdsc
);
1236 icdsc
= (iconv_t
)(-1);
1237 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
1241 subtitle
* subcp_recode (subtitle
*sub
)
1244 size_t ileft
, oleft
;
1246 if(icdsc
== (iconv_t
)(-1)) return sub
;
1249 ip
= sub
->text
[--l
];
1253 if (!(ot
= malloc(oleft
+ 1))){
1254 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1258 if (iconv(icdsc
, &ip
, &ileft
,
1259 &op
, &oleft
) == (size_t)(-1)) {
1260 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line.\n");
1264 // In some stateful encodings, we must clear the state to handle the last character
1265 if (iconv(icdsc
, NULL
, NULL
,
1266 &op
, &oleft
) == (size_t)(-1)) {
1267 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line, can't clear encoding state.\n");
1270 free (sub
->text
[l
]);
1277 #ifdef CONFIG_FRIBIDI
1279 * Do conversion necessary for right-to-left language support via fribidi.
1280 * @param sub subtitle to convert
1281 * @param sub_utf8 whether the subtitle is encoded in UTF-8
1282 * @param from first new subtitle, all lines before this are assumed to be already converted
1284 static subtitle
* sub_fribidi (subtitle
*sub
, int sub_utf8
, int from
)
1286 FriBidiChar logical
[LINE_LEN
+1], visual
[LINE_LEN
+1]; // Hopefully these two won't smash the stack
1287 char *ip
= NULL
, *op
= NULL
;
1288 size_t len
,orig_len
;
1291 fribidi_boolean log2vis
;
1294 fribidi_set_mirroring(1);
1295 fribidi_set_reorder_nsm(0);
1297 if( sub_utf8
== 0 ) {
1298 char_set_num
= fribidi_parse_charset (fribidi_charset
?fribidi_charset
:"ISO8859-8");
1300 char_set_num
= fribidi_parse_charset ("UTF-8");
1303 ip
= sub
->text
[--l
];
1304 orig_len
= len
= strlen( ip
); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1305 if(len
> LINE_LEN
) {
1306 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: sub->text is longer than LINE_LEN.\n");
1310 len
= fribidi_charset_to_unicode (char_set_num
, ip
, len
, logical
);
1311 #if FRIBIDI_INTERFACE_VERSION < 3
1312 FriBidiCharType base
= fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
1314 FriBidiParType base
= fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
1316 log2vis
= fribidi_log2vis (logical
, len
, &base
,
1318 visual
, NULL
, NULL
, NULL
);
1320 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
,
1322 if((op
= malloc((FFMAX(2*orig_len
,2*len
) + 1))) == NULL
) {
1323 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1327 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1333 for (l
= sub
->lines
; l
;)
1334 free (sub
->text
[--l
]);
1342 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1343 int sub_num
, int sub_uses_time
) {
1347 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1348 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1352 if (sub
->end
<= sub
->start
){
1353 sub
->end
= sub
->start
+ subfms
;
1360 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1361 // these subtitles overlap for less than 0.2 seconds
1362 // and would result in very short overlapping subtitle
1363 // so let's fix the problem here, before overlapping code
1364 // get its hands on them
1365 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1366 sub
->end
-= half
+ 1;
1367 nextsub
->start
+= delta
- half
;
1369 if (sub
->end
>= nextsub
->start
){
1370 sub
->end
= nextsub
->start
- 1;
1371 if (sub
->end
- sub
->start
> subfms
)
1372 sub
->end
= sub
->start
+ subfms
;
1379 * Movies are often converted from FILM (24 fps)
1380 * to PAL (25) by simply speeding it up, so we
1381 * to multiply the original timestmaps by
1382 * (Movie's FPS / Subtitle's (guessed) FPS)
1383 * so eg. for 23.98 fps movie and PAL time based
1384 * subtitles we say -subfps 25 and we're fine!
1387 /* timed sub fps correction ::atmos */
1388 /* the frame-based case is handled in mpcommon.c
1389 * where find_sub is called */
1390 if(sub_uses_time
&& sub_fps
) {
1391 sub
->start
*= sub_fps
/fps
;
1392 sub
->end
*= sub_fps
/fps
;
1398 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: Adjusted %d subtitle(s).\n", n
);
1402 subtitle
* (*read
)(stream_t
*st
, subtitle
*dest
,
1403 struct readline_args
*args
);
1404 void (*post
)(subtitle
*dest
);
1409 const char* guess_buffer_cp(unsigned char* buffer
, int buflen
, const char *preferred_language
, const char *fallback
)
1411 const char **languages
;
1413 EncaAnalyser analyser
;
1414 EncaEncoding encoding
;
1415 const char *detected_sub_cp
= NULL
;
1418 languages
= enca_get_languages(&langcnt
);
1419 mp_msg(MSGT_SUBREADER
, MSGL_V
, "ENCA supported languages: ");
1420 for (i
= 0; i
< langcnt
; i
++) {
1421 mp_msg(MSGT_SUBREADER
, MSGL_V
, "%s ", languages
[i
]);
1423 mp_msg(MSGT_SUBREADER
, MSGL_V
, "\n");
1425 for (i
= 0; i
< langcnt
; i
++) {
1426 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1427 analyser
= enca_analyser_alloc(languages
[i
]);
1428 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1429 enca_analyser_free(analyser
);
1430 if (encoding
.charset
!= ENCA_CS_UNKNOWN
) {
1431 detected_sub_cp
= enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
);
1438 if (!detected_sub_cp
) {
1439 detected_sub_cp
= fallback
;
1440 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detection failed: fallback to %s\n", fallback
);
1442 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", detected_sub_cp
);
1445 return detected_sub_cp
;
1448 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1449 const char* guess_cp(stream_t
*st
, const char *preferred_language
, const char *fallback
)
1452 unsigned char *buffer
;
1453 const char *detected_sub_cp
= NULL
;
1455 buffer
= malloc(MAX_GUESS_BUFFER_SIZE
);
1456 buflen
= stream_read(st
,buffer
, MAX_GUESS_BUFFER_SIZE
);
1458 detected_sub_cp
= guess_buffer_cp(buffer
, buflen
, preferred_language
, fallback
);
1464 return detected_sub_cp
;
1466 #undef MAX_GUESS_BUFFER_SIZE
1469 sub_data
* sub_read_file(char *filename
, float fps
, struct MPOpts
*opts
)
1473 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1474 subtitle
*first
, *second
, *sub
, *return_sub
, *alloced_sub
= NULL
;
1475 sub_data
*subt_data
;
1476 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1477 static const struct subreader sr
[]=
1479 { sub_read_line_microdvd
, NULL
, "microdvd" },
1480 { sub_read_line_subrip
, NULL
, "subrip" },
1481 { sub_read_line_subviewer
, NULL
, "subviewer" },
1482 { sub_read_line_sami
, NULL
, "sami" },
1483 { sub_read_line_vplayer
, NULL
, "vplayer" },
1484 { sub_read_line_rt
, NULL
, "rt" },
1485 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1486 { sub_read_line_pjs
, NULL
, "pjs" },
1487 { sub_read_line_mpsub
, NULL
, "mpsub" },
1488 { sub_read_line_aqt
, NULL
, "aqt" },
1489 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1490 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1491 { sub_read_line_jacosub
, NULL
, "jacosub" },
1492 { sub_read_line_mpl2
, NULL
, "mpl2" }
1494 const struct subreader
*srp
;
1496 if(filename
==NULL
) return NULL
; //qnx segfault
1497 fd
=open_stream (filename
, NULL
, NULL
); if (!fd
) return NULL
;
1499 sub_format
= SUB_INVALID
;
1500 for (utf16
= 0; sub_format
== SUB_INVALID
&& utf16
< 3; utf16
++) {
1501 sub_format
=sub_autodetect (fd
, &uses_time
, utf16
);
1507 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1508 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1510 mp_msg(MSGT_SUBREADER
, MSGL_V
, "SUB: Detected subtitle file format: %s\n", srp
->name
);
1513 sub_utf8_prev
=sub_utf8
;
1517 if ((l
=strlen(filename
))>4){
1518 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1520 if (l
>= strlen(exts
[k
]) && !strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1525 if (k
<0) subcp_open(fd
);
1530 first
=malloc(n_max
*sizeof(subtitle
));
1534 sub_utf8
=sub_utf8_prev
;
1539 #ifdef CONFIG_SORTSUB
1541 sub
= malloc(sizeof(subtitle
));
1542 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1543 //as the beginning of the following
1544 previous_sub_end
= 0;
1549 first
=realloc(first
,n_max
*sizeof(subtitle
));
1551 #ifndef CONFIG_SORTSUB
1552 sub
= &first
[sub_num
];
1554 memset(sub
, '\0', sizeof(subtitle
));
1555 sub
=srp
->read(fd
, sub
, &(struct readline_args
){utf16
, opts
});
1556 if(!sub
) break; // EOF
1558 if ((sub
!=ERR
) && sub_utf8
== 2) sub
=subcp_recode(sub
);
1560 #ifdef CONFIG_FRIBIDI
1561 /* Libass has its own BiDi handling now, and running this before
1562 * giving the subtitle to libass would only break things. */
1563 if (sub
!= ERR
&& !opts
->ass_enabled
)
1564 sub
= sub_fribidi(sub
, sub_utf8
, 0);
1575 // Apply any post processing that needs recoding first
1576 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1577 #ifdef CONFIG_SORTSUB
1578 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1579 first
[sub_num
].start
= sub
->start
;
1580 first
[sub_num
].end
= sub
->end
;
1581 first
[sub_num
].lines
= sub
->lines
;
1582 first
[sub_num
].alignment
= sub
->alignment
;
1583 for(i
= 0; i
< sub
->lines
; ++i
){
1584 first
[sub_num
].text
[i
] = sub
->text
[i
];
1586 if (previous_sub_end
){
1587 first
[sub_num
- 1].end
= previous_sub_end
;
1588 previous_sub_end
= 0;
1591 for(j
= sub_num
- 1; j
>= 0; --j
){
1592 first
[j
+ 1].start
= first
[j
].start
;
1593 first
[j
+ 1].end
= first
[j
].end
;
1594 first
[j
+ 1].lines
= first
[j
].lines
;
1595 first
[j
+ 1].alignment
= first
[j
].alignment
;
1596 for(i
= 0; i
< first
[j
].lines
; ++i
){
1597 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1599 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1600 first
[j
].start
= sub
->start
;
1601 first
[j
].end
= sub
->end
;
1602 first
[j
].lines
= sub
->lines
;
1603 first
[j
].alignment
= sub
->alignment
;
1604 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1605 first
[j
].text
[i
] = sub
->text
[i
];
1607 if (previous_sub_end
){
1608 first
[j
].end
= first
[j
- 1].end
;
1609 first
[j
- 1].end
= previous_sub_end
;
1610 previous_sub_end
= 0;
1617 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1627 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1628 mp_msg(MSGT_SUBREADER
, MSGL_V
,"SUB: Read %i subtitles, %i bad line(s).\n",
1636 // we do overlap if the user forced it (suboverlap_enable == 2) or
1637 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1638 // this is because usually overlapping subtitles are found in these formats,
1639 // while in others they are probably result of bad timing
1640 if ((suboverlap_enabled
== 2) ||
1641 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1642 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1643 // here we manage overlapping subtitles
1648 // for each subtitle in first[] we deal with its 'block' of
1650 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1651 unsigned long global_start
= first
[sub_first
].start
,
1652 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1653 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1654 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1655 char real_block
= 1;
1657 // here we find the number of subtitles inside the 'block'
1658 // and its span interval. this works well only with sorted
1660 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1662 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1663 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1664 global_start
= first
[sub_first
+ sub_to_add
].start
;
1666 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1667 global_end
= first
[sub_first
+ sub_to_add
].end
;
1671 /* Avoid n^2 memory use for the "placeholder" data structure
1672 * below with subtitles that have a huge number of
1673 * consecutive overlapping lines. */
1674 lines_to_add
= FFMIN(lines_to_add
, SUB_MAX_TEXT
);
1676 // we need a structure to keep trace of the screen lines
1677 // used by the subs, a 'placeholder'
1678 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1679 // from a block of sub_to_add+1 subs
1680 placeholder
= malloc(sizeof(int *) * counter
);
1681 for (i
= 0; i
< counter
; ++i
) {
1682 placeholder
[i
] = malloc(sizeof(int) * lines_to_add
);
1683 for (j
= 0; j
< lines_to_add
; ++j
) {
1684 placeholder
[i
][j
] = -1;
1689 local_end
= global_start
- 1;
1693 // here we find the beginning and the end of a new
1694 // subtitle in the block
1695 local_start
= local_end
+ 1;
1696 local_end
= global_end
;
1697 for (j
= 0; j
<= sub_to_add
; ++j
) {
1698 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1699 local_end
= first
[sub_first
+ j
].start
- 1;
1700 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1701 local_end
= first
[sub_first
+ j
].end
;
1704 // here we allocate the screen lines to subs we must
1705 // display in current local_start-local_end interval.
1706 // if the subs were yet presents in the previous interval
1707 // they keep the same lines, otherside they get unused lines
1708 for (j
= 0; j
<= sub_to_add
; ++j
) {
1709 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1710 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1713 int fragment_position
= -1;
1715 // if this is not the first new sub of the block
1716 // we find if this sub was present in the previous
1719 for (i
= 0; i
< lines_to_add
; ++i
) {
1720 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1721 placeholder
[counter
][i
] = sub_first
+ j
;
1728 // we are looking for the shortest among all groups of
1729 // sequential blank lines whose length is greater than or
1730 // equal to sub_lines. we store in fragment_position the
1731 // position of the shortest group, in fragment_length its
1732 // length, and in tmp the length of the group currently
1734 for (i
= 0; i
< lines_to_add
; ++i
) {
1735 if (placeholder
[counter
][i
] == -1) {
1736 // placeholder[counter][i] is part of the current group
1740 if (tmp
== sub_lines
) {
1741 // current group's size fits exactly the one we
1742 // need, so we stop looking
1743 fragment_position
= i
- tmp
;
1747 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1748 // current group is the best we found till here,
1749 // but is still bigger than the one we are looking
1750 // for, so we keep on looking
1751 fragment_length
= tmp
;
1752 fragment_position
= i
- tmp
;
1755 // current group doesn't fit at all, so we forget it
1761 // last screen line is blank, a group ends with it
1762 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1763 fragment_position
= i
- tmp
;
1766 if (fragment_position
== -1) {
1767 // it was not possible to find free screen line(s) for a subtitle,
1768 // usually this means a bug in the code; however we do not overlap
1769 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1770 higher_line
= SUB_MAX_TEXT
+ 1;
1773 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1774 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1779 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1780 if (placeholder
[counter
][j
] != -1)
1785 if (higher_line
>= SUB_MAX_TEXT
) {
1786 // the 'block' has too much lines, so we don't overlap the
1788 second
= realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1789 for (j
= 0; j
<= sub_to_add
; ++j
) {
1791 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1792 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1793 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1794 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1795 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1796 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1797 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1800 sub_num
+= sub_to_add
+ 1;
1801 sub_first
+= sub_to_add
;
1806 // we read the placeholder structure and create the new
1808 second
= realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1809 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1810 second
[sub_num
].start
= local_start
;
1811 second
[sub_num
].end
= local_end
;
1812 second
[sub_num
].alignment
= first
[sub_first
].alignment
;
1813 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1814 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1815 if (placeholder
[counter
][j
] != -1) {
1816 int lines
= first
[placeholder
[counter
][j
]].lines
;
1817 for (ls
= 0; ls
< lines
; ++ls
) {
1818 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1822 second
[sub_num
].text
[i
++] = strdup(" ");
1827 } while (local_end
< global_end
);
1829 for (i
= 0; i
< counter
; ++i
)
1830 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1832 counter
= 2 * sub_to_add
+ 1;
1833 for (i
= 0; i
< counter
; ++i
) {
1834 free(placeholder
[i
]);
1837 sub_first
+= sub_to_add
;
1840 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1841 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1842 free(first
[j
].text
[i
]);
1847 return_sub
= second
;
1848 } else { //if(suboverlap_enabled)
1849 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1852 if (return_sub
== NULL
) return NULL
;
1853 subt_data
= malloc(sizeof(sub_data
));
1854 subt_data
->filename
= strdup(filename
);
1855 subt_data
->sub_uses_time
= uses_time
;
1856 subt_data
->sub_num
= sub_num
;
1857 subt_data
->sub_errs
= sub_errs
;
1858 subt_data
->subtitles
= return_sub
;
1862 void list_sub_file(sub_data
* subd
){
1864 subtitle
*subs
= subd
->subtitles
;
1866 for(j
=0; j
< subd
->sub_num
; j
++){
1867 subtitle
* egysub
=&subs
[j
];
1868 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"%i line%c (%li-%li)\n",
1870 (1==egysub
->lines
)?' ':'s',
1873 for (i
=0; i
<egysub
->lines
; i
++) {
1874 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
1876 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\n");
1879 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Subtitle format %s time.\n",
1880 subd
->sub_uses_time
? "uses":"doesn't use");
1881 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
1884 void dump_srt(sub_data
* subd
, float fps
){
1890 subtitle
*subs
= subd
->subtitles
;
1892 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1894 fd
=fopen("dumpsub.srt","w");
1897 perror("dump_srt: fopen");
1900 for(i
=0; i
< subd
->sub_num
; i
++)
1902 onesub
=subs
+i
; //=&subs[i];
1903 fprintf(fd
,"%d\n",i
+1);//line number
1906 if (!subd
->sub_uses_time
)
1907 temp
= temp
* 100 / sub_fps
;
1908 temp
-= sub_delay
* 100;
1909 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
1910 m
=temp
/6000; temp
%=6000; //m =1*100*60
1911 s
=temp
/100; temp
%=100; //s =1*100
1912 ms
=temp
*10; //ms=1*10
1913 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
1916 if (!subd
->sub_uses_time
)
1917 temp
= temp
* 100 / sub_fps
;
1918 temp
-= sub_delay
* 100;
1919 h
=temp
/360000;temp
%=360000;
1920 m
=temp
/6000; temp
%=6000;
1921 s
=temp
/100; temp
%=100;
1923 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
1925 for(j
=0;j
<onesub
->lines
;j
++)
1926 fprintf(fd
,"%s\n",onesub
->text
[j
]);
1931 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
1934 void dump_mpsub(sub_data
* subd
, float fps
){
1938 subtitle
*subs
= subd
->subtitles
;
1940 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
1941 if (sub_fps
==0) sub_fps
=fps
;
1943 fd
=fopen ("dump.mpsub", "w");
1945 perror ("dump_mpsub: fopen");
1950 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
1951 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
1953 for(j
=0; j
< subd
->sub_num
; j
++){
1954 subtitle
* egysub
=&subs
[j
];
1955 if (subd
->sub_uses_time
) {
1956 a
=((egysub
->start
-mpsub_position
)/100.0);
1957 b
=((egysub
->end
-egysub
->start
)/100.0);
1958 if ( (float)((int)a
) == a
)
1959 fprintf (fd
, "%.0f",a
);
1961 fprintf (fd
, "%.2f",a
);
1963 if ( (float)((int)b
) == b
)
1964 fprintf (fd
, " %.0f\n",b
);
1966 fprintf (fd
, " %.2f\n",b
);
1968 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
1969 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
1972 mpsub_position
= egysub
->end
;
1973 for (i
=0; i
<egysub
->lines
; i
++) {
1974 fprintf (fd
, "%s\n",egysub
->text
[i
]);
1979 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
1982 void dump_microdvd(sub_data
* subd
, float fps
) {
1985 subtitle
*subs
= subd
->subtitles
;
1988 fd
= fopen("dumpsub.sub", "w");
1990 perror("dumpsub.sub: fopen");
1993 delay
= sub_delay
* sub_fps
;
1994 for (i
= 0; i
< subd
->sub_num
; ++i
) {
1996 start
= subs
[i
].start
;
1998 if (subd
->sub_uses_time
) {
1999 start
= start
* sub_fps
/ 100 ;
2000 end
= end
* sub_fps
/ 100;
2003 start
= start
* sub_fps
/ fps
;
2004 end
= end
* sub_fps
/ fps
;
2008 fprintf(fd
, "{%d}{%d}", start
, end
);
2009 for (j
= 0; j
< subs
[i
].lines
; ++j
)
2010 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
2014 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.sub\'.\n");
2017 void dump_jacosub(sub_data
* subd
, float fps
) {
2023 subtitle
*subs
= subd
->subtitles
;
2025 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2027 fd
=fopen("dumpsub.jss","w");
2030 perror("dump_jacosub: fopen");
2033 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
2034 for(i
=0; i
< subd
->sub_num
; i
++)
2036 onesub
=subs
+i
; //=&subs[i];
2039 if (!subd
->sub_uses_time
)
2040 temp
= temp
* 100 / sub_fps
;
2041 temp
-= sub_delay
* 100;
2042 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2043 m
=temp
/6000; temp
%=6000; //m =1*100*60
2044 s
=temp
/100; temp
%=100; //s =1*100
2046 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2049 if (!subd
->sub_uses_time
)
2050 temp
= temp
* 100 / sub_fps
;
2051 temp
-= sub_delay
* 100;
2052 h
=temp
/360000;temp
%=360000;
2053 m
=temp
/6000; temp
%=6000;
2054 s
=temp
/100; temp
%=100;
2056 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2058 for(j
=0;j
<onesub
->lines
;j
++)
2059 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2064 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2067 void dump_sami(sub_data
* subd
, float fps
) {
2072 subtitle
*subs
= subd
->subtitles
;
2074 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2076 fd
=fopen("dumpsub.smi","w");
2079 perror("dump_jacosub: fopen");
2082 fprintf(fd
, "<SAMI>\n"
2084 " <STYLE TYPE=\"Text/css\">\n"
2086 " 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"
2087 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2092 for(i
=0; i
< subd
->sub_num
; i
++)
2094 onesub
=subs
+i
; //=&subs[i];
2097 if (!subd
->sub_uses_time
)
2098 temp
= temp
* 100 / sub_fps
;
2099 temp
-= sub_delay
* 100;
2100 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2101 "\t <P>", temp
* 10);
2103 for(j
=0;j
<onesub
->lines
;j
++)
2104 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2109 if (!subd
->sub_uses_time
)
2110 temp
= temp
* 100 / sub_fps
;
2111 temp
-= sub_delay
* 100;
2112 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2113 "\t <P> \n", temp
* 10);
2115 fprintf(fd
, "</BODY>\n"
2118 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2121 void sub_free( sub_data
* subd
)
2125 if ( !subd
) return;
2127 for (i
= 0; i
< subd
->sub_num
; i
++)
2128 for (j
= 0; j
< subd
->subtitles
[i
].lines
; j
++)
2129 free( subd
->subtitles
[i
].text
[j
] );
2130 free( subd
->subtitles
);
2131 free( subd
->filename
);
2135 #define MAX_SUBLINE 512
2137 * \brief parse text and append it to subtitle in sub
2138 * \param sub subtitle struct to add text to
2139 * \param txt text to parse
2140 * \param len length of text in txt
2141 * \param endpts pts at which this subtitle text should be removed again
2143 * <> and {} are interpreted as comment delimiters, "\n", "\N", '\n', '\r'
2144 * and '\0' are interpreted as newlines, duplicate, leading and trailing
2145 * newlines are ignored.
2147 void sub_add_text(subtitle
*sub
, const char *txt
, int len
, double endpts
) {
2149 int double_newline
= 1; // ignore newlines at the beginning
2152 #ifdef CONFIG_FRIBIDI
2153 int orig_lines
= sub
->lines
;
2155 if (sub
->lines
>= SUB_MAX_TEXT
) return;
2157 buf
= malloc(MAX_SUBLINE
+ 1);
2158 sub
->text
[sub
->lines
] = buf
;
2159 sub
->endpts
[sub
->lines
] = endpts
;
2160 for (i
= 0; i
< len
&& pos
< MAX_SUBLINE
; i
++) {
2162 if (c
== '<') comment
|= 1;
2163 if (c
== '{') comment
|= 2;
2165 if (c
== '}') comment
&= ~2;
2166 if (c
== '>') comment
&= ~1;
2169 if (pos
== MAX_SUBLINE
- 1) {
2173 if (c
== '\\' && i
+ 1 < len
) {
2175 if (c
== 'n' || c
== 'N') c
= 0;
2177 if (c
== '\n' || c
== '\r') c
= 0;
2181 } else if (!double_newline
) {
2182 if (sub
->lines
>= SUB_MAX_TEXT
- 1) {
2183 mp_msg(MSGT_VO
, MSGL_WARN
, "Too many subtitle lines\n");
2190 buf
= malloc(MAX_SUBLINE
+ 1);
2191 sub
->text
[sub
->lines
] = buf
;
2192 sub
->endpts
[sub
->lines
] = endpts
;
2196 if (sub
->lines
< SUB_MAX_TEXT
&&
2197 strlen(sub
->text
[sub
->lines
]))
2199 #ifdef CONFIG_FRIBIDI
2200 sub
= sub_fribidi(sub
, sub_utf8
, orig_lines
);
2205 * \brief remove outdated subtitle lines.
2206 * \param sub subtitle struct to modify
2207 * \param pts current pts. All lines with endpts <= this will be removed.
2208 * Use MP_NOPTS_VALUE to remove all lines
2209 * \return 1 if sub was modified, 0 otherwise.
2211 int sub_clear_text(subtitle
*sub
, double pts
) {
2214 while (i
< sub
->lines
) {
2215 double endpts
= sub
->endpts
[i
];
2216 if (pts
== MP_NOPTS_VALUE
|| (endpts
!= MP_NOPTS_VALUE
&& pts
>= endpts
)) {
2219 for (j
= i
+ 1; j
< sub
->lines
; j
++) {
2220 sub
->text
[j
- 1] = sub
->text
[j
];
2221 sub
->endpts
[j
- 1] = sub
->endpts
[j
];