From ce4d1f461a31da6adae370087f0516312c9410e4 Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 25 Nov 2013 23:38:51 +0100 Subject: sub: respect detected language for fuzzy-matched external subtitles Solve this by passing through the language to the player, which then uses the generic subtitle selection code to make a choice. Fixes #367. --- mpvcore/player/loadfile.c | 15 +++++++++------ sub/find_subfiles.c | 24 ++++++++++-------------- sub/find_subfiles.h | 8 +++++++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/mpvcore/player/loadfile.c b/mpvcore/player/loadfile.c index a913c25519..46574898e5 100644 --- a/mpvcore/player/loadfile.c +++ b/mpvcore/player/loadfile.c @@ -697,20 +697,23 @@ static void open_subtitles_from_options(struct MPContext *mpctx) mp_add_subtitles(mpctx, mpctx->opts->sub_name[i]); } if (mpctx->opts->sub_auto) { // auto load sub file ... - char **tmp = find_text_subtitles(mpctx->opts, mpctx->filename); - int nsub = MP_TALLOC_ELEMS(tmp); - for (int i = 0; i < nsub; i++) { - char *filename = tmp[i]; + struct subfn *list = find_text_subtitles(mpctx->opts, mpctx->filename); + for (int i = 0; list[i].fname; i++) { + char *filename = list[i].fname; + char *lang = list[i].lang; for (int n = 0; n < mpctx->num_sources; n++) { if (strcmp(mpctx->sources[n]->stream->url, filename) == 0) goto skip; } struct track *track = mp_add_subtitles(mpctx, filename); - if (track) + if (track) { track->auto_loaded = true; + if (!track->lang) + track->lang = talloc_strdup(track, lang); + } skip:; } - talloc_free(tmp); + talloc_free(list); } } diff --git a/sub/find_subfiles.c b/sub/find_subfiles.c index fc55484c52..479e8a0785 100644 --- a/sub/find_subfiles.c +++ b/sub/find_subfiles.c @@ -41,11 +41,6 @@ static struct bstr get_ext(struct bstr s) return bstr_splice(s, dotpos + 1, s.len); } -struct subfn { - int priority; - char *fname; -}; - static int compare_sub_filename(const void *a, const void *b) { const struct subfn *s1 = a; @@ -135,14 +130,15 @@ static void append_dir_subtitles(struct MPOpts *opts, // we have a (likely) subtitle file int prio = 0; + char *found_lang = NULL; if (opts->sub_lang) { if (bstr_startswith(tmp_fname_trim, f_fname_trim)) { struct bstr lang = guess_lang_from_filename(tmp_fname_trim); if (lang.len) { for (int n = 0; opts->sub_lang[n]; n++) { - if (bstr_startswith(lang, - bstr0(opts->sub_lang[n]))) { + if (bstr_startswith0(lang, opts->sub_lang[n])) { prio = 4; // matches the movie name + lang extension + found_lang = opts->sub_lang[n]; break; } } @@ -177,6 +173,7 @@ static void append_dir_subtitles(struct MPOpts *opts, sub->priority = prio; sub->fname = subpath; + sub->lang = found_lang; } else talloc_free(subpath); } @@ -217,9 +214,10 @@ static void filter_subidx(struct subfn **slist, int *nsub) } } -char **find_text_subtitles(struct MPOpts *opts, const char *fname) +// Return a list of subtitles found, sorted by priority. +// Last element is terminated with a fname==NULL entry. +struct subfn *find_text_subtitles(struct MPOpts *opts, const char *fname) { - char **subnames = NULL; struct subfn *slist = talloc_array_ptrtype(NULL, slist, 1); int n = 0; @@ -249,10 +247,8 @@ char **find_text_subtitles(struct MPOpts *opts, const char *fname) // Sort subs by priority and append them qsort(slist, n, sizeof(*slist), compare_sub_priority); - subnames = talloc_array_ptrtype(NULL, subnames, n); - for (int i = 0; i < n; i++) - subnames[i] = talloc_strdup(subnames, slist[i].fname); + struct subfn z = {0}; + MP_TARRAY_APPEND(NULL, slist, n, z); - talloc_free(slist); - return subnames; + return slist; } diff --git a/sub/find_subfiles.h b/sub/find_subfiles.h index 30a26bb596..fa47864e71 100644 --- a/sub/find_subfiles.h +++ b/sub/find_subfiles.h @@ -21,6 +21,12 @@ struct MPOpts; -char **find_text_subtitles(struct MPOpts *opts, const char *fname); +struct subfn { + int priority; + char *fname; + char *lang; +}; + +struct subfn *find_text_subtitles(struct MPOpts *opts, const char *fname); #endif /* MPLAYER_FINDFILES_H */ -- cgit v1.2.3