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