]> git.sesse.net Git - vlc/blob - modules/demux/subtitle.c
demux: avi: don't allow SET_TIME on non seekable
[vlc] / modules / demux / subtitle.c
1 /*****************************************************************************
2  * subtitle.c: Demux for subtitle text files.
3  *****************************************************************************
4  * Copyright (C) 1999-2007 VLC authors and VideoLAN
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 it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * 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_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_input.h>
37 #include <vlc_memory.h>
38
39 #include <ctype.h>
40
41 #include <vlc_demux.h>
42 #include <vlc_charset.h>
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open ( vlc_object_t *p_this );
48 static void Close( vlc_object_t *p_this );
49
50 #define SUB_DELAY_LONGTEXT \
51     N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
52 #define SUB_FPS_LONGTEXT \
53     N_("Override the normal frames per second settings. " \
54     "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
55 #define SUB_TYPE_LONGTEXT \
56     N_("Force the subtiles format. Selecting \"auto\" means autodetection and should always work.")
57 #define SUB_DESCRIPTION_LONGTEXT \
58     N_("Override the default track description.")
59
60 static const char *const ppsz_sub_type[] =
61 {
62     "auto", "microdvd", "subrip", "subviewer", "ssa1",
63     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
64     "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
65     "subviewer1","vtt"
66 };
67
68 vlc_module_begin ()
69     set_shortname( N_("Subtitles"))
70     set_description( N_("Text subtitle parser") )
71     set_capability( "demux", 0 )
72     set_category( CAT_INPUT )
73     set_subcategory( SUBCAT_INPUT_DEMUX )
74     add_float( "sub-fps", 0.0,
75                N_("Frames per Second"),
76                SUB_FPS_LONGTEXT, true )
77     add_integer( "sub-delay", 0,
78                N_("Subtitle delay"),
79                SUB_DELAY_LONGTEXT, true )
80     add_string( "sub-type", "auto", N_("Subtitle format"),
81                 SUB_TYPE_LONGTEXT, true )
82         change_string_list( ppsz_sub_type, ppsz_sub_type )
83     add_string( "sub-description", NULL, N_("Subtitle description"),
84                 SUB_DESCRIPTION_LONGTEXT, true )
85     set_callbacks( Open, Close )
86
87     add_shortcut( "subtitle" )
88 vlc_module_end ()
89
90 /*****************************************************************************
91  * Prototypes:
92  *****************************************************************************/
93 enum
94 {
95     SUB_TYPE_UNKNOWN = -1,
96     SUB_TYPE_MICRODVD,
97     SUB_TYPE_SUBRIP,
98     SUB_TYPE_SSA1,
99     SUB_TYPE_SSA2_4,
100     SUB_TYPE_ASS,
101     SUB_TYPE_VPLAYER,
102     SUB_TYPE_SAMI,
103     SUB_TYPE_SUBVIEWER, /* SUBVIEWER 2 */
104     SUB_TYPE_DVDSUBTITLE, /* Mplayer calls it subviewer2 */
105     SUB_TYPE_MPL2,
106     SUB_TYPE_AQT,
107     SUB_TYPE_PJS,
108     SUB_TYPE_MPSUB,
109     SUB_TYPE_JACOSUB,
110     SUB_TYPE_PSB,
111     SUB_TYPE_RT,
112     SUB_TYPE_DKS,
113     SUB_TYPE_SUBVIEW1, /* SUBVIEWER 1 - mplayer calls it subrip09,
114                          and Gnome subtitles SubViewer 1.0 */
115     SUB_TYPE_VTT
116 };
117
118 typedef struct
119 {
120     int     i_line_count;
121     int     i_line;
122     char    **line;
123 } text_t;
124
125 static int  TextLoad( text_t *, stream_t *s );
126 static void TextUnload( text_t * );
127
128 typedef struct
129 {
130     int64_t i_start;
131     int64_t i_stop;
132
133     char    *psz_text;
134 } subtitle_t;
135
136
137 struct demux_sys_t
138 {
139     int         i_type;
140     text_t      txt;
141     es_out_id_t *es;
142
143     int64_t     i_next_demux_date;
144     int64_t     i_microsecperframe;
145
146     char        *psz_header;
147     int         i_subtitle;
148     int         i_subtitles;
149     subtitle_t  *subtitle;
150
151     int64_t     i_length;
152
153     /* */
154     struct
155     {
156         bool b_inited;
157
158         int i_comment;
159         int i_time_resolution;
160         int i_time_shift;
161     } jss;
162     struct
163     {
164         bool  b_inited;
165
166         float f_total;
167         float f_factor;
168     } mpsub;
169 };
170
171 static int  ParseMicroDvd   ( demux_t *, subtitle_t *, int );
172 static int  ParseSubRip     ( demux_t *, subtitle_t *, int );
173 static int  ParseSubViewer  ( demux_t *, subtitle_t *, int );
174 static int  ParseSSA        ( demux_t *, subtitle_t *, int );
175 static int  ParseVplayer    ( demux_t *, subtitle_t *, int );
176 static int  ParseSami       ( demux_t *, subtitle_t *, int );
177 static int  ParseDVDSubtitle( demux_t *, subtitle_t *, int );
178 static int  ParseMPL2       ( demux_t *, subtitle_t *, int );
179 static int  ParseAQT        ( demux_t *, subtitle_t *, int );
180 static int  ParsePJS        ( demux_t *, subtitle_t *, int );
181 static int  ParseMPSub      ( demux_t *, subtitle_t *, int );
182 static int  ParseJSS        ( demux_t *, subtitle_t *, int );
183 static int  ParsePSB        ( demux_t *, subtitle_t *, int );
184 static int  ParseRealText   ( demux_t *, subtitle_t *, int );
185 static int  ParseDKS        ( demux_t *, subtitle_t *, int );
186 static int  ParseSubViewer1 ( demux_t *, subtitle_t *, int );
187 static int  ParseVTT        ( demux_t *, subtitle_t *, int );
188
189 static const struct
190 {
191     const char *psz_type_name;
192     int  i_type;
193     const char *psz_name;
194     int  (*pf_read)( demux_t *, subtitle_t*, int );
195 } sub_read_subtitle_function [] =
196 {
197     { "microdvd",   SUB_TYPE_MICRODVD,    "MicroDVD",    ParseMicroDvd },
198     { "subrip",     SUB_TYPE_SUBRIP,      "SubRIP",      ParseSubRip },
199     { "subviewer",  SUB_TYPE_SUBVIEWER,   "SubViewer",   ParseSubViewer },
200     { "ssa1",       SUB_TYPE_SSA1,        "SSA-1",       ParseSSA },
201     { "ssa2-4",     SUB_TYPE_SSA2_4,      "SSA-2/3/4",   ParseSSA },
202     { "ass",        SUB_TYPE_ASS,         "SSA/ASS",     ParseSSA },
203     { "vplayer",    SUB_TYPE_VPLAYER,     "VPlayer",     ParseVplayer },
204     { "sami",       SUB_TYPE_SAMI,        "SAMI",        ParseSami },
205     { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
206     { "mpl2",       SUB_TYPE_MPL2,        "MPL2",        ParseMPL2 },
207     { "aqt",        SUB_TYPE_AQT,         "AQTitle",     ParseAQT },
208     { "pjs",        SUB_TYPE_PJS,         "PhoenixSub",  ParsePJS },
209     { "mpsub",      SUB_TYPE_MPSUB,       "MPSub",       ParseMPSub },
210     { "jacosub",    SUB_TYPE_JACOSUB,     "JacoSub",     ParseJSS },
211     { "psb",        SUB_TYPE_PSB,         "PowerDivx",   ParsePSB },
212     { "realtext",   SUB_TYPE_RT,          "RealText",    ParseRealText },
213     { "dks",        SUB_TYPE_DKS,         "DKS",         ParseDKS },
214     { "subviewer1", SUB_TYPE_SUBVIEW1,    "Subviewer 1", ParseSubViewer1 },
215     { "text/vtt",   SUB_TYPE_VTT,         "WebVTT",      ParseVTT },
216     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
217 };
218 /* When adding support for more formats, be sure to add their file extension
219  * to src/input/subtitles.c to enable auto-detection.
220  */
221
222 static int Demux( demux_t * );
223 static int Control( demux_t *, int, va_list );
224
225 static void Fix( demux_t * );
226 static char * get_language_from_filename( const char * );
227
228 /*****************************************************************************
229  * Module initializer
230  *****************************************************************************/
231 static int Open ( vlc_object_t *p_this )
232 {
233     demux_t        *p_demux = (demux_t*)p_this;
234     demux_sys_t    *p_sys;
235     es_format_t    fmt;
236     float          f_fps;
237     char           *psz_type;
238     int  (*pf_read)( demux_t *, subtitle_t*, int );
239     int            i, i_max;
240
241     if( !p_demux->b_force )
242     {
243         msg_Dbg( p_demux, "subtitle demux discarded" );
244         return VLC_EGENERIC;
245     }
246
247     p_demux->pf_demux = Demux;
248     p_demux->pf_control = Control;
249     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
250     if( p_sys == NULL )
251         return VLC_ENOMEM;
252
253     p_sys->psz_header         = NULL;
254     p_sys->i_subtitle         = 0;
255     p_sys->i_subtitles        = 0;
256     p_sys->subtitle           = NULL;
257     p_sys->i_microsecperframe = 40000;
258
259     p_sys->jss.b_inited       = false;
260     p_sys->mpsub.b_inited     = false;
261
262     /* Get the FPS */
263     f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
264     if( f_fps >= 1.0 )
265         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
266
267     msg_Dbg( p_demux, "Movie fps: %f", f_fps );
268
269     /* Check for override of the fps */
270     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
271     if( f_fps >= 1.0 )
272     {
273         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
274         msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
275     }
276
277     /* Get or probe the type */
278     p_sys->i_type = SUB_TYPE_UNKNOWN;
279     psz_type = var_CreateGetString( p_demux, "sub-type" );
280     if( psz_type && *psz_type )
281     {
282         int i;
283
284         for( i = 0; ; i++ )
285         {
286             if( sub_read_subtitle_function[i].psz_type_name == NULL )
287                 break;
288
289             if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
290                          psz_type ) )
291             {
292                 p_sys->i_type = sub_read_subtitle_function[i].i_type;
293                 break;
294             }
295         }
296     }
297     free( psz_type );
298
299     /* Detect Unicode while skipping the UTF-8 Byte Order Mark */
300     bool unicode = false;
301     const uint8_t *p_data;
302     if( stream_Peek( p_demux->s, &p_data, 3 ) >= 3
303      && !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
304     {
305         unicode = true;
306         stream_Seek( p_demux->s, 3 ); /* skip BOM */
307         msg_Dbg( p_demux, "detected Unicode Byte Order Mark" );
308     }
309
310     /* Probe if unknown type */
311     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
312     {
313         int     i_try;
314         char    *s = NULL;
315
316         msg_Dbg( p_demux, "autodetecting subtitle format" );
317         for( i_try = 0; i_try < 256; i_try++ )
318         {
319             int i_dummy;
320             char p_dummy;
321
322             if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
323                 break;
324
325             if( strcasestr( s, "<SAMI>" ) )
326             {
327                 p_sys->i_type = SUB_TYPE_SAMI;
328                 break;
329             }
330             else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
331                      sscanf( s, "{%d}{}", &i_dummy ) == 1)
332             {
333                 p_sys->i_type = SUB_TYPE_MICRODVD;
334                 break;
335             }
336             else if( sscanf( s, "%d:%d:%d,%d --> %d:%d:%d,%d",
337                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
338                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
339                      sscanf( s, "%d:%d:%d --> %d:%d:%d,%d",
340                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
341                              &i_dummy,&i_dummy,&i_dummy ) == 7 ||
342                      sscanf( s, "%d:%d:%d,%d --> %d:%d:%d",
343                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
344                              &i_dummy,&i_dummy,&i_dummy ) == 7 ||
345                      sscanf( s, "%d:%d:%d.%d --> %d:%d:%d.%d",
346                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
347                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
348                      sscanf( s, "%d:%d:%d --> %d:%d:%d.%d",
349                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
350                              &i_dummy,&i_dummy,&i_dummy ) == 7 ||
351                      sscanf( s, "%d:%d:%d.%d --> %d:%d:%d",
352                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
353                              &i_dummy,&i_dummy,&i_dummy ) == 7 ||
354                      sscanf( s, "%d:%d:%d --> %d:%d:%d",
355                              &i_dummy,&i_dummy,&i_dummy,
356                              &i_dummy,&i_dummy,&i_dummy ) == 6 )
357             {
358                 p_sys->i_type = SUB_TYPE_SUBRIP;
359                 break;
360             }
361             else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
362             {
363                 p_sys->i_type = SUB_TYPE_SSA1;
364                 break;
365             }
366             else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
367             {
368                 p_sys->i_type = SUB_TYPE_ASS;
369                 break;
370             }
371             else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
372             {
373                 p_sys->i_type = SUB_TYPE_SSA2_4;
374                 break;
375             }
376             else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
377             {
378                 p_sys->i_type = SUB_TYPE_SSA2_4;
379                 break;
380             }
381             else if( !strncasecmp( s, "Dialogue:", 9  ) )
382             {
383                 p_sys->i_type = SUB_TYPE_ASS;
384                 break;
385             }
386             else if( strcasestr( s, "[INFORMATION]" ) )
387             {
388                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
389                 break;
390             }
391             else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
392                                  &i_dummy, &i_dummy, &i_dummy, &i_dummy,
393                                  &i_dummy, &i_dummy, &i_dummy ) == 7 ||
394                      sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
395             {
396                 p_sys->i_type = SUB_TYPE_JACOSUB;
397                 break;
398             }
399             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
400                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
401             {
402                 p_sys->i_type = SUB_TYPE_VPLAYER;
403                 break;
404             }
405             else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
406                              &i_dummy, &i_dummy ) == 4 )
407             {
408                 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
409                 break;
410             }
411             else if( sscanf( s, "[%d:%d:%d]%c",
412                      &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
413             {
414                 p_sys->i_type = SUB_TYPE_DKS;
415                 break;
416             }
417             else if( strstr( s, "*** START SCRIPT" ) )
418             {
419                 p_sys->i_type = SUB_TYPE_SUBVIEW1;
420                 break;
421             }
422             else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
423                      sscanf( s, "[%d][]", &i_dummy ) == 1)
424             {
425                 p_sys->i_type = SUB_TYPE_MPL2;
426                 break;
427             }
428             else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
429                      ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
430                        && p_dummy =='E' ) )
431             {
432                 p_sys->i_type = SUB_TYPE_MPSUB;
433                 break;
434             }
435             else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
436             {
437                 p_sys->i_type = SUB_TYPE_AQT;
438                 break;
439             }
440             else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
441             {
442                 p_sys->i_type = SUB_TYPE_PJS;
443                 break;
444             }
445             else if( sscanf( s, "{%d:%d:%d}",
446                                 &i_dummy, &i_dummy, &i_dummy ) == 3 )
447             {
448                 p_sys->i_type = SUB_TYPE_PSB;
449                 break;
450             }
451             else if( strcasestr( s, "<time" ) )
452             {
453                 p_sys->i_type = SUB_TYPE_RT;
454                 break;
455             }
456             else if( !strncasecmp( s, "WEBVTT",6 ) )
457             {
458                 p_sys->i_type = SUB_TYPE_VTT;
459                 break;
460             }
461
462             free( s );
463             s = NULL;
464         }
465
466         free( s );
467
468         /* It will nearly always work even for non seekable stream thanks the
469          * caching system, and if it fails we lose just a few sub */
470         if( stream_Seek( p_demux->s, unicode ? 3 : 0 ) )
471             msg_Warn( p_demux, "failed to rewind" );
472     }
473
474     /* Quit on unknown subtitles */
475     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
476     {
477         stream_Seek( p_demux->s, 0 );
478         msg_Warn( p_demux, "failed to recognize subtitle type" );
479         free( p_sys );
480         return VLC_EGENERIC;
481     }
482
483     for( i = 0; ; i++ )
484     {
485         if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
486         {
487             msg_Dbg( p_demux, "detected %s format",
488                      sub_read_subtitle_function[i].psz_name );
489             pf_read = sub_read_subtitle_function[i].pf_read;
490             break;
491         }
492     }
493
494     msg_Dbg( p_demux, "loading all subtitles..." );
495
496     /* Load the whole file */
497     TextLoad( &p_sys->txt, p_demux->s );
498
499     /* Parse it */
500     for( i_max = 0;; )
501     {
502         if( p_sys->i_subtitles >= i_max )
503         {
504             i_max += 500;
505             if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
506                                               sizeof(subtitle_t) * i_max ) ) )
507             {
508                 TextUnload( &p_sys->txt );
509                 free( p_sys );
510                 return VLC_ENOMEM;
511             }
512         }
513
514         if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
515                      p_sys->i_subtitles ) )
516             break;
517
518         p_sys->i_subtitles++;
519     }
520     /* Unload */
521     TextUnload( &p_sys->txt );
522
523     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
524
525     /* Fix subtitle (order and time) *** */
526     p_sys->i_subtitle = 0;
527     p_sys->i_length = 0;
528     if( p_sys->i_subtitles > 0 )
529     {
530         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
531         /* +1 to avoid 0 */
532         if( p_sys->i_length <= 0 )
533             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
534     }
535
536     /* *** add subtitle ES *** */
537     if( p_sys->i_type == SUB_TYPE_SSA1 ||
538              p_sys->i_type == SUB_TYPE_SSA2_4 ||
539              p_sys->i_type == SUB_TYPE_ASS )
540     {
541         Fix( p_demux );
542         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
543     }
544     else
545         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
546
547     /* Stupid language detection in the filename */
548     char * psz_language = get_language_from_filename( p_demux->psz_file );
549
550     if( psz_language )
551     {
552         fmt.psz_language = psz_language;
553         msg_Dbg( p_demux, "detected language %s of subtitle: %s", psz_language,
554                  p_demux->psz_location );
555     }
556
557     if( unicode )
558         fmt.subs.psz_encoding = strdup( "UTF-8" );
559     char *psz_description = var_InheritString( p_demux, "sub-description" );
560     if( psz_description && *psz_description )
561         fmt.psz_description = psz_description;
562     else
563         free( psz_description );
564     if( p_sys->psz_header != NULL )
565     {
566         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
567         fmt.p_extra = strdup( p_sys->psz_header );
568     }
569     p_sys->es = es_out_Add( p_demux->out, &fmt );
570     es_format_Clean( &fmt );
571
572     return VLC_SUCCESS;
573 }
574
575 /*****************************************************************************
576  * Close: Close subtitle demux
577  *****************************************************************************/
578 static void Close( vlc_object_t *p_this )
579 {
580     demux_t *p_demux = (demux_t*)p_this;
581     demux_sys_t *p_sys = p_demux->p_sys;
582     int i;
583
584     for( i = 0; i < p_sys->i_subtitles; i++ )
585         free( p_sys->subtitle[i].psz_text );
586     free( p_sys->subtitle );
587
588     free( p_sys );
589 }
590
591 /*****************************************************************************
592  * Control:
593  *****************************************************************************/
594 static int Control( demux_t *p_demux, int i_query, va_list args )
595 {
596     demux_sys_t *p_sys = p_demux->p_sys;
597     int64_t *pi64, i64;
598     double *pf, f;
599
600     switch( i_query )
601     {
602         case DEMUX_GET_LENGTH:
603             pi64 = (int64_t*)va_arg( args, int64_t * );
604             *pi64 = p_sys->i_length;
605             return VLC_SUCCESS;
606
607         case DEMUX_GET_TIME:
608             pi64 = (int64_t*)va_arg( args, int64_t * );
609             if( p_sys->i_subtitle < p_sys->i_subtitles )
610             {
611                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
612                 return VLC_SUCCESS;
613             }
614             return VLC_EGENERIC;
615
616         case DEMUX_SET_TIME:
617             i64 = (int64_t)va_arg( args, int64_t );
618             p_sys->i_subtitle = 0;
619             while( p_sys->i_subtitle < p_sys->i_subtitles )
620             {
621                 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
622
623                 if( p_subtitle->i_start > i64 )
624                     break;
625                 if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
626                     break;
627
628                 p_sys->i_subtitle++;
629             }
630
631             if( p_sys->i_subtitle >= p_sys->i_subtitles )
632                 return VLC_EGENERIC;
633             return VLC_SUCCESS;
634
635         case DEMUX_GET_POSITION:
636             pf = (double*)va_arg( args, double * );
637             if( p_sys->i_subtitle >= p_sys->i_subtitles )
638             {
639                 *pf = 1.0;
640             }
641             else if( p_sys->i_subtitles > 0 )
642             {
643                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
644                       (double)p_sys->i_length;
645             }
646             else
647             {
648                 *pf = 0.0;
649             }
650             return VLC_SUCCESS;
651
652         case DEMUX_SET_POSITION:
653             f = (double)va_arg( args, double );
654             i64 = f * p_sys->i_length;
655
656             p_sys->i_subtitle = 0;
657             while( p_sys->i_subtitle < p_sys->i_subtitles &&
658                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
659             {
660                 p_sys->i_subtitle++;
661             }
662             if( p_sys->i_subtitle >= p_sys->i_subtitles )
663                 return VLC_EGENERIC;
664             return VLC_SUCCESS;
665
666         case DEMUX_SET_NEXT_DEMUX_TIME:
667             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
668             return VLC_SUCCESS;
669
670         case DEMUX_GET_PTS_DELAY:
671         case DEMUX_GET_FPS:
672         case DEMUX_GET_META:
673         case DEMUX_GET_ATTACHMENTS:
674         case DEMUX_GET_TITLE_INFO:
675         case DEMUX_HAS_UNSUPPORTED_META:
676         case DEMUX_CAN_RECORD:
677             return VLC_EGENERIC;
678
679         default:
680             msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
681             return VLC_EGENERIC;
682     }
683 }
684
685 /*****************************************************************************
686  * Demux: Send subtitle to decoder
687  *****************************************************************************/
688 static int Demux( demux_t *p_demux )
689 {
690     demux_sys_t *p_sys = p_demux->p_sys;
691     int64_t i_maxdate;
692
693     if( p_sys->i_subtitle >= p_sys->i_subtitles )
694         return 0;
695
696     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
697     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
698     {
699         /* Should not happen */
700         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
701     }
702
703     while( p_sys->i_subtitle < p_sys->i_subtitles &&
704            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
705     {
706         const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
707
708         block_t *p_block;
709         int i_len = strlen( p_subtitle->psz_text ) + 1;
710
711         if( i_len <= 1 || p_subtitle->i_start < 0 )
712         {
713             p_sys->i_subtitle++;
714             continue;
715         }
716
717         if( ( p_block = block_Alloc( i_len ) ) == NULL )
718         {
719             p_sys->i_subtitle++;
720             continue;
721         }
722
723         p_block->i_dts =
724         p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
725         if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
726             p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
727
728         memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
729
730         es_out_Send( p_demux->out, p_sys->es, p_block );
731
732         p_sys->i_subtitle++;
733     }
734
735     /* */
736     p_sys->i_next_demux_date = 0;
737
738     return 1;
739 }
740
741 /*****************************************************************************
742  * Fix: fix time stamp and order of subtitle
743  *****************************************************************************/
744 static void Fix( demux_t *p_demux )
745 {
746     demux_sys_t *p_sys = p_demux->p_sys;
747     bool b_done;
748
749     /* *** fix order (to be sure...) *** */
750     /* We suppose that there are near in order and this durty bubble sort
751      * would not take too much time
752      */
753     do
754     {
755         b_done = true;
756         for( int i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
757         {
758             if( p_sys->subtitle[i_index].i_start <
759                 p_sys->subtitle[i_index - 1].i_start )
760             {
761                 subtitle_t sub_xch;
762                 memcpy( &sub_xch,
763                         p_sys->subtitle + i_index - 1,
764                         sizeof( subtitle_t ) );
765                 memcpy( p_sys->subtitle + i_index - 1,
766                         p_sys->subtitle + i_index,
767                         sizeof( subtitle_t ) );
768                 memcpy( p_sys->subtitle + i_index,
769                         &sub_xch,
770                         sizeof( subtitle_t ) );
771                 b_done = false;
772             }
773         }
774     } while( !b_done );
775 }
776
777 static int TextLoad( text_t *txt, stream_t *s )
778 {
779     int   i_line_max;
780
781     /* init txt */
782     i_line_max          = 500;
783     txt->i_line_count   = 0;
784     txt->i_line         = 0;
785     txt->line           = calloc( i_line_max, sizeof( char * ) );
786     if( !txt->line )
787         return VLC_ENOMEM;
788
789     /* load the complete file */
790     for( ;; )
791     {
792         char *psz = stream_ReadLine( s );
793
794         if( psz == NULL )
795             break;
796
797         txt->line[txt->i_line_count++] = psz;
798         if( txt->i_line_count >= i_line_max )
799         {
800             i_line_max += 100;
801             txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
802             if( !txt->line )
803                 return VLC_ENOMEM;
804         }
805     }
806
807     if( txt->i_line_count <= 0 )
808     {
809         free( txt->line );
810         return VLC_EGENERIC;
811     }
812
813     return VLC_SUCCESS;
814 }
815 static void TextUnload( text_t *txt )
816 {
817     int i;
818
819     for( i = 0; i < txt->i_line_count; i++ )
820     {
821         free( txt->line[i] );
822     }
823     free( txt->line );
824     txt->i_line       = 0;
825     txt->i_line_count = 0;
826 }
827
828 static char *TextGetLine( text_t *txt )
829 {
830     if( txt->i_line >= txt->i_line_count )
831         return( NULL );
832
833     return txt->line[txt->i_line++];
834 }
835 static void TextPreviousLine( text_t *txt )
836 {
837     if( txt->i_line > 0 )
838         txt->i_line--;
839 }
840
841 /*****************************************************************************
842  * Specific Subtitle function
843  *****************************************************************************/
844 /* ParseMicroDvd:
845  *  Format:
846  *      {n1}{n2}Line1|Line2|Line3....
847  *  where n1 and n2 are the video frame number (n2 can be empty)
848  */
849 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
850                           int i_idx )
851 {
852     VLC_UNUSED( i_idx );
853     demux_sys_t *p_sys = p_demux->p_sys;
854     text_t      *txt = &p_sys->txt;
855     char *psz_text;
856     int  i_start;
857     int  i_stop;
858     int  i;
859
860     for( ;; )
861     {
862         const char *s = TextGetLine( txt );
863         if( !s )
864             return VLC_EGENERIC;
865
866         psz_text = malloc( strlen(s) + 1 );
867         if( !psz_text )
868             return VLC_ENOMEM;
869
870         i_start = 0;
871         i_stop  = -1;
872         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
873             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
874         {
875             float f_fps;
876             if( i_start != 1 || i_stop != 1 )
877                 break;
878
879             /* We found a possible setting of the framerate "{1}{1}23.976" */
880             /* Check if it's usable, and if the sub-fps is not set */
881             f_fps = us_strtod( psz_text, NULL );
882             if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
883                 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
884         }
885         free( psz_text );
886     }
887
888     /* replace | by \n */
889     for( i = 0; psz_text[i] != '\0'; i++ )
890     {
891         if( psz_text[i] == '|' )
892             psz_text[i] = '\n';
893     }
894
895     /* */
896     p_subtitle->i_start  = i_start * p_sys->i_microsecperframe;
897     p_subtitle->i_stop   = i_stop >= 0 ? (i_stop  * p_sys->i_microsecperframe) : -1;
898     p_subtitle->psz_text = psz_text;
899     return VLC_SUCCESS;
900 }
901
902 /* ParseSubRipSubViewer
903  *  Format SubRip
904  *      n
905  *      h1:m1:s1,d1 --> h2:m2:s2,d2
906  *      Line1
907  *      Line2
908  *      ....
909  *      [Empty line]
910  *  Format SubViewer v1/v2
911  *      h1:m1:s1.d1,h2:m2:s2.d2
912  *      Line1[br]Line2
913  *      Line3
914  *      ...
915  *      [empty line]
916  *  We ignore line number for SubRip
917  */
918 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
919                                  int (* pf_parse_timing)(subtitle_t *, const char *),
920                                  bool b_replace_br )
921 {
922     demux_sys_t *p_sys = p_demux->p_sys;
923     text_t      *txt = &p_sys->txt;
924     char    *psz_text;
925
926     for( ;; )
927     {
928         const char *s = TextGetLine( txt );
929
930         if( !s )
931             return VLC_EGENERIC;
932
933         if( pf_parse_timing( p_subtitle, s) == VLC_SUCCESS &&
934             p_subtitle->i_start < p_subtitle->i_stop )
935         {
936             break;
937         }
938     }
939
940     /* Now read text until an empty line */
941     psz_text = strdup("");
942     if( !psz_text )
943         return VLC_ENOMEM;
944
945     for( ;; )
946     {
947         const char *s = TextGetLine( txt );
948         int i_len;
949         int i_old;
950
951         i_len = s ? strlen( s ) : 0;
952         if( i_len <= 0 )
953         {
954             p_subtitle->psz_text = psz_text;
955             return VLC_SUCCESS;
956         }
957
958         i_old = strlen( psz_text );
959         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
960         if( !psz_text )
961         {
962             return VLC_ENOMEM;
963         }
964         strcat( psz_text, s );
965         strcat( psz_text, "\n" );
966
967         /* replace [br] by \n */
968         if( b_replace_br )
969         {
970             char *p;
971
972             while( ( p = strstr( psz_text, "[br]" ) ) )
973             {
974                 *p++ = '\n';
975                 memmove( p, &p[3], strlen(&p[3])+1 );
976             }
977         }
978     }
979 }
980
981 /* subtitle_ParseSubRipTimingValue
982  * Parses SubRip timing value.
983  */
984 static int subtitle_ParseSubRipTimingValue(int64_t *timing_value,
985                                            const char *s)
986 {
987     int h1, m1, s1, d1 = 0;
988
989     if ( sscanf( s, "%d:%d:%d,%d",
990                  &h1, &m1, &s1, &d1 ) == 4 ||
991          sscanf( s, "%d:%d:%d.%d",
992                  &h1, &m1, &s1, &d1 ) == 4 ||
993          sscanf( s, "%d:%d:%d",
994                  &h1, &m1, &s1) == 3 )
995     {
996         (*timing_value) = ( (int64_t)h1 * 3600 * 1000 +
997                             (int64_t)m1 * 60 * 1000 +
998                             (int64_t)s1 * 1000 +
999                             (int64_t)d1 ) * 1000;
1000
1001         return VLC_SUCCESS;
1002     }
1003
1004     return VLC_EGENERIC;
1005 }
1006
1007 /* subtitle_ParseSubRipTiming
1008  * Parses SubRip timing.
1009  */
1010 static int subtitle_ParseSubRipTiming( subtitle_t *p_subtitle,
1011                                        const char *s )
1012 {
1013     int i_result = VLC_EGENERIC;
1014     char *psz_start, *psz_stop;
1015     psz_start = malloc( strlen(s) + 1 );
1016     psz_stop = malloc( strlen(s) + 1 );
1017
1018     if( sscanf( s, "%s --> %s", psz_start, psz_stop) == 2 &&
1019         subtitle_ParseSubRipTimingValue( &p_subtitle->i_start, psz_start ) == VLC_SUCCESS &&
1020         subtitle_ParseSubRipTimingValue( &p_subtitle->i_stop,  psz_stop ) == VLC_SUCCESS )
1021     {
1022         i_result = VLC_SUCCESS;
1023     }
1024
1025     free(psz_start);
1026     free(psz_stop);
1027
1028     return i_result;
1029 }
1030 /* ParseSubRip
1031  */
1032 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
1033                          int i_idx )
1034 {
1035     VLC_UNUSED( i_idx );
1036     return ParseSubRipSubViewer( p_demux, p_subtitle,
1037                                  &subtitle_ParseSubRipTiming,
1038                                  false );
1039 }
1040
1041 /* subtitle_ParseSubViewerTiming
1042  * Parses SubViewer timing.
1043  */
1044 static int subtitle_ParseSubViewerTiming( subtitle_t *p_subtitle,
1045                                    const char *s )
1046 {
1047     int h1, m1, s1, d1, h2, m2, s2, d2;
1048
1049     if( sscanf( s, "%d:%d:%d.%d,%d:%d:%d.%d",
1050                 &h1, &m1, &s1, &d1, &h2, &m2, &s2, &d2) == 8 )
1051     {
1052         p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1053                                 (int64_t)m1 * 60*1000 +
1054                                 (int64_t)s1 * 1000 +
1055                                 (int64_t)d1 ) * 1000;
1056
1057         p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1058                                 (int64_t)m2 * 60*1000 +
1059                                 (int64_t)s2 * 1000 +
1060                                 (int64_t)d2 ) * 1000;
1061         return VLC_SUCCESS;
1062     }
1063     return VLC_EGENERIC;
1064 }
1065
1066 /* ParseSubViewer
1067  */
1068 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
1069                             int i_idx )
1070 {
1071     VLC_UNUSED( i_idx );
1072
1073     return ParseSubRipSubViewer( p_demux, p_subtitle,
1074                                  &subtitle_ParseSubViewerTiming,
1075                                  true );
1076 }
1077
1078 /* ParseSSA
1079  */
1080 static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
1081                       int i_idx )
1082 {
1083     demux_sys_t *p_sys = p_demux->p_sys;
1084     text_t      *txt = &p_sys->txt;
1085
1086     for( ;; )
1087     {
1088         const char *s = TextGetLine( txt );
1089         int h1, m1, s1, c1, h2, m2, s2, c2;
1090         char *psz_text, *psz_temp;
1091         char temp[16];
1092
1093         if( !s )
1094             return VLC_EGENERIC;
1095
1096         /* We expect (SSA2-4):
1097          * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1098          * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1099          *
1100          * 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.
1101          */
1102
1103         /* For ASS:
1104          * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1105          * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1106          */
1107
1108         /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1109         psz_text = malloc( strlen(s) );
1110         if( !psz_text )
1111             return VLC_ENOMEM;
1112
1113         if( sscanf( s,
1114                     "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1115                     temp,
1116                     &h1, &m1, &s1, &c1,
1117                     &h2, &m2, &s2, &c2,
1118                     psz_text ) == 10 )
1119         {
1120             /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1121             /* (Layer comes from ASS specs ... it's empty for SSA.) */
1122             if( p_sys->i_type == SUB_TYPE_SSA1 )
1123             {
1124                 /* SSA1 has only 8 commas before the text starts, not 9 */
1125                 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1126                 psz_text[0] = ',';
1127             }
1128             else
1129             {
1130                 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1131
1132                 /* ReadOrder, Layer, %s(rest of fields) */
1133                 if( asprintf( &psz_temp, "%d,%d,%s", i_idx, i_layer, psz_text ) == -1 )
1134                 {
1135                     free( psz_text );
1136                     return VLC_ENOMEM;
1137                 }
1138
1139                 free( psz_text );
1140                 psz_text = psz_temp;
1141             }
1142
1143             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1144                                     (int64_t)m1 * 60*1000 +
1145                                     (int64_t)s1 * 1000 +
1146                                     (int64_t)c1 * 10 ) * 1000;
1147             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1148                                     (int64_t)m2 * 60*1000 +
1149                                     (int64_t)s2 * 1000 +
1150                                     (int64_t)c2 * 10 ) * 1000;
1151             p_subtitle->psz_text = psz_text;
1152             return VLC_SUCCESS;
1153         }
1154         free( psz_text );
1155
1156         /* All the other stuff we add to the header field */
1157         char *psz_header;
1158         if( asprintf( &psz_header, "%s%s\n",
1159                        p_sys->psz_header ? p_sys->psz_header : "", s ) == -1 )
1160             return VLC_ENOMEM;
1161         p_sys->psz_header = psz_header;
1162     }
1163 }
1164
1165 /* ParseVplayer
1166  *  Format
1167  *      h:m:s:Line1|Line2|Line3....
1168  *  or
1169  *      h:m:s Line1|Line2|Line3....
1170  */
1171 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1172                           int i_idx )
1173 {
1174     VLC_UNUSED( i_idx );
1175
1176     demux_sys_t *p_sys = p_demux->p_sys;
1177     text_t      *txt = &p_sys->txt;
1178     char *psz_text;
1179     int i;
1180
1181     for( ;; )
1182     {
1183         const char *s = TextGetLine( txt );
1184         int h1, m1, s1;
1185
1186         if( !s )
1187             return VLC_EGENERIC;
1188
1189         psz_text = malloc( strlen( s ) + 1 );
1190         if( !psz_text )
1191             return VLC_ENOMEM;
1192
1193         if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1194                     &h1, &m1, &s1, psz_text ) == 4 )
1195         {
1196             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1197                                     (int64_t)m1 * 60*1000 +
1198                                     (int64_t)s1 * 1000 ) * 1000;
1199             p_subtitle->i_stop  = -1;
1200             break;
1201         }
1202         free( psz_text );
1203     }
1204
1205     /* replace | by \n */
1206     for( i = 0; psz_text[i] != '\0'; i++ )
1207     {
1208         if( psz_text[i] == '|' )
1209             psz_text[i] = '\n';
1210     }
1211     p_subtitle->psz_text = psz_text;
1212     return VLC_SUCCESS;
1213 }
1214
1215 /* ParseSami
1216  */
1217 static char *ParseSamiSearch( text_t *txt,
1218                               char *psz_start, const char *psz_str )
1219 {
1220     if( psz_start && strcasestr( psz_start, psz_str ) )
1221     {
1222         char *s = strcasestr( psz_start, psz_str );
1223         return &s[strlen( psz_str )];
1224     }
1225
1226     for( ;; )
1227     {
1228         char *p = TextGetLine( txt );
1229         if( !p )
1230             return NULL;
1231
1232         if( strcasestr( p, psz_str ) )
1233         {
1234             char *s = strcasestr( p, psz_str );
1235             return &s[strlen( psz_str )];
1236         }
1237     }
1238 }
1239 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1240 {
1241     VLC_UNUSED( i_idx );
1242     demux_sys_t *p_sys = p_demux->p_sys;
1243     text_t      *txt = &p_sys->txt;
1244
1245     char *s;
1246     int64_t i_start;
1247
1248     unsigned int i_text;
1249     char text[8192]; /* Arbitrary but should be long enough */
1250
1251     /* search "Start=" */
1252     if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1253         return VLC_EGENERIC;
1254
1255     /* get start value */
1256     i_start = strtol( s, &s, 0 );
1257
1258     /* search <P */
1259     if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1260         return VLC_EGENERIC;
1261
1262     /* search > */
1263     if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1264         return VLC_EGENERIC;
1265
1266     i_text = 0;
1267     text[0] = '\0';
1268     /* now get all txt until  a "Start=" line */
1269     for( ;; )
1270     {
1271         char c = '\0';
1272         /* Search non empty line */
1273         while( s && *s == '\0' )
1274             s = TextGetLine( txt );
1275         if( !s )
1276             break;
1277
1278         if( *s == '<' )
1279         {
1280             if( !strncasecmp( s, "<br", 3 ) )
1281             {
1282                 c = '\n';
1283             }
1284             else if( strcasestr( s, "Start=" ) )
1285             {
1286                 TextPreviousLine( txt );
1287                 break;
1288             }
1289             s = ParseSamiSearch( txt, s, ">" );
1290         }
1291         else if( !strncmp( s, "&nbsp;", 6 ) )
1292         {
1293             c = ' ';
1294             s += 6;
1295         }
1296         else if( *s == '\t' )
1297         {
1298             c = ' ';
1299             s++;
1300         }
1301         else
1302         {
1303             c = *s;
1304             s++;
1305         }
1306         if( c != '\0' && i_text+1 < sizeof(text) )
1307         {
1308             text[i_text++] = c;
1309             text[i_text] = '\0';
1310         }
1311     }
1312
1313     p_subtitle->i_start = i_start * 1000;
1314     p_subtitle->i_stop  = -1;
1315     p_subtitle->psz_text = strdup( text );
1316
1317     return VLC_SUCCESS;
1318 }
1319
1320 /* ParseDVDSubtitle
1321  *  Format
1322  *      {T h1:m1:s1:c1
1323  *      Line1
1324  *      Line2
1325  *      ...
1326  *      }
1327  * TODO it can have a header
1328  *      { HEAD
1329  *          ...
1330  *          CODEPAGE=...
1331  *          FORMAT=...
1332  *          LANG=English
1333  *      }
1334  *      LANG support would be cool
1335  *      CODEPAGE is probably mandatory FIXME
1336  */
1337 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1338                              int i_idx )
1339 {
1340     VLC_UNUSED( i_idx );
1341
1342     demux_sys_t *p_sys = p_demux->p_sys;
1343     text_t      *txt = &p_sys->txt;
1344     char *psz_text;
1345
1346     for( ;; )
1347     {
1348         const char *s = TextGetLine( txt );
1349         int h1, m1, s1, c1;
1350
1351         if( !s )
1352             return VLC_EGENERIC;
1353
1354         if( sscanf( s,
1355                     "{T %d:%d:%d:%d",
1356                     &h1, &m1, &s1, &c1 ) == 4 )
1357         {
1358             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1359                                     (int64_t)m1 * 60*1000 +
1360                                     (int64_t)s1 * 1000 +
1361                                     (int64_t)c1 * 10) * 1000;
1362             p_subtitle->i_stop = -1;
1363             break;
1364         }
1365     }
1366
1367     /* Now read text until a line containing "}" */
1368     psz_text = strdup("");
1369     if( !psz_text )
1370         return VLC_ENOMEM;
1371     for( ;; )
1372     {
1373         const char *s = TextGetLine( txt );
1374         int i_len;
1375         int i_old;
1376
1377         if( !s )
1378         {
1379             free( psz_text );
1380             return VLC_EGENERIC;
1381         }
1382
1383         i_len = strlen( s );
1384         if( i_len == 1 && s[0] == '}')
1385         {
1386             p_subtitle->psz_text = psz_text;
1387             return VLC_SUCCESS;
1388         }
1389
1390         i_old = strlen( psz_text );
1391         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1392         if( !psz_text )
1393             return VLC_ENOMEM;
1394         strcat( psz_text, s );
1395         strcat( psz_text, "\n" );
1396     }
1397 }
1398
1399 /* ParseMPL2
1400  *  Format
1401  *     [n1][n2]Line1|Line2|Line3...
1402  *  where n1 and n2 are the video frame number (n2 can be empty)
1403  */
1404 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1405 {
1406     VLC_UNUSED( i_idx );
1407
1408     demux_sys_t *p_sys = p_demux->p_sys;
1409     text_t      *txt = &p_sys->txt;
1410     char *psz_text;
1411     int i;
1412
1413     for( ;; )
1414     {
1415         const char *s = TextGetLine( txt );
1416         int i_start;
1417         int i_stop;
1418
1419         if( !s )
1420             return VLC_EGENERIC;
1421
1422         psz_text = malloc( strlen(s) + 1 );
1423         if( !psz_text )
1424             return VLC_ENOMEM;
1425
1426         i_start = 0;
1427         i_stop  = -1;
1428         if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1429             sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1430         {
1431             p_subtitle->i_start = (int64_t)i_start * 100000;
1432             p_subtitle->i_stop  = i_stop >= 0 ? ((int64_t)i_stop  * 100000) : -1;
1433             break;
1434         }
1435         free( psz_text );
1436     }
1437
1438     for( i = 0; psz_text[i] != '\0'; )
1439     {
1440         /* replace | by \n */
1441         if( psz_text[i] == '|' )
1442             psz_text[i] = '\n';
1443
1444         /* Remove italic */
1445         if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1446             memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1447         else
1448             i++;
1449     }
1450     p_subtitle->psz_text = psz_text;
1451     return VLC_SUCCESS;
1452 }
1453
1454 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1455 {
1456     VLC_UNUSED( i_idx );
1457
1458     demux_sys_t *p_sys = p_demux->p_sys;
1459     text_t      *txt = &p_sys->txt;
1460     char *psz_text = strdup( "" );
1461     int i_old = 0;
1462     int i_firstline = 1;
1463
1464     for( ;; )
1465     {
1466         int t; /* Time */
1467
1468         const char *s = TextGetLine( txt );
1469
1470         if( !s )
1471         {
1472             free( psz_text );
1473             return VLC_EGENERIC;
1474         }
1475
1476         /* Data Lines */
1477         if( sscanf (s, "-->> %d", &t) == 1)
1478         {
1479             p_subtitle->i_start = (int64_t)t; /* * FPS*/
1480             p_subtitle->i_stop  = -1;
1481
1482             /* Starting of a subtitle */
1483             if( i_firstline )
1484             {
1485                 i_firstline = 0;
1486             }
1487             /* We have been too far: end of the subtitle, begin of next */
1488             else
1489             {
1490                 TextPreviousLine( txt );
1491                 break;
1492             }
1493         }
1494         /* Text Lines */
1495         else
1496         {
1497             i_old = strlen( psz_text ) + 1;
1498             psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1499             if( !psz_text )
1500                  return VLC_ENOMEM;
1501             strcat( psz_text, s );
1502             strcat( psz_text, "\n" );
1503             if( txt->i_line == txt->i_line_count )
1504                 break;
1505         }
1506     }
1507     p_subtitle->psz_text = psz_text;
1508     return VLC_SUCCESS;
1509 }
1510
1511 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1512 {
1513     VLC_UNUSED( i_idx );
1514
1515     demux_sys_t *p_sys = p_demux->p_sys;
1516     text_t      *txt = &p_sys->txt;
1517     char *psz_text;
1518     int i;
1519
1520     for( ;; )
1521     {
1522         const char *s = TextGetLine( txt );
1523         int t1, t2;
1524
1525         if( !s )
1526             return VLC_EGENERIC;
1527
1528         psz_text = malloc( strlen(s) + 1 );
1529         if( !psz_text )
1530             return VLC_ENOMEM;
1531
1532         /* Data Lines */
1533         if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1534         {
1535             /* 1/10th of second ? Frame based ? FIXME */
1536             p_subtitle->i_start = 10 * t1;
1537             p_subtitle->i_stop = 10 * t2;
1538             /* Remove latest " */
1539             psz_text[ strlen(psz_text) - 1 ] = '\0';
1540
1541             break;
1542         }
1543         free( psz_text );
1544     }
1545
1546     /* replace | by \n */
1547     for( i = 0; psz_text[i] != '\0'; i++ )
1548     {
1549         if( psz_text[i] == '|' )
1550             psz_text[i] = '\n';
1551     }
1552
1553     p_subtitle->psz_text = psz_text;
1554     msg_Dbg( p_demux, "%s", psz_text );
1555     return VLC_SUCCESS;
1556 }
1557
1558 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1559 {
1560     VLC_UNUSED( i_idx );
1561
1562     demux_sys_t *p_sys = p_demux->p_sys;
1563     text_t      *txt = &p_sys->txt;
1564     char *psz_text = strdup( "" );
1565
1566     if( !p_sys->mpsub.b_inited )
1567     {
1568         p_sys->mpsub.f_total = 0.0;
1569         p_sys->mpsub.f_factor = 0.0;
1570
1571         p_sys->mpsub.b_inited = true;
1572     }
1573
1574     for( ;; )
1575     {
1576         float f1, f2;
1577         char p_dummy;
1578         char *psz_temp;
1579
1580         const char *s = TextGetLine( txt );
1581         if( !s )
1582         {
1583             free( psz_text );
1584             return VLC_EGENERIC;
1585         }
1586
1587         if( strstr( s, "FORMAT" ) )
1588         {
1589             if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1590             {
1591                 p_sys->mpsub.f_factor = 100.0;
1592                 break;
1593             }
1594
1595             psz_temp = malloc( strlen(s) );
1596             if( !psz_temp )
1597             {
1598                 free( psz_text );
1599                 return VLC_ENOMEM;
1600             }
1601
1602             if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1603             {
1604                 float f_fps;
1605                 f_fps = us_strtod( psz_temp, NULL );
1606                 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1607                     var_SetFloat( p_demux, "sub-fps", f_fps );
1608
1609                 p_sys->mpsub.f_factor = 1.0;
1610                 free( psz_temp );
1611                 break;
1612             }
1613             free( psz_temp );
1614         }
1615         /* Data Lines */
1616         f1 = us_strtod( s, &psz_temp );
1617         if( *psz_temp )
1618         {
1619             f2 = us_strtod( psz_temp, NULL );
1620             p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
1621             p_subtitle->i_start = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1622             p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
1623             p_subtitle->i_stop = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1624             break;
1625         }
1626     }
1627
1628     for( ;; )
1629     {
1630         const char *s = TextGetLine( txt );
1631
1632         if( !s )
1633         {
1634             free( psz_text );
1635             return VLC_EGENERIC;
1636         }
1637
1638         int i_len = strlen( s );
1639         if( i_len == 0 )
1640             break;
1641
1642         int i_old = strlen( psz_text );
1643
1644         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1645         if( !psz_text )
1646              return VLC_ENOMEM;
1647
1648         strcat( psz_text, s );
1649         strcat( psz_text, "\n" );
1650     }
1651
1652     p_subtitle->psz_text = psz_text;
1653     return VLC_SUCCESS;
1654 }
1655
1656 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1657 {
1658     VLC_UNUSED( i_idx );
1659
1660     demux_sys_t  *p_sys = p_demux->p_sys;
1661     text_t       *txt = &p_sys->txt;
1662     char         *psz_text, *psz_orig;
1663     char         *psz_text2, *psz_orig2;
1664     int h1, h2, m1, m2, s1, s2, f1, f2;
1665
1666     if( !p_sys->jss.b_inited )
1667     {
1668         p_sys->jss.i_comment = 0;
1669         p_sys->jss.i_time_resolution = 30;
1670         p_sys->jss.i_time_shift = 0;
1671
1672         p_sys->jss.b_inited = true;
1673     }
1674
1675     /* Parse the main lines */
1676     for( ;; )
1677     {
1678         const char *s = TextGetLine( txt );
1679         if( !s )
1680             return VLC_EGENERIC;
1681
1682         psz_orig = malloc( strlen( s ) + 1 );
1683         if( !psz_orig )
1684             return VLC_ENOMEM;
1685         psz_text = psz_orig;
1686
1687         /* Complete time lines */
1688         if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1689                     &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1690         {
1691             p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1692                 (int64_t)( ( f1 +  p_sys->jss.i_time_shift ) /  p_sys->jss.i_time_resolution ) )
1693                 * 1000000;
1694             p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1695                 (int64_t)( ( f2 +  p_sys->jss.i_time_shift ) /  p_sys->jss.i_time_resolution ) )
1696                 * 1000000;
1697             break;
1698         }
1699         /* Short time lines */
1700         else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1701         {
1702             p_subtitle->i_start = (int64_t)(
1703                     ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1704             p_subtitle->i_stop = (int64_t)(
1705                     ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1706             break;
1707         }
1708         /* General Directive lines */
1709         /* Only TIME and SHIFT are supported so far */
1710         else if( s[0] == '#' )
1711         {
1712             int h = 0, m =0, sec = 1, f = 1;
1713             unsigned shift = 1;
1714             int inv = 1;
1715
1716             strcpy( psz_text, s );
1717
1718             switch( toupper( (unsigned char)psz_text[1] ) )
1719             {
1720             case 'S':
1721                  shift = isalpha( (unsigned char)psz_text[2] ) ? 6 : 2 ;
1722
1723                  if( sscanf( &psz_text[shift], "%d", &h ) )
1724                  {
1725                      /* Negative shifting */
1726                      if( h < 0 )
1727                      {
1728                          h *= -1;
1729                          inv = -1;
1730                      }
1731
1732                      if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1733                      {
1734                          if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1735                          {
1736                              sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1737                          }
1738                          else
1739                          {
1740                              h = 0;
1741                              sscanf( &psz_text[shift], "%d:%d.%d",
1742                                      &m, &sec, &f );
1743                              m *= inv;
1744                          }
1745                      }
1746                      else
1747                      {
1748                          h = m = 0;
1749                          sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1750                          sec *= inv;
1751                      }
1752                      p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1753                          * p_sys->jss.i_time_resolution + f ) * inv;
1754                  }
1755                  break;
1756
1757             case 'T':
1758                 shift = isalpha( (unsigned char)psz_text[2] ) ? 8 : 2 ;
1759
1760                 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
1761                 break;
1762             }
1763             free( psz_orig );
1764             continue;
1765         }
1766         else
1767             /* Unkown type line, probably a comment */
1768         {
1769             free( psz_orig );
1770             continue;
1771         }
1772     }
1773
1774     while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1775     {
1776         const char *s2 = TextGetLine( txt );
1777
1778         if( !s2 )
1779         {
1780             free( psz_orig );
1781             return VLC_EGENERIC;
1782         }
1783
1784         int i_len = strlen( s2 );
1785         if( i_len == 0 )
1786             break;
1787
1788         int i_old = strlen( psz_text );
1789
1790         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1791         if( !psz_text )
1792              return VLC_ENOMEM;
1793
1794         psz_orig = psz_text;
1795         strcat( psz_text, s2 );
1796     }
1797
1798     /* Skip the blanks */
1799     while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1800
1801     /* Parse the directives */
1802     if( isalpha( (unsigned char)*psz_text ) || *psz_text == '[' )
1803     {
1804         while( *psz_text != ' ' )
1805         { psz_text++ ;};
1806
1807         /* Directives are NOT parsed yet */
1808         /* This has probably a better place in a decoder ? */
1809         /* directive = malloc( strlen( psz_text ) + 1 );
1810            if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1811     }
1812
1813     /* Skip the blanks after directives */
1814     while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1815
1816     /* Clean all the lines from inline comments and other stuffs */
1817     psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1818     psz_text2 = psz_orig2;
1819
1820     for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1821     {
1822         switch( *psz_text )
1823         {
1824         case '{':
1825             p_sys->jss.i_comment++;
1826             break;
1827         case '}':
1828             if( p_sys->jss.i_comment )
1829             {
1830                 p_sys->jss.i_comment = 0;
1831                 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1832             }
1833             break;
1834         case '~':
1835             if( !p_sys->jss.i_comment )
1836             {
1837                 *psz_text2 = ' ';
1838                 psz_text2++;
1839             }
1840             break;
1841         case ' ':
1842         case '\t':
1843             if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1844                 break;
1845             if( !p_sys->jss.i_comment )
1846             {
1847                 *psz_text2 = ' ';
1848                 psz_text2++;
1849             }
1850             break;
1851         case '\\':
1852             if( (*(psz_text + 1 ) ) == 'n' )
1853             {
1854                 *psz_text2 = '\n';
1855                 psz_text++;
1856                 psz_text2++;
1857                 break;
1858             }
1859             if( ( toupper((unsigned char)*(psz_text + 1 ) ) == 'C' ) ||
1860                     ( toupper((unsigned char)*(psz_text + 1 ) ) == 'F' ) )
1861             {
1862                 psz_text++; psz_text++;
1863                 break;
1864             }
1865             if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1866                 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1867                 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1868                 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1869             {
1870                 psz_text++;
1871                 break;
1872             }
1873             if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1874                 (*(psz_text + 1 ) ) == '\\' )
1875                 psz_text++;
1876             else if( *(psz_text + 1 ) == '\r' ||  *(psz_text + 1 ) == '\n' ||
1877                      *(psz_text + 1 ) == '\0' )
1878             {
1879                 psz_text++;
1880             }
1881             break;
1882         default:
1883             if( !p_sys->jss.i_comment )
1884             {
1885                 *psz_text2 = *psz_text;
1886                 psz_text2++;
1887             }
1888         }
1889         psz_text++;
1890     }
1891
1892     p_subtitle->psz_text = psz_orig2;
1893     msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
1894     free( psz_orig );
1895     return VLC_SUCCESS;
1896 }
1897
1898 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1899 {
1900     VLC_UNUSED( i_idx );
1901
1902     demux_sys_t *p_sys = p_demux->p_sys;
1903     text_t      *txt = &p_sys->txt;
1904     char *psz_text;
1905     int i;
1906
1907     for( ;; )
1908     {
1909         int h1, m1, s1;
1910         int h2, m2, s2;
1911         const char *s = TextGetLine( txt );
1912
1913         if( !s )
1914             return VLC_EGENERIC;
1915
1916         psz_text = malloc( strlen( s ) + 1 );
1917         if( !psz_text )
1918             return VLC_ENOMEM;
1919
1920         if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1921                     &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1922         {
1923             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1924                                     (int64_t)m1 * 60*1000 +
1925                                     (int64_t)s1 * 1000 ) * 1000;
1926             p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
1927                                     (int64_t)m2 * 60*1000 +
1928                                     (int64_t)s2 * 1000 ) * 1000;
1929             break;
1930         }
1931         free( psz_text );
1932     }
1933
1934     /* replace | by \n */
1935     for( i = 0; psz_text[i] != '\0'; i++ )
1936     {
1937         if( psz_text[i] == '|' )
1938             psz_text[i] = '\n';
1939     }
1940     p_subtitle->psz_text = psz_text;
1941     return VLC_SUCCESS;
1942 }
1943
1944 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1945 {
1946     if( *psz == '\0' ) return 0;
1947     if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1948             sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1949             sscanf( psz, "%d.%d", s, f ) == 2 ||
1950             sscanf( psz, "%d:%d", m, s ) == 2 ||
1951             sscanf( psz, "%d", s ) == 1 )
1952     {
1953         return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1954                + (int64_t)*f * 10 * 1000;
1955     }
1956     else return VLC_EGENERIC;
1957 }
1958
1959 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1960 {
1961     VLC_UNUSED( i_idx );
1962     demux_sys_t *p_sys = p_demux->p_sys;
1963     text_t      *txt = &p_sys->txt;
1964     char *psz_text = NULL;
1965
1966     for( ;; )
1967     {
1968         int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1969         int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1970         const char *s = TextGetLine( txt );
1971         free( psz_text );
1972
1973         if( !s )
1974             return VLC_EGENERIC;
1975
1976         psz_text = malloc( strlen( s ) + 1 );
1977         if( !psz_text )
1978             return VLC_ENOMEM;
1979
1980         /* Find the good begining. This removes extra spaces at the beginning
1981            of the line.*/
1982         char *psz_temp = strcasestr( s, "<time");
1983         if( psz_temp != NULL )
1984         {
1985             char psz_end[12], psz_begin[12];
1986             /* Line has begin and end */
1987             if( ( sscanf( psz_temp,
1988                   "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
1989                             psz_begin, psz_end, psz_text) != 3 ) &&
1990                     /* Line has begin and no end */
1991                     ( sscanf( psz_temp,
1992                               "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
1993                               psz_begin, psz_text ) != 2) )
1994                 /* Line is not recognized */
1995             {
1996                 continue;
1997             }
1998
1999             /* Get the times */
2000             int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
2001             p_subtitle->i_start = i_time >= 0 ? i_time : 0;
2002
2003             i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
2004             p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
2005             break;
2006         }
2007     }
2008
2009     /* Get the following Lines */
2010     for( ;; )
2011     {
2012         const char *s = TextGetLine( txt );
2013
2014         if( !s )
2015         {
2016             free( psz_text );
2017             return VLC_EGENERIC;
2018         }
2019
2020         int i_len = strlen( s );
2021         if( i_len == 0 ) break;
2022
2023         if( strcasestr( s, "<time" ) ||
2024             strcasestr( s, "<clear/") )
2025         {
2026             TextPreviousLine( txt );
2027             break;
2028         }
2029
2030         int i_old = strlen( psz_text );
2031
2032         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
2033         if( !psz_text )
2034             return VLC_ENOMEM;
2035
2036         strcat( psz_text, s );
2037         strcat( psz_text, "\n" );
2038     }
2039
2040     /* Remove the starting ">" that remained after the sscanf */
2041     memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
2042
2043     p_subtitle->psz_text = psz_text;
2044
2045     return VLC_SUCCESS;
2046 }
2047
2048 static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2049 {
2050     VLC_UNUSED( i_idx );
2051
2052     demux_sys_t *p_sys = p_demux->p_sys;
2053     text_t      *txt = &p_sys->txt;
2054     char *psz_text;
2055
2056     for( ;; )
2057     {
2058         int h1, m1, s1;
2059         int h2, m2, s2;
2060         char *s = TextGetLine( txt );
2061
2062         if( !s )
2063             return VLC_EGENERIC;
2064
2065         psz_text = malloc( strlen( s ) + 1 );
2066         if( !psz_text )
2067             return VLC_ENOMEM;
2068
2069         if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
2070                     &h1, &m1, &s1, psz_text ) == 4 )
2071         {
2072             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2073                                     (int64_t)m1 * 60*1000 +
2074                                     (int64_t)s1 * 1000 ) * 1000;
2075
2076             char *s = TextGetLine( txt );
2077             if( !s )
2078             {
2079                 free( psz_text );
2080                 return VLC_EGENERIC;
2081             }
2082
2083             if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2084                 p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
2085                                         (int64_t)m2 * 60*1000 +
2086                                         (int64_t)s2 * 1000 ) * 1000;
2087             else
2088                 p_subtitle->i_stop  = -1;
2089             break;
2090         }
2091         free( psz_text );
2092     }
2093
2094     /* replace [br] by \n */
2095     char *p;
2096     while( ( p = strstr( psz_text, "[br]" ) ) )
2097     {
2098         *p++ = '\n';
2099         memmove( p, &p[3], strlen(&p[3])+1 );
2100     }
2101
2102     p_subtitle->psz_text = psz_text;
2103     return VLC_SUCCESS;
2104 }
2105
2106 static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2107 {
2108     VLC_UNUSED( i_idx );
2109
2110     demux_sys_t *p_sys = p_demux->p_sys;
2111     text_t      *txt = &p_sys->txt;
2112     char *psz_text;
2113
2114     for( ;; )
2115     {
2116         int h1, m1, s1;
2117         int h2, m2, s2;
2118         char *s = TextGetLine( txt );
2119
2120         if( !s )
2121             return VLC_EGENERIC;
2122
2123         if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2124         {
2125             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2126                                     (int64_t)m1 * 60*1000 +
2127                                     (int64_t)s1 * 1000 ) * 1000;
2128
2129             char *s = TextGetLine( txt );
2130             if( !s )
2131                 return VLC_EGENERIC;
2132
2133             psz_text = strdup( s );
2134             if( !psz_text )
2135                 return VLC_ENOMEM;
2136
2137             s = TextGetLine( txt );
2138             if( !s )
2139             {
2140                 free( psz_text );
2141                 return VLC_EGENERIC;
2142             }
2143
2144             if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2145                 p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
2146                                         (int64_t)m2 * 60*1000 +
2147                                         (int64_t)s2 * 1000 ) * 1000;
2148             else
2149                 p_subtitle->i_stop  = -1;
2150
2151             break;
2152         }
2153     }
2154
2155     p_subtitle->psz_text = psz_text;
2156
2157     return VLC_SUCCESS;
2158 }
2159 /*Parsing WebVTT */
2160 static int  ParseVTT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2161 {
2162     VLC_UNUSED( i_idx );
2163
2164     demux_sys_t *p_sys = p_demux->p_sys;
2165     text_t      *txt = &p_sys->txt;
2166     char        *psz_text;
2167
2168     for( ;; )
2169     {
2170         const char *s = TextGetLine( txt );
2171         int h1 = 0, m1 = 0, s1 = 0, d1 = 0;
2172         int h2 = 0, m2 = 0, s2 = 0, d2 = 0;
2173
2174         if( !s )
2175             return VLC_EGENERIC;
2176
2177         if( sscanf( s,"%d:%d:%d.%d --> %d:%d:%d.%d",
2178                     &h1, &m1, &s1, &d1,
2179                     &h2, &m2, &s2, &d2 ) == 8 ||
2180             sscanf( s,"%d:%d:%d.%d --> %d:%d.%d",
2181                     &h1, &m1, &s1, &d1,
2182                          &m2, &s2, &d2 ) == 7 ||
2183             sscanf( s,"%d:%d.%d --> %d:%d:%d.%d",
2184                          &m1, &s1, &d1,
2185                     &h2, &m2, &s2, &d2 ) == 7 ||
2186             sscanf( s,"%d:%d.%d --> %d:%d.%d",
2187                          &m1, &s1, &d1,
2188                          &m2, &s2, &d2 ) == 6 )
2189         {
2190             p_subtitle->i_start = ( (int64_t)h1 * 3600 * 1000 +
2191                                     (int64_t)m1 * 60 * 1000 +
2192                                     (int64_t)s1 * 1000 +
2193                                     (int64_t)d1 ) * 1000;
2194
2195             p_subtitle->i_stop  = ( (int64_t)h2 * 3600 * 1000 +
2196                                     (int64_t)m2 * 60 * 1000 +
2197                                     (int64_t)s2 * 1000 +
2198                                     (int64_t)d2 ) * 1000;
2199             if( p_subtitle->i_start < p_subtitle->i_stop )
2200                 break;
2201         }
2202     }
2203
2204     /* Now read text until an empty line */
2205     psz_text = strdup("");
2206     if( !psz_text )
2207         return VLC_ENOMEM;
2208
2209     for( ;; )
2210     {
2211         const char *s = TextGetLine( txt );
2212         int i_len;
2213         int i_old;
2214
2215         i_len = s ? strlen( s ) : 0;
2216         if( i_len <= 0 )
2217         {
2218             p_subtitle->psz_text = psz_text;
2219             return VLC_SUCCESS;
2220         }
2221
2222         i_old = strlen( psz_text );
2223         psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
2224         if( !psz_text )
2225             return VLC_ENOMEM;
2226
2227         strcat( psz_text, s );
2228         strcat( psz_text, "\n" );
2229     }
2230 }
2231
2232 /* Matches filename.xx.srt */
2233 static char * get_language_from_filename( const char * psz_sub_file )
2234 {
2235     char *psz_ret = NULL;
2236     char *psz_tmp, *psz_language_begin;
2237
2238     if( !psz_sub_file ) return NULL;
2239     char *psz_work = strdup( psz_sub_file );
2240
2241     /* Removing extension, but leaving the dot */
2242     psz_tmp = strrchr( psz_work, '.' );
2243     if( psz_tmp )
2244     {
2245         psz_tmp[0] = '\0';
2246         psz_language_begin = strrchr( psz_work, '.' );
2247         if( psz_language_begin )
2248             psz_ret = strdup(++psz_language_begin);
2249         psz_tmp[0] = '.';
2250     }
2251
2252     free( psz_work );
2253     return psz_ret;
2254 }