]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
Trigger intf_UserLoginPassword() when authorization of rtsp link failed while using...
[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             return VLC_EGENERIC;
512
513         default:
514             msg_Err( p_demux, "unknown query in subtitle control" );
515             return VLC_EGENERIC;
516     }
517 }
518
519 /*****************************************************************************
520  * Demux: Send subtitle to decoder
521  *****************************************************************************/
522 static int Demux( demux_t *p_demux )
523 {
524     demux_sys_t *p_sys = p_demux->p_sys;
525     int64_t i_maxdate;
526
527     if( p_sys->i_subtitle >= p_sys->i_subtitles )
528         return 0;
529
530     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
531     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
532     {
533         /* Should not happen */
534         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
535     }
536
537     while( p_sys->i_subtitle < p_sys->i_subtitles &&
538            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
539     {
540         block_t *p_block;
541         int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
542
543         if( i_len <= 1 )
544         {
545             /* empty subtitle */
546             p_sys->i_subtitle++;
547             continue;
548         }
549
550         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
551         {
552             p_sys->i_subtitle++;
553             continue;
554         }
555
556         if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
557         {
558             p_sys->i_subtitle++;
559             continue;
560         }
561
562         p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
563         p_block->i_dts = p_block->i_pts;
564         if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
565         {
566             p_block->i_length =
567                 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
568         }
569
570         memcpy( p_block->p_buffer,
571                 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
572         if( p_block->i_pts > 0 )
573         {
574             es_out_Send( p_demux->out, p_sys->es, p_block );
575         }
576         else
577         {
578             block_Release( p_block );
579         }
580         p_sys->i_subtitle++;
581     }
582
583     /* */
584     p_sys->i_next_demux_date = 0;
585
586     return 1;
587 }
588
589 /*****************************************************************************
590  * Fix: fix time stamp and order of subtitle
591  *****************************************************************************/
592 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
593 static void Fix( demux_t *p_demux )
594 {
595     demux_sys_t *p_sys = p_demux->p_sys;
596     vlc_bool_t b_done;
597     int     i_index;
598
599     /* *** fix order (to be sure...) *** */
600     /* We suppose that there are near in order and this durty bubble sort
601      * wont take too much time
602      */
603     do
604     {
605         b_done = VLC_TRUE;
606         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
607         {
608             if( p_sys->subtitle[i_index].i_start <
609                     p_sys->subtitle[i_index - 1].i_start )
610             {
611                 subtitle_t sub_xch;
612                 memcpy( &sub_xch,
613                         p_sys->subtitle + i_index - 1,
614                         sizeof( subtitle_t ) );
615                 memcpy( p_sys->subtitle + i_index - 1,
616                         p_sys->subtitle + i_index,
617                         sizeof( subtitle_t ) );
618                 memcpy( p_sys->subtitle + i_index,
619                         &sub_xch,
620                         sizeof( subtitle_t ) );
621                 b_done = VLC_FALSE;
622             }
623         }
624     } while( !b_done );
625 }
626 #endif
627
628 static int TextLoad( text_t *txt, stream_t *s )
629 {
630     int   i_line_max;
631
632     /* init txt */
633     i_line_max          = 500;
634     txt->i_line_count   = 0;
635     txt->i_line         = 0;
636     txt->line           = calloc( i_line_max, sizeof( char * ) );
637
638     /* load the complete file */
639     for( ;; )
640     {
641         char *psz = stream_ReadLine( s );
642
643         if( psz == NULL )
644             break;
645
646         txt->line[txt->i_line_count++] = psz;
647         if( txt->i_line_count >= i_line_max )
648         {
649             i_line_max += 100;
650             txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
651         }
652     }
653
654     if( txt->i_line_count <= 0 )
655     {
656         free( txt->line );
657         return VLC_EGENERIC;
658     }
659
660     return VLC_SUCCESS;
661 }
662 static void TextUnload( text_t *txt )
663 {
664     int i;
665
666     for( i = 0; i < txt->i_line_count; i++ )
667     {
668         free( txt->line[i] );
669     }
670     free( txt->line );
671     txt->i_line       = 0;
672     txt->i_line_count = 0;
673 }
674
675 static char *TextGetLine( text_t *txt )
676 {
677     if( txt->i_line >= txt->i_line_count )
678         return( NULL );
679
680     return txt->line[txt->i_line++];
681 }
682 static void TextPreviousLine( text_t *txt )
683 {
684     if( txt->i_line > 0 )
685         txt->i_line--;
686 }
687
688 /*****************************************************************************
689  * Specific Subtitle function
690  *****************************************************************************/
691 /* ParseMicroDvd:
692  *  Format:
693  *      {n1}{n2}Line1|Line2|Line3....
694  *  where n1 and n2 are the video frame number (n2 can be empty)
695  */
696 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
697 {
698     demux_sys_t *p_sys = p_demux->p_sys;
699     text_t      *txt = &p_sys->txt;
700     char *psz_text;
701     int  i_start;
702     int  i_stop;
703     int  i;
704
705     for( ;; )
706     {
707         const char *s = TextGetLine( txt );
708         if( !s )
709             return VLC_EGENERIC;
710
711         psz_text = malloc( strlen(s) + 1 );
712         if( !psz_text )
713             return VLC_ENOMEM;
714
715         i_start = 0;
716         i_stop  = 0;
717         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
718             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
719         {
720             float f_fps;
721             if( i_start != 1 || i_stop != 1 )
722                 break;
723
724             /* We found a possible setting of the framerate "{1}{1}23.976" */
725             /* Check if it's usable, and if the sub-fps is not set */
726             f_fps = us_strtod( psz_text, NULL );
727             if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
728                 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
729         }
730         free( psz_text );
731     }
732
733     /* replace | by \n */
734     for( i = 0; psz_text[i] != '\0'; i++ )
735     {
736         if( psz_text[i] == '|' )
737             psz_text[i] = '\n';
738     }
739
740     /* */
741     p_subtitle->i_start  = i_start * p_sys->i_microsecperframe;
742     p_subtitle->i_stop   = i_stop  * p_sys->i_microsecperframe;
743     p_subtitle->psz_text = psz_text;
744     return VLC_SUCCESS;
745 }
746
747 /* ParseSubRipSubViewer
748  *  Format SubRip
749  *      n
750  *      h1:m1:s1,d1 --> h2:m2:s2,d2
751  *      Line1
752  *      Line2
753  *      ....
754  *      [Empty line]
755  *  Format SubViewer v1/v2
756  *      h1:m1:s1.d1,h2:m2:s2.d2
757  *      Line1[br]Line2
758  *      Line3
759  *      ...
760  *      [empty line]
761  *  We ignore line number for SubRip
762  */
763 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
764                                  const char *psz_fmt,
765                                  vlc_bool_t b_replace_br )
766 {
767     demux_sys_t *p_sys = p_demux->p_sys;
768     text_t      *txt = &p_sys->txt;
769     char    *psz_text;
770
771     for( ;; )
772     {
773         const char *s = TextGetLine( txt );
774         int h1, m1, s1, d1, h2, m2, s2, d2;
775
776         if( !s )
777             return VLC_EGENERIC;
778
779         if( sscanf( s, psz_fmt,
780                     &h1, &m1, &s1, &d1,
781                     &h2, &m2, &s2, &d2 ) == 8 )
782         {
783             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
784                                     (int64_t)m1 * 60*1000 +
785                                     (int64_t)s1 * 1000 +
786                                     (int64_t)d1 ) * 1000;
787
788             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
789                                     (int64_t)m2 * 60*1000 +
790                                     (int64_t)s2 * 1000 +
791                                     (int64_t)d2 ) * 1000;
792             break;
793         }
794     }
795
796     /* Now read text until an empty line */
797     psz_text = strdup("");
798     if( !psz_text )
799         return VLC_ENOMEM;
800     for( ;; )
801     {
802         const char *s = TextGetLine( txt );
803         int i_len;
804         int i_old;
805
806         if( !s )
807         {
808             free( psz_text );
809             return VLC_EGENERIC;
810         }
811
812         i_len = strlen( s );
813         if( i_len <= 0 )
814         {
815             p_subtitle->psz_text = psz_text;
816             return VLC_SUCCESS;
817         }
818
819         i_old = strlen( psz_text );
820         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
821         if( !psz_text )
822             return VLC_ENOMEM;
823         strcat( psz_text, s );
824         strcat( psz_text, "\n" );
825
826         /* replace [br] by \n */
827         if( b_replace_br )
828         {
829             char *p;
830  
831             while( ( p = strstr( psz_text, "[br]" ) ) )
832             {
833                 *p++ = '\n';
834                 memmove( p, &p[3], strlen(&p[3])+1 );
835             }
836         }
837     }
838 }
839 /* ParseSubRip
840  */
841 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
842 {
843     return ParseSubRipSubViewer( p_demux, p_subtitle,
844                                  "%d:%d:%d,%d --> %d:%d:%d,%d",
845                                  VLC_FALSE );
846 }
847 /* ParseSubViewer
848  */
849 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
850 {
851     return ParseSubRipSubViewer( p_demux, p_subtitle,
852                                  "%d:%d:%d.%d,%d:%d:%d.%d",
853                                  VLC_TRUE );
854 }
855
856 /* ParseSSA
857  */
858 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
859 {
860     demux_sys_t *p_sys = p_demux->p_sys;
861     text_t      *txt = &p_sys->txt;
862
863     for( ;; )
864     {
865         const char *s = TextGetLine( txt );
866         int h1, m1, s1, c1, h2, m2, s2, c2;
867         char *psz_text;
868
869         if( !s )
870             return VLC_EGENERIC;
871
872         /* We expect (SSA2-4):
873          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
874          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
875          *
876          * 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.
877          */
878
879         /* For ASS:
880          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
881          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
882          */
883         psz_text = malloc( 2 + strlen( s ) + 1 );
884         if( !psz_text )
885             return VLC_ENOMEM;
886
887         if( sscanf( s,
888                     "Dialogue: %*[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
889                     &h1, &m1, &s1, &c1,
890                     &h2, &m2, &s2, &c2,
891                     psz_text ) == 9 )
892         {
893             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
894             /* (Layer comes from ASS specs ... it's empty for SSA.) */
895             if( p_sys->i_type == SUB_TYPE_SSA1 )
896             {
897                 /* SSA1 has only 8 commas before the text starts, not 9 */
898                 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
899                 psz_text[0] = ',';
900             }
901             else
902             {
903                 /* ReadOrder, Layer, %s(rest of fields) */
904                 memmove( &psz_text[2], psz_text, strlen(psz_text)+1 );
905                 psz_text[0] = ',';
906                 psz_text[1] = ',';
907             }
908
909             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
910                                     (int64_t)m1 * 60*1000 +
911                                     (int64_t)s1 * 1000 +
912                                     (int64_t)c1 * 10 ) * 1000;
913             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
914                                     (int64_t)m2 * 60*1000 +
915                                     (int64_t)s2 * 1000 +
916                                     (int64_t)c2 * 10 ) * 1000;
917             p_subtitle->psz_text = psz_text;
918             return VLC_SUCCESS;
919         }
920         free( psz_text );
921
922         /* All the other stuff we add to the header field */
923         if( !p_sys->psz_header )
924             p_sys->psz_header = strdup( "" );
925         if( !p_sys->psz_header )
926             return VLC_ENOMEM;
927
928         p_sys->psz_header =
929             realloc( p_sys->psz_header,
930                      strlen( p_sys->psz_header ) + strlen( s ) + 2 );
931         strcat( p_sys->psz_header,  s );
932         strcat( p_sys->psz_header, "\n" );
933     }
934 }
935
936 /* ParseVplayer
937  *  Format
938  *      h:m:s:Line1|Line2|Line3....
939  *  or
940  *      h:m:s Line1|Line2|Line3....
941  */
942 static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
943 {
944     demux_sys_t *p_sys = p_demux->p_sys;
945     text_t      *txt = &p_sys->txt;
946     char *psz_text;
947     int i;
948
949     for( ;; )
950     {
951         const char *s = TextGetLine( txt );
952         int h1, m1, s1;
953
954         if( !s )
955             return VLC_EGENERIC;
956
957         psz_text = malloc( strlen( s ) + 1 );
958         if( !psz_text )
959             return VLC_ENOMEM;
960
961         if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
962                     &h1, &m1, &s1, psz_text ) == 4 )
963         {
964             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
965                                     (int64_t)m1 * 60*1000 +
966                                     (int64_t)s1 * 1000 ) * 1000;
967             p_subtitle->i_stop  = 0;
968             break;
969         }
970         free( psz_text );
971     }
972
973     /* replace | by \n */
974     for( i = 0; psz_text[i] != '\0'; i++ )
975     {
976         if( psz_text[i] == '|' )
977             psz_text[i] = '\n';
978     }
979     p_subtitle->psz_text = psz_text;
980     return VLC_SUCCESS;
981 }
982
983 /* ParseSami
984  */
985 static char *ParseSamiSearch( text_t *txt,
986                               char *psz_start, const char *psz_str )
987 {
988     if( psz_start && strcasestr( psz_start, psz_str ) )
989     {
990         char *s = strcasestr( psz_start, psz_str );
991         return &s[strlen( psz_str )];
992     }
993
994     for( ;; )
995     {
996         char *p = TextGetLine( txt );
997         if( !p )
998             return NULL;
999
1000         if( strcasestr( p, psz_str ) )
1001         {
1002             char *s = strcasestr( p, psz_str );
1003             return &s[strlen( psz_str )];
1004         }
1005     }
1006 }
1007 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1008 {
1009     demux_sys_t *p_sys = p_demux->p_sys;
1010     text_t      *txt = &p_sys->txt;
1011
1012     char *s;
1013     int64_t i_start;
1014
1015     unsigned int i_text;
1016     char text[8192]; /* Arbitrary but should be long enough */
1017
1018     /* search "Start=" */
1019     if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1020         return VLC_EGENERIC;
1021
1022     /* get start value */
1023     i_start = strtol( s, &s, 0 );
1024
1025     /* search <P */
1026     if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1027         return VLC_EGENERIC;
1028
1029     /* search > */
1030     if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1031         return VLC_EGENERIC;
1032
1033     i_text = 0;
1034     text[0] = '\0';
1035     /* now get all txt until  a "Start=" line */
1036     for( ;; )
1037     {
1038         char c = '\0';
1039         /* Search non empty line */
1040         while( s && *s == '\0' )
1041             s = TextGetLine( txt );
1042         if( !s )
1043             break;
1044
1045         if( *s == '<' )
1046         {
1047             if( !strncasecmp( s, "<br", 3 ) )
1048             {
1049                 c = '\n';
1050             }
1051             else if( strcasestr( s, "Start=" ) )
1052             {
1053                 TextPreviousLine( txt );
1054                 break;
1055             }
1056             s = ParseSamiSearch( txt, s, ">" );
1057         }
1058         else if( !strncmp( s, "&nbsp;", 6 ) )
1059         {
1060             c = ' ';
1061             s += 6;
1062         }
1063         else if( *s == '\t' )
1064         {
1065             c = ' ';
1066             s++;
1067         }
1068         else
1069         {
1070             c = *s;
1071             s++;
1072         }
1073         if( c != '\0' && i_text+1 < sizeof(text) )
1074         {
1075             text[i_text++] = c;
1076             text[i_text] = '\0';
1077         }
1078     }
1079
1080     p_subtitle->i_start = i_start * 1000;
1081     p_subtitle->i_stop  = 0;
1082     p_subtitle->psz_text = strdup( text );
1083
1084     return VLC_SUCCESS;
1085 }
1086
1087 /* ParseDVDSubtitle
1088  *  Format
1089  *      {T h1:m1:s1:c1
1090  *      Line1
1091  *      Line2
1092  *      ...
1093  *      }
1094  * TODO it can have a header
1095  *      { HEAD
1096  *          ...
1097  *          CODEPAGE=...
1098  *          FORMAT=...
1099  *          LANG=English
1100  *      }
1101  *      LANG support would be cool
1102  *      CODEPAGE is probably mandatory FIXME
1103  */
1104 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1105 {
1106     demux_sys_t *p_sys = p_demux->p_sys;
1107     text_t      *txt = &p_sys->txt;
1108     char *psz_text;
1109
1110     for( ;; )
1111     {
1112         const char *s = TextGetLine( txt );
1113         int h1, m1, s1, c1;
1114
1115         if( !s )
1116             return VLC_EGENERIC;
1117
1118         if( sscanf( s,
1119                     "{T %d:%d:%d:%d",
1120                     &h1, &m1, &s1, &c1 ) == 4 )
1121         {
1122             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1123                                     (int64_t)m1 * 60*1000 +
1124                                     (int64_t)s1 * 1000 +
1125                                     (int64_t)c1 * 10) * 1000;
1126             p_subtitle->i_stop = 0;
1127             break;
1128         }
1129     }
1130
1131     /* Now read text until a line containing "}" */
1132     psz_text = strdup("");
1133     if( !psz_text )
1134         return VLC_ENOMEM;
1135     for( ;; )
1136     {
1137         const char *s = TextGetLine( txt );
1138         int i_len;
1139         int i_old;
1140
1141         if( !s )
1142         {
1143             free( psz_text );
1144             return VLC_EGENERIC;
1145         }
1146
1147         i_len = strlen( s );
1148         if( i_len == 1 && s[0] == '}')
1149         {
1150             p_subtitle->psz_text = psz_text;
1151             return VLC_SUCCESS;
1152         }
1153
1154         i_old = strlen( psz_text );
1155         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1156         if( !psz_text )
1157             return VLC_ENOMEM;
1158         strcat( psz_text, s );
1159         strcat( psz_text, "\n" );
1160     }
1161 }
1162
1163 /* ParseMPL2
1164  *  Format
1165  *     [n1][n2]Line1|Line2|Line3...
1166  *  where n1 and n2 are the video frame number (n2 can be empty)
1167  */
1168 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1169 {
1170     demux_sys_t *p_sys = p_demux->p_sys;
1171     text_t      *txt = &p_sys->txt;
1172     char *psz_text;
1173     int i;
1174
1175     for( ;; )
1176     {
1177         const char *s = TextGetLine( txt );
1178         int i_start;
1179         int i_stop;
1180
1181         if( !s )
1182             return VLC_EGENERIC;
1183
1184         psz_text = malloc( strlen(s) + 1 );
1185         if( !psz_text )
1186             return VLC_ENOMEM;
1187
1188         i_start = 0;
1189         i_stop  = 0;
1190         if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1191             sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1192         {
1193             p_subtitle->i_start = (int64_t)i_start * 100000;
1194             p_subtitle->i_stop  = (int64_t)i_stop  * 100000;
1195             break;
1196         }
1197         free( psz_text );
1198     }
1199
1200     /* replace | by \n */
1201     for( i = 0; psz_text[i] != '\0'; )
1202     {
1203         if( psz_text[i] == '|' )
1204             psz_text[i] = '\n';
1205
1206         /* Remove italic */
1207         if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1208             memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1209         else
1210             i++;
1211     }
1212     p_subtitle->psz_text = psz_text;
1213     return VLC_SUCCESS;
1214 }
1215