[PATCH] Add subdirs option

Clément Bœsch ubitux at gmail.com
Tue Nov 9 23:48:05 CET 2010


---
 cfg-common.h    |    1 +
 mencoder.c      |   19 +------
 mpcommon.h      |    1 +
 mplayer.c       |   17 +-----
 sub/subreader.c |  162 +++++++++++++++++++++++++++++++++++++++++++------------
 sub/subreader.h |    2 +-
 6 files changed, 134 insertions(+), 68 deletions(-)

diff --git a/cfg-common.h b/cfg-common.h
index 352340c..bb0d7ab 100644
--- a/cfg-common.h
+++ b/cfg-common.h
@@ -562,6 +562,7 @@ const m_option_t common_opts[] = {
 // ------------------------- subtitles options --------------------
 
     {"sub", &sub_name, CONF_TYPE_STRING_LIST, 0, 0, 0, NULL},
+    {"subdirs", &sub_dirs, CONF_TYPE_STRING_LIST, 0, 0, 0, NULL},
 #ifdef CONFIG_FRIBIDI
     {"fribidi-charset", &fribidi_charset, CONF_TYPE_STRING, 0, 0, 0, NULL},
     {"flip-hebrew", &flip_hebrew, CONF_TYPE_FLAG, 0, 0, 1, NULL},
diff --git a/mencoder.c b/mencoder.c
index 57afbc2..403fbac 100644
--- a/mencoder.c
+++ b/mencoder.c
@@ -179,6 +179,7 @@ char *font_name=NULL;
 char *sub_font_name=NULL;
 float font_factor=0.75;
 char **sub_name=NULL;
+char **sub_dirs = NULL;
 float sub_delay=0;
 float sub_fps=0;
 int   sub_auto = 0;
@@ -1019,23 +1020,7 @@ default: {
 // after reading video params we should load subtitles because
 // we know fps so now we can adjust subtitles time to ~6 seconds AST
 // check .sub
-  if(sub_name && sub_name[0]){
-    for (i = 0; sub_name[i] != NULL; ++i)
-        add_subtitles (sub_name[i], sh_video->fps, 0);
-  } else
-  if(sub_auto && filename) { // auto load sub file ...
-    char **tmp = NULL;
-    int i = 0;
-    char *psub = get_path( "sub/" );
-    tmp = sub_filenames((psub ? psub : ""), filename);
-    free(psub);
-    while (tmp[i])
-    {
-      add_subtitles (tmp[i], sh_video->fps, 0);
-      free(tmp[i++]);
-    }
-    free(tmp);
-  }
+    load_subtitles(filename, sh_video->fps, add_subtitles);
 
     mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
     init_best_video_codec(sh_video,video_codec_list,video_fm_list);
diff --git a/mpcommon.h b/mpcommon.h
index 19110d6..4a2147e 100644
--- a/mpcommon.h
+++ b/mpcommon.h
@@ -35,6 +35,7 @@ extern int sub_auto;
 extern float sub_delay;
 extern float sub_fps;
 extern char **sub_name;
+extern char **sub_dirs;
 extern char  *font_name;
 extern char  *sub_font_name;
 extern char  *audio_lang;
diff --git a/mplayer.c b/mplayer.c
index e03bfe4..dd8e284 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -292,6 +292,7 @@ char *font_name=NULL;
 char *sub_font_name=NULL;
 float font_factor=0.75;
 char **sub_name=NULL;
+char **sub_dirs = NULL;
 float sub_delay=0;
 float sub_fps=0;
 int   sub_auto = 1;
@@ -3562,21 +3563,7 @@ if(1 || mpctx->sh_video) {
 // check .sub
   double fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
   current_module="read_subtitles_file";
-  if(sub_name){
-    for (i = 0; sub_name[i] != NULL; ++i)
-        add_subtitles (sub_name[i], fps, 0);
-  }
-  if(sub_auto) { // auto load sub file ...
-    char *psub = get_path( "sub/" );
-    char **tmp = sub_filenames((psub ? psub : ""), filename);
-    int i = 0;
-    free(psub); // release the buffer created by get_path() above
-    while (tmp[i]) {
-        add_subtitles (tmp[i], fps, 1);
-        free(tmp[i++]);
-    }
-    free(tmp);
-  }
+  load_subtitles(filename, fps, add_subtitles);
   if (mpctx->set_of_sub_size > 0)
       mpctx->sub_counts[SUB_SOURCE_SUBS] = mpctx->set_of_sub_size;
 }
diff --git a/sub/subreader.c b/sub/subreader.c
index 42d523b..def89b2 100644
--- a/sub/subreader.c
+++ b/sub/subreader.c
@@ -33,6 +33,7 @@
 #include "config.h"
 #include "mp_msg.h"
 #include "mpcommon.h"
+#include "path.h"
 #include "subreader.h"
 #include "subassconvert.h"
 #include "sub.h"
@@ -1876,8 +1877,15 @@ typedef struct subfn
 {
     int priority;
     char *fname;
+    int noerr;
 } subfn;
 
+struct sub_list {
+    struct subfn *subs;
+    size_t size;
+    int sid;
+};
+
 static int compare_sub_priority(const void *a, const void *b)
 {
     if (((const subfn*)a)->priority > ((const subfn*)b)->priority) {
@@ -1889,17 +1897,35 @@ static int compare_sub_priority(const void *a, const void *b)
     }
 }
 
-char** sub_filenames(const char* path, char *fname)
+static void append_sub(struct sub_list *dst, struct subfn *src)
+{
+    if (dst->sid >= (int)dst->size - 1) {
+        dst->size += 32;
+        dst->subs = realloc(dst->subs, sizeof(*dst->subs) * dst->size);
+    }
+    memcpy(&dst->subs[dst->sid], src, sizeof(*src));
+    dst->sid++;
+}
+
+static void merge_subs(struct sub_list *dst, struct sub_list *src)
+{
+    if (src->sid == 0)
+        return;
+    dst->size = dst->sid + src->sid;
+    dst->subs = realloc(dst->subs, sizeof(*dst->subs) * dst->size);
+    memcpy(&dst->subs[dst->sid], src->subs, src->sid * sizeof(*src->subs));
+    dst->sid += src->sid;
+}
+
+static struct sub_list get_sub_list(const char* path, char *fname)
 {
-    char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
+    char *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;
+    int len, found, i;
     char * sub_exts[] = {  "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
-    subfn *result;
-    char **result2;
 
-    int subcnt;
+    struct sub_list subs = {0};
 
     FILE *f;
 
@@ -1909,7 +1935,6 @@ char** sub_filenames(const char* path, char *fname)
     len = (strlen(fname) > 256 ? strlen(fname) : 256)
 	+(strlen(path) > 256 ? strlen(path) : 256)+2;
 
-    f_dir = malloc(len);
     f_fname = malloc(len);
     f_fname_noext = malloc(len);
     f_fname_trim = malloc(len);
@@ -1920,10 +1945,6 @@ char** sub_filenames(const char* path, char *fname)
 
     tmpresult = malloc(len);
 
-    result = calloc(MAX_SUBTITLE_FILES, sizeof(*result));
-
-    subcnt = 0;
-
     tmp = strrchr(fname,'/');
 #if HAVE_DOS_PATHS
     if(!tmp)tmp = strrchr(fname,'\\');
@@ -1933,12 +1954,8 @@ char** sub_filenames(const char* path, char *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);
@@ -1954,9 +1971,9 @@ char** sub_filenames(const char* path, char *fname)
     // 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);
+	d = opendir(path);
 	if (d) {
+            mp_msg(MSGT_SUBREADER, MSGL_INFO, "Load subtitles in %s\n", path);
 	    while ((de = readdir(d))) {
 		// retrieve various parts of the filename
 		strcpy_strip_ext(tmp_fname_noext, de->d_name);
@@ -2012,8 +2029,7 @@ char** sub_filenames(const char* path, char *fname)
 		    }
 		    if (!prio) {
 			// doesn't contain the movie name
-			// don't try in the mplayer subtitle directory
-			if ((j == 0) && (sub_match_fuzziness >= 2)) {
+			if (sub_match_fuzziness >= 2) {
 			    prio = 1;
 			}
 		    }
@@ -2025,27 +2041,27 @@ char** sub_filenames(const char* path, char *fname)
 			    prio++;
 			}
 #endif
-			sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
+			sprintf(tmpresult, "%s%s", path, de->d_name);
 //			fprintf(stderr, "%s priority %d\n", tmpresult, prio);
 			if ((f = fopen(tmpresult, "rt"))) {
-			    fclose(f);
-			    result[subcnt].priority = prio;
-			    result[subcnt].fname = strdup(tmpresult);
-			    subcnt++;
+                            struct subfn sub = {
+                                .fname    = strdup(tmpresult),
+                                .priority = prio,
+                                .noerr    = 1
+                            };
+
+                            fclose(f);
+                            append_sub(&subs, &sub);
 			}
 		    }
 
 		}
-		if (subcnt >= MAX_SUBTITLE_FILES) break;
 	    }
 	    closedir(d);
 	}
 
-    }
-
     free(tmp_sub_id);
 
