Forced subs support for mencoder
[mplayer/glamo.git] / subreader.c
blobf11fce6fbe3ff05a1f21513807046c982256bc68
1 /*
2 * Subtitle reader with format autodetection
4 * Written by laaz
5 * Some code cleanup & realloc() by A'rpi/ESP-team
6 * dunnowhat sub format by szabi
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 #define ERR ((void *) -1)
24 #ifdef USE_ICONV
25 #include <iconv.h>
26 char *sub_cp=NULL;
27 #endif
28 #ifdef USE_FRIBIDI
29 #include <fribidi/fribidi.h>
30 char *fribidi_charset = NULL;
31 int flip_hebrew = 1;
32 #endif
34 extern char* dvdsub_lang;
36 /* Maximal length of line of a subtitle */
37 #define LINE_LEN 1000
38 static float mpsub_position=0;
39 static float mpsub_multiplier=1.;
40 static int sub_slacktime = 20000; //20 sec
42 int sub_no_text_pp=0; // 1 => do not apply text post-processing
43 // like {\...} elimination in SSA format.
45 int sub_match_fuzziness=0; // level of sub name matching fuzziness
47 /* Use the SUB_* constant defined in the header file */
48 int sub_format=SUB_INVALID;
49 #ifdef USE_SORTSUB
50 /*
51 Some subtitling formats, namely AQT and Subrip09, define the end of a
52 subtitle as the beginning of the following. Since currently we read one
53 subtitle at time, for these format we keep two global *subtitle,
54 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
55 so we can change its end when we read current subtitle starting time.
56 When USE_SORTSUB is defined, we use a single global unsigned long,
57 previous_sub_end, for both (and even future) formats, to store the end of
58 the previous sub: it is initialized to 0 in sub_read_file and eventually
59 modified by sub_read_aqt_line or sub_read_subrip09_line.
61 unsigned long previous_sub_end;
62 #endif
64 static int eol(char p) {
65 return (p=='\r' || p=='\n' || p=='\0');
68 /* Remove leading and trailing space */
69 static void trail_space(char *s) {
70 int i = 0;
71 while (isspace(s[i])) ++i;
72 if (i) strcpy(s, s + i);
73 i = strlen(s) - 1;
74 while (i > 0 && isspace(s[i])) s[i--] = '\0';
77 static char *stristr(const char *haystack, const char *needle) {
78 int len = 0;
79 const char *p = haystack;
81 if (!(haystack && needle)) return NULL;
83 len=strlen(needle);
84 while (*p != '\0') {
85 if (strncasecmp(p, needle, len) == 0) return (char*)p;
86 p++;
89 return NULL;
92 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) {
93 static char line[LINE_LEN+1];
94 static char *s = NULL, *slacktime_s;
95 char text[LINE_LEN+1], *p=NULL, *q;
96 int state;
98 current->lines = current->start = current->end = 0;
99 state = 0;
101 /* read the first line */
102 if (!s)
103 if (!(s = fgets(line, LINE_LEN, fd))) return 0;
105 do {
106 switch (state) {
108 case 0: /* find "START=" or "Slacktime:" */
109 slacktime_s = stristr (s, "Slacktime:");
110 if (slacktime_s)
111 sub_slacktime = strtol (slacktime_s+10, NULL, 0) / 10;
113 s = stristr (s, "Start=");
114 if (s) {
115 current->start = strtol (s + 6, &s, 0) / 10;
116 state = 1; continue;
118 break;
120 case 1: /* find "<P" */
121 if ((s = stristr (s, "<P"))) { s += 2; state = 2; continue; }
122 break;
124 case 2: /* find ">" */
125 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; }
126 break;
128 case 3: /* get all text until '<' appears */
129 if (*s == '\0') break;
130 else if (!strncasecmp (s, "<br>", 4)) {
131 *p = '\0'; p = text; trail_space (text);
132 if (text[0] != '\0')
133 current->text[current->lines++] = strdup (text);
134 s += 4;
136 else if (*s == '<') { state = 4; }
137 else if (!strncasecmp (s, "&nbsp;", 6)) { *p++ = ' '; s += 6; }
138 else if (*s == '\t') { *p++ = ' '; s++; }
139 else if (*s == '\r' || *s == '\n') { s++; }
140 else *p++ = *s++;
142 /* skip duplicated space */
143 if (p > text + 2) if (*(p-1) == ' ' && *(p-2) == ' ') p--;
145 continue;
147 case 4: /* get current->end or skip <TAG> */
148 q = stristr (s, "Start=");
149 if (q) {
150 current->end = strtol (q + 6, &q, 0) / 10 - 1;
151 *p = '\0'; trail_space (text);
152 if (text[0] != '\0')
153 current->text[current->lines++] = strdup (text);
154 if (current->lines > 0) { state = 99; break; }
155 state = 0; continue;
157 s = strchr (s, '>');
158 if (s) { s++; state = 3; continue; }
159 break;
162 /* read next line */
163 if (state != 99 && !(s = fgets (line, LINE_LEN, fd))) {
164 if (current->start > 0) {
165 break; // if it is the last subtitle
166 } else {
167 return 0;
171 } while (state != 99);
173 // For the last subtitle
174 if (current->end <= 0) {
175 current->end = current->start + sub_slacktime;
176 *p = '\0'; trail_space (text);
177 if (text[0] != '\0')
178 current->text[current->lines++] = strdup (text);
181 return current;
185 char *sub_readtext(char *source, char **dest) {
186 int len=0;
187 char *p=source;
189 // printf("src=%p dest=%p \n",source,dest);
191 while ( !eol(*p) && *p!= '|' ) {
192 p++,len++;
195 *dest= (char *)malloc (len+1);
196 if (!dest) {return ERR;}
198 strncpy(*dest, source, len);
199 (*dest)[len]=0;
201 while (*p=='\r' || *p=='\n' || *p=='|') p++;
203 if (*p) return p; // not-last text field
204 else return NULL; // last text field
207 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
208 char line[LINE_LEN+1];
209 char line2[LINE_LEN+1];
210 char *p, *next;
211 int i;
213 do {
214 if (!fgets (line, LINE_LEN, fd)) return NULL;
215 } while ((sscanf (line,
216 "{%ld}{}%[^\r\n]",
217 &(current->start), line2) < 2) &&
218 (sscanf (line,
219 "{%ld}{%ld}%[^\r\n]",
220 &(current->start), &(current->end), line2) < 3));
222 p=line2;
224 next=p, i=0;
225 while ((next =sub_readtext (next, &(current->text[i])))) {
226 if (current->text[i]==ERR) {return ERR;}
227 i++;
228 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
230 current->lines= ++i;
232 return current;
235 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
236 char line[LINE_LEN+1];
237 int a1,a2,a3,a4,b1,b2,b3,b4;
238 char *p=NULL, *q=NULL;
239 int len;
241 while (1) {
242 if (!fgets (line, LINE_LEN, fd)) return NULL;
243 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
244 current->start = a1*360000+a2*6000+a3*100+a4;
245 current->end = b1*360000+b2*6000+b3*100+b4;
247 if (!fgets (line, LINE_LEN, fd)) return NULL;
249 p=q=line;
250 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
251 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && *p!='|' && strncmp(p,"[br]",4); p++,len++);
252 current->text[current->lines-1]=(char *)malloc (len+1);
253 if (!current->text[current->lines-1]) return ERR;
254 strncpy (current->text[current->lines-1], q, len);
255 current->text[current->lines-1][len]='\0';
256 if (!*p || *p=='\r' || *p=='\n') break;
257 if (*p=='|') p++;
258 else while (*p++!=']');
260 break;
262 return current;
265 subtitle *sub_read_line_subviewer(FILE *fd,subtitle *current) {
266 char line[LINE_LEN+1];
267 int a1,a2,a3,a4,b1,b2,b3,b4;
268 char *p=NULL;
269 int i,len;
271 while (!current->text[0]) {
272 if (!fgets (line, LINE_LEN, fd)) return NULL;
273 if ((len=sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",&a1,&a2,&a3,(char *)&i,&a4,&b1,&b2,&b3,(char *)&i,&b4)) < 10)
274 continue;
275 current->start = a1*360000+a2*6000+a3*100+a4/10;
276 current->end = b1*360000+b2*6000+b3*100+b4/10;
277 for (i=0; i<SUB_MAX_TEXT;) {
278 if (!fgets (line, LINE_LEN, fd)) break;
279 len=0;
280 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
281 if (len) {
282 int j=0,skip=0;
283 char *curptr=current->text[i]=(char *)malloc (len+1);
284 if (!current->text[i]) return ERR;
285 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
286 for(; j<len; j++) {
287 /* let's filter html tags ::atmos */
288 if(line[j]=='>') {
289 skip=0;
290 continue;
292 if(line[j]=='<') {
293 skip=1;
294 continue;
296 if(skip) {
297 continue;
299 *curptr=line[j];
300 curptr++;
302 *curptr='\0';
304 i++;
305 } else {
306 break;
309 current->lines=i;
311 return current;
314 subtitle *sub_read_line_subviewer2(FILE *fd,subtitle *current) {
315 char line[LINE_LEN+1];
316 int a1,a2,a3,a4;
317 char *p=NULL;
318 int i,len;
320 while (!current->text[0]) {
321 if (!fgets (line, LINE_LEN, fd)) return NULL;
322 if (line[0]!='{')
323 continue;
324 if ((len=sscanf (line, "{T %d:%d:%d:%d",&a1,&a2,&a3,&a4)) < 4)
325 continue;
326 current->start = a1*360000+a2*6000+a3*100+a4/10;
327 for (i=0; i<SUB_MAX_TEXT;) {
328 if (!fgets (line, LINE_LEN, fd)) break;
329 if (line[0]=='}') break;
330 len=0;
331 for (p=line; *p!='\n' && *p!='\r' && *p; ++p,++len);
332 if (len) {
333 current->text[i]=(char *)malloc (len+1);
334 if (!current->text[i]) return ERR;
335 strncpy (current->text[i], line, len); current->text[i][len]='\0';
336 ++i;
337 } else {
338 break;
341 current->lines=i;
343 return current;
347 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) {
348 char line[LINE_LEN+1];
349 int a1,a2,a3;
350 char *p=NULL, *next,separator;
351 int i,len,plen;
353 while (!current->text[0]) {
354 if (!fgets (line, LINE_LEN, fd)) return NULL;
355 if ((len=sscanf (line, "%d:%d:%d%c%n",&a1,&a2,&a3,&separator,&plen)) < 4)
356 continue;
358 if (!(current->start = a1*360000+a2*6000+a3*100))
359 continue;
360 /* removed by wodzu
361 p=line;
362 // finds the body of the subtitle
363 for (i=0; i<3; i++){
364 p=strchr(p,':');
365 if (p==NULL) break;
366 ++p;
368 if (p==NULL) {
369 printf("SUB: Skipping incorrect subtitle line!\n");
370 continue;
373 // by wodzu: hey! this time we know what length it has! what is
374 // that magic for? it can't deal with space instead of third
375 // colon! look, what simple it can be:
376 p = &line[ plen ];
378 i=0;
379 if (*p!='|') {
381 next = p,i=0;
382 while ((next =sub_readtext (next, &(current->text[i])))) {
383 if (current->text[i]==ERR) {return ERR;}
384 i++;
385 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
387 current->lines=i+1;
390 return current;
393 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) {
394 //TODO: This format uses quite rich (sub/super)set of xhtml
395 // I couldn't check it since DTD is not included.
396 // WARNING: full XML parses can be required for proper parsing
397 char line[LINE_LEN+1];
398 int a1,a2,a3,a4,b1,b2,b3,b4;
399 char *p=NULL,*next=NULL;
400 int i,len,plen;
402 while (!current->text[0]) {
403 if (!fgets (line, LINE_LEN, fd)) return NULL;
404 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
405 //to describe the same moment in time. Maybe there are even more formats in use.
406 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
407 plen=a1=a2=a3=a4=b1=b2=b3=b4=0;
408 if (
409 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen)) < 4) &&
410 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen)) < 5) &&
411 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) &&
412 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) &&
413 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
414 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&b4,&plen)) < 6) &&
415 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4,&plen)) < 8) &&
416 //now try it without end time
417 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen)) < 2) &&
418 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen)) < 2) &&
419 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen)) < 3) &&
420 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen)) < 4)
422 continue;
423 current->start = a1*360000+a2*6000+a3*100+a4/10;
424 current->end = b1*360000+b2*6000+b3*100+b4/10;
425 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)
426 current->end = current->start+200;
427 p=line; p+=plen;i=0;
428 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
429 next = strstr(line,"<clear/>");
430 if(next && strlen(next)>8){
431 next+=8;i=0;
432 while ((next =sub_readtext (next, &(current->text[i])))) {
433 if (current->text[i]==ERR) {return ERR;}
434 i++;
435 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
438 current->lines=i+1;
440 return current;
443 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) {
445 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
446 * other Sub Station Alpha scripts have only 8 commas before subtitle
447 * Reading the "ScriptType:" field is not reliable since many scripts appear
448 * w/o it
450 * http://www.scriptclub.org is a good place to find more examples
451 * http://www.eswat.demon.co.uk is where the SSA specs can be found
453 int comma;
454 static int max_comma = 32; /* let's use 32 for the case that the */
455 /* amount of commas increase with newer SSA versions */
457 int hour1, min1, sec1, hunsec1,
458 hour2, min2, sec2, hunsec2, nothing;
459 int num;
461 char line[LINE_LEN+1],
462 line3[LINE_LEN+1],
463 *line2;
464 char *tmp;
466 do {
467 if (!fgets (line, LINE_LEN, fd)) return NULL;
468 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
469 "%[^\n\r]", &nothing,
470 &hour1, &min1, &sec1, &hunsec1,
471 &hour2, &min2, &sec2, &hunsec2,
472 line3) < 9
474 sscanf (line, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
475 "%[^\n\r]", &nothing,
476 &hour1, &min1, &sec1, &hunsec1,
477 &hour2, &min2, &sec2, &hunsec2,
478 line3) < 9 );
480 line2=strchr(line3, ',');
482 for (comma = 4; comma < max_comma; comma ++)
484 tmp = line2;
485 if(!(tmp=strchr(++tmp, ','))) break;
486 if(*(++tmp) == ' ') break;
487 /* a space after a comma means we're already in a sentence */
488 line2 = tmp;
491 if(comma < max_comma)max_comma = comma;
492 /* eliminate the trailing comma */
493 if(*line2 == ',') line2++;
495 current->lines=0;num=0;
496 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1;
497 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2;
499 while (((tmp=strstr(line2, "\\n")) != NULL) || ((tmp=strstr(line2, "\\N")) != NULL) ){
500 current->text[num]=(char *)malloc(tmp-line2+1);
501 strncpy (current->text[num], line2, tmp-line2);
502 current->text[num][tmp-line2]='\0';
503 line2=tmp+2;
504 num++;
505 current->lines++;
506 if (current->lines >= SUB_MAX_TEXT) return current;
509 current->text[num]=strdup(line2);
510 current->lines++;
512 return current;
515 void sub_pp_ssa(subtitle *sub) {
516 int l=sub->lines;
517 char *so,*de,*start;
519 while (l){
520 /* eliminate any text enclosed with {}, they are font and color settings */
521 so=de=sub->text[--l];
522 while (*so) {
523 if(*so == '{' && so[1]=='\\') {
524 for (start=so; *so && *so!='}'; so++);
525 if(*so) so++; else so=start;
527 if(*so) {
528 *de=*so;
529 so++; de++;
532 *de=*so;
536 subtitle *sub_read_line_dunnowhat(FILE *fd,subtitle *current) {
537 char line[LINE_LEN+1];
538 char text[LINE_LEN+1];
540 if (!fgets (line, LINE_LEN, fd))
541 return NULL;
542 if (sscanf (line, "%ld,%ld,\"%[^\"]", &(current->start),
543 &(current->end), text) <3)
544 return ERR;
545 current->text[0] = strdup(text);
546 current->lines = 1;
548 return current;
551 subtitle *sub_read_line_mpsub(FILE *fd, subtitle *current) {
552 char line[LINE_LEN+1];
553 float a,b;
554 int num=0;
555 char *p, *q;
559 if (!fgets(line, LINE_LEN, fd)) return NULL;
560 } while (sscanf (line, "%f %f", &a, &b) !=2);
562 mpsub_position += a*mpsub_multiplier;
563 current->start=(int) mpsub_position;
564 mpsub_position += b*mpsub_multiplier;
565 current->end=(int) mpsub_position;
567 while (num < SUB_MAX_TEXT) {
568 if (!fgets (line, LINE_LEN, fd)) {
569 if (num == 0) return NULL;
570 else return current;
572 p=line;
573 while (isspace(*p)) p++;
574 if (eol(*p) && num > 0) return current;
575 if (eol(*p)) return NULL;
577 for (q=p; !eol(*q); q++);
578 *q='\0';
579 if (strlen(p)) {
580 current->text[num]=strdup(p);
581 // printf (">%s<\n",p);
582 current->lines = ++num;
583 } else {
584 if (num) return current;
585 else return NULL;
588 return NULL; // we should have returned before if it's OK
591 #ifndef USE_SORTSUB
592 //we don't need this if we use previous_sub_end
593 subtitle *previous_aqt_sub = NULL;
594 #endif
596 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) {
597 char line[LINE_LEN+1];
598 char *next;
599 int i;
601 while (1) {
602 // try to locate next subtitle
603 if (!fgets (line, LINE_LEN, fd))
604 return NULL;
605 if (!(sscanf (line, "-->> %ld", &(current->start)) <1))
606 break;
609 #ifdef USE_SORTSUB
610 previous_sub_end = (current->start) ? current->start - 1 : 0;
611 #else
612 if (previous_aqt_sub != NULL)
613 previous_aqt_sub->end = current->start-1;
615 previous_aqt_sub = current;
616 #endif
618 if (!fgets (line, LINE_LEN, fd))
619 return NULL;
621 sub_readtext((char *) &line,&current->text[0]);
622 current->lines = 1;
623 current->end = current->start; // will be corrected by next subtitle
625 if (!fgets (line, LINE_LEN, fd))
626 return current;
628 next = line,i=1;
629 while ((next =sub_readtext (next, &(current->text[i])))) {
630 if (current->text[i]==ERR) {return ERR;}
631 i++;
632 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
634 current->lines=i+1;
636 if ((current->text[0]=="") && (current->text[1]=="")) {
637 #ifdef USE_SORTSUB
638 previous_sub_end = 0;
639 #else
640 // void subtitle -> end of previous marked and exit
641 previous_aqt_sub = NULL;
642 #endif
643 return NULL;
646 return current;
649 #ifndef USE_SORTSUB
650 subtitle *previous_subrip09_sub = NULL;
651 #endif
653 subtitle *sub_read_line_subrip09(FILE *fd,subtitle *current) {
654 char line[LINE_LEN+1];
655 int a1,a2,a3;
656 char * next=NULL;
657 int i,len;
659 while (1) {
660 // try to locate next subtitle
661 if (!fgets (line, LINE_LEN, fd))
662 return NULL;
663 if (!((len=sscanf (line, "[%d:%d:%d]",&a1,&a2,&a3)) < 3))
664 break;
667 current->start = a1*360000+a2*6000+a3*100;
669 #ifdef USE_SORTSUB
670 previous_sub_end = (current->start) ? current->start - 1 : 0;
671 #else
672 if (previous_subrip09_sub != NULL)
673 previous_subrip09_sub->end = current->start-1;
675 previous_subrip09_sub = current;
676 #endif
678 if (!fgets (line, LINE_LEN, fd))
679 return NULL;
681 next = line,i=0;
683 current->text[0]=""; // just to be sure that string is clear
685 while ((next =sub_readtext (next, &(current->text[i])))) {
686 if (current->text[i]==ERR) {return ERR;}
687 i++;
688 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
690 current->lines=i+1;
692 if ((current->text[0]=="") && (i==0)) {
693 #ifdef USE_SORTSUB
694 previous_sub_end = 0;
695 #else
696 // void subtitle -> end of previous marked and exit
697 previous_subrip09_sub = NULL;
698 #endif
699 return NULL;
702 return current;
705 subtitle *sub_read_line_jacosub(FILE * fd, subtitle * current)
707 char line1[LINE_LEN], line2[LINE_LEN], directive[LINE_LEN], *p, *q;
708 unsigned a1, a2, a3, a4, b1, b2, b3, b4, comment = 0;
709 static unsigned jacoTimeres = 30;
710 static int jacoShift = 0;
712 bzero(current, sizeof(subtitle));
713 bzero(line1, LINE_LEN);
714 bzero(line2, LINE_LEN);
715 bzero(directive, LINE_LEN);
716 while (!current->text[0]) {
717 if (!fgets(line1, LINE_LEN, fd)) {
718 return NULL;
720 if (sscanf
721 (line1, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1, &a2, &a3, &a4,
722 &b1, &b2, &b3, &b4, line2) < 9) {
723 if (sscanf(line1, "@%u @%u %[^\n\r]", &a4, &b4, line2) < 3) {
724 if (line1[0] == '#') {
725 int hours = 0, minutes = 0, seconds, delta, inverter =
727 unsigned units = jacoShift;
728 switch (toupper(line1[1])) {
729 case 'S':
730 if (isalpha(line1[2])) {
731 delta = 6;
732 } else {
733 delta = 2;
735 if (sscanf(&line1[delta], "%d", &hours)) {
736 if (hours < 0) {
737 hours *= -1;
738 inverter = -1;
740 if (sscanf(&line1[delta], "%*d:%d", &minutes)) {
741 if (sscanf
742 (&line1[delta], "%*d:%*d:%d",
743 &seconds)) {
744 sscanf(&line1[delta], "%*d:%*d:%*d.%d",
745 &units);
746 } else {
747 hours = 0;
748 sscanf(&line1[delta], "%d:%d.%d",
749 &minutes, &seconds, &units);
750 minutes *= inverter;
752 } else {
753 hours = minutes = 0;
754 sscanf(&line1[delta], "%d.%d", &seconds,
755 &units);
756 seconds *= inverter;
758 jacoShift =
759 ((hours * 3600 + minutes * 60 +
760 seconds) * jacoTimeres +
761 units) * inverter;
763 break;
764 case 'T':
765 if (isalpha(line1[2])) {
766 delta = 8;
767 } else {
768 delta = 2;
770 sscanf(&line1[delta], "%u", &jacoTimeres);
771 break;
774 continue;
775 } else {
776 current->start =
777 (unsigned long) ((a4 + jacoShift) * 100.0 /
778 jacoTimeres);
779 current->end =
780 (unsigned long) ((b4 + jacoShift) * 100.0 /
781 jacoTimeres);
783 } else {
784 current->start =
785 (unsigned
786 long) (((a1 * 3600 + a2 * 60 + a3) * jacoTimeres + a4 +
787 jacoShift) * 100.0 / jacoTimeres);
788 current->end =
789 (unsigned
790 long) (((b1 * 3600 + b2 * 60 + b3) * jacoTimeres + b4 +
791 jacoShift) * 100.0 / jacoTimeres);
793 current->lines = 0;
794 p = line2;
795 while ((*p == ' ') || (*p == '\t')) {
796 ++p;
798 if (isalpha(*p)||*p == '[') {
799 int cont, jLength;
801 if (sscanf(p, "%s %[^\n\r]", directive, line1) < 2)
802 return (subtitle *) ERR;
803 jLength = strlen(directive);
804 for (cont = 0; cont < jLength; ++cont) {
805 if (isalpha(*(directive + cont)))
806 *(directive + cont) = toupper(*(directive + cont));
808 if ((strstr(directive, "RDB") != NULL)
809 || (strstr(directive, "RDC") != NULL)
810 || (strstr(directive, "RLB") != NULL)
811 || (strstr(directive, "RLG") != NULL)) {
812 continue;
814 if (strstr(directive, "JL") != NULL) {
815 current->alignment = SUB_ALIGNMENT_HLEFT;
816 } else if (strstr(directive, "JR") != NULL) {
817 current->alignment = SUB_ALIGNMENT_HRIGHT;
818 } else {
819 current->alignment = SUB_ALIGNMENT_HCENTER;
821 strcpy(line2, line1);
822 p = line2;
824 for (q = line1; (!eol(*p)) && (current->lines < SUB_MAX_TEXT); ++p) {
825 switch (*p) {
826 case '{':
827 comment++;
828 break;
829 case '}':
830 if (comment) {
831 --comment;
832 //the next line to get rid of a blank after the comment
833 if ((*(p + 1)) == ' ')
834 p++;
836 break;
837 case '~':
838 if (!comment) {
839 *q = ' ';
840 ++q;
842 break;
843 case ' ':
844 case '\t':
845 if ((*(p + 1) == ' ') || (*(p + 1) == '\t'))
846 break;
847 if (!comment) {
848 *q = ' ';
849 ++q;
851 break;
852 case '\\':
853 if (*(p + 1) == 'n') {
854 *q = '\0';
855 q = line1;
856 current->text[current->lines++] = strdup(line1);
857 ++p;
858 break;
860 if ((toupper(*(p + 1)) == 'C')
861 || (toupper(*(p + 1)) == 'F')) {
862 ++p,++p;
863 break;
865 if ((*(p + 1) == 'B') || (*(p + 1) == 'b') || (*(p + 1) == 'D') || //actually this means "insert current date here"
866 (*(p + 1) == 'I') || (*(p + 1) == 'i') || (*(p + 1) == 'N') || (*(p + 1) == 'T') || //actually this means "insert current time here"
867 (*(p + 1) == 'U') || (*(p + 1) == 'u')) {
868 ++p;
869 break;
871 if ((*(p + 1) == '\\') ||
872 (*(p + 1) == '~') || (*(p + 1) == '{')) {
873 ++p;
874 } else if (eol(*(p + 1))) {
875 if (!fgets(directive, LINE_LEN, fd))
876 return NULL;
877 trail_space(directive);
878 strncat(line2, directive,
879 (LINE_LEN > 511) ? LINE_LEN : 511);
880 break;
882 default:
883 if (!comment) {
884 *q = *p;
885 ++q;
887 } //-- switch
888 } //-- for
889 *q = '\0';
890 current->text[current->lines] = strdup(line1);
891 } //-- while
892 current->lines++;
893 return current;
896 int sub_autodetect (FILE *fd, int *uses_time) {
897 char line[LINE_LEN+1];
898 int i,j=0;
899 char p;
901 while (j < 100) {
902 j++;
903 if (!fgets (line, LINE_LEN, fd))
904 return SUB_INVALID;
906 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
907 {*uses_time=0;return SUB_MICRODVD;}
908 if (sscanf (line, "{%d}{}", &i)==1)
909 {*uses_time=0;return SUB_MICRODVD;}
910 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
911 {*uses_time=1;return SUB_SUBRIP;}
912 if (sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i, &i, &i, (char *)&i, &i, &i, &i, &i, (char *)&i, &i)==10)
913 {*uses_time=1;return SUB_SUBVIEWER;}
914 if (sscanf (line, "{T %d:%d:%d:%d",&i, &i, &i, &i)==4)
915 {*uses_time=1;return SUB_SUBVIEWER2;}
916 if (strstr (line, "<SAMI>"))
917 {*uses_time=1; return SUB_SAMI;}
918 if (sscanf(line, "%d:%d:%d.%d %d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i) == 8)
919 {*uses_time = 1; return SUB_JACOSUB;}
920 if (sscanf(line, "@%d @%d", &i, &i) == 2)
921 {*uses_time = 1; return SUB_JACOSUB;}
922 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3)
923 {*uses_time=1;return SUB_VPLAYER;}
924 if (sscanf (line, "%d:%d:%d ", &i, &i, &i )==3)
925 {*uses_time=1;return SUB_VPLAYER;}
926 //TODO: just checking if first line of sub starts with "<" is WAY
927 // too weak test for RT
928 // Please someone who knows the format of RT... FIX IT!!!
929 // It may conflict with other sub formats in the future (actually it doesn't)
930 if ( *line == '<' )
931 {*uses_time=1;return SUB_RT;}
933 if (!memcmp(line, "Dialogue: Marked", 16))
934 {*uses_time=1; return SUB_SSA;}
935 if (!memcmp(line, "Dialogue: ", 10))
936 {*uses_time=1; return SUB_SSA;}
937 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3)
938 {*uses_time=0;return SUB_DUNNOWHAT;}
939 if (sscanf (line, "FORMAT=%d", &i) == 1)
940 {*uses_time=0; return SUB_MPSUB;}
941 if (sscanf (line, "FORMAT=TIM%c", &p)==1 && p=='E')
942 {*uses_time=1; return SUB_MPSUB;}
943 if (strstr (line, "-->>"))
944 {*uses_time=0; return SUB_AQTITLE;}
945 if (sscanf (line, "[%d:%d:%d]", &i, &i, &i)==3)
946 {*uses_time=1;return SUB_SUBRIP09;}
949 return SUB_INVALID; // too many bad lines
952 #ifdef DUMPSUBS
953 int sub_utf8=0;
954 #else
955 extern int sub_utf8;
956 int sub_utf8_prev=0;
957 #endif
959 extern float sub_delay;
960 extern float sub_fps;
962 #ifdef USE_ICONV
963 static iconv_t icdsc = (iconv_t)(-1);
965 void subcp_open (void)
967 char *tocp = "UTF-8";
969 if (sub_cp){
970 if ((icdsc = iconv_open (tocp, sub_cp)) != (iconv_t)(-1)){
971 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: opened iconv descriptor.\n");
972 sub_utf8 = 2;
973 } else
974 mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n");
978 void subcp_close (void)
980 if (icdsc != (iconv_t)(-1)){
981 (void) iconv_close (icdsc);
982 icdsc = (iconv_t)(-1);
983 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: closed iconv descriptor.\n");
987 #define ICBUFFSIZE 512
988 static char icbuffer[ICBUFFSIZE];
990 subtitle* subcp_recode (subtitle *sub)
992 int l=sub->lines;
993 size_t ileft, oleft;
994 char *op, *ip, *ot;
996 while (l){
997 op = icbuffer;
998 ip = sub->text[--l];
999 ileft = strlen(ip);
1000 oleft = ICBUFFSIZE - 1;
1002 if (iconv(icdsc, &ip, &ileft,
1003 &op, &oleft) == (size_t)(-1)) {
1004 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error recoding line (1).\n");
1005 l++;
1006 break;
1008 if (!(ot = (char *)malloc(op - icbuffer + 1))){
1009 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1010 l++;
1011 break;
1013 *op='\0' ;
1014 strcpy (ot, icbuffer);
1015 free (sub->text[l]);
1016 sub->text[l] = ot;
1018 if (l){
1019 for (l = sub->lines; l;)
1020 free (sub->text[--l]);
1021 return ERR;
1023 return sub;
1026 // for demux_ogg.c:
1027 subtitle* subcp_recode1 (subtitle *sub)
1029 int l=sub->lines;
1030 size_t ileft, oleft;
1032 if(icdsc == (iconv_t)(-1)) return sub;
1034 while (l){
1035 char *ip = icbuffer;
1036 char *op = sub->text[--l];
1037 strcpy(ip, op);
1038 ileft = strlen(ip);
1039 oleft = ICBUFFSIZE - 1;
1041 if (iconv(icdsc, &ip, &ileft,
1042 &op, &oleft) == (size_t)(-1)) {
1043 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: error recoding line (2).\n");
1044 return sub;
1046 *op='\0' ;
1048 return sub;
1050 #endif
1052 #ifdef USE_FRIBIDI
1053 #ifndef max
1054 #define max(a,b) (((a)>(b))?(a):(b))
1055 #endif
1056 subtitle* sub_fribidi (subtitle *sub, int sub_utf8)
1058 FriBidiChar logical[LINE_LEN+1], visual[LINE_LEN+1]; // Hopefully these two won't smash the stack
1059 char *ip = NULL, *op = NULL;
1060 FriBidiCharType base;
1061 size_t len,orig_len;
1062 int l=sub->lines;
1063 int char_set_num;
1064 fribidi_boolean log2vis;
1065 if(flip_hebrew) { // Please fix the indentation someday
1066 fribidi_set_mirroring (FRIBIDI_TRUE);
1067 fribidi_set_reorder_nsm (FRIBIDI_FALSE);
1069 if( sub_utf8 == 0 ) {
1070 char_set_num = fribidi_parse_charset (fribidi_charset?fribidi_charset:"ISO8859-8");
1071 }else {
1072 char_set_num = fribidi_parse_charset ("UTF-8");
1074 while (l) {
1075 ip = sub->text[--l];
1076 orig_len = len = strlen( ip ); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1077 if(len > LINE_LEN) {
1078 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: sub->text is longer than LINE_LEN.\n");
1079 l++;
1080 break;
1082 len = fribidi_charset_to_unicode (char_set_num, ip, len, logical);
1083 base = FRIBIDI_TYPE_ON;
1084 log2vis = fribidi_log2vis (logical, len, &base,
1085 /* output */
1086 visual, NULL, NULL, NULL);
1087 if(log2vis) {
1088 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL,
1089 NULL);
1090 if((op = (char*)malloc(sizeof(char)*(max(2*orig_len,2*len) + 1))) == NULL) {
1091 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1092 l++;
1093 break;
1095 fribidi_unicode_to_charset ( char_set_num, visual, len,op);
1096 free (ip);
1097 sub->text[l] = op;
1100 if (l){
1101 for (l = sub->lines; l;)
1102 free (sub->text[--l]);
1103 return ERR;
1106 return sub;
1109 #endif
1111 static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block,
1112 int sub_num, int sub_uses_time) {
1113 int n,m;
1114 subtitle* nextsub;
1115 int i = sub_num;
1116 unsigned long subfms = (sub_uses_time ? 100 : fps) * subtime;
1117 unsigned long overlap = (sub_uses_time ? 100 : fps) / 5; // 0.2s
1119 n=m=0;
1120 if (i) for (;;){
1121 if (sub->end <= sub->start){
1122 sub->end = sub->start + subfms;
1123 m++;
1124 n++;
1126 if (!--i) break;
1127 nextsub = sub + 1;
1128 if(block){
1129 if ((sub->end > nextsub->start) && (sub->end <= nextsub->start + overlap)) {
1130 // these subtitles overlap for less than 0.2 seconds
1131 // and would result in very short overlapping subtitle
1132 // so let's fix the problem here, before overlapping code
1133 // get its hands on them
1134 unsigned delta = sub->end - nextsub->start, half = delta / 2;
1135 sub->end -= half + 1;
1136 nextsub->start += delta - half;
1138 if (sub->end >= nextsub->start){
1139 sub->end = nextsub->start - 1;
1140 if (sub->end - sub->start > subfms)
1141 sub->end = sub->start + subfms;
1142 if (!m)
1143 n++;
1147 /* Theory:
1148 * Movies are often converted from FILM (24 fps)
1149 * to PAL (25) by simply speeding it up, so we
1150 * to multiply the original timestmaps by
1151 * (Movie's FPS / Subtitle's (guessed) FPS)
1152 * so eg. for 23.98 fps movie and PAL time based
1153 * subtitles we say -subfps 25 and we're fine!
1156 /* timed sub fps correction ::atmos */
1157 if(sub_uses_time && sub_fps) {
1158 sub->start *= sub_fps/fps;
1159 sub->end *= sub_fps/fps;
1162 sub = nextsub;
1163 m = 0;
1165 if (n) mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Adjusted %d subtitle(s).\n", n);
1168 struct subreader {
1169 subtitle * (*read)(FILE *fd,subtitle *dest);
1170 void (*post)(subtitle *dest);
1171 const char *name;
1174 sub_data* sub_read_file (char *filename, float fps) {
1175 //filename is assumed to be malloc'ed, free() is used in sub_free()
1176 FILE *fd;
1177 int n_max, n_first, i, j, sub_first, sub_orig;
1178 subtitle *first, *second, *sub, *return_sub;
1179 sub_data *subt_data;
1180 int uses_time = 0, sub_num = 0, sub_errs = 0;
1181 struct subreader sr[]=
1183 { sub_read_line_microdvd, NULL, "microdvd" },
1184 { sub_read_line_subrip, NULL, "subrip" },
1185 { sub_read_line_subviewer, NULL, "subviewer" },
1186 { sub_read_line_sami, NULL, "sami" },
1187 { sub_read_line_vplayer, NULL, "vplayer" },
1188 { sub_read_line_rt, NULL, "rt" },
1189 { sub_read_line_ssa, sub_pp_ssa, "ssa" },
1190 { sub_read_line_dunnowhat, NULL, "dunnowhat" },
1191 { sub_read_line_mpsub, NULL, "mpsub" },
1192 { sub_read_line_aqt, NULL, "aqt" },
1193 { sub_read_line_subviewer2, NULL, "subviewer 2.0" },
1194 { sub_read_line_subrip09, NULL, "subrip 0.9" },
1195 { sub_read_line_jacosub, NULL, "jacosub" }
1197 struct subreader *srp;
1199 if(filename==NULL) return NULL; //qnx segfault
1200 fd=fopen (filename, "r"); if (!fd) return NULL;
1202 sub_format=sub_autodetect (fd, &uses_time);
1203 mpsub_multiplier = (uses_time ? 100.0 : 1.0);
1204 if (sub_format==SUB_INVALID) {mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: Could not determine file format\n");return NULL;}
1205 srp=sr+sub_format;
1206 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected subtitle file format: %s\n", srp->name);
1208 rewind (fd);
1210 #ifdef USE_ICONV
1211 sub_utf8_prev=sub_utf8;
1213 int l,k;
1214 k = -1;
1215 if ((l=strlen(filename))>4){
1216 char *exts[] = {".utf", ".utf8", ".utf-8" };
1217 for (k=3;--k>=0;)
1218 if (!strcasecmp(filename+(l - strlen(exts[k])), exts[k])){
1219 sub_utf8 = 1;
1220 break;
1223 if (k<0) subcp_open();
1225 #endif
1227 sub_num=0;n_max=32;
1228 first=(subtitle *)malloc(n_max*sizeof(subtitle));
1229 if(!first){
1230 #ifdef USE_ICONV
1231 subcp_close();
1232 sub_utf8=sub_utf8_prev;
1233 #endif
1234 return NULL;
1237 #ifdef USE_SORTSUB
1238 sub = (subtitle *)malloc(sizeof(subtitle));
1239 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1240 //as the beginning of the following
1241 previous_sub_end = 0;
1242 #endif
1243 while(1){
1244 if(sub_num>=n_max){
1245 n_max+=16;
1246 first=realloc(first,n_max*sizeof(subtitle));
1248 #ifndef USE_SORTSUB
1249 sub = &first[sub_num];
1250 #endif
1251 memset(sub, '\0', sizeof(subtitle));
1252 sub=srp->read(fd,sub);
1253 if(!sub) break; // EOF
1254 #ifdef USE_ICONV
1255 if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub);
1256 #endif
1257 #ifdef USE_FRIBIDI
1258 if (sub!=ERR) sub=sub_fribidi(sub,sub_utf8);
1259 #endif
1260 if ( sub == ERR )
1262 #ifdef USE_ICONV
1263 subcp_close();
1264 #endif
1265 if ( first ) free(first);
1266 return NULL;
1268 // Apply any post processing that needs recoding first
1269 if ((sub!=ERR) && !sub_no_text_pp && srp->post) srp->post(sub);
1270 #ifdef USE_SORTSUB
1271 if(!sub_num || (first[sub_num - 1].start <= sub->start)){
1272 first[sub_num].start = sub->start;
1273 first[sub_num].end = sub->end;
1274 first[sub_num].lines = sub->lines;
1275 first[sub_num].alignment = sub->alignment;
1276 for(i = 0; i < sub->lines; ++i){
1277 first[sub_num].text[i] = sub->text[i];
1279 if (previous_sub_end){
1280 first[sub_num - 1].end = previous_sub_end;
1281 previous_sub_end = 0;
1283 } else {
1284 for(j = sub_num - 1; j >= 0; --j){
1285 first[j + 1].start = first[j].start;
1286 first[j + 1].end = first[j].end;
1287 first[j + 1].lines = first[j].lines;
1288 first[j + 1].alignment = first[j].alignment;
1289 for(i = 0; i < first[j].lines; ++i){
1290 first[j + 1].text[i] = first[j].text[i];
1292 if(!j || (first[j - 1].start <= sub->start)){
1293 first[j].start = sub->start;
1294 first[j].end = sub->end;
1295 first[j].lines = sub->lines;
1296 first[j].alignment = sub->alignment;
1297 for(i = 0; i < SUB_MAX_TEXT; ++i){
1298 first[j].text[i] = sub->text[i];
1300 if (previous_sub_end){
1301 first[j].end = first[j - 1].end;
1302 first[j - 1].end = previous_sub_end;
1303 previous_sub_end = 0;
1305 break;
1309 #endif
1310 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
1313 fclose(fd);
1315 #ifdef USE_ICONV
1316 subcp_close();
1317 #endif
1319 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1320 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Read %i subtitles", sub_num);
1321 if (sub_errs) mp_msg(MSGT_SUBREADER,MSGL_INFO,", %i bad line(s).\n", sub_errs);
1322 else mp_msg(MSGT_SUBREADER,MSGL_INFO,".\n");
1324 if(sub_num<=0){
1325 free(first);
1326 return NULL;
1329 // we do overlap if the user forced it (suboverlap_enable == 2) or
1330 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1331 // this is because usually overlapping subtitles are found in these formats,
1332 // while in others they are probably result of bad timing
1333 if ((suboverlap_enabled == 2) ||
1334 ((suboverlap_enabled) && ((sub_format == SUB_JACOSUB) || (sub_format == SUB_SSA)))) {
1335 adjust_subs_time(first, 6.0, fps, 0, sub_num, uses_time);/*~6 secs AST*/
1336 // here we manage overlapping subtitles
1337 sub_orig = sub_num;
1338 n_first = sub_num;
1339 sub_num = 0;
1340 second = NULL;
1341 // for each subtitle in first[] we deal with its 'block' of
1342 // bonded subtitles
1343 for (sub_first = 0; sub_first < n_first; ++sub_first) {
1344 unsigned long global_start = first[sub_first].start,
1345 global_end = first[sub_first].end, local_start, local_end;
1346 int lines_to_add = first[sub_first].lines, sub_to_add = 0,
1347 **placeholder = NULL, higher_line = 0, counter, start_block_sub = sub_num;
1348 char real_block = 1;
1350 // here we find the number of subtitles inside the 'block'
1351 // and its span interval. this works well only with sorted
1352 // subtitles
1353 while ((sub_first + sub_to_add + 1 < n_first) && (first[sub_first + sub_to_add + 1].start < global_end)) {
1354 ++sub_to_add;
1355 lines_to_add += first[sub_first + sub_to_add].lines;
1356 if (first[sub_first + sub_to_add].start < global_start) {
1357 global_start = first[sub_first + sub_to_add].start;
1359 if (first[sub_first + sub_to_add].end > global_end) {
1360 global_end = first[sub_first + sub_to_add].end;
1364 // we need a structure to keep trace of the screen lines
1365 // used by the subs, a 'placeholder'
1366 counter = 2 * sub_to_add + 1; // the maximum number of subs derived
1367 // from a block of sub_to_add+1 subs
1368 placeholder = (int **) malloc(sizeof(int *) * counter);
1369 for (i = 0; i < counter; ++i) {
1370 placeholder[i] = (int *) malloc(sizeof(int) * lines_to_add);
1371 for (j = 0; j < lines_to_add; ++j) {
1372 placeholder[i][j] = -1;
1376 counter = 0;
1377 local_end = global_start - 1;
1378 do {
1379 int ls;
1381 // here we find the beginning and the end of a new
1382 // subtitle in the block
1383 local_start = local_end + 1;
1384 local_end = global_end;
1385 for (j = 0; j <= sub_to_add; ++j) {
1386 if ((first[sub_first + j].start - 1 > local_start) && (first[sub_first + j].start - 1 < local_end)) {
1387 local_end = first[sub_first + j].start - 1;
1388 } else if ((first[sub_first + j].end > local_start) && (first[sub_first + j].end < local_end)) {
1389 local_end = first[sub_first + j].end;
1392 // here we allocate the screen lines to subs we must
1393 // display in current local_start-local_end interval.
1394 // if the subs were yet presents in the previous interval
1395 // they keep the same lines, otherside they get unused lines
1396 for (j = 0; j <= sub_to_add; ++j) {
1397 if ((first[sub_first + j].start <= local_end) && (first[sub_first + j].end > local_start)) {
1398 unsigned long sub_lines = first[sub_first + j].lines, fragment_length = lines_to_add + 1,
1399 tmp = 0;
1400 char boolean = 0;
1401 int fragment_position = -1;
1403 // if this is not the first new sub of the block
1404 // we find if this sub was present in the previous
1405 // new sub
1406 if (counter)
1407 for (i = 0; i < lines_to_add; ++i) {
1408 if (placeholder[counter - 1][i] == sub_first + j) {
1409 placeholder[counter][i] = sub_first + j;
1410 boolean = 1;
1413 if (boolean)
1414 continue;
1416 // we are looking for the shortest among all groups of
1417 // sequential blank lines whose length is greater than or
1418 // equal to sub_lines. we store in fragment_position the
1419 // position of the shortest group, in fragment_length its
1420 // length, and in tmp the length of the group currently
1421 // examinated
1422 for (i = 0; i < lines_to_add; ++i) {
1423 if (placeholder[counter][i] == -1) {
1424 // placeholder[counter][i] is part of the current group
1425 // of blank lines
1426 ++tmp;
1427 } else {
1428 if (tmp == sub_lines) {
1429 // current group's size fits exactly the one we
1430 // need, so we stop looking
1431 fragment_position = i - tmp;
1432 tmp = 0;
1433 break;
1435 if ((tmp) && (tmp > sub_lines) && (tmp < fragment_length)) {
1436 // current group is the best we found till here,
1437 // but is still bigger than the one we are looking
1438 // for, so we keep on looking
1439 fragment_length = tmp;
1440 fragment_position = i - tmp;
1441 tmp = 0;
1442 } else {
1443 // current group doesn't fit at all, so we forget it
1444 tmp = 0;
1448 if (tmp) {
1449 // last screen line is blank, a group ends with it
1450 if ((tmp >= sub_lines) && (tmp < fragment_length)) {
1451 fragment_position = i - tmp;
1454 if (fragment_position == -1) {
1455 // it was not possible to find free screen line(s) for a subtitle,
1456 // usually this means a bug in the code; however we do not overlap
1457 mp_msg(MSGT_SUBREADER, MSGL_WARN, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1458 higher_line = SUB_MAX_TEXT + 1;
1459 break;
1460 } else {
1461 for (tmp = 0; tmp < sub_lines; ++tmp) {
1462 placeholder[counter][fragment_position + tmp] = sub_first + j;
1467 for (j = higher_line + 1; j < lines_to_add; ++j) {
1468 if (placeholder[counter][j] != -1)
1469 higher_line = j;
1470 else
1471 break;
1473 if (higher_line >= SUB_MAX_TEXT) {
1474 // the 'block' has too much lines, so we don't overlap the
1475 // subtitles
1476 second = (subtitle *) realloc(second, (sub_num + sub_to_add + 1) * sizeof(subtitle));
1477 for (j = 0; j <= sub_to_add; ++j) {
1478 int ls;
1479 memset(&second[sub_num + j], '\0', sizeof(subtitle));
1480 second[sub_num + j].start = first[sub_first + j].start;
1481 second[sub_num + j].end = first[sub_first + j].end;
1482 second[sub_num + j].lines = first[sub_first + j].lines;
1483 second[sub_num + j].alignment = first[sub_first + j].alignment;
1484 for (ls = 0; ls < second[sub_num + j].lines; ls++) {
1485 second[sub_num + j].text[ls] = strdup(first[sub_first + j].text[ls]);
1488 sub_num += sub_to_add + 1;
1489 sub_first += sub_to_add;
1490 real_block = 0;
1491 break;
1494 // we read the placeholder structure and create the new
1495 // subs.
1496 second = (subtitle *) realloc(second, (sub_num + 1) * sizeof(subtitle));
1497 memset(&second[sub_num], '\0', sizeof(subtitle));
1498 second[sub_num].start = local_start;
1499 second[sub_num].end = local_end;
1500 second[sub_num].alignment = SUB_ALIGNMENT_HCENTER;
1501 n_max = (lines_to_add < SUB_MAX_TEXT) ? lines_to_add : SUB_MAX_TEXT;
1502 for (i = 0, j = 0; j < n_max; ++j) {
1503 if (placeholder[counter][j] != -1) {
1504 int lines = first[placeholder[counter][j]].lines;
1505 for (ls = 0; ls < lines; ++ls) {
1506 second[sub_num].text[i++] = strdup(first[placeholder[counter][j]].text[ls]);
1508 j += lines - 1;
1509 } else {
1510 second[sub_num].text[i++] = strdup(" ");
1513 ++sub_num;
1514 ++counter;
1515 } while (local_end < global_end);
1516 if (real_block)
1517 for (i = 0; i < counter; ++i)
1518 second[start_block_sub + i].lines = higher_line + 1;
1520 counter = 2 * sub_to_add + 1;
1521 for (i = 0; i < counter; ++i) {
1522 free(placeholder[i]);
1524 free(placeholder);
1525 sub_first += sub_to_add;
1528 for (j = sub_orig - 1; j >= 0; --j) {
1529 for (i = first[j].lines - 1; i >= 0; --i) {
1530 free(first[j].text[i]);
1533 free(first);
1535 return_sub = second;
1536 } else { //if(suboverlap_enabled)
1537 adjust_subs_time(first, 6.0, fps, 1, sub_num, uses_time);/*~6 secs AST*/
1538 return_sub = first;
1540 if (return_sub == NULL) return NULL;
1541 subt_data = (sub_data *)malloc(sizeof(sub_data));
1542 subt_data->filename = filename;
1543 subt_data->sub_uses_time = uses_time;
1544 subt_data->sub_num = sub_num;
1545 subt_data->sub_errs = sub_errs;
1546 subt_data->subtitles = return_sub;
1547 return subt_data;
1550 #if 0
1551 char * strreplace( char * in,char * what,char * whereof )
1553 int i;
1554 char * tmp;
1556 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
1557 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
1558 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
1559 return in;
1561 #endif
1564 static void strcpy_trim(char *d, char *s)
1566 // skip leading whitespace
1567 while (*s && !isalnum(*s)) {
1568 s++;
1570 for (;;) {
1571 // copy word
1572 while (*s && isalnum(*s)) {
1573 *d = tolower(*s);
1574 s++; d++;
1576 if (*s == 0) break;
1577 // trim excess whitespace
1578 while (*s && !isalnum(*s)) {
1579 s++;
1581 if (*s == 0) break;
1582 *d++ = ' ';
1584 *d = 0;
1587 static void strcpy_strip_ext(char *d, char *s)
1589 char *tmp = strrchr(s,'.');
1590 if (!tmp) {
1591 strcpy(d, s);
1592 return;
1593 } else {
1594 strncpy(d, s, tmp-s);
1595 d[tmp-s] = 0;
1597 while (*d) {
1598 *d = tolower(*d);
1599 d++;
1603 static void strcpy_get_ext(char *d, char *s)
1605 char *tmp = strrchr(s,'.');
1606 if (!tmp) {
1607 strcpy(d, "");
1608 return;
1609 } else {
1610 strcpy(d, tmp+1);
1614 static int whiteonly(char *s)
1616 while (*s) {
1617 if (isalnum(*s)) return 0;
1618 s++;
1620 return 1;
1623 typedef struct _subfn
1625 int priority;
1626 char *fname;
1627 } subfn;
1629 static int compare_sub_priority(const void *a, const void *b)
1631 if (((subfn*)a)->priority > ((subfn*)b)->priority) {
1632 return -1;
1633 } else if (((subfn*)a)->priority < ((subfn*)b)->priority) {
1634 return 1;
1635 } else {
1636 return strcoll(((subfn*)a)->fname, ((subfn*)b)->fname);
1640 char** sub_filenames(char* path, char *fname)
1642 char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
1643 char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
1645 int len, pos, found, i, j;
1646 char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
1647 subfn *result;
1648 char **result2;
1650 int subcnt;
1652 FILE *f;
1654 DIR *d;
1655 struct dirent *de;
1657 len = (strlen(fname) > 256 ? strlen(fname) : 256)
1658 +(strlen(path) > 256 ? strlen(path) : 256)+2;
1660 f_dir = (char*)malloc(len);
1661 f_fname = (char*)malloc(len);
1662 f_fname_noext = (char*)malloc(len);
1663 f_fname_trim = (char*)malloc(len);
1665 tmp_fname_noext = (char*)malloc(len);
1666 tmp_fname_trim = (char*)malloc(len);
1667 tmp_fname_ext = (char*)malloc(len);
1669 tmpresult = (char*)malloc(len);
1671 result = (subfn*)malloc(sizeof(subfn)*MAX_SUBTITLE_FILES);
1672 memset(result, 0, sizeof(subfn)*MAX_SUBTITLE_FILES);
1674 subcnt = 0;
1676 tmp = strrchr(fname,'/');
1677 #ifdef WIN32
1678 if(!tmp)tmp = strrchr(fname,'\\');
1679 #endif
1681 // extract filename & dirname from fname
1682 if (tmp) {
1683 strcpy(f_fname, tmp+1);
1684 pos = tmp - fname;
1685 strncpy(f_dir, fname, pos+1);
1686 f_dir[pos+1] = 0;
1687 } else {
1688 strcpy(f_fname, fname);
1689 strcpy(f_dir, "./");
1692 strcpy_strip_ext(f_fname_noext, f_fname);
1693 strcpy_trim(f_fname_trim, f_fname_noext);
1695 tmp_sub_id = NULL;
1696 if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
1697 tmp_sub_id = (char*)malloc(strlen(dvdsub_lang)+1);
1698 strcpy_trim(tmp_sub_id, dvdsub_lang);
1701 // 0 = nothing
1702 // 1 = any subtitle file
1703 // 2 = any sub file containing movie name
1704 // 3 = sub file containing movie name and the lang extension
1705 for (j = 0; j <= 1; j++) {
1706 d = opendir(j == 0 ? f_dir : path);
1707 if (d) {
1708 while ((de = readdir(d))) {
1709 // retrieve various parts of the filename
1710 strcpy_strip_ext(tmp_fname_noext, de->d_name);
1711 strcpy_get_ext(tmp_fname_ext, de->d_name);
1712 strcpy_trim(tmp_fname_trim, tmp_fname_noext);
1714 // does it end with a subtitle extension?
1715 found = 0;
1716 #ifdef USE_ICONV
1717 for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
1718 #else
1719 for (i = 0; sub_exts[i]; i++) {
1720 #endif
1721 if (strcmp(sub_exts[i], tmp_fname_ext) == 0) {
1722 found = 1;
1723 break;
1727 // we have a (likely) subtitle file
1728 if (found) {
1729 int prio = 0;
1730 if (!prio && tmp_sub_id)
1732 sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id);
1733 printf("dvdsublang...%s\n", tmpresult);
1734 if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) {
1735 // matches the movie name + lang extension
1736 prio = 5;
1739 if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
1740 // matches the movie name
1741 prio = 4;
1743 if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && (sub_match_fuzziness >= 1)) {
1744 // contains the movie name
1745 tmp += strlen(f_fname_trim);
1746 if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
1747 // with sub_id specified prefer localized subtitles
1748 prio = 3;
1749 } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
1750 // without sub_id prefer "plain" name
1751 prio = 3;
1752 } else {
1753 // with no localized subs found, try any else instead
1754 prio = 2;
1757 if (!prio) {
1758 // doesn't contain the movie name
1759 // don't try in the mplayer subtitle directory
1760 if ((j == 0) && (sub_match_fuzziness >= 2)) {
1761 prio = 1;
1765 if (prio) {
1766 prio += prio;
1767 #ifdef USE_ICONV
1768 if (i<3){ // prefer UTF-8 coded
1769 prio++;
1771 #endif
1772 sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
1773 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1774 if ((f = fopen(tmpresult, "rt"))) {
1775 fclose(f);
1776 result[subcnt].priority = prio;
1777 result[subcnt].fname = strdup(tmpresult);
1778 subcnt++;
1783 if (subcnt >= MAX_SUBTITLE_FILES) break;
1785 closedir(d);
1790 if (tmp_sub_id) free(tmp_sub_id);
1792 free(f_dir);
1793 free(f_fname);
1794 free(f_fname_noext);
1795 free(f_fname_trim);
1797 free(tmp_fname_noext);
1798 free(tmp_fname_trim);
1799 free(tmp_fname_ext);
1801 free(tmpresult);
1803 qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
1805 result2 = (char**)malloc(sizeof(char*)*(subcnt+1));
1806 memset(result2, 0, sizeof(char*)*(subcnt+1));
1808 for (i = 0; i < subcnt; i++) {
1809 result2[i] = result[i].fname;
1811 result2[subcnt] = NULL;
1813 free(result);
1815 return result2;
1818 void list_sub_file(sub_data* subd){
1819 int i,j;
1820 subtitle *subs = subd->subtitles;
1822 for(j=0; j < subd->sub_num; j++){
1823 subtitle* egysub=&subs[j];
1824 printf ("%i line%c (%li-%li)\n",
1825 egysub->lines,
1826 (1==egysub->lines)?' ':'s',
1827 egysub->start,
1828 egysub->end);
1829 for (i=0; i<egysub->lines; i++) {
1830 printf ("\t\t%d: %s%s", i,egysub->text[i], i==egysub->lines-1?"":" \n ");
1832 printf ("\n");
1835 printf ("Subtitle format %s time.\n",
1836 subd->sub_uses_time ? "uses":"doesn't use");
1837 printf ("Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);
1840 void dump_srt(sub_data* subd, float fps){
1841 int i,j;
1842 int h,m,s,ms;
1843 FILE * fd;
1844 subtitle * onesub;
1845 unsigned long temp;
1846 subtitle *subs = subd->subtitles;
1848 if (!subd->sub_uses_time && sub_fps == 0)
1849 sub_fps = fps;
1850 fd=fopen("dumpsub.srt","w");
1851 if(!fd)
1853 perror("dump_srt: fopen");
1854 return;
1856 for(i=0; i < subd->sub_num; i++)
1858 onesub=subs+i; //=&subs[i];
1859 fprintf(fd,"%d\n",i+1);//line number
1861 temp=onesub->start;
1862 if (!subd->sub_uses_time)
1863 temp = temp * 100 / sub_fps;
1864 temp -= sub_delay * 100;
1865 h=temp/360000;temp%=360000; //h =1*100*60*60
1866 m=temp/6000; temp%=6000; //m =1*100*60
1867 s=temp/100; temp%=100; //s =1*100
1868 ms=temp*10; //ms=1*10
1869 fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms);
1871 temp=onesub->end;
1872 if (!subd->sub_uses_time)
1873 temp = temp * 100 / sub_fps;
1874 temp -= sub_delay * 100;
1875 h=temp/360000;temp%=360000;
1876 m=temp/6000; temp%=6000;
1877 s=temp/100; temp%=100;
1878 ms=temp*10;
1879 fprintf(fd,"%02d:%02d:%02d,%03d\n",h,m,s,ms);
1881 for(j=0;j<onesub->lines;j++)
1882 fprintf(fd,"%s\n",onesub->text[j]);
1884 fprintf(fd,"\n");
1886 fclose(fd);
1887 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
1890 void dump_mpsub(sub_data* subd, float fps){
1891 int i,j;
1892 FILE *fd;
1893 float a,b;
1894 subtitle *subs = subd->subtitles;
1896 mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps);
1897 if (sub_fps==0) sub_fps=fps;
1899 fd=fopen ("dump.mpsub", "w");
1900 if (!fd) {
1901 perror ("dump_mpsub: fopen");
1902 return;
1906 if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
1907 else fprintf (fd, "FORMAT=%5.2f\n\n", fps);
1909 for(j=0; j < subd->sub_num; j++){
1910 subtitle* egysub=&subs[j];
1911 if (subd->sub_uses_time) {
1912 a=((egysub->start-mpsub_position)/100.0);
1913 b=((egysub->end-egysub->start)/100.0);
1914 if ( (float)((int)a) == a)
1915 fprintf (fd, "%.0f",a);
1916 else
1917 fprintf (fd, "%.2f",a);
1919 if ( (float)((int)b) == b)
1920 fprintf (fd, " %.0f\n",b);
1921 else
1922 fprintf (fd, " %.2f\n",b);
1923 } else {
1924 fprintf (fd, "%ld %ld\n", (long)((egysub->start*(fps/sub_fps))-((mpsub_position*(fps/sub_fps)))),
1925 (long)(((egysub->end)-(egysub->start))*(fps/sub_fps)));
1928 mpsub_position = egysub->end;
1929 for (i=0; i<egysub->lines; i++) {
1930 fprintf (fd, "%s\n",egysub->text[i]);
1932 fprintf (fd, "\n");
1934 fclose (fd);
1935 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
1938 void dump_microdvd(sub_data* subd, float fps) {
1939 int i, delay;
1940 FILE *fd;
1941 subtitle *subs = subd->subtitles;
1942 if (sub_fps == 0)
1943 sub_fps = fps;
1944 fd = fopen("dumpsub.txt", "w");
1945 if (!fd) {
1946 perror("dumpsub.txt: fopen");
1947 return;
1949 delay = sub_delay * sub_fps;
1950 for (i = 0; i < subd->sub_num; ++i) {
1951 int j, start, end;
1952 start = subs[i].start;
1953 end = subs[i].end;
1954 if (subd->sub_uses_time) {
1955 start = start * sub_fps / 100 ;
1956 end = end * sub_fps / 100;
1958 else {
1959 start = start * sub_fps / fps;
1960 end = end * sub_fps / fps;
1962 start -= delay;
1963 end -= delay;
1964 fprintf(fd, "{%d}{%d}", start, end);
1965 for (j = 0; j < subs[i].lines; ++j)
1966 fprintf(fd, "%s%s", j ? "|" : "", subs[i].text[j]);
1967 fprintf(fd, "\n");
1969 fclose(fd);
1970 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
1973 void dump_jacosub(sub_data* subd, float fps) {
1974 int i,j;
1975 int h,m,s,cs;
1976 FILE * fd;
1977 subtitle * onesub;
1978 unsigned long temp;
1979 subtitle *subs = subd->subtitles;
1981 if (!subd->sub_uses_time && sub_fps == 0)
1982 sub_fps = fps;
1983 fd=fopen("dumpsub.jss","w");
1984 if(!fd)
1986 perror("dump_jacosub: fopen");
1987 return;
1989 fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps);
1990 for(i=0; i < subd->sub_num; i++)
1992 onesub=subs+i; //=&subs[i];
1994 temp=onesub->start;
1995 if (!subd->sub_uses_time)
1996 temp = temp * 100 / sub_fps;
1997 temp -= sub_delay * 100;
1998 h=temp/360000;temp%=360000; //h =1*100*60*60
1999 m=temp/6000; temp%=6000; //m =1*100*60
2000 s=temp/100; temp%=100; //s =1*100
2001 cs=temp; //cs=1*10
2002 fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs);
2004 temp=onesub->end;
2005 if (!subd->sub_uses_time)
2006 temp = temp * 100 / sub_fps;
2007 temp -= sub_delay * 100;
2008 h=temp/360000;temp%=360000;
2009 m=temp/6000; temp%=6000;
2010 s=temp/100; temp%=100;
2011 cs=temp;
2012 fprintf(fd,"%02d:%02d:%02d.%02d {~} ",h,m,s,cs);
2014 for(j=0;j<onesub->lines;j++)
2015 fprintf(fd,"%s%s",j ? "\\n" : "", onesub->text[j]);
2017 fprintf(fd,"\n");
2019 fclose(fd);
2020 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2023 void dump_sami(sub_data* subd, float fps) {
2024 int i,j;
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.smi","w");
2033 if(!fd)
2035 perror("dump_jacosub: fopen");
2036 return;
2038 fprintf(fd, "<SAMI>\n"
2039 "<HEAD>\n"
2040 " <STYLE TYPE=\"Text/css\">\n"
2041 " <!--\n"
2042 " P {margin-left: 29pt; margin-right: 29pt; font-size: 24pt; text-align: center; font-family: Tahoma; font-weight: bold; color: #FCDD03; background-color: #000000;}\n"
2043 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2044 " -->\n"
2045 " </STYLE>\n"
2046 "</HEAD>\n"
2047 "<BODY>\n");
2048 for(i=0; i < subd->sub_num; i++)
2050 onesub=subs+i; //=&subs[i];
2052 temp=onesub->start;
2053 if (!subd->sub_uses_time)
2054 temp = temp * 100 / sub_fps;
2055 temp -= sub_delay * 100;
2056 fprintf(fd,"\t<SYNC Start=%lu>\n"
2057 "\t <P>", temp * 10);
2059 for(j=0;j<onesub->lines;j++)
2060 fprintf(fd,"%s%s",j ? "<br>" : "", onesub->text[j]);
2062 fprintf(fd,"\n");
2064 temp=onesub->end;
2065 if (!subd->sub_uses_time)
2066 temp = temp * 100 / sub_fps;
2067 temp -= sub_delay * 100;
2068 fprintf(fd,"\t<SYNC Start=%lu>\n"
2069 "\t <P>&nbsp;\n", temp * 10);
2071 fprintf(fd, "</BODY>\n"
2072 "</SAMI>\n");
2073 fclose(fd);
2074 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2077 void sub_free( sub_data * subd )
2079 int i;
2081 if ( !subd ) return;
2083 if (subd->subtitles) {
2084 for (i=0; i < subd->subtitles->lines; i++) free( subd->subtitles->text[i] );
2085 free( subd->subtitles );
2087 if (subd->filename) free( subd->filename );
2088 free( subd );
2091 #ifdef DUMPSUBS
2092 int main(int argc, char **argv) { // for testing
2093 sub_data *subd;
2095 if(argc<2){
2096 printf("\nUsage: subreader filename.sub\n\n");
2097 exit(1);
2099 sub_cp = argv[2];
2100 subd = sub_read_file(argv[1]);
2101 if(!subd){
2102 printf("Couldn't load file.\n");
2103 exit(1);
2106 list_sub_file(subd);
2108 return 0;
2110 #endif