libcdio
[mplayer.git] / subreader.c
blobe2cdb674dcbef1b6534ae54e15bf81eab10dab5e
1 /*
2 * Subtitle reader with format autodetection
4 * Written by laaz
5 * Some code cleanup & realloc() by A'rpi/ESP-team
7 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include <sys/types.h>
16 #include <dirent.h>
18 #include "config.h"
19 #include "mp_msg.h"
20 #include "subreader.h"
22 #ifdef HAVE_ENCA
23 #include <enca.h>
24 #endif
26 #define ERR ((void *) -1)
28 #ifdef USE_ICONV
29 #include <iconv.h>
30 char *sub_cp=NULL;
31 #endif
32 #ifdef USE_FRIBIDI
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
37 #endif
39 extern char* dvdsub_lang;
41 /* Maximal length of line of a subtitle */
42 #define LINE_LEN 1000
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;
54 #ifdef USE_SORTSUB
55 /*
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;
67 #endif
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) {
75 int i = 0;
76 while (isspace(s[i])) ++i;
77 if (i) strcpy(s, s + i);
78 i = strlen(s) - 1;
79 while (i > 0 && isspace(s[i])) s[i--] = '\0';
82 static char *stristr(const char *haystack, const char *needle) {
83 int len = 0;
84 const char *p = haystack;
86 if (!(haystack && needle)) return NULL;
88 len=strlen(needle);
89 while (*p != '\0') {
90 if (strncasecmp(p, needle, len) == 0) return (char*)p;
91 p++;
94 return NULL;
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;
101 int state;
103 current->lines = current->start = current->end = 0;
104 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
105 state = 0;
107 /* read the first line */
108 if (!s)
109 if (!(s = fgets(line, LINE_LEN, fd))) return 0;
111 do {
112 switch (state) {
114 case 0: /* find "START=" or "Slacktime:" */
115 slacktime_s = stristr (s, "Slacktime:");
116 if (slacktime_s)
117 sub_slacktime = strtol (slacktime_s+10, NULL, 0) / 10;
119 s = stristr (s, "Start=");
120 if (s) {
121 current->start = strtol (s + 6, &s, 0) / 10;
122 /* eat '>' */
123 for (; *s != '>' && *s != '\0'; s++);
124 s++;
125 state = 1; continue;
127 break;
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 */
133 s++;
134 if (*s == 'P' || *s == 'p') { s++; state = 2; continue; } /* found '<P' */
135 for (; *s != '>' && *s != '\0'; s++); /* skip remains of non-<P> TAG */
136 if (s == '\0')
137 break;
138 s++;
139 continue;
141 case 2: /* find ">" */
142 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; }
143 break;
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);
149 if (text[0] != '\0')
150 current->text[current->lines++] = strdup (text);
151 s += 4;
153 else if ((*s == '{') && !sub_no_text_pp) { state = 5; ++s; continue; }
154 else if (*s == '<') { state = 4; }
155 else if (!strncasecmp (s, "&nbsp;", 6)) { *p++ = ' '; s += 6; }
156 else if (*s == '\t') { *p++ = ' '; s++; }
157 else if (*s == '\r' || *s == '\n') { s++; }
158 else *p++ = *s++;
160 /* skip duplicated space */
161 if (p > text + 2) if (*(p-1) == ' ' && *(p-2) == ' ') p--;
163 continue;
165 case 4: /* get current->end or skip <TAG> */
166 q = stristr (s, "Start=");
167 if (q) {
168 current->end = strtol (q + 6, &q, 0) / 10 - 1;
169 *p = '\0'; trail_space (text);
170 if (text[0] != '\0')
171 current->text[current->lines++] = strdup (text);
172 if (current->lines > 0) { state = 99; break; }
173 state = 0; continue;
175 s = strchr (s, '>');
176 if (s) { s++; state = 3; continue; }
177 break;
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;
182 s = s + 3;
184 if (stristr(s, "\\a2") != NULL) {
185 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
186 s = s + 3;
187 } else if (stristr(s, "\\a3") != NULL) {
188 current->alignment = SUB_ALIGNMENT_BOTTOMRIGHT;
189 s = s + 3;
190 } else if ((stristr(s, "\\a4") != NULL) || (stristr(s, "\\a5") != NULL) || (stristr(s, "\\a8") != NULL)) {
191 current->alignment = SUB_ALIGNMENT_TOPLEFT;
192 s = s + 3;
193 } else if (stristr(s, "\\a6") != NULL) {
194 current->alignment = SUB_ALIGNMENT_TOPCENTER;
195 s = s + 3;
196 } else if (stristr(s, "\\a7") != NULL) {
197 current->alignment = SUB_ALIGNMENT_TOPRIGHT;
198 s = s + 3;
199 } else if (stristr(s, "\\a9") != NULL) {
200 current->alignment = SUB_ALIGNMENT_MIDDLELEFT;
201 s = s + 3;
202 } else if (stristr(s, "\\a10") != NULL) {
203 current->alignment = SUB_ALIGNMENT_MIDDLECENTER;
204 s = s + 4;
205 } else if (stristr(s, "\\a11") != NULL) {
206 current->alignment = SUB_ALIGNMENT_MIDDLERIGHT;
207 s = s + 4;
210 if (*s == '}') state = 3;
211 ++s;
212 continue;
215 /* read next line */
216 if (state != 99 && !(s = fgets (line, LINE_LEN, fd))) {
217 if (current->start > 0) {
218 break; // if it is the last subtitle
219 } else {
220 return 0;
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);
230 if (text[0] != '\0')
231 current->text[current->lines++] = strdup (text);
234 return current;
238 char *sub_readtext(char *source, char **dest) {
239 int len=0;
240 char *p=source;
242 // printf("src=%p dest=%p \n",source,dest);
244 while ( !eol(*p) && *p!= '|' ) {
245 p++,len++;
248 *dest= (char *)malloc (len+1);
249 if (!dest) {return ERR;}
251 strncpy(*dest, source, len);
252 (*dest)[len]=0;
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];
263 char *p, *next;
264 int i;
266 do {
267 if (!fgets (line, LINE_LEN, fd)) return NULL;
268 } while ((sscanf (line,
269 "{%ld}{}%[^\r\n]",
270 &(current->start), line2) < 2) &&
271 (sscanf (line,
272 "{%ld}{%ld}%[^\r\n]",
273 &(current->start), &(current->end), line2) < 3));
275 p=line2;
277 next=p, i=0;
278 while ((next =sub_readtext (next, &(current->text[i])))) {
279 if (current->text[i]==ERR) {return ERR;}
280 i++;
281 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
283 current->lines= ++i;
285 return current;
288 subtitle *sub_read_line_mpl2(FILE *fd,subtitle *current) {
289 char line[LINE_LEN+1];
290 char line2[LINE_LEN+1];
291 char *p, *next;
292 int i;
294 do {
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;
300 current->end *= 10;
301 p=line2;
303 next=p, i=0;
304 while ((next =sub_readtext (next, &(current->text[i])))) {
305 if (current->text[i]==ERR) {return ERR;}
306 i++;
307 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
309 current->lines= ++i;
311 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;
318 int len;
320 while (1) {
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;
328 p=q=line;
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;
336 if (*p=='|') p++;
337 else while (*p++!=']');
339 break;
341 return current;
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;
347 char *p=NULL;
348 int i,len;
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)
353 continue;
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;
358 len=0;
359 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
360 if (len) {
361 int j=0,skip=0;
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';
365 for(; j<len; j++) {
366 /* let's filter html tags ::atmos */
367 if(line[j]=='>') {
368 skip=0;
369 continue;
371 if(line[j]=='<') {
372 skip=1;
373 continue;
375 if(skip) {
376 continue;
378 *curptr=line[j];
379 curptr++;
381 *curptr='\0';
383 i++;
384 } else {
385 break;
388 current->lines=i;
390 return current;
393 subtitle *sub_read_line_subviewer2(FILE *fd,subtitle *current) {
394 char line[LINE_LEN+1];
395 int a1,a2,a3,a4;
396 char *p=NULL;
397 int i,len;
399 while (!current->text[0]) {
400 if (!fgets (line, LINE_LEN, fd)) return NULL;
401 if (line[0]!='{')
402 continue;
403 if ((len=sscanf (line, "{T %d:%d:%d:%d",&a1,&a2,&a3,&a4)) < 4)
404 continue;
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;
409 len=0;
410 for (p=line; *p!='\n' && *p!='\r' && *p; ++p,++len);
411 if (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';
415 ++i;
416 } else {
417 break;
420 current->lines=i;
422 return current;
426 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) {
427 char line[LINE_LEN+1];
428 int a1,a2,a3;
429 char *p=NULL, *next,separator;
430 int i,len,plen;
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)
435 continue;
437 if (!(current->start = a1*360000+a2*6000+a3*100))
438 continue;
439 /* removed by wodzu
440 p=line;
441 // finds the body of the subtitle
442 for (i=0; i<3; i++){
443 p=strchr(p,':');
444 if (p==NULL) break;
445 ++p;
447 if (p==NULL) {
448 printf("SUB: Skipping incorrect subtitle line!\n");
449 continue;
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:
455 p = &line[ plen ];
457 i=0;
458 if (*p!='|') {
460 next = p,i=0;
461 while ((next =sub_readtext (next, &(current->text[i])))) {
462 if (current->text[i]==ERR) {return ERR;}
463 i++;
464 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
466 current->lines=i+1;
469 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;
479 int i,len,plen;
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;
487 if (
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)
501 continue;
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;
506 p=line; p+=plen;i=0;
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){
510 next+=8;i=0;
511 while ((next =sub_readtext (next, &(current->text[i])))) {
512 if (current->text[i]==ERR) {return ERR;}
513 i++;
514 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
517 current->lines=i+1;
519 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
527 * w/o it
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
532 int comma;
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;
538 int num;
540 char line[LINE_LEN+1],
541 line3[LINE_LEN+1],
542 *line2;
543 char *tmp;
545 do {
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]", &nothing,
549 &hour1, &min1, &sec1, &hunsec1,
550 &hour2, &min2, &sec2, &hunsec2,
551 line3) < 9
553 sscanf (line, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
554 "%[^\n\r]", &nothing,
555 &hour1, &min1, &sec1, &hunsec1,
556 &hour2, &min2, &sec2, &hunsec2,
557 line3) < 9 );
559 line2=strchr(line3, ',');
561 for (comma = 4; comma < max_comma; comma ++)
563 tmp = line2;
564 if(!(tmp=strchr(++tmp, ','))) break;
565 if(*(++tmp) == ' ') break;
566 /* a space after a comma means we're already in a sentence */
567 line2 = tmp;
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';
582 line2=tmp+2;
583 num++;
584 current->lines++;
585 if (current->lines >= SUB_MAX_TEXT) return current;
588 current->text[num]=strdup(line2);
589 current->lines++;
591 return current;
594 void sub_pp_ssa(subtitle *sub) {
595 int l=sub->lines;
596 char *so,*de,*start;
598 while (l){
599 /* eliminate any text enclosed with {}, they are font and color settings */
600 so=de=sub->text[--l];
601 while (*so) {
602 if(*so == '{' && so[1]=='\\') {
603 for (start=so; *so && *so!='}'; so++);
604 if(*so) so++; else so=start;
606 if(*so) {
607 *de=*so;
608 so++; de++;
611 *de=*so;
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))
628 return NULL;
629 /* skip spaces */
630 for (s=line; *s && isspace(*s); s++);
631 /* allow empty lines at the end of the file */
632 if (*s==0)
633 return NULL;
634 /* get the time */
635 if (sscanf (s, "%ld,%ld,", &(current->start),
636 &(current->end)) <2) {
637 return ERR;
639 /* the files I have are in tenths of second */
640 current->start *= 10;
641 current->end *= 10;
642 /* walk to the beggining of the string */
643 for (; *s; s++) if (*s==',') break;
644 if (*s) {
645 for (s++; *s; s++) if (*s==',') break;
646 if (*s) s++;
648 if (*s!='"') {
649 return ERR;
651 /* copy the string to the text buffer */
652 for (s++, d=text; *s && *s!='"'; s++, d++)
653 *d=*s;
654 *d=0;
655 current->text[0] = strdup(text);
656 current->lines = 1;
658 return current;
661 subtitle *sub_read_line_mpsub(FILE *fd, subtitle *current) {
662 char line[LINE_LEN+1];
663 float a,b;
664 int num=0;
665 char *p, *q;
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;
680 else return current;
682 p=line;
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++);
688 *q='\0';
689 if (strlen(p)) {
690 current->text[num]=strdup(p);
691 // printf (">%s<\n",p);
692 current->lines = ++num;
693 } else {
694 if (num) return current;
695 else return NULL;
698 return NULL; // we should have returned before if it's OK
701 #ifndef USE_SORTSUB
702 //we don't need this if we use previous_sub_end
703 subtitle *previous_aqt_sub = NULL;
704 #endif
706 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) {
707 char line[LINE_LEN+1];
708 char *next;
709 int i;
711 while (1) {
712 // try to locate next subtitle
713 if (!fgets (line, LINE_LEN, fd))
714 return NULL;
715 if (!(sscanf (line, "-->> %ld", &(current->start)) <1))
716 break;
719 #ifdef USE_SORTSUB
720 previous_sub_end = (current->start) ? current->start - 1 : 0;
721 #else
722 if (previous_aqt_sub != NULL)
723 previous_aqt_sub->end = current->start-1;
725 previous_aqt_sub = current;
726 #endif
728 if (!fgets (line, LINE_LEN, fd))
729 return NULL;
731 sub_readtext((char *) &line,&current->text[0]);
732 current->lines = 1;
733 current->end = current->start; // will be corrected by next subtitle
735 if (!fgets (line, LINE_LEN, fd))
736 return current;
738 next = line,i=1;
739 while ((next =sub_readtext (next, &(current->text[i])))) {
740 if (current->text[i]==ERR) {return ERR;}
741 i++;
742 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
744 current->lines=i+1;
746 if ((current->text[0]=="") && (current->text[1]=="")) {
747 #ifdef USE_SORTSUB
748 previous_sub_end = 0;
749 #else
750 // void subtitle -> end of previous marked and exit
751 previous_aqt_sub = NULL;
752 #endif
753 return NULL;
756 return current;
759 #ifndef USE_SORTSUB
760 subtitle *previous_subrip09_sub = NULL;
761 #endif
763 subtitle *sub_read_line_subrip09(FILE *fd,subtitle *current) {
764 char line[LINE_LEN+1];
765 int a1,a2,a3;
766 char * next=NULL;
767 int i,len;
769 while (1) {
770 // try to locate next subtitle
771 if (!fgets (line, LINE_LEN, fd))
772 return NULL;
773 if (!((len=sscanf (line, "[%d:%d:%d]",&a1,&a2,&a3)) < 3))
774 break;
777 current->start = a1*360000+a2*6000+a3*100;
779 #ifdef USE_SORTSUB
780 previous_sub_end = (current->start) ? current->start - 1 : 0;
781 #else
782 if (previous_subrip09_sub != NULL)
783 previous_subrip09_sub->end = current->start-1;
785 previous_subrip09_sub = current;
786 #endif
788 if (!fgets (line, LINE_LEN, fd))
789 return NULL;
791 next = line,i=0;
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;}
797 i++;
798 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
800 current->lines=i+1;
802 if ((current->text[0]=="") && (i==0)) {
803 #ifdef USE_SORTSUB
804 previous_sub_end = 0;
805 #else
806 // void subtitle -> end of previous marked and exit
807 previous_subrip09_sub = NULL;
808 #endif
809 return NULL;
812 return current;
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)) {
828 return NULL;
830 if (sscanf
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])) {
839 case 'S':
840 if (isalpha(line1[2])) {
841 delta = 6;
842 } else {
843 delta = 2;
845 if (sscanf(&line1[delta], "%d", &hours)) {
846 if (hours < 0) {
847 hours *= -1;
848 inverter = -1;
850 if (sscanf(&line1[delta], "%*d:%d", &minutes)) {
851 if (sscanf
852 (&line1[delta], "%*d:%*d:%d",
853 &seconds)) {
854 sscanf(&line1[delta], "%*d:%*d:%*d.%d",
855 &units);
856 } else {
857 hours = 0;
858 sscanf(&line1[delta], "%d:%d.%d",
859 &minutes, &seconds, &units);
860 minutes *= inverter;
862 } else {
863 hours = minutes = 0;
864 sscanf(&line1[delta], "%d.%d", &seconds,
865 &units);
866 seconds *= inverter;
868 jacoShift =
869 ((hours * 3600 + minutes * 60 +
870 seconds) * jacoTimeres +
871 units) * inverter;
873 break;
874 case 'T':
875 if (isalpha(line1[2])) {
876 delta = 8;
877 } else {
878 delta = 2;
880 sscanf(&line1[delta], "%u", &jacoTimeres);
881 break;
884 continue;
885 } else {
886 current->start =
887 (unsigned long) ((a4 + jacoShift) * 100.0 /
888 jacoTimeres);
889 current->end =
890 (unsigned long) ((b4 + jacoShift) * 100.0 /
891 jacoTimeres);
893 } else {
894 current->start =
895 (unsigned
896 long) (((a1 * 3600 + a2 * 60 + a3) * jacoTimeres + a4 +
897 jacoShift) * 100.0 / jacoTimeres);
898 current->end =
899 (unsigned
900 long) (((b1 * 3600 + b2 * 60 + b3) * jacoTimeres + b4 +
901 jacoShift) * 100.0 / jacoTimeres);
903 current->lines = 0;
904 p = line2;
905 while ((*p == ' ') || (*p == '\t')) {
906 ++p;
908 if (isalpha(*p)||*p == '[') {
909 int cont, jLength;
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)) {
922 continue;
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;
928 } else {
929 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
931 strcpy(line2, line1);
932 p = line2;
934 for (q = line1; (!eol(*p)) && (current->lines < SUB_MAX_TEXT); ++p) {
935 switch (*p) {
936 case '{':
937 comment++;
938 break;
939 case '}':
940 if (comment) {
941 --comment;
942 //the next line to get rid of a blank after the comment
943 if ((*(p + 1)) == ' ')
944 p++;
946 break;
947 case '~':
948 if (!comment) {
949 *q = ' ';
950 ++q;
952 break;
953 case ' ':
954 case '\t':
955 if ((*(p + 1) == ' ') || (*(p + 1) == '\t'))
956 break;
957 if (!comment) {
958 *q = ' ';
959 ++q;
961 break;
962 case '\\':
963 if (*(p + 1) == 'n') {
964 *q = '\0';
965 q = line1;
966 current->text[current->lines++] = strdup(line1);
967 ++p;
968 break;
970 if ((toupper(*(p + 1)) == 'C')
971 || (toupper(*(p + 1)) == 'F')) {
972 ++p,++p;
973 break;
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')) {
978 ++p;
979 break;
981 if ((*(p + 1) == '\\') ||
982 (*(p + 1) == '~') || (*(p + 1) == '{')) {
983 ++p;
984 } else if (eol(*(p + 1))) {
985 if (!fgets(directive, LINE_LEN, fd))
986 return NULL;
987 trail_space(directive);
988 strncat(line2, directive,
989 (LINE_LEN > 511) ? LINE_LEN : 511);
990 break;
992 default:
993 if (!comment) {
994 *q = *p;
995 ++q;
997 } //-- switch
998 } //-- for
999 *q = '\0';
1000 current->text[current->lines] = strdup(line1);
1001 } //-- while
1002 current->lines++;
1003 return current;
1006 int sub_autodetect (FILE *fd, int *uses_time) {
1007 char line[LINE_LEN+1];
1008 int i,j=0;
1009 char p;
1011 while (j < 100) {
1012 j++;
1013 if (!fgets (line, LINE_LEN, fd))
1014 return SUB_INVALID;
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)
1042 if ( *line == '<' )
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
1064 #ifdef DUMPSUBS
1065 int sub_utf8=0;
1066 #else
1067 extern int sub_utf8;
1068 int sub_utf8_prev=0;
1069 #endif
1071 extern float sub_delay;
1072 extern float sub_fps;
1074 #ifdef USE_ICONV
1075 static iconv_t icdsc = (iconv_t)(-1);
1077 void subcp_open (FILE *enca_fd)
1079 char *tocp = "UTF-8";
1081 if (sub_cp){
1082 char *cp_tmp = sub_cp;
1083 #ifdef HAVE_ENCA
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) {
1088 if (enca_fd) {
1089 cp_tmp = guess_cp(enca_fd, enca_lang, enca_fallback);
1090 free_cp_tmp = 1;
1091 } else {
1092 cp_tmp = enca_fallback;
1095 #endif
1096 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){
1097 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: opened iconv descriptor.\n");
1098 sub_utf8 = 2;
1099 } else
1100 mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n");
1101 #ifdef HAVE_ENCA
1102 if (free_cp_tmp && cp_tmp) free(cp_tmp);
1103 #endif
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)
1121 int l=sub->lines;
1122 size_t ileft, oleft;
1123 char *op, *ip, *ot;
1125 while (l){
1126 op = icbuffer;
1127 ip = sub->text[--l];
1128 ileft = strlen(ip);
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");
1134 l++;
1135 break;
1137 if (!(ot = (char *)malloc(op - icbuffer + 1))){
1138 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1139 l++;
1140 break;
1142 *op='\0' ;
1143 strcpy (ot, icbuffer);
1144 free (sub->text[l]);
1145 sub->text[l] = ot;
1147 if (l){
1148 for (l = sub->lines; l;)
1149 free (sub->text[--l]);
1150 return ERR;
1152 return sub;
1155 // for demux_ogg.c:
1156 subtitle* subcp_recode1 (subtitle *sub)
1158 int l=sub->lines;
1159 size_t ileft, oleft;
1161 if(icdsc == (iconv_t)(-1)) return sub;
1163 while (l){
1164 char *ip = icbuffer;
1165 char *op = sub->text[--l];
1166 strlcpy(ip, op, ICBUFFSIZE);
1167 ileft = strlen(ip);
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");
1173 return sub;
1175 *op='\0' ;
1177 return sub;
1179 #endif
1181 #ifdef USE_FRIBIDI
1182 #ifndef max
1183 #define max(a,b) (((a)>(b))?(a):(b))
1184 #endif
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;
1191 int l=sub->lines;
1192 int char_set_num;
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");
1200 }else {
1201 char_set_num = fribidi_parse_charset ("UTF-8");
1203 while (l) {
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");
1208 l++;
1209 break;
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,
1214 /* output */
1215 visual, NULL, NULL, NULL);
1216 if(log2vis) {
1217 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL,
1218 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");
1221 l++;
1222 break;
1224 fribidi_unicode_to_charset ( char_set_num, visual, len,op);
1225 free (ip);
1226 sub->text[l] = op;
1229 if (l){
1230 for (l = sub->lines; l;)
1231 free (sub->text[--l]);
1232 return ERR;
1235 return sub;
1238 #endif
1240 static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block,
1241 int sub_num, int sub_uses_time) {
1242 int n,m;
1243 subtitle* nextsub;
1244 int i = sub_num;
1245 unsigned long subfms = (sub_uses_time ? 100 : fps) * subtime;
1246 unsigned long overlap = (sub_uses_time ? 100 : fps) / 5; // 0.2s
1248 n=m=0;
1249 if (i) for (;;){
1250 if (sub->end <= sub->start){
1251 sub->end = sub->start + subfms;
1252 m++;
1253 n++;
1255 if (!--i) break;
1256 nextsub = sub + 1;
1257 if(block){
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;
1271 if (!m)
1272 n++;
1276 /* Theory:
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;
1291 sub = nextsub;
1292 m = 0;
1294 if (n) mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Adjusted %d subtitle(s).\n", n);
1297 struct subreader {
1298 subtitle * (*read)(FILE *fd,subtitle *dest);
1299 void (*post)(subtitle *dest);
1300 const char *name;
1303 #ifdef HAVE_ENCA
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;
1313 int i;
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 char *tmp;
1328 if (strcasecmp(languages[i], preferred_language) != 0) continue;
1329 analyser = enca_analyser_alloc(languages[i]);
1330 encoding = enca_analyse_const(analyser, buffer, buflen);
1331 tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
1332 if (tmp && encoding.charset != ENCA_CS_UNKNOWN) {
1333 detected_sub_cp = strdup(tmp);
1334 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", tmp);
1336 enca_analyser_free(analyser);
1339 free(languages);
1340 free(buffer);
1341 rewind(fd);
1343 if (!detected_sub_cp) {
1344 detected_sub_cp = strdup(fallback);
1345 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detection failed: fallback to %s\n", fallback);
1348 return detected_sub_cp;
1350 #endif
1352 sub_data* sub_read_file (char *filename, float fps) {
1353 FILE *fd;
1354 int n_max, n_first, i, j, sub_first, sub_orig;
1355 subtitle *first, *second, *sub, *return_sub;
1356 sub_data *subt_data;
1357 int uses_time = 0, sub_num = 0, sub_errs = 0;
1358 struct subreader sr[]=
1360 { sub_read_line_microdvd, NULL, "microdvd" },
1361 { sub_read_line_subrip, NULL, "subrip" },
1362 { sub_read_line_subviewer, NULL, "subviewer" },
1363 { sub_read_line_sami, NULL, "sami" },
1364 { sub_read_line_vplayer, NULL, "vplayer" },
1365 { sub_read_line_rt, NULL, "rt" },
1366 { sub_read_line_ssa, sub_pp_ssa, "ssa" },
1367 { sub_read_line_pjs, NULL, "pjs" },
1368 { sub_read_line_mpsub, NULL, "mpsub" },
1369 { sub_read_line_aqt, NULL, "aqt" },
1370 { sub_read_line_subviewer2, NULL, "subviewer 2.0" },
1371 { sub_read_line_subrip09, NULL, "subrip 0.9" },
1372 { sub_read_line_jacosub, NULL, "jacosub" },
1373 { sub_read_line_mpl2, NULL, "mpl2" }
1375 struct subreader *srp;
1377 if(filename==NULL) return NULL; //qnx segfault
1378 fd=fopen (filename, "r"); if (!fd) return NULL;
1380 sub_format=sub_autodetect (fd, &uses_time);
1381 mpsub_multiplier = (uses_time ? 100.0 : 1.0);
1382 if (sub_format==SUB_INVALID) {mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: Could not determine file format\n");return NULL;}
1383 srp=sr+sub_format;
1384 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected subtitle file format: %s\n", srp->name);
1386 rewind (fd);
1388 #ifdef USE_ICONV
1389 sub_utf8_prev=sub_utf8;
1391 int l,k;
1392 k = -1;
1393 if ((l=strlen(filename))>4){
1394 char *exts[] = {".utf", ".utf8", ".utf-8" };
1395 for (k=3;--k>=0;)
1396 if (!strcasecmp(filename+(l - strlen(exts[k])), exts[k])){
1397 sub_utf8 = 1;
1398 break;
1401 if (k<0) subcp_open(fd);
1403 #endif
1405 sub_num=0;n_max=32;
1406 first=(subtitle *)malloc(n_max*sizeof(subtitle));
1407 if(!first){
1408 #ifdef USE_ICONV
1409 subcp_close();
1410 sub_utf8=sub_utf8_prev;
1411 #endif
1412 return NULL;
1415 #ifdef USE_SORTSUB
1416 sub = (subtitle *)malloc(sizeof(subtitle));
1417 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1418 //as the beginning of the following
1419 previous_sub_end = 0;
1420 #endif
1421 while(1){
1422 if(sub_num>=n_max){
1423 n_max+=16;
1424 first=realloc(first,n_max*sizeof(subtitle));
1426 #ifndef USE_SORTSUB
1427 sub = &first[sub_num];
1428 #endif
1429 memset(sub, '\0', sizeof(subtitle));
1430 sub=srp->read(fd,sub);
1431 if(!sub) break; // EOF
1432 #ifdef USE_ICONV
1433 if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub);
1434 #endif
1435 #ifdef USE_FRIBIDI
1436 if (sub!=ERR) sub=sub_fribidi(sub,sub_utf8);
1437 #endif
1438 if ( sub == ERR )
1440 #ifdef USE_ICONV
1441 subcp_close();
1442 #endif
1443 if ( first ) free(first);
1444 return NULL;
1446 // Apply any post processing that needs recoding first
1447 if ((sub!=ERR) && !sub_no_text_pp && srp->post) srp->post(sub);
1448 #ifdef USE_SORTSUB
1449 if(!sub_num || (first[sub_num - 1].start <= sub->start)){
1450 first[sub_num].start = sub->start;
1451 first[sub_num].end = sub->end;
1452 first[sub_num].lines = sub->lines;
1453 first[sub_num].alignment = sub->alignment;
1454 for(i = 0; i < sub->lines; ++i){
1455 first[sub_num].text[i] = sub->text[i];
1457 if (previous_sub_end){
1458 first[sub_num - 1].end = previous_sub_end;
1459 previous_sub_end = 0;
1461 } else {
1462 for(j = sub_num - 1; j >= 0; --j){
1463 first[j + 1].start = first[j].start;
1464 first[j + 1].end = first[j].end;
1465 first[j + 1].lines = first[j].lines;
1466 first[j + 1].alignment = first[j].alignment;
1467 for(i = 0; i < first[j].lines; ++i){
1468 first[j + 1].text[i] = first[j].text[i];
1470 if(!j || (first[j - 1].start <= sub->start)){
1471 first[j].start = sub->start;
1472 first[j].end = sub->end;
1473 first[j].lines = sub->lines;
1474 first[j].alignment = sub->alignment;
1475 for(i = 0; i < SUB_MAX_TEXT; ++i){
1476 first[j].text[i] = sub->text[i];
1478 if (previous_sub_end){
1479 first[j].end = first[j - 1].end;
1480 first[j - 1].end = previous_sub_end;
1481 previous_sub_end = 0;
1483 break;
1487 #endif
1488 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
1491 fclose(fd);
1493 #ifdef USE_ICONV
1494 subcp_close();
1495 #endif
1497 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1498 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Read %i subtitles", sub_num);
1499 if (sub_errs) mp_msg(MSGT_SUBREADER,MSGL_INFO,", %i bad line(s).\n", sub_errs);
1500 else mp_msg(MSGT_SUBREADER,MSGL_INFO,".\n");
1502 if(sub_num<=0){
1503 free(first);
1504 return NULL;
1507 // we do overlap if the user forced it (suboverlap_enable == 2) or
1508 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1509 // this is because usually overlapping subtitles are found in these formats,
1510 // while in others they are probably result of bad timing
1511 if ((suboverlap_enabled == 2) ||
1512 ((suboverlap_enabled) && ((sub_format == SUB_JACOSUB) || (sub_format == SUB_SSA)))) {
1513 adjust_subs_time(first, 6.0, fps, 0, sub_num, uses_time);/*~6 secs AST*/
1514 // here we manage overlapping subtitles
1515 sub_orig = sub_num;
1516 n_first = sub_num;
1517 sub_num = 0;
1518 second = NULL;
1519 // for each subtitle in first[] we deal with its 'block' of
1520 // bonded subtitles
1521 for (sub_first = 0; sub_first < n_first; ++sub_first) {
1522 unsigned long global_start = first[sub_first].start,
1523 global_end = first[sub_first].end, local_start, local_end;
1524 int lines_to_add = first[sub_first].lines, sub_to_add = 0,
1525 **placeholder = NULL, higher_line = 0, counter, start_block_sub = sub_num;
1526 char real_block = 1;
1528 // here we find the number of subtitles inside the 'block'
1529 // and its span interval. this works well only with sorted
1530 // subtitles
1531 while ((sub_first + sub_to_add + 1 < n_first) && (first[sub_first + sub_to_add + 1].start < global_end)) {
1532 ++sub_to_add;
1533 lines_to_add += first[sub_first + sub_to_add].lines;
1534 if (first[sub_first + sub_to_add].start < global_start) {
1535 global_start = first[sub_first + sub_to_add].start;
1537 if (first[sub_first + sub_to_add].end > global_end) {
1538 global_end = first[sub_first + sub_to_add].end;
1542 // we need a structure to keep trace of the screen lines
1543 // used by the subs, a 'placeholder'
1544 counter = 2 * sub_to_add + 1; // the maximum number of subs derived
1545 // from a block of sub_to_add+1 subs
1546 placeholder = (int **) malloc(sizeof(int *) * counter);
1547 for (i = 0; i < counter; ++i) {
1548 placeholder[i] = (int *) malloc(sizeof(int) * lines_to_add);
1549 for (j = 0; j < lines_to_add; ++j) {
1550 placeholder[i][j] = -1;
1554 counter = 0;
1555 local_end = global_start - 1;
1556 do {
1557 int ls;
1559 // here we find the beginning and the end of a new
1560 // subtitle in the block
1561 local_start = local_end + 1;
1562 local_end = global_end;
1563 for (j = 0; j <= sub_to_add; ++j) {
1564 if ((first[sub_first + j].start - 1 > local_start) && (first[sub_first + j].start - 1 < local_end)) {
1565 local_end = first[sub_first + j].start - 1;
1566 } else if ((first[sub_first + j].end > local_start) && (first[sub_first + j].end < local_end)) {
1567 local_end = first[sub_first + j].end;
1570 // here we allocate the screen lines to subs we must
1571 // display in current local_start-local_end interval.
1572 // if the subs were yet presents in the previous interval
1573 // they keep the same lines, otherside they get unused lines
1574 for (j = 0; j <= sub_to_add; ++j) {
1575 if ((first[sub_first + j].start <= local_end) && (first[sub_first + j].end > local_start)) {
1576 unsigned long sub_lines = first[sub_first + j].lines, fragment_length = lines_to_add + 1,
1577 tmp = 0;
1578 char boolean = 0;
1579 int fragment_position = -1;
1581 // if this is not the first new sub of the block
1582 // we find if this sub was present in the previous
1583 // new sub
1584 if (counter)
1585 for (i = 0; i < lines_to_add; ++i) {
1586 if (placeholder[counter - 1][i] == sub_first + j) {
1587 placeholder[counter][i] = sub_first + j;
1588 boolean = 1;
1591 if (boolean)
1592 continue;
1594 // we are looking for the shortest among all groups of
1595 // sequential blank lines whose length is greater than or
1596 // equal to sub_lines. we store in fragment_position the
1597 // position of the shortest group, in fragment_length its
1598 // length, and in tmp the length of the group currently
1599 // examinated
1600 for (i = 0; i < lines_to_add; ++i) {
1601 if (placeholder[counter][i] == -1) {
1602 // placeholder[counter][i] is part of the current group
1603 // of blank lines
1604 ++tmp;
1605 } else {
1606 if (tmp == sub_lines) {
1607 // current group's size fits exactly the one we
1608 // need, so we stop looking
1609 fragment_position = i - tmp;
1610 tmp = 0;
1611 break;
1613 if ((tmp) && (tmp > sub_lines) && (tmp < fragment_length)) {
1614 // current group is the best we found till here,
1615 // but is still bigger than the one we are looking
1616 // for, so we keep on looking
1617 fragment_length = tmp;
1618 fragment_position = i - tmp;
1619 tmp = 0;
1620 } else {
1621 // current group doesn't fit at all, so we forget it
1622 tmp = 0;
1626 if (tmp) {
1627 // last screen line is blank, a group ends with it
1628 if ((tmp >= sub_lines) && (tmp < fragment_length)) {
1629 fragment_position = i - tmp;
1632 if (fragment_position == -1) {
1633 // it was not possible to find free screen line(s) for a subtitle,
1634 // usually this means a bug in the code; however we do not overlap
1635 mp_msg(MSGT_SUBREADER, MSGL_WARN, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1636 higher_line = SUB_MAX_TEXT + 1;
1637 break;
1638 } else {
1639 for (tmp = 0; tmp < sub_lines; ++tmp) {
1640 placeholder[counter][fragment_position + tmp] = sub_first + j;
1645 for (j = higher_line + 1; j < lines_to_add; ++j) {
1646 if (placeholder[counter][j] != -1)
1647 higher_line = j;
1648 else
1649 break;
1651 if (higher_line >= SUB_MAX_TEXT) {
1652 // the 'block' has too much lines, so we don't overlap the
1653 // subtitles
1654 second = (subtitle *) realloc(second, (sub_num + sub_to_add + 1) * sizeof(subtitle));
1655 for (j = 0; j <= sub_to_add; ++j) {
1656 int ls;
1657 memset(&second[sub_num + j], '\0', sizeof(subtitle));
1658 second[sub_num + j].start = first[sub_first + j].start;
1659 second[sub_num + j].end = first[sub_first + j].end;
1660 second[sub_num + j].lines = first[sub_first + j].lines;
1661 second[sub_num + j].alignment = first[sub_first + j].alignment;
1662 for (ls = 0; ls < second[sub_num + j].lines; ls++) {
1663 second[sub_num + j].text[ls] = strdup(first[sub_first + j].text[ls]);
1666 sub_num += sub_to_add + 1;
1667 sub_first += sub_to_add;
1668 real_block = 0;
1669 break;
1672 // we read the placeholder structure and create the new
1673 // subs.
1674 second = (subtitle *) realloc(second, (sub_num + 1) * sizeof(subtitle));
1675 memset(&second[sub_num], '\0', sizeof(subtitle));
1676 second[sub_num].start = local_start;
1677 second[sub_num].end = local_end;
1678 second[sub_num].alignment = first[sub_first].alignment;
1679 n_max = (lines_to_add < SUB_MAX_TEXT) ? lines_to_add : SUB_MAX_TEXT;
1680 for (i = 0, j = 0; j < n_max; ++j) {
1681 if (placeholder[counter][j] != -1) {
1682 int lines = first[placeholder[counter][j]].lines;
1683 for (ls = 0; ls < lines; ++ls) {
1684 second[sub_num].text[i++] = strdup(first[placeholder[counter][j]].text[ls]);
1686 j += lines - 1;
1687 } else {
1688 second[sub_num].text[i++] = strdup(" ");
1691 ++sub_num;
1692 ++counter;
1693 } while (local_end < global_end);
1694 if (real_block)
1695 for (i = 0; i < counter; ++i)
1696 second[start_block_sub + i].lines = higher_line + 1;
1698 counter = 2 * sub_to_add + 1;
1699 for (i = 0; i < counter; ++i) {
1700 free(placeholder[i]);
1702 free(placeholder);
1703 sub_first += sub_to_add;
1706 for (j = sub_orig - 1; j >= 0; --j) {
1707 for (i = first[j].lines - 1; i >= 0; --i) {
1708 free(first[j].text[i]);
1711 free(first);
1713 return_sub = second;
1714 } else { //if(suboverlap_enabled)
1715 adjust_subs_time(first, 6.0, fps, 1, sub_num, uses_time);/*~6 secs AST*/
1716 return_sub = first;
1718 if (return_sub == NULL) return NULL;
1719 subt_data = (sub_data *)malloc(sizeof(sub_data));
1720 subt_data->filename = strdup(filename);
1721 subt_data->sub_uses_time = uses_time;
1722 subt_data->sub_num = sub_num;
1723 subt_data->sub_errs = sub_errs;
1724 subt_data->subtitles = return_sub;
1725 return subt_data;
1728 #if 0
1729 char * strreplace( char * in,char * what,char * whereof )
1731 int i;
1732 char * tmp;
1734 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
1735 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
1736 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
1737 return in;
1739 #endif
1742 static void strcpy_trim(char *d, char *s)
1744 // skip leading whitespace
1745 while (*s && !isalnum(*s)) {
1746 s++;
1748 for (;;) {
1749 // copy word
1750 while (*s && isalnum(*s)) {
1751 *d = tolower(*s);
1752 s++; d++;
1754 if (*s == 0) break;
1755 // trim excess whitespace
1756 while (*s && !isalnum(*s)) {
1757 s++;
1759 if (*s == 0) break;
1760 *d++ = ' ';
1762 *d = 0;
1765 static void strcpy_strip_ext(char *d, char *s)
1767 char *tmp = strrchr(s,'.');
1768 if (!tmp) {
1769 strcpy(d, s);
1770 return;
1771 } else {
1772 strncpy(d, s, tmp-s);
1773 d[tmp-s] = 0;
1775 while (*d) {
1776 *d = tolower(*d);
1777 d++;
1781 static void strcpy_get_ext(char *d, char *s)
1783 char *tmp = strrchr(s,'.');
1784 if (!tmp) {
1785 strcpy(d, "");
1786 return;
1787 } else {
1788 strcpy(d, tmp+1);
1792 static int whiteonly(char *s)
1794 while (*s) {
1795 if (isalnum(*s)) return 0;
1796 s++;
1798 return 1;
1801 typedef struct _subfn
1803 int priority;
1804 char *fname;
1805 } subfn;
1807 static int compare_sub_priority(const void *a, const void *b)
1809 if (((subfn*)a)->priority > ((subfn*)b)->priority) {
1810 return -1;
1811 } else if (((subfn*)a)->priority < ((subfn*)b)->priority) {
1812 return 1;
1813 } else {
1814 return strcoll(((subfn*)a)->fname, ((subfn*)b)->fname);
1818 char** sub_filenames(char* path, char *fname)
1820 char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
1821 char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
1823 int len, pos, found, i, j;
1824 char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
1825 subfn *result;
1826 char **result2;
1828 int subcnt;
1830 FILE *f;
1832 DIR *d;
1833 struct dirent *de;
1835 len = (strlen(fname) > 256 ? strlen(fname) : 256)
1836 +(strlen(path) > 256 ? strlen(path) : 256)+2;
1838 f_dir = (char*)malloc(len);
1839 f_fname = (char*)malloc(len);
1840 f_fname_noext = (char*)malloc(len);
1841 f_fname_trim = (char*)malloc(len);
1843 tmp_fname_noext = (char*)malloc(len);
1844 tmp_fname_trim = (char*)malloc(len);
1845 tmp_fname_ext = (char*)malloc(len);
1847 tmpresult = (char*)malloc(len);
1849 result = (subfn*)malloc(sizeof(subfn)*MAX_SUBTITLE_FILES);
1850 memset(result, 0, sizeof(subfn)*MAX_SUBTITLE_FILES);
1852 subcnt = 0;
1854 tmp = strrchr(fname,'/');
1855 #ifdef WIN32
1856 if(!tmp)tmp = strrchr(fname,'\\');
1857 #endif
1859 // extract filename & dirname from fname
1860 if (tmp) {
1861 strcpy(f_fname, tmp+1);
1862 pos = tmp - fname;
1863 strncpy(f_dir, fname, pos+1);
1864 f_dir[pos+1] = 0;
1865 } else {
1866 strcpy(f_fname, fname);
1867 strcpy(f_dir, "./");
1870 strcpy_strip_ext(f_fname_noext, f_fname);
1871 strcpy_trim(f_fname_trim, f_fname_noext);
1873 tmp_sub_id = NULL;
1874 if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
1875 tmp_sub_id = (char*)malloc(strlen(dvdsub_lang)+1);
1876 strcpy_trim(tmp_sub_id, dvdsub_lang);
1879 // 0 = nothing
1880 // 1 = any subtitle file
1881 // 2 = any sub file containing movie name
1882 // 3 = sub file containing movie name and the lang extension
1883 for (j = 0; j <= 1; j++) {
1884 d = opendir(j == 0 ? f_dir : path);
1885 if (d) {
1886 while ((de = readdir(d))) {
1887 // retrieve various parts of the filename
1888 strcpy_strip_ext(tmp_fname_noext, de->d_name);
1889 strcpy_get_ext(tmp_fname_ext, de->d_name);
1890 strcpy_trim(tmp_fname_trim, tmp_fname_noext);
1892 // does it end with a subtitle extension?
1893 found = 0;
1894 #ifdef USE_ICONV
1895 #ifdef HAVE_ENCA
1896 for (i = ((sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0); sub_exts[i]; i++) {
1897 #else
1898 for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
1899 #endif
1900 #else
1901 for (i = 0; sub_exts[i]; i++) {
1902 #endif
1903 if (strcasecmp(sub_exts[i], tmp_fname_ext) == 0) {
1904 found = 1;
1905 break;
1909 // we have a (likely) subtitle file
1910 if (found) {
1911 int prio = 0;
1912 if (!prio && tmp_sub_id)
1914 sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id);
1915 printf("dvdsublang...%s\n", tmpresult);
1916 if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) {
1917 // matches the movie name + lang extension
1918 prio = 5;
1921 if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
1922 // matches the movie name
1923 prio = 4;
1925 if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && (sub_match_fuzziness >= 1)) {
1926 // contains the movie name
1927 tmp += strlen(f_fname_trim);
1928 if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
1929 // with sub_id specified prefer localized subtitles
1930 prio = 3;
1931 } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
1932 // without sub_id prefer "plain" name
1933 prio = 3;
1934 } else {
1935 // with no localized subs found, try any else instead
1936 prio = 2;
1939 if (!prio) {
1940 // doesn't contain the movie name
1941 // don't try in the mplayer subtitle directory
1942 if ((j == 0) && (sub_match_fuzziness >= 2)) {
1943 prio = 1;
1947 if (prio) {
1948 prio += prio;
1949 #ifdef USE_ICONV
1950 if (i<3){ // prefer UTF-8 coded
1951 prio++;
1953 #endif
1954 sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
1955 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1956 if ((f = fopen(tmpresult, "rt"))) {
1957 fclose(f);
1958 result[subcnt].priority = prio;
1959 result[subcnt].fname = strdup(tmpresult);
1960 subcnt++;
1965 if (subcnt >= MAX_SUBTITLE_FILES) break;
1967 closedir(d);
1972 if (tmp_sub_id) free(tmp_sub_id);
1974 free(f_dir);
1975 free(f_fname);
1976 free(f_fname_noext);
1977 free(f_fname_trim);
1979 free(tmp_fname_noext);
1980 free(tmp_fname_trim);
1981 free(tmp_fname_ext);
1983 free(tmpresult);
1985 qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
1987 result2 = (char**)malloc(sizeof(char*)*(subcnt+1));
1988 memset(result2, 0, sizeof(char*)*(subcnt+1));
1990 for (i = 0; i < subcnt; i++) {
1991 result2[i] = result[i].fname;
1993 result2[subcnt] = NULL;
1995 free(result);
1997 return result2;
2000 void list_sub_file(sub_data* subd){
2001 int i,j;
2002 subtitle *subs = subd->subtitles;
2004 for(j=0; j < subd->sub_num; j++){
2005 subtitle* egysub=&subs[j];
2006 printf ("%i line%c (%li-%li)\n",
2007 egysub->lines,
2008 (1==egysub->lines)?' ':'s',
2009 egysub->start,
2010 egysub->end);
2011 for (i=0; i<egysub->lines; i++) {
2012 printf ("\t\t%d: %s%s", i,egysub->text[i], i==egysub->lines-1?"":" \n ");
2014 printf ("\n");
2017 printf ("Subtitle format %s time.\n",
2018 subd->sub_uses_time ? "uses":"doesn't use");
2019 printf ("Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);
2022 void dump_srt(sub_data* subd, float fps){
2023 int i,j;
2024 int h,m,s,ms;
2025 FILE * fd;
2026 subtitle * onesub;
2027 unsigned long temp;
2028 subtitle *subs = subd->subtitles;
2030 if (!subd->sub_uses_time && sub_fps == 0)
2031 sub_fps = fps;
2032 fd=fopen("dumpsub.srt","w");
2033 if(!fd)
2035 perror("dump_srt: fopen");
2036 return;
2038 for(i=0; i < subd->sub_num; i++)
2040 onesub=subs+i; //=&subs[i];
2041 fprintf(fd,"%d\n",i+1);//line number
2043 temp=onesub->start;
2044 if (!subd->sub_uses_time)
2045 temp = temp * 100 / sub_fps;
2046 temp -= sub_delay * 100;
2047 h=temp/360000;temp%=360000; //h =1*100*60*60
2048 m=temp/6000; temp%=6000; //m =1*100*60
2049 s=temp/100; temp%=100; //s =1*100
2050 ms=temp*10; //ms=1*10
2051 fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms);
2053 temp=onesub->end;
2054 if (!subd->sub_uses_time)
2055 temp = temp * 100 / sub_fps;
2056 temp -= sub_delay * 100;
2057 h=temp/360000;temp%=360000;
2058 m=temp/6000; temp%=6000;
2059 s=temp/100; temp%=100;
2060 ms=temp*10;
2061 fprintf(fd,"%02d:%02d:%02d,%03d\n",h,m,s,ms);
2063 for(j=0;j<onesub->lines;j++)
2064 fprintf(fd,"%s\n",onesub->text[j]);
2066 fprintf(fd,"\n");
2068 fclose(fd);
2069 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2072 void dump_mpsub(sub_data* subd, float fps){
2073 int i,j;
2074 FILE *fd;
2075 float a,b;
2076 subtitle *subs = subd->subtitles;
2078 mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps);
2079 if (sub_fps==0) sub_fps=fps;
2081 fd=fopen ("dump.mpsub", "w");
2082 if (!fd) {
2083 perror ("dump_mpsub: fopen");
2084 return;
2088 if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
2089 else fprintf (fd, "FORMAT=%5.2f\n\n", fps);
2091 for(j=0; j < subd->sub_num; j++){
2092 subtitle* egysub=&subs[j];
2093 if (subd->sub_uses_time) {
2094 a=((egysub->start-mpsub_position)/100.0);
2095 b=((egysub->end-egysub->start)/100.0);
2096 if ( (float)((int)a) == a)
2097 fprintf (fd, "%.0f",a);
2098 else
2099 fprintf (fd, "%.2f",a);
2101 if ( (float)((int)b) == b)
2102 fprintf (fd, " %.0f\n",b);
2103 else
2104 fprintf (fd, " %.2f\n",b);
2105 } else {
2106 fprintf (fd, "%ld %ld\n", (long)((egysub->start*(fps/sub_fps))-((mpsub_position*(fps/sub_fps)))),
2107 (long)(((egysub->end)-(egysub->start))*(fps/sub_fps)));
2110 mpsub_position = egysub->end;
2111 for (i=0; i<egysub->lines; i++) {
2112 fprintf (fd, "%s\n",egysub->text[i]);
2114 fprintf (fd, "\n");
2116 fclose (fd);
2117 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2120 void dump_microdvd(sub_data* subd, float fps) {
2121 int i, delay;
2122 FILE *fd;
2123 subtitle *subs = subd->subtitles;
2124 if (sub_fps == 0)
2125 sub_fps = fps;
2126 fd = fopen("dumpsub.txt", "w");
2127 if (!fd) {
2128 perror("dumpsub.txt: fopen");
2129 return;
2131 delay = sub_delay * sub_fps;
2132 for (i = 0; i < subd->sub_num; ++i) {
2133 int j, start, end;
2134 start = subs[i].start;
2135 end = subs[i].end;
2136 if (subd->sub_uses_time) {
2137 start = start * sub_fps / 100 ;
2138 end = end * sub_fps / 100;
2140 else {
2141 start = start * sub_fps / fps;
2142 end = end * sub_fps / fps;
2144 start -= delay;
2145 end -= delay;
2146 fprintf(fd, "{%d}{%d}", start, end);
2147 for (j = 0; j < subs[i].lines; ++j)
2148 fprintf(fd, "%s%s", j ? "|" : "", subs[i].text[j]);
2149 fprintf(fd, "\n");
2151 fclose(fd);
2152 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
2155 void dump_jacosub(sub_data* subd, float fps) {
2156 int i,j;
2157 int h,m,s,cs;
2158 FILE * fd;
2159 subtitle * onesub;
2160 unsigned long temp;
2161 subtitle *subs = subd->subtitles;
2163 if (!subd->sub_uses_time && sub_fps == 0)
2164 sub_fps = fps;
2165 fd=fopen("dumpsub.jss","w");
2166 if(!fd)
2168 perror("dump_jacosub: fopen");
2169 return;
2171 fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps);
2172 for(i=0; i < subd->sub_num; i++)
2174 onesub=subs+i; //=&subs[i];
2176 temp=onesub->start;
2177 if (!subd->sub_uses_time)
2178 temp = temp * 100 / sub_fps;
2179 temp -= sub_delay * 100;
2180 h=temp/360000;temp%=360000; //h =1*100*60*60
2181 m=temp/6000; temp%=6000; //m =1*100*60
2182 s=temp/100; temp%=100; //s =1*100
2183 cs=temp; //cs=1*10
2184 fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs);
2186 temp=onesub->end;
2187 if (!subd->sub_uses_time)
2188 temp = temp * 100 / sub_fps;
2189 temp -= sub_delay * 100;
2190 h=temp/360000;temp%=360000;
2191 m=temp/6000; temp%=6000;
2192 s=temp/100; temp%=100;
2193 cs=temp;
2194 fprintf(fd,"%02d:%02d:%02d.%02d {~} ",h,m,s,cs);
2196 for(j=0;j<onesub->lines;j++)
2197 fprintf(fd,"%s%s",j ? "\\n" : "", onesub->text[j]);
2199 fprintf(fd,"\n");
2201 fclose(fd);
2202 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2205 void dump_sami(sub_data* subd, float fps) {
2206 int i,j;
2207 FILE * fd;
2208 subtitle * onesub;
2209 unsigned long temp;
2210 subtitle *subs = subd->subtitles;
2212 if (!subd->sub_uses_time && sub_fps == 0)
2213 sub_fps = fps;
2214 fd=fopen("dumpsub.smi","w");
2215 if(!fd)
2217 perror("dump_jacosub: fopen");
2218 return;
2220 fprintf(fd, "<SAMI>\n"
2221 "<HEAD>\n"
2222 " <STYLE TYPE=\"Text/css\">\n"
2223 " <!--\n"
2224 " 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"
2225 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2226 " -->\n"
2227 " </STYLE>\n"
2228 "</HEAD>\n"
2229 "<BODY>\n");
2230 for(i=0; i < subd->sub_num; i++)
2232 onesub=subs+i; //=&subs[i];
2234 temp=onesub->start;
2235 if (!subd->sub_uses_time)
2236 temp = temp * 100 / sub_fps;
2237 temp -= sub_delay * 100;
2238 fprintf(fd,"\t<SYNC Start=%lu>\n"
2239 "\t <P>", temp * 10);
2241 for(j=0;j<onesub->lines;j++)
2242 fprintf(fd,"%s%s",j ? "<br>" : "", onesub->text[j]);
2244 fprintf(fd,"\n");
2246 temp=onesub->end;
2247 if (!subd->sub_uses_time)
2248 temp = temp * 100 / sub_fps;
2249 temp -= sub_delay * 100;
2250 fprintf(fd,"\t<SYNC Start=%lu>\n"
2251 "\t <P>&nbsp;\n", temp * 10);
2253 fprintf(fd, "</BODY>\n"
2254 "</SAMI>\n");
2255 fclose(fd);
2256 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2259 void sub_free( sub_data * subd )
2261 int i;
2263 if ( !subd ) return;
2265 if (subd->subtitles) {
2266 for (i=0; i < subd->subtitles->lines; i++) free( subd->subtitles->text[i] );
2267 free( subd->subtitles );
2269 if (subd->filename) free( subd->filename );
2270 free( subd );
2273 #ifdef DUMPSUBS
2274 int main(int argc, char **argv) { // for testing
2275 sub_data *subd;
2277 if(argc<2){
2278 printf("\nUsage: subreader filename.sub\n\n");
2279 exit(1);
2281 sub_cp = argv[2];
2282 subd = sub_read_file(argv[1]);
2283 if(!subd){
2284 printf("Couldn't load file.\n");
2285 exit(1);
2288 list_sub_file(subd);
2290 return 0;
2292 #endif