chromecast: refactor, make place for SPUs
[vlc.git] / modules / demux / subtitle.c
blobf5320e6ca6a88d18ca4d9d6c96bf19d099db7e3b
1 /*****************************************************************************
2 * subtitle.c: Demux for subtitle text files.
3 *****************************************************************************
4 * Copyright (C) 1999-2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Derk-Jan Hartman <hartman at videolan dot org>
9 * Jean-Baptiste Kempf <jb@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_input.h>
37 #include <vlc_memory.h>
39 #include <ctype.h>
40 #include <math.h>
41 #include <assert.h>
43 #include <vlc_demux.h>
44 #include <vlc_charset.h>
46 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
49 static int Open ( vlc_object_t *p_this );
50 static void Close( vlc_object_t *p_this );
52 #define SUB_DELAY_LONGTEXT \
53 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
54 #define SUB_FPS_LONGTEXT \
55 N_("Override the normal frames per second settings. " \
56 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
57 #define SUB_TYPE_LONGTEXT \
58 N_("Force the subtiles format. Selecting \"auto\" means autodetection and should always work.")
59 #define SUB_DESCRIPTION_LONGTEXT \
60 N_("Override the default track description.")
62 static const char *const ppsz_sub_type[] =
64 "auto", "microdvd", "subrip", "subviewer", "ssa1",
65 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
66 "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
67 "subviewer1", "sbv"
70 vlc_module_begin ()
71 set_shortname( N_("Subtitles"))
72 set_description( N_("Text subtitle parser") )
73 set_capability( "demux", 0 )
74 set_category( CAT_INPUT )
75 set_subcategory( SUBCAT_INPUT_DEMUX )
76 add_float( "sub-fps", 0.0,
77 N_("Frames per Second"),
78 SUB_FPS_LONGTEXT, true )
79 add_integer( "sub-delay", 0,
80 N_("Subtitle delay"),
81 SUB_DELAY_LONGTEXT, true )
82 add_string( "sub-type", "auto", N_("Subtitle format"),
83 SUB_TYPE_LONGTEXT, true )
84 change_string_list( ppsz_sub_type, ppsz_sub_type )
85 add_string( "sub-description", NULL, N_("Subtitle description"),
86 SUB_DESCRIPTION_LONGTEXT, true )
87 set_callbacks( Open, Close )
89 add_shortcut( "subtitle" )
90 vlc_module_end ()
92 /*****************************************************************************
93 * Prototypes:
94 *****************************************************************************/
95 enum subtitle_type_e
97 SUB_TYPE_UNKNOWN = -1,
98 SUB_TYPE_MICRODVD,
99 SUB_TYPE_SUBRIP,
100 SUB_TYPE_SSA1,
101 SUB_TYPE_SSA2_4,
102 SUB_TYPE_ASS,
103 SUB_TYPE_VPLAYER,
104 SUB_TYPE_SAMI,
105 SUB_TYPE_SUBVIEWER, /* SUBVIEWER 2 */
106 SUB_TYPE_DVDSUBTITLE, /* Mplayer calls it subviewer2 */
107 SUB_TYPE_MPL2,
108 SUB_TYPE_AQT,
109 SUB_TYPE_PJS,
110 SUB_TYPE_MPSUB,
111 SUB_TYPE_JACOSUB,
112 SUB_TYPE_PSB,
113 SUB_TYPE_RT,
114 SUB_TYPE_DKS,
115 SUB_TYPE_SUBVIEW1, /* SUBVIEWER 1 - mplayer calls it subrip09,
116 and Gnome subtitles SubViewer 1.0 */
117 SUB_TYPE_SBV,
118 SUB_TYPE_SCC, /* Scenarist Closed Caption */
121 typedef struct
123 size_t i_line_count;
124 size_t i_line;
125 char **line;
126 } text_t;
128 static int TextLoad( text_t *, stream_t *s );
129 static void TextUnload( text_t * );
131 typedef struct
133 int64_t i_start;
134 int64_t i_stop;
136 char *psz_text;
137 } subtitle_t;
139 typedef struct
141 enum subtitle_type_e i_type;
142 int64_t i_microsecperframe;
144 char *psz_header; /* SSA */
146 struct
148 bool b_inited;
150 int i_comment;
151 int i_time_resolution;
152 int i_time_shift;
153 } jss;
155 struct
157 bool b_inited;
159 float f_total;
160 float f_factor;
161 } mpsub;
163 struct
165 const char *psz_start;
166 } sami;
168 } subs_properties_t;
170 struct demux_sys_t
172 es_out_id_t *es;
173 bool b_slave;
174 bool b_first_time;
176 int64_t i_next_demux_date;
178 struct
180 subtitle_t *p_array;
181 size_t i_count;
182 size_t i_current;
183 } subtitles;
185 int64_t i_length;
187 /* */
188 subs_properties_t props;
190 block_t * (*pf_convert)( const subtitle_t * );
193 static int ParseMicroDvd ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
194 static int ParseSubRip ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
195 static int ParseSubViewer ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
196 static int ParseSSA ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
197 static int ParseVplayer ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
198 static int ParseSami ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
199 static int ParseDVDSubtitle( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
200 static int ParseMPL2 ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
201 static int ParseAQT ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
202 static int ParsePJS ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
203 static int ParseMPSub ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
204 static int ParseJSS ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
205 static int ParsePSB ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
206 static int ParseRealText ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
207 static int ParseDKS ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
208 static int ParseSubViewer1 ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
209 static int ParseCommonSBV ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
210 static int ParseSCC ( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t *, size_t );
212 static const struct
214 const char *psz_type_name;
215 int i_type;
216 const char *psz_name;
217 int (*pf_read)( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t*, size_t );
218 } sub_read_subtitle_function [] =
220 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
221 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
222 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
223 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
224 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
225 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
226 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
227 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
228 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
229 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
230 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
231 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
232 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
233 { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS },
234 { "psb", SUB_TYPE_PSB, "PowerDivx", ParsePSB },
235 { "realtext", SUB_TYPE_RT, "RealText", ParseRealText },
236 { "dks", SUB_TYPE_DKS, "DKS", ParseDKS },
237 { "subviewer1", SUB_TYPE_SUBVIEW1, "Subviewer 1", ParseSubViewer1 },
238 { "sbv", SUB_TYPE_SBV, "SBV", ParseCommonSBV },
239 { "scc", SUB_TYPE_SCC, "SCC", ParseSCC },
240 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
242 /* When adding support for more formats, be sure to add their file extension
243 * to src/input/subtitles.c to enable auto-detection.
246 static int Demux( demux_t * );
247 static int Control( demux_t *, int, va_list );
249 static void Fix( demux_t * );
250 static char * get_language_from_filename( const char * );
252 /*****************************************************************************
253 * Decoder format output function
254 *****************************************************************************/
256 static block_t *ToTextBlock( const subtitle_t *p_subtitle )
258 block_t *p_block;
259 size_t i_len = strlen( p_subtitle->psz_text ) + 1;
261 if( i_len <= 1 || !(p_block = block_Alloc( i_len )) )
262 return NULL;
264 memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
266 return p_block;
269 static block_t *ToEIA608Block( const subtitle_t *p_subtitle )
271 block_t *p_block;
272 const size_t i_len = strlen( p_subtitle->psz_text );
273 const size_t i_block = (1 + i_len / 5) * 3;
275 if( i_len < 4 || !(p_block = block_Alloc( i_block )) )
276 return NULL;
278 p_block->i_buffer = 0;
280 char *saveptr = NULL;
281 char *psz_tok = strtok_r( p_subtitle->psz_text, " ", &saveptr );
282 unsigned a, b;
283 while( psz_tok &&
284 sscanf( psz_tok, "%2x%2x", &a, &b ) == 2 &&
285 i_block - p_block->i_buffer >= 3 )
287 uint8_t *p_data = &p_block->p_buffer[p_block->i_buffer];
288 p_data[0] = 0xFC;
289 p_data[1] = a;
290 p_data[2] = b;
291 p_block->i_buffer += 3;
292 psz_tok = strtok_r( NULL, " ", &saveptr );
295 return p_block;
298 /*****************************************************************************
299 * Module initializer
300 *****************************************************************************/
301 static int Open ( vlc_object_t *p_this )
303 demux_t *p_demux = (demux_t*)p_this;
304 demux_sys_t *p_sys;
305 es_format_t fmt;
306 float f_fps;
307 char *psz_type;
308 int (*pf_read)( vlc_object_t *, subs_properties_t *, text_t *, subtitle_t*, size_t );
310 if( !p_demux->obj.force )
312 msg_Dbg( p_demux, "subtitle demux discarded" );
313 return VLC_EGENERIC;
316 p_demux->pf_demux = Demux;
317 p_demux->pf_control = Control;
318 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
319 if( p_sys == NULL )
320 return VLC_ENOMEM;
322 p_sys->b_slave = false;
323 p_sys->b_first_time = true;
324 p_sys->i_next_demux_date = 0;
326 p_sys->pf_convert = ToTextBlock;
328 p_sys->subtitles.i_current= 0;
329 p_sys->subtitles.i_count = 0;
330 p_sys->subtitles.p_array = NULL;
332 p_sys->props.psz_header = NULL;
333 p_sys->props.i_microsecperframe = 40000;
334 p_sys->props.jss.b_inited = false;
335 p_sys->props.mpsub.b_inited = false;
336 p_sys->props.sami.psz_start = NULL;
338 /* Get the FPS */
339 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
340 if( f_fps >= 1.f )
341 p_sys->props.i_microsecperframe = llroundf( 1000000.f / f_fps );
343 msg_Dbg( p_demux, "Movie fps: %f", (double) f_fps );
345 /* Check for override of the fps */
346 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
347 if( f_fps >= 1.f )
349 p_sys->props.i_microsecperframe = llroundf( 1000000.f / f_fps );
350 msg_Dbg( p_demux, "Override subtitle fps %f", (double) f_fps );
353 /* Get or probe the type */
354 p_sys->props.i_type = SUB_TYPE_UNKNOWN;
355 psz_type = var_CreateGetString( p_demux, "sub-type" );
356 if( psz_type && *psz_type )
358 for( int i = 0; ; i++ )
360 if( sub_read_subtitle_function[i].psz_type_name == NULL )
361 break;
363 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
364 psz_type ) )
366 p_sys->props.i_type = sub_read_subtitle_function[i].i_type;
367 break;
371 free( psz_type );
373 #ifndef NDEBUG
374 const uint64_t i_start_pos = vlc_stream_Tell( p_demux->s );
375 #endif
377 size_t i_peek;
378 const uint8_t *p_peek;
379 if( vlc_stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
381 free( p_sys );
382 return VLC_EGENERIC;
385 enum
387 UTF8BOM,
388 UTF16LE,
389 UTF16BE,
390 NOBOM,
391 } e_bom = NOBOM;
392 const char *psz_bom = NULL;
394 i_peek = 4096;
395 /* Detect Unicode while skipping the UTF-8 Byte Order Mark */
396 if( !memcmp( p_peek, "\xEF\xBB\xBF", 3 ) )
398 e_bom = UTF8BOM;
399 psz_bom = "UTF-8";
401 else if( !memcmp( p_peek, "\xFF\xFE", 2 ) )
403 e_bom = UTF16LE;
404 psz_bom = "UTF-16LE";
405 i_peek *= 2;
407 else if( !memcmp( p_peek, "\xFE\xFF", 2 ) )
409 e_bom = UTF16BE;
410 psz_bom = "UTF-16BE";
411 i_peek *= 2;
414 if( e_bom != NOBOM )
415 msg_Dbg( p_demux, "detected %s Byte Order Mark", psz_bom );
417 i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_peek );
418 if( unlikely(i_peek < 16) )
420 free( p_sys );
421 return VLC_EGENERIC;
424 stream_t *p_probestream = NULL;
425 if( e_bom != UTF8BOM && e_bom != NOBOM )
427 if( i_peek > 16 )
429 char *p_outbuf = FromCharset( psz_bom, p_peek, i_peek );
430 if( p_outbuf != NULL )
431 p_probestream = vlc_stream_MemoryNew( p_demux, (uint8_t *)p_outbuf,
432 strlen( p_outbuf ),
433 false ); /* free p_outbuf on release */
436 else
438 const size_t i_skip = (e_bom == UTF8BOM) ? 3 : 0;
439 p_probestream = vlc_stream_MemoryNew( p_demux, (uint8_t *) &p_peek[i_skip],
440 i_peek - i_skip, true );
443 if( p_probestream == NULL )
445 free( p_sys );
446 return VLC_EGENERIC;
449 /* Probe if unknown type */
450 if( p_sys->props.i_type == SUB_TYPE_UNKNOWN )
452 int i_try;
453 char *s = NULL;
455 msg_Dbg( p_demux, "autodetecting subtitle format" );
456 for( i_try = 0; i_try < 256; i_try++ )
458 int i_dummy;
459 char p_dummy;
461 if( (s = vlc_stream_ReadLine( p_probestream ) ) == NULL )
462 break;
464 if( strcasestr( s, "<SAMI>" ) )
466 p_sys->props.i_type = SUB_TYPE_SAMI;
467 break;
469 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
470 sscanf( s, "{%d}{}", &i_dummy ) == 1)
472 p_sys->props.i_type = SUB_TYPE_MICRODVD;
473 break;
475 else if( sscanf( s, "%d:%d:%d,%d --> %d:%d:%d,%d",
476 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
477 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
478 sscanf( s, "%d:%d:%d --> %d:%d:%d,%d",
479 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
480 &i_dummy,&i_dummy,&i_dummy ) == 7 ||
481 sscanf( s, "%d:%d:%d,%d --> %d:%d:%d",
482 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
483 &i_dummy,&i_dummy,&i_dummy ) == 7 ||
484 sscanf( s, "%d:%d:%d.%d --> %d:%d:%d.%d",
485 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
486 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
487 sscanf( s, "%d:%d:%d --> %d:%d:%d.%d",
488 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
489 &i_dummy,&i_dummy,&i_dummy ) == 7 ||
490 sscanf( s, "%d:%d:%d.%d --> %d:%d:%d",
491 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
492 &i_dummy,&i_dummy,&i_dummy ) == 7 ||
493 sscanf( s, "%d:%d:%d --> %d:%d:%d",
494 &i_dummy,&i_dummy,&i_dummy,
495 &i_dummy,&i_dummy,&i_dummy ) == 6 )
497 p_sys->props.i_type = SUB_TYPE_SUBRIP;
498 break;
500 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
502 p_sys->props.i_type = SUB_TYPE_SSA1;
503 break;
505 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
507 p_sys->props.i_type = SUB_TYPE_ASS;
508 break;
510 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
512 p_sys->props.i_type = SUB_TYPE_SSA2_4;
513 break;
515 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
517 p_sys->props.i_type = SUB_TYPE_SSA2_4;
518 break;
520 else if( !strncasecmp( s, "Dialogue:", 9 ) )
522 p_sys->props.i_type = SUB_TYPE_ASS;
523 break;
525 else if( strcasestr( s, "[INFORMATION]" ) )
527 p_sys->props.i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
528 break;
530 else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
531 &i_dummy, &i_dummy, &i_dummy, &i_dummy,
532 &i_dummy, &i_dummy, &i_dummy ) == 7 ||
533 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
535 p_sys->props.i_type = SUB_TYPE_JACOSUB;
536 break;
538 else if( sscanf( s, "%d:%d:%d.%d,%d:%d:%d.%d",
539 &i_dummy, &i_dummy, &i_dummy, &i_dummy,
540 &i_dummy, &i_dummy, &i_dummy, &i_dummy ) == 8 )
542 p_sys->props.i_type = SUB_TYPE_SBV;
543 break;
545 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
546 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
548 p_sys->props.i_type = SUB_TYPE_VPLAYER;
549 break;
551 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
552 &i_dummy, &i_dummy ) == 4 )
554 p_sys->props.i_type = SUB_TYPE_DVDSUBTITLE;
555 break;
557 else if( sscanf( s, "[%d:%d:%d]%c",
558 &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
560 p_sys->props.i_type = SUB_TYPE_DKS;
561 break;
563 else if( strstr( s, "*** START SCRIPT" ) )
565 p_sys->props.i_type = SUB_TYPE_SUBVIEW1;
566 break;
568 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
569 sscanf( s, "[%d][]", &i_dummy ) == 1)
571 p_sys->props.i_type = SUB_TYPE_MPL2;
572 break;
574 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
575 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
576 && p_dummy =='E' ) )
578 p_sys->props.i_type = SUB_TYPE_MPSUB;
579 break;
581 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
583 p_sys->props.i_type = SUB_TYPE_AQT;
584 break;
586 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
588 p_sys->props.i_type = SUB_TYPE_PJS;
589 break;
591 else if( sscanf( s, "{%d:%d:%d}",
592 &i_dummy, &i_dummy, &i_dummy ) == 3 )
594 p_sys->props.i_type = SUB_TYPE_PSB;
595 break;
597 else if( strcasestr( s, "<time" ) )
599 p_sys->props.i_type = SUB_TYPE_RT;
600 break;
602 else if( !strncasecmp( s, "WEBVTT",6 ) )
604 /* FAIL */
605 break;
607 else if( !strncasecmp( s, "Scenarist_SCC V1.0", 18 ) )
609 p_sys->props.i_type = SUB_TYPE_SCC;
610 p_sys->pf_convert = ToEIA608Block;
611 break;
614 free( s );
615 s = NULL;
618 free( s );
621 vlc_stream_Delete( p_probestream );
623 /* Quit on unknown subtitles */
624 if( p_sys->props.i_type == SUB_TYPE_UNKNOWN )
626 #ifndef NDEBUG
627 /* Ensure it will work with non seekable streams */
628 assert( i_start_pos == vlc_stream_Tell( p_demux->s ) );
629 #endif
630 msg_Warn( p_demux, "failed to recognize subtitle type" );
631 free( p_sys );
632 return VLC_EGENERIC;
635 for( int i = 0; ; i++ )
637 if( sub_read_subtitle_function[i].i_type == p_sys->props.i_type )
639 msg_Dbg( p_demux, "detected %s format",
640 sub_read_subtitle_function[i].psz_name );
641 pf_read = sub_read_subtitle_function[i].pf_read;
642 break;
646 msg_Dbg( p_demux, "loading all subtitles..." );
648 if( e_bom == UTF8BOM && /* skip BOM */
649 vlc_stream_Read( p_demux->s, NULL, 3 ) != 3 )
651 Close( p_this );
652 return VLC_EGENERIC;
655 /* Load the whole file */
656 text_t txtlines;
657 TextLoad( &txtlines, p_demux->s );
659 /* Parse it */
660 for( size_t i_max = 0; i_max < SIZE_MAX - 500 * sizeof(subtitle_t); )
662 if( p_sys->subtitles.i_count >= i_max )
664 i_max += 500;
665 subtitle_t *p_realloc = realloc( p_sys->subtitles.p_array, sizeof(subtitle_t) * i_max );
666 if( p_realloc == NULL )
668 TextUnload( &txtlines );
669 Close( p_this );
670 return VLC_ENOMEM;
672 p_sys->subtitles.p_array = p_realloc;
675 if( pf_read( VLC_OBJECT(p_demux), &p_sys->props, &txtlines,
676 &p_sys->subtitles.p_array[p_sys->subtitles.i_count],
677 p_sys->subtitles.i_count ) )
678 break;
680 p_sys->subtitles.i_count++;
682 /* Unload */
683 TextUnload( &txtlines );
685 msg_Dbg(p_demux, "loaded %zu subtitles", p_sys->subtitles.i_count );
687 /* Fix subtitle (order and time) *** */
688 p_sys->subtitles.i_current = 0;
689 p_sys->i_length = 0;
690 if( p_sys->subtitles.i_count > 0 )
691 p_sys->i_length = p_sys->subtitles.p_array[p_sys->subtitles.i_count-1].i_stop;
693 /* *** add subtitle ES *** */
694 if( p_sys->props.i_type == SUB_TYPE_SSA1 ||
695 p_sys->props.i_type == SUB_TYPE_SSA2_4 ||
696 p_sys->props.i_type == SUB_TYPE_ASS )
698 Fix( p_demux );
699 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
701 else if( p_sys->props.i_type == SUB_TYPE_SCC )
703 es_format_Init( &fmt, SPU_ES, VLC_CODEC_CEA608 );
704 fmt.subs.cc.i_reorder_depth = -1;
706 else
707 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
709 /* Stupid language detection in the filename */
710 char * psz_language = get_language_from_filename( p_demux->psz_filepath );
712 if( psz_language )
714 fmt.psz_language = psz_language;
715 msg_Dbg( p_demux, "detected language %s of subtitle: %s", psz_language,
716 p_demux->psz_location );
719 if( psz_bom )
720 fmt.subs.psz_encoding = strdup( psz_bom );
721 char *psz_description = var_InheritString( p_demux, "sub-description" );
722 if( psz_description && *psz_description )
723 fmt.psz_description = psz_description;
724 else
725 free( psz_description );
726 if( p_sys->props.psz_header != NULL &&
727 (fmt.p_extra = strdup( p_sys->props.psz_header )) )
729 fmt.i_extra = strlen( p_sys->props.psz_header ) + 1;
732 p_sys->es = es_out_Add( p_demux->out, &fmt );
733 es_format_Clean( &fmt );
734 if( p_sys->es == NULL )
736 Close( p_this );
737 return VLC_EGENERIC;
740 return VLC_SUCCESS;
743 /*****************************************************************************
744 * Close: Close subtitle demux
745 *****************************************************************************/
746 static void Close( vlc_object_t *p_this )
748 demux_t *p_demux = (demux_t*)p_this;
749 demux_sys_t *p_sys = p_demux->p_sys;
751 for( size_t i = 0; i < p_sys->subtitles.i_count; i++ )
752 free( p_sys->subtitles.p_array[i].psz_text );
753 free( p_sys->subtitles.p_array );
754 free( p_sys->props.psz_header );
756 free( p_sys );
759 /*****************************************************************************
760 * Control:
761 *****************************************************************************/
762 static int Control( demux_t *p_demux, int i_query, va_list args )
764 demux_sys_t *p_sys = p_demux->p_sys;
765 int64_t *pi64, i64;
766 double *pf, f;
768 switch( i_query )
770 case DEMUX_CAN_SEEK:
771 *va_arg( args, bool * ) = true;
772 return VLC_SUCCESS;
774 case DEMUX_GET_LENGTH:
775 pi64 = va_arg( args, int64_t * );
776 *pi64 = p_sys->i_length;
777 return VLC_SUCCESS;
779 case DEMUX_GET_TIME:
780 pi64 = va_arg( args, int64_t * );
781 *pi64 = p_sys->i_next_demux_date - var_GetInteger( p_demux->obj.parent, "spu-delay" );
782 if( *pi64 < 0 )
783 *pi64 = p_sys->i_next_demux_date;
784 return VLC_SUCCESS;
786 case DEMUX_SET_TIME:
787 i64 = va_arg( args, int64_t );
788 for( size_t i = 0; i + 1< p_sys->subtitles.i_count; i++ )
790 if( p_sys->subtitles.p_array[i + 1].i_start >= i64 )
792 p_sys->subtitles.i_current = i;
793 p_sys->i_next_demux_date = i64;
794 p_sys->b_first_time = true;
795 return VLC_SUCCESS;
798 break;
800 case DEMUX_GET_POSITION:
801 pf = va_arg( args, double * );
802 if( p_sys->subtitles.i_current >= p_sys->subtitles.i_count )
804 *pf = 1.0;
806 else if( p_sys->subtitles.i_count > 0 && p_sys->i_length )
808 *pf = p_sys->i_next_demux_date - var_GetInteger( p_demux->obj.parent, "spu-delay" );
809 if( *pf < 0 )
810 *pf = p_sys->i_next_demux_date;
811 *pf /= p_sys->i_length;
813 else
815 *pf = 0.0;
817 return VLC_SUCCESS;
819 case DEMUX_SET_POSITION:
820 f = va_arg( args, double );
821 if( p_sys->subtitles.i_count && p_sys->i_length )
823 i64 = VLC_TS_0 + f * p_sys->i_length;
824 return demux_Control( p_demux, DEMUX_SET_TIME, i64 );
826 break;
828 case DEMUX_SET_NEXT_DEMUX_TIME:
829 p_sys->b_slave = true;
830 p_sys->i_next_demux_date = va_arg( args, int64_t ) - VLC_TS_0;
831 return VLC_SUCCESS;
833 case DEMUX_CAN_PAUSE:
834 case DEMUX_SET_PAUSE_STATE:
835 case DEMUX_CAN_CONTROL_PACE:
836 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
838 case DEMUX_GET_PTS_DELAY:
839 case DEMUX_GET_FPS:
840 case DEMUX_GET_META:
841 case DEMUX_GET_ATTACHMENTS:
842 case DEMUX_GET_TITLE_INFO:
843 case DEMUX_HAS_UNSUPPORTED_META:
844 case DEMUX_CAN_RECORD:
845 default:
846 break;
849 return VLC_EGENERIC;
852 /*****************************************************************************
853 * Demux: Send subtitle to decoder
854 *****************************************************************************/
855 static int Demux( demux_t *p_demux )
857 demux_sys_t *p_sys = p_demux->p_sys;
859 int64_t i_barrier = p_sys->i_next_demux_date - var_GetInteger( p_demux->obj.parent, "spu-delay" );
860 if( i_barrier < 0 )
861 i_barrier = p_sys->i_next_demux_date;
863 while( p_sys->subtitles.i_current < p_sys->subtitles.i_count &&
864 p_sys->subtitles.p_array[p_sys->subtitles.i_current].i_start <= i_barrier )
866 const subtitle_t *p_subtitle = &p_sys->subtitles.p_array[p_sys->subtitles.i_current];
868 if ( !p_sys->b_slave && p_sys->b_first_time )
870 es_out_SetPCR( p_demux->out, VLC_TS_0 + i_barrier );
871 p_sys->b_first_time = false;
874 if( p_subtitle->i_start >= 0 )
876 block_t *p_block = p_sys->pf_convert( p_subtitle );
877 if( p_block )
879 p_block->i_dts =
880 p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
881 if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
882 p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
884 es_out_Send( p_demux->out, p_sys->es, p_block );
888 p_sys->subtitles.i_current++;
891 if ( !p_sys->b_slave )
893 es_out_SetPCR( p_demux->out, VLC_TS_0 + i_barrier );
894 p_sys->i_next_demux_date += CLOCK_FREQ / 8;
897 if( p_sys->subtitles.i_current >= p_sys->subtitles.i_count )
898 return VLC_DEMUXER_EOF;
900 return VLC_DEMUXER_SUCCESS;
904 static int subtitle_cmp( const void *first, const void *second )
906 int64_t result = ((subtitle_t *)(first))->i_start - ((subtitle_t *)(second))->i_start;
907 /* Return -1, 0 ,1, and not directly subtraction
908 * as result can be > INT_MAX */
909 return result == 0 ? 0 : result > 0 ? 1 : -1;
911 /*****************************************************************************
912 * Fix: fix time stamp and order of subtitle
913 *****************************************************************************/
914 static void Fix( demux_t *p_demux )
916 demux_sys_t *p_sys = p_demux->p_sys;
918 /* *** fix order (to be sure...) *** */
919 qsort( p_sys->subtitles.p_array, p_sys->subtitles.i_count, sizeof( p_sys->subtitles.p_array[0] ), subtitle_cmp);
922 static int TextLoad( text_t *txt, stream_t *s )
924 size_t i_line_max;
926 /* init txt */
927 i_line_max = 500;
928 txt->i_line_count = 0;
929 txt->i_line = 0;
930 txt->line = calloc( i_line_max, sizeof( char * ) );
931 if( !txt->line )
932 return VLC_ENOMEM;
934 /* load the complete file */
935 for( ;; )
937 char *psz = vlc_stream_ReadLine( s );
939 if( psz == NULL )
940 break;
942 txt->line[txt->i_line_count] = psz;
943 if( txt->i_line_count + 1 >= i_line_max )
945 i_line_max += 100;
946 char **p_realloc = realloc( txt->line, i_line_max * sizeof( char * ) );
947 if( p_realloc == NULL )
948 return VLC_ENOMEM;
949 txt->line = p_realloc;
951 txt->i_line_count++;
954 if( txt->i_line_count == 0 )
956 free( txt->line );
957 return VLC_EGENERIC;
960 return VLC_SUCCESS;
962 static void TextUnload( text_t *txt )
964 if( txt->i_line_count )
966 for( size_t i = 0; i < txt->i_line_count; i++ )
967 free( txt->line[i] );
968 free( txt->line );
970 txt->i_line = 0;
971 txt->i_line_count = 0;
974 static char *TextGetLine( text_t *txt )
976 if( txt->i_line >= txt->i_line_count )
977 return( NULL );
979 return txt->line[txt->i_line++];
981 static void TextPreviousLine( text_t *txt )
983 if( txt->i_line > 0 )
984 txt->i_line--;
987 /*****************************************************************************
988 * Specific Subtitle function
989 *****************************************************************************/
990 /* ParseMicroDvd:
991 * Format:
992 * {n1}{n2}Line1|Line2|Line3....
993 * where n1 and n2 are the video frame number (n2 can be empty)
995 static int ParseMicroDvd( vlc_object_t *p_obj, subs_properties_t *p_props,
996 text_t *txt, subtitle_t *p_subtitle,
997 size_t i_idx )
999 VLC_UNUSED( i_idx );
1000 char *psz_text;
1001 int i_start;
1002 int i_stop;
1003 int i;
1005 for( ;; )
1007 const char *s = TextGetLine( txt );
1008 if( !s )
1009 return VLC_EGENERIC;
1011 psz_text = malloc( strlen(s) + 1 );
1012 if( !psz_text )
1013 return VLC_ENOMEM;
1015 i_start = 0;
1016 i_stop = -1;
1017 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
1018 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1020 if( i_start != 1 || i_stop != 1 )
1021 break;
1023 /* We found a possible setting of the framerate "{1}{1}23.976" */
1024 /* Check if it's usable, and if the sub-fps is not set */
1025 float f_fps = us_strtof( psz_text, NULL );
1026 if( f_fps > 0.f && var_GetFloat( p_obj, "sub-fps" ) <= 0.f )
1027 p_props->i_microsecperframe = llroundf(1000000.f / f_fps);
1029 free( psz_text );
1032 /* replace | by \n */
1033 for( i = 0; psz_text[i] != '\0'; i++ )
1035 if( psz_text[i] == '|' )
1036 psz_text[i] = '\n';
1039 /* */
1040 p_subtitle->i_start = i_start * p_props->i_microsecperframe;
1041 p_subtitle->i_stop = i_stop >= 0 ? (i_stop * p_props->i_microsecperframe) : -1;
1042 p_subtitle->psz_text = psz_text;
1043 return VLC_SUCCESS;
1046 /* ParseSubRipSubViewer
1047 * Format SubRip
1049 * h1:m1:s1,d1 --> h2:m2:s2,d2
1050 * Line1
1051 * Line2
1052 * ....
1053 * [Empty line]
1054 * Format SubViewer v1/v2
1055 * h1:m1:s1.d1,h2:m2:s2.d2
1056 * Line1[br]Line2
1057 * Line3
1058 * ...
1059 * [empty line]
1060 * We ignore line number for SubRip
1062 static int ParseSubRipSubViewer( vlc_object_t *p_obj, subs_properties_t *p_props,
1063 text_t *txt, subtitle_t *p_subtitle,
1064 int (* pf_parse_timing)(subtitle_t *, const char *),
1065 bool b_replace_br )
1067 VLC_UNUSED(p_obj);
1068 VLC_UNUSED(p_props);
1069 char *psz_text;
1071 for( ;; )
1073 const char *s = TextGetLine( txt );
1075 if( !s )
1076 return VLC_EGENERIC;
1078 if( pf_parse_timing( p_subtitle, s) == VLC_SUCCESS &&
1079 p_subtitle->i_start < p_subtitle->i_stop )
1081 break;
1085 /* Now read text until an empty line */
1086 psz_text = strdup("");
1087 if( !psz_text )
1088 return VLC_ENOMEM;
1090 for( ;; )
1092 const char *s = TextGetLine( txt );
1093 size_t i_len;
1094 size_t i_old;
1096 i_len = s ? strlen( s ) : 0;
1097 if( i_len <= 0 )
1099 p_subtitle->psz_text = psz_text;
1100 return VLC_SUCCESS;
1103 i_old = strlen( psz_text );
1104 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1105 if( !psz_text )
1107 return VLC_ENOMEM;
1109 strcat( psz_text, s );
1110 strcat( psz_text, "\n" );
1112 /* replace [br] by \n */
1113 if( b_replace_br )
1115 char *p;
1117 while( ( p = strstr( psz_text, "[br]" ) ) )
1119 *p++ = '\n';
1120 memmove( p, &p[3], strlen(&p[3])+1 );
1126 /* subtitle_ParseSubRipTimingValue
1127 * Parses SubRip timing value.
1129 static int subtitle_ParseSubRipTimingValue(int64_t *timing_value,
1130 const char *s)
1132 int h1, m1, s1, d1 = 0;
1134 if ( sscanf( s, "%d:%d:%d,%d",
1135 &h1, &m1, &s1, &d1 ) == 4 ||
1136 sscanf( s, "%d:%d:%d.%d",
1137 &h1, &m1, &s1, &d1 ) == 4 ||
1138 sscanf( s, "%d:%d:%d",
1139 &h1, &m1, &s1) == 3 )
1141 (*timing_value) = ( (int64_t)h1 * 3600 * 1000 +
1142 (int64_t)m1 * 60 * 1000 +
1143 (int64_t)s1 * 1000 +
1144 (int64_t)d1 ) * 1000;
1146 return VLC_SUCCESS;
1149 return VLC_EGENERIC;
1152 /* subtitle_ParseSubRipTiming
1153 * Parses SubRip timing.
1155 static int subtitle_ParseSubRipTiming( subtitle_t *p_subtitle,
1156 const char *s )
1158 int i_result = VLC_EGENERIC;
1159 char *psz_start, *psz_stop;
1160 psz_start = malloc( strlen(s) + 1 );
1161 psz_stop = malloc( strlen(s) + 1 );
1163 if( sscanf( s, "%s --> %s", psz_start, psz_stop) == 2 &&
1164 subtitle_ParseSubRipTimingValue( &p_subtitle->i_start, psz_start ) == VLC_SUCCESS &&
1165 subtitle_ParseSubRipTimingValue( &p_subtitle->i_stop, psz_stop ) == VLC_SUCCESS )
1167 i_result = VLC_SUCCESS;
1170 free(psz_start);
1171 free(psz_stop);
1173 return i_result;
1175 /* ParseSubRip
1177 static int ParseSubRip( vlc_object_t *p_obj, subs_properties_t *p_props,
1178 text_t *txt, subtitle_t *p_subtitle,
1179 size_t i_idx )
1181 VLC_UNUSED( i_idx );
1182 return ParseSubRipSubViewer( p_obj, p_props, txt, p_subtitle,
1183 &subtitle_ParseSubRipTiming,
1184 false );
1187 /* subtitle_ParseSubViewerTiming
1188 * Parses SubViewer timing.
1190 static int subtitle_ParseSubViewerTiming( subtitle_t *p_subtitle,
1191 const char *s )
1193 int h1, m1, s1, d1, h2, m2, s2, d2;
1195 if( sscanf( s, "%d:%d:%d.%d,%d:%d:%d.%d",
1196 &h1, &m1, &s1, &d1, &h2, &m2, &s2, &d2) == 8 )
1198 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1199 (int64_t)m1 * 60*1000 +
1200 (int64_t)s1 * 1000 +
1201 (int64_t)d1 ) * 1000;
1203 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1204 (int64_t)m2 * 60*1000 +
1205 (int64_t)s2 * 1000 +
1206 (int64_t)d2 ) * 1000;
1207 return VLC_SUCCESS;
1209 return VLC_EGENERIC;
1212 /* ParseSubViewer
1214 static int ParseSubViewer( vlc_object_t *p_obj, subs_properties_t *p_props,
1215 text_t *txt, subtitle_t *p_subtitle,
1216 size_t i_idx )
1218 VLC_UNUSED( i_idx );
1220 return ParseSubRipSubViewer( p_obj, p_props, txt, p_subtitle,
1221 &subtitle_ParseSubViewerTiming,
1222 true );
1225 /* ParseSSA
1227 static int ParseSSA( vlc_object_t *p_obj, subs_properties_t *p_props,
1228 text_t *txt, subtitle_t *p_subtitle,
1229 size_t i_idx )
1231 VLC_UNUSED(p_obj);
1232 size_t header_len = 0;
1234 for( ;; )
1236 const char *s = TextGetLine( txt );
1237 int h1, m1, s1, c1, h2, m2, s2, c2;
1238 char *psz_text, *psz_temp;
1239 char temp[16];
1241 if( !s )
1242 return VLC_EGENERIC;
1244 /* We expect (SSA2-4):
1245 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1246 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1248 * SSA-1 is similar but only has 8 commas up untill the subtitle text. Probably the Effect field is no present, but not 100 % sure.
1251 /* For ASS:
1252 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1253 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1256 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1257 psz_text = malloc( strlen(s) );
1258 if( !psz_text )
1259 return VLC_ENOMEM;
1261 if( sscanf( s,
1262 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1263 temp,
1264 &h1, &m1, &s1, &c1,
1265 &h2, &m2, &s2, &c2,
1266 psz_text ) == 10 )
1268 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1269 /* (Layer comes from ASS specs ... it's empty for SSA.) */
1270 if( p_props->i_type == SUB_TYPE_SSA1 )
1272 /* SSA1 has only 8 commas before the text starts, not 9 */
1273 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1274 psz_text[0] = ',';
1276 else
1278 int i_layer = ( p_props->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1280 /* ReadOrder, Layer, %s(rest of fields) */
1281 if( asprintf( &psz_temp, "%zu,%d,%s", i_idx, i_layer, psz_text ) == -1 )
1283 free( psz_text );
1284 return VLC_ENOMEM;
1287 free( psz_text );
1288 psz_text = psz_temp;
1291 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1292 (int64_t)m1 * 60*1000 +
1293 (int64_t)s1 * 1000 +
1294 (int64_t)c1 * 10 ) * 1000;
1295 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1296 (int64_t)m2 * 60*1000 +
1297 (int64_t)s2 * 1000 +
1298 (int64_t)c2 * 10 ) * 1000;
1299 p_subtitle->psz_text = psz_text;
1300 return VLC_SUCCESS;
1302 free( psz_text );
1304 /* All the other stuff we add to the header field */
1305 if( header_len == 0 && p_props->psz_header )
1306 header_len = strlen( p_props->psz_header );
1308 size_t s_len = strlen( s );
1309 p_props->psz_header = realloc_or_free( p_props->psz_header, header_len + s_len + 2 );
1310 if( !p_props->psz_header )
1311 return VLC_ENOMEM;
1312 snprintf( p_props->psz_header + header_len, s_len + 2, "%s\n", s );
1313 header_len += s_len + 1;
1317 /* ParseVplayer
1318 * Format
1319 * h:m:s:Line1|Line2|Line3....
1320 * or
1321 * h:m:s Line1|Line2|Line3....
1323 static int ParseVplayer( vlc_object_t *p_obj, subs_properties_t *p_props,
1324 text_t *txt, subtitle_t *p_subtitle,
1325 size_t i_idx )
1327 VLC_UNUSED(p_obj);
1328 VLC_UNUSED(p_props);
1329 VLC_UNUSED( i_idx );
1330 char *psz_text;
1332 for( ;; )
1334 const char *s = TextGetLine( txt );
1335 int h1, m1, s1;
1337 if( !s )
1338 return VLC_EGENERIC;
1340 psz_text = malloc( strlen( s ) + 1 );
1341 if( !psz_text )
1342 return VLC_ENOMEM;
1344 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1345 &h1, &m1, &s1, psz_text ) == 4 )
1347 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1348 (int64_t)m1 * 60*1000 +
1349 (int64_t)s1 * 1000 ) * 1000;
1350 p_subtitle->i_stop = -1;
1351 break;
1353 free( psz_text );
1356 /* replace | by \n */
1357 for( size_t i = 0; psz_text[i] != '\0'; i++ )
1359 if( psz_text[i] == '|' )
1360 psz_text[i] = '\n';
1362 p_subtitle->psz_text = psz_text;
1363 return VLC_SUCCESS;
1366 /* ParseSami
1368 static const char *ParseSamiSearch( text_t *txt,
1369 const char *psz_start, const char *psz_str )
1371 if( psz_start && strcasestr( psz_start, psz_str ) )
1373 const char *s = strcasestr( psz_start, psz_str );
1374 return &s[strlen( psz_str )];
1377 for( ;; )
1379 const char *p = TextGetLine( txt );
1380 if( !p )
1381 return NULL;
1383 const char *s = strcasestr( p, psz_str );
1384 if( s != NULL )
1385 return &s[strlen( psz_str )];
1388 static int ParseSami( vlc_object_t *p_obj, subs_properties_t *p_props,
1389 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
1391 VLC_UNUSED(p_obj);
1392 VLC_UNUSED(p_props);
1393 VLC_UNUSED( i_idx );
1394 const char *s;
1395 int64_t i_start;
1397 unsigned int i_text;
1398 char text[8192]; /* Arbitrary but should be long enough */
1400 /* search "Start=" */
1401 s = ParseSamiSearch( txt, p_props->sami.psz_start, "Start=" );
1402 p_props->sami.psz_start = NULL;
1403 if( !s )
1404 return VLC_EGENERIC;
1406 /* get start value */
1407 char *psz_end;
1408 i_start = strtol( s, &psz_end, 0 );
1409 s = psz_end;
1411 /* search <P */
1412 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1413 return VLC_EGENERIC;
1415 /* search > */
1416 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1417 return VLC_EGENERIC;
1419 i_text = 0;
1420 text[0] = '\0';
1421 /* now get all txt until a "Start=" line */
1422 for( ;; )
1424 char c = '\0';
1425 /* Search non empty line */
1426 while( s && *s == '\0' )
1427 s = TextGetLine( txt );
1428 if( !s )
1429 break;
1431 if( *s == '<' )
1433 if( !strncasecmp( s, "<br", 3 ) )
1435 c = '\n';
1437 else if( strcasestr( s, "Start=" ) )
1439 p_props->sami.psz_start = s;
1440 break;
1442 s = ParseSamiSearch( txt, s, ">" );
1444 else if( !strncmp( s, "&nbsp;", 6 ) )
1446 c = ' ';
1447 s += 6;
1449 else if( *s == '\t' )
1451 c = ' ';
1452 s++;
1454 else
1456 c = *s;
1457 s++;
1459 if( c != '\0' && i_text+1 < sizeof(text) )
1461 text[i_text++] = c;
1462 text[i_text] = '\0';
1466 p_subtitle->i_start = i_start * 1000;
1467 p_subtitle->i_stop = -1;
1468 p_subtitle->psz_text = strdup( text );
1470 return VLC_SUCCESS;
1473 /* ParseDVDSubtitle
1474 * Format
1475 * {T h1:m1:s1:c1
1476 * Line1
1477 * Line2
1478 * ...
1480 * TODO it can have a header
1481 * { HEAD
1482 * ...
1483 * CODEPAGE=...
1484 * FORMAT=...
1485 * LANG=English
1487 * LANG support would be cool
1488 * CODEPAGE is probably mandatory FIXME
1490 static int ParseDVDSubtitle(vlc_object_t *p_obj, subs_properties_t *p_props,
1491 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
1493 VLC_UNUSED(p_obj);
1494 VLC_UNUSED(p_props);
1495 VLC_UNUSED( i_idx );
1496 char *psz_text;
1498 for( ;; )
1500 const char *s = TextGetLine( txt );
1501 int h1, m1, s1, c1;
1503 if( !s )
1504 return VLC_EGENERIC;
1506 if( sscanf( s,
1507 "{T %d:%d:%d:%d",
1508 &h1, &m1, &s1, &c1 ) == 4 )
1510 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1511 (int64_t)m1 * 60*1000 +
1512 (int64_t)s1 * 1000 +
1513 (int64_t)c1 * 10) * 1000;
1514 p_subtitle->i_stop = -1;
1515 break;
1519 /* Now read text until a line containing "}" */
1520 psz_text = strdup("");
1521 if( !psz_text )
1522 return VLC_ENOMEM;
1523 for( ;; )
1525 const char *s = TextGetLine( txt );
1526 int i_len;
1527 int i_old;
1529 if( !s )
1531 free( psz_text );
1532 return VLC_EGENERIC;
1535 i_len = strlen( s );
1536 if( i_len == 1 && s[0] == '}')
1538 p_subtitle->psz_text = psz_text;
1539 return VLC_SUCCESS;
1542 i_old = strlen( psz_text );
1543 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1544 if( !psz_text )
1545 return VLC_ENOMEM;
1546 strcat( psz_text, s );
1547 strcat( psz_text, "\n" );
1551 /* ParseMPL2
1552 * Format
1553 * [n1][n2]Line1|Line2|Line3...
1554 * where n1 and n2 are the video frame number (n2 can be empty)
1556 static int ParseMPL2(vlc_object_t *p_obj, subs_properties_t *p_props,
1557 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
1559 VLC_UNUSED(p_obj);
1560 VLC_UNUSED(p_props);
1561 VLC_UNUSED( i_idx );
1562 char *psz_text;
1563 int i;
1565 for( ;; )
1567 const char *s = TextGetLine( txt );
1568 int i_start;
1569 int i_stop;
1571 if( !s )
1572 return VLC_EGENERIC;
1574 psz_text = malloc( strlen(s) + 1 );
1575 if( !psz_text )
1576 return VLC_ENOMEM;
1578 i_start = 0;
1579 i_stop = -1;
1580 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1581 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1583 p_subtitle->i_start = (int64_t)i_start * 100000;
1584 p_subtitle->i_stop = i_stop >= 0 ? ((int64_t)i_stop * 100000) : -1;
1585 break;
1587 free( psz_text );
1590 for( i = 0; psz_text[i] != '\0'; )
1592 /* replace | by \n */
1593 if( psz_text[i] == '|' )
1594 psz_text[i] = '\n';
1596 /* Remove italic */
1597 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1598 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1599 else
1600 i++;
1602 p_subtitle->psz_text = psz_text;
1603 return VLC_SUCCESS;
1606 static int ParseAQT(vlc_object_t *p_obj, subs_properties_t *p_props, text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
1608 VLC_UNUSED(p_obj);
1609 VLC_UNUSED(p_props);
1610 VLC_UNUSED( i_idx );
1612 char *psz_text = strdup( "" );
1613 int i_old = 0;
1614 int i_firstline = 1;
1616 for( ;; )
1618 int t; /* Time */
1620 const char *s = TextGetLine( txt );
1622 if( !s )
1624 free( psz_text );
1625 return VLC_EGENERIC;
1628 /* Data Lines */
1629 if( sscanf (s, "-->> %d", &t) == 1)
1631 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1632 p_subtitle->i_stop = -1;
1634 /* Starting of a subtitle */
1635 if( i_firstline )
1637 i_firstline = 0;
1639 /* We have been too far: end of the subtitle, begin of next */
1640 else
1642 TextPreviousLine( txt );
1643 break;
1646 /* Text Lines */
1647 else
1649 i_old = strlen( psz_text ) + 1;
1650 psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1651 if( !psz_text )
1652 return VLC_ENOMEM;
1653 strcat( psz_text, s );
1654 strcat( psz_text, "\n" );
1655 if( txt->i_line == txt->i_line_count )
1656 break;
1659 p_subtitle->psz_text = psz_text;
1660 return VLC_SUCCESS;
1663 static int ParsePJS(vlc_object_t *p_obj, subs_properties_t *p_props,
1664 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
1666 VLC_UNUSED(p_obj);
1667 VLC_UNUSED(p_props);
1668 VLC_UNUSED( i_idx );
1670 char *psz_text;
1671 int i;
1673 for( ;; )
1675 const char *s = TextGetLine( txt );
1676 int t1, t2;
1678 if( !s )
1679 return VLC_EGENERIC;
1681 psz_text = malloc( strlen(s) + 1 );
1682 if( !psz_text )
1683 return VLC_ENOMEM;
1685 /* Data Lines */
1686 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1688 /* 1/10th of second ? Frame based ? FIXME */
1689 p_subtitle->i_start = 10 * t1;
1690 p_subtitle->i_stop = 10 * t2;
1691 /* Remove latest " */
1692 psz_text[ strlen(psz_text) - 1 ] = '\0';
1694 break;
1696 free( psz_text );
1699 /* replace | by \n */
1700 for( i = 0; psz_text[i] != '\0'; i++ )
1702 if( psz_text[i] == '|' )
1703 psz_text[i] = '\n';
1706 p_subtitle->psz_text = psz_text;
1707 msg_Dbg( p_obj, "%s", psz_text );
1708 return VLC_SUCCESS;
1711 static int ParseMPSub( vlc_object_t *p_obj, subs_properties_t *p_props,
1712 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
1714 VLC_UNUSED( i_idx );
1716 char *psz_text = strdup( "" );
1718 if( !p_props->mpsub.b_inited )
1720 p_props->mpsub.f_total = 0.0;
1721 p_props->mpsub.f_factor = 0.0;
1723 p_props->mpsub.b_inited = true;
1726 for( ;; )
1728 char p_dummy;
1729 char *psz_temp;
1731 const char *s = TextGetLine( txt );
1732 if( !s )
1734 free( psz_text );
1735 return VLC_EGENERIC;
1738 if( strstr( s, "FORMAT" ) )
1740 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1742 p_props->mpsub.f_factor = 100.0;
1743 break;
1746 psz_temp = malloc( strlen(s) );
1747 if( !psz_temp )
1749 free( psz_text );
1750 return VLC_ENOMEM;
1753 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1755 float f_fps = us_strtof( psz_temp, NULL );
1757 if( f_fps > 0.f && var_GetFloat( p_obj, "sub-fps" ) <= 0.f )
1758 var_SetFloat( p_obj, "sub-fps", f_fps );
1760 p_props->mpsub.f_factor = 1.f;
1761 free( psz_temp );
1762 break;
1764 free( psz_temp );
1767 /* Data Lines */
1768 float f1 = us_strtof( s, &psz_temp );
1769 if( *psz_temp )
1771 float f2 = us_strtof( psz_temp, NULL );
1772 p_props->mpsub.f_total += f1 * p_props->mpsub.f_factor;
1773 p_subtitle->i_start = llroundf(10000.f * p_props->mpsub.f_total);
1774 p_props->mpsub.f_total += f2 * p_props->mpsub.f_factor;
1775 p_subtitle->i_stop = llroundf(10000.f * p_props->mpsub.f_total);
1776 break;
1780 for( ;; )
1782 const char *s = TextGetLine( txt );
1784 if( !s )
1786 free( psz_text );
1787 return VLC_EGENERIC;
1790 size_t i_len = strlen( s );
1791 if( i_len == 0 )
1792 break;
1794 size_t i_old = strlen( psz_text );
1796 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1797 if( !psz_text )
1798 return VLC_ENOMEM;
1800 strcat( psz_text, s );
1801 strcat( psz_text, "\n" );
1804 p_subtitle->psz_text = psz_text;
1805 return VLC_SUCCESS;
1808 static int ParseJSS( vlc_object_t *p_obj, subs_properties_t *p_props,
1809 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
1811 VLC_UNUSED( i_idx );
1812 char *psz_text, *psz_orig;
1813 char *psz_text2, *psz_orig2;
1815 if( !p_props->jss.b_inited )
1817 p_props->jss.i_comment = 0;
1818 p_props->jss.i_time_resolution = 30;
1819 p_props->jss.i_time_shift = 0;
1821 p_props->jss.b_inited = true;
1824 /* Parse the main lines */
1825 for( ;; )
1827 const char *s = TextGetLine( txt );
1828 if( !s )
1829 return VLC_EGENERIC;
1831 size_t line_length = strlen( s );
1832 psz_orig = malloc( line_length + 1 );
1833 if( !psz_orig )
1834 return VLC_ENOMEM;
1835 psz_text = psz_orig;
1837 /* Complete time lines */
1838 int h1, h2, m1, m2, s1, s2, f1, f2;
1839 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1840 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1842 p_subtitle->i_start = ( ( (int64_t) h1 *3600 + m1 * 60 + s1 ) +
1843 (int64_t)( ( f1 + p_props->jss.i_time_shift ) / p_props->jss.i_time_resolution ) )
1844 * 1000000;
1845 p_subtitle->i_stop = ( ( (int64_t) h2 *3600 + m2 * 60 + s2 ) +
1846 (int64_t)( ( f2 + p_props->jss.i_time_shift ) / p_props->jss.i_time_resolution ) )
1847 * 1000000;
1848 break;
1850 /* Short time lines */
1851 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1853 p_subtitle->i_start = ((int64_t)
1854 ( f1 + p_props->jss.i_time_shift ) / p_props->jss.i_time_resolution * 1000000.0 );
1855 p_subtitle->i_stop = ((int64_t)
1856 ( f2 + p_props->jss.i_time_shift ) / p_props->jss.i_time_resolution * 1000000.0 );
1857 break;
1859 /* General Directive lines */
1860 /* Only TIME and SHIFT are supported so far */
1861 else if( s[0] == '#' )
1863 int h = 0, m =0, sec = 1, f = 1;
1864 unsigned shift = 1;
1865 int inv = 1;
1867 strcpy( psz_text, s );
1869 switch( toupper( (unsigned char)psz_text[1] ) )
1871 case 'S':
1872 shift = isalpha( (unsigned char)psz_text[2] ) ? 6 : 2 ;
1873 if ( shift > line_length )
1874 break;
1876 if( sscanf( &psz_text[shift], "%d", &h ) )
1878 /* Negative shifting */
1879 if( h < 0 )
1881 h *= -1;
1882 inv = -1;
1885 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1887 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1889 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1891 else
1893 h = 0;
1894 sscanf( &psz_text[shift], "%d:%d.%d",
1895 &m, &sec, &f );
1896 m *= inv;
1899 else
1901 h = m = 0;
1902 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1903 sec *= inv;
1905 p_props->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1906 * p_props->jss.i_time_resolution + f ) * inv;
1908 break;
1910 case 'T':
1911 shift = isalpha( (unsigned char)psz_text[2] ) ? 8 : 2 ;
1912 if ( shift > line_length )
1913 break;
1915 sscanf( &psz_text[shift], "%d", &p_props->jss.i_time_resolution );
1916 if( !p_props->jss.i_time_resolution )
1917 p_props->jss.i_time_resolution = 30;
1918 break;
1920 free( psz_orig );
1921 continue;
1923 else
1924 /* Unkown type line, probably a comment */
1926 free( psz_orig );
1927 continue;
1931 while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1933 const char *s2 = TextGetLine( txt );
1935 if( !s2 )
1937 free( psz_orig );
1938 return VLC_EGENERIC;
1941 size_t i_len = strlen( s2 );
1942 if( i_len == 0 )
1943 break;
1945 size_t i_old = strlen( psz_text );
1947 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1948 if( !psz_text )
1949 return VLC_ENOMEM;
1951 psz_orig = psz_text;
1952 strcat( psz_text, s2 );
1955 /* Skip the blanks */
1956 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1958 /* Parse the directives */
1959 if( isalpha( (unsigned char)*psz_text ) || *psz_text == '[' )
1961 while( *psz_text && *psz_text != ' ' )
1962 ++psz_text;
1964 /* Directives are NOT parsed yet */
1965 /* This has probably a better place in a decoder ? */
1966 /* directive = malloc( strlen( psz_text ) + 1 );
1967 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1970 /* Skip the blanks after directives */
1971 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1973 /* Clean all the lines from inline comments and other stuffs */
1974 psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1975 psz_text2 = psz_orig2;
1977 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1979 switch( *psz_text )
1981 case '{':
1982 p_props->jss.i_comment++;
1983 break;
1984 case '}':
1985 if( p_props->jss.i_comment )
1987 p_props->jss.i_comment = 0;
1988 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1990 break;
1991 case '~':
1992 if( !p_props->jss.i_comment )
1994 *psz_text2 = ' ';
1995 psz_text2++;
1997 break;
1998 case ' ':
1999 case '\t':
2000 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
2001 break;
2002 if( !p_props->jss.i_comment )
2004 *psz_text2 = ' ';
2005 psz_text2++;
2007 break;
2008 case '\\':
2009 if( (*(psz_text + 1 ) ) == 'n' )
2011 *psz_text2 = '\n';
2012 psz_text++;
2013 psz_text2++;
2014 break;
2016 if( ( toupper((unsigned char)*(psz_text + 1 ) ) == 'C' ) ||
2017 ( toupper((unsigned char)*(psz_text + 1 ) ) == 'F' ) )
2019 psz_text++;
2020 break;
2022 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
2023 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
2024 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
2025 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
2027 psz_text++;
2028 break;
2030 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
2031 (*(psz_text + 1 ) ) == '\\' )
2032 psz_text++;
2033 else if( ( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n' ) &&
2034 *(psz_text + 1 ) != '\0' )
2036 psz_text++;
2038 break;
2039 default:
2040 if( !p_props->jss.i_comment )
2042 *psz_text2 = *psz_text;
2043 psz_text2++;
2046 psz_text++;
2049 p_subtitle->psz_text = psz_orig2;
2050 msg_Dbg( p_obj, "%s", p_subtitle->psz_text );
2051 free( psz_orig );
2052 return VLC_SUCCESS;
2055 static int ParsePSB( vlc_object_t *p_obj, subs_properties_t *p_props,
2056 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
2058 VLC_UNUSED(p_obj);
2059 VLC_UNUSED(p_props);
2060 VLC_UNUSED( i_idx );
2062 char *psz_text;
2063 int i;
2065 for( ;; )
2067 int h1, m1, s1;
2068 int h2, m2, s2;
2069 const char *s = TextGetLine( txt );
2071 if( !s )
2072 return VLC_EGENERIC;
2074 psz_text = malloc( strlen( s ) + 1 );
2075 if( !psz_text )
2076 return VLC_ENOMEM;
2078 if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
2079 &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
2081 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2082 (int64_t)m1 * 60*1000 +
2083 (int64_t)s1 * 1000 ) * 1000;
2084 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2085 (int64_t)m2 * 60*1000 +
2086 (int64_t)s2 * 1000 ) * 1000;
2087 break;
2089 free( psz_text );
2092 /* replace | by \n */
2093 for( i = 0; psz_text[i] != '\0'; i++ )
2095 if( psz_text[i] == '|' )
2096 psz_text[i] = '\n';
2098 p_subtitle->psz_text = psz_text;
2099 return VLC_SUCCESS;
2102 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
2104 if( *psz == '\0' ) return 0;
2105 if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
2106 sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
2107 sscanf( psz, "%d.%d", s, f ) == 2 ||
2108 sscanf( psz, "%d:%d", m, s ) == 2 ||
2109 sscanf( psz, "%d", s ) == 1 )
2111 return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
2112 + (int64_t)*f * 10 * 1000;
2114 else return VLC_EGENERIC;
2117 static int ParseRealText( vlc_object_t *p_obj, subs_properties_t *p_props,
2118 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
2120 VLC_UNUSED(p_obj);
2121 VLC_UNUSED(p_props);
2122 VLC_UNUSED( i_idx );
2123 char *psz_text = NULL;
2125 for( ;; )
2127 int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
2128 int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
2129 const char *s = TextGetLine( txt );
2130 free( psz_text );
2132 if( !s )
2133 return VLC_EGENERIC;
2135 psz_text = malloc( strlen( s ) + 1 );
2136 if( !psz_text )
2137 return VLC_ENOMEM;
2139 /* Find the good begining. This removes extra spaces at the beginning
2140 of the line.*/
2141 char *psz_temp = strcasestr( s, "<time");
2142 if( psz_temp != NULL )
2144 char psz_end[12], psz_begin[12];
2145 /* Line has begin and end */
2146 if( ( sscanf( psz_temp,
2147 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
2148 psz_begin, psz_end, psz_text) != 3 ) &&
2149 /* Line has begin and no end */
2150 ( sscanf( psz_temp,
2151 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
2152 psz_begin, psz_text ) != 2) )
2153 /* Line is not recognized */
2155 continue;
2158 /* Get the times */
2159 int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
2160 p_subtitle->i_start = i_time >= 0 ? i_time : 0;
2162 i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
2163 p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
2164 break;
2168 /* Get the following Lines */
2169 for( ;; )
2171 const char *s = TextGetLine( txt );
2173 if( !s )
2175 free( psz_text );
2176 return VLC_EGENERIC;
2179 size_t i_len = strlen( s );
2180 if( i_len == 0 ) break;
2182 if( strcasestr( s, "<time" ) ||
2183 strcasestr( s, "<clear/") )
2185 TextPreviousLine( txt );
2186 break;
2189 size_t i_old = strlen( psz_text );
2191 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
2192 if( !psz_text )
2193 return VLC_ENOMEM;
2195 strcat( psz_text, s );
2196 strcat( psz_text, "\n" );
2199 /* Remove the starting ">" that remained after the sscanf */
2200 memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
2202 p_subtitle->psz_text = psz_text;
2204 return VLC_SUCCESS;
2207 static int ParseDKS( vlc_object_t *p_obj, subs_properties_t *p_props,
2208 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
2210 VLC_UNUSED(p_obj);
2211 VLC_UNUSED(p_props);
2212 VLC_UNUSED( i_idx );
2214 char *psz_text;
2216 for( ;; )
2218 int h1, m1, s1;
2219 int h2, m2, s2;
2220 char *s = TextGetLine( txt );
2222 if( !s )
2223 return VLC_EGENERIC;
2225 psz_text = malloc( strlen( s ) + 1 );
2226 if( !psz_text )
2227 return VLC_ENOMEM;
2229 if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
2230 &h1, &m1, &s1, psz_text ) == 4 )
2232 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2233 (int64_t)m1 * 60*1000 +
2234 (int64_t)s1 * 1000 ) * 1000;
2236 s = TextGetLine( txt );
2237 if( !s )
2239 free( psz_text );
2240 return VLC_EGENERIC;
2243 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2244 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2245 (int64_t)m2 * 60*1000 +
2246 (int64_t)s2 * 1000 ) * 1000;
2247 else
2248 p_subtitle->i_stop = -1;
2249 break;
2251 free( psz_text );
2254 /* replace [br] by \n */
2255 char *p;
2256 while( ( p = strstr( psz_text, "[br]" ) ) )
2258 *p++ = '\n';
2259 memmove( p, &p[3], strlen(&p[3])+1 );
2262 p_subtitle->psz_text = psz_text;
2263 return VLC_SUCCESS;
2266 static int ParseSubViewer1( vlc_object_t *p_obj, subs_properties_t *p_props,
2267 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
2269 VLC_UNUSED(p_obj);
2270 VLC_UNUSED(p_props);
2271 VLC_UNUSED( i_idx );
2272 char *psz_text;
2274 for( ;; )
2276 int h1, m1, s1;
2277 int h2, m2, s2;
2278 char *s = TextGetLine( txt );
2280 if( !s )
2281 return VLC_EGENERIC;
2283 if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2285 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2286 (int64_t)m1 * 60*1000 +
2287 (int64_t)s1 * 1000 ) * 1000;
2289 s = TextGetLine( txt );
2290 if( !s )
2291 return VLC_EGENERIC;
2293 psz_text = strdup( s );
2294 if( !psz_text )
2295 return VLC_ENOMEM;
2297 s = TextGetLine( txt );
2298 if( !s )
2300 free( psz_text );
2301 return VLC_EGENERIC;
2304 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2305 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2306 (int64_t)m2 * 60*1000 +
2307 (int64_t)s2 * 1000 ) * 1000;
2308 else
2309 p_subtitle->i_stop = -1;
2311 break;
2315 p_subtitle->psz_text = psz_text;
2317 return VLC_SUCCESS;
2320 static int ParseCommonSBV( vlc_object_t *p_obj, subs_properties_t *p_props,
2321 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
2323 VLC_UNUSED(p_obj);
2324 VLC_UNUSED( i_idx );
2325 VLC_UNUSED( p_props );
2326 char *psz_text;
2328 for( ;; )
2330 const char *s = TextGetLine( txt );
2331 int h1 = 0, m1 = 0, s1 = 0, d1 = 0;
2332 int h2 = 0, m2 = 0, s2 = 0, d2 = 0;
2334 if( !s )
2335 return VLC_EGENERIC;
2337 if( sscanf( s,"%d:%d:%d.%d,%d:%d:%d.%d",
2338 &h1, &m1, &s1, &d1,
2339 &h2, &m2, &s2, &d2 ) == 8 )
2341 p_subtitle->i_start = ( (int64_t)h1 * 3600 * 1000 +
2342 (int64_t)m1 * 60 * 1000 +
2343 (int64_t)s1 * 1000 +
2344 (int64_t)d1 ) * 1000;
2346 p_subtitle->i_stop = ( (int64_t)h2 * 3600 * 1000 +
2347 (int64_t)m2 * 60 * 1000 +
2348 (int64_t)s2 * 1000 +
2349 (int64_t)d2 ) * 1000;
2350 if( p_subtitle->i_start < p_subtitle->i_stop )
2351 break;
2355 /* Now read text until an empty line */
2356 psz_text = strdup("");
2357 if( !psz_text )
2358 return VLC_ENOMEM;
2360 for( ;; )
2362 const char *s = TextGetLine( txt );
2363 size_t i_len;
2364 size_t i_old;
2366 i_len = s ? strlen( s ) : 0;
2367 if( i_len <= 0 )
2369 p_subtitle->psz_text = psz_text;
2370 return VLC_SUCCESS;
2373 i_old = strlen( psz_text );
2374 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
2375 if( !psz_text )
2376 return VLC_ENOMEM;
2378 strcat( psz_text, s );
2379 strcat( psz_text, "\n" );
2383 static int ParseSCC( vlc_object_t *p_obj, subs_properties_t *p_props,
2384 text_t *txt, subtitle_t *p_subtitle, size_t i_idx )
2386 VLC_UNUSED(p_obj);
2387 VLC_UNUSED( i_idx );
2388 VLC_UNUSED( p_props );
2390 static const struct rates
2392 unsigned val;
2393 vlc_rational_t rate;
2394 bool b_drop_allowed;
2395 } framerates[] = {
2396 { 2398, { 24000, 1001 }, false },
2397 { 2400, { 24, 1 }, false },
2398 { 2500, { 25, 1 }, false },
2399 { 2997, { 30000, 1001 }, true }, /* encoding rate */
2400 { 3000, { 30, 1 }, false },
2401 { 5000, { 50, 1 }, false },
2402 { 5994, { 60000, 1001 }, true },
2403 { 6000, { 60, 1 }, false },
2405 const struct rates *p_rate = &framerates[3];
2406 float f_fps = var_GetFloat( p_obj, "sub-fps" );
2407 if( f_fps > 1.0 )
2409 for( size_t i=0; i<ARRAY_SIZE(framerates); i++ )
2411 if( (unsigned)(f_fps * 100) == framerates[i].val )
2413 p_rate = &framerates[i];
2414 break;
2419 for( ;; )
2421 const char *psz_line = TextGetLine( txt );
2422 if( !psz_line )
2423 return VLC_EGENERIC;
2425 unsigned h, m, s, f;
2426 char c;
2427 if( sscanf( psz_line, "%u:%u:%u%[:;]%u ", &h, &m, &s, &c, &f ) != 5 )
2428 continue;
2430 /* convert everything to seconds */
2431 mtime_t i_frames = h * 3600 + m * 60 + s;
2433 if( c == ';' && p_rate->b_drop_allowed ) /* dropframe */
2435 /* convert to frame # to be accurate between inter drop drift
2436 * of 18 frames see http://andrewduncan.net/timecodes/ */
2437 const unsigned i_mins = h * 60 + m;
2438 i_frames = i_frames * p_rate[+1].rate.num + f
2439 - (p_rate[+1].rate.den * 2 * (i_mins - i_mins % 10));
2441 else
2443 /* convert to frame # at 29.97 */
2444 i_frames = i_frames * framerates[3].rate.num / framerates[3].rate.den + f;
2446 p_subtitle->i_start = VLC_TS_0 + i_frames * CLOCK_FREQ *
2447 p_rate->rate.den / p_rate->rate.num;
2448 p_subtitle->i_stop = -1;
2450 const char *psz_text = strchr( psz_line, '\t' );
2451 if( !psz_text && !(psz_text = strchr( psz_line, ' ' )) )
2452 continue;
2454 if ( psz_text[1] == '\0' )
2455 continue;
2457 p_subtitle->psz_text = strdup( psz_text + 1 );
2458 if( !p_subtitle->psz_text )
2459 return VLC_ENOMEM;
2461 break;
2464 return VLC_SUCCESS;
2467 /* Matches filename.xx.srt */
2468 static char * get_language_from_filename( const char * psz_sub_file )
2470 char *psz_ret = NULL;
2471 char *psz_tmp, *psz_language_begin;
2473 if( !psz_sub_file ) return NULL;
2474 char *psz_work = strdup( psz_sub_file );
2476 /* Removing extension, but leaving the dot */
2477 psz_tmp = strrchr( psz_work, '.' );
2478 if( psz_tmp )
2480 psz_tmp[0] = '\0';
2481 psz_language_begin = strrchr( psz_work, '.' );
2482 if( psz_language_begin )
2483 psz_ret = strdup(++psz_language_begin);
2484 psz_tmp[0] = '.';
2487 free( psz_work );
2488 return psz_ret;