1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: sub.c,v 1.19 2003/07/23 23:05:25 gbazin 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 *****************************************************************************/
30 #include <sys/types.h>
33 #include <vlc/input.h>
34 #include "vlc_video.h"
39 static int Open ( vlc_object_t *p_this );
41 static int sub_open ( subtitle_demux_t *p_sub,
42 input_thread_t *p_input,
44 mtime_t i_microsecperframe );
45 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate );
46 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date );
47 static void sub_close( subtitle_demux_t *p_sub );
49 static void sub_fix( subtitle_demux_t *p_sub );
51 static char *ppsz_sub_type[] = { "microdvd", "subrip", "ssa1", "ssa2-4", "vplayer", "sami", NULL };
54 /*****************************************************************************
56 *****************************************************************************/
57 #define SUB_DELAY_LONGTEXT \
58 "Delay subtitles (in 1/10s)"
59 #define SUB_FPS_LONGTEXT \
60 "Override frames per second. " \
61 "It will only work with MicroDVD subtitles."
62 #define SUB_TYPE_LONGTEXT \
63 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
64 "\"sami\" (nothing for autodetection, it should always work)."
67 set_description( _("Text subtitles demux") );
68 set_capability( "subtitle demux", 12 );
69 add_category_hint( "Subtitles", NULL, VLC_TRUE );
70 add_file( "sub-file", NULL, NULL,
71 "Subtitles file name", "Subtitles file name", VLC_TRUE );
72 add_float( "sub-fps", 0.0, NULL,
74 SUB_FPS_LONGTEXT, VLC_TRUE );
75 add_integer( "sub-delay", 0, NULL,
76 "Delay subtitles (in 1/10s)",
77 SUB_DELAY_LONGTEXT, VLC_TRUE );
78 add_string_from_list( "sub-type", NULL, ppsz_sub_type, NULL,
80 SUB_TYPE_LONGTEXT, VLC_TRUE );
81 set_callbacks( Open, NULL );
84 /*****************************************************************************
86 *****************************************************************************/
87 static int Open ( vlc_object_t *p_this )
89 subtitle_demux_t *p_sub = (subtitle_demux_t*)p_this;
91 p_sub->pf_open = sub_open;
92 p_sub->pf_demux = sub_demux;
93 p_sub->pf_seek = sub_seek;
94 p_sub->pf_close = sub_close;
96 /* Initialize the variables */
97 var_Create( p_this, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
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 )
243 int (*pf_read_subtitle)( text_t *, subtitle_t *, mtime_t ) = NULL;
245 p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
247 p_sub->i_subtitles = 0;
248 p_sub->subtitle = NULL;
249 p_sub->p_input = p_input;
251 if( !psz_name || !*psz_name)
253 var_Get( p_sub, "sub-file", &val );
254 if( !val.psz_string || !*val.psz_string )
256 if( val.psz_string ) free( val.psz_string );
257 msg_Err( p_sub, "cannot open `%s' subtitle fileeee", val.psz_string );
260 psz_name = val.psz_string;
264 psz_name = strdup( psz_name );
267 /* *** load the file *** */
268 if( text_load( &txt, psz_name ) )
270 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
274 msg_Dbg( p_sub, "opened `%s'", psz_name );
277 var_Get( p_sub, "sub-fps", &val );
278 if( val.i_int >= 1.0 )
280 var_Get( p_sub, "sub-fps", &val );
281 i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
283 else if( i_microsecperframe <= 0 )
285 i_microsecperframe = 40000; /* default: 25fps */
288 var_Get( p_sub, "sub-type", &val);
289 if( val.psz_string && *val.psz_string )
295 if( sub_read_subtitle_function[i].psz_type_name == NULL )
297 i_sub_type = SUB_TYPE_UNKNOWN;
300 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
303 i_sub_type = sub_read_subtitle_function[i].i_type;
310 i_sub_type = SUB_TYPE_UNKNOWN;
312 FREE( val.psz_string );
314 /* *** Now try to autodetect subtitle format *** */
315 if( i_sub_type == SUB_TYPE_UNKNOWN )
320 msg_Dbg( p_input, "trying to autodetect file format" );
321 for( i_try = 0; i_try < MAX_TRY; i_try++ )
325 if( ( s = text_get_line( &txt ) ) == NULL )
330 if( strstr( s, "<SAMI>" ) )
332 i_sub_type = SUB_TYPE_SAMI;
335 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
336 sscanf( s, "{%d}{}", &i_dummy ) == 1)
338 i_sub_type = SUB_TYPE_MICRODVD;
342 "%d:%d:%d,%d --> %d:%d:%d,%d",
343 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
344 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
346 i_sub_type = SUB_TYPE_SUBRIP;
350 "!: This is a Sub Station Alpha v%d.x script.",
355 i_sub_type = SUB_TYPE_SSA1;
359 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
363 else if( strstr( s, "This is a Sub Station Alpha v4 script" ) )
365 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
367 else if( !strncmp( s, "Dialogue: Marked", 16 ) )
369 i_sub_type = SUB_TYPE_SSA2_4; // could be wrong
372 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
373 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
375 i_sub_type = SUB_TYPE_VPLAYER;
383 /* *** Load this file in memory *** */
386 if( sub_read_subtitle_function[i].i_type == SUB_TYPE_UNKNOWN )
388 msg_Dbg( p_input, "unknown subtitle file" );
393 if( sub_read_subtitle_function[i].i_type == i_sub_type )
395 msg_Dbg( p_input, "detected %s format",
396 sub_read_subtitle_function[i].psz_name );
397 pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle;
404 if( p_sub->i_subtitles >= i_max )
407 if( p_sub->subtitle )
409 p_sub->subtitle = realloc( p_sub->subtitle,
410 sizeof( subtitle_t ) * i_max );
414 p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max );
417 if( pf_read_subtitle( &txt,
418 p_sub->subtitle + p_sub->i_subtitles,
419 i_microsecperframe ) < 0 )
423 p_sub->i_subtitles++;
425 msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
427 /* *** Close the file *** */
430 /* *** fix subtitle (order and time) *** */
431 p_sub->i_subtitle = 0; // will be modified by sub_fix
434 /* *** add subtitle ES *** */
435 vlc_mutex_lock( &p_input->stream.stream_lock );
436 p_sub->p_es = input_AddES( p_input, p_input->stream.p_selected_program,
439 vlc_mutex_unlock( &p_input->stream.stream_lock );
441 p_sub->p_es->i_stream_id = 0xff; // FIXME
442 p_sub->p_es->i_fourcc = VLC_FOURCC( 's','u','b','t' );
444 p_sub->i_previously_selected = 0;
448 /*****************************************************************************
449 * sub_demux: Send subtitle to decoder until i_maxdate
450 *****************************************************************************/
451 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
453 if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
455 p_sub->i_previously_selected = 1;
456 p_sub->pf_seek( p_sub, i_maxdate );
459 else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
461 p_sub->i_previously_selected = 0;
465 while( p_sub->i_subtitle < p_sub->i_subtitles &&
466 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
469 data_packet_t *p_data;
473 i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
481 if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
487 if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
490 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
494 p_data->p_payload_end = p_data->p_payload_start + i_len;
497 input_ClockGetTS( p_sub->p_input,
498 p_sub->p_input->stream.p_selected_program,
499 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
500 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
503 * i_dts means end of display...
506 input_ClockGetTS( p_sub->p_input,
507 p_sub->p_input->stream.p_selected_program,
508 p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
515 p_pes->i_nb_data = 1;
517 p_pes->p_last = p_data;
518 p_pes->i_pes_size = i_len;
520 memcpy( p_data->p_payload_start,
521 p_sub->subtitle[p_sub->i_subtitle].psz_text,
523 if( p_sub->p_es->p_decoder_fifo && p_pes->i_pts > 0 )
526 input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
530 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
538 /*****************************************************************************
539 * sub_seek: Seek to i_date
540 *****************************************************************************/
541 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
543 /* should be fast enough... */
544 p_sub->i_subtitle = 0;
545 while( p_sub->i_subtitle < p_sub->i_subtitles &&
546 p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
554 /*****************************************************************************
555 * sub_close: Close subtitle demux
556 *****************************************************************************/
557 static void sub_close( subtitle_demux_t *p_sub )
559 if( p_sub->subtitle )
562 for( i = 0; i < p_sub->i_subtitles; i++ )
564 if( p_sub->subtitle[i].psz_text )
566 free( p_sub->subtitle[i].psz_text );
569 free( p_sub->subtitle );
572 /*****************************************************************************
574 * sub_fix: fix time stamp and order of subtitle
575 *****************************************************************************/
576 static void sub_fix( subtitle_demux_t *p_sub )
584 /* *** fix order (to be sure...) *** */
585 /* We suppose that there are near in order and this durty bubble sort
586 * wont take too much time
591 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
593 if( p_sub->subtitle[i_index].i_start <
594 p_sub->subtitle[i_index - 1].i_start )
598 p_sub->subtitle + i_index - 1,
599 sizeof( subtitle_t ) );
600 memcpy( p_sub->subtitle + i_index - 1,
601 p_sub->subtitle + i_index,
602 sizeof( subtitle_t ) );
603 memcpy( p_sub->subtitle + i_index,
605 sizeof( subtitle_t ) );
611 /* *** and at the end add delay *** */
612 var_Get( p_sub, "sub-delay", &val );
613 i_delay = (mtime_t) val.i_int * 100000;
616 for( i = 0; i < p_sub->i_subtitles; i++ )
618 p_sub->subtitle[i].i_start += i_delay;
619 p_sub->subtitle[i].i_stop += i_delay;
620 if( p_sub->subtitle[i].i_start < 0 )
622 p_sub->i_subtitle = i + 1;
630 /*****************************************************************************
631 * Specific Subtitle function
632 *****************************************************************************/
633 static int sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
637 * {n1}{n2}Line1|Line2|Line3....
638 * where n1 and n2 are the video frame number...
643 char buffer_text[MAX_LINE + 1];
650 if( ( s = text_get_line( txt ) ) == NULL )
652 return( VLC_EGENERIC );
657 memset( buffer_text, '\0', MAX_LINE );
658 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
659 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
664 /* replace | by \n */
665 for( i = 0; i < strlen( buffer_text ); i++ )
667 if( buffer_text[i] == '|' )
669 buffer_text[i] = '\n';
672 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
673 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
674 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
678 static int sub_SubRipRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
682 * h1:m1:s1,d1 --> h2:m2:s2,d2
690 char buffer_text[ 10 * MAX_LINE];
697 int h1, m1, s1, d1, h2, m2, s2, d2;
698 if( ( s = text_get_line( txt ) ) == NULL )
700 return( VLC_EGENERIC );
703 "%d:%d:%d,%d --> %d:%d:%d,%d",
705 &h2, &m2, &s2, &d2 ) == 8 )
707 i_start = ( (mtime_t)h1 * 3600*1000 +
708 (mtime_t)m1 * 60*1000 +
710 (mtime_t)d1 ) * 1000;
712 i_stop = ( (mtime_t)h2 * 3600*1000 +
713 (mtime_t)m2 * 60*1000 +
715 (mtime_t)d2 ) * 1000;
717 /* Now read text until an empty line */
718 for( i_buffer_text = 0;; )
721 if( ( s = text_get_line( txt ) ) == NULL )
723 return( VLC_EGENERIC );
729 // empty line -> end of this subtitle
730 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
731 p_subtitle->i_start = i_start;
732 p_subtitle->i_stop = i_stop;
733 p_subtitle->psz_text = strdup( buffer_text );
738 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
740 memcpy( buffer_text + i_buffer_text,
743 i_buffer_text += i_len;
745 buffer_text[i_buffer_text] = '\n';
755 static int sub_SSARead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe, int i_comma_count )
757 char buffer_text[ 10 * MAX_LINE];
767 int h1, m1, s1, c1, h2, m2, s2, c2;
770 if( ( s = text_get_line( txt ) ) == NULL )
772 return( VLC_EGENERIC );
775 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
779 buffer_text ) == 10 )
781 i_start = ( (mtime_t)h1 * 3600*1000 +
782 (mtime_t)m1 * 60*1000 +
784 (mtime_t)c1 * 10 ) * 1000;
786 i_stop = ( (mtime_t)h2 * 3600*1000 +
787 (mtime_t)m2 * 60*1000 +
789 (mtime_t)c2 * 10 ) * 1000;
791 p_buffer_text = buffer_text;
793 while( i_comma < i_comma_count &&
794 *p_buffer_text != '\0' )
796 if( *p_buffer_text == ',' )
802 p_subtitle->psz_text = malloc( strlen( p_buffer_text ) + 1);
804 while( *p_buffer_text )
806 if( *p_buffer_text == '\\' && ( *p_buffer_text =='n' || *p_buffer_text =='N' ) )
808 p_subtitle->psz_text[i_text] = '\n';
812 else if( *p_buffer_text == '{' && *p_buffer_text == '\\')
814 while( *p_buffer_text && *p_buffer_text != '}' )
821 p_subtitle->psz_text[i_text] = *p_buffer_text;
826 p_subtitle->psz_text[i_text] = '\0';
827 p_subtitle->i_start = i_start;
828 p_subtitle->i_stop = i_stop;
834 static int sub_SSA1Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
836 return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 8 ) );
838 static int sub_SSA2_4Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
840 return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 9 ) );
843 static int sub_Vplayer( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
847 * h:m:s:Line1|Line2|Line3....
849 * h:m:s Line1|Line2|Line3....
850 * where n1 and n2 are the video frame number...
854 char buffer_text[MAX_LINE + 1];
863 if( ( p = text_get_line( txt ) ) == NULL )
865 return( VLC_EGENERIC );
870 memset( buffer_text, '\0', MAX_LINE );
871 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
873 i_start = ( (mtime_t)h * 3600*1000 +
874 (mtime_t)m * 60*1000 +
875 (mtime_t)s * 1000 ) * 1000;
880 /* replace | by \n */
881 for( i = 0; i < strlen( buffer_text ); i++ )
883 if( buffer_text[i] == '|' )
885 buffer_text[i] = '\n';
888 p_subtitle->i_start = i_start;
890 p_subtitle->i_stop = 0;
891 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
895 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
899 if( strstr( psz_start, psz_str ) )
901 char *s = strstr( psz_start, psz_str );
903 s += strlen( psz_str );
911 if( ( p = text_get_line( txt ) ) == NULL )
915 if( strstr( p, psz_str ) )
917 char *s = strstr( p, psz_str );
919 s += strlen( psz_str );
926 static int sub_Sami( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
932 char buffer_text[10*MAX_LINE + 1];
934 if( i_text < 10*MAX_LINE ) \
936 buffer_text[i_text++] = c; \
937 buffer_text[i_text] = '\0'; \
940 /* search "Start=" */
941 if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
946 /* get start value */
947 i_start = strtol( p, &p, 0 );
950 if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
955 if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
961 buffer_text[0] = '\0';
962 /* now get all txt until a "Start=" line */
969 if( !strncmp( p, "<br", 3 ) || !strncmp( p, "<BR", 3 ) )
973 else if( strstr( p, "Start=" ) )
975 text_previous_line( txt );
978 p = sub_SamiSearch( txt, p, ">" );
980 else if( !strncmp( p, " ", 6 ) )
985 else if( *p == '\t' )
998 p = text_get_line( txt );
1007 p_subtitle->i_start = i_start * 1000;
1008 p_subtitle->i_stop = 0;
1009 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1011 return( VLC_SUCCESS );