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