[PATCH 1/2] Make load of n subtitles directories possible

Clément Bœsch ubitux at gmail.com
Sat Nov 20 21:07:43 CET 2010


---
 mencoder.c      |   18 +------
 mplayer.c       |   16 +-----
 sub/subreader.c |  171 ++++++++++++++++++++++++++++++++++++++----------------
 sub/subreader.h |    2 +-
 4 files changed, 123 insertions(+), 84 deletions(-)

diff --git a/mencoder.c b/mencoder.c
index 57afbc2..6efe907 100644
--- a/mencoder.c
+++ b/mencoder.c
@@ -1019,23 +1019,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/mplayer.c b/mplayer.c
index 517448d..3682b29 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -3558,21 +3558,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..198f1d9 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,14 @@ typedef struct subfn
 {
     int priority;
     char *fname;
+    int noerr;
 } subfn;
 
+struct sub_list {
+    struct subfn *subs;
+    int sid;
+};
+
 static int compare_sub_priority(const void *a, const void *b)
 {
     if (((const subfn*)a)->priority > ((const subfn*)b)->priority) {
@@ -1889,17 +1896,44 @@ static int compare_sub_priority(const void *a, const void *b)
     }
 }
 
-char** sub_filenames(const char* path, char *fname)
+/**
+ * \brief Append a subtitle to a list
+ * \param dst Destination subtitles list
+ * \param fname Subtitle filename
+ * \param priority Subtitle priority
+ * \param noerr Subtitle raising error flag on open failure
+ */
+static int append_sub(struct sub_list *dst, const char *fname,
+                      int priority, int noerr)
+{
+    struct subfn *sub;
+
+    if (dst->sid >= MAX_SUBTITLE_FILES)
+        return -1;
+    sub = &dst->subs[dst->sid];
+    sub->fname    = strdup(fname);
+    sub->priority = priority;
+    sub->noerr    = noerr;
+    if (!sub->fname)
+        return -1;
+    dst->sid++;
+    return 0;
+}
+
+/**
+ * \brief Append all the subtitles in the given path matching fname
+ * \param path Look for subtitles in this directory
+ * \param fname Subtitle filename (pattern)
+ * \param limit_fuzziness Ignore flag when sub_fuziness == 2
+ */
+static void append_dir_subtitles(struct sub_list *slist, const char *path,
+                                 const char *fname, int limit_fuzziness)
 {
-    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;
 
     FILE *f;
 
@@ -1909,8 +1943,7 @@ 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 = strdup(mp_basename(fname));
     f_fname_noext = malloc(len);
     f_fname_trim = malloc(len);
 
@@ -1920,27 +1953,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,'\\');
-    if(!tmp)tmp = strrchr(fname,':');
-#endif
-
-    // 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);
 
@@ -1954,9 +1966,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 +2024,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 (!limit_fuzziness && sub_match_fuzziness >= 2) {
 			    prio = 1;
 			}
 		    }
@@ -2025,27 +2036,22 @@ 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++;
+			   fclose(f);
+			   if (append_sub(slist, tmpresult, prio, 1) < 0)
+				break;
 			}
 		    }
 
 		}
-		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);
@@ -2055,19 +2061,82 @@ char** sub_filenames(const char* path, char *fname)
     free(tmp_fname_ext);
 
     free(tmpresult);
+}
 
-    qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
-
-    result2 = calloc(subcnt + 1, sizeof(*result2));
+/**
+ * \brief Append a new series of subtitles by tracking a given directory
+ * \param dir Look for subtitles in this directory
+ * \param fname Subtitle filename (pattern)
+ * \param slist Subtitles list where new subtitles are appended
+ */
+static int track_sub_directory(const char *dir, const char *fname,
+                               struct sub_list *slist)
+{
+    char *path, *tmp;
 
-    for (i = 0; i < subcnt; i++) {
-	result2[i] = result[i].fname;
+    path = mp_dirname(fname);
+    if (!path)
+        return -1;
+    tmp = realloc(path, strlen(path) + strlen(dir) + 1);
+    if (!tmp) {
+        free(path);
+        return -1;
     }
-    result2[subcnt] = NULL;
+    path = tmp;
+    strcat(path, dir);
 
-    free(result);
+    append_dir_subtitles(slist, path, fname, 0);
 
-    return result2;
+    free(path);
+    return 0;
+}
+
+/**
+ * \brief Load all subtitles matching the subtitle filename
+ * \param fname Path to subtitle filename
+ * \param fps FPS parameter for the add subtitle function
+ * \param add_f Add subtitle function to call for each sub
+ *
+ * Subtitles are tracked and scored in various places according to the user
+ * options, sorted, and then added by calling the add_f function.
+ */
+void load_subtitles(const char *fname, int fps, void add_f(char *, float, int))
+{
+    int i;
+    char *mp_subdir;
+    struct sub_list slist;
+
+    // Load subtitles specified by sub option first
+    if (sub_name)
+        for (i = 0; sub_name[i]; i++)
+            add_f(sub_name[i], fps, 0);
+
+    // Stop here if automatic detection disabled
+    if (!sub_auto || !fname)
+        return;
+
+    slist.sid  = 0;
+    slist.subs = calloc(MAX_SUBTITLE_FILES, sizeof(*slist.subs));
+    if (!slist.subs)
+        return;
+
+    // Load subtitles from current media directory
+    track_sub_directory("", fname, &slist);
+
+    // Load subtitles in ~/.mplayer/sub
+    mp_subdir = get_path("sub/");
+    if (mp_subdir)
+        append_dir_subtitles(&slist, mp_subdir, fname, 1);
+    free(mp_subdir);
+
+    // Sort subs by priority and append them
+    qsort(slist.subs, slist.sid, sizeof(*slist.subs), compare_sub_priority);
+    for (i = 0; i < slist.sid; i++) {
+        struct subfn *sub = &slist.subs[i];
+        add_f(sub->fname, fps, sub->noerr);
+        free(sub->fname);
+    }
+    free(slist.subs);
 }
 
 void list_sub_file(sub_data* subd){
diff --git a/sub/subreader.h b/sub/subreader.h
index 323b576..dd08a22 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(const 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


--cWoXeonUoKmBZSoM
Content-Type: text/plain; charset=utf-8
Content-Disposition: attachment; filename="0002-Add-subdirs-option.patch"



More information about the MPlayer-dev-eng mailing list