1 /*****************************************************************************
2 * vobsub.c: Demux vobsub files.
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t *p_this );
46 static void Close( vlc_object_t *p_this );
49 set_description( N_("Vobsub subtitles parser") )
50 set_category( CAT_INPUT )
51 set_subcategory( SUBCAT_INPUT_DEMUX )
52 set_capability( "demux", 1 )
54 set_callbacks( Open, Close )
56 add_shortcut( "vobsub", "subtitle" )
59 /*****************************************************************************
61 *****************************************************************************/
73 int i_vobsub_location;
81 int i_current_subtitle;
83 subtitle_t *p_subtitles;
90 int64_t i_next_demux_date;
94 stream_t *p_vobsub_stream;
98 vobsub_track_t *track;
100 int i_original_frame_width;
101 int i_original_frame_height;
103 uint32_t palette[16];
107 static int Demux( demux_t * );
108 static int Control( demux_t *, int, va_list );
110 static int TextLoad( text_t *, stream_t *s );
111 static void TextUnload( text_t * );
112 static int ParseVobSubIDX( demux_t * );
113 static int DemuxVobSub( demux_t *, block_t *);
115 /*****************************************************************************
117 *****************************************************************************/
118 static int Open ( vlc_object_t *p_this )
120 demux_t *p_demux = (demux_t*)p_this;
122 char *psz_vobname, *s;
125 if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
127 if( !strcasestr( s, "# VobSub index file" ) )
129 msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
131 if( stream_Seek( p_demux->s, 0 ) )
133 msg_Warn( p_demux, "failed to rewind" );
141 msg_Dbg( p_demux, "could not read vobsub IDX file" );
146 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
147 if( unlikely( !p_sys ) )
151 p_sys->p_vobsub_stream = NULL;
153 p_sys->track = malloc( sizeof( vobsub_track_t ) );
154 if( unlikely( !p_sys->track ) )
156 p_sys->i_original_frame_width = -1;
157 p_sys->i_original_frame_height = -1;
158 p_sys->b_palette = false;
159 memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
161 /* Load the whole file */
162 TextLoad( &p_sys->txt, p_demux->s );
165 ParseVobSubIDX( p_demux );
168 TextUnload( &p_sys->txt );
170 /* Find the total length of the vobsubs */
171 if( p_sys->i_tracks > 0 )
173 for( int i = 0; i < p_sys->i_tracks; i++ )
175 if( p_sys->track[i].i_subtitles > 1 )
177 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
178 p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
183 if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1 )
186 i_len = strlen( psz_vobname );
187 if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
190 p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
191 if( p_sys->p_vobsub_stream == NULL )
193 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
200 p_demux->pf_demux = Demux;
201 p_demux->pf_control = Control;
206 /* Clean all subs from all tracks */
207 for( int i = 0; i < p_sys->i_tracks; i++ )
208 free( p_sys->track[i].p_subtitles );
209 free( p_sys->track );
215 /*****************************************************************************
216 * Close: Close subtitle demux
217 *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
220 demux_t *p_demux = (demux_t*)p_this;
221 demux_sys_t *p_sys = p_demux->p_sys;
223 if( p_sys->p_vobsub_stream )
224 stream_Delete( p_sys->p_vobsub_stream );
226 /* Clean all subs from all tracks */
227 for( int i = 0; i < p_sys->i_tracks; i++ )
228 free( p_sys->track[i].p_subtitles );
229 free( p_sys->track );
233 /*****************************************************************************
235 *****************************************************************************/
236 static int Control( demux_t *p_demux, int i_query, va_list args )
238 demux_sys_t *p_sys = p_demux->p_sys;
245 case DEMUX_GET_LENGTH:
246 pi64 = (int64_t*)va_arg( args, int64_t * );
247 *pi64 = (int64_t) p_sys->i_length;
251 pi64 = (int64_t*)va_arg( args, int64_t * );
252 for( i = 0; i < p_sys->i_tracks; i++ )
255 /* Check the ES is selected */
256 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
257 p_sys->track[i].p_es, &b_selected );
258 if( b_selected ) break;
260 if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
262 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
268 i64 = (int64_t)va_arg( args, int64_t );
269 for( i = 0; i < p_sys->i_tracks; i++ )
271 p_sys->track[i].i_current_subtitle = 0;
272 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
273 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
275 p_sys->track[i].i_current_subtitle++;
278 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
283 case DEMUX_GET_POSITION:
284 pf = (double*)va_arg( args, double * );
285 for( i = 0; i < p_sys->i_tracks; i++ )
288 /* Check the ES is selected */
289 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
290 p_sys->track[i].p_es, &b_selected );
291 if( b_selected ) break;
293 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
297 else if( p_sys->track[i].i_subtitles > 0 )
299 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
300 (double)p_sys->i_length;
308 case DEMUX_SET_POSITION:
309 f = (double)va_arg( args, double );
310 i64 = (int64_t) f * p_sys->i_length;
312 for( i = 0; i < p_sys->i_tracks; i++ )
314 p_sys->track[i].i_current_subtitle = 0;
315 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
316 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
318 p_sys->track[i].i_current_subtitle++;
320 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
325 case DEMUX_SET_NEXT_DEMUX_TIME:
326 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
329 case DEMUX_GET_PTS_DELAY:
332 case DEMUX_GET_TITLE_INFO:
333 case DEMUX_HAS_UNSUPPORTED_META:
334 case DEMUX_GET_ATTACHMENTS:
335 case DEMUX_CAN_RECORD:
339 msg_Warn( p_demux, "unknown query in subtitle control" );
344 /*****************************************************************************
345 * Demux: Send subtitle to decoder
346 *****************************************************************************/
347 static int Demux( demux_t *p_demux )
349 demux_sys_t *p_sys = p_demux->p_sys;
353 for( int i = 0; i < p_sys->i_tracks; i++ )
355 #define tk p_sys->track[i]
356 if( tk.i_current_subtitle >= tk.i_subtitles )
359 i_maxdate = p_sys->i_next_demux_date;
360 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
362 /* Should not happen */
363 i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
366 while( tk.i_current_subtitle < tk.i_subtitles &&
367 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
369 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
373 /* first compute SPU size */
374 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
376 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
378 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
380 /* Seek at the right place */
381 if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
384 "cannot seek in the VobSub to the correct time %d", i_pos );
385 tk.i_current_subtitle++;
389 /* allocate a packet */
390 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
392 tk.i_current_subtitle++;
397 i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
400 block_Release( p_block );
401 tk.i_current_subtitle++;
404 p_block->i_buffer = i_read;
407 p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
409 /* demux this block */
410 DemuxVobSub( p_demux, p_block );
412 tk.i_current_subtitle++;
418 p_sys->i_next_demux_date = 0;
423 static int TextLoad( text_t *txt, stream_t *s )
428 /* load the complete file */
431 char *psz = stream_ReadLine( s );
434 if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
437 ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
438 if( ppsz_new == NULL )
447 txt->i_line_count = n;
454 static void TextUnload( text_t *txt )
456 for( int i = 0; i < txt->i_line_count; i++ )
457 free( txt->line[i] );
461 txt->i_line_count = 0;
464 static char *TextGetLine( text_t *txt )
466 if( txt->i_line >= txt->i_line_count )
469 return txt->line[txt->i_line++];
472 static int ParseVobSubIDX( demux_t *p_demux )
474 demux_sys_t *p_sys = p_demux->p_sys;
475 text_t *txt = &p_sys->txt;
477 vobsub_track_t *current_tk = NULL;
481 if( ( line = TextGetLine( txt ) ) == NULL )
483 return( VLC_EGENERIC );
486 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
490 else if( !strncmp( "size:", line, 5 ) )
492 /* Store the original size of the video */
493 if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
494 &p_sys->i_original_frame_height ) == VLC_SUCCESS )
496 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
500 msg_Warn( p_demux, "reading original frame size failed" );
503 else if( !strncmp( "palette:", line, 8 ) )
505 if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
507 p_sys->b_palette = true;
508 msg_Dbg( p_demux, "vobsub palette read" );
512 msg_Warn( p_demux, "reading original palette failed" );
515 else if( !strncmp( "id:", line, 3 ) )
517 char language[33]; /* Usually 2 or 3 letters, sometimes more.
518 Spec (or lack of) doesn't define any limit */
522 /* Lets start a new track */
523 if( sscanf( line, "id: %32[^ ,], index: %d",
524 language, &i_track_id ) != 2 )
526 if( sscanf( line, "id: , index: %d", &i_track_id ) != 1 )
528 msg_Warn( p_demux, "reading new track failed" );
535 p_sys->track = xrealloc( p_sys->track,
536 sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
539 current_tk = &p_sys->track[p_sys->i_tracks - 1];
540 memset( current_tk, 0, sizeof( vobsub_track_t ) );
541 current_tk->i_current_subtitle = 0;
542 current_tk->i_subtitles = 0;
543 current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );
544 current_tk->i_track_id = i_track_id;
545 current_tk->i_delay = (int64_t)0;
547 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
548 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
549 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
550 fmt.psz_language = language;
551 if( p_sys->b_palette )
553 fmt.subs.spu.palette[0] = 0xBeef;
554 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
557 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
558 msg_Dbg( p_demux, "new vobsub track detected" );
560 else if( !strncmp( line, "timestamp:", 10 ) )
563 * timestamp: [sign]hh:mm:ss:mss, filepos: loc
564 * loc is the hex location of the spu in the .sub file
566 int h, m, s, ms, count, loc = 0;
568 int64_t i_start, i_location = 0;
570 if( p_sys->i_tracks > 0 &&
571 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
572 &h, &count, &m, &s, &ms, &loc ) >= 5 )
574 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
575 subtitle_t *current_sub;
577 if( line[count-3] == '-' )
582 i_start = (int64_t) ( h * 3600*1000 +
588 current_tk->i_subtitles++;
589 current_tk->p_subtitles =
590 xrealloc( current_tk->p_subtitles,
591 sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
592 current_sub = ¤t_tk->p_subtitles[current_tk->i_subtitles - 1];
594 current_sub->i_start = i_start * i_sign;
595 current_sub->i_start += current_tk->i_delay;
596 current_sub->i_vobsub_location = i_location;
600 msg_Warn( p_demux, "reading timestamp failed" );
603 else if( !strncasecmp( line, "delay:", 6 ) )
606 * delay: [sign]hh:mm:ss:mss
608 int h, m, s, ms, count = 0;
612 if( p_sys->i_tracks > 0 &&
613 sscanf( line, "%*celay: %d%n:%d:%d:%d",
614 &h, &count, &m, &s, &ms ) >= 4 )
616 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
617 if( line[count-3] == '-' )
622 i_gap = (int64_t) ( h * 3600*1000 +
627 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
628 msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
629 i_sign, i_gap, current_tk->i_delay );
633 msg_Warn( p_demux, "reading delay failed" );
640 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
642 demux_sys_t *p_sys = p_demux->p_sys;
643 uint8_t *p = p_bk->p_buffer;
644 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
647 while( p + 6 < p_end )
649 int i_size = ps_pkt_size( p, p_end - p );
657 if( i_size > p_end - p )
659 msg_Warn( p_demux, "broken PES size" );
663 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
665 msg_Warn( p_demux, "invalid PES" );
671 /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
677 p_pkt = block_New( p_demux, i_size );
678 if( unlikely(p_pkt == NULL) )
680 memcpy( p_pkt->p_buffer, p, i_size);
683 i_id = ps_pkt_id( p_pkt );
684 if( (i_id&0xffe0) != 0xbd20 ||
685 ps_pkt_parse_pes( p_pkt, 1 ) )
687 block_Release( p_pkt );
691 /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
693 for( i = 0; i < p_sys->i_tracks; i++ )
695 vobsub_track_t *p_tk = &p_sys->track[i];
697 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
700 if( p_tk->p_es && p_tk->i_track_id == i_spu )
702 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
703 p_bk->i_pts = VLC_TS_INVALID; /*only first packet has a pts */
707 if( i >= p_sys->i_tracks )
709 block_Release( p_pkt );