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