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