1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: sub.c,v 1.2 2003/01/21 16:46:17 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;
120 p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
122 p_sub->i_subtitles = 0;
123 p_sub->subtitle = NULL;
124 p_sub->p_input = p_input;
126 if( !psz_name || !*psz_name)
128 psz_name = config_GetPsz( p_sub, "sub-file" );
129 if( !psz_name || !*psz_name )
135 if( config_GetFloat( p_sub, "sub-fps" ) >= 1.0 )
137 i_microsecperframe = (mtime_t)( (float)1000000 /
138 config_GetFloat( p_sub, "sub-fps" ) );
140 else if( i_microsecperframe <= 0 )
142 i_microsecperframe = 40000; /* default: 25fps */
145 /* *** Open the file *** */
146 if( !( p_file = fopen( psz_name, "r" ) ) )
148 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
153 msg_Dbg( p_sub, "opened `%s'", psz_name );
156 psz_file_type = config_GetPsz( p_sub, "sub-type" );
157 if( psz_file_type && *psz_file_type)
159 if( !strcmp( psz_file_type, "microdvd" ) )
161 i_sub_type = SUB_TYPE_MICRODVD;
163 else if( !strcmp( psz_file_type, "subrip" ) )
165 i_sub_type = SUB_TYPE_SUBRIP;
167 else if( !strcmp( psz_file_type, "ssa1" ) )
169 i_sub_type = SUB_TYPE_SSA1;
171 else if( !strcmp( psz_file_type, "ssa2-4" ) )
173 i_sub_type = SUB_TYPE_SSA2_4;
177 i_sub_type = SUB_TYPE_UNKNOWN;
182 i_sub_type = SUB_TYPE_UNKNOWN;
185 /* *** Now try to autodetect subtitle format *** */
186 if( i_sub_type == SUB_TYPE_UNKNOWN )
188 msg_Dbg( p_input, "trying to autodetect file format" );
189 for( i_try = 0; i_try < MAX_TRY; i_try++ )
192 if( fgets( buffer, MAX_LINE, p_file ) <= 0 )
197 if( sscanf( buffer, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
198 sscanf( buffer, "{%d}{}", &i_dummy ) == 1)
200 i_sub_type = SUB_TYPE_MICRODVD;
203 else if( sscanf( buffer,
204 "%d:%d:%d,%d --> %d:%d:%d,%d",
205 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
206 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
208 i_sub_type = SUB_TYPE_SUBRIP;
211 else if( sscanf( buffer,
212 "!: This is a Sub Station Alpha v%d.x script.",
217 i_sub_type = SUB_TYPE_SSA1;
221 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
224 else if( !strcmp( buffer,
225 "Dialogue: Marked" ) )
227 i_sub_type = SUB_TYPE_SSA2_4; // could be wrong
234 /* *** Load this file in memory *** */
237 case SUB_TYPE_MICRODVD:
238 msg_Dbg( p_input, "detected MicroDVD format" );
239 pf_read_subtitle = sub_MicroDvdRead;
241 case SUB_TYPE_SUBRIP:
242 msg_Dbg( p_input, "detected SubRIP format" );
243 pf_read_subtitle = sub_SubRipRead;
246 msg_Dbg( p_input, "detected SSAv1 Script format" );
247 pf_read_subtitle = sub_SSA1Read;
249 case SUB_TYPE_SSA2_4:
250 msg_Dbg( p_input, "detected SSAv2-4 Script format" );
251 pf_read_subtitle = sub_SSA2_4Read;
254 msg_Err( p_sub, "unknown subtitile file" );
259 if( fseek( p_file, 0L, SEEK_SET ) < 0 )
261 msg_Err( p_input, "cannot read file from begining" );
267 if( p_sub->i_subtitles <= i_max )
270 if( p_sub->subtitle )
272 p_sub->subtitle = realloc( p_sub->subtitle,
273 sizeof( subtitle_t ) * i_max );
277 p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max );
280 if( pf_read_subtitle( p_file,
281 p_sub->subtitle + p_sub->i_subtitles,
282 i_microsecperframe ) < 0 )
286 p_sub->i_subtitles++;
288 msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
291 /* *** Close the file *** */
294 /* *** fix subtitle (order and time) *** */
295 p_sub->i_subtitle = 0; // will be modified by sub_fix
298 /* *** add subtitle ES *** */
299 vlc_mutex_lock( &p_input->stream.stream_lock );
300 p_sub->p_es = input_AddES( p_input,
301 p_input->stream.p_selected_program,
304 vlc_mutex_unlock( &p_input->stream.stream_lock );
306 p_sub->p_es->i_stream_id = 0xff; // FIXME
307 p_sub->p_es->i_fourcc = VLC_FOURCC( 's','u','b','t' );
308 p_sub->p_es->i_cat = SPU_ES;
310 p_sub->i_previously_selected = 0;
314 /*****************************************************************************
315 * sub_demux: Send subtitle to decoder until i_maxdate
316 *****************************************************************************/
317 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
320 if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
322 p_sub->i_previously_selected = 1;
323 p_sub->pf_seek( p_sub, i_maxdate );
326 else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
328 p_sub->i_previously_selected = 0;
332 while( p_sub->i_subtitle < p_sub->i_subtitles &&
333 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
336 data_packet_t *p_data;
340 i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
348 if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
354 if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
357 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
363 input_ClockGetTS( p_sub->p_input,
364 p_sub->p_input->stream.p_selected_program,
365 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
366 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
369 * i_dts means end of display...
372 input_ClockGetTS( p_sub->p_input,
373 p_sub->p_input->stream.p_selected_program,
374 p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
380 p_pes->i_nb_data = 1;
382 p_pes->p_last = p_data;
383 p_pes->i_pes_size = i_len;
385 memcpy( p_data->p_payload_start,
386 p_sub->subtitle[p_sub->i_subtitle].psz_text,
388 if( p_sub->p_es->p_decoder_fifo )
391 input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
395 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
404 /*****************************************************************************
405 * sub_seek: Seek to i_date
406 *****************************************************************************/
407 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
409 /* should be fast enough... */
410 p_sub->i_subtitle = 0;
411 while( p_sub->i_subtitle < p_sub->i_subtitles &&
412 p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
420 /*****************************************************************************
421 * sub_close: Close subtitle demux
422 *****************************************************************************/
423 static void sub_close( subtitle_demux_t *p_sub )
425 if( p_sub->subtitle )
428 for( i = 0; i < p_sub->i_subtitles; i++ )
430 if( p_sub->subtitle[i].psz_text )
432 free( p_sub->subtitle[i].psz_text );
435 free( p_sub->subtitle );
438 /*****************************************************************************
440 * sub_fix: fix time stamp and order of subtitle
441 *****************************************************************************/
442 static void sub_fix( subtitle_demux_t *p_sub )
449 /* *** fix order (to be sure...) *** */
450 /* We suppose that there are near in order and this durty bubble sort
451 * wont take too much time
456 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
458 if( p_sub->subtitle[i_index].i_start <
459 p_sub->subtitle[i_index - 1].i_start )
463 p_sub->subtitle + i_index - 1,
464 sizeof( subtitle_t ) );
465 memcpy( p_sub->subtitle + i_index - 1,
466 p_sub->subtitle + i_index,
467 sizeof( subtitle_t ) );
468 memcpy( p_sub->subtitle + i_index,
470 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)
520 if( sscanf( buffer, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
521 sscanf( buffer, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
526 /* replace | by \n */
527 for( i = 0; i < MAX_LINE; i++ )
529 if( buffer_text[i] == '|' )
531 buffer_text[i] = '\n';
534 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
535 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
536 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
540 static int sub_SubRipRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
544 * h1:m1:s1,d1 --> h2:m2:s2,d2
551 char buffer[MAX_LINE + 1];
552 char buffer_text[ 10 * MAX_LINE];
559 int h1, m1, s1, d1, h2, m2, s2, d2;
560 if( fgets( buffer, MAX_LINE, p_file ) <= 0)
565 "%d:%d:%d,%d --> %d:%d:%d,%d",
567 &h2, &m2, &s2, &d2 ) == 8 )
569 i_start = ( (mtime_t)h1 * 3600*1000 +
570 (mtime_t)m1 * 60*1000 +
572 (mtime_t)d1 ) * 1000;
574 i_stop = ( (mtime_t)h2 * 3600*1000 +
575 (mtime_t)m2 * 60*1000 +
577 (mtime_t)d2 ) * 1000;
579 /* Now read text until an empty line */
580 for( i_buffer_text = 0;; )
583 if( fgets( buffer, MAX_LINE, p_file ) <= 0)
587 buffer[MAX_LINE] = '\0'; // just in case
588 i_len = strlen( buffer );
589 if( buffer[0] == '\r' || buffer[0] == '\n' || i_len <= 1 )
591 // empty line -> end of this subtitle
592 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
593 p_subtitle->i_start = i_start;
594 p_subtitle->i_stop = i_stop;
595 p_subtitle->psz_text = strdup( buffer_text );
600 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
602 memcpy( buffer_text + i_buffer_text,
605 i_buffer_text += i_len;
607 buffer_text[i_buffer_text] = '\n';
618 static int sub_SSARead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe, int i_comma_count )
620 char buffer[MAX_LINE + 1];
621 char buffer_text[ 10 * MAX_LINE];
630 int h1, m1, s1, c1, h2, m2, s2, c2;
632 if( fgets( buffer, MAX_LINE, p_file ) <= 0)
637 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
641 buffer_text ) == 10 )
643 i_start = ( (mtime_t)h1 * 3600*1000 +
644 (mtime_t)m1 * 60*1000 +
646 (mtime_t)c1 * 10 ) * 1000;
648 i_stop = ( (mtime_t)h2 * 3600*1000 +
649 (mtime_t)m2 * 60*1000 +
651 (mtime_t)c2 * 10 ) * 1000;
653 p_buffer_text = buffer_text;
655 while( i_comma < i_comma_count &&
656 *p_buffer_text != '\0' )
658 if( *p_buffer_text == ',' )
664 p_subtitle->psz_text = malloc( strlen( p_buffer_text ) + 1);
666 while( *p_buffer_text )
668 if( *p_buffer_text == '\\' && ( *p_buffer_text =='n' || *p_buffer_text =='N' ) )
670 p_subtitle->psz_text[i_text] = '\n';
674 else if( *p_buffer_text == '{' && *p_buffer_text == '\\')
676 while( *p_buffer_text && *p_buffer_text != '}' )
683 p_subtitle->psz_text[i_text] = *p_buffer_text;
688 p_subtitle->psz_text[i_text] = '\0';
689 p_subtitle->i_start = i_start;
690 p_subtitle->i_stop = i_stop;
696 static int sub_SSA1Read( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
698 return( sub_SSARead( p_file, p_subtitle, i_microsecperframe, 8 ) );
700 static int sub_SSA2_4Read( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
702 return( sub_SSARead( p_file, p_subtitle, i_microsecperframe, 9 ) );