]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
Fix memleak in error handling
[vlc] / modules / demux / subtitle.c
1 /*****************************************************************************
2  * subtitle.c: Demux for subtitle text files.
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
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         free( p_sys );
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                 if( p_sys->subtitle != NULL )
352                     free( p_sys->subtitle );
353                 TextUnload( &p_sys->txt );
354                 free( p_sys );
355                 return VLC_ENOMEM;
356             }
357         }
358
359         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
360             break;
361
362         p_sys->i_subtitles++;
363     }
364     /* Unload */
365     TextUnload( &p_sys->txt );
366
367     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
368
369     /* Fix subtitle (order and time) *** */
370     p_sys->i_subtitle = 0;
371     p_sys->i_length = 0;
372     if( p_sys->i_subtitles > 0 )
373     {
374         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
375         /* +1 to avoid 0 */
376         if( p_sys->i_length <= 0 )
377             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
378     }
379
380     /* *** add subtitle ES *** */
381     if( p_sys->i_type == SUB_TYPE_SSA1 ||
382              p_sys->i_type == SUB_TYPE_SSA2_4 ||
383              p_sys->i_type == SUB_TYPE_ASS )
384     {
385         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
386     }
387     else
388     {
389         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
390     }
391     if( p_sys->psz_header != NULL )
392     {
393         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
394         fmt.p_extra = strdup( p_sys->psz_header );
395     }
396     p_sys->es = es_out_Add( p_demux->out, &fmt );
397
398     return VLC_SUCCESS;
399 }
400
401 /*****************************************************************************
402  * Close: Close subtitle demux
403  *****************************************************************************/
404 static void Close( vlc_object_t *p_this )
405 {
406     demux_t *p_demux = (demux_t*)p_this;
407     demux_sys_t *p_sys = p_demux->p_sys;
408     int i;
409
410     for( i = 0; i < p_sys->i_subtitles; i++ )
411     {
412         if( p_sys->subtitle[i].psz_text )
413             free( p_sys->subtitle[i].psz_text );
414     }
415     if( p_sys->subtitle )
416         free( p_sys->subtitle );
417
418     free( p_sys );
419 }
420
421 /*****************************************************************************
422  * Control:
423  *****************************************************************************/
424 static int Control( demux_t *p_demux, int i_query, va_list args )
425 {
426     demux_sys_t *p_sys = p_demux->p_sys;
427     int64_t *pi64, i64;
428     double *pf, f;
429
430     switch( i_query )
431     {
432         case DEMUX_GET_LENGTH:
433             pi64 = (int64_t*)va_arg( args, int64_t * );
434             *pi64 = p_sys->i_length;
435             return VLC_SUCCESS;
436
437         case DEMUX_GET_TIME:
438             pi64 = (int64_t*)va_arg( args, int64_t * );
439             if( p_sys->i_subtitle < p_sys->i_subtitles )
440             {
441                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
442                 return VLC_SUCCESS;
443             }
444             return VLC_EGENERIC;
445
446         case DEMUX_SET_TIME:
447             i64 = (int64_t)va_arg( args, int64_t );
448             p_sys->i_subtitle = 0;
449             while( p_sys->i_subtitle < p_sys->i_subtitles &&
450                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
451             {
452                 p_sys->i_subtitle++;
453             }
454
455             if( p_sys->i_subtitle >= p_sys->i_subtitles )
456                 return VLC_EGENERIC;
457             return VLC_SUCCESS;
458
459         case DEMUX_GET_POSITION:
460             pf = (double*)va_arg( args, double * );
461             if( p_sys->i_subtitle >= p_sys->i_subtitles )
462             {
463                 *pf = 1.0;
464             }
465             else if( p_sys->i_subtitles > 0 )
466             {
467                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
468                       (double)p_sys->i_length;
469             }
470             else
471             {
472                 *pf = 0.0;
473             }
474             return VLC_SUCCESS;
475
476         case DEMUX_SET_POSITION:
477             f = (double)va_arg( args, double );
478             i64 = f * p_sys->i_length;
479
480             p_sys->i_subtitle = 0;
481             while( p_sys->i_subtitle < p_sys->i_subtitles &&
482                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
483             {
484                 p_sys->i_subtitle++;
485             }
486             if( p_sys->i_subtitle >= p_sys->i_subtitles )
487                 return VLC_EGENERIC;
488             return VLC_SUCCESS;
489
490         case DEMUX_SET_NEXT_DEMUX_TIME:
491             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
492             return VLC_SUCCESS;
493
494         case DEMUX_GET_FPS:
495         case DEMUX_GET_META:
496         case DEMUX_GET_TITLE_INFO:
497             return VLC_EGENERIC;
498
499         default:
500             msg_Err( p_demux, "unknown query in subtitle control" );
501             return VLC_EGENERIC;
502     }
503 }
504
505 /*****************************************************************************
506  * Demux: Send subtitle to decoder
507  *****************************************************************************/
508 static int Demux( demux_t *p_demux )
509 {
510     demux_sys_t *p_sys = p_demux->p_sys;
511     int64_t i_maxdate;
512
513     if( p_sys->i_subtitle >= p_sys->i_subtitles )
514         return 0;
515
516     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
517     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
518     {
519         /* Should not happen */
520         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
521     }
522
523     while( p_sys->i_subtitle < p_sys->i_subtitles &&
524            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
525     {
526         block_t *p_block;
527         int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
528
529         if( i_len <= 1 )
530         {
531             /* empty subtitle */
532             p_sys->i_subtitle++;
533             continue;
534         }
535
536         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
537         {
538             p_sys->i_subtitle++;
539             continue;
540         }
541
542         if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
543         {
544             p_sys->i_subtitle++;
545             continue;
546         }
547
548         p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
549         p_block->i_dts = p_block->i_pts;
550         if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
551         {
552             p_block->i_length =
553                 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
554         }
555
556         memcpy( p_block->p_buffer,
557                 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
558         if( p_block->i_pts > 0 )
559         {
560             es_out_Send( p_demux->out, p_sys->es, p_block );
561         }
562         else
563         {
564             block_Release( p_block );
565         }
566         p_sys->i_subtitle++;
567     }
568
569     /* */
570     p_sys->i_next_demux_date = 0;
571
572     return 1;
573 }
574
575 /*****************************************************************************
576  * Fix: fix time stamp and order of subtitle
577  *****************************************************************************/
578 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
579 static void Fix( demux_t *p_demux )
580 {
581     demux_sys_t *p_sys = p_demux->p_sys;
582     vlc_bool_t b_done;
583     int     i_index;
584
585     /* *** fix order (to be sure...) *** */
586     /* We suppose that there are near in order and this durty bubble sort
587      * wont take too much time
588      */
589     do
590     {
591         b_done = VLC_TRUE;
592         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
593         {
594             if( p_sys->subtitle[i_index].i_start <
595                     p_sys->subtitle[i_index - 1].i_start )
596             {
597                 subtitle_t sub_xch;
598                 memcpy( &sub_xch,
599                         p_sys->subtitle + i_index - 1,
600                         sizeof( subtitle_t ) );
601                 memcpy( p_sys->subtitle + i_index - 1,
602                         p_sys->subtitle + i_index,
603                         sizeof( subtitle_t ) );
604                 memcpy( p_sys->subtitle + i_index,
605                         &sub_xch,
606                         sizeof( subtitle_t ) );
607                 b_done = VLC_FALSE;
608             }
609         }
610     } while( !b_done );
611 }
612 #endif
613
614 static int TextLoad( text_t *txt, stream_t *s )
615 {
616     int   i_line_max;
617
618     /* init txt */
619     i_line_max          = 500;
620     txt->i_line_count   = 0;
621     txt->i_line         = 0;
622     txt->line           = calloc( i_line_max, sizeof( char * ) );
623
624     /* load the complete file */
625     for( ;; )
626     {
627         char *psz = stream_ReadLine( s );
628
629         if( psz == NULL )
630             break;
631
632         txt->line[txt->i_line_count++] = psz;
633         if( txt->i_line_count >= i_line_max )
634         {
635             i_line_max += 100;
636             txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
637         }
638     }
639
640     if( txt->i_line_count <= 0 )
641     {
642         free( txt->line );
643         return VLC_EGENERIC;
644     }
645
646     return VLC_SUCCESS;
647 }
648 static void TextUnload( text_t *txt )
649 {
650     int i;
651
652     for( i = 0; i < txt->i_line_count; i++ )
653     {
654         free( txt->line[i] );
655     }
656     free( txt->line );
657     txt->i_line       = 0;
658     txt->i_line_count = 0;
659 }
660
661 static char *TextGetLine( text_t *txt )
662 {
663     if( txt->i_line >= txt->i_line_count )
664         return( NULL );
665
666     return txt->line[txt->i_line++];
667 }
668 static void TextPreviousLine( text_t *txt )
669 {
670     if( txt->i_line > 0 )
671         txt->i_line--;
672 }
673
674 /*****************************************************************************
675  * Specific Subtitle function
676  *****************************************************************************/
677 #define MAX_LINE 8192
678 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
679 {
680     demux_sys_t *p_sys = p_demux->p_sys;
681     text_t      *txt = &p_sys->txt;
682     /*
683      * each line:
684      *  {n1}{n2}Line1|Line2|Line3....
685      * where n1 and n2 are the video frame number...
686      * {n2} can also be {}
687      */
688     char *s;
689
690     char buffer_text[MAX_LINE + 1];
691     int    i_start;
692     int    i_stop;
693     unsigned int i;
694
695     int i_microsecperframe = 40000; /* default to 25 fps */
696     if( p_sys->i_microsecperframe > 0 ) 
697         i_microsecperframe = p_sys->i_microsecperframe;
698     
699     p_subtitle->i_start = 0;
700     p_subtitle->i_stop  = 0;
701     p_subtitle->psz_text = NULL;
702
703     for( ;; )
704     {
705         if( ( s = TextGetLine( txt ) ) == NULL )
706         {
707             return( VLC_EGENERIC );
708         }
709         i_start = 0;
710         i_stop  = 0;
711
712         memset( buffer_text, '\0', MAX_LINE );
713         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
714             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
715         {
716             break;
717         }
718     }
719     /* replace | by \n */
720     for( i = 0; i < strlen( buffer_text ); i++ )
721     {
722         if( buffer_text[i] == '|' )
723         {
724             buffer_text[i] = '\n';
725         }
726     }
727
728     p_subtitle->i_start = (int64_t)i_start * i_microsecperframe;
729     p_subtitle->i_stop  = (int64_t)i_stop  * i_microsecperframe;
730     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
731     return( 0 );
732 }
733
734 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
735 {
736     demux_sys_t *p_sys = p_demux->p_sys;
737     text_t      *txt = &p_sys->txt;
738
739     /*
740      * n
741      * h1:m1:s1,d1 --> h2:m2:s2,d2
742      * Line1
743      * Line2
744      * ...
745      * [empty line]
746      *
747      */
748     char *s;
749     char buffer_text[ 10 * MAX_LINE];
750     int  i_buffer_text;
751     int64_t     i_start;
752     int64_t     i_stop;
753
754     p_subtitle->i_start = 0;
755     p_subtitle->i_stop  = 0;
756     p_subtitle->psz_text = NULL;
757
758     for( ;; )
759     {
760         int h1, m1, s1, d1, h2, m2, s2, d2;
761         if( ( s = TextGetLine( txt ) ) == NULL )
762         {
763             return( VLC_EGENERIC );
764         }
765         if( sscanf( s,
766                     "%d:%d:%d,%d --> %d:%d:%d,%d",
767                     &h1, &m1, &s1, &d1,
768                     &h2, &m2, &s2, &d2 ) == 8 )
769         {
770             i_start = ( (int64_t)h1 * 3600*1000 +
771                         (int64_t)m1 * 60*1000 +
772                         (int64_t)s1 * 1000 +
773                         (int64_t)d1 ) * 1000;
774
775             i_stop  = ( (int64_t)h2 * 3600*1000 +
776                         (int64_t)m2 * 60*1000 +
777                         (int64_t)s2 * 1000 +
778                         (int64_t)d2 ) * 1000;
779
780             /* Now read text until an empty line */
781             for( i_buffer_text = 0;; )
782             {
783                 int i_len;
784                 if( ( s = TextGetLine( txt ) ) == NULL )
785                 {
786                     return( VLC_EGENERIC );
787                 }
788
789                 i_len = strlen( s );
790                 if( i_len <= 0 )
791                 {
792                     /* empty line -> end of this subtitle */
793                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
794                     p_subtitle->i_start = i_start;
795                     p_subtitle->i_stop = i_stop;
796                     p_subtitle->psz_text = strdup( buffer_text );
797                     /* If framerate is available, use sub-fps */
798                     if( p_sys->i_microsecperframe != 0 &&
799                         p_sys->i_original_mspf != 0)
800                     {
801                         p_subtitle->i_start = (int64_t)i_start *
802                                               p_sys->i_microsecperframe/
803                                               p_sys->i_original_mspf;
804                         p_subtitle->i_stop  = (int64_t)i_stop  *
805                                               p_sys->i_microsecperframe /
806                                               p_sys->i_original_mspf;
807                     }
808                     return 0;
809                 }
810                 else
811                 {
812                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
813                     {
814                         memcpy( buffer_text + i_buffer_text,
815                                 s,
816                                 i_len );
817                         i_buffer_text += i_len;
818
819                         buffer_text[i_buffer_text] = '\n';
820                         i_buffer_text++;
821                     }
822                 }
823             }
824         }
825     }
826 }
827
828 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
829 {
830     demux_sys_t *p_sys = p_demux->p_sys;
831     text_t      *txt = &p_sys->txt;
832
833     /*
834      * h1:m1:s1.d1,h2:m2:s2.d2
835      * Line1[br]Line2
836      * Line3
837      * ...
838      * [empty line]
839      * ( works with subviewer and subviewer v2 )
840      */
841     char *s;
842     char buffer_text[ 10 * MAX_LINE];
843     int  i_buffer_text;
844     int64_t     i_start;
845     int64_t     i_stop;
846
847     p_subtitle->i_start = 0;
848     p_subtitle->i_stop  = 0;
849     p_subtitle->psz_text = NULL;
850
851     for( ;; )
852     {
853         int h1, m1, s1, d1, h2, m2, s2, d2;
854         if( ( s = TextGetLine( txt ) ) == NULL )
855         {
856             return( VLC_EGENERIC );
857         }
858         if( sscanf( s,
859                     "%d:%d:%d.%d,%d:%d:%d.%d",
860                     &h1, &m1, &s1, &d1,
861                     &h2, &m2, &s2, &d2 ) == 8 )
862         {
863             i_start = ( (int64_t)h1 * 3600*1000 +
864                         (int64_t)m1 * 60*1000 +
865                         (int64_t)s1 * 1000 +
866                         (int64_t)d1 ) * 1000;
867
868             i_stop  = ( (int64_t)h2 * 3600*1000 +
869                         (int64_t)m2 * 60*1000 +
870                         (int64_t)s2 * 1000 +
871                         (int64_t)d2 ) * 1000;
872
873             /* Now read text until an empty line */
874             for( i_buffer_text = 0;; )
875             {
876                 int i_len, i;
877                 if( ( s = TextGetLine( txt ) ) == NULL )
878                 {
879                     return( VLC_EGENERIC );
880                 }
881
882                 i_len = strlen( s );
883                 if( i_len <= 0 )
884                 {
885                     /* empty line -> end of this subtitle */
886                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
887                     p_subtitle->i_start = i_start;
888                     p_subtitle->i_stop = i_stop;
889
890                     /* replace [br] by \n */
891                     for( i = 0; i < i_buffer_text - 3; i++ )
892                     {
893                         if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
894                             buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
895                         {
896                             char *temp = buffer_text + i + 1;
897                             buffer_text[i] = '\n';
898                             memmove( temp, temp+3, strlen( temp ) -3 );
899                             temp[strlen( temp )-3] = '\0';
900                         }
901                     }
902                     p_subtitle->psz_text = strdup( buffer_text );
903                     return( 0 );
904                 }
905                 else
906                 {
907                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
908                     {
909                         memcpy( buffer_text + i_buffer_text,
910                                 s,
911                                 i_len );
912                         i_buffer_text += i_len;
913
914                         buffer_text[i_buffer_text] = '\n';
915                         i_buffer_text++;
916                     }
917                 }
918             }
919         }
920     }
921 }
922
923
924 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
925 {
926     demux_sys_t *p_sys = p_demux->p_sys;
927     text_t      *txt = &p_sys->txt;
928
929     char buffer_text[ 10 * MAX_LINE];
930     char buffer_text2[ 10 * MAX_LINE];
931     char *s;
932     int64_t     i_start;
933     int64_t     i_stop;
934
935     p_subtitle->i_start = 0;
936     p_subtitle->i_stop  = 0;
937     p_subtitle->psz_text = NULL;
938
939     for( ;; )
940     {
941         int h1, m1, s1, c1, h2, m2, s2, c2;
942
943         if( ( s = TextGetLine( txt ) ) == NULL )
944         {
945             return( VLC_EGENERIC );
946         }
947         p_subtitle->psz_text = malloc( strlen( s ) );
948
949         /* We expect (SSA2-4):
950          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
951          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
952          *
953          * 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.
954          */
955
956         /* For ASS:
957          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
958          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
959          */
960         if( sscanf( s,
961                     "Dialogue: %[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
962                     buffer_text2,
963                     &h1, &m1, &s1, &c1,
964                     &h2, &m2, &s2, &c2,
965                     buffer_text ) == 10 )
966         {
967             i_start = ( (int64_t)h1 * 3600*1000 +
968                         (int64_t)m1 * 60*1000 +
969                         (int64_t)s1 * 1000 +
970                         (int64_t)c1 * 10 ) * 1000;
971
972             i_stop  = ( (int64_t)h2 * 3600*1000 +
973                         (int64_t)m2 * 60*1000 +
974                         (int64_t)s2 * 1000 +
975                         (int64_t)c2 * 10 ) * 1000;
976
977             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
978             /* (Layer comes from ASS specs ... it's empty for SSA.) */
979             if( p_sys->i_type == SUB_TYPE_SSA1 )
980             {
981                 sprintf( p_subtitle->psz_text,
982                          ",%s", strdup( buffer_text) ); /* SSA1 has only 8 commas before the text starts, not 9 */
983             }
984             else
985             {
986                 sprintf( p_subtitle->psz_text,
987                          ",,%s", strdup( buffer_text) ); /* ReadOrder, Layer, %s(rest of fields) */
988             }
989             p_subtitle->i_start = i_start;
990             p_subtitle->i_stop = i_stop;
991             return 0;
992         }
993         else
994         {
995             /* All the other stuff we add to the header field */
996             if( p_sys->psz_header != NULL )
997             {
998                 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
999                           strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
1000                 {
1001                     msg_Err( p_demux, "out of memory");
1002                     return VLC_ENOMEM;
1003                 }
1004                 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
1005                 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1006             }
1007             else
1008             {
1009                 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1010                 {
1011                     msg_Err( p_demux, "out of memory");
1012                     return VLC_ENOMEM;
1013                 }
1014                 p_sys->psz_header = strdup( s );
1015                 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1016             }
1017         }
1018     }
1019 }
1020
1021 static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1022 {
1023     demux_sys_t *p_sys = p_demux->p_sys;
1024     text_t      *txt = &p_sys->txt;
1025
1026     /*
1027      * each line:
1028      *  h:m:s:Line1|Line2|Line3....
1029      *  or
1030      *  h:m:s Line1|Line2|Line3....
1031      *
1032      */
1033     char *p;
1034     char buffer_text[MAX_LINE + 1];
1035     int64_t    i_start;
1036     unsigned int i;
1037
1038     p_subtitle->i_start = 0;
1039     p_subtitle->i_stop  = 0;
1040     p_subtitle->psz_text = NULL;
1041
1042     for( ;; )
1043     {
1044         int h, m, s;
1045         char c;
1046
1047         if( ( p = TextGetLine( txt ) ) == NULL )
1048         {
1049             return( VLC_EGENERIC );
1050         }
1051
1052         i_start = 0;
1053
1054         memset( buffer_text, '\0', MAX_LINE );
1055         if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1056         {
1057             i_start = ( (int64_t)h * 3600*1000 +
1058                         (int64_t)m * 60*1000 +
1059                         (int64_t)s * 1000 ) * 1000;
1060             break;
1061         }
1062     }
1063
1064     /* replace | by \n */
1065     for( i = 0; i < strlen( buffer_text ); i++ )
1066     {
1067         if( buffer_text[i] == '|' )
1068         {
1069             buffer_text[i] = '\n';
1070         }
1071     }
1072     p_subtitle->i_start = i_start;
1073
1074     p_subtitle->i_stop  = 0;
1075     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1076     return( 0 );
1077 }
1078
1079 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1080 {
1081     if( psz_start )
1082     {
1083         if( strcasestr( psz_start, psz_str ) )
1084         {
1085             char *s = strcasestr( psz_start, psz_str );
1086
1087             s += strlen( psz_str );
1088
1089             return( s );
1090         }
1091     }
1092     for( ;; )
1093     {
1094         char *p;
1095         if( ( p = TextGetLine( txt ) ) == NULL )
1096         {
1097             return NULL;
1098         }
1099         if( strcasestr( p, psz_str ) )
1100         {
1101             char *s = strcasestr( p, psz_str );
1102
1103             s += strlen( psz_str );
1104
1105             return(  s);
1106         }
1107     }
1108 }
1109
1110 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1111 {
1112     demux_sys_t *p_sys = p_demux->p_sys;
1113     text_t      *txt = &p_sys->txt;
1114
1115     char *p;
1116     int64_t i_start;
1117
1118     int  i_text;
1119     char buffer_text[10*MAX_LINE + 1];
1120
1121     p_subtitle->i_start = 0;
1122     p_subtitle->i_stop  = 0;
1123     p_subtitle->psz_text = NULL;
1124
1125 #define ADDC( c ) \
1126     if( i_text < 10*MAX_LINE )      \
1127     {                               \
1128         buffer_text[i_text++] = c;  \
1129         buffer_text[i_text] = '\0'; \
1130     }
1131
1132     /* search "Start=" */
1133     if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1134     {
1135         return VLC_EGENERIC;
1136     }
1137
1138     /* get start value */
1139     i_start = strtol( p, &p, 0 );
1140
1141     /* search <P */
1142     if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1143     {
1144         return VLC_EGENERIC;
1145     }
1146     /* search > */
1147     if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1148     {
1149         return VLC_EGENERIC;
1150     }
1151
1152     i_text = 0;
1153     buffer_text[0] = '\0';
1154     /* now get all txt until  a "Start=" line */
1155     for( ;; )
1156     {
1157         if( *p )
1158         {
1159             if( *p == '<' )
1160             {
1161                 if( !strncasecmp( p, "<br", 3 ) )
1162                 {
1163                     ADDC( '\n' );
1164                 }
1165                 else if( strcasestr( p, "Start=" ) )
1166                 {
1167                     TextPreviousLine( txt );
1168                     break;
1169                 }
1170                 p = ParseSamiSearch( txt, p, ">" );
1171             }
1172             else if( !strncmp( p, "&nbsp;", 6 ) )
1173             {
1174                 ADDC( ' ' );
1175                 p += 6;
1176             }
1177             else if( *p == '\t' )
1178             {
1179                 ADDC( ' ' );
1180                 p++;
1181             }
1182             else
1183             {
1184                 ADDC( *p );
1185                 p++;
1186             }
1187         }
1188         else
1189         {
1190             p = TextGetLine( txt );
1191         }
1192
1193         if( p == NULL )
1194         {
1195             break;
1196         }
1197     }
1198
1199     p_subtitle->i_start = i_start * 1000;
1200     p_subtitle->i_stop  = 0;
1201     p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1202
1203     return( VLC_SUCCESS );
1204 #undef ADDC
1205 }