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