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