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