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