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