]> git.sesse.net Git - vlc/blob - modules/demux/vobsub.c
modules: use the new add_shortcut capability (add multiple shortcuts at a time).
[vlc] / modules / demux / vobsub.c
1 /*****************************************************************************
2  * subtitle.c: Demux vobsub files.
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34
35 #include <sys/types.h>
36 #include <limits.h>
37
38 #include <vlc_demux.h>
39
40 #include "ps.h"
41 #include "vobsub.h"
42
43 #define MAX_LINE 8192
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 static int  Open ( vlc_object_t *p_this );
49 static void Close( vlc_object_t *p_this );
50
51 vlc_module_begin ()
52     set_description( N_("Vobsub subtitles parser") )
53     set_category( CAT_INPUT )
54     set_subcategory( SUBCAT_INPUT_DEMUX )
55     set_capability( "demux", 1 )
56
57     set_callbacks( Open, Close )
58
59     add_shortcut( "vobsub", "subtitle" )
60 vlc_module_end ()
61
62 /*****************************************************************************
63  * Prototypes:
64  *****************************************************************************/
65
66 typedef struct
67 {
68     int     i_line_count;
69     int     i_line;
70     char    **line;
71 } text_t;
72 static int  TextLoad( text_t *, stream_t *s );
73 static void TextUnload( text_t * );
74
75 typedef struct
76 {
77     int64_t i_start;
78     int     i_vobsub_location;
79 } subtitle_t;
80
81 typedef struct
82 {
83     es_format_t fmt;
84     es_out_id_t *p_es;
85     int         i_track_id;
86
87     int         i_current_subtitle;
88     int         i_subtitles;
89     subtitle_t  *p_subtitles;
90
91     int64_t     i_delay;
92 } vobsub_track_t;
93
94 struct demux_sys_t
95 {
96     int64_t     i_next_demux_date;
97     int64_t     i_length;
98
99     text_t      txt;
100     stream_t    *p_vobsub_stream;
101
102     /* all tracks */
103     int            i_tracks;
104     vobsub_track_t *track;
105
106     int         i_original_frame_width;
107     int         i_original_frame_height;
108     bool  b_palette;
109     uint32_t    palette[16];
110 };
111
112 static int Demux( demux_t * );
113 static int Control( demux_t *, int, va_list );
114
115 static int ParseVobSubIDX( demux_t * );
116 static int DemuxVobSub( demux_t *, block_t *);
117
118 /*****************************************************************************
119  * Module initializer
120  *****************************************************************************/
121 static int Open ( vlc_object_t *p_this )
122 {
123     demux_t     *p_demux = (demux_t*)p_this;
124     demux_sys_t *p_sys;
125     char *psz_vobname, *s;
126     int i_len;
127
128     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
129     {
130         if( !strcasestr( s, "# VobSub index file" ) )
131         {
132             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
133             free( s );
134             if( stream_Seek( p_demux->s, 0 ) )
135             {
136                 msg_Warn( p_demux, "failed to rewind" );
137             }
138             return VLC_EGENERIC;
139         }
140         free( s );
141
142     }
143     else
144     {
145         msg_Dbg( p_demux, "could not read vobsub IDX file" );
146         return VLC_EGENERIC;
147     }
148
149     p_demux->pf_demux = Demux;
150     p_demux->pf_control = Control;
151     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
152     if( unlikely( !p_sys ) )
153         return VLC_ENOMEM;
154     p_sys->i_length = 0;
155     p_sys->p_vobsub_stream = NULL;
156     p_sys->i_tracks = 0;
157     p_sys->track = malloc( sizeof( vobsub_track_t ) );
158     if( unlikely( !p_sys->track ) )
159     {
160         free( p_sys );
161         return VLC_ENOMEM;
162     }
163     p_sys->i_original_frame_width = -1;
164     p_sys->i_original_frame_height = -1;
165     p_sys->b_palette = false;
166     memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
167
168     /* Load the whole file */
169     TextLoad( &p_sys->txt, p_demux->s );
170
171     /* Parse it */
172     ParseVobSubIDX( p_demux );
173
174     /* Unload */
175     TextUnload( &p_sys->txt );
176
177     /* Find the total length of the vobsubs */
178     if( p_sys->i_tracks > 0 )
179     {
180         int i;
181         for( i = 0; i < p_sys->i_tracks; i++ )
182         {
183             if( p_sys->track[i].i_subtitles > 1 )
184             {
185                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
186                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
187             }
188         }
189     }
190
191     if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path ) == -1 )
192     {
193         free( p_sys );
194         return VLC_EGENERIC;
195     }
196     i_len = strlen( psz_vobname );
197     if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
198
199     /* open file */
200     p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
201     if( p_sys->p_vobsub_stream == NULL )
202     {
203         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
204                  psz_vobname );
205         free( psz_vobname );
206         free( p_sys );
207         return VLC_EGENERIC;
208     }
209     free( psz_vobname );
210
211     return VLC_SUCCESS;
212 }
213
214 /*****************************************************************************
215  * Close: Close subtitle demux
216  *****************************************************************************/
217 static void Close( vlc_object_t *p_this )
218 {
219     int i;
220     demux_t *p_demux = (demux_t*)p_this;
221     demux_sys_t *p_sys = p_demux->p_sys;
222
223     /* Clean all subs from all tracks */
224     for( i = 0; i < p_sys->i_tracks; i++ )
225         free( p_sys->track[i].p_subtitles );
226
227     free( p_sys->track );
228
229     if( p_sys->p_vobsub_stream )
230         stream_Delete( p_sys->p_vobsub_stream );
231
232     free( p_sys );
233 }
234
235 /*****************************************************************************
236  * Control:
237  *****************************************************************************/
238 static int Control( demux_t *p_demux, int i_query, va_list args )
239 {
240     demux_sys_t *p_sys = p_demux->p_sys;
241     int64_t *pi64, i64;
242     int i;
243     double *pf, f;
244
245     switch( i_query )
246     {
247         case DEMUX_GET_LENGTH:
248             pi64 = (int64_t*)va_arg( args, int64_t * );
249             *pi64 = (int64_t) p_sys->i_length;
250             return VLC_SUCCESS;
251
252         case DEMUX_GET_TIME:
253             pi64 = (int64_t*)va_arg( args, int64_t * );
254             for( i = 0; i < p_sys->i_tracks; i++ )
255             {
256                 bool b_selected;
257                 /* Check the ES is selected */
258                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
259                                 p_sys->track[i].p_es, &b_selected );
260                 if( b_selected ) break;
261             }
262             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
263             {
264                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
265                 return VLC_SUCCESS;
266             }
267             return VLC_EGENERIC;
268
269         case DEMUX_SET_TIME:
270             i64 = (int64_t)va_arg( args, int64_t );
271             for( i = 0; i < p_sys->i_tracks; i++ )
272             {
273                 p_sys->track[i].i_current_subtitle = 0;
274                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
275                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
276                 {
277                     p_sys->track[i].i_current_subtitle++;
278                 }
279
280                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
281                     return VLC_EGENERIC;
282             }
283             return VLC_SUCCESS;
284
285         case DEMUX_GET_POSITION:
286             pf = (double*)va_arg( args, double * );
287             for( i = 0; i < p_sys->i_tracks; i++ )
288             {
289                 bool b_selected;
290                 /* Check the ES is selected */
291                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
292                                 p_sys->track[i].p_es, &b_selected );
293                 if( b_selected ) break;
294             }
295             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
296             {
297                 *pf = 1.0;
298             }
299             else if( p_sys->track[i].i_subtitles > 0 )
300             {
301                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
302                       (double)p_sys->i_length;
303             }
304             else
305             {
306                 *pf = 0.0;
307             }
308             return VLC_SUCCESS;
309
310         case DEMUX_SET_POSITION:
311             f = (double)va_arg( args, double );
312             i64 = (int64_t) f * p_sys->i_length;
313
314             for( i = 0; i < p_sys->i_tracks; i++ )
315             {
316                 p_sys->track[i].i_current_subtitle = 0;
317                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
318                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
319                 {
320                     p_sys->track[i].i_current_subtitle++;
321                 }
322                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
323                     return VLC_EGENERIC;
324             }
325             return VLC_SUCCESS;
326
327         case DEMUX_SET_NEXT_DEMUX_TIME:
328             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
329             return VLC_SUCCESS;
330
331         case DEMUX_GET_FPS:
332         case DEMUX_GET_META:
333         case DEMUX_GET_TITLE_INFO:
334         case DEMUX_HAS_UNSUPPORTED_META:
335         case DEMUX_GET_ATTACHMENTS:
336         case DEMUX_CAN_RECORD:
337             return VLC_EGENERIC;
338
339         default:
340             msg_Warn( p_demux, "unknown query in subtitle control" );
341             return VLC_EGENERIC;
342     }
343 }
344
345 /*****************************************************************************
346  * Demux: Send subtitle to decoder
347  *****************************************************************************/
348 static int Demux( demux_t *p_demux )
349 {
350     demux_sys_t *p_sys = p_demux->p_sys;
351     int64_t i_maxdate;
352     int i, i_read;
353
354     for( i = 0; i < p_sys->i_tracks; i++ )
355     {
356 #define tk p_sys->track[i]
357         if( tk.i_current_subtitle >= tk.i_subtitles )
358             continue;
359
360         i_maxdate = p_sys->i_next_demux_date;
361         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
362         {
363             /* Should not happen */
364             i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
365         }
366
367         while( tk.i_current_subtitle < tk.i_subtitles &&
368                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
369         {
370             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
371             block_t *p_block;
372             int i_size = 0;
373
374             /* first compute SPU size */
375             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
376             {
377                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
378             }
379             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
380
381             /* Seek at the right place */
382             if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
383             {
384                 msg_Warn( p_demux,
385                           "cannot seek in the VobSub to the correct time %d", i_pos );
386                 tk.i_current_subtitle++;
387                 continue;
388             }
389
390             /* allocate a packet */
391             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
392             {
393                 tk.i_current_subtitle++;
394                 continue;
395             }
396
397             /* read data */
398             i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
399             if( i_read <= 6 )
400             {
401                 block_Release( p_block );
402                 tk.i_current_subtitle++;
403                 continue;
404             }
405             p_block->i_buffer = i_read;
406
407             /* pts */
408             p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
409
410             /* demux this block */
411             DemuxVobSub( p_demux, p_block );
412
413             tk.i_current_subtitle++;
414         }
415 #undef tk
416     }
417
418     /* */
419     p_sys->i_next_demux_date = 0;
420
421     return 1;
422 }
423
424 static int TextLoad( text_t *txt, stream_t *s )
425 {
426     char **lines = NULL;
427     size_t n = 0;
428
429     /* load the complete file */
430     for( ;; )
431     {
432         char *psz = stream_ReadLine( s );
433         char **ppsz_new;
434
435         if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
436             break;
437
438         ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
439         if( ppsz_new == NULL )
440         {
441             free( psz );
442             break;
443         }
444         lines = ppsz_new;
445         lines[n++] = psz;
446     }
447
448     txt->i_line_count = n;
449     txt->i_line       = 0;
450     txt->line         = lines;
451
452     return VLC_SUCCESS;
453 }
454
455 static void TextUnload( text_t *txt )
456 {
457     int i;
458
459     for( i = 0; i < txt->i_line_count; i++ )
460         free( txt->line[i] );
461
462     free( txt->line );
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[3];
521             int i_track_id;
522             es_format_t fmt;
523
524             /* Lets start a new track */
525             if( sscanf( line, "id: %2s, index: %d",
526                         language, &i_track_id ) == 2 )
527             {
528                 p_sys->i_tracks++;
529                 p_sys->track = xrealloc( p_sys->track,
530                           sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
531                 language[2] = '\0';
532
533                 /* Init the track */
534                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
535                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
536                 current_tk->i_current_subtitle = 0;
537                 current_tk->i_subtitles = 0;
538                 current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );;
539                 current_tk->i_track_id = i_track_id;
540                 current_tk->i_delay = (int64_t)0;
541
542                 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
543                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
544                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
545                 fmt.psz_language = language;
546                 if( p_sys->b_palette )
547                 {
548                     fmt.subs.spu.palette[0] = 0xBeef;
549                     memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
550                 }
551
552                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
553                 msg_Dbg( p_demux, "new vobsub track detected" );
554             }
555             else
556             {
557                 msg_Warn( p_demux, "reading new track failed" );
558             }
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_New( p_demux, i_size );
678         memcpy( p_pkt->p_buffer, p, i_size);
679         p += i_size;
680
681         i_id = ps_pkt_id( p_pkt );
682         if( (i_id&0xffe0) != 0xbd20 ||
683             ps_pkt_parse_pes( p_pkt, 1 ) )
684         {
685             block_Release( p_pkt );
686             continue;
687         }
688         i_spu = i_id&0x1f;
689         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
690
691         for( i = 0; i < p_sys->i_tracks; i++ )
692         {
693             vobsub_track_t *p_tk = &p_sys->track[i];
694
695             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
696             p_pkt->i_length = 0;
697
698             if( p_tk->p_es && p_tk->i_track_id == i_spu )
699             {
700                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
701                 p_bk->i_pts = VLC_TS_INVALID;     /*only first packet has a pts */
702                 break;
703             }
704         }
705         if( i >= p_sys->i_tracks )
706         {
707             block_Release( p_pkt );
708         }
709     }
710
711     return VLC_SUCCESS;
712 }
713