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