gui: macos: use float for rate
[vlc.git] / src / input / subtitles.c
blob25ebb52087f2ee69b530419febe0f110f16844fd
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 *****************************************************************************/
24 /**
25 * \file
26 * This file contains functions to detect subtitle files.
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <ctype.h> /* isalnum() */
34 #include <unistd.h>
35 #include <sys/stat.h>
37 #include <vlc_common.h>
38 #include <vlc_fs.h>
39 #include <vlc_url.h>
41 #include "input_internal.h"
43 /**
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 )
50 unsigned char c;
52 /* skip leading whitespace */
53 while( ((c = *s) != '\0') && !isalnum(c) )
55 s++;
57 for(;;)
59 /* copy word */
60 while( ((c = *s) != '\0') && isalnum(c) )
62 *d = tolower(c);
63 s++; d++;
65 if( *s == 0 ) break;
66 /* trim excess whitespace */
67 while( ((c = *s) != '\0') && !isalnum(c) )
69 s++;
71 if( *s == 0 ) break;
72 *d++ = ' ';
74 *d = 0;
77 static void strcpy_strip_ext( char *d, const char *s )
79 unsigned char c;
81 const char *tmp = strrchr(s, '.');
82 if( !tmp )
84 strcpy(d, s);
85 return;
87 else
88 strlcpy(d, s, tmp - s + 1 );
89 while( (c = *d) != '\0' )
91 *d = tolower(c);
92 d++;
96 static void strcpy_get_ext( char *d, const char *s )
98 const char *tmp = strrchr(s, '.');
99 if( !tmp )
100 strcpy(d, "");
101 else
102 strcpy( d, tmp + 1 );
105 static int whiteonly( const char *s )
107 unsigned char c;
109 while( (c = *s) != '\0' )
111 if( isalnum( c ) )
112 return 0;
113 s++;
115 return 1;
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 )
124 return 0;
126 /* We can compare these uris since they come from the file system */
127 #ifdef HAVE_STRCOLL
128 return strcoll( p_slave0->psz_uri, p_slave1->psz_uri );
129 #else
130 return strcmp( p_slave0->psz_uri, p_slave1->psz_uri );
131 #endif
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, '.');
141 if( !tmp )
142 return 0;
143 tmp++;
145 for( int i = 0; sub_exts[i][0]; i++ )
146 if( strcasecmp( sub_exts[i], tmp ) == 0 )
147 return 1;
148 return 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 )
162 return NULL;
164 for( k = 0, i_nb_subdirs = 1; psz_path[k] != '\0'; k++ )
166 if( psz_path[k] == ',' )
167 i_nb_subdirs++;
170 subdirs = calloc( i_nb_subdirs + 1, sizeof(char*) );
171 if( !subdirs )
172 return NULL;
174 for( i = 0; psz_parser && *psz_parser != '\0' ; )
176 char *psz_subdir = psz_parser;
177 psz_parser = strchr( psz_subdir, ',' );
178 if( psz_parser )
180 *psz_parser++ = '\0';
181 while( *psz_parser == ' ' )
182 psz_parser++;
184 if( *psz_subdir == '\0' )
185 continue;
187 if( asprintf( &subdirs[i++], "%s%s",
188 psz_subdir[0] == '.' ? psz_dir : "",
189 psz_subdir ) == -1 )
190 break;
192 subdirs[i] = NULL;
194 return subdirs;
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
203 * subtitles files.
205 * \ingroup Demux
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" );
217 if ( i_fuzzy == 0 )
218 return VLC_EGENERIC;
219 int i_fname_len;
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 */
225 if( !psz_name_org )
226 return VLC_EGENERIC;
228 char *psz_fname = vlc_uri2path( psz_name_org );
229 if( !psz_fname )
230 return VLC_EGENERIC;
232 /* extract filename & dirname from psz_fname */
233 char *f_dir = strdup( psz_fname );
234 if( f_dir == 0 )
236 free( psz_fname );
237 return VLC_ENOMEM;
240 const char *f_fname = strrchr( psz_fname, DIR_SEP_CHAR );
241 if( !f_fname )
243 free( f_dir );
244 free( psz_fname );
245 return VLC_EGENERIC;
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 )
256 free( f_dir );
257 free( f_fname_noext );
258 free( f_fname_trim );
259 free( psz_fname );
260 return VLC_ENOMEM;
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 ) ) )
271 continue;
273 /* parse psz_src dir */
274 DIR *dir = vlc_opendir( psz_dir );
275 if( dir == NULL )
276 continue;
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 ) )
284 continue;
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];
289 const char *tmp;
290 int i_prio = 0;
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;
311 else
313 /* chars after (and possibly in front of)
314 * the movie name */
315 i_prio = SLAVE_PRIORITY_MATCH_LEFT;
318 else if( j == -1 )
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 )
325 struct stat st;
326 char *path;
328 size_t i_len = strlen( psz_dir );
329 const char *psz_format;
330 if ( i_len == 0 )
331 continue;
332 if( psz_dir[i_len - 1] == DIR_SEP_CHAR )
333 psz_format = "%s%s";
334 else
335 psz_format = "%s"DIR_SEP"%s";
337 if( asprintf( &path, psz_format, psz_dir, psz_name ) < 0 )
338 continue;
340 if( strcmp( path, psz_fname )
341 && vlc_stat( path, &st ) == 0
342 && S_ISREG( st.st_mode ) )
344 msg_Dbg( p_this,
345 "autodetected subtitle: %s with priority %d",
346 path, i_prio );
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 )
350 : NULL;
351 if( p_sub )
353 p_sub->b_forced = true;
354 TAB_APPEND(i_slaves, pp_slaves, p_sub);
356 free( psz_uri );
358 free( path );
361 closedir( dir );
363 if( subdirs )
365 for( size_t j = 0; subdirs[j] != NULL; j++ )
366 free( subdirs[j] );
367 free( subdirs );
369 free( f_dir );
370 free( f_fname_trim );
371 free( f_fname_noext );
372 free( psz_fname );
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, '.' );
380 if( !psz_ext )
381 continue;
382 psz_ext++;
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 )
392 continue;
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 ) )
397 continue;
399 char *psz_ext_inner = strrchr( p_sub_inner->psz_uri, '.' );
400 if( !psz_ext_inner )
401 continue;
402 psz_ext_inner++;
404 /* check that we have an idx file */
405 if( !strcasecmp( psz_ext_inner, "idx" ) )
407 b_reject = true;
408 break;
412 else if( !strcasecmp( psz_ext, "cdg" ) )
414 if( p_sub->i_priority < SLAVE_PRIORITY_MATCH_ALL )
415 b_reject = true;
417 if( b_reject )
419 pp_slaves[i] = NULL;
420 input_item_slave_Delete( p_sub );
424 /* Sort alphabetically */
425 if( i_slaves > 0 )
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;
430 return VLC_SUCCESS;