1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2003 VideoLAN
5 * $Id: sub.c,v 1.28 2003/10/11 22:40:04 hartman Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <sys/types.h>
32 #include <vlc/input.h>
33 #include "vlc_video.h"
37 #if (!defined( WIN32 ) || defined(__MINGW32__))
41 static int Open ( vlc_object_t *p_this );
43 static int sub_open ( subtitle_demux_t *p_sub,
44 input_thread_t *p_input,
46 mtime_t i_microsecperframe,
48 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate );
49 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date );
50 static void sub_close( subtitle_demux_t *p_sub );
52 static void sub_fix( subtitle_demux_t *p_sub );
54 static char *ppsz_sub_type[] = { "microdvd", "subrip", "ssa1", "ssa2-4", "vplayer", "sami", NULL };
57 /*****************************************************************************
59 *****************************************************************************/
60 #define SUB_DELAY_LONGTEXT \
61 "Delay subtitles (in 1/10s)"
62 #define SUB_FPS_LONGTEXT \
63 "Override frames per second. " \
64 "It will only work with MicroDVD subtitles."
65 #define SUB_TYPE_LONGTEXT \
66 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
67 "\"sami\" (nothing for autodetection, it should always work)."
70 set_description( _("Text subtitles demux") );
71 set_capability( "subtitle demux", 12 );
72 add_category_hint( "Subtitles", NULL, VLC_TRUE );
73 add_float( "sub-fps", 0.0, NULL,
75 SUB_FPS_LONGTEXT, VLC_TRUE );
76 add_integer( "sub-delay", 0, NULL,
77 "Delay subtitles (in 1/10s)",
78 SUB_DELAY_LONGTEXT, VLC_TRUE );
79 add_string_from_list( "sub-type", NULL, ppsz_sub_type, NULL,
81 SUB_TYPE_LONGTEXT, VLC_TRUE );
82 set_callbacks( Open, NULL );
85 /*****************************************************************************
87 *****************************************************************************/
88 static int Open ( vlc_object_t *p_this )
90 subtitle_demux_t *p_sub = (subtitle_demux_t*)p_this;
92 p_sub->pf_open = sub_open;
93 p_sub->pf_demux = sub_demux;
94 p_sub->pf_seek = sub_seek;
95 p_sub->pf_close = sub_close;
97 /* Initialize the variables */
98 var_Create( p_this, "sub-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
99 var_Create( p_this, "sub-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
100 var_Create( p_this, "sub-type", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
105 #define MAX_LINE 2048
107 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
116 static int text_load( text_t *txt, char *psz_name )
123 txt->i_line_count = 0;
125 txt->line = calloc( i_line_max, sizeof( char * ) );
128 if( !( f = fopen( psz_name, "rb" ) ) )
133 /* load the complete file */
139 if( fgets( buffer, 8096, f ) <= 0)
143 while( ( p = strchr( buffer, '\r' ) ) )
147 while( ( p = strchr( buffer, '\n' ) ) )
152 txt->line[txt->i_line_count++] = strdup( buffer );
154 if( txt->i_line_count >= i_line_max )
157 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
163 if( txt->i_line_count <= 0 )
166 return( VLC_EGENERIC );
169 return( VLC_SUCCESS );
171 static void text_unload( text_t *txt )
175 for( i = 0; i < txt->i_line_count; i++ )
177 FREE( txt->line[i] );
181 txt->i_line_count = 0;
184 static char *text_get_line( text_t *txt )
186 if( txt->i_line >= txt->i_line_count )
191 return( txt->line[txt->i_line++] );
193 static void text_previous_line( text_t *txt )
195 if( txt->i_line > 0 )
200 static void text_rewind( text_t *txt )
205 static int sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
206 static int sub_SubRipRead ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
207 static int sub_SSA1Read ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
208 static int sub_SSA2_4Read ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
209 static int sub_Vplayer ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
210 static int sub_Sami ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
217 int (*pf_read_subtitle) ( text_t *, subtitle_t*, mtime_t );
218 } sub_read_subtitle_function [] =
220 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", sub_MicroDvdRead },
221 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", sub_SubRipRead },
222 { "ssa1", SUB_TYPE_SSA1, "SSA-1", sub_SSA1Read },
223 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",sub_SSA2_4Read },
224 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", sub_Vplayer },
225 { "sami", SUB_TYPE_SAMI, "SAMI", sub_Sami },
226 { NULL, SUB_TYPE_UNKNOWN, "Unknow", NULL }
229 /*****************************************************************************
230 * sub_open: Open a subtitle file and add subtitle ES
231 *****************************************************************************/
232 static int sub_open ( subtitle_demux_t *p_sub,
233 input_thread_t *p_input,
235 mtime_t i_microsecperframe,
244 int (*pf_read_subtitle)( text_t *, subtitle_t *, mtime_t ) = NULL;
246 p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
248 p_sub->i_subtitles = 0;
249 p_sub->subtitle = NULL;
250 p_sub->p_input = p_input;
252 if( !psz_name || !*psz_name )
254 msg_Err( p_sub, "no subtitle file specified" );
255 if( psz_name ) free( psz_name );
259 /* *** load the file *** */
260 if( text_load( &txt, psz_name ) )
262 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
266 msg_Dbg( p_sub, "opened `%s'", psz_name );
269 var_Get( p_sub, "sub-fps", &val );
270 if( val.i_int >= 1.0 )
272 var_Get( p_sub, "sub-fps", &val );
273 i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
275 else if( i_microsecperframe <= 0 )
277 i_microsecperframe = 40000; /* default: 25fps */
280 var_Get( p_sub, "sub-type", &val);
281 if( val.psz_string && *val.psz_string )
287 if( sub_read_subtitle_function[i].psz_type_name == NULL )
289 i_sub_type = SUB_TYPE_UNKNOWN;
292 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
295 i_sub_type = sub_read_subtitle_function[i].i_type;
302 i_sub_type = SUB_TYPE_UNKNOWN;
304 FREE( val.psz_string );
306 /* *** Now try to autodetect subtitle format *** */
307 if( i_sub_type == SUB_TYPE_UNKNOWN )
312 msg_Dbg( p_input, "trying to autodetect file format" );
313 for( i_try = 0; i_try < MAX_TRY; i_try++ )
317 if( ( s = text_get_line( &txt ) ) == NULL )
322 if( strstr( s, "<SAMI>" ) )
324 i_sub_type = SUB_TYPE_SAMI;
327 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
328 sscanf( s, "{%d}{}", &i_dummy ) == 1)
330 i_sub_type = SUB_TYPE_MICRODVD;
334 "%d:%d:%d,%d --> %d:%d:%d,%d",
335 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
336 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
338 i_sub_type = SUB_TYPE_SUBRIP;
342 "!: This is a Sub Station Alpha v%d.x script.",
347 i_sub_type = SUB_TYPE_SSA1;
351 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
355 else if( strstr( s, "This is a Sub Station Alpha v4 script" ) )
357 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
359 else if( !strncmp( s, "Dialogue: Marked", 16 ) )
361 i_sub_type = SUB_TYPE_SSA2_4; // could be wrong
364 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
365 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
367 i_sub_type = SUB_TYPE_VPLAYER;
375 /* *** Load this file in memory *** */
378 if( sub_read_subtitle_function[i].i_type == SUB_TYPE_UNKNOWN )
380 msg_Dbg( p_input, "unknown subtitle file" );
385 if( sub_read_subtitle_function[i].i_type == i_sub_type )
387 msg_Dbg( p_input, "detected %s format",
388 sub_read_subtitle_function[i].psz_name );
389 pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle;
396 if( p_sub->i_subtitles >= i_max )
399 if( p_sub->subtitle )
401 p_sub->subtitle = realloc( p_sub->subtitle,
402 sizeof( subtitle_t ) * i_max );
406 p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max );
409 if( pf_read_subtitle( &txt,
410 p_sub->subtitle + p_sub->i_subtitles,
411 i_microsecperframe ) < 0 )
415 p_sub->i_subtitles++;
417 msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
419 /* *** Close the file *** */
422 /* *** fix subtitle (order and time) *** */
423 p_sub->i_subtitle = 0; // will be modified by sub_fix
426 /* *** add subtitle ES *** */
427 vlc_mutex_lock( &p_input->stream.stream_lock );
428 p_sub->p_es = input_AddES( p_input, p_input->stream.p_selected_program,
429 0xff - i_track_id, /* FIXME */
431 vlc_mutex_unlock( &p_input->stream.stream_lock );
433 p_sub->p_es->i_stream_id = 0xff - i_track_id; /* FIXME */
434 p_sub->p_es->i_fourcc = VLC_FOURCC( 's','u','b','t' );
436 p_sub->i_previously_selected = 0;
440 /*****************************************************************************
441 * sub_demux: Send subtitle to decoder until i_maxdate
442 *****************************************************************************/
443 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
445 if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
447 p_sub->i_previously_selected = 1;
448 p_sub->pf_seek( p_sub, i_maxdate );
451 else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
453 p_sub->i_previously_selected = 0;
457 while( p_sub->i_subtitle < p_sub->i_subtitles &&
458 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
461 data_packet_t *p_data;
465 i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
473 if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
479 if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
482 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
486 p_data->p_payload_end = p_data->p_payload_start + i_len;
489 input_ClockGetTS( p_sub->p_input,
490 p_sub->p_input->stream.p_selected_program,
491 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
492 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
495 * i_dts means end of display...
498 input_ClockGetTS( p_sub->p_input,
499 p_sub->p_input->stream.p_selected_program,
500 p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
507 p_pes->i_nb_data = 1;
509 p_pes->p_last = p_data;
510 p_pes->i_pes_size = i_len;
512 memcpy( p_data->p_payload_start,
513 p_sub->subtitle[p_sub->i_subtitle].psz_text,
515 if( p_sub->p_es->p_decoder_fifo && p_pes->i_pts > 0 )
518 input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
522 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
530 /*****************************************************************************
531 * sub_seek: Seek to i_date
532 *****************************************************************************/
533 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
535 /* should be fast enough... */
536 p_sub->i_subtitle = 0;
537 while( p_sub->i_subtitle < p_sub->i_subtitles &&
538 p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
546 /*****************************************************************************
547 * sub_close: Close subtitle demux
548 *****************************************************************************/
549 static void sub_close( subtitle_demux_t *p_sub )
551 if( p_sub->subtitle )
554 for( i = 0; i < p_sub->i_subtitles; i++ )
556 if( p_sub->subtitle[i].psz_text )
558 free( p_sub->subtitle[i].psz_text );
561 free( p_sub->subtitle );
564 /*****************************************************************************
566 * sub_fix: fix time stamp and order of subtitle
567 *****************************************************************************/
568 static void sub_fix( subtitle_demux_t *p_sub )
576 /* *** fix order (to be sure...) *** */
577 /* We suppose that there are near in order and this durty bubble sort
578 * wont take too much time
583 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
585 if( p_sub->subtitle[i_index].i_start <
586 p_sub->subtitle[i_index - 1].i_start )
590 p_sub->subtitle + i_index - 1,
591 sizeof( subtitle_t ) );
592 memcpy( p_sub->subtitle + i_index - 1,
593 p_sub->subtitle + i_index,
594 sizeof( subtitle_t ) );
595 memcpy( p_sub->subtitle + i_index,
597 sizeof( subtitle_t ) );
603 /* *** and at the end add delay *** */
604 var_Get( p_sub, "sub-delay", &val );
605 i_delay = (mtime_t) val.i_int * 100000;
608 for( i = 0; i < p_sub->i_subtitles; i++ )
610 p_sub->subtitle[i].i_start += i_delay;
611 p_sub->subtitle[i].i_stop += i_delay;
612 if( p_sub->subtitle[i].i_start < 0 )
614 p_sub->i_subtitle = i + 1;
622 /*****************************************************************************
623 * Specific Subtitle function
624 *****************************************************************************/
625 static int sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
629 * {n1}{n2}Line1|Line2|Line3....
630 * where n1 and n2 are the video frame number...
635 char buffer_text[MAX_LINE + 1];
642 if( ( s = text_get_line( txt ) ) == NULL )
644 return( VLC_EGENERIC );
649 memset( buffer_text, '\0', MAX_LINE );
650 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
651 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
656 /* replace | by \n */
657 for( i = 0; i < strlen( buffer_text ); i++ )
659 if( buffer_text[i] == '|' )
661 buffer_text[i] = '\n';
664 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
665 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
666 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
670 static int sub_SubRipRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
674 * h1:m1:s1,d1 --> h2:m2:s2,d2
682 char buffer_text[ 10 * MAX_LINE];
689 int h1, m1, s1, d1, h2, m2, s2, d2;
690 if( ( s = text_get_line( txt ) ) == NULL )
692 return( VLC_EGENERIC );
695 "%d:%d:%d,%d --> %d:%d:%d,%d",
697 &h2, &m2, &s2, &d2 ) == 8 )
699 i_start = ( (mtime_t)h1 * 3600*1000 +
700 (mtime_t)m1 * 60*1000 +
702 (mtime_t)d1 ) * 1000;
704 i_stop = ( (mtime_t)h2 * 3600*1000 +
705 (mtime_t)m2 * 60*1000 +
707 (mtime_t)d2 ) * 1000;
709 /* Now read text until an empty line */
710 for( i_buffer_text = 0;; )
713 if( ( s = text_get_line( txt ) ) == NULL )
715 return( VLC_EGENERIC );
721 // empty line -> end of this subtitle
722 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
723 p_subtitle->i_start = i_start;
724 p_subtitle->i_stop = i_stop;
725 p_subtitle->psz_text = strdup( buffer_text );
730 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
732 memcpy( buffer_text + i_buffer_text,
735 i_buffer_text += i_len;
737 buffer_text[i_buffer_text] = '\n';
747 static int sub_SSARead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe, int i_comma_count )
749 char buffer_text[ 10 * MAX_LINE];
759 int h1, m1, s1, c1, h2, m2, s2, c2;
762 if( ( s = text_get_line( txt ) ) == NULL )
764 return( VLC_EGENERIC );
767 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
771 buffer_text ) == 10 )
773 i_start = ( (mtime_t)h1 * 3600*1000 +
774 (mtime_t)m1 * 60*1000 +
776 (mtime_t)c1 * 10 ) * 1000;
778 i_stop = ( (mtime_t)h2 * 3600*1000 +
779 (mtime_t)m2 * 60*1000 +
781 (mtime_t)c2 * 10 ) * 1000;
783 p_buffer_text = buffer_text;
785 while( i_comma < i_comma_count &&
786 *p_buffer_text != '\0' )
788 if( *p_buffer_text == ',' )
794 p_subtitle->psz_text = malloc( strlen( p_buffer_text ) + 1);
796 while( *p_buffer_text )
798 if( *p_buffer_text == '\\' && ( *p_buffer_text =='n' || *p_buffer_text =='N' ) )
800 p_subtitle->psz_text[i_text] = '\n';
804 else if( *p_buffer_text == '{' && *p_buffer_text == '\\')
806 while( *p_buffer_text && *p_buffer_text != '}' )
813 p_subtitle->psz_text[i_text] = *p_buffer_text;
818 p_subtitle->psz_text[i_text] = '\0';
819 p_subtitle->i_start = i_start;
820 p_subtitle->i_stop = i_stop;
826 static int sub_SSA1Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
828 return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 8 ) );
830 static int sub_SSA2_4Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
832 return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 9 ) );
835 static int sub_Vplayer( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
839 * h:m:s:Line1|Line2|Line3....
841 * h:m:s Line1|Line2|Line3....
842 * where n1 and n2 are the video frame number...
846 char buffer_text[MAX_LINE + 1];
855 if( ( p = text_get_line( txt ) ) == NULL )
857 return( VLC_EGENERIC );
862 memset( buffer_text, '\0', MAX_LINE );
863 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
865 i_start = ( (mtime_t)h * 3600*1000 +
866 (mtime_t)m * 60*1000 +
867 (mtime_t)s * 1000 ) * 1000;
872 /* replace | by \n */
873 for( i = 0; i < strlen( buffer_text ); i++ )
875 if( buffer_text[i] == '|' )
877 buffer_text[i] = '\n';
880 p_subtitle->i_start = i_start;
882 p_subtitle->i_stop = 0;
883 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
887 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
891 if( strstr( psz_start, psz_str ) )
893 char *s = strstr( psz_start, psz_str );
895 s += strlen( psz_str );
903 if( ( p = text_get_line( txt ) ) == NULL )
907 if( strstr( p, psz_str ) )
909 char *s = strstr( p, psz_str );
911 s += strlen( psz_str );
918 static int sub_Sami( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
924 char buffer_text[10*MAX_LINE + 1];
926 if( i_text < 10*MAX_LINE ) \
928 buffer_text[i_text++] = c; \
929 buffer_text[i_text] = '\0'; \
932 /* search "Start=" */
933 if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
938 /* get start value */
939 i_start = strtol( p, &p, 0 );
942 if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
947 if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
953 buffer_text[0] = '\0';
954 /* now get all txt until a "Start=" line */
961 if( !strncmp( p, "<br", 3 ) || !strncmp( p, "<BR", 3 ) )
965 else if( strstr( p, "Start=" ) )
967 text_previous_line( txt );
970 p = sub_SamiSearch( txt, p, ">" );
972 else if( !strncmp( p, " ", 6 ) )
977 else if( *p == '\t' )
990 p = text_get_line( txt );
999 p_subtitle->i_start = i_start * 1000;
1000 p_subtitle->i_stop = 0;
1001 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1003 return( VLC_SUCCESS );