vobsub.c 21.4 KB
Newer Older
hartman's avatar
hartman committed
1 2 3
/*****************************************************************************
 * subtitle.c: Demux vobsub files.
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
hartman's avatar
hartman committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Derk-Jan Hartman <hartman at videolan dot org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
hartman's avatar
hartman committed
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
34

hartman's avatar
hartman committed
35
#include <sys/types.h>
36
#include <limits.h>
hartman's avatar
hartman committed
37

zorglub's avatar
zorglub committed
38
#include <vlc_demux.h>
hartman's avatar
hartman committed
39 40

#include "ps.h"
41
#include "vobsub.h"
hartman's avatar
hartman committed
42 43 44 45 46 47 48 49 50

#define MAX_LINE 8192

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t *p_this );
static void Close( vlc_object_t *p_this );

51 52 53 54 55
vlc_module_begin ()
    set_description( N_("Vobsub subtitles parser") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_capability( "demux", 1 )
zorglub's avatar
zorglub committed
56

57
    set_callbacks( Open, Close )
hartman's avatar
hartman committed
58

59 60 61
    add_shortcut( "vobsub" )
    add_shortcut( "subtitle" )
vlc_module_end ()
hartman's avatar
hartman committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/*****************************************************************************
 * Prototypes:
 *****************************************************************************/

typedef struct
{
    int     i_line_count;
    int     i_line;
    char    **line;
} text_t;
static int  TextLoad( text_t *, stream_t *s );
static void TextUnload( text_t * );

typedef struct
{
78
    int64_t i_start;
hartman's avatar
hartman committed
79 80 81
    int     i_vobsub_location;
} subtitle_t;

82 83 84 85 86
typedef struct
{
    es_format_t fmt;
    es_out_id_t *p_es;
    int         i_track_id;
87

88 89 90
    int         i_current_subtitle;
    int         i_subtitles;
    subtitle_t  *p_subtitles;
hartman's avatar
hartman committed
91 92

    int64_t     i_delay;
93
} vobsub_track_t;
hartman's avatar
hartman committed
94 95 96 97

struct demux_sys_t
{
    int64_t     i_next_demux_date;
98
    int64_t     i_length;
hartman's avatar
hartman committed
99

100
    text_t      txt;
101
    stream_t    *p_vobsub_stream;
102

103
    /* all tracks */
104 105
    int            i_tracks;
    vobsub_track_t *track;
106

107 108
    int         i_original_frame_width;
    int         i_original_frame_height;
109
    bool  b_palette;
hartman's avatar
hartman committed
110
    uint32_t    palette[16];
hartman's avatar
hartman committed
111 112 113 114 115
};

static int Demux( demux_t * );
static int Control( demux_t *, int, va_list );

116 117
static int ParseVobSubIDX( demux_t * );
static int DemuxVobSub( demux_t *, block_t *);
hartman's avatar
hartman committed
118 119 120 121 122 123 124 125

/*****************************************************************************
 * Module initializer
 *****************************************************************************/
static int Open ( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
126
    char *psz_vobname, *s;
hartman's avatar
hartman committed
127
    int i_len;
hartman's avatar
hartman committed
128 129 130 131 132

    if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
    {
        if( !strcasestr( s, "# VobSub index file" ) )
        {
133
            msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
hartman's avatar
hartman committed
134
            free( s );
135 136 137 138
            if( stream_Seek( p_demux->s, 0 ) )
            {
                msg_Warn( p_demux, "failed to rewind" );
            }
hartman's avatar
hartman committed
139 140 141 142 143 144 145
            return VLC_EGENERIC;
        }
        free( s );

    }
    else
    {
146
        msg_Dbg( p_demux, "could not read vobsub IDX file" );
hartman's avatar
hartman committed
147 148 149
        return VLC_EGENERIC;
    }

150 151 152
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
153 154
    if( unlikely( !p_sys ) )
        return VLC_ENOMEM;
155
    p_sys->i_length = 0;
156
    p_sys->p_vobsub_stream = NULL;
157
    p_sys->i_tracks = 0;
158 159 160 161 162 163
    p_sys->track = malloc( sizeof( vobsub_track_t ) );
    if( unlikely( !p_sys->track ) )
    {
        free( p_sys );
        return VLC_ENOMEM;
    }
164 165
    p_sys->i_original_frame_width = -1;
    p_sys->i_original_frame_height = -1;
166
    p_sys->b_palette = false;
hartman's avatar
hartman committed
167
    memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
168

hartman's avatar
hartman committed
169 170 171 172
    /* Load the whole file */
    TextLoad( &p_sys->txt, p_demux->s );

    /* Parse it */
173
    ParseVobSubIDX( p_demux );
hartman's avatar
hartman committed
174 175 176 177

    /* Unload */
    TextUnload( &p_sys->txt );

hartman's avatar
hartman committed
178 179 180 181
    /* Find the total length of the vobsubs */
    if( p_sys->i_tracks > 0 )
    {
        int i;
182
        for( i = 0; i < p_sys->i_tracks; i++ )
hartman's avatar
hartman committed
183
        {
184
            if( p_sys->track[i].i_subtitles > 1 )
hartman's avatar
hartman committed
185
            {
186
                if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
187
                    p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
hartman's avatar
hartman committed
188 189 190 191
            }
        }
    }

192 193 194 195 196
    if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path ) == -1 )
    {
        free( p_sys );
        return VLC_EGENERIC;
    }
