subtitles.c 12.3 KB
Newer Older
1
/*****************************************************************************
2
 * subtitles.c : subtitles detection
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2003-2009 VLC authors and VideoLAN
5
 * $Id$
6 7 8 9
 *
 * Authors: Derk-Jan Hartman <hartman at videolan.org>
 * This is adapted code from the GPL'ed MPlayer (http://mplayerhq.hu)
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24
 *****************************************************************************/

25 26
/**
 *  \file
27
 *  This file contains functions to detect subtitle files.
28 29
 */

30 31 32 33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <ctype.h> /* isalnum() */
35
#include <unistd.h>
36
#include <sys/stat.h>
37

38 39 40
#include <vlc_common.h>
#include <vlc_fs.h>
#include <vlc_url.h>
41

42
#include "input_internal.h"
43

44
/**
45
 * The possible extensions for subtitle files we support
46
 */
47
static const char *const sub_exts[] = { SLAVE_SPU_EXTENSIONS, "" };
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
48

49
static void strcpy_trim( char *d, const char *s )
50
{
51 52
    unsigned char c;

53
    /* skip leading whitespace */
54
    while( ((c = *s) != '\0') && !isalnum(c) )
55
    {
56
        s++;
57 58 59
    }
    for(;;)
    {
60
        /* copy word */
61
        while( ((c = *s) != '\0') && isalnum(c) )
62
        {
63
            *d = tolower(c);
64 65
            s++; d++;
        }
66
        if( *s == 0 ) break;
67
        /* trim excess whitespace */
68
        while( ((c = *s) != '\0') && !isalnum(c) )
69
        {
70 71 72 73
            s++;
        }
        if( *s == 0 ) break;
        *d++ = ' ';
74 75 76
    }
    *d = 0;
}
77

78
static void strcpy_strip_ext( char *d, const char *s )
79
{
80 81
    unsigned char c;

82
    const char *tmp = strrchr(s, '.');
83 84
    if( !tmp )
    {
85 86
        strcpy(d, s);
        return;
87 88
    }
    else
89
        strlcpy(d, s, tmp - s + 1 );
90
    while( (c = *d) != '\0' )
91
    {
92
        *d = tolower(c);
93
        d++;
94 95
    }
}
96

97
static void strcpy_get_ext( char *d, const char *s )
98
{
99
    const char *tmp = strrchr(s, '.');
100
    if( !tmp )
101
        strcpy(d, "");
102 103
    else
        strcpy( d, tmp + 1 );
104 105
}

106
static int whiteonly( const char *s )
107
{
108 109 110
    unsigned char c;

    while( (c = *s) != '\0' )
111
    {
112
        if( isalnum( c ) )
113
            return 0;
114
        s++;
115 116
    }
    return 1;
117 118
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
static int slave_strcmp( const void *a, const void *b )
{
    const input_item_slave_t *p_slave0 = *((const input_item_slave_t **) a);
    const input_item_slave_t *p_slave1 = *((const input_item_slave_t **) b);

    if( p_slave0 == NULL || p_slave1 == NULL )
        return 0;

    /* We can compare these uris since they come from the file system */
#ifdef HAVE_STRCOLL
    return strcoll( p_slave0->psz_uri, p_slave1->psz_uri );
#else
    return strcmp( p_slave0->psz_uri, p_slave1->psz_uri );
#endif
}

135 136 137
/*
 * Check if a file ends with a subtitle extension
 */
138
int subtitles_Filter( const char *psz_dir_content )
139
{
140
    const char *tmp = strrchr( psz_dir_content, '.');
141 142

    if( !tmp )
143
        return 0;
144
    tmp++;
145

146
    for( int i = 0; sub_exts[i][0]; i++ )
147 148
        if( strcasecmp( sub_exts[i], tmp ) == 0 )
            return 1;
149
    return 0;
150 151 152
}


153
/**
154
 * Convert a list of paths separated by ',' to a char**
155
 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
156
static char **paths_to_list( const char *psz_dir, char *psz_path )
157
{
158
    unsigned int i, k, i_nb_subdirs;
159
    char **subdirs; /* list of subdirectories to look in */
160
    char *psz_parser = psz_path;
161

162 163
    if( !psz_dir || !psz_path )
        return NULL;
164

165
    for( k = 0, i_nb_subdirs = 1; psz_path[k] != '\0'; k++ )
166
    {
167
        if( psz_path[k] == ',' )
168 169
            i_nb_subdirs++;
    }
170

171 172 173
    subdirs = calloc( i_nb_subdirs + 1, sizeof(char*) );
    if( !subdirs )
        return NULL;
174

175
    for( i = 0; psz_parser && *psz_parser != '\0' ; )
176 177 178 179
    {
        char *psz_subdir = psz_parser;
        psz_parser = strchr( psz_subdir, ',' );
        if( psz_parser )
180
        {
181 182
            *psz_parser++ = '\0';
            while( *psz_parser == ' ' )
183 184
                psz_parser++;
        }
185 186 187
        if( *psz_subdir == '\0' )
            continue;

188
        if( asprintf( &subdirs[i++], "%s%s",
189
                  psz_subdir[0] == '.' ? psz_dir : "",
190
                  psz_subdir ) == -1 )
191
            break;
192
    }
193 194
    subdirs[i] = NULL;

195 196 197 198 199 200
    return subdirs;
}

