vobsub.c 21.9 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * vobsub.c: Demux vobsub files.
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5 6 7 8 9
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Derk-Jan Hartman <hartman at videolan dot org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <limits.h>
34

35 36
#include <vlc_common.h>
#include <vlc_plugin.h>
37
#include <vlc_demux.h>
38

39
#include "mpeg/pes.h"
40
#include "mpeg/ps.h"
41
#include "vobsub.h"
42
#include "subtitle_helper.h"
43 44 45 46 47 48 49

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

50 51 52 53 54
vlc_module_begin ()
    set_description( N_("Vobsub subtitles parser") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_capability( "demux", 1 )
Clément Stenac's avatar
Clément Stenac committed
55

56
    set_callbacks( Open, Close )
57

58
    add_shortcut( "vobsub", "subtitle" )
59
vlc_module_end ()
60 61 62 63 64 65 66 67 68 69 70 71 72 73

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

typedef struct
{
    int     i_line_count;
    int     i_line;
    char    **line;
} text_t;

typedef struct
{
74
    int64_t i_start;
75 76 77
    int     i_vobsub_location;
} subtitle_t;

78 79 80 81
typedef struct
{
    es_out_id_t *p_es;
    int         i_track_id;
82

83 84 85
    int         i_current_subtitle;
    int         i_subtitles;
    subtitle_t  *p_subtitles;
86 87

    int64_t     i_delay;
88
} vobsub_track_t;
89 90 91

struct demux_sys_t
{
92 93
    int64_t        i_next_demux_date;
    int64_t        i_length;
94

95 96
    text_t         txt;
    stream_t       *p_vobsub_stream;
97

98
    /* all tracks */
99 100
    int            i_tracks;
    vobsub_track_t *track;
101

102 103 104 105
    int            i_original_frame_width;
    int            i_original_frame_height;
    bool           b_palette;
    uint32_t       palette[16];
106 107
};

108

109 110 111
static int Demux( demux_t * );
static int Control( demux_t *, int, va_list );

112 113
static int  TextLoad( text_t *, stream_t *s );
static void TextUnload( text_t * );
114 115
static int ParseVobSubIDX( demux_t * );
static int DemuxVobSub( demux_t *, block_t *);
116 117 118 119 120 121 122 123

/*****************************************************************************
 * Module initializer
 *****************************************************************************/
static int Open ( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
124
    char *psz_vobname, *s;
125
    int i_len;
126
    uint64_t i_read_offset = 0;
127

128
    if( ( s = peek_Readline( p_demux->s, &i_read_offset ) ) != NULL )
129 130 131
    {
        if( !strcasestr( s, "# VobSub index file" ) )
        {
132
            msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
133 134 135 136 137 138 139
            free( s );
            return VLC_EGENERIC;
        }
        free( s );
    }
    else
    {
140
        msg_Dbg( p_demux, "could not read vobsub IDX file" );
141 142 143
        return VLC_EGENERIC;
    }

144
    /* */
145
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
146 147
    if( unlikely( !p_sys ) )
        return VLC_ENOMEM;
148

149
    p_sys->i_length = 0;
150
    p_sys->p_vobsub_stream = NULL;
151
    p_sys->i_tracks = 0;
152 153
    p_sys->track = malloc( sizeof( vobsub_track_t ) );
    if( unlikely( !p_sys->track ) )
154
        goto error;
155 156
    p_sys->i_original_frame_width = -1;
    p_sys->i_original_frame_height = -1;
157
    p_sys->b_palette = false;
158
    memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
159

160 161 162 163
    /* Load the whole file */
    TextLoad( &p_sys->txt, p_demux->s );

    /* Parse it */
164
    ParseVobSubIDX( p_demux );
165 166 167 168

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

169 170 171
    /* Find the total length of the vobsubs */
    if( p_sys->i_tracks > 0 )
    {
172
        for( int i = 0; i < p_sys->i_tracks; i++ )
173
        {
174
            if( p_sys->track[i].i_subtitles > 1 )
175
            {
176
                if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
177
                    p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
178 179 180 181
            }
        }
    }

182
    if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1 )
183 184
        goto error;

185
    i_len = strlen( psz_vobname );
186
    if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
187 188

    /* open file */
189 190
    p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
    if( p_sys->p_vobsub_stream == NULL )
191 192 193
    {
        msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
                 psz_vobname );
194
        free( psz_vobname );
195
        goto error;
196
    }
