]> git.sesse.net Git - vlc/blob - modules/demux/subtitle_asa.c
99f091310bf535df69f18279a4b5f8b2e6c8221c
[vlc] / modules / demux / subtitle_asa.c
1 /*****************************************************************************
2  * subtitle_asa.c: Demux for subtitle text files using the asa engine.
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: David Lamparter <equinox at videolan dot org>
8  *
9  * Originated from subtitle.c
10  *
11  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
12  *          Derk-Jan Hartman <hartman at videolan dot org>
13  *
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.
18  *
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.
23  *
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  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include "config.h"
33 #include <vlc/vlc.h>
34 #include <vlc_input.h>
35
36
37 #include <errno.h>
38 #ifdef HAVE_SYS_TYPES_H
39 #   include <sys/types.h>
40 #endif
41 #include <ctype.h>
42
43 #include <vlc_demux.h>
44 #include <vlc_charset.h>
45
46 #include "asademux.h"
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  Open ( vlc_object_t *p_this );
52 static void Close( vlc_object_t *p_this );
53
54 #define SUB_DELAY_LONGTEXT \
55     N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
56 #define SUB_FPS_LONGTEXT \
57     N_("Override the normal frames per second settings. " \
58     "This will only affect frame-based subtitle formats without a fixed value.")
59 #define SUB_TYPE_LONGTEXT \
60     N_("Force the subtiles format. Use \"auto\", the set of supported values varies.")
61
62 vlc_module_begin();
63     set_shortname( _("Subtitles (asa demuxer)"));
64     set_description( _("Text subtitles parser") );
65     set_capability( "demux2", 50 );
66     set_category( CAT_INPUT );
67     set_subcategory( SUBCAT_INPUT_DEMUX );
68     add_float( "sub-fps", 0.0, NULL,
69                N_("Frames per second"),
70                SUB_FPS_LONGTEXT, VLC_TRUE );
71     add_integer( "sub-delay", 0, NULL,
72                N_("Subtitles delay"),
73                SUB_DELAY_LONGTEXT, VLC_TRUE );
74     add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
75                 SUB_TYPE_LONGTEXT, VLC_TRUE );
76     set_callbacks( Open, Close );
77
78     add_shortcut( "asademux" );
79 vlc_module_end();
80
81 /*****************************************************************************
82  * Prototypes:
83  *****************************************************************************/
84 typedef struct
85 {
86     int64_t i_start;
87     int64_t i_stop;
88
89     char    *psz_text;
90 } subtitle_t;
91
92
93 struct demux_sys_t
94 {
95     int         i_type;
96     es_out_id_t *es;
97
98     int64_t     i_next_demux_date;
99     int64_t     i_microsecperframe;
100
101     char        *psz_header;
102     int         i_subtitle;
103     int         i_subtitles;
104     int         i_subs_alloc;
105     subtitle_t  *subtitle;
106
107     int64_t     i_length;
108 };
109
110 static int Demux( demux_t * );
111 static int Control( demux_t *, int, va_list );
112
113 static void Fix( demux_t * );
114
115 static int ProcessLine( demux_t *, void *, int64_t, int64_t,
116                          const char *, size_t );
117
118 /*****************************************************************************
119  * Module initializer
120  *****************************************************************************/
121 static int Open ( vlc_object_t *p_this )
122 {
123     demux_t        *p_demux = (demux_t*)p_this;
124     demux_sys_t    *p_sys;
125     es_format_t    fmt;
126     input_thread_t *p_input;
127     float          f_fps;
128     char           *psz_type;
129     int64_t        i_ssize;
130     void           *p_data;
131     struct asa_import_detect *p_detect = NULL;
132
133     if( strcmp( p_demux->psz_demux, "asademux" ) )
134     {
135         msg_Dbg( p_demux, "asademux discarded" );
136         return VLC_EGENERIC;
137     }
138
139     p_demux->pf_demux = Demux;
140     p_demux->pf_control = Control;
141     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
142     p_sys->psz_header         = NULL;
143     p_sys->i_subtitle         = 0;
144     p_sys->i_subtitles        = 0;
145     p_sys->i_subs_alloc       = 0;
146     p_sys->subtitle           = NULL;
147     p_sys->i_microsecperframe = 40000;
148
149     /* Get the FPS */
150     p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
151     if( p_input )
152     {
153         f_fps = var_GetFloat( p_input, "sub-original-fps" );
154         if( f_fps >= 1.0 )
155             p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
156
157         msg_Dbg( p_demux, "Movie fps: %f", f_fps );
158         vlc_object_release( p_input );
159     }
160
161     /* Check for override of the fps */
162     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
163     if( f_fps >= 1.0 )
164     {
165         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
166         msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
167     }
168
169     /* Get or probe the type */
170     psz_type = var_CreateGetString( p_demux, "sub-type" );
171     if( *psz_type )
172     {
173         for( p_detect = asa_det_first; p_detect; p_detect = p_detect->next )
174         {
175             if( !strcmp( p_detect->name, psz_type ) )
176             {
177                 break;
178             }
179         }
180         if( !p_detect )
181         {
182             msg_Warn( p_demux, "unknown sub-type \"%s\"", psz_type );
183         }
184     }
185     free( psz_type );
186
187     /* Probe if unknown type */
188     if( !p_detect )
189     {
190         int i_size;
191         const uint8_t *p;
192
193         msg_Dbg( p_demux, "autodetecting subtitle format" );
194         i_size = stream_Peek( p_demux->s, &p, 4096 );
195
196         if( i_size <= 0)
197         {
198             msg_Warn( p_demux, "cannot process subtitles (no data?)" );
199             return VLC_EGENERIC;
200         }
201         p_detect = asa_imports_detect( p, i_size );
202
203     }
204     if( !p_detect )
205     {
206         msg_Err( p_demux, "failed to recognize subtitle type" );
207         free( p_sys );
208         return VLC_EGENERIC;
209     }
210     if( !p_detect->fmt )
211     {
212         msg_Err( p_demux, "detected %s subtitle format, no asa support" );
213         free( p_sys );
214         return VLC_EGENERIC;
215     }
216     msg_Dbg( p_demux, "detected %s subtitle format", p_detect->name );
217
218     stream_Control( p_demux->s, STREAM_GET_SIZE, &i_ssize );
219     p_data = malloc( i_ssize );
220     if( !p_data )
221     {
222         msg_Err( p_demux, "out of memory");
223         free( p_sys );
224         return VLC_ENOMEM;
225     }
226     if( stream_Read( p_demux->s, &p_data, i_ssize ) != i_ssize )
227     {
228         msg_Err( p_demux, "subtitle stream read error" );
229         free( p_data );
230         free( p_sys );
231         return VLC_EGENERIC;
232     }
233     asa_import( p_demux, p_data, i_ssize, p_sys->i_microsecperframe, p_detect,
234                 ProcessLine, NULL );
235     free( p_data );
236
237     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
238
239     /* Fix subtitle (order and time) *** */
240     Fix( p_demux );
241     p_sys->i_subtitle = 0;
242     p_sys->i_length = 0;
243     if( p_sys->i_subtitles > 0 )
244     {
245         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
246         /* +1 to avoid 0 */
247         if( p_sys->i_length <= 0 )
248             p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
249     }
250
251     /* *** add subtitle ES *** */
252     if( p_detect->fmt->target == ASAI_TARGET_SSA )
253     {
254         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
255     }
256     else
257     {
258         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
259     }
260     p_sys->es = es_out_Add( p_demux->out, &fmt );
261
262     return VLC_SUCCESS;
263 }
264
265 /*****************************************************************************
266  * ProcessLine: Callback for asa_import, fed one line
267  * (note: return values are not kept. nonzero signals abort to asa_import)
268  *****************************************************************************/
269 static int ProcessLine( demux_t *p_demux, void *p_arg,
270                          int64_t i_start, int64_t i_stop,
271                          const char *p_buffer, size_t i_buffer_length )
272 {
273     demux_sys_t *p_sys = p_demux->p_sys;
274     subtitle_t *p_subtitle;
275     char *psz_text;
276
277     if( p_sys->i_subtitles >= p_sys->i_subs_alloc )
278     {
279         p_sys->i_subs_alloc += 500;
280         if( !( p_sys->subtitle = realloc( p_sys->subtitle, sizeof(subtitle_t)
281                                           * p_sys->i_subs_alloc ) ) )
282         {
283             msg_Err( p_demux, "out of memory");
284             return VLC_ENOMEM;
285         }
286     }
287     p_subtitle = &p_sys->subtitle[p_sys->i_subtitles];
288
289     psz_text = malloc( i_buffer_length + 1 );
290     if( !psz_text )
291         return VLC_ENOMEM;
292     memcpy( psz_text, p_buffer, i_buffer_length );
293     psz_text[i_buffer_length] = '\0';
294
295     p_subtitle->i_start = i_start;
296     p_subtitle->i_stop  = i_stop;
297     p_subtitle->psz_text = psz_text;
298
299     p_sys->i_subtitles++;
300     return VLC_SUCCESS;
301 }
302
303 /*****************************************************************************
304  * Close: Close subtitle demux
305  *****************************************************************************/
306 static void Close( vlc_object_t *p_this )
307 {
308     demux_t *p_demux = (demux_t*)p_this;
309     demux_sys_t *p_sys = p_demux->p_sys;
310     int i;
311
312     for( i = 0; i < p_sys->i_subtitles; i++ )
313     {
314         if( p_sys->subtitle[i].psz_text )
315             free( p_sys->subtitle[i].psz_text );
316     }
317     if( p_sys->subtitle )
318         free( p_sys->subtitle );
319
320     free( p_sys );
321 }
322
323 /*****************************************************************************
324  * Control:
325  *****************************************************************************/
326 static int Control( demux_t *p_demux, int i_query, va_list args )
327 {
328     demux_sys_t *p_sys = p_demux->p_sys;
329     int64_t *pi64, i64;
330     double *pf, f;
331
332     switch( i_query )
333     {
334         case DEMUX_GET_LENGTH:
335             pi64 = (int64_t*)va_arg( args, int64_t * );
336             *pi64 = p_sys->i_length;
337             return VLC_SUCCESS;
338
339         case DEMUX_GET_TIME:
340             pi64 = (int64_t*)va_arg( args, int64_t * );
341             if( p_sys->i_subtitle < p_sys->i_subtitles )
342             {
343                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
344                 return VLC_SUCCESS;
345             }
346             return VLC_EGENERIC;
347
348         case DEMUX_SET_TIME:
349             i64 = (int64_t)va_arg( args, int64_t );
350             p_sys->i_subtitle = 0;
351             while( p_sys->i_subtitle < p_sys->i_subtitles &&
352                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
353             {
354                 p_sys->i_subtitle++;
355             }
356
357             if( p_sys->i_subtitle >= p_sys->i_subtitles )
358                 return VLC_EGENERIC;
359             return VLC_SUCCESS;
360
361         case DEMUX_GET_POSITION:
362             pf = (double*)va_arg( args, double * );
363             if( p_sys->i_subtitle >= p_sys->i_subtitles )
364             {
365                 *pf = 1.0;
366             }
367             else if( p_sys->i_subtitles > 0 )
368             {
369                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
370                       (double)p_sys->i_length;
371             }
372             else
373             {
374                 *pf = 0.0;
375             }
376             return VLC_SUCCESS;
377
378         case DEMUX_SET_POSITION:
379             f = (double)va_arg( args, double );
380             i64 = f * p_sys->i_length;
381
382             p_sys->i_subtitle = 0;
383             while( p_sys->i_subtitle < p_sys->i_subtitles &&
384                    p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
385             {
386                 p_sys->i_subtitle++;
387             }
388             if( p_sys->i_subtitle >= p_sys->i_subtitles )
389                 return VLC_EGENERIC;
390             return VLC_SUCCESS;
391
392         case DEMUX_SET_NEXT_DEMUX_TIME:
393             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
394             return VLC_SUCCESS;
395
396         case DEMUX_GET_FPS:
397         case DEMUX_GET_META:
398         case DEMUX_GET_ATTACHMENTS:
399         case DEMUX_GET_TITLE_INFO:
400             return VLC_EGENERIC;
401
402         default:
403             msg_Err( p_demux, "unknown query in subtitle control" );
404             return VLC_EGENERIC;
405     }
406 }
407
408 /*****************************************************************************
409  * Demux: Send subtitle to decoder
410  *****************************************************************************/
411 static int Demux( demux_t *p_demux )
412 {
413     demux_sys_t *p_sys = p_demux->p_sys;
414     int64_t i_maxdate;
415
416     if( p_sys->i_subtitle >= p_sys->i_subtitles )
417         return 0;
418
419     i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
420     if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
421     {
422         /* Should not happen */
423         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
424     }
425
426     while( p_sys->i_subtitle < p_sys->i_subtitles &&
427            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
428     {
429         block_t *p_block;
430         int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
431
432         if( i_len <= 1 )
433         {
434             /* empty subtitle */
435             p_sys->i_subtitle++;
436             continue;
437         }
438
439         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
440         {
441             p_sys->i_subtitle++;
442             continue;
443         }
444
445         if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
446         {
447             p_sys->i_subtitle++;
448             continue;
449         }
450
451         p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
452         p_block->i_dts = p_block->i_pts;
453         if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
454         {
455             p_block->i_length =
456                 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
457         }
458
459         memcpy( p_block->p_buffer,
460                 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
461         if( p_block->i_pts > 0 )
462         {
463             es_out_Send( p_demux->out, p_sys->es, p_block );
464         }
465         else
466         {
467             block_Release( p_block );
468         }
469         p_sys->i_subtitle++;
470     }
471
472     /* */
473     p_sys->i_next_demux_date = 0;
474
475     return 1;
476 }
477
478 /*****************************************************************************
479  * Fix: fix time stamp and order of subtitle
480  *****************************************************************************/
481 static void Fix( demux_t *p_demux )
482 {
483     demux_sys_t *p_sys = p_demux->p_sys;
484     vlc_bool_t b_done;
485     int     i_index;
486
487     /* *** fix order (to be sure...) *** */
488     /* We suppose that there are near in order and this durty bubble sort
489      * wont take too much time
490      */
491     do
492     {
493         b_done = VLC_TRUE;
494         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
495         {
496             if( p_sys->subtitle[i_index].i_start <
497                     p_sys->subtitle[i_index - 1].i_start )
498             {
499                 subtitle_t sub_xch;
500                 memcpy( &sub_xch,
501                         p_sys->subtitle + i_index - 1,
502                         sizeof( subtitle_t ) );
503                 memcpy( p_sys->subtitle + i_index - 1,
504                         p_sys->subtitle + i_index,
505                         sizeof( subtitle_t ) );
506                 memcpy( p_sys->subtitle + i_index,
507                         &sub_xch,
508                         sizeof( subtitle_t ) );
509                 b_done = VLC_FALSE;
510             }
511         }
512     } while( !b_done );
513 }
514