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