diff options
-rw-r--r-- | bstr.c | 8 | ||||
-rw-r--r-- | bstr.h | 1 | ||||
-rw-r--r-- | mpcommon.h | 5 | ||||
-rw-r--r-- | mplayer.c | 28 | ||||
-rw-r--r-- | sub/find_subfiles.c | 25 | ||||
-rw-r--r-- | sub/find_subfiles.h | 1 |
6 files changed, 47 insertions, 21 deletions
@@ -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)) { @@ -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; @@ -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 */ |