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