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