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