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 *****************************************************************************/
30 #include <sys/types.h>
33 #include <vlc/input.h>
34 #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 parser") );
49 set_category( CAT_INPUT );
50 set_subcategory( SUBCAT_INPUT_DEMUX );
51 set_capability( "demux2", 1 );
53 set_callbacks( Open, Close );
55 add_shortcut( "vobsub" );
56 add_shortcut( "subtitle" );
59 /*****************************************************************************
61 *****************************************************************************/
69 static int TextLoad( text_t *, stream_t *s );
70 static void TextUnload( text_t * );
75 int i_vobsub_location;
84 int i_current_subtitle;
86 subtitle_t *p_subtitles;
93 int64_t i_next_demux_date;
97 stream_t *p_vobsub_stream;
101 vobsub_track_t *track;
103 int i_original_frame_width;
104 int i_original_frame_height;
105 vlc_bool_t b_palette;
106 uint32_t palette[16];
109 static int Demux( demux_t * );
110 static int Control( demux_t *, int, va_list );
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" );
142 msg_Dbg( p_demux, "could not read vobsub IDX file" );
146 p_demux->pf_demux = Demux;
147 p_demux->pf_control = Control;
148 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
150 p_sys->p_vobsub_stream = NULL;
152 p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
153 p_sys->i_original_frame_width = -1;
154 p_sys->i_original_frame_height = -1;
155 p_sys->b_palette = VLC_FALSE;
156 memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
158 /* Load the whole file */
159 TextLoad( &p_sys->txt, p_demux->s );
162 ParseVobSubIDX( p_demux );
165 TextUnload( &p_sys->txt );
167 /* Find the total length of the vobsubs */
168 if( p_sys->i_tracks > 0 )
171 for( i = 0; i < p_sys->i_tracks; i++ )
173 if( p_sys->track[i].i_subtitles > 1 )
175 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
176 p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
181 psz_vobname = strdup( p_demux->psz_path );
182 i_len = strlen( psz_vobname );
183 memcpy( psz_vobname + i_len - 4, ".sub", 4 );
186 p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
187 if( p_sys->p_vobsub_stream == NULL )
189 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
200 /*****************************************************************************
201 * Close: Close subtitle demux
202 *****************************************************************************/
203 static void Close( vlc_object_t *p_this )
206 demux_t *p_demux = (demux_t*)p_this;
207 demux_sys_t *p_sys = p_demux->p_sys;
209 /* Clean all subs from all tracks */
210 for( i = 0; i < p_sys->i_tracks; i++ )
212 if( p_sys->track[i].p_subtitles ) free( p_sys->track[i].p_subtitles );
214 if( p_sys->track ) free( p_sys->track );
216 if( p_sys->p_vobsub_stream )
217 stream_Delete( p_sys->p_vobsub_stream );
222 /*****************************************************************************
224 *****************************************************************************/
225 static int Control( demux_t *p_demux, int i_query, va_list args )
227 demux_sys_t *p_sys = p_demux->p_sys;
234 case DEMUX_GET_LENGTH:
235 pi64 = (int64_t*)va_arg( args, int64_t * );
236 *pi64 = (int64_t) p_sys->i_length;
240 pi64 = (int64_t*)va_arg( args, int64_t * );
241 for( i = 0; i < p_sys->i_tracks; i++ )
243 vlc_bool_t b_selected;
244 /* Check the ES is selected */
245 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
246 p_sys->track[i].p_es, &b_selected );
247 if( b_selected ) break;
249 if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
251 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
257 i64 = (int64_t)va_arg( args, int64_t );
258 for( i = 0; i < p_sys->i_tracks; i++ )
260 p_sys->track[i].i_current_subtitle = 0;
261 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
262 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
264 p_sys->track[i].i_current_subtitle++;
267 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
272 case DEMUX_GET_POSITION:
273 pf = (double*)va_arg( args, double * );
274 for( i = 0; i < p_sys->i_tracks; i++ )
276 vlc_bool_t b_selected;
277 /* Check the ES is selected */
278 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
279 p_sys->track[i].p_es, &b_selected );
280 if( b_selected ) break;
282 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
286 else if( p_sys->track[i].i_subtitles > 0 )
288 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
289 (double)p_sys->i_length;
297 case DEMUX_SET_POSITION:
298 f = (double)va_arg( args, double );
299 i64 = (int64_t) f * p_sys->i_length;
301 for( i = 0; i < p_sys->i_tracks; i++ )
303 p_sys->track[i].i_current_subtitle = 0;
304 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
305 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
307 p_sys->track[i].i_current_subtitle++;
309 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
314 case DEMUX_SET_NEXT_DEMUX_TIME:
315 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
320 case DEMUX_GET_TITLE_INFO:
324 msg_Err( p_demux, "unknown query in subtitle control" );
329 /*****************************************************************************
330 * Demux: Send subtitle to decoder
331 *****************************************************************************/
332 static int Demux( demux_t *p_demux )
334 demux_sys_t *p_sys = p_demux->p_sys;
338 for( i = 0; i < p_sys->i_tracks; i++ )
340 #define tk p_sys->track[i]
341 if( tk.i_current_subtitle >= tk.i_subtitles )
344 i_maxdate = (int64_t) p_sys->i_next_demux_date;
345 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
347 /* Should not happen */
348 i_maxdate = (int64_t) tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
351 while( tk.i_current_subtitle < tk.i_subtitles &&
352 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
354 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
358 /* first compute SPU size */
359 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
361 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
363 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
365 /* Seek at the right place */
366 if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
369 "cannot seek in the VobSub to the correct time %d", i_pos );
370 tk.i_current_subtitle++;
374 /* allocate a packet */
375 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
377 tk.i_current_subtitle++;
382 p_block->i_buffer = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
383 if( p_block->i_buffer <= 6 )
385 block_Release( p_block );
386 tk.i_current_subtitle++;
391 p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
393 /* demux this block */
394 DemuxVobSub( p_demux, p_block );
396 tk.i_current_subtitle++;
402 p_sys->i_next_demux_date = 0;
407 static int TextLoad( text_t *txt, stream_t *s )
413 txt->i_line_count = 0;
415 txt->line = calloc( i_line_max, sizeof( char * ) );
417 /* load the complete file */
420 char *psz = stream_ReadLine( s );
425 txt->line[txt->i_line_count++] = psz;
426 if( txt->i_line_count >= i_line_max )
429 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
433 if( txt->i_line_count <= 0 )
435 if( txt->line ) free( txt->line );
441 static void TextUnload( text_t *txt )
445 for( i = 0; i < txt->i_line_count; i++ )
447 if( txt->line[i] ) free( txt->line[i] );
449 if( txt->line ) free( txt->line );
451 txt->i_line_count = 0;
454 static char *TextGetLine( text_t *txt )
456 if( txt->i_line >= txt->i_line_count )
459 return txt->line[txt->i_line++];
462 static int ParseVobSubIDX( demux_t *p_demux )
464 demux_sys_t *p_sys = p_demux->p_sys;
465 text_t *txt = &p_sys->txt;
467 vobsub_track_t *current_tk;
471 if( ( line = TextGetLine( txt ) ) == NULL )
473 return( VLC_EGENERIC );
476 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
478 else if( !strncmp( "size:", line, 5 ) )
480 /* Store the original size of the video */
481 if( sscanf( line, "size: %dx%d",
482 &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
484 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
488 msg_Warn( p_demux, "reading original frame size failed" );
491 else if( !strncmp( "palette:", line, 8 ) )
495 /* Store the palette of the subs */
496 if( sscanf( line, "palette: %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x",
497 &p_sys->palette[0], &p_sys->palette[1], &p_sys->palette[2], &p_sys->palette[3],
498 &p_sys->palette[4], &p_sys->palette[5], &p_sys->palette[6], &p_sys->palette[7],
499 &p_sys->palette[8], &p_sys->palette[9], &p_sys->palette[10], &p_sys->palette[11],
500 &p_sys->palette[12], &p_sys->palette[13], &p_sys->palette[14], &p_sys->palette[15] ) == 16 )
502 for( i = 0; i < 16; i++ )
504 uint8_t r = 0, g = 0, b = 0;
505 uint8_t y = 0, u = 0, v = 0;
506 r = (p_sys->palette[i] >> 16) & 0xff;
507 g = (p_sys->palette[i] >> 8) & 0xff;
508 b = (p_sys->palette[i] >> 0) & 0xff;
509 /* msg_Dbg( p_demux, "palette %d: R=%x, G=%x, B=%x", i, r, g, b ); */
510 y = (uint8_t) __MIN(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235);
511 u = (uint8_t) __MIN(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240);
512 v = (uint8_t) __MIN(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
513 p_sys->palette[i] = 0;
514 p_sys->palette[i] |= (y&0xff)<<16;
515 p_sys->palette[i] |= (u&0xff);
516 p_sys->palette[i] |= (v&0xff)<<8;
517 /* msg_Dbg( p_demux, "palette %d: y=%x, u=%x, v=%x", i, y, u, v ); */
520 p_sys->b_palette = VLC_TRUE;
521 msg_Dbg( p_demux, "vobsub palette read" );
525 msg_Warn( p_demux, "reading original palette failed" );
528 else if( !strncmp( "id:", line, 3 ) )
534 /* Lets start a new track */
535 if( sscanf( line, "id: %2s, index: %d",
536 language, &i_track_id ) == 2 )
539 p_sys->track = (vobsub_track_t*)realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
542 current_tk = &p_sys->track[p_sys->i_tracks - 1];
543 memset( current_tk, 0, sizeof( vobsub_track_t ) );
544 current_tk->i_current_subtitle = 0;
545 current_tk->i_subtitles = 0;
546 current_tk->p_subtitles = (subtitle_t*)malloc( sizeof( subtitle_t ) );;
547 current_tk->i_track_id = i_track_id;
548 current_tk->i_delay = (int64_t)0;
550 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
551 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
552 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
553 fmt.psz_language = strdup( language );
554 if( p_sys->b_palette )
556 fmt.subs.spu.palette[0] = 0xBeef;
557 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
560 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
561 msg_Dbg( p_demux, "new vobsub track detected" );
565 msg_Warn( p_demux, "reading new track failed" );
568 else if( !strncmp( line, "timestamp:", 10 ) )
571 * timestamp: [sign]hh:mm:ss:mss, filepos: loc
572 * loc is the hex location of the spu in the .sub file
574 int h, m, s, ms, count, loc = 0;
576 int64_t i_start, i_location = 0;
578 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
580 if( sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
581 &h, &count, &m, &s, &ms, &loc ) >= 5 )
583 subtitle_t *current_sub;
585 if( line[count-3] == '-' )
590 i_start = (int64_t) ( h * 3600*1000 +
596 current_tk->i_subtitles++;
597 current_tk->p_subtitles = (subtitle_t*)realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
598 current_sub = ¤t_tk->p_subtitles[current_tk->i_subtitles - 1];
600 current_sub->i_start = (int64_t) i_start * i_sign;
601 current_sub->i_start += current_tk->i_delay;
602 current_sub->i_vobsub_location = i_location;
605 else if( !strncasecmp( line, "delay:", 6 ) )
608 * delay: [sign]hh:mm:ss:mss
610 int h, m, s, ms, count = 0;
614 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
616 if( sscanf( line, "%*celay: %d%n:%d:%d:%d",
617 &h, &count, &m, &s, &ms ) >= 4 )
619 if( line[count-3] == '-' )
624 i_gap = (int64_t) ( h * 3600*1000 +
629 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
630 msg_Dbg( p_demux, "sign: %+d gap: %+lld global delay: %+lld", i_sign, i_gap, current_tk->i_delay );
637 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
639 demux_sys_t *p_sys = p_demux->p_sys;
640 uint8_t *p = p_bk->p_buffer;
641 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
646 int i_size = ps_pkt_size( p, p_end - p );
655 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
657 msg_Warn( p_demux, "invalid PES" );
663 /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
669 p_pkt = block_New( p_demux, i_size );
670 memcpy( p_pkt->p_buffer, p, i_size);
673 i_id = ps_pkt_id( p_pkt );
674 if( (i_id&0xffe0) != 0xbd20 ||
675 ps_pkt_parse_pes( p_pkt, 1 ) )
677 block_Release( p_pkt );
681 /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
683 for( i = 0; i < p_sys->i_tracks; i++ )
685 #define tk p_sys->track[i]
686 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
689 if( tk.p_es && tk.i_track_id == i_spu )
691 es_out_Send( p_demux->out, tk.p_es, p_pkt );
692 p_bk->i_pts = 0; /*only first packet has a pts */
695 else if( i == p_sys->i_tracks - 1 )
697 block_Release( p_pkt );