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