]> git.sesse.net Git - vlc/blob - modules/demux/vobsub.c
l10n: Dutch update
[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             break;
436
437         ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
438         if( ppsz_new == NULL )
439         {
440             free( psz );
441             break;
442         }
443         lines = ppsz_new;
444         lines[n++] = psz;
445     }
446
447     txt->i_line_count = n;
448     txt->i_line       = 0;
449     txt->line         = lines;
450
451     return VLC_SUCCESS;
452 }
453
454 static void TextUnload( text_t *txt )
455 {
456     for( int i = 0; i < txt->i_line_count; i++ )
457         free( txt->line[i] );
458     free( txt->line );
459
460     txt->i_line       = 0;
461     txt->i_line_count = 0;
462 }
463
464 static char *TextGetLine( text_t *txt )
465 {
466     if( txt->i_line >= txt->i_line_count )
467         return( NULL );
468
469     return txt->line[txt->i_line++];
470 }
471
472 static int ParseVobSubIDX( demux_t *p_demux )
473 {
474     demux_sys_t *p_sys = p_demux->p_sys;
475     text_t      *txt = &p_sys->txt;
476     char        *line;
477     vobsub_track_t *current_tk = NULL;
478
479     for( ;; )
480     {
481         if( ( line = TextGetLine( txt ) ) == NULL )
482         {
483             return( VLC_EGENERIC );
484         }
485
486         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
487         {
488             continue;
489         }
490         else if( !strncmp( "size:", line, 5 ) )
491         {
492             /* Store the original size of the video */
493             if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
494                                    &p_sys->i_original_frame_height ) == VLC_SUCCESS )
495             {
496                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
497             }
498             else
499             {
500                 msg_Warn( p_demux, "reading original frame size failed" );
501             }
502         }
503         else if( !strncmp( "palette:", line, 8 ) )
504         {
505             if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
506             {
507                 p_sys->b_palette = true;
508                 msg_Dbg( p_demux, "vobsub palette read" );
509             }
510             else
511             {
512                 msg_Warn( p_demux, "reading original palette failed" );
513             }
514         }
515         else if( !strncmp( "id:", line, 3 ) )
516         {
517             char language[33]; /* Usually 2 or 3 letters, sometimes more.
518                                   Spec (or lack of) doesn't define any limit */
519             int i_track_id;
520             es_format_t fmt;
521
522             /* Lets start a new track */
523             if( sscanf( line, "id: %32[^ ,], index: %d",
524                         language, &i_track_id ) != 2 )
525             {
526                 if( sscanf( line, "id: , index: %d", &i_track_id ) != 1 )
527                 {
528                     msg_Warn( p_demux, "reading new track failed" );
529                     continue;
530                 }
531                 language[0] = '\0';
532             }
533
534             p_sys->i_tracks++;
535             p_sys->track = xrealloc( p_sys->track,
536                     sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
537
538             /* Init the track */
539             current_tk = &p_sys->track[p_sys->i_tracks - 1];
540             memset( current_tk, 0, sizeof( vobsub_track_t ) );
541             current_tk->i_current_subtitle = 0;
542             current_tk->i_subtitles = 0;
543             current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );
544             current_tk->i_track_id = i_track_id;
545             current_tk->i_delay = (int64_t)0;
546
547             es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
548             fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
549             fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
550             fmt.psz_language = language;
551             if( p_sys->b_palette )
552             {
553                 fmt.subs.spu.palette[0] = 0xBeef;
554                 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
555             }
556
557             current_tk->p_es = es_out_Add( p_demux->out, &fmt );
558             msg_Dbg( p_demux, "new vobsub track detected" );
559         }
560         else if( !strncmp( line, "timestamp:", 10 ) )
561         {
562             /*
563              * timestamp: [sign]hh:mm:ss:mss, filepos: loc
564              * loc is the hex location of the spu in the .sub file
565              */
566             int h, m, s, ms, count, loc = 0;
567             int i_sign = 1;
568             int64_t i_start, i_location = 0;
569
570             if( p_sys->i_tracks > 0 &&
571                 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
572                         &h, &count, &m, &s, &ms, &loc ) >= 5  )
573             {
574                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
575                 subtitle_t *current_sub;
576
577                 if( line[count-3] == '-' )
578                 {
579                     i_sign = -1;
580                     h = -h;
581                 }
582                 i_start = (int64_t) ( h * 3600*1000 +
583                             m * 60*1000 +
584                             s * 1000 +
585                             ms ) * 1000;
586                 i_location = loc;
587
588                 current_tk->i_subtitles++;
589                 current_tk->p_subtitles =
590                     xrealloc( current_tk->p_subtitles,
591                       sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
592                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
593
594                 current_sub->i_start = i_start * i_sign;
595                 current_sub->i_start += current_tk->i_delay;
596                 current_sub->i_vobsub_location = i_location;
597             }
598             else
599             {
600                 msg_Warn( p_demux, "reading timestamp failed" );
601             }
602         }
603         else if( !strncasecmp( line, "delay:", 6 ) )
604         {
605             /*
606              * delay: [sign]hh:mm:ss:mss
607              */
608             int h, m, s, ms, count = 0;
609             int i_sign = 1;
610             int64_t i_gap = 0;
611
612             if( p_sys->i_tracks > 0 &&
613                 sscanf( line, "%*celay: %d%n:%d:%d:%d",
614                         &h, &count, &m, &s, &ms ) >= 4 )
615             {
616                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
617                 if( line[count-3] == '-' )
618                 {
619                     i_sign = -1;
620                     h = -h;
621                 }
622                 i_gap = (int64_t) ( h * 3600*1000 +
623                             m * 60*1000 +
624                             s * 1000 +
625                             ms ) * 1000;
626
627                 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
628                 msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
629                          i_sign, i_gap, current_tk->i_delay );
630             }
631             else
632             {
633                 msg_Warn( p_demux, "reading delay failed" );
634             }
635         }
636     }
637     return( 0 );
638 }
639
640 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
641 {
642     demux_sys_t *p_sys = p_demux->p_sys;
643     uint8_t     *p = p_bk->p_buffer;
644     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
645     int i;
646
647     while( p + 6 < p_end )
648     {
649         int i_size = ps_pkt_size( p, p_end - p );
650         block_t *p_pkt;
651         int      i_id;
652         int      i_spu;
653
654         if( i_size <= 0 )
655             break;
656
657         if( i_size > p_end - p )
658         {
659             msg_Warn( p_demux, "broken PES size" );
660             break;
661         }
662
663         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
664         {
665             msg_Warn( p_demux, "invalid PES" );
666             break;
667         }
668
669         if( p[3] != 0xbd )
670         {
671             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
672             p += i_size;
673             continue;
674         }
675
676         /* Create a block */
677         p_pkt = block_Alloc( i_size );
678         if( unlikely(p_pkt == NULL) )
679             break;
680         memcpy( p_pkt->p_buffer, p, i_size);
681         p += i_size;
682
683         i_id = ps_pkt_id( p_pkt );
684         if( (i_id&0xffe0) != 0xbd20 ||
685             ps_pkt_parse_pes( p_pkt, 1 ) )
686         {
687             block_Release( p_pkt );
688             continue;
689         }
690         i_spu = i_id&0x1f;
691         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
692
693         for( i = 0; i < p_sys->i_tracks; i++ )
694         {
695             vobsub_track_t *p_tk = &p_sys->track[i];
696
697             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
698             p_pkt->i_length = 0;
699
700             if( p_tk->p_es && p_tk->i_track_id == i_spu )
701             {
702                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
703                 p_bk->i_pts = VLC_TS_INVALID;     /*only first packet has a pts */
704                 break;
705             }
706         }
707         if( i >= p_sys->i_tracks )
708         {
709             block_Release( p_pkt );
710         }
711     }
712
713     return VLC_SUCCESS;
714 }
715