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