]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / demux / subtitle.c
1 /*****************************************************************************
2  * subtitle.c: Demux for subtitle text files.
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc_input.h>
30
31
32 #include <errno.h>
33 #ifdef HAVE_SYS_TYPES_H
34 #   include <sys/types.h>
35 #endif
36 #include <ctype.h>
37
38 #include <vlc_demux.h>
39 #include <vlc_charset.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open ( vlc_object_t *p_this );
45 static void Close( vlc_object_t *p_this );
46
47 #define SUB_DELAY_LONGTEXT \
48     N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
49 #define SUB_FPS_LONGTEXT \
50     N_("Override the normal frames per second settings. " \
51     "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
52 #define SUB_TYPE_LONGTEXT \
53     N_("Force the subtiles format. Valid values are : \"microdvd\", " \
54     "\"subrip\",  \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
55     "\"sami\", \"dvdsubtitle\", \"mpl2\" and \"auto\" (meaning autodetection, this " \
56     "should always work).")
57 static const char *ppsz_sub_type[] =
58 {
59     "auto", "microdvd", "subrip", "subviewer", "ssa1",
60     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2"
61 };
62
63 vlc_module_begin();
64     set_shortname( _("Subtitles"));
65     set_description( _("Text subtitles parser") );
66     set_capability( "demux2", 0 );
67     set_category( CAT_INPUT );
68     set_subcategory( SUBCAT_INPUT_DEMUX );
69     add_float( "sub-fps", 0.0, NULL,
70                N_("Frames per second"),
71                SUB_FPS_LONGTEXT, VLC_TRUE );
72         change_safe();
73     add_integer( "sub-delay", 0, NULL,
74                N_("Subtitles delay"),
75                SUB_DELAY_LONGTEXT, VLC_TRUE );
76         change_safe();
77     add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
78                 SUB_TYPE_LONGTEXT, VLC_TRUE );
79         change_string_list( ppsz_sub_type, NULL, NULL );
80         change_safe();
81     set_callbacks( Open, Close );
82
83     add_shortcut( "subtitle" );
84 vlc_module_end();
85
86 /*****************************************************************************
87  * Prototypes:
88  *****************************************************************************/
89 enum
90 {
91     SUB_TYPE_UNKNOWN = -1,
92     SUB_TYPE_MICRODVD,
93     SUB_TYPE_SUBRIP,
94     SUB_TYPE_SSA1,
95     SUB_TYPE_SSA2_4,
96     SUB_TYPE_ASS,
97     SUB_TYPE_VPLAYER,
98     SUB_TYPE_SAMI,
99     SUB_TYPE_SUBVIEWER,
100     SUB_TYPE_DVDSUBTITLE,
101     SUB_TYPE_MPL2
102 };
103
104 typedef struct
105 {
106     int     i_line_count;
107     int     i_line;
108     char    **line;
109 } text_t;
110 static int  TextLoad( text_t *, stream_t *s );
111 static void TextUnload( text_t * );
112
113 typedef struct
114 {
115     int64_t i_start;
116     int64_t i_stop;
117
118     char    *psz_text;
119 } subtitle_t;
120
121
122 struct demux_sys_t
123 {
124     int         i_type;
125     text_t      txt;
126     es_out_id_t *es;
127
128     int64_t     i_next_demux_date;
129     int64_t     i_microsecperframe;
130
131     char        *psz_header;
132     int         i_subtitle;
133     int         i_subtitles;
134     subtitle_t  *subtitle;
135
136     int64_t     i_length;
137 };
138
139 static int  ParseMicroDvd   ( demux_t *, subtitle_t * );
140 static int  ParseSubRip     ( demux_t *, subtitle_t * );
141 static int  ParseSubViewer  ( demux_t *, subtitle_t * );
142 static int  ParseSSA        ( demux_t *, subtitle_t * );
143 static int  ParseVplayer    ( demux_t *, subtitle_t * );
144 static int  ParseSami       ( demux_t *, subtitle_t * );
145 static int  ParseDVDSubtitle( demux_t *, subtitle_t * );
146 static int  ParseMPL2       ( demux_t *, subtitle_t * );
147
148 static struct
149 {
150     const char *psz_type_name;
151     int  i_type;
152     const char *psz_name;
153     int  (*pf_read)( demux_t *, subtitle_t* );
154 } sub_read_subtitle_function [] =
155 {
156     { "microdvd",   SUB_TYPE_MICRODVD,    "MicroDVD",    ParseMicroDvd },
157     { "subrip",     SUB_TYPE_SUBRIP,      "SubRIP",      ParseSubRip },
158     { "subviewer",  SUB_TYPE_SUBVIEWER,   "SubViewer",   ParseSubViewer },
159     { "ssa1",       SUB_TYPE_SSA1,        "SSA-1",       ParseSSA },
160     { "ssa2-4",     SUB_TYPE_SSA2_4,      "SSA-2/3/4",   ParseSSA },
161     { "ass",        SUB_TYPE_ASS,         "SSA/ASS",     ParseSSA },
162     { "vplayer",    SUB_TYPE_VPLAYER,     "VPlayer",     ParseVplayer },
163     { "sami",       SUB_TYPE_SAMI,        "SAMI",        ParseSami },
164     { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
165     { "mpl2",       SUB_TYPE_MPL2,        "MPL2",        ParseMPL2 },
166     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
167 };
168
169 static int Demux( demux_t * );
170 static int Control( demux_t *, int, va_list );
171
172 /*static void Fix( demux_t * );*/
173
174 /*****************************************************************************
175  * Module initializer
176  *****************************************************************************/
177 static int Open ( vlc_object_t *p_this )
178 {
179     demux_t        *p_demux = (demux_t*)p_this;
180     demux_sys_t    *p_sys;
181     es_format_t    fmt;
182     float          f_fps;
183     char           *psz_type;
184     int  (*pf_read)( demux_t *, subtitle_t* );
185     int            i, i_max;
186
187     if( !p_demux->b_force )
188     {
189         msg_Dbg( p_demux, "subtitle demux discarded" );
190         return VLC_EGENERIC;
191     }
192
193     p_demux->pf_demux = Demux;
194     p_demux->pf_control = Control;
195     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
196     p_sys->psz_header         = NULL;
197     p_sys->i_subtitle         = 0;
198     p_sys->i_subtitles        = 0;
199     p_sys->subtitle           = NULL;
200     p_sys->i_microsecperframe = 40000;
201
202     /* Get the FPS */
203     f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
204     if( f_fps >= 1.0 )
205         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
206
207     msg_Dbg( p_demux, "Movie fps: %f", f_fps );
208
209     /* Check for override of the fps */
210     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
211     if( f_fps >= 1.0 )
212     {
213         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
214         msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
215     }
216
217     /* Get or probe the type */
218     p_sys->i_type = SUB_TYPE_UNKNOWN;
219     psz_type = var_CreateGetString( p_demux, "sub-type" );
220     if( *psz_type )
221     {
222         int i;
223
224         for( i = 0; ; i++ )
225         {
226             if( sub_read_subtitle_function[i].psz_type_name == NULL )
227                 break;
228
229             if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
230                          psz_type ) )
231             {
232                 p_sys->i_type = sub_read_subtitle_function[i].i_type;
233                 break;
234             }
235         }
236     }
237     free( psz_type );
238
239     /* Probe if unknown type */
240     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
241     {
242         int     i_try;
243         char    *s = NULL;
244
245         msg_Dbg( p_demux, "autodetecting subtitle format" );
246         for( i_try = 0; i_try < 256; i_try++ )
247         {
248             int i_dummy;
249
250             if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
251                 break;
252
253             if( strcasestr( s, "<SAMI>" ) )
254             {
255                 p_sys->i_type = SUB_TYPE_SAMI;
256                 break;
257             }
258             else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
259                      sscanf( s, "{%d}{}", &i_dummy ) == 1)
260             {
261                 p_sys->i_type = SUB_TYPE_MICRODVD;
262                 break;
263             }
264             else if( sscanf( s,
265                              "%d:%d:%d,%d --> %d:%d:%d,%d",
266                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
267                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
268             {
269                 p_sys->i_type = SUB_TYPE_SUBRIP;
270                 break;
271             }
272             else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
273             {
274                 p_sys->i_type = SUB_TYPE_SSA1;
275                 break;
276             }
277             else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
278             {
279                 p_sys->i_type = SUB_TYPE_ASS;
280                 break;
281             }
282             else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
283             {
284                 p_sys->i_type = SUB_TYPE_SSA2_4;
285                 break;
286             }
287             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
288             {
289                 p_sys->i_type = SUB_TYPE_SSA2_4;
290                 break;
291             }
292             else if( !strncasecmp( s, "Dialogue:", 9  ) )
293             {
294                 p_sys->i_type = SUB_TYPE_ASS;
295                 break;
296             }
297             else if( strcasestr( s, "[INFORMATION]" ) )
298             {
299                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
300                 break;
301             }
302             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
303                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
304             {
305                 p_sys->i_type = SUB_TYPE_VPLAYER;
306                 break;
307             }
308             else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
309                              &i_dummy, &i_dummy ) == 4 )
310             {
311                 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
312                 break;
313             }
314             else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
315                      sscanf( s, "[%d][]", &i_dummy ) == 1)
316             {
317                 p_sys->i_type = SUB_TYPE_MPL2;
318                 break;
319             }
320
321             free( s );
322             s = NULL;
323         }
324
325         if( s ) free( s );
326
327         /* It will nearly always work even for non seekable stream thanks the
328          * caching system, and if it fails we lose just a few sub */
329         if( stream_Seek( p_demux->s, 0 ) )
330         {
331             msg_Warn( p_demux, "failed to rewind" );
332         }
333     }
334     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
335     {
336         msg_Err( p_demux, "failed to recognize subtitle type" );
337         free( p_sys );
338         return VLC_EGENERIC;
339     }
340
341     for( i = 0; ; i++ )
342     {
343         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
344         {
345             msg_Dbg( p_demux, "detected %s format",
346                      sub_read_subtitle_function[i].psz_name );
347             pf_read = sub_read_subtitle_function[i].pf_read;
348             break;
349         }
350     }
351
352     msg_Dbg( p_demux, "loading all subtitles..." );
353
354     /* Load the whole file */
355     TextLoad( &p_sys->txt, p_demux->s );
356
357     /* Parse it */
358     for( i_max = 0;; )
359     {
360         if( p_sys->i_subtitles >= i_max )
361         {
362             i_max += 500;
363             if( !( p_sys->subtitle = realloc( p_sys->subtitle,
364                                               sizeof(subtitle_t) * i_max ) ) )
365             {
366                 msg_Err( p_demux, "out of memory");
367                 if( p_sys->subtitle != NULL )
368                     free( p_sys->subtitle );
369                 TextUnload( &p_sys->txt );
370                 free( p_sys );
371                 return VLC_ENOMEM;
372             }
373         }
374
375         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
376             break;
377
378         p_sys->i_subtitles++;
379     }
380     /* Unload */
381     TextUnload( &p_sys->txt );
382
383     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
384
385     /* Fix subtitle (order and time) *** */
386     p_sys->i_subtitle = 0;
387     p_sys->i_length = 0;
388     if( p_sys->i_subtitles > 0 )
389     {
390         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
391         /* +1 to avoid 0 */
392         if( p_sys->i_length <= 0 )
393             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
394     }
395
396     /* *** add subtitle ES *** */
397     if( p_sys->i_type == SUB_TYPE_SSA1 ||
398              p_sys->i_type == SUB_TYPE_SSA2_4 ||
399              p_sys->i_type == SUB_TYPE_ASS )
400     {
401         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
402     }
403     else
404     {
405         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
406     }
407     if( p_sys->psz_header != NULL )
408     {
409         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
410         fmt.p_extra = strdup( p_sys->psz_header );
411     }
412     p_sys->es = es_out_Add( p_demux->out, &fmt );
413
414     return VLC_SUCCESS;
415 }
416
417 /*****************************************************************************
418  * Close: Close subtitle demux
419  *****************************************************************************/
420 static void Close( vlc_object_t *p_this )
421 {
422     demux_t *p_demux = (demux_t*)p_this;
423     demux_sys_t *p_sys = p_demux->p_sys;
424     int i;
425
426     for( i = 0; i < p_sys->i_subtitles; i++ )
427     {
428         if( p_sys->subtitle[i].psz_text )
429             free( p_sys->subtitle[i].psz_text );
430     }
431     if( p_sys->subtitle )
432         free( p_sys->subtitle );
433
434     free( p_sys );
435 }
436
437 /*****************************************************************************
438  * Control:
439  *****************************************************************************/
440 static int Control( demux_t *p_demux, int i_query, va_list args )
441 {
442     demux_sys_t *p_sys = p_demux->p_sys;
443     int64_t *pi64, i64;
444     double *pf, f;
445
446     switch( i_query )
447     {
448         case DEMUX_GET_LENGTH:
449             pi64 = (int64_t*)va_arg( args, int64_t * );
450             *pi64 = p_sys->i_length;
451             return VLC_SUCCESS;
452
453         case DEMUX_GET_TIME:
454             pi64 = (int64_t*)va_arg( args, int64_t * );
455             if( p_sys->i_subtitle < p_sys->i_subtitles )
456             {
457                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
458                 return VLC_SUCCESS;
459             }
460             return VLC_EGENERIC;
461
462         case DEMUX_SET_TIME:
463             i64 = (int64_t)va_arg( args, int64_t );
464             p_sys->i_subtitle = 0;
465             while( p_sys->i_subtitle < p_sys->i_subtitles &&
466                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
467             {
468                 p_sys->i_subtitle++;
469             }
470
471             if( p_sys->i_subtitle >= p_sys->i_subtitles )
472                 return VLC_EGENERIC;
473             return VLC_SUCCESS;
474
475         case DEMUX_GET_POSITION:
476             pf = (double*)va_arg( args, double * );
477             if( p_sys->i_subtitle >= p_sys->i_subtitles )
478             {
479                 *pf = 1.0;
480             }
481             else if( p_sys->i_subtitles > 0 )
482             {
483                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
484                       (double)p_sys->i_length;
485             }
486             else
487             {
488                 *pf = 0.0;
489             }
490             return VLC_SUCCESS;
491
492         case DEMUX_SET_POSITION:
493             f = (double)va_arg( args, double );
494             i64 = f * p_sys->i_length;
495
496             p_sys->i_subtitle = 0;
497             while( p_sys->i_subtitle < p_sys->i_subtitles &&
498                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
499             {
500                 p_sys->i_subtitle++;
501             }
502             if( p_sys->i_subtitle >= p_sys->i_subtitles )
503                 return VLC_EGENERIC;
504             return VLC_SUCCESS;
505
506         case DEMUX_SET_NEXT_DEMUX_TIME:
507             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
508             return VLC_SUCCESS;
509
510         case DEMUX_GET_FPS:
511         case DEMUX_GET_META:
512         case DEMUX_GET_ATTACHMENTS:
513         case DEMUX_GET_TITLE_INFO:
514         case DEMUX_HAS_UNSUPPORTED_META:
515             return VLC_EGENERIC;
516
517         default:
518             msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
519             return VLC_EGENERIC;
520     }
521 }
522
523 /*****************************************************************************
524  * Demux: Send subtitle to decoder
525  *****************************************************************************/
526 static int Demux( demux_t *p_demux )
527 {
528     demux_sys_t *p_sys = p_demux->p_sys;
529     int64_t i_maxdate;
530
531     if( p_sys->i_subtitle >= p_sys->i_subtitles )
532         return 0;
533
534     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
535     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
536     {
537         /* Should not happen */
538         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
539     }
540
541     while( p_sys->i_subtitle < p_sys->i_subtitles &&
542            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
543     {
544         block_t *p_block;
545         int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
546
547         if( i_len <= 1 )
548         {
549             /* empty subtitle */
550             p_sys->i_subtitle++;
551             continue;
552         }
553
554         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
555         {
556             p_sys->i_subtitle++;
557             continue;
558         }
559
560         if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
561         {
562             p_sys->i_subtitle++;
563             continue;
564         }
565
566         p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
567         p_block->i_dts = p_block->i_pts;
568         if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
569         {
570             p_block->i_length =
571                 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
572         }
573
574         memcpy( p_block->p_buffer,
575                 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
576         if( p_block->i_pts > 0 )
577         {
578             es_out_Send( p_demux->out, p_sys->es, p_block );
579         }
580         else
581         {
582             block_Release( p_block );
583         }
584         p_sys->i_subtitle++;
585     }
586
587     /* */
588     p_sys->i_next_demux_date = 0;
589
590     return 1;
591 }
592
593 /*****************************************************************************
594  * Fix: fix time stamp and order of subtitle
595  *****************************************************************************/
596 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
597 static void Fix( demux_t *p_demux )
598 {
599     demux_sys_t *p_sys = p_demux->p_sys;
600     vlc_bool_t b_done;
601     int     i_index;
602
603     /* *** fix order (to be sure...) *** */
604     /* We suppose that there are near in order and this durty bubble sort
605      * wont take too much time
606      */
607     do
608     {
609         b_done = VLC_TRUE;
610         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
611         {
612             if( p_sys->subtitle[i_index].i_start <
613                     p_sys->subtitle[i_index - 1].i_start )
614             {
615                 subtitle_t sub_xch;
616                 memcpy( &sub_xch,
617                         p_sys->subtitle + i_index - 1,
618                         sizeof( subtitle_t ) );
619                 memcpy( p_sys->subtitle + i_index - 1,
620                         p_sys->subtitle + i_index,
621                         sizeof( subtitle_t ) );
622                 memcpy( p_sys->subtitle + i_index,
623                         &sub_xch,
624                         sizeof( subtitle_t ) );
625                 b_done = VLC_FALSE;
626             }
627         }
628     } while( !b_done );
629 }
630 #endif
631
632 static int TextLoad( text_t *txt, stream_t *s )
633 {
634     int   i_line_max;
635
636     /* init txt */
637     i_line_max          = 500;
638     txt->i_line_count   = 0;
639     txt->i_line         = 0;
640     txt->line           = calloc( i_line_max, sizeof( char * ) );
641
642     /* load the complete file */
643     for( ;; )
644     {
645         char *psz = stream_ReadLine( s );
646
647         if( psz == NULL )
648             break;
649
650         txt->line[txt->i_line_count++] = psz;
651         if( txt->i_line_count >= i_line_max )
652         {
653             i_line_max += 100;
654             txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
655         }
656     }
657
658     if( txt->i_line_count <= 0 )
659     {
660         free( txt->line );
661         return VLC_EGENERIC;
662     }
663
664     return VLC_SUCCESS;
665 }
666 static void TextUnload( text_t *txt )
667 {
668     int i;
669
670     for( i = 0; i < txt->i_line_count; i++ )
671     {
672         free( txt->line[i] );
673     }
674     free( txt->line );
675     txt->i_line       = 0;
676     txt->i_line_count = 0;
677 }
678
679 static char *TextGetLine( text_t *txt )
680 {
681     if( txt->i_line >= txt->i_line_count )
682         return( NULL );
683
684     return txt->line[txt->i_line++];
685 }
686 static void TextPreviousLine( text_t *txt )
687 {
688     if( txt->i_line > 0 )
689         txt->i_line--;
690 }
691
692 /*****************************************************************************
693  * Specific Subtitle function
694  *****************************************************************************/
695 /* ParseMicroDvd:
696  *  Format:
697  *      {n1}{n2}Line1|Line2|Line3....
698  *  where n1 and n2 are the video frame number (n2 can be empty)
699  */
700 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
701 {
702     demux_sys_t *p_sys = p_demux->p_sys;
703     text_t      *txt = &p_sys->txt;
704     char *psz_text;
705     int  i_start;
706     int  i_stop;
707     int  i;
708
709     for( ;; )
710     {
711         const char *s = TextGetLine( txt );
712         if( !s )
713             return VLC_EGENERIC;
714
715         psz_text = malloc( strlen(s) + 1 );
716         if( !psz_text )
717             return VLC_ENOMEM;
718
719         i_start = 0;
720         i_stop  = 0;
721         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
722             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
723         {
724             float f_fps;
725             if( i_start != 1 || i_stop != 1 )
726                 break;
727
728             /* We found a possible setting of the framerate "{1}{1}23.976" */
729             /* Check if it's usable, and if the sub-fps is not set */
730             f_fps = us_strtod( psz_text, NULL );
731             if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
732                 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
733         }
734         free( psz_text );
735     }
736
737     /* replace | by \n */
738     for( i = 0; psz_text[i] != '\0'; i++ )
739     {
740         if( psz_text[i] == '|' )
741             psz_text[i] = '\n';
742     }
743
744     /* */
745     p_subtitle->i_start  = i_start * p_sys->i_microsecperframe;
746     p_subtitle->i_stop   = i_stop  * p_sys->i_microsecperframe;
747     p_subtitle->psz_text = psz_text;
748     return VLC_SUCCESS;
749 }
750
751 /* ParseSubRipSubViewer
752  *  Format SubRip
753  *      n
754  *      h1:m1:s1,d1 --> h2:m2:s2,d2
755  *      Line1
756  *      Line2
757  *      ....
758  *      [Empty line]
759  *  Format SubViewer v1/v2
760  *      h1:m1:s1.d1,h2:m2:s2.d2
761  *      Line1[br]Line2
762  *      Line3
763  *      ...
764  *      [empty line]
765  *  We ignore line number for SubRip
766  */
767 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
768                                  const char *psz_fmt,
769                                  vlc_bool_t b_replace_br )
770 {
771     demux_sys_t *p_sys = p_demux->p_sys;
772     text_t      *txt = &p_sys->txt;
773     char    *psz_text;
774
775     for( ;; )
776     {
777         const char *s = TextGetLine( txt );
778         int h1, m1, s1, d1, h2, m2, s2, d2;
779
780         if( !s )
781             return VLC_EGENERIC;
782
783         if( sscanf( s, psz_fmt,
784                     &h1, &m1, &s1, &d1,
785                     &h2, &m2, &s2, &d2 ) == 8 )
786         {
787             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
788                                     (int64_t)m1 * 60*1000 +
789                                     (int64_t)s1 * 1000 +
790                                     (int64_t)d1 ) * 1000;
791
792             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
793                                     (int64_t)m2 * 60*1000 +
794                                     (int64_t)s2 * 1000 +
795                                     (int64_t)d2 ) * 1000;
796             break;
797         }
798     }
799
800     /* Now read text until an empty line */
801     psz_text = strdup("");
802     if( !psz_text )
803         return VLC_ENOMEM;
804     for( ;; )
805     {
806         const char *s = TextGetLine( txt );
807         int i_len;
808         int i_old;
809
810         if( !s )
811         {
812             free( psz_text );
813             return VLC_EGENERIC;
814         }
815
816         i_len = strlen( s );
817         if( i_len <= 0 )
818         {
819             p_subtitle->psz_text = psz_text;
820             return VLC_SUCCESS;
821         }
822
823         i_old = strlen( psz_text );
824         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
825         if( !psz_text )
826             return VLC_ENOMEM;
827         strcat( psz_text, s );
828         strcat( psz_text, "\n" );
829
830         /* replace [br] by \n */
831         if( b_replace_br )
832         {
833             char *p;
834  
835             while( ( p = strstr( psz_text, "[br]" ) ) )
836             {
837                 *p++ = '\n';
838                 memmove( p, &p[3], strlen(&p[3])+1 );
839             }
840         }
841     }
842 }
843 /* ParseSubRip
844  */
845 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
846 {
847     return ParseSubRipSubViewer( p_demux, p_subtitle,
848                                  "%d:%d:%d,%d --> %d:%d:%d,%d",
849                                  VLC_FALSE );
850 }
851 /* ParseSubViewer
852  */
853 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
854 {
855     return ParseSubRipSubViewer( p_demux, p_subtitle,
856                                  "%d:%d:%d.%d,%d:%d:%d.%d",
857                                  VLC_TRUE );
858 }
859
860 /* ParseSSA
861  */
862 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
863 {
864     demux_sys_t *p_sys = p_demux->p_sys;
865     text_t      *txt = &p_sys->txt;
866
867     for( ;; )
868     {
869         const char *s = TextGetLine( txt );
870         int h1, m1, s1, c1, h2, m2, s2, c2;
871         char *psz_text;
872
873         if( !s )
874             return VLC_EGENERIC;
875
876         /* We expect (SSA2-4):
877          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
878          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
879          *
880          * 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.
881          */
882
883         /* For ASS:
884          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
885          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
886          */
887         psz_text = malloc( 2 + strlen( s ) + 1 );
888         if( !psz_text )
889             return VLC_ENOMEM;
890
891         if( sscanf( s,
892                     "Dialogue: %*[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
893                     &h1, &m1, &s1, &c1,
894                     &h2, &m2, &s2, &c2,
895                     psz_text ) == 9 )
896         {
897             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
898             /* (Layer comes from ASS specs ... it's empty for SSA.) */
899             if( p_sys->i_type == SUB_TYPE_SSA1 )
900             {
901                 /* SSA1 has only 8 commas before the text starts, not 9 */
902                 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
903                 psz_text[0] = ',';
904             }
905             else
906             {
907                 /* ReadOrder, Layer, %s(rest of fields) */
908                 memmove( &psz_text[2], psz_text, strlen(psz_text)+1 );
909                 psz_text[0] = ',';
910                 psz_text[1] = ',';
911             }
912
913             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
914                                     (int64_t)m1 * 60*1000 +
915                                     (int64_t)s1 * 1000 +
916                                     (int64_t)c1 * 10 ) * 1000;
917             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
918                                     (int64_t)m2 * 60*1000 +
919                                     (int64_t)s2 * 1000 +
920                                     (int64_t)c2 * 10 ) * 1000;
921             p_subtitle->psz_text = psz_text;
922             return VLC_SUCCESS;
923         }
924         free( psz_text );
925
926         /* All the other stuff we add to the header field */
927         if( !p_sys->psz_header )
928             p_sys->psz_header = strdup( "" );
929         if( !p_sys->psz_header )
930             return VLC_ENOMEM;
931
932         p_sys->psz_header =
933             realloc( p_sys->psz_header,
934                      strlen( p_sys->psz_header ) + strlen( s ) + 2 );
935         strcat( p_sys->psz_header,  s );
936         strcat( p_sys->psz_header, "\n" );
937     }
938 }
939
940 /* ParseVplayer
941  *  Format
942  *      h:m:s:Line1|Line2|Line3....
943  *  or
944  *      h:m:s Line1|Line2|Line3....
945  */
946 static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
947 {
948     demux_sys_t *p_sys = p_demux->p_sys;
949     text_t      *txt = &p_sys->txt;
950     char *psz_text;
951     int i;
952
953     for( ;; )
954     {
955         const char *s = TextGetLine( txt );
956         int h1, m1, s1;
957
958         if( !s )
959             return VLC_EGENERIC;
960
961         psz_text = malloc( strlen( s ) + 1 );
962         if( !psz_text )
963             return VLC_ENOMEM;
964
965         if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
966                     &h1, &m1, &s1, psz_text ) == 4 )
967         {
968             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
969                                     (int64_t)m1 * 60*1000 +
970                                     (int64_t)s1 * 1000 ) * 1000;
971             p_subtitle->i_stop  = 0;
972             break;
973         }
974         free( psz_text );
975     }
976
977     /* replace | by \n */
978     for( i = 0; psz_text[i] != '\0'; i++ )
979     {
980         if( psz_text[i] == '|' )
981             psz_text[i] = '\n';
982     }
983     p_subtitle->psz_text = psz_text;
984     return VLC_SUCCESS;
985 }
986
987 /* ParseSami
988  */
989 static char *ParseSamiSearch( text_t *txt,
990                               char *psz_start, const char *psz_str )
991 {
992     if( psz_start && strcasestr( psz_start, psz_str ) )
993     {
994         char *s = strcasestr( psz_start, psz_str );
995         return &s[strlen( psz_str )];
996     }
997
998     for( ;; )
999     {
1000         char *p = TextGetLine( txt );
1001         if( !p )
1002             return NULL;
1003
1004         if( strcasestr( p, psz_str ) )
1005         {
1006             char *s = strcasestr( p, psz_str );
1007             return &s[strlen( psz_str )];
1008         }
1009     }
1010 }
1011 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1012 {
1013     demux_sys_t *p_sys = p_demux->p_sys;
1014     text_t      *txt = &p_sys->txt;
1015
1016     char *s;
1017     int64_t i_start;
1018
1019     unsigned int i_text;
1020     char text[8192]; /* Arbitrary but should be long enough */
1021
1022     /* search "Start=" */
1023     if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1024         return VLC_EGENERIC;
1025
1026     /* get start value */
1027     i_start = strtol( s, &s, 0 );
1028
1029     /* search <P */
1030     if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1031         return VLC_EGENERIC;
1032
1033     /* search > */
1034     if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1035         return VLC_EGENERIC;
1036
1037     i_text = 0;
1038     text[0] = '\0';
1039     /* now get all txt until  a "Start=" line */
1040     for( ;; )
1041     {
1042         char c = '\0';
1043         /* Search non empty line */
1044         while( s && *s == '\0' )
1045             s = TextGetLine( txt );
1046         if( !s )
1047             break;
1048
1049         if( *s == '<' )
1050         {
1051             if( !strncasecmp( s, "<br", 3 ) )
1052             {
1053                 c = '\n';
1054             }
1055             else if( strcasestr( s, "Start=" ) )
1056             {
1057                 TextPreviousLine( txt );
1058                 break;
1059             }
1060             s = ParseSamiSearch( txt, s, ">" );
1061         }
1062         else if( !strncmp( s, "&nbsp;", 6 ) )
1063         {
1064             c = ' ';
1065             s += 6;
1066         }
1067         else if( *s == '\t' )
1068         {
1069             c = ' ';
1070             s++;
1071         }
1072         else
1073         {
1074             c = *s;
1075             s++;
1076         }
1077         if( c != '\0' && i_text+1 < sizeof(text) )
1078         {
1079             text[i_text++] = c;
1080             text[i_text] = '\0';
1081         }
1082     }
1083
1084     p_subtitle->i_start = i_start * 1000;
1085     p_subtitle->i_stop  = 0;
1086     p_subtitle->psz_text = strdup( text );
1087
1088     return VLC_SUCCESS;
1089 }
1090
1091 /* ParseDVDSubtitle
1092  *  Format
1093  *      {T h1:m1:s1:c1
1094  *      Line1
1095  *      Line2
1096  *      ...
1097  *      }
1098  * TODO it can have a header
1099  *      { HEAD
1100  *          ...
1101  *          CODEPAGE=...
1102  *          FORMAT=...
1103  *          LANG=English
1104  *      }
1105  *      LANG support would be cool
1106  *      CODEPAGE is probably mandatory FIXME
1107  */
1108 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1109 {
1110     demux_sys_t *p_sys = p_demux->p_sys;
1111     text_t      *txt = &p_sys->txt;
1112     char *psz_text;
1113
1114     for( ;; )
1115     {
1116         const char *s = TextGetLine( txt );
1117         int h1, m1, s1, c1;
1118
1119         if( !s )
1120             return VLC_EGENERIC;
1121
1122         if( sscanf( s,
1123                     "{T %d:%d:%d:%d",
1124                     &h1, &m1, &s1, &c1 ) == 4 )
1125         {
1126             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1127                                     (int64_t)m1 * 60*1000 +
1128                                     (int64_t)s1 * 1000 +
1129                                     (int64_t)c1 * 10) * 1000;
1130             p_subtitle->i_stop = 0;
1131             break;
1132         }
1133     }
1134
1135     /* Now read text until a line containing "}" */
1136     psz_text = strdup("");
1137     if( !psz_text )
1138         return VLC_ENOMEM;
1139     for( ;; )
1140     {
1141         const char *s = TextGetLine( txt );
1142         int i_len;
1143         int i_old;
1144
1145         if( !s )
1146         {
1147             free( psz_text );
1148             return VLC_EGENERIC;
1149         }
1150
1151         i_len = strlen( s );
1152         if( i_len == 1 && s[0] == '}')
1153         {
1154             p_subtitle->psz_text = psz_text;
1155             return VLC_SUCCESS;
1156         }
1157
1158         i_old = strlen( psz_text );
1159         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1160         if( !psz_text )
1161             return VLC_ENOMEM;
1162         strcat( psz_text, s );
1163         strcat( psz_text, "\n" );
1164     }
1165 }
1166
1167 /* ParseMPL2
1168  *  Format
1169  *     [n1][n2]Line1|Line2|Line3...
1170  *  where n1 and n2 are the video frame number (n2 can be empty)
1171  */
1172 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1173 {
1174     demux_sys_t *p_sys = p_demux->p_sys;
1175     text_t      *txt = &p_sys->txt;
1176     char *psz_text;
1177     int i;
1178
1179     for( ;; )
1180     {
1181         const char *s = TextGetLine( txt );
1182         int i_start;
1183         int i_stop;
1184
1185         if( !s )
1186             return VLC_EGENERIC;
1187
1188         psz_text = malloc( strlen(s) + 1 );
1189         if( !psz_text )
1190             return VLC_ENOMEM;
1191
1192         i_start = 0;
1193         i_stop  = 0;
1194         if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1195             sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1196         {
1197             p_subtitle->i_start = (int64_t)i_start * 100000;
1198             p_subtitle->i_stop  = (int64_t)i_stop  * 100000;
1199             break;
1200         }
1201         free( psz_text );
1202     }
1203
1204     /* replace | by \n */
1205     for( i = 0; psz_text[i] != '\0'; )
1206     {
1207         if( psz_text[i] == '|' )
1208             psz_text[i] = '\n';
1209
1210         /* Remove italic */
1211         if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1212             memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1213         else
1214             i++;
1215     }
1216     p_subtitle->psz_text = psz_text;
1217     return VLC_SUCCESS;
1218 }
1219