diff subreader.c @ 9870:09d630a4f991

support for multiple subtitle files by Marcin Wojdyr <wojdyr@unipress.waw.pl>
author henry
date Mon, 07 Apr 2003 16:04:02 +0000
parents cc20a6dc9bc3
children eea06a971711
line wrap: on
line diff
--- a/subreader.c	Mon Apr 07 14:28:00 2003 +0000
+++ b/subreader.c	Mon Apr 07 16:04:02 2003 +0000
@@ -12,6 +12,9 @@
 #include <string.h>
 #include <ctype.h>
 
+#include <sys/types.h>
+#include <dirent.h>
+
 #include "config.h"
 #include "mp_msg.h"
 #include "subreader.h"
@@ -26,15 +29,14 @@
 #include <fribidi/fribidi.h>
 #endif
 
+extern char* dvdsub_lang;
+
 /* Maximal length of line of a subtitle */
 #define LINE_LEN 1000
-
 static float mpsub_position=0;
+static float mpsub_multiplier=1.;
+static int sub_slacktime = 20000; //20 sec
 
-int sub_uses_time=0;
-int sub_errs=0;
-int sub_num=0;          // number of subtitle structs
-int sub_slacktime=2000; // 20 seconds
 int sub_no_text_pp=0;   // 1 => do not apply text post-processing
                         // like {\...} elimination in SSA format.
 
@@ -87,7 +89,8 @@
 
 	case 0: /* find "START=" or "Slacktime:" */
 	    slacktime_s = strstr (s, "Slacktime:");
-	    if (slacktime_s) sub_slacktime = strtol (slacktime_s + 10, NULL, 0) / 10;
+	    if (slacktime_s) 
+                sub_slacktime = strtol (slacktime_s+10, NULL, 0) / 10;
 
 	    s = strstr (s, "Start=");
 	    if (s) {
@@ -532,9 +535,9 @@
 		if (!fgets(line, LINE_LEN, fd)) return NULL;
 	} while (sscanf (line, "%f %f", &a, &b) !=2);
 
-	mpsub_position += a*(sub_uses_time ? 100.0 : 1.0);
+	mpsub_position += a*mpsub_multiplier; 
 	current->start=(int) mpsub_position;
-	mpsub_position += b*(sub_uses_time ? 100.0 : 1.0);
+	mpsub_position += b*mpsub_multiplier; 
 	current->end=(int) mpsub_position;
 
 	while (num < SUB_MAX_TEXT) {
@@ -860,7 +863,7 @@
     return current;
 }
 
