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