]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
forwardport [18177]
[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 #include <vlc_input.h>
30
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include <errno.h>
35 #ifdef HAVE_SYS_TYPES_H
36 #   include <sys/types.h>
37 #endif
38 #include <ctype.h>
39
40 #include <vlc_demux.h>
41 #include <vlc_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     N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
51 #define SUB_FPS_LONGTEXT \
52     N_("Override the normal frames per second settings. " \
53     "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
54 #define SUB_TYPE_LONGTEXT \
55     N_("Force the subtiles format. Valid values are : \"microdvd\", " \
56     "\"subrip\",  \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
57     "\"sami\", \"dvdsubtitle\" and \"auto\" (meaning autodetection, this " \
58     "should always work).")
59 static const 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, NULL, NULL );
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     int64_t     i_microsecperframe;
128
129     char        *psz_header;
130     int         i_subtitle;
131     int         i_subtitles;
132     subtitle_t  *subtitle;
133
134     int64_t     i_length;
135 };
136
137 static int  ParseMicroDvd   ( demux_t *, subtitle_t * );
138 static int  ParseSubRip     ( demux_t *, subtitle_t * );
139 static int  ParseSubViewer  ( demux_t *, subtitle_t * );
140 static int  ParseSSA        ( demux_t *, subtitle_t * );
141 static int  ParseVplayer    ( demux_t *, subtitle_t * );
142 static int  ParseSami       ( demux_t *, subtitle_t * );
143 static int  ParseDVDSubtitle( demux_t *, subtitle_t * );
144
145 static struct
146 {
147     const char *psz_type_name;
148     int  i_type;
149     const char *psz_name;
150     int  (*pf_read)( demux_t *, subtitle_t* );
151 } sub_read_subtitle_function [] =
152 {
153     { "microdvd",   SUB_TYPE_MICRODVD,    "MicroDVD",    ParseMicroDvd },
154     { "subrip",     SUB_TYPE_SUBRIP,      "SubRIP",      ParseSubRip },
155     { "subviewer",  SUB_TYPE_SUBVIEWER,   "SubViewer",   ParseSubViewer },
156     { "ssa1",       SUB_TYPE_SSA1,        "SSA-1",       ParseSSA },
157     { "ssa2-4",     SUB_TYPE_SSA2_4,      "SSA-2/3/4",   ParseSSA },
158     { "ass",        SUB_TYPE_ASS,         "SSA/ASS",     ParseSSA },
159     { "vplayer",    SUB_TYPE_VPLAYER,     "VPlayer",     ParseVplayer },
160     { "sami",       SUB_TYPE_SAMI,        "SAMI",        ParseSami },
161     { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
162     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
163 };
164
165 static int Demux( demux_t * );
166 static int Control( demux_t *, int, va_list );
167
168 /*static void Fix( demux_t * );*/
169
170 /*****************************************************************************
171  * Module initializer
172  *****************************************************************************/
173 static int Open ( vlc_object_t *p_this )
174 {
175     demux_t        *p_demux = (demux_t*)p_this;
176     demux_sys_t    *p_sys;
177     es_format_t    fmt;
178     input_thread_t *p_input;
179     float          f_fps;
180     char           *psz_type;
181     int  (*pf_read)( demux_t *, subtitle_t* );
182     int            i, i_max;
183
184     if( strcmp( p_demux->psz_demux, "subtitle" ) )
185     {
186         msg_Dbg( p_demux, "subtitle demux discarded" );
187         return VLC_EGENERIC;
188     }
189
190     p_demux->pf_demux = Demux;
191     p_demux->pf_control = Control;
192     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
193     p_sys->psz_header         = NULL;
194     p_sys->i_subtitle         = 0;
195     p_sys->i_subtitles        = 0;
196     p_sys->subtitle           = NULL;
197     p_sys->i_microsecperframe = 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         msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
205     }
206
207     p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
208     if( p_input )
209     {
210         f_fps = var_GetFloat( p_input, "sub-original-fps" );
211         if( f_fps >= 1.0 )
212             p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
213
214         msg_Dbg( p_demux, "Movie fps: %f", f_fps );
215         vlc_object_release( p_input );
216     }
217
218     /* Check for override of the fps */
219     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
220     if( f_fps >= 1.0 )
221     {
222         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
223     }
224
225     /* Get or probe the type */
226     p_sys->i_type = SUB_TYPE_UNKNOWN;
227     psz_type = var_CreateGetString( p_demux, "sub-type" );
228     if( *psz_type )
229     {
230         int i;
231
232         for( i = 0; ; i++ )
233         {
234             if( sub_read_subtitle_function[i].psz_type_name == NULL )
235                 break;
236
237             if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
238                          psz_type ) )
239             {
240                 p_sys->i_type = sub_read_subtitle_function[i].i_type;
241                 break;
242             }
243         }
244     }
245     free( psz_type );
246
247     /* Probe if unknown type */
248     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
249     {
250         int     i_try;
251         char    *s = NULL;
252
253         msg_Dbg( p_demux, "autodetecting subtitle format" );
254         for( i_try = 0; i_try < 256; i_try++ )
255         {
256             int i_dummy;
257
258             if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
259                 break;
260
261             if( strcasestr( s, "<SAMI>" ) )
262             {
263                 p_sys->i_type = SUB_TYPE_SAMI;
264                 break;
265             }
266             else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
267                      sscanf( s, "{%d}{}", &i_dummy ) == 1)
268             {
269                 p_sys->i_type = SUB_TYPE_MICRODVD;
270                 break;
271             }
272             else if( sscanf( s,
273                              "%d:%d:%d,%d --> %d:%d:%d,%d",
274                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
275                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
276             {
277                 p_sys->i_type = SUB_TYPE_SUBRIP;
278                 break;
279             }
280             else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
281             {
282                 p_sys->i_type = SUB_TYPE_SSA1;
283                 break;
284             }
285             else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
286             {
287                 p_sys->i_type = SUB_TYPE_ASS;
288                 break;
289             }
290             else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
291             {
292                 p_sys->i_type = SUB_TYPE_SSA2_4;
293                 break;
294             }
295             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
296             {
297                 p_sys->i_type = SUB_TYPE_SSA2_4;
298                 break;
299             }
300             else if( !strncasecmp( s, "Dialogue:", 9  ) )
301             {
302                 p_sys->i_type = SUB_TYPE_ASS;
303                 break;
304             }
305             else if( strcasestr( s, "[INFORMATION]" ) )
306             {
307                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
308                 break;
309             }
310             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
311                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
312             {
313                 p_sys->i_type = SUB_TYPE_VPLAYER;
314                 break;
315             }
316             else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
317                              &i_dummy, &i_dummy ) == 4 )
318             {
319                 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
320                 break;
321             }
322
323             free( s );
324             s = NULL;
325         }
326
327         if( s ) free( s );
328
329         /* It will nearly always work even for non seekable stream thanks the
330          * caching system, and if it fails we lose just a few sub */
331         if( stream_Seek( p_demux->s, 0 ) )
332         {
333             msg_Warn( p_demux, "failed to rewind" );
334         }
335     }
336     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
337     {
338         msg_Err( p_demux, "failed to recognize subtitle type" );
339         free( p_sys );
340         return VLC_EGENERIC;
341     }
342
343     for( i = 0; ; i++ )
344     {
345         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
346         {
347             msg_Dbg( p_demux, "detected %s format",
348                      sub_read_subtitle_function[i].psz_name );
349             pf_read = sub_read_subtitle_function[i].pf_read;
350             break;
351         }
352     }
353
354     msg_Dbg( p_demux, "loading all subtitles..." );
355
356     /* Load the whole file */
357     TextLoad( &p_sys->txt, p_demux->s );
358
359     /* Parse it */
360     for( i_max = 0;; )
361     {
362         if( p_sys->i_subtitles >= i_max )
363         {
364             i_max += 500;
365             if( !( p_sys->subtitle = realloc( p_sys->subtitle,
366                                               sizeof(subtitle_t) * i_max ) ) )
367             {
368                 msg_Err( p_demux, "out of memory");
369                 if( p_sys->subtitle != NULL )
370                     free( p_sys->subtitle );
371                 TextUnload( &p_sys->txt );
372                 free( p_sys );
373                 return VLC_ENOMEM;
374             }
375         }
376
377         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
378             break;
379
380         p_sys->i_subtitles++;
381     }
382     /* Unload */
383     TextUnload( &p_sys->txt );
384
385     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
386
387     /* Fix subtitle (order and time) *** */
388     p_sys->i_subtitle = 0;
389     p_sys->i_length = 0;
390     if( p_sys->i_subtitles > 0 )
391     {
392         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
393         /* +1 to avoid 0 */
394         if( p_sys->i_length <= 0 )
395             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
396     }
397
398     /* *** add subtitle ES *** */
399     if( p_sys->i_type == SUB_TYPE_SSA1 ||
400              p_sys->i_type == SUB_TYPE_SSA2_4 ||
401              p_sys->i_type == SUB_TYPE_ASS )
402     {
403         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
404     }
405     else
406     {
407         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
408     }
409     if( p_sys->psz_header != NULL )
410     {
411         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
412         fmt.p_extra = strdup( p_sys->psz_header );
413     }
414     p_sys->es = es_out_Add( p_demux->out, &fmt );
415
416     return VLC_SUCCESS;
417 }
418
419 /*****************************************************************************
420  * Close: Close subtitle demux
421  *****************************************************************************/
422 static void Close( vlc_object_t *p_this )
423 {
424     demux_t *p_demux = (demux_t*)p_this;
425     demux_sys_t *p_sys = p_demux->p_sys;
426     int i;
427
428     for( i = 0; i < p_sys->i_subtitles; i++ )
429     {
430         if( p_sys->subtitle[i].psz_text )
431             free( p_sys->subtitle[i].psz_text );
432     }
433     if( p_sys->subtitle )
434         free( p_sys->subtitle );
435
436     free( p_sys );
437 }
438
439 /*****************************************************************************
440  * Control:
441  *****************************************************************************/
442 static int Control( demux_t *p_demux, int i_query, va_list args )
443 {
444     demux_sys_t *p_sys = p_demux->p_sys;
445     int64_t *pi64, i64;
446     double *pf, f;
447
448     switch( i_query )
449     {
450         case DEMUX_GET_LENGTH:
451             pi64 = (int64_t*)va_arg( args, int64_t * );
452             *pi64 = p_sys->i_length;
453             return VLC_SUCCESS;
454
455         case DEMUX_GET_TIME:
456             pi64 = (int64_t*)va_arg( args, int64_t * );
457             if( p_sys->i_subtitle < p_sys->i_subtitles )
458             {
459                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
460                 return VLC_SUCCESS;
461             }
462             return VLC_EGENERIC;
463
464         case DEMUX_SET_TIME:
465             i64 = (int64_t)va_arg( args, int64_t );
466             p_sys->i_subtitle = 0;
467             while( p_sys->i_subtitle < p_sys->i_subtitles &&
468                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
469             {
470                 p_sys->i_subtitle++;
471             }
472
473             if( p_sys->i_subtitle >= p_sys->i_subtitles )
474                 return VLC_EGENERIC;
475             return VLC_SUCCESS;
476
477         case DEMUX_GET_POSITION:
478             pf = (double*)va_arg( args, double * );
479             if( p_sys->i_subtitle >= p_sys->i_subtitles )
480             {
481                 *pf = 1.0;
482             }
483             else if( p_sys->i_subtitles > 0 )
484             {
485                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
486                       (double)p_sys->i_length;
487             }
488             else
489             {
490                 *pf = 0.0;
491             }
492             return VLC_SUCCESS;
493
494         case DEMUX_SET_POSITION:
495             f = (double)va_arg( args, double );
496             i64 = f * p_sys->i_length;
497
498             p_sys->i_subtitle = 0;
499             while( p_sys->i_subtitle < p_sys->i_subtitles &&
500                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
501             {
502                 p_sys->i_subtitle++;
503             }
504             if( p_sys->i_subtitle >= p_sys->i_subtitles )
505                 return VLC_EGENERIC;
506             return VLC_SUCCESS;
507
508         case DEMUX_SET_NEXT_DEMUX_TIME:
509             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
510             return VLC_SUCCESS;
511
512         case DEMUX_GET_FPS:
513         case DEMUX_GET_META:
514         case DEMUX_GET_TITLE_INFO:
515             return VLC_EGENERIC;
516
517         default:
518             msg_Err( p_demux, "unknown query in subtitle control" );
519             return VLC_EGENERIC;
520     }
521 }
522
523 /*****************************************************************************
524  * Demux: Send subtitle to decoder
525  *****************************************************************************/
526 static int Demux( demux_t *p_demux )
527 {
528     demux_sys_t *p_sys = p_demux->p_sys;
529     int64_t i_maxdate;
530
531     if( p_sys->i_subtitle >= p_sys->i_subtitles )
532         return 0;
533
534     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
535     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
536     {
537         /* Should not happen */
538         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
539     }
540
541     while( p_sys->i_subtitle < p_sys->i_subtitles &&
542            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
543     {
544         block_t *p_block;
545         int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
546
547         if( i_len <= 1 )
548         {
549             /* empty subtitle */
550             p_sys->i_subtitle++;
551             continue;
552         }
553
554         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
555         {
556             p_sys->i_subtitle++;
557             continue;
558         }
559
560         if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
561         {
562             p_sys->i_subtitle++;
563             continue;
564         }
565
566         p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
567         p_block->i_dts = p_block->i_pts;
568         if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
569         {
570             p_block->i_length =
571                 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
572         }
573
574         memcpy( p_block->p_buffer,
575                 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
576         if( p_block->i_pts > 0 )
577         {
578             es_out_Send( p_demux->out, p_sys->es, p_block );
579         }
580         else
581         {
582             block_Release( p_block );
583         }
584         p_sys->i_subtitle++;
585     }
586
587     /* */
588     p_sys->i_next_demux_date = 0;
589
590     return 1;
591 }
592
593 /*****************************************************************************
594  * Fix: fix time stamp and order of subtitle
595  *****************************************************************************/
596 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
597 static void Fix( demux_t *p_demux )
598 {
599     demux_sys_t *p_sys = p_demux->p_sys;
600     vlc_bool_t b_done;
601     int     i_index;
602
603     /* *** fix order (to be sure...) *** */
604     /* We suppose that there are near in order and this durty bubble sort
605      * wont take too much time
606      */
607     do
608     {
609         b_done = VLC_TRUE;
610         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
611         {
612             if( p_sys->subtitle[i_index].i_start <
613                     p_sys->subtitle[i_index - 1].i_start )
614             {
615                 subtitle_t sub_xch;
616                 memcpy( &sub_xch,
617                         p_sys->subtitle + i_index - 1,
618                         sizeof( subtitle_t ) );
619                 memcpy( p_sys->subtitle + i_index - 1,
620                         p_sys->subtitle + i_index,
621                         sizeof( subtitle_t ) );
622                 memcpy( p_sys->subtitle + i_index,
623                         &sub_xch,
624                         sizeof( subtitle_t ) );
625                 b_done = VLC_FALSE;
626             }
627         }
628     } while( !b_done );
629 }
630 #endif
631
632 static int TextLoad( text_t *txt, stream_t *s )
633 {
634     int   i_line_max;
635
636     /* init txt */
637     i_line_max          = 500;
638     txt->i_line_count   = 0;
639     txt->i_line         = 0;
640     txt->line           = calloc( i_line_max, sizeof( char * ) );
641
642     /* load the complete file */
643     for( ;; )
644     {
645         char *psz = stream_ReadLine( s );
646
647         if( psz == NULL )
648             break;
649
650         txt->line[txt->i_line_count++] = psz;
651         if( txt->i_line_count >= i_line_max )
652         {
653             i_line_max += 100;
654             txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
655         }
656     }
657
658     if( txt->i_line_count <= 0 )
659     {
660         free( txt->line );
661         return VLC_EGENERIC;
662     }
663
664     return VLC_SUCCESS;
665 }
666 static void TextUnload( text_t *txt )
667 {
668     int i;
669
670     for( i = 0; i < txt->i_line_count; i++ )
671     {
672         free( txt->line[i] );
673     }
674     free( txt->line );
675     txt->i_line       = 0;
676     txt->i_line_count = 0;
677 }
678
679 static char *TextGetLine( text_t *txt )
680 {
681     if( txt->i_line >= txt->i_line_count )
682         return( NULL );
683
684     return txt->line[txt->i_line++];
685 }
686 static void TextPreviousLine( text_t *txt )
687 {
688     if( txt->i_line > 0 )
689         txt->i_line--;
690 }
691
692 /*****************************************************************************
693  * Specific Subtitle function
694  *****************************************************************************/
695 #define MAX_LINE 8192
696 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
697 {
698     demux_sys_t *p_sys = p_demux->p_sys;
699     text_t      *txt = &p_sys->txt;
700     /*
701      * each line:
702      *  {n1}{n2}Line1|Line2|Line3....
703      * where n1 and n2 are the video frame number...
704      * {n2} can also be {}
705      */
706     char *s;
707
708     char buffer_text[MAX_LINE + 1];
709     int    i_start;
710     int    i_stop;
711     unsigned int i;
712
713     /* Try sub-fps value if set, movie rate if know, else 25fps (40000) */
714     int i_microsecperframe = p_sys->i_original_mspf > 0 ? p_sys->i_original_mspf : 40000;
715     if( p_sys->i_microsecperframe > 0 )
716         i_microsecperframe = p_sys->i_microsecperframe;
717
718     p_subtitle->i_start = 0;
719     p_subtitle->i_stop  = 0;
720     p_subtitle->psz_text = NULL;
721
722 next:
723     for( ;; )
724     {
725         if( ( s = TextGetLine( txt ) ) == NULL )
726         {
727             return( VLC_EGENERIC );
728         }
729         i_start = 0;
730         i_stop  = 0;
731
732         memset( buffer_text, '\0', MAX_LINE );
733         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
734             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
735         {
736             break;
737         }
738     }
739     if( i_start == 1 && i_stop == 1 )
740     {
741         /* We found a possible setting of the framerate "{1}{1}23.976" */
742         /* Check if it's usable, and if the sub-fps is not set */
743         float tmp = us_strtod( buffer_text, NULL );
744         if( tmp > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
745             p_sys->i_microsecperframe = (int64_t)( (float)1000000 / tmp );
746         goto next;
747     }
748
749     /* replace | by \n */
750     for( i = 0; i < strlen( buffer_text ); i++ )
751     {
752         if( buffer_text[i] == '|' )
753         {
754             buffer_text[i] = '\n';
755         }
756     }
757
758     p_subtitle->i_start = (int64_t)i_start * p_sys->i_microsecperframe;
759     p_subtitle->i_stop  = (int64_t)i_stop  * p_sys->i_microsecperframe;
760     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
761     return( 0 );
762 }
763
764 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
765 {
766     demux_sys_t *p_sys = p_demux->p_sys;
767     text_t      *txt = &p_sys->txt;
768
769     /*
770      * n
771      * h1:m1:s1,d1 --> h2:m2:s2,d2
772      * Line1
773      * Line2
774      * ...
775      * [empty line]
776      *
777      */
778     char *s;
779     char buffer_text[ 10 * MAX_LINE];
780     int  i_buffer_text;
781     int64_t     i_start;
782     int64_t     i_stop;
783
784     p_subtitle->i_start = 0;
785     p_subtitle->i_stop  = 0;
786     p_subtitle->psz_text = NULL;
787
788     for( ;; )
789     {
790         int h1, m1, s1, d1, h2, m2, s2, d2;
791         if( ( s = TextGetLine( txt ) ) == NULL )
792         {
793             return( VLC_EGENERIC );
794         }
795         if( sscanf( s,
796                     "%d:%d:%d,%d --> %d:%d:%d,%d",
797                     &h1, &m1, &s1, &d1,
798                     &h2, &m2, &s2, &d2 ) == 8 )
799         {
800             i_start = ( (int64_t)h1 * 3600*1000 +
801                         (int64_t)m1 * 60*1000 +
802                         (int64_t)s1 * 1000 +
803                         (int64_t)d1 ) * 1000;
804
805             i_stop  = ( (int64_t)h2 * 3600*1000 +
806                         (int64_t)m2 * 60*1000 +
807                         (int64_t)s2 * 1000 +
808                         (int64_t)d2 ) * 1000;
809
810             /* Now read text until an empty line */
811             for( i_buffer_text = 0;; )
812             {
813                 int i_len;
814                 if( ( s = TextGetLine( txt ) ) == NULL )
815                 {
816                     return( VLC_EGENERIC );
817                 }
818
819                 i_len = strlen( s );
820                 if( i_len <= 0 )
821                 {
822                     /* empty line -> end of this subtitle */
823                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
824                     p_subtitle->i_start = i_start;
825                     p_subtitle->i_stop = i_stop;
826                     p_subtitle->psz_text = strdup( buffer_text );
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, const 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