-    free(f_dir);
     free(f_fname);
     free(f_fname_noext);
     free(f_fname_trim);
@@ -2056,18 +2072,94 @@ char** sub_filenames(const char* path, char *fname)
 
     free(tmpresult);
 
-    qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
+    return subs;
+}
+
+static void track_sub_directory(char *dir, char *fname, struct sub_list *fslist)
+{
+    struct sub_list tmp_list;
+    int dir_len = strlen(dir), path_len;
+    char *path, *tmp;
+
+    // Build path
+    path = malloc(strlen(fname) + dir_len + 2); // trailing '/' and '\0'
+    tmp = path;
+    if (dir[0] != '/') {
+        strcpy(path, fname);
+        tmp = strrchr(path, '/');
+        tmp = tmp ? tmp + 1 : path;
+    }
+    strcpy(tmp, dir);
+    path_len = strlen(path);
+    if (path[path_len - 1] != '/')
+        strcpy(&path[path_len], "/");
+
+    // Grab and merge subtitles filenames
+    tmp_list = get_sub_list(path, fname);
+    merge_subs(fslist, &tmp_list);
+    free(tmp_list.subs);
+
+    free(path);
+}
+
+static struct sub_list get_full_sub_list(char *fname)
+{
+    struct sub_list fslist = {0};
+    char *mp_subdir;
+
+    // Load subtitles specified by sub option with highest priority
+    if (sub_name) {
+        char **tmp;
+        for (tmp = sub_name; *tmp; tmp++) {
+            struct subfn sub = {
+                .fname    = strdup(*tmp),
+                .priority = 6,
+                .noerr    = 0
+            };
+            append_sub(&fslist, &sub);
+        }
+    }
+
+    // Automatic detection disabled
+    if (!sub_auto || !fname)
+        return fslist;
+
+    // Load subtitles from current media directory
+    track_sub_directory("", fname, &fslist);
 
-    result2 = calloc(subcnt + 1, sizeof(*result2));
+    // Load subtitles in dirs specified by subdirs option
+    if (sub_dirs) {
+        int i;
+        for (i = 0; sub_dirs[i]; i++)
+            track_sub_directory(sub_dirs[i], fname, &fslist);
+    }
 
-    for (i = 0; i < subcnt; i++) {
-	result2[i] = result[i].fname;
+    // Load subtitles in ~/.mplayer/sub
+    mp_subdir = get_path("sub/");
+    if (mp_subdir) {
+        struct sub_list tmp_list = get_sub_list(mp_subdir, fname);
+        merge_subs(&fslist, &tmp_list);
+        free(tmp_list.subs);
     }
-    result2[subcnt] = NULL;
+    free(mp_subdir);
 
-    free(result);
+    return fslist;
+}
 
-    return result2;
+void load_subtitles(char *fname, int fps, void add_f(char *, float, int))
+{
+    int i;
+    struct sub_list fslist = get_full_sub_list(fname);
+
+    if (fslist.sid == 0)
+        return;
+    qsort(fslist.subs, fslist.sid, sizeof(*fslist.subs), compare_sub_priority);
+    for (i = 0; i < fslist.sid; i++) {
+        struct subfn *sub = &fslist.subs[i];
+        add_f(sub->fname, fps, sub->noerr);
+        free(sub->fname);
+    }
+    free(fslist.subs);
 }
 
 void list_sub_file(sub_data* subd){
diff --git a/sub/subreader.h b/sub/subreader.h
index 323b576..fb67760 100644
--- a/sub/subreader.h
+++ b/sub/subreader.h
@@ -94,7 +94,7 @@ void subcp_close (void); /* for demux_ogg.c */
 const char* guess_buffer_cp(unsigned char* buffer, int buflen, const char *preferred_language, const char *fallback);
 const char* guess_cp(struct stream *st, const char *preferred_language, const char *fallback);
 #endif
-char ** sub_filenames(const char *path, char *fname);
+void load_subtitles(char *fname, int fps, void add_f(char *, float, int));
 void list_sub_file(sub_data* subd);
 void dump_srt(sub_data* subd, float fps);
 void dump_mpsub(sub_data* subd, float fps);
-- 
1.7.3.2


--EMQjp+MvU6EBGjHc--


More information about the MPlayer-dev-eng mailing list