[PATCH 2/3] Make load of n subtitles directories possible
Clément Bœsch
ubitux at gmail.com
Sat Nov 20 21:07:43 CET 2010
---
mencoder.c | 18 +------
mplayer.c | 16 +-----
sub/subreader.c | 166 ++++++++++++++++++++++++++++++++++++++----------------
sub/subreader.h | 2 +-
4 files changed, 120 insertions(+), 82 deletions(-)
diff --git a/mencoder.c b/mencoder.c
index 57afbc2..6efe907 100644
--- a/mencoder.c
+++ b/mencoder.c
@@ -1019,23 +1019,7 @@ default: {
// after reading video params we should load subtitles because
// we know fps so now we can adjust subtitles time to ~6 seconds AST
// check .sub
- if(sub_name && sub_name[0]){
- for (i = 0; sub_name[i] != NULL; ++i)
- add_subtitles (sub_name[i], sh_video->fps, 0);
- } else
- if(sub_auto && filename) { // auto load sub file ...
- char **tmp = NULL;
- int i = 0;
- char *psub = get_path( "sub/" );
- tmp = sub_filenames((psub ? psub : ""), filename);
- free(psub);
- while (tmp[i])
- {
- add_subtitles (tmp[i], sh_video->fps, 0);
- free(tmp[i++]);
- }
- free(tmp);
- }
+ load_subtitles(filename, sh_video->fps, add_subtitles);
mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
init_best_video_codec(sh_video,video_codec_list,video_fm_list);
diff --git a/mplayer.c b/mplayer.c
index 517448d..3682b29 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -3558,21 +3558,7 @@ if(1 || mpctx->sh_video) {
// check .sub
double fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
current_module="read_subtitles_file";
- if(sub_name){
- for (i = 0; sub_name[i] != NULL; ++i)
- add_subtitles (sub_name[i], fps, 0);
- }
- if(sub_auto) { // auto load sub file ...
- char *psub = get_path( "sub/" );
- char **tmp = sub_filenames((psub ? psub : ""), filename);
- int i = 0;
- free(psub); // release the buffer created by get_path() above
- while (tmp[i]) {
- add_subtitles (tmp[i], fps, 1);
- free(tmp[i++]);
- }
- free(tmp);
- }
+ load_subtitles(filename, fps, add_subtitles);
if (mpctx->set_of_sub_size > 0)
mpctx->sub_counts[SUB_SOURCE_SUBS] = mpctx->set_of_sub_size;
}
diff --git a/sub/subreader.c b/sub/subreader.c
index 42d523b..d076afc 100644
--- a/sub/subreader.c
+++ b/sub/subreader.c
@@ -33,6 +33,7 @@
#include "config.h"
#include "mp_msg.h"
#include "mpcommon.h"
+#include "path.h"
#include "subreader.h"
#include "subassconvert.h"
#include "sub.h"
@@ -1876,8 +1877,15 @@ typedef struct subfn
{
int priority;
char *fname;
+ int noerr;
} subfn;
+struct sub_list {
+ struct subfn *subs;
+ size_t size;
+ int sid;
+};
+
static int compare_sub_priority(const void *a, const void *b)
{
if (((const subfn*)a)->priority > ((const subfn*)b)->priority) {
@@ -1889,17 +1897,35 @@ static int compare_sub_priority(const void *a, const void *b)
}
}
-char** sub_filenames(const char* path, char *fname)
+static void append_sub(struct sub_list *dst, struct subfn *src)
+{
+ if (dst->sid >= (int)dst->size - 1) {
+ dst->size += 32;
+ dst->subs = realloc(dst->subs, sizeof(*dst->subs) * dst->size);
+ }
+ memcpy(&dst->subs[dst->sid], src, sizeof(*src));
+ dst->sid++;
+}
+
+static void merge_subs(struct sub_list *dst, struct sub_list *src)
+{
+ if (src->sid == 0)
+ return;
+ dst->size = dst->sid + src->sid;
+ dst->subs = realloc(dst->subs, sizeof(*dst->subs) * dst->size);
+ memcpy(&dst->subs[dst->sid], src->subs, src->sid * sizeof(*src->subs));
+ dst->sid += src->sid;
+}
+
+static struct sub_list get_sub_list(const char* path, char *fname)
{
- char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
+ 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, pos, found, i, j;
+ int len, found, i;
char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
- subfn *result;
- char **result2;
- int subcnt;
+ struct sub_list subs = {0};
FILE *f;
@@ -1909,8 +1935,7 @@ char** sub_filenames(const char* path, char *fname)
len = (strlen(fname) > 256 ? strlen(fname) : 256)
+(strlen(path) > 256 ? strlen(path) : 256)+2;
- f_dir = malloc(len);
- f_fname = malloc(len);
+ f_fname = strdup(mp_basename(fname));
f_fname_noext = malloc(len);
f_fname_trim = malloc(len);
@@ -1920,27 +1945,6 @@ char** sub_filenames(const char* path, char *fname)
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);
@@ -1954,9 +1958,9 @@ char** sub_filenames(const char* path, char *fname)
// 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);
+ d = opendir(path);
if (d) {
+ mp_msg(MSGT_SUBREADER, MSGL_INFO, "Load subtitles in %s\n", path);
while ((de = readdir(d))) {
// retrieve various parts of the filename
strcpy_strip_ext(tmp_fname_noext, de->d_name);
@@ -2012,8 +2016,7 @@ char** sub_filenames(const char* path, char *fname)
}
if (!prio) {
// doesn't contain the movie name
- // don't try in the mplayer subtitle directory
- if ((j == 0) && (sub_match_fuzziness >= 2)) {
+ if (sub_match_fuzziness >= 2) {
prio = 1;
}
}
@@ -2025,27 +2028,29 @@ char** sub_filenames(const char* path, char *fname)
prio++;
}
#endif
- sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
+ sprintf(tmpresult, "%s%s", 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++;
+ struct subfn sub = {
+ .fname = strdup(tmpresult),
+ .priority = prio,
+ .noerr = 1
+ };
+
+ fclose(f);
+ append_sub(&subs, &sub);
}
}
}
- if (subcnt >= MAX_SUBTITLE_FILES) break;
+ if (subs.sid >= MAX_SUBTITLE_FILES)
+ break;
}
closedir(d);
}
- }
-
free(tmp_sub_id);
- free(f_dir);
free(f_fname);
free(f_fname_noext);
free(f_fname_trim);
@@ -2056,18 +2061,81 @@ char** sub_filenames(const char* path, char *fname)
free(tmpresult);
- qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
+ return subs;
+}
+
+static void track_sub_directory(char *dir, char *fname, struct sub_list *fslist)
+{
+ struct sub_list tmp_list;
+ char *path;
+ size_t plen;
+
+ // Build path
+ path = mp_dirname(fname);
+ plen = strlen(path);
+ path = realloc(path, plen + strlen(dir) + 1);
+ strcpy(path + plen, dir);
+
+ // Grab and merge subtitles filenames
+ tmp_list = get_sub_list(path, fname);
+ merge_subs(fslist, &tmp_list);
+
+ free(tmp_list.subs);
+ free(path);
+}
+
+static struct sub_list get_full_sub_list(char *fname)
+{
+ struct sub_list fslist = {0};
+ char *mp_subdir;
+
+ // Load subtitles specified by sub option with highest priority
+ if (sub_name) {
+ char **tmp;
+ for (tmp = sub_name; *tmp; tmp++) {
+ struct subfn sub = {
+ .fname = strdup(*tmp),
+ .priority = INT_MAX,
+ .noerr = 0
+ };
+ append_sub(&fslist, &sub);
+ }
+ }
+
+ // Stop here if automatic detection disabled
+ if (!sub_auto || !fname)
+ return fslist;
+
+ // Load subtitles from current media directory
+ track_sub_directory("", fname, &fslist);
- result2 = calloc(subcnt + 1, sizeof(*result2));
- for (i = 0; i < subcnt; i++) {
- result2[i] = result[i].fname;
+ // Load subtitles in ~/.mplayer/sub
+ mp_subdir = get_path("sub/");
+ if (mp_subdir) {
+ struct sub_list tmp_list = get_sub_list(mp_subdir, fname);
+ merge_subs(&fslist, &tmp_list);
+ free(tmp_list.subs);
}
- result2[subcnt] = NULL;
+ free(mp_subdir);
- free(result);
+ return fslist;
+}
- return result2;
+void load_subtitles(char *fname, int fps, void add_f(char *, float, int))
+{
+ int i;
+ struct sub_list fslist = get_full_sub_list(fname);
+
+ if (fslist.sid == 0)
+ return;
+ qsort(fslist.subs, fslist.sid, sizeof(*fslist.subs), compare_sub_priority);
+ for (i = 0; i < fslist.sid; i++) {
+ struct subfn *sub = &fslist.subs[i];
+ add_f(sub->fname, fps, sub->noerr);
+ free(sub->fname);
+ }
+ free(fslist.subs);
}
void list_sub_file(sub_data* subd){
diff --git a/sub/subreader.h b/sub/subreader.h
index 323b576..fb67760 100644
--- a/sub/subreader.h
+++ b/sub/subreader.h
@@ -94,7 +94,7 @@ 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 load_subtitles(char *fname, int fps, void add_f(char *, float, int));
void list_sub_file(sub_data* subd);
void dump_srt(sub_data* subd, float fps);
void dump_mpsub(sub_data* subd, float fps);
--
1.7.3.2
--JBi0ZxuS5uaEhkUZ
Content-Type: text/plain; charset=utf-8
Content-Disposition: attachment; filename="0003-Add-subdirs-option.patch"
More information about the MPlayer-dev-eng
mailing list