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"
26 #define ERR ((void *) -1)
33 #include <fribidi/fribidi.h>
34 char *fribidi_charset
= NULL
;
38 extern char* dvdsub_lang
;
40 /* Maximal length of line of a subtitle */
42 static float mpsub_position
=0;
43 static float mpsub_multiplier
=1.;
44 static int sub_slacktime
= 20000; //20 sec
46 int sub_no_text_pp
=0; // 1 => do not apply text post-processing
47 // like {\...} elimination in SSA format.
49 int sub_match_fuzziness
=0; // level of sub name matching fuzziness
51 /* Use the SUB_* constant defined in the header file */
52 int sub_format
=SUB_INVALID
;
55 Some subtitling formats, namely AQT and Subrip09, define the end of a
56 subtitle as the beginning of the following. Since currently we read one
57 subtitle at time, for these format we keep two global *subtitle,
58 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
59 so we can change its end when we read current subtitle starting time.
60 When USE_SORTSUB is defined, we use a single global unsigned long,
61 previous_sub_end, for both (and even future) formats, to store the end of
62 the previous sub: it is initialized to 0 in sub_read_file and eventually
63 modified by sub_read_aqt_line or sub_read_subrip09_line.
65 unsigned long previous_sub_end
;
68 static int eol(char p
) {
69 return (p
=='\r' || p
=='\n' || p
=='\0');
72 /* Remove leading and trailing space */
73 static void trail_space(char *s
) {
75 while (isspace(s
[i
])) ++i
;
76 if (i
) strcpy(s
, s
+ i
);
78 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
81 static char *stristr(const char *haystack
, const char *needle
) {
83 const char *p
= haystack
;
85 if (!(haystack
&& needle
)) return NULL
;
89 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
96 subtitle
*sub_read_line_sami(FILE *fd
, subtitle
*current
) {
97 static char line
[LINE_LEN
+1];
98 static char *s
= NULL
, *slacktime_s
;
99 char text
[LINE_LEN
+1], *p
=NULL
, *q
;
102 current
->lines
= current
->start
= current
->end
= 0;
105 /* read the first line */
107 if (!(s
= fgets(line
, LINE_LEN
, fd
))) return 0;
112 case 0: /* find "START=" or "Slacktime:" */
113 slacktime_s
= stristr (s
, "Slacktime:");
115 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
117 s
= stristr (s
, "Start=");
119 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
121 for (; *s
!= '>' && *s
!= '\0'; s
++);
127 case 1: /* find (optionnal) "<P", skip other TAGs */
128 for (; *s
== ' ' || *s
== '\t'; s
++); /* strip blanks, if any */
129 if (*s
== '\0') break;
130 if (*s
!= '<') { state
= 3; p
= text
; continue; } /* not a TAG */
132 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
133 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
139 case 2: /* find ">" */
140 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
143 case 3: /* get all text until '<' appears */
144 if (*s
== '\0') break;
145 else if (!strncasecmp (s
, "<br>", 4)) {
146 *p
= '\0'; p
= text
; trail_space (text
);
148 current
->text
[current
->lines
++] = strdup (text
);
151 else if ((*s
== '{') && !sub_no_text_pp
) { state
= 5; ++s
; continue; }
152 else if (*s
== '<') { state
= 4; }
153 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
154 else if (*s
== '\t') { *p
++ = ' '; s
++; }
155 else if (*s
== '\r' || *s
== '\n') { s
++; }
158 /* skip duplicated space */
159 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
163 case 4: /* get current->end or skip <TAG> */
164 q
= stristr (s
, "Start=");
166 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
167 *p
= '\0'; trail_space (text
);
169 current
->text
[current
->lines
++] = strdup (text
);
170 if (current
->lines
> 0) { state
= 99; break; }
174 if (s
) { s
++; state
= 3; continue; }
176 case 5: /* get rid of {...} text */
177 if (*s
== '}') state
= 3;
183 if (state
!= 99 && !(s
= fgets (line
, LINE_LEN
, fd
))) {
184 if (current
->start
> 0) {
185 break; // if it is the last subtitle
191 } while (state
!= 99);
193 // For the last subtitle
194 if (current
->end
<= 0) {
195 current
->end
= current
->start
+ sub_slacktime
;
196 *p
= '\0'; trail_space (text
);
198 current
->text
[current
->lines
++] = strdup (text
);
205 char *sub_readtext(char *source
, char **dest
) {
209 // printf("src=%p dest=%p \n",source,dest);
211 while ( !eol(*p
) && *p
!= '|' ) {
215 *dest
= (char *)malloc (len
+1);
216 if (!dest
) {return ERR
;}
218 strncpy(*dest
, source
, len
);
221 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
223 if (*p
) return p
; // not-last text field
224 else return NULL
; // last text field
227 subtitle
*sub_read_line_microdvd(FILE *fd
,subtitle
*current
) {
228 char line
[LINE_LEN
+1];
229 char line2
[LINE_LEN
+1];
234 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
235 } while ((sscanf (line
,
237 &(current
->start
), line2
) < 2) &&
239 "{%ld}{%ld}%[^\r\n]",
240 &(current
->start
), &(current
->end
), line2
) < 3));
245 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
246 if (current
->text
[i
]==ERR
) {return ERR
;}
248 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
255 subtitle
*sub_read_line_mpl2(FILE *fd
,subtitle
*current
) {
256 char line
[LINE_LEN
+1];
257 char line2
[LINE_LEN
+1];
262 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
263 } while ((sscanf (line
,
264 "[%ld][%ld]%[^\r\n]",
265 &(current
->start
), &(current
->end
), line2
) < 3));
266 current
->start
*= 10;
271 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
272 if (current
->text
[i
]==ERR
) {return ERR
;}
274 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
281 subtitle
*sub_read_line_subrip(FILE *fd
, subtitle
*current
) {
282 char line
[LINE_LEN
+1];
283 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
284 char *p
=NULL
, *q
=NULL
;
288 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
289 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
290 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
291 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
293 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
296 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
297 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
298 current
->text
[current
->lines
-1]=(char *)malloc (len
+1);
299 if (!current
->text
[current
->lines
-1]) return ERR
;
300 strncpy (current
->text
[current
->lines
-1], q
, len
);
301 current
->text
[current
->lines
-1][len
]='\0';
302 if (!*p
|| *p
=='\r' || *p
=='\n') break;
304 else while (*p
++!=']');
311 subtitle
*sub_read_line_subviewer(FILE *fd
,subtitle
*current
) {
312 char line
[LINE_LEN
+1];
313 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
317 while (!current
->text
[0]) {
318 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
319 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)
321 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
322 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
323 for (i
=0; i
<SUB_MAX_TEXT
;) {
324 if (!fgets (line
, LINE_LEN
, fd
)) break;
326 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++);
329 char *curptr
=current
->text
[i
]=(char *)malloc (len
+1);
330 if (!current
->text
[i
]) return ERR
;
331 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
333 /* let's filter html tags ::atmos */
360 subtitle
*sub_read_line_subviewer2(FILE *fd
,subtitle
*current
) {
361 char line
[LINE_LEN
+1];
366 while (!current
->text
[0]) {
367 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
370 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
372 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
373 for (i
=0; i
<SUB_MAX_TEXT
;) {
374 if (!fgets (line
, LINE_LEN
, fd
)) break;
375 if (line
[0]=='}') break;
377 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
379 current
->text
[i
]=(char *)malloc (len
+1);
380 if (!current
->text
[i
]) return ERR
;
381 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
393 subtitle
*sub_read_line_vplayer(FILE *fd
,subtitle
*current
) {
394 char line
[LINE_LEN
+1];
396 char *p
=NULL
, *next
,separator
;
399 while (!current
->text
[0]) {
400 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
401 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
404 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
408 // finds the body of the subtitle
415 printf("SUB: Skipping incorrect subtitle line!\n");
419 // by wodzu: hey! this time we know what length it has! what is
420 // that magic for? it can't deal with space instead of third
421 // colon! look, what simple it can be:
428 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
429 if (current
->text
[i
]==ERR
) {return ERR
;}
431 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
439 subtitle
*sub_read_line_rt(FILE *fd
,subtitle
*current
) {
440 //TODO: This format uses quite rich (sub/super)set of xhtml
441 // I couldn't check it since DTD is not included.
442 // WARNING: full XML parses can be required for proper parsing
443 char line
[LINE_LEN
+1];
444 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
445 char *p
=NULL
,*next
=NULL
;
448 while (!current
->text
[0]) {
449 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
450 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
451 //to describe the same moment in time. Maybe there are even more formats in use.
452 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
453 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
455 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
456 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
457 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
458 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
459 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
460 ((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) &&
461 ((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) &&
462 //now try it without end time
463 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
464 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
465 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
466 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
469 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
470 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
471 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
472 current
->end
= current
->start
+200;
474 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
475 next
= strstr(line
,"<clear/>");
476 if(next
&& strlen(next
)>8){
478 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
479 if (current
->text
[i
]==ERR
) {return ERR
;}
481 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
489 subtitle
*sub_read_line_ssa(FILE *fd
,subtitle
*current
) {
491 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
492 * other Sub Station Alpha scripts have only 8 commas before subtitle
493 * Reading the "ScriptType:" field is not reliable since many scripts appear
496 * http://www.scriptclub.org is a good place to find more examples
497 * http://www.eswat.demon.co.uk is where the SSA specs can be found
500 static int max_comma
= 32; /* let's use 32 for the case that the */
501 /* amount of commas increase with newer SSA versions */
503 int hour1
, min1
, sec1
, hunsec1
,
504 hour2
, min2
, sec2
, hunsec2
, nothing
;
507 char line
[LINE_LEN
+1],
513 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
514 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
515 "%[^\n\r]", ¬hing
,
516 &hour1
, &min1
, &sec1
, &hunsec1
,
517 &hour2
, &min2
, &sec2
, &hunsec2
,
520 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
521 "%[^\n\r]", ¬hing
,
522 &hour1
, &min1
, &sec1
, &hunsec1
,
523 &hour2
, &min2
, &sec2
, &hunsec2
,
526 line2
=strchr(line3
, ',');
528 for (comma
= 4; comma
< max_comma
; comma
++)
531 if(!(tmp
=strchr(++tmp
, ','))) break;
532 if(*(++tmp
) == ' ') break;
533 /* a space after a comma means we're already in a sentence */
537 if(comma
< max_comma
)max_comma
= comma
;
538 /* eliminate the trailing comma */
539 if(*line2
== ',') line2
++;
541 current
->lines
=0;num
=0;
542 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
543 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
545 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
546 current
->text
[num
]=(char *)malloc(tmp
-line2
+1);
547 strncpy (current
->text
[num
], line2
, tmp
-line2
);
548 current
->text
[num
][tmp
-line2
]='\0';
552 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
555 current
->text
[num
]=strdup(line2
);
561 void sub_pp_ssa(subtitle
*sub
) {
566 /* eliminate any text enclosed with {}, they are font and color settings */
567 so
=de
=sub
->text
[--l
];
569 if(*so
== '{' && so
[1]=='\\') {
570 for (start
=so
; *so
&& *so
!='}'; so
++);
571 if(*so
) so
++; else so
=start
;
583 * PJS subtitles reader.
584 * That's the "Phoenix Japanimation Society" format.
585 * I found some of them in http://www.scriptsclub.org/ (used for anime).
586 * The time is in tenths of second.
588 * by set, based on code by szabi (dunnowhat sub format ;-)
590 subtitle
*sub_read_line_pjs(FILE *fd
,subtitle
*current
) {
591 char line
[LINE_LEN
+1];
592 char text
[LINE_LEN
+1], *s
, *d
;
594 if (!fgets (line
, LINE_LEN
, fd
))
597 for (s
=line
; *s
&& isspace(*s
); s
++);
598 /* allow empty lines at the end of the file */
602 if (sscanf (s
, "%ld,%ld,", &(current
->start
),
603 &(current
->end
)) <2) {
606 /* the files I have are in tenths of second */
607 current
->start
*= 10;
609 /* walk to the beggining of the string */
610 for (; *s
; s
++) if (*s
==',') break;
612 for (s
++; *s
; s
++) if (*s
==',') break;
618 /* copy the string to the text buffer */
619 for (s
++, d
=text
; *s
&& *s
!='"'; s
++, d
++)
622 current
->text
[0] = strdup(text
);
628 subtitle
*sub_read_line_mpsub(FILE *fd
, subtitle
*current
) {
629 char line
[LINE_LEN
+1];
636 if (!fgets(line
, LINE_LEN
, fd
)) return NULL
;
637 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
639 mpsub_position
+= a
*mpsub_multiplier
;
640 current
->start
=(int) mpsub_position
;
641 mpsub_position
+= b
*mpsub_multiplier
;
642 current
->end
=(int) mpsub_position
;
644 while (num
< SUB_MAX_TEXT
) {
645 if (!fgets (line
, LINE_LEN
, fd
)) {
646 if (num
== 0) return NULL
;
650 while (isspace(*p
)) p
++;
651 if (eol(*p
) && num
> 0) return current
;
652 if (eol(*p
)) return NULL
;
654 for (q
=p
; !eol(*q
); q
++);
657 current
->text
[num
]=strdup(p
);
658 // printf (">%s<\n",p);
659 current
->lines
= ++num
;
661 if (num
) return current
;
665 return NULL
; // we should have returned before if it's OK
669 //we don't need this if we use previous_sub_end
670 subtitle
*previous_aqt_sub
= NULL
;
673 subtitle
*sub_read_line_aqt(FILE *fd
,subtitle
*current
) {
674 char line
[LINE_LEN
+1];
679 // try to locate next subtitle
680 if (!fgets (line
, LINE_LEN
, fd
))
682 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
687 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
689 if (previous_aqt_sub
!= NULL
)
690 previous_aqt_sub
->end
= current
->start
-1;
692 previous_aqt_sub
= current
;
695 if (!fgets (line
, LINE_LEN
, fd
))
698 sub_readtext((char *) &line
,¤t
->text
[0]);
700 current
->end
= current
->start
; // will be corrected by next subtitle
702 if (!fgets (line
, LINE_LEN
, fd
))
706 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
707 if (current
->text
[i
]==ERR
) {return ERR
;}
709 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
713 if ((current
->text
[0]=="") && (current
->text
[1]=="")) {
715 previous_sub_end
= 0;
717 // void subtitle -> end of previous marked and exit
718 previous_aqt_sub
= NULL
;
727 subtitle
*previous_subrip09_sub
= NULL
;
730 subtitle
*sub_read_line_subrip09(FILE *fd
,subtitle
*current
) {
731 char line
[LINE_LEN
+1];
737 // try to locate next subtitle
738 if (!fgets (line
, LINE_LEN
, fd
))
740 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
744 current
->start
= a1
*360000+a2
*6000+a3
*100;
747 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
749 if (previous_subrip09_sub
!= NULL
)
750 previous_subrip09_sub
->end
= current
->start
-1;
752 previous_subrip09_sub
= current
;
755 if (!fgets (line
, LINE_LEN
, fd
))
760 current
->text
[0]=""; // just to be sure that string is clear
762 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
763 if (current
->text
[i
]==ERR
) {return ERR
;}
765 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
769 if ((current
->text
[0]=="") && (i
==0)) {
771 previous_sub_end
= 0;
773 // void subtitle -> end of previous marked and exit
774 previous_subrip09_sub
= NULL
;
782 subtitle
*sub_read_line_jacosub(FILE * fd
, subtitle
* current
)
784 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
785 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
786 static unsigned jacoTimeres
= 30;
787 static int jacoShift
= 0;
789 bzero(current
, sizeof(subtitle
));
790 bzero(line1
, LINE_LEN
);
791 bzero(line2
, LINE_LEN
);
792 bzero(directive
, LINE_LEN
);
793 while (!current
->text
[0]) {
794 if (!fgets(line1
, LINE_LEN
, fd
)) {
798 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
799 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
800 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
801 if (line1
[0] == '#') {
802 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
804 unsigned units
= jacoShift
;
805 switch (toupper(line1
[1])) {
807 if (isalpha(line1
[2])) {
812 if (sscanf(&line1
[delta
], "%d", &hours
)) {
817 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
819 (&line1
[delta
], "%*d:%*d:%d",
821 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
825 sscanf(&line1
[delta
], "%d:%d.%d",
826 &minutes
, &seconds
, &units
);
831 sscanf(&line1
[delta
], "%d.%d", &seconds
,
836 ((hours
* 3600 + minutes
* 60 +
837 seconds
) * jacoTimeres
+
842 if (isalpha(line1
[2])) {
847 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
854 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
857 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
863 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
864 jacoShift
) * 100.0 / jacoTimeres
);
867 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
868 jacoShift
) * 100.0 / jacoTimeres
);
872 while ((*p
== ' ') || (*p
== '\t')) {
875 if (isalpha(*p
)||*p
== '[') {
878 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
879 return (subtitle
*) ERR
;
880 jLength
= strlen(directive
);
881 for (cont
= 0; cont
< jLength
; ++cont
) {
882 if (isalpha(*(directive
+ cont
)))
883 *(directive
+ cont
) = toupper(*(directive
+ cont
));
885 if ((strstr(directive
, "RDB") != NULL
)
886 || (strstr(directive
, "RDC") != NULL
)
887 || (strstr(directive
, "RLB") != NULL
)
888 || (strstr(directive
, "RLG") != NULL
)) {
891 if (strstr(directive
, "JL") != NULL
) {
892 current
->alignment
= SUB_ALIGNMENT_HLEFT
;
893 } else if (strstr(directive
, "JR") != NULL
) {
894 current
->alignment
= SUB_ALIGNMENT_HRIGHT
;
896 current
->alignment
= SUB_ALIGNMENT_HCENTER
;
898 strcpy(line2
, line1
);
901 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
909 //the next line to get rid of a blank after the comment
910 if ((*(p
+ 1)) == ' ')
922 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
930 if (*(p
+ 1) == 'n') {
933 current
->text
[current
->lines
++] = strdup(line1
);
937 if ((toupper(*(p
+ 1)) == 'C')
938 || (toupper(*(p
+ 1)) == 'F')) {
942 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
943 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
944 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
948 if ((*(p
+ 1) == '\\') ||
949 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
951 } else if (eol(*(p
+ 1))) {
952 if (!fgets(directive
, LINE_LEN
, fd
))
954 trail_space(directive
);
955 strncat(line2
, directive
,
956 (LINE_LEN
> 511) ? LINE_LEN
: 511);
967 current
->text
[current
->lines
] = strdup(line1
);
973 int sub_autodetect (FILE *fd
, int *uses_time
) {
974 char line
[LINE_LEN
+1];
980 if (!fgets (line
, LINE_LEN
, fd
))
983 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
984 {*uses_time
=0;return SUB_MICRODVD
;}
985 if (sscanf (line
, "{%d}{}", &i
)==1)
986 {*uses_time
=0;return SUB_MICRODVD
;}
987 if (sscanf (line
, "[%d][%d]", &i
, &i
)==2)
988 {*uses_time
=1;return SUB_MPL2
;}
989 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
990 {*uses_time
=1;return SUB_SUBRIP
;}
991 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
992 {*uses_time
=1;return SUB_SUBVIEWER
;}
993 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
994 {*uses_time
=1;return SUB_SUBVIEWER2
;}
995 if (strstr (line
, "<SAMI>"))
996 {*uses_time
=1; return SUB_SAMI
;}
997 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
998 {*uses_time
= 1; return SUB_JACOSUB
;}
999 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
1000 {*uses_time
= 1; return SUB_JACOSUB
;}
1001 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
1002 {*uses_time
=1;return SUB_VPLAYER
;}
1003 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
1004 {*uses_time
=1;return SUB_VPLAYER
;}
1005 //TODO: just checking if first line of sub starts with "<" is WAY
1006 // too weak test for RT
1007 // Please someone who knows the format of RT... FIX IT!!!
1008 // It may conflict with other sub formats in the future (actually it doesn't)
1010 {*uses_time
=1;return SUB_RT
;}
1012 if (!memcmp(line
, "Dialogue: Marked", 16))
1013 {*uses_time
=1; return SUB_SSA
;}
1014 if (!memcmp(line
, "Dialogue: ", 10))
1015 {*uses_time
=1; return SUB_SSA
;}
1016 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
1017 {*uses_time
=1;return SUB_PJS
;}
1018 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
1019 {*uses_time
=0; return SUB_MPSUB
;}
1020 if (sscanf (line
, "FORMAT=TIM%c", &p
)==1 && p
=='E')
1021 {*uses_time
=1; return SUB_MPSUB
;}
1022 if (strstr (line
, "-->>"))
1023 {*uses_time
=0; return SUB_AQTITLE
;}
1024 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
1025 {*uses_time
=1;return SUB_SUBRIP09
;}
1028 return SUB_INVALID
; // too many bad lines
1034 extern int sub_utf8
;
1035 int sub_utf8_prev
=0;
1038 extern float sub_delay
;
1039 extern float sub_fps
;
1042 static iconv_t icdsc
= (iconv_t
)(-1);
1044 void subcp_open (FILE *enca_fd
)
1046 char *tocp
= "UTF-8";
1049 char *cp_tmp
= sub_cp
;
1051 char enca_lang
[3], enca_fallback
[100];
1052 int free_cp_tmp
= 0;
1053 if (sscanf(sub_cp
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
1054 || sscanf(sub_cp
, "ENCA:%2s:%99s", enca_lang
, enca_fallback
) == 2) {
1056 cp_tmp
= guess_cp(enca_fd
, enca_lang
, enca_fallback
);
1059 cp_tmp
= enca_fallback
;
1063 if ((icdsc
= iconv_open (tocp
, cp_tmp
)) != (iconv_t
)(-1)){
1064 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1067 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1069 if (free_cp_tmp
&& cp_tmp
) free(cp_tmp
);
1074 void subcp_close (void)
1076 if (icdsc
!= (iconv_t
)(-1)){
1077 (void) iconv_close (icdsc
);
1078 icdsc
= (iconv_t
)(-1);
1079 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
1083 #define ICBUFFSIZE 512
1084 static char icbuffer
[ICBUFFSIZE
];
1086 subtitle
* subcp_recode (subtitle
*sub
)
1089 size_t ileft
, oleft
;
1094 ip
= sub
->text
[--l
];
1096 oleft
= ICBUFFSIZE
- 1;
1098 if (iconv(icdsc
, &ip
, &ileft
,
1099 &op
, &oleft
) == (size_t)(-1)) {
1100 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line (1).\n");
1104 if (!(ot
= (char *)malloc(op
- icbuffer
+ 1))){
1105 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1110 strcpy (ot
, icbuffer
);
1111 free (sub
->text
[l
]);
1115 for (l
= sub
->lines
; l
;)
1116 free (sub
->text
[--l
]);
1123 subtitle
* subcp_recode1 (subtitle
*sub
)
1126 size_t ileft
, oleft
;
1128 if(icdsc
== (iconv_t
)(-1)) return sub
;
1131 char *ip
= icbuffer
;
1132 char *op
= sub
->text
[--l
];
1133 strlcpy(ip
, op
, ICBUFFSIZE
);
1135 oleft
= ICBUFFSIZE
- 1;
1137 if (iconv(icdsc
, &ip
, &ileft
,
1138 &op
, &oleft
) == (size_t)(-1)) {
1139 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: error recoding line (2).\n");
1150 #define max(a,b) (((a)>(b))?(a):(b))
1152 subtitle
* sub_fribidi (subtitle
*sub
, int sub_utf8
)
1154 FriBidiChar logical
[LINE_LEN
+1], visual
[LINE_LEN
+1]; // Hopefully these two won't smash the stack
1155 char *ip
= NULL
, *op
= NULL
;
1156 FriBidiCharType base
;
1157 size_t len
,orig_len
;
1160 fribidi_boolean log2vis
;
1161 if(flip_hebrew
) { // Please fix the indentation someday
1162 fribidi_set_mirroring (FRIBIDI_TRUE
);
1163 fribidi_set_reorder_nsm (FRIBIDI_FALSE
);
1165 if( sub_utf8
== 0 ) {
1166 char_set_num
= fribidi_parse_charset (fribidi_charset
?fribidi_charset
:"ISO8859-8");
1168 char_set_num
= fribidi_parse_charset ("UTF-8");
1171 ip
= sub
->text
[--l
];
1172 orig_len
= len
= strlen( ip
); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1173 if(len
> LINE_LEN
) {
1174 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: sub->text is longer than LINE_LEN.\n");
1178 len
= fribidi_charset_to_unicode (char_set_num
, ip
, len
, logical
);
1179 base
= FRIBIDI_TYPE_ON
;
1180 log2vis
= fribidi_log2vis (logical
, len
, &base
,
1182 visual
, NULL
, NULL
, NULL
);
1184 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
,
1186 if((op
= (char*)malloc(sizeof(char)*(max(2*orig_len
,2*len
) + 1))) == NULL
) {
1187 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1191 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1197 for (l
= sub
->lines
; l
;)
1198 free (sub
->text
[--l
]);
1207 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1208 int sub_num
, int sub_uses_time
) {
1212 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1213 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1217 if (sub
->end
<= sub
->start
){
1218 sub
->end
= sub
->start
+ subfms
;
1225 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1226 // these subtitles overlap for less than 0.2 seconds
1227 // and would result in very short overlapping subtitle
1228 // so let's fix the problem here, before overlapping code
1229 // get its hands on them
1230 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1231 sub
->end
-= half
+ 1;
1232 nextsub
->start
+= delta
- half
;
1234 if (sub
->end
>= nextsub
->start
){
1235 sub
->end
= nextsub
->start
- 1;
1236 if (sub
->end
- sub
->start
> subfms
)
1237 sub
->end
= sub
->start
+ subfms
;
1244 * Movies are often converted from FILM (24 fps)
1245 * to PAL (25) by simply speeding it up, so we
1246 * to multiply the original timestmaps by
1247 * (Movie's FPS / Subtitle's (guessed) FPS)
1248 * so eg. for 23.98 fps movie and PAL time based
1249 * subtitles we say -subfps 25 and we're fine!
1252 /* timed sub fps correction ::atmos */
1253 if(sub_uses_time
&& sub_fps
) {
1254 sub
->start
*= sub_fps
/fps
;
1255 sub
->end
*= sub_fps
/fps
;
1261 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Adjusted %d subtitle(s).\n", n
);
1265 subtitle
* (*read
)(FILE *fd
,subtitle
*dest
);
1266 void (*post
)(subtitle
*dest
);
1271 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1272 void* guess_cp(FILE *fd
, char *preferred_language
, char *fallback
)
1274 const char **languages
;
1275 size_t langcnt
, buflen
;
1276 EncaAnalyser analyser
;
1277 EncaEncoding encoding
;
1278 unsigned char *buffer
;
1279 char *detected_sub_cp
= NULL
;
1282 buffer
= (unsigned char*)malloc(MAX_GUESS_BUFFER_SIZE
*sizeof(char));
1283 buflen
= fread(buffer
, 1, MAX_GUESS_BUFFER_SIZE
, fd
);
1285 languages
= enca_get_languages(&langcnt
);
1286 mp_msg(MSGT_SUBREADER
, MSGL_V
, "ENCA supported languages: ");
1287 for (i
= 0; i
< langcnt
; i
++) {
1288 mp_msg(MSGT_SUBREADER
, MSGL_V
, "%s ", languages
[i
]);
1290 mp_msg(MSGT_SUBREADER
, MSGL_V
, "\n");
1292 for (i
= 0; i
< langcnt
; i
++) {
1293 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1294 analyser
= enca_analyser_alloc(languages
[i
]);
1295 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1296 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
));
1297 detected_sub_cp
= strdup(enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
));
1298 enca_analyser_free(analyser
);
1305 if (!detected_sub_cp
) detected_sub_cp
= strdup(fallback
);
1307 return detected_sub_cp
;
1311 sub_data
* sub_read_file (char *filename
, float fps
) {
1312 //filename is assumed to be malloc'ed, free() is used in sub_free()
1314 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1315 subtitle
*first
, *second
, *sub
, *return_sub
;
1316 sub_data
*subt_data
;
1317 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1318 struct subreader sr
[]=
1320 { sub_read_line_microdvd
, NULL
, "microdvd" },
1321 { sub_read_line_subrip
, NULL
, "subrip" },
1322 { sub_read_line_subviewer
, NULL
, "subviewer" },
1323 { sub_read_line_sami
, NULL
, "sami" },
1324 { sub_read_line_vplayer
, NULL
, "vplayer" },
1325 { sub_read_line_rt
, NULL
, "rt" },
1326 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1327 { sub_read_line_pjs
, NULL
, "pjs" },
1328 { sub_read_line_mpsub
, NULL
, "mpsub" },
1329 { sub_read_line_aqt
, NULL
, "aqt" },
1330 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1331 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1332 { sub_read_line_jacosub
, NULL
, "jacosub" },
1333 { sub_read_line_mpl2
, NULL
, "mpl2" }
1335 struct subreader
*srp
;
1337 if(filename
==NULL
) return NULL
; //qnx segfault
1338 fd
=fopen (filename
, "r"); if (!fd
) return NULL
;
1340 sub_format
=sub_autodetect (fd
, &uses_time
);
1341 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1342 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1344 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Detected subtitle file format: %s\n", srp
->name
);
1349 sub_utf8_prev
=sub_utf8
;
1353 if ((l
=strlen(filename
))>4){
1354 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1356 if (!strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1361 if (k
<0) subcp_open(fd
);
1366 first
=(subtitle
*)malloc(n_max
*sizeof(subtitle
));
1370 sub_utf8
=sub_utf8_prev
;
1376 sub
= (subtitle
*)malloc(sizeof(subtitle
));
1377 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1378 //as the beginning of the following
1379 previous_sub_end
= 0;
1384 first
=realloc(first
,n_max
*sizeof(subtitle
));
1387 sub
= &first
[sub_num
];
1389 memset(sub
, '\0', sizeof(subtitle
));
1390 sub
=srp
->read(fd
,sub
);
1391 if(!sub
) break; // EOF
1393 if ((sub
!=ERR
) && (sub_utf8
& 2)) sub
=subcp_recode(sub
);
1396 if (sub
!=ERR
) sub
=sub_fribidi(sub
,sub_utf8
);
1403 if ( first
) free(first
);
1406 // Apply any post processing that needs recoding first
1407 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1409 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1410 first
[sub_num
].start
= sub
->start
;
1411 first
[sub_num
].end
= sub
->end
;
1412 first
[sub_num
].lines
= sub
->lines
;
1413 first
[sub_num
].alignment
= sub
->alignment
;
1414 for(i
= 0; i
< sub
->lines
; ++i
){
1415 first
[sub_num
].text
[i
] = sub
->text
[i
];
1417 if (previous_sub_end
){
1418 first
[sub_num
- 1].end
= previous_sub_end
;
1419 previous_sub_end
= 0;
1422 for(j
= sub_num
- 1; j
>= 0; --j
){
1423 first
[j
+ 1].start
= first
[j
].start
;
1424 first
[j
+ 1].end
= first
[j
].end
;
1425 first
[j
+ 1].lines
= first
[j
].lines
;
1426 first
[j
+ 1].alignment
= first
[j
].alignment
;
1427 for(i
= 0; i
< first
[j
].lines
; ++i
){
1428 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1430 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1431 first
[j
].start
= sub
->start
;
1432 first
[j
].end
= sub
->end
;
1433 first
[j
].lines
= sub
->lines
;
1434 first
[j
].alignment
= sub
->alignment
;
1435 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1436 first
[j
].text
[i
] = sub
->text
[i
];
1438 if (previous_sub_end
){
1439 first
[j
].end
= first
[j
- 1].end
;
1440 first
[j
- 1].end
= previous_sub_end
;
1441 previous_sub_end
= 0;
1448 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1457 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1458 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Read %i subtitles", sub_num
);
1459 if (sub_errs
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,", %i bad line(s).\n", sub_errs
);
1460 else mp_msg(MSGT_SUBREADER
,MSGL_INFO
,".\n");
1467 // we do overlap if the user forced it (suboverlap_enable == 2) or
1468 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1469 // this is because usually overlapping subtitles are found in these formats,
1470 // while in others they are probably result of bad timing
1471 if ((suboverlap_enabled
== 2) ||
1472 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1473 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1474 // here we manage overlapping subtitles
1479 // for each subtitle in first[] we deal with its 'block' of
1481 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1482 unsigned long global_start
= first
[sub_first
].start
,
1483 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1484 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1485 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1486 char real_block
= 1;
1488 // here we find the number of subtitles inside the 'block'
1489 // and its span interval. this works well only with sorted
1491 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1493 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1494 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1495 global_start
= first
[sub_first
+ sub_to_add
].start
;
1497 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1498 global_end
= first
[sub_first
+ sub_to_add
].end
;
1502 // we need a structure to keep trace of the screen lines
1503 // used by the subs, a 'placeholder'
1504 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1505 // from a block of sub_to_add+1 subs
1506 placeholder
= (int **) malloc(sizeof(int *) * counter
);
1507 for (i
= 0; i
< counter
; ++i
) {
1508 placeholder
[i
] = (int *) malloc(sizeof(int) * lines_to_add
);
1509 for (j
= 0; j
< lines_to_add
; ++j
) {
1510 placeholder
[i
][j
] = -1;
1515 local_end
= global_start
- 1;
1519 // here we find the beginning and the end of a new
1520 // subtitle in the block
1521 local_start
= local_end
+ 1;
1522 local_end
= global_end
;
1523 for (j
= 0; j
<= sub_to_add
; ++j
) {
1524 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1525 local_end
= first
[sub_first
+ j
].start
- 1;
1526 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1527 local_end
= first
[sub_first
+ j
].end
;
1530 // here we allocate the screen lines to subs we must
1531 // display in current local_start-local_end interval.
1532 // if the subs were yet presents in the previous interval
1533 // they keep the same lines, otherside they get unused lines
1534 for (j
= 0; j
<= sub_to_add
; ++j
) {
1535 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1536 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1539 int fragment_position
= -1;
1541 // if this is not the first new sub of the block
1542 // we find if this sub was present in the previous
1545 for (i
= 0; i
< lines_to_add
; ++i
) {
1546 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1547 placeholder
[counter
][i
] = sub_first
+ j
;
1554 // we are looking for the shortest among all groups of
1555 // sequential blank lines whose length is greater than or
1556 // equal to sub_lines. we store in fragment_position the
1557 // position of the shortest group, in fragment_length its
1558 // length, and in tmp the length of the group currently
1560 for (i
= 0; i
< lines_to_add
; ++i
) {
1561 if (placeholder
[counter
][i
] == -1) {
1562 // placeholder[counter][i] is part of the current group
1566 if (tmp
== sub_lines
) {
1567 // current group's size fits exactly the one we
1568 // need, so we stop looking
1569 fragment_position
= i
- tmp
;
1573 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1574 // current group is the best we found till here,
1575 // but is still bigger than the one we are looking
1576 // for, so we keep on looking
1577 fragment_length
= tmp
;
1578 fragment_position
= i
- tmp
;
1581 // current group doesn't fit at all, so we forget it
1587 // last screen line is blank, a group ends with it
1588 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1589 fragment_position
= i
- tmp
;
1592 if (fragment_position
== -1) {
1593 // it was not possible to find free screen line(s) for a subtitle,
1594 // usually this means a bug in the code; however we do not overlap
1595 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1596 higher_line
= SUB_MAX_TEXT
+ 1;
1599 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1600 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1605 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1606 if (placeholder
[counter
][j
] != -1)
1611 if (higher_line
>= SUB_MAX_TEXT
) {
1612 // the 'block' has too much lines, so we don't overlap the
1614 second
= (subtitle
*) realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1615 for (j
= 0; j
<= sub_to_add
; ++j
) {
1617 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1618 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1619 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1620 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1621 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1622 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1623 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1626 sub_num
+= sub_to_add
+ 1;
1627 sub_first
+= sub_to_add
;
1632 // we read the placeholder structure and create the new
1634 second
= (subtitle
*) realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1635 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1636 second
[sub_num
].start
= local_start
;
1637 second
[sub_num
].end
= local_end
;
1638 second
[sub_num
].alignment
= SUB_ALIGNMENT_HCENTER
;
1639 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1640 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1641 if (placeholder
[counter
][j
] != -1) {
1642 int lines
= first
[placeholder
[counter
][j
]].lines
;
1643 for (ls
= 0; ls
< lines
; ++ls
) {
1644 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1648 second
[sub_num
].text
[i
++] = strdup(" ");
1653 } while (local_end
< global_end
);
1655 for (i
= 0; i
< counter
; ++i
)
1656 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1658 counter
= 2 * sub_to_add
+ 1;
1659 for (i
= 0; i
< counter
; ++i
) {
1660 free(placeholder
[i
]);
1663 sub_first
+= sub_to_add
;
1666 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1667 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1668 free(first
[j
].text
[i
]);
1673 return_sub
= second
;
1674 } else { //if(suboverlap_enabled)
1675 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1678 if (return_sub
== NULL
) return NULL
;
1679 subt_data
= (sub_data
*)malloc(sizeof(sub_data
));
1680 subt_data
->filename
= filename
;
1681 subt_data
->sub_uses_time
= uses_time
;
1682 subt_data
->sub_num
= sub_num
;
1683 subt_data
->sub_errs
= sub_errs
;
1684 subt_data
->subtitles
= return_sub
;
1689 char * strreplace( char * in
,char * what
,char * whereof
)
1694 if ( ( in
== NULL
)||( what
== NULL
)||( whereof
== NULL
)||( ( tmp
=strstr( in
,what
) ) == NULL
) ) return NULL
;
1695 for( i
=0;i
<strlen( whereof
);i
++ ) tmp
[i
]=whereof
[i
];
1696 if ( strlen( what
) > strlen( whereof
) ) tmp
[i
]=0;
1702 static void strcpy_trim(char *d
, char *s
)
1704 // skip leading whitespace
1705 while (*s
&& !isalnum(*s
)) {
1710 while (*s
&& isalnum(*s
)) {
1715 // trim excess whitespace
1716 while (*s
&& !isalnum(*s
)) {
1725 static void strcpy_strip_ext(char *d
, char *s
)
1727 char *tmp
= strrchr(s
,'.');
1732 strncpy(d
, s
, tmp
-s
);
1741 static void strcpy_get_ext(char *d
, char *s
)
1743 char *tmp
= strrchr(s
,'.');
1752 static int whiteonly(char *s
)
1755 if (isalnum(*s
)) return 0;
1761 typedef struct _subfn
1767 static int compare_sub_priority(const void *a
, const void *b
)
1769 if (((subfn
*)a
)->priority
> ((subfn
*)b
)->priority
) {
1771 } else if (((subfn
*)a
)->priority
< ((subfn
*)b
)->priority
) {
1774 return strcoll(((subfn
*)a
)->fname
, ((subfn
*)b
)->fname
);
1778 char** sub_filenames(char* path
, char *fname
)
1780 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
1781 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
1783 int len
, pos
, found
, i
, j
;
1784 char * sub_exts
[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
1795 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
1796 +(strlen(path
) > 256 ? strlen(path
) : 256)+2;
1798 f_dir
= (char*)malloc(len
);
1799 f_fname
= (char*)malloc(len
);
1800 f_fname_noext
= (char*)malloc(len
);
1801 f_fname_trim
= (char*)malloc(len
);
1803 tmp_fname_noext
= (char*)malloc(len
);
1804 tmp_fname_trim
= (char*)malloc(len
);
1805 tmp_fname_ext
= (char*)malloc(len
);
1807 tmpresult
= (char*)malloc(len
);
1809 result
= (subfn
*)malloc(sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1810 memset(result
, 0, sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1814 tmp
= strrchr(fname
,'/');
1816 if(!tmp
)tmp
= strrchr(fname
,'\\');
1819 // extract filename & dirname from fname
1821 strcpy(f_fname
, tmp
+1);
1823 strncpy(f_dir
, fname
, pos
+1);
1826 strcpy(f_fname
, fname
);
1827 strcpy(f_dir
, "./");
1830 strcpy_strip_ext(f_fname_noext
, f_fname
);
1831 strcpy_trim(f_fname_trim
, f_fname_noext
);
1834 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
1835 tmp_sub_id
= (char*)malloc(strlen(dvdsub_lang
)+1);
1836 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
1840 // 1 = any subtitle file
1841 // 2 = any sub file containing movie name
1842 // 3 = sub file containing movie name and the lang extension
1843 for (j
= 0; j
<= 1; j
++) {
1844 d
= opendir(j
== 0 ? f_dir
: path
);
1846 while ((de
= readdir(d
))) {
1847 // retrieve various parts of the filename
1848 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
1849 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
1850 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
1852 // does it end with a subtitle extension?
1856 for (i
= ((sub_cp
&& strncasecmp(sub_cp
, "enca", 4) != 0) ? 3 : 0); sub_exts
[i
]; i
++) {
1858 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
1861 for (i
= 0; sub_exts
[i
]; i
++) {
1863 if (strcasecmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
1869 // we have a (likely) subtitle file
1872 if (!prio
&& tmp_sub_id
)
1874 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
1875 printf("dvdsublang...%s\n", tmpresult
);
1876 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
1877 // matches the movie name + lang extension
1881 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
1882 // matches the movie name
1885 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && (sub_match_fuzziness
>= 1)) {
1886 // contains the movie name
1887 tmp
+= strlen(f_fname_trim
);
1888 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
1889 // with sub_id specified prefer localized subtitles
1891 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
1892 // without sub_id prefer "plain" name
1895 // with no localized subs found, try any else instead
1900 // doesn't contain the movie name
1901 // don't try in the mplayer subtitle directory
1902 if ((j
== 0) && (sub_match_fuzziness
>= 2)) {
1910 if (i
<3){ // prefer UTF-8 coded
1914 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
1915 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1916 if ((f
= fopen(tmpresult
, "rt"))) {
1918 result
[subcnt
].priority
= prio
;
1919 result
[subcnt
].fname
= strdup(tmpresult
);
1925 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
1932 if (tmp_sub_id
) free(tmp_sub_id
);
1936 free(f_fname_noext
);
1939 free(tmp_fname_noext
);
1940 free(tmp_fname_trim
);
1941 free(tmp_fname_ext
);
1945 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
1947 result2
= (char**)malloc(sizeof(char*)*(subcnt
+1));
1948 memset(result2
, 0, sizeof(char*)*(subcnt
+1));
1950 for (i
= 0; i
< subcnt
; i
++) {
1951 result2
[i
] = result
[i
].fname
;
1953 result2
[subcnt
] = NULL
;
1960 void list_sub_file(sub_data
* subd
){
1962 subtitle
*subs
= subd
->subtitles
;
1964 for(j
=0; j
< subd
->sub_num
; j
++){
1965 subtitle
* egysub
=&subs
[j
];
1966 printf ("%i line%c (%li-%li)\n",
1968 (1==egysub
->lines
)?' ':'s',
1971 for (i
=0; i
<egysub
->lines
; i
++) {
1972 printf ("\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
1977 printf ("Subtitle format %s time.\n",
1978 subd
->sub_uses_time
? "uses":"doesn't use");
1979 printf ("Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
1982 void dump_srt(sub_data
* subd
, float fps
){
1988 subtitle
*subs
= subd
->subtitles
;
1990 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1992 fd
=fopen("dumpsub.srt","w");
1995 perror("dump_srt: fopen");
1998 for(i
=0; i
< subd
->sub_num
; i
++)
2000 onesub
=subs
+i
; //=&subs[i];
2001 fprintf(fd
,"%d\n",i
+1);//line number
2004 if (!subd
->sub_uses_time
)
2005 temp
= temp
* 100 / sub_fps
;
2006 temp
-= sub_delay
* 100;
2007 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2008 m
=temp
/6000; temp
%=6000; //m =1*100*60
2009 s
=temp
/100; temp
%=100; //s =1*100
2010 ms
=temp
*10; //ms=1*10
2011 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
2014 if (!subd
->sub_uses_time
)
2015 temp
= temp
* 100 / sub_fps
;
2016 temp
-= sub_delay
* 100;
2017 h
=temp
/360000;temp
%=360000;
2018 m
=temp
/6000; temp
%=6000;
2019 s
=temp
/100; temp
%=100;
2021 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
2023 for(j
=0;j
<onesub
->lines
;j
++)
2024 fprintf(fd
,"%s\n",onesub
->text
[j
]);
2029 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2032 void dump_mpsub(sub_data
* subd
, float fps
){
2036 subtitle
*subs
= subd
->subtitles
;
2038 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
2039 if (sub_fps
==0) sub_fps
=fps
;
2041 fd
=fopen ("dump.mpsub", "w");
2043 perror ("dump_mpsub: fopen");
2048 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
2049 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
2051 for(j
=0; j
< subd
->sub_num
; j
++){
2052 subtitle
* egysub
=&subs
[j
];
2053 if (subd
->sub_uses_time
) {
2054 a
=((egysub
->start
-mpsub_position
)/100.0);
2055 b
=((egysub
->end
-egysub
->start
)/100.0);
2056 if ( (float)((int)a
) == a
)
2057 fprintf (fd
, "%.0f",a
);
2059 fprintf (fd
, "%.2f",a
);
2061 if ( (float)((int)b
) == b
)
2062 fprintf (fd
, " %.0f\n",b
);
2064 fprintf (fd
, " %.2f\n",b
);
2066 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
2067 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
2070 mpsub_position
= egysub
->end
;
2071 for (i
=0; i
<egysub
->lines
; i
++) {
2072 fprintf (fd
, "%s\n",egysub
->text
[i
]);
2077 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2080 void dump_microdvd(sub_data
* subd
, float fps
) {
2083 subtitle
*subs
= subd
->subtitles
;
2086 fd
= fopen("dumpsub.txt", "w");
2088 perror("dumpsub.txt: fopen");
2091 delay
= sub_delay
* sub_fps
;
2092 for (i
= 0; i
< subd
->sub_num
; ++i
) {
2094 start
= subs
[i
].start
;
2096 if (subd
->sub_uses_time
) {
2097 start
= start
* sub_fps
/ 100 ;
2098 end
= end
* sub_fps
/ 100;
2101 start
= start
* sub_fps
/ fps
;
2102 end
= end
* sub_fps
/ fps
;
2106 fprintf(fd
, "{%d}{%d}", start
, end
);
2107 for (j
= 0; j
< subs
[i
].lines
; ++j
)
2108 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
2112 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
2115 void dump_jacosub(sub_data
* subd
, float fps
) {
2121 subtitle
*subs
= subd
->subtitles
;
2123 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2125 fd
=fopen("dumpsub.jss","w");
2128 perror("dump_jacosub: fopen");
2131 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
2132 for(i
=0; i
< subd
->sub_num
; i
++)
2134 onesub
=subs
+i
; //=&subs[i];
2137 if (!subd
->sub_uses_time
)
2138 temp
= temp
* 100 / sub_fps
;
2139 temp
-= sub_delay
* 100;
2140 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2141 m
=temp
/6000; temp
%=6000; //m =1*100*60
2142 s
=temp
/100; temp
%=100; //s =1*100
2144 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2147 if (!subd
->sub_uses_time
)
2148 temp
= temp
* 100 / sub_fps
;
2149 temp
-= sub_delay
* 100;
2150 h
=temp
/360000;temp
%=360000;
2151 m
=temp
/6000; temp
%=6000;
2152 s
=temp
/100; temp
%=100;
2154 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2156 for(j
=0;j
<onesub
->lines
;j
++)
2157 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2162 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2165 void dump_sami(sub_data
* subd
, float fps
) {
2170 subtitle
*subs
= subd
->subtitles
;
2172 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2174 fd
=fopen("dumpsub.smi","w");
2177 perror("dump_jacosub: fopen");
2180 fprintf(fd
, "<SAMI>\n"
2182 " <STYLE TYPE=\"Text/css\">\n"
2184 " 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"
2185 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2190 for(i
=0; i
< subd
->sub_num
; i
++)
2192 onesub
=subs
+i
; //=&subs[i];
2195 if (!subd
->sub_uses_time
)
2196 temp
= temp
* 100 / sub_fps
;
2197 temp
-= sub_delay
* 100;
2198 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2199 "\t <P>", temp
* 10);
2201 for(j
=0;j
<onesub
->lines
;j
++)
2202 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2207 if (!subd
->sub_uses_time
)
2208 temp
= temp
* 100 / sub_fps
;
2209 temp
-= sub_delay
* 100;
2210 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2211 "\t <P> \n", temp
* 10);
2213 fprintf(fd
, "</BODY>\n"
2216 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2219 void sub_free( sub_data
* subd
)
2223 if ( !subd
) return;
2225 if (subd
->subtitles
) {
2226 for (i
=0; i
< subd
->subtitles
->lines
; i
++) free( subd
->subtitles
->text
[i
] );
2227 free( subd
->subtitles
);
2229 if (subd
->filename
) free( subd
->filename
);
2234 int main(int argc
, char **argv
) { // for testing
2238 printf("\nUsage: subreader filename.sub\n\n");
2242 subd
= sub_read_file(argv
[1]);
2244 printf("Couldn't load file.\n");
2248 list_sub_file(subd
);