1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: sub.c,v 1.5 2003/02/09 13:25:42 fenrir 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 int sub_MicroDvdRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
51 static int sub_SubRipRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
52 static int sub_SSA1Read( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
53 static int sub_SSA2_4Read( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
55 static void sub_fix( subtitle_demux_t *p_sub );
57 static char *ppsz_sub_type[] = { "microdvd", "subrip", "ssa1", "ssa2-4", NULL };
58 /*****************************************************************************
60 *****************************************************************************/
62 #define SUB_FPS_LONGTEXT \
63 "Override frames per second. " \
64 "It will work only with MicroDVD"
65 #define SUB_TYPE_LONGTEXT \
66 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\" " \
67 "(nothing for autodetection, it should always work)"
70 set_description( _("text subtitle demux") );
71 set_capability( "subtitle demux", 12 );
72 add_category_hint( "subtitle", NULL );
73 add_string( "sub-file", NULL, NULL,
74 "subtitle file name", "subtitle file name" );
75 add_float( "sub-fps", 0.0, NULL,
76 "override frames per second",
78 add_integer( "sub-delay", 0, NULL,
79 "delay subtitles (in 1/10s)",
80 "delay subtitles (in 1/10s)" );
81 add_string_from_list( "sub-type", NULL, ppsz_sub_type, NULL,
84 set_callbacks( Open, NULL );
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t *p_this )
92 subtitle_demux_t *p_sub = (subtitle_demux_t*)p_this;
94 p_sub->pf_open = sub_open;
95 p_sub->pf_demux = sub_demux;
96 p_sub->pf_seek = sub_seek;
97 p_sub->pf_close = sub_close;
102 #define MAX_LINE 2048
103 /*****************************************************************************
104 * sub_open: Open a subtitle file and add subtitle ES
105 *****************************************************************************/
106 static int sub_open ( subtitle_demux_t *p_sub,
107 input_thread_t *p_input,
109 mtime_t i_microsecperframe )
113 char buffer[MAX_LINE + 1];
117 int (*pf_read_subtitle)( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe ) = NULL;
119 p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
121 p_sub->i_subtitles = 0;
122 p_sub->subtitle = NULL;
123 p_sub->p_input = p_input;
125 if( !psz_name || !*psz_name)
127 psz_name = config_GetPsz( p_sub, "sub-file" );
128 if( !psz_name || !*psz_name )
135 psz_name = strdup( psz_name );
138 if( config_GetFloat( p_sub, "sub-fps" ) >= 1.0 )
140 i_microsecperframe = (mtime_t)( (float)1000000 /
141 config_GetFloat( p_sub, "sub-fps" ) );
143 else if( i_microsecperframe <= 0 )
145 i_microsecperframe = 40000; /* default: 25fps */
148 /* *** Open the file *** */
149 if( !( p_file = fopen( psz_name, "rb" ) ) )
151 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
157 msg_Dbg( p_sub, "opened `%s'", psz_name );
161 psz_file_type = config_GetPsz( p_sub, "sub-type" );
162 if( psz_file_type && *psz_file_type)
164 if( !strcmp( psz_file_type, "microdvd" ) )
166 i_sub_type = SUB_TYPE_MICRODVD;
168 else if( !strcmp( psz_file_type, "subrip" ) )
170 i_sub_type = SUB_TYPE_SUBRIP;
172 else if( !strcmp( psz_file_type, "ssa1" ) )
174 i_sub_type = SUB_TYPE_SSA1;
176 else if( !strcmp( psz_file_type, "ssa2-4" ) )
178 i_sub_type = SUB_TYPE_SSA2_4;
182 i_sub_type = SUB_TYPE_UNKNOWN;
187 i_sub_type = SUB_TYPE_UNKNOWN;
190 /* *** Now try to autodetect subtitle format *** */
191 if( i_sub_type == SUB_TYPE_UNKNOWN )
193 msg_Dbg( p_input, "trying to autodetect file format" );
194 for( i_try = 0; i_try < MAX_TRY; i_try++ )
197 if( fgets( buffer, MAX_LINE, p_file ) <= 0 )
202 if( sscanf( buffer, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
203 sscanf( buffer, "{%d}{}", &i_dummy ) == 1)
205 i_sub_type = SUB_TYPE_MICRODVD;
208 else if( sscanf( buffer,
209 "%d:%d:%d,%d --> %d:%d:%d,%d",
210 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
211 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
213 i_sub_type = SUB_TYPE_SUBRIP;
216 else if( sscanf( buffer,
217 "!: This is a Sub Station Alpha v%d.x script.",
222 i_sub_type = SUB_TYPE_SSA1;
226 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
229 else if( !strcmp( buffer,
230 "Dialogue: Marked" ) )
232 i_sub_type = SUB_TYPE_SSA2_4; // could be wrong
237 /* *** Load this file in memory *** */
240 case SUB_TYPE_MICRODVD:
241 msg_Dbg( p_input, "detected MicroDVD format" );
242 pf_read_subtitle = sub_MicroDvdRead;
244 case SUB_TYPE_SUBRIP:
245 msg_Dbg( p_input, "detected SubRIP format" );
246 pf_read_subtitle = sub_SubRipRead;
249 msg_Dbg( p_input, "detected SSAv1 Script format" );
250 pf_read_subtitle = sub_SSA1Read;
252 case SUB_TYPE_SSA2_4:
253 msg_Dbg( p_input, "detected SSAv2-4 Script format" );
254 pf_read_subtitle = sub_SSA2_4Read;
257 msg_Err( p_sub, "unknown subtitile file" );
262 if( fseek( p_file, 0L, SEEK_SET ) < 0 )
264 msg_Err( p_input, "cannot read file from begining" );
270 if( p_sub->i_subtitles >= i_max )
273 if( p_sub->subtitle )
275 p_sub->subtitle = realloc( p_sub->subtitle,
276 sizeof( subtitle_t ) * i_max );
280 p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max );
283 if( pf_read_subtitle( p_file,
284 p_sub->subtitle + p_sub->i_subtitles,
285 i_microsecperframe ) < 0 )
289 p_sub->i_subtitles++;
291 msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
293 /* *** Close the file *** */
296 /* *** fix subtitle (order and time) *** */
297 p_sub->i_subtitle = 0; // will be modified by sub_fix
300 /* *** add subtitle ES *** */
301 vlc_mutex_lock( &p_input->stream.stream_lock );
302 p_sub->p_es = input_AddES( p_input,
303 p_input->stream.p_selected_program,
306 vlc_mutex_unlock( &p_input->stream.stream_lock );
308 p_sub->p_es->i_stream_id = 0xff; // FIXME
309 p_sub->p_es->i_fourcc = VLC_FOURCC( 's','u','b','t' );
310 p_sub->p_es->i_cat = SPU_ES;
312 p_sub->i_previously_selected = 0;
316 /*****************************************************************************
317 * sub_demux: Send subtitle to decoder until i_maxdate
318 *****************************************************************************/
319 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
322 if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
324 p_sub->i_previously_selected = 1;
325 p_sub->pf_seek( p_sub, i_maxdate );
328 else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
330 p_sub->i_previously_selected = 0;
334 while( p_sub->i_subtitle < p_sub->i_subtitles &&
335 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
338 data_packet_t *p_data;
342 i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
350 if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
356 if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
359 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
365 input_ClockGetTS( p_sub->p_input,
366 p_sub->p_input->stream.p_selected_program,
367 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
368 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
371 * i_dts means end of display...
374 input_ClockGetTS( p_sub->p_input,
375 p_sub->p_input->stream.p_selected_program,
376 p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
382 p_pes->i_nb_data = 1;
384 p_pes->p_last = p_data;
385 p_pes->i_pes_size = i_len;
387 memcpy( p_data->p_payload_start,
388 p_sub->subtitle[p_sub->i_subtitle].psz_text,
390 if( p_sub->p_es->p_decoder_fifo && p_pes->i_pts > 0 )
393 input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
397 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
405 /*****************************************************************************
406 * sub_seek: Seek to i_date
407 *****************************************************************************/
408 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
410 /* should be fast enough... */
411 p_sub->i_subtitle = 0;
412 while( p_sub->i_subtitle < p_sub->i_subtitles &&
413 p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
421 /*****************************************************************************
422 * sub_close: Close subtitle demux
423 *****************************************************************************/
424 static void sub_close( subtitle_demux_t *p_sub )
426 if( p_sub->subtitle )
429 for( i = 0; i < p_sub->i_subtitles; i++ )
431 if( p_sub->subtitle[i].psz_text )
433 free( p_sub->subtitle[i].psz_text );
436 free( p_sub->subtitle );
439 /*****************************************************************************
441 * sub_fix: fix time stamp and order of subtitle
442 *****************************************************************************/
443 static void sub_fix( subtitle_demux_t *p_sub )
450 /* *** fix order (to be sure...) *** */
451 /* We suppose that there are near in order and this durty bubble sort
452 * wont take too much time
457 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
459 if( p_sub->subtitle[i_index].i_start <
460 p_sub->subtitle[i_index - 1].i_start )
464 p_sub->subtitle + i_index - 1,
465 sizeof( subtitle_t ) );
466 memcpy( p_sub->subtitle + i_index - 1,
467 p_sub->subtitle + i_index,
468 sizeof( subtitle_t ) );
469 memcpy( p_sub->subtitle + i_index,
471 sizeof( subtitle_t ) );
477 /* *** and at the end add delay *** */
478 i_delay = (mtime_t)config_GetInt( p_sub, "sub-delay" ) * 100000;
481 for( i = 0; i < p_sub->i_subtitles; i++ )
483 p_sub->subtitle[i].i_start += i_delay;
484 p_sub->subtitle[i].i_stop += i_delay;
485 if( p_sub->subtitle[i].i_start < 0 )
487 p_sub->i_subtitle = i + 1;
495 /*****************************************************************************
496 * Specific Subtitle function
497 *****************************************************************************/
498 static int sub_MicroDvdRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
502 * {n1}{n2}Line1|Line2|Line3....
503 * where n1 and n2 are the video frame number...
506 char buffer[MAX_LINE + 1];
507 char buffer_text[MAX_LINE + 1];
514 if( fgets( buffer, MAX_LINE, p_file ) <= 0)
521 buffer[MAX_LINE] = '\0';
522 memset( buffer_text, '\0', MAX_LINE );
523 if( sscanf( buffer, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
524 sscanf( buffer, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
529 /* replace | by \n */
530 for( i = 0; i < strlen( buffer_text ); i++ )
532 if( buffer_text[i] == '|' )
534 buffer_text[i] = '\n';
537 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
538 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
539 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
543 static int sub_SubRipRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
547 * h1:m1:s1,d1 --> h2:m2:s2,d2
554 char buffer[MAX_LINE + 1];
555 char buffer_text[ 10 * MAX_LINE];
562 int h1, m1, s1, d1, h2, m2, s2, d2;
563 if( fgets( buffer, MAX_LINE, p_file ) <= 0)
568 "%d:%d:%d,%d --> %d:%d:%d,%d",
570 &h2, &m2, &s2, &d2 ) == 8 )
572 i_start = ( (mtime_t)h1 * 3600*1000 +
573 (mtime_t)m1 * 60*1000 +
575 (mtime_t)d1 ) * 1000;
577 i_stop = ( (mtime_t)h2 * 3600*1000 +
578 (mtime_t)m2 * 60*1000 +
580 (mtime_t)d2 ) * 1000;
582 /* Now read text until an empty line */
583 for( i_buffer_text = 0;; )
586 if( fgets( buffer, MAX_LINE, p_file ) <= 0 )
590 buffer[MAX_LINE] = '\0'; // just in case
591 i_len = strlen( buffer );
592 if( buffer[0] == '\r' || buffer[0] == '\n' || i_len <= 1 )
594 // empty line -> end of this subtitle
595 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
596 p_subtitle->i_start = i_start;
597 p_subtitle->i_stop = i_stop;
598 p_subtitle->psz_text = strdup( buffer_text );
603 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
605 memcpy( buffer_text + i_buffer_text,
608 i_buffer_text += i_len;
610 buffer_text[i_buffer_text] = '\n';
620 static int sub_SSARead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe, int i_comma_count )
622 char buffer[MAX_LINE + 1];
623 char buffer_text[ 10 * MAX_LINE];
632 int h1, m1, s1, c1, h2, m2, s2, c2;
634 if( fgets( buffer, MAX_LINE, p_file ) <= 0 )
639 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
643 buffer_text ) == 10 )
645 i_start = ( (mtime_t)h1 * 3600*1000 +
646 (mtime_t)m1 * 60*1000 +
648 (mtime_t)c1 * 10 ) * 1000;
650 i_stop = ( (mtime_t)h2 * 3600*1000 +
651 (mtime_t)m2 * 60*1000 +
653 (mtime_t)c2 * 10 ) * 1000;
655 p_buffer_text = buffer_text;
657 while( i_comma < i_comma_count &&
658 *p_buffer_text != '\0' )
660 if( *p_buffer_text == ',' )
666 p_subtitle->psz_text = malloc( strlen( p_buffer_text ) + 1);
668 while( *p_buffer_text )
670 if( *p_buffer_text == '\\' && ( *p_buffer_text =='n' || *p_buffer_text =='N' ) )
672 p_subtitle->psz_text[i_text] = '\n';
676 else if( *p_buffer_text == '{' && *p_buffer_text == '\\')
678 while( *p_buffer_text && *p_buffer_text != '}' )
685 p_subtitle->psz_text[i_text] = *p_buffer_text;
690 p_subtitle->psz_text[i_text] = '\0';
691 p_subtitle->i_start = i_start;
692 p_subtitle->i_stop = i_stop;
698 static int sub_SSA1Read( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
700 return( sub_SSARead( p_file, p_subtitle, i_microsecperframe, 8 ) );
702 static int sub_SSA2_4Read( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
704 return( sub_SSARead( p_file, p_subtitle, i_microsecperframe, 9 ) );