changeset 32642:7b135236a44a

Factorize subtitles loading between mplayer and mencoder.
author cboesch
date Fri, 24 Dec 2010 23:02:24 +0000
parents a2822f8f5e6d
children d68c55785751
files mencoder.c mplayer.c sub/subreader.c sub/subreader.h
diffstat 4 files changed, 30 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/mencoder.c	Thu Dec 23 18:20:51 2010 +0000
+++ b/mencoder.c	Fri Dec 24 23:02:24 2010 +0000
@@ -1019,23 +1019,7 @@
 // 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);
--- a/mplayer.c	Thu Dec 23 18:20:51 2010 +0000
+++ b/mplayer.c	Fri Dec 24 23:02:24 2010 +0000
@@ -3508,21 +3508,7 @@
 // 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;
 }
--- a/sub/subreader.c	Thu Dec 23 18:20:51 2010 +0000
+++ b/sub/subreader.c	Fri Dec 24 23:02:24 2010 +0000
@@ -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"
@@ -1889,7 +1890,7 @@
     }
 }
 
-char** sub_filenames(const char* path, char *fname)
+static char **sub_filenames(const char *path, const 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;
@@ -2070,6 +2071,31 @@
     return result2;
 }
 
+/**
+ * @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
+ */
+void load_subtitles(const char *fname, int fps, void add_f(char *, float, int))
+{
+    int i;
+    if (sub_name)
+        for (i = 0; sub_name[i]; i++)
+            add_f(sub_name[i], fps, 0);
+    if (sub_auto && fname) {
+        char *psub = get_path("sub/");
+        char **tmp = sub_filenames((psub ? psub : ""), fname);
+        free(psub);
+        for (i = 0; tmp[i]; i++) {
+            add_f(tmp[i], fps, 1);
+            free(tmp[i]);
+        }
+        free(tmp);
+    }
+}
+
 void list_sub_file(sub_data* subd){
     int i,j;
     subtitle *subs = subd->subtitles;
--- a/sub/subreader.h	Thu Dec 23 18:20:51 2010 +0000
+++ b/sub/subreader.h	Fri Dec 24 23:02:24 2010 +0000
@@ -94,7 +94,7 @@
 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);