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