demux: provide full URL instead of access name (refs #18504)
[vlc.git] / modules / demux / vobsub.c
blob8a832ff3bd4e5a45e3be6e2bc63168fbce008e88
1 /*****************************************************************************
2 * vobsub.c: Demux vobsub files.
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5 * $Id$
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <limits.h>
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
39 #include "mpeg/pes.h"
40 #include "mpeg/ps.h"
41 #include "vobsub.h"
42 #include "subtitle_helper.h"
44 /*****************************************************************************
45 * Module descriptor
46 *****************************************************************************/
47 static int Open ( vlc_object_t *p_this );
48 static void Close( vlc_object_t *p_this );
50 vlc_module_begin ()
51 set_description( N_("Vobsub subtitles parser") )
52 set_category( CAT_INPUT )
53 set_subcategory( SUBCAT_INPUT_DEMUX )
54 set_capability( "demux", 1 )
56 set_callbacks( Open, Close )
58 add_shortcut( "vobsub", "subtitle" )
59 vlc_module_end ()
61 /*****************************************************************************
62 * Prototypes:
63 *****************************************************************************/
65 typedef struct
67 int i_line_count;
68 int i_line;
69 char **line;
70 } text_t;
72 typedef struct
74 int64_t i_start;
75 int i_vobsub_location;
76 } subtitle_t;
78 typedef struct
80 es_out_id_t *p_es;
81 int i_track_id;
83 int i_current_subtitle;
84 int i_subtitles;
85 subtitle_t *p_subtitles;
87 int64_t i_delay;
88 } vobsub_track_t;
90 struct demux_sys_t
92 int64_t i_next_demux_date;
93 int64_t i_length;
95 text_t txt;
96 stream_t *p_vobsub_stream;
98 /* all tracks */
99 int i_tracks;
100 vobsub_track_t *track;
102 int i_original_frame_width;
103 int i_original_frame_height;
104 bool b_palette;
105 uint32_t palette[16];
109 static int Demux( demux_t * );
110 static int Control( demux_t *, int, va_list );
112 static int TextLoad( text_t *, stream_t *s );
113 static void TextUnload( text_t * );
114 static int ParseVobSubIDX( demux_t * );
115 static int DemuxVobSub( demux_t *, block_t *);
117 /*****************************************************************************
118 * Module initializer
119 *****************************************************************************/
120 static int Open ( vlc_object_t *p_this )
122 demux_t *p_demux = (demux_t*)p_this;
123 demux_sys_t *p_sys;
124 char *psz_vobname, *s;
125 int i_len;
126 uint64_t i_read_offset = 0;
128 if( ( s = peek_Readline( p_demux->s, &i_read_offset ) ) != NULL )
130 if( !strcasestr( s, "# VobSub index file" ) )
132 msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
133 free( s );
134 return VLC_EGENERIC;
136 free( s );
138 else
140 msg_Dbg( p_demux, "could not read vobsub IDX file" );
141 return VLC_EGENERIC;
144 /* */
145 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
146 if( unlikely( !p_sys ) )
147 return VLC_ENOMEM;
149 p_sys->i_length = 0;
150 p_sys->p_vobsub_stream = NULL;
151 p_sys->i_tracks = 0;
152 p_sys->track = malloc( sizeof( vobsub_track_t ) );
153 if( unlikely( !p_sys->track ) )
154 goto error;
155 p_sys->i_original_frame_width = -1;
156 p_sys->i_original_frame_height = -1;
157 p_sys->b_palette = false;
158 memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
160 /* Load the whole file */
161 TextLoad( &p_sys->txt, p_demux->s );
163 /* Parse it */
164 ParseVobSubIDX( p_demux );
166 /* Unload */
167 TextUnload( &p_sys->txt );
169 /* Find the total length of the vobsubs */
170 if( p_sys->i_tracks > 0 )
172 for( int i = 0; i < p_sys->i_tracks; i++ )
174 if( p_sys->track[i].i_subtitles > 1 )
176 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
177 p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
182 psz_vobname = strdup( p_demux->psz_url );
183 if( psz_vobname == NULL )
184 goto error;
186 i_len = strlen( psz_vobname );
187 if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
189 /* open file */
190 p_sys->p_vobsub_stream = vlc_stream_NewURL( p_demux, psz_vobname );
191 if( p_sys->p_vobsub_stream == NULL )
193 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
194 psz_vobname );
195 free( psz_vobname );
196 goto error;
198 free( psz_vobname );
200 p_demux->pf_demux = Demux;
201 p_demux->pf_control = Control;
203 return VLC_SUCCESS;
205 error:
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 );
210 free( p_sys );
212 return VLC_EGENERIC;
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 vlc_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 );
230 free( p_sys );
233 /*****************************************************************************
234 * Control:
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;
239 int64_t *pi64, i64;
240 int i;
241 double *pf, f;
243 switch( i_query )
245 case DEMUX_CAN_SEEK:
246 *va_arg( args, bool * ) = true;
247 return VLC_SUCCESS;
249 case DEMUX_GET_LENGTH:
250 pi64 = va_arg( args, int64_t * );
251 *pi64 = (int64_t) p_sys->i_length;
252 return VLC_SUCCESS;
254 case DEMUX_GET_TIME:
255 pi64 = va_arg( args, int64_t * );
256 for( i = 0; i < p_sys->i_tracks; i++ )
258 bool b_selected;
259 /* Check the ES is selected */
260 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
261 p_sys->track[i].p_es, &b_selected );
262 if( b_selected ) break;
264 if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
266 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
267 return VLC_SUCCESS;
269 return VLC_EGENERIC;
271 case DEMUX_SET_TIME:
272 i64 = va_arg( args, int64_t );
273 for( i = 0; i < p_sys->i_tracks; i++ )
275 p_sys->track[i].i_current_subtitle = 0;
276 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
277 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
279 p_sys->track[i].i_current_subtitle++;
282 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
283 return VLC_EGENERIC;
285 return VLC_SUCCESS;
287 case DEMUX_GET_POSITION:
288 pf = va_arg( args, double * );
289 for( i = 0; i < p_sys->i_tracks; i++ )
291 bool b_selected;
292 /* Check the ES is selected */
293 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
294 p_sys->track[i].p_es, &b_selected );
295 if( b_selected ) break;
297 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
299 *pf = 1.0;
301 else if( p_sys->track[i].i_subtitles > 0 )
303 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
304 (double)p_sys->i_length;
306 else
308 *pf = 0.0;
310 return VLC_SUCCESS;
312 case DEMUX_SET_POSITION:
313 f = va_arg( args, double );
314 i64 = (int64_t) f * p_sys->i_length;
316 for( i = 0; i < p_sys->i_tracks; i++ )
318 p_sys->track[i].i_current_subtitle = 0;
319 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
320 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
322 p_sys->track[i].i_current_subtitle++;
324 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
325 return VLC_EGENERIC;
327 return VLC_SUCCESS;
329 case DEMUX_SET_NEXT_DEMUX_TIME:
330 p_sys->i_next_demux_date = va_arg( args, int64_t );
331 return VLC_SUCCESS;
333 case DEMUX_GET_PTS_DELAY:
334 case DEMUX_GET_FPS:
335 case DEMUX_GET_META:
336 case DEMUX_GET_TITLE_INFO:
337 case DEMUX_HAS_UNSUPPORTED_META:
338 case DEMUX_GET_ATTACHMENTS:
339 case DEMUX_CAN_RECORD:
340 return VLC_EGENERIC;
342 default:
343 msg_Warn( p_demux, "unknown query in subtitle control" );
344 return VLC_EGENERIC;
348 /*****************************************************************************
349 * Demux: Send subtitle to decoder
350 *****************************************************************************/
351 static int Demux( demux_t *p_demux )
353 demux_sys_t *p_sys = p_demux->p_sys;
354 int64_t i_maxdate;
355 int i_read;
357 for( int i = 0; i < p_sys->i_tracks; i++ )
359 #define tk p_sys->track[i]
360 if( tk.i_current_subtitle >= tk.i_subtitles )
361 continue;
363 i_maxdate = p_sys->i_next_demux_date;
364 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
366 /* Should not happen */
367 i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
370 while( tk.i_current_subtitle < tk.i_subtitles &&
371 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
373 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
374 block_t *p_block;
375 int i_size = 0;
377 /* first compute SPU size */
378 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
380 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
382 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
384 /* Seek at the right place */
385 if( vlc_stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
387 msg_Warn( p_demux,
388 "cannot seek in the VobSub to the correct time %d", i_pos );
389 tk.i_current_subtitle++;
390 continue;
393 /* allocate a packet */
394 if( ( p_block = block_Alloc( i_size ) ) == NULL )
396 tk.i_current_subtitle++;
397 continue;
400 /* read data */
401 i_read = vlc_stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
402 if( i_read <= 6 )
404 block_Release( p_block );
405 tk.i_current_subtitle++;
406 continue;
408 p_block->i_buffer = i_read;
410 /* pts */
411 p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
413 /* demux this block */
414 DemuxVobSub( p_demux, p_block );
416 block_Release( p_block );
418 tk.i_current_subtitle++;
420 #undef tk
423 /* */
424 p_sys->i_next_demux_date = 0;
426 return 1;
429 static int TextLoad( text_t *txt, stream_t *s )
431 char **lines = NULL;
432 size_t n = 0;
434 /* load the complete file */
435 for( ;; )
437 char *psz = vlc_stream_ReadLine( s );
438 char **ppsz_new;
440 if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
442 free( psz );
443 break;
446 ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
447 if( ppsz_new == NULL )
449 free( psz );
450 break;
452 lines = ppsz_new;
453 lines[n++] = psz;
456 txt->i_line_count = n;
457 txt->i_line = 0;
458 txt->line = lines;
460 return VLC_SUCCESS;
463 static void TextUnload( text_t *txt )
465 for( int i = 0; i < txt->i_line_count; i++ )
466 free( txt->line[i] );
467 free( txt->line );
469 txt->i_line = 0;
470 txt->i_line_count = 0;
473 static char *TextGetLine( text_t *txt )
475 if( txt->i_line >= txt->i_line_count )
476 return( NULL );
478 return txt->line[txt->i_line++];
481 static int ParseVobSubIDX( demux_t *p_demux )
483 demux_sys_t *p_sys = p_demux->p_sys;
484 text_t *txt = &p_sys->txt;
485 char *line;
487 for( ;; )
489 if( ( line = TextGetLine( txt ) ) == NULL )
491 return( VLC_EGENERIC );
494 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
496 continue;
498 else if( !strncmp( "size:", line, 5 ) )
500 /* Store the original size of the video */
501 if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
502 &p_sys->i_original_frame_height ) == VLC_SUCCESS )
504 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
506 else
508 msg_Warn( p_demux, "reading original frame size failed" );
511 else if( !strncmp( "palette:", line, 8 ) )
513 if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
515 p_sys->b_palette = true;
516 msg_Dbg( p_demux, "vobsub palette read" );
518 else
520 msg_Warn( p_demux, "reading original palette failed" );
523 else if( !strncmp( "id:", line, 3 ) )
525 char language[33]; /* Usually 2 or 3 letters, sometimes more.
526 Spec (or lack of) doesn't define any limit */
527 int i_track_id;
528 es_format_t fmt;
530 /* Lets start a new track */
531 if( sscanf( line, "id: %32[^ ,], index: %d",
532 language, &i_track_id ) != 2 )
534 if( sscanf( line, "id: , index: %d", &i_track_id ) != 1 )
536 msg_Warn( p_demux, "reading new track failed" );
537 continue;
539 language[0] = '\0';
542 p_sys->i_tracks++;
543 p_sys->track = xrealloc( p_sys->track,
544 sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
546 /* Init the track */
547 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
548 memset( current_tk, 0, sizeof( vobsub_track_t ) );
549 current_tk->i_current_subtitle = 0;
550 current_tk->i_subtitles = 0;
551 current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );
552 current_tk->i_track_id = i_track_id;
553 current_tk->i_delay = (int64_t)0;
555 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
556 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
557 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
558 fmt.psz_language = language;
559 if( p_sys->b_palette )
561 fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
562 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
565 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
566 msg_Dbg( p_demux, "New vobsub track detected: %i [%s]", i_track_id, language );
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;
575 int i_sign = 1;
576 int64_t i_start, i_location = 0;
578 if( p_sys->i_tracks > 0 &&
579 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
580 &h, &count, &m, &s, &ms, &loc ) >= 5 )
582 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
583 subtitle_t *current_sub;
585 if( line[count-3] == '-' )
587 i_sign = -1;
588 h = -h;
590 i_start = (int64_t) ( h * 3600*1000 +
591 m * 60*1000 +
592 s * 1000 +
593 ms ) * 1000;
594 i_location = loc;
596 current_tk->i_subtitles++;
597 current_tk->p_subtitles =
598 xrealloc( current_tk->p_subtitles,
599 sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
600 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
602 current_sub->i_start = i_start * i_sign;
603 current_sub->i_start += current_tk->i_delay;
604 current_sub->i_vobsub_location = i_location;
606 else
608 msg_Warn( p_demux, "reading timestamp failed" );
611 else if( !strncasecmp( line, "delay:", 6 ) )
614 * delay: [sign]hh:mm:ss:mss
616 int h, m, s, ms, count = 0;
617 int i_sign = 1;
618 int64_t i_gap = 0;
620 if( p_sys->i_tracks > 0 &&
621 sscanf( line, "%*celay: %d%n:%d:%d:%d",
622 &h, &count, &m, &s, &ms ) >= 4 )
624 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
625 if( line[count-3] == '-' )
627 i_sign = -1;
628 h = -h;
630 i_gap = (int64_t) ( h * 3600*1000 +
631 m * 60*1000 +
632 s * 1000 +
633 ms ) * 1000;
635 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
636 msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
637 i_sign, i_gap, current_tk->i_delay );
639 else
641 msg_Warn( p_demux, "reading delay failed" );
645 return( 0 );
648 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
650 demux_sys_t *p_sys = p_demux->p_sys;
651 uint8_t *p = p_bk->p_buffer;
652 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
653 int i;
655 while( p + 6 < p_end )
657 int i_size = ps_pkt_size( p, p_end - p );
658 block_t *p_pkt;
659 int i_id;
660 int i_spu;
662 if( i_size <= 0 )
663 break;
665 if( i_size > p_end - p )
667 msg_Warn( p_demux, "broken PES size" );
668 break;
671 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
673 msg_Warn( p_demux, "invalid PES" );
674 break;
677 if( p[3] != 0xbd )
679 /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
680 p += i_size;
681 continue;
684 /* Create a block */
685 p_pkt = block_Alloc( i_size );
686 if( unlikely(p_pkt == NULL) )
687 break;
688 memcpy( p_pkt->p_buffer, p, i_size);
689 p += i_size;
691 i_id = ps_pkt_id( p_pkt );
692 if( (i_id&0xffe0) != 0xbd20 ||
693 ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, 1 ) )
695 block_Release( p_pkt );
696 continue;
698 i_spu = i_id&0x1f;
699 /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
701 for( i = 0; i < p_sys->i_tracks; i++ )
703 vobsub_track_t *p_tk = &p_sys->track[i];
705 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
706 p_pkt->i_length = 0;
708 if( p_tk->p_es && p_tk->i_track_id == i_spu )
710 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
711 p_bk->i_pts = VLC_TS_INVALID; /*only first packet has a pts */
712 break;
715 if( i >= p_sys->i_tracks )
717 block_Release( p_pkt );
721 return VLC_SUCCESS;