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