/**
 * Detect subtitle files.
 *
201
 * When called this function will split up the psz_name string into a
202 203 204 205 206 207 208
 * directory, filename and extension. It then opens the directory
 * in which the file resides and tries to find possible matches of
 * subtitles files.
 *
 * \ingroup Demux
 * \param p_this the calling \ref input_thread_t
 * \param psz_path a list of subdirectories (separated by a ',') to look in.
209 210 211 212
 * \param psz_name_org the complete filename to base the search on.
 * \param pp_slaves an initialized input item slave list to append detected subtitles to
 * \param p_slaves pointer to the size of the slave list
 * \return VLC_SUCCESS if ok
213
 */
214 215
int subtitles_Detect( input_thread_t *p_this, char *psz_path, const char *psz_name_org,
                      input_item_slave_t ***ppp_slaves, int *p_slaves )
216
{
217 218
    int i_fuzzy = var_GetInteger( p_this, "sub-autodetect-fuzzy" );
    if ( i_fuzzy == 0 )
219
        return VLC_EGENERIC;
220
    int i_fname_len;
221 222
    input_item_slave_t **pp_slaves = *ppp_slaves;
    int i_slaves = *p_slaves;
223
    char *f_fname_noext = NULL, *f_fname_trim = NULL;
224
    char **subdirs; /* list of subdirectories to look in */
225

226
    if( !psz_name_org )
227
        return VLC_EGENERIC;
228

229
    char *psz_fname = vlc_uri2path( psz_name_org );
230
    if( !psz_fname )
231
        return VLC_EGENERIC;
232

233
    /* extract filename & dirname from psz_fname */
234
    char *f_dir = strdup( psz_fname );
235
    if( f_dir == 0 )
236
    {
237
        free( psz_fname );
238
        return VLC_ENOMEM;
239
    }
240

241
    const char *f_fname = strrchr( psz_fname, DIR_SEP_CHAR );
242
    if( !f_fname )
243
    {
244
        free( f_dir );
245
        free( psz_fname );
246
        return VLC_EGENERIC;
247
    }
248 249
    f_fname++; /* Skip the '/' */
    f_dir[f_fname - psz_fname] = 0; /* keep dir separator in f_dir */
250

251
    i_fname_len = strlen( f_fname );
252

253 254
    f_fname_noext = malloc(i_fname_len + 1);
    f_fname_trim = malloc(i_fname_len + 1 );
255 256
    if( !f_fname_noext || !f_fname_trim )
    {
257 258 259
        free( f_dir );
        free( f_fname_noext );
        free( f_fname_trim );
260
        free( psz_fname );
261
        return VLC_ENOMEM;
262
    }
263

264 265
    strcpy_strip_ext( f_fname_noext, f_fname );
    strcpy_trim( f_fname_trim, f_fname_noext );
266

267
    subdirs = paths_to_list( f_dir, psz_path );
268
    for( int j = -1; (j == -1) || ( j >= 0 && subdirs != NULL && subdirs[j] != NULL ); j++ )
269
    {
270
        const char *psz_dir = (j < 0) ? f_dir : subdirs[j];
271
        if( psz_dir == NULL || ( j >= 0 && !strcmp( psz_dir, f_dir ) ) )
272 273
            continue;

274
        /* parse psz_src dir */
275 276
        DIR *dir = vlc_opendir( psz_dir );
        if( dir == NULL )
277
            continue;
278

279
        msg_Dbg( p_this, "looking for a subtitle file in %s", psz_dir );
280

281
        const char *psz_name;
282
        while( (psz_name = vlc_readdir( dir )) )
283
        {
284 285 286
            if( psz_name[0] == '.' || !subtitles_Filter( psz_name ) )
                continue;

287 288 289
            char tmp_fname_noext[strlen( psz_name ) + 1];
            char tmp_fname_trim[strlen( psz_name ) + 1];
            char tmp_fname_ext[strlen( psz_name ) + 1];
290
            const char *tmp;
291
            int i_prio = 0;
292

293 294 295 296
            /* retrieve various parts of the filename */
            strcpy_strip_ext( tmp_fname_noext, psz_name );
            strcpy_get_ext( tmp_fname_ext, psz_name );
            strcpy_trim( tmp_fname_trim, tmp_fname_noext );
297

298
            if( !strcmp( tmp_fname_trim, f_fname_trim ) )
299 300
            {
                /* matches the movie name exactly */
301
                i_prio = SLAVE_PRIORITY_MATCH_ALL;
302
            }
303
            else if( (tmp = strstr( tmp_fname_trim, f_fname_trim )) )
304 305 306 307
            {
                /* contains the movie name */
                tmp += strlen( f_fname_trim );
                if( whiteonly( tmp ) )
308
                {
309
                    /* chars in front of the movie name */
310
                    i_prio = SLAVE_PRIORITY_MATCH_RIGHT;
311
                }
312
                else
313
                {
314 315
                    /* chars after (and possibly in front of)
                     * the movie name */
316
                    i_prio = SLAVE_PRIORITY_MATCH_LEFT;
317
                }
318
            }
319
            else if( j == -1 )
320 321
            {
                /* doesn't contain the movie name, prefer files in f_dir over subdirs */
322
                i_prio = SLAVE_PRIORITY_MATCH_NONE;
323
            }
324
            if( i_prio >= i_fuzzy )
325 326
            {
                struct stat st;
327
                char *path;
328

329 330
                size_t i_len = strlen( psz_dir );
                const char *psz_format;
331 332
                if ( i_len == 0 )
                    continue;
333 334 335 336 337 338
                if( psz_dir[i_len - 1] == DIR_SEP_CHAR )
                    psz_format = "%s%s";
                else
                    psz_format = "%s"DIR_SEP"%s";

                if( asprintf( &path, psz_format, psz_dir, psz_name ) < 0 )
339
                    continue;
Laurent Aimar's avatar
Laurent Aimar committed
340

341 342
                if( strcmp( path, psz_fname )
                 && vlc_stat( path, &st ) == 0
343
                 && S_ISREG( st.st_mode ) )
344
                {
345
                    msg_Dbg( p_this,
346
                            "autodetected subtitle: %s with priority %d",
347
                            path, i_prio );
348 349 350 351 352
                    char *psz_uri = vlc_path2uri( path, NULL );
                    input_item_slave_t *p_sub = psz_uri != NULL ?
                        input_item_slave_New( psz_uri, SLAVE_TYPE_SPU, i_prio )
                        : NULL;
                    if( p_sub )
353 354
                    {
                        p_sub->b_forced = true;
355
                        TAB_APPEND(i_slaves, pp_slaves, p_sub);
356
                    }
357
                    free( psz_uri );
358
                }
359
                free( path );
360
            }
361
        }
362
        closedir( dir );
363
    }
364 365
    if( subdirs )
    {
366
        for( size_t j = 0; subdirs[j] != NULL; j++ )
367
            free( subdirs[j] );
368 369 370 371 372
        free( subdirs );
    }
    free( f_dir );
    free( f_fname_trim );
    free( f_fname_noext );
373
    free( psz_fname );
374

375
    for( int i = 0; i < i_slaves; i++ )
376
    {
377
        input_item_slave_t *p_sub = pp_slaves[i];
378

379 380 381 382 383
        bool b_reject = false;
        char *psz_ext = strrchr( p_sub->psz_uri, '.' );
        if( !psz_ext )
            continue;
        psz_ext++;
384

385
        if( !strcasecmp( psz_ext, "sub" ) )
386
        {
387
            for( int j = 0; j < i_slaves; j++ )
388
            {
389 390
                input_item_slave_t *p_sub_inner = pp_slaves[j];

391 392 393 394
                /* A slave can be null if it's already rejected */
                if( p_sub_inner == NULL )
                    continue;

395 396 397 398 399 400 401 402 403 404 405 406 407 408
                /* check that the filenames without extension match */
                if( strncasecmp( p_sub->psz_uri, p_sub_inner->psz_uri,
                    strlen( p_sub->psz_uri ) - 3 ) )
                    continue;

                char *psz_ext_inner = strrchr( p_sub_inner->psz_uri, '.' );
                if( !psz_ext_inner )
                    continue;
                psz_ext_inner++;

                /* check that we have an idx file */
                if( !strcasecmp( psz_ext_inner, "idx" ) )
                {
                    b_reject = true;
409
                    break;
410
                }
411
            }
Laurent Aimar's avatar
Laurent Aimar committed
412
        }
413
        else if( !strcasecmp( psz_ext, "cdg" ) )
414
        {
415
            if( p_sub->i_priority < SLAVE_PRIORITY_MATCH_ALL )
416
                b_reject = true;
417
        }
418 419 420 421 422
        if( b_reject )
        {
            pp_slaves[i] = NULL;
            input_item_slave_Delete( p_sub );
        }
423 424
    }

425 426 427 428
    /* Sort alphabetically */
    if( i_slaves > 0 )
        qsort( pp_slaves, i_slaves, sizeof (input_item_slave_t*), slave_strcmp );

429 430 431
    *ppp_slaves = pp_slaves; /* in case of realloc */
    *p_slaves = i_slaves;
    return VLC_SUCCESS;
432
}