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