caopengllayer: correct vertical alignment
[vlc.git] / modules / demux / vobsub.c
blob119233b26a5e75319a49fdf1f6fe1922adbb08f7
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 if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1 )
183 goto error;
185 i_len = strlen( psz_vobname );
186 if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
188 /* open file */
189 p_sys->p_vobsub_stream = vlc_stream_NewURL( p_demux, psz_vobname );
190 if( p_sys->p_vobsub_stream == NULL )
192 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
193 psz_vobname );
194 free( psz_vobname );
195 goto error;
197 free( psz_vobname );
199 p_demux->pf_demux = Demux;
200 p_demux->pf_control = Control;
202 return VLC_SUCCESS;
204 error:
205 /* Clean all subs from all tracks */
206 for( int i = 0; i < p_sys->i_tracks; i++ )
207 free( p_sys->track[i].p_subtitles );
208 free( p_sys->track );
209 free( p_sys );
211 return VLC_EGENERIC;
214 /*****************************************************************************
215 * Close: Close subtitle demux
216 *****************************************************************************/
217 static void Close( vlc_object_t *p_this )
219 demux_t *p_demux = (demux_t*)p_this;
220 demux_sys_t *p_sys = p_demux->p_sys;
222 if( p_sys->p_vobsub_stream )
223 vlc_stream_Delete( p_sys->p_vobsub_stream );
225 /* Clean all subs from all tracks */
226 for( int i = 0; i < p_sys->i_tracks; i++ )
227 free( p_sys->track[i].p_subtitles );
228 free( p_sys->track );
229 free( p_sys );
232 /*****************************************************************************
233 * Control:
234 *****************************************************************************/
235 static int Control( demux_t *p_demux, int i_query, va_list args )
237 demux_sys_t *p_sys = p_demux->p_sys;
238 int64_t *pi64, i64;
239 int i;
240 double *pf, f;
242 switch( i_query )
244 case DEMUX_CAN_SEEK:
245 *va_arg( args, bool * ) = true;
246 return VLC_SUCCESS;
248 case DEMUX_GET_LENGTH:
249 pi64 = va_arg( args, int64_t * );
250 *pi64 = (int64_t) p_sys->i_length;
251 return VLC_SUCCESS;
253 case DEMUX_GET_TIME:
254 pi64 = va_arg( args, int64_t * );
255 for( i = 0; i < p_sys->i_tracks; i++ )
257 bool b_selected;
258 /* Check the ES is selected */
259 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
260 p_sys->track[i].p_es, &b_selected );
261 if( b_selected ) break;
263 if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
265 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
266 return VLC_SUCCESS;
268 return VLC_EGENERIC;
270 case DEMUX_SET_TIME:
271 i64 = va_arg( args, int64_t );
272 for( i = 0; i < p_sys->i_tracks; i++ )
274 p_sys->track[i].i_current_subtitle = 0;
275 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
276 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
278 p_sys->track[i].i_current_subtitle++;
281 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
282 return VLC_EGENERIC;
284 return VLC_SUCCESS;
286 case DEMUX_GET_POSITION:
287 pf = va_arg( args, double * );
288 for( i = 0; i < p_sys->i_tracks; i++ )
290 bool b_selected;
291 /* Check the ES is selected */
292 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
293 p_sys->track[i].p_es, &b_selected );
294 if( b_selected ) break;
296 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
298 *pf = 1.0;
300 else if( p_sys->track[i].i_subtitles > 0 )
302 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
303 (double)p_sys->i_length;
305 else
307 *pf = 0.0;
309 return VLC_SUCCESS;
311 case DEMUX_SET_POSITION:
312 f = va_arg( args, double );
313 i64 = (int64_t) f * p_sys->i_length;
315 for( i = 0; i < p_sys->i_tracks; i++ )
317 p_sys->track[i].i_current_subtitle = 0;
318 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
319 p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
321 p_sys->track[i].i_current_subtitle++;
323 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
324 return VLC_EGENERIC;
326 return VLC_SUCCESS;
328 case DEMUX_SET_NEXT_DEMUX_TIME:
329 p_sys->i_next_demux_date = va_arg( args, int64_t );
330 return VLC_SUCCESS;
332 case DEMUX_GET_PTS_DELAY:
333 case DEMUX_GET_FPS:
334 case DEMUX_GET_META:
335 case DEMUX_GET_TITLE_INFO:
336 case DEMUX_HAS_UNSUPPORTED_META:
337 case DEMUX_GET_ATTACHMENTS:
338 case DEMUX_CAN_RECORD:
339 return VLC_EGENERIC;
341 default:
342 msg_Warn( p_demux, "unknown query in subtitle control" );
343 return VLC_EGENERIC;
347 /*****************************************************************************
348 * Demux: Send subtitle to decoder
349 *****************************************************************************/
350 static int Demux( demux_t *p_demux )
352 demux_sys_t *p_sys = p_demux->p_sys;
353 int64_t i_maxdate;
354 int i_read;
356 for( int i = 0; i < p_sys->i_tracks; i++ )
358 #define tk p_sys->track[i]
359 if( tk.i_current_subtitle >= tk.i_subtitles )
360 continue;
362 i_maxdate = p_sys->i_next_demux_date;
363 if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
365 /* Should not happen */
366 i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
369 while( tk.i_current_subtitle < tk.i_subtitles &&
370 tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
372 int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
373 block_t *p_block;
374 int i_size = 0;
376 /* first compute SPU size */
377 if( tk.i_current_subtitle + 1 < tk.i_subtitles )
379 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
381 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
383 /* Seek at the right place */
384 if( vlc_stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
386 msg_Warn( p_demux,
387 "cannot seek in the VobSub to the correct time %d", i_pos );
388 tk.i_current_subtitle++;
389 continue;
392 /* allocate a packet */
393 if( ( p_block = block_Alloc( i_size ) ) == NULL )
395 tk.i_current_subtitle++;
396 continue;
399 /* read data */
400 i_read = vlc_stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
401 if( i_read <= 6 )
403 block_Release( p_block );
404 tk.i_current_subtitle++;
405 continue;
407 p_block->i_buffer = i_read;
409 /* pts */
410 p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
412 /* demux this block */
413 DemuxVobSub( p_demux, p_block );
415 block_Release( p_block );
417 tk.i_current_subtitle++;
419 #undef tk
422 /* */
423 p_sys->i_next_demux_date = 0;
425 return 1;
428 static int TextLoad( text_t *txt, stream_t *s )
430 char **lines = NULL;
431 size_t n = 0;
433 /* load the complete file */
434 for( ;; )
436 char *psz = vlc_stream_ReadLine( s );
437 char **ppsz_new;
439 if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
441 free( psz );
442 break;
445 ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
446 if( ppsz_new == NULL )
448 free( psz );
449 break;
451 lines = ppsz_new;
452 lines[n++] = psz;
455 txt->i_line_count = n;
456 txt->i_line = 0;
457 txt->line = lines;
459 return VLC_SUCCESS;
462 static void TextUnload( text_t *txt )
464 for( int i = 0; i < txt->i_line_count; i++ )
465 free( txt->line[i] );
466 free( txt->line );
468 txt->i_line = 0;
469 txt->i_line_count = 0;
472 static char *TextGetLine( text_t *txt )
474 if( txt->i_line >= txt->i_line_count )
475 return( NULL );
477 return txt->line[txt->i_line++];
480 static int ParseVobSubIDX( demux_t *p_demux )
482 demux_sys_t *p_sys = p_demux->p_sys;
483 text_t *txt = &p_sys->txt;
484 char *line;
486 for( ;; )
488 if( ( line = TextGetLine( txt ) ) == NULL )
490 return( VLC_EGENERIC );
493 if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
495 continue;
497 else if( !strncmp( "size:", line, 5 ) )
499 /* Store the original size of the video */
500 if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
501 &p_sys->i_original_frame_height ) == VLC_SUCCESS )
503 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
505 else
507 msg_Warn( p_demux, "reading original frame size failed" );
510 else if( !strncmp( "palette:", line, 8 ) )
512 if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
514 p_sys->b_palette = true;
515 msg_Dbg( p_demux, "vobsub palette read" );
517 else
519 msg_Warn( p_demux, "reading original palette failed" );
522 else if( !strncmp( "id:", line, 3 ) )
524 char language[33]; /* Usually 2 or 3 letters, sometimes more.
525 Spec (or lack of) doesn't define any limit */
526 int i_track_id;
527 es_format_t fmt;
529 /* Lets start a new track */
530 if( sscanf( line, "id: %32[^ ,], index: %d",
531 language, &i_track_id ) != 2 )
533 if( sscanf( line, "id: , index: %d", &i_track_id ) != 1 )
535 msg_Warn( p_demux, "reading new track failed" );
536 continue;
538 language[0] = '\0';
541 p_sys->i_tracks++;
542 p_sys->track = xrealloc( p_sys->track,
543 sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
545 /* Init the track */
546 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
547 memset( current_tk, 0, sizeof( vobsub_track_t ) );
548 current_tk->i_current_subtitle = 0;
549 current_tk->i_subtitles = 0;
550 current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );
551 current_tk->i_track_id = i_track_id;
552 current_tk->i_delay = (int64_t)0;
554 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
555 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
556 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
557 fmt.psz_language = language;
558 if( p_sys->b_palette )
560 fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
561 memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
564 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
565 msg_Dbg( p_demux, "New vobsub track detected: %i [%s]", i_track_id, language );
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;
574 int i_sign = 1;
575 int64_t i_start, i_location = 0;
577 if( p_sys->i_tracks > 0 &&
578 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
579 &h, &count, &m, &s, &ms, &loc ) >= 5 )
581 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
582 subtitle_t *current_sub;
584 if( line[count-3] == '-' )
586 i_sign = -1;
587 h = -h;
589 i_start = (int64_t) ( h * 3600*1000 +
590 m * 60*1000 +
591 s * 1000 +
592 ms ) * 1000;
593 i_location = loc;
595 current_tk->i_subtitles++;
596 current_tk->p_subtitles =
597 xrealloc( current_tk->p_subtitles,
598 sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
599 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
601 current_sub->i_start = i_start * i_sign;
602 current_sub->i_start += current_tk->i_delay;
603 current_sub->i_vobsub_location = i_location;
605 else
607 msg_Warn( p_demux, "reading timestamp failed" );
610 else if( !strncasecmp( line, "delay:", 6 ) )
613 * delay: [sign]hh:mm:ss:mss
615 int h, m, s, ms, count = 0;
616 int i_sign = 1;
617 int64_t i_gap = 0;
619 if( p_sys->i_tracks > 0 &&
620 sscanf( line, "%*celay: %d%n:%d:%d:%d",
621 &h, &count, &m, &s, &ms ) >= 4 )
623 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
624 if( line[count-3] == '-' )
626 i_sign = -1;
627 h = -h;
629 i_gap = (int64_t) ( h * 3600*1000 +
630 m * 60*1000 +
631 s * 1000 +
632 ms ) * 1000;
634 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
635 msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
636 i_sign, i_gap, current_tk->i_delay );
638 else
640 msg_Warn( p_demux, "reading delay failed" );
644 return( 0 );
647 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
649 demux_sys_t *p_sys = p_demux->p_sys;
650 uint8_t *p = p_bk->p_buffer;
651 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
652 int i;
654 while( p + 6 < p_end )
656 int i_size = ps_pkt_size( p, p_end - p );
657 block_t *p_pkt;
658 int i_id;
659 int i_spu;
661 if( i_size <= 0 )
662 break;
664 if( i_size > p_end - p )
666 msg_Warn( p_demux, "broken PES size" );
667 break;
670 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
672 msg_Warn( p_demux, "invalid PES" );
673 break;
676 if( p[3] != 0xbd )
678 /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
679 p += i_size;
680 continue;
683 /* Create a block */
684 p_pkt = block_Alloc( i_size );
685 if( unlikely(p_pkt == NULL) )
686 break;
687 memcpy( p_pkt->p_buffer, p, i_size);
688 p += i_size;
690 i_id = ps_pkt_id( p_pkt );
691 if( (i_id&0xffe0) != 0xbd20 ||
692 ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, 1 ) )
694 block_Release( p_pkt );
695 continue;
697 i_spu = i_id&0x1f;
698 /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
700 for( i = 0; i < p_sys->i_tracks; i++ )
702 vobsub_track_t *p_tk = &p_sys->track[i];
704 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
705 p_pkt->i_length = 0;
707 if( p_tk->p_es && p_tk->i_track_id == i_spu )
709 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
710 p_bk->i_pts = VLC_TS_INVALID; /*only first packet has a pts */
711 break;
714 if( i >= p_sys->i_tracks )
716 block_Release( p_pkt );
720 return VLC_SUCCESS;