summaryrefslogtreecommitdiffstats
path: root/sub/find_subfiles.c
blob: 85ea47b0c2e1b7635ea5ab12272776ab4427dea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "mp_msg.h"
#include "options.h"
#include "path.h"
#include "mpcommon.h"
#include "sub/find_subfiles.h"
#include "sub/sub.h"

static void strcpy_trim(char *d, char *s)
{
    // skip leading whitespace
    while (*s && isspace(*s)) {
        s++;
    }
    for (;;) {
        // copy word
        while (*s && !isspace(*s)) {
            *d = tolower(*s);
            s++; d++;
        }
        if (*s == 0)
            break;
        // trim excess whitespace
        while (*s && isspace(*s)) {
            s++;
        }
        if (*s == 0)
            break;
        *d++ = ' ';
    }
    *d = 0;
}

static void strcpy_strip_ext(char *d, char *s)
{
    char *tmp = strrchr(s, '.');
    if (!tmp) {
        strcpy(d, s);
        return;
    } else {
        strncpy(d, s, tmp-s);
        d[tmp-s] = 0;
    }
    while (*d) {
        *d = tolower(*d);
        d++;
    }
}

static void strcpy_get_ext(char *d, char *s)
{
    char *tmp = strrchr(s, '.');
    if (!tmp) {
        strcpy(d, "");
        return;
    } else {
        strcpy(d, tmp+1);
    }
}

static int whiteonly(char *s)
{
    while (*s) {
        if (!isspace(*s))
            return 0;
        s++;
    }
    return 1;
}

typedef struct subfn {
    int priority;
    char *fname;
} subfn;

static int compare_sub_priority(const void *a, const void *b)
{
    if (((const subfn*)a)->priority > ((const subfn*)b)->priority) {
        return -1;
    } else if (((const subfn*)a)->priority < ((const subfn*)b)->priority) {
        return 1;
    } else {
        return strcoll(((const subfn*)a)->fname, ((const subfn*)b)->fname);
    }
}

/**
 * @brief Append all the subtitles in the given path matching fname
 * @param slist pointer to the subtitles list tallocated
 * @param nsub pointer to the number of subtitles
 * @param path Look for subtitles in this directory
 * @param fname Subtitle filename (pattern)
 * @param limit_fuzziness Ignore flag when sub_fuziness == 2
 */