197
    free( psz_vobname );
198

199 200 201
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;

202
    return VLC_SUCCESS;
203 204 205 206 207 208 209 210 211

error:
    /* Clean all subs from all tracks */
    for( int i = 0; i < p_sys->i_tracks; i++ )
        free( p_sys->track[i].p_subtitles );
    free( p_sys->track );
    free( p_sys );

    return VLC_EGENERIC;
212 213 214 215 216 217 218 219 220 221
}

/*****************************************************************************
 * Close: Close subtitle demux
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

222 223
    if( p_sys->p_vobsub_stream )
        stream_Delete( p_sys->p_vobsub_stream );
224

225 226 227 228
    /* Clean all subs from all tracks */
    for( int i = 0; i < p_sys->i_tracks; i++ )
        free( p_sys->track[i].p_subtitles );
    free( p_sys->track );
229 230 231 232 233 234 235 236 237 238
    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;
239
    int i;
240 241 242 243
    double *pf, f;

    switch( i_query )
    {
244 245 246 247
        case DEMUX_CAN_SEEK:
            *va_arg( args, bool * ) = true;
            return VLC_SUCCESS;

248 249
        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
250
            *pi64 = (int64_t) p_sys->i_length;
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
                /* Check the ES is selected */
                es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
260
                                p_sys->track[i].p_es, &b_selected );
261 262 263
                if( b_selected ) break;
            }
            if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
264
            {
265
                *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
266
                return VLC_SUCCESS;
267
            }
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++ )
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;
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
                /* Check the ES is selected */
                es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
293
                                p_sys->track[i].p_es, &b_selected );
294 295 296
                if( b_selected ) break;
            }
            if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
297 298 299
            {
                *pf = 1.0;
            }
300
            else if( p_sys->track[i].i_subtitles > 0 )
301
            {
302
                *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
303 304 305 306 307
                      (double)p_sys->i_length;
            }
            else
            {
                *pf = 0.0;
308
            }
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;
314

315
            for( i = 0; i < p_sys->i_tracks; i++ )
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;
325 326 327 328 329 330 331
            }
            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;

332
        case DEMUX_GET_PTS_DELAY:
333 334 335
        case DEMUX_GET_FPS:
        case DEMUX_GET_META:
        case DEMUX_GET_TITLE_INFO:
Laurent Aimar's avatar
Laurent Aimar committed
336 337 338
        case DEMUX_HAS_UNSUPPORTED_META:
        case DEMUX_GET_ATTACHMENTS:
        case DEMUX_CAN_RECORD:
339 340 341
            return VLC_EGENERIC;

        default:
Laurent Aimar's avatar
Laurent Aimar committed
342
            msg_Warn( p_demux, "unknown query in subtitle control" );
343 344 345 346 347 348 349 350 351 352 353
            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;
354
    int i_read;
355

356
    for( int i = 0; i < p_sys->i_tracks; i++ )
357
    {
358 359
#define tk p_sys->track[i]
        if( tk.i_current_subtitle >= tk.i_subtitles )
360
            continue;
361

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

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

376 377 378 379 380 381
            /* 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 */
382

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

            /* allocate a packet */
393
            if( ( p_block = block_Alloc( i_size ) ) == NULL )
394 395 396 397 398 399
            {
                tk.i_current_subtitle++;
                continue;
            }

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

409
            /* pts */
410
            p_block->i_pts = VLC_TS_0 + tk.p_subtitles[tk.i_current_subtitle].i_start;
411

412 413
            /* demux this block */
            DemuxVobSub( p_demux, p_block );
414

Hannes Domani's avatar
Hannes Domani committed
415 416
            block_Release( p_block );

417 418 419
            tk.i_current_subtitle++;
        }
#undef tk
420 421 422 423 424 425 426 427 428 429
    }

    /* */
    p_sys->i_next_demux_date = 0;

    return 1;
}

