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;
120 case 1: /* find "<P" */
121 if ((s
= stristr (s
, "<P"))) { s
+= 2; state
= 2; continue; }
124 case 2: /* find ">" */
125 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
128 case 3: /* get all text until '<' appears */
129 if (*s
== '\0') break;
130 else if (!strncasecmp (s
, "<br>", 4)) {
131 *p
= '\0'; p
= text
; trail_space (text
);
133 current
->text
[current
->lines
++] = strdup (text
);
136 else if (*s
== '<') { state
= 4; }
137 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
138 else if (*s
== '\t') { *p
++ = ' '; s
++; }
139 else if (*s
== '\r' || *s
== '\n') { s
++; }
142 /* skip duplicated space */
143 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
147 case 4: /* get current->end or skip <TAG> */
148 q
= stristr (s
, "Start=");
150 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
151 *p
= '\0'; trail_space (text
);
153 current
->text
[current
->lines
++] = strdup (text
);
154 if (current
->lines
> 0) { state
= 99; break; }
158 if (s
) { s
++; state
= 3; continue; }
163 if (state
!= 99 && !(s
= fgets (line
, LINE_LEN
, fd
))) {
164 if (current
->start
> 0) {
165 break; // if it is the last subtitle
171 } while (state
!= 99);
173 // For the last subtitle
174 if (current
->end
<= 0) {
175 current
->end
= current
->start
+ sub_slacktime
;
176 *p
= '\0'; trail_space (text
);
178 current
->text
[current
->lines
++] = strdup (text
);
185 char *sub_readtext(char *source
, char **dest
) {
189 // printf("src=%p dest=%p \n",source,dest);
191 while ( !eol(*p
) && *p
!= '|' ) {
195 *dest
= (char *)malloc (len
+1);
196 if (!dest
) {return ERR
;}
198 strncpy(*dest
, source
, len
);
201 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
203 if (*p
) return p
; // not-last text field
204 else return NULL
; // last text field
207 subtitle
*sub_read_line_microdvd(FILE *fd
,subtitle
*current
) {
208 char line
[LINE_LEN
+1];
209 char line2
[LINE_LEN
+1];
214 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
215 } while ((sscanf (line
,
217 &(current
->start
), line2
) < 2) &&
219 "{%ld}{%ld}%[^\r\n]",
220 &(current
->start
), &(current
->end
), line2
) < 3));
225 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
226 if (current
->text
[i
]==ERR
) {return ERR
;}
228 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
235 subtitle
*sub_read_line_subrip(FILE *fd
, subtitle
*current
) {
236 char line
[LINE_LEN
+1];
237 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
238 char *p
=NULL
, *q
=NULL
;
242 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
243 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
244 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
245 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
247 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
250 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
251 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
252 current
->text
[current
->lines
-1]=(char *)malloc (len
+1);
253 if (!current
->text
[current
->lines
-1]) return ERR
;
254 strncpy (current
->text
[current
->lines
-1], q
, len
);
255 current
->text
[current
->lines
-1][len
]='\0';
256 if (!*p
|| *p
=='\r' || *p
=='\n') break;
258 else while (*p
++!=']');
265 subtitle
*sub_read_line_subviewer(FILE *fd
,subtitle
*current
) {
266 char line
[LINE_LEN
+1];
267 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
271 while (!current
->text
[0]) {
272 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
273 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)
275 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
276 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
277 for (i
=0; i
<SUB_MAX_TEXT
;) {
278 if (!fgets (line
, LINE_LEN
, fd
)) break;
280 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++);
283 char *curptr
=current
->text
[i
]=(char *)malloc (len
+1);
284 if (!current
->text
[i
]) return ERR
;
285 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
287 /* let's filter html tags ::atmos */
314 subtitle
*sub_read_line_subviewer2(FILE *fd
,subtitle
*current
) {
315 char line
[LINE_LEN
+1];
320 while (!current
->text
[0]) {
321 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
324 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
326 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
327 for (i
=0; i
<SUB_MAX_TEXT
;) {
328 if (!fgets (line
, LINE_LEN
, fd
)) break;
329 if (line
[0]=='}') break;
331 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
333 current
->text
[i
]=(char *)malloc (len
+1);
334 if (!current
->text
[i
]) return ERR
;
335 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
347 subtitle
*sub_read_line_vplayer(FILE *fd
,subtitle
*current
) {
348 char line
[LINE_LEN
+1];
350 char *p
=NULL
, *next
,separator
;
353 while (!current
->text
[0]) {
354 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
355 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
358 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
362 // finds the body of the subtitle
369 printf("SUB: Skipping incorrect subtitle line!\n");
373 // by wodzu: hey! this time we know what length it has! what is
374 // that magic for? it can't deal with space instead of third
375 // colon! look, what simple it can be:
382 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
383 if (current
->text
[i
]==ERR
) {return ERR
;}
385 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
393 subtitle
*sub_read_line_rt(FILE *fd
,subtitle
*current
) {
394 //TODO: This format uses quite rich (sub/super)set of xhtml
395 // I couldn't check it since DTD is not included.
396 // WARNING: full XML parses can be required for proper parsing
397 char line
[LINE_LEN
+1];
398 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
399 char *p
=NULL
,*next
=NULL
;
402 while (!current
->text
[0]) {
403 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
404 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
405 //to describe the same moment in time. Maybe there are even more formats in use.
406 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
407 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
409 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
410 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
411 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
412 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
413 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
414 ((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) &&
415 ((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) &&
416 //now try it without end time
417 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
418 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
419 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
420 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
423 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
424 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
425 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
426 current
->end
= current
->start
+200;
428 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
429 next
= strstr(line
,"<clear/>");
430 if(next
&& strlen(next
)>8){
432 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
433 if (current
->text
[i
]==ERR
) {return ERR
;}
435 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
443 subtitle
*sub_read_line_ssa(FILE *fd
,subtitle
*current
) {
445 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
446 * other Sub Station Alpha scripts have only 8 commas before subtitle
447 * Reading the "ScriptType:" field is not reliable since many scripts appear
450 * http://www.scriptclub.org is a good place to find more examples
451 * http://www.eswat.demon.co.uk is where the SSA specs can be found
454 static int max_comma
= 32; /* let's use 32 for the case that the */
455 /* amount of commas increase with newer SSA versions */
457 int hour1
, min1
, sec1
, hunsec1
,
458 hour2
, min2
, sec2
, hunsec2
, nothing
;
461 char line
[LINE_LEN
+1],
467 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
468 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
469 "%[^\n\r]", ¬hing
,
470 &hour1
, &min1
, &sec1
, &hunsec1
,
471 &hour2
, &min2
, &sec2
, &hunsec2
,
474 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
475 "%[^\n\r]", ¬hing
,
476 &hour1
, &min1
, &sec1
, &hunsec1
,
477 &hour2
, &min2
, &sec2
, &hunsec2
,
480 line2
=strchr(line3
, ',');
482 for (comma
= 4; comma
< max_comma
; comma
++)
485 if(!(tmp
=strchr(++tmp
, ','))) break;
486 if(*(++tmp
) == ' ') break;
487 /* a space after a comma means we're already in a sentence */
491 if(comma
< max_comma
)max_comma
= comma
;
492 /* eliminate the trailing comma */
493 if(*line2
== ',') line2
++;
495 current
->lines
=0;num
=0;
496 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
497 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
499 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
500 current
->text
[num
]=(char *)malloc(tmp
-line2
+1);
501 strncpy (current
->text
[num
], line2
, tmp
-line2
);
502 current
->text
[num
][tmp
-line2
]='\0';
506 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
509 current
->text
[num
]=strdup(line2
);
515 void sub_pp_ssa(subtitle
*sub
) {
520 /* eliminate any text enclosed with {}, they are font and color settings */
521 so
=de
=sub
->text
[--l
];
523 if(*so
== '{' && so
[1]=='\\') {
524 for (start
=so
; *so
&& *so
!='}'; so
++);
525 if(*so
) so
++; else so
=start
;
536 subtitle
*sub_read_line_dunnowhat(FILE *fd
,subtitle
*current
) {
537 char line
[LINE_LEN
+1];
538 char text
[LINE_LEN
+1];
540 if (!fgets (line
, LINE_LEN
, fd
))
542 if (sscanf (line
, "%ld,%ld,\"%[^\"]", &(current
->start
),
543 &(current
->end
), text
) <3)
545 current
->text
[0] = strdup(text
);
551 subtitle
*sub_read_line_mpsub(FILE *fd
, subtitle
*current
) {
552 char line
[LINE_LEN
+1];
559 if (!fgets(line
, LINE_LEN
, fd
)) return NULL
;
560 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
562 mpsub_position
+= a
*mpsub_multiplier
;
563 current
->start
=(int) mpsub_position
;
564 mpsub_position
+= b
*mpsub_multiplier
;
565 current
->end
=(int) mpsub_position
;
567 while (num
< SUB_MAX_TEXT
) {
568 if (!fgets (line
, LINE_LEN
, fd
)) {
569 if (num
== 0) return NULL
;
573 while (isspace(*p
)) p
++;
574 if (eol(*p
) && num
> 0) return current
;
575 if (eol(*p
)) return NULL
;
577 for (q
=p
; !eol(*q
); q
++);
580 current
->text
[num
]=strdup(p
);
581 // printf (">%s<\n",p);
582 current
->lines
= ++num
;
584 if (num
) return current
;
588 return NULL
; // we should have returned before if it's OK
592 //we don't need this if we use previous_sub_end
593 subtitle
*previous_aqt_sub
= NULL
;
596 subtitle
*sub_read_line_aqt(FILE *fd
,subtitle
*current
) {
597 char line
[LINE_LEN
+1];
602 // try to locate next subtitle
603 if (!fgets (line
, LINE_LEN
, fd
))
605 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
610 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
612 if (previous_aqt_sub
!= NULL
)
613 previous_aqt_sub
->end
= current
->start
-1;
615 previous_aqt_sub
= current
;
618 if (!fgets (line
, LINE_LEN
, fd
))
621 sub_readtext((char *) &line
,¤t
->text
[0]);
623 current
->end
= current
->start
; // will be corrected by next subtitle
625 if (!fgets (line
, LINE_LEN
, fd
))
629 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
630 if (current
->text
[i
]==ERR
) {return ERR
;}
632 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
636 if ((current
->text
[0]=="") && (current
->text
[1]=="")) {
638 previous_sub_end
= 0;
640 // void subtitle -> end of previous marked and exit
641 previous_aqt_sub
= NULL
;
650 subtitle
*previous_subrip09_sub
= NULL
;
653 subtitle
*sub_read_line_subrip09(FILE *fd
,subtitle
*current
) {
654 char line
[LINE_LEN
+1];
660 // try to locate next subtitle
661 if (!fgets (line
, LINE_LEN
, fd
))
663 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
667 current
->start
= a1
*360000+a2
*6000+a3
*100;
670 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
672 if (previous_subrip09_sub
!= NULL
)
673 previous_subrip09_sub
->end
= current
->start
-1;
675 previous_subrip09_sub
= current
;
678 if (!fgets (line
, LINE_LEN
, fd
))
683 current
->text
[0]=""; // just to be sure that string is clear
685 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
686 if (current
->text
[i
]==ERR
) {return ERR
;}
688 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
692 if ((current
->text
[0]=="") && (i
==0)) {
694 previous_sub_end
= 0;
696 // void subtitle -> end of previous marked and exit
697 previous_subrip09_sub
= NULL
;
705 subtitle
*sub_read_line_jacosub(FILE * fd
, subtitle
* current
)
707 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
708 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
709 static unsigned jacoTimeres
= 30;
710 static int jacoShift
= 0;
712 bzero(current
, sizeof(subtitle
));
713 bzero(line1
, LINE_LEN
);
714 bzero(line2
, LINE_LEN
);
715 bzero(directive
, LINE_LEN
);
716 while (!current
->text
[0]) {
717 if (!fgets(line1
, LINE_LEN
, fd
)) {
721 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
722 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
723 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
724 if (line1
[0] == '#') {
725 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
727 unsigned units
= jacoShift
;
728 switch (toupper(line1
[1])) {
730 if (isalpha(line1
[2])) {
735 if (sscanf(&line1
[delta
], "%d", &hours
)) {
740 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
742 (&line1
[delta
], "%*d:%*d:%d",
744 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
748 sscanf(&line1
[delta
], "%d:%d.%d",
749 &minutes
, &seconds
, &units
);
754 sscanf(&line1
[delta
], "%d.%d", &seconds
,
759 ((hours
* 3600 + minutes
* 60 +
760 seconds
) * jacoTimeres
+
765 if (isalpha(line1
[2])) {
770 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
777 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
780 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
786 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
787 jacoShift
) * 100.0 / jacoTimeres
);
790 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
791 jacoShift
) * 100.0 / jacoTimeres
);
795 while ((*p
== ' ') || (*p
== '\t')) {
798 if (isalpha(*p
)||*p
== '[') {
801 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
802 return (subtitle
*) ERR
;
803 jLength
= strlen(directive
);
804 for (cont
= 0; cont
< jLength
; ++cont
) {
805 if (isalpha(*(directive
+ cont
)))
806 *(directive
+ cont
) = toupper(*(directive
+ cont
));
808 if ((strstr(directive
, "RDB") != NULL
)
809 || (strstr(directive
, "RDC") != NULL
)
810 || (strstr(directive
, "RLB") != NULL
)
811 || (strstr(directive
, "RLG") != NULL
)) {
814 if (strstr(directive
, "JL") != NULL
) {
815 current
->alignment
= SUB_ALIGNMENT_HLEFT
;
816 } else if (strstr(directive
, "JR") != NULL
) {
817 current
->alignment
= SUB_ALIGNMENT_HRIGHT
;
819 current
->alignment
= SUB_ALIGNMENT_HCENTER
;
821 strcpy(line2
, line1
);
824 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
832 //the next line to get rid of a blank after the comment
833 if ((*(p
+ 1)) == ' ')
845 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
853 if (*(p
+ 1) == 'n') {
856 current
->text
[current
->lines
++] = strdup(line1
);
860 if ((toupper(*(p
+ 1)) == 'C')
861 || (toupper(*(p
+ 1)) == 'F')) {
865 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
866 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
867 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
871 if ((*(p
+ 1) == '\\') ||
872 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
874 } else if (eol(*(p
+ 1))) {
875 if (!fgets(directive
, LINE_LEN
, fd
))
877 trail_space(directive
);
878 strncat(line2
, directive
,
879 (LINE_LEN
> 511) ? LINE_LEN
: 511);
890 current
->text
[current
->lines
] = strdup(line1
);
896 int sub_autodetect (FILE *fd
, int *uses_time
) {
897 char line
[LINE_LEN
+1];
903 if (!fgets (line
, LINE_LEN
, fd
))
906 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
907 {*uses_time
=0;return SUB_MICRODVD
;}
908 if (sscanf (line
, "{%d}{}", &i
)==1)
909 {*uses_time
=0;return SUB_MICRODVD
;}
910 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
911 {*uses_time
=1;return SUB_SUBRIP
;}
912 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
913 {*uses_time
=1;return SUB_SUBVIEWER
;}
914 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
915 {*uses_time
=1;return SUB_SUBVIEWER2
;}
916 if (strstr (line
, "<SAMI>"))
917 {*uses_time
=1; return SUB_SAMI
;}
918 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
919 {*uses_time
= 1; return SUB_JACOSUB
;}
920 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
921 {*uses_time
= 1; return SUB_JACOSUB
;}
922 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
923 {*uses_time
=1;return SUB_VPLAYER
;}
924 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
925 {*uses_time
=1;return SUB_VPLAYER
;}
926 //TODO: just checking if first line of sub starts with "<" is WAY
927 // too weak test for RT
928 // Please someone who knows the format of RT... FIX IT!!!
929 // It may conflict with other sub formats in the future (actually it doesn't)
931 {*uses_time
=1;return SUB_RT
;}
933 if (!memcmp(line
, "Dialogue: Marked", 16))
934 {*uses_time
=1; return SUB_SSA
;}
935 if (!memcmp(line
, "Dialogue: ", 10))
936 {*uses_time
=1; return SUB_SSA
;}
937 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
938 {*uses_time
=0;return SUB_DUNNOWHAT
;}
939 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
940 {*uses_time
=0; return SUB_MPSUB
;}
941 if (sscanf (line
, "FORMAT=TIM%c", &p
)==1 && p
=='E')
942 {*uses_time
=1; return SUB_MPSUB
;}
943 if (strstr (line
, "-->>"))
944 {*uses_time
=0; return SUB_AQTITLE
;}
945 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
946 {*uses_time
=1;return SUB_SUBRIP09
;}
949 return SUB_INVALID
; // too many bad lines
959 extern float sub_delay
;
960 extern float sub_fps
;
963 static iconv_t icdsc
= (iconv_t
)(-1);
965 void subcp_open (void)
967 char *tocp
= "UTF-8";
970 if ((icdsc
= iconv_open (tocp
, sub_cp
)) != (iconv_t
)(-1)){
971 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
974 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
978 void subcp_close (void)
980 if (icdsc
!= (iconv_t
)(-1)){
981 (void) iconv_close (icdsc
);
982 icdsc
= (iconv_t
)(-1);
983 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
987 #define ICBUFFSIZE 512
988 static char icbuffer
[ICBUFFSIZE
];
990 subtitle
* subcp_recode (subtitle
*sub
)
1000 oleft
= ICBUFFSIZE
- 1;
1002 if (iconv(icdsc
, &ip
, &ileft
,
1003 &op
, &oleft
) == (size_t)(-1)) {
1004 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line (1).\n");
1008 if (!(ot
= (char *)malloc(op
- icbuffer
+ 1))){
1009 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1014 strcpy (ot
, icbuffer
);
1015 free (sub
->text
[l
]);
1019 for (l
= sub
->lines
; l
;)
1020 free (sub
->text
[--l
]);
1027 subtitle
* subcp_recode1 (subtitle
*sub
)
1030 size_t ileft
, oleft
;
1032 if(icdsc
== (iconv_t
)(-1)) return sub
;
1035 char *ip
= icbuffer
;
1036 char *op
= sub
->text
[--l
];
1039 oleft
= ICBUFFSIZE
- 1;
1041 if (iconv(icdsc
, &ip
, &ileft
,
1042 &op
, &oleft
) == (size_t)(-1)) {
1043 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: error recoding line (2).\n");
1054 #define max(a,b) (((a)>(b))?(a):(b))
1056 subtitle
* sub_fribidi (subtitle
*sub
, int sub_utf8
)
1058 FriBidiChar logical
[LINE_LEN
+1], visual
[LINE_LEN
+1]; // Hopefully these two won't smash the stack
1059 char *ip
= NULL
, *op
= NULL
;
1060 FriBidiCharType base
;
1061 size_t len
,orig_len
;
1064 fribidi_boolean log2vis
;
1065 if(flip_hebrew
) { // Please fix the indentation someday
1066 fribidi_set_mirroring (FRIBIDI_TRUE
);
1067 fribidi_set_reorder_nsm (FRIBIDI_FALSE
);
1069 if( sub_utf8
== 0 ) {
1070 char_set_num
= fribidi_parse_charset (fribidi_charset
?fribidi_charset
:"ISO8859-8");
1072 char_set_num
= fribidi_parse_charset ("UTF-8");
1075 ip
= sub
->text
[--l
];
1076 orig_len
= len
= strlen( ip
); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1077 if(len
> LINE_LEN
) {
1078 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: sub->text is longer than LINE_LEN.\n");
1082 len
= fribidi_charset_to_unicode (char_set_num
, ip
, len
, logical
);
1083 base
= FRIBIDI_TYPE_ON
;
1084 log2vis
= fribidi_log2vis (logical
, len
, &base
,
1086 visual
, NULL
, NULL
, NULL
);
1088 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
,
1090 if((op
= (char*)malloc(sizeof(char)*(max(2*orig_len
,2*len
) + 1))) == NULL
) {
1091 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1095 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1101 for (l
= sub
->lines
; l
;)
1102 free (sub
->text
[--l
]);
1111 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1112 int sub_num
, int sub_uses_time
) {
1116 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1117 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1121 if (sub
->end
<= sub
->start
){
1122 sub
->end
= sub
->start
+ subfms
;
1129 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1130 // these subtitles overlap for less than 0.2 seconds
1131 // and would result in very short overlapping subtitle
1132 // so let's fix the problem here, before overlapping code
1133 // get its hands on them
1134 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1135 sub
->end
-= half
+ 1;
1136 nextsub
->start
+= delta
- half
;
1138 if (sub
->end
>= nextsub
->start
){
1139 sub
->end
= nextsub
->start
- 1;
1140 if (sub
->end
- sub
->start
> subfms
)
1141 sub
->end
= sub
->start
+ subfms
;
1148 * Movies are often converted from FILM (24 fps)
1149 * to PAL (25) by simply speeding it up, so we
1150 * to multiply the original timestmaps by
1151 * (Movie's FPS / Subtitle's (guessed) FPS)
1152 * so eg. for 23.98 fps movie and PAL time based
1153 * subtitles we say -subfps 25 and we're fine!
1156 /* timed sub fps correction ::atmos */
1157 if(sub_uses_time
&& sub_fps
) {
1158 sub
->start
*= sub_fps
/fps
;
1159 sub
->end
*= sub_fps
/fps
;
1165 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Adjusted %d subtitle(s).\n", n
);
1169 subtitle
* (*read
)(FILE *fd
,subtitle
*dest
);
1170 void (*post
)(subtitle
*dest
);
1174 sub_data
* sub_read_file (char *filename
, float fps
) {
1175 //filename is assumed to be malloc'ed, free() is used in sub_free()
1177 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1178 subtitle
*first
, *second
, *sub
, *return_sub
;
1179 sub_data
*subt_data
;
1180 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1181 struct subreader sr
[]=
1183 { sub_read_line_microdvd
, NULL
, "microdvd" },
1184 { sub_read_line_subrip
, NULL
, "subrip" },
1185 { sub_read_line_subviewer
, NULL
, "subviewer" },
1186 { sub_read_line_sami
, NULL
, "sami" },
1187 { sub_read_line_vplayer
, NULL
, "vplayer" },
1188 { sub_read_line_rt
, NULL
, "rt" },
1189 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1190 { sub_read_line_dunnowhat
, NULL
, "dunnowhat" },
1191 { sub_read_line_mpsub
, NULL
, "mpsub" },
1192 { sub_read_line_aqt
, NULL
, "aqt" },
1193 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1194 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1195 { sub_read_line_jacosub
, NULL
, "jacosub" }
1197 struct subreader
*srp
;
1199 if(filename
==NULL
) return NULL
; //qnx segfault
1200 fd
=fopen (filename
, "r"); if (!fd
) return NULL
;
1202 sub_format
=sub_autodetect (fd
, &uses_time
);
1203 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1204 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1206 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Detected subtitle file format: %s\n", srp
->name
);
1211 sub_utf8_prev
=sub_utf8
;
1215 if ((l
=strlen(filename
))>4){
1216 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1218 if (!strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1223 if (k
<0) subcp_open();
1228 first
=(subtitle
*)malloc(n_max
*sizeof(subtitle
));
1232 sub_utf8
=sub_utf8_prev
;
1238 sub
= (subtitle
*)malloc(sizeof(subtitle
));
1239 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1240 //as the beginning of the following
1241 previous_sub_end
= 0;
1246 first
=realloc(first
,n_max
*sizeof(subtitle
));
1249 sub
= &first
[sub_num
];
1251 memset(sub
, '\0', sizeof(subtitle
));
1252 sub
=srp
->read(fd
,sub
);
1253 if(!sub
) break; // EOF
1255 if ((sub
!=ERR
) && (sub_utf8
& 2)) sub
=subcp_recode(sub
);
1258 if (sub
!=ERR
) sub
=sub_fribidi(sub
,sub_utf8
);
1265 if ( first
) free(first
);
1268 // Apply any post processing that needs recoding first
1269 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1271 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1272 first
[sub_num
].start
= sub
->start
;
1273 first
[sub_num
].end
= sub
->end
;
1274 first
[sub_num
].lines
= sub
->lines
;
1275 first
[sub_num
].alignment
= sub
->alignment
;
1276 for(i
= 0; i
< sub
->lines
; ++i
){
1277 first
[sub_num
].text
[i
] = sub
->text
[i
];
1279 if (previous_sub_end
){
1280 first
[sub_num
- 1].end
= previous_sub_end
;
1281 previous_sub_end
= 0;
1284 for(j
= sub_num
- 1; j
>= 0; --j
){
1285 first
[j
+ 1].start
= first
[j
].start
;
1286 first
[j
+ 1].end
= first
[j
].end
;
1287 first
[j
+ 1].lines
= first
[j
].lines
;
1288 first
[j
+ 1].alignment
= first
[j
].alignment
;
1289 for(i
= 0; i
< first
[j
].lines
; ++i
){
1290 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1292 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1293 first
[j
].start
= sub
->start
;
1294 first
[j
].end
= sub
->end
;
1295 first
[j
].lines
= sub
->lines
;
1296 first
[j
].alignment
= sub
->alignment
;
1297 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1298 first
[j
].text
[i
] = sub
->text
[i
];
1300 if (previous_sub_end
){
1301 first
[j
].end
= first
[j
- 1].end
;
1302 first
[j
- 1].end
= previous_sub_end
;
1303 previous_sub_end
= 0;
1310 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1319 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1320 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Read %i subtitles", sub_num
);
1321 if (sub_errs
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,", %i bad line(s).\n", sub_errs
);
1322 else mp_msg(MSGT_SUBREADER
,MSGL_INFO
,".\n");
1329 // we do overlap if the user forced it (suboverlap_enable == 2) or
1330 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1331 // this is because usually overlapping subtitles are found in these formats,
1332 // while in others they are probably result of bad timing
1333 if ((suboverlap_enabled
== 2) ||
1334 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1335 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1336 // here we manage overlapping subtitles
1341 // for each subtitle in first[] we deal with its 'block' of
1343 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1344 unsigned long global_start
= first
[sub_first
].start
,
1345 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1346 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1347 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1348 char real_block
= 1;
1350 // here we find the number of subtitles inside the 'block'
1351 // and its span interval. this works well only with sorted
1353 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1355 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1356 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1357 global_start
= first
[sub_first
+ sub_to_add
].start
;
1359 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1360 global_end
= first
[sub_first
+ sub_to_add
].end
;
1364 // we need a structure to keep trace of the screen lines
1365 // used by the subs, a 'placeholder'
1366 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1367 // from a block of sub_to_add+1 subs
1368 placeholder
= (int **) malloc(sizeof(int *) * counter
);
1369 for (i
= 0; i
< counter
; ++i
) {
1370 placeholder
[i
] = (int *) malloc(sizeof(int) * lines_to_add
);
1371 for (j
= 0; j
< lines_to_add
; ++j
) {
1372 placeholder
[i
][j
] = -1;
1377 local_end
= global_start
- 1;
1381 // here we find the beginning and the end of a new
1382 // subtitle in the block
1383 local_start
= local_end
+ 1;
1384 local_end
= global_end
;
1385 for (j
= 0; j
<= sub_to_add
; ++j
) {
1386 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1387 local_end
= first
[sub_first
+ j
].start
- 1;
1388 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1389 local_end
= first
[sub_first
+ j
].end
;
1392 // here we allocate the screen lines to subs we must
1393 // display in current local_start-local_end interval.
1394 // if the subs were yet presents in the previous interval
1395 // they keep the same lines, otherside they get unused lines
1396 for (j
= 0; j
<= sub_to_add
; ++j
) {
1397 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1398 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1401 int fragment_position
= -1;
1403 // if this is not the first new sub of the block
1404 // we find if this sub was present in the previous
1407 for (i
= 0; i
< lines_to_add
; ++i
) {
1408 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1409 placeholder
[counter
][i
] = sub_first
+ j
;
1416 // we are looking for the shortest among all groups of
1417 // sequential blank lines whose length is greater than or
1418 // equal to sub_lines. we store in fragment_position the
1419 // position of the shortest group, in fragment_length its
1420 // length, and in tmp the length of the group currently
1422 for (i
= 0; i
< lines_to_add
; ++i
) {
1423 if (placeholder
[counter
][i
] == -1) {
1424 // placeholder[counter][i] is part of the current group
1428 if (tmp
== sub_lines
) {
1429 // current group's size fits exactly the one we
1430 // need, so we stop looking
1431 fragment_position
= i
- tmp
;
1435 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1436 // current group is the best we found till here,
1437 // but is still bigger than the one we are looking
1438 // for, so we keep on looking
1439 fragment_length
= tmp
;
1440 fragment_position
= i
- tmp
;
1443 // current group doesn't fit at all, so we forget it
1449 // last screen line is blank, a group ends with it
1450 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1451 fragment_position
= i
- tmp
;
1454 if (fragment_position
== -1) {
1455 // it was not possible to find free screen line(s) for a subtitle,
1456 // usually this means a bug in the code; however we do not overlap
1457 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1458 higher_line
= SUB_MAX_TEXT
+ 1;
1461 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1462 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1467 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1468 if (placeholder
[counter
][j
] != -1)
1473 if (higher_line
>= SUB_MAX_TEXT
) {
1474 // the 'block' has too much lines, so we don't overlap the
1476 second
= (subtitle
*) realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1477 for (j
= 0; j
<= sub_to_add
; ++j
) {
1479 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1480 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1481 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1482 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1483 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1484 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1485 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1488 sub_num
+= sub_to_add
+ 1;
1489 sub_first
+= sub_to_add
;
1494 // we read the placeholder structure and create the new
1496 second
= (subtitle
*) realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1497 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1498 second
[sub_num
].start
= local_start
;
1499 second
[sub_num
].end
= local_end
;
1500 second
[sub_num
].alignment
= SUB_ALIGNMENT_HCENTER
;
1501 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1502 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1503 if (placeholder
[counter
][j
] != -1) {
1504 int lines
= first
[placeholder
[counter
][j
]].lines
;
1505 for (ls
= 0; ls
< lines
; ++ls
) {
1506 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1510 second
[sub_num
].text
[i
++] = strdup(" ");
1515 } while (local_end
< global_end
);
1517 for (i
= 0; i
< counter
; ++i
)
1518 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1520 counter
= 2 * sub_to_add
+ 1;
1521 for (i
= 0; i
< counter
; ++i
) {
1522 free(placeholder
[i
]);
1525 sub_first
+= sub_to_add
;
1528 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1529 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1530 free(first
[j
].text
[i
]);
1535 return_sub
= second
;
1536 } else { //if(suboverlap_enabled)
1537 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1540 if (return_sub
== NULL
) return NULL
;
1541 subt_data
= (sub_data
*)malloc(sizeof(sub_data
));
1542 subt_data
->filename
= filename
;
1543 subt_data
->sub_uses_time
= uses_time
;
1544 subt_data
->sub_num
= sub_num
;
1545 subt_data
->sub_errs
= sub_errs
;
1546 subt_data
->subtitles
= return_sub
;
1551 char * strreplace( char * in
,char * what
,char * whereof
)
1556 if ( ( in
== NULL
)||( what
== NULL
)||( whereof
== NULL
)||( ( tmp
=strstr( in
,what
) ) == NULL
) ) return NULL
;
1557 for( i
=0;i
<strlen( whereof
);i
++ ) tmp
[i
]=whereof
[i
];
1558 if ( strlen( what
) > strlen( whereof
) ) tmp
[i
]=0;
1564 static void strcpy_trim(char *d
, char *s
)
1566 // skip leading whitespace
1567 while (*s
&& !isalnum(*s
)) {
1572 while (*s
&& isalnum(*s
)) {
1577 // trim excess whitespace
1578 while (*s
&& !isalnum(*s
)) {
1587 static void strcpy_strip_ext(char *d
, char *s
)
1589 char *tmp
= strrchr(s
,'.');
1594 strncpy(d
, s
, tmp
-s
);
1603 static void strcpy_get_ext(char *d
, char *s
)
1605 char *tmp
= strrchr(s
,'.');
1614 static int whiteonly(char *s
)
1617 if (isalnum(*s
)) return 0;
1623 typedef struct _subfn
1629 static int compare_sub_priority(const void *a
, const void *b
)
1631 if (((subfn
*)a
)->priority
> ((subfn
*)b
)->priority
) {
1633 } else if (((subfn
*)a
)->priority
< ((subfn
*)b
)->priority
) {
1636 return strcoll(((subfn
*)a
)->fname
, ((subfn
*)b
)->fname
);
1640 char** sub_filenames(char* path
, char *fname
)
1642 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
1643 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
1645 int len
, pos
, found
, i
, j
;
1646 char * sub_exts
[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
1657 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
1658 +(strlen(path
) > 256 ? strlen(path
) : 256)+2;
1660 f_dir
= (char*)malloc(len
);
1661 f_fname
= (char*)malloc(len
);
1662 f_fname_noext
= (char*)malloc(len
);
1663 f_fname_trim
= (char*)malloc(len
);
1665 tmp_fname_noext
= (char*)malloc(len
);
1666 tmp_fname_trim
= (char*)malloc(len
);
1667 tmp_fname_ext
= (char*)malloc(len
);
1669 tmpresult
= (char*)malloc(len
);
1671 result
= (subfn
*)malloc(sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1672 memset(result
, 0, sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1676 tmp
= strrchr(fname
,'/');
1678 if(!tmp
)tmp
= strrchr(fname
,'\\');
1681 // extract filename & dirname from fname
1683 strcpy(f_fname
, tmp
+1);
1685 strncpy(f_dir
, fname
, pos
+1);
1688 strcpy(f_fname
, fname
);
1689 strcpy(f_dir
, "./");
1692 strcpy_strip_ext(f_fname_noext
, f_fname
);
1693 strcpy_trim(f_fname_trim
, f_fname_noext
);
1696 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
1697 tmp_sub_id
= (char*)malloc(strlen(dvdsub_lang
)+1);
1698 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
1702 // 1 = any subtitle file
1703 // 2 = any sub file containing movie name
1704 // 3 = sub file containing movie name and the lang extension
1705 for (j
= 0; j
<= 1; j
++) {
1706 d
= opendir(j
== 0 ? f_dir
: path
);
1708 while ((de
= readdir(d
))) {
1709 // retrieve various parts of the filename
1710 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
1711 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
1712 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
1714 // does it end with a subtitle extension?
1717 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
1719 for (i
= 0; sub_exts
[i
]; i
++) {
1721 if (strcmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
1727 // we have a (likely) subtitle file
1730 if (!prio
&& tmp_sub_id
)
1732 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
1733 printf("dvdsublang...%s\n", tmpresult
);
1734 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
1735 // matches the movie name + lang extension
1739 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
1740 // matches the movie name
1743 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && (sub_match_fuzziness
>= 1)) {
1744 // contains the movie name
1745 tmp
+= strlen(f_fname_trim
);
1746 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
1747 // with sub_id specified prefer localized subtitles
1749 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
1750 // without sub_id prefer "plain" name
1753 // with no localized subs found, try any else instead
1758 // doesn't contain the movie name
1759 // don't try in the mplayer subtitle directory
1760 if ((j
== 0) && (sub_match_fuzziness
>= 2)) {
1768 if (i
<3){ // prefer UTF-8 coded
1772 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
1773 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1774 if ((f
= fopen(tmpresult
, "rt"))) {
1776 result
[subcnt
].priority
= prio
;
1777 result
[subcnt
].fname
= strdup(tmpresult
);
1783 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
1790 if (tmp_sub_id
) free(tmp_sub_id
);
1794 free(f_fname_noext
);
1797 free(tmp_fname_noext
);
1798 free(tmp_fname_trim
);
1799 free(tmp_fname_ext
);
1803 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
1805 result2
= (char**)malloc(sizeof(char*)*(subcnt
+1));
1806 memset(result2
, 0, sizeof(char*)*(subcnt
+1));
1808 for (i
= 0; i
< subcnt
; i
++) {
1809 result2
[i
] = result
[i
].fname
;
1811 result2
[subcnt
] = NULL
;
1818 void list_sub_file(sub_data
* subd
){
1820 subtitle
*subs
= subd
->subtitles
;
1822 for(j
=0; j
< subd
->sub_num
; j
++){
1823 subtitle
* egysub
=&subs
[j
];
1824 printf ("%i line%c (%li-%li)\n",
1826 (1==egysub
->lines
)?' ':'s',
1829 for (i
=0; i
<egysub
->lines
; i
++) {
1830 printf ("\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
1835 printf ("Subtitle format %s time.\n",
1836 subd
->sub_uses_time
? "uses":"doesn't use");
1837 printf ("Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
1840 void dump_srt(sub_data
* subd
, float fps
){
1846 subtitle
*subs
= subd
->subtitles
;
1848 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1850 fd
=fopen("dumpsub.srt","w");
1853 perror("dump_srt: fopen");
1856 for(i
=0; i
< subd
->sub_num
; i
++)
1858 onesub
=subs
+i
; //=&subs[i];
1859 fprintf(fd
,"%d\n",i
+1);//line number
1862 if (!subd
->sub_uses_time
)
1863 temp
= temp
* 100 / sub_fps
;
1864 temp
-= sub_delay
* 100;
1865 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
1866 m
=temp
/6000; temp
%=6000; //m =1*100*60
1867 s
=temp
/100; temp
%=100; //s =1*100
1868 ms
=temp
*10; //ms=1*10
1869 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
1872 if (!subd
->sub_uses_time
)
1873 temp
= temp
* 100 / sub_fps
;
1874 temp
-= sub_delay
* 100;
1875 h
=temp
/360000;temp
%=360000;
1876 m
=temp
/6000; temp
%=6000;
1877 s
=temp
/100; temp
%=100;
1879 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
1881 for(j
=0;j
<onesub
->lines
;j
++)
1882 fprintf(fd
,"%s\n",onesub
->text
[j
]);
1887 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
1890 void dump_mpsub(sub_data
* subd
, float fps
){
1894 subtitle
*subs
= subd
->subtitles
;
1896 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
1897 if (sub_fps
==0) sub_fps
=fps
;
1899 fd
=fopen ("dump.mpsub", "w");
1901 perror ("dump_mpsub: fopen");
1906 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
1907 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
1909 for(j
=0; j
< subd
->sub_num
; j
++){
1910 subtitle
* egysub
=&subs
[j
];
1911 if (subd
->sub_uses_time
) {
1912 a
=((egysub
->start
-mpsub_position
)/100.0);
1913 b
=((egysub
->end
-egysub
->start
)/100.0);
1914 if ( (float)((int)a
) == a
)
1915 fprintf (fd
, "%.0f",a
);
1917 fprintf (fd
, "%.2f",a
);
1919 if ( (float)((int)b
) == b
)
1920 fprintf (fd
, " %.0f\n",b
);
1922 fprintf (fd
, " %.2f\n",b
);
1924 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
1925 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
1928 mpsub_position
= egysub
->end
;
1929 for (i
=0; i
<egysub
->lines
; i
++) {
1930 fprintf (fd
, "%s\n",egysub
->text
[i
]);
1935 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
1938 void dump_microdvd(sub_data
* subd
, float fps
) {
1941 subtitle
*subs
= subd
->subtitles
;
1944 fd
= fopen("dumpsub.txt", "w");
1946 perror("dumpsub.txt: fopen");
1949 delay
= sub_delay
* sub_fps
;
1950 for (i
= 0; i
< subd
->sub_num
; ++i
) {
1952 start
= subs
[i
].start
;
1954 if (subd
->sub_uses_time
) {
1955 start
= start
* sub_fps
/ 100 ;
1956 end
= end
* sub_fps
/ 100;
1959 start
= start
* sub_fps
/ fps
;
1960 end
= end
* sub_fps
/ fps
;
1964 fprintf(fd
, "{%d}{%d}", start
, end
);
1965 for (j
= 0; j
< subs
[i
].lines
; ++j
)
1966 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
1970 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
1973 void dump_jacosub(sub_data
* subd
, float fps
) {
1979 subtitle
*subs
= subd
->subtitles
;
1981 if (!subd
->sub_uses_time
&& sub_fps
== 0)
1983 fd
=fopen("dumpsub.jss","w");
1986 perror("dump_jacosub: fopen");
1989 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
1990 for(i
=0; i
< subd
->sub_num
; i
++)
1992 onesub
=subs
+i
; //=&subs[i];
1995 if (!subd
->sub_uses_time
)
1996 temp
= temp
* 100 / sub_fps
;
1997 temp
-= sub_delay
* 100;
1998 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
1999 m
=temp
/6000; temp
%=6000; //m =1*100*60
2000 s
=temp
/100; temp
%=100; //s =1*100
2002 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2005 if (!subd
->sub_uses_time
)
2006 temp
= temp
* 100 / sub_fps
;
2007 temp
-= sub_delay
* 100;
2008 h
=temp
/360000;temp
%=360000;
2009 m
=temp
/6000; temp
%=6000;
2010 s
=temp
/100; temp
%=100;
2012 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2014 for(j
=0;j
<onesub
->lines
;j
++)
2015 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2020 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2023 void dump_sami(sub_data
* subd
, float fps
) {
2028 subtitle
*subs
= subd
->subtitles
;
2030 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2032 fd
=fopen("dumpsub.smi","w");
2035 perror("dump_jacosub: fopen");
2038 fprintf(fd
, "<SAMI>\n"
2040 " <STYLE TYPE=\"Text/css\">\n"
2042 " 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"
2043 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2048 for(i
=0; i
< subd
->sub_num
; i
++)
2050 onesub
=subs
+i
; //=&subs[i];
2053 if (!subd
->sub_uses_time
)
2054 temp
= temp
* 100 / sub_fps
;
2055 temp
-= sub_delay
* 100;
2056 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2057 "\t <P>", temp
* 10);
2059 for(j
=0;j
<onesub
->lines
;j
++)
2060 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2065 if (!subd
->sub_uses_time
)
2066 temp
= temp
* 100 / sub_fps
;
2067 temp
-= sub_delay
* 100;
2068 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2069 "\t <P> \n", temp
* 10);
2071 fprintf(fd
, "</BODY>\n"
2074 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2077 void sub_free( sub_data
* subd
)
2081 if ( !subd
) return;
2083 if (subd
->subtitles
) {
2084 for (i
=0; i
< subd
->subtitles
->lines
; i
++) free( subd
->subtitles
->text
[i
] );
2085 free( subd
->subtitles
);
2087 if (subd
->filename
) free( subd
->filename
);
2092 int main(int argc
, char **argv
) { // for testing
2096 printf("\nUsage: subreader filename.sub\n\n");
2100 subd
= sub_read_file(argv
[1]);
2102 printf("Couldn't load file.\n");
2106 list_sub_file(subd
);