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