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