[MPlayer-dev-eng] [PATCH] Sorting subtitles (2)

Salvatore Falco sfalco at studenti.ing.uniroma1.it
Thu Nov 14 19:40:27 CET 2002


Hi all.

I re-send this patch since I noticed that previous one was uncorrect.
This patch is for ordering the subtitles.

Bye!
-------------- next part --------------
--- subreader.c.20021114	Thu Nov 14 10:40:01 2002
+++ subreader.c	Thu Nov 14 12:38:09 2002
@@ -35,6 +35,7 @@
 
 /* Use the SUB_* constant defined in the header file */
 int sub_format=SUB_INVALID;
+unsigned long previous_sub_end;
 
 static int eol(char p) {
     return (p=='\r' || p=='\n' || p=='\0');
@@ -510,8 +511,6 @@
 	return NULL; // we should have returned before if it's OK
 }
 
-subtitle *previous_aqt_sub = NULL;
-
 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) {
     char line[LINE_LEN+1];
     char *next;
@@ -525,10 +524,7 @@
 		break;
     }
     
-    if (previous_aqt_sub != NULL) 
-	previous_aqt_sub->end = current->start-1;
-    
-    previous_aqt_sub = current;
+    previous_sub_end = (current->start) ? current->start - 1 : 0; 
 
     if (!fgets (line, LINE_LEN, fd))
 	return NULL;
@@ -549,16 +545,12 @@
     current->lines=i+1;
 
     if ((current->text[0]=="") && (current->text[1]=="")) {
-	// void subtitle -> end of previous marked and exit
-	previous_aqt_sub = NULL;
 	return NULL;
 	}
 
     return current;
 }
 
-subtitle *previous_subrip09_sub = NULL;
-
 subtitle *sub_read_line_subrip09(FILE *fd,subtitle *current) {
     char line[LINE_LEN+1];
     int a1,a2,a3;
@@ -573,16 +565,13 @@
 		break;
     }
     
-    if (previous_subrip09_sub != NULL) 
-	previous_subrip09_sub->end = current->start-1;
-    
-    previous_subrip09_sub = current;
+    current->start = a1*360000+a2*6000+a3*100;
+
+    previous_sub_end = (current->start) ? current->start - 1 : 0; 
 
     if (!fgets (line, LINE_LEN, fd))
 	return NULL;
 
-    current->start = a1*360000+a2*6000+a3*100;
-
     next = line,i=0;
     
     current->text[0]=""; // just to be sure that string is clear
@@ -596,7 +585,6 @@
 
     if ((current->text[0]=="") && (i==0)) {
 	// void subtitle -> end of previous marked and exit
-	previous_subrip09_sub = NULL;
 	return NULL;
 	}
 
@@ -970,7 +954,7 @@
 subtitle* sub_read_file (char *filename, float fps) {
     FILE *fd;
     int n_max, n_first, i, j, sub_first, sub_orig;
-    subtitle *first, *second;
+    subtitle *first, *second, *sub;
     char *fmtname[] = { "microdvd", "subrip", "subviewer", "sami", "vplayer",
 			"rt", "ssa", "dunnowhat", "mpsub", "aqt", "subviewer 2.0", "subrip 0.9", "jacosub" };
     subtitle * (*func[])(FILE *fd,subtitle *dest)= 
@@ -1007,19 +991,56 @@
     first=(subtitle *)malloc(n_max*sizeof(subtitle));
     if(!first) return NULL;
     
+    sub = (subtitle *)malloc(sizeof(subtitle));
+    //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
+    //as the beginning of the following
+    previous_sub_end = 0;
     while(1){
-        subtitle *sub;
         if(sub_num>=n_max){
             n_max+=16;
             first=realloc(first,n_max*sizeof(subtitle));
         }
-	sub = &first[sub_num];
 	memset(sub, '\0', sizeof(subtitle));
         sub=func[sub_format](fd,sub);
         if(!sub) break;   // EOF
 #ifdef USE_ICONV
 	if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub);
 #endif
+	if(!sub_num || (first[sub_num - 1].start <= sub->start)){
+	    first[sub_num].start = sub->start;
+  	    first[sub_num].end   = sub->end;
+	    first[sub_num].lines = sub->lines;
+  	    for(i = 0; i < sub->lines; ++i){
+		first[sub_num].text[i] = sub->text[i];
+  	    }
+	    if (previous_sub_end){
+  		first[sub_num - 1].end = previous_sub_end;
+    		previous_sub_end = 0;
+	    }
+	} else {
+	    for(j = sub_num - 1; j >= 0; --j){
+    		first[j + 1].start = first[j].start;
+    		first[j + 1].end   = first[j].end;
+		first[j + 1].lines = first[j].lines;
+    		for(i = 0; i < first[j].lines; ++i){
+      		    first[j + 1].text[i] = first[j].text[i];
+		}
+		if(!j || (first[j - 1].start <= sub->start)){
+	    	    first[j].start = sub->start;
+	    	    first[j].end   = sub->end;
+	    	    first[j].lines = sub->lines;
+	    	    for(i = 0; i < SUB_MAX_TEXT; ++i){
+			first[j].text[i] = sub->text[i];
+		    }
+		    if (previous_sub_end){
+			first[j].end = first[j - 1].end;
+			first[j - 1].end = previous_sub_end;
+			previous_sub_end = 0;
+		    }
+		    break;
+    		}
+	    }
+	}
         if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
     }
     
@@ -1049,7 +1070,7 @@
     second = NULL;
 // for each subtitle in first[]
     for (sub_first = 0; sub_first < n_first; ++sub_first) {
-	while (first[sub_first].start <= first[sub_first].end) {
+	while (first[sub_first].start < first[sub_first].end) {
 	    unsigned long end_time = first[sub_first].end;
 	    int lines_to_add = 0, sub_to_add, event, ls, lf;
 
@@ -1126,7 +1147,7 @@
 	    ++sub_num;
 	}			// while
     }
-    adjust_subs_time(second, 6.0, fps, 1);	/* ~6 secs AST */
+//    adjust_subs_time(second, 6.0, fps, 1);	/* ~6 secs AST */
 
     for (j = sub_orig - 1; j <= 0; --j) {
 	for (i = first[j].lines - 1; i <= 0; --i) {


More information about the MPlayer-dev-eng mailing list