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