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