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", 0 );
51 set_callbacks( Open, Close );
53 add_shortcut( "vobsub" );
56 /*****************************************************************************
58 *****************************************************************************/
66 static int TextLoad( text_t *, stream_t *s );
67 static void TextUnload( text_t * );
72 int i_vobsub_location;
81 int i_current_subtitle;
83 subtitle_t *p_subtitles;
88 int64_t i_next_demux_date;
96 vobsub_track_t *track;
98 int i_original_frame_width;
99 int i_original_frame_height;
102 static int Demux( demux_t * );
103 static int Control( demux_t *, int, va_list );
105 static int ParseVobSubIDX( demux_t * );
106 static int DemuxVobSub( demux_t *, block_t *);
108 /*****************************************************************************
110 *****************************************************************************/
111 static int Open ( vlc_object_t *p_this )
113 demux_t *p_demux = (demux_t*)p_this;
117 if( strcmp( p_demux->psz_demux, "vobsub" ) )
119 msg_Dbg( p_demux, "vobsub demux discarded" );
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;
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;
133 if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
135 if( !strcasestr( s, "# VobSub index file" ) )
137 msg_Err( p_demux, "this doesn't seem to be a vobsub file, bailing" );
144 if( stream_Seek( p_demux->s, 0 ) )
146 msg_Warn( p_demux, "failed to rewind" );
151 msg_Err( p_demux, "could not read vobsub IDX file" );
155 /* Load the whole file */
156 TextLoad( &p_sys->txt, p_demux->s );
159 ParseVobSubIDX( p_demux );
162 TextUnload( &p_sys->txt );
164 int i_len = strlen( p_demux->psz_path );
165 char *psz_vobname = strdup( p_demux->psz_path );
167 strcpy( psz_vobname + i_len - 4, ".sub" );
170 if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
172 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
180 /*****************************************************************************
181 * Close: Close subtitle demux
182 *****************************************************************************/
183 static void Close( vlc_object_t *p_this )
185 demux_t *p_demux = (demux_t*)p_this;
186 demux_sys_t *p_sys = p_demux->p_sys;
188 /* Clean all subs from all tracks
189 if( p_sys->subtitle )
190 free( p_sys->subtitle );
192 if( p_sys->p_vobsub_file )
193 fclose( p_sys->p_vobsub_file );
198 /*****************************************************************************
200 *****************************************************************************/
201 static int Control( demux_t *p_demux, int i_query, va_list args )
203 demux_sys_t *p_sys = p_demux->p_sys;
209 case DEMUX_GET_LENGTH:
210 pi64 = (int64_t*)va_arg( args, int64_t * );
211 //*pi64 = p_sys->i_length;
215 pi64 = (int64_t*)va_arg( args, int64_t * );
216 /*if( p_sys->i_current_subtitle < p_sys->i_subtitles )
218 *pi64 = p_sys->subtitle[p_sys->i_current_subtitle].i_start;
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 )
229 p_sys->i_current_subtitle++;
232 if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
233 return VLC_EGENERIC;*/
236 case DEMUX_GET_POSITION:
237 pf = (double*)va_arg( args, double * );
238 /*if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
242 else if( p_sys->i_subtitles > 0 )
244 *pf = (double)p_sys->subtitle[p_sys->i_current_subtitle].i_start /
245 (double)p_sys->i_length;
253 case DEMUX_SET_POSITION:
254 f = (double)va_arg( args, double );
255 /*i64 = f * p_sys->i_length;
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 )
261 p_sys->i_current_subtitle++;
263 if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
264 return VLC_EGENERIC;*/
267 case DEMUX_SET_NEXT_DEMUX_TIME:
268 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
273 case DEMUX_GET_TITLE_INFO:
277 msg_Err( p_demux, "unknown query in subtitle control" );
282 /*****************************************************************************
283 * Demux: Send subtitle to decoder
284 *****************************************************************************/
285 static int Demux( demux_t *p_demux )
287 demux_sys_t *p_sys = p_demux->p_sys;
291 for( i = 0; i < p_sys->i_tracks; i++ )
293 #define tk p_sys->track[i]
294 if( tk.i_current_subtitle >= tk.i_subtitles )
297 i_maxdate = p_sys->i_next_demux_date;
298 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
300 /* Should not happen */
301 i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
304 while( tk.i_current_subtitle < tk.i_subtitles &&
305 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
307 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
311 /* first compute SPU size */
312 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
314 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
316 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
318 /* Seek at the right place */
319 if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
322 "cannot seek at right vobsub location %d", i_pos );
323 tk.i_current_subtitle++;
327 /* allocate a packet */
328 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
330 tk.i_current_subtitle++;
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 )
339 block_Release( p_block );
340 tk.i_current_subtitle++;
345 p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
347 /* demux this block */
348 DemuxVobSub( p_demux, p_block );
350 tk.i_current_subtitle++;
356 p_sys->i_next_demux_date = 0;
361 static int TextLoad( text_t *txt, stream_t *s )
367 txt->i_line_count = 0;
369 txt->line = calloc( i_line_max, sizeof( char * ) );
371 /* load the complete file */
374 char *psz = stream_ReadLine( s );
379 txt->line[txt->i_line_count++] = psz;
380 if( txt->i_line_count >= i_line_max )
383 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
387 if( txt->i_line_count <= 0 )
395 static void TextUnload( text_t *txt )
399 for( i = 0; i < txt->i_line_count; i++ )
401 free( txt->line[i] );
405 txt->i_line_count = 0;
408 static char *TextGetLine( text_t *txt )
410 if( txt->i_line >= txt->i_line_count )
413 return txt->line[txt->i_line++];
416 static int ParseVobSubIDX( demux_t *p_demux )
418 demux_sys_t *p_sys = p_demux->p_sys;
419 text_t *txt = &p_sys->txt;
421 vobsub_track_t *current_tk;
425 if( ( line = TextGetLine( txt ) ) == NULL )
427 return( VLC_EGENERIC );
430 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
432 else if( !strncmp( "size:", line, 5 ) )
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 )
438 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
442 msg_Warn( p_demux, "reading original frame size failed" );
445 else if( !strncmp( "id:", line, 3 ) )
451 /* Lets start a new track */
452 if( sscanf( line, "id: %s, index: %d",
453 language, &i_track_id ) == 2 )
456 p_sys->track = (vobsub_track_t*)realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
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;
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 );
472 msg_Dbg( p_demux, "new vobsub track detected" );
476 msg_Warn( p_demux, "reading new track failed" );
479 else if( !strncmp( line, "timestamp:", 10 ) )
482 * timestamp: hh:mm:ss:mss, filepos: loc
483 * loc is the hex location of the spu in the .sub file
486 unsigned int h, m, s, ms, loc;
487 int i_start, i_location = 0;
489 if( sscanf( line, "timestamp: %d:%d:%d:%d, filepos: %x",
490 &h, &m, &s, &ms, &loc ) == 5 )
492 i_start = ( (mtime_t)h * 3600*1000 +
493 (mtime_t)m * 60*1000 +
495 (mtime_t)ms ) * 1000;
504 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
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];
513 int i_size = ps_pkt_size( p, p_end - p );
522 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
524 msg_Warn( p_demux, "invalid PES" );
530 msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
536 p_pkt = block_New( p_demux, i_size );
537 memcpy( p_pkt->p_buffer, p, i_size);
540 i_id = ps_pkt_id( p_pkt );
541 if( (i_id&0xffe0) != 0xbd20 ||
542 ps_pkt_parse_pes( p_pkt, 1 ) )
544 block_Release( p_pkt );
548 msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
550 /* FIXME i_spu == determines which of the spu tracks we will show. */
551 for( i = 0; i < p_sys->i_tracks; i++ )
553 #define tk p_sys->track[i]
554 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
557 if( tk.p_es && tk.i_track_id == i_id )
559 es_out_Send( p_demux->out, tk.p_es, p_pkt );
560 p_bk->i_pts = 0; /* only first packet has a pts */
564 block_Release( p_pkt );