]> git.sesse.net Git - vlc/blob - modules/demux/vobsub.c
vobsub: strdup memleak (stream_Add copies the format)
[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     p_sys->i_length = 0;
156     p_sys->p_vobsub_stream = NULL;
157     p_sys->i_tracks = 0;
158     p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
159     p_sys->i_original_frame_width = -1;
160     p_sys->i_original_frame_height = -1;
161     p_sys->b_palette = false;
162     memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
163
164     /* Load the whole file */
165     TextLoad( &p_sys->txt, p_demux->s );
166
167     /* Parse it */
168     ParseVobSubIDX( p_demux );
169
170     /* Unload */
171     TextUnload( &p_sys->txt );
172
173     /* Find the total length of the vobsubs */
174     if( p_sys->i_tracks > 0 )
175     {
176         int i;
177         for( i = 0; i < p_sys->i_tracks; i++ )
178         {
179             if( p_sys->track[i].i_subtitles > 1 )
180             {
181                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
182                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
183             }
184         }
185     }
186
187     if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path ) == -1 )
188     {
189         free( p_sys );
190         return VLC_EGENERIC;
191     }
192     i_len = strlen( psz_vobname );
193     if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
194
195     /* open file */
196     p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
197     if( p_sys->p_vobsub_stream == NULL )
198     {
199         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
200                  psz_vobname );
201         free( psz_vobname );
202         free( p_sys );
203         return VLC_EGENERIC;
204     }
205     free( psz_vobname );
206
207     return VLC_SUCCESS;
208 }
209
210 /*****************************************************************************
211  * Close: Close subtitle demux
212  *****************************************************************************/
213 static void Close( vlc_object_t *p_this )
214 {
215     int i;
216     demux_t *p_demux = (demux_t*)p_this;
217     demux_sys_t *p_sys = p_demux->p_sys;
218
219     /* Clean all subs from all tracks */
220     for( i = 0; i < p_sys->i_tracks; i++ )
221         free( p_sys->track[i].p_subtitles );
222
223     free( p_sys->track );
224
225     if( p_sys->p_vobsub_stream )
226         stream_Delete( p_sys->p_vobsub_stream );
227
228     free( p_sys );
229 }
230
231 /*****************************************************************************
232  * Control:
233  *****************************************************************************/
234 static int Control( demux_t *p_demux, int i_query, va_list args )
235 {
236     demux_sys_t *p_sys = p_demux->p_sys;
237     int64_t *pi64, i64;
238     int i;
239     double *pf, f;
240
241     switch( i_query )
242     {
243         case DEMUX_GET_LENGTH:
244             pi64 = (int64_t*)va_arg( args, int64_t * );
245             *pi64 = (int64_t) p_sys->i_length;
246             return VLC_SUCCESS;
247
248         case DEMUX_GET_TIME:
249             pi64 = (int64_t*)va_arg( args, int64_t * );
250             for( i = 0; i < p_sys->i_tracks; i++ )
251             {
252                 bool b_selected;
253                 /* Check the ES is selected */
254                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
255                                 p_sys->track[i].p_es, &b_selected );
256                 if( b_selected ) break;
257             }
258             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
259             {
260                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
261                 return VLC_SUCCESS;
262             }
263             return VLC_EGENERIC;
264
265         case DEMUX_SET_TIME:
266             i64 = (int64_t)va_arg( args, int64_t );
267             for( i = 0; i < p_sys->i_tracks; i++ )
268             {
269                 p_sys->track[i].i_current_subtitle = 0;
270                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
271                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
272                 {
273                     p_sys->track[i].i_current_subtitle++;
274                 }
275
276                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
277                     return VLC_EGENERIC;
278             }
279             return VLC_SUCCESS;
280
281         case DEMUX_GET_POSITION:
282             pf = (double*)va_arg( args, double * );
283             for( i = 0; i < p_sys->i_tracks; i++ )
284             {
285                 bool b_selected;
286                 /* Check the ES is selected */
287                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
288                                 p_sys->track[i].p_es, &b_selected );
289                 if( b_selected ) break;
290             }
291             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
292             {
293                 *pf = 1.0;
294             }
295             else if( p_sys->track[i].i_subtitles > 0 )
296             {
297                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
298                       (double)p_sys->i_length;
299             }
300             else
301             {
302                 *pf = 0.0;
303             }
304             return VLC_SUCCESS;
305
306         case DEMUX_SET_POSITION:
307             f = (double)va_arg( args, double );
308             i64 = (int64_t) f * p_sys->i_length;
309
310             for( i = 0; i < p_sys->i_tracks; i++ )
311             {
312                 p_sys->track[i].i_current_subtitle = 0;
313                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
314                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
315                 {
316                     p_sys->track[i].i_current_subtitle++;
317                 }
318                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
319                     return VLC_EGENERIC;
320             }
321             return VLC_SUCCESS;
322
323         case DEMUX_SET_NEXT_DEMUX_TIME:
324             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
325             return VLC_SUCCESS;
326
327         case DEMUX_GET_FPS:
328         case DEMUX_GET_META:
329         case DEMUX_GET_TITLE_INFO:
330         case DEMUX_HAS_UNSUPPORTED_META:
331         case DEMUX_GET_ATTACHMENTS:
332         case DEMUX_CAN_RECORD:
333             return VLC_EGENERIC;
334
335         default:
336             msg_Warn( p_demux, "unknown query in subtitle control" );
337             return VLC_EGENERIC;
338     }
339 }
340
341 /*****************************************************************************
342  * Demux: Send subtitle to decoder
343  *****************************************************************************/
344 static int Demux( demux_t *p_demux )
345 {
346     demux_sys_t *p_sys = p_demux->p_sys;
347     int64_t i_maxdate;
348     int i, i_read;
349
350     for( i = 0; i < p_sys->i_tracks; i++ )
351     {
352 #define tk p_sys->track[i]
353         if( tk.i_current_subtitle >= tk.i_subtitles )
354             continue;
355
356         i_maxdate = p_sys->i_next_demux_date;
357         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
358         {
359             /* Should not happen */
360             i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
361         }
362
363         while( tk.i_current_subtitle < tk.i_subtitles &&
364                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
365         {
366             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
367             block_t *p_block;
368             int i_size = 0;
369
370             /* first compute SPU size */
371             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
372             {
373                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
374             }
375             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
376
377             /* Seek at the right place */
378             if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
379             {
380                 msg_Warn( p_demux,
381                           "cannot seek in the VobSub to the correct time %d", i_pos );
382                 tk.i_current_subtitle++;
383                 continue;
384             }
385
386             /* allocate a packet */
387             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
388             {
389                 tk.i_current_subtitle++;
390                 continue;
391             }
392
393             /* read data */
394             i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
395             if( i_read <= 6 )
396             {
397                 block_Release( p_block );
398                 tk.i_current_subtitle++;
399                 continue;
400             }
401             p_block->i_buffer = i_read;
402
403             /* pts */
404             p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
405
406             /* demux this block */
407             DemuxVobSub( p_demux, p_block );
408
409             tk.i_current_subtitle++;
410         }
411 #undef tk
412     }
413
414     /* */
415     p_sys->i_next_demux_date = 0;
416
417     return 1;
418 }
419
420 static int TextLoad( text_t *txt, stream_t *s )
421 {
422     char **lines = NULL;
423     size_t n = 0;
424
425     /* load the complete file */
426     for( ;; )
427     {
428         char *psz = stream_ReadLine( s );
429         char **ppsz_new;
430
431         if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
432             break;
433
434         ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
435         if( ppsz_new == NULL )
436         {
437             free( psz );
438             break;
439         }
440         lines = ppsz_new;
441         lines[n++] = psz;
442     }
443
444     txt->i_line_count = n;
445     txt->i_line       = 0;
446     txt->line         = lines;
447
448     return VLC_SUCCESS;
449 }
450
451 static void TextUnload( text_t *txt )
452 {
453     int i;
454
455     for( i = 0; i < txt->i_line_count; i++ )
456         free( txt->line[i] );
457
458     free( txt->line );
459     txt->i_line       = 0;
460     txt->i_line_count = 0;
461 }
462
463 static char *TextGetLine( text_t *txt )
464 {
465     if( txt->i_line >= txt->i_line_count )
466         return( NULL );
467
468     return txt->line[txt->i_line++];
469 }
470
471 static int ParseVobSubIDX( demux_t *p_demux )
472 {
473     demux_sys_t *p_sys = p_demux->p_sys;
474     text_t      *txt = &p_sys->txt;
475     char        *line;
476     vobsub_track_t *current_tk = NULL;
477
478     for( ;; )
479     {
480         if( ( line = TextGetLine( txt ) ) == NULL )
481         {
482             return( VLC_EGENERIC );
483         }
484
485         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
486         {
487             continue;
488         }
489         else if( !strncmp( "size:", line, 5 ) )
490         {
491             /* Store the original size of the video */
492             if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
493                                    &p_sys->i_original_frame_height ) == VLC_SUCCESS )
494             {
495                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
496             }
497             else
498             {
499                 msg_Warn( p_demux, "reading original frame size failed" );
500             }
501         }
502         else if( !strncmp( "palette:", line, 8 ) )
503         {
504             if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
505             {
506                 p_sys->b_palette = true;
507                 msg_Dbg( p_demux, "vobsub palette read" );
508             }
509             else
510             {
511                 msg_Warn( p_demux, "reading original palette failed" );
512             }
513         }
514         else if( !strncmp( "id:", line, 3 ) )
515         {
516             char language[3];
517             int i_track_id;
518             es_format_t fmt;
519
520             /* Lets start a new track */
521             if( sscanf( line, "id: %2s, index: %d",
522                         language, &i_track_id ) == 2 )
523             {
524                 p_sys->i_tracks++;
525                 p_sys->track = realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
526                 language[2] = '\0';
527
528                 /* Init the track */
529                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
530                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
531                 current_tk->i_current_subtitle = 0;
532                 current_tk->i_subtitles = 0;
533                 current_tk->p_subtitles = malloc( sizeof( subtitle_t ) );;
534                 current_tk->i_track_id = i_track_id;
535                 current_tk->i_delay = (int64_t)0;
536
537                 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
538                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
539                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
540                 fmt.psz_language = language;
541                 if( p_sys->b_palette )
542                 {
543                     fmt.subs.spu.palette[0] = 0xBeef;
544                     memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
545                 }
546
547                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
548                 msg_Dbg( p_demux, "new vobsub track detected" );
549             }
550             else
551             {
552                 msg_Warn( p_demux, "reading new track failed" );
553             }
554         }
555         else if( !strncmp( line, "timestamp:", 10 ) )
556         {
557             /*
558              * timestamp: [sign]hh:mm:ss:mss, filepos: loc
559              * loc is the hex location of the spu in the .sub file
560              */
561             int h, m, s, ms, count, loc = 0;
562             int i_sign = 1;
563             int64_t i_start, i_location = 0;
564
565             if( p_sys->i_tracks > 0 &&
566                 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
567                         &h, &count, &m, &s, &ms, &loc ) >= 5  )
568             {
569                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
570                 subtitle_t *current_sub;
571
572                 if( line[count-3] == '-' )
573                 {
574                     i_sign = -1;
575                     h = -h;
576                 }
577                 i_start = (int64_t) ( h * 3600*1000 +
578                             m * 60*1000 +
579                             s * 1000 +
580                             ms ) * 1000;
581                 i_location = loc;
582
583                 current_tk->i_subtitles++;
584                 current_tk->p_subtitles = realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
585                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
586
587                 current_sub->i_start = i_start * i_sign;
588                 current_sub->i_start += current_tk->i_delay;
589                 current_sub->i_vobsub_location = i_location;
590             }
591             else
592             {
593                 msg_Warn( p_demux, "reading timestamp failed" );
594             }
595         }
596         else if( !strncasecmp( line, "delay:", 6 ) )
597         {
598             /*
599              * delay: [sign]hh:mm:ss:mss
600              */
601             int h, m, s, ms, count = 0;
602             int i_sign = 1;
603             int64_t i_gap = 0;
604
605             if( p_sys->i_tracks > 0 &&
606                 sscanf( line, "%*celay: %d%n:%d:%d:%d",
607                         &h, &count, &m, &s, &ms ) >= 4 )
608             {
609                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
610                 if( line[count-3] == '-' )
611                 {
612                     i_sign = -1;
613                     h = -h;
614                 }
615                 i_gap = (int64_t) ( h * 3600*1000 +
616                             m * 60*1000 +
617                             s * 1000 +
618                             ms ) * 1000;
619
620                 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
621                 msg_Dbg( p_demux, "sign: %+d gap: %+lld global delay: %+lld",
622                          i_sign, (long long)i_gap,
623                          (long long)current_tk->i_delay  );
624             }
625             else
626             {
627                 msg_Warn( p_demux, "reading delay failed" );
628             }
629         }
630     }
631     return( 0 );
632 }
633
634 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
635 {
636     demux_sys_t *p_sys = p_demux->p_sys;
637     uint8_t     *p = p_bk->p_buffer;
638     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
639     int i;
640
641     while( p + 6 < p_end )
642     {
643         int i_size = ps_pkt_size( p, p_end - p );
644         block_t *p_pkt;
645         int      i_id;
646         int      i_spu;
647
648         if( i_size <= 0 )
649             break;
650
651         if( i_size > p_end - p )
652         {
653             msg_Warn( p_demux, "broken PES size" );
654             break;
655         }
656
657         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
658         {
659             msg_Warn( p_demux, "invalid PES" );
660             break;
661         }
662
663         if( p[3] != 0xbd )
664         {
665             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
666             p += i_size;
667             continue;
668         }
669
670         /* Create a block */
671         p_pkt = block_New( p_demux, i_size );
672         memcpy( p_pkt->p_buffer, p, i_size);
673         p += i_size;
674
675         i_id = ps_pkt_id( p_pkt );
676         if( (i_id&0xffe0) != 0xbd20 ||
677             ps_pkt_parse_pes( p_pkt, 1 ) )
678         {
679             block_Release( p_pkt );
680             continue;
681         }
682         i_spu = i_id&0x1f;
683         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
684
685         for( i = 0; i < p_sys->i_tracks; i++ )
686         {
687             vobsub_track_t *p_tk = &p_sys->track[i];
688
689             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
690             p_pkt->i_length = 0;
691
692             if( p_tk->p_es && p_tk->i_track_id == i_spu )
693             {
694                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
695                 p_bk->i_pts = 0;     /*only first packet has a pts */
696                 break;
697             }
698         }
699         if( i >= p_sys->i_tracks )
700         {
701             block_Release( p_pkt );
702         }
703     }
704
705     return VLC_SUCCESS;
706 }
707