]> git.sesse.net Git - vlc/blob - modules/demux/vobsub.c
grain: fix clobber list
[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
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_format_t fmt;
79     es_out_id_t *p_es;
80     int         i_track_id;
81
82     int         i_current_subtitle;
83     int         i_subtitles;
84     subtitle_t  *p_subtitles;
85
86     int64_t     i_delay;
87 } vobsub_track_t;
88
89 struct demux_sys_t
90 {
91     int64_t        i_next_demux_date;
92     int64_t        i_length;
93
94     text_t         txt;
95     stream_t       *p_vobsub_stream;
96
97     /* all tracks */
98     int            i_tracks;
99     vobsub_track_t *track;
100
101     int            i_original_frame_width;
102     int            i_original_frame_height;
103     bool           b_palette;
104     uint32_t       palette[16];
105 };
106
107
108 static int Demux( demux_t * );
109 static int Control( demux_t *, int, va_list );
110
111 static int  TextLoad( text_t *, stream_t *s );
112 static void TextUnload( text_t * );
113 static int ParseVobSubIDX( demux_t * );
114 static int DemuxVobSub( demux_t *, block_t *);
115
116 /*****************************************************************************
117  * Module initializer
118  *****************************************************************************/
119 static int Open ( vlc_object_t *p_this )
120 {
121     demux_t     *p_demux = (demux_t*)p_this;
122     demux_sys_t *p_sys;
123     char *psz_vobname, *s;
124     int i_len;
125
126     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
127     {
128         if( !strcasestr( s, "# VobSub index file" ) )
129         {
130             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
131             free( s );
132             if( stream_Seek( p_demux->s, 0 ) )
133             {
134                 msg_Warn( p_demux, "failed to rewind" );
135             }
136             return VLC_EGENERIC;
137         }
138         free( s );
139     }
140     else
141     {
142         msg_Dbg( p_demux, "could not read vobsub IDX file" );
143         return VLC_EGENERIC;
144     }
145
146     /* */
147     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
148     if( unlikely( !p_sys ) )
149         return VLC_ENOMEM;
150
151     p_sys->i_length = 0;
152     p_sys->p_vobsub_stream = NULL;
153     p_sys->i_tracks = 0;
154     p_sys->track = malloc( sizeof( vobsub_track_t ) );
155     if( unlikely( !p_sys->track ) )
156         goto error;
157     p_sys->i_original_frame_width = -1;
158     p_sys->i_original_frame_height = -1;
159     p_sys->b_palette = false;
160     memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
161
162     /* Load the whole file */
163     TextLoad( &p_sys->txt, p_demux->s );
164
165     /* Parse it */
166     ParseVobSubIDX( p_demux );
167
168     /* Unload */
169     TextUnload( &p_sys->txt );
170
171     /* Find the total length of the vobsubs */
172     if( p_sys->i_tracks > 0 )
173     {
174         for( int i = 0; i < p_sys->i_tracks; i++ )
175         {
176             if( p_sys->track[i].i_subtitles > 1 )
177             {
178                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
179                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
180             }
181         }
182     }
183
184     if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1 )
185         goto error;
186
187     i_len = strlen( psz_vobname );
188     if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
189
190     /* open file */
191     p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
192     if( p_sys->p_vobsub_stream == NULL )
193     {
194         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
195                  psz_vobname );
196         free( psz_vobname );
197         goto error;
198     }
199     free( psz_vobname );
200
201     p_demux->pf_demux = Demux;
202     p_demux->pf_control = Control;
203
204     return VLC_SUCCESS;
205
206 error:
207     /* Clean all subs from all tracks */
208     for( int i = 0; i < p_sys->i_tracks; i++ )
209         free( p_sys->track[i].p_subtitles );
210     free( p_sys->track );
211     free( p_sys );
212
213     return VLC_EGENERIC;
214 }
215
216 /*****************************************************************************
217  * Close: Close subtitle demux
218  *****************************************************************************/
219 static void Close( vlc_object_t *p_this )
220 {
221     demux_t *p_demux = (demux_t*)p_this;
222     demux_sys_t *p_sys = p_demux->p_sys;
223
224     if( p_sys->p_vobsub_stream )
225         stream_Delete( p_sys->p_vobsub_stream );
226
227     /* Clean all subs from all tracks */
228     for( int i = 0; i < p_sys->i_tracks; i++ )
229         free( p_sys->track[i].p_subtitles );
230     free( p_sys->track );
231     free( p_sys );
232 }
233
234 /*****************************************************************************
235  * Control:
236  *****************************************************************************/
237 static int Control( demux_t *p_demux, int i_query, va_list args )
238 {
239     demux_sys_t *p_sys = p_demux->p_sys;
240     int64_t *pi64, i64;
241     int i;
242     double *pf, f;
243
244     switch( i_query )
245     {
246         case DEMUX_GET_LENGTH:
247             pi64 = (int64_t*)va_arg( args, int64_t * );
248             *pi64 = (int64_t) p_sys->i_length;
249             return VLC_SUCCESS;
250
251         case DEMUX_GET_TIME:
252             pi64 = (int64_t*)va_arg( args, int64_t * );
253             for( i = 0; i < p_sys->i_tracks; i++ )
254             {
255                 bool b_selected;
256                 /* Check the ES is selected */
257                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
258                                 p_sys->track[i].p_es, &b_selected );
259                 if( b_selected ) break;
260             }
261             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
262             {
263                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
264                 return VLC_SUCCESS;
265             }
266             return VLC_EGENERIC;
267
268         case DEMUX_SET_TIME:
269             i64 = (int64_t)va_arg( args, int64_t );
270             for( i = 0; i < p_sys->i_tracks; i++ )
271             {
272                 p_sys->track[i].i_current_subtitle = 0;
273                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
274                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
275                 {
276                     p_sys->track[i].i_current_subtitle++;
277                 }
278
279                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
280                     return VLC_EGENERIC;
281             }
282             return VLC_SUCCESS;
283
284         case DEMUX_GET_POSITION:
285             pf = (double*)va_arg( args, double * );
286             for( i = 0; i < p_sys->i_tracks; i++ )
287             {
288                 bool b_selected;
289                 /* Check the ES is selected */
290                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
291                                 p_sys->track[i].p_es, &b_selected );
292                 if( b_selected ) break;
293             }
294             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
295             {
296                 *pf = 1.0;
297             }
298             else if( p_sys->track[i].i_subtitles > 0 )
299             {
300                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
301                       (double)p_sys->i_length;
302             }
303             else
304             {
305                 *pf = 0.0;
306             }
307             return VLC_SUCCESS;
308
309         case DEMUX_SET_POSITION:
310             f = (double)va_arg( args, double );
311             i64 = (int64_t) f * p_sys->i_length;
312
313             for( i = 0; i < p_sys->i_tracks; i++ )
314             {
315                 p_sys->track[i].i_current_subtitle = 0;
316                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
317                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
318                 {
319                     p_sys->track[i].i_current_subtitle++;
320                 }
321                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
322                     return VLC_EGENERIC;
323             }
324             return VLC_SUCCESS;
325
326         case DEMUX_SET_NEXT_DEMUX_TIME:
327             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
328             return VLC_SUCCESS;
329
330         case DEMUX_GET_PTS_DELAY:
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_read;
353
354     for( int 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     for( int i = 0; i < txt->i_line_count; i++ )
458         free( txt->line[i] );
459     free( txt->line );
460
461     txt->i_line       = 0;
462     txt->i_line_count = 0;
463 }
464
465 static char *TextGetLine( text_t *txt )
466 {
467     if( txt->i_line >= txt->i_line_count )
468         return( NULL );
469
470     return txt->line[txt->i_line++];
471 }
472
473 static int ParseVobSubIDX( demux_t *p_demux )
474 {
475     demux_sys_t *p_sys = p_demux->p_sys;
476     text_t      *txt = &p_sys->txt;
477     char        *line;
478     vobsub_track_t *current_tk = NULL;
479
480     for( ;; )
481     {
482         if( ( line = TextGetLine( txt ) ) == NULL )
483         {
484             return( VLC_EGENERIC );
485         }
486
487         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
488         {
489             continue;
490         }
491         else if( !strncmp( "size:", line, 5 ) )
492         {
493             /* Store the original size of the video */
494             if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
495                                    &p_sys->i_original_frame_height ) == VLC_SUCCESS )
496             {
497                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
498             }
499             else
500             {
501                 msg_Warn( p_demux, "reading original frame size failed" );
502             }
503         }
504         else if( !strncmp( "palette:", line, 8 ) )
505         {
506             if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
507             {
508                 p_sys->b_palette = true;
509                 msg_Dbg( p_demux, "vobsub palette read" );
510             }
511             else
512             {
513                 msg_Warn( p_demux, "reading original palette failed" );
514             }
515         }
516         else if( !strncmp( "id:", line, 3 ) )
517         {
518             char language[33]; /* Usually 2 or 3 letters, sometimes more.
519                                   Spec (or lack of) doesn't define any limit */
520             int i_track_id;
521             es_format_t fmt;
522
523             /* Lets start a new track */
524             if( sscanf( line, "id: %32[^ ,], index: %d",
525                         language, &i_track_id ) == 2 )
526             {
527                 p_sys->i_tracks++;
528                 p_sys->track = xrealloc( p_sys->track,
529                           sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
530
531                 /* Init the track */
532                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
533                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
534                 current_tk->i_current_subtitle = 0;
535                 current_tk->i_subtitles = 0;
536                 current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );
537                 current_tk->i_track_id = i_track_id;
538                 current_tk->i_delay = (int64_t)0;
539
540                 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
541                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
542                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
543                 fmt.psz_language = language;
544                 if( p_sys->b_palette )
545                 {
546                     fmt.subs.spu.palette[0] = 0xBeef;
547                     memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
548                 }
549
550                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
551                 msg_Dbg( p_demux, "new vobsub track detected" );
552             }
553             else
554             {
555                 msg_Warn( p_demux, "reading new track failed" );
556             }
557         }
558         else if( !strncmp( line, "timestamp:", 10 ) )
559         {
560             /*
561              * timestamp: [sign]hh:mm:ss:mss, filepos: loc
562              * loc is the hex location of the spu in the .sub file
563              */
564             int h, m, s, ms, count, loc = 0;
565             int i_sign = 1;
566             int64_t i_start, i_location = 0;
567
568             if( p_sys->i_tracks > 0 &&
569                 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
570                         &h, &count, &m, &s, &ms, &loc ) >= 5  )
571             {
572                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
573                 subtitle_t *current_sub;
574
575                 if( line[count-3] == '-' )
576                 {
577                     i_sign = -1;
578                     h = -h;
579                 }
580                 i_start = (int64_t) ( h * 3600*1000 +
581                             m * 60*1000 +
582                             s * 1000 +
583                             ms ) * 1000;
584                 i_location = loc;
585
586                 current_tk->i_subtitles++;
587                 current_tk->p_subtitles =
588                     xrealloc( current_tk->p_subtitles,
589                       sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
590                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
591
592                 current_sub->i_start = i_start * i_sign;
593                 current_sub->i_start += current_tk->i_delay;
594                 current_sub->i_vobsub_location = i_location;
595             }
596             else
597             {
598                 msg_Warn( p_demux, "reading timestamp failed" );
599             }
600         }
601         else if( !strncasecmp( line, "delay:", 6 ) )
602         {
603             /*
604              * delay: [sign]hh:mm:ss:mss
605              */
606             int h, m, s, ms, count = 0;
607             int i_sign = 1;
608             int64_t i_gap = 0;
609
610             if( p_sys->i_tracks > 0 &&
611                 sscanf( line, "%*celay: %d%n:%d:%d:%d",
612                         &h, &count, &m, &s, &ms ) >= 4 )
613             {
614                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
615                 if( line[count-3] == '-' )
616                 {
617                     i_sign = -1;
618                     h = -h;
619                 }
620                 i_gap = (int64_t) ( h * 3600*1000 +
621                             m * 60*1000 +
622                             s * 1000 +
623                             ms ) * 1000;
624
625                 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
626                 msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
627                          i_sign, i_gap, current_tk->i_delay );
628             }
629             else
630             {
631                 msg_Warn( p_demux, "reading delay failed" );
632             }
633         }
634     }
635     return( 0 );
636 }
637
638 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
639 {
640     demux_sys_t *p_sys = p_demux->p_sys;
641     uint8_t     *p = p_bk->p_buffer;
642     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
643     int i;
644
645     while( p + 6 < p_end )
646     {
647         int i_size = ps_pkt_size( p, p_end - p );
648         block_t *p_pkt;
649         int      i_id;
650         int      i_spu;
651
652         if( i_size <= 0 )
653             break;
654
655         if( i_size > p_end - p )
656         {
657             msg_Warn( p_demux, "broken PES size" );
658             break;
659         }
660
661         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
662         {
663             msg_Warn( p_demux, "invalid PES" );
664             break;
665         }
666
667         if( p[3] != 0xbd )
668         {
669             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
670             p += i_size;
671             continue;
672         }
673
674         /* Create a block */
675         p_pkt = block_New( p_demux, i_size );
676         memcpy( p_pkt->p_buffer, p, i_size);
677         p += i_size;
678
679         i_id = ps_pkt_id( p_pkt );
680         if( (i_id&0xffe0) != 0xbd20 ||
681             ps_pkt_parse_pes( p_pkt, 1 ) )
682         {
683             block_Release( p_pkt );
684             continue;
685         }
686         i_spu = i_id&0x1f;
687         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
688
689         for( i = 0; i < p_sys->i_tracks; i++ )
690         {
691             vobsub_track_t *p_tk = &p_sys->track[i];
692
693             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
694             p_pkt->i_length = 0;
695
696             if( p_tk->p_es && p_tk->i_track_id == i_spu )
697             {
698                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
699                 p_bk->i_pts = VLC_TS_INVALID;     /*only first packet has a pts */
700                 break;
701             }
702         }
703         if( i >= p_sys->i_tracks )
704         {
705             block_Release( p_pkt );
706         }
707     }
708
709     return VLC_SUCCESS;
710 }
711