adding the code documentation guide lines
[mplayer/glamo.git] / subreader.c
blob07d6346004540540f9a13e508cca0fbe1fd593d1
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;
35 int flip_hebrew = 1;
36 #endif
38 extern char* dvdsub_lang;
40 /* Maximal length of line of a subtitle */
41 #define LINE_LEN 1000
42 static float mpsub_position=0;
43 static float mpsub_multiplier=1.;
44 static int sub_slacktime = 20000; //20 sec
46 int sub_no_text_pp=0; // 1 => do not apply text post-processing
47 // like {\...} elimination in SSA format.
49 int sub_match_fuzziness=0; // level of sub name matching fuzziness
51 /* Use the SUB_* constant defined in the header file */
52 int sub_format=SUB_INVALID;
53 #ifdef USE_SORTSUB
54 /*
55 Some subtitling formats, namely AQT and Subrip09, define the end of a
56 subtitle as the beginning of the following. Since currently we read one
57 subtitle at time, for these format we keep two global *subtitle,
58 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
59 so we can change its end when we read current subtitle starting time.
60 When USE_SORTSUB is defined, we use a single global unsigned long,
61 previous_sub_end, for both (and even future) formats, to store the end of
62 the previous sub: it is initialized to 0 in sub_read_file and eventually
63 modified by sub_read_aqt_line or sub_read_subrip09_line.
65 unsigned long previous_sub_end;
66 #endif
68 static int eol(char p) {
69 return (p=='\r' || p=='\n' || p=='\0');
72 /* Remove leading and trailing space */
73 static void trail_space(char *s) {
74 int i = 0;
75 while (isspace(s[i])) ++i;
76 if (i) strcpy(s, s + i);
77 i = strlen(s) - 1;
78 while (i > 0 && isspace(s[i])) s[i--] = '\0';
81 static char *stristr(const char *haystack, const char *needle) {
82 int len = 0;
83 const char *p = haystack;
85 if (!(haystack && needle)) return NULL;
87 len=strlen(needle);
88 while (*p != '\0') {
89 if (strncasecmp(p, needle, len) == 0) return (char*)p;
90 p++;
93 return NULL;
96 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) {
97 static char line[LINE_LEN+1];
98 static char *s = NULL, *slacktime_s;
99 char text[LINE_LEN+1], *p=NULL, *q;
100 int state;
102 current->lines = current->start = current->end = 0;
103 state = 0;
105 /* read the first line */
106 if (!s)
107 if (!(s = fgets(line, LINE_LEN, fd))) return 0;
109 do {
110 switch (state) {
112 case 0: /* find "START=" or "Slacktime:" */
113 slacktime_s = stristr (s, "Slacktime:");
114 if (slacktime_s)
115 sub_slacktime = strtol (slacktime_s+10, NULL, 0) / 10;
117 s = stristr (s, "Start=");
118 if (s) {
119 current->start = strtol (s + 6, &s, 0) / 10;
120 /* eat '>' */
121 for (; *s != '>' && *s != '\0'; s++);
122 s++;
123 state = 1; continue;
125 break;
127 case 1: /* find (optionnal) "<P", skip other TAGs */
128 for (; *s == ' ' || *s == '\t'; s++); /* strip blanks, if any */
129 if (*s == '\0') break;
130 if (*s != '<') { state = 3; p = text; continue; } /* not a TAG */
131 s++;
132 if (*s == 'P' || *s == 'p') { s++; state = 2; continue; } /* found '<P' */
133 for (; *s != '>' && *s != '\0'; s++); /* skip remains of non-<P> TAG */
134 if (s == '\0')
135 break;
136 s++;
137 continue;
139 case 2: /* find ">" */
140 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; }
141 break;
143 case 3: /* get all text until '<' appears */
144 if (*s == '\0') break;
145 else if (!strncasecmp (s, "<br>", 4)) {
146 *p = '\0'; p = text; trail_space (text);
147 if (text[0] != '\0')
148 current->text[current->lines++] = strdup (text);
149 s += 4;
151 else if ((*s == '{') && !sub_no_text_pp) { state = 5; ++s; continue; }
152 else if (*s == '<') { state = 4; }
153 else if (!strncasecmp (s, "&nbsp;", 6)) { *p++ = ' '; s += 6; }
154 else if (*s == '\t') { *p++ = ' '; s++; }
155 else if (*s == '\r' || *s == '\n') { s++; }
156 else *p++ = *s++;
158 /* skip duplicated space */
159 if (p > text + 2) if (*(p-1) == ' ' && *(p-2) == ' ') p--;
161 continue;
163 case 4: /* get current->end or skip <TAG> */
164 q = stristr (s, "Start=");
165 if (q) {
166 current->end = strtol (q + 6, &q, 0) / 10 - 1;
167 *p = '\0'; trail_space (text);
168 if (text[0] != '\0')
169 current->text[current->lines++] = strdup (text);
170 if (current->lines > 0) { state = 99; break; }
171 state = 0; continue;
173 s = strchr (s, '>');
174 if (s) { s++; state = 3; continue; }
175 break;
176 case 5: /* get rid of {...} text */
177 if (*s == '}') state = 3;
178 ++s;
179 continue;
182 /* read next line */
183 if (state != 99 && !(s = fgets (line, LINE_LEN, fd))) {
184 if (current->start > 0) {
185 break; // if it is the last subtitle
186 } else {
187 return 0;
191 } while (state != 99);
193 // For the last subtitle
194 if (current->end <= 0) {
195 current->end = current->start + sub_slacktime;
196 *p = '\0'; trail_space (text);
197 if (text[0] != '\0')
198 current->text[current->lines++] = strdup (text);
201 return current;
205 char *sub_readtext(char *source, char **dest) {
206 int len=0;
207 char *p=source;
209 // printf("src=%p dest=%p \n",source,dest);
211 while ( !eol(*p) && *p!= '|' ) {
212 p++,len++;
215 *dest= (char *)malloc (len+1);
216 if (!dest) {return ERR;}
218 strncpy(*dest, source, len);
219 (*dest)[len]=0;
221 while (*p=='\r' || *p=='\n' || *p=='|') p++;
223 if (*p) return p; // not-last text field
224 else return NULL; // last text field
227 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
228 char line[LINE_LEN+1];
229 char line2[LINE_LEN+1];
230 char *p, *next;
231 int i;
233 do {
234 if (!fgets (line, LINE_LEN, fd)) return NULL;
235 } while ((sscanf (line,
236 "{%ld}{}%[^\r\n]",
237 &(current->start), line2) < 2) &&
238 (sscanf (line,
239 "{%ld}{%ld}%[^\r\n]",
240 &(current->start), &(current->end), line2) < 3));
242 p=line2;
244 next=p, i=0;
245 while ((next =sub_readtext (next, &(current->text[i])))) {
246 if (current->text[i]==ERR) {return ERR;}
247 i++;
248 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
250 current->lines= ++i;
252 return current;
255 subtitle *sub_read_line_mpl2(FILE *fd,subtitle *current) {
256 char line[LINE_LEN+1];
257 char line2[LINE_LEN+1];
258 char *p, *next;
259 int i;
261 do {
262 if (!fgets (line, LINE_LEN, fd)) return NULL;
263 } while ((sscanf (line,
264 "[%ld][%ld]%[^\r\n]",
265 &(current->start), &(current->end), line2) < 3));
266 current->start *= 10;
267 current->end *= 10;
268 p=line2;
270 next=p, i=0;
271 while ((next =sub_readtext (next, &(current->text[i])))) {
272 if (current->text[i]==ERR) {return ERR;}
273 i++;
274 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
276 current->lines= ++i;
278 return current;
281 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
282 char line[LINE_LEN+1];
283 int a1,a2,a3,a4,b1,b2,b3,b4;
284 char *p=NULL, *q=NULL;
285 int len;
287 while (1) {
288 if (!fgets (line, LINE_LEN, fd)) return NULL;
289 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
290 current->start = a1*360000+a2*6000+a3*100+a4;
291 current->end = b1*360000+b2*6000+b3*100+b4;
293 if (!fgets (line, LINE_LEN, fd)) return NULL;
295 p=q=line;
296 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
297 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && *p!='|' && strncmp(p,"[br]",4); p++,len++);
298 current->text[current->lines-1]=(char *)malloc (len+1);
299 if (!current->text[current->lines-1]) return ERR;
300 strncpy (current->text[current->lines-1], q, len);
301 current->text[current->lines-1][len]='\0';
302 if (!*p || *p=='\r' || *p=='\n') break;
303 if (*p=='|') p++;
304 else while (*p++!=']');
306 break;
308 return current;
311 subtitle *sub_read_line_subviewer(FILE *fd,subtitle *current) {
312 char line[LINE_LEN+1];
313 int a1,a2,a3,a4,b1,b2,b3,b4;
314 char *p=NULL;
315 int i,len;
317 while (!current->text[0]) {
318 if (!fgets (line, LINE_LEN, fd)) return NULL;
319 if ((len=sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",&a1,&a2,&a3,(char *)&i,&a4,&b1,&b2,&b3,(char *)&i,&b4)) < 10)
320 continue;
321 current->start = a1*360000+a2*6000+a3*100+a4/10;
322 current->end = b1*360000+b2*6000+b3*100+b4/10;
323 for (i=0; i<SUB_MAX_TEXT;) {
324 if (!fgets (line, LINE_LEN, fd)) break;
325 len=0;
326 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
327 if (len) {
328 int j=0,skip=0;
329 char *curptr=current->text[i]=(char *)malloc (len+1);
330 if (!current->text[i]) return ERR;
331 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
332 for(; j<len; j++) {
333 /* let's filter html tags ::atmos */
334 if(line[j]=='>') {
335 skip=0;
336 continue;
338 if(line[j]=='<') {
339 skip=1;
340 continue;
342 if(skip) {
343 continue;
345 *curptr=line[j];
346 curptr++;
348 *curptr='\0';
350 i++;
351 } else {
352 break;
355 current->lines=i;
357 return current;
360 subtitle *sub_read_line_subviewer2(FILE *fd,subtitle *current) {
361 char line[LINE_LEN+1];
362 int a1,a2,a3,a4;
363 char *p=NULL;
364 int i,len;
366 while (!current->text[0]) {
367 if (!fgets (line, LINE_LEN, fd)) return NULL;
368 if (line[0]!='{')
369 continue;
370 if ((len=sscanf (line, "{T %d:%d:%d:%d",&a1,&a2,&a3,&a4)) < 4)
371 continue;
372 current->start = a1*360000+a2*6000+a3*100+a4/10;
373 for (i=0; i<SUB_MAX_TEXT;) {
374 if (!fgets (line, LINE_LEN, fd)) break;
375 if (line[0]=='}') break;
376 len=0;
377 for (p=line; *p!='\n' && *p!='\r' && *p; ++p,++len);
378 if (len) {
379 current->text[i]=(char *)malloc (len+1);
380 if (!current->text[i]) return ERR;
381 strncpy (current->text[i], line, len); current->text[i][len]='\0';
382 ++i;
383 } else {
384 break;
387 current->lines=i;
389 return current;
393 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) {
394 char line[LINE_LEN+1];
395 int a1,a2,a3;
396 char *p=NULL, *next,separator;
397 int i,len,plen;
399 while (!current->text[0]) {
400 if (!fgets (line, LINE_LEN, fd)) return NULL;
401 if ((len=sscanf (line, "%d:%d:%d%c%n",&a1,&a2,&a3,&separator,&plen)) < 4)
402 continue;
404 if (!(current->start = a1*360000+a2*6000+a3*100))
405 continue;
406 /* removed by wodzu
407 p=line;
408 // finds the body of the subtitle
409 for (i=0; i<3; i++){
410 p=strchr(p,':');
411 if (p==NULL) break;
412 ++p;
414 if (p==NULL) {
415 printf("SUB: Skipping incorrect subtitle line!\n");
416 continue;
419 // by wodzu: hey! this time we know what length it has! what is
420 // that magic for? it can't deal with space instead of third
421 // colon! look, what simple it can be:
422 p = &line[ plen ];
424 i=0;
425 if (*p!='|') {
427 next = p,i=0;
428 while ((next =sub_readtext (next, &(current->text[i])))) {
429 if (current->text[i]==ERR) {return ERR;}
430 i++;
431 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
433 current->lines=i+1;
436 return current;
439 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) {
440 //TODO: This format uses quite rich (sub/super)set of xhtml
441 // I couldn't check it since DTD is not included.
442 // WARNING: full XML parses can be required for proper parsing
443 char line[LINE_LEN+1];
444 int a1,a2,a3,a4,b1,b2,b3,b4;
445 char *p=NULL,*next=NULL;
446 int i,len,plen;
448 while (!current->text[0]) {
449 if (!fgets (line, LINE_LEN, fd)) return NULL;
450 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
451 //to describe the same moment in time. Maybe there are even more formats in use.
452 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
453 plen=a1=a2=a3=a4=b1=b2=b3=b4=0;
454 if (
455 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen)) < 4) &&
456 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen)) < 5) &&
457 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) &&
458 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) &&
459 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
460 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&b4,&plen)) < 6) &&
461 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4,&plen)) < 8) &&
462 //now try it without end time
463 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen)) < 2) &&
464 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen)) < 2) &&
465 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen)) < 3) &&
466 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen)) < 4)
468 continue;
469 current->start = a1*360000+a2*6000+a3*100+a4/10;
470 current->end = b1*360000+b2*6000+b3*100+b4/10;
471 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)
472 current->end = current->start+200;
473 p=line; p+=plen;i=0;
474 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
475 next = strstr(line,"<clear/>");
476 if(next && strlen(next)>8){
477 next+=8;i=0;
478 while ((next =sub_readtext (next, &(current->text[i])))) {
479 if (current->text[i]==ERR) {return ERR;}
480 i++;
481 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
484 current->lines=i+1;
486 return current;
489 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) {
491 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
492 * other Sub Station Alpha scripts have only 8 commas before subtitle
493 * Reading the "ScriptType:" field is not reliable since many scripts appear
494 * w/o it
496 * http://www.scriptclub.org is a good place to find more examples
497 * http://www.eswat.demon.co.uk is where the SSA specs can be found
499 int comma;
500 static int max_comma = 32; /* let's use 32 for the case that the */
501 /* amount of commas increase with newer SSA versions */
503 int hour1, min1, sec1, hunsec1,
504 hour2, min2, sec2, hunsec2, nothing;
505 int num;
507 char line[LINE_LEN+1],
508 line3[LINE_LEN+1],
509 *line2;
510 char *tmp;
512 do {
513 if (!fgets (line, LINE_LEN, fd)) return NULL;
514 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
515 "%[^\n\r]", &nothing,
516 &hour1, &min1, &sec1, &hunsec1,
517 &hour2, &min2, &sec2, &hunsec2,
518 line3) < 9
520 sscanf (line, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
521 "%[^\n\r]", &nothing,
522 &hour1, &min1, &sec1, &hunsec1,
523 &hour2, &min2, &sec2, &hunsec2,
524 line3) < 9 );
526 line2=strchr(line3, ',');
528 for (comma = 4; comma < max_comma; comma ++)
530 tmp = line2;
531 if(!(tmp=strchr(++tmp, ','))) break;
532 if(*(++tmp) == ' ') break;
533 /* a space after a comma means we're already in a sentence */
534 line2 = tmp;
537 if(comma < max_comma)max_comma = comma;
538 /* eliminate the trailing comma */
539 if(*line2 == ',') line2++;
541 current->lines=0;num=0;
542 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1;
543 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2;
545 while (((tmp=strstr(line2, "\\n")) != NULL) || ((tmp=strstr(line2, "\\N")) != NULL) ){
546 current->text[num]=(char *)malloc(tmp-line2+1);
547 strncpy (current->text[num], line2, tmp-line2);
548 current->text[num][tmp-line2]='\0';
549 line2=tmp+2;
550 num++;
551 current->lines++;
552 if (current->lines >= SUB_MAX_TEXT) return current;
555 current->text[num]=strdup(line2);
556 current->lines++;
558 return current;
561 void sub_pp_ssa(subtitle *sub) {
562 int l=sub->lines;
563 char *so,*de,*start;
565 while (l){
566 /* eliminate any text enclosed with {}, they are font and color settings */
567 so=de=sub->text[--l];
568 while (*so) {
569 if(*so == '{' && so[1]=='\\') {
570 for (start=so; *so && *so!='}'; so++);
571 if(*so) so++; else so=start;
573 if(*so) {
574 *de=*so;
575 so++; de++;
578 *de=*so;
583 * PJS subtitles reader.
584 * That's the "Phoenix Japanimation Society" format.
585 * I found some of them in http://www.scriptsclub.org/ (used for anime).
586 * The time is in tenths of second.
588 * by set, based on code by szabi (dunnowhat sub format ;-)
590 subtitle *sub_read_line_pjs(FILE *fd,subtitle *current) {
591 char line[LINE_LEN+1];
592 char text[LINE_LEN+1], *s, *d;
594 if (!fgets (line, LINE_LEN, fd))
595 return NULL;
596 /* skip spaces */
597 for (s=line; *s && isspace(*s); s++);
598 /* allow empty lines at the end of the file */
599 if (*s==0)
600 return NULL;
601 /* get the time */
602 if (sscanf (s, "%ld,%ld,", &(current->start),
603 &(current->end)) <2) {
604 return ERR;
606 /* the files I have are in tenths of second */
607 current->start *= 10;
608 current->end *= 10;
609 /* walk to the beggining of the string */
610 for (; *s; s++) if (*s==',') break;
611 if (*s) {
612 for (s++; *s; s++) if (*s==',') break;
613 if (*s) s++;
615 if (*s!='"') {
616 return ERR;
618 /* copy the string to the text buffer */
619 for (s++, d=text; *s && *s!='"'; s++, d++)
620 *d=*s;
621 *d=0;
622 current->text[0] = strdup(text);
623 current->lines = 1;
625 return current;
628 subtitle *sub_read_line_mpsub(FILE *fd, subtitle *current) {
629 char line[LINE_LEN+1];
630 float a,b;
631 int num=0;
632 char *p, *q;
636 if (!fgets(line, LINE_LEN, fd)) return NULL;
637 } while (sscanf (line, "%f %f", &a, &b) !=2);
639 mpsub_position += a*mpsub_multiplier;
640 current->start=(int) mpsub_position;
641 mpsub_position += b*mpsub_multiplier;
642 current->end=(int) mpsub_position;
644 while (num < SUB_MAX_TEXT) {
645 if (!fgets (line, LINE_LEN, fd)) {
646 if (num == 0) return NULL;
647 else return current;
649 p=line;
650 while (isspace(*p)) p++;
651 if (eol(*p) && num > 0) return current;
652 if (eol(*p)) return NULL;
654 for (q=p; !eol(*q); q++);
655 *q='\0';
656 if (strlen(p)) {
657 current->text[num]=strdup(p);
658 // printf (">%s<\n",p);
659 current->lines = ++num;
660 } else {
661 if (num) return current;
662 else return NULL;
665 return NULL; // we should have returned before if it's OK
668 #ifndef USE_SORTSUB
669 //we don't need this if we use previous_sub_end
670 subtitle *previous_aqt_sub = NULL;
671 #endif
673 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) {
674 char line[LINE_LEN+1];
675 char *next;
676 int i;
678 while (1) {
679 // try to locate next subtitle
680 if (!fgets (line, LINE_LEN, fd))
681 return NULL;
682 if (!(sscanf (line, "-->> %ld", &(current->start)) <1))
683 break;
686 #ifdef USE_SORTSUB
687 previous_sub_end = (current->start) ? current->start - 1 : 0;
688 #else
689 if (previous_aqt_sub != NULL)
690 previous_aqt_sub->end = current->start-1;
692 previous_aqt_sub = current;
693 #endif
695 if (!fgets (line, LINE_LEN, fd))
696 return NULL;
698 sub_readtext((char *) &line,&current->text[0]);
699 current->lines = 1;
700 current->end = current->start; // will be corrected by next subtitle
702 if (!fgets (line, LINE_LEN, fd))
703 return current;
705 next = line,i=1;
706 while ((next =sub_readtext (next, &(current->text[i])))) {
707 if (current->text[i]==ERR) {return ERR;}
708 i++;
709 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
711 current->lines=i+1;
713 if ((current->text[0]=="") && (current->text[1]=="")) {
714 #ifdef USE_SORTSUB
715 previous_sub_end = 0;
716 #else
717 // void subtitle -> end of previous marked and exit
718 previous_aqt_sub = NULL;
719 #endif
720 return NULL;
723 return current;
726 #ifndef USE_SORTSUB
727 subtitle *previous_subrip09_sub = NULL;
728 #endif
730 subtitle *sub_read_line_subrip09(FILE *fd,subtitle *current) {
731 char line[LINE_LEN+1];
732 int a1,a2,a3;
733 char * next=NULL;
734 int i,len;
736 while (1) {
737 // try to locate next subtitle
738 if (!fgets (line, LINE_LEN, fd))
739 return NULL;
740 if (!((len=sscanf (line, "[%d:%d:%d]",&a1,&a2,&a3)) < 3))
741 break;
744 current->start = a1*360000+a2*6000+a3*100;
746 #ifdef USE_SORTSUB
747 previous_sub_end = (current->start) ? current->start - 1 : 0;
748 #else
749 if (previous_subrip09_sub != NULL)
750 previous_subrip09_sub->end = current->start-1;
752 previous_subrip09_sub = current;
753 #endif
755 if (!fgets (line, LINE_LEN, fd))
756 return NULL;
758 next = line,i=0;
760 current->text[0]=""; // just to be sure that string is clear
762 while ((next =sub_readtext (next, &(current->text[i])))) {
763 if (current->text[i]==ERR) {return ERR;}
764 i++;
765 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
767 current->lines=i+1;
769 if ((current->text[0]=="") && (i==0)) {
770 #ifdef USE_SORTSUB
771 previous_sub_end = 0;
772 #else
773 // void subtitle -> end of previous marked and exit
774 previous_subrip09_sub = NULL;
775 #endif
776 return NULL;
779 return current;
782 subtitle *sub_read_line_jacosub(FILE * fd, subtitle * current)
784 char line1[LINE_LEN], line2[LINE_LEN], directive[LINE_LEN], *p, *q;
785 unsigned a1, a2, a3, a4, b1, b2, b3, b4, comment = 0;
786 static unsigned jacoTimeres = 30;
787 static int jacoShift = 0;
789 bzero(current, sizeof(subtitle));
790 bzero(line1, LINE_LEN);
791 bzero(line2, LINE_LEN);
792 bzero(directive, LINE_LEN);
793 while (!current->text[0]) {
794 if (!fgets(line1, LINE_LEN, fd)) {
795 return NULL;
797 if (sscanf
798 (line1, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1, &a2, &a3, &a4,
799 &b1, &b2, &b3, &b4, line2) < 9) {
800 if (sscanf(line1, "@%u @%u %[^\n\r]", &a4, &b4, line2) < 3) {
801 if (line1[0] == '#') {
802 int hours = 0, minutes = 0, seconds, delta, inverter =
804 unsigned units = jacoShift;
805 switch (toupper(line1[1])) {
806 case 'S':
807 if (isalpha(line1[2])) {
808 delta = 6;
809 } else {
810 delta = 2;
812 if (sscanf(&line1[delta], "%d", &hours)) {
813 if (hours < 0) {
814 hours *= -1;
815 inverter = -1;
817 if (sscanf(&line1[delta], "%*d:%d", &minutes)) {
818 if (sscanf
819 (&line1[delta], "%*d:%*d:%d",
820 &seconds)) {
821 sscanf(&line1[delta], "%*d:%*d:%*d.%d",
822 &units);
823 } else {
824 hours = 0;
825 sscanf(&line1[delta], "%d:%d.%d",
826 &minutes, &seconds, &units);
827 minutes *= inverter;
829 } else {
830 hours = minutes = 0;
831 sscanf(&line1[delta], "%d.%d", &seconds,
832 &units);
833 seconds *= inverter;
835 jacoShift =
836 ((hours * 3600 + minutes * 60 +
837 seconds) * jacoTimeres +
838 units) * inverter;
840 break;
841 case 'T':
842 if (isalpha(line1[2])) {
843 delta = 8;
844 } else {
845 delta = 2;
847 sscanf(&line1[delta], "%u", &jacoTimeres);
848 break;
851 continue;
852 } else {
853 current->start =
854 (unsigned long) ((a4 + jacoShift) * 100.0 /
855 jacoTimeres);
856 current->end =
857 (unsigned long) ((b4 + jacoShift) * 100.0 /
858 jacoTimeres);
860 } else {
861 current->start =
862 (unsigned
863 long) (((a1 * 3600 + a2 * 60 + a3) * jacoTimeres + a4 +
864 jacoShift) * 100.0 / jacoTimeres);
865 current->end =
866 (unsigned
867 long) (((b1 * 3600 + b2 * 60 + b3) * jacoTimeres + b4 +
868 jacoShift) * 100.0 / jacoTimeres);
870 current->lines = 0;
871 p = line2;
872 while ((*p == ' ') || (*p == '\t')) {
873 ++p;
875 if (isalpha(*p)||*p == '[') {
876 int cont, jLength;
878 if (sscanf(p, "%s %[^\n\r]", directive, line1) < 2)
879 return (subtitle *) ERR;
880 jLength = strlen(directive);
881 for (cont = 0; cont < jLength; ++cont) {
882 if (isalpha(*(directive + cont)))
883 *(directive + cont) = toupper(*(directive + cont));
885 if ((strstr(directive, "RDB") != NULL)
886 || (strstr(directive, "RDC") != NULL)
887 || (strstr(directive, "RLB") != NULL)
888 || (strstr(directive, "RLG") != NULL)) {
889 continue;
891 if (strstr(directive, "JL") != NULL) {
892 current->alignment = SUB_ALIGNMENT_HLEFT;
893 } else if (strstr(directive, "JR") != NULL) {
894 current->alignment = SUB_ALIGNMENT_HRIGHT;
895 } else {
896 current->alignment = SUB_ALIGNMENT_HCENTER;
898 strcpy(line2, line1);
899 p = line2;
901 for (q = line1; (!eol(*p)) && (current->lines < SUB_MAX_TEXT); ++p) {
902 switch (*p) {
903 case '{':
904 comment++;
905 break;
906 case '}':
907 if (comment) {
908 --comment;
909 //the next line to get rid of a blank after the comment
910 if ((*(p + 1)) == ' ')
911 p++;
913 break;
914 case '~':
915 if (!comment) {
916 *q = ' ';
917 ++q;
919 break;
920 case ' ':
921 case '\t':
922 if ((*(p + 1) == ' ') || (*(p + 1) == '\t'))
923 break;
924 if (!comment) {
925 *q = ' ';
926 ++q;
928 break;
929 case '\\':
930 if (*(p + 1) == 'n') {
931 *q = '\0';
932 q = line1;
933 current->text[current->lines++] = strdup(line1);
934 ++p;
935 break;
937 if ((toupper(*(p + 1)) == 'C')
938 || (toupper(*(p + 1)) == 'F')) {
939 ++p,++p;
940 break;
942 if ((*(p + 1) == 'B') || (*(p + 1) == 'b') || (*(p + 1) == 'D') || //actually this means "insert current date here"
943 (*(p + 1) == 'I') || (*(p + 1) == 'i') || (*(p + 1) == 'N') || (*(p + 1) == 'T') || //actually this means "insert current time here"
944 (*(p + 1) == 'U') || (*(p + 1) == 'u')) {
945 ++p;
946 break;
948 if ((*(p + 1) == '\\') ||
949 (*(p + 1) == '~') || (*(p + 1) == '{')) {
950 ++p;
951 } else if (eol(*(p + 1))) {
952 if (!fgets(directive, LINE_LEN, fd))
953 return NULL;
954 trail_space(directive);
955 strncat(line2, directive,
956 (LINE_LEN > 511) ? LINE_LEN : 511);
957 break;
959 default:
960 if (!comment) {
961 *q = *p;
962 ++q;
964 } //-- switch
965 } //-- for
966 *q = '\0';
967 current->text[current->lines] = strdup(line1);
968 } //-- while
969 current->lines++;
970 return current;
973 int sub_autodetect (FILE *fd, int *uses_time) {
974 char line[LINE_LEN+1];
975 int i,j=0;
976 char p;
978 while (j < 100) {
979 j++;
980 if (!fgets (line, LINE_LEN, fd))
981 return SUB_INVALID;
983 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
984 {*uses_time=0;return SUB_MICRODVD;}
985 if (sscanf (line, "{%d}{}", &i)==1)
986 {*uses_time=0;return SUB_MICRODVD;}
987 if (sscanf (line, "[%d][%d]", &i, &i)==2)
988 {*uses_time=1;return SUB_MPL2;}
989 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
990 {*uses_time=1;return SUB_SUBRIP;}
991 if (sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i, &i, &i, (char *)&i, &i, &i, &i, &i, (char *)&i, &i)==10)
992 {*uses_time=1;return SUB_SUBVIEWER;}
993 if (sscanf (line, "{T %d:%d:%d:%d",&i, &i, &i, &i)==4)
994 {*uses_time=1;return SUB_SUBVIEWER2;}
995 if (strstr (line, "<SAMI>"))
996 {*uses_time=1; return SUB_SAMI;}
997 if (sscanf(line, "%d:%d:%d.%d %d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i) == 8)
998 {*uses_time = 1; return SUB_JACOSUB;}
999 if (sscanf(line, "@%d @%d", &i, &i) == 2)
1000 {*uses_time = 1; return SUB_JACOSUB;}
1001 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3)
1002 {*uses_time=1;return SUB_VPLAYER;}
1003 if (sscanf (line, "%d:%d:%d ", &i, &i, &i )==3)
1004 {*uses_time=1;return SUB_VPLAYER;}
1005 //TODO: just checking if first line of sub starts with "<" is WAY
1006 // too weak test for RT
1007 // Please someone who knows the format of RT... FIX IT!!!
1008 // It may conflict with other sub formats in the future (actually it doesn't)
1009 if ( *line == '<' )
1010 {*uses_time=1;return SUB_RT;}
1012 if (!memcmp(line, "Dialogue: Marked", 16))
1013 {*uses_time=1; return SUB_SSA;}
1014 if (!memcmp(line, "Dialogue: ", 10))
1015 {*uses_time=1; return SUB_SSA;}
1016 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3)
1017 {*uses_time=1;return SUB_PJS;}
1018 if (sscanf (line, "FORMAT=%d", &i) == 1)
1019 {*uses_time=0; return SUB_MPSUB;}
1020 if (sscanf (line, "FORMAT=TIM%c", &p)==1 && p=='E')
1021 {*uses_time=1; return SUB_MPSUB;}
1022 if (strstr (line, "-->>"))
1023 {*uses_time=0; return SUB_AQTITLE;}
1024 if (sscanf (line, "[%d:%d:%d]", &i, &i, &i)==3)
1025 {*uses_time=1;return SUB_SUBRIP09;}
1028 return SUB_INVALID; // too many bad lines
1031 #ifdef DUMPSUBS
1032 int sub_utf8=0;
1033 #else
1034 extern int sub_utf8;
1035 int sub_utf8_prev=0;
1036 #endif
1038 extern float sub_delay;
1039 extern float sub_fps;
1041 #ifdef USE_ICONV
1042 static iconv_t icdsc = (iconv_t)(-1);
1044 void subcp_open (FILE *enca_fd)
1046 char *tocp = "UTF-8";
1048 if (sub_cp){
1049 char *cp_tmp = sub_cp;
1050 #ifdef HAVE_ENCA
1051 char enca_lang[3], enca_fallback[100];
1052 int free_cp_tmp = 0;
1053 if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
1054 || sscanf(sub_cp, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) {
1055 if (enca_fd) {
1056 cp_tmp = guess_cp(enca_fd, enca_lang, enca_fallback);
1057 free_cp_tmp = 1;
1058 } else {
1059 cp_tmp = enca_fallback;
1062 #endif
1063 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){
1064 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: opened iconv descriptor.\n");
1065 sub_utf8 = 2;
1066 } else
1067 mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n");
1068 #ifdef HAVE_ENCA
1069 if (free_cp_tmp && cp_tmp) free(cp_tmp);
1070 #endif
1074 void subcp_close (void)
1076 if (icdsc != (iconv_t)(-1)){
1077 (void) iconv_close (icdsc);
1078 icdsc = (iconv_t)(-1);
1079 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: closed iconv descriptor.\n");
1083 #define ICBUFFSIZE 512
1084 static char icbuffer[ICBUFFSIZE];
1086 subtitle* subcp_recode (subtitle *sub)
1088 int l=sub->lines;
1089 size_t ileft, oleft;
1090 char *op, *ip, *ot;
1092 while (l){
1093 op = icbuffer;
1094 ip = sub->text[--l];
1095 ileft = strlen(ip);
1096 oleft = ICBUFFSIZE - 1;
1098 if (iconv(icdsc, &ip, &ileft,
1099 &op, &oleft) == (size_t)(-1)) {
1100 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error recoding line (1).\n");
1101 l++;
1102 break;
1104 if (!(ot = (char *)malloc(op - icbuffer + 1))){
1105 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1106 l++;
1107 break;
1109 *op='\0' ;
1110 strcpy (ot, icbuffer);
1111 free (sub->text[l]);
1112 sub->text[l] = ot;
1114 if (l){
1115 for (l = sub->lines; l;)
1116 free (sub->text[--l]);
1117 return ERR;
1119 return sub;
1122 // for demux_ogg.c:
1123 subtitle* subcp_recode1 (subtitle *sub)
1125 int l=sub->lines;
1126 size_t ileft, oleft;
1128 if(icdsc == (iconv_t)(-1)) return sub;
1130 while (l){
1131 char *ip = icbuffer;
1132 char *op = sub->text[--l];
1133 strlcpy(ip, op, ICBUFFSIZE);
1134 ileft = strlen(ip);
1135 oleft = ICBUFFSIZE - 1;
1137 if (iconv(icdsc, &ip, &ileft,
1138 &op, &oleft) == (size_t)(-1)) {
1139 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: error recoding line (2).\n");
1140 return sub;
1142 *op='\0' ;
1144 return sub;
1146 #endif
1148 #ifdef USE_FRIBIDI
1149 #ifndef max
1150 #define max(a,b) (((a)>(b))?(a):(b))
1151 #endif
1152 subtitle* sub_fribidi (subtitle *sub, int sub_utf8)
1154 FriBidiChar logical[LINE_LEN+1], visual[LINE_LEN+1]; // Hopefully these two won't smash the stack
1155 char *ip = NULL, *op = NULL;
1156 FriBidiCharType base;
1157 size_t len,orig_len;
1158 int l=sub->lines;
1159 int char_set_num;
1160 fribidi_boolean log2vis;
1161 if(flip_hebrew) { // Please fix the indentation someday
1162 fribidi_set_mirroring (FRIBIDI_TRUE);
1163 fribidi_set_reorder_nsm (FRIBIDI_FALSE);
1165 if( sub_utf8 == 0 ) {
1166 char_set_num = fribidi_parse_charset (fribidi_charset?fribidi_charset:"ISO8859-8");
1167 }else {
1168 char_set_num = fribidi_parse_charset ("UTF-8");
1170 while (l) {
1171 ip = sub->text[--l];
1172 orig_len = len = strlen( ip ); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1173 if(len > LINE_LEN) {
1174 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: sub->text is longer than LINE_LEN.\n");
1175 l++;
1176 break;
1178 len = fribidi_charset_to_unicode (char_set_num, ip, len, logical);
1179 base = FRIBIDI_TYPE_ON;
1180 log2vis = fribidi_log2vis (logical, len, &base,
1181 /* output */
1182 visual, NULL, NULL, NULL);
1183 if(log2vis) {
1184 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL,
1185 NULL);
1186 if((op = (char*)malloc(sizeof(char)*(max(2*orig_len,2*len) + 1))) == NULL) {
1187 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1188 l++;
1189 break;
1191 fribidi_unicode_to_charset ( char_set_num, visual, len,op);
1192 free (ip);
1193 sub->text[l] = op;
1196 if (l){
1197 for (l = sub->lines; l;)
1198 free (sub->text[--l]);
1199 return ERR;
1202 return sub;
1205 #endif
1207 static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block,
1208 int sub_num, int sub_uses_time) {
1209 int n,m;
1210 subtitle* nextsub;
1211 int i = sub_num;
1212 unsigned long subfms = (sub_uses_time ? 100 : fps) * subtime;
1213 unsigned long overlap = (sub_uses_time ? 100 : fps) / 5; // 0.2s
1215 n=m=0;
1216 if (i) for (;;){
1217 if (sub->end <= sub->start){
1218 sub->end = sub->start + subfms;
1219 m++;
1220 n++;
1222 if (!--i) break;
1223 nextsub = sub + 1;
1224 if(block){
1225 if ((sub->end > nextsub->start) && (sub->end <= nextsub->start + overlap)) {
1226 // these subtitles overlap for less than 0.2 seconds
1227 // and would result in very short overlapping subtitle
1228 // so let's fix the problem here, before overlapping code
1229 // get its hands on them
1230 unsigned delta = sub->end - nextsub->start, half = delta / 2;
1231 sub->end -= half + 1;
1232 nextsub->start += delta - half;
1234 if (sub->end >= nextsub->start){
1235 sub->end = nextsub->start - 1;
1236 if (sub->end - sub->start > subfms)
1237 sub->end = sub->start + subfms;
1238 if (!m)
1239 n++;
1243 /* Theory:
1244 * Movies are often converted from FILM (24 fps)
1245 * to PAL (25) by simply speeding it up, so we
1246 * to multiply the original timestmaps by
1247 * (Movie's FPS / Subtitle's (guessed) FPS)
1248 * so eg. for 23.98 fps movie and PAL time based
1249 * subtitles we say -subfps 25 and we're fine!
1252 /* timed sub fps correction ::atmos */
1253 if(sub_uses_time && sub_fps) {
1254 sub->start *= sub_fps/fps;
1255 sub->end *= sub_fps/fps;
1258 sub = nextsub;
1259 m = 0;
1261 if (n) mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Adjusted %d subtitle(s).\n", n);
1264 struct subreader {
1265 subtitle * (*read)(FILE *fd,subtitle *dest);
1266 void (*post)(subtitle *dest);
1267 const char *name;
1270 #ifdef HAVE_ENCA
1271 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1272 void* guess_cp(FILE *fd, char *preferred_language, char *fallback)
1274 const char **languages;
1275 size_t langcnt, buflen;
1276 EncaAnalyser analyser;
1277 EncaEncoding encoding;
1278 unsigned char *buffer;
1279 char *detected_sub_cp = NULL;
1280 int i;
1282 buffer = (unsigned char*)malloc(MAX_GUESS_BUFFER_SIZE*sizeof(char));
1283 buflen = fread(buffer, 1, MAX_GUESS_BUFFER_SIZE, fd);
1285 languages = enca_get_languages(&langcnt);
1286 mp_msg(MSGT_SUBREADER, MSGL_V, "ENCA supported languages: ");
1287 for (i = 0; i < langcnt; i++) {
1288 mp_msg(MSGT_SUBREADER, MSGL_V, "%s ", languages[i]);
1290 mp_msg(MSGT_SUBREADER, MSGL_V, "\n");
1292 for (i = 0; i < langcnt; i++) {
1293 if (strcasecmp(languages[i], preferred_language) != 0) continue;
1294 analyser = enca_analyser_alloc(languages[i]);
1295 encoding = enca_analyse_const(analyser, buffer, buflen);
1296 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV));
1297 detected_sub_cp = strdup(enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV));
1298 enca_analyser_free(analyser);
1301 free(languages);
1302 free(buffer);
1303 rewind(fd);
1305 if (!detected_sub_cp) detected_sub_cp = strdup(fallback);
1307 return detected_sub_cp;
1309 #endif
1311 sub_data* sub_read_file (char *filename, float fps) {
1312 //filename is assumed to be malloc'ed, free() is used in sub_free()
1313 FILE *fd;
1314 int n_max, n_first, i, j, sub_first, sub_orig;
1315 subtitle *first, *second, *sub, *return_sub;
1316 sub_data *subt_data;
1317 int uses_time = 0, sub_num = 0, sub_errs = 0;
1318 struct subreader sr[]=
1320 { sub_read_line_microdvd, NULL, "microdvd" },
1321 { sub_read_line_subrip, NULL, "subrip" },
1322 { sub_read_line_subviewer, NULL, "subviewer" },
1323 { sub_read_line_sami, NULL, "sami" },
1324 { sub_read_line_vplayer, NULL, "vplayer" },
1325 { sub_read_line_rt, NULL, "rt" },
1326 { sub_read_line_ssa, sub_pp_ssa, "ssa" },
1327 { sub_read_line_pjs, NULL, "pjs" },
1328 { sub_read_line_mpsub, NULL, "mpsub" },
1329 { sub_read_line_aqt, NULL, "aqt" },
1330 { sub_read_line_subviewer2, NULL, "subviewer 2.0" },
1331 { sub_read_line_subrip09, NULL, "subrip 0.9" },
1332 { sub_read_line_jacosub, NULL, "jacosub" },
1333 { sub_read_line_mpl2, NULL, "mpl2" }
1335 struct subreader *srp;
1337 if(filename==NULL) return NULL; //qnx segfault
1338 fd=fopen (filename, "r"); if (!fd) return NULL;
1340 sub_format=sub_autodetect (fd, &uses_time);
1341 mpsub_multiplier = (uses_time ? 100.0 : 1.0);
1342 if (sub_format==SUB_INVALID) {mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: Could not determine file format\n");return NULL;}
1343 srp=sr+sub_format;
1344 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected subtitle file format: %s\n", srp->name);
1346 rewind (fd);
1348 #ifdef USE_ICONV
1349 sub_utf8_prev=sub_utf8;
1351 int l,k;
1352 k = -1;
1353 if ((l=strlen(filename))>4){
1354 char *exts[] = {".utf", ".utf8", ".utf-8" };
1355 for (k=3;--k>=0;)
1356 if (!strcasecmp(filename+(l - strlen(exts[k])), exts[k])){
1357 sub_utf8 = 1;
1358 break;
1361 if (k<0) subcp_open(fd);
1363 #endif
1365 sub_num=0;n_max=32;
1366 first=(subtitle *)malloc(n_max*sizeof(subtitle));
1367 if(!first){
1368 #ifdef USE_ICONV
1369 subcp_close();
1370 sub_utf8=sub_utf8_prev;
1371 #endif
1372 return NULL;
1375 #ifdef USE_SORTSUB
1376 sub = (subtitle *)malloc(sizeof(subtitle));
1377 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1378 //as the beginning of the following
1379 previous_sub_end = 0;
1380 #endif
1381 while(1){
1382 if(sub_num>=n_max){
1383 n_max+=16;
1384 first=realloc(first,n_max*sizeof(subtitle));
1386 #ifndef USE_SORTSUB
1387 sub = &first[sub_num];
1388 #endif
1389 memset(sub, '\0', sizeof(subtitle));
1390 sub=srp->read(fd,sub);
1391 if(!sub) break; // EOF
1392 #ifdef USE_ICONV
1393 if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub);
1394 #endif
1395 #ifdef USE_FRIBIDI
1396 if (sub!=ERR) sub=sub_fribidi(sub,sub_utf8);
1397 #endif
1398 if ( sub == ERR )
1400 #ifdef USE_ICONV
1401 subcp_close();
1402 #endif
1403 if ( first ) free(first);
1404 return NULL;
1406 // Apply any post processing that needs recoding first
1407 if ((sub!=ERR) && !sub_no_text_pp && srp->post) srp->post(sub);
1408 #ifdef USE_SORTSUB
1409 if(!sub_num || (first[sub_num - 1].start <= sub->start)){
1410 first[sub_num].start = sub->start;
1411 first[sub_num].end = sub->end;
1412 first[sub_num].lines = sub->lines;
1413 first[sub_num].alignment = sub->alignment;
1414 for(i = 0; i < sub->lines; ++i){
1415 first[sub_num].text[i] = sub->text[i];
1417 if (previous_sub_end){
1418 first[sub_num - 1].end = previous_sub_end;
1419 previous_sub_end = 0;
1421 } else {
1422 for(j = sub_num - 1; j >= 0; --j){
1423 first[j + 1].start = first[j].start;
1424 first[j + 1].end = first[j].end;
1425 first[j + 1].lines = first[j].lines;
1426 first[j + 1].alignment = first[j].alignment;
1427 for(i = 0; i < first[j].lines; ++i){
1428 first[j + 1].text[i] = first[j].text[i];
1430 if(!j || (first[j - 1].start <= sub->start)){
1431 first[j].start = sub->start;
1432 first[j].end = sub->end;
1433 first[j].lines = sub->lines;
1434 first[j].alignment = sub->alignment;
1435 for(i = 0; i < SUB_MAX_TEXT; ++i){
1436 first[j].text[i] = sub->text[i];
1438 if (previous_sub_end){
1439 first[j].end = first[j - 1].end;
1440 first[j - 1].end = previous_sub_end;
1441 previous_sub_end = 0;
1443 break;
1447 #endif
1448 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
1451 fclose(fd);
1453 #ifdef USE_ICONV
1454 subcp_close();
1455 #endif
1457 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1458 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Read %i subtitles", sub_num);
1459 if (sub_errs) mp_msg(MSGT_SUBREADER,MSGL_INFO,", %i bad line(s).\n", sub_errs);
1460 else mp_msg(MSGT_SUBREADER,MSGL_INFO,".\n");
1462 if(sub_num<=0){
1463 free(first);
1464 return NULL;
1467 // we do overlap if the user forced it (suboverlap_enable == 2) or
1468 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1469 // this is because usually overlapping subtitles are found in these formats,
1470 // while in others they are probably result of bad timing
1471 if ((suboverlap_enabled == 2) ||
1472 ((suboverlap_enabled) && ((sub_format == SUB_JACOSUB) || (sub_format == SUB_SSA)))) {
1473 adjust_subs_time(first, 6.0, fps, 0, sub_num, uses_time);/*~6 secs AST*/
1474 // here we manage overlapping subtitles
1475 sub_orig = sub_num;
1476 n_first = sub_num;
1477 sub_num = 0;
1478 second = NULL;
1479 // for each subtitle in first[] we deal with its 'block' of
1480 // bonded subtitles
1481 for (sub_first = 0; sub_first < n_first; ++sub_first) {
1482 unsigned long global_start = first[sub_first].start,
1483 global_end = first[sub_first].end, local_start, local_end;
1484 int lines_to_add = first[sub_first].lines, sub_to_add = 0,
1485 **placeholder = NULL, higher_line = 0, counter, start_block_sub = sub_num;
1486 char real_block = 1;
1488 // here we find the number of subtitles inside the 'block'
1489 // and its span interval. this works well only with sorted
1490 // subtitles
1491 while ((sub_first + sub_to_add + 1 < n_first) && (first[sub_first + sub_to_add + 1].start < global_end)) {
1492 ++sub_to_add;
1493 lines_to_add += first[sub_first + sub_to_add].lines;
1494 if (first[sub_first + sub_to_add].start < global_start) {
1495 global_start = first[sub_first + sub_to_add].start;
1497 if (first[sub_first + sub_to_add].end > global_end) {
1498 global_end = first[sub_first + sub_to_add].end;
1502 // we need a structure to keep trace of the screen lines
1503 // used by the subs, a 'placeholder'
1504 counter = 2 * sub_to_add + 1; // the maximum number of subs derived
1505 // from a block of sub_to_add+1 subs
1506 placeholder = (int **) malloc(sizeof(int *) * counter);
1507 for (i = 0; i < counter; ++i) {
1508 placeholder[i] = (int *) malloc(sizeof(int) * lines_to_add);
1509 for (j = 0; j < lines_to_add; ++j) {
1510 placeholder[i][j] = -1;
1514 counter = 0;
1515 local_end = global_start - 1;
1516 do {
1517 int ls;
1519 // here we find the beginning and the end of a new
1520 // subtitle in the block
1521 local_start = local_end + 1;
1522 local_end = global_end;
1523 for (j = 0; j <= sub_to_add; ++j) {
1524 if ((first[sub_first + j].start - 1 > local_start) && (first[sub_first + j].start - 1 < local_end)) {
1525 local_end = first[sub_first + j].start - 1;
1526 } else if ((first[sub_first + j].end > local_start) && (first[sub_first + j].end < local_end)) {
1527 local_end = first[sub_first + j].end;
1530 // here we allocate the screen lines to subs we must
1531 // display in current local_start-local_end interval.
1532 // if the subs were yet presents in the previous interval
1533 // they keep the same lines, otherside they get unused lines
1534 for (j = 0; j <= sub_to_add; ++j) {
1535 if ((first[sub_first + j].start <= local_end) && (first[sub_first + j].end > local_start)) {
1536 unsigned long sub_lines = first[sub_first + j].lines, fragment_length = lines_to_add + 1,
1537 tmp = 0;
1538 char boolean = 0;
1539 int fragment_position = -1;
1541 // if this is not the first new sub of the block
1542 // we find if this sub was present in the previous
1543 // new sub
1544 if (counter)
1545 for (i = 0; i < lines_to_add; ++i) {
1546 if (placeholder[counter - 1][i] == sub_first + j) {
1547 placeholder[counter][i] = sub_first + j;
1548 boolean = 1;
1551 if (boolean)
1552 continue;
1554 // we are looking for the shortest among all groups of
1555 // sequential blank lines whose length is greater than or
1556 // equal to sub_lines. we store in fragment_position the
1557 // position of the shortest group, in fragment_length its
1558 // length, and in tmp the length of the group currently
1559 // examinated
1560 for (i = 0; i < lines_to_add; ++i) {
1561 if (placeholder[counter][i] == -1) {
1562 // placeholder[counter][i] is part of the current group
1563 // of blank lines
1564 ++tmp;
1565 } else {
1566 if (tmp == sub_lines) {
1567 // current group's size fits exactly the one we
1568 // need, so we stop looking
1569 fragment_position = i - tmp;
1570 tmp = 0;
1571 break;
1573 if ((tmp) && (tmp > sub_lines) && (tmp < fragment_length)) {
1574 // current group is the best we found till here,
1575 // but is still bigger than the one we are looking
1576 // for, so we keep on looking
1577 fragment_length = tmp;
1578 fragment_position = i - tmp;
1579 tmp = 0;
1580 } else {
1581 // current group doesn't fit at all, so we forget it
1582 tmp = 0;
1586 if (tmp) {
1587 // last screen line is blank, a group ends with it
1588 if ((tmp >= sub_lines) && (tmp < fragment_length)) {
1589 fragment_position = i - tmp;
1592 if (fragment_position == -1) {
1593 // it was not possible to find free screen line(s) for a subtitle,
1594 // usually this means a bug in the code; however we do not overlap
1595 mp_msg(MSGT_SUBREADER, MSGL_WARN, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1596 higher_line = SUB_MAX_TEXT + 1;
1597 break;
1598 } else {
1599 for (tmp = 0; tmp < sub_lines; ++tmp) {
1600 placeholder[counter][fragment_position + tmp] = sub_first + j;
1605 for (j = higher_line + 1; j < lines_to_add; ++j) {
1606 if (placeholder[counter][j] != -1)
1607 higher_line = j;
1608 else
1609 break;
1611 if (higher_line >= SUB_MAX_TEXT) {
1612 // the 'block' has too much lines, so we don't overlap the
1613 // subtitles
1614 second = (subtitle *) realloc(second, (sub_num + sub_to_add + 1) * sizeof(subtitle));
1615 for (j = 0; j <= sub_to_add; ++j) {
1616 int ls;
1617 memset(&second[sub_num + j], '\0', sizeof(subtitle));
1618 second[sub_num + j].start = first[sub_first + j].start;
1619 second[sub_num + j].end = first[sub_first + j].end;
1620 second[sub_num + j].lines = first[sub_first + j].lines;
1621 second[sub_num + j].alignment = first[sub_first + j].alignment;
1622 for (ls = 0; ls < second[sub_num + j].lines; ls++) {
1623 second[sub_num + j].text[ls] = strdup(first[sub_first + j].text[ls]);
1626 sub_num += sub_to_add + 1;
1627 sub_first += sub_to_add;
1628 real_block = 0;
1629 break;
1632 // we read the placeholder structure and create the new
1633 // subs.
1634 second = (subtitle *) realloc(second, (sub_num + 1) * sizeof(subtitle));
1635 memset(&second[sub_num], '\0', sizeof(subtitle));
1636 second[sub_num].start = local_start;
1637 second[sub_num].end = local_end;
1638 second[sub_num].alignment = SUB_ALIGNMENT_HCENTER;
1639 n_max = (lines_to_add < SUB_MAX_TEXT) ? lines_to_add : SUB_MAX_TEXT;
1640 for (i = 0, j = 0; j < n_max; ++j) {
1641 if (placeholder[counter][j] != -1) {
1642 int lines = first[placeholder[counter][j]].lines;
1643 for (ls = 0; ls < lines; ++ls) {
1644 second[sub_num].text[i++] = strdup(first[placeholder[counter][j]].text[ls]);
1646 j += lines - 1;
1647 } else {
1648 second[sub_num].text[i++] = strdup(" ");
1651 ++sub_num;
1652 ++counter;
1653 } while (local_end < global_end);
1654 if (real_block)
1655 for (i = 0; i < counter; ++i)
1656 second[start_block_sub + i].lines = higher_line + 1;
1658 counter = 2 * sub_to_add + 1;
1659 for (i = 0; i < counter; ++i) {
1660 free(placeholder[i]);
1662 free(placeholder);
1663 sub_first += sub_to_add;
1666 for (j = sub_orig - 1; j >= 0; --j) {
1667 for (i = first[j].lines - 1; i >= 0; --i) {
1668 free(first[j].text[i]);
1671 free(first);
1673 return_sub = second;
1674 } else { //if(suboverlap_enabled)
1675 adjust_subs_time(first, 6.0, fps, 1, sub_num, uses_time);/*~6 secs AST*/
1676 return_sub = first;
1678 if (return_sub == NULL) return NULL;
1679 subt_data = (sub_data *)malloc(sizeof(sub_data));
1680 subt_data->filename = filename;
1681 subt_data->sub_uses_time = uses_time;
1682 subt_data->sub_num = sub_num;
1683 subt_data->sub_errs = sub_errs;
1684 subt_data->subtitles = return_sub;
1685 return subt_data;
1688 #if 0
1689 char * strreplace( char * in,char * what,char * whereof )
1691 int i;
1692 char * tmp;
1694 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
1695 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
1696 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
1697 return in;
1699 #endif
1702 static void strcpy_trim(char *d, char *s)
1704 // skip leading whitespace
1705 while (*s && !isalnum(*s)) {
1706 s++;
1708 for (;;) {
1709 // copy word
1710 while (*s && isalnum(*s)) {
1711 *d = tolower(*s);
1712 s++; d++;
1714 if (*s == 0) break;
1715 // trim excess whitespace
1716 while (*s && !isalnum(*s)) {
1717 s++;
1719 if (*s == 0) break;
1720 *d++ = ' ';
1722 *d = 0;
1725 static void strcpy_strip_ext(char *d, char *s)
1727 char *tmp = strrchr(s,'.');
1728 if (!tmp) {
1729 strcpy(d, s);
1730 return;
1731 } else {
1732 strncpy(d, s, tmp-s);
1733 d[tmp-s] = 0;
1735 while (*d) {
1736 *d = tolower(*d);
1737 d++;
1741 static void strcpy_get_ext(char *d, char *s)
1743 char *tmp = strrchr(s,'.');
1744 if (!tmp) {
1745 strcpy(d, "");
1746 return;
1747 } else {
1748 strcpy(d, tmp+1);
1752 static int whiteonly(char *s)
1754 while (*s) {
1755 if (isalnum(*s)) return 0;
1756 s++;
1758 return 1;
1761 typedef struct _subfn
1763 int priority;
1764 char *fname;
1765 } subfn;
1767 static int compare_sub_priority(const void *a, const void *b)
1769 if (((subfn*)a)->priority > ((subfn*)b)->priority) {
1770 return -1;
1771 } else if (((subfn*)a)->priority < ((subfn*)b)->priority) {
1772 return 1;
1773 } else {
1774 return strcoll(((subfn*)a)->fname, ((subfn*)b)->fname);
1778 char** sub_filenames(char* path, char *fname)
1780 char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
1781 char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
1783 int len, pos, found, i, j;
1784 char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
1785 subfn *result;
1786 char **result2;
1788 int subcnt;
1790 FILE *f;
1792 DIR *d;
1793 struct dirent *de;
1795 len = (strlen(fname) > 256 ? strlen(fname) : 256)
1796 +(strlen(path) > 256 ? strlen(path) : 256)+2;
1798 f_dir = (char*)malloc(len);
1799 f_fname = (char*)malloc(len);
1800 f_fname_noext = (char*)malloc(len);
1801 f_fname_trim = (char*)malloc(len);
1803 tmp_fname_noext = (char*)malloc(len);
1804 tmp_fname_trim = (char*)malloc(len);
1805 tmp_fname_ext = (char*)malloc(len);
1807 tmpresult = (char*)malloc(len);
1809 result = (subfn*)malloc(sizeof(subfn)*MAX_SUBTITLE_FILES);
1810 memset(result, 0, sizeof(subfn)*MAX_SUBTITLE_FILES);
1812 subcnt = 0;
1814 tmp = strrchr(fname,'/');
1815 #ifdef WIN32
1816 if(!tmp)tmp = strrchr(fname,'\\');
1817 #endif
1819 // extract filename & dirname from fname
1820 if (tmp) {
1821 strcpy(f_fname, tmp+1);
1822 pos = tmp - fname;
1823 strncpy(f_dir, fname, pos+1);
1824 f_dir[pos+1] = 0;
1825 } else {
1826 strcpy(f_fname, fname);
1827 strcpy(f_dir, "./");
1830 strcpy_strip_ext(f_fname_noext, f_fname);
1831 strcpy_trim(f_fname_trim, f_fname_noext);
1833 tmp_sub_id = NULL;
1834 if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
1835 tmp_sub_id = (char*)malloc(strlen(dvdsub_lang)+1);
1836 strcpy_trim(tmp_sub_id, dvdsub_lang);
1839 // 0 = nothing
1840 // 1 = any subtitle file
1841 // 2 = any sub file containing movie name
1842 // 3 = sub file containing movie name and the lang extension
1843 for (j = 0; j <= 1; j++) {
1844 d = opendir(j == 0 ? f_dir : path);
1845 if (d) {
1846 while ((de = readdir(d))) {
1847 // retrieve various parts of the filename
1848 strcpy_strip_ext(tmp_fname_noext, de->d_name);
1849 strcpy_get_ext(tmp_fname_ext, de->d_name);
1850 strcpy_trim(tmp_fname_trim, tmp_fname_noext);
1852 // does it end with a subtitle extension?
1853 found = 0;
1854 #ifdef USE_ICONV
1855 #ifdef HAVE_ENCA
1856 for (i = ((sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0); sub_exts[i]; i++) {
1857 #else
1858 for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
1859 #endif
1860 #else
1861 for (i = 0; sub_exts[i]; i++) {
1862 #endif
1863 if (strcasecmp(sub_exts[i], tmp_fname_ext) == 0) {
1864 found = 1;
1865 break;
1869 // we have a (likely) subtitle file
1870 if (found) {
1871 int prio = 0;
1872 if (!prio && tmp_sub_id)
1874 sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id);
1875 printf("dvdsublang...%s\n", tmpresult);
1876 if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) {
1877 // matches the movie name + lang extension
1878 prio = 5;
1881 if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
1882 // matches the movie name
1883 prio = 4;
1885 if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && (sub_match_fuzziness >= 1)) {
1886 // contains the movie name
1887 tmp += strlen(f_fname_trim);
1888 if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
1889 // with sub_id specified prefer localized subtitles
1890 prio = 3;
1891 } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
1892 // without sub_id prefer "plain" name
1893 prio = 3;
1894 } else {
1895 // with no localized subs found, try any else instead
1896 prio = 2;
1899 if (!prio) {
1900 // doesn't contain the movie name
1901 // don't try in the mplayer subtitle directory
1902 if ((j == 0) && (sub_match_fuzziness >= 2)) {
1903 prio = 1;
1907 if (prio) {
1908 prio += prio;
1909 #ifdef USE_ICONV
1910 if (i<3){ // prefer UTF-8 coded
1911 prio++;
1913 #endif
1914 sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
1915 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1916 if ((f = fopen(tmpresult, "rt"))) {
1917 fclose(f);
1918 result[subcnt].priority = prio;
1919 result[subcnt].fname = strdup(tmpresult);
1920 subcnt++;
1925 if (subcnt >= MAX_SUBTITLE_FILES) break;
1927 closedir(d);
1932 if (tmp_sub_id) free(tmp_sub_id);
1934 free(f_dir);
1935 free(f_fname);
1936 free(f_fname_noext);
1937 free(f_fname_trim);
1939 free(tmp_fname_noext);
1940 free(tmp_fname_trim);
1941 free(tmp_fname_ext);
1943 free(tmpresult);
1945 qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
1947 result2 = (char**)malloc(sizeof(char*)*(subcnt+1));
1948 memset(result2, 0, sizeof(char*)*(subcnt+1));
1950 for (i = 0; i < subcnt; i++) {
1951 result2[i] = result[i].fname;
1953 result2[subcnt] = NULL;
1955 free(result);
1957 return result2;
1960 void list_sub_file(sub_data* subd){
1961 int i,j;
1962 subtitle *subs = subd->subtitles;
1964 for(j=0; j < subd->sub_num; j++){
1965 subtitle* egysub=&subs[j];
1966 printf ("%i line%c (%li-%li)\n",
1967 egysub->lines,
1968 (1==egysub->lines)?' ':'s',
1969 egysub->start,
1970 egysub->end);
1971 for (i=0; i<egysub->lines; i++) {
1972 printf ("\t\t%d: %s%s", i,egysub->text[i], i==egysub->lines-1?"":" \n ");
1974 printf ("\n");
1977 printf ("Subtitle format %s time.\n",
1978 subd->sub_uses_time ? "uses":"doesn't use");
1979 printf ("Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);
1982 void dump_srt(sub_data* subd, float fps){
1983 int i,j;
1984 int h,m,s,ms;
1985 FILE * fd;
1986 subtitle * onesub;
1987 unsigned long temp;
1988 subtitle *subs = subd->subtitles;
1990 if (!subd->sub_uses_time && sub_fps == 0)
1991 sub_fps = fps;
1992 fd=fopen("dumpsub.srt","w");
1993 if(!fd)
1995 perror("dump_srt: fopen");
1996 return;
1998 for(i=0; i < subd->sub_num; i++)
2000 onesub=subs+i; //=&subs[i];
2001 fprintf(fd,"%d\n",i+1);//line number
2003 temp=onesub->start;
2004 if (!subd->sub_uses_time)
2005 temp = temp * 100 / sub_fps;
2006 temp -= sub_delay * 100;
2007 h=temp/360000;temp%=360000; //h =1*100*60*60
2008 m=temp/6000; temp%=6000; //m =1*100*60
2009 s=temp/100; temp%=100; //s =1*100
2010 ms=temp*10; //ms=1*10
2011 fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms);
2013 temp=onesub->end;
2014 if (!subd->sub_uses_time)
2015 temp = temp * 100 / sub_fps;
2016 temp -= sub_delay * 100;
2017 h=temp/360000;temp%=360000;
2018 m=temp/6000; temp%=6000;
2019 s=temp/100; temp%=100;
2020 ms=temp*10;
2021 fprintf(fd,"%02d:%02d:%02d,%03d\n",h,m,s,ms);
2023 for(j=0;j<onesub->lines;j++)
2024 fprintf(fd,"%s\n",onesub->text[j]);
2026 fprintf(fd,"\n");
2028 fclose(fd);
2029 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2032 void dump_mpsub(sub_data* subd, float fps){
2033 int i,j;
2034 FILE *fd;
2035 float a,b;
2036 subtitle *subs = subd->subtitles;
2038 mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps);
2039 if (sub_fps==0) sub_fps=fps;
2041 fd=fopen ("dump.mpsub", "w");
2042 if (!fd) {
2043 perror ("dump_mpsub: fopen");
2044 return;
2048 if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
2049 else fprintf (fd, "FORMAT=%5.2f\n\n", fps);
2051 for(j=0; j < subd->sub_num; j++){
2052 subtitle* egysub=&subs[j];
2053 if (subd->sub_uses_time) {
2054 a=((egysub->start-mpsub_position)/100.0);
2055 b=((egysub->end-egysub->start)/100.0);
2056 if ( (float)((int)a) == a)
2057 fprintf (fd, "%.0f",a);
2058 else
2059 fprintf (fd, "%.2f",a);
2061 if ( (float)((int)b) == b)
2062 fprintf (fd, " %.0f\n",b);
2063 else
2064 fprintf (fd, " %.2f\n",b);
2065 } else {
2066 fprintf (fd, "%ld %ld\n", (long)((egysub->start*(fps/sub_fps))-((mpsub_position*(fps/sub_fps)))),
2067 (long)(((egysub->end)-(egysub->start))*(fps/sub_fps)));
2070 mpsub_position = egysub->end;
2071 for (i=0; i<egysub->lines; i++) {
2072 fprintf (fd, "%s\n",egysub->text[i]);
2074 fprintf (fd, "\n");
2076 fclose (fd);
2077 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2080 void dump_microdvd(sub_data* subd, float fps) {
2081 int i, delay;
2082 FILE *fd;
2083 subtitle *subs = subd->subtitles;
2084 if (sub_fps == 0)
2085 sub_fps = fps;
2086 fd = fopen("dumpsub.txt", "w");
2087 if (!fd) {
2088 perror("dumpsub.txt: fopen");
2089 return;
2091 delay = sub_delay * sub_fps;
2092 for (i = 0; i < subd->sub_num; ++i) {
2093 int j, start, end;
2094 start = subs[i].start;
2095 end = subs[i].end;
2096 if (subd->sub_uses_time) {
2097 start = start * sub_fps / 100 ;
2098 end = end * sub_fps / 100;
2100 else {
2101 start = start * sub_fps / fps;
2102 end = end * sub_fps / fps;
2104 start -= delay;
2105 end -= delay;
2106 fprintf(fd, "{%d}{%d}", start, end);
2107 for (j = 0; j < subs[i].lines; ++j)
2108 fprintf(fd, "%s%s", j ? "|" : "", subs[i].text[j]);
2109 fprintf(fd, "\n");
2111 fclose(fd);
2112 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
2115 void dump_jacosub(sub_data* subd, float fps) {
2116 int i,j;
2117 int h,m,s,cs;
2118 FILE * fd;
2119 subtitle * onesub;
2120 unsigned long temp;
2121 subtitle *subs = subd->subtitles;
2123 if (!subd->sub_uses_time && sub_fps == 0)
2124 sub_fps = fps;
2125 fd=fopen("dumpsub.jss","w");
2126 if(!fd)
2128 perror("dump_jacosub: fopen");
2129 return;
2131 fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps);
2132 for(i=0; i < subd->sub_num; i++)
2134 onesub=subs+i; //=&subs[i];
2136 temp=onesub->start;
2137 if (!subd->sub_uses_time)
2138 temp = temp * 100 / sub_fps;
2139 temp -= sub_delay * 100;
2140 h=temp/360000;temp%=360000; //h =1*100*60*60
2141 m=temp/6000; temp%=6000; //m =1*100*60
2142 s=temp/100; temp%=100; //s =1*100
2143 cs=temp; //cs=1*10
2144 fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs);
2146 temp=onesub->end;
2147 if (!subd->sub_uses_time)
2148 temp = temp * 100 / sub_fps;
2149 temp -= sub_delay * 100;
2150 h=temp/360000;temp%=360000;
2151 m=temp/6000; temp%=6000;
2152 s=temp/100; temp%=100;
2153 cs=temp;
2154 fprintf(fd,"%02d:%02d:%02d.%02d {~} ",h,m,s,cs);
2156 for(j=0;j<onesub->lines;j++)
2157 fprintf(fd,"%s%s",j ? "\\n" : "", onesub->text[j]);
2159 fprintf(fd,"\n");
2161 fclose(fd);
2162 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2165 void dump_sami(sub_data* subd, float fps) {
2166 int i,j;
2167 FILE * fd;
2168 subtitle * onesub;
2169 unsigned long temp;
2170 subtitle *subs = subd->subtitles;
2172 if (!subd->sub_uses_time && sub_fps == 0)
2173 sub_fps = fps;
2174 fd=fopen("dumpsub.smi","w");
2175 if(!fd)
2177 perror("dump_jacosub: fopen");
2178 return;
2180 fprintf(fd, "<SAMI>\n"
2181 "<HEAD>\n"
2182 " <STYLE TYPE=\"Text/css\">\n"
2183 " <!--\n"
2184 " P {margin-left: 29pt; margin-right: 29pt; font-size: 24pt; text-align: center; font-family: Tahoma; font-weight: bold; color: #FCDD03; background-color: #000000;}\n"
2185 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2186 " -->\n"
2187 " </STYLE>\n"
2188 "</HEAD>\n"
2189 "<BODY>\n");
2190 for(i=0; i < subd->sub_num; i++)
2192 onesub=subs+i; //=&subs[i];
2194 temp=onesub->start;
2195 if (!subd->sub_uses_time)
2196 temp = temp * 100 / sub_fps;
2197 temp -= sub_delay * 100;
2198 fprintf(fd,"\t<SYNC Start=%lu>\n"
2199 "\t <P>", temp * 10);
2201 for(j=0;j<onesub->lines;j++)
2202 fprintf(fd,"%s%s",j ? "<br>" : "", onesub->text[j]);
2204 fprintf(fd,"\n");
2206 temp=onesub->end;
2207 if (!subd->sub_uses_time)
2208 temp = temp * 100 / sub_fps;
2209 temp -= sub_delay * 100;
2210 fprintf(fd,"\t<SYNC Start=%lu>\n"
2211 "\t <P>&nbsp;\n", temp * 10);
2213 fprintf(fd, "</BODY>\n"
2214 "</SAMI>\n");
2215 fclose(fd);
2216 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2219 void sub_free( sub_data * subd )
2221 int i;
2223 if ( !subd ) return;
2225 if (subd->subtitles) {
2226 for (i=0; i < subd->subtitles->lines; i++) free( subd->subtitles->text[i] );
2227 free( subd->subtitles );
2229 if (subd->filename) free( subd->filename );
2230 free( subd );
2233 #ifdef DUMPSUBS
2234 int main(int argc, char **argv) { // for testing
2235 sub_data *subd;
2237 if(argc<2){
2238 printf("\nUsage: subreader filename.sub\n\n");
2239 exit(1);
2241 sub_cp = argv[2];
2242 subd = sub_read_file(argv[1]);
2243 if(!subd){
2244 printf("Couldn't load file.\n");
2245 exit(1);
2248 list_sub_file(subd);
2250 return 0;
2252 #endif