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