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