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