2 * Subtitle reader with format autodetection
5 * Some code cleanup & realloc() by A'rpi/ESP-team
15 #include <sys/types.h>
20 #include "subreader.h"
21 #include "stream/stream.h"
27 #define ERR ((void *) -1)
34 #include <fribidi/fribidi.h>
35 char *fribidi_charset
= NULL
; ///character set that will be passed to FriBiDi
36 int flip_hebrew
= 1; ///flip subtitles using fribidi
37 int fribidi_flip_commas
= 0; ///flip comma when fribidi is used
40 extern char* dvdsub_lang
;
42 /* Maximal length of line of a subtitle */
44 static float mpsub_position
=0;
45 static float mpsub_multiplier
=1.;
46 static int sub_slacktime
= 20000; //20 sec
48 int sub_no_text_pp
=0; // 1 => do not apply text post-processing
49 // like {\...} elimination in SSA format.
51 int sub_match_fuzziness
=0; // level of sub name matching fuzziness
53 /* Use the SUB_* constant defined in the header file */
54 int sub_format
=SUB_INVALID
;
57 Some subtitling formats, namely AQT and Subrip09, define the end of a
58 subtitle as the beginning of the following. Since currently we read one
59 subtitle at time, for these format we keep two global *subtitle,
60 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
61 so we can change its end when we read current subtitle starting time.
62 When CONFIG_SORTSUB is defined, we use a single global unsigned long,
63 previous_sub_end, for both (and even future) formats, to store the end of
64 the previous sub: it is initialized to 0 in sub_read_file and eventually
65 modified by sub_read_aqt_line or sub_read_subrip09_line.
67 unsigned long previous_sub_end
;
70 static int eol(char p
) {
71 return p
=='\r' || p
=='\n' || p
=='\0';
74 /* Remove leading and trailing space */
75 static void trail_space(char *s
) {
77 while (isspace(s
[i
])) ++i
;
78 if (i
) strcpy(s
, s
+ i
);
80 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
83 static char *stristr(const char *haystack
, const char *needle
) {
85 const char *p
= haystack
;
87 if (!(haystack
&& needle
)) return NULL
;
91 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
98 static subtitle
*sub_read_line_sami(stream_t
* st
, subtitle
*current
) {
99 static char line
[LINE_LEN
+1];
100 static char *s
= NULL
, *slacktime_s
;
101 char text
[LINE_LEN
+1], *p
=NULL
, *q
;
104 current
->lines
= current
->start
= current
->end
= 0;
105 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
108 /* read the first line */
110 if (!(s
= stream_read_line(st
, line
, LINE_LEN
))) return 0;
115 case 0: /* find "START=" or "Slacktime:" */
116 slacktime_s
= stristr (s
, "Slacktime:");
118 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
120 s
= stristr (s
, "Start=");
122 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
124 for (; *s
!= '>' && *s
!= '\0'; s
++);
130 case 1: /* find (optionnal) "<P", skip other TAGs */
131 for (; *s
== ' ' || *s
== '\t'; s
++); /* strip blanks, if any */
132 if (*s
== '\0') break;
133 if (*s
!= '<') { state
= 3; p
= text
; continue; } /* not a TAG */
135 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
136 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
142 case 2: /* find ">" */
143 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
146 case 3: /* get all text until '<' appears */
147 if (*s
== '\0') break;
148 else if (!strncasecmp (s
, "<br>", 4)) {
149 *p
= '\0'; p
= text
; trail_space (text
);
151 current
->text
[current
->lines
++] = strdup (text
);
154 else if ((*s
== '{') && !sub_no_text_pp
) { state
= 5; ++s
; continue; }
155 else if (*s
== '<') { state
= 4; }
156 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
157 else if (*s
== '\t') { *p
++ = ' '; s
++; }
158 else if (*s
== '\r' || *s
== '\n') { s
++; }
161 /* skip duplicated space */
162 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
166 case 4: /* get current->end or skip <TAG> */
167 q
= stristr (s
, "Start=");
169 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
170 *p
= '\0'; trail_space (text
);
172 current
->text
[current
->lines
++] = strdup (text
);
173 if (current
->lines
> 0) { state
= 99; break; }
177 if (s
) { s
++; state
= 3; continue; }
179 case 5: /* get rid of {...} text, but read the alignment code */
180 if ((*s
== '\\') && (*(s
+ 1) == 'a') && !sub_no_text_pp
) {
181 if (stristr(s
, "\\a1") != NULL
) {
182 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
185 if (stristr(s
, "\\a2") != NULL
) {
186 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
188 } else if (stristr(s
, "\\a3") != NULL
) {
189 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
191 } else if ((stristr(s
, "\\a4") != NULL
) || (stristr(s
, "\\a5") != NULL
) || (stristr(s
, "\\a8") != NULL
)) {
192 current
->alignment
= SUB_ALIGNMENT_TOPLEFT
;
194 } else if (stristr(s
, "\\a6") != NULL
) {
195 current
->alignment
= SUB_ALIGNMENT_TOPCENTER
;
197 } else if (stristr(s
, "\\a7") != NULL
) {
198 current
->alignment
= SUB_ALIGNMENT_TOPRIGHT
;
200 } else if (stristr(s
, "\\a9") != NULL
) {
201 current
->alignment
= SUB_ALIGNMENT_MIDDLELEFT
;
203 } else if (stristr(s
, "\\a10") != NULL
) {
204 current
->alignment
= SUB_ALIGNMENT_MIDDLECENTER
;
206 } else if (stristr(s
, "\\a11") != NULL
) {
207 current
->alignment
= SUB_ALIGNMENT_MIDDLERIGHT
;
211 if (*s
== '}') state
= 3;
217 if (state
!= 99 && !(s
= stream_read_line (st
, line
, LINE_LEN
))) {
218 if (current
->start
> 0) {
219 break; // if it is the last subtitle
225 } while (state
!= 99);
227 // For the last subtitle
228 if (current
->end
<= 0) {
229 current
->end
= current
->start
+ sub_slacktime
;
230 *p
= '\0'; trail_space (text
);
232 current
->text
[current
->lines
++] = strdup (text
);
239 static char *sub_readtext(char *source
, char **dest
) {
243 // printf("src=%p dest=%p \n",source,dest);
245 while ( !eol(*p
) && *p
!= '|' ) {
249 *dest
= malloc (len
+1);
250 if (!dest
) {return ERR
;}
252 strncpy(*dest
, source
, len
);
255 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
257 if (*p
) return p
; // not-last text field
258 else return NULL
; // last text field
261 static subtitle
*sub_read_line_microdvd(stream_t
*st
,subtitle
*current
) {
262 char line
[LINE_LEN
+1];
263 char line2
[LINE_LEN
+1];
268 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
269 } while ((sscanf (line
,
271 &(current
->start
), line2
) < 2) &&
273 "{%ld}{%ld}%[^\r\n]",
274 &(current
->start
), &(current
->end
), line2
) < 3));
279 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
280 if (current
->text
[i
]==ERR
) {return ERR
;}
282 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
289 static subtitle
*sub_read_line_mpl2(stream_t
*st
,subtitle
*current
) {
290 char line
[LINE_LEN
+1];
291 char line2
[LINE_LEN
+1];
296 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
297 } while ((sscanf (line
,
298 "[%ld][%ld]%[^\r\n]",
299 &(current
->start
), &(current
->end
), line2
) < 3));
300 current
->start
*= 10;
305 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
306 if (current
->text
[i
]==ERR
) {return ERR
;}
308 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
315 static subtitle
*sub_read_line_subrip(stream_t
* st
, subtitle
*current
) {
316 char line
[LINE_LEN
+1];
317 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
318 char *p
=NULL
, *q
=NULL
;
322 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
323 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
324 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
325 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
327 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
330 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
331 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
332 current
->text
[current
->lines
-1]=malloc (len
+1);
333 if (!current
->text
[current
->lines
-1]) return ERR
;
334 strncpy (current
->text
[current
->lines
-1], q
, len
);
335 current
->text
[current
->lines
-1][len
]='\0';
336 if (!*p
|| *p
=='\r' || *p
=='\n') break;
338 else while (*p
++!=']');
345 static subtitle
*sub_read_line_subviewer(stream_t
*st
,subtitle
*current
) {
346 char line
[LINE_LEN
+1];
347 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
351 while (!current
->text
[0]) {
352 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
353 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)
355 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
356 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
357 for (i
=0; i
<SUB_MAX_TEXT
;) {
359 if (!stream_read_line (st
, line
, LINE_LEN
)) break;
361 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++)
362 if (*p
!= ' ' && *p
!= '\t')
366 char *curptr
=current
->text
[i
]=malloc (len
+1);
367 if (!current
->text
[i
]) return ERR
;
368 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
370 /* let's filter html tags ::atmos */
397 static subtitle
*sub_read_line_subviewer2(stream_t
*st
,subtitle
*current
) {
398 char line
[LINE_LEN
+1];
403 while (!current
->text
[0]) {
404 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
407 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
409 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
410 for (i
=0; i
<SUB_MAX_TEXT
;) {
411 if (!stream_read_line (st
, line
, LINE_LEN
)) break;
412 if (line
[0]=='}') break;
414 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
416 current
->text
[i
]=malloc (len
+1);
417 if (!current
->text
[i
]) return ERR
;
418 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
430 static subtitle
*sub_read_line_vplayer(stream_t
*st
,subtitle
*current
) {
431 char line
[LINE_LEN
+1];
433 char *p
=NULL
, *next
,separator
;
436 while (!current
->text
[0]) {
437 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
438 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
441 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
445 // finds the body of the subtitle
452 printf("SUB: Skipping incorrect subtitle line!\n");
456 // by wodzu: hey! this time we know what length it has! what is
457 // that magic for? it can't deal with space instead of third
458 // colon! look, what simple it can be:
465 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
466 if (current
->text
[i
]==ERR
) {return ERR
;}
468 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
476 static subtitle
*sub_read_line_rt(stream_t
*st
,subtitle
*current
) {
477 //TODO: This format uses quite rich (sub/super)set of xhtml
478 // I couldn't check it since DTD is not included.
479 // WARNING: full XML parses can be required for proper parsing
480 char line
[LINE_LEN
+1];
481 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
482 char *p
=NULL
,*next
=NULL
;
485 while (!current
->text
[0]) {
486 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
487 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
488 //to describe the same moment in time. Maybe there are even more formats in use.
489 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
490 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
492 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
493 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
494 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
495 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
496 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
497 ((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) &&
498 ((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) &&
499 //now try it without end time
500 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
501 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
502 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
503 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
506 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
507 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
508 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
509 current
->end
= current
->start
+200;
511 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
512 next
= strstr(line
,"<clear/>");
513 if(next
&& strlen(next
)>8){
515 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
516 if (current
->text
[i
]==ERR
) {return ERR
;}
518 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
526 static subtitle
*sub_read_line_ssa(stream_t
*st
,subtitle
*current
) {
528 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
529 * other Sub Station Alpha scripts have only 8 commas before subtitle
530 * Reading the "ScriptType:" field is not reliable since many scripts appear
533 * http://www.scriptclub.org is a good place to find more examples
534 * http://www.eswat.demon.co.uk is where the SSA specs can be found
537 static int max_comma
= 32; /* let's use 32 for the case that the */
538 /* amount of commas increase with newer SSA versions */
540 int hour1
, min1
, sec1
, hunsec1
,
541 hour2
, min2
, sec2
, hunsec2
, nothing
;
544 char line
[LINE_LEN
+1],
550 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
551 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
552 "%[^\n\r]", ¬hing
,
553 &hour1
, &min1
, &sec1
, &hunsec1
,
554 &hour2
, &min2
, &sec2
, &hunsec2
,
557 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
558 "%[^\n\r]", ¬hing
,
559 &hour1
, &min1
, &sec1
, &hunsec1
,
560 &hour2
, &min2
, &sec2
, &hunsec2
,
563 line2
=strchr(line3
, ',');
565 for (comma
= 4; comma
< max_comma
; comma
++)
568 if(!(tmp
=strchr(++tmp
, ','))) break;
569 if(*(++tmp
) == ' ') break;
570 /* a space after a comma means we're already in a sentence */
574 if(comma
< max_comma
)max_comma
= comma
;
575 /* eliminate the trailing comma */
576 if(*line2
== ',') line2
++;
578 current
->lines
=0;num
=0;
579 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
580 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
582 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
583 current
->text
[num
]=malloc(tmp
-line2
+1);
584 strncpy (current
->text
[num
], line2
, tmp
-line2
);
585 current
->text
[num
][tmp
-line2
]='\0';
589 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
592 current
->text
[num
]=strdup(line2
);
598 static void sub_pp_ssa(subtitle
*sub
) {
603 /* eliminate any text enclosed with {}, they are font and color settings */
604 so
=de
=sub
->text
[--l
];
606 if(*so
== '{' && so
[1]=='\\') {
607 for (start
=so
; *so
&& *so
!='}'; so
++);
608 if(*so
) so
++; else so
=start
;
620 * PJS subtitles reader.
621 * That's the "Phoenix Japanimation Society" format.
622 * I found some of them in http://www.scriptsclub.org/ (used for anime).
623 * The time is in tenths of second.
625 * by set, based on code by szabi (dunnowhat sub format ;-)
627 static subtitle
*sub_read_line_pjs(stream_t
*st
,subtitle
*current
) {
628 char line
[LINE_LEN
+1];
629 char text
[LINE_LEN
+1], *s
, *d
;
631 if (!stream_read_line (st
, line
, LINE_LEN
))
634 for (s
=line
; *s
&& isspace(*s
); s
++);
635 /* allow empty lines at the end of the file */
639 if (sscanf (s
, "%ld,%ld,", &(current
->start
),
640 &(current
->end
)) <2) {
643 /* the files I have are in tenths of second */
644 current
->start
*= 10;
646 /* walk to the beggining of the string */
647 for (; *s
; s
++) if (*s
==',') break;
649 for (s
++; *s
; s
++) if (*s
==',') break;
655 /* copy the string to the text buffer */
656 for (s
++, d
=text
; *s
&& *s
!='"'; s
++, d
++)
659 current
->text
[0] = strdup(text
);
665 static subtitle
*sub_read_line_mpsub(stream_t
*st
, subtitle
*current
) {
666 char line
[LINE_LEN
+1];
673 if (!stream_read_line(st
, line
, LINE_LEN
)) return NULL
;
674 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
676 mpsub_position
+= a
*mpsub_multiplier
;
677 current
->start
=(int) mpsub_position
;
678 mpsub_position
+= b
*mpsub_multiplier
;
679 current
->end
=(int) mpsub_position
;
681 while (num
< SUB_MAX_TEXT
) {
682 if (!stream_read_line (st
, line
, LINE_LEN
)) {
683 if (num
== 0) return NULL
;
687 while (isspace(*p
)) p
++;
688 if (eol(*p
) && num
> 0) return current
;
689 if (eol(*p
)) return NULL
;
691 for (q
=p
; !eol(*q
); q
++);
694 current
->text
[num
]=strdup(p
);
695 // printf (">%s<\n",p);
696 current
->lines
= ++num
;
698 if (num
) return current
;
702 return NULL
; // we should have returned before if it's OK
705 #ifndef CONFIG_SORTSUB
706 //we don't need this if we use previous_sub_end
707 subtitle
*previous_aqt_sub
= NULL
;
710 static subtitle
*sub_read_line_aqt(stream_t
*st
,subtitle
*current
) {
711 char line
[LINE_LEN
+1];
716 // try to locate next subtitle
717 if (!stream_read_line (st
, line
, LINE_LEN
))
719 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
723 #ifdef CONFIG_SORTSUB
724 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
726 if (previous_aqt_sub
!= NULL
)
727 previous_aqt_sub
->end
= current
->start
-1;
729 previous_aqt_sub
= current
;
732 if (!stream_read_line (st
, line
, LINE_LEN
))
735 sub_readtext((char *) &line
,¤t
->text
[0]);
737 current
->end
= current
->start
; // will be corrected by next subtitle
739 if (!stream_read_line (st
, line
, LINE_LEN
))
743 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
744 if (current
->text
[i
]==ERR
) {return ERR
;}
746 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
750 if (!strlen(current
->text
[0]) && !strlen(current
->text
[1])) {
751 #ifdef CONFIG_SORTSUB
752 previous_sub_end
= 0;
754 // void subtitle -> end of previous marked and exit
755 previous_aqt_sub
= NULL
;
763 #ifndef CONFIG_SORTSUB
764 subtitle
*previous_subrip09_sub
= NULL
;
767 static subtitle
*sub_read_line_subrip09(stream_t
*st
,subtitle
*current
) {
768 char line
[LINE_LEN
+1];
774 // try to locate next subtitle
775 if (!stream_read_line (st
, line
, LINE_LEN
))
777 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
781 current
->start
= a1
*360000+a2
*6000+a3
*100;
783 #ifdef CONFIG_SORTSUB
784 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
786 if (previous_subrip09_sub
!= NULL
)
787 previous_subrip09_sub
->end
= current
->start
-1;
789 previous_subrip09_sub
= current
;
792 if (!stream_read_line (st
, line
, LINE_LEN
))
797 current
->text
[0]=""; // just to be sure that string is clear
799 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
800 if (current
->text
[i
]==ERR
) {return ERR
;}
802 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
806 if (!strlen(current
->text
[0]) && (i
==0)) {
807 #ifdef CONFIG_SORTSUB
808 previous_sub_end
= 0;
810 // void subtitle -> end of previous marked and exit
811 previous_subrip09_sub
= NULL
;
819 static subtitle
*sub_read_line_jacosub(stream_t
* st
, subtitle
* current
)
821 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
822 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
823 static unsigned jacoTimeres
= 30;
824 static int jacoShift
= 0;
826 memset(current
, 0, sizeof(subtitle
));
827 memset(line1
, 0, LINE_LEN
);
828 memset(line2
, 0, LINE_LEN
);
829 memset(directive
, 0, LINE_LEN
);
830 while (!current
->text
[0]) {
831 if (!stream_read_line(st
, line1
, LINE_LEN
)) {
835 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
836 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
837 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
838 if (line1
[0] == '#') {
839 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
841 unsigned units
= jacoShift
;
842 switch (toupper(line1
[1])) {
844 if (isalpha(line1
[2])) {
849 if (sscanf(&line1
[delta
], "%d", &hours
)) {
854 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
856 (&line1
[delta
], "%*d:%*d:%d",
858 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
862 sscanf(&line1
[delta
], "%d:%d.%d",
863 &minutes
, &seconds
, &units
);
868 sscanf(&line1
[delta
], "%d.%d", &seconds
,
873 ((hours
* 3600 + minutes
* 60 +
874 seconds
) * jacoTimeres
+
879 if (isalpha(line1
[2])) {
884 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
891 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
894 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
900 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
901 jacoShift
) * 100.0 / jacoTimeres
);
904 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
905 jacoShift
) * 100.0 / jacoTimeres
);
909 while ((*p
== ' ') || (*p
== '\t')) {
912 if (isalpha(*p
)||*p
== '[') {
915 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
916 return (subtitle
*) ERR
;
917 jLength
= strlen(directive
);
918 for (cont
= 0; cont
< jLength
; ++cont
) {
919 if (isalpha(*(directive
+ cont
)))
920 *(directive
+ cont
) = toupper(*(directive
+ cont
));
922 if ((strstr(directive
, "RDB") != NULL
)
923 || (strstr(directive
, "RDC") != NULL
)
924 || (strstr(directive
, "RLB") != NULL
)
925 || (strstr(directive
, "RLG") != NULL
)) {
928 if (strstr(directive
, "JL") != NULL
) {
929 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
930 } else if (strstr(directive
, "JR") != NULL
) {
931 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
933 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
935 strcpy(line2
, line1
);
938 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
946 //the next line to get rid of a blank after the comment
947 if ((*(p
+ 1)) == ' ')
959 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
967 if (*(p
+ 1) == 'n') {
970 current
->text
[current
->lines
++] = strdup(line1
);
974 if ((toupper(*(p
+ 1)) == 'C')
975 || (toupper(*(p
+ 1)) == 'F')) {
979 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
980 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
981 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
985 if ((*(p
+ 1) == '\\') ||
986 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
988 } else if (eol(*(p
+ 1))) {
989 if (!stream_read_line(st
, directive
, LINE_LEN
))
991 trail_space(directive
);
992 strncat(line2
, directive
,
993 (LINE_LEN
> 511) ? LINE_LEN
: 511);
1004 current
->text
[current
->lines
] = strdup(line1
);
1010 static int sub_autodetect (stream_t
* st
, int *uses_time
) {
1011 char line
[LINE_LEN
+1];
1017 if (!stream_read_line (st
, line
, LINE_LEN
))
1020 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
1021 {*uses_time
=0;return SUB_MICRODVD
;}
1022 if (sscanf (line
, "{%d}{}", &i
)==1)
1023 {*uses_time
=0;return SUB_MICRODVD
;}
1024 if (sscanf (line
, "[%d][%d]", &i
, &i
)==2)
1025 {*uses_time
=1;return SUB_MPL2
;}
1026 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
1027 {*uses_time
=1;return SUB_SUBRIP
;}
1028 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
1029 {*uses_time
=1;return SUB_SUBVIEWER
;}
1030 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
1031 {*uses_time
=1;return SUB_SUBVIEWER2
;}
1032 if (strstr (line
, "<SAMI>"))
1033 {*uses_time
=1; return SUB_SAMI
;}
1034 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
1035 {*uses_time
= 1; return SUB_JACOSUB
;}
1036 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
1037 {*uses_time
= 1; return SUB_JACOSUB
;}
1038 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
1039 {*uses_time
=1;return SUB_VPLAYER
;}
1040 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
1041 {*uses_time
=1;return SUB_VPLAYER
;}
1042 //TODO: just checking if first line of sub starts with "<" is WAY
1043 // too weak test for RT
1044 // Please someone who knows the format of RT... FIX IT!!!
1045 // It may conflict with other sub formats in the future (actually it doesn't)
1047 {*uses_time
=1;return SUB_RT
;}
1049 if (!memcmp(line
, "Dialogue: Marked", 16))
1050 {*uses_time
=1; return SUB_SSA
;}
1051 if (!memcmp(line
, "Dialogue: ", 10))
1052 {*uses_time
=1; return SUB_SSA
;}
1053 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
1054 {*uses_time
=1;return SUB_PJS
;}
1055 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
1056 {*uses_time
=0; return SUB_MPSUB
;}
1057 if (sscanf (line
, "FORMAT=TIM%c", &p
)==1 && p
=='E')
1058 {*uses_time
=1; return SUB_MPSUB
;}
1059 if (strstr (line
, "-->>"))
1060 {*uses_time
=0; return SUB_AQTITLE
;}
1061 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
1062 {*uses_time
=1;return SUB_SUBRIP09
;}
1065 return SUB_INVALID
; // too many bad lines
1068 extern int sub_utf8
;
1069 int sub_utf8_prev
=0;
1071 extern float sub_delay
;
1072 extern float sub_fps
;
1075 static iconv_t icdsc
= (iconv_t
)(-1);
1077 void subcp_open (stream_t
*st
)
1079 char *tocp
= "UTF-8";
1082 const char *cp_tmp
= sub_cp
;
1084 char enca_lang
[3], enca_fallback
[100];
1085 if (sscanf(sub_cp
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
1086 || sscanf(sub_cp
, "ENCA:%2s:%99s", enca_lang
, enca_fallback
) == 2) {
1087 if (st
&& st
->flags
& STREAM_SEEK
) {
1088 cp_tmp
= guess_cp(st
, enca_lang
, enca_fallback
);
1090 cp_tmp
= enca_fallback
;
1092 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: enca failed, stream must be seekable.\n");
1096 if ((icdsc
= iconv_open (tocp
, cp_tmp
)) != (iconv_t
)(-1)){
1097 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1100 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1104 void subcp_close (void)
1106 if (icdsc
!= (iconv_t
)(-1)){
1107 (void) iconv_close (icdsc
);
1108 icdsc
= (iconv_t
)(-1);
1109 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
1113 subtitle
* subcp_recode (subtitle
*sub
)
1116 size_t ileft
, oleft
;
1118 if(icdsc
== (iconv_t
)(-1)) return sub
;
1121 ip
= sub
->text
[--l
];
1125 if (!(ot
= malloc(oleft
+ 1))){
1126 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1130 if (iconv(icdsc
, &ip
, &ileft
,
1131 &op
, &oleft
) == (size_t)(-1)) {
1132 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line.\n");
1136 // In some stateful encodings, we must clear the state to handle the last character
1137 if (iconv(icdsc
, NULL
, NULL
,
1138 &op
, &oleft
) == (size_t)(-1)) {
1139 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line, can't clear encoding state.\n");
1142 free (sub
->text
[l
]);
1149 #ifdef CONFIG_FRIBIDI
1151 #define max(a,b) (((a)>(b))?(a):(b))
1153 static subtitle
* sub_fribidi (subtitle
*sub
, int sub_utf8
)
1155 FriBidiChar logical
[LINE_LEN
+1], visual
[LINE_LEN
+1]; // Hopefully these two won't smash the stack
1156 char *ip
= NULL
, *op
= NULL
;
1157 FriBidiCharType base
;
1158 size_t len
,orig_len
;
1161 fribidi_boolean log2vis
;
1162 if(flip_hebrew
) { // Please fix the indentation someday
1163 fribidi_set_mirroring(1);
1164 fribidi_set_reorder_nsm(0);
1166 if( sub_utf8
== 0 ) {
1167 char_set_num
= fribidi_parse_charset (fribidi_charset
?fribidi_charset
:"ISO8859-8");
1169 char_set_num
= fribidi_parse_charset ("UTF-8");
1172 ip
= sub
->text
[--l
];
1173 orig_len
= len
= strlen( ip
); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1174 if(len
> LINE_LEN
) {
1175 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: sub->text is longer than LINE_LEN.\n");
1179 len
= fribidi_charset_to_unicode (char_set_num
, ip
, len
, logical
);
1180 base
= fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
1181 log2vis
= fribidi_log2vis (logical
, len
, &base
,
1183 visual
, NULL
, NULL
, NULL
);
1185 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
,
1187 if((op
= malloc((max(2*orig_len
,2*len
) + 1))) == NULL
) {
1188 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1192 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1198 for (l
= sub
->lines
; l
;)
1199 free (sub
->text
[--l
]);
1208 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1209 int sub_num
, int sub_uses_time
) {
1213 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1214 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1218 if (sub
->end
<= sub
->start
){
1219 sub
->end
= sub
->start
+ subfms
;
1226 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1227 // these subtitles overlap for less than 0.2 seconds
1228 // and would result in very short overlapping subtitle
1229 // so let's fix the problem here, before overlapping code
1230 // get its hands on them
1231 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1232 sub
->end
-= half
+ 1;
1233 nextsub
->start
+= delta
- half
;
1235 if (sub
->end
>= nextsub
->start
){
1236 sub
->end
= nextsub
->start
- 1;
1237 if (sub
->end
- sub
->start
> subfms
)
1238 sub
->end
= sub
->start
+ subfms
;
1245 * Movies are often converted from FILM (24 fps)
1246 * to PAL (25) by simply speeding it up, so we
1247 * to multiply the original timestmaps by
1248 * (Movie's FPS / Subtitle's (guessed) FPS)
1249 * so eg. for 23.98 fps movie and PAL time based
1250 * subtitles we say -subfps 25 and we're fine!
1253 /* timed sub fps correction ::atmos */
1254 /* the frame-based case is handled in mpcommon.c
1255 * where find_sub is called */
1256 if(sub_uses_time
&& sub_fps
) {
1257 sub
->start
*= sub_fps
/fps
;
1258 sub
->end
*= sub_fps
/fps
;
1264 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Adjusted %d subtitle(s).\n", n
);
1268 subtitle
* (*read
)(stream_t
*st
,subtitle
*dest
);
1269 void (*post
)(subtitle
*dest
);
1274 const char* guess_buffer_cp(unsigned char* buffer
, int buflen
, const char *preferred_language
, const char *fallback
)
1276 const char **languages
;
1278 EncaAnalyser analyser
;
1279 EncaEncoding encoding
;
1280 const char *detected_sub_cp
= NULL
;
1283 languages
= enca_get_languages(&langcnt
);
1284 mp_msg(MSGT_SUBREADER
, MSGL_V
, "ENCA supported languages: ");
1285 for (i
= 0; i
< langcnt
; i
++) {
1286 mp_msg(MSGT_SUBREADER
, MSGL_V
, "%s ", languages
[i
]);
1288 mp_msg(MSGT_SUBREADER
, MSGL_V
, "\n");
1290 for (i
= 0; i
< langcnt
; i
++) {
1291 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1292 analyser
= enca_analyser_alloc(languages
[i
]);
1293 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1294 enca_analyser_free(analyser
);
1295 if (encoding
.charset
!= ENCA_CS_UNKNOWN
) {
1296 detected_sub_cp
= enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
);
1303 if (!detected_sub_cp
) {
1304 detected_sub_cp
= fallback
;
1305 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detection failed: fallback to %s\n", fallback
);
1307 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", detected_sub_cp
);
1310 return detected_sub_cp
;
1313 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1314 const char* guess_cp(stream_t
*st
, const char *preferred_language
, const char *fallback
)
1317 unsigned char *buffer
;
1318 const char *detected_sub_cp
= NULL
;
1320 buffer
= malloc(MAX_GUESS_BUFFER_SIZE
);
1321 buflen
= stream_read(st
,buffer
, MAX_GUESS_BUFFER_SIZE
);
1323 detected_sub_cp
= guess_buffer_cp(buffer
, buflen
, preferred_language
, fallback
);
1329 return detected_sub_cp
;
1331 #undef MAX_GUESS_BUFFER_SIZE
1334 sub_data
* sub_read_file (char *filename
, float fps
) {
1336 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1337 subtitle
*first
, *second
, *sub
, *return_sub
;
1338 sub_data
*subt_data
;
1339 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1340 struct subreader sr
[]=
1342 { sub_read_line_microdvd
, NULL
, "microdvd" },
1343 { sub_read_line_subrip
, NULL
, "subrip" },
1344 { sub_read_line_subviewer
, NULL
, "subviewer" },
1345 { sub_read_line_sami
, NULL
, "sami" },
1346 { sub_read_line_vplayer
, NULL
, "vplayer" },
1347 { sub_read_line_rt
, NULL
, "rt" },
1348 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1349 { sub_read_line_pjs
, NULL
, "pjs" },
1350 { sub_read_line_mpsub
, NULL
, "mpsub" },
1351 { sub_read_line_aqt
, NULL
, "aqt" },
1352 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1353 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1354 { sub_read_line_jacosub
, NULL
, "jacosub" },
1355 { sub_read_line_mpl2
, NULL
, "mpl2" }
1357 struct subreader
*srp
;
1359 if(filename
==NULL
) return NULL
; //qnx segfault
1361 fd
=open_stream (filename
, NULL
, &i
); if (!fd
) return NULL
;
1363 sub_format
=sub_autodetect (fd
, &uses_time
);
1364 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1365 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1367 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Detected subtitle file format: %s\n", srp
->name
);
1373 sub_utf8_prev
=sub_utf8
;
1377 if ((l
=strlen(filename
))>4){
1378 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1380 if (l
>= strlen(exts
[k
]) && !strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1385 if (k
<0) subcp_open(fd
);
1390 first
=malloc(n_max
*sizeof(subtitle
));
1394 sub_utf8
=sub_utf8_prev
;
1399 #ifdef CONFIG_SORTSUB
1400 sub
= malloc(sizeof(subtitle
));
1401 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1402 //as the beginning of the following
1403 previous_sub_end
= 0;
1408 first
=realloc(first
,n_max
*sizeof(subtitle
));
1410 #ifndef CONFIG_SORTSUB
1411 sub
= &first
[sub_num
];
1413 memset(sub
, '\0', sizeof(subtitle
));
1414 sub
=srp
->read(fd
,sub
);
1415 if(!sub
) break; // EOF
1417 if ((sub
!=ERR
) && (sub_utf8
& 2)) sub
=subcp_recode(sub
);
1419 #ifdef CONFIG_FRIBIDI
1420 if (sub
!=ERR
) sub
=sub_fribidi(sub
,sub_utf8
);
1427 if ( first
) free(first
);
1430 // Apply any post processing that needs recoding first
1431 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1432 #ifdef CONFIG_SORTSUB
1433 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1434 first
[sub_num
].start
= sub
->start
;
1435 first
[sub_num
].end
= sub
->end
;
1436 first
[sub_num
].lines
= sub
->lines
;
1437 first
[sub_num
].alignment
= sub
->alignment
;
1438 for(i
= 0; i
< sub
->lines
; ++i
){
1439 first
[sub_num
].text
[i
] = sub
->text
[i
];
1441 if (previous_sub_end
){
1442 first
[sub_num
- 1].end
= previous_sub_end
;
1443 previous_sub_end
= 0;
1446 for(j
= sub_num
- 1; j
>= 0; --j
){
1447 first
[j
+ 1].start
= first
[j
].start
;
1448 first
[j
+ 1].end
= first
[j
].end
;
1449 first
[j
+ 1].lines
= first
[j
].lines
;
1450 first
[j
+ 1].alignment
= first
[j
].alignment
;
1451 for(i
= 0; i
< first
[j
].lines
; ++i
){
1452 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1454 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1455 first
[j
].start
= sub
->start
;
1456 first
[j
].end
= sub
->end
;
1457 first
[j
].lines
= sub
->lines
;
1458 first
[j
].alignment
= sub
->alignment
;
1459 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1460 first
[j
].text
[i
] = sub
->text
[i
];
1462 if (previous_sub_end
){
1463 first
[j
].end
= first
[j
- 1].end
;
1464 first
[j
- 1].end
= previous_sub_end
;
1465 previous_sub_end
= 0;
1472 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1481 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1482 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Read %i subtitles", sub_num
);
1483 if (sub_errs
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,", %i bad line(s).\n", sub_errs
);
1484 else mp_msg(MSGT_SUBREADER
,MSGL_INFO
,".\n");
1491 // we do overlap if the user forced it (suboverlap_enable == 2) or
1492 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1493 // this is because usually overlapping subtitles are found in these formats,
1494 // while in others they are probably result of bad timing
1495 if ((suboverlap_enabled
== 2) ||
1496 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1497 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1498 // here we manage overlapping subtitles
1503 // for each subtitle in first[] we deal with its 'block' of
1505 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1506 unsigned long global_start
= first
[sub_first
].start
,
1507 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1508 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1509 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1510 char real_block
= 1;
1512 // here we find the number of subtitles inside the 'block'
1513 // and its span interval. this works well only with sorted
1515 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1517 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1518 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1519 global_start
= first
[sub_first
+ sub_to_add
].start
;
1521 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1522 global_end
= first
[sub_first
+ sub_to_add
].end
;
1526 // we need a structure to keep trace of the screen lines
1527 // used by the subs, a 'placeholder'
1528 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1529 // from a block of sub_to_add+1 subs
1530 placeholder
= malloc(sizeof(int *) * counter
);
1531 for (i
= 0; i
< counter
; ++i
) {
1532 placeholder
[i
] = malloc(sizeof(int) * lines_to_add
);
1533 for (j
= 0; j
< lines_to_add
; ++j
) {
1534 placeholder
[i
][j
] = -1;
1539 local_end
= global_start
- 1;
1543 // here we find the beginning and the end of a new
1544 // subtitle in the block
1545 local_start
= local_end
+ 1;
1546 local_end
= global_end
;
1547 for (j
= 0; j
<= sub_to_add
; ++j
) {
1548 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1549 local_end
= first
[sub_first
+ j
].start
- 1;
1550 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1551 local_end
= first
[sub_first
+ j
].end
;
1554 // here we allocate the screen lines to subs we must
1555 // display in current local_start-local_end interval.
1556 // if the subs were yet presents in the previous interval
1557 // they keep the same lines, otherside they get unused lines
1558 for (j
= 0; j
<= sub_to_add
; ++j
) {
1559 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1560 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1563 int fragment_position
= -1;
1565 // if this is not the first new sub of the block
1566 // we find if this sub was present in the previous
1569 for (i
= 0; i
< lines_to_add
; ++i
) {
1570 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1571 placeholder
[counter
][i
] = sub_first
+ j
;
1578 // we are looking for the shortest among all groups of
1579 // sequential blank lines whose length is greater than or
1580 // equal to sub_lines. we store in fragment_position the
1581 // position of the shortest group, in fragment_length its
1582 // length, and in tmp the length of the group currently
1584 for (i
= 0; i
< lines_to_add
; ++i
) {
1585 if (placeholder
[counter
][i
] == -1) {
1586 // placeholder[counter][i] is part of the current group
1590 if (tmp
== sub_lines
) {
1591 // current group's size fits exactly the one we
1592 // need, so we stop looking
1593 fragment_position
= i
- tmp
;
1597 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1598 // current group is the best we found till here,
1599 // but is still bigger than the one we are looking
1600 // for, so we keep on looking
1601 fragment_length
= tmp
;
1602 fragment_position
= i
- tmp
;
1605 // current group doesn't fit at all, so we forget it
1611 // last screen line is blank, a group ends with it
1612 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1613 fragment_position
= i
- tmp
;
1616 if (fragment_position
== -1) {
1617 // it was not possible to find free screen line(s) for a subtitle,
1618 // usually this means a bug in the code; however we do not overlap
1619 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1620 higher_line
= SUB_MAX_TEXT
+ 1;
1623 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1624 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1629 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1630 if (placeholder
[counter
][j
] != -1)
1635 if (higher_line
>= SUB_MAX_TEXT
) {
1636 // the 'block' has too much lines, so we don't overlap the
1638 second
= (subtitle
*) realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1639 for (j
= 0; j
<= sub_to_add
; ++j
) {
1641 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1642 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1643 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1644 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1645 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1646 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1647 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1650 sub_num
+= sub_to_add
+ 1;
1651 sub_first
+= sub_to_add
;
1656 // we read the placeholder structure and create the new
1658 second
= (subtitle
*) realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1659 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1660 second
[sub_num
].start
= local_start
;
1661 second
[sub_num
].end
= local_end
;
1662 second
[sub_num
].alignment
= first
[sub_first
].alignment
;
1663 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1664 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1665 if (placeholder
[counter
][j
] != -1) {
1666 int lines
= first
[placeholder
[counter
][j
]].lines
;
1667 for (ls
= 0; ls
< lines
; ++ls
) {
1668 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1672 second
[sub_num
].text
[i
++] = strdup(" ");
1677 } while (local_end
< global_end
);
1679 for (i
= 0; i
< counter
; ++i
)
1680 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1682 counter
= 2 * sub_to_add
+ 1;
1683 for (i
= 0; i
< counter
; ++i
) {
1684 free(placeholder
[i
]);
1687 sub_first
+= sub_to_add
;
1690 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1691 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1692 free(first
[j
].text
[i
]);
1697 return_sub
= second
;
1698 } else { //if(suboverlap_enabled)
1699 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1702 if (return_sub
== NULL
) return NULL
;
1703 subt_data
= malloc(sizeof(sub_data
));
1704 subt_data
->filename
= strdup(filename
);
1705 subt_data
->sub_uses_time
= uses_time
;
1706 subt_data
->sub_num
= sub_num
;
1707 subt_data
->sub_errs
= sub_errs
;
1708 subt_data
->subtitles
= return_sub
;
1713 char * strreplace( char * in
,char * what
,char * whereof
)
1718 if ( ( in
== NULL
)||( what
== NULL
)||( whereof
== NULL
)||( ( tmp
=strstr( in
,what
) ) == NULL
) ) return NULL
;
1719 for( i
=0;i
<strlen( whereof
);i
++ ) tmp
[i
]=whereof
[i
];
1720 if ( strlen( what
) > strlen( whereof
) ) tmp
[i
]=0;
1726 static void strcpy_trim(char *d
, char *s
)
1728 // skip leading whitespace
1729 while (*s
&& !isalnum(*s
)) {
1734 while (*s
&& isalnum(*s
)) {
1739 // trim excess whitespace
1740 while (*s
&& !isalnum(*s
)) {
1749 static void strcpy_strip_ext(char *d
, char *s
)
1751 char *tmp
= strrchr(s
,'.');
1756 strncpy(d
, s
, tmp
-s
);
1765 static void strcpy_get_ext(char *d
, char *s
)
1767 char *tmp
= strrchr(s
,'.');
1776 static int whiteonly(char *s
)
1779 if (isalnum(*s
)) return 0;
1785 typedef struct subfn
1791 static int compare_sub_priority(const void *a
, const void *b
)
1793 if (((const subfn
*)a
)->priority
> ((const subfn
*)b
)->priority
) {
1795 } else if (((const subfn
*)a
)->priority
< ((const subfn
*)b
)->priority
) {
1798 return strcoll(((const subfn
*)a
)->fname
, ((const subfn
*)b
)->fname
);
1802 char** sub_filenames(const char* path
, char *fname
)
1804 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
1805 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
1807 int len
, pos
, found
, i
, j
;
1808 char * sub_exts
[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
1819 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
1820 +(strlen(path
) > 256 ? strlen(path
) : 256)+2;
1822 f_dir
= malloc(len
);
1823 f_fname
= malloc(len
);
1824 f_fname_noext
= malloc(len
);
1825 f_fname_trim
= malloc(len
);
1827 tmp_fname_noext
= malloc(len
);
1828 tmp_fname_trim
= malloc(len
);
1829 tmp_fname_ext
= malloc(len
);
1831 tmpresult
= malloc(len
);
1833 result
= malloc(sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1834 memset(result
, 0, sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1838 tmp
= strrchr(fname
,'/');
1839 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1840 if(!tmp
)tmp
= strrchr(fname
,'\\');
1841 if(!tmp
)tmp
= strrchr(fname
,':');
1844 // extract filename & dirname from fname
1846 strcpy(f_fname
, tmp
+1);
1848 strncpy(f_dir
, fname
, pos
+1);
1851 strcpy(f_fname
, fname
);
1852 strcpy(f_dir
, "./");
1855 strcpy_strip_ext(f_fname_noext
, f_fname
);
1856 strcpy_trim(f_fname_trim
, f_fname_noext
);
1859 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
1860 tmp_sub_id
= malloc(strlen(dvdsub_lang
)+1);
1861 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
1865 // 1 = any subtitle file
1866 // 2 = any sub file containing movie name
1867 // 3 = sub file containing movie name and the lang extension
1868 for (j
= 0; j
<= 1; j
++) {
1869 d
= opendir(j
== 0 ? f_dir
: path
);
1871 while ((de
= readdir(d
))) {
1872 // retrieve various parts of the filename
1873 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
1874 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
1875 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
1877 // does it end with a subtitle extension?
1881 for (i
= ((sub_cp
&& strncasecmp(sub_cp
, "enca", 4) != 0) ? 3 : 0); sub_exts
[i
]; i
++) {
1883 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
1886 for (i
= 0; sub_exts
[i
]; i
++) {
1888 if (strcasecmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
1894 // we have a (likely) subtitle file
1897 if (!prio
&& tmp_sub_id
)
1899 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
1900 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"dvdsublang...%s\n", tmpresult
);
1901 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
1902 // matches the movie name + lang extension
1906 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
1907 // matches the movie name
1910 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && (sub_match_fuzziness
>= 1)) {
1911 // contains the movie name
1912 tmp
+= strlen(f_fname_trim
);
1913 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
1914 // with sub_id specified prefer localized subtitles
1916 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
1917 // without sub_id prefer "plain" name
1920 // with no localized subs found, try any else instead
1925 // doesn't contain the movie name
1926 // don't try in the mplayer subtitle directory
1927 if ((j
== 0) && (sub_match_fuzziness
>= 2)) {
1935 if (i
<3){ // prefer UTF-8 coded
1939 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
1940 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1941 if ((f
= fopen(tmpresult
, "rt"))) {
1943 result
[subcnt
].priority
= prio
;
1944 result
[subcnt
].fname
= strdup(tmpresult
);
1950 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
1957 if (tmp_sub_id
) free(tmp_sub_id
);
1961 free(f_fname_noext
);
1964 free(tmp_fname_noext
);
1965 free(tmp_fname_trim
);
1966 free(tmp_fname_ext
);
1970 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
1972 result2
= malloc(sizeof(char*)*(subcnt
+1));
1973 memset(result2
, 0, sizeof(char*)*(subcnt
+1));
1975 for (i
= 0; i
< subcnt
; i
++) {
1976 result2
[i
] = result
[i
].fname
;
1978 result2
[subcnt
] = NULL
;
1985 void list_sub_file(sub_data
* subd
){
1987 subtitle
*subs
= subd
->subtitles
;
1989 for(j
=0; j
< subd
->sub_num
; j
++){
1990 subtitle
* egysub
=&subs
[j
];
1991 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"%i line%c (%li-%li)\n",
1993 (1==egysub
->lines
)?' ':'s',
1996 for (i
=0; i
<egysub
->lines
; i
++) {
1997 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
1999 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\n");
2002 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Subtitle format %s time.\n",
2003 subd
->sub_uses_time
? "uses":"doesn't use");
2004 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
2007 void dump_srt(sub_data
* subd
, float fps
){
2013 subtitle
*subs
= subd
->subtitles
;
2015 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2017 fd
=fopen("dumpsub.srt","w");
2020 perror("dump_srt: fopen");
2023 for(i
=0; i
< subd
->sub_num
; i
++)
2025 onesub
=subs
+i
; //=&subs[i];
2026 fprintf(fd
,"%d\n",i
+1);//line number
2029 if (!subd
->sub_uses_time
)
2030 temp
= temp
* 100 / sub_fps
;
2031 temp
-= sub_delay
* 100;
2032 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2033 m
=temp
/6000; temp
%=6000; //m =1*100*60
2034 s
=temp
/100; temp
%=100; //s =1*100
2035 ms
=temp
*10; //ms=1*10
2036 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
2039 if (!subd
->sub_uses_time
)
2040 temp
= temp
* 100 / sub_fps
;
2041 temp
-= sub_delay
* 100;
2042 h
=temp
/360000;temp
%=360000;
2043 m
=temp
/6000; temp
%=6000;
2044 s
=temp
/100; temp
%=100;
2046 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
2048 for(j
=0;j
<onesub
->lines
;j
++)
2049 fprintf(fd
,"%s\n",onesub
->text
[j
]);
2054 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2057 void dump_mpsub(sub_data
* subd
, float fps
){
2061 subtitle
*subs
= subd
->subtitles
;
2063 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
2064 if (sub_fps
==0) sub_fps
=fps
;
2066 fd
=fopen ("dump.mpsub", "w");
2068 perror ("dump_mpsub: fopen");
2073 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
2074 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
2076 for(j
=0; j
< subd
->sub_num
; j
++){
2077 subtitle
* egysub
=&subs
[j
];
2078 if (subd
->sub_uses_time
) {
2079 a
=((egysub
->start
-mpsub_position
)/100.0);
2080 b
=((egysub
->end
-egysub
->start
)/100.0);
2081 if ( (float)((int)a
) == a
)
2082 fprintf (fd
, "%.0f",a
);
2084 fprintf (fd
, "%.2f",a
);
2086 if ( (float)((int)b
) == b
)
2087 fprintf (fd
, " %.0f\n",b
);
2089 fprintf (fd
, " %.2f\n",b
);
2091 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
2092 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
2095 mpsub_position
= egysub
->end
;
2096 for (i
=0; i
<egysub
->lines
; i
++) {
2097 fprintf (fd
, "%s\n",egysub
->text
[i
]);
2102 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2105 void dump_microdvd(sub_data
* subd
, float fps
) {
2108 subtitle
*subs
= subd
->subtitles
;
2111 fd
= fopen("dumpsub.sub", "w");
2113 perror("dumpsub.sub: fopen");
2116 delay
= sub_delay
* sub_fps
;
2117 for (i
= 0; i
< subd
->sub_num
; ++i
) {
2119 start
= subs
[i
].start
;
2121 if (subd
->sub_uses_time
) {
2122 start
= start
* sub_fps
/ 100 ;
2123 end
= end
* sub_fps
/ 100;
2126 start
= start
* sub_fps
/ fps
;
2127 end
= end
* sub_fps
/ fps
;
2131 fprintf(fd
, "{%d}{%d}", start
, end
);
2132 for (j
= 0; j
< subs
[i
].lines
; ++j
)
2133 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
2137 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.sub\'.\n");
2140 void dump_jacosub(sub_data
* subd
, float fps
) {
2146 subtitle
*subs
= subd
->subtitles
;
2148 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2150 fd
=fopen("dumpsub.jss","w");
2153 perror("dump_jacosub: fopen");
2156 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
2157 for(i
=0; i
< subd
->sub_num
; i
++)
2159 onesub
=subs
+i
; //=&subs[i];
2162 if (!subd
->sub_uses_time
)
2163 temp
= temp
* 100 / sub_fps
;
2164 temp
-= sub_delay
* 100;
2165 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2166 m
=temp
/6000; temp
%=6000; //m =1*100*60
2167 s
=temp
/100; temp
%=100; //s =1*100
2169 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2172 if (!subd
->sub_uses_time
)
2173 temp
= temp
* 100 / sub_fps
;
2174 temp
-= sub_delay
* 100;
2175 h
=temp
/360000;temp
%=360000;
2176 m
=temp
/6000; temp
%=6000;
2177 s
=temp
/100; temp
%=100;
2179 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2181 for(j
=0;j
<onesub
->lines
;j
++)
2182 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2187 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2190 void dump_sami(sub_data
* subd
, float fps
) {
2195 subtitle
*subs
= subd
->subtitles
;
2197 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2199 fd
=fopen("dumpsub.smi","w");
2202 perror("dump_jacosub: fopen");
2205 fprintf(fd
, "<SAMI>\n"
2207 " <STYLE TYPE=\"Text/css\">\n"
2209 " 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"
2210 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2215 for(i
=0; i
< subd
->sub_num
; i
++)
2217 onesub
=subs
+i
; //=&subs[i];
2220 if (!subd
->sub_uses_time
)
2221 temp
= temp
* 100 / sub_fps
;
2222 temp
-= sub_delay
* 100;
2223 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2224 "\t <P>", temp
* 10);
2226 for(j
=0;j
<onesub
->lines
;j
++)
2227 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2232 if (!subd
->sub_uses_time
)
2233 temp
= temp
* 100 / sub_fps
;
2234 temp
-= sub_delay
* 100;
2235 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2236 "\t <P> \n", temp
* 10);
2238 fprintf(fd
, "</BODY>\n"
2241 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2244 void sub_free( sub_data
* subd
)
2248 if ( !subd
) return;
2250 if (subd
->subtitles
) {
2251 for (i
=0; i
< subd
->subtitles
->lines
; i
++) free( subd
->subtitles
->text
[i
] );
2252 free( subd
->subtitles
);
2254 if (subd
->filename
) free( subd
->filename
);
2258 #define MAX_SUBLINE 512
2260 * \brief parse text and append it to subtitle in sub
2261 * \param sub subtitle struct to add text to
2262 * \param txt text to parse
2263 * \param len length of text in txt
2264 * \param endpts pts at which this subtitle text should be removed again
2266 * <> and {} are interpreted as comment delimiters, "\n", "\N", '\n', '\r'
2267 * and '\0' are interpreted as newlines, duplicate, leading and trailing
2268 * newlines are ignored.
2270 void sub_add_text(subtitle
*sub
, const char *txt
, int len
, double endpts
) {
2272 int double_newline
= 1; // ignore newlines at the beginning
2275 if (sub
->lines
>= SUB_MAX_TEXT
) return;
2277 buf
= malloc(MAX_SUBLINE
+ 1);
2278 sub
->text
[sub
->lines
] = buf
;
2279 sub
->endpts
[sub
->lines
] = endpts
;
2280 for (i
= 0; i
< len
&& pos
< MAX_SUBLINE
; i
++) {
2282 if (c
== '<') comment
|= 1;
2283 if (c
== '{') comment
|= 2;
2285 if (c
== '}') comment
&= ~2;
2286 if (c
== '>') comment
&= ~1;
2289 if (pos
== MAX_SUBLINE
- 1) {
2293 if (c
== '\\' && i
+ 1 < len
) {
2295 if (c
== 'n' || c
== 'N') c
= 0;
2297 if (c
== '\n' || c
== '\r') c
= 0;
2301 } else if (!double_newline
) {
2302 if (sub
->lines
>= SUB_MAX_TEXT
- 1) {
2303 mp_msg(MSGT_VO
, MSGL_WARN
, "Too many subtitle lines\n");
2310 buf
= malloc(MAX_SUBLINE
+ 1);
2311 sub
->text
[sub
->lines
] = buf
;
2312 sub
->endpts
[sub
->lines
] = endpts
;
2316 if (sub
->lines
< SUB_MAX_TEXT
&&
2317 strlen(sub
->text
[sub
->lines
]))
2321 #define MP_NOPTS_VALUE (-1LL<<63)
2323 * \brief remove outdated subtitle lines.
2324 * \param sub subtitle struct to modify
2325 * \param pts current pts. All lines with endpts <= this will be removed.
2326 * Use MP_NOPTS_VALUE to remove all lines
2327 * \return 1 if sub was modified, 0 otherwise.
2329 int sub_clear_text(subtitle
*sub
, double pts
) {
2332 while (i
< sub
->lines
) {
2333 double endpts
= sub
->endpts
[i
];
2334 if (pts
== MP_NOPTS_VALUE
|| (endpts
!= MP_NOPTS_VALUE
&& pts
>= endpts
)) {
2337 for (j
= i
+ 1; j
< sub
->lines
; j
++) {
2338 sub
->text
[j
- 1] = sub
->text
[j
];
2339 sub
->endpts
[j
- 1] = sub
->endpts
[j
];