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