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