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