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