-int sub_autodetect (FILE *fd) {
+int sub_autodetect (FILE *fd, int *uses_time) {
     char line[LINE_LEN+1];
     int i,j=0;
     char p;
@@ -871,44 +874,44 @@
 	    return SUB_INVALID;
 
 	if (sscanf (line, "{%d}{%d}", &i, &i)==2)
-		{sub_uses_time=0;return SUB_MICRODVD;}
+		{*uses_time=0;return SUB_MICRODVD;}
 	if (sscanf (line, "{%d}{}", &i)==1)
-		{sub_uses_time=0;return SUB_MICRODVD;}
+		{*uses_time=0;return SUB_MICRODVD;}
 	if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",     &i, &i, &i, &i, &i, &i, &i, &i)==8)
-		{sub_uses_time=1;return SUB_SUBRIP;}
+		{*uses_time=1;return SUB_SUBRIP;}
 	if (sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i, &i, &i, (char *)&i, &i, &i, &i, &i, (char *)&i, &i)==10)
-		{sub_uses_time=1;return SUB_SUBVIEWER;}
+		{*uses_time=1;return SUB_SUBVIEWER;}
 	if (sscanf (line, "{T %d:%d:%d:%d",&i, &i, &i, &i))
-		{sub_uses_time=1;return SUB_SUBVIEWER2;}
+		{*uses_time=1;return SUB_SUBVIEWER2;}
 	if (strstr (line, "<SAMI>"))
-		{sub_uses_time=1; return SUB_SAMI;}
+		{*uses_time=1; return SUB_SAMI;}
 	if (sscanf(line, "%d:%d:%d.%d %d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i) == 8)
-		{sub_uses_time = 1; return SUB_JACOSUB;}
+		{*uses_time = 1; return SUB_JACOSUB;}
 	if (sscanf(line, "@%d @%d", &i, &i) == 2)
-		{sub_uses_time = 1; return SUB_JACOSUB;}
+		{*uses_time = 1; return SUB_JACOSUB;}
 	if (sscanf (line, "%d:%d:%d:",     &i, &i, &i )==3)
-		{sub_uses_time=1;return SUB_VPLAYER;}
+		{*uses_time=1;return SUB_VPLAYER;}
 	if (sscanf (line, "%d:%d:%d ",     &i, &i, &i )==3)
-		{sub_uses_time=1;return SUB_VPLAYER;}
+		{*uses_time=1;return SUB_VPLAYER;}
 	//TODO: just checking if first line of sub starts with "<" is WAY
 	// too weak test for RT
 	// Please someone who knows the format of RT... FIX IT!!!
 	// It may conflict with other sub formats in the future (actually it doesn't)
 	if ( *line == '<' )
-		{sub_uses_time=1;return SUB_RT;}
+		{*uses_time=1;return SUB_RT;}
 
 	if (!memcmp(line, "Dialogue: Marked", 16))
-		{sub_uses_time=1; return SUB_SSA;}
+		{*uses_time=1; return SUB_SSA;}
 	if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3)
-		{sub_uses_time=0;return SUB_DUNNOWHAT;}
+		{*uses_time=0;return SUB_DUNNOWHAT;}
 	if (sscanf (line, "FORMAT=%d", &i) == 1)
-		{sub_uses_time=0; return SUB_MPSUB;}
+		{*uses_time=0; return SUB_MPSUB;}
 	if (sscanf (line, "FORMAT=TIM%c", &p)==1 && p=='E')
-		{sub_uses_time=1; return SUB_MPSUB;}
+		{*uses_time=1; return SUB_MPSUB;}
 	if (strstr (line, "-->>"))
-		{sub_uses_time=0; return SUB_AQTITLE;}
+		{*uses_time=0; return SUB_AQTITLE;}
 	if (sscanf (line, "[%d:%d:%d]", &i, &i, &i)==3)
-		{sub_uses_time=1;return SUB_SUBRIP09;}
+		{*uses_time=1;return SUB_SUBRIP09;}
     }
 
     return SUB_INVALID;  // too many bad lines
@@ -1071,7 +1074,8 @@
 
 #endif
 
-static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block){
+static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block,
+                             int sub_num, int sub_uses_time) {
 	int n,m;
 	subtitle* nextsub;
 	int i = sub_num;
@@ -1133,10 +1137,13 @@
     const char *name;
 };
 
