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
; ///character set that will be passed to FriBiDi
35 int flip_hebrew
= 1; ///flip subtitles using fribidi
36 int fribidi_flip_commas
= 0; ///flip comma when fribidi is used
39 extern char* dvdsub_lang
;
41 /* Maximal length of line of a subtitle */
43 static float mpsub_position
=0;
44 static float mpsub_multiplier
=1.;
45 static int sub_slacktime
= 20000; //20 sec
47 int sub_no_text_pp
=0; // 1 => do not apply text post-processing
48 // like {\...} elimination in SSA format.
50 int sub_match_fuzziness
=0; // level of sub name matching fuzziness
52 /* Use the SUB_* constant defined in the header file */
53 int sub_format
=SUB_INVALID
;
56 Some subtitling formats, namely AQT and Subrip09, define the end of a
57 subtitle as the beginning of the following. Since currently we read one
58 subtitle at time, for these format we keep two global *subtitle,
59 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
60 so we can change its end when we read current subtitle starting time.
61 When USE_SORTSUB is defined, we use a single global unsigned long,
62 previous_sub_end, for both (and even future) formats, to store the end of
63 the previous sub: it is initialized to 0 in sub_read_file and eventually
64 modified by sub_read_aqt_line or sub_read_subrip09_line.
66 unsigned long previous_sub_end
;
69 static int eol(char p
) {
70 return (p
=='\r' || p
=='\n' || p
=='\0');
73 /* Remove leading and trailing space */
74 static void trail_space(char *s
) {
76 while (isspace(s
[i
])) ++i
;
77 if (i
) strcpy(s
, s
+ i
);
79 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
82 static char *stristr(const char *haystack
, const char *needle
) {
84 const char *p
= haystack
;
86 if (!(haystack
&& needle
)) return NULL
;
90 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
97 subtitle
*sub_read_line_sami(FILE *fd
, subtitle
*current
) {
98 static char line
[LINE_LEN
+1];
99 static char *s
= NULL
, *slacktime_s
;
100 char text
[LINE_LEN
+1], *p
=NULL
, *q
;
103 current
->lines
= current
->start
= current
->end
= 0;
104 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
107 /* read the first line */
109 if (!(s
= fgets(line
, LINE_LEN
, fd
))) return 0;
114 case 0: /* find "START=" or "Slacktime:" */
115 slacktime_s
= stristr (s
, "Slacktime:");
117 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
119 s
= stristr (s
, "Start=");
121 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
123 for (; *s
!= '>' && *s
!= '\0'; s
++);
129 case 1: /* find (optionnal) "<P", skip other TAGs */
130 for (; *s
== ' ' || *s
== '\t'; s
++); /* strip blanks, if any */
131 if (*s
== '\0') break;
132 if (*s
!= '<') { state
= 3; p
= text
; continue; } /* not a TAG */
134 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
135 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
141 case 2: /* find ">" */
142 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
145 case 3: /* get all text until '<' appears */
146 if (*s
== '\0') break;
147 else if (!strncasecmp (s
, "<br>", 4)) {
148 *p
= '\0'; p
= text
; trail_space (text
);
150 current
->text
[current
->lines
++] = strdup (text
);
153 else if ((*s
== '{') && !sub_no_text_pp
) { state
= 5; ++s
; continue; }
154 else if (*s
== '<') { state
= 4; }
155 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
156 else if (*s
== '\t') { *p
++ = ' '; s
++; }
157 else if (*s
== '\r' || *s
== '\n') { s
++; }
160 /* skip duplicated space */
161 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
165 case 4: /* get current->end or skip <TAG> */
166 q
= stristr (s
, "Start=");
168 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
169 *p
= '\0'; trail_space (text
);
171 current
->text
[current
->lines
++] = strdup (text
);
172 if (current
->lines
> 0) { state
= 99; break; }
176 if (s
) { s
++; state
= 3; continue; }
178 case 5: /* get rid of {...} text, but read the alignment code */
179 if ((*s
== '\\') && (*(s
+ 1) == 'a') && !sub_no_text_pp
) {
180 if (stristr(s
, "\\a1") != NULL
) {
181 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
184 if (stristr(s
, "\\a2") != NULL
) {
185 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
187 } else if (stristr(s
, "\\a3") != NULL
) {
188 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
190 } else if ((stristr(s
, "\\a4") != NULL
) || (stristr(s
, "\\a5") != NULL
) || (stristr(s
, "\\a8") != NULL
)) {
191 current
->alignment
= SUB_ALIGNMENT_TOPLEFT
;
193 } else if (stristr(s
, "\\a6") != NULL
) {
194 current
->alignment
= SUB_ALIGNMENT_TOPCENTER
;
196 } else if (stristr(s
, "\\a7") != NULL
) {
197 current
->alignment
= SUB_ALIGNMENT_TOPRIGHT
;
199 } else if (stristr(s
, "\\a9") != NULL
) {
200 current
->alignment
= SUB_ALIGNMENT_MIDDLELEFT
;
202 } else if (stristr(s
, "\\a10") != NULL
) {
203 current
->alignment
= SUB_ALIGNMENT_MIDDLECENTER
;
205 } else if (stristr(s
, "\\a11") != NULL
) {
206 current
->alignment
= SUB_ALIGNMENT_MIDDLERIGHT
;
210 if (*s
== '}') state
= 3;
216 if (state
!= 99 && !(s
= fgets (line
, LINE_LEN
, fd
))) {
217 if (current
->start
> 0) {
218 break; // if it is the last subtitle
224 } while (state
!= 99);
226 // For the last subtitle
227 if (current
->end
<= 0) {
228 current
->end
= current
->start
+ sub_slacktime
;
229 *p
= '\0'; trail_space (text
);
231 current
->text
[current
->lines
++] = strdup (text
);
238 char *sub_readtext(char *source
, char **dest
) {
242 // printf("src=%p dest=%p \n",source,dest);
244 while ( !eol(*p
) && *p
!= '|' ) {
248 *dest
= (char *)malloc (len
+1);
249 if (!dest
) {return ERR
;}
251 strncpy(*dest
, source
, len
);
254 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
256 if (*p
) return p
; // not-last text field
257 else return NULL
; // last text field
260 subtitle
*sub_read_line_microdvd(FILE *fd
,subtitle
*current
) {
261 char line
[LINE_LEN
+1];
262 char line2
[LINE_LEN
+1];
267 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
268 } while ((sscanf (line
,
270 &(current
->start
), line2
) < 2) &&
272 "{%ld}{%ld}%[^\r\n]",
273 &(current
->start
), &(current
->end
), line2
) < 3));
278 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
279 if (current
->text
[i
]==ERR
) {return ERR
;}
281 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
288 subtitle
*sub_read_line_mpl2(FILE *fd
,subtitle
*current
) {
289 char line
[LINE_LEN
+1];
290 char line2
[LINE_LEN
+1];
295 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
296 } while ((sscanf (line
,
297 "[%ld][%ld]%[^\r\n]",
298 &(current
->start
), &(current
->end
), line2
) < 3));
299 current
->start
*= 10;
304 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
305 if (current
->text
[i
]==ERR
) {return ERR
;}
307 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
314 subtitle
*sub_read_line_subrip(FILE *fd
, subtitle
*current
) {
315 char line
[LINE_LEN
+1];
316 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
317 char *p
=NULL
, *q
=NULL
;
321 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
322 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
323 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
324 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
326 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
329 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
330 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
331 current
->text
[current
->lines
-1]=(char *)malloc (len
+1);
332 if (!current
->text
[current
->lines
-1]) return ERR
;
333 strncpy (current
->text
[current
->lines
-1], q
, len
);
334 current
->text
[current
->lines
-1][len
]='\0';
335 if (!*p
|| *p
=='\r' || *p
=='\n') break;
337 else while (*p
++!=']');
344 subtitle
*sub_read_line_subviewer(FILE *fd
,subtitle
*current
) {
345 char line
[LINE_LEN
+1];
346 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
350 while (!current
->text
[0]) {
351 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
352 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)
354 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
355 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
356 for (i
=0; i
<SUB_MAX_TEXT
;) {
357 if (!fgets (line
, LINE_LEN
, fd
)) break;
359 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++);
362 char *curptr
=current
->text
[i
]=(char *)malloc (len
+1);
363 if (!current
->text
[i
]) return ERR
;
364 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
366 /* let's filter html tags ::atmos */
393 subtitle
*sub_read_line_subviewer2(FILE *fd
,subtitle
*current
) {
394 char line
[LINE_LEN
+1];
399 while (!current
->text
[0]) {
400 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
403 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
405 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
406 for (i
=0; i
<SUB_MAX_TEXT
;) {
407 if (!fgets (line
, LINE_LEN
, fd
)) break;
408 if (line
[0]=='}') break;
410 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
412 current
->text
[i
]=(char *)malloc (len
+1);
413 if (!current
->text
[i
]) return ERR
;
414 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
426 subtitle
*sub_read_line_vplayer(FILE *fd
,subtitle
*current
) {
427 char line
[LINE_LEN
+1];
429 char *p
=NULL
, *next
,separator
;
432 while (!current
->text
[0]) {
433 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
434 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
437 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
441 // finds the body of the subtitle
448 printf("SUB: Skipping incorrect subtitle line!\n");
452 // by wodzu: hey! this time we know what length it has! what is
453 // that magic for? it can't deal with space instead of third
454 // colon! look, what simple it can be:
461 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
462 if (current
->text
[i
]==ERR
) {return ERR
;}
464 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
472 subtitle
*sub_read_line_rt(FILE *fd
,subtitle
*current
) {
473 //TODO: This format uses quite rich (sub/super)set of xhtml
474 // I couldn't check it since DTD is not included.
475 // WARNING: full XML parses can be required for proper parsing
476 char line
[LINE_LEN
+1];
477 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
478 char *p
=NULL
,*next
=NULL
;
481 while (!current
->text
[0]) {
482 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
483 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
484 //to describe the same moment in time. Maybe there are even more formats in use.
485 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
486 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
488 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
489 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
490 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
491 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
492 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
493 ((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) &&
494 ((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) &&
495 //now try it without end time
496 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
497 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
498 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
499 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
502 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
503 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
504 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
505 current
->end
= current
->start
+200;
507 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
508 next
= strstr(line
,"<clear/>");
509 if(next
&& strlen(next
)>8){
511 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
512 if (current
->text
[i
]==ERR
) {return ERR
;}
514 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
522 subtitle
*sub_read_line_ssa(FILE *fd
,subtitle
*current
) {
524 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
525 * other Sub Station Alpha scripts have only 8 commas before subtitle
526 * Reading the "ScriptType:" field is not reliable since many scripts appear
529 * http://www.scriptclub.org is a good place to find more examples
530 * http://www.eswat.demon.co.uk is where the SSA specs can be found
533 static int max_comma
= 32; /* let's use 32 for the case that the */
534 /* amount of commas increase with newer SSA versions */
536 int hour1
, min1
, sec1
, hunsec1
,
537 hour2
, min2
, sec2
, hunsec2
, nothing
;
540 char line
[LINE_LEN
+1],
546 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
547 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
548 "%[^\n\r]", ¬hing
,
549 &hour1
, &min1
, &sec1
, &hunsec1
,
550 &hour2
, &min2
, &sec2
, &hunsec2
,
553 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
554 "%[^\n\r]", ¬hing
,
555 &hour1
, &min1
, &sec1
, &hunsec1
,
556 &hour2
, &min2
, &sec2
, &hunsec2
,
559 line2
=strchr(line3
, ',');
561 for (comma
= 4; comma
< max_comma
; comma
++)
564 if(!(tmp
=strchr(++tmp
, ','))) break;
565 if(*(++tmp
) == ' ') break;
566 /* a space after a comma means we're already in a sentence */
570 if(comma
< max_comma
)max_comma
= comma
;
571 /* eliminate the trailing comma */
572 if(*line2
== ',') line2
++;
574 current
->lines
=0;num
=0;
575 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
576 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
578 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
579 current
->text
[num
]=(char *)malloc(tmp
-line2
+1);
580 strncpy (current
->text
[num
], line2
, tmp
-line2
);
581 current
->text
[num
][tmp
-line2
]='\0';
585 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
588 current
->text
[num
]=strdup(line2
);
594 void sub_pp_ssa(subtitle
*sub
) {
599 /* eliminate any text enclosed with {}, they are font and color settings */
600 so
=de
=sub
->text
[--l
];
602 if(*so
== '{' && so
[1]=='\\') {
603 for (start
=so
; *so
&& *so
!='}'; so
++);
604 if(*so
) so
++; else so
=start
;
616 * PJS subtitles reader.
617 * That's the "Phoenix Japanimation Society" format.
618 * I found some of them in http://www.scriptsclub.org/ (used for anime).
619 * The time is in tenths of second.
621 * by set, based on code by szabi (dunnowhat sub format ;-)
623 subtitle
*sub_read_line_pjs(FILE *fd
,subtitle
*current
) {
624 char line
[LINE_LEN
+1];
625 char text
[LINE_LEN
+1], *s
, *d
;
627 if (!fgets (line
, LINE_LEN
, fd
))
630 for (s
=line
; *s
&& isspace(*s
); s
++);
631 /* allow empty lines at the end of the file */
635 if (sscanf (s
, "%ld,%ld,", &(current
->start
),
636 &(current
->end
)) <2) {
639 /* the files I have are in tenths of second */
640 current
->start
*= 10;
642 /* walk to the beggining of the string */
643 for (; *s
; s
++) if (*s
==',') break;
645 for (s
++; *s
; s
++) if (*s
==',') break;
651 /* copy the string to the text buffer */
652 for (s
++, d
=text
; *s
&& *s
!='"'; s
++, d
++)
655 current
->text
[0] = strdup(text
);
661 subtitle
*sub_read_line_mpsub(FILE *fd
, subtitle
*current
) {
662 char line
[LINE_LEN
+1];
669 if (!fgets(line
, LINE_LEN
, fd
)) return NULL
;
670 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
672 mpsub_position
+= a
*mpsub_multiplier
;
673 current
->start
=(int) mpsub_position
;
674 mpsub_position
+= b
*mpsub_multiplier
;
675 current
->end
=(int) mpsub_position
;
677 while (num
< SUB_MAX_TEXT
) {
678 if (!fgets (line
, LINE_LEN
, fd
)) {
679 if (num
== 0) return NULL
;
683 while (isspace(*p
)) p
++;
684 if (eol(*p
) && num
> 0) return current
;
685 if (eol(*p
)) return NULL
;
687 for (q
=p
; !eol(*q
); q
++);
690 current
->text
[num
]=strdup(p
);
691 // printf (">%s<\n",p);
692 current
->lines
= ++num
;
694 if (num
) return current
;
698 return NULL
; // we should have returned before if it's OK
702 //we don't need this if we use previous_sub_end
703 subtitle
*previous_aqt_sub
= NULL
;
706 subtitle
*sub_read_line_aqt(FILE *fd
,subtitle
*current
) {
707 char line
[LINE_LEN
+1];
712 // try to locate next subtitle
713 if (!fgets (line
, LINE_LEN
, fd
))
715 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
720 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
722 if (previous_aqt_sub
!= NULL
)
723 previous_aqt_sub
->end
= current
->start
-1;
725 previous_aqt_sub
= current
;
728 if (!fgets (line
, LINE_LEN
, fd
))
731 sub_readtext((char *) &line
,¤t
->text
[0]);
733 current
->end
= current
->start
; // will be corrected by next subtitle
735 if (!fgets (line
, LINE_LEN
, fd
))
739 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
740 if (current
->text
[i
]==ERR
) {return ERR
;}
742 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
746 if ((current
->text
[0]=="") && (current
->text
[1]=="")) {
748 previous_sub_end
= 0;
750 // void subtitle -> end of previous marked and exit
751 previous_aqt_sub
= NULL
;
760 subtitle
*previous_subrip09_sub
= NULL
;
763 subtitle
*sub_read_line_subrip09(FILE *fd
,subtitle
*current
) {
764 char line
[LINE_LEN
+1];
770 // try to locate next subtitle
771 if (!fgets (line
, LINE_LEN
, fd
))
773 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
777 current
->start
= a1
*360000+a2
*6000+a3
*100;
780 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
782 if (previous_subrip09_sub
!= NULL
)
783 previous_subrip09_sub
->end
= current
->start
-1;
785 previous_subrip09_sub
= current
;
788 if (!fgets (line
, LINE_LEN
, fd
))
793 current
->text
[0]=""; // just to be sure that string is clear
795 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
796 if (current
->text
[i
]==ERR
) {return ERR
;}
798 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
802 if ((current
->text
[0]=="") && (i
==0)) {
804 previous_sub_end
= 0;
806 // void subtitle -> end of previous marked and exit
807 previous_subrip09_sub
= NULL
;
815 subtitle
*sub_read_line_jacosub(FILE * fd
, subtitle
* current
)
817 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
818 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
819 static unsigned jacoTimeres
= 30;
820 static int jacoShift
= 0;
822 memset(current
, 0, sizeof(subtitle
));
823 memset(line1
, 0, LINE_LEN
);
824 memset(line2
, 0, LINE_LEN
);
825 memset(directive
, 0, LINE_LEN
);
826 while (!current
->text
[0]) {
827 if (!fgets(line1
, LINE_LEN
, fd
)) {
831 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
832 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
833 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
834 if (line1
[0] == '#') {
835 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
837 unsigned units
= jacoShift
;
838 switch (toupper(line1
[1])) {
840 if (isalpha(line1
[2])) {
845 if (sscanf(&line1
[delta
], "%d", &hours
)) {
850 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
852 (&line1
[delta
], "%*d:%*d:%d",
854 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
858 sscanf(&line1
[delta
], "%d:%d.%d",
859 &minutes
, &seconds
, &units
);
864 sscanf(&line1
[delta
], "%d.%d", &seconds
,
869 ((hours
* 3600 + minutes
* 60 +
870 seconds
) * jacoTimeres
+
875 if (isalpha(line1
[2])) {
880 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
887 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
890 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
896 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
897 jacoShift
) * 100.0 / jacoTimeres
);
900 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
901 jacoShift
) * 100.0 / jacoTimeres
);
905 while ((*p
== ' ') || (*p
== '\t')) {
908 if (isalpha(*p
)||*p
== '[') {
911 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
912 return (subtitle
*) ERR
;
913 jLength
= strlen(directive
);
914 for (cont
= 0; cont
< jLength
; ++cont
) {
915 if (isalpha(*(directive
+ cont
)))
916 *(directive
+ cont
) = toupper(*(directive
+ cont
));
918 if ((strstr(directive
, "RDB") != NULL
)
919 || (strstr(directive
, "RDC") != NULL
)
920 || (strstr(directive
, "RLB") != NULL
)
921 || (strstr(directive
, "RLG") != NULL
)) {
924 if (strstr(directive
, "JL") != NULL
) {
925 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
926 } else if (strstr(directive
, "JR") != NULL
) {
927 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
929 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
931 strcpy(line2
, line1
);
934 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
942 //the next line to get rid of a blank after the comment
943 if ((*(p
+ 1)) == ' ')
955 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
963 if (*(p
+ 1) == 'n') {
966 current
->text
[current
->lines
++] = strdup(line1
);
970 if ((toupper(*(p
+ 1)) == 'C')
971 || (toupper(*(p
+ 1)) == 'F')) {
975 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
976 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
977 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
981 if ((*(p
+ 1) == '\\') ||
982 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
984 } else if (eol(*(p
+ 1))) {
985 if (!fgets(directive
, LINE_LEN
, fd
))
987 trail_space(directive
);
988 strncat(line2
, directive
,
989 (LINE_LEN
> 511) ? LINE_LEN
: 511);
1000 current
->text
[current
->lines
] = strdup(line1
);
1006 int sub_autodetect (FILE *fd
, int *uses_time
) {
1007 char line
[LINE_LEN
+1];
1013 if (!fgets (line
, LINE_LEN
, fd
))
1016 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
1017 {*uses_time
=0;return SUB_MICRODVD
;}
1018 if (sscanf (line
, "{%d}{}", &i
)==1)
1019 {*uses_time
=0;return SUB_MICRODVD
;}
1020 if (sscanf (line
, "[%d][%d]", &i
, &i
)==2)
1021 {*uses_time
=1;return SUB_MPL2
;}
1022 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
1023 {*uses_time
=1;return SUB_SUBRIP
;}
1024 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
1025 {*uses_time
=1;return SUB_SUBVIEWER
;}
1026 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
1027 {*uses_time
=1;return SUB_SUBVIEWER2
;}
1028 if (strstr (line
, "<SAMI>"))
1029 {*uses_time
=1; return SUB_SAMI
;}
1030 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
1031 {*uses_time
= 1; return SUB_JACOSUB
;}
1032 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
1033 {*uses_time
= 1; return SUB_JACOSUB
;}
1034 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
1035 {*uses_time
=1;return SUB_VPLAYER
;}
1036 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
1037 {*uses_time
=1;return SUB_VPLAYER
;}
1038 //TODO: just checking if first line of sub starts with "<" is WAY
1039 // too weak test for RT
1040 // Please someone who knows the format of RT... FIX IT!!!
1041 // It may conflict with other sub formats in the future (actually it doesn't)
1043 {*uses_time
=1;return SUB_RT
;}
1045 if (!memcmp(line
, "Dialogue: Marked", 16))
1046 {*uses_time
=1; return SUB_SSA
;}
1047 if (!memcmp(line
, "Dialogue: ", 10))
1048 {*uses_time
=1; return SUB_SSA
;}
1049 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
1050 {*uses_time
=1;return SUB_PJS
;}
1051 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
1052 {*uses_time
=0; return SUB_MPSUB
;}
1053 if (sscanf (line
, "FORMAT=TIM%c", &p
)==1 && p
=='E')
1054 {*uses_time
=1; return SUB_MPSUB
;}
1055 if (strstr (line
, "-->>"))
1056 {*uses_time
=0; return SUB_AQTITLE
;}
1057 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
1058 {*uses_time
=1;return SUB_SUBRIP09
;}
1061 return SUB_INVALID
; // too many bad lines
1067 extern int sub_utf8
;
1068 int sub_utf8_prev
=0;
1071 extern float sub_delay
;
1072 extern float sub_fps
;
1075 static iconv_t icdsc
= (iconv_t
)(-1);
1077 void subcp_open (FILE *enca_fd
)
1079 char *tocp
= "UTF-8";
1082 char *cp_tmp
= sub_cp
;
1084 char enca_lang
[3], enca_fallback
[100];
1085 int free_cp_tmp
= 0;
1086 if (sscanf(sub_cp
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
1087 || sscanf(sub_cp
, "ENCA:%2s:%99s", enca_lang
, enca_fallback
) == 2) {
1089 cp_tmp
= guess_cp(enca_fd
, enca_lang
, enca_fallback
);
1092 cp_tmp
= enca_fallback
;
1096 if ((icdsc
= iconv_open (tocp
, cp_tmp
)) != (iconv_t
)(-1)){
1097 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1100 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1102 if (free_cp_tmp
&& cp_tmp
) free(cp_tmp
);
1107 void subcp_close (void)
1109 if (icdsc
!= (iconv_t
)(-1)){
1110 (void) iconv_close (icdsc
);
1111 icdsc
= (iconv_t
)(-1);
1112 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
1116 #define ICBUFFSIZE 512
1117 static char icbuffer
[ICBUFFSIZE
];
1119 subtitle
* subcp_recode (subtitle
*sub
)
1122 size_t ileft
, oleft
;
1127 ip
= sub
->text
[--l
];
1129 oleft
= ICBUFFSIZE
- 1;
1131 if (iconv(icdsc
, &ip
, &ileft
,
1132 &op
, &oleft
) == (size_t)(-1)) {
1133 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line (1).\n");
1137 if (!(ot
= (char *)malloc(op
- icbuffer
+ 1))){
1138 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1143 strcpy (ot
, icbuffer
);
1144 free (sub
->text
[l
]);
1148 for (l
= sub
->lines
; l
;)
1149 free (sub
->text
[--l
]);
1156 subtitle
* subcp_recode1 (subtitle
*sub
)
1159 size_t ileft
, oleft
;
1161 if(icdsc
== (iconv_t
)(-1)) return sub
;
1164 char *ip
= icbuffer
;
1165 char *op
= sub
->text
[--l
];
1166 strlcpy(ip
, op
, ICBUFFSIZE
);
1168 oleft
= ICBUFFSIZE
- 1;
1170 if (iconv(icdsc
, &ip
, &ileft
,
1171 &op
, &oleft
) == (size_t)(-1)) {
1172 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: error recoding line (2).\n");
1183 #define max(a,b) (((a)>(b))?(a):(b))
1185 subtitle
* sub_fribidi (subtitle
*sub
, int sub_utf8
)
1187 FriBidiChar logical
[LINE_LEN
+1], visual
[LINE_LEN
+1]; // Hopefully these two won't smash the stack
1188 char *ip
= NULL
, *op
= NULL
;
1189 FriBidiCharType base
;
1190 size_t len
,orig_len
;
1193 fribidi_boolean log2vis
;
1194 if(flip_hebrew
) { // Please fix the indentation someday
1195 fribidi_set_mirroring (FRIBIDI_TRUE
);
1196 fribidi_set_reorder_nsm (FRIBIDI_FALSE
);
1198 if( sub_utf8
== 0 ) {
1199 char_set_num
= fribidi_parse_charset (fribidi_charset
?fribidi_charset
:"ISO8859-8");
1201 char_set_num
= fribidi_parse_charset ("UTF-8");
1204 ip
= sub
->text
[--l
];
1205 orig_len
= len
= strlen( ip
); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1206 if(len
> LINE_LEN
) {
1207 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: sub->text is longer than LINE_LEN.\n");
1211 len
= fribidi_charset_to_unicode (char_set_num
, ip
, len
, logical
);
1212 base
= fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
1213 log2vis
= fribidi_log2vis (logical
, len
, &base
,
1215 visual
, NULL
, NULL
, NULL
);
1217 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
,
1219 if((op
= (char*)malloc(sizeof(char)*(max(2*orig_len
,2*len
) + 1))) == NULL
) {
1220 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1224 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1230 for (l
= sub
->lines
; l
;)
1231 free (sub
->text
[--l
]);
1240 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1241 int sub_num
, int sub_uses_time
) {
1245 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1246 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1250 if (sub
->end
<= sub
->start
){
1251 sub
->end
= sub
->start
+ subfms
;
1258 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1259 // these subtitles overlap for less than 0.2 seconds
1260 // and would result in very short overlapping subtitle
1261 // so let's fix the problem here, before overlapping code
1262 // get its hands on them
1263 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1264 sub
->end
-= half
+ 1;
1265 nextsub
->start
+= delta
- half
;
1267 if (sub
->end
>= nextsub
->start
){
1268 sub
->end
= nextsub
->start
- 1;
1269 if (sub
->end
- sub
->start
> subfms
)
1270 sub
->end
= sub
->start
+ subfms
;
1277 * Movies are often converted from FILM (24 fps)
1278 * to PAL (25) by simply speeding it up, so we
1279 * to multiply the original timestmaps by
1280 * (Movie's FPS / Subtitle's (guessed) FPS)
1281 * so eg. for 23.98 fps movie and PAL time based
1282 * subtitles we say -subfps 25 and we're fine!
1285 /* timed sub fps correction ::atmos */
1286 if(sub_uses_time
&& sub_fps
) {
1287 sub
->start
*= sub_fps
/fps
;
1288 sub
->end
*= sub_fps
/fps
;
1294 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Adjusted %d subtitle(s).\n", n
);
1298 subtitle
* (*read
)(FILE *fd
,subtitle
*dest
);
1299 void (*post
)(subtitle
*dest
);
1304 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1305 void* guess_cp(FILE *fd
, char *preferred_language
, char *fallback
)
1307 const char **languages
;
1308 size_t langcnt
, buflen
;
1309 EncaAnalyser analyser
;
1310 EncaEncoding encoding
;
1311 unsigned char *buffer
;
1312 char *detected_sub_cp
= NULL
;
1315 buffer
= (unsigned char*)malloc(MAX_GUESS_BUFFER_SIZE
*sizeof(char));
1316 buflen
= fread(buffer
, 1, MAX_GUESS_BUFFER_SIZE
, fd
);
1318 languages
= enca_get_languages(&langcnt
);
1319 mp_msg(MSGT_SUBREADER
, MSGL_V
, "ENCA supported languages: ");
1320 for (i
= 0; i
< langcnt
; i
++) {
1321 mp_msg(MSGT_SUBREADER
, MSGL_V
, "%s ", languages
[i
]);
1323 mp_msg(MSGT_SUBREADER
, MSGL_V
, "\n");
1325 for (i
= 0; i
< langcnt
; i
++) {
1326 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1327 analyser
= enca_analyser_alloc(languages
[i
]);
1328 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1329 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
));
1330 detected_sub_cp
= strdup(enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
));
1331 enca_analyser_free(analyser
);
1338 if (!detected_sub_cp
) detected_sub_cp
= strdup(fallback
);
1340 return detected_sub_cp
;
1344 sub_data
* sub_read_file (char *filename
, float fps
) {
1345 //filename is assumed to be malloc'ed, free() is used in sub_free()
1347 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1348 subtitle
*first
, *second
, *sub
, *return_sub
;
1349 sub_data
*subt_data
;
1350 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1351 struct subreader sr
[]=
1353 { sub_read_line_microdvd
, NULL
, "microdvd" },
1354 { sub_read_line_subrip
, NULL
, "subrip" },
1355 { sub_read_line_subviewer
, NULL
, "subviewer" },
1356 { sub_read_line_sami
, NULL
, "sami" },
1357 { sub_read_line_vplayer
, NULL
, "vplayer" },
1358 { sub_read_line_rt
, NULL
, "rt" },
1359 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1360 { sub_read_line_pjs
, NULL
, "pjs" },
1361 { sub_read_line_mpsub
, NULL
, "mpsub" },
1362 { sub_read_line_aqt
, NULL
, "aqt" },
1363 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1364 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1365 { sub_read_line_jacosub
, NULL
, "jacosub" },
1366 { sub_read_line_mpl2
, NULL
, "mpl2" }
1368 struct subreader
*srp
;
1370 if(filename
==NULL
) return NULL
; //qnx segfault
1371 fd
=fopen (filename
, "r"); if (!fd
) return NULL
;
1373 sub_format
=sub_autodetect (fd
, &uses_time
);
1374 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1375 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1377 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Detected subtitle file format: %s\n", srp
->name
);
1382 sub_utf8_prev
=sub_utf8
;
1386 if ((l
=strlen(filename
))>4){
1387 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1389 if (!strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1394 if (k
<0) subcp_open(fd
);
1399 first
=(subtitle
*)malloc(n_max
*sizeof(subtitle
));
1403 sub_utf8
=sub_utf8_prev
;
1409 sub
= (subtitle
*)malloc(sizeof(subtitle
));
1410 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1411 //as the beginning of the following
1412 previous_sub_end
= 0;
1417 first
=realloc(first
,n_max
*sizeof(subtitle
));
1420 sub
= &first
[sub_num
];
1422 memset(sub
, '\0', sizeof(subtitle
));
1423 sub
=srp
->read(fd
,sub
);
1424 if(!sub
) break; // EOF
1426 if ((sub
!=ERR
) && (sub_utf8
& 2)) sub
=subcp_recode(sub
);
1429 if (sub
!=ERR
) sub
=sub_fribidi(sub
,sub_utf8
);
1436 if ( first
) free(first
);
1439 // Apply any post processing that needs recoding first
1440 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1442 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1443 first
[sub_num
].start
= sub
->start
;
1444 first
[sub_num
].end
= sub
->end
;
1445 first
[sub_num
].lines
= sub
->lines
;
1446 first
[sub_num
].alignment
= sub
->alignment
;
1447 for(i
= 0; i
< sub
->lines
; ++i
){
1448 first
[sub_num
].text
[i
] = sub
->text
[i
];
1450 if (previous_sub_end
){
1451 first
[sub_num
- 1].end
= previous_sub_end
;
1452 previous_sub_end
= 0;
1455 for(j
= sub_num
- 1; j
>= 0; --j
){
1456 first
[j
+ 1].start
= first
[j
].start
;
1457 first
[j
+ 1].end
= first
[j
].end
;
1458 first
[j
+ 1].lines
= first
[j
].lines
;
1459 first
[j
+ 1].alignment
= first
[j
].alignment
;
1460 for(i
= 0; i
< first
[j
].lines
; ++i
){
1461 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1463 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1464 first
[j
].start
= sub
->start
;
1465 first
[j
].end
= sub
->end
;
1466 first
[j
].lines
= sub
->lines
;
1467 first
[j
].alignment
= sub
->alignment
;
1468 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1469 first
[j
].text
[i
] = sub
->text
[i
];
1471 if (previous_sub_end
){
1472 first
[j
].end
= first
[j
- 1].end
;
1473 first
[j
- 1].end
= previous_sub_end
;
1474 previous_sub_end
= 0;
1481 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1490 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1491 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Read %i subtitles", sub_num
);
1492 if (sub_errs
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,", %i bad line(s).\n", sub_errs
);
1493 else mp_msg(MSGT_SUBREADER
,MSGL_INFO
,".\n");
1500 // we do overlap if the user forced it (suboverlap_enable == 2) or
1501 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1502 // this is because usually overlapping subtitles are found in these formats,
1503 // while in others they are probably result of bad timing
1504 if ((suboverlap_enabled
== 2) ||
1505 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1506 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1507 // here we manage overlapping subtitles
1512 // for each subtitle in first[] we deal with its 'block' of
1514 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1515 unsigned long global_start
= first
[sub_first
].start
,
1516 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1517 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1518 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1519 char real_block
= 1;
1521 // here we find the number of subtitles inside the 'block'
1522 // and its span interval. this works well only with sorted
1524 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1526 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1527 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1528 global_start
= first
[sub_first
+ sub_to_add
].start
;
1530 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1531 global_end
= first
[sub_first
+ sub_to_add
].end
;
1535 // we need a structure to keep trace of the screen lines
1536 // used by the subs, a 'placeholder'
1537 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1538 // from a block of sub_to_add+1 subs
1539 placeholder
= (int **) malloc(sizeof(int *) * counter
);
1540 for (i
= 0; i
< counter
; ++i
) {
1541 placeholder
[i
] = (int *) malloc(sizeof(int) * lines_to_add
);
1542 for (j
= 0; j
< lines_to_add
; ++j
) {
1543 placeholder
[i
][j
] = -1;
1548 local_end
= global_start
- 1;
1552 // here we find the beginning and the end of a new
1553 // subtitle in the block
1554 local_start
= local_end
+ 1;
1555 local_end
= global_end
;
1556 for (j
= 0; j
<= sub_to_add
; ++j
) {
1557 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1558 local_end
= first
[sub_first
+ j
].start
- 1;
1559 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1560 local_end
= first
[sub_first
+ j
].end
;
1563 // here we allocate the screen lines to subs we must
1564 // display in current local_start-local_end interval.
1565 // if the subs were yet presents in the previous interval
1566 // they keep the same lines, otherside they get unused lines
1567 for (j
= 0; j
<= sub_to_add
; ++j
) {
1568 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1569 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1572 int fragment_position
= -1;
1574 // if this is not the first new sub of the block
1575 // we find if this sub was present in the previous
1578 for (i
= 0; i
< lines_to_add
; ++i
) {
1579 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1580 placeholder
[counter
][i
] = sub_first
+ j
;
1587 // we are looking for the shortest among all groups of
1588 // sequential blank lines whose length is greater than or
1589 // equal to sub_lines. we store in fragment_position the
1590 // position of the shortest group, in fragment_length its
1591 // length, and in tmp the length of the group currently
1593 for (i
= 0; i
< lines_to_add
; ++i
) {
1594 if (placeholder
[counter
][i
] == -1) {
1595 // placeholder[counter][i] is part of the current group
1599 if (tmp
== sub_lines
) {
1600 // current group's size fits exactly the one we
1601 // need, so we stop looking
1602 fragment_position
= i
- tmp
;
1606 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1607 // current group is the best we found till here,
1608 // but is still bigger than the one we are looking
1609 // for, so we keep on looking
1610 fragment_length
= tmp
;
1611 fragment_position
= i
- tmp
;
1614 // current group doesn't fit at all, so we forget it
1620 // last screen line is blank, a group ends with it
1621 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1622 fragment_position
= i
- tmp
;
1625 if (fragment_position
== -1) {
1626 // it was not possible to find free screen line(s) for a subtitle,
1627 // usually this means a bug in the code; however we do not overlap
1628 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1629 higher_line
= SUB_MAX_TEXT
+ 1;
1632 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1633 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1638 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1639 if (placeholder
[counter
][j
] != -1)
1644 if (higher_line
>= SUB_MAX_TEXT
) {
1645 // the 'block' has too much lines, so we don't overlap the
1647 second
= (subtitle
*) realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1648 for (j
= 0; j
<= sub_to_add
; ++j
) {
1650 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1651 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1652 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1653 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1654 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1655 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1656 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1659 sub_num
+= sub_to_add
+ 1;
1660 sub_first
+= sub_to_add
;
1665 // we read the placeholder structure and create the new
1667 second
= (subtitle
*) realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1668 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1669 second
[sub_num
].start
= local_start
;
1670 second
[sub_num
].end
= local_end
;
1671 second
[sub_num
].alignment
= first
[sub_first
].alignment
;
1672 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1673 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1674 if (placeholder
[counter
][j
] != -1) {
1675 int lines
= first
[placeholder
[counter
][j
]].lines
;
1676 for (ls
= 0; ls
< lines
; ++ls
) {
1677 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1681 second
[sub_num
].text
[i
++] = strdup(" ");
1686 } while (local_end
< global_end
);
1688 for (i
= 0; i
< counter
; ++i
)
1689 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1691 counter
= 2 * sub_to_add
+ 1;
1692 for (i
= 0; i
< counter
; ++i
) {
1693 free(placeholder
[i
]);
1696 sub_first
+= sub_to_add
;
1699 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1700 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1701 free(first
[j
].text
[i
]);
1706 return_sub
= second
;
1707 } else { //if(suboverlap_enabled)
1708 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1711 if (return_sub
== NULL
) return NULL
;
1712 subt_data
= (sub_data
*)malloc(sizeof(sub_data
));
1713 subt_data
->filename
= filename
;
1714 subt_data
->sub_uses_time
= uses_time
;
1715 subt_data
->sub_num
= sub_num
;
1716 subt_data
->sub_errs
= sub_errs
;
1717 subt_data
->subtitles
= return_sub
;
1722 char * strreplace( char * in
,char * what
,char * whereof
)
1727 if ( ( in
== NULL
)||( what
== NULL
)||( whereof
== NULL
)||( ( tmp
=strstr( in
,what
) ) == NULL
) ) return NULL
;
1728 for( i
=0;i
<strlen( whereof
);i
++ ) tmp
[i
]=whereof
[i
];
1729 if ( strlen( what
) > strlen( whereof
) ) tmp
[i
]=0;
1735 static void strcpy_trim(char *d
, char *s
)
1737 // skip leading whitespace
1738 while (*s
&& !isalnum(*s
)) {
1743 while (*s
&& isalnum(*s
)) {
1748 // trim excess whitespace
1749 while (*s
&& !isalnum(*s
)) {
1758 static void strcpy_strip_ext(char *d
, char *s
)
1760 char *tmp
= strrchr(s
,'.');
1765 strncpy(d
, s
, tmp
-s
);
1774 static void strcpy_get_ext(char *d
, char *s
)
1776 char *tmp
= strrchr(s
,'.');
1785 static int whiteonly(char *s
)
1788 if (isalnum(*s
)) return 0;
1794 typedef struct _subfn
1800 static int compare_sub_priority(const void *a
, const void *b
)
1802 if (((subfn
*)a
)->priority
> ((subfn
*)b
)->priority
) {
1804 } else if (((subfn
*)a
)->priority
< ((subfn
*)b
)->priority
) {
1807 return strcoll(((subfn
*)a
)->fname
, ((subfn
*)b
)->fname
);
1811 char** sub_filenames(char* path
, char *fname
)
1813 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
1814 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
1816 int len
, pos
, found
, i
, j
;
1817 char * sub_exts
[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
1828 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
1829 +(strlen(path
) > 256 ? strlen(path
) : 256)+2;
1831 f_dir
= (char*)malloc(len
);
1832 f_fname
= (char*)malloc(len
);
1833 f_fname_noext
= (char*)malloc(len
);
1834 f_fname_trim
= (char*)malloc(len
);
1836 tmp_fname_noext
= (char*)malloc(len
);
1837 tmp_fname_trim
= (char*)malloc(len
);
1838 tmp_fname_ext
= (char*)malloc(len
);
1840 tmpresult
= (char*)malloc(len
);
1842 result
= (subfn
*)malloc(sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1843 memset(result
, 0, sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1847 tmp
= strrchr(fname
,'/');
1849 if(!tmp
)tmp
= strrchr(fname
,'\\');
1852 // extract filename & dirname from fname
1854 strcpy(f_fname
, tmp
+1);
1856 strncpy(f_dir
, fname
, pos
+1);
1859 strcpy(f_fname
, fname
);
1860 strcpy(f_dir
, "./");
1863 strcpy_strip_ext(f_fname_noext
, f_fname
);
1864 strcpy_trim(f_fname_trim
, f_fname_noext
);
1867 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
1868 tmp_sub_id
= (char*)malloc(strlen(dvdsub_lang
)+1);
1869 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
1873 // 1 = any subtitle file
1874 // 2 = any sub file containing movie name
1875 // 3 = sub file containing movie name and the lang extension
1876 for (j
= 0; j
<= 1; j
++) {
1877 d
= opendir(j
== 0 ? f_dir
: path
);
1879 while ((de
= readdir(d
))) {
1880 // retrieve various parts of the filename
1881 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
1882 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
1883 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
1885 // does it end with a subtitle extension?
1889 for (i
= ((sub_cp
&& strncasecmp(sub_cp
, "enca", 4) != 0) ? 3 : 0); sub_exts
[i
]; i
++) {
1891 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
1894 for (i
= 0; sub_exts
[i
]; i
++) {
1896 if (strcasecmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
1902 // we have a (likely) subtitle file
1905 if (!prio
&& tmp_sub_id
)
1907 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
1908 printf("dvdsublang...%s\n", tmpresult
);
1909 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
1910 // matches the movie name + lang extension
1914 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
1915 // matches the movie name
1918 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && (sub_match_fuzziness
>= 1)) {
1919 // contains the movie name
1920 tmp
+= strlen(f_fname_trim
);
1921 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
1922 // with sub_id specified prefer localized subtitles
1924 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
1925 // without sub_id prefer "plain" name
1928 // with no localized subs found, try any else instead
1933 // doesn't contain the movie name
1934 // don't try in the mplayer subtitle directory
1935 if ((j
== 0) && (sub_match_fuzziness
>= 2)) {
1943 if (i
<3){ // prefer UTF-8 coded
1947 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
1948 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1949 if ((f
= fopen(tmpresult
, "rt"))) {
1951 result
[subcnt
].priority
= prio
;
1952 result
[subcnt
].fname
= strdup(tmpresult
);
1958 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
1965 if (tmp_sub_id
) free(tmp_sub_id
);
1969 free(f_fname_noext
);
1972 free(tmp_fname_noext
);
1973 free(tmp_fname_trim
);
1974 free(tmp_fname_ext
);
1978 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
1980 result2
= (char**)malloc(sizeof(char*)*(subcnt
+1));
1981 memset(result2
, 0, sizeof(char*)*(subcnt
+1));
1983 for (i
= 0; i
< subcnt
; i
++) {
1984 result2
[i
] = result
[i
].fname
;
1986 result2
[subcnt
] = NULL
;
1993 void list_sub_file(sub_data
* subd
){
1995 subtitle
*subs
= subd
->subtitles
;
1997 for(j
=0; j
< subd
->sub_num
; j
++){
1998 subtitle
* egysub
=&subs
[j
];
1999 printf ("%i line%c (%li-%li)\n",
2001 (1==egysub
->lines
)?' ':'s',
2004 for (i
=0; i
<egysub
->lines
; i
++) {
2005 printf ("\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
2010 printf ("Subtitle format %s time.\n",
2011 subd
->sub_uses_time
? "uses":"doesn't use");
2012 printf ("Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
2015 void dump_srt(sub_data
* subd
, float fps
){
2021 subtitle
*subs
= subd
->subtitles
;
2023 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2025 fd
=fopen("dumpsub.srt","w");
2028 perror("dump_srt: fopen");
2031 for(i
=0; i
< subd
->sub_num
; i
++)
2033 onesub
=subs
+i
; //=&subs[i];
2034 fprintf(fd
,"%d\n",i
+1);//line number
2037 if (!subd
->sub_uses_time
)
2038 temp
= temp
* 100 / sub_fps
;
2039 temp
-= sub_delay
* 100;
2040 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2041 m
=temp
/6000; temp
%=6000; //m =1*100*60
2042 s
=temp
/100; temp
%=100; //s =1*100
2043 ms
=temp
*10; //ms=1*10
2044 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
2047 if (!subd
->sub_uses_time
)
2048 temp
= temp
* 100 / sub_fps
;
2049 temp
-= sub_delay
* 100;
2050 h
=temp
/360000;temp
%=360000;
2051 m
=temp
/6000; temp
%=6000;
2052 s
=temp
/100; temp
%=100;
2054 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
2056 for(j
=0;j
<onesub
->lines
;j
++)
2057 fprintf(fd
,"%s\n",onesub
->text
[j
]);
2062 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2065 void dump_mpsub(sub_data
* subd
, float fps
){
2069 subtitle
*subs
= subd
->subtitles
;
2071 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
2072 if (sub_fps
==0) sub_fps
=fps
;
2074 fd
=fopen ("dump.mpsub", "w");
2076 perror ("dump_mpsub: fopen");
2081 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
2082 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
2084 for(j
=0; j
< subd
->sub_num
; j
++){
2085 subtitle
* egysub
=&subs
[j
];
2086 if (subd
->sub_uses_time
) {
2087 a
=((egysub
->start
-mpsub_position
)/100.0);
2088 b
=((egysub
->end
-egysub
->start
)/100.0);
2089 if ( (float)((int)a
) == a
)
2090 fprintf (fd
, "%.0f",a
);
2092 fprintf (fd
, "%.2f",a
);
2094 if ( (float)((int)b
) == b
)
2095 fprintf (fd
, " %.0f\n",b
);
2097 fprintf (fd
, " %.2f\n",b
);
2099 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
2100 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
2103 mpsub_position
= egysub
->end
;
2104 for (i
=0; i
<egysub
->lines
; i
++) {
2105 fprintf (fd
, "%s\n",egysub
->text
[i
]);
2110 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2113 void dump_microdvd(sub_data
* subd
, float fps
) {
2116 subtitle
*subs
= subd
->subtitles
;
2119 fd
= fopen("dumpsub.txt", "w");
2121 perror("dumpsub.txt: fopen");
2124 delay
= sub_delay
* sub_fps
;
2125 for (i
= 0; i
< subd
->sub_num
; ++i
) {
2127 start
= subs
[i
].start
;
2129 if (subd
->sub_uses_time
) {
2130 start
= start
* sub_fps
/ 100 ;
2131 end
= end
* sub_fps
/ 100;
2134 start
= start
* sub_fps
/ fps
;
2135 end
= end
* sub_fps
/ fps
;
2139 fprintf(fd
, "{%d}{%d}", start
, end
);
2140 for (j
= 0; j
< subs
[i
].lines
; ++j
)
2141 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
2145 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
2148 void dump_jacosub(sub_data
* subd
, float fps
) {
2154 subtitle
*subs
= subd
->subtitles
;
2156 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2158 fd
=fopen("dumpsub.jss","w");
2161 perror("dump_jacosub: fopen");
2164 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
2165 for(i
=0; i
< subd
->sub_num
; i
++)
2167 onesub
=subs
+i
; //=&subs[i];
2170 if (!subd
->sub_uses_time
)
2171 temp
= temp
* 100 / sub_fps
;
2172 temp
-= sub_delay
* 100;
2173 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2174 m
=temp
/6000; temp
%=6000; //m =1*100*60
2175 s
=temp
/100; temp
%=100; //s =1*100
2177 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2180 if (!subd
->sub_uses_time
)
2181 temp
= temp
* 100 / sub_fps
;
2182 temp
-= sub_delay
* 100;
2183 h
=temp
/360000;temp
%=360000;
2184 m
=temp
/6000; temp
%=6000;
2185 s
=temp
/100; temp
%=100;
2187 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2189 for(j
=0;j
<onesub
->lines
;j
++)
2190 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2195 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2198 void dump_sami(sub_data
* subd
, float fps
) {
2203 subtitle
*subs
= subd
->subtitles
;
2205 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2207 fd
=fopen("dumpsub.smi","w");
2210 perror("dump_jacosub: fopen");
2213 fprintf(fd
, "<SAMI>\n"
2215 " <STYLE TYPE=\"Text/css\">\n"
2217 " 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"
2218 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2223 for(i
=0; i
< subd
->sub_num
; i
++)
2225 onesub
=subs
+i
; //=&subs[i];
2228 if (!subd
->sub_uses_time
)
2229 temp
= temp
* 100 / sub_fps
;
2230 temp
-= sub_delay
* 100;
2231 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2232 "\t <P>", temp
* 10);
2234 for(j
=0;j
<onesub
->lines
;j
++)
2235 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2240 if (!subd
->sub_uses_time
)
2241 temp
= temp
* 100 / sub_fps
;
2242 temp
-= sub_delay
* 100;
2243 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2244 "\t <P> \n", temp
* 10);
2246 fprintf(fd
, "</BODY>\n"
2249 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2252 void sub_free( sub_data
* subd
)
2256 if ( !subd
) return;
2258 if (subd
->subtitles
) {
2259 for (i
=0; i
< subd
->subtitles
->lines
; i
++) free( subd
->subtitles
->text
[i
] );
2260 free( subd
->subtitles
);
2262 if (subd
->filename
) free( subd
->filename
);
2267 int main(int argc
, char **argv
) { // for testing
2271 printf("\nUsage: subreader filename.sub\n\n");
2275 subd
= sub_read_file(argv
[1]);
2277 printf("Couldn't load file.\n");
2281 list_sub_file(subd
);