/src/mpv/player/external_files.c
Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <string.h> |
19 | | #include <stdlib.h> |
20 | | #include <assert.h> |
21 | | |
22 | | #include "osdep/io.h" |
23 | | |
24 | | #include "common/common.h" |
25 | | #include "common/global.h" |
26 | | #include "common/msg.h" |
27 | | #include "misc/charset_conv.h" |
28 | | #include "misc/language.h" |
29 | | #include "misc/natural_sort.h" |
30 | | #include "options/options.h" |
31 | | #include "options/path.h" |
32 | | #include "player/core.h" |
33 | | #include "external_files.h" |
34 | | |
35 | | static int test_ext(MPOpts *opts, bstr ext) |
36 | 0 | { |
37 | 0 | if (bstr_in_list0(ext, opts->sub_auto_exts)) |
38 | 0 | return STREAM_SUB; |
39 | 0 | if (bstr_in_list0(ext, opts->audio_exts)) |
40 | 0 | return STREAM_AUDIO; |
41 | 0 | if (bstr_in_list0(ext, opts->image_exts)) |
42 | 0 | return STREAM_VIDEO; |
43 | 0 | return -1; |
44 | 0 | } |
45 | | |
46 | | static int test_cover_filename(bstr fname, char **cover_files) |
47 | 0 | { |
48 | 0 | int idx = bstr_find_in_list0(fname, cover_files, false); |
49 | | // This equals to 0 if not in list (idx == -1) |
50 | 0 | return -idx - 1; |
51 | 0 | } |
52 | | |
53 | | static int compare_filename(const void *a, const void *b) |
54 | 0 | { |
55 | 0 | const struct subfn *s1 = a; |
56 | 0 | const struct subfn *s2 = b; |
57 | 0 | return mp_natural_sort_cmp(s1->fname, s2->fname); |
58 | 0 | } |
59 | | |
60 | | static int compare_priority(const void *a, const void *b) |
61 | 0 | { |
62 | 0 | const struct subfn *s1 = a; |
63 | 0 | const struct subfn *s2 = b; |
64 | 0 | if (s1->priority > s2->priority) |
65 | 0 | return -1; |
66 | 0 | if (s1->priority < s2->priority) |
67 | 0 | return 1; |
68 | 0 | return mp_natural_sort_cmp(s1->fname, s2->fname); |
69 | 0 | } |
70 | | |
71 | | static void append_dir_external_files(struct mpv_global *global, struct MPOpts *opts, |
72 | | struct subfn **slist, int *nsub, |
73 | | struct bstr path, const char *fname, |
74 | | int limit_fuzziness, int limit_type) |
75 | 0 | { |
76 | 0 | void *tmpmem = talloc_new(NULL); |
77 | 0 | struct mp_log *log = mp_log_new(tmpmem, global->log, "find_files"); |
78 | |
|
79 | 0 | struct bstr f_fbname = bstr0(mp_basename(fname)); |
80 | 0 | struct bstr f_fname = mp_iconv_to_utf8(log, f_fbname, |
81 | 0 | "UTF-8-MAC", MP_NO_LATIN1_FALLBACK); |
82 | 0 | struct bstr f_fname_noext = bstrdup(tmpmem, mp_strip_ext(f_fname)); |
83 | 0 | struct bstr f_fname_trim = bstr_strip(f_fname_noext); |
84 | |
|
85 | 0 | if (f_fbname.start != f_fname.start) |
86 | 0 | talloc_steal(tmpmem, f_fname.start); |
87 | |
|
88 | 0 | char *path0 = bstrdup0(tmpmem, path); |
89 | |
|
90 | 0 | if (mp_is_url(bstr0(path0))) |
91 | 0 | goto out; |
92 | | |
93 | 0 | DIR *d = opendir(path0); |
94 | 0 | if (!d) |
95 | 0 | goto out; |
96 | 0 | mp_verbose(log, "Loading external files in %.*s\n", BSTR_P(path)); |
97 | 0 | struct dirent *de; |
98 | 0 | while ((de = readdir(d))) { |
99 | 0 | void *tmpmem2 = talloc_new(tmpmem); |
100 | 0 | struct bstr den = bstr0(de->d_name); |
101 | 0 | struct bstr dename = mp_iconv_to_utf8(log, den, |
102 | 0 | "UTF-8-MAC", MP_NO_LATIN1_FALLBACK); |
103 | | // retrieve various parts of the filename |
104 | 0 | struct bstr tmp_fname_noext = bstrdup(tmpmem2, mp_strip_ext(dename)); |
105 | 0 | struct bstr tmp_fname_ext = mp_get_ext(dename); |
106 | 0 | struct bstr tmp_fname_trim = bstr_strip(tmp_fname_noext); |
107 | |
|
108 | 0 | if (den.start != dename.start) |
109 | 0 | talloc_steal(tmpmem2, dename.start); |
110 | | |
111 | | // check what it is (most likely) |
112 | 0 | int type = test_ext(opts, tmp_fname_ext); |
113 | 0 | char **langs = NULL; |
114 | 0 | int fuzz = -1; |
115 | 0 | switch (type) { |
116 | 0 | case STREAM_SUB: |
117 | 0 | langs = opts->stream_lang[type]; |
118 | 0 | fuzz = opts->sub_auto; |
119 | 0 | break; |
120 | 0 | case STREAM_AUDIO: |
121 | 0 | langs = opts->stream_lang[type]; |
122 | 0 | fuzz = opts->audiofile_auto; |
123 | 0 | break; |
124 | 0 | case STREAM_VIDEO: |
125 | 0 | fuzz = opts->coverart_auto; |
126 | 0 | break; |
127 | 0 | } |
128 | | |
129 | 0 | if (fuzz < 0 || (limit_type >= 0 && limit_type != type)) |
130 | 0 | goto next_file; |
131 | | |
132 | | // we have a (likely) subtitle file |
133 | | // higher prio -> auto-selection may prefer it (0 = not loaded) |
134 | 0 | int prio = 0; |
135 | |
|
136 | 0 | if (bstrcasecmp(tmp_fname_trim, f_fname_trim) == 0) |
137 | 0 | prio |= 32; // exact movie name match |
138 | |
|
139 | 0 | bstr lang = {0}; |
140 | 0 | int start = 0; |
141 | 0 | enum track_flags flags = 0; |
142 | 0 | lang = mp_guess_lang_from_filename(dename, &start, &flags); |
143 | 0 | if (bstr_case_startswith(tmp_fname_trim, f_fname_trim)) { |
144 | 0 | if (lang.len && start == f_fname_trim.len) |
145 | 0 | prio |= 16; // exact movie name + followed by lang |
146 | |
|
147 | 0 | if (lang.len && fuzz >= 1) |
148 | 0 | prio |= 4; // matches the movie name + a language was matched |
149 | |
|
150 | 0 | for (int n = 0; langs && langs[n]; n++) { |
151 | 0 | if (lang.len && bstr_case_startswith(lang, bstr0(langs[n]))) { |
152 | 0 | if (fuzz >= 1) |
153 | 0 | prio |= 8; // known language -> boost priority |
154 | 0 | break; |
155 | 0 | } |
156 | 0 | } |
157 | 0 | } |
158 | |
|
159 | 0 | if (bstr_find(tmp_fname_trim, f_fname_trim) >= 0 && fuzz >= 1) |
160 | 0 | prio |= 2; // contains the movie name |
161 | |
|
162 | 0 | if (type == STREAM_VIDEO && prio == 0) |
163 | 0 | prio = test_cover_filename(tmp_fname_trim, opts->coverart_whitelist); |
164 | | |
165 | | // doesn't contain the movie name |
166 | | // don't try in the mplayer subtitle directory |
167 | 0 | if (!limit_fuzziness && fuzz >= 2 && prio == 0) |
168 | 0 | prio = INT_MIN; |
169 | |
|
170 | 0 | if (prio) { |
171 | 0 | mp_trace(log, "Potential external file: \"%s\" Priority: %d\n", |
172 | 0 | de->d_name, prio); |
173 | 0 | char *extpath = mp_path_join_bstr(*slist, path, dename); |
174 | 0 | if (mp_path_exists(extpath)) { |
175 | 0 | MP_TARRAY_GROW(NULL, *slist, *nsub); |
176 | 0 | struct subfn *sub = *slist + (*nsub)++; |
177 | | |
178 | | // annoying and redundant |
179 | 0 | if (strncmp(extpath, "./", 2) == 0) |
180 | 0 | extpath += 2; |
181 | |
|
182 | 0 | sub->type = type; |
183 | 0 | sub->priority = prio; |
184 | 0 | sub->fname = extpath; |
185 | 0 | sub->lang = lang.len ? bstrdup0(*slist, lang) : NULL; |
186 | 0 | sub->flags = flags; |
187 | 0 | } else |
188 | 0 | talloc_free(extpath); |
189 | 0 | } |
190 | |
|
191 | 0 | next_file: |
192 | 0 | talloc_free(tmpmem2); |
193 | 0 | } |
194 | 0 | closedir(d); |
195 | |
|
196 | 0 | out: |
197 | 0 | talloc_free(tmpmem); |
198 | 0 | } |
199 | | |
200 | | static bool case_endswith(const char *s, const char *end) |
201 | 0 | { |
202 | 0 | size_t len = strlen(s); |
203 | 0 | size_t elen = strlen(end); |
204 | 0 | return len >= elen && strcasecmp(s + len - elen, end) == 0; |
205 | 0 | } |
206 | | |
207 | | // Drop .sub file if .idx file exists. |
208 | | // Assumes slist is sorted by compare_filename. |
209 | | static void filter_subidx(struct subfn **slist, int *nsub) |
210 | 0 | { |
211 | 0 | const char *prev = NULL; |
212 | 0 | for (int n = 0; n < *nsub; n++) { |
213 | 0 | const char *fname = (*slist)[n].fname; |
214 | 0 | if (case_endswith(fname, ".idx")) { |
215 | 0 | prev = fname; |
216 | 0 | } else if (case_endswith(fname, ".sub")) { |
217 | 0 | if (prev && strncmp(prev, fname, strlen(fname) - 4) == 0) |
218 | 0 | (*slist)[n].priority = 0; |
219 | 0 | } |
220 | 0 | } |
221 | 0 | for (int n = *nsub - 1; n >= 0; n--) { |
222 | 0 | if ((*slist)[n].priority == 0) |
223 | 0 | MP_TARRAY_REMOVE_AT(*slist, *nsub, n); |
224 | 0 | } |
225 | 0 | } |
226 | | |
227 | | static void load_paths(struct mpv_global *global, struct MPOpts *opts, |
228 | | struct subfn **slist, int *nsubs, const char *fname, |
229 | | char **paths, char *cfg_path, int type) |
230 | 0 | { |
231 | 0 | for (int i = 0; paths && paths[i]; i++) { |
232 | 0 | char *expanded_path = mp_get_user_path(NULL, global, paths[i]); |
233 | 0 | char *path = mp_path_join_bstr( |
234 | 0 | *slist, mp_dirname(fname), |
235 | 0 | bstr0(expanded_path ? expanded_path : paths[i])); |
236 | 0 | append_dir_external_files(global, opts, slist, nsubs, bstr0(path), |
237 | 0 | fname, 0, type); |
238 | 0 | talloc_free(expanded_path); |
239 | 0 | } |
240 | | |
241 | | // Load subtitles in ~/.mpv/sub (or similar) limiting sub fuzziness |
242 | 0 | char *mp_subdir = mp_find_config_file(NULL, global, cfg_path); |
243 | 0 | if (mp_subdir) { |
244 | 0 | append_dir_external_files(global, opts, slist, nsubs, bstr0(mp_subdir), |
245 | 0 | fname, 1, type); |
246 | 0 | } |
247 | 0 | talloc_free(mp_subdir); |
248 | 0 | } |
249 | | |
250 | | // Return a list of subtitles and audio files found, sorted by priority. |
251 | | // Last element is terminated with a fname==NULL entry. |
252 | | struct subfn *find_external_files(struct mpv_global *global, const char *fname, |
253 | | struct MPOpts *opts) |
254 | 0 | { |
255 | 0 | struct subfn *slist = talloc_array_ptrtype(NULL, slist, 1); |
256 | 0 | int n = 0; |
257 | | |
258 | | // Load subtitles from current media directory |
259 | 0 | append_dir_external_files(global, opts, &slist, &n, mp_dirname(fname), fname, 0, -1); |
260 | | |
261 | | // Load subtitles in dirs specified by sub-paths option |
262 | 0 | if (opts->sub_auto >= 0) { |
263 | 0 | load_paths(global, opts, &slist, &n, fname, opts->sub_paths, "sub", |
264 | 0 | STREAM_SUB); |
265 | 0 | } |
266 | |
|
267 | 0 | if (opts->audiofile_auto >= 0) { |
268 | 0 | load_paths(global, opts, &slist, &n, fname, opts->audiofile_paths, |
269 | 0 | "audio", STREAM_AUDIO); |
270 | 0 | } |
271 | | |
272 | | // Sort by name for filter_subidx() |
273 | 0 | qsort(slist, n, sizeof(*slist), compare_filename); |
274 | |
|
275 | 0 | filter_subidx(&slist, &n); |
276 | | |
277 | | // Sort subs by priority and append them |
278 | 0 | qsort(slist, n, sizeof(*slist), compare_priority); |
279 | |
|
280 | 0 | struct subfn z = {0}; |
281 | 0 | MP_TARRAY_APPEND(NULL, slist, n, z); |
282 | |
|
283 | 0 | return slist; |
284 | 0 | } |