]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
Copyright fixes
[vlc] / modules / demux / subtitle.c
1 /*****************************************************************************
2  * subtitle.c: Demux for subtitle text files.
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <errno.h>
31 #ifdef HAVE_SYS_TYPES_H
32 #   include <sys/types.h>
33 #endif
34 #include <ctype.h>
35
36 #include <vlc/vlc.h>
37 #include <vlc/input.h>
38 #include "vlc_video.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t *p_this );
44 static void Close( vlc_object_t *p_this );
45
46 #define SUB_DELAY_LONGTEXT \
47     "Delay subtitles (in 1/10s)"
48 #define SUB_FPS_LONGTEXT \
49     "Override frames per second. " \
50     "It will only work with MicroDVD subtitles."
51 #define SUB_TYPE_LONGTEXT \
52     "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
53     "\"sami\" (auto for autodetection, it should always work)."
54 static char *ppsz_sub_type[] =
55 {
56     "auto", "microdvd", "subrip", "subviewer", "ssa1",
57     "ssa2-4", "ass", "vplayer", "sami"
58 };
59
60 vlc_module_begin();
61     set_shortname( _("Subtitles"));
62     set_description( _("Text subtitles demux") );
63     set_capability( "demux2", 0 );
64     set_category( CAT_INPUT );
65     set_subcategory( SUBCAT_INPUT_DEMUX );
66     add_float( "sub-fps", 0.0, NULL,
67                N_("Frames per second"),
68                SUB_FPS_LONGTEXT, VLC_TRUE );
69     add_integer( "sub-delay", 0, NULL,
70                N_("Subtitles delay"),
71                SUB_DELAY_LONGTEXT, VLC_TRUE );
72     add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
73                 SUB_TYPE_LONGTEXT, VLC_TRUE );
74         change_string_list( ppsz_sub_type, 0, 0 );
75     set_callbacks( Open, Close );
76
77     add_shortcut( "subtitle" );
78 vlc_module_end();
79
80 /*****************************************************************************
81  * Prototypes:
82  *****************************************************************************/
83 enum
84 {
85     SUB_TYPE_UNKNOWN = -1,
86     SUB_TYPE_MICRODVD,
87     SUB_TYPE_SUBRIP,
88     SUB_TYPE_SSA1,
89     SUB_TYPE_SSA2_4,
90     SUB_TYPE_ASS,
91     SUB_TYPE_VPLAYER,
92     SUB_TYPE_SAMI,
93     SUB_TYPE_SUBVIEWER,
94 };
95
96 typedef struct
97 {
98     int     i_line_count;
99     int     i_line;
100     char    **line;
101 } text_t;
102 static int  TextLoad( text_t *, stream_t *s );
103 static void TextUnload( text_t * );
104
105 typedef struct
106 {
107     int64_t i_start;
108     int64_t i_stop;
109
110     char    *psz_text;
111 } subtitle_t;
112
113
114 struct demux_sys_t
115 {
116     int         i_type;
117     text_t      txt;
118     es_out_id_t *es;
119
120     int64_t     i_next_demux_date;
121
122     int64_t     i_microsecperframe;
123     int64_t     i_original_mspf;
124
125     char        *psz_header;
126     int         i_subtitle;
127     int         i_subtitles;
128     subtitle_t  *subtitle;
129
130     int64_t     i_length;
131 };
132
133 static int  ParseMicroDvd ( demux_t *, subtitle_t * );
134 static int  ParseSubRip   ( demux_t *, subtitle_t * );
135 static int  ParseSubViewer( demux_t *, subtitle_t * );
136 static int  ParseSSA      ( demux_t *, subtitle_t * );
137 static int  ParseVplayer  ( demux_t *, subtitle_t * );
138 static int  ParseSami     ( demux_t *, subtitle_t * );
139
140 static struct
141 {
142     char *psz_type_name;
143     int  i_type;
144     char *psz_name;
145     int  (*pf_read)( demux_t *, subtitle_t* );
146 } sub_read_subtitle_function [] =
147 {
148     { "microdvd",   SUB_TYPE_MICRODVD,  "MicroDVD", ParseMicroDvd },
149     { "subrip",     SUB_TYPE_SUBRIP,    "SubRIP",   ParseSubRip },
150     { "subviewer",  SUB_TYPE_SUBVIEWER, "SubViewer",ParseSubViewer },
151     { "ssa1",       SUB_TYPE_SSA1,      "SSA-1",    ParseSSA },
152     { "ssa2-4",     SUB_TYPE_SSA2_4,    "SSA-2/3/4",ParseSSA },
153     { "ass",        SUB_TYPE_ASS,       "SSA/ASS",  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( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
269             {
270                 p_sys->i_type = SUB_TYPE_SSA1;
271                 break;
272             }
273             else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
274             {
275                 p_sys->i_type = SUB_TYPE_ASS;
276                 break;
277             }
278             else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
279             {
280                 p_sys->i_type = SUB_TYPE_SSA2_4;
281                 break;
282             }
283             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
284             {
285                 p_sys->i_type = SUB_TYPE_SSA2_4;
286                 break;
287             }
288             else if( !strncasecmp( s, "Dialogue:", 9  ) )
289             {
290                 p_sys->i_type = SUB_TYPE_ASS;
291                 break;
292             }
293             else if( strcasestr( s, "[INFORMATION]" ) )
294             {
295                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
296                 break;
297             }
298             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
299                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
300             {
301                 p_sys->i_type = SUB_TYPE_VPLAYER;
302                 break;
303             }
304
305             free( s );
306             s = NULL;
307         }
308
309         if( s ) free( s );
310
311         /* It will nearly always work even for non seekable stream thanks the
312          * caching system, and if it fails we loose just a few sub */
313         if( stream_Seek( p_demux->s, 0 ) )
314         {
315             msg_Warn( p_demux, "failed to rewind" );
316         }
317     }
318     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
319     {
320         msg_Err( p_demux, "failed to recognize subtitle type" );
321         return VLC_EGENERIC;
322     }
323
324     for( i = 0; ; i++ )
325     {
326         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
327         {
328             msg_Dbg( p_demux, "detected %s format",
329                      sub_read_subtitle_function[i].psz_name );
330             pf_read = sub_read_subtitle_function[i].pf_read;
331             break;
332         }
333     }
334
335     msg_Dbg( p_demux, "loading all subtitles..." );
336
337     /* Load the whole file */
338     TextLoad( &p_sys->txt, p_demux->s );
339
340     /* Parse it */
341     for( i_max = 0;; )
342     {
343         if( p_sys->i_subtitles >= i_max )
344         {
345             i_max += 500;
346             if( !( p_sys->subtitle = realloc( p_sys->subtitle,
347                                               sizeof(subtitle_t) * i_max ) ) )
348             {
349                 msg_Err( p_demux, "out of memory");
350                 return VLC_ENOMEM;
351             }
352         }
353
354         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
355             break;
356
357         p_sys->i_subtitles++;
358     }
359     /* Unload */
360     TextUnload( &p_sys->txt );
361
362     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
363
364     /* Fix subtitle (order and time) *** */
365     p_sys->i_subtitle = 0;
366     p_sys->i_length = 0;
367     if( p_sys->i_subtitles > 0 )
368     {
369         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
370         /* +1 to avoid 0 */
371         if( p_sys->i_length <= 0 )
372             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
373     }
374
375     /* *** add subtitle ES *** */
376     if( p_sys->i_type == SUB_TYPE_SSA1 ||
377              p_sys->i_type == SUB_TYPE_SSA2_4 ||
378              p_sys->i_type == SUB_TYPE_ASS )
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 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
574 static void Fix( demux_t *p_demux )
575 {
576     demux_sys_t *p_sys = p_demux->p_sys;
577     vlc_bool_t b_done;
578     int     i_index;
579
580     /* *** fix order (to be sure...) *** */
581     /* We suppose that there are near in order and this durty bubble sort
582      * wont take too much time
583      */
584     do
585     {
586         b_done = VLC_TRUE;
587         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
588         {
589             if( p_sys->subtitle[i_index].i_start <
590                     p_sys->subtitle[i_index - 1].i_start )
591             {
592                 subtitle_t sub_xch;
593                 memcpy( &sub_xch,
594                         p_sys->subtitle + i_index - 1,
595                         sizeof( subtitle_t ) );
596                 memcpy( p_sys->subtitle + i_index - 1,
597                         p_sys->subtitle + i_index,
598                         sizeof( subtitle_t ) );
599                 memcpy( p_sys->subtitle + i_index,
600                         &sub_xch,
601                         sizeof( subtitle_t ) );
602                 b_done = VLC_FALSE;
603             }
604         }
605     } while( !b_done );
606 }
607 #endif
608
609 static int TextLoad( text_t *txt, stream_t *s )
610 {
611     int   i_line_max;
612
613     /* init txt */
614     i_line_max          = 500;
615     txt->i_line_count   = 0;
616     txt->i_line         = 0;
617     txt->line           = calloc( i_line_max, sizeof( char * ) );
618
619     /* load the complete file */
620     for( ;; )
621     {
622         char *psz = stream_ReadLine( s );
623
624         if( psz == NULL )
625             break;
626
627         txt->line[txt->i_line_count++] = psz;
628         if( txt->i_line_count >= i_line_max )
629         {
630             i_line_max += 100;
631             txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
632         }
633     }
634
635     if( txt->i_line_count <= 0 )
636     {
637         free( txt->line );
638         return VLC_EGENERIC;
639     }
640
641     return VLC_SUCCESS;
642 }
643 static void TextUnload( text_t *txt )
644 {
645     int i;
646
647     for( i = 0; i < txt->i_line_count; i++ )
648     {
649         free( txt->line[i] );
650     }
651     free( txt->line );
652     txt->i_line       = 0;
653     txt->i_line_count = 0;
654 }
655
656 static char *TextGetLine( text_t *txt )
657 {
658     if( txt->i_line >= txt->i_line_count )
659         return( NULL );
660
661     return txt->line[txt->i_line++];
662 }
663 static void TextPreviousLine( text_t *txt )
664 {
665     if( txt->i_line > 0 )
666         txt->i_line--;
667 }
668
669 /*****************************************************************************
670  * Specific Subtitle function
671  *****************************************************************************/
672 #define MAX_LINE 8192
673 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
674 {
675     demux_sys_t *p_sys = p_demux->p_sys;
676     text_t      *txt = &p_sys->txt;
677     /*
678      * each line:
679      *  {n1}{n2}Line1|Line2|Line3....
680      * where n1 and n2 are the video frame number...
681      * {n2} can also be {}
682      */
683     char *s;
684
685     char buffer_text[MAX_LINE + 1];
686     int    i_start;
687     int    i_stop;
688     unsigned int i;
689
690     int i_microsecperframe = 40000; /* default to 25 fps */
691     if( p_sys->i_microsecperframe > 0 ) 
692         i_microsecperframe = p_sys->i_microsecperframe;
693     
694     p_subtitle->i_start = 0;
695     p_subtitle->i_stop  = 0;
696     p_subtitle->psz_text = NULL;
697
698     for( ;; )
699     {
700         if( ( s = TextGetLine( txt ) ) == NULL )
701         {
702             return( VLC_EGENERIC );
703         }
704         i_start = 0;
705         i_stop  = 0;
706
707         memset( buffer_text, '\0', MAX_LINE );
708         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
709             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
710         {
711             break;
712         }
713     }
714     /* replace | by \n */
715     for( i = 0; i < strlen( buffer_text ); i++ )
716     {
717         if( buffer_text[i] == '|' )
718         {
719             buffer_text[i] = '\n';
720         }
721     }
722
723     p_subtitle->i_start = (int64_t)i_start * i_microsecperframe;
724     p_subtitle->i_stop  = (int64_t)i_stop  * i_microsecperframe;
725     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
726     return( 0 );
727 }
728
729 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
730 {
731     demux_sys_t *p_sys = p_demux->p_sys;
732     text_t      *txt = &p_sys->txt;
733
734     /*
735      * n
736      * h1:m1:s1,d1 --> h2:m2:s2,d2
737      * Line1
738      * Line2
739      * ...
740      * [empty line]
741      *
742      */
743     char *s;
744     char buffer_text[ 10 * MAX_LINE];
745     int  i_buffer_text;
746     int64_t     i_start;
747     int64_t     i_stop;
748
749     p_subtitle->i_start = 0;
750     p_subtitle->i_stop  = 0;
751     p_subtitle->psz_text = NULL;
752
753     for( ;; )
754     {
755         int h1, m1, s1, d1, h2, m2, s2, d2;
756         if( ( s = TextGetLine( txt ) ) == NULL )
757         {
758             return( VLC_EGENERIC );
759         }
760         if( sscanf( s,
761                     "%d:%d:%d,%d --> %d:%d:%d,%d",
762                     &h1, &m1, &s1, &d1,
763                     &h2, &m2, &s2, &d2 ) == 8 )
764         {
765             i_start = ( (int64_t)h1 * 3600*1000 +
766                         (int64_t)m1 * 60*1000 +
767                         (int64_t)s1 * 1000 +
768                         (int64_t)d1 ) * 1000;
769
770             i_stop  = ( (int64_t)h2 * 3600*1000 +
771                         (int64_t)m2 * 60*1000 +
772                         (int64_t)s2 * 1000 +
773                         (int64_t)d2 ) * 1000;
774
775             /* Now read text until an empty line */
776             for( i_buffer_text = 0;; )
777             {
778                 int i_len;
779                 if( ( s = TextGetLine( txt ) ) == NULL )
780                 {
781                     return( VLC_EGENERIC );
782                 }
783
784                 i_len = strlen( s );
785                 if( i_len <= 0 )
786                 {
787                     /* empty line -> end of this subtitle */
788                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
789                     p_subtitle->i_start = i_start;
790                     p_subtitle->i_stop = i_stop;
791                     p_subtitle->psz_text = strdup( buffer_text );
792                     /* If framerate is available, use sub-fps */
793                     if( p_sys->i_microsecperframe != 0 &&
794                         p_sys->i_original_mspf != 0)
795                     {
796                         p_subtitle->i_start = (int64_t)i_start *
797                                               p_sys->i_microsecperframe/
798                                               p_sys->i_original_mspf;
799                         p_subtitle->i_stop  = (int64_t)i_stop  *
800                                               p_sys->i_microsecperframe /
801                                               p_sys->i_original_mspf;
802                     }
803                     return 0;
804                 }
805                 else
806                 {
807                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
808                     {
809                         memcpy( buffer_text + i_buffer_text,
810                                 s,
811                                 i_len );
812                         i_buffer_text += i_len;
813
814                         buffer_text[i_buffer_text] = '\n';
815                         i_buffer_text++;
816                     }
817                 }
818             }
819         }
820     }
821 }
822
823 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
824 {
825     demux_sys_t *p_sys = p_demux->p_sys;
826     text_t      *txt = &p_sys->txt;
827
828     /*
829      * h1:m1:s1.d1,h2:m2:s2.d2
830      * Line1[br]Line2
831      * Line3
832      * ...
833      * [empty line]
834      * ( works with subviewer and subviewer v2 )
835      */
836     char *s;
837     char buffer_text[ 10 * MAX_LINE];
838     int  i_buffer_text;
839     int64_t     i_start;
840     int64_t     i_stop;
841
842     p_subtitle->i_start = 0;
843     p_subtitle->i_stop  = 0;
844     p_subtitle->psz_text = NULL;
845
846     for( ;; )
847     {
848         int h1, m1, s1, d1, h2, m2, s2, d2;
849         if( ( s = TextGetLine( txt ) ) == NULL )
850         {
851             return( VLC_EGENERIC );
852         }
853         if( sscanf( s,
854                     "%d:%d:%d.%d,%d:%d:%d.%d",
855                     &h1, &m1, &s1, &d1,
856                     &h2, &m2, &s2, &d2 ) == 8 )
857         {
858             i_start = ( (int64_t)h1 * 3600*1000 +
859                         (int64_t)m1 * 60*1000 +
860                         (int64_t)s1 * 1000 +
861                         (int64_t)d1 ) * 1000;
862
863             i_stop  = ( (int64_t)h2 * 3600*1000 +
864                         (int64_t)m2 * 60*1000 +
865                         (int64_t)s2 * 1000 +
866                         (int64_t)d2 ) * 1000;
867
868             /* Now read text until an empty line */
869             for( i_buffer_text = 0;; )
870             {
871                 int i_len, i;
872                 if( ( s = TextGetLine( txt ) ) == NULL )
873                 {
874                     return( VLC_EGENERIC );
875                 }
876
877                 i_len = strlen( s );
878                 if( i_len <= 0 )
879                 {
880                     /* empty line -> end of this subtitle */
881                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
882                     p_subtitle->i_start = i_start;
883                     p_subtitle->i_stop = i_stop;
884
885                     /* replace [br] by \n */
886                     for( i = 0; i < i_buffer_text - 3; i++ )
887                     {
888                         if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
889                             buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
890                         {
891                             char *temp = buffer_text + i + 1;
892                             buffer_text[i] = '\n';
893                             memmove( temp, temp+3, strlen( temp ) -3 );
894                             temp[strlen( temp )-3] = '\0';
895                         }
896                     }
897                     p_subtitle->psz_text = strdup( buffer_text );
898                     return( 0 );
899                 }
900                 else
901                 {
902                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
903                     {
904                         memcpy( buffer_text + i_buffer_text,
905                                 s,
906                                 i_len );
907                         i_buffer_text += i_len;
908
909                         buffer_text[i_buffer_text] = '\n';
910                         i_buffer_text++;
911                     }
912                 }
913             }
914         }
915     }
916 }
917
918
919 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
920 {
921     demux_sys_t *p_sys = p_demux->p_sys;
922     text_t      *txt = &p_sys->txt;
923
924     char buffer_text[ 10 * MAX_LINE];
925     char buffer_text2[ 10 * MAX_LINE];
926     char *s;
927     int64_t     i_start;
928     int64_t     i_stop;
929
930     p_subtitle->i_start = 0;
931     p_subtitle->i_stop  = 0;
932     p_subtitle->psz_text = NULL;
933
934     for( ;; )
935     {
936         int h1, m1, s1, c1, h2, m2, s2, c2;
937
938         if( ( s = TextGetLine( txt ) ) == NULL )
939         {
940             return( VLC_EGENERIC );
941         }
942         p_subtitle->psz_text = malloc( strlen( s ) );
943
944         /* We expect (SSA2-4):
945          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
946          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
947          *
948          * SSA-1 is similar but only has 8 commas up untill the subtitle text. Probably the Effect field is no present, but not 100 % sure.
949          */
950
951         /* For ASS:
952          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
953          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
954          */
955         if( sscanf( s,
956                     "Dialogue: %[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
957                     buffer_text2,
958                     &h1, &m1, &s1, &c1,
959                     &h2, &m2, &s2, &c2,
960                     buffer_text ) == 10 )
961         {
962             i_start = ( (int64_t)h1 * 3600*1000 +
963                         (int64_t)m1 * 60*1000 +
964                         (int64_t)s1 * 1000 +
965                         (int64_t)c1 * 10 ) * 1000;
966
967             i_stop  = ( (int64_t)h2 * 3600*1000 +
968                         (int64_t)m2 * 60*1000 +
969                         (int64_t)s2 * 1000 +
970                         (int64_t)c2 * 10 ) * 1000;
971
972             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
973             /* (Layer comes from ASS specs ... it's empty for SSA.) */
974             if( p_sys->i_type == SUB_TYPE_SSA1 )
975             {
976                 sprintf( p_subtitle->psz_text,
977                          ",%s", strdup( buffer_text) ); /* SSA1 has only 8 commas before the text starts, not 9 */
978             }
979             else
980             {
981                 sprintf( p_subtitle->psz_text,
982                          ",,%s", strdup( buffer_text) ); /* ReadOrder, Layer, %s(rest of fields) */
983             }
984             p_subtitle->i_start = i_start;
985             p_subtitle->i_stop = i_stop;
986             return 0;
987         }
988         else
989         {
990             /* All the other stuff we add to the header field */
991             if( p_sys->psz_header != NULL )
992             {
993                 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
994                           strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
995                 {
996                     msg_Err( p_demux, "out of memory");
997                     return VLC_ENOMEM;
998                 }
999                 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
1000                 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1001             }
1002             else
1003             {
1004                 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1005                 {
1006                     msg_Err( p_demux, "out of memory");
1007                     return VLC_ENOMEM;
1008                 }
1009                 p_sys->psz_header = strdup( s );
1010                 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1011             }
1012         }
1013     }
1014 }
1015
1016 static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1017 {
1018     demux_sys_t *p_sys = p_demux->p_sys;
1019     text_t      *txt = &p_sys->txt;
1020
1021     /*
1022      * each line:
1023      *  h:m:s:Line1|Line2|Line3....
1024      *  or
1025      *  h:m:s Line1|Line2|Line3....
1026      *
1027      */
1028     char *p;
1029     char buffer_text[MAX_LINE + 1];
1030     int64_t    i_start;
1031     unsigned int i;
1032
1033     p_subtitle->i_start = 0;
1034     p_subtitle->i_stop  = 0;
1035     p_subtitle->psz_text = NULL;
1036
1037     for( ;; )
1038     {
1039         int h, m, s;
1040         char c;
1041
1042         if( ( p = TextGetLine( txt ) ) == NULL )
1043         {
1044             return( VLC_EGENERIC );
1045         }
1046
1047         i_start = 0;
1048
1049         memset( buffer_text, '\0', MAX_LINE );
1050         if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1051         {
1052             i_start = ( (int64_t)h * 3600*1000 +
1053                         (int64_t)m * 60*1000 +
1054                         (int64_t)s * 1000 ) * 1000;
1055             break;
1056         }
1057     }
1058
1059     /* replace | by \n */
1060     for( i = 0; i < strlen( buffer_text ); i++ )
1061     {
1062         if( buffer_text[i] == '|' )
1063         {
1064             buffer_text[i] = '\n';
1065         }
1066     }
1067     p_subtitle->i_start = i_start;
1068
1069     p_subtitle->i_stop  = 0;
1070     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1071     return( 0 );
1072 }
1073
1074 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1075 {
1076     if( psz_start )
1077     {
1078         if( strcasestr( psz_start, psz_str ) )
1079         {
1080             char *s = strcasestr( psz_start, psz_str );
1081
1082             s += strlen( psz_str );
1083
1084             return( s );
1085         }
1086     }
1087     for( ;; )
1088     {
1089         char *p;
1090         if( ( p = TextGetLine( txt ) ) == NULL )
1091         {
1092             return NULL;
1093         }
1094         if( strcasestr( p, psz_str ) )
1095         {
1096             char *s = strcasestr( p, psz_str );
1097
1098             s += strlen( psz_str );
1099
1100             return(  s);
1101         }
1102     }
1103 }
1104
1105 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1106 {
1107     demux_sys_t *p_sys = p_demux->p_sys;
1108     text_t      *txt = &p_sys->txt;
1109
1110     char *p;
1111     int64_t i_start;
1112
1113     int  i_text;
1114     char buffer_text[10*MAX_LINE + 1];
1115
1116     p_subtitle->i_start = 0;
1117     p_subtitle->i_stop  = 0;
1118     p_subtitle->psz_text = NULL;
1119
1120 #define ADDC( c ) \
1121     if( i_text < 10*MAX_LINE )      \
1122     {                               \
1123         buffer_text[i_text++] = c;  \
1124         buffer_text[i_text] = '\0'; \
1125     }
1126
1127     /* search "Start=" */
1128     if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1129     {
1130         return VLC_EGENERIC;
1131     }
1132
1133     /* get start value */
1134     i_start = strtol( p, &p, 0 );
1135
1136     /* search <P */
1137     if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1138     {
1139         return VLC_EGENERIC;
1140     }
1141     /* search > */
1142     if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1143     {
1144         return VLC_EGENERIC;
1145     }
1146
1147     i_text = 0;
1148     buffer_text[0] = '\0';
1149     /* now get all txt until  a "Start=" line */
1150     for( ;; )
1151     {
1152         if( *p )
1153         {
1154             if( *p == '<' )
1155             {
1156                 if( !strncasecmp( p, "<br", 3 ) )
1157                 {
1158                     ADDC( '\n' );
1159                 }
1160                 else if( strcasestr( p, "Start=" ) )
1161                 {
1162                     TextPreviousLine( txt );
1163                     break;
1164                 }
1165                 p = ParseSamiSearch( txt, p, ">" );
1166             }
1167             else if( !strncmp( p, "&nbsp;", 6 ) )
1168             {
1169                 ADDC( ' ' );
1170                 p += 6;
1171             }
1172             else if( *p == '\t' )
1173             {
1174                 ADDC( ' ' );
1175                 p++;
1176             }
1177             else
1178             {
1179                 ADDC( *p );
1180                 p++;
1181             }
1182         }
1183         else
1184         {
1185             p = TextGetLine( txt );
1186         }
1187
1188         if( p == NULL )
1189         {
1190             break;
1191         }
1192     }
1193
1194     p_subtitle->i_start = i_start * 1000;
1195     p_subtitle->i_stop  = 0;
1196     p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1197
1198     return( VLC_SUCCESS );
1199 #undef ADDC
1200 }