]> git.sesse.net Git - vlc/blob - modules/demux/vobsub.c
macosx: Update progress dialog on the main thread, make check thread safe
[vlc] / modules / demux / vobsub.c
1 /*****************************************************************************
2  * vobsub.c: Demux vobsub files.
3  *****************************************************************************
4  * Copyright (C) 1999-2004 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  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <limits.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
38
39 #include "ps.h"
40 #include "vobsub.h"
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open ( vlc_object_t *p_this );
46 static void Close( vlc_object_t *p_this );
47
48 vlc_module_begin ()
49     set_description( N_("Vobsub subtitles parser") )
50     set_category( CAT_INPUT )
51     set_subcategory( SUBCAT_INPUT_DEMUX )
52     set_capability( "demux", 1 )
53
54     set_callbacks( Open, Close )
55
56     add_shortcut( "vobsub", "subtitle" )
57 vlc_module_end ()
58
59 /*****************************************************************************
60  * Prototypes:
61  *****************************************************************************/
62
63 typedef struct
64 {
65     int     i_line_count;
66     int     i_line;
67     char    **line;
68 } text_t;
69
70 typedef struct
71 {
72     int64_t i_start;
73     int     i_vobsub_location;
74 } subtitle_t;
75
76 typedef struct
77 {
78     es_out_id_t *p_es;
79     int         i_track_id;
80
81     int         i_current_subtitle;
82     int         i_subtitles;
83     subtitle_t  *p_subtitles;
84
85     int64_t     i_delay;
86 } vobsub_track_t;
87
88 struct demux_sys_t
89 {
90     int64_t        i_next_demux_date;
91     int64_t        i_length;
92
93     text_t         txt;
94     stream_t       *p_vobsub_stream;
95
96     /* all tracks */
97     int            i_tracks;
98     vobsub_track_t *track;
99
100     int            i_original_frame_width;
101     int            i_original_frame_height;
102     bool           b_palette;
103     uint32_t       palette[16];
104 };
105
106
107 static int Demux( demux_t * );
108 static int Control( demux_t *, int, va_list );
109
110 static int  TextLoad( text_t *, stream_t *s );
111 static void TextUnload( text_t * );
112 static int ParseVobSubIDX( demux_t * );
113 static int DemuxVobSub( demux_t *, block_t *);
114
115 /*****************************************************************************
116  * Module initializer
117  *****************************************************************************/
118 static int Open ( vlc_object_t *p_this )
119 {
120     demux_t     *p_demux = (demux_t*)p_this;
121     demux_sys_t *p_sys;
122     char *psz_vobname, *s;
123     int i_len;
124
125     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
126     {
127         if( !strcasestr( s, "# VobSub index file" ) )
128         {
129             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
130             free( s );
131             if( stream_Seek( p_demux->s, 0 ) )
132             {
133                 msg_Warn( p_demux, "failed to rewind" );
134             }
135             return VLC_EGENERIC;
136         }
137         free( s );
138     }
139     else
140     {
141         msg_Dbg( p_demux, "could not read vobsub IDX file" );
142         return VLC_EGENERIC;
143     }
144
145     /* */
146     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
147     if( unlikely( !p_sys ) )
148         return VLC_ENOMEM;
149
150     p_sys->i_length = 0;
151     p_sys->p_vobsub_stream = NULL;
152     p_sys->i_tracks = 0;
153     p_sys->track = malloc( sizeof( vobsub_track_t ) );
154     if( unlikely( !p_sys->track ) )
155         goto error;
156     p_sys->i_original_frame_width = -1;
157     p_sys->i_original_frame_height = -1;
158     p_sys->b_palette = false;
159     memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
160
161     /* Load the whole file */
162     TextLoad( &p_sys->txt, p_demux->s );
163
164     /* Parse it */
165     ParseVobSubIDX( p_demux );
166
167     /* Unload */
168     TextUnload( &p_sys->txt );
169
170     /* Find the total length of the vobsubs */
171     if( p_sys->i_tracks > 0 )
172     {
173         for( int i = 0; i < p_sys->i_tracks; i++ )
174         {
175             if( p_sys->track[i].i_subtitles > 1 )
176             {
177                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
178                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
179             }
180         }
181     }
182
183     if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1 )
184         goto error;
185
186     i_len = strlen( psz_vobname );
187     if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
188
189     /* open file */
190     p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
191     if( p_sys->p_vobsub_stream == NULL )
192     {
193         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
194                  psz_vobname );
195         free( psz_vobname );
196         goto error;
197     }
198     free( psz_vobname );
199
200     p_demux->pf_demux = Demux;
201     p_demux->pf_control = Control;
202
203     return VLC_SUCCESS;
204
205 error:
206     /* Clean all subs from all tracks */
207     for( int i = 0; i < p_sys->i_tracks; i++ )
208         free( p_sys->track[i].p_subtitles );
209     free( p_sys->track );
210     free( p_sys );
211
212     return VLC_EGENERIC;
213 }
214
215 /*****************************************************************************
216  * Close: Close subtitle demux
217  *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
219 {
220     demux_t *p_demux = (demux_t*)p_this;
221     demux_sys_t *p_sys = p_demux->p_sys;
222
223     if( p_sys->p_vobsub_stream )
224         stream_Delete( p_sys->p_vobsub_stream );
225
226     /* Clean all subs from all tracks */
227     for( int i = 0; i < p_sys->i_tracks; i++ )
228         free( p_sys->track[i].p_subtitles );
229     free( p_sys->track );
230     free( p_sys );
231 }
232
233 /*****************************************************************************
234  * Control:
235  *****************************************************************************/
236 static int Control( demux_t *p_demux, int i_query, va_list args )
237 {
238     demux_sys_t *p_sys = p_demux->p_sys;
239     int64_t *pi64, i64;
240     int i;
241     double *pf, f;
242
243     switch( i_query )
244     {
245         case DEMUX_GET_LENGTH:
246             pi64 = (int64_t*)va_arg( args, int64_t * );
247             *pi64 = (int64_t) p_sys->i_length;
248             return VLC_SUCCESS;
249
250         case DEMUX_GET_TIME:
251             pi64 = (int64_t*)va_arg( args, int64_t * );
252             for( i = 0; i < p_sys->i_tracks; i++ )
253             {
254                 bool b_selected;
255                 /* Check the ES is selected */
256                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
257                                 p_sys->track[i].p_es, &b_selected );
258                 if( b_selected ) break;
259             }
260             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
261             {
262                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
263                 return VLC_SUCCESS;
264             }
265             return VLC_EGENERIC;
266
267         case DEMUX_SET_TIME:
268             i64 = (int64_t)va_arg( args, int64_t );
269             for( i = 0; i < p_sys->i_tracks; i++ )
270             {
271                 p_sys->track[i].i_current_subtitle = 0;
272                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
273                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
274                 {
275                     p_sys->track[i].i_current_subtitle++;
276                 }
277
278                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
279                     return VLC_EGENERIC;
280             }
281             return VLC_SUCCESS;
282
283         case DEMUX_GET_POSITION:
284             pf = (double*)va_arg( args, double * );
285             for( i = 0; i < p_sys->i_tracks; i++ )
286             {
287                 bool b_selected;
288                 /* Check the ES is selected */
289                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
290                                 p_sys->track[i].p_es, &b_selected );
291                 if( b_selected ) break;
292             }
293             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
294             {
295                 *pf = 1.0;
296             }
297             else if( p_sys->track[i].i_subtitles > 0 )
298             {
299                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
300                       (double)p_sys->i_length;
301             }
302             else
303             {
304                 *pf = 0.0;
305             }
306             return VLC_SUCCESS;
307
308         case DEMUX_SET_POSITION:
309             f = (double)va_arg( args, double );
310             i64 = (int64_t) f * p_sys->i_length;
311
312             for( i = 0; i < p_sys->i_tracks; i++ )
313             {
314                 p_sys->track[i].i_current_subtitle = 0;
315                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
316                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
317                 {
318                     p_sys->track[i].i_current_subtitle++;
319                 }
320                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
321                     return VLC_EGENERIC;
322             }
323             return VLC_SUCCESS;
324
325         case DEMUX_SET_NEXT_DEMUX_TIME:
326             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
327             return VLC_SUCCESS;
328
329         case DEMUX_GET_PTS_DELAY:
330         case DEMUX_GET_FPS:
331         case DEMUX_GET_META:
332         case DEMUX_GET_TITLE_INFO:
333         case DEMUX_HAS_UNSUPPORTED_META:
334         case DEMUX_GET_ATTACHMENTS:
335         case DEMUX_CAN_RECORD:
336             return VLC_EGENERIC;
337
338         default:
339             msg_Warn( p_demux, "unknown query in subtitle control" );
340             return VLC_EGENERIC;
341     }
342 }
343
344 /*****************************************************************************
345  * Demux: Send subtitle to decoder
346  *****************************************************************************/
347 static int Demux( demux_t *p_demux )
348 {
349     demux_sys_t *p_sys = p_demux->p_sys;
350     int64_t i_maxdate;
351     int i_read;
352
353     for( int i = 0; i < p_sys->i_tracks; i++ )
354     {
355 #define tk p_sys->track[i]
356         if( tk.i_current_subtitle >= tk.i_subtitles )
357             continue;
358
359         i_maxdate = p_sys->i_next_demux_date;
360         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
361         {
362             /* Should not happen */
363             i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
364         }
365
366         while( tk.i_current_subtitle < tk.i_subtitles &&
367                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
368         {
369             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
370             block_t *p_block;
371             int i_size = 0;
372
373             /* first compute SPU size */
374             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
375             {
376                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
377             }
378             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
379
380             /* Seek at the right place */
381             if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
382             {
383                 msg_Warn( p_demux,
384                           "cannot seek in the VobSub to the correct time %d", i_pos );
385                 tk.i_current_subtitle++;
386                 continue;
387             }
388
389             /* allocate a packet */
390             if( ( p_block = block_Alloc( i_size ) ) == NULL )
391             {
392                 tk.i_current_subtitle++;
393                 continue;
394             }
395
396             /* read data */
397             i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
398             if( i_read <= 6 )
399             {
400                 block_Release( p_block );
401                 tk.i_current_subtitle++;
402                 continue;
403             }
404             p_block->i_buffer = i_read;
405
406             /* pts */
407             p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
408
409             /* demux this block */
410             DemuxVobSub( p_demux, p_block );
411
412             tk.i_current_subtitle++;
413         }
414 #undef tk
415     }
416
417     /* */
418     p_sys->i_next_demux_date = 0;
419
420     return 1;
421 }
422
423 static int TextLoad( text_t *txt, stream_t *s )
424 {
425     char **lines = NULL;
426     size_t n = 0;
427
428     /* load the complete file */
429     for( ;; )
430     {
431         char *psz = stream_ReadLine( s );
432         char **ppsz_new;
433
434         if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
435         {
436             free( psz );
437             break;
438         }
439
440         ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
441         if( ppsz_new == NULL )
442         {
443             free( psz );
444             break;
445         }
446         lines = ppsz_new;
447         lines[n++] = psz;
448     }
449
450     txt->i_line_count = n;
451     txt->i_line       = 0;
452     txt->line         = lines;
453
454     return VLC_SUCCESS;
455 }
456
457 static void TextUnload( text_t *txt )
458 {
459     for( int i = 0; i < txt->i_line_count; i++ )
460         free( txt->line[i] );
461     free( txt->line );
462
463     txt->i_line       = 0;
464     txt->i_line_count = 0;
465 }
466
467 static char *TextGetLine( text_t *txt )
468 {
469     if( txt->i_line >= txt->i_line_count )
470         return( NULL );
471
472     return txt->line[txt->i_line++];
473 }
474
475 static int ParseVobSubIDX( demux_t *p_demux )
476 {
477     demux_sys_t *p_sys = p_demux->p_sys;
478     text_t      *txt = &p_sys->txt;
479     char        *line;
480     vobsub_track_t *current_tk = NULL;
481
482     for( ;; )
483     {
484         if( ( line = TextGetLine( txt ) ) == NULL )
485         {
486             return( VLC_EGENERIC );
487         }
488
489         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
490         {
491             continue;
492         }
493         else if( !strncmp( "size:", line, 5 ) )
494         {
495             /* Store the original size of the video */
496             if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
497                                    &p_sys->i_original_frame_height ) == VLC_SUCCESS )
498             {
499                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
500             }
501             else
502             {
503                 msg_Warn( p_demux, "reading original frame size failed" );
504             }
505         }
506         else if( !strncmp( "palette:", line, 8 ) )
507         {
508             if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
509             {
510                 p_sys->b_palette = true;
511                 msg_Dbg( p_demux, "vobsub palette read" );
512             }
513             else
514             {
515                 msg_Warn( p_demux, "reading original palette failed" );
516             }
517         }
518         else if( !strncmp( "id:", line, 3 ) )
519         {
520             char language[33]; /* Usually 2 or 3 letters, sometimes more.
521                                   Spec (or lack of) doesn't define any limit */
522             int i_track_id;
523             es_format_t fmt;
524
525             /* Lets start a new track */
526             if( sscanf( line, "id: %32[^ ,], index: %d",
527                         language, &i_track_id ) != 2 )
528             {
529                 if( sscanf( line, "id: , index: %d", &i_track_id ) != 1 )
530                 {
531                     msg_Warn( p_demux, "reading new track failed" );
532                     continue;
533                 }
534                 language[0] = '\0';
535             }
536
537             p_sys->i_tracks++;
538             p_sys->track = xrealloc( p_sys->track,
539                     sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
540
541             /* Init the track */
542             current_tk = &p_sys->track[p_sys->i_tracks - 1];
543             memset( current_tk, 0, sizeof( vobsub_track_t ) );
544             current_tk->i_current_subtitle = 0;
545             current_tk->i_subtitles = 0;
546             current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );
547             current_tk->i_track_id = i_track_id;
548             current_tk->i_delay = (int64_t)0;
549
550             es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
551             fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
552             fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
553             fmt.psz_language = language;
554             if( p_sys->b_palette )
555             {
556                 fmt.subs.spu.palette[0] = 0xBeef;
557                 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
558             }
559
560             current_tk->p_es = es_out_Add( p_demux->out, &fmt );
561             msg_Dbg( p_demux, "New vobsub track detected: %i [%s]", i_track_id, language );
562         }
563         else if( !strncmp( line, "timestamp:", 10 ) )
564         {
565             /*
566              * timestamp: [sign]hh:mm:ss:mss, filepos: loc
567              * loc is the hex location of the spu in the .sub file
568              */
569             int h, m, s, ms, count, loc = 0;
570             int i_sign = 1;
571             int64_t i_start, i_location = 0;
572
573             if( p_sys->i_tracks > 0 &&
574                 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
575                         &h, &count, &m, &s, &ms, &loc ) >= 5  )
576             {
577                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
578                 subtitle_t *current_sub;
579
580                 if( line[count-3] == '-' )
581                 {
582                     i_sign = -1;
583                     h = -h;
584                 }
585                 i_start = (int64_t) ( h * 3600*1000 +
586                             m * 60*1000 +
587                             s * 1000 +
588                             ms ) * 1000;
589                 i_location = loc;
590
591                 current_tk->i_subtitles++;
592                 current_tk->p_subtitles =
593                     xrealloc( current_tk->p_subtitles,
594                       sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
595                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
596
597                 current_sub->i_start = i_start * i_sign;
598                 current_sub->i_start += current_tk->i_delay;
599                 current_sub->i_vobsub_location = i_location;
600             }
601             else
602             {
603                 msg_Warn( p_demux, "reading timestamp failed" );
604             }
605         }
606         else if( !strncasecmp( line, "delay:", 6 ) )
607         {
608             /*
609              * delay: [sign]hh:mm:ss:mss
610              */
611             int h, m, s, ms, count = 0;
612             int i_sign = 1;
613             int64_t i_gap = 0;
614
615             if( p_sys->i_tracks > 0 &&
616                 sscanf( line, "%*celay: %d%n:%d:%d:%d",
617                         &h, &count, &m, &s, &ms ) >= 4 )
618             {
619                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
620                 if( line[count-3] == '-' )
621                 {
622                     i_sign = -1;
623                     h = -h;
624                 }
625                 i_gap = (int64_t) ( h * 3600*1000 +
626                             m * 60*1000 +
627                             s * 1000 +
628                             ms ) * 1000;
629
630                 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
631                 msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
632                          i_sign, i_gap, current_tk->i_delay );
633             }
634             else
635             {
636                 msg_Warn( p_demux, "reading delay failed" );
637             }
638         }
639     }
640     return( 0 );
641 }
642
643 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
644 {
645     demux_sys_t *p_sys = p_demux->p_sys;
646     uint8_t     *p = p_bk->p_buffer;
647     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
648     int i;
649
650     while( p + 6 < p_end )
651     {
652         int i_size = ps_pkt_size( p, p_end - p );
653         block_t *p_pkt;
654         int      i_id;
655         int      i_spu;
656
657         if( i_size <= 0 )
658             break;
659
660         if( i_size > p_end - p )
661         {
662             msg_Warn( p_demux, "broken PES size" );
663             break;
664         }
665
666         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
667         {
668             msg_Warn( p_demux, "invalid PES" );
669             break;
670         }
671
672         if( p[3] != 0xbd )
673         {
674             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
675             p += i_size;
676             continue;
677         }
678
679         /* Create a block */
680         p_pkt = block_Alloc( i_size );
681         if( unlikely(p_pkt == NULL) )
682             break;
683         memcpy( p_pkt->p_buffer, p, i_size);
684         p += i_size;
685
686         i_id = ps_pkt_id( p_pkt );
687         if( (i_id&0xffe0) != 0xbd20 ||
688             ps_pkt_parse_pes( p_pkt, 1 ) )
689         {
690             block_Release( p_pkt );
691             continue;
692         }
693         i_spu = i_id&0x1f;
694         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
695
696         for( i = 0; i < p_sys->i_tracks; i++ )
697         {
698             vobsub_track_t *p_tk = &p_sys->track[i];
699
700             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
701             p_pkt->i_length = 0;
702
703             if( p_tk->p_es && p_tk->i_track_id == i_spu )
704             {
705                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
706                 p_bk->i_pts = VLC_TS_INVALID;     /*only first packet has a pts */
707                 break;
708             }
709         }
710         if( i >= p_sys->i_tracks )
711         {
712             block_Release( p_pkt );
713         }
714     }
715
716     return VLC_SUCCESS;
717 }
718