197
    i_len = strlen( psz_vobname );
198
    if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
hartman's avatar
hartman committed
199 200

    /* open file */
201 202
    p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
    if( p_sys->p_vobsub_stream == NULL )
hartman's avatar
hartman committed
203 204 205
    {
        msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
                 psz_vobname );
206
        free( psz_vobname );
207 208
        free( p_sys );
        return VLC_EGENERIC;
hartman's avatar
hartman committed
209
    }
210
    free( psz_vobname );
hartman's avatar
hartman committed
211 212 213 214 215 216 217 218 219

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: Close subtitle demux
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
hartman's avatar
hartman committed
220
    int i;
hartman's avatar
hartman committed
221 222 223
    demux_t *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

hartman's avatar
hartman committed
224 225
    /* Clean all subs from all tracks */
    for( i = 0; i < p_sys->i_tracks; i++ )
226 227 228
        free( p_sys->track[i].p_subtitles );

    free( p_sys->track );
229

230 231
    if( p_sys->p_vobsub_stream )
        stream_Delete( p_sys->p_vobsub_stream );
hartman's avatar
hartman committed
232 233 234 235 236 237 238 239 240 241 242

    free( p_sys );
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    int64_t *pi64, i64;
243
    int i;
hartman's avatar
hartman committed
244 245 246 247 248 249
    double *pf, f;

    switch( i_query )
    {
        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
250
            *pi64 = (int64_t) p_sys->i_length;
hartman's avatar
hartman committed
251 252 253 254
            return VLC_SUCCESS;

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
255 256
            for( i = 0; i < p_sys->i_tracks; i++ )
            {
257
                bool b_selected;
258 259 260 261 262 263
                /* Check the ES is selected */
                es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
                                p_sys->track[i].p_es, &b_selected );
                if( b_selected ) break;
            }
            if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
hartman's avatar
hartman committed
264
            {
265
                *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
hartman's avatar
hartman committed
266
                return VLC_SUCCESS;
267
            }
hartman's avatar
hartman committed
268 269 270 271
            return VLC_EGENERIC;

        case DEMUX_SET_TIME:
            i64 = (int64_t)va_arg( args, int64_t );
272
            for( i = 0; i < p_sys->i_tracks; i++ )
hartman's avatar
hartman committed
273
            {
274 275 276 277 278 279 280 281 282
                p_sys->track[i].i_current_subtitle = 0;
                while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
                       p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
                {
                    p_sys->track[i].i_current_subtitle++;
                }

                if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
                    return VLC_EGENERIC;
hartman's avatar
hartman committed
283 284 285 286 287
            }
            return VLC_SUCCESS;

        case DEMUX_GET_POSITION:
            pf = (double*)va_arg( args, double * );
288 289
            for( i = 0; i < p_sys->i_tracks; i++ )
            {
290
                bool b_selected;
291 292 293 294 295 296
                /* Check the ES is selected */
                es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
                                p_sys->track[i].p_es, &b_selected );
                if( b_selected ) break;
            }
            if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
hartman's avatar
hartman committed
297 298 299
            {
                *pf = 1.0;
            }
300
            else if( p_sys->track[i].i_subtitles > 0 )
hartman's avatar
hartman committed
301
            {
302
                *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
hartman's avatar
hartman committed
303 304 305 306 307
                      (double)p_sys->i_length;
            }
            else
            {
                *pf = 0.0;
308
            }
hartman's avatar
hartman committed
309 310 311 312
            return VLC_SUCCESS;

        case DEMUX_SET_POSITION:
            f = (double)va_arg( args, double );
313
            i64 = (int64_t) f * p_sys->i_length;
