]> git.sesse.net Git - vlc/blob - modules/demux/vobsub.c
Used VLC_CODEC_* and vlc_fourcc_GetCodec when suitable.
[vlc] / modules / demux / vobsub.c
1 /*****************************************************************************
2  * subtitle.c: Demux vobsub files.
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <limits.h>
38
39 #include <vlc_demux.h>
40 #include <vlc_charset.h>
41
42 #include "ps.h"
43
44 #define MAX_LINE 8192
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  Open ( vlc_object_t *p_this );
50 static void Close( vlc_object_t *p_this );
51
52 vlc_module_begin ()
53     set_description( N_("Vobsub subtitles parser") )
54     set_category( CAT_INPUT )
55     set_subcategory( SUBCAT_INPUT_DEMUX )
56     set_capability( "demux", 1 )
57
58     set_callbacks( Open, Close )
59
60     add_shortcut( "vobsub" )
61     add_shortcut( "subtitle" )
62 vlc_module_end ()
63
64 /*****************************************************************************
65  * Prototypes:
66  *****************************************************************************/
67
68 typedef struct
69 {
70     int     i_line_count;
71     int     i_line;
72     char    **line;
73 } text_t;
74 static int  TextLoad( text_t *, stream_t *s );
75 static void TextUnload( text_t * );
76
77 typedef struct
78 {
79     int64_t i_start;
80     int     i_vobsub_location;
81 } subtitle_t;
82
83 typedef struct
84 {
85     es_format_t fmt;
86     es_out_id_t *p_es;
87     int         i_track_id;
88
89     int         i_current_subtitle;
90     int         i_subtitles;
91     subtitle_t  *p_subtitles;
92
93     int64_t     i_delay;
94 } vobsub_track_t;
95
96 struct demux_sys_t
97 {
98     int64_t     i_next_demux_date;
99     int64_t     i_length;
100
101     text_t      txt;
102     stream_t    *p_vobsub_stream;
103
104     /* all tracks */
105     int            i_tracks;
106     vobsub_track_t *track;
107
108     int         i_original_frame_width;
109     int         i_original_frame_height;
110     bool  b_palette;
111     uint32_t    palette[16];
112 };
113
114 static int Demux( demux_t * );
115 static int Control( demux_t *, int, va_list );
116
117 static int ParseVobSubIDX( demux_t * );
118 static int DemuxVobSub( demux_t *, block_t *);
119
120 /*****************************************************************************
121  * Module initializer
122  *****************************************************************************/
123 static int Open ( vlc_object_t *p_this )
124 {
125     demux_t     *p_demux = (demux_t*)p_this;
126     demux_sys_t *p_sys;
127     char *psz_vobname, *s;
128     int i_len;
129
130     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
131     {
132         if( !strcasestr( s, "# VobSub index file" ) )
133         {
134             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
135             free( s );
136             if( stream_Seek( p_demux->s, 0 ) )
137             {
138                 msg_Warn( p_demux, "failed to rewind" );
139             }
140             return VLC_EGENERIC;
141         }
142         free( s );
143
144     }
145     else
146     {
147         msg_Dbg( p_demux, "could not read vobsub IDX file" );
148         return VLC_EGENERIC;
149     }
150
151     p_demux->pf_demux = Demux;
152     p_demux->pf_control = Control;
153     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
154     p_sys->i_length = 0;
155     p_sys->p_vobsub_stream = NULL;
156     p_sys->i_tracks = 0;
157     p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
158     p_sys->i_original_frame_width = -1;
159     p_sys->i_original_frame_height = -1;
160     p_sys->b_palette = false;
161     memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
162
163     /* Load the whole file */
164     TextLoad( &p_sys->txt, p_demux->s );
165
166     /* Parse it */
167     ParseVobSubIDX( p_demux );
168
169     /* Unload */
170     TextUnload( &p_sys->txt );
171
172     /* Find the total length of the vobsubs */
173     if( p_sys->i_tracks > 0 )
174     {
175         int i;
176         for( i = 0; i < p_sys->i_tracks; i++ )
177         {
178             if( p_sys->track[i].i_subtitles > 1 )
179             {
180                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
181                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
182             }
183         }
184     }
185
186     if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path ) == -1 )
187     {
188         free( p_sys );
189         return VLC_EGENERIC;
190     }
191     i_len = strlen( psz_vobname );
192     if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
193
194     /* open file */
195     p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
196     if( p_sys->p_vobsub_stream == NULL )
197     {
198         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
199                  psz_vobname );
200         free( psz_vobname );
201         free( p_sys );
202         return VLC_EGENERIC;
203     }
204     free( psz_vobname );
205
206     return VLC_SUCCESS;
207 }
208
209 /*****************************************************************************
210  * Close: Close subtitle demux
211  *****************************************************************************/
212 static void Close( vlc_object_t *p_this )
213 {
214     int i;
215     demux_t *p_demux = (demux_t*)p_this;
216     demux_sys_t *p_sys = p_demux->p_sys;
217
218     /* Clean all subs from all tracks */
219     for( i = 0; i < p_sys->i_tracks; i++ )
220         free( p_sys->track[i].p_subtitles );
221
222     free( p_sys->track );
223
224     if( p_sys->p_vobsub_stream )
225         stream_Delete( p_sys->p_vobsub_stream );
226
227     free( p_sys );
228 }
229
230 /*****************************************************************************
231  * Control:
232  *****************************************************************************/
233 static int Control( demux_t *p_demux, int i_query, va_list args )
234 {
235     demux_sys_t *p_sys = p_demux->p_sys;
236     int64_t *pi64, i64;
237     int i;
238     double *pf, f;
239
240     switch( i_query )
241     {
242         case DEMUX_GET_LENGTH:
243             pi64 = (int64_t*)va_arg( args, int64_t * );
244             *pi64 = (int64_t) p_sys->i_length;
245             return VLC_SUCCESS;
246
247         case DEMUX_GET_TIME:
248             pi64 = (int64_t*)va_arg( args, int64_t * );
249             for( i = 0; i < p_sys->i_tracks; i++ )
250             {
251                 bool b_selected;
252                 /* Check the ES is selected */
253                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
254                                 p_sys->track[i].p_es, &b_selected );
255                 if( b_selected ) break;
256             }
257             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
258             {
259                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
260                 return VLC_SUCCESS;
261             }
262             return VLC_EGENERIC;
263
264         case DEMUX_SET_TIME:
265             i64 = (int64_t)va_arg( args, int64_t );
266             for( i = 0; i < p_sys->i_tracks; i++ )
267             {
268                 p_sys->track[i].i_current_subtitle = 0;
269                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
270                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
271                 {
272                     p_sys->track[i].i_current_subtitle++;
273                 }
274
275                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
276                     return VLC_EGENERIC;
277             }
278             return VLC_SUCCESS;
279
280         case DEMUX_GET_POSITION:
281             pf = (double*)va_arg( args, double * );
282             for( i = 0; i < p_sys->i_tracks; i++ )
283             {
284                 bool b_selected;
285                 /* Check the ES is selected */
286                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
287                                 p_sys->track[i].p_es, &b_selected );
288                 if( b_selected ) break;
289             }
290             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
291             {
292                 *pf = 1.0;
293             }
294             else if( p_sys->track[i].i_subtitles > 0 )
295             {
296                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
297                       (double)p_sys->i_length;
298             }
299             else
300             {
301                 *pf = 0.0;
302             }
303             return VLC_SUCCESS;
304
305         case DEMUX_SET_POSITION:
306             f = (double)va_arg( args, double );
307             i64 = (int64_t) f * p_sys->i_length;
308
309             for( i = 0; i < p_sys->i_tracks; i++ )
310             {
311                 p_sys->track[i].i_current_subtitle = 0;
312                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
313                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
314                 {
315                     p_sys->track[i].i_current_subtitle++;
316                 }
317                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
318                     return VLC_EGENERIC;
319             }
320             return VLC_SUCCESS;
321
322         case DEMUX_SET_NEXT_DEMUX_TIME:
323             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
324             return VLC_SUCCESS;
325
326         case DEMUX_GET_FPS:
327         case DEMUX_GET_META:
328         case DEMUX_GET_TITLE_INFO:
329         case DEMUX_HAS_UNSUPPORTED_META:
330         case DEMUX_GET_ATTACHMENTS:
331         case DEMUX_CAN_RECORD:
332             return VLC_EGENERIC;
333
334         default:
335             msg_Warn( p_demux, "unknown query in subtitle control" );
336             return VLC_EGENERIC;
337     }
338 }
339
340 /*****************************************************************************
341  * Demux: Send subtitle to decoder
342  *****************************************************************************/
343 static int Demux( demux_t *p_demux )
344 {
345     demux_sys_t *p_sys = p_demux->p_sys;
346     int64_t i_maxdate;
347     int i, i_read;
348
349     for( i = 0; i < p_sys->i_tracks; i++ )
350     {
351 #define tk p_sys->track[i]
352         if( tk.i_current_subtitle >= tk.i_subtitles )
353             continue;
354
355         i_maxdate = p_sys->i_next_demux_date;
356         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
357         {
358             /* Should not happen */
359             i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
360         }
361
362         while( tk.i_current_subtitle < tk.i_subtitles &&
363                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
364         {
365             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
366             block_t *p_block;
367             int i_size = 0;
368
369             /* first compute SPU size */
370             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
371             {
372                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
373             }
374             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
375
376             /* Seek at the right place */
377             if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
378             {
379                 msg_Warn( p_demux,
380                           "cannot seek in the VobSub to the correct time %d", i_pos );
381                 tk.i_current_subtitle++;
382                 continue;
383             }
384
385             /* allocate a packet */
386             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
387             {
388                 tk.i_current_subtitle++;
389                 continue;
390             }
391
392             /* read data */
393             i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
394             if( i_read <= 6 )
395             {
396                 block_Release( p_block );
397                 tk.i_current_subtitle++;
398                 continue;
399             }
400             p_block->i_buffer = i_read;
401
402             /* pts */
403             p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
404
405             /* demux this block */
406             DemuxVobSub( p_demux, p_block );
407
408             tk.i_current_subtitle++;
409         }
410 #undef tk
411     }
412
413     /* */
414     p_sys->i_next_demux_date = 0;
415
416     return 1;
417 }
418
419 static int TextLoad( text_t *txt, stream_t *s )
420 {
421     char **lines = NULL;
422     size_t n = 0;
423
424     /* load the complete file */
425     for( ;; )
426     {
427         char *psz = stream_ReadLine( s );
428         char **ppsz_new;
429
430         if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
431             break;
432
433         ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
434         if( ppsz_new == NULL )
435         {
436             free( psz );
437             break;
438         }
439         lines = ppsz_new;
440         lines[n++] = psz;
441     }
442
443     txt->i_line_count = n;
444     txt->i_line       = 0;
445     txt->line         = lines;
446
447     return VLC_SUCCESS;
448 }
449
450 static void TextUnload( text_t *txt )
451 {
452     int i;
453
454     for( i = 0; i < txt->i_line_count; i++ )
455         free( txt->line[i] );
456
457     free( txt->line );
458     txt->i_line       = 0;
459     txt->i_line_count = 0;
460 }
461
462 static char *TextGetLine( text_t *txt )
463 {
464     if( txt->i_line >= txt->i_line_count )
465         return( NULL );
466
467     return txt->line[txt->i_line++];
468 }
469
470 static int ParseVobSubIDX( demux_t *p_demux )
471 {
472     demux_sys_t *p_sys = p_demux->p_sys;
473     text_t      *txt = &p_sys->txt;
474     char        *line;
475     vobsub_track_t *current_tk = NULL;
476
477     for( ;; )
478     {
479         if( ( line = TextGetLine( txt ) ) == NULL )
480         {
481             return( VLC_EGENERIC );
482         }
483
484         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
485         {
486             continue;
487         }
488         else if( !strncmp( "size:", line, 5 ) )
489         {
490             /* Store the original size of the video */
491             if( sscanf( line, "size: %dx%d",
492                         &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
493             {
494                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
495             }
496             else
497             {
498                 msg_Warn( p_demux, "reading original frame size failed" );
499             }
500         }
501         else if( !strncmp( "palette:", line, 8 ) )
502         {
503             int i;
504
505             /* Store the palette of the subs */
506             if( sscanf( line, "palette: %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x",
507                         &p_sys->palette[0], &p_sys->palette[1], &p_sys->palette[2], &p_sys->palette[3],
508                         &p_sys->palette[4], &p_sys->palette[5], &p_sys->palette[6], &p_sys->palette[7],
509                         &p_sys->palette[8], &p_sys->palette[9], &p_sys->palette[10], &p_sys->palette[11],
510                         &p_sys->palette[12], &p_sys->palette[13], &p_sys->palette[14], &p_sys->palette[15] ) == 16 )
511             {
512                 for( i = 0; i < 16; i++ )
513                 {
514                     uint8_t r = 0, g = 0, b = 0;
515                     uint8_t y = 0, u = 0, v = 0;
516                     r = (p_sys->palette[i] >> 16) & 0xff;
517                     g = (p_sys->palette[i] >> 8) & 0xff;
518                     b = (p_sys->palette[i] >> 0) & 0xff;
519                     /* msg_Dbg( p_demux, "palette %d: R=%x, G=%x, B=%x", i, r, g, b ); */
520                     y = (uint8_t) __MIN(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235);
521                     u = (uint8_t) __MIN(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240);
522                     v = (uint8_t) __MIN(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
523                     p_sys->palette[i] = 0;
524                     p_sys->palette[i] |= (y&0xff)<<16;
525                     p_sys->palette[i] |= (u&0xff);
526                     p_sys->palette[i] |= (v&0xff)<<8;
527                     /* msg_Dbg( p_demux, "palette %d: y=%x, u=%x, v=%x", i, y, u, v ); */
528
529                 }
530                 p_sys->b_palette = true;
531                 msg_Dbg( p_demux, "vobsub palette read" );
532             }
533             else
534             {
535                 msg_Warn( p_demux, "reading original palette failed" );
536             }
537         }
538         else if( !strncmp( "id:", line, 3 ) )
539         {
540             char language[20];
541             int i_track_id;
542             es_format_t fmt;
543
544             /* Lets start a new track */
545             if( sscanf( line, "id: %2s, index: %d",
546                         language, &i_track_id ) == 2 )
547             {
548                 p_sys->i_tracks++;
549                 p_sys->track = realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
550
551                 /* Init the track */
552                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
553                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
554                 current_tk->i_current_subtitle = 0;
555                 current_tk->i_subtitles = 0;
556                 current_tk->p_subtitles = malloc( sizeof( subtitle_t ) );;
557                 current_tk->i_track_id = i_track_id;
558                 current_tk->i_delay = (int64_t)0;
559
560                 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SPU );
561                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
562                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
563                 fmt.psz_language = strdup( language );
564                 if( p_sys->b_palette )
565                 {
566                     fmt.subs.spu.palette[0] = 0xBeef;
567                     memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
568                 }
569
570                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
571                 msg_Dbg( p_demux, "new vobsub track detected" );
572             }
573             else
574             {
575                 msg_Warn( p_demux, "reading new track failed" );
576             }
577         }
578         else if( !strncmp( line, "timestamp:", 10 ) )
579         {
580             /*
581              * timestamp: [sign]hh:mm:ss:mss, filepos: loc
582              * loc is the hex location of the spu in the .sub file
583              */
584             int h, m, s, ms, count, loc = 0;
585             int i_sign = 1;
586             int64_t i_start, i_location = 0;
587
588             if( p_sys->i_tracks > 0 &&
589                 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
590                         &h, &count, &m, &s, &ms, &loc ) >= 5  )
591             {
592                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
593                 subtitle_t *current_sub;
594
595                 if( line[count-3] == '-' )
596                 {
597                     i_sign = -1;
598                     h = -h;
599                 }
600                 i_start = (int64_t) ( h * 3600*1000 +
601                             m * 60*1000 +
602                             s * 1000 +
603                             ms ) * 1000;
604                 i_location = loc;
605
606                 current_tk->i_subtitles++;
607                 current_tk->p_subtitles = realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
608                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
609
610                 current_sub->i_start = i_start * i_sign;
611                 current_sub->i_start += current_tk->i_delay;
612                 current_sub->i_vobsub_location = i_location;
613             }
614             else
615             {
616                 msg_Warn( p_demux, "reading timestamp failed" );
617             }
618         }
619         else if( !strncasecmp( line, "delay:", 6 ) )
620         {
621             /*
622              * delay: [sign]hh:mm:ss:mss
623              */
624             int h, m, s, ms, count = 0;
625             int i_sign = 1;
626             int64_t i_gap = 0;
627
628             if( p_sys->i_tracks > 0 &&
629                 sscanf( line, "%*celay: %d%n:%d:%d:%d",
630                         &h, &count, &m, &s, &ms ) >= 4 )
631             {
632                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
633                 if( line[count-3] == '-' )
634                 {
635                     i_sign = -1;
636                     h = -h;
637                 }
638                 i_gap = (int64_t) ( h * 3600*1000 +
639                             m * 60*1000 +
640                             s * 1000 +
641                             ms ) * 1000;
642
643                 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
644                 msg_Dbg( p_demux, "sign: %+d gap: %+lld global delay: %+lld",
645                          i_sign, (long long)i_gap,
646                          (long long)current_tk->i_delay  );
647             }
648             else
649             {
650                 msg_Warn( p_demux, "reading delay failed" );
651             }
652         }
653     }
654     return( 0 );
655 }
656
657 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
658 {
659     demux_sys_t *p_sys = p_demux->p_sys;
660     uint8_t     *p = p_bk->p_buffer;
661     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
662     int i;
663
664     while( p + 6 < p_end )
665     {
666         int i_size = ps_pkt_size( p, p_end - p );
667         block_t *p_pkt;
668         int      i_id;
669         int      i_spu;
670
671         if( i_size <= 0 )
672             break;
673
674         if( i_size > p_end - p )
675         {
676             msg_Warn( p_demux, "broken PES size" );
677             break;
678         }
679
680         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
681         {
682             msg_Warn( p_demux, "invalid PES" );
683             break;
684         }
685
686         if( p[3] != 0xbd )
687         {
688             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
689             p += i_size;
690             continue;
691         }
692
693         /* Create a block */
694         p_pkt = block_New( p_demux, i_size );
695         memcpy( p_pkt->p_buffer, p, i_size);
696         p += i_size;
697
698         i_id = ps_pkt_id( p_pkt );
699         if( (i_id&0xffe0) != 0xbd20 ||
700             ps_pkt_parse_pes( p_pkt, 1 ) )
701         {
702             block_Release( p_pkt );
703             continue;
704         }
705         i_spu = i_id&0x1f;
706         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
707
708         for( i = 0; i < p_sys->i_tracks; i++ )
709         {
710             vobsub_track_t *p_tk = &p_sys->track[i];
711
712             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
713             p_pkt->i_length = 0;
714
715             if( p_tk->p_es && p_tk->i_track_id == i_spu )
716             {
717                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
718                 p_bk->i_pts = 0;     /*only first packet has a pts */
719                 break;
720             }
721         }
722         if( i >= p_sys->i_tracks )
723         {
724             block_Release( p_pkt );
725         }
726     }
727
728     return VLC_SUCCESS;
729 }
730