[PATCH] Replace hacky vobsub loading with a new clean one.

Clément Bœsch ubitux at gmail.com
Mon Jan 10 23:59:37 CET 2011


---
 mencoder.c      |    1 +
 mplayer.c       |   43 +++++++++++++++----------------------------
 sub/sub.h       |    1 +
 sub/subreader.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 sub/subreader.h |    3 +++
 5 files changed, 65 insertions(+), 28 deletions(-)

diff --git a/mencoder.c b/mencoder.c
index 6efe907..58c3848 100644
--- a/mencoder.c
+++ b/mencoder.c
@@ -182,6 +182,7 @@ char **sub_name=NULL;
 float sub_delay=0;
 float sub_fps=0;
 int   sub_auto = 0;
+char *vobsub_name = NULL;
 int   subcc_enabled=0;
 int   suboverlap_enabled = 1;
 
diff --git a/mplayer.c b/mplayer.c
index 67de2b3..70a9725 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -1079,6 +1079,20 @@ void add_subtitles(char *filename, float fps, int noerr)
 	    filename_recode(filename));
 }
 
+static int add_vob_subtitle(const char *vobname, const char * const ifo, int force, void *spu)
+{
+    if (!vobname)
+        return 0;
+
+    vo_vobsub = vobsub_open(vobname, ifo, force, spu);
+
+    if (!vo_vobsub && force)
+        mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub,
+               filename_recode(vobname));
+
+    return !!vo_vobsub;
+}
+
 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
 void update_set_of_subtitles(void)
     // subdata was changed, set_of_sub... have to be updated.
@@ -3134,34 +3148,7 @@ while (player_idle_mode && !filename) {
 //==================== Open VOB-Sub ============================
 
     current_module="vobsub";
-    if (vobsub_name){
-      vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
-      if(vo_vobsub==NULL)
-        mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
-		filename_recode(vobsub_name));
-    } else if (sub_auto && filename){
-      /* try to autodetect vobsub from movie filename ::atmos */
-      char *buf = strdup(filename), *psub;
-      char *pdot = strrchr(buf, '.');
-      char *pslash = strrchr(buf, '/');
-#if defined(__MINGW32__) || defined(__CYGWIN__)
-      if (!pslash) pslash = strrchr(buf, '\\');
-#endif
-      if (pdot && (!pslash || pdot > pslash))
-        *pdot = '\0';
-      vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
-      /* try from ~/.mplayer/sub */
-      if(!vo_vobsub && (psub = get_path( "sub/" ))) {
-          const char *bname = mp_basename(buf);
-          int l;
-          l = strlen(psub) + strlen(bname) + 1;
-          psub = realloc(psub,l);
-          strcat(psub,bname);
-          vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
-          free(psub);
-      }
-      free(buf);
-    }
+    load_vob_subtitle(filename, spudec_ifo, &vo_spudec, add_vob_subtitle);
     if(vo_vobsub){
       initialized_flags|=INITIALIZED_VOBSUB;
       vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
diff --git a/sub/sub.h b/sub/sub.h
index 744f8a5..c6021b0 100644
--- a/sub/sub.h
+++ b/sub/sub.h
@@ -86,6 +86,7 @@ extern mp_osd_obj_t *vo_osd_list;
 
 extern void* vo_spudec;
 extern void* vo_vobsub;
+extern char *vobsub_name;
 
 #define OSD_PLAY 0x01
 #define OSD_PAUSE 0x02
diff --git a/sub/subreader.c b/sub/subreader.c
index 5542002..3e5b320 100644
--- a/sub/subreader.c
+++ b/sub/subreader.c
@@ -37,6 +37,7 @@
 #include "subreader.h"
 #include "subassconvert.h"
 #include "sub.h"
+#include "vobsub.h"
 #include "stream/stream.h"
 #include "libavutil/common.h"
 #include "libavutil/avstring.h"
@@ -2100,6 +2101,50 @@ void load_subtitles(const char *fname, int fps, void add_f(char *, float, int))
     free(slist.subs);
 }
 
+/**
+ * @brief Load VOB subtitle matching the subtitle filename.
+ *
+ * @param fname Path to subtitle filename.
+ * @param ifo Path to .ifo file.
+ * @spu SPU decoder instance.
+ * @add_f Function called when adding a vobsub.
+ */
+void load_vob_subtitle(const char *fname, const char * const ifo, void **spu,
+                       open_vob_func add_f)
+{
+    char *name, *mp_subdir;
+
+    // Load subtitles specified by vobsub option
+    if (vobsub_name) {
+        add_f(vobsub_name, ifo, 1, spu);
+        return;
+    }
+
+    // Stop here if automatic detection disabled
+    if (!sub_auto || !fname)
+        return;
+
+    // Get only the name of the subtitle file and try to open it
+    name = malloc(strlen(fname) + 1);
+    if (!name)
+        return;
+    strcpy_strip_ext(name, fname);
+    if (add_f(name, ifo, 0, spu)) {
+        free(name);
+        return;
+    }
+
+    // If still no VOB found, try loading it from ~/.mplayer/sub
+    mp_subdir = get_path("sub/");
+    if (mp_subdir) {
+        char *psub = mp_path_join(mp_subdir, mp_basename(name));
+        add_f(psub, ifo, 0, spu);
+        free(psub);
+    }
+    free(mp_subdir);
+    free(name);
+}
+
 void list_sub_file(sub_data* subd){
     int i,j;
     subtitle *subs = subd->subtitles;
diff --git a/sub/subreader.h b/sub/subreader.h
index dd08a22..00212c1 100644
--- a/sub/subreader.h
+++ b/sub/subreader.h
@@ -83,6 +83,8 @@ extern char *fribidi_charset;
 extern int flip_hebrew;
 extern int fribidi_flip_commas;
 
+typedef int (*open_vob_func)(const char *, const char * const, int, void *);
+
 sub_data* sub_read_file (char *filename, float pts);
 subtitle* subcp_recode (subtitle *sub);
 // enca_fd is the file enca uses to determine the codepage.
@@ -95,6 +97,7 @@ const char* guess_buffer_cp(unsigned char* buffer, int buflen, const char *prefe
 const char* guess_cp(struct stream *st, const char *preferred_language, const char *fallback);
 #endif
 void load_subtitles(const char *fname, int fps, void add_f(char *, float, int));
+void load_vob_subtitle(const char *fname, const char * const spudec_ifo, void **spu, open_vob_func add_f);
 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.5


--PGNNI9BzQDUtgA2J--


More information about the MPlayer-dev-eng mailing list