1 /*****************************************************************************
2 * subtitle.c: Demux vobsub files.
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Derk-Jan Hartman <hartman at videolan dot org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <sys/types.h>
34 #include <vlc/input.h>
35 #include "vlc_video.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t *p_this );
45 static void Close( vlc_object_t *p_this );
48 set_description( _("Vobsub subtitles demux") );
49 set_capability( "demux2", 1 );
51 set_callbacks( Open, Close );
53 add_shortcut( "vobsub" );
54 add_shortcut( "subtitle" );
57 /*****************************************************************************
59 *****************************************************************************/
67 static int TextLoad( text_t *, stream_t *s );
68 static void TextUnload( text_t * );
73 int i_vobsub_location;
82 int i_current_subtitle;
84 subtitle_t *p_subtitles;
89 int64_t i_next_demux_date;
97 vobsub_track_t *track;
99 int i_original_frame_width;
100 int i_original_frame_height;
103 static int Demux( demux_t * );
104 static int Control( demux_t *, int, va_list );
106 static int ParseVobSubIDX( demux_t * );
107 static int DemuxVobSub( demux_t *, block_t *);
109 /*****************************************************************************
111 *****************************************************************************/
112 static int Open ( vlc_object_t *p_this )
114 demux_t *p_demux = (demux_t*)p_this;
116 char *psz_vobname, *s;
119 if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
121 if( !strcasestr( s, "# VobSub index file" ) )
123 msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
125 if( stream_Seek( p_demux->s, 0 ) )
127 msg_Warn( p_demux, "failed to rewind" );
136 msg_Dbg( p_demux, "could not read vobsub IDX file" );
140 p_demux->pf_demux = Demux;
141 p_demux->pf_control = Control;
142 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
144 p_sys->p_vobsub_file = NULL;
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;
150 /* Load the whole file */
151 TextLoad( &p_sys->txt, p_demux->s );
154 ParseVobSubIDX( p_demux );
157 TextUnload( &p_sys->txt );
159 /* Find the total length of the vobsubs */
160 if( p_sys->i_tracks > 0 )
163 for( i = 0; i < p_sys->i_tracks; i++ )
165 if( p_sys->track[i].i_subtitles > 1 )
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;
173 i_len = strlen( p_demux->psz_path );
174 psz_vobname = strdup( p_demux->psz_path );
176 strcpy( psz_vobname + i_len - 4, ".sub" );
179 if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
181 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
192 /*****************************************************************************
193 * Close: Close subtitle demux
194 *****************************************************************************/
195 static void Close( vlc_object_t *p_this )
197 demux_t *p_demux = (demux_t*)p_this;
198 demux_sys_t *p_sys = p_demux->p_sys;
200 /* Clean all subs from all tracks
201 if( p_sys->subtitle )
202 free( p_sys->subtitle );
204 if( p_sys->p_vobsub_file )
205 fclose( p_sys->p_vobsub_file );
210 /*****************************************************************************
212 *****************************************************************************/
213 static int Control( demux_t *p_demux, int i_query, va_list args )
215 demux_sys_t *p_sys = p_demux->p_sys;
221 case DEMUX_GET_LENGTH:
222 pi64 = (int64_t*)va_arg( args, int64_t * );
223 *pi64 = p_sys->i_length;
227 pi64 = (int64_t*)va_arg( args, int64_t * );
228 /*if( p_sys->i_current_subtitle < p_sys->i_subtitles )
230 *pi64 = p_sys->subtitle[p_sys->i_current_subtitle].i_start;
236 i64 = (int64_t)va_arg( args, int64_t );
237 /*p_sys->i_current_subtitle = 0;
238 while( p_sys->i_current_subtitle < p_sys->i_subtitles &&
239 p_sys->subtitle[p_sys->i_current_subtitle].i_start < i64 )
241 p_sys->i_current_subtitle++;
244 if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
245 return VLC_EGENERIC;*/
248 case DEMUX_GET_POSITION:
249 pf = (double*)va_arg( args, double * );
250 /*if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
254 else if( p_sys->i_subtitles > 0 )
256 *pf = (double)p_sys->subtitle[p_sys->i_current_subtitle].i_start /
257 (double)p_sys->i_length;
265 case DEMUX_SET_POSITION:
266 f = (double)va_arg( args, double );
267 /*i64 = f * p_sys->i_length;
269 p_sys->i_current_subtitle = 0;
270 while( p_sys->i_current_subtitle < p_sys->i_subtitles &&
271 p_sys->subtitle[p_sys->i_current_subtitle].i_start < i64 )
273 p_sys->i_current_subtitle++;
275 if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
276 return VLC_EGENERIC;*/
279 case DEMUX_SET_NEXT_DEMUX_TIME:
280 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
285 case DEMUX_GET_TITLE_INFO:
289 msg_Err( p_demux, "unknown query in subtitle control" );
294 /*****************************************************************************
295 * Demux: Send subtitle to decoder
296 *****************************************************************************/
297 static int Demux( demux_t *p_demux )
299 demux_sys_t *p_sys = p_demux->p_sys;
303 for( i = 0; i < p_sys->i_tracks; i++ )
305 #define tk p_sys->track[i]
306 if( tk.i_current_subtitle >= tk.i_subtitles )
309 i_maxdate = p_sys->i_next_demux_date;
310 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
312 /* Should not happen */
313 i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
316 while( tk.i_current_subtitle < tk.i_subtitles &&
317 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
319 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
323 /* first compute SPU size */
324 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
326 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
328 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
330 /* Seek at the right place */
331 if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
334 "cannot seek at right vobsub location %d", i_pos );
335 tk.i_current_subtitle++;
339 /* allocate a packet */
340 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
342 tk.i_current_subtitle++;
347 p_block->i_buffer = fread( p_block->p_buffer, 1, i_size,
348 p_sys->p_vobsub_file );
349 if( p_block->i_buffer <= 6 )
351 block_Release( p_block );
352 tk.i_current_subtitle++;
357 p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
359 /* demux this block */
360 DemuxVobSub( p_demux, p_block );
362 tk.i_current_subtitle++;
368 p_sys->i_next_demux_date = 0;
373 static int TextLoad( text_t *txt, stream_t *s )
379 txt->i_line_count = 0;
381 txt->line = calloc( i_line_max, sizeof( char * ) );
383 /* load the complete file */
386 char *psz = stream_ReadLine( s );
391 txt->line[txt->i_line_count++] = psz;
392 if( txt->i_line_count >= i_line_max )
395 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
399 if( txt->i_line_count <= 0 )
407 static void TextUnload( text_t *txt )
411 for( i = 0; i < txt->i_line_count; i++ )
413 free( txt->line[i] );
417 txt->i_line_count = 0;
420 static char *TextGetLine( text_t *txt )
422 if( txt->i_line >= txt->i_line_count )
425 return txt->line[txt->i_line++];
428 static int ParseVobSubIDX( demux_t *p_demux )
430 demux_sys_t *p_sys = p_demux->p_sys;
431 text_t *txt = &p_sys->txt;
433 vobsub_track_t *current_tk;
437 if( ( line = TextGetLine( txt ) ) == NULL )
439 return( VLC_EGENERIC );
442 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
444 else if( !strncmp( "size:", line, 5 ) )
446 /* Store the original size of the video */
447 if( sscanf( line, "size: %dx%d",
448 &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
450 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
454 msg_Warn( p_demux, "reading original frame size failed" );
457 else if( !strncmp( "id:", line, 3 ) )
463 /* Lets start a new track */
464 if( sscanf( line, "id: %2s, index: %d",
465 language, &i_track_id ) == 2 )
468 p_sys->track = (vobsub_track_t*)realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
471 current_tk = &p_sys->track[p_sys->i_tracks - 1];
472 memset( current_tk, 0, sizeof( vobsub_track_t ) );
473 current_tk->i_current_subtitle = 0;
474 current_tk->i_subtitles = 0;
475 current_tk->p_subtitles = (subtitle_t*)malloc( sizeof( subtitle_t ) );;
476 current_tk->i_track_id = i_track_id;
478 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
479 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
480 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
481 fmt.psz_language = strdup( language );
482 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
484 msg_Dbg( p_demux, "new vobsub track detected" );
488 msg_Warn( p_demux, "reading new track failed" );
491 else if( !strncmp( line, "timestamp:", 10 ) )
494 * timestamp: hh:mm:ss:mss, filepos: loc
495 * loc is the hex location of the spu in the .sub file
498 unsigned int h, m, s, ms, loc;
499 int i_start, i_location = 0;
501 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
503 if( sscanf( line, "timestamp: %d:%d:%d:%d, filepos: %x",
504 &h, &m, &s, &ms, &loc ) == 5 )
506 subtitle_t *current_sub;
508 i_start = ( (mtime_t)h * 3600*1000 +
509 (mtime_t)m * 60*1000 +
511 (mtime_t)ms ) * 1000;
514 current_tk->i_subtitles++;
515 current_tk->p_subtitles = (subtitle_t*)realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
516 current_sub = ¤t_tk->p_subtitles[current_tk->i_subtitles - 1];
518 current_sub->i_start = i_start;
519 current_sub->i_vobsub_location = i_location;
526 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
528 demux_sys_t *p_sys = p_demux->p_sys;
529 uint8_t *p = p_bk->p_buffer;
530 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
535 int i_size = ps_pkt_size( p, p_end - p );
544 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
546 msg_Warn( p_demux, "invalid PES" );
552 msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
558 p_pkt = block_New( p_demux, i_size );
559 memcpy( p_pkt->p_buffer, p, i_size);
562 i_id = ps_pkt_id( p_pkt );
563 if( (i_id&0xffe0) != 0xbd20 ||
564 ps_pkt_parse_pes( p_pkt, 1 ) )
566 block_Release( p_pkt );
570 msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
572 /* FIXME i_spu == determines which of the spu tracks we will show. */
573 for( i = 0; i < p_sys->i_tracks; i++ )
575 #define tk p_sys->track[i]
576 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
579 if( tk.p_es && tk.i_track_id == i_spu )
581 es_out_Send( p_demux->out, tk.p_es, p_pkt );
582 p_bk->i_pts = 0; /*only first packet has a pts */
585 else if( i == p_sys->i_tracks - 1 )
587 block_Release( p_pkt );