]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
Revert the so-called whitelisting commits that are actually blacklisting
[vlc] / modules / demux / subtitle.c
1 /*****************************************************************************
2  * subtitle.c: Demux for subtitle text files.
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc_input.h>
30
31
32 #include <errno.h>
33 #ifdef HAVE_SYS_TYPES_H
34 #   include <sys/types.h>
35 #endif
36 #include <ctype.h>
37
38 #include <vlc_demux.h>
39 #include <vlc_charset.h>
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open ( vlc_object_t *p_this );
45 static void Close( vlc_object_t *p_this );
46
47 #define SUB_DELAY_LONGTEXT \
48     N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
49 #define SUB_FPS_LONGTEXT \
50     N_("Override the normal frames per second settings. " \
51     "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
52 #define SUB_TYPE_LONGTEXT \
53     N_("Force the subtiles format. Valid values are : \"microdvd\", " \
54     "\"subrip\",  \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
55     "\"sami\", \"dvdsubtitle\", \"mpl2\" and \"auto\" (meaning autodetection, this " \
56     "should always work).")
57 static const char *ppsz_sub_type[] =
58 {
59     "auto", "microdvd", "subrip", "subviewer", "ssa1",
60     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2"
61 };
62
63 vlc_module_begin();
64     set_shortname( _("Subtitles"));
65     set_description( _("Text subtitles parser") );
66     set_capability( "demux2", 0 );
67     set_category( CAT_INPUT );
68     set_subcategory( SUBCAT_INPUT_DEMUX );
69     add_float( "sub-fps", 0.0, NULL,
70                N_("Frames per second"),
71                SUB_FPS_LONGTEXT, VLC_TRUE );
72     add_integer( "sub-delay", 0, NULL,
73                N_("Subtitles delay"),
74                SUB_DELAY_LONGTEXT, VLC_TRUE );
75     add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
76                 SUB_TYPE_LONGTEXT, VLC_TRUE );
77         change_string_list( ppsz_sub_type, NULL, NULL );
78     set_callbacks( Open, Close );
79
80     add_shortcut( "subtitle" );
81 vlc_module_end();
82
83 /*****************************************************************************
84  * Prototypes:
85  *****************************************************************************/
86 enum
87 {
88     SUB_TYPE_UNKNOWN = -1,
89     SUB_TYPE_MICRODVD,
90     SUB_TYPE_SUBRIP,
91     SUB_TYPE_SSA1,
92     SUB_TYPE_SSA2_4,
93     SUB_TYPE_ASS,
94     SUB_TYPE_VPLAYER,
95     SUB_TYPE_SAMI,
96     SUB_TYPE_SUBVIEWER,
97     SUB_TYPE_DVDSUBTITLE,
98     SUB_TYPE_MPL2
99 };
100
101 typedef struct
102 {
103     int     i_line_count;
104     int     i_line;
105     char    **line;
106 } text_t;
107 static int  TextLoad( text_t *, stream_t *s );
108 static void TextUnload( text_t * );
109
110 typedef struct
111 {
112     int64_t i_start;
113     int64_t i_stop;
114
115     char    *psz_text;
116 } subtitle_t;
117
118
119 struct demux_sys_t
120 {
121     int         i_type;
122     text_t      txt;
123     es_out_id_t *es;
124
125     int64_t     i_next_demux_date;
126     int64_t     i_microsecperframe;
127
128     char        *psz_header;
129     int         i_subtitle;
130     int         i_subtitles;
131     subtitle_t  *subtitle;
132
133     int64_t     i_length;
134 };
135
136 static int  ParseMicroDvd   ( demux_t *, subtitle_t * );
137 static int  ParseSubRip     ( demux_t *, subtitle_t * );
138 static int  ParseSubViewer  ( demux_t *, subtitle_t * );
139 static int  ParseSSA        ( demux_t *, subtitle_t * );
140 static int  ParseVplayer    ( demux_t *, subtitle_t * );
141 static int  ParseSami       ( demux_t *, subtitle_t * );
142 static int  ParseDVDSubtitle( demux_t *, subtitle_t * );
143 static int  ParseMPL2       ( demux_t *, subtitle_t * );
144
145 static struct
146 {
147     const char *psz_type_name;
148     int  i_type;
149     const char *psz_name;
150     int  (*pf_read)( demux_t *, subtitle_t* );
151 } sub_read_subtitle_function [] =
152 {
153     { "microdvd",   SUB_TYPE_MICRODVD,    "MicroDVD",    ParseMicroDvd },
154     { "subrip",     SUB_TYPE_SUBRIP,      "SubRIP",      ParseSubRip },
155     { "subviewer",  SUB_TYPE_SUBVIEWER,   "SubViewer",   ParseSubViewer },
156     { "ssa1",       SUB_TYPE_SSA1,        "SSA-1",       ParseSSA },
157     { "ssa2-4",     SUB_TYPE_SSA2_4,      "SSA-2/3/4",   ParseSSA },
158     { "ass",        SUB_TYPE_ASS,         "SSA/ASS",     ParseSSA },
159     { "vplayer",    SUB_TYPE_VPLAYER,     "VPlayer",     ParseVplayer },
160     { "sami",       SUB_TYPE_SAMI,        "SAMI",        ParseSami },
161     { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
162     { "mpl2",       SUB_TYPE_MPL2,        "MPL2",        ParseMPL2 },
163     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
164 };
165
166 static int Demux( demux_t * );
167 static int Control( demux_t *, int, va_list );
168
169 /*static void Fix( demux_t * );*/
170
171 /*****************************************************************************
172  * Module initializer
173  *****************************************************************************/
174 static int Open ( vlc_object_t *p_this )
175 {
176     demux_t        *p_demux = (demux_t*)p_this;
177     demux_sys_t    *p_sys;
178     es_format_t    fmt;
179     float          f_fps;
180     char           *psz_type;
181     int  (*pf_read)( demux_t *, subtitle_t* );
182     int            i, i_max;
183
184     if( !p_demux->b_force )
185     {
186         msg_Dbg( p_demux, "subtitle demux discarded" );
187         return VLC_EGENERIC;
188     }
189
190     p_demux->pf_demux = Demux;
191     p_demux->pf_control = Control;
192     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
193     p_sys->psz_header         = NULL;
194     p_sys->i_subtitle         = 0;
195     p_sys->i_subtitles        = 0;
196     p_sys->subtitle           = NULL;
197     p_sys->i_microsecperframe = 40000;
198
199     /* Get the FPS */
200     f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
201     if( f_fps >= 1.0 )
202         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
203
204     msg_Dbg( p_demux, "Movie fps: %f", f_fps );
205
206     /* Check for override of the fps */
207     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
208     if( f_fps >= 1.0 )
209     {
210         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
211         msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
212     }
213
214     /* Get or probe the type */
215     p_sys->i_type = SUB_TYPE_UNKNOWN;
216     psz_type = var_CreateGetString( p_demux, "sub-type" );
217     if( *psz_type )
218     {
219         int i;
220
221         for( i = 0; ; i++ )
222         {
223             if( sub_read_subtitle_function[i].psz_type_name == NULL )
224                 break;
225
226             if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
227                          psz_type ) )
228             {
229                 p_sys->i_type = sub_read_subtitle_function[i].i_type;
230                 break;
231             }
232         }
233     }
234     free( psz_type );
235
236     /* Probe if unknown type */
237     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
238     {
239         int     i_try;
240         char    *s = NULL;
241
242         msg_Dbg( p_demux, "autodetecting subtitle format" );
243         for( i_try = 0; i_try < 256; i_try++ )
244         {
245             int i_dummy;
246
247             if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
248                 break;
249
250             if( strcasestr( s, "<SAMI>" ) )
251             {
252                 p_sys->i_type = SUB_TYPE_SAMI;
253                 break;
254             }
255             else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
256                      sscanf( s, "{%d}{}", &i_dummy ) == 1)
257             {
258                 p_sys->i_type = SUB_TYPE_MICRODVD;
259                 break;
260             }
261             else if( sscanf( s,
262                              "%d:%d:%d,%d --> %d:%d:%d,%d",
263                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
264                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
265             {
266                 p_sys->i_type = SUB_TYPE_SUBRIP;
267                 break;
268             }
269             else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
270             {
271                 p_sys->i_type = SUB_TYPE_SSA1;
272                 break;
273             }
274             else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
275             {
276                 p_sys->i_type = SUB_TYPE_ASS;
277                 break;
278             }
279             else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
280             {
281                 p_sys->i_type = SUB_TYPE_SSA2_4;
282                 break;
283             }
284             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
285             {
286                 p_sys->i_type = SUB_TYPE_SSA2_4;
287                 break;
288             }
289             else if( !strncasecmp( s, "Dialogue:", 9  ) )
290             {
291                 p_sys->i_type = SUB_TYPE_ASS;
292                 break;
293             }
294             else if( strcasestr( s, "[INFORMATION]" ) )
295             {
296                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
297                 break;
298             }
299             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
300                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
301             {
302                 p_sys->i_type = SUB_TYPE_VPLAYER;
303                 break;
304             }
305             else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
306                              &i_dummy, &i_dummy ) == 4 )
307             {
308                 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
309                 break;
310             }
311             else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
312                      sscanf( s, "[%d][]", &i_dummy ) == 1)
313             {
314                 p_sys->i_type = SUB_TYPE_MPL2;
315                 break;
316             }
317
318             free( s );
319             s = NULL;
320         }
321
322         if( s ) free( s );
323
324         /* It will nearly always work even for non seekable stream thanks the
325          * caching system, and if it fails we lose just a few sub */
326         if( stream_Seek( p_demux->s, 0 ) )
327         {
328             msg_Warn( p_demux, "failed to rewind" );
329         }
330     }
331     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
332     {
333         msg_Err( p_demux, "failed to recognize subtitle type" );
334         free( p_sys );
335         return VLC_EGENERIC;
336     }
337
338     for( i = 0; ; i++ )
339     {
340         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
341         {
342             msg_Dbg( p_demux, "detected %s format",
343                      sub_read_subtitle_function[i].psz_name );
344             pf_read = sub_read_subtitle_function[i].pf_read;
345             break;
346         }
347     }
348
349     msg_Dbg( p_demux, "loading all subtitles..." );
350
351     /* Load the whole file */
352     TextLoad( &p_sys->txt, p_demux->s );
353
354     /* Parse it */
355     for( i_max = 0;; )
356     {
357         if( p_sys->i_subtitles >= i_max )
358         {
359             i_max += 500;
360             if( !( p_sys->subtitle = realloc( p_sys->subtitle,
361                                               sizeof(subtitle_t) * i_max ) ) )
362             {
363                 msg_Err( p_demux, "out of memory");
364                 if( p_sys->subtitle != NULL )
365                     free( p_sys->subtitle );
366                 TextUnload( &p_sys->txt );
367                 free( p_sys );
368                 return VLC_ENOMEM;
369             }
370         }
371
372         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
373             break;
374
375         p_sys->i_subtitles++;
376     }
377     /* Unload */
378     TextUnload( &p_sys->txt );
379
380     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
381
382     /* Fix subtitle (order and time) *** */
383     p_sys->i_subtitle = 0;
384     p_sys->i_length = 0;
385     if( p_sys->i_subtitles > 0 )
386     {
387         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
388         /* +1 to avoid 0 */
389         if( p_sys->i_length <= 0 )
390             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
391     }
392
393     /* *** add subtitle ES *** */
394     if( p_sys->i_type == SUB_TYPE_SSA1 ||
395              p_sys->i_type == SUB_TYPE_SSA2_4 ||
396              p_sys->i_type == SUB_TYPE_ASS )
397     {
398         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
399     }
400     else
401     {
402         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
403     }
404     if( p_sys->psz_header != NULL )
405     {
406         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
407         fmt.p_extra = strdup( p_sys->psz_header );
408     }
409     p_sys->es = es_out_Add( p_demux->out, &fmt );
410
411     return VLC_SUCCESS;
412 }
413
414 /*****************************************************************************
415  * Close: Close subtitle demux
416  *****************************************************************************/
417 static void Close( vlc_object_t *p_this )
418 {
419     demux_t *p_demux = (demux_t*)p_this;
420     demux_sys_t *p_sys = p_demux->p_sys;
421     int i;
422
423     for( i = 0; i < p_sys->i_subtitles; i++ )
424     {
425         if( p_sys->subtitle[i].psz_text )
426             free( p_sys->subtitle[i].psz_text );
427     }
428     if( p_sys->subtitle )
429         free( p_sys->subtitle );
430
431     free( p_sys );
432 }
433
434 /*****************************************************************************
435  * Control:
436  *****************************************************************************/
437 static int Control( demux_t *p_demux, int i_query, va_list args )
438 {
439     demux_sys_t *p_sys = p_demux->p_sys;
440     int64_t *pi64, i64;
441     double *pf, f;
442
443     switch( i_query )
444     {
445         case DEMUX_GET_LENGTH:
446             pi64 = (int64_t*)va_arg( args, int64_t * );
447             *pi64 = p_sys->i_length;
448             return VLC_SUCCESS;
449
450         case DEMUX_GET_TIME:
451             pi64 = (int64_t*)va_arg( args, int64_t * );
452             if( p_sys->i_subtitle < p_sys->i_subtitles )
453             {
454                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
455                 return VLC_SUCCESS;
456             }
457             return VLC_EGENERIC;
458
459         case DEMUX_SET_TIME:
460             i64 = (int64_t)va_arg( args, int64_t );
461             p_sys->i_subtitle = 0;
462             while( p_sys->i_subtitle < p_sys->i_subtitles &&
463                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
464             {
465                 p_sys->i_subtitle++;
466             }
467
468             if( p_sys->i_subtitle >= p_sys->i_subtitles )
469                 return VLC_EGENERIC;
470             return VLC_SUCCESS;
471
472         case DEMUX_GET_POSITION:
473             pf = (double*)va_arg( args, double * );
474             if( p_sys->i_subtitle >= p_sys->i_subtitles )
475             {
476                 *pf = 1.0;
477             }
478             else if( p_sys->i_subtitles > 0 )
479             {
480                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
481                       (double)p_sys->i_length;
482             }
483             else
484             {
485                 *pf = 0.0;
486             }
487             return VLC_SUCCESS;
488
489         case DEMUX_SET_POSITION:
490             f = (double)va_arg( args, double );
491             i64 = f * p_sys->i_length;
492
493             p_sys->i_subtitle = 0;
494             while( p_sys->i_subtitle < p_sys->i_subtitles &&
495                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
496             {
497                 p_sys->i_subtitle++;
498             }
499             if( p_sys->i_subtitle >= p_sys->i_subtitles )
500                 return VLC_EGENERIC;
501             return VLC_SUCCESS;
502
503         case DEMUX_SET_NEXT_DEMUX_TIME:
504             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
505             return VLC_SUCCESS;
506
507         case DEMUX_GET_FPS:
508         case DEMUX_GET_META:
509         case DEMUX_GET_ATTACHMENTS:
510         case DEMUX_GET_TITLE_INFO:
511         case DEMUX_HAS_UNSUPPORTED_META:
512             return VLC_EGENERIC;
513
514         default:
515             msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
516             return VLC_EGENERIC;
517     }
518 }
519
520 /*****************************************************************************
521  * Demux: Send subtitle to decoder
522  *****************************************************************************/
523 static int Demux( demux_t *p_demux )
524 {
525     demux_sys_t *p_sys = p_demux->p_sys;
526     int64_t i_maxdate;
527
528     if( p_sys->i_subtitle >= p_sys->i_subtitles )
529         return 0;
530
531     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
532     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
533     {
534         /* Should not happen */
535         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
536     }
537
538     while( p_sys->i_subtitle < p_sys->i_subtitles &&
539            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
540     {
541         block_t *p_block;
542         int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
543
544         if( i_len <= 1 )
545         {
546             /* empty subtitle */
547             p_sys->i_subtitle++;
548             continue;
549         }
550
551         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
552         {
553             p_sys->i_subtitle++;
554             continue;
555         }
556
557         if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
558         {
559             p_sys->i_subtitle++;
560             continue;
561         }
562
563         p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
564         p_block->i_dts = p_block->i_pts;
565         if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
566         {
567             p_block->i_length =
568                 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
569         }
570
571         memcpy( p_block->p_buffer,
572                 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
573         if( p_block->i_pts > 0 )
574         {
575             es_out_Send( p_demux->out, p_sys->es, p_block );
576         }
577         else
578         {
579             block_Release( p_block );
580         }
581         p_sys->i_subtitle++;
582     }
583
584     /* */
585     p_sys->i_next_demux_date = 0;
586
587     return 1;
588 }
589
590 /*****************************************************************************
591  * Fix: fix time stamp and order of subtitle
592  *****************************************************************************/
593 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
594 static void Fix( demux_t *p_demux )
595 {
596     demux_sys_t *p_sys = p_demux->p_sys;
597     vlc_bool_t b_done;
598     int     i_index;
599
600     /* *** fix order (to be sure...) *** */
601     /* We suppose that there are near in order and this durty bubble sort
602      * wont take too much time
603      */
604     do
605     {
606         b_done = VLC_TRUE;
607         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
608         {
609             if( p_sys->subtitle[i_index].i_start <
610                     p_sys->subtitle[i_index - 1].i_start )
611             {
612                 subtitle_t sub_xch;
613                 memcpy( &sub_xch,
614                         p_sys->subtitle + i_index - 1,
615                         sizeof( subtitle_t ) );
616                 memcpy( p_sys->subtitle + i_index - 1,
617                         p_sys->subtitle + i_index,
618                         sizeof( subtitle_t ) );
619                 memcpy( p_sys->subtitle + i_index,
620                         &sub_xch,
621                         sizeof( subtitle_t ) );
622                 b_done = VLC_FALSE;
623             }
624         }
625     } while( !b_done );
626 }
627 #endif
628
629 static int TextLoad( text_t *txt, stream_t *s )
630 {
631     int   i_line_max;
632
633     /* init txt */
634     i_line_max          = 500;
635     txt->i_line_count   = 0;
636     txt->i_line         = 0;
637     txt->line           = calloc( i_line_max, sizeof( char * ) );
638
639     /* load the complete file */
640     for( ;; )
641     {
642         char *psz = stream_ReadLine( s );
643
644         if( psz == NULL )
645             break;
646
647         txt->line[txt->i_line_count++] = psz;
648         if( txt->i_line_count >= i_line_max )
649         {
650             i_line_max += 100;
651             txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
652         }
653     }
654
655     if( txt->i_line_count <= 0 )
656     {
657         free( txt->line );
658         return VLC_EGENERIC;
659     }
660
661     return VLC_SUCCESS;
662 }
663 static void TextUnload( text_t *txt )
664 {
665     int i;
666
667     for( i = 0; i < txt->i_line_count; i++ )
668     {
669         free( txt->line[i] );
670     }
671     free( txt->line );
672     txt->i_line       = 0;
673     txt->i_line_count = 0;
674 }
675
676 static char *TextGetLine( text_t *txt )
677 {
678     if( txt->i_line >= txt->i_line_count )
679         return( NULL );
680
681     return txt->line[txt->i_line++];
682 }
683 static void TextPreviousLine( text_t *txt )
684 {
685     if( txt->i_line > 0 )
686         txt->i_line--;
687 }
688
689 /*****************************************************************************
690  * Specific Subtitle function
691  *****************************************************************************/
692 /* ParseMicroDvd:
693  *  Format:
694  *      {n1}{n2}Line1|Line2|Line3....
695  *  where n1 and n2 are the video frame number (n2 can be empty)
696  */
697 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
698 {
699     demux_sys_t *p_sys = p_demux->p_sys;
700     text_t      *txt = &p_sys->txt;
701     char *psz_text;
702     int  i_start;
703     int  i_stop;
704     int  i;
705
706     for( ;; )
707     {
708         const char *s = TextGetLine( txt );
709         if( !s )
710             return VLC_EGENERIC;
711
712         psz_text = malloc( strlen(s) + 1 );
713         if( !psz_text )
714             return VLC_ENOMEM;
715
716         i_start = 0;
717         i_stop  = 0;
718         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
719             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
720         {
721             float f_fps;
722             if( i_start != 1 || i_stop != 1 )
723                 break;
724
725             /* We found a possible setting of the framerate "{1}{1}23.976" */
726             /* Check if it's usable, and if the sub-fps is not set */
727             f_fps = us_strtod( psz_text, NULL );
728             if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
729                 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
730         }
731         free( psz_text );
732     }
733
734     /* replace | by \n */
735     for( i = 0; psz_text[i] != '\0'; i++ )
736     {
737         if( psz_text[i] == '|' )
738             psz_text[i] = '\n';
739     }
740
741     /* */
742     p_subtitle->i_start  = i_start * p_sys->i_microsecperframe;
743     p_subtitle->i_stop   = i_stop  * p_sys->i_microsecperframe;
744     p_subtitle->psz_text = psz_text;
745     return VLC_SUCCESS;
746 }
747
748 /* ParseSubRipSubViewer
749  *  Format SubRip
750  *      n
751  *      h1:m1:s1,d1 --> h2:m2:s2,d2
752  *      Line1
753  *      Line2
754  *      ....
755  *      [Empty line]
756  *  Format SubViewer v1/v2
757  *      h1:m1:s1.d1,h2:m2:s2.d2
758  *      Line1[br]Line2
759  *      Line3
760  *      ...
761  *      [empty line]
762  *  We ignore line number for SubRip
763  */
764 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
765                                  const char *psz_fmt,
766                                  vlc_bool_t b_replace_br )
767 {
768     demux_sys_t *p_sys = p_demux->p_sys;
769     text_t      *txt = &p_sys->txt;
770     char    *psz_text;
771
772     for( ;; )
773     {
774         const char *s = TextGetLine( txt );
775         int h1, m1, s1, d1, h2, m2, s2, d2;
776
777         if( !s )
778             return VLC_EGENERIC;
779
780         if( sscanf( s, psz_fmt,
781                     &h1, &m1, &s1, &d1,
782                     &h2, &m2, &s2, &d2 ) == 8 )
783         {
784             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
785                                     (int64_t)m1 * 60*1000 +
786                                     (int64_t)s1 * 1000 +
787                                     (int64_t)d1 ) * 1000;
788
789             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
790                                     (int64_t)m2 * 60*1000 +
791                                     (int64_t)s2 * 1000 +
792                                     (int64_t)d2 ) * 1000;
793             break;
794         }
795     }
796
797     /* Now read text until an empty line */
798     psz_text = strdup("");
799     if( !psz_text )
800         return VLC_ENOMEM;
801     for( ;; )
802     {
803         const char *s = TextGetLine( txt );
804         int i_len;
805         int i_old;
806
807         if( !s )
808         {
809             free( psz_text );
810             return VLC_EGENERIC;
811         }
812
813         i_len = strlen( s );
814         if( i_len <= 0 )
815         {
816             p_subtitle->psz_text = psz_text;
817             return VLC_SUCCESS;
818         }
819
820         i_old = strlen( psz_text );
821         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
822         if( !psz_text )
823             return VLC_ENOMEM;
824         strcat( psz_text, s );
825         strcat( psz_text, "\n" );
826
827         /* replace [br] by \n */
828         if( b_replace_br )
829         {
830             char *p;
831  
832             while( ( p = strstr( psz_text, "[br]" ) ) )
833             {
834                 *p++ = '\n';
835                 memmove( p, &p[3], strlen(&p[3])+1 );
836             }
837         }
838     }
839 }
840 /* ParseSubRip
841  */
842 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
843 {
844     return ParseSubRipSubViewer( p_demux, p_subtitle,
845                                  "%d:%d:%d,%d --> %d:%d:%d,%d",
846                                  VLC_FALSE );
847 }
848 /* ParseSubViewer
849  */
850 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
851 {
852     return ParseSubRipSubViewer( p_demux, p_subtitle,
853                                  "%d:%d:%d.%d,%d:%d:%d.%d",
854                                  VLC_TRUE );
855 }
856
857 /* ParseSSA
858  */
859 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
860 {
861     demux_sys_t *p_sys = p_demux->p_sys;
862     text_t      *txt = &p_sys->txt;
863
864     for( ;; )
865     {
866         const char *s = TextGetLine( txt );
867         int h1, m1, s1, c1, h2, m2, s2, c2;
868         char *psz_text;
869
870         if( !s )
871             return VLC_EGENERIC;
872
873         /* We expect (SSA2-4):
874          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
875          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
876          *
877          * SSA-1 is similar but only has 8 commas up untill the subtitle text. Probably the Effect field is no present, but not 100 % sure.
878          */
879
880         /* For ASS:
881          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
882          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
883          */
884         psz_text = malloc( 2 + strlen( s ) + 1 );
885         if( !psz_text )
886             return VLC_ENOMEM;
887
888         if( sscanf( s,
889                     "Dialogue: %*[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
890                     &h1, &m1, &s1, &c1,
891                     &h2, &m2, &s2, &c2,
892                     psz_text ) == 9 )
893         {
894             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
895             /* (Layer comes from ASS specs ... it's empty for SSA.) */
896             if( p_sys->i_type == SUB_TYPE_SSA1 )
897             {
898                 /* SSA1 has only 8 commas before the text starts, not 9 */
899                 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
900                 psz_text[0] = ',';
901             }
902             else
903             {
904                 /* ReadOrder, Layer, %s(rest of fields) */
905                 memmove( &psz_text[2], psz_text, strlen(psz_text)+1 );
906                 psz_text[0] = ',';
907                 psz_text[1] = ',';
908             }
909
910             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
911                                     (int64_t)m1 * 60*1000 +
912                                     (int64_t)s1 * 1000 +
913                                     (int64_t)c1 * 10 ) * 1000;
914             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
915                                     (int64_t)m2 * 60*1000 +
916                                     (int64_t)s2 * 1000 +
917                                     (int64_t)c2 * 10 ) * 1000;
918             p_subtitle->psz_text = psz_text;
919             return VLC_SUCCESS;
920         }
921         free( psz_text );
922
923         /* All the other stuff we add to the header field */
924         if( !p_sys->psz_header )
925             p_sys->psz_header = strdup( "" );
926         if( !p_sys->psz_header )
927             return VLC_ENOMEM;
928
929         p_sys->psz_header =
930             realloc( p_sys->psz_header,
931                      strlen( p_sys->psz_header ) + strlen( s ) + 2 );
932         strcat( p_sys->psz_header,  s );
933         strcat( p_sys->psz_header, "\n" );
934     }
935 }
936
937 /* ParseVplayer
938  *  Format
939  *      h:m:s:Line1|Line2|Line3....
940  *  or
941  *      h:m:s Line1|Line2|Line3....
942  */
943 static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
944 {
945     demux_sys_t *p_sys = p_demux->p_sys;
946     text_t      *txt = &p_sys->txt;
947     char *psz_text;
948     int i;
949
950     for( ;; )
951     {
952         const char *s = TextGetLine( txt );
953         int h1, m1, s1;
954
955         if( !s )
956             return VLC_EGENERIC;
957
958         psz_text = malloc( strlen( s ) + 1 );
959         if( !psz_text )
960             return VLC_ENOMEM;
961
962         if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
963                     &h1, &m1, &s1, psz_text ) == 4 )
964         {
965             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
966                                     (int64_t)m1 * 60*1000 +
967                                     (int64_t)s1 * 1000 ) * 1000;
968             p_subtitle->i_stop  = 0;
969             break;
970         }
971         free( psz_text );
972     }
973
974     /* replace | by \n */
975     for( i = 0; psz_text[i] != '\0'; i++ )
976     {
977         if( psz_text[i] == '|' )
978             psz_text[i] = '\n';
979     }
980     p_subtitle->psz_text = psz_text;
981     return VLC_SUCCESS;
982 }
983
984 /* ParseSami
985  */
986 static char *ParseSamiSearch( text_t *txt,
987                               char *psz_start, const char *psz_str )
988 {
989     if( psz_start && strcasestr( psz_start, psz_str ) )
990     {
991         char *s = strcasestr( psz_start, psz_str );
992         return &s[strlen( psz_str )];
993     }
994
995     for( ;; )
996     {
997         char *p = TextGetLine( txt );
998         if( !p )
999             return NULL;
1000
1001         if( strcasestr( p, psz_str ) )
1002         {
1003             char *s = strcasestr( p, psz_str );
1004             return &s[strlen( psz_str )];
1005         }
1006     }
1007 }
1008 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1009 {
1010     demux_sys_t *p_sys = p_demux->p_sys;
1011     text_t      *txt = &p_sys->txt;
1012
1013     char *s;
1014     int64_t i_start;
1015
1016     unsigned int i_text;
1017     char text[8192]; /* Arbitrary but should be long enough */
1018
1019     /* search "Start=" */
1020     if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1021         return VLC_EGENERIC;
1022
1023     /* get start value */
1024     i_start = strtol( s, &s, 0 );
1025
1026     /* search <P */
1027     if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1028         return VLC_EGENERIC;
1029
1030     /* search > */
1031     if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1032         return VLC_EGENERIC;
1033
1034     i_text = 0;
1035     text[0] = '\0';
1036     /* now get all txt until  a "Start=" line */
1037     for( ;; )
1038     {
1039         char c = '\0';
1040         /* Search non empty line */
1041         while( s && *s == '\0' )
1042             s = TextGetLine( txt );
1043         if( !s )
1044             break;
1045
1046         if( *s == '<' )
1047         {
1048             if( !strncasecmp( s, "<br", 3 ) )
1049             {
1050                 c = '\n';
1051             }
1052             else if( strcasestr( s, "Start=" ) )
1053             {
1054                 TextPreviousLine( txt );
1055                 break;
1056             }
1057             s = ParseSamiSearch( txt, s, ">" );
1058         }
1059         else if( !strncmp( s, "&nbsp;", 6 ) )
1060         {
1061             c = ' ';
1062             s += 6;
1063         }
1064         else if( *s == '\t' )
1065         {
1066             c = ' ';
1067             s++;
1068         }
1069         else
1070         {
1071             c = *s;
1072             s++;
1073         }
1074         if( c != '\0' && i_text+1 < sizeof(text) )
1075         {
1076             text[i_text++] = c;
1077             text[i_text] = '\0';
1078         }
1079     }
1080
1081     p_subtitle->i_start = i_start * 1000;
1082     p_subtitle->i_stop  = 0;
1083     p_subtitle->psz_text = strdup( text );
1084
1085     return VLC_SUCCESS;
1086 }
1087
1088 /* ParseDVDSubtitle
1089  *  Format
1090  *      {T h1:m1:s1:c1
1091  *      Line1
1092  *      Line2
1093  *      ...
1094  *      }
1095  * TODO it can have a header
1096  *      { HEAD
1097  *          ...
1098  *          CODEPAGE=...
1099  *          FORMAT=...
1100  *          LANG=English
1101  *      }
1102  *      LANG support would be cool
1103  *      CODEPAGE is probably mandatory FIXME
1104  */
1105 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1106 {
1107     demux_sys_t *p_sys = p_demux->p_sys;
1108     text_t      *txt = &p_sys->txt;
1109     char *psz_text;
1110
1111     for( ;; )
1112     {
1113         const char *s = TextGetLine( txt );
1114         int h1, m1, s1, c1;
1115
1116         if( !s )
1117             return VLC_EGENERIC;
1118
1119         if( sscanf( s,
1120                     "{T %d:%d:%d:%d",
1121                     &h1, &m1, &s1, &c1 ) == 4 )
1122         {
1123             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1124                                     (int64_t)m1 * 60*1000 +
1125                                     (int64_t)s1 * 1000 +
1126                                     (int64_t)c1 * 10) * 1000;
1127             p_subtitle->i_stop = 0;
1128             break;
1129         }
1130     }
1131
1132     /* Now read text until a line containing "}" */
1133     psz_text = strdup("");
1134     if( !psz_text )
1135         return VLC_ENOMEM;
1136     for( ;; )
1137     {
1138         const char *s = TextGetLine( txt );
1139         int i_len;
1140         int i_old;
1141
1142         if( !s )
1143         {
1144             free( psz_text );
1145             return VLC_EGENERIC;
1146         }
1147
1148         i_len = strlen( s );
1149         if( i_len == 1 && s[0] == '}')
1150         {
1151             p_subtitle->psz_text = psz_text;
1152             return VLC_SUCCESS;
1153         }
1154
1155         i_old = strlen( psz_text );
1156         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1157         if( !psz_text )
1158             return VLC_ENOMEM;
1159         strcat( psz_text, s );
1160         strcat( psz_text, "\n" );
1161     }
1162 }
1163
1164 /* ParseMPL2
1165  *  Format
1166  *     [n1][n2]Line1|Line2|Line3...
1167  *  where n1 and n2 are the video frame number (n2 can be empty)
1168  */
1169 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1170 {
1171     demux_sys_t *p_sys = p_demux->p_sys;
1172     text_t      *txt = &p_sys->txt;
1173     char *psz_text;
1174     int i;
1175
1176     for( ;; )
1177     {
1178         const char *s = TextGetLine( txt );
1179         int i_start;
1180         int i_stop;
1181
1182         if( !s )
1183             return VLC_EGENERIC;
1184
1185         psz_text = malloc( strlen(s) + 1 );
1186         if( !psz_text )
1187             return VLC_ENOMEM;
1188
1189         i_start = 0;
1190         i_stop  = 0;
1191         if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1192             sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1193         {
1194             p_subtitle->i_start = (int64_t)i_start * 100000;
1195             p_subtitle->i_stop  = (int64_t)i_stop  * 100000;
1196             break;
1197         }
1198         free( psz_text );
1199     }
1200
1201     /* replace | by \n */
1202     for( i = 0; psz_text[i] != '\0'; )
1203     {
1204         if( psz_text[i] == '|' )
1205             psz_text[i] = '\n';
1206
1207         /* Remove italic */
1208         if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1209             memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1210         else
1211             i++;
1212     }
1213     p_subtitle->psz_text = psz_text;
1214     return VLC_SUCCESS;
1215 }
1216