]> git.sesse.net Git - vlc/blob - modules/demux/util/sub.c
4e4a93a443164425d1c6a9fd9b35d0a13e8a650d
[vlc] / modules / demux / util / sub.c
1 /*****************************************************************************
2  * sub.c
3  *****************************************************************************
4  * Copyright (C) 1999-2003 VideoLAN
5  * $Id: sub.c,v 1.40 2004/01/06 14:35:16 hartman Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <ctype.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include "vlc_video.h"
35 #include <codecs.h>
36
37 #include "sub.h"
38
39 #if (!defined( WIN32 ) || defined(__MINGW32__))
40 #    include <dirent.h>
41 #endif
42
43 static int  Open ( vlc_object_t *p_this );
44
45 static int  sub_open ( subtitle_demux_t *p_sub,
46                        input_thread_t  *p_input,
47                        char  *psz_name,
48                        mtime_t i_microsecperframe,
49                        int i_track_id );
50 static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate );
51 static int  sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date );
52 static void sub_close( subtitle_demux_t *p_sub );
53
54 static void sub_fix( subtitle_demux_t *p_sub );
55
56 static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1",
57   "ssa2-4", "vplayer", "sami", "vobsub" };
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 #define SUB_DELAY_LONGTEXT \
63     "Delay subtitles (in 1/10s)"
64 #define SUB_FPS_LONGTEXT \
65     "Override frames per second. " \
66     "It will only work with MicroDVD subtitles."
67 #define SUB_TYPE_LONGTEXT \
68     "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
69     "\"sami\" (auto for autodetection, it should always work)."
70
71 vlc_module_begin();
72     set_description( _("Text subtitles demux") );
73     set_capability( "subtitle demux", 12 );
74     add_category_hint( "Subtitles", NULL, VLC_TRUE );
75         add_float( "sub-fps", 25.0, NULL,
76                    "Frames per second",
77                    SUB_FPS_LONGTEXT, VLC_TRUE );
78         add_integer( "sub-delay", 0, NULL,
79                      "Delay subtitles (in 1/10s)",
80                      SUB_DELAY_LONGTEXT, VLC_TRUE );
81         add_string( "sub-type", "auto", NULL, "subtitles type",
82                     SUB_TYPE_LONGTEXT, VLC_TRUE );
83             change_string_list( ppsz_sub_type, 0, 0 );
84     set_callbacks( Open, NULL );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * Module initializer
89  *****************************************************************************/
90 static int Open ( vlc_object_t *p_this )
91 {
92     subtitle_demux_t *p_sub = (subtitle_demux_t*)p_this;
93
94     p_sub->pf_open  = sub_open;
95     p_sub->pf_demux = sub_demux;
96     p_sub->pf_seek  = sub_seek;
97     p_sub->pf_close = sub_close;
98
99     /* Initialize the variables */
100     var_Create( p_this, "sub-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
101     var_Create( p_this, "sub-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
102     var_Create( p_this, "sub-type", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
103
104     return VLC_SUCCESS;
105 }
106 #define MAX_TRY     256
107 #define MAX_LINE    2048
108
109 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
110
111 typedef struct
112 {
113     int     i_line_count;
114     int     i_line;
115     char    **line;
116 } text_t;
117
118 static int  text_load( text_t *txt, char *psz_name )
119 {
120     FILE *f;
121     int   i_line_max;
122
123     /* init txt */
124     i_line_max          = 100;
125     txt->i_line_count   = 0;
126     txt->i_line         = 0;
127     txt->line           = calloc( i_line_max, sizeof( char * ) );
128
129     /* open file */
130     if( !( f = fopen( psz_name, "rb" ) ) )
131     {
132         return VLC_EGENERIC;
133     }
134
135     /* load the complete file */
136     for( ;; )
137     {
138         char buffer[8096];
139         char *p;
140
141         if( fgets( buffer, 8096, f ) <= 0)
142         {
143             break;
144         }
145         while( ( p = strchr( buffer, '\r' ) ) )
146         {
147             *p = '\0';
148         }
149         while( ( p = strchr( buffer, '\n' ) ) )
150         {
151             *p = '\0';
152         }
153
154         txt->line[txt->i_line_count++] = strdup( buffer );
155
156         if( txt->i_line_count >= i_line_max )
157         {
158             i_line_max += 100;
159             txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
160         }
161     }
162
163     fclose( f );
164
165     if( txt->i_line_count <= 0 )
166     {
167         FREE( txt->line );
168         return( VLC_EGENERIC );
169     }
170
171     return( VLC_SUCCESS );
172 }
173 static void text_unload( text_t *txt )
174 {
175     int i;
176
177     for( i = 0; i < txt->i_line_count; i++ )
178     {
179         FREE( txt->line[i] );
180     }
181     FREE( txt->line );
182     txt->i_line       = 0;
183     txt->i_line_count = 0;
184 }
185
186 static char *text_get_line( text_t *txt )
187 {
188     if( txt->i_line >= txt->i_line_count )
189     {
190         return( NULL );
191     }
192
193     return( txt->line[txt->i_line++] );
194 }
195 static void text_previous_line( text_t *txt )
196 {
197     if( txt->i_line > 0 )
198     {
199         txt->i_line--;
200     }
201 }
202 static void text_rewind( text_t *txt )
203 {
204     txt->i_line = 0;
205 }
206
207 static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
208 static int  sub_SubRipRead  ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
209 static int  sub_SSARead     ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
210 static int  sub_Vplayer     ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
211 static int  sub_Sami        ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
212 static int  sub_VobSub      ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
213
214 static struct
215 {
216     char *psz_type_name;
217     int  i_type;
218     char *psz_name;
219     int  (*pf_read_subtitle)    ( subtitle_demux_t *, text_t *, subtitle_t*, mtime_t );
220 } sub_read_subtitle_function [] =
221 {
222     { "microdvd",   SUB_TYPE_MICRODVD,  "MicroDVD", sub_MicroDvdRead },
223     { "subrip",     SUB_TYPE_SUBRIP,    "SubRIP",   sub_SubRipRead },
224     { "ssa1",       SUB_TYPE_SSA1,      "SSA-1",    sub_SSARead },
225     { "ssa2-4",     SUB_TYPE_SSA2_4,    "SSA-2/3/4",sub_SSARead },
226     { "vplayer",    SUB_TYPE_VPLAYER,   "VPlayer",  sub_Vplayer },
227     { "sami",       SUB_TYPE_SAMI,      "SAMI",     sub_Sami },
228     { "vobsub",     SUB_TYPE_VOBSUB,    "VobSub",   sub_VobSub },
229     { NULL,         SUB_TYPE_UNKNOWN,   "Unknown",  NULL }
230 };
231
232 static char * local_stristr( char *psz_big, char *psz_little)
233 {
234     char *p_pos = psz_big;
235
236     if (!psz_big || !psz_little || !*psz_little) return psz_big;
237
238     while (*p_pos)
239     {
240         if (toupper(*p_pos) == toupper(*psz_little))
241         {
242             char * psz_cur1 = p_pos + 1;
243             char * psz_cur2 = psz_little + 1;
244             while (*psz_cur1 && *psz_cur2 && toupper(*psz_cur1) == toupper(*psz_cur2))
245             {
246                 psz_cur1++;
247                 psz_cur2++;
248             }
249             if (!*psz_cur2) return p_pos;
250         }
251         p_pos++;
252     }
253     return NULL;
254 }
255
256 /*****************************************************************************
257  * sub_open: Open a subtitle file and add subtitle ES
258  *****************************************************************************/
259 static int  sub_open ( subtitle_demux_t *p_sub,
260                        input_thread_t  *p_input,
261                        char     *psz_name,
262                        mtime_t i_microsecperframe,
263                        int i_track_id )
264 {
265     text_t  txt;
266     vlc_value_t val;
267     es_format_t  fmt;
268
269     int     i;
270     int     i_sub_type;
271     int     i_max;
272     int (*pf_read_subtitle)( subtitle_demux_t *, text_t *, subtitle_t *, mtime_t ) = NULL;
273
274     p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
275     p_sub->p_es = NULL;
276     p_sub->i_subtitles = 0;
277     p_sub->subtitle = NULL;
278     p_sub->p_input = p_input;
279
280     if( !psz_name || !*psz_name )
281     {
282         msg_Err( p_sub, "no subtitle file specified" );
283         if( psz_name ) free( psz_name );
284         return VLC_EGENERIC;
285     }
286
287     /* *** load the file *** */
288     if( text_load( &txt, psz_name ) )
289     {
290         msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
291         free( psz_name );
292         return VLC_EGENERIC;
293     }
294     msg_Dbg( p_sub, "opened `%s'", psz_name );
295     free( psz_name );
296
297     var_Get( p_sub, "sub-fps", &val );
298     if( val.i_int >= 1.0 )
299     {
300         i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
301     }
302     else if( val.f_float <= 0 )
303     {
304         i_microsecperframe = 40000; /* default: 25fps */
305     }
306
307     var_Get( p_sub, "sub-type", &val);
308     if( val.psz_string && *val.psz_string )
309     {
310         int i;
311
312         for( i = 0; ; i++ )
313         {
314             if( sub_read_subtitle_function[i].psz_type_name == NULL )
315             {
316                 i_sub_type = SUB_TYPE_UNKNOWN;
317                 break;
318             }
319             if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
320                          val.psz_string ) )
321             {
322                 i_sub_type = sub_read_subtitle_function[i].i_type;
323                 break;
324             }
325         }
326     }
327     else
328     {
329         i_sub_type = SUB_TYPE_UNKNOWN;
330     }
331     FREE( val.psz_string );
332
333     /* *** Now try to autodetect subtitle format *** */
334     if( i_sub_type == SUB_TYPE_UNKNOWN )
335     {
336         int     i_try;
337         char    *s;
338
339         msg_Dbg( p_input, "trying to autodetect file format" );
340         for( i_try = 0; i_try < MAX_TRY; i_try++ )
341         {
342             int i_dummy;
343
344             if( ( s = text_get_line( &txt ) ) == NULL )
345             {
346                 break;
347             }
348
349             if( local_stristr( s, "<SAMI>" ) )
350             {
351                 i_sub_type = SUB_TYPE_SAMI;
352                 break;
353             }
354             else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
355                      sscanf( s, "{%d}{}", &i_dummy ) == 1)
356             {
357                 i_sub_type = SUB_TYPE_MICRODVD;
358                 break;
359             }
360             else if( sscanf( s,
361                              "%d:%d:%d,%d --> %d:%d:%d,%d",
362                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
363                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
364             {
365                 i_sub_type = SUB_TYPE_SUBRIP;
366                 break;
367             }
368             else if( sscanf( s,
369                              "!: This is a Sub Station Alpha v%d.x script.",
370                              &i_dummy ) == 1)
371             {
372                 if( i_dummy <= 1 )
373                 {
374                     i_sub_type = SUB_TYPE_SSA1;
375                 }
376                 else
377                 {
378                     i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
379                 }
380                 break;
381             }
382             else if( local_stristr( s, "This is a Sub Station Alpha v4 script" ) )
383             {
384                 i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
385                 break;
386             }
387             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
388             {
389                 i_sub_type = SUB_TYPE_SSA2_4; /* could be wrong */
390                 break;
391             }
392             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
393                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
394             {
395                 i_sub_type = SUB_TYPE_VPLAYER;
396                 break;
397             }
398             else if( local_stristr( s, "# VobSub index file" ) )
399             {
400                 i_sub_type = SUB_TYPE_VOBSUB;
401                 break;
402             }
403         }
404
405         text_rewind( &txt );
406     }
407
408     /* *** Load this file in memory *** */
409     for( i = 0; ; i++ )
410     {
411         if( sub_read_subtitle_function[i].i_type == SUB_TYPE_UNKNOWN )
412         {
413             msg_Dbg( p_input, "unknown subtitle file" );
414             text_unload( &txt );
415             return VLC_EGENERIC;
416         }
417
418         if( sub_read_subtitle_function[i].i_type == i_sub_type )
419         {
420             msg_Dbg( p_input, "detected %s format",
421                     sub_read_subtitle_function[i].psz_name );
422             p_sub->i_sub_type = i_sub_type;
423             pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle;
424             break;
425         }
426     }
427
428     for( i_max = 0;; )
429     {
430         if( p_sub->i_subtitles >= i_max )
431         {
432             i_max += 128;
433             if( p_sub->subtitle )
434             {
435                 if( !( p_sub->subtitle = realloc( p_sub->subtitle,
436                                            sizeof( subtitle_t ) * i_max ) ) )
437                 {
438                     msg_Err( p_sub, "out of memory");
439                     return VLC_ENOMEM;
440                 }
441             }
442             else
443             {
444                 if( !(  p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max ) ) )
445                 {
446                     msg_Err( p_sub, "out of memory");
447                     return VLC_ENOMEM;
448                 }
449             }
450         }
451         if( pf_read_subtitle( p_sub, &txt,
452                               p_sub->subtitle + p_sub->i_subtitles,
453                               i_microsecperframe ) < 0 )
454         {
455             break;
456         }
457         p_sub->i_subtitles++;
458     }
459     msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
460
461     /* *** Close the file *** */
462     text_unload( &txt );
463
464     /* *** fix subtitle (order and time) *** */
465     p_sub->i_subtitle = 0;  /* will be modified by sub_fix */
466     if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
467     {
468         sub_fix( p_sub );
469     }
470
471     /* *** add subtitle ES *** */
472     if( p_sub->i_sub_type == SUB_TYPE_VOBSUB )
473     {
474         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
475     }
476     else if( p_sub->i_sub_type == SUB_TYPE_SSA1 ||
477              p_sub->i_sub_type == SUB_TYPE_SSA2_4 )
478     {
479         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
480     }
481     else
482     {
483         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
484     }
485     if( p_sub->psz_header != NULL )
486     {
487         fmt.i_extra = strlen( p_sub->psz_header ) + 1;
488         fmt.p_extra = strdup( p_sub->psz_header );
489     }
490     p_sub->p_es = es_out_Add( p_input->p_es_out, &fmt );
491
492     p_sub->i_previously_selected = 0;
493     return VLC_SUCCESS;
494 }
495
496 /*****************************************************************************
497  * sub_demux: Send subtitle to decoder until i_maxdate
498  *****************************************************************************/
499 static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
500 {
501     input_thread_t *p_input = p_sub->p_input;
502     vlc_bool_t     b;
503
504     es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_sub->p_es, &b );
505     if( b && !p_sub->i_previously_selected )
506     {
507         p_sub->i_previously_selected = 1;
508         p_sub->pf_seek( p_sub, i_maxdate );
509         return VLC_SUCCESS;
510     }
511     else if( !b && p_sub->i_previously_selected )
512     {
513         p_sub->i_previously_selected = 0;
514         return VLC_SUCCESS;
515     }
516
517     while( p_sub->i_subtitle < p_sub->i_subtitles &&
518            p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
519     {
520         block_t *p_block;
521         int i_len;
522
523         i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
524
525         if( i_len <= 1 )
526         {
527             /* empty subtitle */
528             p_sub->i_subtitle++;
529             continue;
530         }
531
532         if( !( p_block = block_New( p_sub->p_input, i_len ) ) )
533         {
534             p_sub->i_subtitle++;
535             continue;
536         }
537
538         p_block->i_pts =
539             input_ClockGetTS( p_sub->p_input,
540                               p_sub->p_input->stream.p_selected_program,
541                               p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
542         if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
543         {
544             /* FIXME kludge ...
545              * i_dts means end of display...
546              */
547             p_block->i_dts =
548                 input_ClockGetTS( p_sub->p_input,
549                               p_sub->p_input->stream.p_selected_program,
550                               p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
551         }
552         else
553         {
554             p_block->i_dts = 0;
555         }
556
557         memcpy( p_block->p_buffer,
558                 p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
559
560         if( p_block->i_pts > 0 )
561         {
562             es_out_Send( p_input->p_es_out, p_sub->p_es, p_block );
563         }
564         else
565         {
566             block_Release( p_block );
567         }
568
569         p_sub->i_subtitle++;
570     }
571     return VLC_SUCCESS;
572 }
573
574 /*****************************************************************************
575  * sub_seek: Seek to i_date
576  *****************************************************************************/
577 static int  sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
578 {
579     /* should be fast enough... */
580     p_sub->i_subtitle = 0;
581     while( p_sub->i_subtitle < p_sub->i_subtitles &&
582            p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
583     {
584         p_sub->i_subtitle++;
585     }
586     return( 0 );
587 }
588
589 /*****************************************************************************
590  * sub_close: Close subtitle demux
591  *****************************************************************************/
592 static void sub_close( subtitle_demux_t *p_sub )
593 {
594     if( p_sub->subtitle )
595     {
596         int i;
597         for( i = 0; i < p_sub->i_subtitles; i++ )
598         {
599             if( p_sub->subtitle[i].psz_text )
600             {
601                 free( p_sub->subtitle[i].psz_text );
602             }
603         }
604         free( p_sub->subtitle );
605     }
606 }
607
608 /*****************************************************************************
609  * sub_fix: fix time stamp and order of subtitle
610  *****************************************************************************/
611 static void  sub_fix( subtitle_demux_t *p_sub )
612 {
613     int     i;
614     mtime_t i_delay;
615     int     i_index;
616     int     i_done;
617     vlc_value_t val;
618
619     /* *** fix order (to be sure...) *** */
620     /* We suppose that there are near in order and this durty bubble sort
621      * wont take too much time
622      */
623     do
624     {
625         i_done = 1;
626         for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
627         {
628             if( p_sub->subtitle[i_index].i_start <
629                     p_sub->subtitle[i_index - 1].i_start )
630             {
631                 subtitle_t sub_xch;
632                 memcpy( &sub_xch,
633                         p_sub->subtitle + i_index - 1,
634                         sizeof( subtitle_t ) );
635                 memcpy( p_sub->subtitle + i_index - 1,
636                         p_sub->subtitle + i_index,
637                         sizeof( subtitle_t ) );
638                 memcpy( p_sub->subtitle + i_index,
639                         &sub_xch,
640                         sizeof( subtitle_t ) );
641                 i_done = 0;
642             }
643         }
644     } while( !i_done );
645
646     /* *** and at the end add delay *** */
647     var_Get( p_sub, "sub-delay", &val );
648     i_delay = (mtime_t) val.i_int * 100000;
649     if( i_delay != 0 )
650     {
651         for( i = 0; i < p_sub->i_subtitles; i++ )
652         {
653             p_sub->subtitle[i].i_start += i_delay;
654             p_sub->subtitle[i].i_stop += i_delay;
655             if( p_sub->subtitle[i].i_start < 0 )
656             {
657                 p_sub->i_subtitle = i + 1;
658             }
659         }
660     }
661 }
662
663
664
665 /*****************************************************************************
666  * Specific Subtitle function
667  *****************************************************************************/
668 static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
669 {
670     /*
671      * each line:
672      *  {n1}{n2}Line1|Line2|Line3....
673      * where n1 and n2 are the video frame number...
674      *
675      */
676     char *s;
677
678     char buffer_text[MAX_LINE + 1];
679     uint32_t    i_start;
680     uint32_t    i_stop;
681     unsigned int i;
682
683     for( ;; )
684     {
685         if( ( s = text_get_line( txt ) ) == NULL )
686         {
687             return( VLC_EGENERIC );
688         }
689         i_start = 0;
690         i_stop  = 0;
691
692         memset( buffer_text, '\0', MAX_LINE );
693         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
694             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
695         {
696             break;
697         }
698     }
699     /* replace | by \n */
700     for( i = 0; i < strlen( buffer_text ); i++ )
701     {
702         if( buffer_text[i] == '|' )
703         {
704             buffer_text[i] = '\n';
705         }
706     }
707     p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
708     p_subtitle->i_stop  = (mtime_t)i_stop  * (mtime_t)i_microsecperframe;
709     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
710     return( 0 );
711 }
712
713 static int  sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
714 {
715     /*
716      * n
717      * h1:m1:s1,d1 --> h2:m2:s2,d2
718      * Line1
719      * Line2
720      * ...
721      * [empty line]
722      *
723      */
724     char *s;
725     char buffer_text[ 10 * MAX_LINE];
726     int  i_buffer_text;
727     mtime_t     i_start;
728     mtime_t     i_stop;
729
730     for( ;; )
731     {
732         int h1, m1, s1, d1, h2, m2, s2, d2;
733         if( ( s = text_get_line( txt ) ) == NULL )
734         {
735             return( VLC_EGENERIC );
736         }
737         if( sscanf( s,
738                     "%d:%d:%d,%d --> %d:%d:%d,%d",
739                     &h1, &m1, &s1, &d1,
740                     &h2, &m2, &s2, &d2 ) == 8 )
741         {
742             i_start = ( (mtime_t)h1 * 3600*1000 +
743                         (mtime_t)m1 * 60*1000 +
744                         (mtime_t)s1 * 1000 +
745                         (mtime_t)d1 ) * 1000;
746
747             i_stop  = ( (mtime_t)h2 * 3600*1000 +
748                         (mtime_t)m2 * 60*1000 +
749                         (mtime_t)s2 * 1000 +
750                         (mtime_t)d2 ) * 1000;
751
752             /* Now read text until an empty line */
753             for( i_buffer_text = 0;; )
754             {
755                 int i_len;
756                 if( ( s = text_get_line( txt ) ) == NULL )
757                 {
758                     return( VLC_EGENERIC );
759                 }
760
761                 i_len = strlen( s );
762                 if( i_len <= 1 )
763                 {
764                     /* empty line -> end of this subtitle */
765                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
766                     p_subtitle->i_start = i_start;
767                     p_subtitle->i_stop = i_stop;
768                     p_subtitle->psz_text = strdup( buffer_text );
769                     return( 0 );
770                 }
771                 else
772                 {
773                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
774                     {
775                         memcpy( buffer_text + i_buffer_text,
776                                 s,
777                                 i_len );
778                         i_buffer_text += i_len;
779
780                         buffer_text[i_buffer_text] = '\n';
781                         i_buffer_text++;
782                     }
783                 }
784             }
785         }
786     }
787 }
788
789
790 static int  sub_SSARead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
791 {
792     char buffer_text[ 10 * MAX_LINE];
793     char *s;
794     mtime_t     i_start;
795     mtime_t     i_stop;
796
797     for( ;; )
798     {
799         int h1, m1, s1, c1, h2, m2, s2, c2;
800         int i_dummy;
801
802         if( ( s = text_get_line( txt ) ) == NULL )
803         {
804             return( VLC_EGENERIC );
805         }
806         p_subtitle->psz_text = malloc( strlen( s ) );
807
808         if( sscanf( s,
809                     "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
810                     &i_dummy,
811                     &h1, &m1, &s1, &c1,
812                     &h2, &m2, &s2, &c2,
813                     buffer_text ) == 10 )
814         {
815             i_start = ( (mtime_t)h1 * 3600*1000 +
816                         (mtime_t)m1 * 60*1000 +
817                         (mtime_t)s1 * 1000 +
818                         (mtime_t)c1 * 10 ) * 1000;
819
820             i_stop  = ( (mtime_t)h2 * 3600*1000 +
821                         (mtime_t)m2 * 60*1000 +
822                         (mtime_t)s2 * 1000 +
823                         (mtime_t)c2 * 10 ) * 1000;
824
825             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
826             if( p_sub->i_sub_type == SUB_TYPE_SSA1 )
827             {
828                 sprintf( p_subtitle->psz_text, ",%d%s", i_dummy, strdup( buffer_text) );
829             }
830             else
831             {
832                 sprintf( p_subtitle->psz_text, ",%d,%s", i_dummy, strdup( buffer_text) );
833             }
834             p_subtitle->i_start = i_start;
835             p_subtitle->i_stop = i_stop;
836             return( 0 );
837         }
838         else
839         {
840             /* All the other stuff we add to the header field */
841             if( p_sub->psz_header != NULL )
842             {
843                 if( !( p_sub->psz_header = realloc( p_sub->psz_header,
844                           strlen( p_sub->psz_header ) + strlen( s ) + 2 ) ) )
845                 {
846                     msg_Err( p_sub, "out of memory");
847                     return VLC_ENOMEM;
848                 }
849                 p_sub->psz_header = strcat( p_sub->psz_header, strdup( s ) );
850                 p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
851             }
852             else
853             {
854                 if( !( p_sub->psz_header = malloc( strlen( s ) + 2 ) ) )
855                 {
856                     msg_Err( p_sub, "out of memory");
857                     return VLC_ENOMEM;
858                 }
859                 p_sub->psz_header = strdup( s );
860                 p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
861             }
862         }
863     }
864 }
865
866 static int  sub_Vplayer( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
867 {
868     /*
869      * each line:
870      *  h:m:s:Line1|Line2|Line3....
871      *  or
872      *  h:m:s Line1|Line2|Line3....
873      * where n1 and n2 are the video frame number...
874      *
875      */
876     char *p;
877     char buffer_text[MAX_LINE + 1];
878     mtime_t    i_start;
879     unsigned int i;
880
881     for( ;; )
882     {
883         int h, m, s;
884         char c;
885
886         if( ( p = text_get_line( txt ) ) == NULL )
887         {
888             return( VLC_EGENERIC );
889         }
890
891         i_start = 0;
892
893         memset( buffer_text, '\0', MAX_LINE );
894         if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
895         {
896             i_start = ( (mtime_t)h * 3600*1000 +
897                         (mtime_t)m * 60*1000 +
898                         (mtime_t)s * 1000 ) * 1000;
899             break;
900         }
901     }
902
903     /* replace | by \n */
904     for( i = 0; i < strlen( buffer_text ); i++ )
905     {
906         if( buffer_text[i] == '|' )
907         {
908             buffer_text[i] = '\n';
909         }
910     }
911     p_subtitle->i_start = i_start;
912
913     p_subtitle->i_stop  = 0;
914     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
915     return( 0 );
916 }
917
918 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
919 {
920     if( psz_start )
921     {
922         if( local_stristr( psz_start, psz_str ) )
923         {
924             char *s = local_stristr( psz_start, psz_str );
925
926             s += strlen( psz_str );
927
928             return( s );
929         }
930     }
931     for( ;; )
932     {
933         char *p;
934         if( ( p = text_get_line( txt ) ) == NULL )
935         {
936             return NULL;
937         }
938         if( local_stristr( p, psz_str ) )
939         {
940             char *s = local_stristr( p, psz_str );
941
942             s += strlen( psz_str );
943
944             return(  s);
945         }
946     }
947 }
948
949 static int  sub_Sami( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
950 {
951     char *p;
952     int i_start;
953
954     int  i_text;
955     char buffer_text[10*MAX_LINE + 1];
956 #define ADDC( c ) \
957     if( i_text < 10*MAX_LINE )      \
958     {                               \
959         buffer_text[i_text++] = c;  \
960         buffer_text[i_text] = '\0'; \
961     }
962
963     /* search "Start=" */
964     if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
965     {
966         return VLC_EGENERIC;
967     }
968
969     /* get start value */
970     i_start = strtol( p, &p, 0 );
971
972     /* search <P */
973     if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
974     {
975         return VLC_EGENERIC;
976     }
977     /* search > */
978     if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
979     {
980         return VLC_EGENERIC;
981     }
982
983     i_text = 0;
984     buffer_text[0] = '\0';
985     /* now get all txt until  a "Start=" line */
986     for( ;; )
987     {
988         if( *p )
989         {
990             if( *p == '<' )
991             {
992                 if( !strncasecmp( p, "<br", 3 ) )
993                 {
994                     ADDC( '\n' );
995                 }
996                 else if( local_stristr( p, "Start=" ) )
997                 {
998                     text_previous_line( txt );
999                     break;
1000                 }
1001                 p = sub_SamiSearch( txt, p, ">" );
1002             }
1003             else if( !strncmp( p, "&nbsp;", 6 ) )
1004             {
1005                 ADDC( ' ' );
1006                 p += 6;
1007             }
1008             else if( *p == '\t' )
1009             {
1010                 ADDC( ' ' );
1011                 p++;
1012             }
1013             else
1014             {
1015                 ADDC( *p );
1016                 p++;
1017             }
1018         }
1019         else
1020         {
1021             p = text_get_line( txt );
1022         }
1023
1024         if( p == NULL )
1025         {
1026             break;
1027         }
1028     }
1029
1030     p_subtitle->i_start = i_start * 1000;
1031     p_subtitle->i_stop  = 0;
1032     p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1033
1034     return( VLC_SUCCESS );
1035 #undef ADDC
1036 }
1037
1038 static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
1039 {
1040     /*
1041      * Parse the idx file. Each line:
1042      * timestamp: hh:mm:ss:mss, filepos: loc
1043      * hexint is the hex location of the vobsub in the .sub file
1044      *
1045      */
1046     char *p;
1047
1048     char buffer_text[MAX_LINE + 1];
1049     uint32_t    i_start, i_location;
1050
1051     for( ;; )
1052     {
1053         unsigned int h, m, s, ms, loc;
1054
1055         if( ( p = text_get_line( txt ) ) == NULL )
1056         {
1057             return( VLC_EGENERIC );
1058         }
1059         i_start = 0;
1060
1061         memset( buffer_text, '\0', MAX_LINE );
1062         if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
1063                     &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
1064         {
1065             i_start = ( (mtime_t)h * 3600*1000 +
1066                         (mtime_t)m * 60*1000 +
1067                         (mtime_t)s * 1000 +
1068                         (mtime_t)ms ) * 1000;
1069             i_location = loc;
1070             break;
1071         }
1072     }
1073     p_subtitle->i_start = (mtime_t)i_start;
1074     p_subtitle->i_stop  = 0;
1075     p_subtitle->i_vobsub_location = i_location;
1076     fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
1077     return( 0 );
1078 }
1079
1080