static void append_dir_subtitles(struct subfn **slist, int *nsub,
                                 struct bstr path, const char *fname,
                                 int limit_fuzziness)
{
    char *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
    char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;

    int len, found, i;
    char *sub_exts[] = {"utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
    FILE *f;

    DIR *d;
    struct dirent *de;

    len =   (strlen(fname) > 256 ? strlen(fname) : 256)
          + (path.len      > 256 ? path.len      : 256) + 2;

    f_fname       = mp_basename(fname);
    f_fname_noext = malloc(len);
    f_fname_trim  = malloc(len);

    tmp_fname_noext = malloc(len);
    tmp_fname_trim  = malloc(len);
    tmp_fname_ext   = malloc(len);

    tmpresult = malloc(len);

    strcpy_strip_ext(f_fname_noext, f_fname);
    strcpy_trim(f_fname_trim, f_fname_noext);

    /* The code using sub language here is broken - it assumes strict
     * "videoname languagename" syntax for the subtitle file, which is
     * very unlikely to match especially if language name uses "en,de"
     * syntax... */
    tmp_sub_id = NULL;
#if 0
    if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
        tmp_sub_id = malloc(strlen(dvdsub_lang) + 1);
        strcpy_trim(tmp_sub_id, dvdsub_lang);
    }
#endif

    // 0 = nothing
    // 1 = any subtitle file
    // 2 = any sub file containing movie name
    // 3 = sub file containing movie name and the lang extension
    char *path0 = bstrdup0(NULL, path);
    d = opendir(path0);
    talloc_free(path0);
    if (d) {
        mp_msg(MSGT_SUBREADER, MSGL_INFO, "Load subtitles in %.*s\n", BSTR_P(path));
        while ((de = readdir(d))) {
            // retrieve various parts of the filename
            strcpy_strip_ext(tmp_fname_noext, de->d_name);
            strcpy_get_ext(tmp_fname_ext, de->d_name);
            strcpy_trim(tmp_fname_trim, tmp_fname_noext);

            // does it end with a subtitle extension?
            found = 0;
#ifdef CONFIG_ICONV
#ifdef CONFIG_ENCA
            for (i = ((sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0); sub_exts[i]; i++) {
#else
            for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
#endif
#else
            for (i = 0; sub_exts[i]; i++) {
#endif
                if (strcasecmp(sub_exts[i], tmp_fname_ext) == 0) {
                    found = 1;
                    break;
                }
            }

            // we have a (likely) subtitle file
            if (found) {
                int prio = 0;
                if (!prio && tmp_sub_id) {
                    sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id);
                    if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) {
                        // matches the movie name + lang extension
                        prio = 5;
                    }
                }
                if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
                    // matches the movie name
                    prio = 4;
                }
                if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && sub_match_fuzziness >= 1) {
                    // contains the movie name
                    tmp += strlen(f_fname_trim);
                    if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
                        // with sub_id specified prefer localized subtitles
                        prio = 3;
                    } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
                        // without sub_id prefer "plain" name
                        prio = 3;
                    } else {
                        // with no localized subs found, try any else instead
                        prio = 2;
                    }
                }
                if (!prio) {
                    // doesn't contain the movie name
                    // don't try in the mplayer subtitle directory
                    if (!limit_fuzziness && sub_match_fuzziness >= 2) {
                        prio = 1;
                    }
                }

                mp_msg(MSGT_SUBREADER, MSGL_DBG2, "Potential sub file: "
                       "\"%s\"  Priority: %d\n", de->d_name, prio);
                if (prio) {
                    prio += prio;
#ifdef CONFIG_ICONV
                    if (i < 3) // prefer UTF-8 coded
                        prio++;
#endif
                    char *subpath = mp_path_join(*slist, path, BSTR(de->d_name));
                    if ((f = fopen(subpath, "rt"))) {
                        MP_GROW_ARRAY(*slist, *nsub);
                        struct subfn *sub = *slist + (*nsub)++;

                        fclose(f);
                        sub->priority = prio;
                        sub->fname    = subpath;
                    } else
                        talloc_free(subpath);
                }
            }
        }
        closedir(d);
    }

    free(tmp_sub_id);

    free(f_fname_noext);
    free(f_fname_trim);

    free(tmp_fname_noext);
    free(tmp_fname_trim);
    free(tmp_fname_ext);

    free(tmpresult);
}

char **find_text_subtitles(struct MPOpts *opts, const char *fname)
{
    char **subnames = NULL;
    struct subfn *slist = talloc_array_ptrtype(NULL, slist, 1);
    int n = 0;

    // Load subtitles from current media directory
    append_dir_subtitles(&slist, &n, mp_dirname(fname), fname, 0);

    // Load subtitles in dirs specified by sub-paths option
    if (opts->sub_paths) {
        for (int i = 0; opts->sub_paths[i]; i++) {
            char *path = mp_path_join(slist, mp_dirname(fname),
                                      BSTR(opts->sub_paths[i]));
            append_dir_subtitles(&slist, &n, BSTR(path), fname, 0);
        }
    }

    // Load subtitles in ~/.mplayer/sub limiting sub fuzziness
    char *mp_subdir = get_path("sub/");
    if (mp_subdir)
        append_dir_subtitles(&slist, &n, BSTR(mp_subdir), fname, 1);
    free(mp_subdir);

    // 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);

    talloc_free(slist);
    return subnames;
}

char **find_vob_subtitles(struct MPOpts *opts, 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 vobsubs in directories specified by sub-paths option
    if (opts->sub_paths) {
        for (int i = 0; opts->sub_paths[i]; i++) {
            char *path = mp_path_join(NULL, mp_dirname(fname),
                                      BSTR(opts->sub_paths[i]));
            MP_GROW_ARRAY(vobs, n);
            vobs[n++] = mp_path_join(vobs, BSTR(path), bname);
            talloc_free(path);
        }
    }

    // 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;
}