]> git.sesse.net Git - vlc/blob - modules/demux/vobsub.c
* Fix the vobsub length.
[vlc] / modules / demux / vobsub.c
1 /*****************************************************************************
2  * subtitle.c: Demux vobsub files.
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/input.h>
35 #include "vlc_video.h"
36
37 #include "ps.h"
38
39 #define MAX_LINE 8192
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open ( vlc_object_t *p_this );
45 static void Close( vlc_object_t *p_this );
46
47 vlc_module_begin();
48     set_description( _("Vobsub subtitles demux") );
49     set_capability( "demux2", 1 );
50     
51     set_callbacks( Open, Close );
52
53     add_shortcut( "vobsub" );
54     add_shortcut( "subtitle" );
55 vlc_module_end();
56
57 /*****************************************************************************
58  * Prototypes:
59  *****************************************************************************/
60
61 typedef struct
62 {
63     int     i_line_count;
64     int     i_line;
65     char    **line;
66 } text_t;
67 static int  TextLoad( text_t *, stream_t *s );
68 static void TextUnload( text_t * );
69
70 typedef struct
71 {
72     mtime_t i_start;
73     int     i_vobsub_location;
74 } subtitle_t;
75
76 typedef struct
77 {
78     es_format_t fmt;
79     es_out_id_t *p_es;
80     int         i_track_id;
81     
82     int         i_current_subtitle;
83     int         i_subtitles;
84     subtitle_t  *p_subtitles;
85 } vobsub_track_t;
86
87 struct demux_sys_t
88 {
89     int64_t     i_next_demux_date;
90     int64_t     i_length;
91
92     text_t      txt;
93     FILE        *p_vobsub_file;
94     
95     /* all tracks */
96     int                     i_tracks;
97     vobsub_track_t          *track;
98     
99     int         i_original_frame_width;
100     int         i_original_frame_height;
101 };
102
103 static int Demux( demux_t * );
104 static int Control( demux_t *, int, va_list );
105
106 static int ParseVobSubIDX( demux_t * );
107 static int DemuxVobSub( demux_t *, block_t *);
108
109 /*****************************************************************************
110  * Module initializer
111  *****************************************************************************/
112 static int Open ( vlc_object_t *p_this )
113 {
114     demux_t     *p_demux = (demux_t*)p_this;
115     demux_sys_t *p_sys;
116     int i_len;
117     char *psz_vobname;
118
119     p_demux->pf_demux = Demux;
120     p_demux->pf_control = Control;
121     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
122     p_sys->i_length = 0;
123     p_sys->p_vobsub_file = NULL;
124     p_sys->i_tracks = 0;
125     p_sys->track = (vobsub_track_t*)malloc( sizeof( vobsub_track_t ) );
126     p_sys->i_original_frame_width = -1;
127     p_sys->i_original_frame_height = -1;
128
129     char *s = NULL;
130     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
131     {
132         if( !strcasestr( s, "# VobSub index file" ) )
133         {
134             msg_Err( p_demux, "this doesn't seem to be a vobsub file, bailing" );
135             free( s );
136             return VLC_EGENERIC;
137         }
138         free( s );
139         s = NULL;
140
141         if( stream_Seek( p_demux->s, 0 ) )
142         {
143             msg_Warn( p_demux, "failed to rewind" );
144         }
145     }
146     else
147     {
148         msg_Err( p_demux, "could not read vobsub IDX file" );
149         return VLC_EGENERIC;
150     }
151
152     /* Load the whole file */
153     TextLoad( &p_sys->txt, p_demux->s );
154
155     /* Parse it */
156     ParseVobSubIDX( p_demux );
157
158     /* Unload */
159     TextUnload( &p_sys->txt );
160
161     /* Find the total length of the vobsubs */
162     if( p_sys->i_tracks > 0 )
163     {
164         int i;
165         for( i = 0; i < p_sys->i_tracks )
166         {
167             if( p_sys->track[i]->i_subtitles > 1 )
168             {
169                 if( p_sys->track[i]->p_subtitles[p_sys->track[i]->i_subtitles-1]->i_start > p_sys->i_length )
170                     p_sys->i_length = (mtime_t) p_sys->track[i]->p_subtitles[p_sys->track[i]->i_subtitles-1]->i_start + 1 * 1000 * 1000;
171             }
172         }
173     }
174
175     i_len = strlen( p_demux->psz_path );
176     psz_vobname = strdup( p_demux->psz_path );
177
178     strcpy( psz_vobname + i_len - 4, ".sub" );
179
180     /* open file */
181     if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
182     {
183         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
184                  psz_vobname );
185     }
186     free( psz_vobname );
187
188     return VLC_SUCCESS;
189 }
190
191 /*****************************************************************************
192  * Close: Close subtitle demux
193  *****************************************************************************/
194 static void Close( vlc_object_t *p_this )
195 {
196     demux_t *p_demux = (demux_t*)p_this;
197     demux_sys_t *p_sys = p_demux->p_sys;
198
199     /* Clean all subs from all tracks
200     if( p_sys->subtitle )
201         free( p_sys->subtitle );
202 */
203     if( p_sys->p_vobsub_file )
204         fclose( p_sys->p_vobsub_file );
205
206     free( p_sys );
207 }
208
209 /*****************************************************************************
210  * Control:
211  *****************************************************************************/
212 static int Control( demux_t *p_demux, int i_query, va_list args )
213 {
214     demux_sys_t *p_sys = p_demux->p_sys;
215     int64_t *pi64, i64;
216     double *pf, f;
217
218     switch( i_query )
219     {
220         case DEMUX_GET_LENGTH:
221             pi64 = (int64_t*)va_arg( args, int64_t * );
222             *pi64 = p_sys->i_length;
223             return VLC_SUCCESS;
224
225         case DEMUX_GET_TIME:
226             pi64 = (int64_t*)va_arg( args, int64_t * );
227             /*if( p_sys->i_current_subtitle < p_sys->i_subtitles )
228             {
229                 *pi64 = p_sys->subtitle[p_sys->i_current_subtitle].i_start;
230                 return VLC_SUCCESS;
231             }*/
232             return VLC_EGENERIC;
233
234         case DEMUX_SET_TIME:
235             i64 = (int64_t)va_arg( args, int64_t );
236             /*p_sys->i_current_subtitle = 0;
237             while( p_sys->i_current_subtitle < p_sys->i_subtitles &&
238                    p_sys->subtitle[p_sys->i_current_subtitle].i_start < i64 )
239             {
240                 p_sys->i_current_subtitle++;
241             }
242
243             if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
244                 return VLC_EGENERIC;*/
245             return VLC_SUCCESS;
246
247         case DEMUX_GET_POSITION:
248             pf = (double*)va_arg( args, double * );
249             /*if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
250             {
251                 *pf = 1.0;
252             }
253             else if( p_sys->i_subtitles > 0 )
254             {
255                 *pf = (double)p_sys->subtitle[p_sys->i_current_subtitle].i_start /
256                       (double)p_sys->i_length;
257             }
258             else
259             {
260                 *pf = 0.0;
261             }*/
262             return VLC_SUCCESS;
263
264         case DEMUX_SET_POSITION:
265             f = (double)va_arg( args, double );
266             /*i64 = f * p_sys->i_length;
267
268             p_sys->i_current_subtitle = 0;
269             while( p_sys->i_current_subtitle < p_sys->i_subtitles &&
270                    p_sys->subtitle[p_sys->i_current_subtitle].i_start < i64 )
271             {
272                 p_sys->i_current_subtitle++;
273             }
274             if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
275                 return VLC_EGENERIC;*/
276             return VLC_SUCCESS;
277
278         case DEMUX_SET_NEXT_DEMUX_TIME:
279             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
280             return VLC_SUCCESS;
281
282         case DEMUX_GET_FPS:
283         case DEMUX_GET_META:
284         case DEMUX_GET_TITLE_INFO:
285             return VLC_EGENERIC;
286
287         default:
288             msg_Err( p_demux, "unknown query in subtitle control" );
289             return VLC_EGENERIC;
290     }
291 }
292
293 /*****************************************************************************
294  * Demux: Send subtitle to decoder
295  *****************************************************************************/
296 static int Demux( demux_t *p_demux )
297 {
298     demux_sys_t *p_sys = p_demux->p_sys;
299     int64_t i_maxdate;
300     int i;
301
302     for( i = 0; i < p_sys->i_tracks; i++ )
303     {
304 #define tk p_sys->track[i]
305         if( tk.i_current_subtitle >= tk.i_subtitles )
306             return 0;
307
308         i_maxdate = p_sys->i_next_demux_date;
309         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
310         {
311             /* Should not happen */
312             i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
313         }
314
315         while( tk.i_current_subtitle < tk.i_subtitles &&
316                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
317         {
318             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
319             block_t *p_block;
320             int i_size = 0;
321
322             /* first compute SPU size */
323             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
324             {
325                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
326             }
327             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
328
329             /* Seek at the right place */
330             if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
331             {
332                 msg_Warn( p_demux,
333                           "cannot seek at right vobsub location %d", i_pos );
334                 tk.i_current_subtitle++;
335                 continue;
336             }
337
338             /* allocate a packet */
339             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
340             {
341                 tk.i_current_subtitle++;
342                 continue;
343             }
344
345             /* read data */
346             p_block->i_buffer = fread( p_block->p_buffer, 1, i_size,
347                                        p_sys->p_vobsub_file );
348             if( p_block->i_buffer <= 6 )
349             {
350                 block_Release( p_block );
351                 tk.i_current_subtitle++;
352                 continue;
353             }
354
355             /* pts */
356             p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
357
358             /* demux this block */
359             DemuxVobSub( p_demux, p_block );
360
361             tk.i_current_subtitle++;
362         }
363 #undef tk
364     }
365
366     /* */
367     p_sys->i_next_demux_date = 0;
368
369     return 1;
370 }
371
372 static int TextLoad( text_t *txt, stream_t *s )
373 {
374     int   i_line_max;
375
376     /* init txt */
377     i_line_max          = 500;
378     txt->i_line_count   = 0;
379     txt->i_line         = 0;
380     txt->line           = calloc( i_line_max, sizeof( char * ) );
381
382     /* load the complete file */
383     for( ;; )
384     {
385         char *psz = stream_ReadLine( s );
386
387         if( psz == NULL )
388             break;
389
390         txt->line[txt->i_line_count++] = psz;
391         if( txt->i_line_count >= i_line_max )
392         {
393             i_line_max += 100;
394             txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
395         }
396     }
397
398     if( txt->i_line_count <= 0 )
399     {
400         free( txt->line );
401         return VLC_EGENERIC;
402     }
403
404     return VLC_SUCCESS;
405 }
406 static void TextUnload( text_t *txt )
407 {
408     int i;
409
410     for( i = 0; i < txt->i_line_count; i++ )
411     {
412         free( txt->line[i] );
413     }
414     free( txt->line );
415     txt->i_line       = 0;
416     txt->i_line_count = 0;
417 }
418
419 static char *TextGetLine( text_t *txt )
420 {
421     if( txt->i_line >= txt->i_line_count )
422         return( NULL );
423
424     return txt->line[txt->i_line++];
425 }
426
427 static int ParseVobSubIDX( demux_t *p_demux )
428 {
429     demux_sys_t *p_sys = p_demux->p_sys;
430     text_t      *txt = &p_sys->txt;
431     char        *line;
432     vobsub_track_t *current_tk;
433
434     for( ;; )
435     {
436         if( ( line = TextGetLine( txt ) ) == NULL )
437         {
438             return( VLC_EGENERIC );
439         }
440         
441         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' ) 
442             continue;
443         else if( !strncmp( "size:", line, 5 ) )
444         {
445             /* Store the original size of the video */
446             if( sscanf( line, "size: %dx%d",
447                         &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
448             {
449                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
450             }
451             else
452             {
453                 msg_Warn( p_demux, "reading original frame size failed" );
454             }
455         }
456         else if( !strncmp( "id:", line, 3 ) )
457         {
458             char language[20];
459             int i_track_id;
460             es_format_t fmt;
461
462             /* Lets start a new track */
463             if( sscanf( line, "id: %2s, index: %d",
464                         language, &i_track_id ) == 2 )
465             {
466                 p_sys->i_tracks++;
467                 p_sys->track = (vobsub_track_t*)realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
468
469                 /* Init the track */
470                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
471                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
472                 current_tk->i_current_subtitle = 0;
473                 current_tk->i_subtitles = 0;
474                 current_tk->p_subtitles = (subtitle_t*)malloc( sizeof( subtitle_t ) );;
475                 current_tk->i_track_id = i_track_id;
476
477                 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
478                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
479                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
480                 fmt.psz_language = strdup( language );
481                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
482
483                 msg_Dbg( p_demux, "new vobsub track detected" );
484             }
485             else
486             {
487                 msg_Warn( p_demux, "reading new track failed" );
488             }
489         }
490         else if( !strncmp( line, "timestamp:", 10 ) )
491         {
492             /*
493              * timestamp: hh:mm:ss:mss, filepos: loc
494              * loc is the hex location of the spu in the .sub file
495              *
496              */
497             unsigned int h, m, s, ms, loc;
498             int i_start, i_location = 0;
499             
500             vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
501
502             if( sscanf( line, "timestamp: %d:%d:%d:%d, filepos: %x",
503                         &h, &m, &s, &ms, &loc ) == 5 )
504             {
505                 subtitle_t *current_sub;
506                 
507                 i_start = ( (mtime_t)h * 3600*1000 +
508                             (mtime_t)m * 60*1000 +
509                             (mtime_t)s * 1000 +
510                             (mtime_t)ms ) * 1000;
511                 i_location = loc;
512                 
513                 current_tk->i_subtitles++;
514                 current_tk->p_subtitles = (subtitle_t*)realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
515                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
516                 
517                 current_sub->i_start = i_start;
518                 current_sub->i_vobsub_location = i_location;
519             }
520         }
521     }
522     return( 0 );
523 }
524
525 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
526 {
527     demux_sys_t *p_sys = p_demux->p_sys;
528     uint8_t     *p = p_bk->p_buffer;
529     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
530     int i;
531
532     while( p < p_end )
533     {
534         int i_size = ps_pkt_size( p, p_end - p );
535         block_t *p_pkt;
536         int      i_id;
537         int      i_spu;
538
539         if( i_size <= 0 )
540         {
541             break;
542         }
543         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
544         {
545             msg_Warn( p_demux, "invalid PES" );
546             break;
547         }
548
549         if( p[3] != 0xbd )
550         {
551             msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
552             p += i_size;
553             continue;
554         }
555
556         /* Create a block */
557         p_pkt = block_New( p_demux, i_size );
558         memcpy( p_pkt->p_buffer, p, i_size);
559         p += i_size;
560
561         i_id = ps_pkt_id( p_pkt );
562         if( (i_id&0xffe0) != 0xbd20 ||
563             ps_pkt_parse_pes( p_pkt, 1 ) )
564         {
565             block_Release( p_pkt );
566             continue;
567         }
568         i_spu = i_id&0x1f;
569         msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
570
571         /* FIXME i_spu == determines which of the spu tracks we will show. */
572         for( i = 0; i < p_sys->i_tracks; i++ )
573         {
574 #define tk p_sys->track[i]
575             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
576             p_pkt->i_length = 0;
577             
578             if( tk.p_es && tk.i_track_id == i_spu )
579             {
580                 es_out_Send( p_demux->out, tk.p_es, p_pkt );
581                 p_bk->i_pts = 0;     /*only first packet has a pts */
582                 break;
583             }
584             else if( i == p_sys->i_tracks - 1 )
585             {
586                 block_Release( p_pkt );
587             }
588 #undef tk
589         }
590     }
591
592     return VLC_SUCCESS;
593 }