hartman's avatar
hartman committed
314

315
            for( i = 0; i < p_sys->i_tracks; i++ )
hartman's avatar
hartman committed
316
            {
317 318 319 320 321 322 323 324
                p_sys->track[i].i_current_subtitle = 0;
                while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
                       p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
                {
                    p_sys->track[i].i_current_subtitle++;
                }
                if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
                    return VLC_EGENERIC;
hartman's avatar
hartman committed
325 326 327 328 329 330 331 332 333 334
            }
            return VLC_SUCCESS;

        case DEMUX_SET_NEXT_DEMUX_TIME:
            p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
            return VLC_SUCCESS;

        case DEMUX_GET_FPS:
        case DEMUX_GET_META:
        case DEMUX_GET_TITLE_INFO:
Laurent Aimar's avatar
Laurent Aimar committed
335 336 337
        case DEMUX_HAS_UNSUPPORTED_META:
        case DEMUX_GET_ATTACHMENTS:
        case DEMUX_CAN_RECORD:
hartman's avatar
hartman committed
338 339 340
            return VLC_EGENERIC;

        default:
Laurent Aimar's avatar
Laurent Aimar committed
341
            msg_Warn( p_demux, "unknown query in subtitle control" );
hartman's avatar
hartman committed
342 343 344 345 346 347 348 349 350 351 352
            return VLC_EGENERIC;
    }
}

/*****************************************************************************
 * Demux: Send subtitle to decoder
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    int64_t i_maxdate;
353
    int i, i_read;
hartman's avatar
hartman committed
354

355
    for( i = 0; i < p_sys->i_tracks; i++ )
hartman's avatar
hartman committed
356
    {
357 358
#define tk p_sys->track[i]
        if( tk.i_current_subtitle >= tk.i_subtitles )
359
            continue;
hartman's avatar
hartman committed
360

361
        i_maxdate = p_sys->i_next_demux_date;
362
        if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
hartman's avatar
hartman committed
363
        {
364
            /* Should not happen */
365
            i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
hartman's avatar
hartman committed
366 367
        }

368 369
        while( tk.i_current_subtitle < tk.i_subtitles &&
               tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
hartman's avatar
hartman committed
370
        {
371 372 373
            int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
            block_t *p_block;
            int i_size = 0;
hartman's avatar
hartman committed
374

375 376 377 378 379 380
            /* first compute SPU size */
            if( tk.i_current_subtitle + 1 < tk.i_subtitles )
            {
                i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
            }
            if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
hartman's avatar
hartman committed
381

382
            /* Seek at the right place */
383
            if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
384 385
            {
                msg_Warn( p_demux,
386
                          "cannot seek in the VobSub to the correct time %d", i_pos );
387 388 389 390 391 392 393 394 395 396 397 398
                tk.i_current_subtitle++;
                continue;
            }

            /* allocate a packet */
            if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
            {
                tk.i_current_subtitle++;
                continue;
            }

            /* read data */
399 400
            i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
            if( i_read <= 6 )
401 402 403 404 405
            {
                block_Release( p_block );
                tk.i_current_subtitle++;
                continue;
            }
406
            p_block->i_buffer = i_read;
hartman's avatar
hartman committed
407

408
            /* pts */
409
            p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
hartman's avatar
hartman committed
410

411 412
            /* demux this block */
            DemuxVobSub( p_demux, p_block );
hartman's avatar
hartman committed
413

414 415 416
            tk.i_current_subtitle++;
        }
#undef tk
hartman's avatar
hartman committed
417 418 419 420 421 422 423 424 425 426
    }

    /* */
    p_sys->i_next_demux_date = 0;

    return 1;
}

static int TextLoad( text_t *txt, stream_t *s )
{
427 428
    char **lines = NULL;
    size_t n = 0;
hartman's avatar
hartman committed
429 430 431 432 433

    /* load the complete file */
    for( ;; )
    {
        char *psz = stream_ReadLine( s );
434
        char **ppsz_new;
hartman's avatar
hartman committed
435

436
        if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
hartman's avatar
hartman committed
437 438
            break;

439 440
        ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
        if( ppsz_new == NULL )
hartman's avatar
hartman committed
441
        {
442 443
            free( psz );
            break;
hartman's avatar
hartman committed
444
        }
445 446
        lines = ppsz_new;
        lines[n++] = psz;
hartman's avatar
hartman committed
447 448
    }

449 450
    txt->i_line_count = n;
    txt->i_line       = 0;
451
    txt->line         = lines;
hartman's avatar
hartman committed
452 453 454

    return VLC_SUCCESS;
}
455