-subtitle* sub_read_file (char *filename, float fps) {
+sub_data* sub_read_file (char *filename, float fps) {
+        //filename is assumed to be malloc'ed,  free() is used in sub_free()
     FILE *fd;
     int n_max, n_first, i, j, sub_first, sub_orig;
-    subtitle *first, *second, *sub;
+    subtitle *first, *second, *sub, *return_sub;
+    sub_data *subt_data;
+    int uses_time = 0, sub_num = 0, sub_errs = 0;
     struct subreader sr[]=
     {
 	    { sub_read_line_microdvd, NULL, "microdvd" },
@@ -1158,7 +1165,8 @@
     if(filename==NULL) return NULL; //qnx segfault
     fd=fopen (filename, "r"); if (!fd) return NULL;
 
-    sub_format=sub_autodetect (fd);
+    sub_format=sub_autodetect (fd, &uses_time);
+    mpsub_multiplier = (uses_time ? 100.0 : 1.0);
     if (sub_format==SUB_INVALID) {mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: Could not determine file format\n");return NULL;}
     srp=sr+sub_format;
     mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected subtitle file format: %s\n", srp->name);
@@ -1254,7 +1262,7 @@
     subcp_close();
 #endif
 
-//    printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
+//    printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
     mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Read %i subtitles", sub_num);
     if (sub_errs) mp_msg(MSGT_SUBREADER,MSGL_INFO,", %i bad line(s).\n", sub_errs);
     else 	  mp_msg(MSGT_SUBREADER,MSGL_INFO,".\n");
@@ -1270,7 +1278,7 @@
     // while in others they are probably result of bad timing
 if ((suboverlap_enabled == 2) ||
     ((suboverlap_enabled) && ((sub_format == SUB_JACOSUB) || (sub_format == SUB_SSA)))) {
-    adjust_subs_time(first, 6.0, fps, 0);	/* ~6 secs AST */
+    adjust_subs_time(first, 6.0, fps, 0, sub_num, uses_time);/*~6 secs AST*/
 // here we manage overlapping subtitles
     sub_orig = sub_num;
     n_first = sub_num;
@@ -1468,12 +1476,19 @@
     }
     free(first);
 
-    return second;
+    return_sub = second;
 } else { //if(suboverlap_enabled)
-    adjust_subs_time(first, 6.0, fps, 1);	/* ~6 secs AST */
-
-    return first;
+    adjust_subs_time(first, 6.0, fps, 1, sub_num, uses_time);/*~6 secs AST*/
+    return_sub = first;
 }
+    if (return_sub == NULL) return NULL;
+    subt_data = (sub_data *)malloc(sizeof(sub_data));
+    subt_data->filename = filename;
+    subt_data->sub_uses_time = uses_time;
+    subt_data->sub_num = sub_num;
+    subt_data->sub_errs = sub_errs;
+    subt_data->subtitles = return_sub;
+    return subt_data;
 }
 
 #if 0
@@ -1489,80 +1504,243 @@
 }
 #endif
 
-char * sub_filename(char* path,  char * fname )
+
+static void strcpy_trim(char *d, char *s)
 {
- char * sub_name1;
- char * sub_name2;
- char * aviptr1, * aviptr2, * tmp;
- int    i,j;
- FILE * f;
- int pos=0;
- char * sub_exts[] = 
-  { ".utf",
-    ".UTF",
-    ".sub",
-    ".SUB",
-    ".srt",
-    ".SRT",
-    ".smi",
-    ".SMI",
-    ".rt",
-    ".RT",
-    ".txt",
-    ".TXT",
-    ".ssa",
-    ".SSA",
-    ".aqt",
-    ".AQT",
-    ".jss",
-    ".JSS" };
-
-
- if ( fname == NULL ) return NULL;
+    // skip leading whitespace
+    while (*s && !isalnum(*s)) {
+	s++;
+    }
+    for (;;) {
+	// copy word
+	while (*s && isalnum(*s)) {
+	    *d = tolower(*s);
+	    s++; d++;
+	}
+	if (*s == 0) break;
+	// trim excess whitespace
+	while (*s && !isalnum(*s)) {
+	    s++;
+	}
+	if (*s == 0) break;
+	*d++ = ' ';
+    }
+    *d = 0;
+}
  
- sub_name1=strrchr(fname,'.');
- if (!sub_name1) return NULL;
- pos=sub_name1-fname;
- 
- sub_name1=malloc(strlen(fname)+8);
- strcpy(sub_name1,fname);
-
- sub_name2=malloc (strlen(path) + strlen(fname) + 8);
- if ((tmp=strrchr(fname,'/')))
-	 sprintf (sub_name2, "%s%s", path, tmp+1);
- else
-	 sprintf (sub_name2, "%s%s", path, fname);
- 
- aviptr1=strrchr(sub_name1,'.');
- aviptr2=strrchr(sub_name2,'.');
+static void strcpy_strip_ext(char *d, char *s)
+{
+    char *tmp = strrchr(s,'.');
+    if (!tmp) {
+	strcpy(d, s);
+	return;
+    } else {
+	strncpy(d, s, tmp-s);
+	d[tmp-s] = 0;
+    }
+    while (*d) {
+	*d = tolower(*d);
+	d++;
+    }
+}
  
- for(j=0;j<=1;j++){
-  char* sub_name=j?sub_name1:sub_name2;
-#ifdef USE_ICONV
-  for ( i=(sub_cp?2:0);i<(sizeof(sub_exts)/sizeof(char*));i++ ) {
-#else
-  for ( i=0;i<(sizeof(sub_exts)/sizeof(char*));i++ ) {
-#endif	  
-   strcpy(j?aviptr1:aviptr2,sub_exts[i]);
-//   printf("trying: '%s'\n",sub_name);
-   if((f=fopen( sub_name,"rt" ))) {
-     fclose( f );
-     mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected sub file: %s\n",sub_name );
-     if (i<2) sub_utf8=1;
-     return sub_name;
+static void strcpy_get_ext(char *d, char *s)
+{
+    char *tmp = strrchr(s,'.');
+    if (!tmp) {
+	strcpy(d, "");
+	return;
+    } else {
+	strcpy(d, tmp+1);
    }
+}
+
+static int whiteonly(char *s)
+{
+    while (*s) {
+	if (isalnum(*s)) return 0;
+	s++;
   }
- }
- 
- free(sub_name2);
- free(sub_name1);
- return NULL;
+    return 1;
+}
+
+typedef struct _subfn 
+{
+    int priority;
+    char *fname;
+} subfn;
+
+static int compare_sub_priority(const void *a, const void *b)
+{
+    return ((subfn*)a)->priority < ((subfn*)b)->priority;
 }
 
-void list_sub_file(subtitle* subs){
+char** sub_filenames(char* path, char *fname)
+{
+    char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
+    char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
+ 
+    int len, pos, found, i, j;
+    char * sub_exts[] = { "utf", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", NULL};
+    subfn *result;
+    char **result2;
+    
+    int subcnt;
+ 
+    FILE *f;
+
+    DIR *d;
+    struct dirent *de;
+
+    len = (strlen(fname) > 256 ? strlen(fname) : 256)
+	+(strlen(path) > 256 ? strlen(path) : 256)+2;
+
+    f_dir = (char*)malloc(len);
+    f_fname = (char*)malloc(len);
+    f_fname_noext = (char*)malloc(len);
+    f_fname_trim = (char*)malloc(len);
+
+    tmp_fname_noext = (char*)malloc(len);
+    tmp_fname_trim = (char*)malloc(len);
+    tmp_fname_ext = (char*)malloc(len);
+
+    tmpresult = (char*)malloc(len);
+
+    result = (subfn*)malloc(sizeof(subfn)*MAX_SUBTITLE_FILES);
+    memset(result, 0, sizeof(subfn)*MAX_SUBTITLE_FILES);
+    
+    subcnt = 0;
+    
+    tmp = strrchr(fname,'/');
+    
+    // extract filename & dirname from fname
+    if (tmp) {
+	strcpy(f_fname, tmp+1);
+	pos = tmp - fname;
+	strncpy(f_dir, fname, pos+1);
+	f_dir[pos+1] = 0;
+    } else {
+	strcpy(f_fname, fname);
+	strcpy(f_dir, "./");
+    }
+ 
+    strcpy_strip_ext(f_fname_noext, f_fname);
+    strcpy_trim(f_fname_trim, f_fname_noext);
+
+    tmp_sub_id = NULL;
+    if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
+	tmp_sub_id = (char*)malloc(strlen(dvdsub_lang)+1);
+	strcpy_trim(tmp_sub_id, dvdsub_lang);
+    }
+
+    // 0 = nothing
+    // 1 = any subtitle file
+    // 2 = any sub file containing movie name
+    // 3 = sub file containing movie name and the lang extension
+    for (j = 0; j <= 1; j++) {
+	d = opendir(j == 0 ? f_dir : path);
+	if (d) {
+	    while (de = readdir(d)) {
+		// retrieve various parts of the filename
+		strcpy_strip_ext(tmp_fname_noext, de->d_name);
+		strcpy_get_ext(tmp_fname_ext, de->d_name);
+		strcpy_trim(tmp_fname_trim, tmp_fname_noext);
+
+		// does it end with a subtitle extension?
+		found = 0;
+		for (i = (sub_cp ? 1 : 0); sub_exts[i]; i++) {
+		    if (strcmp(sub_exts[i], tmp_fname_ext) == 0) {
+			found = 1;
+			break;
+		    }
+		}
+ 
+		// we have a (likely) subtitle file
+		if (found) {
+		    // does it contain the movie name?
+		    tmp = strstr(tmp_fname_trim, f_fname_trim);
+		    if (tmp) {
+			tmp += strlen(f_fname_trim);
+			if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
+			    // with sub_id specified prefer localized subtitles
+			    sprintf(tmpresult, "%s%s", f_dir, de->d_name);
+			    if ((f = fopen(tmpresult, "rt"))) {
+				fclose(f);
+				result[subcnt].priority = 3;
+				result[subcnt].fname = strdup(tmpresult);
+				subcnt++;
+			    }
+			} else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
+			    // without sub_id prefer "plain" name
+			    sprintf(tmpresult, "%s%s", f_dir, de->d_name);
+			    if ((f = fopen(tmpresult, "rt"))) {
+				fclose(f);
+				result[subcnt].priority = 3;
+				result[subcnt].fname = strdup(tmpresult);
+				subcnt++;
+			    }
+			} else {
+			    // with no localized subs found, try any else instead
+			    sprintf(tmpresult, "%s%s", f_dir, de->d_name);
+			    if ((f = fopen(tmpresult, "rt"))) {
+				fclose(f);
+				result[subcnt].priority = 2;
+				result[subcnt].fname = strdup(tmpresult);
+				subcnt++;
+			    }
+			}
+		    } else {
+			// doesn't contain the movie name
+			// don't try in the mplayer subtitle directory
+			if (j == 0) {
+			    sprintf(tmpresult, "%s%s", f_dir, de->d_name);
+			    if ((f = fopen(tmpresult, "rt"))) {
+				fclose(f);
+				result[subcnt].priority = 1;
+				result[subcnt].fname = strdup(tmpresult);
+				subcnt++;
+			    }
+			}
+		    }
+		}
+		if (subcnt >= MAX_SUBTITLE_FILES) break;
+	    }
+	    closedir(d);
+	}
+ 
+    }
+
+    if (tmp_sub_id) free(tmp_sub_id);
+    
+    free(f_dir);
+    free(f_fname);
+    free(f_fname_noext);
+    free(f_fname_trim);
+
+    free(tmp_fname_noext);
+    free(tmp_fname_trim);
+    free(tmp_fname_ext);
+
+    free(tmpresult);
+
+    qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
+
+    result2 = (char**)malloc(sizeof(char*)*(subcnt+1));
+    memset(result2, 0, sizeof(char*)*(subcnt+1));
+
+    for (i = 0; i < subcnt; i++) {
+	result2[i] = result[i].fname;
+    }
+    result2[subcnt] = NULL;
+
+    return result2;
+}
+
+void list_sub_file(sub_data* subd){
     int i,j;
+    subtitle *subs = subd->subtitles;
 
-    for(j=0;j<sub_num;j++){
+    for(j=0; j < subd->sub_num; j++){
 	subtitle* egysub=&subs[j];
         printf ("%i line%c (%li-%li)\n",
 		    egysub->lines,
@@ -1575,18 +1753,20 @@
 	printf ("\n");
     }
 
-    printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
-    printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
-
+    printf ("Subtitle format %s time.\n", 
+                                  subd->sub_uses_time ? "uses":"doesn't use");
+    printf ("Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);
 }
-void dump_srt(subtitle* subs, float fps){
-int i,j;
-int h,m,s,ms;
-FILE * fd;
-subtitle * onesub;
-unsigned long temp;
 
-    if (!sub_uses_time && sub_fps == 0)
+void dump_srt(sub_data* subd, float fps){
+    int i,j;
+    int h,m,s,ms;
+    FILE * fd;
+    subtitle * onesub;
+    unsigned long temp;
+    subtitle *subs = subd->subtitles;
+
+    if (!subd->sub_uses_time && sub_fps == 0)
 	sub_fps = fps;
     fd=fopen("dumpsub.srt","w");
     if(!fd)
@@ -1594,13 +1774,13 @@
 	perror("dump_srt: fopen");
 	return;
     }
-    for(i=0;i<sub_num;i++)
+    for(i=0; i < subd->sub_num; i++)
     {
         onesub=subs+i;    //=&subs[i];
 	fprintf(fd,"%d\n",i+1);//line number
 
 	temp=onesub->start;
-	if (!sub_uses_time)
+	if (!subd->sub_uses_time)
 	    temp = temp * 100 / sub_fps;
 	temp -= sub_delay * 100;
 	h=temp/360000;temp%=360000;	//h =1*100*60*60
@@ -1610,7 +1790,7 @@
 	fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms);
 
 	temp=onesub->end;
-	if (!sub_uses_time)
+	if (!subd->sub_uses_time)
 	    temp = temp * 100 / sub_fps;
 	temp -= sub_delay * 100;
 	h=temp/360000;temp%=360000;
@@ -1628,12 +1808,13 @@
     mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
 }
 
-void dump_mpsub(subtitle* subs, float fps){
+void dump_mpsub(sub_data* subd, float fps){
 	int i,j;
 	FILE *fd;
 	float a,b;
+        subtitle *subs = subd->subtitles;
 
-	mpsub_position=sub_uses_time?(sub_delay*100):(sub_delay*fps);
+	mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps);
 	if (sub_fps==0) sub_fps=fps;
 
 	fd=fopen ("dump.mpsub", "w");
@@ -1643,12 +1824,12 @@
 	}
 	
 
-	if (sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
+	if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
 	else fprintf (fd, "FORMAT=%5.2f\n\n", fps);
 
-	for(j=0;j<sub_num;j++){
+	for(j=0; j < subd->sub_num; j++){
 		subtitle* egysub=&subs[j];
-		if (sub_uses_time) {
+		if (subd->sub_uses_time) {
 			a=((egysub->start-mpsub_position)/100.0);
 			b=((egysub->end-egysub->start)/100.0);
 			if ( (float)((int)a) == a)
@@ -1675,9 +1856,10 @@
 	mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
 }
 
-void dump_microdvd(subtitle* subs, float fps) {
+void dump_microdvd(sub_data* subd, float fps) {
     int i, delay;
     FILE *fd;
+    subtitle *subs = subd->subtitles;
     if (sub_fps == 0)
 	sub_fps = fps;
     fd = fopen("dumpsub.txt", "w");
@@ -1686,11 +1868,11 @@
 	return;
     }
     delay = sub_delay * sub_fps;
-    for (i = 0; i < sub_num; ++i) {
+    for (i = 0; i < subd->sub_num; ++i) {
 	int j, start, end;
 	start = subs[i].start;
 	end = subs[i].end;
-	if (sub_uses_time) {
+	if (subd->sub_uses_time) {
 	    start = start * sub_fps / 100 ;
 	    end = end * sub_fps / 100;
 	}
@@ -1709,14 +1891,15 @@
     mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
 }
 
-void dump_jacosub(subtitle* subs, float fps) {
+void dump_jacosub(sub_data* subd, float fps) {
     int i,j;
     int h,m,s,cs;
     FILE * fd;
     subtitle * onesub;
     unsigned long temp;
+    subtitle *subs = subd->subtitles;
 
-    if (!sub_uses_time && sub_fps == 0)
+    if (!subd->sub_uses_time && sub_fps == 0)
 	sub_fps = fps;
     fd=fopen("dumpsub.jss","w");
     if(!fd)
@@ -1724,13 +1907,13 @@
 	perror("dump_jacosub: fopen");
 	return;
     }
-    fprintf(fd, "#TIMERES %d\n", (sub_uses_time) ? 100 : (int)sub_fps);    
-    for(i=0;i<sub_num;i++)
+    fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps); 
+    for(i=0; i < subd->sub_num; i++)
     {
         onesub=subs+i;    //=&subs[i];
 
 	temp=onesub->start;
-	if (!sub_uses_time)
+	if (!subd->sub_uses_time)
 	    temp = temp * 100 / sub_fps;
 	temp -= sub_delay * 100;
 	h=temp/360000;temp%=360000;	//h =1*100*60*60
@@ -1740,7 +1923,7 @@
 	fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs);
 
 	temp=onesub->end;
-	if (!sub_uses_time)
+	if (!subd->sub_uses_time)
 	    temp = temp * 100 / sub_fps;
 	temp -= sub_delay * 100;
 	h=temp/360000;temp%=360000;
@@ -1758,13 +1941,14 @@
     mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
 }
 
-void dump_sami(subtitle* subs, float fps) {
+void dump_sami(sub_data* subd, float fps) {
     int i,j;
     FILE * fd;
     subtitle * onesub;
     unsigned long temp;
+    subtitle *subs = subd->subtitles;
 
-    if (!sub_uses_time && sub_fps == 0)
+    if (!subd->sub_uses_time && sub_fps == 0)
 	sub_fps = fps;
     fd=fopen("dumpsub.smi","w");
     if(!fd)
@@ -1782,12 +1966,12 @@
 		"	</STYLE>\n"
 		"</HEAD>\n"
 		"<BODY>\n");
-    for(i=0;i<sub_num;i++)
+    for(i=0; i < subd->sub_num; i++)
     {
         onesub=subs+i;    //=&subs[i];
 
 	temp=onesub->start;
-	if (!sub_uses_time)
+	if (!subd->sub_uses_time)
 	    temp = temp * 100 / sub_fps;
 	temp -= sub_delay * 100;
 	fprintf(fd,"\t<SYNC Start=%lu>\n"
@@ -1799,7 +1983,7 @@
 	fprintf(fd,"\n");
 
 	temp=onesub->end;
-	if (!sub_uses_time)
+	if (!subd->sub_uses_time)
 	    temp = temp * 100 / sub_fps;
 	temp -= sub_delay * 100;
 	fprintf(fd,"\t<SYNC Start=%lu>\n"
@@ -1811,38 +1995,36 @@
     mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
 }
 
-void sub_free( subtitle * subs )
+void sub_free( sub_data * subd )
 {
  int i;
  
- if ( !subs ) return;
+    if ( !subd ) return;
  
- sub_num=0;
- sub_errs=0;
- for ( i=0;i<subs->lines;i++ ) free( subs->text[i] );
- free( subs );
- subs=NULL;
+    if (subd->subtitles) {
+	for (i=0; i < subd->subtitles->lines; i++) free( subd->subtitles->text[i] );
+	free( subd->subtitles );
+    }
+    if (subd->filename) free( subd->filename );
+    free( subd );
 }
 
 #ifdef DUMPSUBS
 int main(int argc, char **argv) {  // for testing
-
-    int i,j;
-    subtitle *subs;
-    subtitle *egysub;
+    sub_data *subd;
     
     if(argc<2){
         printf("\nUsage: subreader filename.sub\n\n");
         exit(1);
     }
     sub_cp = argv[2]; 
-    subs=sub_read_file(argv[1]);
-    if(!subs){
+    subd = sub_read_file(argv[1]);
+    if(!subd){
         printf("Couldn't load file.\n");
         exit(1);
     }
     
-    list_sub_file(subs);
+    list_sub_file(subd);
 
     return 0;
 }