]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
* subtitle: compilation fix.
[vlc] / modules / demux / subtitle.c
1 /*****************************************************************************
2  * subtitle.c: Demux for text and vobsub files.
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <ctype.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc/input.h>
36 #include "vlc_video.h"
37
38 #include "ps.h"
39
40 #if (!defined( WIN32 ) || defined(__MINGW32__))
41 #    include <dirent.h>
42 #endif
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open ( vlc_object_t *p_this );
48 static void Close( vlc_object_t *p_this );
49
50 #define SUB_DELAY_LONGTEXT \
51     "Delay subtitles (in 1/10s)"
52 #define SUB_FPS_LONGTEXT \
53     "Override frames per second. " \
54     "It will only work with MicroDVD subtitles."
55 #define SUB_TYPE_LONGTEXT \
56     "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
57     "\"sami\" (auto for autodetection, it should always work)."
58 static char *ppsz_sub_type[] =
59 {
60     "auto", "microdvd", "subrip", "subviewer", "ssa1",
61     "ssa2-4", "vplayer", "sami", "vobsub"
62 };
63
64 vlc_module_begin();
65     set_description( _("Text subtitles demux") );
66     set_capability( "demux2", 0 );
67     add_float( "sub-fps", 0.0, NULL,
68                N_("Frames per second"),
69                SUB_FPS_LONGTEXT, VLC_TRUE );
70     add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
71                 SUB_TYPE_LONGTEXT, VLC_TRUE );
72         change_string_list( ppsz_sub_type, 0, 0 );
73     set_callbacks( Open, Close );
74
75     add_shortcut( "subtitle" );
76     add_shortcut( "vobsub" );
77 vlc_module_end();
78
79 /*****************************************************************************
80  * Prototypes:
81  *****************************************************************************/
82 enum
83 {
84     SUB_TYPE_UNKNOWN = -1,
85     SUB_TYPE_MICRODVD,
86     SUB_TYPE_SUBRIP,
87     SUB_TYPE_SSA1,
88     SUB_TYPE_SSA2_4,
89     SUB_TYPE_VPLAYER,
90     SUB_TYPE_SAMI,
91     SUB_TYPE_SUBVIEWER,
92     SUB_TYPE_VOBSUB,
93 };
94
95 typedef struct
96 {
97     int     i_line_count;
98     int     i_line;
99     char    **line;
100 } text_t;
101 static int  TextLoad( text_t *, stream_t *s );
102 static void TextUnload( text_t * );
103
104 typedef struct
105 {
106     mtime_t i_start;
107     mtime_t i_stop;
108
109     char    *psz_text;
110     int     i_vobsub_location;
111 } subtitle_t;
112
113
114 struct demux_sys_t
115 {
116     int         i_type;
117     text_t      txt;
118     es_out_id_t *es;
119
120     int64_t     i_next_demux_date;
121
122     int64_t     i_microsecperframe;
123
124     char        *psz_header;
125     int         i_subtitle;
126     int         i_subtitles;
127     subtitle_t  *subtitle;
128     FILE        *p_vobsub_file;
129     mtime_t     i_original_mspf;
130
131     int64_t     i_length;
132 };
133
134 static int  ParseMicroDvd ( demux_t *, subtitle_t * );
135 static int  ParseSubRip   ( demux_t *, subtitle_t * );
136 static int  ParseSubViewer( demux_t *, subtitle_t * );
137 static int  ParseSSA      ( demux_t *, subtitle_t * );
138 static int  ParseVplayer  ( demux_t *, subtitle_t * );
139 static int  ParseSami     ( demux_t *, subtitle_t * );
140 static int  ParseVobSubIDX( demux_t *, subtitle_t * );
141
142 static int  DemuxVobSub( demux_t *, block_t * );
143
144 static struct
145 {
146     char *psz_type_name;
147     int  i_type;
148     char *psz_name;
149     int  (*pf_read)( demux_t *, subtitle_t* );
150 } sub_read_subtitle_function [] =
151 {
152     { "microdvd",   SUB_TYPE_MICRODVD,  "MicroDVD", ParseMicroDvd },
153     { "subrip",     SUB_TYPE_SUBRIP,    "SubRIP",   ParseSubRip },
154     { "subviewer",  SUB_TYPE_SUBVIEWER, "SubViewer",ParseSubViewer },
155     { "ssa1",       SUB_TYPE_SSA1,      "SSA-1",    ParseSSA },
156     { "ssa2-4",     SUB_TYPE_SSA2_4,    "SSA-2/3/4",ParseSSA },
157     { "vplayer",    SUB_TYPE_VPLAYER,   "VPlayer",  ParseVplayer },
158     { "sami",       SUB_TYPE_SAMI,      "SAMI",     ParseSami },
159     { "vobsub",     SUB_TYPE_VOBSUB,    "VobSub",   ParseVobSubIDX },
160     { NULL,         SUB_TYPE_UNKNOWN,   "Unknown",  NULL }
161 };
162
163 static int Demux( demux_t * );
164 static int Control( demux_t *, int, va_list );
165
166 static void Fix( demux_t * );
167
168 #define DVD_VIDEO_LB_LEN 2048
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     float f_fps;
179     char *psz_type;
180     int  (*pf_read)( demux_t *, subtitle_t* );
181     int i, i_max;
182
183     if( strcmp( p_demux->psz_demux, "subtitle" ) &&
184         strcmp( p_demux->psz_demux, "vobsub" ) )
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->p_vobsub_file = NULL;
198     p_sys->i_original_mspf = 0;
199
200
201     /* Get the FPS */
202     p_sys->i_microsecperframe = 40000; /* default to 25 fps */
203     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
204     if( f_fps >= 1.0 )
205     {
206         p_sys->i_microsecperframe = (mtime_t)( (float)1000000 / f_fps );
207     }
208
209     /* Get or probe the type */
210     p_sys->i_type = SUB_TYPE_UNKNOWN;
211     psz_type = var_CreateGetString( p_demux, "sub-type" );
212     if( *psz_type )
213     {
214         int i;
215
216         for( i = 0; ; i++ )
217         {
218             if( sub_read_subtitle_function[i].psz_type_name == NULL )
219                 break;
220
221             if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
222                          psz_type ) )
223             {
224                 p_sys->i_type = sub_read_subtitle_function[i].i_type;
225                 break;
226             }
227         }
228     }
229     free( psz_type );
230
231     /* Probe if unknown type */
232     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
233     {
234         int     i_try;
235         char    *s;
236
237         msg_Dbg( p_demux, "autodetecting subtitle format" );
238         for( i_try = 0; i_try < 256; i_try++ )
239         {
240             int i_dummy;
241
242             if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
243                 break;
244
245             if( strcasestr( s, "<SAMI>" ) )
246             {
247                 p_sys->i_type = SUB_TYPE_SAMI;
248                 break;
249             }
250             else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
251                      sscanf( s, "{%d}{}", &i_dummy ) == 1)
252             {
253                 p_sys->i_type = SUB_TYPE_MICRODVD;
254                 break;
255             }
256             else if( sscanf( s,
257                              "%d:%d:%d,%d --> %d:%d:%d,%d",
258                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
259                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
260             {
261                 p_sys->i_type = SUB_TYPE_SUBRIP;
262                 break;
263             }
264             else if( sscanf( s,
265                              "!: This is a Sub Station Alpha v%d.x script.",
266                              &i_dummy ) == 1)
267             {
268                 if( i_dummy <= 1 )
269                 {
270                     p_sys->i_type = SUB_TYPE_SSA1;
271                 }
272                 else
273                 {
274                     p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
275                 }
276                 break;
277             }
278             else if( strcasestr( s, "This is a Sub Station Alpha v4 script" ) )
279             {
280                 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
281                 break;
282             }
283             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
284             {
285                 p_sys->i_type = SUB_TYPE_SSA2_4; /* could be wrong */
286                 break;
287             }
288             else if( strcasestr( s, "[INFORMATION]" ) )
289             {
290                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
291                 break;
292             }
293             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
294                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
295             {
296                 p_sys->i_type = SUB_TYPE_VPLAYER;
297                 break;
298             }
299             else if( strcasestr( s, "# VobSub index file" ) )
300             {
301                 p_sys->i_type = SUB_TYPE_VOBSUB;
302                 break;
303             }
304         }
305
306         /* It will nearly always work even for non seekable stream thanks the
307          * caching system, and if it fails we loose just a few sub */
308         if( stream_Seek( p_demux->s, 0 ) )
309         {
310             msg_Warn( p_demux, "failed to rewind" );
311         }
312     }
313     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
314     {
315         msg_Err( p_demux, "failed to recognize subtitle type" );
316         return VLC_EGENERIC;
317     }
318
319     for( i = 0; ; i++ )
320     {
321         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
322         {
323             msg_Dbg( p_demux, "detected %s format",
324                      sub_read_subtitle_function[i].psz_name );
325             pf_read = sub_read_subtitle_function[i].pf_read;
326             break;
327         }
328     }
329
330     msg_Dbg( p_demux, "loading all subtitles..." );
331
332     /* Load the whole file */
333     TextLoad( &p_sys->txt, p_demux->s );
334
335     /* Parse it */
336     for( i_max = 0;; )
337     {
338         if( p_sys->i_subtitles >= i_max )
339         {
340             i_max += 500;
341             if( !( p_sys->subtitle = realloc( p_sys->subtitle,
342                                               sizeof(subtitle_t) * i_max ) ) )
343             {
344                 msg_Err( p_demux, "out of memory");
345                 return VLC_ENOMEM;
346             }
347         }
348
349         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
350             break;
351
352         p_sys->i_subtitles++;
353     }
354     /* Unload */
355     TextUnload( &p_sys->txt );
356
357     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
358
359     /* Fix subtitle (order and time) *** */
360     p_sys->i_subtitle = 0;
361     if( p_sys->i_type != SUB_TYPE_VOBSUB )
362     {
363         Fix( p_demux );
364     }
365     p_sys->i_length = 0;
366     if( p_sys->i_subtitles > 0 )
367     {
368         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
369         /* +1 to avoid 0 */
370         if( p_sys->i_length <= 0 )
371             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
372     }
373
374     /* *** add subtitle ES *** */
375     if( p_sys->i_type == SUB_TYPE_VOBSUB )
376     {
377         int i_len = strlen( p_demux->psz_path );
378         char *psz_vobname = strdup( p_demux->psz_path );
379
380         strcpy( psz_vobname + i_len - 4, ".sub" );
381
382         /* open file */
383         if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
384         {
385             msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
386                      psz_vobname );
387         }
388         free( psz_vobname );
389
390         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
391     }
392     else if( p_sys->i_type == SUB_TYPE_SSA1 ||
393              p_sys->i_type == SUB_TYPE_SSA2_4 )
394     {
395         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
396     }
397     else
398     {
399         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
400     }
401     if( p_sys->psz_header != NULL )
402     {
403         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
404         fmt.p_extra = strdup( p_sys->psz_header );
405     }
406     p_sys->es = es_out_Add( p_demux->out, &fmt );
407
408     return VLC_SUCCESS;
409 }
410
411 /*****************************************************************************
412  * Close: Close subtitle demux
413  *****************************************************************************/
414 static void Close( vlc_object_t *p_this )
415 {
416     demux_t *p_demux = (demux_t*)p_this;
417     demux_sys_t *p_sys = p_demux->p_sys;
418     int i;
419
420     for( i = 0; i < p_sys->i_subtitles; i++ )
421     {
422         if( p_sys->subtitle[i].psz_text )
423             free( p_sys->subtitle[i].psz_text );
424     }
425     if( p_sys->subtitle )
426         free( p_sys->subtitle );
427
428     if( p_sys->p_vobsub_file )
429         fclose( p_sys->p_vobsub_file );
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             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;
529     msg_Dbg( p_demux, "sub demux until %lld", i_maxdate );
530     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
531     {
532         /* Should not happen */
533         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
534     }
535
536     if( p_sys->i_type != SUB_TYPE_VOBSUB )
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             msg_Dbg( p_demux, "sub[%d] '%s'", p_sys->i_subtitle, p_sys->subtitle[p_sys->i_subtitle].psz_text );
545             if( i_len <= 1 )
546             {
547                 /* empty subtitle */
548                 p_sys->i_subtitle++;
549                 continue;
550             }
551
552             if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
553             {
554                 p_sys->i_subtitle++;
555                 continue;
556             }
557
558             if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
559             {
560                 p_sys->i_subtitle++;
561                 continue;
562             }
563
564             p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
565             p_block->i_dts = p_block->i_pts;
566             if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
567             {
568                 p_block->i_length =
569                     p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
570             }
571
572             memcpy( p_block->p_buffer,
573                     p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
574             if( p_block->i_pts > 0 )
575             {
576                 es_out_Send( p_demux->out, p_sys->es, p_block );
577             }
578             else
579             {
580                 block_Release( p_block );
581             }
582             p_sys->i_subtitle++;
583         }
584     }
585     else
586     {
587         while( p_sys->i_subtitle < p_sys->i_subtitles &&
588                p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
589         {
590             int i_pos = p_sys->subtitle[p_sys->i_subtitle].i_vobsub_location;
591             block_t *p_block;
592             int i_size = 0;
593
594             /* first compute SPU size */
595             if( p_sys->i_subtitle + 1 < p_sys->i_subtitles )
596             {
597                 i_size = p_sys->subtitle[p_sys->i_subtitle+1].i_vobsub_location - i_pos;
598             }
599             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
600
601             /* Seek at the right place */
602             if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
603             {
604                 msg_Warn( p_demux,
605                           "cannot seek at right vobsub location %d", i_pos );
606                 p_sys->i_subtitle++;
607                 continue;
608             }
609
610             /* allocate a packet */
611             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
612             {
613                 p_sys->i_subtitle++;
614                 continue;
615             }
616
617             /* read data */
618             p_block->i_buffer = fread( p_block->p_buffer, 1, i_size,
619                                        p_sys->p_vobsub_file );
620             if( p_block->i_buffer <= 6 )
621             {
622                 block_Release( p_block );
623                 p_sys->i_subtitle++;
624                 continue;
625             }
626
627             /* pts */
628             p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
629
630             /* demux this block */
631             DemuxVobSub( p_demux, p_block );
632
633             p_sys->i_subtitle++;
634         }
635     }
636
637     /* */
638     p_sys->i_next_demux_date = 0;
639
640     return 1;
641 }
642
643 /*****************************************************************************
644  * Fix: fix time stamp and order of subtitle
645  *****************************************************************************/
646 static void Fix( demux_t *p_demux )
647 {
648     demux_sys_t *p_sys = p_demux->p_sys;
649     vlc_bool_t b_done;
650     int     i_index;
651
652     /* *** fix order (to be sure...) *** */
653     /* We suppose that there are near in order and this durty bubble sort
654      * wont take too much time
655      */
656     do
657     {
658         b_done = VLC_TRUE;
659         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
660         {
661             if( p_sys->subtitle[i_index].i_start <
662                     p_sys->subtitle[i_index - 1].i_start )
663             {
664                 subtitle_t sub_xch;
665                 memcpy( &sub_xch,
666                         p_sys->subtitle + i_index - 1,
667                         sizeof( subtitle_t ) );
668                 memcpy( p_sys->subtitle + i_index - 1,
669                         p_sys->subtitle + i_index,
670                         sizeof( subtitle_t ) );
671                 memcpy( p_sys->subtitle + i_index,
672                         &sub_xch,
673                         sizeof( subtitle_t ) );
674                 b_done = VLC_FALSE;
675             }
676         }
677     } while( !b_done );
678 }
679
680 static int TextLoad( text_t *txt, stream_t *s )
681 {
682     int   i_line_max;
683
684     /* init txt */
685     i_line_max          = 500;
686     txt->i_line_count   = 0;
687     txt->i_line         = 0;
688     txt->line           = calloc( i_line_max, sizeof( char * ) );
689
690     /* load the complete file */
691     for( ;; )
692     {
693         char *psz = stream_ReadLine( s );
694
695         if( psz == NULL )
696             break;
697
698         txt->line[txt->i_line_count++] = psz;
699         if( txt->i_line_count >= i_line_max )
700         {
701             i_line_max += 100;
702             txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
703         }
704     }
705
706     if( txt->i_line_count <= 0 )
707     {
708         free( txt->line );
709         return VLC_EGENERIC;
710     }
711
712     return VLC_SUCCESS;
713 }
714 static void TextUnload( text_t *txt )
715 {
716     int i;
717
718     for( i = 0; i < txt->i_line_count; i++ )
719     {
720         free( txt->line[i] );
721     }
722     free( txt->line );
723     txt->i_line       = 0;
724     txt->i_line_count = 0;
725 }
726
727 static char *TextGetLine( text_t *txt )
728 {
729     if( txt->i_line >= txt->i_line_count )
730         return( NULL );
731
732     return txt->line[txt->i_line++];
733 }
734 static void TextPreviousLine( text_t *txt )
735 {
736     if( txt->i_line > 0 )
737         txt->i_line--;
738 }
739
740 /*****************************************************************************
741  * Specific Subtitle function
742  *****************************************************************************/
743 #define MAX_LINE 8192
744 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
745 {
746     demux_sys_t *p_sys = p_demux->p_sys;
747     text_t      *txt = &p_sys->txt;
748     /*
749      * each line:
750      *  {n1}{n2}Line1|Line2|Line3....
751      * where n1 and n2 are the video frame number...
752      *
753      */
754     char *s;
755
756     char buffer_text[MAX_LINE + 1];
757     unsigned int    i_start;
758     unsigned int    i_stop;
759     unsigned int i;
760
761     p_subtitle->i_start = 0;
762     p_subtitle->i_stop  = 0;
763     p_subtitle->i_vobsub_location  = 0;
764     p_subtitle->psz_text = NULL;
765
766     for( ;; )
767     {
768         if( ( s = TextGetLine( txt ) ) == NULL )
769         {
770             return( VLC_EGENERIC );
771         }
772         i_start = 0;
773         i_stop  = 0;
774
775         memset( buffer_text, '\0', MAX_LINE );
776         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
777             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
778         {
779             break;
780         }
781     }
782     /* replace | by \n */
783     for( i = 0; i < strlen( buffer_text ); i++ )
784     {
785         if( buffer_text[i] == '|' )
786         {
787             buffer_text[i] = '\n';
788         }
789     }
790
791     p_subtitle->i_start = (mtime_t)i_start * p_sys->i_microsecperframe;
792     p_subtitle->i_stop  = (mtime_t)i_stop  * p_sys->i_microsecperframe;
793     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
794     return( 0 );
795 }
796
797 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
798 {
799     demux_sys_t *p_sys = p_demux->p_sys;
800     text_t      *txt = &p_sys->txt;
801
802     /*
803      * n
804      * h1:m1:s1,d1 --> h2:m2:s2,d2
805      * Line1
806      * Line2
807      * ...
808      * [empty line]
809      *
810      */
811     char *s;
812     char buffer_text[ 10 * MAX_LINE];
813     int  i_buffer_text;
814     mtime_t     i_start;
815     mtime_t     i_stop;
816
817     p_subtitle->i_start = 0;
818     p_subtitle->i_stop  = 0;
819     p_subtitle->i_vobsub_location  = 0;
820     p_subtitle->psz_text = NULL;
821
822     for( ;; )
823     {
824         int h1, m1, s1, d1, h2, m2, s2, d2;
825         if( ( s = TextGetLine( txt ) ) == NULL )
826         {
827             return( VLC_EGENERIC );
828         }
829         if( sscanf( s,
830                     "%d:%d:%d,%d --> %d:%d:%d,%d",
831                     &h1, &m1, &s1, &d1,
832                     &h2, &m2, &s2, &d2 ) == 8 )
833         {
834             i_start = ( (mtime_t)h1 * 3600*1000 +
835                         (mtime_t)m1 * 60*1000 +
836                         (mtime_t)s1 * 1000 +
837                         (mtime_t)d1 ) * 1000;
838
839             i_stop  = ( (mtime_t)h2 * 3600*1000 +
840                         (mtime_t)m2 * 60*1000 +
841                         (mtime_t)s2 * 1000 +
842                         (mtime_t)d2 ) * 1000;
843
844             /* Now read text until an empty line */
845             for( i_buffer_text = 0;; )
846             {
847                 int i_len;
848                 if( ( s = TextGetLine( txt ) ) == NULL )
849                 {
850                     return( VLC_EGENERIC );
851                 }
852
853                 i_len = strlen( s );
854                 if( i_len <= 1 )
855                 {
856                     /* empty line -> end of this subtitle */
857                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
858                     p_subtitle->i_start = i_start;
859                     p_subtitle->i_stop = i_stop;
860                     p_subtitle->psz_text = strdup( buffer_text );
861                     /* If framerate is available, use sub-fps */
862                     if( p_sys->i_microsecperframe != 0 &&
863                         p_sys->i_original_mspf != 0)
864                     {
865                         p_subtitle->i_start = (mtime_t)i_start *
866                                               p_sys->i_original_mspf /
867                                               p_sys->i_microsecperframe;
868                         p_subtitle->i_stop  = (mtime_t)i_stop  *
869                                               p_sys->i_original_mspf /
870                                               p_sys->i_microsecperframe;
871                     }
872                     return 0;
873                 }
874                 else
875                 {
876                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
877                     {
878                         memcpy( buffer_text + i_buffer_text,
879                                 s,
880                                 i_len );
881                         i_buffer_text += i_len;
882
883                         buffer_text[i_buffer_text] = '\n';
884                         i_buffer_text++;
885                     }
886                 }
887             }
888         }
889     }
890 }
891
892 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
893 {
894     demux_sys_t *p_sys = p_demux->p_sys;
895     text_t      *txt = &p_sys->txt;
896
897     /*
898      * h1:m1:s1.d1,h2:m2:s2.d2
899      * Line1[br]Line2
900      * Line3
901      * ...
902      * [empty line]
903      * ( works with subviewer and subviewer v2 )
904      */
905     char *s;
906     char buffer_text[ 10 * MAX_LINE];
907     int  i_buffer_text;
908     mtime_t     i_start;
909     mtime_t     i_stop;
910
911     p_subtitle->i_start = 0;
912     p_subtitle->i_stop  = 0;
913     p_subtitle->i_vobsub_location  = 0;
914     p_subtitle->psz_text = NULL;
915
916     for( ;; )
917     {
918         int h1, m1, s1, d1, h2, m2, s2, d2;
919         if( ( s = TextGetLine( txt ) ) == NULL )
920         {
921             return( VLC_EGENERIC );
922         }
923         if( sscanf( s,
924                     "%d:%d:%d.%d,%d:%d:%d.%d",
925                     &h1, &m1, &s1, &d1,
926                     &h2, &m2, &s2, &d2 ) == 8 )
927         {
928             i_start = ( (mtime_t)h1 * 3600*1000 +
929                         (mtime_t)m1 * 60*1000 +
930                         (mtime_t)s1 * 1000 +
931                         (mtime_t)d1 ) * 1000;
932
933             i_stop  = ( (mtime_t)h2 * 3600*1000 +
934                         (mtime_t)m2 * 60*1000 +
935                         (mtime_t)s2 * 1000 +
936                         (mtime_t)d2 ) * 1000;
937
938             /* Now read text until an empty line */
939             for( i_buffer_text = 0;; )
940             {
941                 int i_len, i;
942                 if( ( s = TextGetLine( txt ) ) == NULL )
943                 {
944                     return( VLC_EGENERIC );
945                 }
946
947                 i_len = strlen( s );
948                 if( i_len <= 1 )
949                 {
950                     /* empty line -> end of this subtitle */
951                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
952                     p_subtitle->i_start = i_start;
953                     p_subtitle->i_stop = i_stop;
954
955                     /* replace [br] by \n */
956                     for( i = 0; i < strlen( buffer_text ) - 3; i++ )
957                     {
958                         if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
959                             buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
960                         {
961                             char *temp = buffer_text + i + 1;
962                             buffer_text[i] = '\n';
963                             memmove( temp, temp+3, strlen( temp-3 ));
964                         }
965                     }
966                     p_subtitle->psz_text = strdup( buffer_text );
967                     return( 0 );
968                 }
969                 else
970                 {
971                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
972                     {
973                         memcpy( buffer_text + i_buffer_text,
974                                 s,
975                                 i_len );
976                         i_buffer_text += i_len;
977
978                         buffer_text[i_buffer_text] = '\n';
979                         i_buffer_text++;
980                     }
981                 }
982             }
983         }
984     }
985 }
986
987
988 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
989 {
990     demux_sys_t *p_sys = p_demux->p_sys;
991     text_t      *txt = &p_sys->txt;
992
993     char buffer_text[ 10 * MAX_LINE];
994     char *s;
995     mtime_t     i_start;
996     mtime_t     i_stop;
997
998     p_subtitle->i_start = 0;
999     p_subtitle->i_stop  = 0;
1000     p_subtitle->i_vobsub_location  = 0;
1001     p_subtitle->psz_text = NULL;
1002
1003     for( ;; )
1004     {
1005         int h1, m1, s1, c1, h2, m2, s2, c2;
1006         int i_dummy;
1007
1008         if( ( s = TextGetLine( txt ) ) == NULL )
1009         {
1010             return( VLC_EGENERIC );
1011         }
1012         p_subtitle->psz_text = malloc( strlen( s ) );
1013
1014         if( sscanf( s,
1015                     "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
1016                     &i_dummy,
1017                     &h1, &m1, &s1, &c1,
1018                     &h2, &m2, &s2, &c2,
1019                     buffer_text ) == 10 )
1020         {
1021             i_start = ( (mtime_t)h1 * 3600*1000 +
1022                         (mtime_t)m1 * 60*1000 +
1023                         (mtime_t)s1 * 1000 +
1024                         (mtime_t)c1 * 10 ) * 1000;
1025
1026             i_stop  = ( (mtime_t)h2 * 3600*1000 +
1027                         (mtime_t)m2 * 60*1000 +
1028                         (mtime_t)s2 * 1000 +
1029                         (mtime_t)c2 * 10 ) * 1000;
1030
1031             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1032             if( p_sys->i_type == SUB_TYPE_SSA1 )
1033             {
1034                 sprintf( p_subtitle->psz_text,
1035                          ",%d%s", i_dummy, strdup( buffer_text) );
1036             }
1037             else
1038             {
1039                 sprintf( p_subtitle->psz_text,
1040                          ",%d,%s", i_dummy, strdup( buffer_text) );
1041             }
1042             p_subtitle->i_start = i_start;
1043             p_subtitle->i_stop = i_stop;
1044             return 0;
1045         }
1046         else
1047         {
1048             /* All the other stuff we add to the header field */
1049             if( p_sys->psz_header != NULL )
1050             {
1051                 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
1052                           strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
1053                 {
1054                     msg_Err( p_demux, "out of memory");
1055                     return VLC_ENOMEM;
1056                 }
1057                 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
1058                 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1059             }
1060             else
1061             {
1062                 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1063                 {
1064                     msg_Err( p_demux, "out of memory");
1065                     return VLC_ENOMEM;
1066                 }
1067                 p_sys->psz_header = strdup( s );
1068                 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1069             }
1070         }
1071     }
1072 }
1073
1074 static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1075 {
1076     demux_sys_t *p_sys = p_demux->p_sys;
1077     text_t      *txt = &p_sys->txt;
1078
1079     /*
1080      * each line:
1081      *  h:m:s:Line1|Line2|Line3....
1082      *  or
1083      *  h:m:s Line1|Line2|Line3....
1084      *
1085      */
1086     char *p;
1087     char buffer_text[MAX_LINE + 1];
1088     mtime_t    i_start;
1089     unsigned int i;
1090
1091     p_subtitle->i_start = 0;
1092     p_subtitle->i_stop  = 0;
1093     p_subtitle->i_vobsub_location  = 0;
1094     p_subtitle->psz_text = NULL;
1095
1096     for( ;; )
1097     {
1098         int h, m, s;
1099         char c;
1100
1101         if( ( p = TextGetLine( txt ) ) == NULL )
1102         {
1103             return( VLC_EGENERIC );
1104         }
1105
1106         i_start = 0;
1107
1108         memset( buffer_text, '\0', MAX_LINE );
1109         if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1110         {
1111             i_start = ( (mtime_t)h * 3600*1000 +
1112                         (mtime_t)m * 60*1000 +
1113                         (mtime_t)s * 1000 ) * 1000;
1114             break;
1115         }
1116     }
1117
1118     /* replace | by \n */
1119     for( i = 0; i < strlen( buffer_text ); i++ )
1120     {
1121         if( buffer_text[i] == '|' )
1122         {
1123             buffer_text[i] = '\n';
1124         }
1125     }
1126     p_subtitle->i_start = i_start;
1127
1128     p_subtitle->i_stop  = 0;
1129     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1130     return( 0 );
1131 }
1132
1133 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1134 {
1135     if( psz_start )
1136     {
1137         if( strcasestr( psz_start, psz_str ) )
1138         {
1139             char *s = strcasestr( psz_start, psz_str );
1140
1141             s += strlen( psz_str );
1142
1143             return( s );
1144         }
1145     }
1146     for( ;; )
1147     {
1148         char *p;
1149         if( ( p = TextGetLine( txt ) ) == NULL )
1150         {
1151             return NULL;
1152         }
1153         if( strcasestr( p, psz_str ) )
1154         {
1155             char *s = strcasestr( p, psz_str );
1156
1157             s += strlen( psz_str );
1158
1159             return(  s);
1160         }
1161     }
1162 }
1163
1164 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1165 {
1166     demux_sys_t *p_sys = p_demux->p_sys;
1167     text_t      *txt = &p_sys->txt;
1168
1169     char *p;
1170     int i_start;
1171
1172     int  i_text;
1173     char buffer_text[10*MAX_LINE + 1];
1174
1175     p_subtitle->i_start = 0;
1176     p_subtitle->i_stop  = 0;
1177     p_subtitle->i_vobsub_location  = 0;
1178     p_subtitle->psz_text = NULL;
1179
1180 #define ADDC( c ) \
1181     if( i_text < 10*MAX_LINE )      \
1182     {                               \
1183         buffer_text[i_text++] = c;  \
1184         buffer_text[i_text] = '\0'; \
1185     }
1186
1187     /* search "Start=" */
1188     if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1189     {
1190         return VLC_EGENERIC;
1191     }
1192
1193     /* get start value */
1194     i_start = strtol( p, &p, 0 );
1195
1196     /* search <P */
1197     if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1198     {
1199         return VLC_EGENERIC;
1200     }
1201     /* search > */
1202     if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1203     {
1204         return VLC_EGENERIC;
1205     }
1206
1207     i_text = 0;
1208     buffer_text[0] = '\0';
1209     /* now get all txt until  a "Start=" line */
1210     for( ;; )
1211     {
1212         if( *p )
1213         {
1214             if( *p == '<' )
1215             {
1216                 if( !strncasecmp( p, "<br", 3 ) )
1217                 {
1218                     ADDC( '\n' );
1219                 }
1220                 else if( strcasestr( p, "Start=" ) )
1221                 {
1222                     TextPreviousLine( txt );
1223                     break;
1224                 }
1225                 p = ParseSamiSearch( txt, p, ">" );
1226             }
1227             else if( !strncmp( p, "&nbsp;", 6 ) )
1228             {
1229                 ADDC( ' ' );
1230                 p += 6;
1231             }
1232             else if( *p == '\t' )
1233             {
1234                 ADDC( ' ' );
1235                 p++;
1236             }
1237             else
1238             {
1239                 ADDC( *p );
1240                 p++;
1241             }
1242         }
1243         else
1244         {
1245             p = TextGetLine( txt );
1246         }
1247
1248         if( p == NULL )
1249         {
1250             break;
1251         }
1252     }
1253
1254     p_subtitle->i_start = i_start * 1000;
1255     p_subtitle->i_stop  = 0;
1256     p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1257
1258     return( VLC_SUCCESS );
1259 #undef ADDC
1260 }
1261
1262 static int  ParseVobSubIDX( demux_t *p_demux, subtitle_t *p_subtitle )
1263 {
1264     demux_sys_t *p_sys = p_demux->p_sys;
1265     text_t      *txt = &p_sys->txt;
1266
1267     /*
1268      * Parse the idx file. Each line:
1269      * timestamp: hh:mm:ss:mss, filepos: loc
1270      * hexint is the hex location of the vobsub in the .sub file
1271      *
1272      */
1273     char *p;
1274     char buffer_text[MAX_LINE + 1];
1275     unsigned int    i_start, i_location;
1276
1277     p_subtitle->i_start = 0;
1278     p_subtitle->i_stop  = 0;
1279     p_subtitle->i_vobsub_location  = 0;
1280     p_subtitle->psz_text = NULL;
1281
1282     for( ;; )
1283     {
1284         unsigned int h, m, s, ms, loc;
1285
1286         if( ( p = TextGetLine( txt ) ) == NULL )
1287         {
1288             return( VLC_EGENERIC );
1289         }
1290         i_start = 0;
1291
1292         memset( buffer_text, '\0', MAX_LINE );
1293         if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
1294                     &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
1295         {
1296             i_start = ( (mtime_t)h * 3600*1000 +
1297                         (mtime_t)m * 60*1000 +
1298                         (mtime_t)s * 1000 +
1299                         (mtime_t)ms ) * 1000;
1300             i_location = loc;
1301             break;
1302         }
1303     }
1304     p_subtitle->i_start = (mtime_t)i_start;
1305     p_subtitle->i_stop  = 0;
1306     p_subtitle->psz_text = NULL;
1307     p_subtitle->i_vobsub_location = i_location;
1308     fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
1309     return( 0 );
1310 }
1311
1312 static int  DemuxVobSub( demux_t *p_demux, block_t *p_bk )
1313 {
1314     demux_sys_t *p_sys = p_demux->p_sys;
1315     uint8_t     *p = p_bk->p_buffer;
1316     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
1317
1318     while( p < p_end )
1319     {
1320         int i_size = ps_pkt_size( p, p_end - p );
1321         block_t *p_pkt;
1322         int      i_id;
1323         int      i_spu;
1324
1325         if( i_size <= 0 )
1326         {
1327             break;
1328         }
1329         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
1330         {
1331             msg_Warn( p_demux, "invalid PES" );
1332             break;
1333         }
1334
1335         if( p[3] != 0xbd )
1336         {
1337             msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
1338             p += i_size;
1339             continue;
1340         }
1341
1342         /* Create a block */
1343         p_pkt = block_New( p_demux, i_size );
1344         memcpy( p_pkt->p_buffer, p, i_size);
1345         p += i_size;
1346
1347         i_id = ps_pkt_id( p_pkt );
1348         if( (i_id&0xffe0) != 0xbd20 ||
1349             ps_pkt_parse_pes( p_pkt, 1 ) )
1350         {
1351             block_Release( p_pkt );
1352             continue;
1353         }
1354         i_spu = i_id&0x1f;
1355         msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
1356
1357         /* FIXME i_spu == determines which of the spu tracks we will show. */
1358         if( p_sys->es && i_spu == 0 )
1359         {
1360             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
1361             p_pkt->i_length = 0;
1362             es_out_Send( p_demux->out, p_sys->es, p_pkt );
1363
1364             p_bk->i_pts = 0;    /* only first packet has a pts */
1365         }
1366         else
1367         {
1368             block_Release( p_pkt );
1369             continue;
1370         }
1371     }
1372
1373     return VLC_SUCCESS;
1374 }