]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
Support for multiple lines in PJS subs.
[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  *          Jean-Baptiste Kempf <jb@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc/vlc.h>
35 #include <vlc_plugin.h>
36 #include <vlc_input.h>
37
38 #include <errno.h>
39 #ifdef HAVE_SYS_TYPES_H
40 #   include <sys/types.h>
41 #endif
42 #include <ctype.h>
43
44 #include <vlc_demux.h>
45 #include <vlc_charset.h>
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open ( vlc_object_t *p_this );
51 static void Close( vlc_object_t *p_this );
52
53 #define SUB_DELAY_LONGTEXT \
54     N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
55 #define SUB_FPS_LONGTEXT \
56     N_("Override the normal frames per second settings. " \
57     "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
58 #define SUB_TYPE_LONGTEXT \
59     N_("Force the subtiles format. Valid values are : \"microdvd\", " \
60     "\"subrip\",  \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
61     "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\" "\
62     "\"mpsub\" \"jacosub\" and \"auto\" (meaning autodetection, this " \
63     "should always work).")
64 static const char *ppsz_sub_type[] =
65 {
66     "auto", "microdvd", "subrip", "subviewer", "ssa1",
67     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
68     "aqt", "pjs", "mpsub", "jacosub"
69 };
70
71 vlc_module_begin();
72     set_shortname( _("Subtitles"));
73     set_description( _("Text subtitles parser") );
74     set_capability( "demux", 0 );
75     set_category( CAT_INPUT );
76     set_subcategory( SUBCAT_INPUT_DEMUX );
77     add_float( "sub-fps", 0.0, NULL,
78                N_("Frames per second"),
79                SUB_FPS_LONGTEXT, true );
80     add_integer( "sub-delay", 0, NULL,
81                N_("Subtitles delay"),
82                SUB_DELAY_LONGTEXT, true );
83     add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
84                 SUB_TYPE_LONGTEXT, true );
85         change_string_list( ppsz_sub_type, NULL, NULL );
86     set_callbacks( Open, Close );
87
88     add_shortcut( "subtitle" );
89 vlc_module_end();
90
91 /*****************************************************************************
92  * Prototypes:
93  *****************************************************************************/
94 enum
95 {
96     SUB_TYPE_UNKNOWN = -1,
97     SUB_TYPE_MICRODVD,
98     SUB_TYPE_SUBRIP,
99     SUB_TYPE_SSA1,
100     SUB_TYPE_SSA2_4,
101     SUB_TYPE_ASS,
102     SUB_TYPE_VPLAYER,
103     SUB_TYPE_SAMI,
104     SUB_TYPE_SUBVIEWER, //SUBVIEWER 2!
105     SUB_TYPE_DVDSUBTITLE,
106     SUB_TYPE_MPL2,
107     SUB_TYPE_AQT,
108     SUB_TYPE_PJS,
109     SUB_TYPE_MPSUB,
110     SUB_TYPE_JACOSUB,
111     SUB_TYPE_PSB
112 };
113
114 typedef struct
115 {
116     int     i_line_count;
117     int     i_line;
118     char    **line;
119 } text_t;
120
121 static int  TextLoad( text_t *, stream_t *s );
122 static void TextUnload( text_t * );
123
124 typedef struct
125 {
126     int64_t i_start;
127     int64_t i_stop;
128
129     char    *psz_text;
130 } subtitle_t;
131
132
133 struct demux_sys_t
134 {
135     int         i_type;
136     text_t      txt;
137     es_out_id_t *es;
138
139     int64_t     i_next_demux_date;
140     int64_t     i_microsecperframe;
141
142     char        *psz_header;
143     int         i_subtitle;
144     int         i_subtitles;
145     subtitle_t  *subtitle;
146
147     int64_t     i_length;
148 };
149
150 static int  ParseMicroDvd   ( demux_t *, subtitle_t *, int );
151 static int  ParseSubRip     ( demux_t *, subtitle_t *, int );
152 static int  ParseSubViewer  ( demux_t *, subtitle_t *, int );
153 static int  ParseSSA        ( demux_t *, subtitle_t *, int );
154 static int  ParseVplayer    ( demux_t *, subtitle_t *, int );
155 static int  ParseSami       ( demux_t *, subtitle_t *, int );
156 static int  ParseDVDSubtitle( demux_t *, subtitle_t *, int );
157 static int  ParseMPL2       ( demux_t *, subtitle_t *, int );
158 static int  ParseAQT        ( demux_t *, subtitle_t *, int );
159 static int  ParsePJS        ( demux_t *, subtitle_t *, int );
160 static int  ParseMPSub      ( demux_t *, subtitle_t *, int );
161 static int  ParseJSS        ( demux_t *, subtitle_t *, int );
162 static int  ParsePSB        ( demux_t *, subtitle_t *, int );
163
164 static struct
165 {
166     const char *psz_type_name;
167     int  i_type;
168     const char *psz_name;
169     int  (*pf_read)( demux_t *, subtitle_t*, int );
170 } sub_read_subtitle_function [] =
171 {
172     { "microdvd",   SUB_TYPE_MICRODVD,    "MicroDVD",    ParseMicroDvd },
173     { "subrip",     SUB_TYPE_SUBRIP,      "SubRIP",      ParseSubRip },
174     { "subviewer",  SUB_TYPE_SUBVIEWER,   "SubViewer",   ParseSubViewer },
175     { "ssa1",       SUB_TYPE_SSA1,        "SSA-1",       ParseSSA },
176     { "ssa2-4",     SUB_TYPE_SSA2_4,      "SSA-2/3/4",   ParseSSA },
177     { "ass",        SUB_TYPE_ASS,         "SSA/ASS",     ParseSSA },
178     { "vplayer",    SUB_TYPE_VPLAYER,     "VPlayer",     ParseVplayer },
179     { "sami",       SUB_TYPE_SAMI,        "SAMI",        ParseSami },
180     { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
181     { "mpl2",       SUB_TYPE_MPL2,        "MPL2",        ParseMPL2 },
182     { "aqt",        SUB_TYPE_AQT,         "AQTitle",     ParseAQT },
183     { "pjs",        SUB_TYPE_PJS,         "PhoenixSub",  ParsePJS },
184     { "mpsub",      SUB_TYPE_MPSUB,       "MPSub",       ParseMPSub },
185     { "jacosub",    SUB_TYPE_JACOSUB,     "JacoSub",     ParseJSS },
186     { "psb",        SUB_TYPE_PSB,         "PowerDivx",   ParsePSB },
187     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
188 };
189
190 /* Missing Detect
191     SubViewer 1
192     JSS
193     RealText
194     Subrip09
195    */
196
197
198 static int Demux( demux_t * );
199 static int Control( demux_t *, int, va_list );
200
201 /*static void Fix( demux_t * );*/
202
203 /*****************************************************************************
204  * Module initializer
205  *****************************************************************************/
206 static int Open ( vlc_object_t *p_this )
207 {
208     demux_t        *p_demux = (demux_t*)p_this;
209     demux_sys_t    *p_sys;
210     es_format_t    fmt;
211     float          f_fps;
212     char           *psz_type;
213     int  (*pf_read)( demux_t *, subtitle_t*, int );
214     int            i, i_max;
215
216     if( !p_demux->b_force )
217     {
218         msg_Dbg( p_demux, "subtitle demux discarded" );
219         return VLC_EGENERIC;
220     }
221
222     p_demux->pf_demux = Demux;
223     p_demux->pf_control = Control;
224     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
225     if( p_sys == NULL )
226         return VLC_ENOMEM;
227
228     p_sys->psz_header         = NULL;
229     p_sys->i_subtitle         = 0;
230     p_sys->i_subtitles        = 0;
231     p_sys->subtitle           = NULL;
232     p_sys->i_microsecperframe = 40000;
233
234     /* Get the FPS */
235     f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
236     if( f_fps >= 1.0 )
237         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
238
239     msg_Dbg( p_demux, "Movie fps: %f", f_fps );
240
241     /* Check for override of the fps */
242     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
243     if( f_fps >= 1.0 )
244     {
245         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
246         msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
247     }
248
249     /* Get or probe the type */
250     p_sys->i_type = SUB_TYPE_UNKNOWN;
251     psz_type = var_CreateGetString( p_demux, "sub-type" );
252     if( *psz_type )
253     {
254         int i;
255
256         for( i = 0; ; i++ )
257         {
258             if( sub_read_subtitle_function[i].psz_type_name == NULL )
259                 break;
260
261             if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
262                          psz_type ) )
263             {
264                 p_sys->i_type = sub_read_subtitle_function[i].i_type;
265                 break;
266             }
267         }
268     }
269     free( psz_type );
270
271     /* Probe if unknown type */
272     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
273     {
274         int     i_try;
275         char    *s = NULL;
276
277         msg_Dbg( p_demux, "autodetecting subtitle format" );
278         for( i_try = 0; i_try < 256; i_try++ )
279         {
280             int i_dummy;
281             char p_dummy;
282
283             if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
284                 break;
285
286             if( strcasestr( s, "<SAMI>" ) )
287             {
288                 p_sys->i_type = SUB_TYPE_SAMI;
289                 break;
290             }
291             else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
292                      sscanf( s, "{%d}{}", &i_dummy ) == 1)
293             {
294                 p_sys->i_type = SUB_TYPE_MICRODVD;
295                 break;
296             }
297             else if( sscanf( s,
298                              "%d:%d:%d,%d --> %d:%d:%d,%d",
299                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
300                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
301             {
302                 p_sys->i_type = SUB_TYPE_SUBRIP;
303                 break;
304             }
305             else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
306             {
307                 p_sys->i_type = SUB_TYPE_SSA1;
308                 break;
309             }
310             else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
311             {
312                 p_sys->i_type = SUB_TYPE_ASS;
313                 break;
314             }
315             else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
316             {
317                 p_sys->i_type = SUB_TYPE_SSA2_4;
318                 break;
319             }
320             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
321             {
322                 p_sys->i_type = SUB_TYPE_SSA2_4;
323                 break;
324             }
325             else if( !strncasecmp( s, "Dialogue:", 9  ) )
326             {
327                 p_sys->i_type = SUB_TYPE_ASS;
328                 break;
329             }
330             else if( strcasestr( s, "[INFORMATION]" ) )
331             {
332                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
333                 break;
334             }
335             else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d", &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy ) == 7 ||
336                     sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
337             {
338                 p_sys->i_type = SUB_TYPE_JACOSUB;
339             }
340             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
341                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
342             {
343                 p_sys->i_type = SUB_TYPE_VPLAYER;
344                 break;
345             }
346             else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
347                              &i_dummy, &i_dummy ) == 4 )
348             {
349                 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
350                 break;
351             }
352             else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
353                      sscanf( s, "[%d][]", &i_dummy ) == 1)
354             {
355                 p_sys->i_type = SUB_TYPE_MPL2;
356                 break;
357             }
358             else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
359                      ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
360                        && p_dummy =='E' ) )
361             {
362                 p_sys->i_type = SUB_TYPE_MPSUB;
363             }
364             else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
365             {
366                 p_sys->i_type = SUB_TYPE_AQT;
367             }
368             else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
369             {
370                 p_sys->i_type = SUB_TYPE_PJS;
371             }
372             else if( sscanf( s, "{%d:%d:%d}", &i_dummy, &i_dummy, &i_dummy ) == 3 )
373             {
374                 p_sys->i_type = SUB_TYPE_PSB;
375             }
376
377             free( s );
378             s = NULL;
379         }
380
381         free( s );
382
383         /* It will nearly always work even for non seekable stream thanks the
384          * caching system, and if it fails we lose just a few sub */
385         if( stream_Seek( p_demux->s, 0 ) )
386         {
387             msg_Warn( p_demux, "failed to rewind" );
388         }
389     }
390     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
391     {
392         msg_Err( p_demux, "failed to recognize subtitle type" );
393         free( p_sys );
394         return VLC_EGENERIC;
395     }
396
397     for( i = 0; ; i++ )
398     {
399         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
400         {
401             msg_Dbg( p_demux, "detected %s format",
402                      sub_read_subtitle_function[i].psz_name );
403             pf_read = sub_read_subtitle_function[i].pf_read;
404             break;
405         }
406     }
407
408     msg_Dbg( p_demux, "loading all subtitles..." );
409
410     /* Load the whole file */
411     TextLoad( &p_sys->txt, p_demux->s );
412
413     /* Parse it */
414     for( i_max = 0;; )
415     {
416         if( p_sys->i_subtitles >= i_max )
417         {
418             i_max += 500;
419             if( !( p_sys->subtitle = realloc( p_sys->subtitle,
420                                               sizeof(subtitle_t) * i_max ) ) )
421             {
422                 msg_Err( p_demux, "out of memory");
423                 free( p_sys->subtitle );
424                 TextUnload( &p_sys->txt );
425                 free( p_sys );
426                 return VLC_ENOMEM;
427             }
428         }
429
430         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
431                      p_sys->i_subtitles ) )
432             break;
433
434         p_sys->i_subtitles++;
435     }
436     /* Unload */
437     TextUnload( &p_sys->txt );
438
439     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
440
441     /* Fix subtitle (order and time) *** */
442     p_sys->i_subtitle = 0;
443     p_sys->i_length = 0;
444     if( p_sys->i_subtitles > 0 )
445     {
446         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
447         /* +1 to avoid 0 */
448         if( p_sys->i_length <= 0 )
449             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
450     }
451
452     /* *** add subtitle ES *** */
453     if( p_sys->i_type == SUB_TYPE_SSA1 ||
454              p_sys->i_type == SUB_TYPE_SSA2_4 ||
455              p_sys->i_type == SUB_TYPE_ASS )
456     {
457         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
458     }
459     else
460     {
461         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
462     }
463     if( p_sys->psz_header != NULL )
464     {
465         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
466         fmt.p_extra = strdup( p_sys->psz_header );
467     }
468     p_sys->es = es_out_Add( p_demux->out, &fmt );
469
470     return VLC_SUCCESS;
471 }
472
473 /*****************************************************************************
474  * Close: Close subtitle demux
475  *****************************************************************************/
476 static void Close( vlc_object_t *p_this )
477 {
478     demux_t *p_demux = (demux_t*)p_this;
479     demux_sys_t *p_sys = p_demux->p_sys;
480     int i;
481
482     for( i = 0; i < p_sys->i_subtitles; i++ )
483         free( p_sys->subtitle[i].psz_text );
484     free( p_sys->subtitle );
485
486     free( p_sys );
487 }
488
489 /*****************************************************************************
490  * Control:
491  *****************************************************************************/
492 static int Control( demux_t *p_demux, int i_query, va_list args )
493 {
494     demux_sys_t *p_sys = p_demux->p_sys;
495     int64_t *pi64, i64;
496     double *pf, f;
497
498     switch( i_query )
499     {
500         case DEMUX_GET_LENGTH:
501             pi64 = (int64_t*)va_arg( args, int64_t * );
502             *pi64 = p_sys->i_length;
503             return VLC_SUCCESS;
504
505         case DEMUX_GET_TIME:
506             pi64 = (int64_t*)va_arg( args, int64_t * );
507             if( p_sys->i_subtitle < p_sys->i_subtitles )
508             {
509                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
510                 return VLC_SUCCESS;
511             }
512             return VLC_EGENERIC;
513
514         case DEMUX_SET_TIME:
515             i64 = (int64_t)va_arg( args, int64_t );
516             p_sys->i_subtitle = 0;
517             while( p_sys->i_subtitle < p_sys->i_subtitles &&
518                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
519             {
520                 p_sys->i_subtitle++;
521             }
522
523             if( p_sys->i_subtitle >= p_sys->i_subtitles )
524                 return VLC_EGENERIC;
525             return VLC_SUCCESS;
526
527         case DEMUX_GET_POSITION:
528             pf = (double*)va_arg( args, double * );
529             if( p_sys->i_subtitle >= p_sys->i_subtitles )
530             {
531                 *pf = 1.0;
532             }
533             else if( p_sys->i_subtitles > 0 )
534             {
535                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
536                       (double)p_sys->i_length;
537             }
538             else
539             {
540                 *pf = 0.0;
541             }
542             return VLC_SUCCESS;
543
544         case DEMUX_SET_POSITION:
545             f = (double)va_arg( args, double );
546             i64 = f * p_sys->i_length;
547
548             p_sys->i_subtitle = 0;
549             while( p_sys->i_subtitle < p_sys->i_subtitles &&
550                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
551             {
552                 p_sys->i_subtitle++;
553             }
554             if( p_sys->i_subtitle >= p_sys->i_subtitles )
555                 return VLC_EGENERIC;
556             return VLC_SUCCESS;
557
558         case DEMUX_SET_NEXT_DEMUX_TIME:
559             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
560             return VLC_SUCCESS;
561
562         case DEMUX_GET_FPS:
563         case DEMUX_GET_META:
564         case DEMUX_GET_ATTACHMENTS:
565         case DEMUX_GET_TITLE_INFO:
566         case DEMUX_HAS_UNSUPPORTED_META:
567             return VLC_EGENERIC;
568
569         default:
570             msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
571             return VLC_EGENERIC;
572     }
573 }
574
575 /*****************************************************************************
576  * Demux: Send subtitle to decoder
577  *****************************************************************************/
578 static int Demux( demux_t *p_demux )
579 {
580     demux_sys_t *p_sys = p_demux->p_sys;
581     int64_t i_maxdate;
582
583     if( p_sys->i_subtitle >= p_sys->i_subtitles )
584         return 0;
585
586     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
587     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
588     {
589         /* Should not happen */
590         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
591     }
592
593     while( p_sys->i_subtitle < p_sys->i_subtitles &&
594            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
595     {
596         block_t *p_block;
597         int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
598
599         if( i_len <= 1 )
600         {
601             /* empty subtitle */
602             p_sys->i_subtitle++;
603             continue;
604         }
605
606         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
607         {
608             p_sys->i_subtitle++;
609             continue;
610         }
611
612         if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
613         {
614             p_sys->i_subtitle++;
615             continue;
616         }
617
618         p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
619         p_block->i_dts = p_block->i_pts;
620         if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
621         {
622             p_block->i_length =
623                 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
624         }
625
626         memcpy( p_block->p_buffer,
627                 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
628         if( p_block->i_pts > 0 )
629         {
630             es_out_Send( p_demux->out, p_sys->es, p_block );
631         }
632         else
633         {
634             block_Release( p_block );
635         }
636         p_sys->i_subtitle++;
637     }
638
639     /* */
640     p_sys->i_next_demux_date = 0;
641
642     return 1;
643 }
644
645 /*****************************************************************************
646  * Fix: fix time stamp and order of subtitle
647  *****************************************************************************/
648 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
649 static void Fix( demux_t *p_demux )
650 {
651     demux_sys_t *p_sys = p_demux->p_sys;
652     bool b_done;
653     int     i_index;
654
655     /* *** fix order (to be sure...) *** */
656     /* We suppose that there are near in order and this durty bubble sort
657      * wont take too much time
658      */
659     do
660     {
661         b_done = true;
662         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
663         {
664             if( p_sys->subtitle[i_index].i_start <
665                     p_sys->subtitle[i_index - 1].i_start )
666             {
667                 subtitle_t sub_xch;
668                 memcpy( &sub_xch,
669                         p_sys->subtitle + i_index - 1,
670                         sizeof( subtitle_t ) );
671                 memcpy( p_sys->subtitle + i_index - 1,
672                         p_sys->subtitle + i_index,
673                         sizeof( subtitle_t ) );
674                 memcpy( p_sys->subtitle + i_index,
675                         &sub_xch,
676                         sizeof( subtitle_t ) );
677                 b_done = false;
678             }
679         }
680     } while( !b_done );
681 }
682 #endif
683
684 static int TextLoad( text_t *txt, stream_t *s )
685 {
686     int   i_line_max;
687
688     /* init txt */
689     i_line_max          = 500;
690     txt->i_line_count   = 0;
691     txt->i_line         = 0;
692     txt->line           = calloc( i_line_max, sizeof( char * ) );
693
694     /* load the complete file */
695     for( ;; )
696     {
697         char *psz = stream_ReadLine( s );
698
699         if( psz == NULL )
700             break;
701
702         txt->line[txt->i_line_count++] = psz;
703         if( txt->i_line_count >= i_line_max )
704         {
705             i_line_max += 100;
706             txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
707         }
708     }
709
710     if( txt->i_line_count <= 0 )
711     {
712         free( txt->line );
713         return VLC_EGENERIC;
714     }
715
716     return VLC_SUCCESS;
717 }
718 static void TextUnload( text_t *txt )
719 {
720     int i;
721
722     for( i = 0; i < txt->i_line_count; i++ )
723     {
724         free( txt->line[i] );
725     }
726     free( txt->line );
727     txt->i_line       = 0;
728     txt->i_line_count = 0;
729 }
730
731 static char *TextGetLine( text_t *txt )
732 {
733     if( txt->i_line >= txt->i_line_count )
734         return( NULL );
735
736     return txt->line[txt->i_line++];
737 }
738 static void TextPreviousLine( text_t *txt )
739 {
740     if( txt->i_line > 0 )
741         txt->i_line--;
742 }
743
744 /*****************************************************************************
745  * Specific Subtitle function
746  *****************************************************************************/
747 /* ParseMicroDvd:
748  *  Format:
749  *      {n1}{n2}Line1|Line2|Line3....
750  *  where n1 and n2 are the video frame number (n2 can be empty)
751  */
752 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
753                           int i_idx )
754 {
755     VLC_UNUSED( i_idx );
756     demux_sys_t *p_sys = p_demux->p_sys;
757     text_t      *txt = &p_sys->txt;
758     char *psz_text;
759     int  i_start;
760     int  i_stop;
761     int  i;
762
763     for( ;; )
764     {
765         const char *s = TextGetLine( txt );
766         if( !s )
767             return VLC_EGENERIC;
768
769         psz_text = malloc( strlen(s) + 1 );
770         if( !psz_text )
771             return VLC_ENOMEM;
772
773         i_start = 0;
774         i_stop  = 0;
775         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
776             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
777         {
778             float f_fps;
779             if( i_start != 1 || i_stop != 1 )
780                 break;
781
782             /* We found a possible setting of the framerate "{1}{1}23.976" */
783             /* Check if it's usable, and if the sub-fps is not set */
784             f_fps = us_strtod( psz_text, NULL );
785             if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
786                 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
787         }
788         free( psz_text );
789     }
790
791     /* replace | by \n */
792     for( i = 0; psz_text[i] != '\0'; i++ )
793     {
794         if( psz_text[i] == '|' )
795             psz_text[i] = '\n';
796     }
797
798     /* */
799     p_subtitle->i_start  = i_start * p_sys->i_microsecperframe;
800     p_subtitle->i_stop   = i_stop  * p_sys->i_microsecperframe;
801     p_subtitle->psz_text = psz_text;
802     return VLC_SUCCESS;
803 }
804
805 /* ParseSubRipSubViewer
806  *  Format SubRip
807  *      n
808  *      h1:m1:s1,d1 --> h2:m2:s2,d2
809  *      Line1
810  *      Line2
811  *      ....
812  *      [Empty line]
813  *  Format SubViewer v1/v2
814  *      h1:m1:s1.d1,h2:m2:s2.d2
815  *      Line1[br]Line2
816  *      Line3
817  *      ...
818  *      [empty line]
819  *  We ignore line number for SubRip
820  */
821 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
822                                  const char *psz_fmt,
823                                  bool b_replace_br )
824 {
825     demux_sys_t *p_sys = p_demux->p_sys;
826     text_t      *txt = &p_sys->txt;
827     char    *psz_text;
828
829     for( ;; )
830     {
831         const char *s = TextGetLine( txt );
832         int h1, m1, s1, d1, h2, m2, s2, d2;
833
834         if( !s )
835             return VLC_EGENERIC;
836
837         if( sscanf( s, psz_fmt,
838                     &h1, &m1, &s1, &d1,
839                     &h2, &m2, &s2, &d2 ) == 8 )
840         {
841             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
842                                     (int64_t)m1 * 60*1000 +
843                                     (int64_t)s1 * 1000 +
844                                     (int64_t)d1 ) * 1000;
845
846             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
847                                     (int64_t)m2 * 60*1000 +
848                                     (int64_t)s2 * 1000 +
849                                     (int64_t)d2 ) * 1000;
850             break;
851         }
852     }
853
854     /* Now read text until an empty line */
855     psz_text = strdup("");
856     if( !psz_text )
857         return VLC_ENOMEM;
858     for( ;; )
859     {
860         const char *s = TextGetLine( txt );
861         int i_len;
862         int i_old;
863
864         if( !s )
865         {
866             free( psz_text );
867             return VLC_EGENERIC;
868         }
869
870         i_len = strlen( s );
871         if( i_len <= 0 )
872         {
873             p_subtitle->psz_text = psz_text;
874             return VLC_SUCCESS;
875         }
876
877         i_old = strlen( psz_text );
878         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
879         if( !psz_text )
880             return VLC_ENOMEM;
881         strcat( psz_text, s );
882         strcat( psz_text, "\n" );
883
884         /* replace [br] by \n */
885         if( b_replace_br )
886         {
887             char *p;
888  
889             while( ( p = strstr( psz_text, "[br]" ) ) )
890             {
891                 *p++ = '\n';
892                 memmove( p, &p[3], strlen(&p[3])+1 );
893             }
894         }
895     }
896 }
897 /* ParseSubRip
898  */
899 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
900                          int i_idx )
901 {
902     VLC_UNUSED( i_idx );
903     return ParseSubRipSubViewer( p_demux, p_subtitle,
904                                  "%d:%d:%d,%d --> %d:%d:%d,%d",
905                                  false );
906 }
907 /* ParseSubViewer
908  */
909 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
910                             int i_idx )
911 {
912     VLC_UNUSED( i_idx );
913
914     return ParseSubRipSubViewer( p_demux, p_subtitle,
915                                  "%d:%d:%d.%d,%d:%d:%d.%d",
916                                  true );
917 }
918
919 /* ParseSSA
920  */
921 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
922                       int i_idx )
923 {
924     demux_sys_t *p_sys = p_demux->p_sys;
925     text_t      *txt = &p_sys->txt;
926
927     for( ;; )
928     {
929         const char *s = TextGetLine( txt );
930         int h1, m1, s1, c1, h2, m2, s2, c2;
931         char *psz_text;
932         char temp[16];
933
934         if( !s )
935             return VLC_EGENERIC;
936
937         /* We expect (SSA2-4):
938          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
939          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
940          *
941          * 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.
942          */
943
944         /* For ASS:
945          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
946          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
947          */
948
949         /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
950         psz_text = malloc( strlen(s) );
951         if( !psz_text )
952             return VLC_ENOMEM;
953
954         if( sscanf( s,
955                     "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
956                     temp,
957                     &h1, &m1, &s1, &c1,
958                     &h2, &m2, &s2, &c2,
959                     psz_text ) == 10 )
960         {
961             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
962             /* (Layer comes from ASS specs ... it's empty for SSA.) */
963             if( p_sys->i_type == SUB_TYPE_SSA1 )
964             {
965                 /* SSA1 has only 8 commas before the text starts, not 9 */
966                 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
967                 psz_text[0] = ',';
968             }
969             else
970             {
971                 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
972
973                 /* ReadOrder, Layer, %s(rest of fields) */
974                 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
975                 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
976                 memcpy( psz_text, temp, strlen(temp) );
977             }
978
979             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
980                                     (int64_t)m1 * 60*1000 +
981                                     (int64_t)s1 * 1000 +
982                                     (int64_t)c1 * 10 ) * 1000;
983             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
984                                     (int64_t)m2 * 60*1000 +
985                                     (int64_t)s2 * 1000 +
986                                     (int64_t)c2 * 10 ) * 1000;
987             p_subtitle->psz_text = psz_text;
988             return VLC_SUCCESS;
989         }
990         free( psz_text );
991
992         /* All the other stuff we add to the header field */
993         if( !p_sys->psz_header )
994             p_sys->psz_header = strdup( "" );
995         if( !p_sys->psz_header )
996             return VLC_ENOMEM;
997
998         p_sys->psz_header =
999             realloc( p_sys->psz_header,
1000                      strlen( p_sys->psz_header ) + strlen( s ) + 2 );
1001         strcat( p_sys->psz_header,  s );
1002         strcat( p_sys->psz_header, "\n" );
1003     }
1004 }
1005
1006 /* ParseVplayer
1007  *  Format
1008  *      h:m:s:Line1|Line2|Line3....
1009  *  or
1010  *      h:m:s Line1|Line2|Line3....
1011  */
1012 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1013                           int i_idx )
1014 {
1015     VLC_UNUSED( i_idx );
1016
1017     demux_sys_t *p_sys = p_demux->p_sys;
1018     text_t      *txt = &p_sys->txt;
1019     char *psz_text;
1020     int i;
1021
1022     for( ;; )
1023     {
1024         const char *s = TextGetLine( txt );
1025         int h1, m1, s1;
1026
1027         if( !s )
1028             return VLC_EGENERIC;
1029
1030         psz_text = malloc( strlen( s ) + 1 );
1031         if( !psz_text )
1032             return VLC_ENOMEM;
1033
1034         if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1035                     &h1, &m1, &s1, psz_text ) == 4 )
1036         {
1037             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1038                                     (int64_t)m1 * 60*1000 +
1039                                     (int64_t)s1 * 1000 ) * 1000;
1040             p_subtitle->i_stop  = 0;
1041             break;
1042         }
1043         free( psz_text );
1044     }
1045
1046     /* replace | by \n */
1047     for( i = 0; psz_text[i] != '\0'; i++ )
1048     {
1049         if( psz_text[i] == '|' )
1050             psz_text[i] = '\n';
1051     }
1052     p_subtitle->psz_text = psz_text;
1053     return VLC_SUCCESS;
1054 }
1055
1056 /* ParseSami
1057  */
1058 static char *ParseSamiSearch( text_t *txt,
1059                               char *psz_start, const char *psz_str )
1060 {
1061     if( psz_start && strcasestr( psz_start, psz_str ) )
1062     {
1063         char *s = strcasestr( psz_start, psz_str );
1064         return &s[strlen( psz_str )];
1065     }
1066
1067     for( ;; )
1068     {
1069         char *p = TextGetLine( txt );
1070         if( !p )
1071             return NULL;
1072
1073         if( strcasestr( p, psz_str ) )
1074         {
1075             char *s = strcasestr( p, psz_str );
1076             return &s[strlen( psz_str )];
1077         }
1078     }
1079 }
1080 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1081 {
1082     VLC_UNUSED( i_idx );
1083     demux_sys_t *p_sys = p_demux->p_sys;
1084     text_t      *txt = &p_sys->txt;
1085
1086     char *s;
1087     int64_t i_start;
1088
1089     unsigned int i_text;
1090     char text[8192]; /* Arbitrary but should be long enough */
1091
1092     /* search "Start=" */
1093     if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1094         return VLC_EGENERIC;
1095
1096     /* get start value */
1097     i_start = strtol( s, &s, 0 );
1098
1099     /* search <P */
1100     if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1101         return VLC_EGENERIC;
1102
1103     /* search > */
1104     if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1105         return VLC_EGENERIC;
1106
1107     i_text = 0;
1108     text[0] = '\0';
1109     /* now get all txt until  a "Start=" line */
1110     for( ;; )
1111     {
1112         char c = '\0';
1113         /* Search non empty line */
1114         while( s && *s == '\0' )
1115             s = TextGetLine( txt );
1116         if( !s )
1117             break;
1118
1119         if( *s == '<' )
1120         {
1121             if( !strncasecmp( s, "<br", 3 ) )
1122             {
1123                 c = '\n';
1124             }
1125             else if( strcasestr( s, "Start=" ) )
1126             {
1127                 TextPreviousLine( txt );
1128                 break;
1129             }
1130             s = ParseSamiSearch( txt, s, ">" );
1131         }
1132         else if( !strncmp( s, "&nbsp;", 6 ) )
1133         {
1134             c = ' ';
1135             s += 6;
1136         }
1137         else if( *s == '\t' )
1138         {
1139             c = ' ';
1140             s++;
1141         }
1142         else
1143         {
1144             c = *s;
1145             s++;
1146         }
1147         if( c != '\0' && i_text+1 < sizeof(text) )
1148         {
1149             text[i_text++] = c;
1150             text[i_text] = '\0';
1151         }
1152     }
1153
1154     p_subtitle->i_start = i_start * 1000;
1155     p_subtitle->i_stop  = 0;
1156     p_subtitle->psz_text = strdup( text );
1157
1158     return VLC_SUCCESS;
1159 }
1160
1161 /* ParseDVDSubtitle
1162  *  Format
1163  *      {T h1:m1:s1:c1
1164  *      Line1
1165  *      Line2
1166  *      ...
1167  *      }
1168  * TODO it can have a header
1169  *      { HEAD
1170  *          ...
1171  *          CODEPAGE=...
1172  *          FORMAT=...
1173  *          LANG=English
1174  *      }
1175  *      LANG support would be cool
1176  *      CODEPAGE is probably mandatory FIXME
1177  */
1178 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1179                              int i_idx )
1180 {
1181     VLC_UNUSED( i_idx );
1182
1183     demux_sys_t *p_sys = p_demux->p_sys;
1184     text_t      *txt = &p_sys->txt;
1185     char *psz_text;
1186
1187     for( ;; )
1188     {
1189         const char *s = TextGetLine( txt );
1190         int h1, m1, s1, c1;
1191
1192         if( !s )
1193             return VLC_EGENERIC;
1194
1195         if( sscanf( s,
1196                     "{T %d:%d:%d:%d",
1197                     &h1, &m1, &s1, &c1 ) == 4 )
1198         {
1199             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1200                                     (int64_t)m1 * 60*1000 +
1201                                     (int64_t)s1 * 1000 +
1202                                     (int64_t)c1 * 10) * 1000;
1203             p_subtitle->i_stop = 0;
1204             break;
1205         }
1206     }
1207
1208     /* Now read text until a line containing "}" */
1209     psz_text = strdup("");
1210     if( !psz_text )
1211         return VLC_ENOMEM;
1212     for( ;; )
1213     {
1214         const char *s = TextGetLine( txt );
1215         int i_len;
1216         int i_old;
1217
1218         if( !s )
1219         {
1220             free( psz_text );
1221             return VLC_EGENERIC;
1222         }
1223
1224         i_len = strlen( s );
1225         if( i_len == 1 && s[0] == '}')
1226         {
1227             p_subtitle->psz_text = psz_text;
1228             return VLC_SUCCESS;
1229         }
1230
1231         i_old = strlen( psz_text );
1232         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1233         if( !psz_text )
1234             return VLC_ENOMEM;
1235         strcat( psz_text, s );
1236         strcat( psz_text, "\n" );
1237     }
1238 }
1239
1240 /* ParseMPL2
1241  *  Format
1242  *     [n1][n2]Line1|Line2|Line3...
1243  *  where n1 and n2 are the video frame number (n2 can be empty)
1244  */
1245 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1246 {
1247     VLC_UNUSED( i_idx );
1248
1249     demux_sys_t *p_sys = p_demux->p_sys;
1250     text_t      *txt = &p_sys->txt;
1251     char *psz_text;
1252     int i;
1253
1254     for( ;; )
1255     {
1256         const char *s = TextGetLine( txt );
1257         int i_start;
1258         int i_stop;
1259
1260         if( !s )
1261             return VLC_EGENERIC;
1262
1263         psz_text = malloc( strlen(s) + 1 );
1264         if( !psz_text )
1265             return VLC_ENOMEM;
1266
1267         i_start = 0;
1268         i_stop  = 0;
1269         if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1270             sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1271         {
1272             p_subtitle->i_start = (int64_t)i_start * 100000;
1273             p_subtitle->i_stop  = (int64_t)i_stop  * 100000;
1274             break;
1275         }
1276         free( psz_text );
1277     }
1278
1279     for( i = 0; psz_text[i] != '\0'; )
1280     {
1281         /* replace | by \n */
1282         if( psz_text[i] == '|' )
1283             psz_text[i] = '\n';
1284
1285         /* Remove italic */
1286         if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1287             memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1288         else
1289             i++;
1290     }
1291     p_subtitle->psz_text = psz_text;
1292     return VLC_SUCCESS;
1293 }
1294
1295 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1296 {
1297     VLC_UNUSED( i_idx );
1298
1299     demux_sys_t *p_sys = p_demux->p_sys;
1300     text_t      *txt = &p_sys->txt;
1301     char *psz_text = strdup( "" );
1302     int i_old = 0;
1303     int i_firstline = 1;
1304
1305     for( ;; )
1306     {
1307         int t; /* Time */
1308
1309         const char *s = TextGetLine( txt );
1310
1311         if( !s )
1312             return VLC_EGENERIC;
1313
1314         /* Data Lines */
1315         if( sscanf (s, "-->> %d", &t) == 1)
1316         {
1317             p_subtitle->i_start = (int64_t)t; /* * FPS*/
1318             p_subtitle->i_stop  = 0;
1319
1320             /* Starting of a subtitle */
1321             if( i_firstline )
1322             {
1323                 i_firstline = 0;
1324             }
1325             /* We have been too far: end of the subtitle, begin of next */
1326             else
1327             {
1328                 txt->i_line--;
1329                 break;
1330             }
1331         }
1332         /* Text Lines */
1333         else
1334         {
1335             i_old = strlen( psz_text ) + 1;
1336             psz_text = realloc( psz_text, i_old + strlen( s ) + 1 );
1337             if( !psz_text )
1338                  return VLC_ENOMEM;
1339             strcat( psz_text, s );
1340             strcat( psz_text, "\n" );
1341             if( txt->i_line == txt->i_line_count )
1342                 break;
1343         }
1344     }
1345     p_subtitle->psz_text = psz_text;
1346     return VLC_SUCCESS;
1347 }
1348
1349 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1350 {
1351     VLC_UNUSED( i_idx );
1352
1353     demux_sys_t *p_sys = p_demux->p_sys;
1354     text_t      *txt = &p_sys->txt;
1355     char *psz_text;
1356     int i;
1357
1358     for( ;; )
1359     {
1360         const char *s = TextGetLine( txt );
1361         int t1, t2;
1362
1363         if( !s )
1364             return VLC_EGENERIC;
1365
1366         psz_text = malloc( strlen(s) + 1 );
1367         if( !psz_text )
1368             return VLC_ENOMEM;
1369
1370         /* Data Lines */
1371         if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1372         {
1373             /* 1/10th of second ? Frame based ? FIXME */
1374             p_subtitle->i_start = 10 * t1;
1375             p_subtitle->i_stop = 10 * t2;
1376             /* Remove latest " */
1377             psz_text[ strlen(psz_text) - 1 ] = '\0';
1378
1379             break;
1380         }
1381         free( psz_text );
1382     }
1383
1384     /* replace | by \n */
1385     for( i = 0; psz_text[i] != '\0'; i++ )
1386     {
1387         if( psz_text[i] == '|' )
1388             psz_text[i] = '\n';
1389     }
1390
1391     p_subtitle->psz_text = psz_text;
1392     msg_Dbg( p_demux, "%s", psz_text );
1393     return VLC_SUCCESS;
1394 }
1395
1396 static float mpsub_total = 0.0;
1397 static float mpsub_factor = 0.0;
1398
1399 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1400 {
1401     VLC_UNUSED( i_idx );
1402
1403     demux_sys_t *p_sys = p_demux->p_sys;
1404     text_t      *txt = &p_sys->txt;
1405     char *psz_text = strdup( "" );
1406
1407     for( ;; )
1408     {
1409         float f1, f2;
1410         char p_dummy;
1411         char *psz_temp;
1412
1413         const char *s = TextGetLine( txt );
1414         if( !s )
1415             return VLC_EGENERIC;
1416
1417         if( strstr( s, "FORMAT" ) )
1418         {
1419             if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1420             {
1421                 mpsub_factor = 100.0;
1422                 break;
1423             }
1424
1425             psz_temp = malloc( strlen(s) );
1426             if( !psz_temp )
1427                 return VLC_ENOMEM;
1428
1429             if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1430             {
1431                 float f_fps;
1432                 f_fps = us_strtod( psz_temp, NULL );
1433                 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1434                     var_SetFloat( p_demux, "sub-fps", f_fps );
1435
1436                 mpsub_factor = 1.0;
1437                 free( psz_temp );
1438                 break;
1439             }
1440             free( psz_temp );
1441         }
1442         /* Data Lines */
1443         if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
1444         {
1445             mpsub_total += f1 * mpsub_factor;
1446             p_subtitle->i_start = (int64_t)(10000.0 * mpsub_total);
1447             mpsub_total += f2 * mpsub_factor;
1448             p_subtitle->i_stop = (int64_t)(10000.0 * mpsub_total);
1449             break;
1450         }
1451     }
1452
1453     for( ;; )
1454     {
1455         const char *s = TextGetLine( txt );
1456
1457         if( !s )
1458             return VLC_EGENERIC;
1459
1460         int i_len = strlen( s );
1461         if( i_len == 0 )
1462             break;
1463
1464         int i_old = strlen( psz_text );
1465
1466         psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1467         if( !psz_text )
1468              return VLC_ENOMEM;
1469
1470         strcat( psz_text, s );
1471         strcat( psz_text, "\n" );
1472     }
1473
1474     p_subtitle->psz_text = psz_text;
1475     return VLC_SUCCESS;
1476 }
1477
1478 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1479 {
1480     VLC_UNUSED( i_idx );
1481
1482     demux_sys_t  *p_sys = p_demux->p_sys;
1483     text_t       *txt = &p_sys->txt;
1484     char         *psz_text, *psz_orig;
1485     char         *psz_text2, *psz_orig2;
1486     int h1, h2, m1, m2, s1, s2, f1, f2;
1487     static int i_comment = 0;
1488
1489     static int jss_time_resolution = 30;
1490     static int jss_time_shift = 0;
1491
1492     /* Parse the main lines */
1493     for( ;; )
1494     {
1495         const char *s = TextGetLine( txt );
1496         if( !s )
1497             return VLC_EGENERIC;
1498
1499         psz_text = malloc( strlen( s ) + 1 );
1500         if( !psz_text )
1501             return VLC_ENOMEM;
1502         psz_orig = psz_text;
1503
1504         /* Complete time lines */
1505         if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1506                     &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1507         {
1508             p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1509                 (int64_t)( ( f1 +  jss_time_shift ) /  jss_time_resolution ) )
1510                 * 1000000;
1511             p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1512                 (int64_t)( ( f2 +  jss_time_shift ) /  jss_time_resolution ) )
1513                 * 1000000;
1514         }
1515         /* Short time lines */
1516         else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1517         {
1518             p_subtitle->i_start = (int64_t)(
1519                     ( f1 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1520             p_subtitle->i_stop = (int64_t)(
1521                     ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1522         }
1523         /* General Directive lines */
1524         /* Only TIME and SHIFT are supported so far */
1525         else if( s[0] == '#' )
1526         {
1527             int h = 0, m =0, sec = 1, f = 1;
1528             unsigned shift = 1;
1529             int inv = 1;
1530
1531             strcpy( psz_text, s );
1532
1533             switch( toupper( psz_text[1] ) )
1534             {
1535             case 'S':
1536                  shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1537
1538                  if( sscanf( &psz_text[shift], "%d", &h ) )
1539                  {
1540                      /* Negative shifting */
1541                      if( h < 0 )
1542                      {
1543                          h *= -1;
1544                          inv = -1;
1545                      }
1546
1547                      if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1548                      {
1549                          if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1550                          {
1551                              sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1552                          }
1553                          else
1554                          {
1555                              h = 0;
1556                              sscanf( &psz_text[shift], "%d:%d.%d",
1557                                      &m, &sec, &f );
1558                              m *= inv;
1559                          }
1560                      }
1561                      else
1562                      {
1563                          h = m = 0;
1564                          sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1565                          sec *= inv;
1566                      }
1567                      jss_time_shift = ( ( h * 3600 + m * 60 + sec )
1568                          * jss_time_resolution + f ) * inv;
1569                  }
1570                  break;
1571
1572             case 'T':
1573                 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1574
1575                 sscanf( &psz_text[shift], "%d", &jss_time_resolution );
1576                 break;
1577             }
1578             free( psz_text );
1579             continue;
1580         }
1581         else
1582             /* Unkown type line, probably a comment */
1583         {
1584             free( psz_text );
1585             continue;
1586         }
1587
1588         /* Skip the blanks */
1589         while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1590
1591         /* Parse the directives */
1592         if( isalpha( *psz_text ) || *psz_text == '[' )
1593         {
1594             while( *psz_text != ' ' )
1595             { psz_text++ ;};
1596
1597             /* Directives are NOT parsed yet */
1598             /* This has probably a better place in a decoder ? */
1599             /* directive = malloc( strlen( psz_text ) + 1 );
1600             if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1601         }
1602
1603         /* Skip the blanks after directives */
1604         while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1605
1606
1607         /* Clean all the lines from inline comments and other stuffs */
1608         psz_text2 = calloc( strlen( psz_text) + 1, 1 );
1609         psz_orig2 = psz_text2;
1610
1611         for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1612         {
1613             switch( *psz_text )
1614             {
1615             case '{':
1616                 i_comment++;
1617                 break;
1618             case '}':
1619                 if( i_comment )
1620                 {
1621                     i_comment = 0;
1622                     if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1623                 }
1624                 break;
1625             case '~':
1626                 if( !i_comment )
1627                 {
1628                     *psz_text2 = ' ';
1629                     psz_text2++;
1630                 }
1631                 break;
1632             case ' ':
1633             case '\t':
1634                 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1635                     break;
1636                 if( !i_comment )
1637                 {
1638                     *psz_text2 = ' ';
1639                     psz_text2++;
1640                 }
1641                 break;
1642             case '\\':
1643                 if( (*(psz_text + 1 ) ) == 'n' )
1644                 {
1645                     *psz_text2 = '\n';
1646                     psz_text++;
1647                     psz_text2++;
1648                     break;
1649                 }
1650                 if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1651                     ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1652                 {
1653                     psz_text++; psz_text++;
1654                     break;
1655                 }
1656                 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1657                     (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1658                     (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1659                     (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1660                 {
1661                     psz_text++;
1662                     break;
1663                 }
1664                 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1665                     (*(psz_text + 1 ) ) == '\\' )
1666                     psz_text++;
1667                 else if( *(psz_text + 1 ) == '\r' ||  *(psz_text + 1 ) == '\n'
1668                          ||  *(psz_text + 1 ) == '\0' )
1669                 {
1670                     char *s2 = TextGetLine( txt );
1671                     if( !s2 )
1672                         return VLC_EGENERIC;
1673
1674                     while ( *s2 == ' ' ) s2++;
1675
1676                     /* Here to parse the second line, we should add s2 to
1677                        psz_text and go on the for( ) line 1556 in order to
1678                        parse the next line.
1679                     */
1680                 }
1681             default:
1682                 if( !i_comment )
1683                 {
1684                     *psz_text2 = *psz_text;
1685                     psz_text2++;
1686                 }
1687             }
1688             psz_text++;
1689         }
1690
1691         p_subtitle->psz_text = psz_orig2;
1692         free( psz_orig );
1693         return VLC_SUCCESS;
1694     }
1695 }
1696
1697 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1698 {
1699     VLC_UNUSED( i_idx );
1700
1701     demux_sys_t *p_sys = p_demux->p_sys;
1702     text_t      *txt = &p_sys->txt;
1703     char *psz_text;
1704     int i;
1705
1706     for( ;; )
1707     {
1708         int h1, m1, s1;
1709         int h2, m2, s2;
1710         const char *s = TextGetLine( txt );
1711
1712         if( !s )
1713             return VLC_EGENERIC;
1714
1715         psz_text = malloc( strlen( s ) + 1 );
1716         if( !psz_text )
1717             return VLC_ENOMEM;
1718
1719         if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1720                     &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1721         {
1722             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1723                                     (int64_t)m1 * 60*1000 +
1724                                     (int64_t)s1 * 1000 ) * 1000;
1725             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1726                                     (int64_t)m2 * 60*1000 +
1727                                     (int64_t)s2 * 1000 ) * 1000;
1728             break;
1729         }
1730         free( psz_text );
1731     }
1732
1733     /* replace | by \n */
1734     for( i = 0; psz_text[i] != '\0'; i++ )
1735     {
1736         if( psz_text[i] == '|' )
1737             psz_text[i] = '\n';
1738     }
1739     p_subtitle->psz_text = psz_text;
1740     return VLC_SUCCESS;
1741 }
1742
1743