]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
configure: fix detection of ARM NEON
[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 <errno.h>
40 #ifdef HAVE_SYS_TYPES_H
41 #   include <sys/types.h>
42 #endif
43 #include <ctype.h>
44
45 #include <vlc_demux.h>
46 #include <vlc_charset.h>
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  Open ( vlc_object_t *p_this );
52 static void Close( vlc_object_t *p_this );
53
54 #define SUB_DELAY_LONGTEXT \
55     N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
56 #define SUB_FPS_LONGTEXT \
57     N_("Override the normal frames per second settings. " \
58     "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
59 #define SUB_TYPE_LONGTEXT \
60     N_("Force the subtiles format. Valid values are : \"microdvd\", " \
61     "\"subrip\", \"subviewer\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\", " \
62     "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\", "\
63     "\"mpsub\", \"jacosub\", \"psb\", \"realtext\", \"dks\", \"subviewer1\", " \
64     " and \"auto\" (meaning autodetection, this should always work).")
65
66 static const char *const ppsz_sub_type[] =
67 {
68     "auto", "microdvd", "subrip", "subviewer", "ssa1",
69     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
70     "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
71     "subviewer1"
72 };
73
74 vlc_module_begin ()
75     set_shortname( N_("Subtitles"))
76     set_description( N_("Text subtitles parser") )
77     set_capability( "demux", 0 )
78     set_category( CAT_INPUT )
79     set_subcategory( SUBCAT_INPUT_DEMUX )
80     add_float( "sub-fps", 0.0, NULL,
81                N_("Frames per second"),
82                SUB_FPS_LONGTEXT, true )
83     add_integer( "sub-delay", 0, NULL,
84                N_("Subtitles delay"),
85                SUB_DELAY_LONGTEXT, true )
86     add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
87                 SUB_TYPE_LONGTEXT, true )
88         change_string_list( ppsz_sub_type, NULL, NULL )
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         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
522     }
523     else
524     {
525         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
526     }
527     if( p_sys->psz_header != NULL )
528     {
529         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
530         fmt.p_extra = strdup( p_sys->psz_header );
531     }
532     p_sys->es = es_out_Add( p_demux->out, &fmt );
533
534     return VLC_SUCCESS;
535 }
536
537 /*****************************************************************************
538  * Close: Close subtitle demux
539  *****************************************************************************/
540 static void Close( vlc_object_t *p_this )
541 {
542     demux_t *p_demux = (demux_t*)p_this;
543     demux_sys_t *p_sys = p_demux->p_sys;
544     int i;
545
546     for( i = 0; i < p_sys->i_subtitles; i++ )
547         free( p_sys->subtitle[i].psz_text );
548     free( p_sys->subtitle );
549
550     free( p_sys );
551 }
552
553 /*****************************************************************************
554  * Control:
555  *****************************************************************************/
556 static int Control( demux_t *p_demux, int i_query, va_list args )
557 {
558     demux_sys_t *p_sys = p_demux->p_sys;
559     int64_t *pi64, i64;
560     double *pf, f;
561
562     switch( i_query )
563     {
564         case DEMUX_GET_LENGTH:
565             pi64 = (int64_t*)va_arg( args, int64_t * );
566             *pi64 = p_sys->i_length;
567             return VLC_SUCCESS;
568
569         case DEMUX_GET_TIME:
570             pi64 = (int64_t*)va_arg( args, int64_t * );
571             if( p_sys->i_subtitle < p_sys->i_subtitles )
572             {
573                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
574                 return VLC_SUCCESS;
575             }
576             return VLC_EGENERIC;
577
578         case DEMUX_SET_TIME:
579             i64 = (int64_t)va_arg( args, int64_t );
580             p_sys->i_subtitle = 0;
581             while( p_sys->i_subtitle < p_sys->i_subtitles )
582             {
583                 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
584
585                 if( p_subtitle->i_start > i64 )
586                     break;
587                 if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
588                     break;
589
590                 p_sys->i_subtitle++;
591             }
592
593             if( p_sys->i_subtitle >= p_sys->i_subtitles )
594                 return VLC_EGENERIC;
595             return VLC_SUCCESS;
596
597         case DEMUX_GET_POSITION:
598             pf = (double*)va_arg( args, double * );
599             if( p_sys->i_subtitle >= p_sys->i_subtitles )
600             {
601                 *pf = 1.0;
602             }
603             else if( p_sys->i_subtitles > 0 )
604             {
605                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
606                       (double)p_sys->i_length;
607             }
608             else
609             {
610                 *pf = 0.0;
611             }
612             return VLC_SUCCESS;
613
614         case DEMUX_SET_POSITION:
615             f = (double)va_arg( args, double );
616             i64 = f * p_sys->i_length;
617
618             p_sys->i_subtitle = 0;
619             while( p_sys->i_subtitle < p_sys->i_subtitles &&
620                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
621             {
622                 p_sys->i_subtitle++;
623             }
624             if( p_sys->i_subtitle >= p_sys->i_subtitles )
625                 return VLC_EGENERIC;
626             return VLC_SUCCESS;
627
628         case DEMUX_SET_NEXT_DEMUX_TIME:
629             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
630             return VLC_SUCCESS;
631
632         case DEMUX_GET_FPS:
633         case DEMUX_GET_META:
634         case DEMUX_GET_ATTACHMENTS:
635         case DEMUX_GET_TITLE_INFO:
636         case DEMUX_HAS_UNSUPPORTED_META:
637         case DEMUX_CAN_RECORD:
638             return VLC_EGENERIC;
639
640         default:
641             msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
642             return VLC_EGENERIC;
643     }
644 }
645
646 /*****************************************************************************
647  * Demux: Send subtitle to decoder
648  *****************************************************************************/
649 static int Demux( demux_t *p_demux )
650 {
651     demux_sys_t *p_sys = p_demux->p_sys;
652     int64_t i_maxdate;
653
654     if( p_sys->i_subtitle >= p_sys->i_subtitles )
655         return 0;
656
657     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
658     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
659     {
660         /* Should not happen */
661         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
662     }
663
664     while( p_sys->i_subtitle < p_sys->i_subtitles &&
665            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
666     {
667         const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
668
669         block_t *p_block;
670         int i_len = strlen( p_subtitle->psz_text ) + 1;
671
672         if( i_len <= 1 || p_subtitle->i_start < 0 )
673         {
674             p_sys->i_subtitle++;
675             continue;
676         }
677
678         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
679         {
680             p_sys->i_subtitle++;
681             continue;
682         }
683
684         p_block->i_dts =
685         p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
686         if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
687             p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
688
689         memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
690
691         es_out_Send( p_demux->out, p_sys->es, p_block );
692
693         p_sys->i_subtitle++;
694     }
695
696     /* */
697     p_sys->i_next_demux_date = 0;
698
699     return 1;
700 }
701
702 /*****************************************************************************
703  * Fix: fix time stamp and order of subtitle
704  *****************************************************************************/
705 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
706 static void Fix( demux_t *p_demux )
707 {
708     demux_sys_t *p_sys = p_demux->p_sys;
709     bool b_done;
710     int     i_index;
711
712     /* *** fix order (to be sure...) *** */
713     /* We suppose that there are near in order and this durty bubble sort
714      * wont take too much time
715      */
716     do
717     {
718         b_done = true;
719         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
720         {
721             if( p_sys->subtitle[i_index].i_start <
722                     p_sys->subtitle[i_index - 1].i_start )
723             {
724                 subtitle_t sub_xch;
725                 memcpy( &sub_xch,
726                         p_sys->subtitle + i_index - 1,
727                         sizeof( subtitle_t ) );
728                 memcpy( p_sys->subtitle + i_index - 1,
729                         p_sys->subtitle + i_index,
730                         sizeof( subtitle_t ) );
731                 memcpy( p_sys->subtitle + i_index,
732                         &sub_xch,
733                         sizeof( subtitle_t ) );
734                 b_done = false;
735             }
736         }
737     } while( !b_done );
738 }
739 #endif
740
741 static int TextLoad( text_t *txt, stream_t *s )
742 {
743     int   i_line_max;
744
745     /* init txt */
746     i_line_max          = 500;
747     txt->i_line_count   = 0;
748     txt->i_line         = 0;
749     txt->line           = calloc( i_line_max, sizeof( char * ) );
750     if( !txt->line )
751         return VLC_ENOMEM;
752
753     /* load the complete file */
754     for( ;; )
755     {
756         char *psz = stream_ReadLine( s );
757
758         if( psz == NULL )
759             break;
760
761         txt->line[txt->i_line_count++] = psz;
762         if( txt->i_line_count >= i_line_max )
763         {
764             i_line_max += 100;
765             txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
766             if( !txt->line )
767                 return VLC_ENOMEM;
768         }
769     }
770
771     if( txt->i_line_count <= 0 )
772     {
773         free( txt->line );
774         return VLC_EGENERIC;
775     }
776
777     return VLC_SUCCESS;
778 }
779 static void TextUnload( text_t *txt )
780 {
781     int i;
782
783     for( i = 0; i < txt->i_line_count; i++ )
784     {
785         free( txt->line[i] );
786     }
787     free( txt->line );
788     txt->i_line       = 0;
789     txt->i_line_count = 0;
790 }
791
792 static char *TextGetLine( text_t *txt )
793 {
794     if( txt->i_line >= txt->i_line_count )
795         return( NULL );
796
797     return txt->line[txt->i_line++];
798 }
799 static void TextPreviousLine( text_t *txt )
800 {
801     if( txt->i_line > 0 )
802         txt->i_line--;
803 }
804
805 /*****************************************************************************
806  * Specific Subtitle function
807  *****************************************************************************/
808 /* ParseMicroDvd:
809  *  Format:
810  *      {n1}{n2}Line1|Line2|Line3....
811  *  where n1 and n2 are the video frame number (n2 can be empty)
812  */
813 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
814                           int i_idx )
815 {
816     VLC_UNUSED( i_idx );
817     demux_sys_t *p_sys = p_demux->p_sys;
818     text_t      *txt = &p_sys->txt;
819     char *psz_text;
820     int  i_start;
821     int  i_stop;
822     int  i;
823
824     for( ;; )
825     {
826         const char *s = TextGetLine( txt );
827         if( !s )
828             return VLC_EGENERIC;
829
830         psz_text = malloc( strlen(s) + 1 );
831         if( !psz_text )
832             return VLC_ENOMEM;
833
834         i_start = 0;
835         i_stop  = -1;
836         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
837             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
838         {
839             float f_fps;
840             if( i_start != 1 || i_stop != 1 )
841                 break;
842
843             /* We found a possible setting of the framerate "{1}{1}23.976" */
844             /* Check if it's usable, and if the sub-fps is not set */
845             f_fps = us_strtod( psz_text, NULL );
846             if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
847                 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
848         }
849         free( psz_text );
850     }
851
852     /* replace | by \n */
853     for( i = 0; psz_text[i] != '\0'; i++ )
854     {
855         if( psz_text[i] == '|' )
856             psz_text[i] = '\n';
857     }
858
859     /* */
860     p_subtitle->i_start  = i_start * p_sys->i_microsecperframe;
861     p_subtitle->i_stop   = i_stop >= 0 ? (i_stop  * p_sys->i_microsecperframe) : -1;
862     p_subtitle->psz_text = psz_text;
863     return VLC_SUCCESS;
864 }
865
866 /* ParseSubRipSubViewer
867  *  Format SubRip
868  *      n
869  *      h1:m1:s1,d1 --> h2:m2:s2,d2
870  *      Line1
871  *      Line2
872  *      ....
873  *      [Empty line]
874  *  Format SubViewer v1/v2
875  *      h1:m1:s1.d1,h2:m2:s2.d2
876  *      Line1[br]Line2
877  *      Line3
878  *      ...
879  *      [empty line]
880  *  We ignore line number for SubRip
881  */
882 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
883                                  const char *psz_fmt,
884                                  bool b_replace_br )
885 {
886     demux_sys_t *p_sys = p_demux->p_sys;
887     text_t      *txt = &p_sys->txt;
888     char    *psz_text;
889
890     for( ;; )
891     {
892         const char *s = TextGetLine( txt );
893         int h1, m1, s1, d1, h2, m2, s2, d2;
894
895         if( !s )
896             return VLC_EGENERIC;
897
898         if( sscanf( s, psz_fmt,
899                     &h1, &m1, &s1, &d1,
900                     &h2, &m2, &s2, &d2 ) == 8 )
901         {
902             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
903                                     (int64_t)m1 * 60*1000 +
904                                     (int64_t)s1 * 1000 +
905                                     (int64_t)d1 ) * 1000;
906
907             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
908                                     (int64_t)m2 * 60*1000 +
909                                     (int64_t)s2 * 1000 +
910                                     (int64_t)d2 ) * 1000;
911             if( p_subtitle->i_start < p_subtitle->i_stop )
912                 break;
913         }
914     }
915
916     /* Now read text until an empty line */
917     psz_text = strdup("");
918     if( !psz_text )
919         return VLC_ENOMEM;
920
921     for( ;; )
922     {
923         const char *s = TextGetLine( txt );
924         int i_len;
925         int i_old;
926
927         i_len = s ? strlen( s ) : 0;
928         if( i_len <= 0 )
929         {
930             p_subtitle->psz_text = psz_text;
931             return VLC_SUCCESS;
932         }
933
934         i_old = strlen( psz_text );
935         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
936         if( !psz_text )
937         {
938             return VLC_ENOMEM;
939         }
940         strcat( psz_text, s );
941         strcat( psz_text, "\n" );
942
943         /* replace [br] by \n */
944         if( b_replace_br )
945         {
946             char *p;
947
948             while( ( p = strstr( psz_text, "[br]" ) ) )
949             {
950                 *p++ = '\n';
951                 memmove( p, &p[3], strlen(&p[3])+1 );
952             }
953         }
954     }
955 }
956 /* ParseSubRip
957  */
958 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
959                          int i_idx )
960 {
961     VLC_UNUSED( i_idx );
962     return ParseSubRipSubViewer( p_demux, p_subtitle,
963                                  "%d:%d:%d,%d --> %d:%d:%d,%d",
964                                  false );
965 }
966 /* ParseSubRipDot
967  * Special version for buggy file using '.' instead of ','
968  */
969 static int  ParseSubRipDot( demux_t *p_demux, subtitle_t *p_subtitle,
970                             int i_idx )
971 {
972     VLC_UNUSED( i_idx );
973     return ParseSubRipSubViewer( p_demux, p_subtitle,
974                                  "%d:%d:%d.%d --> %d:%d:%d.%d",
975                                  false );
976 }
977 /* ParseSubViewer
978  */
979 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
980                             int i_idx )
981 {
982     VLC_UNUSED( i_idx );
983
984     return ParseSubRipSubViewer( p_demux, p_subtitle,
985                                  "%d:%d:%d.%d,%d:%d:%d.%d",
986                                  true );
987 }
988
989 /* ParseSSA
990  */
991 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
992                       int i_idx )
993 {
994     demux_sys_t *p_sys = p_demux->p_sys;
995     text_t      *txt = &p_sys->txt;
996
997     for( ;; )
998     {
999         const char *s = TextGetLine( txt );
1000         int h1, m1, s1, c1, h2, m2, s2, c2;
1001         char *psz_text;
1002         char temp[16];
1003
1004         if( !s )
1005             return VLC_EGENERIC;
1006
1007         /* We expect (SSA2-4):
1008          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1009          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1010          *
1011          * 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.
1012          */
1013
1014         /* For ASS:
1015          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1016          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1017          */
1018
1019         /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1020         psz_text = malloc( strlen(s) );
1021         if( !psz_text )
1022             return VLC_ENOMEM;
1023
1024         if( sscanf( s,
1025                     "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1026                     temp,
1027                     &h1, &m1, &s1, &c1,
1028                     &h2, &m2, &s2, &c2,
1029                     psz_text ) == 10 )
1030         {
1031             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1032             /* (Layer comes from ASS specs ... it's empty for SSA.) */
1033             if( p_sys->i_type == SUB_TYPE_SSA1 )
1034             {
1035                 /* SSA1 has only 8 commas before the text starts, not 9 */
1036                 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1037                 psz_text[0] = ',';
1038             }
1039             else
1040             {
1041                 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1042
1043                 /* ReadOrder, Layer, %s(rest of fields) */
1044                 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
1045                 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
1046                 memcpy( psz_text, temp, strlen(temp) );
1047             }
1048
1049             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1050                                     (int64_t)m1 * 60*1000 +
1051                                     (int64_t)s1 * 1000 +
1052                                     (int64_t)c1 * 10 ) * 1000;
1053             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1054                                     (int64_t)m2 * 60*1000 +
1055                                     (int64_t)s2 * 1000 +
1056                                     (int64_t)c2 * 10 ) * 1000;
1057             p_subtitle->psz_text = psz_text;
1058             return VLC_SUCCESS;
1059         }
1060         free( psz_text );
1061
1062         /* All the other stuff we add to the header field */
1063         char *psz_header;
1064         if( asprintf( &psz_header, "%s%s\n",
1065                        p_sys->psz_header ? p_sys->psz_header : "", s ) == -1 )
1066             return VLC_ENOMEM;
1067         p_sys->psz_header = psz_header;
1068     }
1069 }
1070
1071 /* ParseVplayer
1072  *  Format
1073  *      h:m:s:Line1|Line2|Line3....
1074  *  or
1075  *      h:m:s Line1|Line2|Line3....
1076  */
1077 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1078                           int i_idx )
1079 {
1080     VLC_UNUSED( i_idx );
1081
1082     demux_sys_t *p_sys = p_demux->p_sys;
1083     text_t      *txt = &p_sys->txt;
1084     char *psz_text;
1085     int i;
1086
1087     for( ;; )
1088     {
1089         const char *s = TextGetLine( txt );
1090         int h1, m1, s1;
1091
1092         if( !s )
1093             return VLC_EGENERIC;
1094
1095         psz_text = malloc( strlen( s ) + 1 );
1096         if( !psz_text )
1097             return VLC_ENOMEM;
1098
1099         if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1100                     &h1, &m1, &s1, psz_text ) == 4 )
1101         {
1102             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1103                                     (int64_t)m1 * 60*1000 +
1104                                     (int64_t)s1 * 1000 ) * 1000;
1105             p_subtitle->i_stop  = -1;
1106             break;
1107         }
1108         free( psz_text );
1109     }
1110
1111     /* replace | by \n */
1112     for( i = 0; psz_text[i] != '\0'; i++ )
1113     {
1114         if( psz_text[i] == '|' )
1115             psz_text[i] = '\n';
1116     }
1117     p_subtitle->psz_text = psz_text;
1118     return VLC_SUCCESS;
1119 }
1120
1121 /* ParseSami
1122  */
1123 static char *ParseSamiSearch( text_t *txt,
1124                               char *psz_start, const char *psz_str )
1125 {
1126     if( psz_start && strcasestr( psz_start, psz_str ) )
1127     {
1128         char *s = strcasestr( psz_start, psz_str );
1129         return &s[strlen( psz_str )];
1130     }
1131
1132     for( ;; )
1133     {
1134         char *p = TextGetLine( txt );
1135         if( !p )
1136             return NULL;
1137
1138         if( strcasestr( p, psz_str ) )
1139         {
1140             char *s = strcasestr( p, psz_str );
1141             return &s[strlen( psz_str )];
1142         }
1143     }
1144 }
1145 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1146 {
1147     VLC_UNUSED( i_idx );
1148     demux_sys_t *p_sys = p_demux->p_sys;
1149     text_t      *txt = &p_sys->txt;
1150
1151     char *s;
1152     int64_t i_start;
1153
1154     unsigned int i_text;
1155     char text[8192]; /* Arbitrary but should be long enough */
1156
1157     /* search "Start=" */
1158     if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1159         return VLC_EGENERIC;
1160
1161     /* get start value */
1162     i_start = strtol( s, &s, 0 );
1163
1164     /* search <P */
1165     if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1166         return VLC_EGENERIC;
1167
1168     /* search > */
1169     if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1170         return VLC_EGENERIC;
1171
1172     i_text = 0;
1173     text[0] = '\0';
1174     /* now get all txt until  a "Start=" line */
1175     for( ;; )
1176     {
1177         char c = '\0';
1178         /* Search non empty line */
1179         while( s && *s == '\0' )
1180             s = TextGetLine( txt );
1181         if( !s )
1182             break;
1183
1184         if( *s == '<' )
1185         {
1186             if( !strncasecmp( s, "<br", 3 ) )
1187             {
1188                 c = '\n';
1189             }
1190             else if( strcasestr( s, "Start=" ) )
1191             {
1192                 TextPreviousLine( txt );
1193                 break;
1194             }
1195             s = ParseSamiSearch( txt, s, ">" );
1196         }
1197         else if( !strncmp( s, "&nbsp;", 6 ) )
1198         {
1199             c = ' ';
1200             s += 6;
1201         }
1202         else if( *s == '\t' )
1203         {
1204             c = ' ';
1205             s++;
1206         }
1207         else
1208         {
1209             c = *s;
1210             s++;
1211         }
1212         if( c != '\0' && i_text+1 < sizeof(text) )
1213         {
1214             text[i_text++] = c;
1215             text[i_text] = '\0';
1216         }
1217     }
1218
1219     p_subtitle->i_start = i_start * 1000;
1220     p_subtitle->i_stop  = -1;
1221     p_subtitle->psz_text = strdup( text );
1222
1223     return VLC_SUCCESS;
1224 }
1225
1226 /* ParseDVDSubtitle
1227  *  Format
1228  *      {T h1:m1:s1:c1
1229  *      Line1
1230  *      Line2
1231  *      ...
1232  *      }
1233  * TODO it can have a header
1234  *      { HEAD
1235  *          ...
1236  *          CODEPAGE=...
1237  *          FORMAT=...
1238  *          LANG=English
1239  *      }
1240  *      LANG support would be cool
1241  *      CODEPAGE is probably mandatory FIXME
1242  */
1243 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1244                              int i_idx )
1245 {
1246     VLC_UNUSED( i_idx );
1247
1248     demux_sys_t *p_sys = p_demux->p_sys;
1249     text_t      *txt = &p_sys->txt;
1250     char *psz_text;
1251
1252     for( ;; )
1253     {
1254         const char *s = TextGetLine( txt );
1255         int h1, m1, s1, c1;
1256
1257         if( !s )
1258             return VLC_EGENERIC;
1259
1260         if( sscanf( s,
1261                     "{T %d:%d:%d:%d",
1262                     &h1, &m1, &s1, &c1 ) == 4 )
1263         {
1264             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1265                                     (int64_t)m1 * 60*1000 +
1266                                     (int64_t)s1 * 1000 +
1267                                     (int64_t)c1 * 10) * 1000;
1268             p_subtitle->i_stop = -1;
1269             break;
1270         }
1271     }
1272
1273     /* Now read text until a line containing "}" */
1274     psz_text = strdup("");
1275     if( !psz_text )
1276         return VLC_ENOMEM;
1277     for( ;; )
1278     {
1279         const char *s = TextGetLine( txt );
1280         int i_len;
1281         int i_old;
1282
1283         if( !s )
1284         {
1285             free( psz_text );
1286             return VLC_EGENERIC;
1287         }
1288
1289         i_len = strlen( s );
1290         if( i_len == 1 && s[0] == '}')
1291         {
1292             p_subtitle->psz_text = psz_text;
1293             return VLC_SUCCESS;
1294         }
1295
1296         i_old = strlen( psz_text );
1297         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1298         if( !psz_text )
1299             return VLC_ENOMEM;
1300         strcat( psz_text, s );
1301         strcat( psz_text, "\n" );
1302     }
1303 }
1304
1305 /* ParseMPL2
1306  *  Format
1307  *     [n1][n2]Line1|Line2|Line3...
1308  *  where n1 and n2 are the video frame number (n2 can be empty)
1309  */
1310 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1311 {
1312     VLC_UNUSED( i_idx );
1313
1314     demux_sys_t *p_sys = p_demux->p_sys;
1315     text_t      *txt = &p_sys->txt;
1316     char *psz_text;
1317     int i;
1318
1319     for( ;; )
1320     {
1321         const char *s = TextGetLine( txt );
1322         int i_start;
1323         int i_stop;
1324
1325         if( !s )
1326             return VLC_EGENERIC;
1327
1328         psz_text = malloc( strlen(s) + 1 );
1329         if( !psz_text )
1330             return VLC_ENOMEM;
1331
1332         i_start = 0;
1333         i_stop  = -1;
1334         if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1335             sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1336         {
1337             p_subtitle->i_start = (int64_t)i_start * 100000;
1338             p_subtitle->i_stop  = i_stop >= 0 ? ((int64_t)i_stop  * 100000) : -1;
1339             break;
1340         }
1341         free( psz_text );
1342     }
1343
1344     for( i = 0; psz_text[i] != '\0'; )
1345     {
1346         /* replace | by \n */
1347         if( psz_text[i] == '|' )
1348             psz_text[i] = '\n';
1349
1350         /* Remove italic */
1351         if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1352             memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1353         else
1354             i++;
1355     }
1356     p_subtitle->psz_text = psz_text;
1357     return VLC_SUCCESS;
1358 }
1359
1360 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1361 {
1362     VLC_UNUSED( i_idx );
1363
1364     demux_sys_t *p_sys = p_demux->p_sys;
1365     text_t      *txt = &p_sys->txt;
1366     char *psz_text = strdup( "" );
1367     int i_old = 0;
1368     int i_firstline = 1;
1369
1370     for( ;; )
1371     {
1372         int t; /* Time */
1373
1374         const char *s = TextGetLine( txt );
1375
1376         if( !s )
1377         {
1378             free( psz_text );
1379             return VLC_EGENERIC;
1380         }
1381
1382         /* Data Lines */
1383         if( sscanf (s, "-->> %d", &t) == 1)
1384         {
1385             p_subtitle->i_start = (int64_t)t; /* * FPS*/
1386             p_subtitle->i_stop  = -1;
1387
1388             /* Starting of a subtitle */
1389             if( i_firstline )
1390             {
1391                 i_firstline = 0;
1392             }
1393             /* We have been too far: end of the subtitle, begin of next */
1394             else
1395             {
1396                 TextPreviousLine( txt );
1397                 break;
1398             }
1399         }
1400         /* Text Lines */
1401         else
1402         {
1403             i_old = strlen( psz_text ) + 1;
1404             psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1405             if( !psz_text )
1406                  return VLC_ENOMEM;
1407             strcat( psz_text, s );
1408             strcat( psz_text, "\n" );
1409             if( txt->i_line == txt->i_line_count )
1410                 break;
1411         }
1412     }
1413     p_subtitle->psz_text = psz_text;
1414     return VLC_SUCCESS;
1415 }
1416
1417 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1418 {
1419     VLC_UNUSED( i_idx );
1420
1421     demux_sys_t *p_sys = p_demux->p_sys;
1422     text_t      *txt = &p_sys->txt;
1423     char *psz_text;
1424     int i;
1425
1426     for( ;; )
1427     {
1428         const char *s = TextGetLine( txt );
1429         int t1, t2;
1430
1431         if( !s )
1432             return VLC_EGENERIC;
1433
1434         psz_text = malloc( strlen(s) + 1 );
1435         if( !psz_text )
1436             return VLC_ENOMEM;
1437
1438         /* Data Lines */
1439         if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1440         {
1441             /* 1/10th of second ? Frame based ? FIXME */
1442             p_subtitle->i_start = 10 * t1;
1443             p_subtitle->i_stop = 10 * t2;
1444             /* Remove latest " */
1445             psz_text[ strlen(psz_text) - 1 ] = '\0';
1446
1447             break;
1448         }
1449         free( psz_text );
1450     }
1451
1452     /* replace | by \n */
1453     for( i = 0; psz_text[i] != '\0'; i++ )
1454     {
1455         if( psz_text[i] == '|' )
1456             psz_text[i] = '\n';
1457     }
1458
1459     p_subtitle->psz_text = psz_text;
1460     msg_Dbg( p_demux, "%s", psz_text );
1461     return VLC_SUCCESS;
1462 }
1463
1464 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1465 {
1466     VLC_UNUSED( i_idx );
1467
1468     demux_sys_t *p_sys = p_demux->p_sys;
1469     text_t      *txt = &p_sys->txt;
1470     char *psz_text = strdup( "" );
1471
1472     if( !p_sys->mpsub.b_inited )
1473     {
1474         p_sys->mpsub.f_total = 0.0;
1475         p_sys->mpsub.f_factor = 0.0;
1476
1477         p_sys->mpsub.b_inited = true;
1478     }
1479
1480     for( ;; )
1481     {
1482         float f1, f2;
1483         char p_dummy;
1484         char *psz_temp;
1485
1486         const char *s = TextGetLine( txt );
1487         if( !s )
1488         {
1489             free( psz_text );
1490             return VLC_EGENERIC;
1491         }
1492
1493         if( strstr( s, "FORMAT" ) )
1494         {
1495             if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1496             {
1497                 p_sys->mpsub.f_factor = 100.0;
1498                 break;
1499             }
1500
1501             psz_temp = malloc( strlen(s) );
1502             if( !psz_temp )
1503             {
1504                 free( psz_text );
1505                 return VLC_ENOMEM;
1506             }
1507
1508             if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1509             {
1510                 float f_fps;
1511                 f_fps = us_strtod( psz_temp, NULL );
1512                 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1513                     var_SetFloat( p_demux, "sub-fps", f_fps );
1514
1515                 p_sys->mpsub.f_factor = 1.0;
1516                 free( psz_temp );
1517                 break;
1518             }
1519             free( psz_temp );
1520         }
1521         /* Data Lines */
1522         f1 = us_strtod( s, &psz_temp );
1523         if( *psz_temp )
1524         {
1525             f2 = us_strtod( psz_temp, NULL );
1526             p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
1527             p_subtitle->i_start = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1528             p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
1529             p_subtitle->i_stop = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1530             break;
1531         }
1532     }
1533
1534     for( ;; )
1535     {
1536         const char *s = TextGetLine( txt );
1537
1538         if( !s )
1539         {
1540             free( psz_text );
1541             return VLC_EGENERIC;
1542         }
1543
1544         int i_len = strlen( s );
1545         if( i_len == 0 )
1546             break;
1547
1548         int i_old = strlen( psz_text );
1549
1550         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1551         if( !psz_text )
1552              return VLC_ENOMEM;
1553
1554         strcat( psz_text, s );
1555         strcat( psz_text, "\n" );
1556     }
1557
1558     p_subtitle->psz_text = psz_text;
1559     return VLC_SUCCESS;
1560 }
1561
1562 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1563 {
1564     VLC_UNUSED( i_idx );
1565
1566     demux_sys_t  *p_sys = p_demux->p_sys;
1567     text_t       *txt = &p_sys->txt;
1568     char         *psz_text, *psz_orig;
1569     char         *psz_text2, *psz_orig2;
1570     int h1, h2, m1, m2, s1, s2, f1, f2;
1571
1572     if( !p_sys->jss.b_inited )
1573     {
1574         p_sys->jss.i_comment = 0;
1575         p_sys->jss.i_time_resolution = 30;
1576         p_sys->jss.i_time_shift = 0;
1577
1578         p_sys->jss.b_inited = true;
1579     }
1580
1581     /* Parse the main lines */
1582     for( ;; )
1583     {
1584         const char *s = TextGetLine( txt );
1585         if( !s )
1586             return VLC_EGENERIC;
1587
1588         psz_orig = malloc( strlen( s ) + 1 );
1589         if( !psz_orig )
1590             return VLC_ENOMEM;
1591         psz_text = psz_orig;
1592
1593         /* Complete time lines */
1594         if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1595                     &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1596         {
1597             p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1598                 (int64_t)( ( f1 +  p_sys->jss.i_time_shift ) /  p_sys->jss.i_time_resolution ) )
1599                 * 1000000;
1600             p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1601                 (int64_t)( ( f2 +  p_sys->jss.i_time_shift ) /  p_sys->jss.i_time_resolution ) )
1602                 * 1000000;
1603             break;
1604         }
1605         /* Short time lines */
1606         else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1607         {
1608             p_subtitle->i_start = (int64_t)(
1609                     ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1610             p_subtitle->i_stop = (int64_t)(
1611                     ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1612             break;
1613         }
1614         /* General Directive lines */
1615         /* Only TIME and SHIFT are supported so far */
1616         else if( s[0] == '#' )
1617         {
1618             int h = 0, m =0, sec = 1, f = 1;
1619             unsigned shift = 1;
1620             int inv = 1;
1621
1622             strcpy( psz_text, s );
1623
1624             switch( toupper( psz_text[1] ) )
1625             {
1626             case 'S':
1627                  shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1628
1629                  if( sscanf( &psz_text[shift], "%d", &h ) )
1630                  {
1631                      /* Negative shifting */
1632                      if( h < 0 )
1633                      {
1634                          h *= -1;
1635                          inv = -1;
1636                      }
1637
1638                      if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1639                      {
1640                          if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1641                          {
1642                              sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1643                          }
1644                          else
1645                          {
1646                              h = 0;
1647                              sscanf( &psz_text[shift], "%d:%d.%d",
1648                                      &m, &sec, &f );
1649                              m *= inv;
1650                          }
1651                      }
1652                      else
1653                      {
1654                          h = m = 0;
1655                          sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1656                          sec *= inv;
1657                      }
1658                      p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1659                          * p_sys->jss.i_time_resolution + f ) * inv;
1660                  }
1661                  break;
1662
1663             case 'T':
1664                 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1665
1666                 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
1667                 break;
1668             }
1669             free( psz_orig );
1670             continue;
1671         }
1672         else
1673             /* Unkown type line, probably a comment */
1674         {
1675             free( psz_orig );
1676             continue;
1677         }
1678     }
1679         
1680     while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1681     {
1682         const char *s2 = TextGetLine( txt );
1683
1684         if( !s2 )
1685         {
1686             free( psz_orig );
1687             return VLC_EGENERIC;
1688         }
1689
1690         int i_len = strlen( s2 );
1691         if( i_len == 0 )
1692             break;
1693
1694         int i_old = strlen( psz_text );
1695
1696         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1697         if( !psz_text )
1698              return VLC_ENOMEM;
1699
1700                 psz_orig = psz_text;
1701         strcat( psz_text, s2 );
1702     }
1703
1704     /* Skip the blanks */
1705     while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1706
1707     /* Parse the directives */
1708     if( isalpha( *psz_text ) || *psz_text == '[' )
1709     {
1710         while( *psz_text != ' ' )
1711         { psz_text++ ;};
1712
1713         /* Directives are NOT parsed yet */
1714         /* This has probably a better place in a decoder ? */
1715         /* directive = malloc( strlen( psz_text ) + 1 );
1716            if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1717     }
1718
1719     /* Skip the blanks after directives */
1720     while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1721
1722     /* Clean all the lines from inline comments and other stuffs */
1723     psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1724     psz_text2 = psz_orig2;
1725
1726     for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1727     {
1728         switch( *psz_text )
1729         {
1730         case '{':
1731             p_sys->jss.i_comment++;
1732             break;
1733         case '}':
1734             if( p_sys->jss.i_comment )
1735             {
1736                 p_sys->jss.i_comment = 0;
1737                 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1738             }
1739             break;
1740         case '~':
1741             if( !p_sys->jss.i_comment )
1742             {
1743                 *psz_text2 = ' ';
1744                 psz_text2++;
1745             }
1746             break;
1747         case ' ':
1748         case '\t':
1749             if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1750                 break;
1751             if( !p_sys->jss.i_comment )
1752             {
1753                 *psz_text2 = ' ';
1754                 psz_text2++;
1755             }
1756             break;
1757         case '\\':
1758             if( (*(psz_text + 1 ) ) == 'n' )
1759             {
1760                 *psz_text2 = '\n';
1761                 psz_text++;
1762                 psz_text2++;
1763                 break;
1764             }
1765             if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1766                     ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1767             {
1768                 psz_text++; psz_text++;
1769                 break;
1770             }
1771             if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1772                 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1773                 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1774                 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1775             {
1776                 psz_text++;
1777                 break;
1778             }
1779             if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1780                 (*(psz_text + 1 ) ) == '\\' )
1781                 psz_text++;
1782             else if( *(psz_text + 1 ) == '\r' ||  *(psz_text + 1 ) == '\n' ||
1783                      *(psz_text + 1 ) == '\0' )
1784             {
1785                                 psz_text++;
1786             }
1787             break;
1788         default:
1789             if( !p_sys->jss.i_comment )
1790             {
1791                 *psz_text2 = *psz_text;
1792                 psz_text2++;
1793             }
1794         }
1795         psz_text++;
1796     }
1797
1798     p_subtitle->psz_text = psz_orig2;
1799     msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
1800     free( psz_orig );
1801     return VLC_SUCCESS;
1802 }
1803
1804 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1805 {
1806     VLC_UNUSED( i_idx );
1807
1808     demux_sys_t *p_sys = p_demux->p_sys;
1809     text_t      *txt = &p_sys->txt;
1810     char *psz_text;
1811     int i;
1812
1813     for( ;; )
1814     {
1815         int h1, m1, s1;
1816         int h2, m2, s2;
1817         const char *s = TextGetLine( txt );
1818
1819         if( !s )
1820             return VLC_EGENERIC;
1821
1822         psz_text = malloc( strlen( s ) + 1 );
1823         if( !psz_text )
1824             return VLC_ENOMEM;
1825
1826         if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1827                     &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1828         {
1829             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1830                                     (int64_t)m1 * 60*1000 +
1831                                     (int64_t)s1 * 1000 ) * 1000;
1832             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1833                                     (int64_t)m2 * 60*1000 +
1834                                     (int64_t)s2 * 1000 ) * 1000;
1835             break;
1836         }
1837         free( psz_text );
1838     }
1839
1840     /* replace | by \n */
1841     for( i = 0; psz_text[i] != '\0'; i++ )
1842     {
1843         if( psz_text[i] == '|' )
1844             psz_text[i] = '\n';
1845     }
1846     p_subtitle->psz_text = psz_text;
1847     return VLC_SUCCESS;
1848 }
1849
1850 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1851 {
1852     if( strlen( psz ) == 0 ) return 0;
1853     if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1854             sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1855             sscanf( psz, "%d.%d", s, f ) == 2 ||
1856             sscanf( psz, "%d:%d", m, s ) == 2 ||
1857             sscanf( psz, "%d", s ) == 1 )
1858     {
1859         return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1860                + (int64_t)*f * 10 * 1000;
1861     }
1862     else return VLC_EGENERIC;
1863 }
1864
1865 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1866 {
1867     VLC_UNUSED( i_idx );
1868     demux_sys_t *p_sys = p_demux->p_sys;
1869     text_t      *txt = &p_sys->txt;
1870     char *psz_text = NULL;
1871
1872     for( ;; )
1873     {
1874         int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1875         int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1876         const char *s = TextGetLine( txt );
1877         free( psz_text );
1878
1879         if( !s )
1880             return VLC_EGENERIC;
1881
1882         psz_text = malloc( strlen( s ) + 1 );
1883         if( !psz_text )
1884             return VLC_ENOMEM;
1885
1886         /* Find the good begining. This removes extra spaces at the beginning
1887            of the line.*/
1888         char *psz_temp = strcasestr( s, "<time");
1889         if( psz_temp != NULL )
1890         {
1891             char psz_end[12], psz_begin[12];
1892             /* Line has begin and end */
1893             if( ( sscanf( psz_temp,
1894                   "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
1895                             psz_begin, psz_end, psz_text) != 3 ) &&
1896                     /* Line has begin and no end */
1897                     ( sscanf( psz_temp,
1898                               "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
1899                               psz_begin, psz_text ) != 2) )
1900                 /* Line is not recognized */
1901             {
1902                 continue;
1903             }
1904
1905             /* Get the times */
1906             int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
1907             p_subtitle->i_start = i_time >= 0 ? i_time : 0;
1908
1909             i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
1910             p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
1911             break;
1912         }
1913     }
1914
1915     /* Get the following Lines */
1916     for( ;; )
1917     {
1918         const char *s = TextGetLine( txt );
1919
1920         if( !s )
1921         {
1922             free( psz_text );
1923             return VLC_EGENERIC;
1924         }
1925
1926         int i_len = strlen( s );
1927         if( i_len == 0 ) break;
1928
1929         if( strcasestr( s, "<time" ) ||
1930             strcasestr( s, "<clear/") )
1931         {
1932             TextPreviousLine( txt );
1933             break;
1934         }
1935
1936         int i_old = strlen( psz_text );
1937
1938         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1939         if( !psz_text )
1940             return VLC_ENOMEM;
1941
1942         strcat( psz_text, s );
1943         strcat( psz_text, "\n" );
1944     }
1945
1946     /* Remove the starting ">" that remained after the sscanf */
1947     memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
1948
1949     p_subtitle->psz_text = psz_text;
1950
1951     return VLC_SUCCESS;
1952 }
1953
1954 static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1955 {
1956     VLC_UNUSED( i_idx );
1957
1958     demux_sys_t *p_sys = p_demux->p_sys;
1959     text_t      *txt = &p_sys->txt;
1960     char *psz_text;
1961
1962     for( ;; )
1963     {
1964         int h1, m1, s1;
1965         int h2, m2, s2;
1966         char *s = TextGetLine( txt );
1967
1968         if( !s )
1969             return VLC_EGENERIC;
1970
1971         psz_text = malloc( strlen( s ) + 1 );
1972         if( !psz_text )
1973             return VLC_ENOMEM;
1974
1975         if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
1976                     &h1, &m1, &s1, psz_text ) == 4 )
1977         {
1978             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1979                                     (int64_t)m1 * 60*1000 +
1980                                     (int64_t)s1 * 1000 ) * 1000;
1981
1982             char *s = TextGetLine( txt );
1983             if( !s )
1984             {
1985                 free( psz_text );
1986                 return VLC_EGENERIC;
1987             }
1988
1989             if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
1990                 p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1991                                         (int64_t)m2 * 60*1000 +
1992                                         (int64_t)s2 * 1000 ) * 1000;
1993             else
1994                 p_subtitle->i_stop  = -1;
1995             break;
1996         }
1997         free( psz_text );
1998     }
1999
2000     /* replace [br] by \n */
2001     char *p;
2002     while( ( p = strstr( psz_text, "[br]" ) ) )
2003     {
2004         *p++ = '\n';
2005         memmove( p, &p[3], strlen(&p[3])+1 );
2006     }
2007
2008     p_subtitle->psz_text = psz_text;
2009     return VLC_SUCCESS;
2010 }
2011
2012 static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2013 {
2014     VLC_UNUSED( i_idx );
2015
2016     demux_sys_t *p_sys = p_demux->p_sys;
2017     text_t      *txt = &p_sys->txt;
2018     char *psz_text;
2019
2020     for( ;; )
2021     {
2022         int h1, m1, s1;
2023         int h2, m2, s2;
2024         char *s = TextGetLine( txt );
2025
2026         if( !s )
2027             return VLC_EGENERIC;
2028
2029         if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2030         {
2031             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2032                                     (int64_t)m1 * 60*1000 +
2033                                     (int64_t)s1 * 1000 ) * 1000;
2034
2035             char *s = TextGetLine( txt );
2036             if( !s )
2037                 return VLC_EGENERIC;
2038
2039             psz_text = strdup( s );
2040             if( !psz_text )
2041                 return VLC_ENOMEM;
2042
2043             s = TextGetLine( txt );
2044             if( !s )
2045             {
2046                 free( psz_text );
2047                 return VLC_EGENERIC;
2048             }
2049
2050             if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2051                 p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
2052                                         (int64_t)m2 * 60*1000 +
2053                                         (int64_t)s2 * 1000 ) * 1000;
2054             else
2055                 p_subtitle->i_stop  = -1;
2056
2057             break;
2058         }
2059     }
2060
2061     p_subtitle->psz_text = psz_text;
2062
2063     return VLC_SUCCESS;
2064 }
2065