1 /*****************************************************************************
2 * subtitle.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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <sys/types.h>
38 #include <vlc_demux.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t *p_this );
49 static void Close( vlc_object_t *p_this );
52 set_description( N_("Vobsub subtitles parser") )
53 set_category( CAT_INPUT )
54 set_subcategory( SUBCAT_INPUT_DEMUX )
55 set_capability( "demux", 1 )
57 set_callbacks( Open, Close )
59 add_shortcut( "vobsub" )
60 add_shortcut( "subtitle" )
63 /*****************************************************************************
65 *****************************************************************************/
73 static int TextLoad( text_t *, stream_t *s );
74 static void TextUnload( text_t * );
79 int i_vobsub_location;
88 int i_current_subtitle;
90 subtitle_t *p_subtitles;
97 int64_t i_next_demux_date;
101 stream_t *p_vobsub_stream;
105 vobsub_track_t *track;
107 int i_original_frame_width;
108 int i_original_frame_height;
110 uint32_t palette[16];
113 static int Demux( demux_t * );
114 static int Control( demux_t *, int, va_list );
116 static int ParseVobSubIDX( demux_t * );
117 static int DemuxVobSub( demux_t *, block_t *);
119 /*****************************************************************************
121 *****************************************************************************/
122 static int Open ( vlc_object_t *p_this )
124 demux_t *p_demux = (demux_t*)p_this;
126 char *psz_vobname, *s;
129 if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
131 if( !strcasestr( s, "# VobSub index file" ) )
133 msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
135 if( stream_Seek( p_demux->s, 0 ) )
137 msg_Warn( p_demux, "failed to rewind" );
146 msg_Dbg( p_demux, "could not read vobsub IDX file" );
150 p_demux->pf_demux = Demux;
151 p_demux->pf_control = Control;
152 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
153 if( unlikely( !p_sys ) )
156 p_sys->p_vobsub_stream = NULL;
158 p_sys->track = malloc( sizeof( vobsub_track_t ) );
159 if( unlikely( !p_sys->track ) )
164 p_sys->i_original_frame_width = -1;
165 p_sys->i_original_frame_height = -1;
166 p_sys->b_palette = false;
167 memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
169 /* Load the whole file */
170 TextLoad( &p_sys->txt, p_demux->s );
173 ParseVobSubIDX( p_demux );
176 TextUnload( &p_sys->txt );
178 /* Find the total length of the vobsubs */
179 if( p_sys->i_tracks > 0 )
182 for( i = 0; i < p_sys->i_tracks; i++ )
184 if( p_sys->track[i].i_subtitles > 1 )
186 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
187 p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
192 if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path ) == -1 )
197 i_len = strlen( psz_vobname );
198 if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
201 p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
202 if( p_sys->p_vobsub_stream == NULL )
204 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
215 /*****************************************************************************
216 * Close: Close subtitle demux
217 *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
221 demux_t *p_demux = (demux_t*)p_this;
222 demux_sys_t *p_sys = p_demux->p_sys;
224 /* Clean all subs from all tracks */
225 for( i = 0; i < p_sys->i_tracks; i++ )
226 free( p_sys->track[i].p_subtitles );
228 free( p_sys->track );
230 if( p_sys->p_vobsub_stream )
231 stream_Delete( p_sys->p_vobsub_stream );
236 /*****************************************************************************
238 *****************************************************************************/
239 static int Control( demux_t *p_demux, int i_query, va_list args )
241 demux_sys_t *p_sys = p_demux->p_sys;
248 case DEMUX_GET_LENGTH:
249 pi64 = (int64_t*)va_arg( args, int64_t * );
250 *pi64 = (int64_t) p_sys->i_length;
254 pi64 = (int64_t*)va_arg( args, int64_t * );
255 for( i = 0; i < p_sys->i_tracks; i++ )
258 /* Check the ES is selected */
259 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
260 p_sys->track[i].p_es, &b_selected );
261 if( b_selected ) break;
263 if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
265 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
271 i64 = (int64_t)va_arg( args, int64_t );
272 for( i = 0; i < p_sys->i_tracks; i++ )
274 p_sys->track[i].i_current_subtitle = 0;
275 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
276 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
278 p_sys->track[i].i_current_subtitle++;
281 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
286 case DEMUX_GET_POSITION:
287 pf = (double*)va_arg( args, double * );
288 for( i = 0; i < p_sys->i_tracks; i++ )
291 /* Check the ES is selected */
292 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
293 p_sys->track[i].p_es, &b_selected );
294 if( b_selected ) break;
296 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
300 else if( p_sys->track[i].i_subtitles > 0 )
302 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
303 (double)p_sys->i_length;
311 case DEMUX_SET_POSITION:
312 f = (double)va_arg( args, double );
313 i64 = (int64_t) f * p_sys->i_length;
315 for( i = 0; i < p_sys->i_tracks; i++ )
317 p_sys->track[i].i_current_subtitle = 0;
318 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
319 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
321 p_sys->track[i].i_current_subtitle++;
323 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
328 case DEMUX_SET_NEXT_DEMUX_TIME:
329 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
334 case DEMUX_GET_TITLE_INFO:
335 case DEMUX_HAS_UNSUPPORTED_META:
336 case DEMUX_GET_ATTACHMENTS:
337 case DEMUX_CAN_RECORD:
341 msg_Warn( p_demux, "unknown query in subtitle control" );
346 /*****************************************************************************
347 * Demux: Send subtitle to decoder
348 *****************************************************************************/
349 static int Demux( demux_t *p_demux )
351 demux_sys_t *p_sys = p_demux->p_sys;
355 for( i = 0; i < p_sys->i_tracks; i++ )
357 #define tk p_sys->track[i]
358 if( tk.i_current_subtitle >= tk.i_subtitles )
361 i_maxdate = p_sys->i_next_demux_date;
362 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
364 /* Should not happen */
365 i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
368 while( tk.i_current_subtitle < tk.i_subtitles &&
369 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
371 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
375 /* first compute SPU size */
376 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
378 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
380 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
382 /* Seek at the right place */
383 if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
386 "cannot seek in the VobSub to the correct time %d", i_pos );
387 tk.i_current_subtitle++;
391 /* allocate a packet */
392 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
394 tk.i_current_subtitle++;
399 i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
402 block_Release( p_block );
403 tk.i_current_subtitle++;
406 p_block->i_buffer = i_read;
409 p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
411 /* demux this block */
412 DemuxVobSub( p_demux, p_block );
414 tk.i_current_subtitle++;
420 p_sys->i_next_demux_date = 0;
425 static int TextLoad( text_t *txt, stream_t *s )
430 /* load the complete file */
433 char *psz = stream_ReadLine( s );
436 if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
439 ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
440 if( ppsz_new == NULL )
449 txt->i_line_count = n;
456 static void TextUnload( text_t *txt )
460 for( i = 0; i < txt->i_line_count; i++ )
461 free( txt->line[i] );
465 txt->i_line_count = 0;
468 static char *TextGetLine( text_t *txt )
470 if( txt->i_line >= txt->i_line_count )
473 return txt->line[txt->i_line++];
476 static int ParseVobSubIDX( demux_t *p_demux )
478 demux_sys_t *p_sys = p_demux->p_sys;
479 text_t *txt = &p_sys->txt;
481 vobsub_track_t *current_tk = NULL;
485 if( ( line = TextGetLine( txt ) ) == NULL )
487 return( VLC_EGENERIC );
490 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
494 else if( !strncmp( "size:", line, 5 ) )
496 /* Store the original size of the video */
497 if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
498 &p_sys->i_original_frame_height ) == VLC_SUCCESS )
500 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
504 msg_Warn( p_demux, "reading original frame size failed" );
507 else if( !strncmp( "palette:", line, 8 ) )
509 if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
511 p_sys->b_palette = true;
512 msg_Dbg( p_demux, "vobsub palette read" );
516 msg_Warn( p_demux, "reading original palette failed" );
519 else if( !strncmp( "id:", line, 3 ) )
525 /* Lets start a new track */
526 if( sscanf( line, "id: %2s, index: %d",
527 language, &i_track_id ) == 2 )
530 p_sys->track = xrealloc( p_sys->track,
531 sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
535 current_tk = &p_sys->track[p_sys->i_tracks - 1];
536 memset( current_tk, 0, sizeof( vobsub_track_t ) );
537 current_tk->i_current_subtitle = 0;
538 current_tk->i_subtitles = 0;
539 current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );;
540 current_tk->i_track_id = i_track_id;
541 current_tk->i_delay = (int64_t)0;
543 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
544 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
545 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
546 fmt.psz_language = language;
547 if( p_sys->b_palette )
549 fmt.subs.spu.palette[0] = 0xBeef;
550 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
553 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
554 msg_Dbg( p_demux, "new vobsub track detected" );
558 msg_Warn( p_demux, "reading new track failed" );
561 else if( !strncmp( line, "timestamp:", 10 ) )
564 * timestamp: [sign]hh:mm:ss:mss, filepos: loc
565 * loc is the hex location of the spu in the .sub file
567 int h, m, s, ms, count, loc = 0;
569 int64_t i_start, i_location = 0;
571 if( p_sys->i_tracks > 0 &&
572 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
573 &h, &count, &m, &s, &ms, &loc ) >= 5 )
575 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
576 subtitle_t *current_sub;
578 if( line[count-3] == '-' )
583 i_start = (int64_t) ( h * 3600*1000 +
589 current_tk->i_subtitles++;
590 current_tk->p_subtitles =
591 xrealloc( current_tk->p_subtitles,
592 sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
593 current_sub = ¤t_tk->p_subtitles[current_tk->i_subtitles - 1];
595 current_sub->i_start = i_start * i_sign;
596 current_sub->i_start += current_tk->i_delay;
597 current_sub->i_vobsub_location = i_location;
601 msg_Warn( p_demux, "reading timestamp failed" );
604 else if( !strncasecmp( line, "delay:", 6 ) )
607 * delay: [sign]hh:mm:ss:mss
609 int h, m, s, ms, count = 0;
613 if( p_sys->i_tracks > 0 &&
614 sscanf( line, "%*celay: %d%n:%d:%d:%d",
615 &h, &count, &m, &s, &ms ) >= 4 )
617 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
618 if( line[count-3] == '-' )
623 i_gap = (int64_t) ( h * 3600*1000 +
628 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
629 msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
630 i_sign, i_gap, current_tk->i_delay );
634 msg_Warn( p_demux, "reading delay failed" );
641 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
643 demux_sys_t *p_sys = p_demux->p_sys;
644 uint8_t *p = p_bk->p_buffer;
645 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
648 while( p + 6 < p_end )
650 int i_size = ps_pkt_size( p, p_end - p );
658 if( i_size > p_end - p )
660 msg_Warn( p_demux, "broken PES size" );
664 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
666 msg_Warn( p_demux, "invalid PES" );
672 /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
678 p_pkt = block_New( p_demux, i_size );
679 memcpy( p_pkt->p_buffer, p, i_size);
682 i_id = ps_pkt_id( p_pkt );
683 if( (i_id&0xffe0) != 0xbd20 ||
684 ps_pkt_parse_pes( p_pkt, 1 ) )
686 block_Release( p_pkt );
690 /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
692 for( i = 0; i < p_sys->i_tracks; i++ )
694 vobsub_track_t *p_tk = &p_sys->track[i];
696 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
699 if( p_tk->p_es && p_tk->i_track_id == i_spu )
701 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
702 p_bk->i_pts = VLC_TS_INVALID; /*only first packet has a pts */
706 if( i >= p_sys->i_tracks )
708 block_Release( p_pkt );