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