1 /*****************************************************************************
2 * subtitles.c : subtitles detection
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
6 * Authors: Derk-Jan Hartman <hartman at videolan.org>
7 * This is adapted code from the GPL'ed MPlayer (http://mplayerhq.hu)
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains functions to detect subtitle files.
33 #include <ctype.h> /* isalnum() */
37 #include <vlc_common.h>
41 #include "input_internal.h"
44 * The possible extensions for subtitle files we support
46 static const char *const sub_exts
[] = { SLAVE_SPU_EXTENSIONS
, "" };
48 static void strcpy_trim( char *d
, const char *s
)
52 /* skip leading whitespace */
53 while( ((c
= *s
) != '\0') && !isalnum(c
) )
60 while( ((c
= *s
) != '\0') && isalnum(c
) )
66 /* trim excess whitespace */
67 while( ((c
= *s
) != '\0') && !isalnum(c
) )
77 static void strcpy_strip_ext( char *d
, const char *s
)
81 const char *tmp
= strrchr(s
, '.');
88 strlcpy(d
, s
, tmp
- s
+ 1 );
89 while( (c
= *d
) != '\0' )
96 static void strcpy_get_ext( char *d
, const char *s
)
98 const char *tmp
= strrchr(s
, '.');
102 strcpy( d
, tmp
+ 1 );
105 static int whiteonly( const char *s
)
109 while( (c
= *s
) != '\0' )
118 static int slave_strcmp( const void *a
, const void *b
)
120 const input_item_slave_t
*p_slave0
= *((const input_item_slave_t
**) a
);
121 const input_item_slave_t
*p_slave1
= *((const input_item_slave_t
**) b
);
123 if( p_slave0
== NULL
|| p_slave1
== NULL
)
126 /* We can compare these uris since they come from the file system */
128 return strcoll( p_slave0
->psz_uri
, p_slave1
->psz_uri
);
130 return strcmp( p_slave0
->psz_uri
, p_slave1
->psz_uri
);
135 * Check if a file ends with a subtitle extension
137 int subtitles_Filter( const char *psz_dir_content
)
139 const char *tmp
= strrchr( psz_dir_content
, '.');
145 for( int i
= 0; sub_exts
[i
][0]; i
++ )
146 if( strcasecmp( sub_exts
[i
], tmp
) == 0 )
153 * Convert a list of paths separated by ',' to a char**
155 static char **paths_to_list( const char *psz_dir
, char *psz_path
)
157 unsigned int i
, k
, i_nb_subdirs
;
158 char **subdirs
; /* list of subdirectories to look in */
159 char *psz_parser
= psz_path
;
161 if( !psz_dir
|| !psz_path
)
164 for( k
= 0, i_nb_subdirs
= 1; psz_path
[k
] != '\0'; k
++ )
166 if( psz_path
[k
] == ',' )
170 subdirs
= calloc( i_nb_subdirs
+ 1, sizeof(char*) );
174 for( i
= 0; psz_parser
&& *psz_parser
!= '\0' ; )
176 char *psz_subdir
= psz_parser
;
177 psz_parser
= strchr( psz_subdir
, ',' );
180 *psz_parser
++ = '\0';
181 while( *psz_parser
== ' ' )
184 if( *psz_subdir
== '\0' )
187 if( asprintf( &subdirs
[i
++], "%s%s",
188 psz_subdir
[0] == '.' ? psz_dir
: "",
198 * Detect subtitle files.
200 * When called this function will split up the psz_name string into a
201 * directory, filename and extension. It then opens the directory
202 * in which the file resides and tries to find possible matches of
206 * \param p_this the calling \ref input_thread_t
207 * \param psz_path a list of subdirectories (separated by a ',') to look in.
208 * \param psz_name_org the complete filename to base the search on.
209 * \param pp_slaves an initialized input item slave list to append detected subtitles to
210 * \param p_slaves pointer to the size of the slave list
211 * \return VLC_SUCCESS if ok
213 int subtitles_Detect( input_thread_t
*p_this
, char *psz_path
, const char *psz_name_org
,
214 input_item_slave_t
***ppp_slaves
, int *p_slaves
)
216 int i_fuzzy
= var_GetInteger( p_this
, "sub-autodetect-fuzzy" );
220 input_item_slave_t
**pp_slaves
= *ppp_slaves
;
221 int i_slaves
= *p_slaves
;
222 char *f_fname_noext
= NULL
, *f_fname_trim
= NULL
;
223 char **subdirs
; /* list of subdirectories to look in */
228 char *psz_fname
= vlc_uri2path( psz_name_org
);
232 /* extract filename & dirname from psz_fname */
233 char *f_dir
= strdup( psz_fname
);
240 const char *f_fname
= strrchr( psz_fname
, DIR_SEP_CHAR
);
247 f_fname
++; /* Skip the '/' */
248 f_dir
[f_fname
- psz_fname
] = 0; /* keep dir separator in f_dir */
250 i_fname_len
= strlen( f_fname
);
252 f_fname_noext
= malloc(i_fname_len
+ 1);
253 f_fname_trim
= malloc(i_fname_len
+ 1 );
254 if( !f_fname_noext
|| !f_fname_trim
)
257 free( f_fname_noext
);
258 free( f_fname_trim
);
263 strcpy_strip_ext( f_fname_noext
, f_fname
);
264 strcpy_trim( f_fname_trim
, f_fname_noext
);
266 subdirs
= paths_to_list( f_dir
, psz_path
);
267 for( int j
= -1; (j
== -1) || ( j
>= 0 && subdirs
!= NULL
&& subdirs
[j
] != NULL
); j
++ )
269 const char *psz_dir
= (j
< 0) ? f_dir
: subdirs
[j
];
270 if( psz_dir
== NULL
|| ( j
>= 0 && !strcmp( psz_dir
, f_dir
) ) )
273 /* parse psz_src dir */
274 DIR *dir
= vlc_opendir( psz_dir
);
278 msg_Dbg( p_this
, "looking for a subtitle file in %s", psz_dir
);
280 const char *psz_name
;
281 while( (psz_name
= vlc_readdir( dir
)) )
283 if( psz_name
[0] == '.' || !subtitles_Filter( psz_name
) )
286 char tmp_fname_noext
[strlen( psz_name
) + 1];
287 char tmp_fname_trim
[strlen( psz_name
) + 1];
288 char tmp_fname_ext
[strlen( psz_name
) + 1];
292 /* retrieve various parts of the filename */
293 strcpy_strip_ext( tmp_fname_noext
, psz_name
);
294 strcpy_get_ext( tmp_fname_ext
, psz_name
);
295 strcpy_trim( tmp_fname_trim
, tmp_fname_noext
);
297 if( !strcmp( tmp_fname_trim
, f_fname_trim
) )
299 /* matches the movie name exactly */
300 i_prio
= SLAVE_PRIORITY_MATCH_ALL
;
302 else if( (tmp
= strstr( tmp_fname_trim
, f_fname_trim
)) )
304 /* contains the movie name */
305 tmp
+= strlen( f_fname_trim
);
306 if( whiteonly( tmp
) )
308 /* chars in front of the movie name */
309 i_prio
= SLAVE_PRIORITY_MATCH_RIGHT
;
313 /* chars after (and possibly in front of)
315 i_prio
= SLAVE_PRIORITY_MATCH_LEFT
;
320 /* doesn't contain the movie name, prefer files in f_dir over subdirs */
321 i_prio
= SLAVE_PRIORITY_MATCH_NONE
;
323 if( i_prio
>= i_fuzzy
)
328 size_t i_len
= strlen( psz_dir
);
329 const char *psz_format
;
332 if( psz_dir
[i_len
- 1] == DIR_SEP_CHAR
)
335 psz_format
= "%s"DIR_SEP
"%s";
337 if( asprintf( &path
, psz_format
, psz_dir
, psz_name
) < 0 )
340 if( strcmp( path
, psz_fname
)
341 && vlc_stat( path
, &st
) == 0
342 && S_ISREG( st
.st_mode
) )
345 "autodetected subtitle: %s with priority %d",
347 char *psz_uri
= vlc_path2uri( path
, NULL
);
348 input_item_slave_t
*p_sub
= psz_uri
!= NULL
?
349 input_item_slave_New( psz_uri
, SLAVE_TYPE_SPU
, i_prio
)
353 p_sub
->b_forced
= true;
354 TAB_APPEND(i_slaves
, pp_slaves
, p_sub
);
365 for( size_t j
= 0; subdirs
[j
] != NULL
; j
++ )
370 free( f_fname_trim
);
371 free( f_fname_noext
);
374 for( int i
= 0; i
< i_slaves
; i
++ )
376 input_item_slave_t
*p_sub
= pp_slaves
[i
];
378 bool b_reject
= false;
379 char *psz_ext
= strrchr( p_sub
->psz_uri
, '.' );
384 if( !strcasecmp( psz_ext
, "sub" ) )
386 for( int j
= 0; j
< i_slaves
; j
++ )
388 input_item_slave_t
*p_sub_inner
= pp_slaves
[j
];
390 /* A slave can be null if it's already rejected */
391 if( p_sub_inner
== NULL
)
394 /* check that the filenames without extension match */
395 if( strncasecmp( p_sub
->psz_uri
, p_sub_inner
->psz_uri
,
396 strlen( p_sub
->psz_uri
) - 3 ) )
399 char *psz_ext_inner
= strrchr( p_sub_inner
->psz_uri
, '.' );
404 /* check that we have an idx file */
405 if( !strcasecmp( psz_ext_inner
, "idx" ) )
412 else if( !strcasecmp( psz_ext
, "cdg" ) )
414 if( p_sub
->i_priority
< SLAVE_PRIORITY_MATCH_ALL
)
420 input_item_slave_Delete( p_sub
);
424 /* Sort alphabetically */
426 qsort( pp_slaves
, i_slaves
, sizeof (input_item_slave_t
*), slave_strcmp
);
428 *ppp_slaves
= pp_slaves
; /* in case of realloc */
429 *p_slaves
= i_slaves
;