1 /*****************************************************************************
2 * subtitles.c : subtitles detection
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
7 * Authors: Derk-Jan Hartman <hartman at videolan.org>
8 * This is adapted code from the GPL'ed MPlayer (http://mplayerhq.hu)
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
27 * This file contains functions to detect subtitle files.
34 #include <ctype.h> /* isalnum() */
38 #include <vlc_common.h>
42 #include "input_internal.h"
45 * The possible extensions for subtitle files we support
47 static const char *const sub_exts
[] = { SLAVE_SPU_EXTENSIONS
, "" };
49 static void strcpy_trim( char *d
, const char *s
)
53 /* skip leading whitespace */
54 while( ((c
= *s
) != '\0') && !isalnum(c
) )
61 while( ((c
= *s
) != '\0') && isalnum(c
) )
67 /* trim excess whitespace */
68 while( ((c
= *s
) != '\0') && !isalnum(c
) )
78 static void strcpy_strip_ext( char *d
, const char *s
)
82 const char *tmp
= strrchr(s
, '.');
89 strlcpy(d
, s
, tmp
- s
+ 1 );
90 while( (c
= *d
) != '\0' )
97 static void strcpy_get_ext( char *d
, const char *s
)
99 const char *tmp
= strrchr(s
, '.');
103 strcpy( d
, tmp
+ 1 );
106 static int whiteonly( const char *s
)
110 while( (c
= *s
) != '\0' )
119 static int slave_strcmp( const void *a
, const void *b
)
121 const input_item_slave_t
*p_slave0
= *((const input_item_slave_t
**) a
);
122 const input_item_slave_t
*p_slave1
= *((const input_item_slave_t
**) b
);
124 if( p_slave0
== NULL
|| p_slave1
== NULL
)
127 /* We can compare these uris since they come from the file system */
129 return strcoll( p_slave0
->psz_uri
, p_slave1
->psz_uri
);
131 return strcmp( p_slave0
->psz_uri
, p_slave1
->psz_uri
);
136 * Check if a file ends with a subtitle extension
138 int subtitles_Filter( const char *psz_dir_content
)
140 const char *tmp
= strrchr( psz_dir_content
, '.');
146 for( int i
= 0; sub_exts
[i
][0]; i
++ )
147 if( strcasecmp( sub_exts
[i
], tmp
) == 0 )
154 * Convert a list of paths separated by ',' to a char**
156 static char **paths_to_list( const char *psz_dir
, char *psz_path
)
158 unsigned int i
, k
, i_nb_subdirs
;
159 char **subdirs
; /* list of subdirectories to look in */
160 char *psz_parser
= psz_path
;
162 if( !psz_dir
|| !psz_path
)
165 for( k
= 0, i_nb_subdirs
= 1; psz_path
[k
] != '\0'; k
++ )
167 if( psz_path
[k
] == ',' )
171 subdirs
= calloc( i_nb_subdirs
+ 1, sizeof(char*) );
175 for( i
= 0; psz_parser
&& *psz_parser
!= '\0' ; )
177 char *psz_subdir
= psz_parser
;
178 psz_parser
= strchr( psz_subdir
, ',' );
181 *psz_parser
++ = '\0';
182 while( *psz_parser
== ' ' )
185 if( *psz_subdir
== '\0' )
188 if( asprintf( &subdirs
[i
++], "%s%s",
189 psz_subdir
[0] == '.' ? psz_dir
: "",
199 * Detect subtitle files.
201 * When called this function will split up the psz_name string into a
202 * directory, filename and extension. It then opens the directory
203 * in which the file resides and tries to find possible matches of
207 * \param p_this the calling \ref input_thread_t
208 * \param psz_path a list of subdirectories (separated by a ',') to look in.
209 * \param psz_name_org the complete filename to base the search on.
210 * \param pp_slaves an initialized input item slave list to append detected subtitles to
211 * \param p_slaves pointer to the size of the slave list
212 * \return VLC_SUCCESS if ok
214 int subtitles_Detect( input_thread_t
*p_this
, char *psz_path
, const char *psz_name_org
,
215 input_item_slave_t
***ppp_slaves
, int *p_slaves
)
217 int i_fuzzy
= var_GetInteger( p_this
, "sub-autodetect-fuzzy" );
221 input_item_slave_t
**pp_slaves
= *ppp_slaves
;
222 int i_slaves
= *p_slaves
;
223 char *f_fname_noext
= NULL
, *f_fname_trim
= NULL
;
224 char **subdirs
; /* list of subdirectories to look in */
229 char *psz_fname
= vlc_uri2path( psz_name_org
);
233 /* extract filename & dirname from psz_fname */
234 char *f_dir
= strdup( psz_fname
);
241 const char *f_fname
= strrchr( psz_fname
, DIR_SEP_CHAR
);
248 f_fname
++; /* Skip the '/' */
249 f_dir
[f_fname
- psz_fname
] = 0; /* keep dir separator in f_dir */
251 i_fname_len
= strlen( f_fname
);
253 f_fname_noext
= malloc(i_fname_len
+ 1);
254 f_fname_trim
= malloc(i_fname_len
+ 1 );
255 if( !f_fname_noext
|| !f_fname_trim
)
258 free( f_fname_noext
);
259 free( f_fname_trim
);
264 strcpy_strip_ext( f_fname_noext
, f_fname
);
265 strcpy_trim( f_fname_trim
, f_fname_noext
);
267 subdirs
= paths_to_list( f_dir
, psz_path
);
268 for( j
= -1; (j
== -1) || ( j
>= 0 && subdirs
!= NULL
&& subdirs
[j
] != NULL
); j
++ )
270 const char *psz_dir
= (j
< 0) ? f_dir
: subdirs
[j
];
271 if( psz_dir
== NULL
|| ( j
>= 0 && !strcmp( psz_dir
, f_dir
) ) )
274 /* parse psz_src dir */
275 DIR *dir
= vlc_opendir( psz_dir
);
279 msg_Dbg( p_this
, "looking for a subtitle file in %s", psz_dir
);
281 const char *psz_name
;
282 while( (psz_name
= vlc_readdir( dir
)) )
284 if( psz_name
[0] == '.' || !subtitles_Filter( psz_name
) )
287 char tmp_fname_noext
[strlen( psz_name
) + 1];
288 char tmp_fname_trim
[strlen( psz_name
) + 1];
289 char tmp_fname_ext
[strlen( psz_name
) + 1];
293 /* retrieve various parts of the filename */
294 strcpy_strip_ext( tmp_fname_noext
, psz_name
);
295 strcpy_get_ext( tmp_fname_ext
, psz_name
);
296 strcpy_trim( tmp_fname_trim
, tmp_fname_noext
);
298 if( !strcmp( tmp_fname_trim
, f_fname_trim
) )
300 /* matches the movie name exactly */
301 i_prio
= SLAVE_PRIORITY_MATCH_ALL
;
303 else if( (tmp
= strstr( tmp_fname_trim
, f_fname_trim
)) )
305 /* contains the movie name */
306 tmp
+= strlen( f_fname_trim
);
307 if( whiteonly( tmp
) )
309 /* chars in front of the movie name */
310 i_prio
= SLAVE_PRIORITY_MATCH_RIGHT
;
314 /* chars after (and possibly in front of)
316 i_prio
= SLAVE_PRIORITY_MATCH_LEFT
;
321 /* doesn't contain the movie name, prefer files in f_dir over subdirs */
322 i_prio
= SLAVE_PRIORITY_MATCH_NONE
;
324 if( i_prio
>= i_fuzzy
)
329 size_t i_len
= strlen( psz_dir
);
330 const char *psz_format
;
333 if( psz_dir
[i_len
- 1] == DIR_SEP_CHAR
)
336 psz_format
= "%s"DIR_SEP
"%s";
338 if( asprintf( &path
, psz_format
, psz_dir
, psz_name
) < 0 )
341 if( strcmp( path
, psz_fname
)
342 && vlc_stat( path
, &st
) == 0
343 && S_ISREG( st
.st_mode
) )
346 "autodetected subtitle: %s with priority %d",
348 char *psz_uri
= vlc_path2uri( path
, NULL
);
349 input_item_slave_t
*p_sub
= psz_uri
!= NULL
?
350 input_item_slave_New( psz_uri
, SLAVE_TYPE_SPU
, i_prio
)
354 p_sub
->b_forced
= true;
355 TAB_APPEND(i_slaves
, pp_slaves
, p_sub
);
366 for( j
= 0; subdirs
[j
]; j
++ )
371 free( f_fname_trim
);
372 free( f_fname_noext
);
375 for( int i
= 0; i
< i_slaves
; i
++ )
377 input_item_slave_t
*p_sub
= pp_slaves
[i
];
379 bool b_reject
= false;
380 char *psz_ext
= strrchr( p_sub
->psz_uri
, '.' );
385 if( !strcasecmp( psz_ext
, "sub" ) )
387 for( int j
= 0; j
< i_slaves
; j
++ )
389 input_item_slave_t
*p_sub_inner
= pp_slaves
[j
];
391 /* A slave can be null if it's already rejected */
392 if( p_sub_inner
== NULL
)
395 /* check that the filenames without extension match */
396 if( strncasecmp( p_sub
->psz_uri
, p_sub_inner
->psz_uri
,
397 strlen( p_sub
->psz_uri
) - 3 ) )
400 char *psz_ext_inner
= strrchr( p_sub_inner
->psz_uri
, '.' );
405 /* check that we have an idx file */
406 if( !strcasecmp( psz_ext_inner
, "idx" ) )
413 else if( !strcasecmp( psz_ext
, "cdg" ) )
415 if( p_sub
->i_priority
< SLAVE_PRIORITY_MATCH_ALL
)
421 input_item_slave_Delete( p_sub
);
425 /* Sort alphabetically */
427 qsort( pp_slaves
, i_slaves
, sizeof (input_item_slave_t
*), slave_strcmp
);
429 *ppp_slaves
= pp_slaves
; /* in case of realloc */
430 *p_slaves
= i_slaves
;