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