c52e19f578097228ceb990718d3c9e3bb96f3164
[vlc.git] / 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
476     for( ;; )
477     {
478         if( ( line = TextGetLine( txt ) ) == NULL )
479         {
480             return( VLC_EGENERIC );
481         }
482
483         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
484         {
485             continue;
486         }
487         else if( !strncmp( "size:", line, 5 ) )
488         {
489             /* Store the original size of the video */
490             if( sscanf( line, "size: %dx%d",
491                         &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
492             {
493                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
494             }
495             else
496             {
497                 msg_Warn( p_demux, "reading original frame size failed" );
498             }
499         }
500         else if( !strncmp( "palette:", line, 8 ) )
501         {
502             int i;
503
504             /* Store the palette of the subs */
505             if( sscanf( line, "palette: %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x",
506                         &p_sys->palette[0], &p_sys->palette[1], &p_sys->palette[2], &p_sys->palette[3],
507                         &p_sys->palette[4], &p_sys->palette[5], &p_sys->palette[6], &p_sys->palette[7],
508                         &p_sys->palette[8], &p_sys->palette[9], &p_sys->palette[10], &p_sys->palette[11],
509                         &p_sys->palette[12], &p_sys->palette[13], &p_sys->palette[14], &p_sys->palette[15] ) == 16 )
510             {
511                 for( i = 0; i < 16; i++ )
512                 {
513                     uint8_t r = 0, g = 0, b = 0;
514                     uint8_t y = 0, u = 0, v = 0;
515                     r = (p_sys->palette[i] >> 16) & 0xff;
516                     g = (p_sys->palette[i] >> 8) & 0xff;
517                     b = (p_sys->palette[i] >> 0) & 0xff;
518                     /* msg_Dbg( p_demux, "palette %d: R=%x, G=%x, B=%x", i, r, g, b ); */
519                     y = (uint8_t) __MIN(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235);
520                     u = (uint8_t) __MIN(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240);
521                     v = (uint8_t) __MIN(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
522                     p_sys->palette[i] = 0;
523                     p_sys->palette[i] |= (y&0xff)<<16;
524                     p_sys->palette[i] |= (u&0xff);
525                     p_sys->palette[i] |= (v&0xff)<<8;
526                     /* msg_Dbg( p_demux, "palette %d: y=%x, u=%x, v=%x", i, y, u, v ); */
527
528                 }
529                 p_sys->b_palette = true;
530                 msg_Dbg( p_demux, "vobsub palette read" );
531             }
532             else
533             {
534                 msg_Warn( p_demux, "reading original palette failed" );
535             }
536         }
537         else if( !strncmp( "id:", line, 3 ) )
538         {
539             char language[20];
540             int i_track_id;
541             es_format_t fmt;
542
543             /* Lets start a new track */
544             if( sscanf( line, "id: %2s, index: %d",
545                         language, &i_track_id ) == 2 )
546             {
547                 p_sys->i_tracks++;
548                 p_sys->track = realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
549
550                 /* Init the track */
551                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
552                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
553                 current_tk->i_current_subtitle = 0;
554                 current_tk->i_subtitles = 0;
555                 current_tk->p_subtitles = malloc( sizeof( subtitle_t ) );;
556                 current_tk->i_track_id = i_track_id;
557                 current_tk->i_delay = (int64_t)0;
558
559                 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
560                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
561                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
562                 fmt.psz_language = strdup( language );
563                 if( p_sys->b_palette )
564                 {
565                     fmt.subs.spu.palette[0] = 0xBeef;
566                     memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
567                 }
568
569                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
570                 msg_Dbg( p_demux, "new vobsub track detected" );
571             }
572             else
573             {
574                 msg_Warn( p_demux, "reading new track failed" );
575             }
576         }
577         else if( !strncmp( line, "timestamp:", 10 ) )
578         {
579             /*
580              * timestamp: [sign]hh:mm:ss:mss, filepos: loc
581              * loc is the hex location of the spu in the .sub file
582              */
583             int h, m, s, ms, count, loc = 0;
584             int i_sign = 1;
585             int64_t i_start, i_location = 0;
586
587             if( p_sys->i_tracks > 0 &&
588                 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
589                         &h, &count, &m, &s, &ms, &loc ) >= 5  )
590             {
591                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
592                 subtitle_t *current_sub;
593
594                 if( line[count-3] == '-' )
595                 {
596                     i_sign = -1;
597                     h = -h;
598                 }
599                 i_start = (int64_t) ( h * 3600*1000 +
600                             m * 60*1000 +
601                             s * 1000 +
602                             ms ) * 1000;
603                 i_location = loc;
604
605                 current_tk->i_subtitles++;
606                 current_tk->p_subtitles = realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
607                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
608
609                 current_sub->i_start = i_start * i_sign;
610                 current_sub->i_start += current_tk->i_delay;
611                 current_sub->i_vobsub_location = i_location;
612             }
613             else
614             {
615                 msg_Warn( p_demux, "reading timestamp failed" );
616             }
617         }
618         else if( !strncasecmp( line, "delay:", 6 ) )
619         {
620             /*
621              * delay: [sign]hh:mm:ss:mss
622              */
623             int h, m, s, ms, count = 0;
624             int i_sign = 1;
625             int64_t i_gap = 0;
626
627             if( p_sys->i_tracks > 0 &&
628                 sscanf( line, "%*celay: %d%n:%d:%d:%d",
629                         &h, &count, &m, &s, &ms ) >= 4 )
630             {
631                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
632                 if( line[count-3] == '-' )
633                 {
634                     i_sign = -1;
635                     h = -h;
636                 }
637                 i_gap = (int64_t) ( h * 3600*1000 +
638                             m * 60*1000 +
639                             s * 1000 +
640                             ms ) * 1000;
641
642                 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
643                 msg_Dbg( p_demux, "sign: %+d gap: %+lld global delay: %+lld",
644                          i_sign, (long long)i_gap,
645                          (long long)current_tk->i_delay  );
646             }
647             else
648             {
649                 msg_Warn( p_demux, "reading delay failed" );
650             }
651         }
652     }
653     return( 0 );
654 }
655
656 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
657 {
658     demux_sys_t *p_sys = p_demux->p_sys;
659     uint8_t     *p = p_bk->p_buffer;
660     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
661     int i;
662
663     while( p + 6 < p_end )
664     {
665         int i_size = ps_pkt_size( p, p_end - p );
666         block_t *p_pkt;
667         int      i_id;
668         int      i_spu;
669
670         if( i_size <= 0 )
671             break;
672
673         if( i_size > p_end - p )
674         {
675             msg_Warn( p_demux, "broken PES size" );
676             break;
677         }
678
679         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
680         {
681             msg_Warn( p_demux, "invalid PES" );
682             break;
683         }
684
685         if( p[3] != 0xbd )
686         {
687             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
688             p += i_size;
689             continue;
690         }
691
692         /* Create a block */
693         p_pkt = block_New( p_demux, i_size );
694         memcpy( p_pkt->p_buffer, p, i_size);
695         p += i_size;
696
697         i_id = ps_pkt_id( p_pkt );
698         if( (i_id&0xffe0) != 0xbd20 ||
699             ps_pkt_parse_pes( p_pkt, 1 ) )
700         {
701             block_Release( p_pkt );
702             continue;
703         }
704         i_spu = i_id&0x1f;
705         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
706
707         for( i = 0; i < p_sys->i_tracks; i++ )
708         {
709             vobsub_track_t *p_tk = &p_sys->track[i];
710
711             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
712             p_pkt->i_length = 0;
713
714             if( p_tk->p_es && p_tk->i_track_id == i_spu )
715             {
716                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
717                 p_bk->i_pts = 0;     /*only first packet has a pts */
718                 break;
719             }
720         }
721         if( i >= p_sys->i_tracks )
722         {
723             block_Release( p_pkt );
724         }
725     }
726
727     return VLC_SUCCESS;
728 }
729