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