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