]> git.sesse.net Git - vlc/blob - modules/demux/util/sub.c
* ALL: changed the prototype of input_AddES() to include enough information so we...
[vlc] / modules / demux / util / sub.c
1 /*****************************************************************************
2  * sub.c
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: sub.c,v 1.13 2003/05/05 22:23:37 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
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, p_input->stream.p_selected_program,
429                                0xff,    // FIXME
430                                SPU_ES, NULL, 0 );
431     vlc_mutex_unlock( &p_input->stream.stream_lock );
432
433     p_sub->p_es->i_stream_id = 0xff;    // FIXME
434     p_sub->p_es->i_fourcc    = VLC_FOURCC( 's','u','b','t' );
435
436     p_sub->i_previously_selected = 0;
437     return VLC_SUCCESS;
438 }
439
440 /*****************************************************************************
441  * sub_demux: Send subtitle to decoder until i_maxdate
442  *****************************************************************************/
443 static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
444 {
445
446     if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
447     {
448         p_sub->i_previously_selected = 1;
449         p_sub->pf_seek( p_sub, i_maxdate );
450         return VLC_SUCCESS;
451     }
452     else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
453     {
454         p_sub->i_previously_selected = 0;
455         return VLC_SUCCESS;
456     }
457
458     while( p_sub->i_subtitle < p_sub->i_subtitles &&
459            p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
460     {
461         pes_packet_t    *p_pes;
462         data_packet_t   *p_data;
463
464         int i_len;
465
466         i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
467
468         if( i_len <= 1 )
469         {
470             /* empty subtitle */
471             p_sub->i_subtitle++;
472             continue;
473         }
474         if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
475         {
476             p_sub->i_subtitle++;
477             continue;
478         }
479
480         if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
481                                          i_len ) ) )
482         {
483             input_DeletePES( p_sub->p_input->p_method_data, p_pes );
484             p_sub->i_subtitle++;
485             continue;
486         }
487         p_data->p_payload_end = p_data->p_payload_start + i_len;
488
489         p_pes->i_pts =
490             input_ClockGetTS( p_sub->p_input,
491                               p_sub->p_input->stream.p_selected_program,
492                               p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
493         if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
494         {
495             /* FIXME kludge ...
496              * i_dts means end of display...
497              */
498             p_pes->i_dts =
499                 input_ClockGetTS( p_sub->p_input,
500                               p_sub->p_input->stream.p_selected_program,
501                               p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
502         }
503         else
504         {
505             p_pes->i_dts = 0;
506         }
507         p_pes->i_nb_data = 1;
508         p_pes->p_first =
509             p_pes->p_last = p_data;
510         p_pes->i_pes_size = i_len;
511
512         memcpy( p_data->p_payload_start,
513                 p_sub->subtitle[p_sub->i_subtitle].psz_text,
514                 i_len );
515         if( p_sub->p_es->p_decoder_fifo && p_pes->i_pts > 0 )
516         {
517
518             input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
519         }
520         else
521         {
522             input_DeletePES( p_sub->p_input->p_method_data, p_pes );
523         }
524
525         p_sub->i_subtitle++;
526     }
527     return( 0 );
528 }
529
530 /*****************************************************************************
531  * sub_seek: Seek to i_date
532  *****************************************************************************/
533 static int  sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
534 {
535     /* should be fast enough... */
536     p_sub->i_subtitle = 0;
537     while( p_sub->i_subtitle < p_sub->i_subtitles &&
538            p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
539     {
540         p_sub->i_subtitle++;
541     }
542
543     return( 0 );
544 }
545
546 /*****************************************************************************
547  * sub_close: Close subtitle demux
548  *****************************************************************************/
549 static void sub_close( subtitle_demux_t *p_sub )
550 {
551     if( p_sub->subtitle )
552     {
553         int i;
554         for( i = 0; i < p_sub->i_subtitles; i++ )
555         {
556             if( p_sub->subtitle[i].psz_text )
557             {
558                 free( p_sub->subtitle[i].psz_text );
559             }
560         }
561         free( p_sub->subtitle );
562     }
563 }
564 /*****************************************************************************
565  *
566  * sub_fix: fix time stamp and order of subtitle
567  *****************************************************************************/
568 static void  sub_fix( subtitle_demux_t *p_sub )
569 {
570     int     i;
571     mtime_t i_delay;
572     int     i_index;
573     int     i_done;
574
575     /* *** fix order (to be sure...) *** */
576     /* We suppose that there are near in order and this durty bubble sort
577      * wont take too much time
578      */
579     do
580     {
581         i_done = 1;
582         for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
583         {
584             if( p_sub->subtitle[i_index].i_start <
585                     p_sub->subtitle[i_index - 1].i_start )
586             {
587                 subtitle_t sub_xch;
588                 memcpy( &sub_xch,
589                         p_sub->subtitle + i_index - 1,
590                         sizeof( subtitle_t ) );
591                 memcpy( p_sub->subtitle + i_index - 1,
592                         p_sub->subtitle + i_index,
593                         sizeof( subtitle_t ) );
594                 memcpy( p_sub->subtitle + i_index,
595                         &sub_xch,
596                         sizeof( subtitle_t ) );
597                 i_done = 0;
598             }
599         }
600     } while( !i_done );
601
602     /* *** and at the end add delay *** */
603     i_delay = (mtime_t)config_GetInt( p_sub, "sub-delay" ) * 100000;
604     if( i_delay != 0 )
605     {
606         for( i = 0; i < p_sub->i_subtitles; i++ )
607         {
608             p_sub->subtitle[i].i_start += i_delay;
609             p_sub->subtitle[i].i_stop += i_delay;
610             if( p_sub->subtitle[i].i_start < 0 )
611             {
612                 p_sub->i_subtitle = i + 1;
613             }
614         }
615     }
616 }
617
618
619
620 /*****************************************************************************
621  * Specific Subtitle function
622  *****************************************************************************/
623 static int  sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
624 {
625     /*
626      * each line:
627      *  {n1}{n2}Line1|Line2|Line3....
628      * where n1 and n2 are the video frame number...
629      *
630      */
631     char *s;
632
633     char buffer_text[MAX_LINE + 1];
634     uint32_t    i_start;
635     uint32_t    i_stop;
636     unsigned int i;
637
638     for( ;; )
639     {
640         if( ( s = text_get_line( txt ) ) == NULL )
641         {
642             return( VLC_EGENERIC );
643         }
644         i_start = 0;
645         i_stop  = 0;
646
647         memset( buffer_text, '\0', MAX_LINE );
648         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
649             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
650         {
651             break;
652         }
653     }
654     /* replace | by \n */
655     for( i = 0; i < strlen( buffer_text ); i++ )
656     {
657         if( buffer_text[i] == '|' )
658         {
659             buffer_text[i] = '\n';
660         }
661     }
662     p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
663     p_subtitle->i_stop  = (mtime_t)i_stop  * (mtime_t)i_microsecperframe;
664     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
665     return( 0 );
666 }
667
668 static int  sub_SubRipRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
669 {
670     /*
671      * n
672      * h1:m1:s1,d1 --> h2:m2:s2,d2
673      * Line1
674      * Line2
675      * ...
676      * [empty line]
677      *
678      */
679     char *s;
680     char buffer_text[ 10 * MAX_LINE];
681     int  i_buffer_text;
682     mtime_t     i_start;
683     mtime_t     i_stop;
684
685     for( ;; )
686     {
687         int h1, m1, s1, d1, h2, m2, s2, d2;
688         if( ( s = text_get_line( txt ) ) == NULL )
689         {
690             return( VLC_EGENERIC );
691         }
692         if( sscanf( s,
693                     "%d:%d:%d,%d --> %d:%d:%d,%d",
694                     &h1, &m1, &s1, &d1,
695                     &h2, &m2, &s2, &d2 ) == 8 )
696         {
697             i_start = ( (mtime_t)h1 * 3600*1000 +
698                         (mtime_t)m1 * 60*1000 +
699                         (mtime_t)s1 * 1000 +
700                         (mtime_t)d1 ) * 1000;
701
702             i_stop  = ( (mtime_t)h2 * 3600*1000 +
703                         (mtime_t)m2 * 60*1000 +
704                         (mtime_t)s2 * 1000 +
705                         (mtime_t)d2 ) * 1000;
706
707             /* Now read text until an empty line */
708             for( i_buffer_text = 0;; )
709             {
710                 int i_len;
711                 if( ( s = text_get_line( txt ) ) == NULL )
712                 {
713                     return( VLC_EGENERIC );
714                 }
715
716                 i_len = strlen( s );
717                 if( i_len <= 1 )
718                 {
719                     // empty line -> end of this subtitle
720                     buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
721                     p_subtitle->i_start = i_start;
722                     p_subtitle->i_stop = i_stop;
723                     p_subtitle->psz_text = strdup( buffer_text );
724                     return( 0 );
725                 }
726                 else
727                 {
728                     if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
729                     {
730                         memcpy( buffer_text + i_buffer_text,
731                                 s,
732                                 i_len );
733                         i_buffer_text += i_len;
734
735                         buffer_text[i_buffer_text] = '\n';
736                         i_buffer_text++;
737                     }
738                 }
739             }
740         }
741     }
742 }
743
744
745 static int  sub_SSARead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe, int i_comma_count )
746 {
747     char buffer_text[ 10 * MAX_LINE];
748     char *s;
749     char *p_buffer_text;
750     mtime_t     i_start;
751     mtime_t     i_stop;
752     int         i_comma;
753     int         i_text;
754
755     for( ;; )
756     {
757         int h1, m1, s1, c1, h2, m2, s2, c2;
758         int i_dummy;
759
760         if( ( s = text_get_line( txt ) ) == NULL )
761         {
762             return( VLC_EGENERIC );
763         }
764         if( sscanf( s,
765                     "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
766                     &i_dummy,
767                     &h1, &m1, &s1, &c1,
768                     &h2, &m2, &s2, &c2,
769                     buffer_text ) == 10 )
770         {
771             i_start = ( (mtime_t)h1 * 3600*1000 +
772                         (mtime_t)m1 * 60*1000 +
773                         (mtime_t)s1 * 1000 +
774                         (mtime_t)c1 * 10 ) * 1000;
775
776             i_stop  = ( (mtime_t)h2 * 3600*1000 +
777                         (mtime_t)m2 * 60*1000 +
778                         (mtime_t)s2 * 1000 +
779                         (mtime_t)c2 * 10 ) * 1000;
780
781             p_buffer_text = buffer_text;
782             i_comma = 3;
783             while( i_comma < i_comma_count &&
784                    *p_buffer_text != '\0' )
785             {
786                 if( *p_buffer_text == ',' )
787                 {
788                     i_comma++;
789                 }
790                 p_buffer_text++;
791             }
792             p_subtitle->psz_text = malloc( strlen( p_buffer_text ) + 1);
793             i_text = 0;
794             while( *p_buffer_text )
795             {
796                 if( *p_buffer_text == '\\' && ( *p_buffer_text =='n' || *p_buffer_text =='N' ) )
797                 {
798                     p_subtitle->psz_text[i_text] = '\n';
799                     i_text++;
800                     p_buffer_text += 2;
801                 }
802                 else if( *p_buffer_text == '{' && *p_buffer_text == '\\')
803                 {
804                     while( *p_buffer_text && *p_buffer_text != '}' )
805                     {
806                         p_buffer_text++;
807                     }
808                 }
809                 else
810                 {
811                     p_subtitle->psz_text[i_text] = *p_buffer_text;
812                     i_text++;
813                     p_buffer_text++;
814                 }
815             }
816             p_subtitle->psz_text[i_text] = '\0';
817             p_subtitle->i_start = i_start;
818             p_subtitle->i_stop = i_stop;
819             return( 0 );
820         }
821     }
822 }
823
824 static int  sub_SSA1Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
825 {
826     return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 8 ) );
827 }
828 static int  sub_SSA2_4Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
829 {
830     return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 9 ) );
831 }
832
833 static int  sub_Vplayer( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
834 {
835     /*
836      * each line:
837      *  h:m:s:Line1|Line2|Line3....
838      *  or
839      *  h:m:s Line1|Line2|Line3....
840      * where n1 and n2 are the video frame number...
841      *
842      */
843     char *p;
844     char buffer_text[MAX_LINE + 1];
845     mtime_t    i_start;
846     unsigned int i;
847
848     for( ;; )
849     {
850         int h, m, s;
851         char c;
852
853         if( ( p = text_get_line( txt ) ) == NULL )
854         {
855             return( VLC_EGENERIC );
856         }
857
858         i_start = 0;
859
860         memset( buffer_text, '\0', MAX_LINE );
861         if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
862         {
863             i_start = ( (mtime_t)h * 3600*1000 +
864                         (mtime_t)m * 60*1000 +
865                         (mtime_t)s * 1000 ) * 1000;
866             break;
867         }
868     }
869
870     /* replace | by \n */
871     for( i = 0; i < strlen( buffer_text ); i++ )
872     {
873         if( buffer_text[i] == '|' )
874         {
875             buffer_text[i] = '\n';
876         }
877     }
878     p_subtitle->i_start = i_start;
879
880     p_subtitle->i_stop  = 0;
881     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
882     return( 0 );
883 }
884
885 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
886 {
887     if( psz_start )
888     {
889         if( strstr( psz_start, psz_str ) )
890         {
891             char *s = strstr( psz_start, psz_str );
892
893             s += strlen( psz_str );
894
895             return( s );
896         }
897     }
898     for( ;; )
899     {
900         char *p;
901         if( ( p = text_get_line( txt ) ) == NULL )
902         {
903             return NULL;
904         }
905         if( strstr( p, psz_str ) )
906         {
907             char *s = strstr( p, psz_str );
908
909             s += strlen( psz_str );
910
911             return(  s);
912         }
913     }
914 }
915
916 static int  sub_Sami( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
917 {
918     char *p;
919     int i_start;
920
921     int  i_text;
922     char buffer_text[10*MAX_LINE + 1];
923 #define ADDC( c ) \
924     if( i_text < 10*MAX_LINE )      \
925     {                               \
926         buffer_text[i_text++] = c;  \
927         buffer_text[i_text] = '\0'; \
928     }
929
930     /* search "Start=" */
931     if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
932     {
933         return VLC_EGENERIC;
934     }
935
936     /* get start value */
937     i_start = strtol( p, &p, 0 );
938
939     /* search <P */
940     if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
941     {
942         return VLC_EGENERIC;
943     }
944     /* search > */
945     if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
946     {
947         return VLC_EGENERIC;
948     }
949
950     i_text = 0;
951     buffer_text[0] = '\0';
952     /* now get all txt until  a "Start=" line */
953     for( ;; )
954     {
955         if( *p )
956         {
957             if( *p == '<' )
958             {
959                 if( !strncmp( p, "<br", 3 ) || !strncmp( p, "<BR", 3 ) )
960                 {
961                     ADDC( '\n' );
962                 }
963                 else if( strstr( p, "Start=" ) )
964                 {
965                     text_previous_line( txt );
966                     break;
967                 }
968                 p = sub_SamiSearch( txt, p, ">" );
969             }
970             else if( !strncmp( p, "&nbsp;", 6 ) )
971             {
972                 ADDC( ' ' );
973                 p += 6;
974             }
975             else if( *p == '\t' )
976             {
977                 ADDC( ' ' );
978                 p++;
979             }
980             else
981             {
982                 ADDC( *p );
983                 p++;
984             }
985         }
986         else
987         {
988             p = text_get_line( txt );
989         }
990
991         if( p == NULL )
992         {
993             break;
994         }
995     }
996
997     p_subtitle->i_start = i_start * 1000;
998     p_subtitle->i_stop  = 0;
999     p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1000
1001     return( VLC_SUCCESS );
1002 #undef ADDC
1003 }
1004