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_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;
91 int64_t i_next_demux_date;
99 vobsub_track_t *track;
101 int i_original_frame_width;
102 int i_original_frame_height;
105 static int Demux( demux_t * );
106 static int Control( demux_t *, int, va_list );
108 static int ParseVobSubIDX( demux_t * );
109 static int DemuxVobSub( demux_t *, block_t *);
111 /*****************************************************************************
113 *****************************************************************************/
114 static int Open ( vlc_object_t *p_this )
116 demux_t *p_demux = (demux_t*)p_this;
118 char *psz_vobname, *s;
121 if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
123 if( !strcasestr( s, "# VobSub index file" ) )
125 msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
127 if( stream_Seek( p_demux->s, 0 ) )
129 msg_Warn( p_demux, "failed to rewind" );
138 msg_Dbg( p_demux, "could not read vobsub IDX file" );
142 p_demux->pf_demux = Demux;
143 p_demux->pf_control = Control;
144 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
146 p_sys->p_vobsub_file = NULL;
148 p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
149 p_sys->i_original_frame_width = -1;
150 p_sys->i_original_frame_height = -1;
152 /* Load the whole file */
153 TextLoad( &p_sys->txt, p_demux->s );
156 ParseVobSubIDX( p_demux );
159 TextUnload( &p_sys->txt );
161 /* Find the total length of the vobsubs */
162 if( p_sys->i_tracks > 0 )
165 for( i = 0; i < p_sys->i_tracks; i++ )
167 if( p_sys->track[i].i_subtitles > 1 )
169 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
170 p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
175 i_len = strlen( p_demux->psz_path );
176 psz_vobname = strdup( p_demux->psz_path );
178 strcpy( psz_vobname + i_len - 4, ".sub" );
181 if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
183 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
194 /*****************************************************************************
195 * Close: Close subtitle demux
196 *****************************************************************************/
197 static void Close( vlc_object_t *p_this )
199 demux_t *p_demux = (demux_t*)p_this;
200 demux_sys_t *p_sys = p_demux->p_sys;
202 /* Clean all subs from all tracks
203 if( p_sys->subtitle )
204 free( p_sys->subtitle );
206 if( p_sys->p_vobsub_file )
207 fclose( p_sys->p_vobsub_file );
212 /*****************************************************************************
214 *****************************************************************************/
215 static int Control( demux_t *p_demux, int i_query, va_list args )
217 demux_sys_t *p_sys = p_demux->p_sys;
224 case DEMUX_GET_LENGTH:
225 pi64 = (int64_t*)va_arg( args, int64_t * );
226 *pi64 = (int64_t) p_sys->i_length;
230 pi64 = (int64_t*)va_arg( args, int64_t * );
231 for( i = 0; i < p_sys->i_tracks; i++ )
233 vlc_bool_t b_selected;
234 /* Check the ES is selected */
235 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
236 p_sys->track[i].p_es, &b_selected );
237 if( b_selected ) break;
239 if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
241 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
247 i64 = (int64_t)va_arg( args, int64_t );
248 for( i = 0; i < p_sys->i_tracks; i++ )
250 p_sys->track[i].i_current_subtitle = 0;
251 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
252 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
254 p_sys->track[i].i_current_subtitle++;
257 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
262 case DEMUX_GET_POSITION:
263 pf = (double*)va_arg( args, double * );
264 for( i = 0; i < p_sys->i_tracks; i++ )
266 vlc_bool_t b_selected;
267 /* Check the ES is selected */
268 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
269 p_sys->track[i].p_es, &b_selected );
270 if( b_selected ) break;
272 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
276 else if( p_sys->track[i].i_subtitles > 0 )
278 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
279 (double)p_sys->i_length;
287 case DEMUX_SET_POSITION:
288 f = (double)va_arg( args, double );
289 i64 = (int64_t) f * p_sys->i_length;
291 for( i = 0; i < p_sys->i_tracks; i++ )
293 p_sys->track[i].i_current_subtitle = 0;
294 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
295 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
297 p_sys->track[i].i_current_subtitle++;
299 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
304 case DEMUX_SET_NEXT_DEMUX_TIME:
305 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
310 case DEMUX_GET_TITLE_INFO:
314 msg_Err( p_demux, "unknown query in subtitle control" );
319 /*****************************************************************************
320 * Demux: Send subtitle to decoder
321 *****************************************************************************/
322 static int Demux( demux_t *p_demux )
324 demux_sys_t *p_sys = p_demux->p_sys;
328 for( i = 0; i < p_sys->i_tracks; i++ )
330 #define tk p_sys->track[i]
331 if( tk.i_current_subtitle >= tk.i_subtitles )
334 i_maxdate = (int64_t) p_sys->i_next_demux_date;
335 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
337 /* Should not happen */
338 i_maxdate = (int64_t) tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
341 while( tk.i_current_subtitle < tk.i_subtitles &&
342 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
344 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
348 /* first compute SPU size */
349 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
351 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
353 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
355 /* Seek at the right place */
356 if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
359 "cannot seek at right vobsub location %d", i_pos );
360 tk.i_current_subtitle++;
364 /* allocate a packet */
365 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
367 tk.i_current_subtitle++;
372 p_block->i_buffer = fread( p_block->p_buffer, 1, i_size,
373 p_sys->p_vobsub_file );
374 if( p_block->i_buffer <= 6 )
376 block_Release( p_block );
377 tk.i_current_subtitle++;
382 p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
384 /* demux this block */
385 DemuxVobSub( p_demux, p_block );
387 tk.i_current_subtitle++;
393 p_sys->i_next_demux_date = 0;
398 static int TextLoad( text_t *txt, stream_t *s )
404 txt->i_line_count = 0;
406 txt->line = calloc( i_line_max, sizeof( char * ) );
408 /* load the complete file */
411 char *psz = stream_ReadLine( s );
416 txt->line[txt->i_line_count++] = psz;
417 if( txt->i_line_count >= i_line_max )
420 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
424 if( txt->i_line_count <= 0 )
432 static void TextUnload( text_t *txt )
436 for( i = 0; i < txt->i_line_count; i++ )
438 free( txt->line[i] );
442 txt->i_line_count = 0;
445 static char *TextGetLine( text_t *txt )
447 if( txt->i_line >= txt->i_line_count )
450 return txt->line[txt->i_line++];
453 static int ParseVobSubIDX( demux_t *p_demux )
455 demux_sys_t *p_sys = p_demux->p_sys;
456 text_t *txt = &p_sys->txt;
458 vobsub_track_t *current_tk;
462 if( ( line = TextGetLine( txt ) ) == NULL )
464 return( VLC_EGENERIC );
467 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
469 else if( !strncmp( "size:", line, 5 ) )
471 /* Store the original size of the video */
472 if( sscanf( line, "size: %dx%d",
473 &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
475 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
479 msg_Warn( p_demux, "reading original frame size failed" );
482 else if( !strncmp( "id:", line, 3 ) )
488 /* Lets start a new track */
489 if( sscanf( line, "id: %2s, index: %d",
490 language, &i_track_id ) == 2 )
493 p_sys->track = (vobsub_track_t*)realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
496 current_tk = &p_sys->track[p_sys->i_tracks - 1];
497 memset( current_tk, 0, sizeof( vobsub_track_t ) );
498 current_tk->i_current_subtitle = 0;
499 current_tk->i_subtitles = 0;
500 current_tk->p_subtitles = (subtitle_t*)malloc( sizeof( subtitle_t ) );;
501 current_tk->i_track_id = i_track_id;
503 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
504 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
505 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
506 fmt.psz_language = strdup( language );
507 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
509 msg_Dbg( p_demux, "new vobsub track detected" );
513 msg_Warn( p_demux, "reading new track failed" );
516 else if( !strncmp( line, "timestamp:", 10 ) )
519 * timestamp: hh:mm:ss:mss, filepos: loc
520 * loc is the hex location of the spu in the .sub file
523 int h, m, s, ms, loc;
524 int64_t i_start, i_location = 0;
526 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
528 if( sscanf( line, "timestamp: %d:%d:%d:%d, filepos: %x",
529 &h, &m, &s, &ms, &loc ) == 5 )
531 subtitle_t *current_sub;
533 i_start = (int64_t) ( h * 3600*1000 +
539 current_tk->i_subtitles++;
540 current_tk->p_subtitles = (subtitle_t*)realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
541 current_sub = ¤t_tk->p_subtitles[current_tk->i_subtitles - 1];
543 current_sub->i_start = (int64_t) i_start;
544 current_sub->i_vobsub_location = i_location;
551 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
553 demux_sys_t *p_sys = p_demux->p_sys;
554 uint8_t *p = p_bk->p_buffer;
555 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
560 int i_size = ps_pkt_size( p, p_end - p );
569 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
571 msg_Warn( p_demux, "invalid PES" );
577 /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
583 p_pkt = block_New( p_demux, i_size );
584 memcpy( p_pkt->p_buffer, p, i_size);
587 i_id = ps_pkt_id( p_pkt );
588 if( (i_id&0xffe0) != 0xbd20 ||
589 ps_pkt_parse_pes( p_pkt, 1 ) )
591 block_Release( p_pkt );
595 /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
597 for( i = 0; i < p_sys->i_tracks; i++ )
599 #define tk p_sys->track[i]
600 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
603 if( tk.p_es && tk.i_track_id == i_spu )
605 es_out_Send( p_demux->out, tk.p_es, p_pkt );
606 p_bk->i_pts = 0; /*only first packet has a pts */
609 else if( i == p_sys->i_tracks - 1 )
611 block_Release( p_pkt );