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>
36 #include <vlc_memory.h>
38 #include <vlc_demux.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t *p_this );
46 static void Close( vlc_object_t *p_this );
48 #define SUB_DELAY_LONGTEXT \
49 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
50 #define SUB_FPS_LONGTEXT \
51 N_("Override the normal frames per second settings. " \
52 "This will only affect frame-based subtitle formats without a fixed value.")
53 #define SUB_TYPE_LONGTEXT \
54 N_("Force the subtiles format. Use \"auto\", the set of supported values varies.")
57 set_shortname( N_("Subtitles (asa demuxer)"))
58 set_description( N_("Text subtitles parser") )
59 set_capability( "demux", 50 )
60 set_category( CAT_INPUT )
61 set_subcategory( SUBCAT_INPUT_DEMUX )
62 add_float( "sub-fps", 0.0, NULL,
63 N_("Frames per second"),
64 SUB_FPS_LONGTEXT, true )
65 add_integer( "sub-delay", 0, NULL,
66 N_("Subtitles delay"),
67 SUB_DELAY_LONGTEXT, true )
68 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
69 SUB_TYPE_LONGTEXT, true )
70 set_callbacks( Open, Close )
72 add_shortcut( "asademux" )
75 /*****************************************************************************
77 *****************************************************************************/
92 int64_t i_next_demux_date;
93 int64_t i_microsecperframe;
104 static int Demux( demux_t * );
105 static int Control( demux_t *, int, va_list );
107 static void Fix( demux_t * );
109 static int ProcessLine( demux_t *, void *, int64_t, int64_t,
110 const char *, size_t );
112 /*****************************************************************************
114 *****************************************************************************/
115 static int Open ( vlc_object_t *p_this )
117 demux_t *p_demux = (demux_t*)p_this;
120 input_thread_t *p_input;
125 struct asa_import_detect *p_detect = NULL;
127 if( strcmp( p_demux->psz_demux, "asademux" ) )
132 p_demux->pf_demux = Demux;
133 p_demux->pf_control = Control;
134 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
137 p_sys->psz_header = NULL;
138 p_sys->i_subtitle = 0;
139 p_sys->i_subtitles = 0;
140 p_sys->i_subs_alloc = 0;
141 p_sys->subtitle = NULL;
142 p_sys->i_microsecperframe = 40000;
145 p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
148 f_fps = var_GetFloat( p_input, "sub-original-fps" );
150 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
152 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
153 vlc_object_release( p_input );
156 /* Check for override of the fps */
157 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
160 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
161 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
164 /* Get or probe the type */
165 psz_type = var_CreateGetString( p_demux, "sub-type" );
168 for( p_detect = asa_det_first; p_detect; p_detect = p_detect->next )
170 if( !strcmp( p_detect->name, psz_type ) )
177 msg_Warn( p_demux, "unknown sub-type \"%s\"", psz_type );
182 /* Probe if unknown type */
188 msg_Dbg( p_demux, "autodetecting subtitle format" );
189 i_size = stream_Peek( p_demux->s, &p, 4096 );
193 msg_Warn( p_demux, "cannot process subtitles (no data?)" );
196 p_detect = asa_imports_detect( p, i_size );
201 msg_Err( p_demux, "failed to recognize subtitle type" );
207 msg_Err( p_demux, "detected %s subtitle format, no asa support", p_detect->name );
211 msg_Dbg( p_demux, "detected %s subtitle format", p_detect->name );
213 stream_Control( p_demux->s, STREAM_GET_SIZE, &i_ssize );
214 p_data = malloc( i_ssize );
220 if( stream_Read( p_demux->s, &p_data, i_ssize ) != i_ssize )
222 msg_Err( p_demux, "subtitle stream read error" );
227 asa_import( p_demux, p_data, i_ssize, p_sys->i_microsecperframe, p_detect,
231 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
233 /* Fix subtitle (order and time) *** */
235 p_sys->i_subtitle = 0;
237 if( p_sys->i_subtitles > 0 )
239 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
241 if( p_sys->i_length <= 0 )
242 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start + VLC_TS_0;
245 /* *** add subtitle ES *** */
246 if( p_detect->fmt->target == ASAI_TARGET_SSA )
248 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
252 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
254 p_sys->es = es_out_Add( p_demux->out, &fmt );
259 /*****************************************************************************
260 * ProcessLine: Callback for asa_import, fed one line
261 * (note: return values are not kept. nonzero signals abort to asa_import)
262 *****************************************************************************/
263 static int ProcessLine( demux_t *p_demux, void *p_arg,
264 int64_t i_start, int64_t i_stop,
265 const char *p_buffer, size_t i_buffer_length )
267 demux_sys_t *p_sys = p_demux->p_sys;
268 subtitle_t *p_subtitle;
273 if( p_sys->i_subtitles >= p_sys->i_subs_alloc )
275 p_sys->i_subs_alloc += 500;
276 if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
277 sizeof(subtitle_t) * p_sys->i_subs_alloc ) ) )
282 p_subtitle = &p_sys->subtitle[p_sys->i_subtitles];
284 psz_text = malloc( i_buffer_length + 1 );
287 memcpy( psz_text, p_buffer, i_buffer_length );
288 psz_text[i_buffer_length] = '\0';
290 p_subtitle->i_start = i_start;
291 p_subtitle->i_stop = i_stop;
292 p_subtitle->psz_text = psz_text;
294 p_sys->i_subtitles++;
298 /*****************************************************************************
299 * Close: Close subtitle demux
300 *****************************************************************************/
301 static void Close( vlc_object_t *p_this )
303 demux_t *p_demux = (demux_t*)p_this;
304 demux_sys_t *p_sys = p_demux->p_sys;
307 for( i = 0; i < p_sys->i_subtitles; i++ )
308 free( p_sys->subtitle[i].psz_text );
309 free( p_sys->subtitle );
314 /*****************************************************************************
316 *****************************************************************************/
317 static int Control( demux_t *p_demux, int i_query, va_list args )
319 demux_sys_t *p_sys = p_demux->p_sys;
325 case DEMUX_GET_LENGTH:
326 pi64 = (int64_t*)va_arg( args, int64_t * );
327 *pi64 = p_sys->i_length;
331 pi64 = (int64_t*)va_arg( args, int64_t * );
332 if( p_sys->i_subtitle < p_sys->i_subtitles )
334 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
340 i64 = (int64_t)va_arg( args, int64_t );
341 p_sys->i_subtitle = 0;
342 while( p_sys->i_subtitle < p_sys->i_subtitles &&
343 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
348 if( p_sys->i_subtitle >= p_sys->i_subtitles )
352 case DEMUX_GET_POSITION:
353 pf = (double*)va_arg( args, double * );
354 if( p_sys->i_subtitle >= p_sys->i_subtitles )
358 else if( p_sys->i_subtitles > 0 )
360 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
361 (double)p_sys->i_length;
369 case DEMUX_SET_POSITION:
370 f = (double)va_arg( args, double );
371 i64 = f * p_sys->i_length;
373 p_sys->i_subtitle = 0;
374 while( p_sys->i_subtitle < p_sys->i_subtitles &&
375 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
379 if( p_sys->i_subtitle >= p_sys->i_subtitles )
383 case DEMUX_SET_NEXT_DEMUX_TIME:
384 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
389 case DEMUX_GET_ATTACHMENTS:
390 case DEMUX_GET_TITLE_INFO:
394 msg_Err( p_demux, "unknown query in subtitle control" );
399 /*****************************************************************************
400 * Demux: Send subtitle to decoder
401 *****************************************************************************/
402 static int Demux( demux_t *p_demux )
404 demux_sys_t *p_sys = p_demux->p_sys;
407 if( p_sys->i_subtitle >= p_sys->i_subtitles )
410 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
411 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
413 /* Should not happen */
414 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
417 while( p_sys->i_subtitle < p_sys->i_subtitles &&
418 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
421 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
430 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
436 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
442 p_block->i_pts = VLC_TS_0 + p_sys->subtitle[p_sys->i_subtitle].i_start;
443 p_block->i_dts = VLC_TS_0 + p_block->i_pts;
444 if( p_sys->subtitle[p_sys->i_subtitle].i_stop >= 0 )
447 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
450 memcpy( p_block->p_buffer,
451 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
452 if( p_block->i_pts > VLC_TS_INVALID )
454 es_out_Send( p_demux->out, p_sys->es, p_block );
458 block_Release( p_block );
464 p_sys->i_next_demux_date = 0;
469 /*****************************************************************************
470 * Fix: fix time stamp and order of subtitle
471 *****************************************************************************/
472 static void Fix( demux_t *p_demux )
474 demux_sys_t *p_sys = p_demux->p_sys;
478 /* *** fix order (to be sure...) *** */
479 /* We suppose that there are near in order and this durty bubble sort
480 * wont take too much time
485 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
487 if( p_sys->subtitle[i_index].i_start <
488 p_sys->subtitle[i_index - 1].i_start )
492 p_sys->subtitle + i_index - 1,
493 sizeof( subtitle_t ) );
494 memcpy( p_sys->subtitle + i_index - 1,
495 p_sys->subtitle + i_index,
496 sizeof( subtitle_t ) );
497 memcpy( p_sys->subtitle + i_index,
499 sizeof( subtitle_t ) );