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