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 *****************************************************************************/
31 #include <sys/types.h>
33 #include <vlc_demux.h>
34 #include <vlc_charset.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t *p_this );
44 static void Close( vlc_object_t *p_this );
47 set_description( _("Vobsub subtitles parser") );
48 set_category( CAT_INPUT );
49 set_subcategory( SUBCAT_INPUT_DEMUX );
50 set_capability( "demux2", 1 );
52 set_callbacks( Open, Close );
54 add_shortcut( "vobsub" );
55 add_shortcut( "subtitle" );
58 /*****************************************************************************
60 *****************************************************************************/
68 static int TextLoad( text_t *, stream_t *s );
69 static void TextUnload( text_t * );
74 int i_vobsub_location;
83 int i_current_subtitle;
85 subtitle_t *p_subtitles;
92 int64_t i_next_demux_date;
96 stream_t *p_vobsub_stream;
100 vobsub_track_t *track;
102 int i_original_frame_width;
103 int i_original_frame_height;
104 vlc_bool_t b_palette;
105 uint32_t palette[16];
108 static int Demux( demux_t * );
109 static int Control( demux_t *, int, va_list );
111 static int ParseVobSubIDX( demux_t * );
112 static int DemuxVobSub( demux_t *, block_t *);
114 /*****************************************************************************
116 *****************************************************************************/
117 static int Open ( vlc_object_t *p_this )
119 demux_t *p_demux = (demux_t*)p_this;
121 char *psz_vobname, *s;
124 if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
126 if( !strcasestr( s, "# VobSub index file" ) )
128 msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
130 if( stream_Seek( p_demux->s, 0 ) )
132 msg_Warn( p_demux, "failed to rewind" );
141 msg_Dbg( p_demux, "could not read vobsub IDX file" );
145 p_demux->pf_demux = Demux;
146 p_demux->pf_control = Control;
147 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
149 p_sys->p_vobsub_stream = NULL;
151 p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
152 p_sys->i_original_frame_width = -1;
153 p_sys->i_original_frame_height = -1;
154 p_sys->b_palette = VLC_FALSE;
155 memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
157 /* Load the whole file */
158 TextLoad( &p_sys->txt, p_demux->s );
161 ParseVobSubIDX( p_demux );
164 TextUnload( &p_sys->txt );
166 /* Find the total length of the vobsubs */
167 if( p_sys->i_tracks > 0 )
170 for( i = 0; i < p_sys->i_tracks; i++ )
172 if( p_sys->track[i].i_subtitles > 1 )
174 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
175 p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
180 asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path );
181 i_len = strlen( psz_vobname );
182 if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
185 p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
186 if( p_sys->p_vobsub_stream == NULL )
188 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
199 /*****************************************************************************
200 * Close: Close subtitle demux
201 *****************************************************************************/
202 static void Close( vlc_object_t *p_this )
205 demux_t *p_demux = (demux_t*)p_this;
206 demux_sys_t *p_sys = p_demux->p_sys;
208 /* Clean all subs from all tracks */
209 for( i = 0; i < p_sys->i_tracks; i++ )
211 if( p_sys->track[i].p_subtitles ) free( p_sys->track[i].p_subtitles );
213 if( p_sys->track ) free( p_sys->track );
215 if( p_sys->p_vobsub_stream )
216 stream_Delete( p_sys->p_vobsub_stream );
221 /*****************************************************************************
223 *****************************************************************************/
224 static int Control( demux_t *p_demux, int i_query, va_list args )
226 demux_sys_t *p_sys = p_demux->p_sys;
233 case DEMUX_GET_LENGTH:
234 pi64 = (int64_t*)va_arg( args, int64_t * );
235 *pi64 = (int64_t) p_sys->i_length;
239 pi64 = (int64_t*)va_arg( args, int64_t * );
240 for( i = 0; i < p_sys->i_tracks; i++ )
242 vlc_bool_t b_selected;
243 /* Check the ES is selected */
244 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
245 p_sys->track[i].p_es, &b_selected );
246 if( b_selected ) break;
248 if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
250 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
256 i64 = (int64_t)va_arg( args, int64_t );
257 for( i = 0; i < p_sys->i_tracks; i++ )
259 p_sys->track[i].i_current_subtitle = 0;
260 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
261 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
263 p_sys->track[i].i_current_subtitle++;
266 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
271 case DEMUX_GET_POSITION:
272 pf = (double*)va_arg( args, double * );
273 for( i = 0; i < p_sys->i_tracks; i++ )
275 vlc_bool_t b_selected;
276 /* Check the ES is selected */
277 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
278 p_sys->track[i].p_es, &b_selected );
279 if( b_selected ) break;
281 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
285 else if( p_sys->track[i].i_subtitles > 0 )
287 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
288 (double)p_sys->i_length;
296 case DEMUX_SET_POSITION:
297 f = (double)va_arg( args, double );
298 i64 = (int64_t) f * p_sys->i_length;
300 for( i = 0; i < p_sys->i_tracks; i++ )
302 p_sys->track[i].i_current_subtitle = 0;
303 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
304 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
306 p_sys->track[i].i_current_subtitle++;
308 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
313 case DEMUX_SET_NEXT_DEMUX_TIME:
314 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
319 case DEMUX_GET_TITLE_INFO:
323 msg_Err( p_demux, "unknown query in subtitle control" );
328 /*****************************************************************************
329 * Demux: Send subtitle to decoder
330 *****************************************************************************/
331 static int Demux( demux_t *p_demux )
333 demux_sys_t *p_sys = p_demux->p_sys;
337 for( i = 0; i < p_sys->i_tracks; i++ )
339 #define tk p_sys->track[i]
340 if( tk.i_current_subtitle >= tk.i_subtitles )
343 i_maxdate = (int64_t) p_sys->i_next_demux_date;
344 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
346 /* Should not happen */
347 i_maxdate = (int64_t) tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
350 while( tk.i_current_subtitle < tk.i_subtitles &&
351 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
353 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
357 /* first compute SPU size */
358 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
360 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
362 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
364 /* Seek at the right place */
365 if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
368 "cannot seek in the VobSub to the correct time %d", i_pos );
369 tk.i_current_subtitle++;
373 /* allocate a packet */
374 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
376 tk.i_current_subtitle++;
381 p_block->i_buffer = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
382 if( p_block->i_buffer <= 6 )
384 block_Release( p_block );
385 tk.i_current_subtitle++;
390 p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
392 /* demux this block */
393 DemuxVobSub( p_demux, p_block );
395 tk.i_current_subtitle++;
401 p_sys->i_next_demux_date = 0;
406 static int TextLoad( text_t *txt, stream_t *s )
412 txt->i_line_count = 0;
414 txt->line = calloc( i_line_max, sizeof( char * ) );
416 /* load the complete file */
419 char *psz = stream_ReadLine( s );
424 txt->line[txt->i_line_count++] = psz;
425 if( txt->i_line_count >= i_line_max )
428 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
432 if( txt->i_line_count <= 0 )
434 if( txt->line ) free( txt->line );
440 static void TextUnload( text_t *txt )
444 for( i = 0; i < txt->i_line_count; i++ )
446 if( txt->line[i] ) free( txt->line[i] );
448 if( txt->line ) free( txt->line );
450 txt->i_line_count = 0;
453 static char *TextGetLine( text_t *txt )
455 if( txt->i_line >= txt->i_line_count )
458 return txt->line[txt->i_line++];
461 static int ParseVobSubIDX( demux_t *p_demux )
463 demux_sys_t *p_sys = p_demux->p_sys;
464 text_t *txt = &p_sys->txt;
466 vobsub_track_t *current_tk;
470 if( ( line = TextGetLine( txt ) ) == NULL )
472 return( VLC_EGENERIC );
475 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
477 else if( !strncmp( "size:", line, 5 ) )
479 /* Store the original size of the video */
480 if( sscanf( line, "size: %dx%d",
481 &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
483 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
487 msg_Warn( p_demux, "reading original frame size failed" );
490 else if( !strncmp( "palette:", line, 8 ) )
494 /* Store the palette of the subs */
495 if( sscanf( line, "palette: %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x",
496 &p_sys->palette[0], &p_sys->palette[1], &p_sys->palette[2], &p_sys->palette[3],
497 &p_sys->palette[4], &p_sys->palette[5], &p_sys->palette[6], &p_sys->palette[7],
498 &p_sys->palette[8], &p_sys->palette[9], &p_sys->palette[10], &p_sys->palette[11],
499 &p_sys->palette[12], &p_sys->palette[13], &p_sys->palette[14], &p_sys->palette[15] ) == 16 )
501 for( i = 0; i < 16; i++ )
503 uint8_t r = 0, g = 0, b = 0;
504 uint8_t y = 0, u = 0, v = 0;
505 r = (p_sys->palette[i] >> 16) & 0xff;
506 g = (p_sys->palette[i] >> 8) & 0xff;
507 b = (p_sys->palette[i] >> 0) & 0xff;
508 /* msg_Dbg( p_demux, "palette %d: R=%x, G=%x, B=%x", i, r, g, b ); */
509 y = (uint8_t) __MIN(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235);
510 u = (uint8_t) __MIN(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240);
511 v = (uint8_t) __MIN(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
512 p_sys->palette[i] = 0;
513 p_sys->palette[i] |= (y&0xff)<<16;
514 p_sys->palette[i] |= (u&0xff);
515 p_sys->palette[i] |= (v&0xff)<<8;
516 /* msg_Dbg( p_demux, "palette %d: y=%x, u=%x, v=%x", i, y, u, v ); */
519 p_sys->b_palette = VLC_TRUE;
520 msg_Dbg( p_demux, "vobsub palette read" );
524 msg_Warn( p_demux, "reading original palette failed" );
527 else if( !strncmp( "id:", line, 3 ) )
533 /* Lets start a new track */
534 if( sscanf( line, "id: %2s, index: %d",
535 language, &i_track_id ) == 2 )
538 p_sys->track = (vobsub_track_t*)realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
541 current_tk = &p_sys->track[p_sys->i_tracks - 1];
542 memset( current_tk, 0, sizeof( vobsub_track_t ) );
543 current_tk->i_current_subtitle = 0;
544 current_tk->i_subtitles = 0;
545 current_tk->p_subtitles = (subtitle_t*)malloc( sizeof( subtitle_t ) );;
546 current_tk->i_track_id = i_track_id;
547 current_tk->i_delay = (int64_t)0;
549 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
550 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
551 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
552 fmt.psz_language = strdup( language );
553 if( p_sys->b_palette )
555 fmt.subs.spu.palette[0] = 0xBeef;
556 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
559 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
560 msg_Dbg( p_demux, "new vobsub track detected" );
564 msg_Warn( p_demux, "reading new track failed" );
567 else if( !strncmp( line, "timestamp:", 10 ) )
570 * timestamp: [sign]hh:mm:ss:mss, filepos: loc
571 * loc is the hex location of the spu in the .sub file
573 int h, m, s, ms, count, loc = 0;
575 int64_t i_start, i_location = 0;
577 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
579 if( sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
580 &h, &count, &m, &s, &ms, &loc ) >= 5 )
582 subtitle_t *current_sub;
584 if( line[count-3] == '-' )
589 i_start = (int64_t) ( h * 3600*1000 +
595 current_tk->i_subtitles++;
596 current_tk->p_subtitles = (subtitle_t*)realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
597 current_sub = ¤t_tk->p_subtitles[current_tk->i_subtitles - 1];
599 current_sub->i_start = (int64_t) i_start * i_sign;
600 current_sub->i_start += current_tk->i_delay;
601 current_sub->i_vobsub_location = i_location;
604 else if( !strncasecmp( line, "delay:", 6 ) )
607 * delay: [sign]hh:mm:ss:mss
609 int h, m, s, ms, count = 0;
613 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
615 if( sscanf( line, "%*celay: %d%n:%d:%d:%d",
616 &h, &count, &m, &s, &ms ) >= 4 )
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: %+lld global delay: %+lld", i_sign, i_gap, current_tk->i_delay );
636 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
638 demux_sys_t *p_sys = p_demux->p_sys;
639 uint8_t *p = p_bk->p_buffer;
640 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
645 int i_size = ps_pkt_size( p, p_end - p );
654 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
656 msg_Warn( p_demux, "invalid PES" );
662 /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
668 p_pkt = block_New( p_demux, i_size );
669 memcpy( p_pkt->p_buffer, p, i_size);
672 i_id = ps_pkt_id( p_pkt );
673 if( (i_id&0xffe0) != 0xbd20 ||
674 ps_pkt_parse_pes( p_pkt, 1 ) )
676 block_Release( p_pkt );
680 /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
682 for( i = 0; i < p_sys->i_tracks; i++ )
684 #define tk p_sys->track[i]
685 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
688 if( tk.p_es && tk.i_track_id == i_spu )
690 es_out_Send( p_demux->out, tk.p_es, p_pkt );
691 p_bk->i_pts = 0; /*only first packet has a pts */
694 else if( i == p_sys->i_tracks - 1 )
696 block_Release( p_pkt );