static int TextLoad( text_t *txt, stream_t *s )
{
430 431
    char **lines = NULL;
    size_t n = 0;
432 433 434 435 436

    /* load the complete file */
    for( ;; )
    {
        char *psz = stream_ReadLine( s );
437
        char **ppsz_new;
438

439
        if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
440 441
        {
            free( psz );
442
            break;
443
        }
444

445 446
        ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
        if( ppsz_new == NULL )
447
        {
448 449
            free( psz );
            break;
450
        }
451 452
        lines = ppsz_new;
        lines[n++] = psz;
453 454
    }

455 456
    txt->i_line_count = n;
    txt->i_line       = 0;
457
    txt->line         = lines;
458 459 460

    return VLC_SUCCESS;
}
461

462 463
static void TextUnload( text_t *txt )
{
464
    for( int i = 0; i < txt->i_line_count; i++ )
465 466
        free( txt->line[i] );
    free( txt->line );
467

468 469 470 471 472 473 474 475 476 477 478 479
    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++];
}

480
static int ParseVobSubIDX( demux_t *p_demux )
481 482 483
{
    demux_sys_t *p_sys = p_demux->p_sys;
    text_t      *txt = &p_sys->txt;
484
    char        *line;
485
    vobsub_track_t *current_tk = NULL;
486 487 488

    for( ;; )
    {
489
        if( ( line = TextGetLine( txt ) ) == NULL )
490 491 492
        {
            return( VLC_EGENERIC );
        }
493

494
        if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
495
        {
496
            continue;
497
        }
498 499 500
        else if( !strncmp( "size:", line, 5 ) )
        {
            /* Store the original size of the video */
501 502
            if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
                                   &p_sys->i_original_frame_height ) == VLC_SUCCESS )
503 504 505 506 507 508 509 510
            {
                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" );
            }
        }
511 512
        else if( !strncmp( "palette:", line, 8 ) )
        {
513
            if( vobsub_palette_parse( line, p_sys->palette ) == VLC_SUCCESS )
514
            {
515
                p_sys->b_palette = true;
516 517 518 519 520 521 522
                msg_Dbg( p_demux, "vobsub palette read" );
            }
            else
            {
                msg_Warn( p_demux, "reading original palette failed" );
            }
        }
523 524
        else if( !strncmp( "id:", line, 3 ) )
        {
525 526
            char language[33]; /* Usually 2 or 3 letters, sometimes more.
                                  Spec (or lack of) doesn't define any limit */
527 528
            int i_track_id;
            es_format_t fmt;
529

530
            /* Lets start a new track */
531
            if( sscanf( line, "id: %32[^ ,], index: %d",
532
                        language, &i_track_id ) != 2 )
533
            {
534
                if( sscanf( line, "id: , index: %d", &i_track_id ) != 1 )
535
                {
536 537
                    msg_Warn( p_demux, "reading new track failed" );
                    continue;
538
                }
539
                language[0] = '\0';
540
            }
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

            p_sys->i_tracks++;
            p_sys->track = xrealloc( p_sys->track,
                    sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );

            /* Init the track */
            current_tk = &p_sys->track[p_sys->i_tracks - 1];
            memset( current_tk, 0, sizeof( vobsub_track_t ) );
            current_tk->i_current_subtitle = 0;
            current_tk->i_subtitles = 0;
            current_tk->p_subtitles = xmalloc( sizeof( subtitle_t ) );
            current_tk->i_track_id = i_track_id;
            current_tk->i_delay = (int64_t)0;

            es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
            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;
            fmt.psz_language = language;
            if( p_sys->b_palette )
560
            {
561 562
                fmt.subs.spu.palette[0] = 0xBeef;
                memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
563
            }
564 565

            current_tk->p_es = es_out_Add( p_demux->out, &fmt );
566
            msg_Dbg( p_demux, "New vobsub track detected: %i [%s]", i_track_id, language );
567 568
        }
        else if( !strncmp( line, "timestamp:", 10 ) )