hartman's avatar
hartman committed
456 457 458 459 460
static void TextUnload( text_t *txt )
{
    int i;

    for( i = 0; i < txt->i_line_count; i++ )
461 462 463
        free( txt->line[i] );

    free( txt->line );
hartman's avatar
hartman committed
464 465 466 467 468 469 470 471 472 473 474 475
    txt->i_line       = 0;
    txt->i_line_count = 0;
}

static char *TextGetLine( text_t *txt )
{
    if( txt->i_line >= txt->i_line_count )
        return( NULL );

    return txt->line[txt->i_line++];
}

476
static int ParseVobSubIDX( demux_t *p_demux )
hartman's avatar
hartman committed
477 478 479
{
    demux_sys_t *p_sys = p_demux->p_sys;
    text_t      *txt = &p_sys->txt;
480
    char        *line;
481
    vobsub_track_t *current_tk = NULL;
hartman's avatar
hartman committed
482 483 484

    for( ;; )
    {
485
        if( ( line = TextGetLine( txt ) ) == NULL )
hartman's avatar
hartman committed
486 487 488
        {
            return( VLC_EGENERIC );
        }
489

490
        if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
491
        {
492
            continue;
493
        }
494 495 496
        else if( !strncmp( "size:", line, 5 ) )
        {
            /* Store the original size of the video */
497 498
            if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
                                   &p_sys->i_original_frame_height ) == VLC_SUCCESS )
499 500 501 502 503 504 505 506
            {
                msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
            }
            else
            {
                msg_Warn( p_demux, "reading original frame size failed" );
            }
        }
hartman's avatar
hartman committed
507 508
        else if( !strncmp( "palette:", line, 8 ) )
        {
509
            if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
hartman's avatar
hartman committed
510
            {
511
                p_sys->b_palette = true;
hartman's avatar
hartman committed
512 513 514 515 516 517 518
                msg_Dbg( p_demux, "vobsub palette read" );
            }
            else
            {
                msg_Warn( p_demux, "reading original palette failed" );
            }
        }
519 520
        else if( !strncmp( "id:", line, 3 ) )
        {
521
            char language[3];
522 523
            int i_track_id;
            es_format_t fmt;
hartman's avatar
hartman committed
524

525
            /* Lets start a new track */
hartman's avatar
hartman committed
526
            if( sscanf( line, "id: %2s, index: %d",
527 528 529
                        language, &i_track_id ) == 2 )
            {
                p_sys->i_tracks++;
530
                p_sys->track = xrealloc( p_sys->track,
531
                          sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
532
                language[2] = '\0';
533 534

                /* Init the track */
535
                current_tk = &p_sys->track[p_sys->i_tracks - 1];
536 537 538
                memset( current_tk, 0, sizeof( vobsub_track_t ) );
                current_tk->i_current_subtitle = 0;
                current_tk->i_subtitles = 0;
539
                current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );;
540
                current_tk->i_track_id = i_track_id;
hartman's avatar
hartman committed
541
                current_tk->i_delay = (int64_t)0;
542

543
                es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
544 545
                fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
                fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
546
                fmt.psz_language = language;
hartman's avatar
hartman committed
547 548 549 550 551
                if( p_sys->b_palette )
                {
                    fmt.subs.spu.palette[0] = 0xBeef;
                    memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
                }
552

hartman's avatar
hartman committed
553
                current_tk->p_es = es_out_Add( p_demux->out, &fmt );
554 555 556 557 558 559 560 561
                msg_Dbg( p_demux, "new vobsub track detected" );
            }
            else
            {
                msg_Warn( p_demux, "reading new track failed" );
            }
        }
        else if( !strncmp( line, "timestamp:", 10 ) )
hartman's avatar
hartman committed
562
        {
563
            /*
hartman's avatar
hartman committed
564
             * timestamp: [sign]hh:mm:ss:mss, filepos: loc
565 566
             * loc is the hex location of the spu in the .sub file
             */
hartman's avatar
hartman committed
567 568
            int h, m, s, ms, count, loc = 0;
            int i_sign = 1;
569
            int64_t i_start, i_location = 0;
570

571 572
            if( p_sys->i_tracks > 0 &&
                sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
hartman's avatar
hartman committed
573
                        &h, &count, &m, &s, &ms, &loc ) >= 5  )
