2 * Subtitle reader with format autodetection
5 * Some code cleanup & realloc() by A'rpi/ESP-team
6 * dunnowhat sub format by szabi
15 #include <sys/types.h>
20 #include "subreader.h"
22 #define ERR ((void *) -1)
29 #include <fribidi/fribidi.h>
30 char *fribidi_charset
= NULL
;
34 extern char* dvdsub_lang
;
36 /* Maximal length of line of a subtitle */
38 static float mpsub_position
=0;
39 static float mpsub_multiplier
=1.;
40 static int sub_slacktime
= 20000; //20 sec
42 int sub_no_text_pp
=0; // 1 => do not apply text post-processing
43 // like {\...} elimination in SSA format.
45 int sub_match_fuzziness
=0; // level of sub name matching fuzziness
47 /* Use the SUB_* constant defined in the header file */
48 int sub_format
=SUB_INVALID
;
51 Some subtitling formats, namely AQT and Subrip09, define the end of a
52 subtitle as the beginning of the following. Since currently we read one
53 subtitle at time, for these format we keep two global *subtitle,
54 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
55 so we can change its end when we read current subtitle starting time.
56 When USE_SORTSUB is defined, we use a single global unsigned long,
57 previous_sub_end, for both (and even future) formats, to store the end of
58 the previous sub: it is initialized to 0 in sub_read_file and eventually
59 modified by sub_read_aqt_line or sub_read_subrip09_line.
61 unsigned long previous_sub_end
;
64 static int eol(char p
) {
65 return (p
=='\r' || p
=='\n' || p
=='\0');
68 /* Remove leading and trailing space */
69 static void trail_space(char *s
) {
71 while (isspace(s
[i
])) ++i
;
72 if (i
) strcpy(s
, s
+ i
);
74 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
77 static char *stristr(const char *haystack
, const char *needle
) {
79 const char *p
= haystack
;
81 if (!(haystack
&& needle
)) return NULL
;
85 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
92 subtitle
*sub_read_line_sami(FILE *fd
, subtitle
*current
) {
93 static char line
[LINE_LEN
+1];
94 static char *s
= NULL
, *slacktime_s
;
95 char text
[LINE_LEN
+1], *p
=NULL
, *q
;
98 current
->lines
= current
->start
= current
->end
= 0;
101 /* read the first line */
103 if (!(s
= fgets(line
, LINE_LEN
, fd
))) return 0;
108 case 0: /* find "START=" or "Slacktime:" */
109 slacktime_s
= stristr (s
, "Slacktime:");
111 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
113 s
= stristr (s
, "Start=");
115 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
117 for (; *s
!= '>' && *s
!= '\0'; s
++);
123 case 1: /* find (optionnal) "<P", skip other TAGs */
124 for (; *s
== ' ' || *s
== '\t'; s
++); /* strip blanks, if any */
125 if (*s
== '\0') break;
126 if (*s
!= '<') { state
= 3; p
= text
; continue; } /* not a TAG */
128 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
129 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
135 case 2: /* find ">" */
136 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
139 case 3: /* get all text until '<' appears */
140 if (*s
== '\0') break;
141 else if (!strncasecmp (s
, "<br>", 4)) {
142 *p
= '\0'; p
= text
; trail_space (text
);
144 current
->text
[current
->lines
++] = strdup (text
);
147 else if (*s
== '<') { state
= 4; }
148 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
149 else if (*s
== '\t') { *p
++ = ' '; s
++; }
150 else if (*s
== '\r' || *s
== '\n') { s
++; }
153 /* skip duplicated space */
154 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
158 case 4: /* get current->end or skip <TAG> */
159 q
= stristr (s
, "Start=");
161 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
162 *p
= '\0'; trail_space (text
);
164 current
->text
[current
->lines
++] = strdup (text
);
165 if (current
->lines
> 0) { state
= 99; break; }
169 if (s
) { s
++; state
= 3; continue; }
174 if (state
!= 99 && !(s
= fgets (line
, LINE_LEN
, fd
))) {
175 if (current
->start
> 0) {
176 break; // if it is the last subtitle
182 } while (state
!= 99);
184 // For the last subtitle
185 if (current
->end
<= 0) {
186 current
->end
= current
->start
+ sub_slacktime
;
187 *p
= '\0'; trail_space (text
);
189 current
->text
[current
->lines
++] = strdup (text
);
196 char *sub_readtext(char *source
, char **dest
) {
200 // printf("src=%p dest=%p \n",source,dest);
202 while ( !eol(*p
) && *p
!= '|' ) {
206 *dest
= (char *)malloc (len
+1);
207 if (!dest
) {return ERR
;}
209 strncpy(*dest
, source
, len
);
212 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
214 if (*p
) return p
; // not-last text field
215 else return NULL
; // last text field
218 subtitle
*sub_read_line_microdvd(FILE *fd
,subtitle
*current
) {
219 char line
[LINE_LEN
+1];
220 char line2
[LINE_LEN
+1];
225 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
226 } while ((sscanf (line
,
228 &(current
->start
), line2
) < 2) &&
230 "{%ld}{%ld}%[^\r\n]",
231 &(current
->start
), &(current
->end
), line2
) < 3));
236 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
237 if (current
->text
[i
]==ERR
) {return ERR
;}
239 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
246 subtitle
*sub_read_line_subrip(FILE *fd
, subtitle
*current
) {
247 char line
[LINE_LEN
+1];
248 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
249 char *p
=NULL
, *q
=NULL
;
253 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
254 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
255 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
256 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
258 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
261 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
262 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
263 current
->text
[current
->lines
-1]=(char *)malloc (len
+1);
264 if (!current
->text
[current
->lines
-1]) return ERR
;
265 strncpy (current
->text
[current
->lines
-1], q
, len
);
266 current
->text
[current
->lines
-1][len
]='\0';
267 if (!*p
|| *p
=='\r' || *p
=='\n') break;
269 else while (*p
++!=']');
276 subtitle
*sub_read_line_subviewer(FILE *fd
,subtitle
*current
) {
277 char line
[LINE_LEN
+1];
278 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
282 while (!current
->text
[0]) {
283 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
284 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)
286 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
287 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
288 for (i
=0; i
<SUB_MAX_TEXT
;) {
289 if (!fgets (line
, LINE_LEN
, fd
)) break;
291 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++);
294 char *curptr
=current
->text
[i
]=(char *)malloc (len
+1);
295 if (!current
->text
[i
]) return ERR
;
296 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
298 /* let's filter html tags ::atmos */
325 subtitle
*sub_read_line_subviewer2(FILE *fd
,subtitle
*current
) {
326 char line
[LINE_LEN
+1];
331 while (!current
->text
[0]) {
332 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
335 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
337 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
338 for (i
=0; i
<SUB_MAX_TEXT
;) {
339 if (!fgets (line
, LINE_LEN
, fd
)) break;
340 if (line
[0]=='}') break;
342 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
344 current
->text
[i
]=(char *)malloc (len
+1);
345 if (!current
->text
[i
]) return ERR
;
346 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
358 subtitle
*sub_read_line_vplayer(FILE *fd
,subtitle
*current
) {
359 char line
[LINE_LEN
+1];
361 char *p
=NULL
, *next
,separator
;
364 while (!current
->text
[0]) {
365 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
366 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
369 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
373 // finds the body of the subtitle
380 printf("SUB: Skipping incorrect subtitle line!\n");
384 // by wodzu: hey! this time we know what length it has! what is
385 // that magic for? it can't deal with space instead of third
386 // colon! look, what simple it can be:
393 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
394 if (current
->text
[i
]==ERR
) {return ERR
;}
396 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
404 subtitle
*sub_read_line_rt(FILE *fd
,subtitle
*current
) {
405 //TODO: This format uses quite rich (sub/super)set of xhtml
406 // I couldn't check it since DTD is not included.
407 // WARNING: full XML parses can be required for proper parsing
408 char line
[LINE_LEN
+1];
409 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
410 char *p
=NULL
,*next
=NULL
;
413 while (!current
->text
[0]) {
414 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
415 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
416 //to describe the same moment in time. Maybe there are even more formats in use.
417 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
418 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
420 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
421 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
422 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
423 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
424 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
425 ((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) &&
426 ((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) &&
427 //now try it without end time
428 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
429 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
430 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
431 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
434 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
435 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
436 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
437 current
->end
= current
->start
+200;
439 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
440 next
= strstr(line
,"<clear/>");
441 if(next
&& strlen(next
)>8){
443 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
444 if (current
->text
[i
]==ERR
) {return ERR
;}
446 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
454 subtitle
*sub_read_line_ssa(FILE *fd
,subtitle
*current
) {
456 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
457 * other Sub Station Alpha scripts have only 8 commas before subtitle
458 * Reading the "ScriptType:" field is not reliable since many scripts appear
461 * http://www.scriptclub.org is a good place to find more examples
462 * http://www.eswat.demon.co.uk is where the SSA specs can be found
465 static int max_comma
= 32; /* let's use 32 for the case that the */
466 /* amount of commas increase with newer SSA versions */
468 int hour1
, min1
, sec1
, hunsec1
,
469 hour2
, min2
, sec2
, hunsec2
, nothing
;
472 char line
[LINE_LEN
+1],
478 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
479 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
480 "%[^\n\r]", ¬hing
,
481 &hour1
, &min1
, &sec1
, &hunsec1
,
482 &hour2
, &min2
, &sec2
, &hunsec2
,
485 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
486 "%[^\n\r]", ¬hing
,
487 &hour1
, &min1
, &sec1
, &hunsec1
,
488 &hour2
, &min2
, &sec2
, &hunsec2
,
491 line2
=strchr(line3
, ',');
493 for (comma
= 4; comma
< max_comma
; comma
++)
496 if(!(tmp
=strchr(++tmp
, ','))) break;
497 if(*(++tmp
) == ' ') break;
498 /* a space after a comma means we're already in a sentence */
502 if(comma
< max_comma
)max_comma
= comma
;
503 /* eliminate the trailing comma */
504 if(*line2
== ',') line2
++;
506 current
->lines
=0;num
=0;
507 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
508 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
510 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
511 current
->text
[num
]=(char *)malloc(tmp
-line2
+1);
512 strncpy (current
->text
[num
], line2
, tmp
-line2
);
513 current
->text
[num
][tmp
-line2
]='\0';
517 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
520 current
->text
[num
]=strdup(line2
);
526 void sub_pp_ssa(subtitle
*sub
) {
531 /* eliminate any text enclosed with {}, they are font and color settings */
532 so
=de
=sub
->text
[--l
];
534 if(*so
== '{' && so
[1]=='\\') {
535 for (start
=so
; *so
&& *so
!='}'; so
++);
536 if(*so
) so
++; else so
=start
;
547 subtitle
*sub_read_line_dunnowhat(FILE *fd
,subtitle
*current
) {
548 char line
[LINE_LEN
+1];
549 char text
[LINE_LEN
+1];
551 if (!fgets (line
, LINE_LEN
, fd
))
553 if (sscanf (line
, "%ld,%ld,\"%[^\"]", &(current
->start
),
554 &(current
->end
), text
) <3)
556 current
->text
[0] = strdup(text
);
562 subtitle
*sub_read_line_mpsub(FILE *fd
, subtitle
*current
) {
563 char line
[LINE_LEN
+1];
570 if (!fgets(line
, LINE_LEN
, fd
)) return NULL
;
571 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
573 mpsub_position
+= a
*mpsub_multiplier
;
574 current
->start
=(int) mpsub_position
;
575 mpsub_position
+= b
*mpsub_multiplier
;
576 current
->end
=(int) mpsub_position
;
578 while (num
< SUB_MAX_TEXT
) {
579 if (!fgets (line
, LINE_LEN
, fd
)) {
580 if (num
== 0) return NULL
;
584 while (isspace(*p
)) p
++;
585 if (eol(*p
) && num
> 0) return current
;
586 if (eol(*p
)) return NULL
;
588 for (q
=p
; !eol(*q
); q
++);
591 current
->text
[num
]=strdup(p
);
592 // printf (">%s<\n",p);
593 current
->lines
= ++num
;
595 if (num
) return current
;
599 return NULL
; // we should have returned before if it's OK
603 //we don't need this if we use previous_sub_end
604 subtitle
*previous_aqt_sub
= NULL
;
607 subtitle
*sub_read_line_aqt(FILE *fd
,subtitle
*current
) {
608 char line
[LINE_LEN
+1];
613 // try to locate next subtitle
614 if (!fgets (line
, LINE_LEN
, fd
))
616 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
621 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
623 if (previous_aqt_sub
!= NULL
)
624 previous_aqt_sub
->end
= current
->start
-1;
626 previous_aqt_sub
= current
;
629 if (!fgets (line
, LINE_LEN
, fd
))
632 sub_readtext((char *) &line
,¤t
->text
[0]);
634 current
->end
= current
->start
; // will be corrected by next subtitle
636 if (!fgets (line
, LINE_LEN
, fd
))
640 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
641 if (current
->text
[i
]==ERR
) {return ERR
;}
643 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
647 if ((current
->text
[0]=="") && (current
->text
[1]=="")) {
649 previous_sub_end
= 0;
651 // void subtitle -> end of previous marked and exit
652 previous_aqt_sub
= NULL
;
661 subtitle
*previous_subrip09_sub
= NULL
;
664 subtitle
*sub_read_line_subrip09(FILE *fd
,subtitle
*current
) {
665 char line
[LINE_LEN
+1];
671 // try to locate next subtitle
672 if (!fgets (line
, LINE_LEN
, fd
))
674 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
678 current
->start
= a1
*360000+a2
*6000+a3
*100;
681 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
683 if (previous_subrip09_sub
!= NULL
)
684 previous_subrip09_sub
->end
= current
->start
-1;
686 previous_subrip09_sub
= current
;
689 if (!fgets (line
, LINE_LEN
, fd
))
694 current
->text
[0]=""; // just to be sure that string is clear
696 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
697 if (current
->text
[i
]==ERR
) {return ERR
;}
699 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
703 if ((current
->text
[0]=="") && (i
==0)) {
705 previous_sub_end
= 0;
707 // void subtitle -> end of previous marked and exit
708 previous_subrip09_sub
= NULL
;
716 subtitle
*sub_read_line_jacosub(FILE * fd
, subtitle
* current
)
718 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
719 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
720 static unsigned jacoTimeres
= 30;
721 static int jacoShift
= 0;
723 bzero(current
, sizeof(subtitle
));
724 bzero(line1
, LINE_LEN
);
725 bzero(line2
, LINE_LEN
);
726 bzero(directive
, LINE_LEN
);
727 while (!current
->text
[0]) {
728 if (!fgets(line1
, LINE_LEN
, fd
)) {
732 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
733 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
734 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
735 if (line1
[0] == '#') {
736 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
738 unsigned units
= jacoShift
;
739 switch (toupper(line1
[1])) {
741 if (isalpha(line1
[2])) {
746 if (sscanf(&line1
[delta
], "%d", &hours
)) {
751 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
753 (&line1
[delta
], "%*d:%*d:%d",
755 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
759 sscanf(&line1
[delta
], "%d:%d.%d",
760 &minutes
, &seconds
, &units
);
765 sscanf(&line1
[delta
], "%d.%d", &seconds
,
770 ((hours
* 3600 + minutes
* 60 +
771 seconds
) * jacoTimeres
+
776 if (isalpha(line1
[2])) {
781 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
788 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
791 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
797 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
798 jacoShift
) * 100.0 / jacoTimeres
);
801 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
802 jacoShift
) * 100.0 / jacoTimeres
);
806 while ((*p
== ' ') || (*p
== '\t')) {
809 if (isalpha(*p
)||*p
== '[') {
812 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
813 return (subtitle
*) ERR
;
814 jLength
= strlen(directive
);
815 for (cont
= 0; cont
< jLength
; ++cont
) {
816 if (isalpha(*(directive
+ cont
)))
817 *(directive
+ cont
) = toupper(*(directive
+ cont
));
819 if ((strstr(directive
, "RDB") != NULL
)
820 || (strstr(directive
, "RDC") != NULL
)
821 || (strstr(directive
, "RLB") != NULL
)
822 || (strstr(directive
, "RLG") != NULL
)) {
825 if (strstr(directive
, "JL") != NULL
) {
826 current
->alignment
= SUB_ALIGNMENT_HLEFT
;
827 } else if (strstr(directive
, "JR") != NULL
) {
828 current
->alignment
= SUB_ALIGNMENT_HRIGHT
;
830 current
->alignment
= SUB_ALIGNMENT_HCENTER
;
832 strcpy(line2
, line1
);
835 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
843 //the next line to get rid of a blank after the comment
844 if ((*(p
+ 1)) == ' ')
856 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
864 if (*(p
+ 1) == 'n') {
867 current
->text
[current
->lines
++] = strdup(line1
);
871 if ((toupper(*(p
+ 1)) == 'C')
872 || (toupper(*(p
+ 1)) == 'F')) {
876 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
877 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
878 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
882 if ((*(p
+ 1) == '\\') ||
883 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
885 } else if (eol(*(p
+ 1))) {
886 if (!fgets(directive
, LINE_LEN
, fd
))
888 trail_space(directive
);
889 strncat(line2
, directive
,
890 (LINE_LEN
> 511) ? LINE_LEN
: 511);
901 current
->text
[current
->lines
] = strdup(line1
);
907 int sub_autodetect (FILE *fd
, int *uses_time
) {
908 char line
[LINE_LEN
+1];
914 if (!fgets (line
, LINE_LEN
, fd
))
917 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
918 {*uses_time
=0;return SUB_MICRODVD
;}
919 if (sscanf (line
, "{%d}{}", &i
)==1)
920 {*uses_time
=0;return SUB_MICRODVD
;}
921 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
922 {*uses_time
=1;return SUB_SUBRIP
;}
923 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
924 {*uses_time
=1;return SUB_SUBVIEWER
;}
925 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
926 {*uses_time
=1;return SUB_SUBVIEWER2
;}
927 if (strstr (line
, "<SAMI>"))
928 {*uses_time
=1; return SUB_SAMI
;}
929 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
930 {*uses_time
= 1; return SUB_JACOSUB
;}
931 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
932 {*uses_time
= 1; return SUB_JACOSUB
;}
933 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
934 {*uses_time
=1;return SUB_VPLAYER
;}
935 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
936 {*uses_time
=1;return SUB_VPLAYER
;}
937 //TODO: just checking if first line of sub starts with "<" is WAY
938 // too weak test for RT
939 // Please someone who knows the format of RT... FIX IT!!!
940 // It may conflict with other sub formats in the future (actually it doesn't)
942 {*uses_time
=1;return SUB_RT
;}
944 if (!memcmp(line
, "Dialogue: Marked", 16))
945 {*uses_time
=1; return SUB_SSA
;}
946 if (!memcmp(line
, "Dialogue: ", 10))
947 {*uses_time
=1; return SUB_SSA
;}
948 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
949 {*uses_time
=0;return SUB_DUNNOWHAT
;}
950 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
951 {*uses_time
=0; return SUB_MPSUB
;}
952 if (sscanf (line
, "FORMAT=TIM%c", &p
)==1 && p
=='E')
953 {*uses_time
=1; return SUB_MPSUB
;}
954 if (strstr (line
, "-->>"))
955 {*uses_time
=0; return SUB_AQTITLE
;}
956 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
957 {*uses_time
=1;return SUB_SUBRIP09
;}
960 return SUB_INVALID
; // too many bad lines
970 extern float sub_delay
;
971 extern float sub_fps
;
974 static iconv_t icdsc
= (iconv_t
)(-1);
976 void subcp_open (void)
978 char *tocp
= "UTF-8";
981 if ((icdsc
= iconv_open (tocp
, sub_cp
)) != (iconv_t
)(-1)){
982 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
985 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
989 void subcp_close (void)
991 if (icdsc
!= (iconv_t
)(-1)){
992 (void) iconv_close (icdsc
);
993 icdsc
= (iconv_t
)(-1);
994 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
998 #define ICBUFFSIZE 512
999 static char icbuffer
[ICBUFFSIZE
];
1001 subtitle
* subcp_recode (subtitle
*sub
)
1004 size_t ileft
, oleft
;
1009 ip
= sub
->text
[--l
];
1011 oleft
= ICBUFFSIZE
- 1;
1013 if (iconv(icdsc
, &ip
, &ileft
,
1014 &op
, &oleft
) == (size_t)(-1)) {
1015 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line (1).\n");
1019 if (!(ot
= (char *)malloc(op
- icbuffer
+ 1))){
1020 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1025 strcpy (ot
, icbuffer
);
1026 free (sub
->text
[l
]);
1030 for (l
= sub
->lines
; l
;)
1031 free (sub
->text
[--l
]);
1038 subtitle
* subcp_recode1 (subtitle
*sub
)
1041 size_t ileft
, oleft
;
1043 if(icdsc
== (iconv_t
)(-1)) return sub
;
1046 char *ip
= icbuffer
;
1047 char *op
= sub
->text
[--l
];
1050 oleft
= ICBUFFSIZE
- 1;
1052 if (iconv(icdsc
, &ip
, &ileft
,
1053 &op
, &oleft
) == (size_t)(-1)) {
1054 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: error recoding line (2).\n");
1065 #define max(a,b) (((a)>(b))?(a):(b))
1067 subtitle
* sub_fribidi (subtitle
*sub
, int sub_utf8
)
1069 FriBidiChar logical
[LINE_LEN
+1], visual
[LINE_LEN
+1]; // Hopefully these two won't smash the stack
1070 char *ip
= NULL
, *op
= NULL
;
1071 FriBidiCharType base
;
1072 size_t len
,orig_len
;
1075 fribidi_boolean log2vis
;
1076 if(flip_hebrew
) { // Please fix the indentation someday
1077 fribidi_set_mirroring (FRIBIDI_TRUE
);
1078 fribidi_set_reorder_nsm (FRIBIDI_FALSE
);
1080 if( sub_utf8
== 0 ) {
1081 char_set_num
= fribidi_parse_charset (fribidi_charset
?fribidi_charset
:"ISO8859-8");
1083 char_set_num
= fribidi_parse_charset ("UTF-8");
1086 ip
= sub
->text
[--l
];
1087 orig_len
= len
= strlen( ip
); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1088 if(len
> LINE_LEN
) {
1089 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: sub->text is longer than LINE_LEN.\n");
1093 len
= fribidi_charset_to_unicode (char_set_num
, ip
, len
, logical
);
1094 base
= FRIBIDI_TYPE_ON
;
1095 log2vis
= fribidi_log2vis (logical
, len
, &base
,
1097 visual
, NULL
, NULL
, NULL
);
1099 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
,
1101 if((op
= (char*)malloc(sizeof(char)*(max(2*orig_len
,2*len
) + 1))) == NULL
) {
1102 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1106 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1112 for (l
= sub
->lines
; l
;)
1113 free (sub
->text
[--l
]);
1122 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1123 int sub_num
, int sub_uses_time
) {
1127 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1128 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1132 if (sub
->end
<= sub
->start
){
1133 sub
->end
= sub
->start
+ subfms
;
1140 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1141 // these subtitles overlap for less than 0.2 seconds
1142 // and would result in very short overlapping subtitle
1143 // so let's fix the problem here, before overlapping code
1144 // get its hands on them
1145 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1146 sub
->end
-= half
+ 1;
1147 nextsub
->start
+= delta
- half
;
1149 if (sub
->end
>= nextsub
->start
){
1150 sub
->end
= nextsub
->start
- 1;
1151 if (sub
->end
- sub
->start
> subfms
)
1152 sub
->end
= sub
->start
+ subfms
;
1159 * Movies are often converted from FILM (24 fps)
1160 * to PAL (25) by simply speeding it up, so we
1161 * to multiply the original timestmaps by
1162 * (Movie's FPS / Subtitle's (guessed) FPS)
1163 * so eg. for 23.98 fps movie and PAL time based
1164 * subtitles we say -subfps 25 and we're fine!
1167 /* timed sub fps correction ::atmos */
1168 if(sub_uses_time
&& sub_fps
) {
1169 sub
->start
*= sub_fps
/fps
;
1170 sub
->end
*= sub_fps
/fps
;
1176 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Adjusted %d subtitle(s).\n", n
);
1180 subtitle
* (*read
)(FILE *fd
,subtitle
*dest
);
1181 void (*post
)(subtitle
*dest
);
1185 sub_data
* sub_read_file (char *filename
, float fps
) {
1186 //filename is assumed to be malloc'ed, free() is used in sub_free()
1188 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1189 subtitle
*first
, *second
, *sub
, *return_sub
;
1190 sub_data
*subt_data
;
1191 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1192 struct subreader sr
[]=
1194 { sub_read_line_microdvd
, NULL
, "microdvd" },
1195 { sub_read_line_subrip
, NULL
, "subrip" },
1196 { sub_read_line_subviewer
, NULL
, "subviewer" },
1197 { sub_read_line_sami
, NULL
, "sami" },
1198 { sub_read_line_vplayer
, NULL
, "vplayer" },
1199 { sub_read_line_rt
, NULL
, "rt" },
1200 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1201 { sub_read_line_dunnowhat
, NULL
, "dunnowhat" },
1202 { sub_read_line_mpsub
, NULL
, "mpsub" },
1203 { sub_read_line_aqt
, NULL
, "aqt" },
1204 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1205 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1206 { sub_read_line_jacosub
, NULL
, "jacosub" }
1208 struct subreader
*srp
;
1210 if(filename
==NULL
) return NULL
; //qnx segfault
1211 fd
=fopen (filename
, "r"); if (!fd
) return NULL
;
1213 sub_format
=sub_autodetect (fd
, &uses_time
);
1214 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1215 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1217 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Detected subtitle file format: %s\n", srp
->name
);
1222 sub_utf8_prev
=sub_utf8
;
1226 if ((l
=strlen(filename
))>4){
1227 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1229 if (!strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1234 if (k
<0) subcp_open();
1239 first
=(subtitle
*)malloc(n_max
*sizeof(subtitle
));
1243 sub_utf8
=sub_utf8_prev
;
1249 sub
= (subtitle
*)malloc(sizeof(subtitle
));
1250 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1251 //as the beginning of the following
1252 previous_sub_end
= 0;
1257 first
=realloc(first
,n_max
*sizeof(subtitle
));
1260 sub
= &first
[sub_num
];
1262 memset(sub
, '\0', sizeof(subtitle
));
1263 sub
=srp
->read(fd
,sub
);
1264 if(!sub
) break; // EOF
1266 if ((sub
!=ERR
) && (sub_utf8
& 2)) sub
=subcp_recode(sub
);
1269 if (sub
!=ERR
) sub
=sub_fribidi(sub
,sub_utf8
);
1276 if ( first
) free(first
);
1279 // Apply any post processing that needs recoding first
1280 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1282 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1283 first
[sub_num
].start
= sub
->start
;
1284 first
[sub_num
].end
= sub
->end
;
1285 first
[sub_num
].lines
= sub
->lines
;
1286 first
[sub_num
].alignment
= sub
->alignment
;
1287 for(i
= 0; i
< sub
->lines
; ++i
){
1288 first
[sub_num
].text
[i
] = sub
->text
[i
];
1290 if (previous_sub_end
){
1291 first
[sub_num
- 1].end
= previous_sub_end
;
1292 previous_sub_end
= 0;
1295 for(j
= sub_num
- 1; j
>= 0; --j
){
1296 first
[j
+ 1].start
= first
[j
].start
;
1297 first
[j
+ 1].end
= first
[j
].end
;
1298 first
[j
+ 1].lines
= first
[j
].lines
;
1299 first
[j
+ 1].alignment
= first
[j
].alignment
;
1300 for(i
= 0; i
< first
[j
].lines
; ++i
){
1301 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1303 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1304 first
[j
].start
= sub
->start
;
1305 first
[j
].end
= sub
->end
;
1306 first
[j
].lines
= sub
->lines
;
1307 first
[j
].alignment
= sub
->alignment
;
1308 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1309 first
[j
].text
[i
] = sub
->text
[i
];
1311 if (previous_sub_end
){
1312 first
[j
].end
= first
[j
- 1].end
;
1313 first
[j
- 1].end
= previous_sub_end
;
1314 previous_sub_end
= 0;
1321 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1330 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1331 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Read %i subtitles", sub_num
);
1332 if (sub_errs
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,", %i bad line(s).\n", sub_errs
);
1333 else mp_msg(MSGT_SUBREADER
,MSGL_INFO
,".\n");
1340 // we do overlap if the user forced it (suboverlap_enable == 2) or
1341 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1342 // this is because usually overlapping subtitles are found in these formats,
1343 // while in others they are probably result of bad timing
1344 if ((suboverlap_enabled
== 2) ||
1345 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1346 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1347 // here we manage overlapping subtitles
1352 // for each subtitle in first[] we deal with its 'block' of
1354 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1355 unsigned long global_start
= first
[sub_first
].start
,
1356 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1357 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1358 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1359 char real_block
= 1;
1361 // here we find the number of subtitles inside the 'block'
1362 // and its span interval. this works well only with sorted
1364 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1366 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1367 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1368 global_start
= first
[sub_first
+ sub_to_add
].start
;
1370 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1371 global_end
= first
[sub_first
+ sub_to_add
].end
;
1375 // we need a structure to keep trace of the screen lines
1376 // used by the subs, a 'placeholder'
1377 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1378 // from a block of sub_to_add+1 subs
1379 placeholder
= (int **) malloc(sizeof(int *) * counter
);
1380 for (i
= 0; i
< counter
; ++i
) {
1381 placeholder
[i
] = (int *) malloc(sizeof(int) * lines_to_add
);
1382 for (j
= 0; j
< lines_to_add
; ++j
) {
1383 placeholder
[i
][j
] = -1;
1388 local_end
= global_start
- 1;
1392 // here we find the beginning and the end of a new
1393 // subtitle in the block
1394 local_start
= local_end
+ 1;
1395 local_end
= global_end
;
1396 for (j
= 0; j
<= sub_to_add
; ++j
) {
1397 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1398 local_end
= first
[sub_first
+ j
].start
- 1;
1399 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1400 local_end
= first
[sub_first
+ j
].end
;
1403 // here we allocate the screen lines to subs we must
1404 // display in current local_start-local_end interval.
1405 // if the subs were yet presents in the previous interval
1406 // they keep the same lines, otherside they get unused lines
1407 for (j
= 0; j
<= sub_to_add
; ++j
) {
1408 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1409 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1412 int fragment_position
= -1;
1414 // if this is not the first new sub of the block
1415 // we find if this sub was present in the previous
1418 for (i
= 0; i
< lines_to_add
; ++i
) {
1419 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1420 placeholder
[counter
][i
] = sub_first
+ j
;
1427 // we are looking for the shortest among all groups of
1428 // sequential blank lines whose length is greater than or
1429 // equal to sub_lines. we store in fragment_position the
1430 // position of the shortest group, in fragment_length its
1431 // length, and in tmp the length of the group currently
1433 for (i
= 0; i
< lines_to_add
; ++i
) {
1434 if (placeholder
[counter
][i
] == -1) {
1435 // placeholder[counter][i] is part of the current group
1439 if (tmp
== sub_lines
) {
1440 // current group's size fits exactly the one we
1441 // need, so we stop looking
1442 fragment_position
= i
- tmp
;
1446 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1447 // current group is the best we found till here,
1448 // but is still bigger than the one we are looking
1449 // for, so we keep on looking
1450 fragment_length
= tmp
;
1451 fragment_position
= i
- tmp
;
1454 // current group doesn't fit at all, so we forget it
1460 // last screen line is blank, a group ends with it
1461 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1462 fragment_position
= i
- tmp
;
1465 if (fragment_position
== -1) {
1466 // it was not possible to find free screen line(s) for a subtitle,
1467 // usually this means a bug in the code; however we do not overlap
1468 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1469 higher_line
= SUB_MAX_TEXT
+ 1;
1472 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1473 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1478 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1479 if (placeholder
[counter
][j
] != -1)
1484 if (higher_line
>= SUB_MAX_TEXT
) {
1485 // the 'block' has too much lines, so we don't overlap the
1487 second
= (subtitle
*) realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1488 for (j
= 0; j
<= sub_to_add
; ++j
) {
1490 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1491 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1492 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1493 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1494 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1495 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1496 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1499 sub_num
+= sub_to_add
+ 1;
1500 sub_first
+= sub_to_add
;
1505 // we read the placeholder structure and create the new
1507 second
= (subtitle
*) realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1508 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1509 second
[sub_num
].start
= local_start
;
1510 second
[sub_num
].end
= local_end
;
1511 second
[sub_num
].alignment
= SUB_ALIGNMENT_HCENTER
;
1512 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1513 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1514 if (placeholder
[counter
][j
] != -1) {
1515 int lines
= first
[placeholder
[counter
][j
]].lines
;
1516 for (ls
= 0; ls
< lines
; ++ls
) {
1517 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1521 second
[sub_num
].text
[i
++] = strdup(" ");
1526 } while (local_end
< global_end
);
1528 for (i
= 0; i
< counter
; ++i
)
1529 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1531 counter
= 2 * sub_to_add
+ 1;
1532 for (i
= 0; i
< counter
; ++i
) {
1533 free(placeholder
[i
]);
1536 sub_first
+= sub_to_add
;
1539 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1540 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1541 free(first
[j
].text
[i
]);
1546 return_sub
= second
;
1547 } else { //if(suboverlap_enabled)
1548 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1551 if (return_sub
== NULL
) return NULL
;
1552 subt_data
= (sub_data
*)malloc(sizeof(sub_data
));
1553 subt_data
->filename
= filename
;
1554 subt_data
->sub_uses_time
= uses_time
;
1555 subt_data
->sub_num
= sub_num
;
1556 subt_data
->sub_errs
= sub_errs
;
1557 subt_data
->subtitles
= return_sub
;
1562 char * strreplace( char * in
,char * what
,char * whereof
)
1567 if ( ( in
== NULL
)||( what
== NULL
)||( whereof
== NULL
)||( ( tmp
=strstr( in
,what
) ) == NULL
) ) return NULL
;
1568 for( i
=0;i
<strlen( whereof
);i
++ ) tmp
[i
]=whereof
[i
];
1569 if ( strlen( what
) > strlen( whereof
) ) tmp
[i
]=0;
1575 static void strcpy_trim(char *d
, char *s
)
1577 // skip leading whitespace
1578 while (*s
&& !isalnum(*s
)) {
1583 while (*s
&& isalnum(*s
)) {
1588 // trim excess whitespace
1589 while (*s
&& !isalnum(*s
)) {
1598 static void strcpy_strip_ext(char *d
, char *s
)
1600 char *tmp
= strrchr(s
,'.');
1605 strncpy(d
, s
, tmp
-s
);
1614 static void strcpy_get_ext(char *d
, char *s
)
1616 char *tmp
= strrchr(s
,'.');
1625 static int whiteonly(char *s
)
1628 if (isalnum(*s
)) return 0;
1634 typedef struct _subfn
1640 static int compare_sub_priority(const void *a
, const void *b
)
1642 if (((subfn
*)a
)->priority
> ((subfn
*)b
)->priority
) {
1644 } else if (((subfn
*)a
)->priority
< ((subfn
*)b
)->priority
) {
1647 return strcoll(((subfn
*)a
)->fname
, ((subfn
*)b
)->fname
);
1651 char** sub_filenames(char* path
, char *fname
)
1653 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
1654 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
1656 int len
, pos
, found
, i
, j
;
1657 char * sub_exts
[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
1668 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
1669 +(strlen(path
) > 256 ? strlen(path
) : 256)+2;
1671 f_dir
= (char*)malloc(len
);
1672 f_fname
= (char*)malloc(len
);
1673 f_fname_noext
= (char*)malloc(len
);
1674 f_fname_trim
= (char*)malloc(len
);
1676 tmp_fname_noext
= (char*)malloc(len
);
1677 tmp_fname_trim
= (char*)malloc(len
);
1678 tmp_fname_ext
= (char*)malloc(len
);
1680 tmpresult
= (char*)malloc(len
);
1682 result
= (subfn
*)malloc(sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1683 memset(result
, 0, sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1687 tmp
= strrchr(fname
,'/');
1689 if(!tmp
)tmp
= strrchr(fname
,'\\');
1692 // extract filename & dirname from fname
1694 strcpy(f_fname
, tmp
+1);
1696 strncpy(f_dir
, fname
, pos
+1);
1699 strcpy(f_fname
, fname
);
1700 strcpy(f_dir
, "./");
1703 strcpy_strip_ext(f_fname_noext
, f_fname
);
1704 strcpy_trim(f_fname_trim
, f_fname_noext
);
1707 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
1708 tmp_sub_id
= (char*)malloc(strlen(dvdsub_lang
)+1);
1709 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
1713 // 1 = any subtitle file
1714 // 2 = any sub file containing movie name
1715 // 3 = sub file containing movie name and the lang extension
1716 for (j
= 0; j
<= 1; j
++) {
1717 d
= opendir(j
== 0 ? f_dir
: path
);
1719 while ((de
= readdir(d
))) {
1720 // retrieve various parts of the filename
1721 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
1722 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
1723 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
1725 // does it end with a subtitle extension?
1728 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
1730 for (i
= 0; sub_exts
[i
]; i
++) {
1732 if (strcmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
1738 // we have a (likely) subtitle file
1741 if (!prio
&& tmp_sub_id
)
1743 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
1744 printf("dvdsublang...%s\n", tmpresult
);
1745 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
1746 // matches the movie name + lang extension
1750 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
1751 // matches the movie name
1754 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && (sub_match_fuzziness
>= 1)) {
1755 // contains the movie name
1756 tmp
+= strlen(f_fname_trim
);
1757 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
1758 // with sub_id specified prefer localized subtitles
1760 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
1761 // without sub_id prefer "plain" name
1764 // with no localized subs found, try any else instead
1769 // doesn't contain the movie name
1770 // don't try in the mplayer subtitle directory
1771 if ((j
== 0) && (sub_match_fuzziness
>= 2)) {
1779 if (i
<3){ // prefer UTF-8 coded
1783 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
1784 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1785 if ((f
= fopen(tmpresult
, "rt"))) {
1787 result
[subcnt
].priority
= prio
;
1788 result
[subcnt
].fname
= strdup(tmpresult
);
1794 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
1801 if (tmp_sub_id
) free(tmp_sub_id
);
1805 free(f_fname_noext
);
1808 free(tmp_fname_noext
);
1809 free(tmp_fname_trim
);
1810 free(tmp_fname_ext
);
1814 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
1816 result2
= (char**)malloc(sizeof(char*)*(subcnt
+1));
1817 memset(result2
, 0, sizeof(char*)*(subcnt
+1));
1819 for (i
= 0; i
< subcnt
; i
++) {
1820 result2
[i
] = result
[i
].fname
;
1822 result2
[subcnt
] = NULL
;
1829 void list_sub_file(sub_data
* subd
){
1831 subtitle
*subs
= subd
->subtitles
;
1833 for(j
=0; j
< subd
->sub_num
; j
++){
1834 subtitle
* egysub
=&subs
[j
];
1835 printf ("%i line%c (%li-%li)\n",
1837 (1==egysub
->lines
)?' ':'s',
1840 for (i
=0; i
<egysub
->lines
; i
++) {
1841 printf ("\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
1846 printf ("Subtitle format %s time.\n",
1847 subd
->sub_uses_time
? "uses":"doesn't use");
1848 printf ("Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
1851 void dump_srt(sub_data
* subd
, float fps
){
1857 subtitle
*subs
= subd
->subtitles
;
1859 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1861 fd
=fopen("dumpsub.srt","w");
1864 perror("dump_srt: fopen");
1867 for(i
=0; i
< subd
->sub_num
; i
++)
1869 onesub
=subs
+i
; //=&subs[i];
1870 fprintf(fd
,"%d\n",i
+1);//line number
1873 if (!subd
->sub_uses_time
)
1874 temp
= temp
* 100 / sub_fps
;
1875 temp
-= sub_delay
* 100;
1876 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
1877 m
=temp
/6000; temp
%=6000; //m =1*100*60
1878 s
=temp
/100; temp
%=100; //s =1*100
1879 ms
=temp
*10; //ms=1*10
1880 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
1883 if (!subd
->sub_uses_time
)
1884 temp
= temp
* 100 / sub_fps
;
1885 temp
-= sub_delay
* 100;
1886 h
=temp
/360000;temp
%=360000;
1887 m
=temp
/6000; temp
%=6000;
1888 s
=temp
/100; temp
%=100;
1890 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
1892 for(j
=0;j
<onesub
->lines
;j
++)
1893 fprintf(fd
,"%s\n",onesub
->text
[j
]);
1898 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
1901 void dump_mpsub(sub_data
* subd
, float fps
){
1905 subtitle
*subs
= subd
->subtitles
;
1907 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
1908 if (sub_fps
==0) sub_fps
=fps
;
1910 fd
=fopen ("dump.mpsub", "w");
1912 perror ("dump_mpsub: fopen");
1917 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
1918 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
1920 for(j
=0; j
< subd
->sub_num
; j
++){
1921 subtitle
* egysub
=&subs
[j
];
1922 if (subd
->sub_uses_time
) {
1923 a
=((egysub
->start
-mpsub_position
)/100.0);
1924 b
=((egysub
->end
-egysub
->start
)/100.0);
1925 if ( (float)((int)a
) == a
)
1926 fprintf (fd
, "%.0f",a
);
1928 fprintf (fd
, "%.2f",a
);
1930 if ( (float)((int)b
) == b
)
1931 fprintf (fd
, " %.0f\n",b
);
1933 fprintf (fd
, " %.2f\n",b
);
1935 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
1936 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
1939 mpsub_position
= egysub
->end
;
1940 for (i
=0; i
<egysub
->lines
; i
++) {
1941 fprintf (fd
, "%s\n",egysub
->text
[i
]);
1946 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
1949 void dump_microdvd(sub_data
* subd
, float fps
) {
1952 subtitle
*subs
= subd
->subtitles
;
1955 fd
= fopen("dumpsub.txt", "w");
1957 perror("dumpsub.txt: fopen");
1960 delay
= sub_delay
* sub_fps
;
1961 for (i
= 0; i
< subd
->sub_num
; ++i
) {
1963 start
= subs
[i
].start
;
1965 if (subd
->sub_uses_time
) {
1966 start
= start
* sub_fps
/ 100 ;
1967 end
= end
* sub_fps
/ 100;
1970 start
= start
* sub_fps
/ fps
;
1971 end
= end
* sub_fps
/ fps
;
1975 fprintf(fd
, "{%d}{%d}", start
, end
);
1976 for (j
= 0; j
< subs
[i
].lines
; ++j
)
1977 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
1981 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
1984 void dump_jacosub(sub_data
* subd
, float fps
) {
1990 subtitle
*subs
= subd
->subtitles
;
1992 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1994 fd
=fopen("dumpsub.jss","w");
1997 perror("dump_jacosub: fopen");
2000 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
2001 for(i
=0; i
< subd
->sub_num
; i
++)
2003 onesub
=subs
+i
; //=&subs[i];
2006 if (!subd
->sub_uses_time
)
2007 temp
= temp
* 100 / sub_fps
;
2008 temp
-= sub_delay
* 100;
2009 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2010 m
=temp
/6000; temp
%=6000; //m =1*100*60
2011 s
=temp
/100; temp
%=100; //s =1*100
2013 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2016 if (!subd
->sub_uses_time
)
2017 temp
= temp
* 100 / sub_fps
;
2018 temp
-= sub_delay
* 100;
2019 h
=temp
/360000;temp
%=360000;
2020 m
=temp
/6000; temp
%=6000;
2021 s
=temp
/100; temp
%=100;
2023 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2025 for(j
=0;j
<onesub
->lines
;j
++)
2026 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2031 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2034 void dump_sami(sub_data
* subd
, float fps
) {
2039 subtitle
*subs
= subd
->subtitles
;
2041 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2043 fd
=fopen("dumpsub.smi","w");
2046 perror("dump_jacosub: fopen");
2049 fprintf(fd
, "<SAMI>\n"
2051 " <STYLE TYPE=\"Text/css\">\n"
2053 " 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"
2054 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2059 for(i
=0; i
< subd
->sub_num
; i
++)
2061 onesub
=subs
+i
; //=&subs[i];
2064 if (!subd
->sub_uses_time
)
2065 temp
= temp
* 100 / sub_fps
;
2066 temp
-= sub_delay
* 100;
2067 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2068 "\t <P>", temp
* 10);
2070 for(j
=0;j
<onesub
->lines
;j
++)
2071 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2076 if (!subd
->sub_uses_time
)
2077 temp
= temp
* 100 / sub_fps
;
2078 temp
-= sub_delay
* 100;
2079 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2080 "\t <P> \n", temp
* 10);
2082 fprintf(fd
, "</BODY>\n"
2085 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2088 void sub_free( sub_data
* subd
)
2092 if ( !subd
) return;
2094 if (subd
->subtitles
) {
2095 for (i
=0; i
< subd
->subtitles
->lines
; i
++) free( subd
->subtitles
->text
[i
] );
2096 free( subd
->subtitles
);
2098 if (subd
->filename
) free( subd
->filename
);
2103 int main(int argc
, char **argv
) { // for testing
2107 printf("\nUsage: subreader filename.sub\n\n");
2111 subd
= sub_read_file(argv
[1]);
2113 printf("Couldn't load file.\n");
2117 list_sub_file(subd
);