]> git.sesse.net Git - vlc/blob - modules/demux/subtitle_asa.c
MP4: Recognize \xa9wrt
[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     input_thread_t *p_input;
121     float          f_fps;
122     char           *psz_type;
123     int64_t        i_ssize;
124     void           *p_data;
125     struct asa_import_detect *p_detect = NULL;
126
127     if( strcmp( p_demux->psz_demux, "asademux" ) )
128     {
129         return VLC_EGENERIC;
130     }
131
132     p_demux->pf_demux = Demux;
133     p_demux->pf_control = Control;
134     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
135     if( !p_sys  )
136         return VLC_ENOMEM;
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;
143
144     /* Get the FPS */
145     p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
146     if( p_input )
147     {
148         f_fps = var_GetFloat( p_input, "sub-original-fps" );
149         if( f_fps >= 1.0 )
150             p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
151
152         msg_Dbg( p_demux, "Movie fps: %f", f_fps );
153         vlc_object_release( p_input );
154     }
155
156     /* Check for override of the fps */
157     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
158     if( f_fps >= 1.0 )
159     {
160         p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
161         msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
162     }
163
164     /* Get or probe the type */
165     psz_type = var_CreateGetString( p_demux, "sub-type" );
166     if( *psz_type )
167     {
168         for( p_detect = asa_det_first; p_detect; p_detect = p_detect->next )
169         {
170             if( !strcmp( p_detect->name, psz_type ) )
171             {
172                 break;
173             }
174         }
175         if( !p_detect )
176         {
177             msg_Warn( p_demux, "unknown sub-type \"%s\"", psz_type );
178         }
179     }
180     free( psz_type );
181
182     /* Probe if unknown type */
183     if( !p_detect )
184     {
185         int i_size;
186         const uint8_t *p;
187
188         msg_Dbg( p_demux, "autodetecting subtitle format" );
189         i_size = stream_Peek( p_demux->s, &p, 4096 );
190
191         if( i_size <= 0)
192         {
193             msg_Warn( p_demux, "cannot process subtitles (no data?)" );
194             return VLC_EGENERIC;
195         }
196         p_detect = asa_imports_detect( p, i_size );
197
198     }
199     if( !p_detect )
200     {
201         msg_Err( p_demux, "failed to recognize subtitle type" );
202         free( p_sys );
203         return VLC_EGENERIC;
204     }
205     if( !p_detect->fmt )
206     {
207         msg_Err( p_demux, "detected %s subtitle format, no asa support", p_detect->name );
208         free( p_sys );
209         return VLC_EGENERIC;
210     }
211     msg_Dbg( p_demux, "detected %s subtitle format", p_detect->name );
212
213     stream_Control( p_demux->s, STREAM_GET_SIZE, &i_ssize );
214     p_data = malloc( i_ssize );
215     if( !p_data )
216     {
217         free( p_sys );
218         return VLC_ENOMEM;
219     }
220     if( stream_Read( p_demux->s, &p_data, i_ssize ) != i_ssize )
221     {
222         msg_Err( p_demux, "subtitle stream read error" );
223         free( p_data );
224         free( p_sys );
225         return VLC_EGENERIC;
226     }
227     asa_import( p_demux, p_data, i_ssize, p_sys->i_microsecperframe, p_detect,
228                 ProcessLine, NULL );
229     free( p_data );
230
231     msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
232
233     /* Fix subtitle (order and time) *** */
234     Fix( p_demux );
235     p_sys->i_subtitle = 0;
236     p_sys->i_length = 0;
237     if( p_sys->i_subtitles > 0 )
238     {
239         p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
240         /* +1 to avoid 0 */
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;
243     }
244
245     /* *** add subtitle ES *** */
246     if( p_detect->fmt->target == ASAI_TARGET_SSA )
247     {
248         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
249     }
250     else
251     {
252         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
253     }
254     p_sys->es = es_out_Add( p_demux->out, &fmt );
255
256     return VLC_SUCCESS;
257 }
258
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 )
266 {
267     demux_sys_t *p_sys = p_demux->p_sys;
268     subtitle_t *p_subtitle;
269     char *psz_text;
270
271     VLC_UNUSED(p_arg);
272
273     if( p_sys->i_subtitles >= p_sys->i_subs_alloc )
274     {
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 ) ) )
278         {
279             return VLC_ENOMEM;
280         }
281     }
282     p_subtitle = &p_sys->subtitle[p_sys->i_subtitles];
283
284     psz_text = malloc( i_buffer_length + 1 );
285     if( !psz_text )
286         return VLC_ENOMEM;
287     memcpy( psz_text, p_buffer, i_buffer_length );
288     psz_text[i_buffer_length] = '\0';
289
290     p_subtitle->i_start = i_start;
291     p_subtitle->i_stop  = i_stop;
292     p_subtitle->psz_text = psz_text;
293
294     p_sys->i_subtitles++;
295     return VLC_SUCCESS;
296 }
297
298 /*****************************************************************************
299  * Close: Close subtitle demux
300  *****************************************************************************/
301 static void Close( vlc_object_t *p_this )
302 {
303     demux_t *p_demux = (demux_t*)p_this;
304     demux_sys_t *p_sys = p_demux->p_sys;
305     int i;
306
307     for( i = 0; i < p_sys->i_subtitles; i++ )
308         free( p_sys->subtitle[i].psz_text );
309     free( p_sys->subtitle );
310
311     free( p_sys );
312 }
313
314 /*****************************************************************************
315  * Control:
316  *****************************************************************************/
317 static int Control( demux_t *p_demux, int i_query, va_list args )
318 {
319     demux_sys_t *p_sys = p_demux->p_sys;
320     int64_t *pi64, i64;
321     double *pf, f;
322
323     switch( i_query )
324     {
325         case DEMUX_GET_LENGTH:
326             pi64 = (int64_t*)va_arg( args, int64_t * );
327             *pi64 = p_sys->i_length;
328             return VLC_SUCCESS;
329
330         case DEMUX_GET_TIME:
331             pi64 = (int64_t*)va_arg( args, int64_t * );
332             if( p_sys->i_subtitle < p_sys->i_subtitles )
333             {
334                 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
335                 return VLC_SUCCESS;
336             }
337             return VLC_EGENERIC;
338
339         case DEMUX_SET_TIME:
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 )
344             {
345                 p_sys->i_subtitle++;
346             }
347
348             if( p_sys->i_subtitle >= p_sys->i_subtitles )
349                 return VLC_EGENERIC;
350             return VLC_SUCCESS;
351
352         case DEMUX_GET_POSITION:
353             pf = (double*)va_arg( args, double * );
354             if( p_sys->i_subtitle >= p_sys->i_subtitles )
355             {
356                 *pf = 1.0;
357             }
358             else if( p_sys->i_subtitles > 0 )
359             {
360                 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
361                       (double)p_sys->i_length;
362             }
363             else
364             {
365                 *pf = 0.0;
366             }
367             return VLC_SUCCESS;
368
369         case DEMUX_SET_POSITION:
370             f = (double)va_arg( args, double );
371             i64 = f * p_sys->i_length;
372
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 )
376             {
377                 p_sys->i_subtitle++;
378             }
379             if( p_sys->i_subtitle >= p_sys->i_subtitles )
380                 return VLC_EGENERIC;
381             return VLC_SUCCESS;
382
383         case DEMUX_SET_NEXT_DEMUX_TIME:
384             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
385             return VLC_SUCCESS;
386
387         case DEMUX_GET_FPS:
388         case DEMUX_GET_META:
389         case DEMUX_GET_ATTACHMENTS:
390         case DEMUX_GET_TITLE_INFO:
391             return VLC_EGENERIC;
392
393         default:
394             msg_Err( p_demux, "unknown query in subtitle control" );
395             return VLC_EGENERIC;
396     }
397 }
398
399 /*****************************************************************************
400  * Demux: Send subtitle to decoder
401  *****************************************************************************/
402 static int Demux( demux_t *p_demux )
403 {
404     demux_sys_t *p_sys = p_demux->p_sys;
405     int64_t i_maxdate;
406
407     if( p_sys->i_subtitle >= p_sys->i_subtitles )
408         return 0;
409
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 )
412     {
413         /* Should not happen */
414         i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
415     }
416
417     while( p_sys->i_subtitle < p_sys->i_subtitles &&
418            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
419     {
420         block_t *p_block;
421         int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
422
423         if( i_len <= 1 )
424         {
425             /* empty subtitle */
426             p_sys->i_subtitle++;
427             continue;
428         }
429
430         if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
431         {
432             p_sys->i_subtitle++;
433             continue;
434         }
435
436         if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
437         {
438             p_sys->i_subtitle++;
439             continue;
440         }
441
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 )
445         {
446             p_block->i_length =
447                 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
448         }
449
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 )
453         {
454             es_out_Send( p_demux->out, p_sys->es, p_block );
455         }
456         else
457         {
458             block_Release( p_block );
459         }
460         p_sys->i_subtitle++;
461     }
462
463     /* */
464     p_sys->i_next_demux_date = 0;
465
466     return 1;
467 }
468
469 /*****************************************************************************
470  * Fix: fix time stamp and order of subtitle
471  *****************************************************************************/
472 static void Fix( demux_t *p_demux )
473 {
474     demux_sys_t *p_sys = p_demux->p_sys;
475     bool b_done;
476     int     i_index;
477
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
481      */
482     do
483     {
484         b_done = true;
485         for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
486         {
487             if( p_sys->subtitle[i_index].i_start <
488                     p_sys->subtitle[i_index - 1].i_start )
489             {
490                 subtitle_t sub_xch;
491                 memcpy( &sub_xch,
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,
498                         &sub_xch,
499                         sizeof( subtitle_t ) );
500                 b_done = false;
501             }
502         }
503     } while( !b_done );
504 }
505