574
            {
575
                vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
576
                subtitle_t *current_sub;
hartman's avatar
hartman committed
577

578 579 580 581 582
                if( line[count-3] == '-' )
                {
                    i_sign = -1;
                    h = -h;
                }
583 584 585 586
                i_start = (int64_t) ( h * 3600*1000 +
                            m * 60*1000 +
                            s * 1000 +
                            ms ) * 1000;
587
                i_location = loc;
588

589
                current_tk->i_subtitles++;
590
                current_tk->p_subtitles =
591
                    xrealloc( current_tk->p_subtitles,
592
                      sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
593
                current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
594

595
                current_sub->i_start = i_start * i_sign;
hartman's avatar
hartman committed
596
                current_sub->i_start += current_tk->i_delay;
597
                current_sub->i_vobsub_location = i_location;
598
            }
599 600 601 602
            else
            {
                msg_Warn( p_demux, "reading timestamp failed" );
            }
hartman's avatar
hartman committed
603
        }
604
        else if( !strncasecmp( line, "delay:", 6 ) )
hartman's avatar
hartman committed
605 606 607 608 609 610 611 612
        {
            /*
             * delay: [sign]hh:mm:ss:mss
             */
            int h, m, s, ms, count = 0;
            int i_sign = 1;
            int64_t i_gap = 0;

613 614
            if( p_sys->i_tracks > 0 &&
                sscanf( line, "%*celay: %d%n:%d:%d:%d",
hartman's avatar
hartman committed
615 616
                        &h, &count, &m, &s, &ms ) >= 4 )
            {
617
                vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
618 619 620 621 622
                if( line[count-3] == '-' )
                {
                    i_sign = -1;
                    h = -h;
                }
hartman's avatar
hartman committed
623 624 625 626 627 628
                i_gap = (int64_t) ( h * 3600*1000 +
                            m * 60*1000 +
                            s * 1000 +
                            ms ) * 1000;

                current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
629 630
                msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
                         i_sign, i_gap, current_tk->i_delay );
hartman's avatar
hartman committed
631
            }
632 633 634 635
            else
            {
                msg_Warn( p_demux, "reading delay failed" );
            }
hartman's avatar
hartman committed
636
        }
hartman's avatar
hartman committed
637 638 639 640
    }
    return( 0 );
}

641
static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
hartman's avatar
hartman committed
642 643 644 645
{
    demux_sys_t *p_sys = p_demux->p_sys;
    uint8_t     *p = p_bk->p_buffer;
    uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
646
    int i;
hartman's avatar
hartman committed
647

648
    while( p + 6 < p_end )
hartman's avatar
hartman committed
649 650 651 652 653 654 655
    {
        int i_size = ps_pkt_size( p, p_end - p );
        block_t *p_pkt;
        int      i_id;
        int      i_spu;

        if( i_size <= 0 )
656 657 658
            break;

        if( i_size > p_end - p )
hartman's avatar
hartman committed
659
        {
660
            msg_Warn( p_demux, "broken PES size" );
hartman's avatar
hartman committed
661 662
            break;
        }
663

hartman's avatar
hartman committed
664 665 666 667 668 669 670 671
        if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
        {
            msg_Warn( p_demux, "invalid PES" );
            break;
        }

        if( p[3] != 0xbd )
        {
672
            /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
hartman's avatar
hartman committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
            p += i_size;
            continue;
        }

        /* Create a block */
        p_pkt = block_New( p_demux, i_size );
        memcpy( p_pkt->p_buffer, p, i_size);
        p += i_size;

        i_id = ps_pkt_id( p_pkt );
        if( (i_id&0xffe0) != 0xbd20 ||
            ps_pkt_parse_pes( p_pkt, 1 ) )
        {
            block_Release( p_pkt );
            continue;
        }
        i_spu = i_id&0x1f;
690
        /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
hartman's avatar
hartman committed
691

692
        for( i = 0; i < p_sys->i_tracks; i++ )
hartman's avatar
hartman committed
693
        {
694 695
            vobsub_track_t *p_tk = &p_sys->track[i];

hartman's avatar
hartman committed
696 697
            p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
            p_pkt->i_length = 0;
698

699
            if( p_tk->p_es && p_tk->i_track_id == i_spu )
700
            {
701
                es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
702
                p_bk->i_pts = VLC_TS_INVALID;     /*only first packet has a pts */
703
                break;
704
            }
hartman's avatar
hartman committed
705 706 707 708
        }
        if( i >= p_sys->i_tracks )
        {
            block_Release( p_pkt );
hartman's avatar
hartman committed
709 710 711 712 713
        }
    }

    return VLC_SUCCESS;
}
714