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