changeset 8362:b5478134c853

optional (compile-time switch) subtitles-sorting feature patch by Salvatore Falco <sfalco@studenti.ing.uniroma1.it>
author arpi
date Thu, 05 Dec 2002 00:05:57 +0000
parents 2202c00001e3
children ae74be3e2ed0
files configure subreader.c
diffstat 2 files changed, 99 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/configure	Thu Dec 05 00:03:35 2002 +0000
+++ b/configure	Thu Dec 05 00:05:57 2002 +0000
@@ -154,6 +154,7 @@
   --enable-new-conf      Enable new config stuff [disabled]
   --enable-menu          Enable osd menu support (need new config) [disabled]
   --enable-qtx-codecs	 Enable Quicktime codecs [disabled]
+  --disable-sortsub      Disable subtitles sorting [enabled]
 
 Codecs:
   --enable-gif		 enable gif89a output support [autodetect]
@@ -1029,6 +1030,7 @@
 _new_conf=no
 _menu=no
 _qtx_codecs=no
+_sortsub=yes
 
 for ac_option do
   case "$ac_option" in
@@ -1221,6 +1223,9 @@
   --enable-qtx-codecs) _qtx_codecs=yes ;;
   --disable-qtx-codecs) _qtx_codecs=no ;;
 
+  --enable-sortsub) _sortsub=yes ;;
+  --disable-sortsub) _sortsub=no ;;
+
   --language=*)
     LINGUAS=`echo $ac_option | cut -d '=' -f 2`
     ;;
@@ -4329,6 +4334,14 @@
 fi
 echores "$_qtx_codecs"
 
+echocheck "Subtitles sorting"
+if test "$_sortsub" = yes ; then
+    _def_sortsub='#define USE_SORTSUB 1'
+else
+    _def_sortsub='#undef USE_SORTSUB'
+fi
+echores "$_sortsub"
+
 # --------------- GUI specific tests begin -------------------
 echocheck "GUI"
 echo "$_gui"
@@ -5081,6 +5094,9 @@
 /* enables / disables osd menu */
 $_def_menu
 
+/* enables / disables subtitles sorting */
+$_def_sortsub
+
 /* Extension defines */
 $_def_3dnow	// only define if you have 3DNOW (AMD k6-2, AMD Athlon, iDT WinChip, etc.)
 $_def_3dnowex	// only define if you have 3DNOWEX (AMD Athlon, etc.)
--- a/subreader.c	Thu Dec 05 00:03:35 2002 +0000
+++ b/subreader.c	Thu Dec 05 00:05:57 2002 +0000
@@ -35,6 +35,20 @@
 
 /* Use the SUB_* constant defined in the header file */
 int sub_format=SUB_INVALID;
+#ifdef USE_SORTSUB
+/* 
+   Some subtitling formats, namely AQT and Subrip09, define the end of a
+   subtitle as the beginning of the following. Since currently we read one
+   subtitle at time, for these format we keep two global *subtitle,
+   previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
+   so we can change its end when we read current subtitle starting time.
+   When USE_SORTSUB is defined, we use a single global unsigned long, 
+   previous_sub_end, for both (and even future) formats, to store the end of
+   the previous sub: it is initialized to 0 in sub_read_file and eventually
+   modified by sub_read_aqt_line or sub_read_subrip09_line.
+ */
+unsigned long previous_sub_end;
+#endif
 
 static int eol(char p) {
     return (p=='\r' || p=='\n' || p=='\0');
@@ -510,7 +524,10 @@
 	return NULL; // we should have returned before if it's OK
 }
 
+#ifndef USE_SORTSUB
+//we don't need this if we use previous_sub_end
 subtitle *previous_aqt_sub = NULL;
+#endif
 
 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) {
     char line[LINE_LEN+1];
@@ -525,10 +542,14 @@
 		break;
     }
     
+#ifdef USE_SORTSUB
+    previous_sub_end = (current->start) ? current->start - 1 : 0; 
+#else
     if (previous_aqt_sub != NULL) 
 	previous_aqt_sub->end = current->start-1;
     
     previous_aqt_sub = current;
+#endif
 
     if (!fgets (line, LINE_LEN, fd))
 	return NULL;
@@ -549,15 +570,21 @@
     current->lines=i+1;
 
     if ((current->text[0]=="") && (current->text[1]=="")) {
+#ifdef USE_SORTSUB
+	previous_sub_end = 0;
+#else
 	// void subtitle -> end of previous marked and exit
 	previous_aqt_sub = NULL;
+#endif	
 	return NULL;
 	}
 
     return current;
 }
 
+#ifndef USE_SORTSUB
 subtitle *previous_subrip09_sub = NULL;
+#endif
 
 subtitle *sub_read_line_subrip09(FILE *fd,subtitle *current) {
     char line[LINE_LEN+1];
@@ -572,17 +599,21 @@
         if (!((len=sscanf (line, "[%d:%d:%d]",&a1,&a2,&a3)) < 3))
 		break;
     }
+
+    current->start = a1*360000+a2*6000+a3*100;
     
+#ifdef USE_SORTSUB
+    previous_sub_end = (current->start) ? current->start - 1 : 0; 
+#else
     if (previous_subrip09_sub != NULL) 
 	previous_subrip09_sub->end = current->start-1;
     
     previous_subrip09_sub = current;
+#endif
 
     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
@@ -595,8 +626,12 @@
     current->lines=i+1;
 
     if ((current->text[0]=="") && (i==0)) {
+#ifdef USE_SORTSUB
+	previous_sub_end = 0;
+#else
 	// void subtitle -> end of previous marked and exit
 	previous_subrip09_sub = NULL;
+#endif	
 	return NULL;
 	}
 
@@ -978,7 +1013,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)= 
@@ -1015,19 +1050,63 @@
     first=(subtitle *)malloc(n_max*sizeof(subtitle));
     if(!first) return NULL;
     
+#ifdef USE_SORTSUB
+    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;
+#endif    
     while(1){
-        subtitle *sub;
         if(sub_num>=n_max){
             n_max+=16;
             first=realloc(first,n_max*sizeof(subtitle));
         }
+#ifndef USE_SORTSUB
 	sub = &first[sub_num];
+#endif	
 	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
+#ifdef USE_SORTSUB
+	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;
+    		}
+	    }
+	}
+#endif	
         if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
     }