569
        {
570
            /*
571
             * timestamp: [sign]hh:mm:ss:mss, filepos: loc
572 573
             * loc is the hex location of the spu in the .sub file
             */
574 575
            int h, m, s, ms, count, loc = 0;
            int i_sign = 1;
576
            int64_t i_start, i_location = 0;
577

578 579
            if( p_sys->i_tracks > 0 &&
                sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
580
                        &h, &count, &m, &s, &ms, &loc ) >= 5  )
581
            {
582
                vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
583
                subtitle_t *current_sub;
584

585 586 587 588 589
                if( line[count-3] == '-' )
                {
                    i_sign = -1;
                    h = -h;
                }
590 591 592 593
                i_start = (int64_t) ( h * 3600*1000 +
                            m * 60*1000 +
                            s * 1000 +
                            ms ) * 1000;
594
                i_location = loc;
595

596
                current_tk->i_subtitles++;
597
                current_tk->p_subtitles =
598
                    xrealloc( current_tk->p_subtitles,
599
                      sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
600
                current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
601

602
                current_sub->i_start = i_start * i_sign;
603
                current_sub->i_start += current_tk->i_delay;
604
                current_sub->i_vobsub_location = i_location;
605
            }
606 607 608 609
            else
            {
                msg_Warn( p_demux, "reading timestamp failed" );
            }
610
        }
611
        else if( !strncasecmp( line, "delay:", 6 ) )
612 613 614 615 616 617 618 619
        {
            /*
             * delay: [sign]hh:mm:ss:mss
             */
            int h, m, s, ms, count = 0;
            int i_sign = 1;
            int64_t i_gap = 0;

620 621
            if( p_sys->i_tracks > 0 &&
                sscanf( line, "%*celay: %d%n:%d:%d:%d",
622 623
                        &h, &count, &m, &s, &ms ) >= 4 )
            {
624
                vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
625 626 627 628 629
                if( line[count-3] == '-' )
                {
                    i_sign = -1;
                    h = -h;
                }
630 631 632 633 634 635
                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);
636 637
                msg_Dbg( p_demux, "sign: %+d gap: %+"PRId64" global delay: %+"PRId64"",
                         i_sign, i_gap, current_tk->i_delay );
638
            }
639 640 641 642
            else
            {
                msg_Warn( p_demux, "reading delay failed" );
            }
643
        }
644 645 646 647
    }
    return( 0 );
}

648
static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
649 650 651 652
{
    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];
653
    int i;
654

655
    while( p + 6 < p_end )
656 657 658 659 660 661 662
    {
        int i_size = ps_pkt_size( p, p_end - p );
        block_t *p_pkt;
        int      i_id;
        int      i_spu;

        if( i_size <= 0 )
663 664 665
            break;

        if( i_size > p_end - p )
666
        {
667
            msg_Warn( p_demux, "broken PES size" );
668 669
            break;
        }
670

671 672 673 674 675 676 677 678
        if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
        {
            msg_Warn( p_demux, "invalid PES" );
            break;
        }

        if( p[3] != 0xbd )
        {
679
            /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
680 681 682 683 684
            p += i_size;
            continue;
        }

        /* Create a block */
685
        p_pkt = block_Alloc( i_size );
686 687
        if( unlikely(p_pkt == NULL) )
            break;
688 689 690 691 692
        memcpy( p_pkt->p_buffer, p, i_size);
        p += i_size;

        i_id = ps_pkt_id( p_pkt );
        if( (i_id&0xffe0) != 0xbd20 ||
693
            ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, 1 ) )
694 695 696 697 698
        {
            block_Release( p_pkt );
            continue;
        }
        i_spu = i_id&0x1f;
699
        /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
700

701
        for( i = 0; i < p_sys->i_tracks; i++ )
702
        {
703 704
            vobsub_track_t *p_tk = &p_sys->track[i];

705 706
            p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
            p_pkt->i_length = 0;
707

708
            if( p_tk->p_es && p_tk->i_track_id == i_spu )
709
            {
710
                es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
711
                p_bk->i_pts = VLC_TS_INVALID;     /*only first packet has a pts */
712
                break;
713
            }
714 715 716 717
        }
        if( i >= p_sys->i_tracks )
        {
            block_Release( p_pkt );
718 719 720 721 722
        }
    }

    return VLC_SUCCESS;
}
723