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