]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
d8b6dc51504ee380b3425433c8af9bb77217e670
[vlc] / modules / demux / subtitle.c
1 /*****************************************************************************
2  * subtitle.c: Demux for subtitle text files.
3  *****************************************************************************
4  * Copyright (C) 1999-2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *          Jean-Baptiste Kempf <jb@videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_input.h>
37 #include <vlc_memory.h>
38
39 #include <ctype.h>
40 #include <math.h>
41
42 #include <vlc_demux.h>
43 #include <vlc_charset.h>
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 static int  Open ( vlc_object_t *p_this );
49 static void Close( vlc_object_t *p_this );
50
51 #define SUB_DELAY_LONGTEXT \
52     N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
53 #define SUB_FPS_LONGTEXT \
54     N_("Override the normal frames per second settings. " \
55     "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
56 #define SUB_TYPE_LONGTEXT \
57     N_("Force the subtiles format. Selecting \"auto\" means autodetection and should always work.")
58 #define SUB_DESCRIPTION_LONGTEXT \
59     N_("Override the default track description.")
60
61 static const char *const ppsz_sub_type[] =
62 {
63     "auto", "microdvd", "subrip", "subviewer", "ssa1",
64     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
65     "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
66     "subviewer1","vtt"
67 };
68
69 vlc_module_begin ()
70     set_shortname( N_("Subtitles"))
71     set_description( N_("Text subtitle parser") )
72     set_capability( "demux", 0 )
73     set_category( CAT_INPUT )
74     set_subcategory( SUBCAT_INPUT_DEMUX )
75     add_float( "sub-fps", 0.0,
76                N_("Frames per Second"),
77                SUB_FPS_LONGTEXT, true )
78     add_integer( "sub-delay", 0,
79                N_("Subtitle delay"),
80                SUB_DELAY_LONGTEXT, true )
81     add_string( "sub-type", "auto", N_("Subtitle format"),
82                 SUB_TYPE_LONGTEXT, true )
83         change_string_list( ppsz_sub_type, ppsz_sub_type )
84     add_string( "sub-description", NULL, N_("Subtitle description"),
85                 SUB_DESCRIPTION_LONGTEXT, true )
86     set_callbacks( Open, Close )
87
88     add_shortcut( "subtitle" )
89 vlc_module_end ()
90
91 /*****************************************************************************
92  * Prototypes:
93  *****************************************************************************/
94 enum
95 {
96     SUB_TYPE_UNKNOWN = -1,
97     SUB_TYPE_MICRODVD,
98     SUB_TYPE_SUBRIP,
99     SUB_TYPE_SSA1,
100     SUB_TYPE_SSA2_4,
101     SUB_TYPE_ASS,
102     SUB_TYPE_VPLAYER,
103     SUB_TYPE_SAMI,
104     SUB_TYPE_SUBVIEWER, /* SUBVIEWER 2 */
105     SUB_TYPE_DVDSUBTITLE, /* Mplayer calls it subviewer2 */
106     SUB_TYPE_MPL2,
107     SUB_TYPE_AQT,
108     SUB_TYPE_PJS,
109     SUB_TYPE_MPSUB,
110     SUB_TYPE_JACOSUB,
111     SUB_TYPE_PSB,
112     SUB_TYPE_RT,
113     SUB_TYPE_DKS,
114     SUB_TYPE_SUBVIEW1, /* SUBVIEWER 1 - mplayer calls it subrip09,
115                          and Gnome subtitles SubViewer 1.0 */
116     SUB_TYPE_VTT
117 };
118
119 typedef struct
120 {
121     int     i_line_count;
122     int     i_line;
123     char    **line;
124 } text_t;
125
126 static int  TextLoad( text_t *, stream_t *s );
127 static void TextUnload( text_t * );
128
129 typedef struct
130 {
131     int64_t i_start;
132     int64_t i_stop;
133
134     char    *psz_text;
135 } subtitle_t;
136
137
138 struct demux_sys_t
139 {
140     int         i_type;
141     text_t      txt;
142     es_out_id_t *es;
143
144     int64_t     i_next_demux_date;
145     int64_t     i_microsecperframe;
146
147     char        *psz_header;
148     int         i_subtitle;
149     int         i_subtitles;
150     subtitle_t  *subtitle;
151
152     int64_t     i_length;
153
154     /* */
155     struct
156     {
157         bool b_inited;
158
159         int i_comment;
160         int i_time_resolution;
161         int i_time_shift;
162     } jss;
163     struct
164     {
165         bool  b_inited;
166
167         float f_total;
168         float f_factor;
169     } mpsub;
170 };
171
172 static int  ParseMicroDvd   ( demux_t *, subtitle_t *, int );
173 static int  ParseSubRip     ( demux_t *, subtitle_t *, int );
174 static int  ParseSubViewer  ( demux_t *, subtitle_t *, int );
175 static int  ParseSSA        ( demux_t *, subtitle_t *, int );
176 static int  ParseVplayer    ( demux_t *, subtitle_t *, int );
177 static int  ParseSami       ( demux_t *, subtitle_t *, int );
178 static int  ParseDVDSubtitle( demux_t *, subtitle_t *, int );
179 static int  ParseMPL2       ( demux_t *, subtitle_t *, int );
180 static int  ParseAQT        ( demux_t *, subtitle_t *, int );
181 static int  ParsePJS        ( demux_t *, subtitle_t *, int );
182 static int  ParseMPSub      ( demux_t *, subtitle_t *, int );
183 static int  ParseJSS        ( demux_t *, subtitle_t *, int );
184 static int  ParsePSB        ( demux_t *, subtitle_t *, int );
185 static int  ParseRealText   ( demux_t *, subtitle_t *, int );
186 static int  ParseDKS        ( demux_t *, subtitle_t *, int );
187 static int  ParseSubViewer1 ( demux_t *, subtitle_t *, int );
188 static int  ParseVTT        ( demux_t *, subtitle_t *, int );
189
190 static const struct
191 {
192     const char *psz_type_name;
193     int  i_type;
194     const char *psz_name;
195     int  (*pf_read)( demux_t *, subtitle_t*, int );
196 } sub_read_subtitle_function [] =
197 {
198     { "microdvd",   SUB_TYPE_MICRODVD,    "MicroDVD",    ParseMicroDvd },
199     { "subrip",     SUB_TYPE_SUBRIP,      "SubRIP",      ParseSubRip },
200     { "subviewer",  SUB_TYPE_SUBVIEWER,   "SubViewer",   ParseSubViewer },
201     { "ssa1",       SUB_TYPE_SSA1,        "SSA-1",       ParseSSA },
202     { "ssa2-4",     SUB_TYPE_SSA2_4,      "SSA-2/3/4",   ParseSSA },
203     { "ass",        SUB_TYPE_ASS,         "SSA/ASS",     ParseSSA },
204     { "vplayer",    SUB_TYPE_VPLAYER,     "VPlayer",     ParseVplayer },
205     { "sami",       SUB_TYPE_SAMI,        "SAMI",        ParseSami },
206     { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
207     { "mpl2",       SUB_TYPE_MPL2,        "MPL2",        ParseMPL2 },
208     { "aqt",        SUB_TYPE_AQT,         "AQTitle",     ParseAQT },
209     { "pjs",        SUB_TYPE_PJS,         "PhoenixSub",  ParsePJS },
210     { "mpsub",      SUB_TYPE_MPSUB,       "MPSub",       ParseMPSub },
211     { "jacosub",    SUB_TYPE_JACOSUB,     "JacoSub",     ParseJSS },
212     { "psb",        SUB_TYPE_PSB,         "PowerDivx",   ParsePSB },
213     { "realtext",   SUB_TYPE_RT,          "RealText",    ParseRealText },
214     { "dks",        SUB_TYPE_DKS,         "DKS",         ParseDKS },
215     { "subviewer1", SUB_TYPE_SUBVIEW1,    "Subviewer 1", ParseSubViewer1 },
216     { "text/vtt",   SUB_TYPE_VTT,         "WebVTT",      ParseVTT },
217     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
218 };
219 /* When adding support for more formats, be sure to add their file extension
220  * to src/input/subtitles.c to enable auto-detection.
221  */
222
223 static int Demux( demux_t * );
224 static int Control( demux_t *, int, va_list );
225
226 static void Fix( demux_t * );
227 static char * get_language_from_filename( const char * );
228
229 /*****************************************************************************
230  * Module initializer
231  *****************************************************************************/
232 static int Open ( vlc_object_t *p_this )
233 {
234     demux_t        *p_demux = (demux_t*)p_this;
235     demux_sys_t    *p_sys;
236     es_format_t    fmt;
237     float          f_fps;
238     char           *psz_type;
239     int  (*pf_read)( demux_t *, subtitle_t*, int );
240     int            i, i_max;
241
242     if( !p_demux->b_force )
243     {
244         msg_Dbg( p_demux, "subtitle demux discarded" );
245         return VLC_EGENERIC;
246     }
247
248     p_demux->pf_demux = Demux;
249     p_demux->pf_control = Control;
250     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
251     if( p_sys == NULL )
252         return VLC_ENOMEM;
253
254     p_sys->psz_header         = NULL;
255     p_sys->i_subtitle         = 0;
256     p_sys->i_subtitles        = 0;
257     p_sys->subtitle           = NULL;
258     p_sys->i_microsecperframe = 40000;
259
260     p_sys->jss.b_inited       = false;
261     p_sys->mpsub.b_inited     = false;
262
263     /* Get the FPS */
264     f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
265     if( f_fps >= 1.f )
266         p_sys->i_microsecperframe = llroundf( 1000000.f / f_fps );
267
268     msg_Dbg( p_demux, "Movie fps: %f", (double) f_fps );
269
270     /* Check for override of the fps */
271     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
272     if( f_fps >= 1.f )
273     {
274         p_sys->i_microsecperframe = llroundf( 1000000.f / f_fps );
275         msg_Dbg( p_demux, "Override subtitle fps %f", (double) f_fps );
276     }
277
278     /* Get or probe the type */
279     p_sys->i_type = SUB_TYPE_UNKNOWN;
280     psz_type = var_CreateGetString( p_demux, "sub-type" );
281     if( psz_type && *psz_type )
282     {
283         int i;
284
285         for( i = 0; ; i++ )
286         {
287             if( sub_read_subtitle_function[i].psz_type_name == NULL )
288                 break;
289
290             if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
291                          psz_type ) )
292             {
293                 p_sys->i_type = sub_read_subtitle_function[i].i_type;
294                 break;
295             }
296         }
297     }
298     free( psz_type );
299
300     /* Detect Unicode while skipping the UTF-8 Byte Order Mark */
301     bool unicode = false;
302     const uint8_t *p_data;
303     if( stream_Peek( p_demux->s, &p_data, 3 ) >= 3
304      && !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
305     {
306         unicode = true;
307         stream_Seek( p_demux->s, 3 ); /* skip BOM */
308         msg_Dbg( p_demux, "detected Unicode Byte Order Mark" );
309     }
310
311     /* Probe if unknown type */
312     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
313     {
314         int     i_try;
315         char    *s = NULL;
316
317         msg_Dbg( p_demux, "autodetecting subtitle format" );
318         for( i_try = 0; i_try < 256; i_try++ )
319         {
320             int i_dummy;
321             char p_dummy;
322
323             if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
324                 break;
325
326             if( strcasestr( s, "<SAMI>" ) )
327             {
328                 p_sys->i_type = SUB_TYPE_SAMI;
329                 break;
330             }
331             else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
332                      sscanf( s, "{%d}{}", &i_dummy ) == 1)
333             {
334                 p_sys->i_type = SUB_TYPE_MICRODVD;
335                 break;
336             }
337             else if( sscanf( s, "%d:%d:%d,%d --> %d:%d:%d,%d",
338                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
339                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
340                      sscanf( s, "%d:%d:%d --> %d:%d:%d,%d",
341                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
342                              &i_dummy,&i_dummy,&i_dummy ) == 7 ||
343                      sscanf( s, "%d:%d:%d,%d --> %d:%d:%d",
344                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
345                              &i_dummy,&i_dummy,&i_dummy ) == 7 ||
346                      sscanf( s, "%d:%d:%d.%d --> %d:%d:%d.%d",
347                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
348                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
349                      sscanf( s, "%d:%d:%d --> %d:%d:%d.%d",
350                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
351                              &i_dummy,&i_dummy,&i_dummy ) == 7 ||
352                      sscanf( s, "%d:%d:%d.%d --> %d:%d:%d",
353                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
354                              &i_dummy,&i_dummy,&i_dummy ) == 7 ||
355                      sscanf( s, "%d:%d:%d --> %d:%d:%d",
356                              &i_dummy,&i_dummy,&i_dummy,
357                              &i_dummy,&i_dummy,&i_dummy ) == 6 )
358             {
359                 p_sys->i_type = SUB_TYPE_SUBRIP;
360                 break;
361             }
362             else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
363             {
364                 p_sys->i_type = SUB_TYPE_SSA1;
365                 break;
366             }
367             else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
368             {
369                 p_sys->i_type = SUB_TYPE_ASS;
370                 break;
371             }
372             else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
373             {
374                 p_sys->i_type = SUB_TYPE_SSA2_4;
375                 break;
376             }
377             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
378             {
379                 p_sys->i_type = SUB_TYPE_SSA2_4;
380                 break;
381             }
382             else if( !strncasecmp( s, "Dialogue:", 9  ) )
383             {
384                 p_sys->i_type = SUB_TYPE_ASS;
385                 break;
386             }
387             else if( strcasestr( s, "[INFORMATION]" ) )
388             {
389                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
390                 break;
391             }
392             else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
393                                  &i_dummy, &i_dummy, &i_dummy, &i_dummy,
394                                  &i_dummy, &i_dummy, &i_dummy ) == 7 ||
395                      sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
396             {
397                 p_sys->i_type = SUB_TYPE_JACOSUB;
398                 break;
399             }
400             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
401                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
402             {
403                 p_sys->i_type = SUB_TYPE_VPLAYER;
404                 break;
405             }
406             else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
407                              &i_dummy, &i_dummy ) == 4 )
408             {
409                 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
410                 break;
411             }
412             else if( sscanf( s, "[%d:%d:%d]%c",
413                      &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
414             {
415                 p_sys->i_type = SUB_TYPE_DKS;
416                 break;
417             }
418             else if( strstr( s, "*** START SCRIPT" ) )
419             {
420                 p_sys->i_type = SUB_TYPE_SUBVIEW1;
421                 break;
422             }
423             else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
424                      sscanf( s, "[%d][]", &i_dummy ) == 1)
425             {
426                 p_sys->i_type = SUB_TYPE_MPL2;
427                 break;
428             }
429             else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
430                      ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
431                        && p_dummy =='E' ) )
432             {
433                 p_sys->i_type = SUB_TYPE_MPSUB;
434                 break;
435             }
436             else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
437             {
438                 p_sys->i_type = SUB_TYPE_AQT;
439                 break;
440             }
441             else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
442             {
443                 p_sys->i_type = SUB_TYPE_PJS;
444                 break;
445             }
446             else if( sscanf( s, "{%d:%d:%d}",
447                                 &i_dummy, &i_dummy, &i_dummy ) == 3 )
448             {
449                 p_sys->i_type = SUB_TYPE_PSB;
450                 break;
451             }
452             else if( strcasestr( s, "<time" ) )
453             {
454                 p_sys->i_type = SUB_TYPE_RT;
455                 break;
456             }
457             else if( !strncasecmp( s, "WEBVTT",6 ) )
458             {
459                 p_sys->i_type = SUB_TYPE_VTT;
460                 break;
461             }
462
463             free( s );
464             s = NULL;
465         }
466
467         free( s );
468
469         /* It will nearly always work even for non seekable stream thanks the
470          * caching system, and if it fails we lose just a few sub */
471         if( stream_Seek( p_demux->s, unicode ? 3 : 0 ) )
472             msg_Warn( p_demux, "failed to rewind" );
473     }
474
475     /* Quit on unknown subtitles */
476     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
477     {
478         stream_Seek( p_demux->s, 0 );
479         msg_Warn( p_demux, "failed to recognize subtitle type" );
480         free( p_sys );
481         return VLC_EGENERIC;
482     }
483
484     for( i = 0; ; i++ )
485     {
486         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
487         {
488             msg_Dbg( p_demux, "detected %s format",
489                      sub_read_subtitle_function[i].psz_name );
490             pf_read = sub_read_subtitle_function[i].pf_read;
491             break;
492         }
493     }
494
495     msg_Dbg( p_demux, "loading all subtitles..." );
496
497     /* Load the whole file */
498     TextLoad( &p_sys->txt, p_demux->s );
499
500     /* Parse it */
501     for( i_max = 0;; )
502     {
503         if( p_sys->i_subtitles >= i_max )
504         {
505             i_max += 500;
506             if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
507                                               sizeof(subtitle_t) * i_max ) ) )
508             {
509                 TextUnload( &p_sys->txt );
510                 free( p_sys );
511                 return VLC_ENOMEM;
512             }
513         }
514
515         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
516                      p_sys->i_subtitles ) )
517             break;
518
519         p_sys->i_subtitles++;
520     }
521     /* Unload */
522     TextUnload( &p_sys->txt );
523
524     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
525
526     /* Fix subtitle (order and time) *** */
527     p_sys->i_subtitle = 0;
528     p_sys->i_length = 0;
529     if( p_sys->i_subtitles > 0 )
530     {
531         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
532         /* +1 to avoid 0 */
533         if( p_sys->i_length <= 0 )
534             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
535     }
536
537     /* *** add subtitle ES *** */
538     if( p_sys->i_type == SUB_TYPE_SSA1 ||
539              p_sys->i_type == SUB_TYPE_SSA2_4 ||
540              p_sys->i_type == SUB_TYPE_ASS )
541     {
542         Fix( p_demux );
543         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
544     }
545     else
546         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
547
548     /* Stupid language detection in the filename */
549     char * psz_language = get_language_from_filename( p_demux->psz_file );
550
551     if( psz_language )
552     {
553         fmt.psz_language = psz_language;
554         msg_Dbg( p_demux, "detected language %s of subtitle: %s", psz_language,
555                  p_demux->psz_location );
556     }
557
558     if( unicode )
559         fmt.subs.psz_encoding = strdup( "UTF-8" );
560     char *psz_description = var_InheritString( p_demux, "sub-description" );
561     if( psz_description && *psz_description )
562         fmt.psz_description = psz_description;
563     else
564         free( psz_description );
565     if( p_sys->psz_header != NULL )
566     {
567         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
568         fmt.p_extra = strdup( p_sys->psz_header );
569     }
570     p_sys->es = es_out_Add( p_demux->out, &fmt );
571     es_format_Clean( &fmt );
572
573     return VLC_SUCCESS;
574 }
575
576 /*****************************************************************************
577  * Close: Close subtitle demux
578  *****************************************************************************/
579 static void Close( vlc_object_t *p_this )
580 {
581     demux_t *p_demux = (demux_t*)p_this;
582     demux_sys_t *p_sys = p_demux->p_sys;
583     int i;
584
585     for( i = 0; i < p_sys->i_subtitles; i++ )
586         free( p_sys->subtitle[i].psz_text );
587     free( p_sys->subtitle );
588     free( p_sys->psz_header );
589
590     free( p_sys );
591 }
592
593 /*****************************************************************************
594  * Control:
595  *****************************************************************************/
596 static int Control( demux_t *p_demux, int i_query, va_list args )
597 {
598     demux_sys_t *p_sys = p_demux->p_sys;
599     int64_t *pi64, i64;
600     double *pf, f;
601
602     switch( i_query )
603     {
604         case DEMUX_GET_LENGTH:
605             pi64 = (int64_t*)va_arg( args, int64_t * );
606             *pi64 = p_sys->i_length;
607             return VLC_SUCCESS;
608
609         case DEMUX_GET_TIME:
610             pi64 = (int64_t*)va_arg( args, int64_t * );
611             if( p_sys->i_subtitle < p_sys->i_subtitles )
612             {
613                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
614                 return VLC_SUCCESS;
615             }
616             return VLC_EGENERIC;
617
618         case DEMUX_SET_TIME:
619             i64 = (int64_t)va_arg( args, int64_t );
620             p_sys->i_subtitle = 0;
621             while( p_sys->i_subtitle < p_sys->i_subtitles )
622             {
623                 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
624
625                 if( p_subtitle->i_start > i64 )
626                     break;
627                 if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
628                     break;
629
630                 p_sys->i_subtitle++;
631             }
632
633             if( p_sys->i_subtitle >= p_sys->i_subtitles )
634                 return VLC_EGENERIC;
635             return VLC_SUCCESS;
636
637         case DEMUX_GET_POSITION:
638             pf = (double*)va_arg( args, double * );
639             if( p_sys->i_subtitle >= p_sys->i_subtitles )
640             {
641                 *pf = 1.0;
642             }
643             else if( p_sys->i_subtitles > 0 )
644             {
645                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
646                       (double)p_sys->i_length;
647             }
648             else
649             {
650                 *pf = 0.0;
651             }
652             return VLC_SUCCESS;
653
654         case DEMUX_SET_POSITION:
655             f = (double)va_arg( args, double );
656             i64 = f * p_sys->i_length;
657
658             p_sys->i_subtitle = 0;
659             while( p_sys->i_subtitle < p_sys->i_subtitles &&
660                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
661             {
662                 p_sys->i_subtitle++;
663             }
664             if( p_sys->i_subtitle >= p_sys->i_subtitles )
665                 return VLC_EGENERIC;
666             return VLC_SUCCESS;
667
668         case DEMUX_SET_NEXT_DEMUX_TIME:
669             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
670             return VLC_SUCCESS;
671
672         case DEMUX_GET_PTS_DELAY:
673         case DEMUX_GET_FPS:
674         case DEMUX_GET_META:
675         case DEMUX_GET_ATTACHMENTS:
676         case DEMUX_GET_TITLE_INFO:
677         case DEMUX_HAS_UNSUPPORTED_META:
678         case DEMUX_CAN_RECORD:
679             return VLC_EGENERIC;
680
681         default:
682             msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
683             return VLC_EGENERIC;
684     }
685 }
686
687 /*****************************************************************************
688  * Demux: Send subtitle to decoder
689  *****************************************************************************/
690 static int Demux( demux_t *p_demux )
691 {
692     demux_sys_t *p_sys = p_demux->p_sys;
693     int64_t i_maxdate;
694
695     if( p_sys->i_subtitle >= p_sys->i_subtitles )
696         return 0;
697
698     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
699     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
700     {
701         /* Should not happen */
702         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
703     }
704
705     while( p_sys->i_subtitle < p_sys->i_subtitles &&
706            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
707     {
708         const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
709
710         block_t *p_block;
711         int i_len = strlen( p_subtitle->psz_text ) + 1;
712
713         if( i_len <= 1 || p_subtitle->i_start < 0 )
714         {
715             p_sys->i_subtitle++;
716             continue;
717         }
718
719         if( ( p_block = block_Alloc( i_len ) ) == NULL )
720         {
721             p_sys->i_subtitle++;
722             continue;
723         }
724
725         p_block->i_dts =
726         p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
727         if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
728             p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
729
730         memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
731
732         es_out_Send( p_demux->out, p_sys->es, p_block );
733
734         p_sys->i_subtitle++;
735     }
736
737     /* */
738     p_sys->i_next_demux_date = 0;
739
740     return 1;
741 }
742
743 /*****************************************************************************
744  * Fix: fix time stamp and order of subtitle
745  *****************************************************************************/
746 static void Fix( demux_t *p_demux )
747 {
748     demux_sys_t *p_sys = p_demux->p_sys;
749     bool b_done;
750
751     /* *** fix order (to be sure...) *** */
752     /* We suppose that there are near in order and this durty bubble sort
753      * would not take too much time
754      */
755     do
756     {
757         b_done = true;
758         for( int i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
759         {
760             if( p_sys->subtitle[i_index].i_start <
761                 p_sys->subtitle[i_index - 1].i_start )
762             {
763                 subtitle_t sub_xch;
764                 memcpy( &sub_xch,
765                         p_sys->subtitle + i_index - 1,
766                         sizeof( subtitle_t ) );
767                 memcpy( p_sys->subtitle + i_index - 1,
768                         p_sys->subtitle + i_index,
769                         sizeof( subtitle_t ) );
770                 memcpy( p_sys->subtitle + i_index,
771                         &sub_xch,
772                         sizeof( subtitle_t ) );
773                 b_done = false;
774             }
775         }
776     } while( !b_done );
777 }
778
779 static int TextLoad( text_t *txt, stream_t *s )
780 {
781     int   i_line_max;
782
783     /* init txt */
784     i_line_max          = 500;
785     txt->i_line_count   = 0;
786     txt->i_line         = 0;
787     txt->line           = calloc( i_line_max, sizeof( char * ) );
788     if( !txt->line )
789         return VLC_ENOMEM;
790
791     /* load the complete file */
792     for( ;; )
793     {
794         char *psz = stream_ReadLine( s );
795
796         if( psz == NULL )
797             break;
798
799         txt->line[txt->i_line_count++] = psz;
800         if( txt->i_line_count >= i_line_max )
801         {
802             i_line_max += 100;
803             txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
804             if( !txt->line )
805                 return VLC_ENOMEM;
806         }
807     }
808
809     if( txt->i_line_count <= 0 )
810     {
811         free( txt->line );
812         return VLC_EGENERIC;
813     }
814
815     return VLC_SUCCESS;
816 }
817 static void TextUnload( text_t *txt )
818 {
819     int i;
820
821     for( i = 0; i < txt->i_line_count; i++ )
822     {
823         free( txt->line[i] );
824     }
825     free( txt->line );
826     txt->i_line       = 0;
827     txt->i_line_count = 0;
828 }
829
830 static char *TextGetLine( text_t *txt )
831 {
832     if( txt->i_line >= txt->i_line_count )
833         return( NULL );
834
835     return txt->line[txt->i_line++];
836 }
837 static void TextPreviousLine( text_t *txt )
838 {
839     if( txt->i_line > 0 )
840         txt->i_line--;
841 }
842
843 /*****************************************************************************
844  * Specific Subtitle function
845  *****************************************************************************/
846 /* ParseMicroDvd:
847  *  Format:
848  *      {n1}{n2}Line1|Line2|Line3....
849  *  where n1 and n2 are the video frame number (n2 can be empty)
850  */
851 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
852                           int i_idx )
853 {
854     VLC_UNUSED( i_idx );
855     demux_sys_t *p_sys = p_demux->p_sys;
856     text_t      *txt = &p_sys->txt;
857     char *psz_text;
858     int  i_start;
859     int  i_stop;
860     int  i;
861
862     for( ;; )
863     {
864         const char *s = TextGetLine( txt );
865         if( !s )
866             return VLC_EGENERIC;
867
868         psz_text = malloc( strlen(s) + 1 );
869         if( !psz_text )
870             return VLC_ENOMEM;
871
872         i_start = 0;
873         i_stop  = -1;
874         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
875             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
876         {
877             if( i_start != 1 || i_stop != 1 )
878                 break;
879
880             /* We found a possible setting of the framerate "{1}{1}23.976" */
881             /* Check if it's usable, and if the sub-fps is not set */
882             float f_fps = us_strtof( psz_text, NULL );
883             if( f_fps > 0.f && var_GetFloat( p_demux, "sub-fps" ) <= 0.f )
884                 p_sys->i_microsecperframe = llroundf(1000000.f / f_fps);
885         }
886         free( psz_text );
887     }
888
889     /* replace | by \n */
890     for( i = 0; psz_text[i] != '\0'; i++ )
891     {
892         if( psz_text[i] == '|' )
893             psz_text[i] = '\n';
894     }
895
896     /* */
897     p_subtitle->i_start  = i_start * p_sys->i_microsecperframe;
898     p_subtitle->i_stop   = i_stop >= 0 ? (i_stop  * p_sys->i_microsecperframe) : -1;
899     p_subtitle->psz_text = psz_text;
900     return VLC_SUCCESS;
901 }
902
903 /* ParseSubRipSubViewer
904  *  Format SubRip
905  *      n
906  *      h1:m1:s1,d1 --> h2:m2:s2,d2
907  *      Line1
908  *      Line2
909  *      ....
910  *      [Empty line]
911  *  Format SubViewer v1/v2
912  *      h1:m1:s1.d1,h2:m2:s2.d2
913  *      Line1[br]Line2
914  *      Line3
915  *      ...
916  *      [empty line]
917  *  We ignore line number for SubRip
918  */
919 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
920                                  int (* pf_parse_timing)(subtitle_t *, const char *),
921                                  bool b_replace_br )
922 {
923     demux_sys_t *p_sys = p_demux->p_sys;
924     text_t      *txt = &p_sys->txt;
925     char    *psz_text;
926
927     for( ;; )
928     {
929         const char *s = TextGetLine( txt );
930
931         if( !s )
932             return VLC_EGENERIC;
933
934         if( pf_parse_timing( p_subtitle, s) == VLC_SUCCESS &&
935             p_subtitle->i_start < p_subtitle->i_stop )
936         {
937             break;
938         }
939     }
940
941     /* Now read text until an empty line */
942     psz_text = strdup("");
943     if( !psz_text )
944         return VLC_ENOMEM;
945
946     for( ;; )
947     {
948         const char *s = TextGetLine( txt );
949         int i_len;
950         int i_old;
951
952         i_len = s ? strlen( s ) : 0;
953         if( i_len <= 0 )
954         {
955             p_subtitle->psz_text = psz_text;
956             return VLC_SUCCESS;
957         }
958
959         i_old = strlen( psz_text );
960         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
961         if( !psz_text )
962         {
963             return VLC_ENOMEM;
964         }
965         strcat( psz_text, s );
966         strcat( psz_text, "\n" );
967
968         /* replace [br] by \n */
969         if( b_replace_br )
970         {
971             char *p;
972
973             while( ( p = strstr( psz_text, "[br]" ) ) )
974             {
975                 *p++ = '\n';
976                 memmove( p, &p[3], strlen(&p[3])+1 );
977             }
978         }
979     }
980 }
981
982 /* subtitle_ParseSubRipTimingValue
983  * Parses SubRip timing value.
984  */
985 static int subtitle_ParseSubRipTimingValue(int64_t *timing_value,
986                                            const char *s)
987 {
988     int h1, m1, s1, d1 = 0;
989
990     if ( sscanf( s, "%d:%d:%d,%d",
991                  &h1, &m1, &s1, &d1 ) == 4 ||
992          sscanf( s, "%d:%d:%d.%d",
993                  &h1, &m1, &s1, &d1 ) == 4 ||
994          sscanf( s, "%d:%d:%d",
995                  &h1, &m1, &s1) == 3 )
996     {
997         (*timing_value) = ( (int64_t)h1 * 3600 * 1000 +
998                             (int64_t)m1 * 60 * 1000 +
999                             (int64_t)s1 * 1000 +
1000                             (int64_t)d1 ) * 1000;
1001
1002         return VLC_SUCCESS;
1003     }
1004
1005     return VLC_EGENERIC;
1006 }
1007
1008 /* subtitle_ParseSubRipTiming
1009  * Parses SubRip timing.
1010  */
1011 static int subtitle_ParseSubRipTiming( subtitle_t *p_subtitle,
1012                                        const char *s )
1013 {
1014     int i_result = VLC_EGENERIC;
1015     char *psz_start, *psz_stop;
1016     psz_start = malloc( strlen(s) + 1 );
1017     psz_stop = malloc( strlen(s) + 1 );
1018
1019     if( sscanf( s, "%s --> %s", psz_start, psz_stop) == 2 &&
1020         subtitle_ParseSubRipTimingValue( &p_subtitle->i_start, psz_start ) == VLC_SUCCESS &&
1021         subtitle_ParseSubRipTimingValue( &p_subtitle->i_stop,  psz_stop ) == VLC_SUCCESS )
1022     {
1023         i_result = VLC_SUCCESS;
1024     }
1025
1026     free(psz_start);
1027     free(psz_stop);
1028
1029     return i_result;
1030 }
1031 /* ParseSubRip
1032  */
1033 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
1034                          int i_idx )
1035 {
1036     VLC_UNUSED( i_idx );
1037     return ParseSubRipSubViewer( p_demux, p_subtitle,
1038                                  &subtitle_ParseSubRipTiming,
1039                                  false );
1040 }
1041
1042 /* subtitle_ParseSubViewerTiming
1043  * Parses SubViewer timing.
1044  */
1045 static int subtitle_ParseSubViewerTiming( subtitle_t *p_subtitle,
1046                                    const char *s )
1047 {
1048     int h1, m1, s1, d1, h2, m2, s2, d2;
1049
1050     if( sscanf( s, "%d:%d:%d.%d,%d:%d:%d.%d",
1051                 &h1, &m1, &s1, &d1, &h2, &m2, &s2, &d2) == 8 )
1052     {
1053         p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1054                                 (int64_t)m1 * 60*1000 +
1055                                 (int64_t)s1 * 1000 +
1056                                 (int64_t)d1 ) * 1000;
1057
1058         p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1059                                 (int64_t)m2 * 60*1000 +
1060                                 (int64_t)s2 * 1000 +
1061                                 (int64_t)d2 ) * 1000;
1062         return VLC_SUCCESS;
1063     }
1064     return VLC_EGENERIC;
1065 }
1066
1067 /* ParseSubViewer
1068  */
1069 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
1070                             int i_idx )
1071 {
1072     VLC_UNUSED( i_idx );
1073
1074     return ParseSubRipSubViewer( p_demux, p_subtitle,
1075                                  &subtitle_ParseSubViewerTiming,
1076                                  true );
1077 }
1078
1079 /* ParseSSA
1080  */
1081 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
1082                       int i_idx )
1083 {
1084     demux_sys_t *p_sys = p_demux->p_sys;
1085     text_t      *txt = &p_sys->txt;
1086     size_t header_len = 0;
1087
1088     for( ;; )
1089     {
1090         const char *s = TextGetLine( txt );
1091         int h1, m1, s1, c1, h2, m2, s2, c2;
1092         char *psz_text, *psz_temp;
1093         char temp[16];
1094
1095         if( !s )
1096             return VLC_EGENERIC;
1097
1098         /* We expect (SSA2-4):
1099          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1100          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1101          *
1102          * 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.
1103          */
1104
1105         /* For ASS:
1106          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1107          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1108          */
1109
1110         /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1111         psz_text = malloc( strlen(s) );
1112         if( !psz_text )
1113             return VLC_ENOMEM;
1114
1115         if( sscanf( s,
1116                     "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1117                     temp,
1118                     &h1, &m1, &s1, &c1,
1119                     &h2, &m2, &s2, &c2,
1120                     psz_text ) == 10 )
1121         {
1122             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1123             /* (Layer comes from ASS specs ... it's empty for SSA.) */
1124             if( p_sys->i_type == SUB_TYPE_SSA1 )
1125             {
1126                 /* SSA1 has only 8 commas before the text starts, not 9 */
1127                 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1128                 psz_text[0] = ',';
1129             }
1130             else
1131             {
1132                 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1133
1134                 /* ReadOrder, Layer, %s(rest of fields) */
1135                 if( asprintf( &psz_temp, "%d,%d,%s", i_idx, i_layer, psz_text ) == -1 )
1136                 {
1137                     free( psz_text );
1138                     return VLC_ENOMEM;
1139                 }
1140
1141                 free( psz_text );
1142                 psz_text = psz_temp;
1143             }
1144
1145             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1146                                     (int64_t)m1 * 60*1000 +
1147                                     (int64_t)s1 * 1000 +
1148                                     (int64_t)c1 * 10 ) * 1000;
1149             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1150                                     (int64_t)m2 * 60*1000 +
1151                                     (int64_t)s2 * 1000 +
1152                                     (int64_t)c2 * 10 ) * 1000;
1153             p_subtitle->psz_text = psz_text;
1154             return VLC_SUCCESS;
1155         }
1156         free( psz_text );
1157
1158         /* All the other stuff we add to the header field */
1159         if( header_len == 0 && p_sys->psz_header )
1160             header_len = strlen( p_sys->psz_header );
1161
1162         size_t s_len = strlen( s );
1163         p_sys->psz_header = realloc_or_free( p_sys->psz_header, header_len + s_len + 2 );
1164         if( !p_sys->psz_header )
1165             return VLC_ENOMEM;
1166         snprintf( p_sys->psz_header + header_len, s_len + 2, "%s\n", s );
1167         header_len += s_len + 1;
1168     }
1169 }
1170
1171 /* ParseVplayer
1172  *  Format
1173  *      h:m:s:Line1|Line2|Line3....
1174  *  or
1175  *      h:m:s Line1|Line2|Line3....
1176  */
1177 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1178                           int i_idx )
1179 {
1180     VLC_UNUSED( i_idx );
1181
1182     demux_sys_t *p_sys = p_demux->p_sys;
1183     text_t      *txt = &p_sys->txt;
1184     char *psz_text;
1185     int i;
1186
1187     for( ;; )
1188     {
1189         const char *s = TextGetLine( txt );
1190         int h1, m1, s1;
1191
1192         if( !s )
1193             return VLC_EGENERIC;
1194
1195         psz_text = malloc( strlen( s ) + 1 );
1196         if( !psz_text )
1197             return VLC_ENOMEM;
1198
1199         if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1200                     &h1, &m1, &s1, psz_text ) == 4 )
1201         {
1202             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1203                                     (int64_t)m1 * 60*1000 +
1204                                     (int64_t)s1 * 1000 ) * 1000;
1205             p_subtitle->i_stop  = -1;
1206             break;
1207         }
1208         free( psz_text );
1209     }
1210
1211     /* replace | by \n */
1212     for( i = 0; psz_text[i] != '\0'; i++ )
1213     {
1214         if( psz_text[i] == '|' )
1215             psz_text[i] = '\n';
1216     }
1217     p_subtitle->psz_text = psz_text;
1218     return VLC_SUCCESS;
1219 }
1220
1221 /* ParseSami
1222  */
1223 static char *ParseSamiSearch( text_t *txt,
1224                               char *psz_start, const char *psz_str )
1225 {
1226     if( psz_start && strcasestr( psz_start, psz_str ) )
1227     {
1228         char *s = strcasestr( psz_start, psz_str );
1229         return &s[strlen( psz_str )];
1230     }
1231
1232     for( ;; )
1233     {
1234         char *p = TextGetLine( txt );
1235         if( !p )
1236             return NULL;
1237
1238         if( strcasestr( p, psz_str ) )
1239         {
1240             char *s = strcasestr( p, psz_str );
1241             return &s[strlen( psz_str )];
1242         }
1243     }
1244 }
1245 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1246 {
1247     VLC_UNUSED( i_idx );
1248     demux_sys_t *p_sys = p_demux->p_sys;
1249     text_t      *txt = &p_sys->txt;
1250
1251     char *s;
1252     int64_t i_start;
1253
1254     unsigned int i_text;
1255     char text[8192]; /* Arbitrary but should be long enough */
1256
1257     /* search "Start=" */
1258     if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1259         return VLC_EGENERIC;
1260
1261     /* get start value */
1262     i_start = strtol( s, &s, 0 );
1263
1264     /* search <P */
1265     if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1266         return VLC_EGENERIC;
1267
1268     /* search > */
1269     if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1270         return VLC_EGENERIC;
1271
1272     i_text = 0;
1273     text[0] = '\0';
1274     /* now get all txt until  a "Start=" line */
1275     for( ;; )
1276     {
1277         char c = '\0';
1278         /* Search non empty line */
1279         while( s && *s == '\0' )
1280             s = TextGetLine( txt );
1281         if( !s )
1282             break;
1283
1284         if( *s == '<' )
1285         {
1286             if( !strncasecmp( s, "<br", 3 ) )
1287             {
1288                 c = '\n';
1289             }
1290             else if( strcasestr( s, "Start=" ) )
1291             {
1292                 TextPreviousLine( txt );
1293                 break;
1294             }
1295             s = ParseSamiSearch( txt, s, ">" );
1296         }
1297         else if( !strncmp( s, "&nbsp;", 6 ) )
1298         {
1299             c = ' ';
1300             s += 6;
1301         }
1302         else if( *s == '\t' )
1303         {
1304             c = ' ';
1305             s++;
1306         }
1307         else
1308         {
1309             c = *s;
1310             s++;
1311         }
1312         if( c != '\0' && i_text+1 < sizeof(text) )
1313         {
1314             text[i_text++] = c;
1315             text[i_text] = '\0';
1316         }
1317     }
1318
1319     p_subtitle->i_start = i_start * 1000;
1320     p_subtitle->i_stop  = -1;
1321     p_subtitle->psz_text = strdup( text );
1322
1323     return VLC_SUCCESS;
1324 }
1325
1326 /* ParseDVDSubtitle
1327  *  Format
1328  *      {T h1:m1:s1:c1
1329  *      Line1
1330  *      Line2
1331  *      ...
1332  *      }
1333  * TODO it can have a header
1334  *      { HEAD
1335  *          ...
1336  *          CODEPAGE=...
1337  *          FORMAT=...
1338  *          LANG=English
1339  *      }
1340  *      LANG support would be cool
1341  *      CODEPAGE is probably mandatory FIXME
1342  */
1343 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1344                              int i_idx )
1345 {
1346     VLC_UNUSED( i_idx );
1347
1348     demux_sys_t *p_sys = p_demux->p_sys;
1349     text_t      *txt = &p_sys->txt;
1350     char *psz_text;
1351
1352     for( ;; )
1353     {
1354         const char *s = TextGetLine( txt );
1355         int h1, m1, s1, c1;
1356
1357         if( !s )
1358             return VLC_EGENERIC;
1359
1360         if( sscanf( s,
1361                     "{T %d:%d:%d:%d",
1362                     &h1, &m1, &s1, &c1 ) == 4 )
1363         {
1364             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1365                                     (int64_t)m1 * 60*1000 +
1366                                     (int64_t)s1 * 1000 +
1367                                     (int64_t)c1 * 10) * 1000;
1368             p_subtitle->i_stop = -1;
1369             break;
1370         }
1371     }
1372
1373     /* Now read text until a line containing "}" */
1374     psz_text = strdup("");
1375     if( !psz_text )
1376         return VLC_ENOMEM;
1377     for( ;; )
1378     {
1379         const char *s = TextGetLine( txt );
1380         int i_len;
1381         int i_old;
1382
1383         if( !s )
1384         {
1385             free( psz_text );
1386             return VLC_EGENERIC;
1387         }
1388
1389         i_len = strlen( s );
1390         if( i_len == 1 && s[0] == '}')
1391         {
1392             p_subtitle->psz_text = psz_text;
1393             return VLC_SUCCESS;
1394         }
1395
1396         i_old = strlen( psz_text );
1397         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1398         if( !psz_text )
1399             return VLC_ENOMEM;
1400         strcat( psz_text, s );
1401         strcat( psz_text, "\n" );
1402     }
1403 }
1404
1405 /* ParseMPL2
1406  *  Format
1407  *     [n1][n2]Line1|Line2|Line3...
1408  *  where n1 and n2 are the video frame number (n2 can be empty)
1409  */
1410 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1411 {
1412     VLC_UNUSED( i_idx );
1413
1414     demux_sys_t *p_sys = p_demux->p_sys;
1415     text_t      *txt = &p_sys->txt;
1416     char *psz_text;
1417     int i;
1418
1419     for( ;; )
1420     {
1421         const char *s = TextGetLine( txt );
1422         int i_start;
1423         int i_stop;
1424
1425         if( !s )
1426             return VLC_EGENERIC;
1427
1428         psz_text = malloc( strlen(s) + 1 );
1429         if( !psz_text )
1430             return VLC_ENOMEM;
1431
1432         i_start = 0;
1433         i_stop  = -1;
1434         if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1435             sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1436         {
1437             p_subtitle->i_start = (int64_t)i_start * 100000;
1438             p_subtitle->i_stop  = i_stop >= 0 ? ((int64_t)i_stop  * 100000) : -1;
1439             break;
1440         }
1441         free( psz_text );
1442     }
1443
1444     for( i = 0; psz_text[i] != '\0'; )
1445     {
1446         /* replace | by \n */
1447         if( psz_text[i] == '|' )
1448             psz_text[i] = '\n';
1449
1450         /* Remove italic */
1451         if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1452             memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1453         else
1454             i++;
1455     }
1456     p_subtitle->psz_text = psz_text;
1457     return VLC_SUCCESS;
1458 }
1459
1460 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1461 {
1462     VLC_UNUSED( i_idx );
1463
1464     demux_sys_t *p_sys = p_demux->p_sys;
1465     text_t      *txt = &p_sys->txt;
1466     char *psz_text = strdup( "" );
1467     int i_old = 0;
1468     int i_firstline = 1;
1469
1470     for( ;; )
1471     {
1472         int t; /* Time */
1473
1474         const char *s = TextGetLine( txt );
1475
1476         if( !s )
1477         {
1478             free( psz_text );
1479             return VLC_EGENERIC;
1480         }
1481
1482         /* Data Lines */
1483         if( sscanf (s, "-->> %d", &t) == 1)
1484         {
1485             p_subtitle->i_start = (int64_t)t; /* * FPS*/
1486             p_subtitle->i_stop  = -1;
1487
1488             /* Starting of a subtitle */
1489             if( i_firstline )
1490             {
1491                 i_firstline = 0;
1492             }
1493             /* We have been too far: end of the subtitle, begin of next */
1494             else
1495             {
1496                 TextPreviousLine( txt );
1497                 break;
1498             }
1499         }
1500         /* Text Lines */
1501         else
1502         {
1503             i_old = strlen( psz_text ) + 1;
1504             psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1505             if( !psz_text )
1506                  return VLC_ENOMEM;
1507             strcat( psz_text, s );
1508             strcat( psz_text, "\n" );
1509             if( txt->i_line == txt->i_line_count )
1510                 break;
1511         }
1512     }
1513     p_subtitle->psz_text = psz_text;
1514     return VLC_SUCCESS;
1515 }
1516
1517 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1518 {
1519     VLC_UNUSED( i_idx );
1520
1521     demux_sys_t *p_sys = p_demux->p_sys;
1522     text_t      *txt = &p_sys->txt;
1523     char *psz_text;
1524     int i;
1525
1526     for( ;; )
1527     {
1528         const char *s = TextGetLine( txt );
1529         int t1, t2;
1530
1531         if( !s )
1532             return VLC_EGENERIC;
1533
1534         psz_text = malloc( strlen(s) + 1 );
1535         if( !psz_text )
1536             return VLC_ENOMEM;
1537
1538         /* Data Lines */
1539         if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1540         {
1541             /* 1/10th of second ? Frame based ? FIXME */
1542             p_subtitle->i_start = 10 * t1;
1543             p_subtitle->i_stop = 10 * t2;
1544             /* Remove latest " */
1545             psz_text[ strlen(psz_text) - 1 ] = '\0';
1546
1547             break;
1548         }
1549         free( psz_text );
1550     }
1551
1552     /* replace | by \n */
1553     for( i = 0; psz_text[i] != '\0'; i++ )
1554     {
1555         if( psz_text[i] == '|' )
1556             psz_text[i] = '\n';
1557     }
1558
1559     p_subtitle->psz_text = psz_text;
1560     msg_Dbg( p_demux, "%s", psz_text );
1561     return VLC_SUCCESS;
1562 }
1563
1564 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1565 {
1566     VLC_UNUSED( i_idx );
1567
1568     demux_sys_t *p_sys = p_demux->p_sys;
1569     text_t      *txt = &p_sys->txt;
1570     char *psz_text = strdup( "" );
1571
1572     if( !p_sys->mpsub.b_inited )
1573     {
1574         p_sys->mpsub.f_total = 0.0;
1575         p_sys->mpsub.f_factor = 0.0;
1576
1577         p_sys->mpsub.b_inited = true;
1578     }
1579
1580     for( ;; )
1581     {
1582         char p_dummy;
1583         char *psz_temp;
1584
1585         const char *s = TextGetLine( txt );
1586         if( !s )
1587         {
1588             free( psz_text );
1589             return VLC_EGENERIC;
1590         }
1591
1592         if( strstr( s, "FORMAT" ) )
1593         {
1594             if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1595             {
1596                 p_sys->mpsub.f_factor = 100.0;
1597                 break;
1598             }
1599
1600             psz_temp = malloc( strlen(s) );
1601             if( !psz_temp )
1602             {
1603                 free( psz_text );
1604                 return VLC_ENOMEM;
1605             }
1606
1607             if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1608             {
1609                 float f_fps = us_strtof( psz_temp, NULL );
1610
1611                 if( f_fps > 0.f && var_GetFloat( p_demux, "sub-fps" ) <= 0.f )
1612                     var_SetFloat( p_demux, "sub-fps", f_fps );
1613
1614                 p_sys->mpsub.f_factor = 1.f;
1615                 free( psz_temp );
1616                 break;
1617             }
1618             free( psz_temp );
1619         }
1620
1621         /* Data Lines */
1622         float f1 = us_strtof( s, &psz_temp );
1623         if( *psz_temp )
1624         {
1625             float f2 = us_strtof( psz_temp, NULL );
1626             p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
1627             p_subtitle->i_start = llroundf(10000.f * p_sys->mpsub.f_total);
1628             p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
1629             p_subtitle->i_stop = llroundf(10000.f * p_sys->mpsub.f_total);
1630             break;
1631         }
1632     }
1633
1634     for( ;; )
1635     {
1636         const char *s = TextGetLine( txt );
1637
1638         if( !s )
1639         {
1640             free( psz_text );
1641             return VLC_EGENERIC;
1642         }
1643
1644         int i_len = strlen( s );
1645         if( i_len == 0 )
1646             break;
1647
1648         int i_old = strlen( psz_text );
1649
1650         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1651         if( !psz_text )
1652              return VLC_ENOMEM;
1653
1654         strcat( psz_text, s );
1655         strcat( psz_text, "\n" );
1656     }
1657
1658     p_subtitle->psz_text = psz_text;
1659     return VLC_SUCCESS;
1660 }
1661
1662 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1663 {
1664     VLC_UNUSED( i_idx );
1665
1666     demux_sys_t  *p_sys = p_demux->p_sys;
1667     text_t       *txt = &p_sys->txt;
1668     char         *psz_text, *psz_orig;
1669     char         *psz_text2, *psz_orig2;
1670     int h1, h2, m1, m2, s1, s2, f1, f2;
1671
1672     if( !p_sys->jss.b_inited )
1673     {
1674         p_sys->jss.i_comment = 0;
1675         p_sys->jss.i_time_resolution = 30;
1676         p_sys->jss.i_time_shift = 0;
1677
1678         p_sys->jss.b_inited = true;
1679     }
1680
1681     /* Parse the main lines */
1682     for( ;; )
1683     {
1684         const char *s = TextGetLine( txt );
1685         if( !s )
1686             return VLC_EGENERIC;
1687
1688         psz_orig = malloc( strlen( s ) + 1 );
1689         if( !psz_orig )
1690             return VLC_ENOMEM;
1691         psz_text = psz_orig;
1692
1693         /* Complete time lines */
1694         if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1695                     &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1696         {
1697             p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1698                 (int64_t)( ( f1 +  p_sys->jss.i_time_shift ) /  p_sys->jss.i_time_resolution ) )
1699                 * 1000000;
1700             p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1701                 (int64_t)( ( f2 +  p_sys->jss.i_time_shift ) /  p_sys->jss.i_time_resolution ) )
1702                 * 1000000;
1703             break;
1704         }
1705         /* Short time lines */
1706         else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1707         {
1708             p_subtitle->i_start = (int64_t)(
1709                     ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1710             p_subtitle->i_stop = (int64_t)(
1711                     ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1712             break;
1713         }
1714         /* General Directive lines */
1715         /* Only TIME and SHIFT are supported so far */
1716         else if( s[0] == '#' )
1717         {
1718             int h = 0, m =0, sec = 1, f = 1;
1719             unsigned shift = 1;
1720             int inv = 1;
1721
1722             strcpy( psz_text, s );
1723
1724             switch( toupper( (unsigned char)psz_text[1] ) )
1725             {
1726             case 'S':
1727                  shift = isalpha( (unsigned char)psz_text[2] ) ? 6 : 2 ;
1728
1729                  if( sscanf( &psz_text[shift], "%d", &h ) )
1730                  {
1731                      /* Negative shifting */
1732                      if( h < 0 )
1733                      {
1734                          h *= -1;
1735                          inv = -1;
1736                      }
1737
1738                      if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1739                      {
1740                          if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1741                          {
1742                              sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1743                          }
1744                          else
1745                          {
1746                              h = 0;
1747                              sscanf( &psz_text[shift], "%d:%d.%d",
1748                                      &m, &sec, &f );
1749                              m *= inv;
1750                          }
1751                      }
1752                      else
1753                      {
1754                          h = m = 0;
1755                          sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1756                          sec *= inv;
1757                      }
1758                      p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1759                          * p_sys->jss.i_time_resolution + f ) * inv;
1760                  }
1761                  break;
1762
1763             case 'T':
1764                 shift = isalpha( (unsigned char)psz_text[2] ) ? 8 : 2 ;
1765
1766                 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
1767                 break;
1768             }
1769             free( psz_orig );
1770             continue;
1771         }
1772         else
1773             /* Unkown type line, probably a comment */
1774         {
1775             free( psz_orig );
1776             continue;
1777         }
1778     }
1779
1780     while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1781     {
1782         const char *s2 = TextGetLine( txt );
1783
1784         if( !s2 )
1785         {
1786             free( psz_orig );
1787             return VLC_EGENERIC;
1788         }
1789
1790         int i_len = strlen( s2 );
1791         if( i_len == 0 )
1792             break;
1793
1794         int i_old = strlen( psz_text );
1795
1796         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1797         if( !psz_text )
1798              return VLC_ENOMEM;
1799
1800         psz_orig = psz_text;
1801         strcat( psz_text, s2 );
1802     }
1803
1804     /* Skip the blanks */
1805     while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1806
1807     /* Parse the directives */
1808     if( isalpha( (unsigned char)*psz_text ) || *psz_text == '[' )
1809     {
1810         while( *psz_text != ' ' )
1811         { psz_text++ ;};
1812
1813         /* Directives are NOT parsed yet */
1814         /* This has probably a better place in a decoder ? */
1815         /* directive = malloc( strlen( psz_text ) + 1 );
1816            if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1817     }
1818
1819     /* Skip the blanks after directives */
1820     while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1821
1822     /* Clean all the lines from inline comments and other stuffs */
1823     psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1824     psz_text2 = psz_orig2;
1825
1826     for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1827     {
1828         switch( *psz_text )
1829         {
1830         case '{':
1831             p_sys->jss.i_comment++;
1832             break;
1833         case '}':
1834             if( p_sys->jss.i_comment )
1835             {
1836                 p_sys->jss.i_comment = 0;
1837                 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1838             }
1839             break;
1840         case '~':
1841             if( !p_sys->jss.i_comment )
1842             {
1843                 *psz_text2 = ' ';
1844                 psz_text2++;
1845             }
1846             break;
1847         case ' ':
1848         case '\t':
1849             if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1850                 break;
1851             if( !p_sys->jss.i_comment )
1852             {
1853                 *psz_text2 = ' ';
1854                 psz_text2++;
1855             }
1856             break;
1857         case '\\':
1858             if( (*(psz_text + 1 ) ) == 'n' )
1859             {
1860                 *psz_text2 = '\n';
1861                 psz_text++;
1862                 psz_text2++;
1863                 break;
1864             }
1865             if( ( toupper((unsigned char)*(psz_text + 1 ) ) == 'C' ) ||
1866                     ( toupper((unsigned char)*(psz_text + 1 ) ) == 'F' ) )
1867             {
1868                 psz_text++; psz_text++;
1869                 break;
1870             }
1871             if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1872                 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1873                 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1874                 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1875             {
1876                 psz_text++;
1877                 break;
1878             }
1879             if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1880                 (*(psz_text + 1 ) ) == '\\' )
1881                 psz_text++;
1882             else if( *(psz_text + 1 ) == '\r' ||  *(psz_text + 1 ) == '\n' ||
1883                      *(psz_text + 1 ) == '\0' )
1884             {
1885                 psz_text++;
1886             }
1887             break;
1888         default:
1889             if( !p_sys->jss.i_comment )
1890             {
1891                 *psz_text2 = *psz_text;
1892                 psz_text2++;
1893             }
1894         }
1895         psz_text++;
1896     }
1897
1898     p_subtitle->psz_text = psz_orig2;
1899     msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
1900     free( psz_orig );
1901     return VLC_SUCCESS;
1902 }
1903
1904 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1905 {
1906     VLC_UNUSED( i_idx );
1907
1908     demux_sys_t *p_sys = p_demux->p_sys;
1909     text_t      *txt = &p_sys->txt;
1910     char *psz_text;
1911     int i;
1912
1913     for( ;; )
1914     {
1915         int h1, m1, s1;
1916         int h2, m2, s2;
1917         const char *s = TextGetLine( txt );
1918
1919         if( !s )
1920             return VLC_EGENERIC;
1921
1922         psz_text = malloc( strlen( s ) + 1 );
1923         if( !psz_text )
1924             return VLC_ENOMEM;
1925
1926         if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1927                     &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1928         {
1929             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1930                                     (int64_t)m1 * 60*1000 +
1931                                     (int64_t)s1 * 1000 ) * 1000;
1932             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1933                                     (int64_t)m2 * 60*1000 +
1934                                     (int64_t)s2 * 1000 ) * 1000;
1935             break;
1936         }
1937         free( psz_text );
1938     }
1939
1940     /* replace | by \n */
1941     for( i = 0; psz_text[i] != '\0'; i++ )
1942     {
1943         if( psz_text[i] == '|' )
1944             psz_text[i] = '\n';
1945     }
1946     p_subtitle->psz_text = psz_text;
1947     return VLC_SUCCESS;
1948 }
1949
1950 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1951 {
1952     if( *psz == '\0' ) return 0;
1953     if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1954             sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1955             sscanf( psz, "%d.%d", s, f ) == 2 ||
1956             sscanf( psz, "%d:%d", m, s ) == 2 ||
1957             sscanf( psz, "%d", s ) == 1 )
1958     {
1959         return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1960                + (int64_t)*f * 10 * 1000;
1961     }
1962     else return VLC_EGENERIC;
1963 }
1964
1965 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1966 {
1967     VLC_UNUSED( i_idx );
1968     demux_sys_t *p_sys = p_demux->p_sys;
1969     text_t      *txt = &p_sys->txt;
1970     char *psz_text = NULL;
1971
1972     for( ;; )
1973     {
1974         int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1975         int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1976         const char *s = TextGetLine( txt );
1977         free( psz_text );
1978
1979         if( !s )
1980             return VLC_EGENERIC;
1981
1982         psz_text = malloc( strlen( s ) + 1 );
1983         if( !psz_text )
1984             return VLC_ENOMEM;
1985
1986         /* Find the good begining. This removes extra spaces at the beginning
1987            of the line.*/
1988         char *psz_temp = strcasestr( s, "<time");
1989         if( psz_temp != NULL )
1990         {
1991             char psz_end[12], psz_begin[12];
1992             /* Line has begin and end */
1993             if( ( sscanf( psz_temp,
1994                   "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
1995                             psz_begin, psz_end, psz_text) != 3 ) &&
1996                     /* Line has begin and no end */
1997                     ( sscanf( psz_temp,
1998                               "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
1999                               psz_begin, psz_text ) != 2) )
2000                 /* Line is not recognized */
2001             {
2002                 continue;
2003             }
2004
2005             /* Get the times */
2006             int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
2007             p_subtitle->i_start = i_time >= 0 ? i_time : 0;
2008
2009             i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
2010             p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
2011             break;
2012         }
2013     }
2014
2015     /* Get the following Lines */
2016     for( ;; )
2017     {
2018         const char *s = TextGetLine( txt );
2019
2020         if( !s )
2021         {
2022             free( psz_text );
2023             return VLC_EGENERIC;
2024         }
2025
2026         int i_len = strlen( s );
2027         if( i_len == 0 ) break;
2028
2029         if( strcasestr( s, "<time" ) ||
2030             strcasestr( s, "<clear/") )
2031         {
2032             TextPreviousLine( txt );
2033             break;
2034         }
2035
2036         int i_old = strlen( psz_text );
2037
2038         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
2039         if( !psz_text )
2040             return VLC_ENOMEM;
2041
2042         strcat( psz_text, s );
2043         strcat( psz_text, "\n" );
2044     }
2045
2046     /* Remove the starting ">" that remained after the sscanf */
2047     memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
2048
2049     p_subtitle->psz_text = psz_text;
2050
2051     return VLC_SUCCESS;
2052 }
2053
2054 static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2055 {
2056     VLC_UNUSED( i_idx );
2057
2058     demux_sys_t *p_sys = p_demux->p_sys;
2059     text_t      *txt = &p_sys->txt;
2060     char *psz_text;
2061
2062     for( ;; )
2063     {
2064         int h1, m1, s1;
2065         int h2, m2, s2;
2066         char *s = TextGetLine( txt );
2067
2068         if( !s )
2069             return VLC_EGENERIC;
2070
2071         psz_text = malloc( strlen( s ) + 1 );
2072         if( !psz_text )
2073             return VLC_ENOMEM;
2074
2075         if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
2076                     &h1, &m1, &s1, psz_text ) == 4 )
2077         {
2078             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2079                                     (int64_t)m1 * 60*1000 +
2080                                     (int64_t)s1 * 1000 ) * 1000;
2081
2082             char *s = TextGetLine( txt );
2083             if( !s )
2084             {
2085                 free( psz_text );
2086                 return VLC_EGENERIC;
2087             }
2088
2089             if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2090                 p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
2091                                         (int64_t)m2 * 60*1000 +
2092                                         (int64_t)s2 * 1000 ) * 1000;
2093             else
2094                 p_subtitle->i_stop  = -1;
2095             break;
2096         }
2097         free( psz_text );
2098     }
2099
2100     /* replace [br] by \n */
2101     char *p;
2102     while( ( p = strstr( psz_text, "[br]" ) ) )
2103     {
2104         *p++ = '\n';
2105         memmove( p, &p[3], strlen(&p[3])+1 );
2106     }
2107
2108     p_subtitle->psz_text = psz_text;
2109     return VLC_SUCCESS;
2110 }
2111
2112 static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2113 {
2114     VLC_UNUSED( i_idx );
2115
2116     demux_sys_t *p_sys = p_demux->p_sys;
2117     text_t      *txt = &p_sys->txt;
2118     char *psz_text;
2119
2120     for( ;; )
2121     {
2122         int h1, m1, s1;
2123         int h2, m2, s2;
2124         char *s = TextGetLine( txt );
2125
2126         if( !s )
2127             return VLC_EGENERIC;
2128
2129         if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2130         {
2131             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2132                                     (int64_t)m1 * 60*1000 +
2133                                     (int64_t)s1 * 1000 ) * 1000;
2134
2135             char *s = TextGetLine( txt );
2136             if( !s )
2137                 return VLC_EGENERIC;
2138
2139             psz_text = strdup( s );
2140             if( !psz_text )
2141                 return VLC_ENOMEM;
2142
2143             s = TextGetLine( txt );
2144             if( !s )
2145             {
2146                 free( psz_text );
2147                 return VLC_EGENERIC;
2148             }
2149
2150             if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2151                 p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
2152                                         (int64_t)m2 * 60*1000 +
2153                                         (int64_t)s2 * 1000 ) * 1000;
2154             else
2155                 p_subtitle->i_stop  = -1;
2156
2157             break;
2158         }
2159     }
2160
2161     p_subtitle->psz_text = psz_text;
2162
2163     return VLC_SUCCESS;
2164 }
2165 /*Parsing WebVTT */
2166 static int  ParseVTT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2167 {
2168     VLC_UNUSED( i_idx );
2169
2170     demux_sys_t *p_sys = p_demux->p_sys;
2171     text_t      *txt = &p_sys->txt;
2172     char        *psz_text;
2173
2174     for( ;; )
2175     {
2176         const char *s = TextGetLine( txt );
2177         int h1 = 0, m1 = 0, s1 = 0, d1 = 0;
2178         int h2 = 0, m2 = 0, s2 = 0, d2 = 0;
2179
2180         if( !s )
2181             return VLC_EGENERIC;
2182
2183         if( sscanf( s,"%d:%d:%d.%d --> %d:%d:%d.%d",
2184                     &h1, &m1, &s1, &d1,
2185                     &h2, &m2, &s2, &d2 ) == 8 ||
2186             sscanf( s,"%d:%d:%d.%d --> %d:%d.%d",
2187                     &h1, &m1, &s1, &d1,
2188                          &m2, &s2, &d2 ) == 7 ||
2189             sscanf( s,"%d:%d.%d --> %d:%d:%d.%d",
2190                          &m1, &s1, &d1,
2191                     &h2, &m2, &s2, &d2 ) == 7 ||
2192             sscanf( s,"%d:%d.%d --> %d:%d.%d",
2193                          &m1, &s1, &d1,
2194                          &m2, &s2, &d2 ) == 6 )
2195         {
2196             p_subtitle->i_start = ( (int64_t)h1 * 3600 * 1000 +
2197                                     (int64_t)m1 * 60 * 1000 +
2198                                     (int64_t)s1 * 1000 +
2199                                     (int64_t)d1 ) * 1000;
2200
2201             p_subtitle->i_stop  = ( (int64_t)h2 * 3600 * 1000 +
2202                                     (int64_t)m2 * 60 * 1000 +
2203                                     (int64_t)s2 * 1000 +
2204                                     (int64_t)d2 ) * 1000;
2205             if( p_subtitle->i_start < p_subtitle->i_stop )
2206                 break;
2207         }
2208     }
2209
2210     /* Now read text until an empty line */
2211     psz_text = strdup("");
2212     if( !psz_text )
2213         return VLC_ENOMEM;
2214
2215     for( ;; )
2216     {
2217         const char *s = TextGetLine( txt );
2218         int i_len;
2219         int i_old;
2220
2221         i_len = s ? strlen( s ) : 0;
2222         if( i_len <= 0 )
2223         {
2224             p_subtitle->psz_text = psz_text;
2225             return VLC_SUCCESS;
2226         }
2227
2228         i_old = strlen( psz_text );
2229         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
2230         if( !psz_text )
2231             return VLC_ENOMEM;
2232
2233         strcat( psz_text, s );
2234         strcat( psz_text, "\n" );
2235     }
2236 }
2237
2238 /* Matches filename.xx.srt */
2239 static char * get_language_from_filename( const char * psz_sub_file )
2240 {
2241     char *psz_ret = NULL;
2242     char *psz_tmp, *psz_language_begin;
2243
2244     if( !psz_sub_file ) return NULL;
2245     char *psz_work = strdup( psz_sub_file );
2246
2247     /* Removing extension, but leaving the dot */
2248     psz_tmp = strrchr( psz_work, '.' );
2249     if( psz_tmp )
2250     {
2251         psz_tmp[0] = '\0';
2252         psz_language_begin = strrchr( psz_work, '.' );
2253         if( psz_language_begin )
2254             psz_ret = strdup(++psz_language_begin);
2255         psz_tmp[0] = '.';
2256     }
2257
2258     free( psz_work );
2259     return psz_ret;
2260 }