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