]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
* subtitle: fixed a small memleak.
[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 = NULL;
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             free( s );
306             s = NULL;
307         }
308
309         if( s ) free( s );
310
311         /* It will nearly always work even for non seekable stream thanks the
312          * caching system, and if it fails we loose just a few sub */
313         if( stream_Seek( p_demux->s, 0 ) )
314         {
315             msg_Warn( p_demux, "failed to rewind" );
316         }
317     }
318     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
319     {
320         msg_Err( p_demux, "failed to recognize subtitle type" );
321         return VLC_EGENERIC;
322     }
323
324     for( i = 0; ; i++ )
325     {
326         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
327         {
328             msg_Dbg( p_demux, "detected %s format",
329                      sub_read_subtitle_function[i].psz_name );
330             pf_read = sub_read_subtitle_function[i].pf_read;
331             break;
332         }
333     }
334
335     msg_Dbg( p_demux, "loading all subtitles..." );
336
337     /* Load the whole file */
338     TextLoad( &p_sys->txt, p_demux->s );
339
340     /* Parse it */
341     for( i_max = 0;; )
342     {
343         if( p_sys->i_subtitles >= i_max )
344         {
345             i_max += 500;
346             if( !( p_sys->subtitle = realloc( p_sys->subtitle,
347                                               sizeof(subtitle_t) * i_max ) ) )
348             {
349                 msg_Err( p_demux, "out of memory");
350                 return VLC_ENOMEM;
351             }
352         }
353
354         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
355             break;
356
357         p_sys->i_subtitles++;
358     }
359     /* Unload */
360     TextUnload( &p_sys->txt );
361
362     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
363
364     /* Fix subtitle (order and time) *** */
365     p_sys->i_subtitle = 0;
366     if( p_sys->i_type != SUB_TYPE_VOBSUB )
367     {
368         Fix( p_demux );
369     }
370     p_sys->i_length = 0;
371     if( p_sys->i_subtitles > 0 )
372     {
373         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
374         /* +1 to avoid 0 */
375         if( p_sys->i_length <= 0 )
376             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
377     }
378
379     /* *** add subtitle ES *** */
380     if( p_sys->i_type == SUB_TYPE_VOBSUB )
381     {
382         int i_len = strlen( p_demux->psz_path );
383         char *psz_vobname = strdup( p_demux->psz_path );
384
385         strcpy( psz_vobname + i_len - 4, ".sub" );
386
387         /* open file */
388         if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
389         {
390             msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
391                      psz_vobname );
392         }
393         free( psz_vobname );
394
395         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
396     }
397     else if( p_sys->i_type == SUB_TYPE_SSA1 ||
398              p_sys->i_type == SUB_TYPE_SSA2_4 )
399     {
400         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
401     }
402     else
403     {
404         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
405     }
406     if( p_sys->psz_header != NULL )
407     {
408         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
409         fmt.p_extra = strdup( p_sys->psz_header );
410     }
411     p_sys->es = es_out_Add( p_demux->out, &fmt );
412
413     return VLC_SUCCESS;
414 }
415
416 /*****************************************************************************
417  * Close: Close subtitle demux
418  *****************************************************************************/
419 static void Close( vlc_object_t *p_this )
420 {
421     demux_t *p_demux = (demux_t*)p_this;
422     demux_sys_t *p_sys = p_demux->p_sys;
423     int i;
424
425     for( i = 0; i < p_sys->i_subtitles; i++ )
426     {
427         if( p_sys->subtitle[i].psz_text )
428             free( p_sys->subtitle[i].psz_text );
429     }
430     if( p_sys->subtitle )
431         free( p_sys->subtitle );
432
433     if( p_sys->p_vobsub_file )
434         fclose( p_sys->p_vobsub_file );
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;
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     if( p_sys->i_type != SUB_TYPE_VOBSUB )
542     {
543         while( p_sys->i_subtitle < p_sys->i_subtitles &&
544                p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
545         {
546             block_t *p_block;
547             int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
548
549             if( i_len <= 1 )
550             {
551                 /* empty subtitle */
552                 p_sys->i_subtitle++;
553                 continue;
554             }
555
556             if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
557             {
558                 p_sys->i_subtitle++;
559                 continue;
560             }
561
562             if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
563             {
564                 p_sys->i_subtitle++;
565                 continue;
566             }
567
568             p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
569             p_block->i_dts = p_block->i_pts;
570             if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
571             {
572                 p_block->i_length =
573                     p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
574             }
575
576             memcpy( p_block->p_buffer,
577                     p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
578             if( p_block->i_pts > 0 )
579             {
580                 es_out_Send( p_demux->out, p_sys->es, p_block );
581             }
582             else
583             {
584                 block_Release( p_block );
585             }
586             p_sys->i_subtitle++;
587         }
588     }
589     else
590     {
591         while( p_sys->i_subtitle < p_sys->i_subtitles &&
592                p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
593         {
594             int i_pos = p_sys->subtitle[p_sys->i_subtitle].i_vobsub_location;
595             block_t *p_block;
596             int i_size = 0;
597
598             /* first compute SPU size */
599             if( p_sys->i_subtitle + 1 < p_sys->i_subtitles )
600             {
601                 i_size = p_sys->subtitle[p_sys->i_subtitle+1].i_vobsub_location - i_pos;
602             }
603             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
604
605             /* Seek at the right place */
606             if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
607             {
608                 msg_Warn( p_demux,
609                           "cannot seek at right vobsub location %d", i_pos );
610                 p_sys->i_subtitle++;
611                 continue;
612             }
613
614             /* allocate a packet */
615             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
616             {
617                 p_sys->i_subtitle++;
618                 continue;
619             }
620
621             /* read data */
622             p_block->i_buffer = fread( p_block->p_buffer, 1, i_size,
623                                        p_sys->p_vobsub_file );
624             if( p_block->i_buffer <= 6 )
625             {
626                 block_Release( p_block );
627                 p_sys->i_subtitle++;
628                 continue;
629             }
630
631             /* pts */
632             p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
633
634             /* demux this block */
635             DemuxVobSub( p_demux, p_block );
636
637             p_sys->i_subtitle++;
638         }
639     }
640
641     /* */
642     p_sys->i_next_demux_date = 0;
643
644     return 1;
645 }
646
647 /*****************************************************************************
648  * Fix: fix time stamp and order of subtitle
649  *****************************************************************************/
650 static void Fix( demux_t *p_demux )
651 {
652     demux_sys_t *p_sys = p_demux->p_sys;
653     vlc_bool_t b_done;
654     int     i_index;
655
656     /* *** fix order (to be sure...) *** */
657     /* We suppose that there are near in order and this durty bubble sort
658      * wont take too much time
659      */
660     do
661     {
662         b_done = VLC_TRUE;
663         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
664         {
665             if( p_sys->subtitle[i_index].i_start <
666                     p_sys->subtitle[i_index - 1].i_start )
667             {
668                 subtitle_t sub_xch;
669                 memcpy( &sub_xch,
670                         p_sys->subtitle + i_index - 1,
671                         sizeof( subtitle_t ) );
672                 memcpy( p_sys->subtitle + i_index - 1,
673                         p_sys->subtitle + i_index,
674                         sizeof( subtitle_t ) );
675                 memcpy( p_sys->subtitle + i_index,
676                         &sub_xch,
677                         sizeof( subtitle_t ) );
678                 b_done = VLC_FALSE;
679             }
680         }
681     } while( !b_done );
682 }
683
684 static int TextLoad( text_t *txt, stream_t *s )
685 {
686     int   i_line_max;
687
688     /* init txt */
689     i_line_max          = 500;
690     txt->i_line_count   = 0;
691     txt->i_line         = 0;
692     txt->line           = calloc( i_line_max, sizeof( char * ) );
693
694     /* load the complete file */
695     for( ;; )
696     {
697         char *psz = stream_ReadLine( s );
698
699         if( psz == NULL )
700             break;
701
702         txt->line[txt->i_line_count++] = psz;
703         if( txt->i_line_count >= i_line_max )
704         {
705             i_line_max += 100;
706             txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
707         }
708     }
709
710     if( txt->i_line_count <= 0 )
711     {
712         free( txt->line );
713         return VLC_EGENERIC;
714     }
715
716     return VLC_SUCCESS;
717 }
718 static void TextUnload( text_t *txt )
719 {
720     int i;
721
722     for( i = 0; i < txt->i_line_count; i++ )
723     {
724         free( txt->line[i] );
725     }
726     free( txt->line );
727     txt->i_line       = 0;
728     txt->i_line_count = 0;
729 }
730
731 static char *TextGetLine( text_t *txt )
732 {
733     if( txt->i_line >= txt->i_line_count )
734         return( NULL );
735
736     return txt->line[txt->i_line++];
737 }
738 static void TextPreviousLine( text_t *txt )
739 {
740     if( txt->i_line > 0 )
741         txt->i_line--;
742 }
743
744 /*****************************************************************************
745  * Specific Subtitle function
746  *****************************************************************************/
747 #define MAX_LINE 8192
748 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
749 {
750     demux_sys_t *p_sys = p_demux->p_sys;
751     text_t      *txt = &p_sys->txt;
752     /*
753      * each line:
754      *  {n1}{n2}Line1|Line2|Line3....
755      * where n1 and n2 are the video frame number...
756      *
757      */
758     char *s;
759
760     char buffer_text[MAX_LINE + 1];
761     unsigned int    i_start;
762     unsigned int    i_stop;
763     unsigned int i;
764
765     p_subtitle->i_start = 0;
766     p_subtitle->i_stop  = 0;
767     p_subtitle->i_vobsub_location  = 0;
768     p_subtitle->psz_text = NULL;
769
770     for( ;; )
771     {
772         if( ( s = TextGetLine( txt ) ) == NULL )
773         {
774             return( VLC_EGENERIC );
775         }
776         i_start = 0;
777         i_stop  = 0;
778
779         memset( buffer_text, '\0', MAX_LINE );
780         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
781             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
782         {
783             break;
784         }
785     }
786     /* replace | by \n */
787     for( i = 0; i < strlen( buffer_text ); i++ )
788     {
789         if( buffer_text[i] == '|' )
790         {
791             buffer_text[i] = '\n';
792         }
793     }
794
795     p_subtitle->i_start = (mtime_t)i_start * p_sys->i_microsecperframe;
796     p_subtitle->i_stop  = (mtime_t)i_stop  * p_sys->i_microsecperframe;
797     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
798     return( 0 );
799 }
800
801 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
802 {
803     demux_sys_t *p_sys = p_demux->p_sys;
804     text_t      *txt = &p_sys->txt;
805
806     /*
807      * n
808      * h1:m1:s1,d1 --> h2:m2:s2,d2
809      * Line1
810      * Line2
811      * ...
812      * [empty line]
813      *
814      */
815     char *s;
816     char buffer_text[ 10 * MAX_LINE];
817     int  i_buffer_text;
818     mtime_t     i_start;
819     mtime_t     i_stop;
820
821     p_subtitle->i_start = 0;
822     p_subtitle->i_stop  = 0;
823     p_subtitle->i_vobsub_location  = 0;
824     p_subtitle->psz_text = NULL;
825
826     for( ;; )
827     {
828         int h1, m1, s1, d1, h2, m2, s2, d2;
829         if( ( s = TextGetLine( txt ) ) == NULL )
830         {
831             return( VLC_EGENERIC );
832         }
833         if( sscanf( s,
834                     "%d:%d:%d,%d --> %d:%d:%d,%d",
835                     &h1, &m1, &s1, &d1,
836                     &h2, &m2, &s2, &d2 ) == 8 )
837         {
838             i_start = ( (mtime_t)h1 * 3600*1000 +
839                         (mtime_t)m1 * 60*1000 +
840                         (mtime_t)s1 * 1000 +
841                         (mtime_t)d1 ) * 1000;
842
843             i_stop  = ( (mtime_t)h2 * 3600*1000 +
844                         (mtime_t)m2 * 60*1000 +
845                         (mtime_t)s2 * 1000 +
846                         (mtime_t)d2 ) * 1000;
847
848             /* Now read text until an empty line */
849             for( i_buffer_text = 0;; )
850             {
851                 int i_len;
852                 if( ( s = TextGetLine( txt ) ) == NULL )
853                 {
854                     return( VLC_EGENERIC );
855                 }
856
857                 i_len = strlen( s );
858                 if( i_len <= 1 )
859                 {
860                     /* empty line -> end of this subtitle */
861                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
862                     p_subtitle->i_start = i_start;
863                     p_subtitle->i_stop = i_stop;
864                     p_subtitle->psz_text = strdup( buffer_text );
865                     /* If framerate is available, use sub-fps */
866                     if( p_sys->i_microsecperframe != 0 &&
867                         p_sys->i_original_mspf != 0)
868                     {
869                         p_subtitle->i_start = (mtime_t)i_start *
870                                               p_sys->i_original_mspf /
871                                               p_sys->i_microsecperframe;
872                         p_subtitle->i_stop  = (mtime_t)i_stop  *
873                                               p_sys->i_original_mspf /
874                                               p_sys->i_microsecperframe;
875                     }
876                     return 0;
877                 }
878                 else
879                 {
880                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
881                     {
882                         memcpy( buffer_text + i_buffer_text,
883                                 s,
884                                 i_len );
885                         i_buffer_text += i_len;
886
887                         buffer_text[i_buffer_text] = '\n';
888                         i_buffer_text++;
889                     }
890                 }
891             }
892         }
893     }
894 }
895
896 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
897 {
898     demux_sys_t *p_sys = p_demux->p_sys;
899     text_t      *txt = &p_sys->txt;
900
901     /*
902      * h1:m1:s1.d1,h2:m2:s2.d2
903      * Line1[br]Line2
904      * Line3
905      * ...
906      * [empty line]
907      * ( works with subviewer and subviewer v2 )
908      */
909     char *s;
910     char buffer_text[ 10 * MAX_LINE];
911     int  i_buffer_text;
912     mtime_t     i_start;
913     mtime_t     i_stop;
914
915     p_subtitle->i_start = 0;
916     p_subtitle->i_stop  = 0;
917     p_subtitle->i_vobsub_location  = 0;
918     p_subtitle->psz_text = NULL;
919
920     for( ;; )
921     {
922         int h1, m1, s1, d1, h2, m2, s2, d2;
923         if( ( s = TextGetLine( txt ) ) == NULL )
924         {
925             return( VLC_EGENERIC );
926         }
927         if( sscanf( s,
928                     "%d:%d:%d.%d,%d:%d:%d.%d",
929                     &h1, &m1, &s1, &d1,
930                     &h2, &m2, &s2, &d2 ) == 8 )
931         {
932             i_start = ( (mtime_t)h1 * 3600*1000 +
933                         (mtime_t)m1 * 60*1000 +
934                         (mtime_t)s1 * 1000 +
935                         (mtime_t)d1 ) * 1000;
936
937             i_stop  = ( (mtime_t)h2 * 3600*1000 +
938                         (mtime_t)m2 * 60*1000 +
939                         (mtime_t)s2 * 1000 +
940                         (mtime_t)d2 ) * 1000;
941
942             /* Now read text until an empty line */
943             for( i_buffer_text = 0;; )
944             {
945                 int i_len, i;
946                 if( ( s = TextGetLine( txt ) ) == NULL )
947                 {
948                     return( VLC_EGENERIC );
949                 }
950
951                 i_len = strlen( s );
952                 if( i_len <= 1 )
953                 {
954                     /* empty line -> end of this subtitle */
955                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
956                     p_subtitle->i_start = i_start;
957                     p_subtitle->i_stop = i_stop;
958
959                     /* replace [br] by \n */
960                     for( i = 0; i < strlen( buffer_text ) - 3; i++ )
961                     {
962                         if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
963                             buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
964                         {
965                             char *temp = buffer_text + i + 1;
966                             buffer_text[i] = '\n';
967                             memmove( temp, temp+3, strlen( temp-3 ));
968                         }
969                     }
970                     p_subtitle->psz_text = strdup( buffer_text );
971                     return( 0 );
972                 }
973                 else
974                 {
975                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
976                     {
977                         memcpy( buffer_text + i_buffer_text,
978                                 s,
979                                 i_len );
980                         i_buffer_text += i_len;
981
982                         buffer_text[i_buffer_text] = '\n';
983                         i_buffer_text++;
984                     }
985                 }
986             }
987         }
988     }
989 }
990
991
992 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
993 {
994     demux_sys_t *p_sys = p_demux->p_sys;
995     text_t      *txt = &p_sys->txt;
996
997     char buffer_text[ 10 * MAX_LINE];
998     char *s;
999     mtime_t     i_start;
1000     mtime_t     i_stop;
1001
1002     p_subtitle->i_start = 0;
1003     p_subtitle->i_stop  = 0;
1004     p_subtitle->i_vobsub_location  = 0;
1005     p_subtitle->psz_text = NULL;
1006
1007     for( ;; )
1008     {
1009         int h1, m1, s1, c1, h2, m2, s2, c2;
1010         int i_dummy;
1011
1012         if( ( s = TextGetLine( txt ) ) == NULL )
1013         {
1014             return( VLC_EGENERIC );
1015         }
1016         p_subtitle->psz_text = malloc( strlen( s ) );
1017
1018         if( sscanf( s,
1019                     "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
1020                     &i_dummy,
1021                     &h1, &m1, &s1, &c1,
1022                     &h2, &m2, &s2, &c2,
1023                     buffer_text ) == 10 )
1024         {
1025             i_start = ( (mtime_t)h1 * 3600*1000 +
1026                         (mtime_t)m1 * 60*1000 +
1027                         (mtime_t)s1 * 1000 +
1028                         (mtime_t)c1 * 10 ) * 1000;
1029
1030             i_stop  = ( (mtime_t)h2 * 3600*1000 +
1031                         (mtime_t)m2 * 60*1000 +
1032                         (mtime_t)s2 * 1000 +
1033                         (mtime_t)c2 * 10 ) * 1000;
1034
1035             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1036             if( p_sys->i_type == SUB_TYPE_SSA1 )
1037             {
1038                 sprintf( p_subtitle->psz_text,
1039                          ",%d%s", i_dummy, strdup( buffer_text) );
1040             }
1041             else
1042             {
1043                 sprintf( p_subtitle->psz_text,
1044                          ",%d,%s", i_dummy, strdup( buffer_text) );
1045             }
1046             p_subtitle->i_start = i_start;
1047             p_subtitle->i_stop = i_stop;
1048             return 0;
1049         }
1050         else
1051         {
1052             /* All the other stuff we add to the header field */
1053             if( p_sys->psz_header != NULL )
1054             {
1055                 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
1056                           strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
1057                 {
1058                     msg_Err( p_demux, "out of memory");
1059                     return VLC_ENOMEM;
1060                 }
1061                 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
1062                 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1063             }
1064             else
1065             {
1066                 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1067                 {
1068                     msg_Err( p_demux, "out of memory");
1069                     return VLC_ENOMEM;
1070                 }
1071                 p_sys->psz_header = strdup( s );
1072                 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1073             }
1074         }
1075     }
1076 }
1077
1078 static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1079 {
1080     demux_sys_t *p_sys = p_demux->p_sys;
1081     text_t      *txt = &p_sys->txt;
1082
1083     /*
1084      * each line:
1085      *  h:m:s:Line1|Line2|Line3....
1086      *  or
1087      *  h:m:s Line1|Line2|Line3....
1088      *
1089      */
1090     char *p;
1091     char buffer_text[MAX_LINE + 1];
1092     mtime_t    i_start;
1093     unsigned int i;
1094
1095     p_subtitle->i_start = 0;
1096     p_subtitle->i_stop  = 0;
1097     p_subtitle->i_vobsub_location  = 0;
1098     p_subtitle->psz_text = NULL;
1099
1100     for( ;; )
1101     {
1102         int h, m, s;
1103         char c;
1104
1105         if( ( p = TextGetLine( txt ) ) == NULL )
1106         {
1107             return( VLC_EGENERIC );
1108         }
1109
1110         i_start = 0;
1111
1112         memset( buffer_text, '\0', MAX_LINE );
1113         if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1114         {
1115             i_start = ( (mtime_t)h * 3600*1000 +
1116                         (mtime_t)m * 60*1000 +
1117                         (mtime_t)s * 1000 ) * 1000;
1118             break;
1119         }
1120     }
1121
1122     /* replace | by \n */
1123     for( i = 0; i < strlen( buffer_text ); i++ )
1124     {
1125         if( buffer_text[i] == '|' )
1126         {
1127             buffer_text[i] = '\n';
1128         }
1129     }
1130     p_subtitle->i_start = i_start;
1131
1132     p_subtitle->i_stop  = 0;
1133     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1134     return( 0 );
1135 }
1136
1137 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1138 {
1139     if( psz_start )
1140     {
1141         if( strcasestr( psz_start, psz_str ) )
1142         {
1143             char *s = strcasestr( psz_start, psz_str );
1144
1145             s += strlen( psz_str );
1146
1147             return( s );
1148         }
1149     }
1150     for( ;; )
1151     {
1152         char *p;
1153         if( ( p = TextGetLine( txt ) ) == NULL )
1154         {
1155             return NULL;
1156         }
1157         if( strcasestr( p, psz_str ) )
1158         {
1159             char *s = strcasestr( p, psz_str );
1160
1161             s += strlen( psz_str );
1162
1163             return(  s);
1164         }
1165     }
1166 }
1167
1168 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1169 {
1170     demux_sys_t *p_sys = p_demux->p_sys;
1171     text_t      *txt = &p_sys->txt;
1172
1173     char *p;
1174     int i_start;
1175
1176     int  i_text;
1177     char buffer_text[10*MAX_LINE + 1];
1178
1179     p_subtitle->i_start = 0;
1180     p_subtitle->i_stop  = 0;
1181     p_subtitle->i_vobsub_location  = 0;
1182     p_subtitle->psz_text = NULL;
1183
1184 #define ADDC( c ) \
1185     if( i_text < 10*MAX_LINE )      \
1186     {                               \
1187         buffer_text[i_text++] = c;  \
1188         buffer_text[i_text] = '\0'; \
1189     }
1190
1191     /* search "Start=" */
1192     if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1193     {
1194         return VLC_EGENERIC;
1195     }
1196
1197     /* get start value */
1198     i_start = strtol( p, &p, 0 );
1199
1200     /* search <P */
1201     if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1202     {
1203         return VLC_EGENERIC;
1204     }
1205     /* search > */
1206     if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1207     {
1208         return VLC_EGENERIC;
1209     }
1210
1211     i_text = 0;
1212     buffer_text[0] = '\0';
1213     /* now get all txt until  a "Start=" line */
1214     for( ;; )
1215     {
1216         if( *p )
1217         {
1218             if( *p == '<' )
1219             {
1220                 if( !strncasecmp( p, "<br", 3 ) )
1221                 {
1222                     ADDC( '\n' );
1223                 }
1224                 else if( strcasestr( p, "Start=" ) )
1225                 {
1226                     TextPreviousLine( txt );
1227                     break;
1228                 }
1229                 p = ParseSamiSearch( txt, p, ">" );
1230             }
1231             else if( !strncmp( p, "&nbsp;", 6 ) )
1232             {
1233                 ADDC( ' ' );
1234                 p += 6;
1235             }
1236             else if( *p == '\t' )
1237             {
1238                 ADDC( ' ' );
1239                 p++;
1240             }
1241             else
1242             {
1243                 ADDC( *p );
1244                 p++;
1245             }
1246         }
1247         else
1248         {
1249             p = TextGetLine( txt );
1250         }
1251
1252         if( p == NULL )
1253         {
1254             break;
1255         }
1256     }
1257
1258     p_subtitle->i_start = i_start * 1000;
1259     p_subtitle->i_stop  = 0;
1260     p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1261
1262     return( VLC_SUCCESS );
1263 #undef ADDC
1264 }
1265
1266 static int  ParseVobSubIDX( demux_t *p_demux, subtitle_t *p_subtitle )
1267 {
1268     demux_sys_t *p_sys = p_demux->p_sys;
1269     text_t      *txt = &p_sys->txt;
1270
1271     /*
1272      * Parse the idx file. Each line:
1273      * timestamp: hh:mm:ss:mss, filepos: loc
1274      * hexint is the hex location of the vobsub in the .sub file
1275      *
1276      */
1277     char *p;
1278     char buffer_text[MAX_LINE + 1];
1279     unsigned int    i_start, i_location;
1280
1281     p_subtitle->i_start = 0;
1282     p_subtitle->i_stop  = 0;
1283     p_subtitle->i_vobsub_location  = 0;
1284     p_subtitle->psz_text = NULL;
1285
1286     for( ;; )
1287     {
1288         unsigned int h, m, s, ms, loc;
1289
1290         if( ( p = TextGetLine( txt ) ) == NULL )
1291         {
1292             return( VLC_EGENERIC );
1293         }
1294         i_start = 0;
1295
1296         memset( buffer_text, '\0', MAX_LINE );
1297         if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
1298                     &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
1299         {
1300             i_start = ( (mtime_t)h * 3600*1000 +
1301                         (mtime_t)m * 60*1000 +
1302                         (mtime_t)s * 1000 +
1303                         (mtime_t)ms ) * 1000;
1304             i_location = loc;
1305             break;
1306         }
1307     }
1308     p_subtitle->i_start = (mtime_t)i_start;
1309     p_subtitle->i_stop  = 0;
1310     p_subtitle->psz_text = NULL;
1311     p_subtitle->i_vobsub_location = i_location;
1312     fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
1313     return( 0 );
1314 }
1315
1316 static int  DemuxVobSub( demux_t *p_demux, block_t *p_bk )
1317 {
1318     demux_sys_t *p_sys = p_demux->p_sys;
1319     uint8_t     *p = p_bk->p_buffer;
1320     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
1321
1322     while( p < p_end )
1323     {
1324         int i_size = ps_pkt_size( p, p_end - p );
1325         block_t *p_pkt;
1326         int      i_id;
1327         int      i_spu;
1328
1329         if( i_size <= 0 )
1330         {
1331             break;
1332         }
1333         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
1334         {
1335             msg_Warn( p_demux, "invalid PES" );
1336             break;
1337         }
1338
1339         if( p[3] != 0xbd )
1340         {
1341             msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
1342             p += i_size;
1343             continue;
1344         }
1345
1346         /* Create a block */
1347         p_pkt = block_New( p_demux, i_size );
1348         memcpy( p_pkt->p_buffer, p, i_size);
1349         p += i_size;
1350
1351         i_id = ps_pkt_id( p_pkt );
1352         if( (i_id&0xffe0) != 0xbd20 ||
1353             ps_pkt_parse_pes( p_pkt, 1 ) )
1354         {
1355             block_Release( p_pkt );
1356             continue;
1357         }
1358         i_spu = i_id&0x1f;
1359         msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
1360
1361         /* FIXME i_spu == determines which of the spu tracks we will show. */
1362         if( p_sys->es && i_spu == 0 )
1363         {
1364             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
1365             p_pkt->i_length = 0;
1366             es_out_Send( p_demux->out, p_sys->es, p_pkt );
1367
1368             p_bk->i_pts = 0;    /* only first packet has a pts */
1369         }
1370         else
1371         {
1372             block_Release( p_pkt );
1373             continue;
1374         }
1375     }
1376
1377     return VLC_SUCCESS;
1378 }