demux: mp4: use static mapping table per layout
[vlc.git] / modules / demux / vobsub.c
blobc52af18016afc33e4c72562533952a947119cb10
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 vlc_tick_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 vlc_tick_t i_delay;
88 } vobsub_track_t;
90 typedef struct
92 vlc_tick_t i_next_demux_date;
93 vlc_tick_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];
106 } demux_sys_t;
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->p_vobsub_stream = NULL;
150 p_sys->i_tracks = 0;
151 p_sys->track = malloc( sizeof( vobsub_track_t ) );
152 if( unlikely( !p_sys->track ) )
153 goto error;
154 p_sys->i_original_frame_width = -1;
155 p_sys->i_original_frame_height = -1;
156 p_sys->b_palette = false;
157 memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
159 /* Load the whole file */
160 TextLoad( &p_sys->txt, p_demux->s );
162 /* Parse it */
163 ParseVobSubIDX( p_demux );
165 /* Unload */
166 TextUnload( &p_sys->txt );
168 /* Find the total length of the vobsubs */
169 p_sys->i_length = 0;
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 = p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start;
181 if ( p_sys->i_length != 0)
182 p_sys->i_length += VLC_TICK_FROM_SEC( 1 );
184 psz_vobname = strdup( p_demux->psz_url );
185 if( psz_vobname == NULL )
186 goto error;
188 i_len = strlen( psz_vobname );
189 if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
191 /* open file */
192 p_sys->p_vobsub_stream = vlc_stream_NewURL( p_demux, psz_vobname );
193 if( p_sys->p_vobsub_stream == NULL )
195 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
196 psz_vobname );
197 free( psz_vobname );
198 goto error;
200 free( psz_vobname );
202 p_demux->pf_demux = Demux;
203 p_demux->pf_control = Control;
205 return VLC_SUCCESS;
207 error:
208 /* Clean all subs from all tracks */
209 for( int i = 0; i < p_sys->i_tracks; i++ )
210 free( p_sys->track[i].p_subtitles );
211 free( p_sys->track );
212 free( p_sys );
214 return VLC_EGENERIC;
217 /*****************************************************************************
218 * Close: Close subtitle demux
219 *****************************************************************************/
220 static void Close( vlc_object_t *p_this )
222 demux_t *p_demux = (demux_t*)p_this;
223 demux_sys_t *p_sys = p_demux->p_sys;
225 if( p_sys->p_vobsub_stream )
226 vlc_stream_Delete( p_sys->p_vobsub_stream );
228 /* Clean all subs from all tracks */
229 for( int i = 0; i < p_sys->i_tracks; i++ )
230 free( p_sys->track[i].p_subtitles );
231 free( p_sys->track );
232 free( p_sys );
235 /*****************************************************************************
236 * Control:
237 *****************************************************************************/
238 static int Control( demux_t *p_demux, int i_query, va_list args )
240 demux_sys_t *p_sys = p_demux->p_sys;
241 vlc_tick_t i64;
242 int i;
243 double *pf, f;
245 switch( i_query )
247 case DEMUX_CAN_SEEK:
248 *va_arg( args, bool * ) = true;
249 return VLC_SUCCESS;
251 case DEMUX_GET_LENGTH:
252 *va_arg( args, vlc_tick_t * ) = p_sys->i_length;
253 return VLC_SUCCESS;
255 case DEMUX_GET_TIME:
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 *va_arg( args, vlc_tick_t * ) = 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, vlc_tick_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 = (vlc_tick_t) f * p_sys->i_length;
315 return demux_Control(p_demux, DEMUX_SET_TIME, i64, false);
317 case DEMUX_SET_NEXT_DEMUX_TIME:
318 p_sys->i_next_demux_date = va_arg( args, vlc_tick_t );
319 return VLC_SUCCESS;
321 case DEMUX_CAN_PAUSE:
322 case DEMUX_SET_PAUSE_STATE:
323 case DEMUX_CAN_CONTROL_PACE:
324 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
326 case DEMUX_GET_PTS_DELAY:
327 case DEMUX_GET_FPS:
328 case DEMUX_GET_META:
329 case DEMUX_GET_TITLE_INFO:
330 case DEMUX_HAS_UNSUPPORTED_META:
331 case DEMUX_GET_ATTACHMENTS:
332 case DEMUX_CAN_RECORD:
333 return VLC_EGENERIC;
335 default:
336 msg_Warn( p_demux, "unknown query in subtitle control" );
337 return VLC_EGENERIC;
341 /*****************************************************************************
342 * Demux: Send subtitle to decoder
343 *****************************************************************************/
344 static int Demux( demux_t *p_demux )
346 demux_sys_t *p_sys = p_demux->p_sys;
347 vlc_tick_t i_maxdate;
348 int i_read;
350 for( int i = 0; i < p_sys->i_tracks; i++ )
352 #define tk p_sys->track[i]
353 if( tk.i_current_subtitle >= tk.i_subtitles )
354 continue;
356 i_maxdate = p_sys->i_next_demux_date;
357 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
359 /* Should not happen */
360 i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
363 while( tk.i_current_subtitle < tk.i_subtitles &&
364 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
366 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
367 block_t *p_block;
368 int i_size = 0;
370 /* first compute SPU size */
371 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
373 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
375 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
377 /* Seek at the right place */
378 if( vlc_stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
380 msg_Warn( p_demux,
381 "cannot seek in the VobSub to the correct time %d", i_pos );
382 tk.i_current_subtitle++;
383 continue;
386 /* allocate a packet */
387 if( ( p_block = block_Alloc( i_size ) ) == NULL )
389 tk.i_current_subtitle++;
390 continue;
393 /* read data */
394 i_read = vlc_stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
395 if( i_read <= 6 )
397 block_Release( p_block );
398 tk.i_current_subtitle++;
399 continue;
401 p_block->i_buffer = i_read;
403 /* pts */
404 p_block->i_pts = VLC_TICK_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
406 /* demux this block */
407 DemuxVobSub( p_demux, p_block );
409 block_Release( p_block );
411 tk.i_current_subtitle++;
413 #undef tk
416 /* */
417 p_sys->i_next_demux_date = 0;
419 return 1;
422 static int TextLoad( text_t *txt, stream_t *s )
424 char **lines = NULL;
425 size_t n = 0;
427 /* load the complete file */
428 for( ;; )
430 char *psz = vlc_stream_ReadLine( s );
431 char **ppsz_new;
433 if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
435 free( psz );
436 break;
439 ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
440 if( ppsz_new == NULL )
442 free( psz );
443 break;
445 lines = ppsz_new;
446 lines[n++] = psz;
449 txt->i_line_count = n;
450 txt->i_line = 0;
451 txt->line = lines;
453 return VLC_SUCCESS;
456 static void TextUnload( text_t *txt )
458 for( int i = 0; i < txt->i_line_count; i++ )
459 free( txt->line[i] );
460 free( txt->line );
462 txt->i_line = 0;
463 txt->i_line_count = 0;
466 static char *TextGetLine( text_t *txt )
468 if( txt->i_line >= txt->i_line_count )
469 return( NULL );
471 return txt->line[txt->i_line++];
474 static int ParseVobSubIDX( demux_t *p_demux )
476 demux_sys_t *p_sys = p_demux->p_sys;
477 text_t *txt = &p_sys->txt;
478 char *line;
480 for( ;; )
482 if( ( line = TextGetLine( txt ) ) == NULL )
484 return( VLC_EGENERIC );
487 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
489 continue;
491 else if( !strncmp( "size:", line, 5 ) )
493 /* Store the original size of the video */
494 if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
495 &p_sys->i_original_frame_height ) == VLC_SUCCESS )
497 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
499 else
501 msg_Warn( p_demux, "reading original frame size failed" );
504 else if( !strncmp( "palette:", line, 8 ) )
506 if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
508 p_sys->b_palette = true;
509 msg_Dbg( p_demux, "vobsub palette read" );
511 else
513 msg_Warn( p_demux, "reading original palette failed" );
516 else if( !strncmp( "id:", line, 3 ) )
518 char language[33]; /* Usually 2 or 3 letters, sometimes more.
519 Spec (or lack of) doesn't define any limit */
520 int i_track_id;
521 es_format_t fmt;
523 /* Lets start a new track */
524 if( sscanf( line, "id: %32[^ ,], index: %d",
525 language, &i_track_id ) != 2 )
527 if( sscanf( line, "id: , index: %d", &i_track_id ) != 1 )
529 msg_Warn( p_demux, "reading new track failed" );
530 continue;
532 language[0] = '\0';
535 p_sys->i_tracks++;
536 p_sys->track = xrealloc( p_sys->track,
537 sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
539 /* Init the track */
540 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
541 memset( current_tk, 0, sizeof( vobsub_track_t ) );
542 current_tk->i_current_subtitle = 0;
543 current_tk->i_subtitles = 0;
544 current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );
545 current_tk->i_track_id = i_track_id;
546 current_tk->i_delay = (vlc_tick_t)0;
548 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
549 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
550 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
551 fmt.psz_language = language;
552 if( p_sys->b_palette )
554 fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
555 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
558 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
559 msg_Dbg( p_demux, "New vobsub track detected: %i [%s]", i_track_id, language );
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;
568 int i_sign = 1;
569 vlc_tick_t i_start;
570 int64_t i_location = 0;
572 if( p_sys->i_tracks > 0 &&
573 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
574 &h, &count, &m, &s, &ms, &loc ) >= 5 )
576 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
577 subtitle_t *current_sub;
579 if( line[count-3] == '-' )
581 i_sign = -1;
582 h = -h;
584 i_start = vlc_tick_from_sec( h * 3600 + m * 60 + s ) + VLC_TICK_FROM_MS( ms );
585 i_location = loc;
587 current_tk->i_subtitles++;
588 current_tk->p_subtitles =
589 xrealloc( current_tk->p_subtitles,
590 sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
591 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
593 current_sub->i_start = i_start * i_sign;
594 current_sub->i_start += current_tk->i_delay;
595 current_sub->i_vobsub_location = i_location;
597 else
599 msg_Warn( p_demux, "reading timestamp failed" );
602 else if( !strncasecmp( line, "delay:", 6 ) )
605 * delay: [sign]hh:mm:ss:mss
607 int h, m, s, ms, count = 0;
608 int i_sign = 1;
609 vlc_tick_t i_gap = 0;
611 if( p_sys->i_tracks > 0 &&
612 sscanf( line, "%*celay: %d%n:%d:%d:%d",
613 &h, &count, &m, &s, &ms ) >= 4 )
615 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
616 if( line[count-3] == '-' )
618 i_sign = -1;
619 h = -h;
621 i_gap = vlc_tick_from_sec( h * 3600 + m * 60 + s ) + VLC_TICK_FROM_MS( ms );
623 current_tk->i_delay += i_gap * i_sign;
624 msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
625 i_sign, i_gap, current_tk->i_delay );
627 else
629 msg_Warn( p_demux, "reading delay failed" );
633 return( 0 );
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];
641 int i;
643 while( p + 6 < p_end )
645 int i_size = ps_pkt_size( p, p_end - p );
646 block_t *p_pkt;
647 int i_id;
648 int i_spu;
650 if( i_size <= 0 )
651 break;
653 if( i_size > p_end - p )
655 msg_Warn( p_demux, "broken PES size" );
656 break;
659 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
661 msg_Warn( p_demux, "invalid PES" );
662 break;
665 if( p[3] != 0xbd )
667 /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
668 p += i_size;
669 continue;
672 /* Create a block */
673 p_pkt = block_Alloc( i_size );
674 if( unlikely(p_pkt == NULL) )
675 break;
676 memcpy( p_pkt->p_buffer, p, i_size);
677 p += i_size;
679 i_id = ps_pkt_id( p_pkt );
680 if( (i_id&0xffe0) != 0xbd20 ||
681 ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, 1 ) )
683 block_Release( p_pkt );
684 continue;
686 i_spu = i_id&0x1f;
687 /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
689 for( i = 0; i < p_sys->i_tracks; i++ )
691 vobsub_track_t *p_tk = &p_sys->track[i];
693 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
694 p_pkt->i_length = 0;
696 if( p_tk->p_es && p_tk->i_track_id == i_spu )
698 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
699 p_bk->i_pts = VLC_TICK_INVALID; /*only first packet has a pts */
700 break;
703 if( i >= p_sys->i_tracks )
705 block_Release( p_pkt );
709 return VLC_SUCCESS;