1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: sub.c,v 1.13 2003/05/05 22:23:37 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>
40 static int Open ( vlc_object_t *p_this );
42 static int sub_open ( subtitle_demux_t *p_sub,
43 input_thread_t *p_input,
45 mtime_t i_microsecperframe );
46 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate );
47 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date );
48 static void sub_close( subtitle_demux_t *p_sub );
50 static void sub_fix( subtitle_demux_t *p_sub );
52 static char *ppsz_sub_type[] = { "microdvd", "subrip", "ssa1", "ssa2-4", "vplayer", "sami", NULL };
55 /*****************************************************************************
57 *****************************************************************************/
59 #define SUB_FPS_LONGTEXT \
60 "Override frames per second. " \
61 "It will work only with MicroDVD"
62 #define SUB_TYPE_LONGTEXT \
63 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" \"sami\"" \
64 "(nothing for autodetection, it should always work)"
67 set_description( _("text subtitle demux") );
68 set_capability( "subtitle demux", 12 );
69 add_category_hint( "subtitle", NULL, VLC_TRUE );
70 add_file( "sub-file", NULL, NULL,
71 "subtitle file name", "subtitle file name", VLC_TRUE );
72 add_float( "sub-fps", 0.0, NULL,
73 "override frames per second",
74 SUB_FPS_LONGTEXT, VLC_TRUE );
75 add_integer( "sub-delay", 0, NULL,
76 "delay subtitles (in 1/10s)",
77 "delay subtitles (in 1/10s)", 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;
101 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
110 static int text_load( text_t *txt, char *psz_name )
117 txt->i_line_count = 0;
119 txt->line = calloc( i_line_max, sizeof( char * ) );
122 if( !( f = fopen( psz_name, "rb" ) ) )
127 /* load the complete file */
133 if( fgets( buffer, 8096, f ) <= 0)
137 while( ( p = strchr( buffer, '\r' ) ) )
141 while( ( p = strchr( buffer, '\n' ) ) )
146 txt->line[txt->i_line_count++] = strdup( buffer );
148 if( txt->i_line_count >= i_line_max )
151 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
157 if( txt->i_line_count <= 0 )
160 return( VLC_EGENERIC );
163 return( VLC_SUCCESS );
165 static void text_unload( text_t *txt )
169 for( i = 0; i < txt->i_line_count; i++ )
171 FREE( txt->line[i] );
175 txt->i_line_count = 0;
178 static char *text_get_line( text_t *txt )
180 if( txt->i_line >= txt->i_line_count )
185 return( txt->line[txt->i_line++] );
187 static void text_previous_line( text_t *txt )
189 if( txt->i_line > 0 )
194 static void text_rewind( text_t *txt )
199 static int sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
200 static int sub_SubRipRead ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
201 static int sub_SSA1Read ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
202 static int sub_SSA2_4Read ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
203 static int sub_Vplayer ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
204 static int sub_Sami ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
211 int (*pf_read_subtitle) ( text_t *, subtitle_t*, mtime_t );
212 } sub_read_subtitle_function [] =
214 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", sub_MicroDvdRead },
215 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", sub_SubRipRead },
216 { "ssa1", SUB_TYPE_SSA1, "SSA-1", sub_SSA1Read },
217 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",sub_SSA2_4Read },
218 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", sub_Vplayer },
219 { "sami", SUB_TYPE_SAMI, "SAMI", sub_Sami },
220 { NULL, SUB_TYPE_UNKNOWN, "Unknow", NULL }
223 /*****************************************************************************
224 * sub_open: Open a subtitle file and add subtitle ES
225 *****************************************************************************/
226 static int sub_open ( subtitle_demux_t *p_sub,
227 input_thread_t *p_input,
229 mtime_t i_microsecperframe )
237 int (*pf_read_subtitle)( text_t *, subtitle_t *, mtime_t ) = NULL;
239 p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
241 p_sub->i_subtitles = 0;
242 p_sub->subtitle = NULL;
243 p_sub->p_input = p_input;
245 if( !psz_name || !*psz_name)
247 psz_name = config_GetPsz( p_sub, "sub-file" );
248 if( !psz_name || !*psz_name )
255 psz_name = strdup( psz_name );
258 /* *** load the file *** */
259 if( text_load( &txt, psz_name ) )
261 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
265 msg_Dbg( p_sub, "opened `%s'", psz_name );
269 if( config_GetFloat( p_sub, "sub-fps" ) >= 1.0 )
271 i_microsecperframe = (mtime_t)( (float)1000000 /
272 config_GetFloat( p_sub, "sub-fps" ) );
274 else if( i_microsecperframe <= 0 )
276 i_microsecperframe = 40000; /* default: 25fps */
279 psz_file_type = config_GetPsz( p_sub, "sub-type" );
280 if( psz_file_type && *psz_file_type)
286 if( sub_read_subtitle_function[i].psz_type_name == NULL )
288 i_sub_type = SUB_TYPE_UNKNOWN;
291 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
294 i_sub_type = sub_read_subtitle_function[i].i_type;
301 i_sub_type = SUB_TYPE_UNKNOWN;
303 FREE( psz_file_type );
305 /* *** Now try to autodetect subtitle format *** */
306 if( i_sub_type == SUB_TYPE_UNKNOWN )
311 msg_Dbg( p_input, "trying to autodetect file format" );
312 for( i_try = 0; i_try < MAX_TRY; i_try++ )
316 if( ( s = text_get_line( &txt ) ) == NULL )
321 if( strstr( s, "<SAMI>" ) )
323 i_sub_type = SUB_TYPE_SAMI;
326 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
327 sscanf( s, "{%d}{}", &i_dummy ) == 1)
329 i_sub_type = SUB_TYPE_MICRODVD;
333 "%d:%d:%d,%d --> %d:%d:%d,%d",
334 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
335 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
337 i_sub_type = SUB_TYPE_SUBRIP;
341 "!: This is a Sub Station Alpha v%d.x script.",
346 i_sub_type = SUB_TYPE_SSA1;
350 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
354 else if( strstr( s, "This is a Sub Station Alpha v4 script" ) )
356 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
358 else if( !strncmp( s, "Dialogue: Marked", 16 ) )
360 i_sub_type = SUB_TYPE_SSA2_4; // could be wrong
363 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
364 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
366 i_sub_type = SUB_TYPE_VPLAYER;
374 /* *** Load this file in memory *** */
377 if( sub_read_subtitle_function[i].i_type == SUB_TYPE_UNKNOWN )
379 msg_Dbg( p_input, "unknown subtitile file" );
384 if( sub_read_subtitle_function[i].i_type == i_sub_type )
387 "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,
431 vlc_mutex_unlock( &p_input->stream.stream_lock );
433 p_sub->p_es->i_stream_id = 0xff; // 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 )
446 if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
448 p_sub->i_previously_selected = 1;
449 p_sub->pf_seek( p_sub, i_maxdate );
452 else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
454 p_sub->i_previously_selected = 0;
458 while( p_sub->i_subtitle < p_sub->i_subtitles &&
459 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
462 data_packet_t *p_data;
466 i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
474 if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
480 if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
483 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
487 p_data->p_payload_end = p_data->p_payload_start + i_len;
490 input_ClockGetTS( p_sub->p_input,
491 p_sub->p_input->stream.p_selected_program,
492 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
493 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
496 * i_dts means end of display...
499 input_ClockGetTS( p_sub->p_input,
500 p_sub->p_input->stream.p_selected_program,
501 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 )
575 /* *** fix order (to be sure...) *** */
576 /* We suppose that there are near in order and this durty bubble sort
577 * wont take too much time
582 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
584 if( p_sub->subtitle[i_index].i_start <
585 p_sub->subtitle[i_index - 1].i_start )
589 p_sub->subtitle + i_index - 1,
590 sizeof( subtitle_t ) );
591 memcpy( p_sub->subtitle + i_index - 1,
592 p_sub->subtitle + i_index,
593 sizeof( subtitle_t ) );
594 memcpy( p_sub->subtitle + i_index,
596 sizeof( subtitle_t ) );
602 /* *** and at the end add delay *** */
603 i_delay = (mtime_t)config_GetInt( p_sub, "sub-delay" ) * 100000;
606 for( i = 0; i < p_sub->i_subtitles; i++ )
608 p_sub->subtitle[i].i_start += i_delay;
609 p_sub->subtitle[i].i_stop += i_delay;
610 if( p_sub->subtitle[i].i_start < 0 )
612 p_sub->i_subtitle = i + 1;
620 /*****************************************************************************
621 * Specific Subtitle function
622 *****************************************************************************/
623 static int sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
627 * {n1}{n2}Line1|Line2|Line3....
628 * where n1 and n2 are the video frame number...
633 char buffer_text[MAX_LINE + 1];
640 if( ( s = text_get_line( txt ) ) == NULL )
642 return( VLC_EGENERIC );
647 memset( buffer_text, '\0', MAX_LINE );
648 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
649 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
654 /* replace | by \n */
655 for( i = 0; i < strlen( buffer_text ); i++ )
657 if( buffer_text[i] == '|' )
659 buffer_text[i] = '\n';
662 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
663 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
664 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
668 static int sub_SubRipRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
672 * h1:m1:s1,d1 --> h2:m2:s2,d2
680 char buffer_text[ 10 * MAX_LINE];
687 int h1, m1, s1, d1, h2, m2, s2, d2;
688 if( ( s = text_get_line( txt ) ) == NULL )
690 return( VLC_EGENERIC );
693 "%d:%d:%d,%d --> %d:%d:%d,%d",
695 &h2, &m2, &s2, &d2 ) == 8 )
697 i_start = ( (mtime_t)h1 * 3600*1000 +
698 (mtime_t)m1 * 60*1000 +
700 (mtime_t)d1 ) * 1000;
702 i_stop = ( (mtime_t)h2 * 3600*1000 +
703 (mtime_t)m2 * 60*1000 +
705 (mtime_t)d2 ) * 1000;
707 /* Now read text until an empty line */
708 for( i_buffer_text = 0;; )
711 if( ( s = text_get_line( txt ) ) == NULL )
713 return( VLC_EGENERIC );
719 // empty line -> end of this subtitle
720 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
721 p_subtitle->i_start = i_start;
722 p_subtitle->i_stop = i_stop;
723 p_subtitle->psz_text = strdup( buffer_text );
728 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
730 memcpy( buffer_text + i_buffer_text,
733 i_buffer_text += i_len;
735 buffer_text[i_buffer_text] = '\n';
745 static int sub_SSARead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe, int i_comma_count )
747 char buffer_text[ 10 * MAX_LINE];
757 int h1, m1, s1, c1, h2, m2, s2, c2;
760 if( ( s = text_get_line( txt ) ) == NULL )
762 return( VLC_EGENERIC );
765 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
769 buffer_text ) == 10 )
771 i_start = ( (mtime_t)h1 * 3600*1000 +
772 (mtime_t)m1 * 60*1000 +
774 (mtime_t)c1 * 10 ) * 1000;
776 i_stop = ( (mtime_t)h2 * 3600*1000 +
777 (mtime_t)m2 * 60*1000 +
779 (mtime_t)c2 * 10 ) * 1000;
781 p_buffer_text = buffer_text;
783 while( i_comma < i_comma_count &&
784 *p_buffer_text != '\0' )
786 if( *p_buffer_text == ',' )
792 p_subtitle->psz_text = malloc( strlen( p_buffer_text ) + 1);
794 while( *p_buffer_text )
796 if( *p_buffer_text == '\\' && ( *p_buffer_text =='n' || *p_buffer_text =='N' ) )
798 p_subtitle->psz_text[i_text] = '\n';
802 else if( *p_buffer_text == '{' && *p_buffer_text == '\\')
804 while( *p_buffer_text && *p_buffer_text != '}' )
811 p_subtitle->psz_text[i_text] = *p_buffer_text;
816 p_subtitle->psz_text[i_text] = '\0';
817 p_subtitle->i_start = i_start;
818 p_subtitle->i_stop = i_stop;
824 static int sub_SSA1Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
826 return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 8 ) );
828 static int sub_SSA2_4Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
830 return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 9 ) );
833 static int sub_Vplayer( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
837 * h:m:s:Line1|Line2|Line3....
839 * h:m:s Line1|Line2|Line3....
840 * where n1 and n2 are the video frame number...
844 char buffer_text[MAX_LINE + 1];
853 if( ( p = text_get_line( txt ) ) == NULL )
855 return( VLC_EGENERIC );
860 memset( buffer_text, '\0', MAX_LINE );
861 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
863 i_start = ( (mtime_t)h * 3600*1000 +
864 (mtime_t)m * 60*1000 +
865 (mtime_t)s * 1000 ) * 1000;
870 /* replace | by \n */
871 for( i = 0; i < strlen( buffer_text ); i++ )
873 if( buffer_text[i] == '|' )
875 buffer_text[i] = '\n';
878 p_subtitle->i_start = i_start;
880 p_subtitle->i_stop = 0;
881 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
885 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
889 if( strstr( psz_start, psz_str ) )
891 char *s = strstr( psz_start, psz_str );
893 s += strlen( psz_str );
901 if( ( p = text_get_line( txt ) ) == NULL )
905 if( strstr( p, psz_str ) )
907 char *s = strstr( p, psz_str );
909 s += strlen( psz_str );
916 static int sub_Sami( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
922 char buffer_text[10*MAX_LINE + 1];
924 if( i_text < 10*MAX_LINE ) \
926 buffer_text[i_text++] = c; \
927 buffer_text[i_text] = '\0'; \
930 /* search "Start=" */
931 if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
936 /* get start value */
937 i_start = strtol( p, &p, 0 );
940 if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
945 if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
951 buffer_text[0] = '\0';
952 /* now get all txt until a "Start=" line */
959 if( !strncmp( p, "<br", 3 ) || !strncmp( p, "<BR", 3 ) )
963 else if( strstr( p, "Start=" ) )
965 text_previous_line( txt );
968 p = sub_SamiSearch( txt, p, ">" );
970 else if( !strncmp( p, " ", 6 ) )
975 else if( *p == '\t' )
988 p = text_get_line( txt );
997 p_subtitle->i_start = i_start * 1000;
998 p_subtitle->i_stop = 0;
999 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1001 return( VLC_SUCCESS );