1 /*****************************************************************************
2 * subtitle_asa.c: Demux for subtitle text files using the asa engine.
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
7 * Authors: David Lamparter <equinox at videolan dot org>
9 * Originated from subtitle.c
11 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
12 * Derk-Jan Hartman <hartman at videolan dot org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
44 #include <vlc_demux.h>
45 #include <vlc_charset.h>
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t *p_this );
53 static void Close( vlc_object_t *p_this );
55 #define SUB_DELAY_LONGTEXT \
56 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
57 #define SUB_FPS_LONGTEXT \
58 N_("Override the normal frames per second settings. " \
59 "This will only affect frame-based subtitle formats without a fixed value.")
60 #define SUB_TYPE_LONGTEXT \
61 N_("Force the subtiles format. Use \"auto\", the set of supported values varies.")
64 set_shortname( N_("Subtitles (asa demuxer)"))
65 set_description( N_("Text subtitles parser") )
66 set_capability( "demux", 50 )
67 set_category( CAT_INPUT )
68 set_subcategory( SUBCAT_INPUT_DEMUX )
69 add_float( "sub-fps", 0.0, NULL,
70 N_("Frames per second"),
71 SUB_FPS_LONGTEXT, true )
72 add_integer( "sub-delay", 0, NULL,
73 N_("Subtitles delay"),
74 SUB_DELAY_LONGTEXT, true )
75 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
76 SUB_TYPE_LONGTEXT, true )
77 set_callbacks( Open, Close )
79 add_shortcut( "asademux" )
82 /*****************************************************************************
84 *****************************************************************************/
99 int64_t i_next_demux_date;
100 int64_t i_microsecperframe;
106 subtitle_t *subtitle;
111 static int Demux( demux_t * );
112 static int Control( demux_t *, int, va_list );
114 static void Fix( demux_t * );
116 static int ProcessLine( demux_t *, void *, int64_t, int64_t,
117 const char *, size_t );
119 /*****************************************************************************
121 *****************************************************************************/
122 static int Open ( vlc_object_t *p_this )
124 demux_t *p_demux = (demux_t*)p_this;
127 input_thread_t *p_input;
132 struct asa_import_detect *p_detect = NULL;
134 if( strcmp( p_demux->psz_demux, "asademux" ) )
139 p_demux->pf_demux = Demux;
140 p_demux->pf_control = Control;
141 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
144 p_sys->psz_header = NULL;
145 p_sys->i_subtitle = 0;
146 p_sys->i_subtitles = 0;
147 p_sys->i_subs_alloc = 0;
148 p_sys->subtitle = NULL;
149 p_sys->i_microsecperframe = 40000;
152 p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
155 f_fps = var_GetFloat( p_input, "sub-original-fps" );
157 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
159 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
160 vlc_object_release( p_input );
163 /* Check for override of the fps */
164 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
167 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
168 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
171 /* Get or probe the type */
172 psz_type = var_CreateGetString( p_demux, "sub-type" );
175 for( p_detect = asa_det_first; p_detect; p_detect = p_detect->next )
177 if( !strcmp( p_detect->name, psz_type ) )
184 msg_Warn( p_demux, "unknown sub-type \"%s\"", psz_type );
189 /* Probe if unknown type */
195 msg_Dbg( p_demux, "autodetecting subtitle format" );
196 i_size = stream_Peek( p_demux->s, &p, 4096 );
200 msg_Warn( p_demux, "cannot process subtitles (no data?)" );
203 p_detect = asa_imports_detect( p, i_size );
208 msg_Err( p_demux, "failed to recognize subtitle type" );
214 msg_Err( p_demux, "detected %s subtitle format, no asa support", p_detect->name );
218 msg_Dbg( p_demux, "detected %s subtitle format", p_detect->name );
220 stream_Control( p_demux->s, STREAM_GET_SIZE, &i_ssize );
221 p_data = malloc( i_ssize );
227 if( stream_Read( p_demux->s, &p_data, i_ssize ) != i_ssize )
229 msg_Err( p_demux, "subtitle stream read error" );
234 asa_import( p_demux, p_data, i_ssize, p_sys->i_microsecperframe, p_detect,
238 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
240 /* Fix subtitle (order and time) *** */
242 p_sys->i_subtitle = 0;
244 if( p_sys->i_subtitles > 0 )
246 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
248 if( p_sys->i_length <= 0 )
249 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
252 /* *** add subtitle ES *** */
253 if( p_detect->fmt->target == ASAI_TARGET_SSA )
255 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
259 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
261 p_sys->es = es_out_Add( p_demux->out, &fmt );
266 /*****************************************************************************
267 * ProcessLine: Callback for asa_import, fed one line
268 * (note: return values are not kept. nonzero signals abort to asa_import)
269 *****************************************************************************/
270 static int ProcessLine( demux_t *p_demux, void *p_arg,
271 int64_t i_start, int64_t i_stop,
272 const char *p_buffer, size_t i_buffer_length )
274 demux_sys_t *p_sys = p_demux->p_sys;
275 subtitle_t *p_subtitle;
280 if( p_sys->i_subtitles >= p_sys->i_subs_alloc )
282 p_sys->i_subs_alloc += 500;
283 if( !( p_sys->subtitle = realloc( p_sys->subtitle, sizeof(subtitle_t)
284 * p_sys->i_subs_alloc ) ) )
289 p_subtitle = &p_sys->subtitle[p_sys->i_subtitles];
291 psz_text = malloc( i_buffer_length + 1 );
294 memcpy( psz_text, p_buffer, i_buffer_length );
295 psz_text[i_buffer_length] = '\0';
297 p_subtitle->i_start = i_start;
298 p_subtitle->i_stop = i_stop;
299 p_subtitle->psz_text = psz_text;
301 p_sys->i_subtitles++;
305 /*****************************************************************************
306 * Close: Close subtitle demux
307 *****************************************************************************/
308 static void Close( vlc_object_t *p_this )
310 demux_t *p_demux = (demux_t*)p_this;
311 demux_sys_t *p_sys = p_demux->p_sys;
314 for( i = 0; i < p_sys->i_subtitles; i++ )
315 free( p_sys->subtitle[i].psz_text );
316 free( p_sys->subtitle );
321 /*****************************************************************************
323 *****************************************************************************/
324 static int Control( demux_t *p_demux, int i_query, va_list args )
326 demux_sys_t *p_sys = p_demux->p_sys;
332 case DEMUX_GET_LENGTH:
333 pi64 = (int64_t*)va_arg( args, int64_t * );
334 *pi64 = p_sys->i_length;
338 pi64 = (int64_t*)va_arg( args, int64_t * );
339 if( p_sys->i_subtitle < p_sys->i_subtitles )
341 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
347 i64 = (int64_t)va_arg( args, int64_t );
348 p_sys->i_subtitle = 0;
349 while( p_sys->i_subtitle < p_sys->i_subtitles &&
350 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
355 if( p_sys->i_subtitle >= p_sys->i_subtitles )
359 case DEMUX_GET_POSITION:
360 pf = (double*)va_arg( args, double * );
361 if( p_sys->i_subtitle >= p_sys->i_subtitles )
365 else if( p_sys->i_subtitles > 0 )
367 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
368 (double)p_sys->i_length;
376 case DEMUX_SET_POSITION:
377 f = (double)va_arg( args, double );
378 i64 = f * p_sys->i_length;
380 p_sys->i_subtitle = 0;
381 while( p_sys->i_subtitle < p_sys->i_subtitles &&
382 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
386 if( p_sys->i_subtitle >= p_sys->i_subtitles )
390 case DEMUX_SET_NEXT_DEMUX_TIME:
391 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
396 case DEMUX_GET_ATTACHMENTS:
397 case DEMUX_GET_TITLE_INFO:
401 msg_Err( p_demux, "unknown query in subtitle control" );
406 /*****************************************************************************
407 * Demux: Send subtitle to decoder
408 *****************************************************************************/
409 static int Demux( demux_t *p_demux )
411 demux_sys_t *p_sys = p_demux->p_sys;
414 if( p_sys->i_subtitle >= p_sys->i_subtitles )
417 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
418 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
420 /* Should not happen */
421 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
424 while( p_sys->i_subtitle < p_sys->i_subtitles &&
425 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
428 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
437 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
443 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
449 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
450 p_block->i_dts = p_block->i_pts;
451 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
454 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
457 memcpy( p_block->p_buffer,
458 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
459 if( p_block->i_pts > 0 )
461 es_out_Send( p_demux->out, p_sys->es, p_block );
465 block_Release( p_block );
471 p_sys->i_next_demux_date = 0;
476 /*****************************************************************************
477 * Fix: fix time stamp and order of subtitle
478 *****************************************************************************/
479 static void Fix( demux_t *p_demux )
481 demux_sys_t *p_sys = p_demux->p_sys;
485 /* *** fix order (to be sure...) *** */
486 /* We suppose that there are near in order and this durty bubble sort
487 * wont take too much time
492 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
494 if( p_sys->subtitle[i_index].i_start <
495 p_sys->subtitle[i_index - 1].i_start )
499 p_sys->subtitle + i_index - 1,
500 sizeof( subtitle_t ) );
501 memcpy( p_sys->subtitle + i_index - 1,
502 p_sys->subtitle + i_index,
503 sizeof( subtitle_t ) );
504 memcpy( p_sys->subtitle + i_index,
506 sizeof( subtitle_t ) );