summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUoti Urpala <uau@mplayer2.org>2011-04-09 05:14:55 +0300
committerUoti Urpala <uau@mplayer2.org>2011-04-20 04:22:52 +0300
commitbdfdece245b5aa863b4c57996c38b5638d7797d0 (patch)
tree3036399e13ea08cc44019d1e7208295f47226865
parent7221e28fe3d743aaf6357bb0838e762781644f0d (diff)
downloadmpv-bdfdece245b5aa863b4c57996c38b5638d7797d0.tar.bz2
mpv-bdfdece245b5aa863b4c57996c38b5638d7797d0.tar.xz
subs: move vobsub loading logic down to find_subfiles.c
Analogously to the previous commit, move path handling logic for loading external vobsub files from mplayer.c to find_subfiles.c. Based on a commit from Clément Bœsch but fixed and simplified.
-rw-r--r--bstr.c8
-rw-r--r--bstr.h1
-rw-r--r--mpcommon.h5
-rw-r--r--mplayer.c28
-rw-r--r--sub/find_subfiles.c25
-rw-r--r--sub/find_subfiles.h1
6 files changed, 47 insertions, 21 deletions
diff --git a/bstr.c b/bstr.c
index d86b488912..f4d3bdef42 100644
--- a/bstr.c
+++ b/bstr.c
@@ -62,6 +62,14 @@ int bstrchr(struct bstr str, int c)
return -1;
}
+int bstrrchr(struct bstr str, int c)
+{
+ for (int i = str.len - 1; i >= 0; i--)
+ if (str.start[i] == c)
+ return i;
+ return -1;
+}
+
struct bstr bstr_strip(struct bstr str)
{
while (str.len && isspace(*str.start)) {
diff --git a/bstr.h b/bstr.h
index 33a47c0abc..cbd96f49c9 100644
--- a/bstr.h
+++ b/bstr.h
@@ -37,6 +37,7 @@ struct bstr {
int bstrcmp(struct bstr str1, struct bstr str2);
int bstrcasecmp(struct bstr str1, struct bstr str2);
int bstrchr(struct bstr str, int c);
+int bstrrchr(struct bstr str, int c);
struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str);
struct bstr bstr_strip(struct bstr str);
struct bstr bstr_split(struct bstr str, char *sep, struct bstr *rest);
diff --git a/mpcommon.h b/mpcommon.h
index bc35934e41..fd520eacf6 100644
--- a/mpcommon.h
+++ b/mpcommon.h
@@ -25,6 +25,11 @@
#define ROUND(x) ((int)((x) < 0 ? (x) - 0.5 : (x) + 0.5))
#define MP_TALLOC_ELEMS(p) (talloc_get_size(p) / sizeof((p)[0]))
+#define MP_GROW_ARRAY(p, nextidx) do { \
+ if ((nextidx) == MP_TALLOC_ELEMS(p)) \
+ p = talloc_realloc_size(NULL, p, talloc_get_size(p) * 2); } while (0)
+#define MP_RESIZE_ARRAY(ctx, p, count) do { \
+ p = talloc_realloc_size((ctx), p, (count) * sizeof(p[0])); } while (0)
extern const char *mplayer_version;
diff --git a/mplayer.c b/mplayer.c
index 59dff418d9..91f314bcf2 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -4174,27 +4174,13 @@ if (edl_output_filename) {
mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
filename_recode(opts->vobsub_name));
} else if (opts->sub_auto && mpctx->filename){
- /* try to autodetect vobsub from movie filename ::atmos */
- char *buf = strdup(mpctx->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);
+ char **vob = find_vob_subtitles(mpctx->filename);
+ for (int i = 0; i < MP_TALLOC_ELEMS(vob); i++) {
+ vo_vobsub = vobsub_open(vob[i], spudec_ifo, 0, &vo_spudec);
+ if (vo_vobsub)
+ break;
+ }
+ talloc_free(vob);
}
if(vo_vobsub){
mpctx->initialized_flags|=INITIALIZED_VOBSUB;
diff --git a/sub/find_subfiles.c b/sub/find_subfiles.c
index 19ee0f065b..862afe53c6 100644
--- a/sub/find_subfiles.c
+++ b/sub/find_subfiles.c
@@ -5,6 +5,7 @@
#include "mp_msg.h"
#include "path.h"
+#include "mpcommon.h"
#include "sub/find_subfiles.h"
#include "sub/sub.h"
@@ -278,3 +279,27 @@ char **find_text_subtitles(const char *fname)
free(psub);
return tmp;
}
+
+char **find_vob_subtitles(const char *fname)
+{
+ char **vobs = talloc_array_ptrtype(NULL, vobs, 1);
+ int n = 0;
+
+ // Potential vobsub in the media directory
+ struct bstr bname = BSTR(mp_basename(fname));
+ int pdot = bstrrchr(bname, '.');
+ if (pdot >= 0)
+ bname.len = pdot;
+ vobs[n++] = mp_path_join(vobs, mp_dirname(fname), bname);
+
+ // Potential vobsub in ~/.mplayer/sub
+ char *mp_subdir = get_path("sub/");
+ if (mp_subdir) {
+ MP_GROW_ARRAY(vobs, n);
+ vobs[n++] = mp_path_join(vobs, BSTR(mp_subdir), bname);
+ }
+
+ free(mp_subdir);
+ MP_RESIZE_ARRAY(NULL, vobs, n);
+ return vobs;
+}
diff --git a/sub/find_subfiles.h b/sub/find_subfiles.h
index c9b0c9137e..c0391277ee 100644
--- a/sub/find_subfiles.h
+++ b/sub/find_subfiles.h
@@ -22,5 +22,6 @@
#define MAX_SUBTITLE_FILES 128
char **find_text_subtitles(const char *fname);
+char **find_vob_subtitles(const char *fname);
#endif /* MPLAYER_FINDFILES_H */