diff options
Diffstat (limited to 'sub')
-rw-r--r-- | sub/ass_mp.c | 46 | ||||
-rw-r--r-- | sub/find_subfiles.c | 275 | ||||
-rw-r--r-- | sub/find_subfiles.h | 29 | ||||
-rw-r--r-- | sub/sub.c | 2 | ||||
-rw-r--r-- | sub/subreader.c | 280 | ||||
-rw-r--r-- | sub/subreader.h | 3 | ||||
-rw-r--r-- | sub/vobsub.c | 10 | ||||
-rw-r--r-- | sub/vobsub.h | 2 |
8 files changed, 324 insertions, 323 deletions
diff --git a/sub/ass_mp.c b/sub/ass_mp.c index 98602ace03..78f607bc7a 100644 --- a/sub/ass_mp.c +++ b/sub/ass_mp.c @@ -233,50 +233,28 @@ ASS_Track *mp_ass_read_subdata(ASS_Library *library, sub_data *subdata, ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname, char *charset) { - int i; - char *buf = NULL; ASS_Track *track; - size_t sz = 0; - size_t buf_alloc = 0; - stream_t *fd; - fd = open_stream(fname, NULL, NULL); - if (!fd) + struct stream *s = open_stream(fname, NULL, NULL); + if (!s) // Stream code should have printed an error already return NULL; - if (fd->end_pos > STREAM_BUFFER_SIZE) - /* read entire file if size is known */ - buf_alloc = fd->end_pos; - else - buf_alloc = 1000; - for (;;) { - if (sz > 100000000) { - mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file " - "larger than 100 MB: %s\n", fname); - sz = 0; - break; - } - buf_alloc = FFMAX(buf_alloc, sz + (sz >> 1)); - buf_alloc = FFMIN(buf_alloc, 100000001); - buf = realloc(buf, buf_alloc + 1); - i = stream_read(fd, buf + sz, buf_alloc - sz); - if (i <= 0) - break; - sz += i; - } - free_stream(fd); - if (!sz) { - free(buf); + struct bstr content = stream_read_complete(s, NULL, 100000000, 1); + if (content.start == NULL) + mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file " + "larger than 100 MB: %s\n", fname); + free_stream(s); + if (content.len == 0) { + talloc_free(content.start); return NULL; } - buf[sz] = 0; - buf = realloc(buf, sz + 1); - track = ass_read_memory(library, buf, sz, charset); + content.start[content.len] = 0; + track = ass_read_memory(library, content.start, content.len, charset); if (track) { free(track->name); track->name = strdup(fname); } - free(buf); + talloc_free(content.start); return track; } diff --git a/sub/find_subfiles.c b/sub/find_subfiles.c new file mode 100644 index 0000000000..1e4b33d0cc --- /dev/null +++ b/sub/find_subfiles.c @@ -0,0 +1,275 @@ +#include <dirent.h> +#include <string.h> +#include <stdlib.h> +#include <ctype.h> +#include <assert.h> + +#include "mp_msg.h" +#include "options.h" +#include "path.h" +#include "mpcommon.h" +#include "sub/find_subfiles.h" +#include "sub/sub.h" + +static struct bstr strip_ext(struct bstr str) +{ + int dotpos = bstrrchr(str, '.'); + if (dotpos < 0) + return str; + return (struct bstr){str.start, dotpos}; +} + +static struct bstr get_ext(struct bstr s) +{ + int dotpos = bstrrchr(s, '.'); + if (dotpos < 0) + return (struct bstr){NULL, 0}; + return bstr_splice(s, dotpos + 1, s.len); +} + +struct subfn { + int priority; + char *fname; +}; + +static int compare_sub_priority(const void *a, const void *b) +{ + const struct subfn *s1 = a; + const struct subfn *s2 = b; + if (s1->priority > s2->priority) + return -1; + if (s1->priority < s2->priority) + return 1; + return strcoll(s1->fname, s2->fname); +} + +static struct bstr guess_lang_from_filename(struct bstr name) +{ + if (name.len < 2) + return (struct bstr){NULL, 0}; + + int n = 0; + int i = name.len - 1; + + if (name.start[i] == ')' || name.start[i] == ']') + i--; + while (i >= 0 && isalpha(name.start[i])) { + n++; + if (n > 3) + return (struct bstr){NULL, 0}; + i--; + } + if (n < 2) + return (struct bstr){NULL, 0}; + return (struct bstr){name.start + i + 1, n}; +} + +struct sub_list { + struct subfn subs[MAX_SUBTITLE_FILES]; + int sid; + void *ctx; +}; + +/** + * @brief Append all the subtitles in the given path matching fname + * @param opts MPlayer options + * @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 MPOpts *opts, + struct subfn **slist, int *nsub, + struct bstr path, const char *fname, + int limit_fuzziness) +{ + char *sub_exts[] = {"utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL}; + void *tmpmem = talloc_new(NULL); + FILE *f; + assert(strlen(fname) < 1e6); + + struct bstr f_fname = BSTR(mp_basename(fname)); + struct bstr f_fname_noext = bstrdup(tmpmem, strip_ext(f_fname)); + bstr_lower(f_fname_noext); + struct bstr f_fname_trim = bstr_strip(f_fname_noext); + + // 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(tmpmem, path); + DIR *d = opendir(path0); + if (!d) + goto out; + mp_msg(MSGT_SUBREADER, MSGL_INFO, "Load subtitles in %.*s\n", BSTR_P(path)); + struct dirent *de; + while ((de = readdir(d))) { + struct bstr dename = BSTR(de->d_name); + void *tmpmem2 = talloc_new(tmpmem); + + // retrieve various parts of the filename + struct bstr tmp_fname_noext = bstrdup(tmpmem2, strip_ext(dename)); + bstr_lower(tmp_fname_noext); + struct bstr tmp_fname_ext = get_ext(dename); + struct bstr tmp_fname_trim = bstr_strip(tmp_fname_noext); + + // If it's a .sub, check if there is a .idx with the same name. If + // there is one, it's certainly a vobsub so we skip it. + if (bstrcasecmp(tmp_fname_ext, BSTR("sub")) == 0) { + char *idxname = talloc_asprintf(tmpmem2, "%.*s.idx", + (int)tmp_fname_noext.len, + de->d_name); + char *idx = mp_path_join(tmpmem2, path, BSTR(idxname)); + f = fopen(idx, "rt"); + if (f) { + fclose(f); + goto next_sub; + } + } + + // does it end with a subtitle extension? +#ifdef CONFIG_ICONV +#ifdef CONFIG_ENCA + int i = (sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0; +#else + int i = sub_cp ? 3 : 0; +#endif +#else + int i = 0; +#endif + while (1) { + if (!sub_exts[i]) + goto next_sub; + if (bstrcasecmp(BSTR(sub_exts[i]), tmp_fname_ext) == 0) + break; + i++; + } + + // we have a (likely) subtitle file + int prio = 0; + 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, + BSTR(opts->sub_lang[n]))) { + prio = 4; // matches the movie name + lang extension + break; + } + } + } + } + } + if (!prio && bstrcmp(tmp_fname_trim, f_fname_trim) == 0) + prio = 3; // matches the movie name + if (!prio && bstr_find(tmp_fname_trim, f_fname_trim) >= 0 + && sub_match_fuzziness >= 1) + prio = 2; // contains the movie name + 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, dename); + 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); + } + + next_sub: + talloc_free(tmpmem2); + } + closedir(d); + + out: + talloc_free(tmpmem); +} + +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(opts, &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(opts, &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(opts, &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; +} diff --git a/sub/find_subfiles.h b/sub/find_subfiles.h new file mode 100644 index 0000000000..c93164c6f8 --- /dev/null +++ b/sub/find_subfiles.h @@ -0,0 +1,29 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPLAYER_FIND_SUBFILES_H +#define MPLAYER_FIND_SUBFILES_H + +#define MAX_SUBTITLE_FILES 128 + +struct MPOpts; + +char **find_text_subtitles(struct MPOpts *opts, const char *fname); +char **find_vob_subtitles(struct MPOpts *opts, const char *fname); + +#endif /* MPLAYER_FINDFILES_H */ @@ -718,7 +718,7 @@ inline static void vo_update_text_sub(struct osd_state *osd, mp_osd_obj_t* obj,i // reading the subtitle words from vo_sub->text[] while (*t) { if (sub_utf8) - c = utf8_get_char(&t); + c = utf8_get_char((const char **)&t); else if ((c = *t++) >= 0x80 && sub_unicode) c = (c<<8) + *t++; if (k==MAX_UCS){ diff --git a/sub/subreader.c b/sub/subreader.c index 1f9294ec47..6acecb5c54 100644 --- a/sub/subreader.c +++ b/sub/subreader.c @@ -24,8 +24,6 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <ctype.h> - #include <sys/types.h> #include <dirent.h> @@ -1856,284 +1854,6 @@ if ((suboverlap_enabled == 2) || return subt_data; } -#if 0 -char * strreplace( char * in,char * what,char * whereof ) -{ - int i; - char * tmp; - - if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL; - for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i]; - if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0; - return in; -} -#endif - - -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); - } -} - -char** sub_filenames(const char* path, char *fname) -{ - char *f_dir, *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, pos, found, i, j; - char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL}; - subfn *result; - char **result2; - - int subcnt; - - FILE *f; - - DIR *d; - struct dirent *de; - - len = (strlen(fname) > 256 ? strlen(fname) : 256) - +(strlen(path) > 256 ? strlen(path) : 256)+2; - - f_dir = malloc(len); - f_fname = malloc(len); - 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); - - result = calloc(MAX_SUBTITLE_FILES, sizeof(*result)); - - subcnt = 0; - - tmp = strrchr(fname,'/'); -#if HAVE_DOS_PATHS - if(!tmp)tmp = strrchr(fname,'\\'); - if(!tmp)tmp = strrchr(fname,':'); -#endif - - // extract filename & dirname from fname - if (tmp) { - strcpy(f_fname, tmp+1); - pos = tmp - fname; - strncpy(f_dir, fname, pos+1); - f_dir[pos+1] = 0; - } else { - strcpy(f_fname, fname); - strcpy(f_dir, "./"); - } - - 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 - for (j = 0; j <= 1; j++) { - d = opendir(j == 0 ? f_dir : path); - if (d) { - 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 ((j == 0) && (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 - sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name); -// fprintf(stderr, "%s priority %d\n", tmpresult, prio); - if ((f = fopen(tmpresult, "rt"))) { - fclose(f); - result[subcnt].priority = prio; - result[subcnt].fname = strdup(tmpresult); - subcnt++; - } - } - - } - if (subcnt >= MAX_SUBTITLE_FILES) break; - } - closedir(d); - } - - } - - free(tmp_sub_id); - - free(f_dir); - free(f_fname); - free(f_fname_noext); - free(f_fname_trim); - - free(tmp_fname_noext); - free(tmp_fname_trim); - free(tmp_fname_ext); - - free(tmpresult); - - qsort(result, subcnt, sizeof(subfn), compare_sub_priority); - - result2 = calloc(subcnt + 1, sizeof(*result2)); - - for (i = 0; i < subcnt; i++) { - result2[i] = result[i].fname; - } - result2[subcnt] = NULL; - - free(result); - - return result2; -} - void list_sub_file(sub_data* subd){ int i,j; subtitle *subs = subd->subtitles; diff --git a/sub/subreader.h b/sub/subreader.h index 9c7465d71a..079238a6c3 100644 --- a/sub/subreader.h +++ b/sub/subreader.h @@ -48,8 +48,6 @@ extern int sub_match_fuzziness; // One of the SUB_* constant above extern int sub_format; -#define MAX_SUBTITLE_FILES 128 - #define SUB_MAX_TEXT 12 #define SUB_ALIGNMENT_BOTTOMLEFT 1 #define SUB_ALIGNMENT_BOTTOMCENTER 2 @@ -97,7 +95,6 @@ void subcp_close (void); /* for demux_ogg.c */ const char* guess_buffer_cp(unsigned char* buffer, int buflen, const char *preferred_language, const char *fallback); const char* guess_cp(struct stream *st, const char *preferred_language, const char *fallback); #endif -char ** sub_filenames(const char *path, char *fname); void list_sub_file(sub_data* subd); void dump_srt(sub_data* subd, float fps); void dump_mpsub(sub_data* subd, float fps); diff --git a/sub/vobsub.c b/sub/vobsub.c index 08efa3acec..a2fc94fb17 100644 --- a/sub/vobsub.c +++ b/sub/vobsub.c @@ -1109,20 +1109,22 @@ int vobsub_get_index_by_id(void *vobhandle, int id) return j; } -int vobsub_set_from_lang(void *vobhandle, unsigned char * lang) +int vobsub_set_from_lang(void *vobhandle, char **lang) { int i; vobsub_t *vob= vobhandle; - while (lang && strlen(lang) >= 2) { + if (!lang) + goto end; + for (int n = 0; lang[n]; n++) { for (i = 0; i < vob->spu_streams_size; i++) if (vob->spu_streams[i].id) - if ((strncmp(vob->spu_streams[i].id, lang, 2) == 0)) { + if ((strncmp(vob->spu_streams[i].id, lang[n], 2) == 0)) { vobsub_id = i; mp_msg(MSGT_VOBSUB, MSGL_INFO, "Selected VOBSUB language: %d language: %s\n", i, vob->spu_streams[i].id); return 0; } - lang+=2;while (lang[0]==',' || lang[0]==' ') ++lang; } +end: mp_msg(MSGT_VOBSUB, MSGL_WARN, "No matching VOBSUB language found!\n"); return -1; } diff --git a/sub/vobsub.h b/sub/vobsub.h index b076e4b6bc..3cef1347e5 100644 --- a/sub/vobsub.h +++ b/sub/vobsub.h @@ -41,7 +41,7 @@ unsigned int vobsub_rgb_to_yuv(unsigned int rgb); void *vobsub_out_open(const char *basename, const unsigned int *palette, unsigned int orig_width, unsigned int orig_height, const char *id, unsigned int index); void vobsub_out_output(void *me, const unsigned char *packet, int len, double pts); void vobsub_out_close(void *me); -int vobsub_set_from_lang(void *vobhandle, unsigned char * lang); +int vobsub_set_from_lang(void *vobhandle, char **lang); void vobsub_seek(void * vobhandle, float pts); #endif /* MPLAYER_VOBSUB_H */ |