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