fourcc: add VLC_CODEC_VAAPI_420_10BPP fallbacks
[vlc.git] / src / input / subtitles.c
blob8b0952c1a022d2df434734ba9048b3914086b471
1 /*****************************************************************************
2 * subtitles.c : subtitles detection
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
5 * $Id$
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 *****************************************************************************/
25 /**
26 * \file
27 * This file contains functions to detect subtitle files.
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <ctype.h> /* isalnum() */
35 #include <unistd.h>
36 #include <sys/stat.h>
38 #include <vlc_common.h>
39 #include <vlc_fs.h>
40 #include <vlc_url.h>
42 #include "input_internal.h"
44 /**
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 )
51 unsigned char c;
53 /* skip leading whitespace */
54 while( ((c = *s) != '\0') && !isalnum(c) )
56 s++;
58 for(;;)
60 /* copy word */
61 while( ((c = *s) != '\0') && isalnum(c) )
63 *d = tolower(c);
64 s++; d++;
66 if( *s == 0 ) break;
67 /* trim excess whitespace */
68 while( ((c = *s) != '\0') && !isalnum(c) )
70 s++;
72 if( *s == 0 ) break;
73 *d++ = ' ';
75 *d = 0;
78 static void strcpy_strip_ext( char *d, const char *s )
80 unsigned char c;
82 const char *tmp = strrchr(s, '.');
83 if( !tmp )
85 strcpy(d, s);
86 return;
88 else
89 strlcpy(d, s, tmp - s + 1 );
90 while( (c = *d) != '\0' )
92 *d = tolower(c);
93 d++;
97 static void strcpy_get_ext( char *d, const char *s )
99 const char *tmp = strrchr(s, '.');
100 if( !tmp )
101 strcpy(d, "");
102 else
103 strcpy( d, tmp + 1 );
106 static int whiteonly( const char *s )
108 unsigned char c;
110 while( (c = *s) != '\0' )
112 if( isalnum( c ) )
113 return 0;
114 s++;
116 return 1;
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 )
125 return 0;
127 /* We can compare these uris since they come from the file system */
128 #ifdef HAVE_STRCOLL
129 return strcoll( p_slave0->psz_uri, p_slave1->psz_uri );
130 #else
131 return strcmp( p_slave0->psz_uri, p_slave1->psz_uri );
132 #endif
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, '.');
142 if( !tmp )
143 return 0;
144 tmp++;
146 for( int i = 0; sub_exts[i][0]; i++ )
147 if( strcasecmp( sub_exts[i], tmp ) == 0 )
148 return 1;
149 return 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 )
163 return NULL;
165 for( k = 0, i_nb_subdirs = 1; psz_path[k] != '\0'; k++ )
167 if( psz_path[k] == ',' )
168 i_nb_subdirs++;
171 subdirs = calloc( i_nb_subdirs + 1, sizeof(char*) );
172 if( !subdirs )
173 return NULL;
175 for( i = 0; psz_parser && *psz_parser != '\0' ; )
177 char *psz_subdir = psz_parser;
178 psz_parser = strchr( psz_subdir, ',' );
179 if( psz_parser )
181 *psz_parser++ = '\0';
182 while( *psz_parser == ' ' )
183 psz_parser++;
185 if( *psz_subdir == '\0' )
186 continue;
188 if( asprintf( &subdirs[i++], "%s%s",
189 psz_subdir[0] == '.' ? psz_dir : "",
190 psz_subdir ) == -1 )
191 break;
193 subdirs[i] = NULL;
195 return subdirs;
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
204 * subtitles files.
206 * \ingroup Demux
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" );
218 if ( i_fuzzy == 0 )
219 return VLC_EGENERIC;
220 int i_fname_len;
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 */
226 if( !psz_name_org )
227 return VLC_EGENERIC;
229 char *psz_fname = vlc_uri2path( psz_name_org );
230 if( !psz_fname )
231 return VLC_EGENERIC;
233 /* extract filename & dirname from psz_fname */
234 char *f_dir = strdup( psz_fname );
235 if( f_dir == 0 )
237 free( psz_fname );
238 return VLC_ENOMEM;
241 const char *f_fname = strrchr( psz_fname, DIR_SEP_CHAR );
242 if( !f_fname )
244 free( f_dir );
245 free( psz_fname );
246 return VLC_EGENERIC;
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 )
257 free( f_dir );
258 free( f_fname_noext );
259 free( f_fname_trim );
260 free( psz_fname );
261 return VLC_ENOMEM;
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( int 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 ) ) )
272 continue;
274 /* parse psz_src dir */
275 DIR *dir = vlc_opendir( psz_dir );
276 if( dir == NULL )
277 continue;
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 ) )
285 continue;
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];
290 const char *tmp;
291 int i_prio = 0;
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;
312 else
314 /* chars after (and possibly in front of)
315 * the movie name */
316 i_prio = SLAVE_PRIORITY_MATCH_LEFT;
319 else if( j == -1 )
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 )
326 struct stat st;
327 char *path;
329 size_t i_len = strlen( psz_dir );
330 const char *psz_format;
331 if ( i_len == 0 )
332 continue;
333 if( psz_dir[i_len - 1] == DIR_SEP_CHAR )
334 psz_format = "%s%s";
335 else
336 psz_format = "%s"DIR_SEP"%s";
338 if( asprintf( &path, psz_format, psz_dir, psz_name ) < 0 )
339 continue;
341 if( strcmp( path, psz_fname )
342 && vlc_stat( path, &st ) == 0
343 && S_ISREG( st.st_mode ) )
345 msg_Dbg( p_this,
346 "autodetected subtitle: %s with priority %d",
347 path, i_prio );
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 )
351 : NULL;
352 if( p_sub )
354 p_sub->b_forced = true;
355 TAB_APPEND(i_slaves, pp_slaves, p_sub);
357 free( psz_uri );
359 free( path );
362 closedir( dir );
364 if( subdirs )
366 for( size_t j = 0; subdirs[j] != NULL; j++ )
367 free( subdirs[j] );
368 free( subdirs );
370 free( f_dir );
371 free( f_fname_trim );
372 free( f_fname_noext );
373 free( psz_fname );
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, '.' );
381 if( !psz_ext )
382 continue;
383 psz_ext++;
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 )
393 continue;
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 ) )
398 continue;
400 char *psz_ext_inner = strrchr( p_sub_inner->psz_uri, '.' );
401 if( !psz_ext_inner )
402 continue;
403 psz_ext_inner++;
405 /* check that we have an idx file */
406 if( !strcasecmp( psz_ext_inner, "idx" ) )
408 b_reject = true;
409 break;
413 else if( !strcasecmp( psz_ext, "cdg" ) )
415 if( p_sub->i_priority < SLAVE_PRIORITY_MATCH_ALL )
416 b_reject = true;
418 if( b_reject )
420 pp_slaves[i] = NULL;
421 input_item_slave_Delete( p_sub );
425 /* Sort alphabetically */
426 if( i_slaves > 0 )
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;
431 return VLC_SUCCESS;