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