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