]> git.sesse.net Git - vlc/blob - modules/demux/vobsub.c
Used VLC_TS_INVALID/0 in vobsub demuxer.
[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 <errno.h>
36 #include <sys/types.h>
37 #include <limits.h>
38
39 #include <vlc_demux.h>
40 #include <vlc_charset.h>
41
42 #include "ps.h"
43 #include "vobsub.h"
44
45 #define MAX_LINE 8192
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open ( vlc_object_t *p_this );
51 static void Close( vlc_object_t *p_this );
52
53 vlc_module_begin ()
54     set_description( N_("Vobsub subtitles parser") )
55     set_category( CAT_INPUT )
56     set_subcategory( SUBCAT_INPUT_DEMUX )
57     set_capability( "demux", 1 )
58
59     set_callbacks( Open, Close )
60
61     add_shortcut( "vobsub" )
62     add_shortcut( "subtitle" )
63 vlc_module_end ()
64
65 /*****************************************************************************
66  * Prototypes:
67  *****************************************************************************/
68
69 typedef struct
70 {
71     int     i_line_count;
72     int     i_line;
73     char    **line;
74 } text_t;
75 static int  TextLoad( text_t *, stream_t *s );
76 static void TextUnload( text_t * );
77
78 typedef struct
79 {
80     int64_t i_start;
81     int     i_vobsub_location;
82 } subtitle_t;
83
84 typedef struct
85 {
86     es_format_t fmt;
87     es_out_id_t *p_es;
88     int         i_track_id;
89
90     int         i_current_subtitle;
91     int         i_subtitles;
92     subtitle_t  *p_subtitles;
93
94     int64_t     i_delay;
95 } vobsub_track_t;
96
97 struct demux_sys_t
98 {
99     int64_t     i_next_demux_date;
100     int64_t     i_length;
101
102     text_t      txt;
103     stream_t    *p_vobsub_stream;
104
105     /* all tracks */
106     int            i_tracks;
107     vobsub_track_t *track;
108
109     int         i_original_frame_width;
110     int         i_original_frame_height;
111     bool  b_palette;
112     uint32_t    palette[16];
113 };
114
115 static int Demux( demux_t * );
116 static int Control( demux_t *, int, va_list );
117
118 static int ParseVobSubIDX( demux_t * );
119 static int DemuxVobSub( demux_t *, block_t *);
120
121 /*****************************************************************************
122  * Module initializer
123  *****************************************************************************/
124 static int Open ( vlc_object_t *p_this )
125 {
126     demux_t     *p_demux = (demux_t*)p_this;
127     demux_sys_t *p_sys;
128     char *psz_vobname, *s;
129     int i_len;
130
131     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
132     {
133         if( !strcasestr( s, "# VobSub index file" ) )
134         {
135             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
136             free( s );
137             if( stream_Seek( p_demux->s, 0 ) )
138             {
139                 msg_Warn( p_demux, "failed to rewind" );
140             }
141             return VLC_EGENERIC;
142         }
143         free( s );
144
145     }
146     else
147     {
148         msg_Dbg( p_demux, "could not read vobsub IDX file" );
149         return VLC_EGENERIC;
150     }
151
152     p_demux->pf_demux = Demux;
153     p_demux->pf_control = Control;
154     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
155     if( unlikely( !p_sys ) )
156         return VLC_ENOMEM;
157     p_sys->i_length = 0;
158     p_sys->p_vobsub_stream = NULL;
159     p_sys->i_tracks = 0;
160     p_sys->track = malloc( sizeof( vobsub_track_t ) );
161     if( unlikely( !p_sys->track ) )
162     {
163         free( p_sys );
164         return VLC_ENOMEM;
165     }
166     p_sys->i_original_frame_width = -1;
167     p_sys->i_original_frame_height = -1;
168     p_sys->b_palette = false;
169     memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
170
171     /* Load the whole file */
172     TextLoad( &p_sys->txt, p_demux->s );
173
174     /* Parse it */
175     ParseVobSubIDX( p_demux );
176
177     /* Unload */
178     TextUnload( &p_sys->txt );
179
180     /* Find the total length of the vobsubs */
181     if( p_sys->i_tracks > 0 )
182     {
183         int i;
184         for( i = 0; i < p_sys->i_tracks; i++ )
185         {
186             if( p_sys->track[i].i_subtitles > 1 )
187             {
188                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
189                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
190             }
191         }
192     }
193
194     if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path ) == -1 )
195     {
196         free( p_sys );
197         return VLC_EGENERIC;
198     }
199     i_len = strlen( psz_vobname );
200     if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
201
202     /* open file */
203     p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
204     if( p_sys->p_vobsub_stream == NULL )
205     {
206         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
207                  psz_vobname );
208         free( psz_vobname );
209         free( p_sys );
210         return VLC_EGENERIC;
211     }
212     free( psz_vobname );
213
214     return VLC_SUCCESS;
215 }
216
217 /*****************************************************************************
218  * Close: Close subtitle demux
219  *****************************************************************************/
220 static void Close( vlc_object_t *p_this )
221 {
222     int i;
223     demux_t *p_demux = (demux_t*)p_this;
224     demux_sys_t *p_sys = p_demux->p_sys;
225
226     /* Clean all subs from all tracks */
227     for( i = 0; i < p_sys->i_tracks; i++ )
228         free( p_sys->track[i].p_subtitles );
229
230     free( p_sys->track );
231
232     if( p_sys->p_vobsub_stream )
233         stream_Delete( p_sys->p_vobsub_stream );
234
235     free( p_sys );
236 }
237
238 /*****************************************************************************
239  * Control:
240  *****************************************************************************/
241 static int Control( demux_t *p_demux, int i_query, va_list args )
242 {
243     demux_sys_t *p_sys = p_demux->p_sys;
244     int64_t *pi64, i64;
245     int i;
246     double *pf, f;
247
248     switch( i_query )
249     {
250         case DEMUX_GET_LENGTH:
251             pi64 = (int64_t*)va_arg( args, int64_t * );
252             *pi64 = (int64_t) p_sys->i_length;
253             return VLC_SUCCESS;
254
255         case DEMUX_GET_TIME:
256             pi64 = (int64_t*)va_arg( args, int64_t * );
257             for( i = 0; i < p_sys->i_tracks; i++ )
258             {
259                 bool b_selected;
260                 /* Check the ES is selected */
261                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
262                                 p_sys->track[i].p_es, &b_selected );
263                 if( b_selected ) break;
264             }
265             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
266             {
267                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
268                 return VLC_SUCCESS;
269             }
270             return VLC_EGENERIC;
271
272         case DEMUX_SET_TIME:
273             i64 = (int64_t)va_arg( args, int64_t );
274             for( i = 0; i < p_sys->i_tracks; i++ )
275             {
276                 p_sys->track[i].i_current_subtitle = 0;
277                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
278                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
279                 {
280                     p_sys->track[i].i_current_subtitle++;
281                 }
282
283                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
284                     return VLC_EGENERIC;
285             }
286             return VLC_SUCCESS;
287
288         case DEMUX_GET_POSITION:
289             pf = (double*)va_arg( args, double * );
290             for( i = 0; i < p_sys->i_tracks; i++ )
291             {
292                 bool b_selected;
293                 /* Check the ES is selected */
294                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
295                                 p_sys->track[i].p_es, &b_selected );
296                 if( b_selected ) break;
297             }
298             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
299             {
300                 *pf = 1.0;
301             }
302             else if( p_sys->track[i].i_subtitles > 0 )
303             {
304                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
305                       (double)p_sys->i_length;
306             }
307             else
308             {
309                 *pf = 0.0;
310             }
311             return VLC_SUCCESS;
312
313         case DEMUX_SET_POSITION:
314             f = (double)va_arg( args, double );
315             i64 = (int64_t) f * p_sys->i_length;
316
317             for( i = 0; i < p_sys->i_tracks; i++ )
318             {
319                 p_sys->track[i].i_current_subtitle = 0;
320                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
321                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
322                 {
323                     p_sys->track[i].i_current_subtitle++;
324                 }
325                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
326                     return VLC_EGENERIC;
327             }
328             return VLC_SUCCESS;
329
330         case DEMUX_SET_NEXT_DEMUX_TIME:
331             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
332             return VLC_SUCCESS;
333
334         case DEMUX_GET_FPS:
335         case DEMUX_GET_META:
336         case DEMUX_GET_TITLE_INFO:
337         case DEMUX_HAS_UNSUPPORTED_META:
338         case DEMUX_GET_ATTACHMENTS:
339         case DEMUX_CAN_RECORD:
340             return VLC_EGENERIC;
341
342         default:
343             msg_Warn( p_demux, "unknown query in subtitle control" );
344             return VLC_EGENERIC;
345     }
346 }
347
348 /*****************************************************************************
349  * Demux: Send subtitle to decoder
350  *****************************************************************************/
351 static int Demux( demux_t *p_demux )
352 {
353     demux_sys_t *p_sys = p_demux->p_sys;
354     int64_t i_maxdate;
355     int i, i_read;
356
357     for( i = 0; i < p_sys->i_tracks; i++ )
358     {
359 #define tk p_sys->track[i]
360         if( tk.i_current_subtitle >= tk.i_subtitles )
361             continue;
362
363         i_maxdate = p_sys->i_next_demux_date;
364         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
365         {
366             /* Should not happen */
367             i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
368         }
369
370         while( tk.i_current_subtitle < tk.i_subtitles &&
371                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
372         {
373             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
374             block_t *p_block;
375             int i_size = 0;
376
377             /* first compute SPU size */
378             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
379             {
380                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
381             }
382             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
383
384             /* Seek at the right place */
385             if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
386             {
387                 msg_Warn( p_demux,
388                           "cannot seek in the VobSub to the correct time %d", i_pos );
389                 tk.i_current_subtitle++;
390                 continue;
391             }
392
393             /* allocate a packet */
394             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
395             {
396                 tk.i_current_subtitle++;
397                 continue;
398             }
399
400             /* read data */
401             i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
402             if( i_read <= 6 )
403             {
404                 block_Release( p_block );
405                 tk.i_current_subtitle++;
406                 continue;
407             }
408             p_block->i_buffer = i_read;
409
410             /* pts */
411             p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
412
413             /* demux this block */
414             DemuxVobSub( p_demux, p_block );
415
416             tk.i_current_subtitle++;
417         }
418 #undef tk
419     }
420
421     /* */
422     p_sys->i_next_demux_date = 0;
423
424     return 1;
425 }
426
427 static int TextLoad( text_t *txt, stream_t *s )
428 {
429     char **lines = NULL;
430     size_t n = 0;
431
432     /* load the complete file */
433     for( ;; )
434     {
435         char *psz = stream_ReadLine( s );
436         char **ppsz_new;
437
438         if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
439             break;
440
441         ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
442         if( ppsz_new == NULL )
443         {
444             free( psz );
445             break;
446         }
447         lines = ppsz_new;
448         lines[n++] = psz;
449     }
450
451     txt->i_line_count = n;
452     txt->i_line       = 0;
453     txt->line         = lines;
454
455     return VLC_SUCCESS;
456 }
457
458 static void TextUnload( text_t *txt )
459 {
460     int i;
461
462     for( i = 0; i < txt->i_line_count; i++ )
463         free( txt->line[i] );
464
465     free( txt->line );
466     txt->i_line       = 0;
467     txt->i_line_count = 0;
468 }
469
470 static char *TextGetLine( text_t *txt )
471 {
472     if( txt->i_line >= txt->i_line_count )
473         return( NULL );
474
475     return txt->line[txt->i_line++];
476 }
477
478 static int ParseVobSubIDX( demux_t *p_demux )
479 {
480     demux_sys_t *p_sys = p_demux->p_sys;
481     text_t      *txt = &p_sys->txt;
482     char        *line;
483     vobsub_track_t *current_tk = NULL;
484
485     for( ;; )
486     {
487         if( ( line = TextGetLine( txt ) ) == NULL )
488         {
489             return( VLC_EGENERIC );
490         }
491
492         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
493         {
494             continue;
495         }
496         else if( !strncmp( "size:", line, 5 ) )
497         {
498             /* Store the original size of the video */
499             if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
500                                    &p_sys->i_original_frame_height ) == VLC_SUCCESS )
501             {
502                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
503             }
504             else
505             {
506                 msg_Warn( p_demux, "reading original frame size failed" );
507             }
508         }
509         else if( !strncmp( "palette:", line, 8 ) )
510         {
511             if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
512             {
513                 p_sys->b_palette = true;
514                 msg_Dbg( p_demux, "vobsub palette read" );
515             }
516             else
517             {
518                 msg_Warn( p_demux, "reading original palette failed" );
519             }
520         }
521         else if( !strncmp( "id:", line, 3 ) )
522         {
523             char language[3];
524             int i_track_id;
525             es_format_t fmt;
526
527             /* Lets start a new track */
528             if( sscanf( line, "id: %2s, index: %d",
529                         language, &i_track_id ) == 2 )
530             {
531                 p_sys->i_tracks++;
532                 p_sys->track = xrealloc( p_sys->track,
533                           sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
534                 language[2] = '\0';
535
536                 /* Init the track */
537                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
538                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
539                 current_tk->i_current_subtitle = 0;
540                 current_tk->i_subtitles = 0;
541                 current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );;
542                 current_tk->i_track_id = i_track_id;
543                 current_tk->i_delay = (int64_t)0;
544
545                 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
546                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
547                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
548                 fmt.psz_language = language;
549                 if( p_sys->b_palette )
550                 {
551                     fmt.subs.spu.palette[0] = 0xBeef;
552                     memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
553                 }
554
555                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
556                 msg_Dbg( p_demux, "new vobsub track detected" );
557             }
558             else
559             {
560                 msg_Warn( p_demux, "reading new track failed" );
561             }
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_New( p_demux, i_size );
681         memcpy( p_pkt->p_buffer, p, i_size);
682         p += i_size;
683
684         i_id = ps_pkt_id( p_pkt );
685         if( (i_id&0xffe0) != 0xbd20 ||
686             ps_pkt_parse_pes( p_pkt, 1 ) )
687         {
688             block_Release( p_pkt );
689             continue;
690         }
691         i_spu = i_id&0x1f;
692         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
693
694         for( i = 0; i < p_sys->i_tracks; i++ )
695         {
696             vobsub_track_t *p_tk = &p_sys->track[i];
697
698             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
699             p_pkt->i_length = 0;
700
701             if( p_tk->p_es && p_tk->i_track_id == i_spu )
702             {
703                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
704                 p_bk->i_pts = VLC_TS_INVALID;     /*only first packet has a pts */
705                 break;
706             }
707         }
708         if( i >= p_sys->i_tracks )
709         {
710             block_Release( p_pkt );
711         }
712     }
713
714     return VLC_SUCCESS;
715 }
716